]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpdec_h263_rfc2190.c
rtpdec: Support H263 in RFC 2190 format
[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 };
39
40 static PayloadContext *h263_new_context(void)
41 {
42     return av_mallocz(sizeof(PayloadContext));
43 }
44
45 static void h263_free_context(PayloadContext *data)
46 {
47     if (!data)
48         return;
49     if (data->buf) {
50         uint8_t *p;
51         avio_close_dyn_buf(data->buf, &p);
52         av_free(p);
53     }
54     av_free(data);
55 }
56
57 static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
58                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
59                               const uint8_t *buf, int len, int flags)
60 {
61     int f, p, i, sbit, ebit; /* Corresponding to header fields in the RFC */
62     int header_size;
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     }
71
72     if (len < 4) {
73         av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet: %d\n", len);
74         return AVERROR_INVALIDDATA;
75     }
76
77     f = buf[0] & 0x80;
78     p = buf[0] & 0x40;
79     if (!f) {
80         /* Mode A */
81         header_size = 4;
82         i = buf[1] & 0x10;
83     } else if (!p) {
84         /* Mode B */
85         header_size = 8;
86         if (len < header_size) {
87             av_log(ctx, AV_LOG_ERROR,
88                    "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
89                    len, header_size);
90             return AVERROR_INVALIDDATA;
91         }
92         i = buf[4] & 0x80;
93     } else {
94         /* Mode C */
95         header_size = 12;
96         if (len < header_size) {
97             av_log(ctx, AV_LOG_ERROR,
98                    "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
99                    len, header_size);
100             return AVERROR_INVALIDDATA;
101         }
102         i = buf[4] & 0x80;
103     }
104     sbit = (buf[0] >> 3) & 0x7;
105     ebit =  buf[0]       & 0x7;
106
107     buf += header_size;
108     len -= header_size;
109
110     if (!data->buf) {
111         /* Check the picture start code, only start buffering a new frame
112          * if this is correct */
113         if (!f && len > 4 && AV_RB32(buf) >> 10 == 0x20) {
114             int ret = avio_open_dyn_buf(&data->buf);
115             if (ret < 0)
116                 return ret;
117             data->timestamp = *timestamp;
118         } else {
119             /* Frame not started yet, skipping */
120             return AVERROR(EAGAIN);
121         }
122     }
123
124     if (data->endbyte_bits || sbit) {
125         if (data->endbyte_bits == sbit) {
126             data->endbyte |= buf[0] & (0xff >> sbit);
127             data->endbyte_bits = 0;
128             buf++;
129             len--;
130             avio_w8(data->buf, data->endbyte);
131         } else {
132             /* Start/end skip bits not matching - missed packets? */
133             GetBitContext gb;
134             init_get_bits(&gb, buf, len*8 - ebit);
135             skip_bits(&gb, sbit);
136             if (data->endbyte_bits) {
137                 data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);
138                 avio_w8(data->buf, data->endbyte);
139             }
140             while (get_bits_left(&gb) >= 8)
141                 avio_w8(data->buf, get_bits(&gb, 8));
142             data->endbyte_bits = get_bits_left(&gb);
143             if (data->endbyte_bits)
144                 data->endbyte = get_bits(&gb, data->endbyte_bits) <<
145                                 (8 - data->endbyte_bits);
146             ebit = 0;
147             len = 0;
148         }
149     }
150     if (ebit) {
151         if (len > 0)
152             avio_write(data->buf, buf, len - 1);
153         data->endbyte_bits = 8 - ebit;
154         data->endbyte = buf[len - 1] & (0xff << ebit);
155     } else {
156         avio_write(data->buf, buf, len);
157     }
158
159     if (!(flags & RTP_FLAG_MARKER))
160         return AVERROR(EAGAIN);
161
162     if (data->endbyte_bits)
163         avio_w8(data->buf, data->endbyte);
164     data->endbyte_bits = 0;
165
166     av_init_packet(pkt);
167     pkt->size         = avio_close_dyn_buf(data->buf, &pkt->data);
168     pkt->destruct     = av_destruct_packet;
169     pkt->stream_index = st->index;
170     if (!i)
171         pkt->flags   |= AV_PKT_FLAG_KEY;
172     data->buf = NULL;
173
174     return 0;
175 }
176
177 RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler = {
178     .codec_type        = AVMEDIA_TYPE_VIDEO,
179     .codec_id          = CODEC_ID_H263,
180     .parse_packet      = h263_handle_packet,
181     .alloc             = h263_new_context,
182     .free              = h263_free_context,
183     .static_payload_id = 34,
184 };