3 * Copyright (c) 2010 Josh Allmann
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * @brief RTP support for the VP8 payload
25 * @author Josh Allmann <joshua.allmann@gmail.com>
26 * @see http://www.webmproject.org/code/specs/rtp/
29 #include "libavcodec/bytestream.h"
31 #include "rtpdec_formats.h"
33 struct PayloadContext {
39 static void prepare_packet(AVPacket *pkt, PayloadContext *vp8, int stream)
42 pkt->stream_index = stream;
43 pkt->flags = vp8->is_keyframe ? AV_PKT_FLAG_KEY : 0;
44 pkt->size = avio_close_dyn_buf(vp8->data, &pkt->data);
45 pkt->destruct = av_destruct_packet;
49 static int vp8_handle_packet(AVFormatContext *ctx,
57 int start_packet, end_packet, has_au, ret = AVERROR(EAGAIN);
60 // only called when vp8_handle_packet returns 1
62 av_log(ctx, AV_LOG_ERROR, "Invalid VP8 data passed\n");
63 return AVERROR_INVALIDDATA;
65 prepare_packet(pkt, vp8, st->index);
66 *timestamp = vp8->timestamp;
70 start_packet = *buf & 1;
71 end_packet = flags & RTP_FLAG_MARKER;
78 uint32_t ts = *timestamp;
80 // missing end marker; return old frame anyway. untested
81 prepare_packet(pkt, vp8, st->index);
82 *timestamp = vp8->timestamp; // reset timestamp from old frame
84 // if current frame fits into one rtp packet, need to hold
85 // that for the next av_get_packet call
86 ret = end_packet ? 1 : 0;
88 if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
90 vp8->is_keyframe = *buf & 1;
94 if (!vp8->data || vp8->timestamp != *timestamp && ret == AVERROR(EAGAIN)) {
95 av_log(ctx, AV_LOG_WARNING,
96 "Received no start marker; dropping frame\n");
97 return AVERROR(EAGAIN);
100 // cycle through VP8AU headers if needed
101 // not tested with actual VP8AUs
104 if (has_au && len > 2) {
105 au_len = AV_RB16(buf);
108 if (buf + au_len > buf + len) {
109 av_log(ctx, AV_LOG_ERROR, "Invalid VP8AU length\n");
110 return AVERROR_INVALIDDATA;
114 avio_write(vp8->data, buf, au_len);
119 if (ret != AVERROR(EAGAIN)) // did we miss a end marker?
123 prepare_packet(pkt, vp8, st->index);
127 return AVERROR(EAGAIN);
130 static PayloadContext *vp8_new_context(void)
132 av_log(NULL, AV_LOG_ERROR, "RTP VP8 payload implementation is incompatible "
133 "with the latest spec drafts.\n");
134 return av_mallocz(sizeof(PayloadContext));
137 static void vp8_free_context(PayloadContext *vp8)
141 avio_close_dyn_buf(vp8->data, &tmp);
147 RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
149 .codec_type = AVMEDIA_TYPE_VIDEO,
150 .codec_id = AV_CODEC_ID_VP8,
151 .alloc = vp8_new_context,
152 .free = vp8_free_context,
153 .parse_packet = vp8_handle_packet,