3 * Copyright (c) 2010 Josh Allmann
4 * Copyright (c) 2012 Martin Storsjo
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @brief RTP support for the VP8 payload
26 * @author Josh Allmann <joshua.allmann@gmail.com>
27 * @see http://tools.ietf.org/html/draft-ietf-payload-vp8-05
30 #include "libavcodec/bytestream.h"
32 #include "rtpdec_formats.h"
34 struct PayloadContext {
39 static void prepare_packet(AVPacket *pkt, PayloadContext *vp8, int stream)
42 pkt->stream_index = stream;
43 pkt->size = avio_close_dyn_buf(vp8->data, &pkt->data);
44 pkt->destruct = av_destruct_packet;
48 static int vp8_handle_packet(AVFormatContext *ctx,
56 int start_partition, end_packet;
57 int extended_bits, non_ref, part_id;
58 int pictureid_present = 0, tl0picidx_present = 0, tid_present = 0,
60 int pictureid = -1, keyidx = -1;
63 return AVERROR_INVALIDDATA;
65 extended_bits = buf[0] & 0x80;
66 non_ref = buf[0] & 0x20;
67 start_partition = buf[0] & 0x10;
68 part_id = buf[0] & 0x0f;
69 end_packet = flags & RTP_FLAG_MARKER;
74 return AVERROR_INVALIDDATA;
75 pictureid_present = buf[0] & 0x80;
76 tl0picidx_present = buf[0] & 0x40;
77 tid_present = buf[0] & 0x20;
78 keyidx_present = buf[0] & 0x10;
82 if (pictureid_present) {
84 return AVERROR_INVALIDDATA;
87 return AVERROR_INVALIDDATA;
88 pictureid = AV_RB16(buf) & 0x7fff;
92 pictureid = buf[0] & 0x7f;
97 if (tl0picidx_present) {
98 // Ignoring temporal level zero index
102 if (tid_present || keyidx_present) {
103 // Ignoring temporal layer index and layer sync bit
105 return AVERROR_INVALIDDATA;
107 keyidx = buf[0] & 0x1f;
112 return AVERROR_INVALIDDATA;
114 if (start_partition && part_id == 0) {
118 avio_close_dyn_buf(vp8->data, &tmp);
122 if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
124 vp8->timestamp = *timestamp;
127 if (!vp8->data || vp8->timestamp != *timestamp) {
128 av_log(ctx, AV_LOG_WARNING,
129 "Received no start marker; dropping frame\n");
130 return AVERROR(EAGAIN);
133 avio_write(vp8->data, buf, len);
136 prepare_packet(pkt, vp8, st->index);
140 return AVERROR(EAGAIN);
143 static PayloadContext *vp8_new_context(void)
145 return av_mallocz(sizeof(PayloadContext));
148 static void vp8_free_context(PayloadContext *vp8)
152 avio_close_dyn_buf(vp8->data, &tmp);
158 RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
160 .codec_type = AVMEDIA_TYPE_VIDEO,
161 .codec_id = AV_CODEC_ID_VP8,
162 .alloc = vp8_new_context,
163 .free = vp8_free_context,
164 .parse_packet = vp8_handle_packet,