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