3 * Copyright (c) 2010 Josh Allmann
4 * Copyright (c) 2012 Martin Storsjo
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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 {
45 static void vp8_free_buffer(PayloadContext *vp8)
50 avio_close_dyn_buf(vp8->data, &tmp);
55 static int vp8_broken_sequence(AVFormatContext *ctx, PayloadContext *vp8,
59 av_log(ctx, AV_LOG_WARNING, "%s", msg);
61 return AVERROR(EAGAIN);
64 static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8,
65 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
66 const uint8_t *buf, int len, uint16_t seq,
69 int start_partition, end_packet;
70 int extended_bits, part_id;
71 int pictureid_present = 0, tl0picidx_present = 0, tid_present = 0,
73 int pictureid = -1, pictureid_mask = 0;
74 int returned_old_frame = 0;
75 uint32_t old_timestamp;
79 int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
82 *timestamp = vp8->timestamp;
85 return AVERROR(EAGAIN);
89 return AVERROR_INVALIDDATA;
91 extended_bits = buf[0] & 0x80;
92 start_partition = buf[0] & 0x10;
93 part_id = buf[0] & 0x0f;
94 end_packet = flags & RTP_FLAG_MARKER;
99 return AVERROR_INVALIDDATA;
100 pictureid_present = buf[0] & 0x80;
101 tl0picidx_present = buf[0] & 0x40;
102 tid_present = buf[0] & 0x20;
103 keyidx_present = buf[0] & 0x10;
107 if (pictureid_present) {
109 return AVERROR_INVALIDDATA;
112 return AVERROR_INVALIDDATA;
113 pictureid = AV_RB16(buf) & 0x7fff;
114 pictureid_mask = 0x7fff;
118 pictureid = buf[0] & 0x7f;
119 pictureid_mask = 0x7f;
124 if (tl0picidx_present) {
125 // Ignoring temporal level zero index
129 if (tid_present || keyidx_present) {
130 // Ignoring temporal layer index, layer sync bit and keyframe index
135 return AVERROR_INVALIDDATA;
137 if (start_partition && part_id == 0 && len >= 3) {
139 int non_key = buf[0] & 0x01;
141 vp8_free_buffer(vp8);
142 // Keyframe, decoding ok again
143 vp8->sequence_ok = 1;
145 int can_continue = vp8->data && !vp8->is_keyframe &&
146 avio_tell(vp8->data) >= vp8->first_part_size;
147 if (!vp8->sequence_ok)
148 return AVERROR(EAGAIN);
149 if (pictureid >= 0) {
150 if (pictureid != ((vp8->prev_pictureid + 1) & pictureid_mask)) {
151 return vp8_broken_sequence(ctx, vp8,
152 "Missed a picture, sequence broken\n");
154 if (vp8->data && !can_continue)
155 return vp8_broken_sequence(ctx, vp8,
156 "Missed a picture, sequence broken\n");
159 uint16_t expected_seq = vp8->prev_seq + 1;
160 int16_t diff = seq - expected_seq;
162 // No picture id, so we can't know if missed packets
163 // contained any new frames. If diff == 0, we did get
164 // later packets from the same frame (matching timestamp),
165 // so we know we didn't miss any frame. If diff == 1 and
166 // we still have data (not flushed by the end of frame
167 // marker), the single missed packet must have been part
168 // of the same frame.
169 if ((diff == 0 || diff == 1) && can_continue) {
170 // Proceed with what we have
172 return vp8_broken_sequence(ctx, vp8,
173 "Missed too much, sequence broken\n");
177 return vp8_broken_sequence(ctx, vp8,
178 "Missed unknown data, sequence broken\n");
182 if (avio_tell(vp8->data) >= vp8->first_part_size) {
183 int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
186 pkt->size = vp8->first_part_size;
187 returned_old_frame = 1;
188 old_timestamp = vp8->timestamp;
191 vp8_free_buffer(vp8);
195 vp8->first_part_size = (AV_RL16(&buf[1]) << 3 | buf[0] >> 5) + 3;
196 if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
198 vp8->timestamp = *timestamp;
199 vp8->broken_frame = 0;
200 vp8->prev_pictureid = pictureid;
201 vp8->is_keyframe = !non_key;
203 uint16_t expected_seq = vp8->prev_seq + 1;
205 if (!vp8->sequence_ok)
206 return AVERROR(EAGAIN);
208 if (vp8->timestamp != *timestamp) {
209 // Missed the start of the new frame, sequence broken
210 vp8->sequence_ok = 0;
211 av_log(ctx, AV_LOG_WARNING,
212 "Received no start marker; dropping frame\n");
213 vp8_free_buffer(vp8);
214 return AVERROR(EAGAIN);
217 if (seq != expected_seq) {
218 if (vp8->is_keyframe) {
219 return vp8_broken_sequence(ctx, vp8,
220 "Missed part of a keyframe, sequence broken\n");
221 } else if (vp8->data && avio_tell(vp8->data) >= vp8->first_part_size) {
222 vp8->broken_frame = 1;
224 return vp8_broken_sequence(ctx, vp8,
225 "Missed part of the first partition, sequence broken\n");
231 return vp8_broken_sequence(ctx, vp8, "Received no start marker\n");
234 avio_write(vp8->data, buf, len);
236 if (returned_old_frame) {
237 *timestamp = old_timestamp;
238 return end_packet ? 1 : 0;
243 ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
246 if (vp8->broken_frame)
247 pkt->size = vp8->first_part_size;
251 return AVERROR(EAGAIN);
254 static PayloadContext *vp8_new_context(void)
256 return av_mallocz(sizeof(PayloadContext));
259 static void vp8_free_context(PayloadContext *vp8)
261 vp8_free_buffer(vp8);
265 RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
267 .codec_type = AVMEDIA_TYPE_VIDEO,
268 .codec_id = AV_CODEC_ID_VP8,
269 .alloc = vp8_new_context,
270 .free = vp8_free_context,
271 .parse_packet = vp8_handle_packet,