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 {
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;
74 int returned_old_frame = 0;
75 uint32_t old_timestamp;
79 int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
84 return AVERROR(EAGAIN);
88 return AVERROR_INVALIDDATA;
90 extended_bits = buf[0] & 0x80;
91 start_partition = buf[0] & 0x10;
92 part_id = buf[0] & 0x0f;
93 end_packet = flags & RTP_FLAG_MARKER;
98 return AVERROR_INVALIDDATA;
99 pictureid_present = buf[0] & 0x80;
100 tl0picidx_present = buf[0] & 0x40;
101 tid_present = buf[0] & 0x20;
102 keyidx_present = buf[0] & 0x10;
106 if (pictureid_present) {
108 return AVERROR_INVALIDDATA;
111 return AVERROR_INVALIDDATA;
112 pictureid = AV_RB16(buf) & 0x7fff;
113 pictureid_mask = 0x7fff;
117 pictureid = buf[0] & 0x7f;
118 pictureid_mask = 0x7f;
123 if (tl0picidx_present) {
124 // Ignoring temporal level zero index
128 if (tid_present || keyidx_present) {
129 // Ignoring temporal layer index, layer sync bit and keyframe index
134 return AVERROR_INVALIDDATA;
136 if (start_partition && part_id == 0 && len >= 3) {
138 int non_key = buf[0] & 0x01;
140 vp8_free_buffer(vp8);
141 // Keyframe, decoding ok again
142 vp8->sequence_ok = 1;
144 int can_continue = vp8->data && !vp8->is_keyframe &&
145 avio_tell(vp8->data) >= vp8->first_part_size;
146 if (!vp8->sequence_ok)
147 return AVERROR(EAGAIN);
148 if (pictureid >= 0) {
149 if (pictureid != ((vp8->prev_pictureid + 1) & pictureid_mask)) {
150 return vp8_broken_sequence(ctx, vp8,
151 "Missed a picture, sequence broken\n");
153 if (vp8->data && !can_continue)
154 return vp8_broken_sequence(ctx, vp8,
155 "Missed a picture, sequence broken\n");
158 uint16_t expected_seq = vp8->prev_seq + 1;
159 int16_t diff = seq - expected_seq;
161 // No picture id, so we can't know if missed packets
162 // contained any new frames. If diff == 0, we did get
163 // later packets from the same frame (matching timestamp),
164 // so we know we didn't miss any frame. If diff == 1 and
165 // we still have data (not flushed by the end of frame
166 // marker), the single missed packet must have been part
167 // of the same frame.
168 if ((diff == 0 || diff == 1) && can_continue) {
169 // Proceed with what we have
171 return vp8_broken_sequence(ctx, vp8,
172 "Missed too much, sequence broken\n");
176 return vp8_broken_sequence(ctx, vp8,
177 "Missed unknown data, sequence broken\n");
181 if (avio_tell(vp8->data) >= vp8->first_part_size) {
182 int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
185 pkt->size = vp8->first_part_size;
186 returned_old_frame = 1;
187 old_timestamp = vp8->timestamp;
190 vp8_free_buffer(vp8);
194 vp8->first_part_size = (AV_RL16(&buf[1]) << 3 | buf[0] >> 5) + 3;
195 if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
197 vp8->timestamp = *timestamp;
198 vp8->broken_frame = 0;
199 vp8->prev_pictureid = pictureid;
200 vp8->is_keyframe = !non_key;
202 uint16_t expected_seq = vp8->prev_seq + 1;
204 if (!vp8->sequence_ok)
205 return AVERROR(EAGAIN);
207 if (vp8->timestamp != *timestamp) {
208 // Missed the start of the new frame, sequence broken
209 vp8->sequence_ok = 0;
210 av_log(ctx, AV_LOG_WARNING,
211 "Received no start marker; dropping frame\n");
212 vp8_free_buffer(vp8);
213 return AVERROR(EAGAIN);
216 if (seq != expected_seq) {
217 if (vp8->is_keyframe) {
218 return vp8_broken_sequence(ctx, vp8,
219 "Missed part of a keyframe, sequence broken\n");
220 } else if (vp8->data && avio_tell(vp8->data) >= vp8->first_part_size) {
221 vp8->broken_frame = 1;
223 return vp8_broken_sequence(ctx, vp8,
224 "Missed part of the first partition, sequence broken\n");
230 return vp8_broken_sequence(ctx, vp8, "Received no start marker\n");
233 avio_write(vp8->data, buf, len);
237 if (returned_old_frame) {
238 *timestamp = old_timestamp;
241 ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
244 if (vp8->broken_frame)
245 pkt->size = vp8->first_part_size;
249 return AVERROR(EAGAIN);
252 static PayloadContext *vp8_new_context(void)
254 return av_mallocz(sizeof(PayloadContext));
257 static void vp8_free_context(PayloadContext *vp8)
259 vp8_free_buffer(vp8);
263 RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
265 .codec_type = AVMEDIA_TYPE_VIDEO,
266 .codec_id = AV_CODEC_ID_VP8,
267 .alloc = vp8_new_context,
268 .free = vp8_free_context,
269 .parse_packet = vp8_handle_packet,