OSDN Git Service

Merge commit '5d8cae45737bed6239bd6b6e0698802dbe1463c8'
[android-x86/external-ffmpeg.git] / libavformat / rtpdec_h263_rfc2190.c
1 /*
2  * RTP H.263 Depacketizer, RFC 2190
3  * Copyright (c) 2012 Martin Storsjo
4  * Based on the GStreamer H.263 Depayloder:
5  * Copyright 2005 Wim Taymans
6  * Copyright 2007 Edward Hervey
7  * Copyright 2007 Nokia Corporation
8  * Copyright 2007 Collabora Ltd, Philippe Kalaf
9  * Copyright 2010 Mark Nauwelaerts
10  *
11  * This file is part of FFmpeg.
12  *
13  * FFmpeg is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * FFmpeg is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with FFmpeg; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27
28 #include "avformat.h"
29 #include "rtpdec_formats.h"
30 #include "libavutil/attributes.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavcodec/get_bits.h"
33
34 struct PayloadContext {
35     AVIOContext *buf;
36     uint8_t      endbyte;
37     int          endbyte_bits;
38     uint32_t     timestamp;
39     int          newformat;
40 };
41
42 static void h263_free_context(PayloadContext *data)
43 {
44     if (!data)
45         return;
46     if (data->buf) {
47         uint8_t *p;
48         avio_close_dyn_buf(data->buf, &p);
49         av_free(p);
50     }
51     av_free(data);
52 }
53
54 static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
55                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
56                               const uint8_t *buf, int len, uint16_t seq,
57                               int flags)
58 {
59     /* Corresponding to header fields in the RFC */
60     int f, p, i, sbit, ebit, src, r;
61     int header_size, ret;
62
63     if (data->newformat)
64         return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf, len,
65                                      seq, flags);
66
67     if (data->buf && data->timestamp != *timestamp) {
68         /* Dropping old buffered, unfinished data */
69         uint8_t *p;
70         avio_close_dyn_buf(data->buf, &p);
71         av_free(p);
72         data->buf = NULL;
73         data->endbyte_bits = 0;
74     }
75
76     if (len < 4) {
77         av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet: %d\n", len);
78         return AVERROR_INVALIDDATA;
79     }
80
81     f = buf[0] & 0x80;
82     p = buf[0] & 0x40;
83     if (!f) {
84         /* Mode A */
85         header_size = 4;
86         i = buf[1] & 0x10;
87         r = ((buf[1] & 0x01) << 3) | ((buf[2] & 0xe0) >> 5);
88     } else if (!p) {
89         /* Mode B */
90         header_size = 8;
91         if (len < header_size) {
92             av_log(ctx, AV_LOG_ERROR,
93                    "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
94                    len, header_size);
95             return AVERROR_INVALIDDATA;
96         }
97         r = buf[3] & 0x03;
98         i = buf[4] & 0x80;
99     } else {
100         /* Mode C */
101         header_size = 12;
102         if (len < header_size) {
103             av_log(ctx, AV_LOG_ERROR,
104                    "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
105                    len, header_size);
106             return AVERROR_INVALIDDATA;
107         }
108         r = buf[3] & 0x03;
109         i = buf[4] & 0x80;
110     }
111     sbit = (buf[0] >> 3) & 0x7;
112     ebit =  buf[0]       & 0x7;
113     src  = (buf[1] & 0xe0) >> 5;
114     if (!(buf[0] & 0xf8)) { /* Reserved bits in RFC 2429/4629 are zero */
115         if ((src == 0 || src >= 6) && r) {
116             /* Invalid src for this format, and bits that should be zero
117              * according to RFC 2190 aren't zero. */
118             av_log(ctx, AV_LOG_WARNING,
119                    "Interpreting H263 RTP data as RFC 2429/4629 even though "
120                    "signalled with a static payload type.\n");
121             data->newformat = 1;
122             return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf,
123                                          len, seq, flags);
124         }
125     }
126
127     buf += header_size;
128     len -= header_size;
129
130     if (!data->buf) {
131         /* Check the picture start code, only start buffering a new frame
132          * if this is correct */
133         if (len > 4 && AV_RB32(buf) >> 10 == 0x20) {
134             ret = avio_open_dyn_buf(&data->buf);
135             if (ret < 0)
136                 return ret;
137             data->timestamp = *timestamp;
138         } else {
139             /* Frame not started yet, skipping */
140             return AVERROR(EAGAIN);
141         }
142     }
143
144     if (data->endbyte_bits || sbit) {
145         if (data->endbyte_bits == sbit) {
146             data->endbyte |= buf[0] & (0xff >> sbit);
147             data->endbyte_bits = 0;
148             buf++;
149             len--;
150             avio_w8(data->buf, data->endbyte);
151         } else {
152             /* Start/end skip bits not matching - missed packets? */
153             GetBitContext gb;
154             init_get_bits(&gb, buf, len*8 - ebit);
155             skip_bits(&gb, sbit);
156             if (data->endbyte_bits) {
157                 data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);
158                 avio_w8(data->buf, data->endbyte);
159             }
160             while (get_bits_left(&gb) >= 8)
161                 avio_w8(data->buf, get_bits(&gb, 8));
162             data->endbyte_bits = get_bits_left(&gb);
163             if (data->endbyte_bits)
164                 data->endbyte = get_bits(&gb, data->endbyte_bits) <<
165                                 (8 - data->endbyte_bits);
166             ebit = 0;
167             len = 0;
168         }
169     }
170     if (ebit) {
171         if (len > 0)
172             avio_write(data->buf, buf, len - 1);
173         data->endbyte_bits = 8 - ebit;
174         data->endbyte = buf[len - 1] & (0xff << ebit);
175     } else {
176         avio_write(data->buf, buf, len);
177     }
178
179     if (!(flags & RTP_FLAG_MARKER))
180         return AVERROR(EAGAIN);
181
182     if (data->endbyte_bits)
183         avio_w8(data->buf, data->endbyte);
184     data->endbyte_bits = 0;
185
186     ret = ff_rtp_finalize_packet(pkt, &data->buf, st->index);
187     if (ret < 0)
188         return ret;
189     if (!i)
190         pkt->flags   |= AV_PKT_FLAG_KEY;
191
192     return 0;
193 }
194
195 RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler = {
196     .codec_type        = AVMEDIA_TYPE_VIDEO,
197     .codec_id          = AV_CODEC_ID_H263,
198     .need_parsing      = AVSTREAM_PARSE_FULL,
199     .parse_packet      = h263_handle_packet,
200     .priv_data_size    = sizeof(PayloadContext),
201     .free              = h263_free_context,
202     .static_payload_id = 34,
203 };