OSDN Git Service

e45778108b1be549749b85e130ce3b2a1651c2ff
[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 Libav.
12  *
13  * Libav 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  * Libav 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 Libav; 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->buf) {
45         uint8_t *p;
46         avio_close_dyn_buf(data->buf, &p);
47         av_free(p);
48     }
49 }
50
51 static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
52                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
53                               const uint8_t *buf, int len, uint16_t seq,
54                               int flags)
55 {
56     /* Corresponding to header fields in the RFC */
57     int f, p, i, sbit, ebit, src, r;
58     int header_size, ret;
59
60     if (data->newformat)
61         return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf, len,
62                                      seq, flags);
63
64     if (data->buf && data->timestamp != *timestamp) {
65         /* Dropping old buffered, unfinished data */
66         uint8_t *p;
67         avio_close_dyn_buf(data->buf, &p);
68         av_free(p);
69         data->buf = NULL;
70         data->endbyte_bits = 0;
71     }
72
73     if (len < 4) {
74         av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet: %d\n", len);
75         return AVERROR_INVALIDDATA;
76     }
77
78     f = buf[0] & 0x80;
79     p = buf[0] & 0x40;
80     if (!f) {
81         /* Mode A */
82         header_size = 4;
83         i = buf[1] & 0x10;
84         r = ((buf[1] & 0x01) << 3) | ((buf[2] & 0xe0) >> 5);
85     } else if (!p) {
86         /* Mode B */
87         header_size = 8;
88         if (len < header_size) {
89             av_log(ctx, AV_LOG_ERROR,
90                    "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
91                    len, header_size);
92             return AVERROR_INVALIDDATA;
93         }
94         r = buf[3] & 0x03;
95         i = buf[4] & 0x80;
96     } else {
97         /* Mode C */
98         header_size = 12;
99         if (len < header_size) {
100             av_log(ctx, AV_LOG_ERROR,
101                    "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
102                    len, header_size);
103             return AVERROR_INVALIDDATA;
104         }
105         r = buf[3] & 0x03;
106         i = buf[4] & 0x80;
107     }
108     sbit = (buf[0] >> 3) & 0x7;
109     ebit =  buf[0]       & 0x7;
110     src  = (buf[1] & 0xe0) >> 5;
111     if (!(buf[0] & 0xf8)) { /* Reserved bits in RFC 2429/4629 are zero */
112         if ((src == 0 || src >= 6) && r) {
113             /* Invalid src for this format, and bits that should be zero
114              * according to RFC 2190 aren't zero. */
115             av_log(ctx, AV_LOG_WARNING,
116                    "Interpreting H263 RTP data as RFC 2429/4629 even though "
117                    "signalled with a static payload type.\n");
118             data->newformat = 1;
119             return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf,
120                                          len, seq, flags);
121         }
122     }
123
124     buf += header_size;
125     len -= header_size;
126
127     if (!data->buf) {
128         /* Check the picture start code, only start buffering a new frame
129          * if this is correct */
130         if (len > 4 && AV_RB32(buf) >> 10 == 0x20) {
131             ret = avio_open_dyn_buf(&data->buf);
132             if (ret < 0)
133                 return ret;
134             data->timestamp = *timestamp;
135         } else {
136             /* Frame not started yet, skipping */
137             return AVERROR(EAGAIN);
138         }
139     }
140
141     if (data->endbyte_bits || sbit) {
142         if (data->endbyte_bits == sbit) {
143             data->endbyte |= buf[0] & (0xff >> sbit);
144             data->endbyte_bits = 0;
145             buf++;
146             len--;
147             avio_w8(data->buf, data->endbyte);
148         } else {
149             /* Start/end skip bits not matching - missed packets? */
150             GetBitContext gb;
151             init_get_bits(&gb, buf, len*8 - ebit);
152             skip_bits(&gb, sbit);
153             if (data->endbyte_bits) {
154                 data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);
155                 avio_w8(data->buf, data->endbyte);
156             }
157             while (get_bits_left(&gb) >= 8)
158                 avio_w8(data->buf, get_bits(&gb, 8));
159             data->endbyte_bits = get_bits_left(&gb);
160             if (data->endbyte_bits)
161                 data->endbyte = get_bits(&gb, data->endbyte_bits) <<
162                                 (8 - data->endbyte_bits);
163             ebit = 0;
164             len = 0;
165         }
166     }
167     if (ebit) {
168         if (len > 0)
169             avio_write(data->buf, buf, len - 1);
170         data->endbyte_bits = 8 - ebit;
171         data->endbyte = buf[len - 1] & (0xff << ebit);
172     } else {
173         avio_write(data->buf, buf, len);
174     }
175
176     if (!(flags & RTP_FLAG_MARKER))
177         return AVERROR(EAGAIN);
178
179     if (data->endbyte_bits)
180         avio_w8(data->buf, data->endbyte);
181     data->endbyte_bits = 0;
182
183     ret = ff_rtp_finalize_packet(pkt, &data->buf, st->index);
184     if (ret < 0)
185         return ret;
186     if (!i)
187         pkt->flags   |= AV_PKT_FLAG_KEY;
188
189     return 0;
190 }
191
192 RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler = {
193     .codec_type        = AVMEDIA_TYPE_VIDEO,
194     .codec_id          = AV_CODEC_ID_H263,
195     .need_parsing      = AVSTREAM_PARSE_FULL,
196     .parse_packet      = h263_handle_packet,
197     .priv_data_size    = sizeof(PayloadContext),
198     .free              = h263_free_context,
199     .static_payload_id = 34,
200 };