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
11 * This file is part of Libav.
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.
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.
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
29 #include "rtpdec_formats.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavcodec/get_bits.h"
33 struct PayloadContext {
40 static PayloadContext *h263_new_context(void)
42 return av_mallocz(sizeof(PayloadContext));
45 static void h263_free_context(PayloadContext *data)
51 avio_close_dyn_buf(data->buf, &p);
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)
61 int f, p, i, sbit, ebit; /* Corresponding to header fields in the RFC */
64 if (data->buf && data->timestamp != *timestamp) {
65 /* Dropping old buffered, unfinished data */
67 avio_close_dyn_buf(data->buf, &p);
73 av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet: %d\n", len);
74 return AVERROR_INVALIDDATA;
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",
90 return AVERROR_INVALIDDATA;
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",
100 return AVERROR_INVALIDDATA;
104 sbit = (buf[0] >> 3) & 0x7;
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);
117 data->timestamp = *timestamp;
119 /* Frame not started yet, skipping */
120 return AVERROR(EAGAIN);
124 if (data->endbyte_bits || sbit) {
125 if (data->endbyte_bits == sbit) {
126 data->endbyte |= buf[0] & (0xff >> sbit);
127 data->endbyte_bits = 0;
130 avio_w8(data->buf, data->endbyte);
132 /* Start/end skip bits not matching - missed packets? */
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);
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);
152 avio_write(data->buf, buf, len - 1);
153 data->endbyte_bits = 8 - ebit;
154 data->endbyte = buf[len - 1] & (0xff << ebit);
156 avio_write(data->buf, buf, len);
159 if (!(flags & RTP_FLAG_MARKER))
160 return AVERROR(EAGAIN);
162 if (data->endbyte_bits)
163 avio_w8(data->buf, data->endbyte);
164 data->endbyte_bits = 0;
167 pkt->size = avio_close_dyn_buf(data->buf, &pkt->data);
168 pkt->destruct = av_destruct_packet;
169 pkt->stream_index = st->index;
171 pkt->flags |= AV_PKT_FLAG_KEY;
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,