yading@11
|
1 /*
|
yading@11
|
2 * RTP H.263 Depacketizer, RFC 4629
|
yading@11
|
3 * Copyright (c) 2010 Martin Storsjo
|
yading@11
|
4 *
|
yading@11
|
5 * This file is part of FFmpeg.
|
yading@11
|
6 *
|
yading@11
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
9 * License as published by the Free Software Foundation; either
|
yading@11
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
11 *
|
yading@11
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
15 * Lesser General Public License for more details.
|
yading@11
|
16 *
|
yading@11
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
20 */
|
yading@11
|
21
|
yading@11
|
22 #include "avformat.h"
|
yading@11
|
23 #include "rtpdec_formats.h"
|
yading@11
|
24 #include "libavutil/intreadwrite.h"
|
yading@11
|
25
|
yading@11
|
26 static int h263_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
|
yading@11
|
27 {
|
yading@11
|
28 if (st_index < 0)
|
yading@11
|
29 return 0;
|
yading@11
|
30 ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
|
yading@11
|
31 return 0;
|
yading@11
|
32 }
|
yading@11
|
33
|
yading@11
|
34 int ff_h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
|
yading@11
|
35 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
|
yading@11
|
36 const uint8_t *buf, int len, uint16_t seq, int flags)
|
yading@11
|
37 {
|
yading@11
|
38 uint8_t *ptr;
|
yading@11
|
39 uint16_t header;
|
yading@11
|
40 int startcode, vrc, picture_header;
|
yading@11
|
41
|
yading@11
|
42 if (len < 2) {
|
yading@11
|
43 av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet\n");
|
yading@11
|
44 return AVERROR_INVALIDDATA;
|
yading@11
|
45 }
|
yading@11
|
46
|
yading@11
|
47 /* Decode the 16 bit H.263+ payload header, as described in section
|
yading@11
|
48 * 5.1 of RFC 4629. The fields of this header are:
|
yading@11
|
49 * - 5 reserved bits, should be ignored.
|
yading@11
|
50 * - One bit (P, startcode), indicating a picture start, picture segment
|
yading@11
|
51 * start or video sequence end. If set, two zero bytes should be
|
yading@11
|
52 * prepended to the payload.
|
yading@11
|
53 * - One bit (V, vrc), indicating the presence of an 8 bit Video
|
yading@11
|
54 * Redundancy Coding field after this 16 bit header.
|
yading@11
|
55 * - 6 bits (PLEN, picture_header), the length (in bytes) of an extra
|
yading@11
|
56 * picture header, following the VRC field.
|
yading@11
|
57 * - 3 bits (PEBIT), the number of bits to ignore of the last byte
|
yading@11
|
58 * of the extra picture header. (Not used at the moment.)
|
yading@11
|
59 */
|
yading@11
|
60 header = AV_RB16(buf);
|
yading@11
|
61 startcode = (header & 0x0400) >> 9;
|
yading@11
|
62 vrc = header & 0x0200;
|
yading@11
|
63 picture_header = (header & 0x01f8) >> 3;
|
yading@11
|
64 buf += 2;
|
yading@11
|
65 len -= 2;
|
yading@11
|
66
|
yading@11
|
67 if (vrc) {
|
yading@11
|
68 /* Skip VRC header if present, not used at the moment. */
|
yading@11
|
69 buf += 1;
|
yading@11
|
70 len -= 1;
|
yading@11
|
71 }
|
yading@11
|
72 if (picture_header) {
|
yading@11
|
73 /* Skip extra picture header if present, not used at the moment. */
|
yading@11
|
74 buf += picture_header;
|
yading@11
|
75 len -= picture_header;
|
yading@11
|
76 }
|
yading@11
|
77
|
yading@11
|
78 if (len < 0) {
|
yading@11
|
79 av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet\n");
|
yading@11
|
80 return AVERROR_INVALIDDATA;
|
yading@11
|
81 }
|
yading@11
|
82
|
yading@11
|
83 if (av_new_packet(pkt, len + startcode)) {
|
yading@11
|
84 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
|
yading@11
|
85 return AVERROR(ENOMEM);
|
yading@11
|
86 }
|
yading@11
|
87 pkt->stream_index = st->index;
|
yading@11
|
88 ptr = pkt->data;
|
yading@11
|
89
|
yading@11
|
90 if (startcode) {
|
yading@11
|
91 *ptr++ = 0;
|
yading@11
|
92 *ptr++ = 0;
|
yading@11
|
93 }
|
yading@11
|
94 memcpy(ptr, buf, len);
|
yading@11
|
95
|
yading@11
|
96 return 0;
|
yading@11
|
97 }
|
yading@11
|
98
|
yading@11
|
99 RTPDynamicProtocolHandler ff_h263_1998_dynamic_handler = {
|
yading@11
|
100 .enc_name = "H263-1998",
|
yading@11
|
101 .codec_type = AVMEDIA_TYPE_VIDEO,
|
yading@11
|
102 .codec_id = AV_CODEC_ID_H263,
|
yading@11
|
103 .init = h263_init,
|
yading@11
|
104 .parse_packet = ff_h263_handle_packet,
|
yading@11
|
105 };
|
yading@11
|
106
|
yading@11
|
107 RTPDynamicProtocolHandler ff_h263_2000_dynamic_handler = {
|
yading@11
|
108 .enc_name = "H263-2000",
|
yading@11
|
109 .codec_type = AVMEDIA_TYPE_VIDEO,
|
yading@11
|
110 .codec_id = AV_CODEC_ID_H263,
|
yading@11
|
111 .init = h263_init,
|
yading@11
|
112 .parse_packet = ff_h263_handle_packet,
|
yading@11
|
113 };
|