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