2010-08-16 14:23:35 +00:00
|
|
|
/*
|
|
|
|
* RTP VP8 Depacketizer
|
|
|
|
* Copyright (c) 2010 Josh Allmann
|
2012-10-08 19:25:55 +00:00
|
|
|
* Copyright (c) 2012 Martin Storsjo
|
2010-08-16 14:23:35 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2010-08-16 14:23:35 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-08-16 14:23:35 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-08-16 14:23:35 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-08-16 14:23:35 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief RTP support for the VP8 payload
|
|
|
|
* @author Josh Allmann <joshua.allmann@gmail.com>
|
2012-10-08 19:25:55 +00:00
|
|
|
* @see http://tools.ietf.org/html/draft-ietf-payload-vp8-05
|
2010-08-16 14:23:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavcodec/bytestream.h"
|
|
|
|
|
2015-02-24 11:37:03 +00:00
|
|
|
#include "avio_internal.h"
|
2010-08-16 14:23:35 +00:00
|
|
|
#include "rtpdec_formats.h"
|
|
|
|
|
|
|
|
struct PayloadContext {
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *data;
|
2012-12-12 14:59:45 +00:00
|
|
|
uint32_t timestamp;
|
2012-12-11 13:12:31 +00:00
|
|
|
int is_keyframe;
|
2012-12-11 14:03:33 +00:00
|
|
|
/* If sequence_ok is set, we keep returning data (even if we might have
|
|
|
|
* lost some data, but we haven't lost any too critical data that would
|
|
|
|
* cause the decoder to desynchronize and output random garbage).
|
|
|
|
*/
|
2012-12-11 13:12:31 +00:00
|
|
|
int sequence_ok;
|
|
|
|
int first_part_size;
|
|
|
|
uint16_t prev_seq;
|
|
|
|
int prev_pictureid;
|
|
|
|
int broken_frame;
|
2012-12-11 14:03:33 +00:00
|
|
|
/* If sequence_dirty is set, we have lost some data (critical or
|
|
|
|
* non-critical) and decoding will have some sort of artefacts, and
|
|
|
|
* we thus should request a new keyframe.
|
|
|
|
*/
|
|
|
|
int sequence_dirty;
|
|
|
|
int got_keyframe;
|
2010-08-16 14:23:35 +00:00
|
|
|
};
|
|
|
|
|
2012-12-11 13:12:31 +00:00
|
|
|
static int vp8_broken_sequence(AVFormatContext *ctx, PayloadContext *vp8,
|
|
|
|
const char *msg)
|
|
|
|
{
|
|
|
|
vp8->sequence_ok = 0;
|
|
|
|
av_log(ctx, AV_LOG_WARNING, "%s", msg);
|
2015-02-24 11:37:03 +00:00
|
|
|
ffio_free_dyn_buf(&vp8->data);
|
2012-12-11 13:12:31 +00:00
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
}
|
|
|
|
|
2012-12-10 12:38:32 +00:00
|
|
|
static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8,
|
|
|
|
AVStream *st, AVPacket *pkt, uint32_t *timestamp,
|
|
|
|
const uint8_t *buf, int len, uint16_t seq,
|
|
|
|
int flags)
|
2010-08-16 14:23:35 +00:00
|
|
|
{
|
2012-10-08 19:25:55 +00:00
|
|
|
int start_partition, end_packet;
|
2012-10-08 22:17:45 +00:00
|
|
|
int extended_bits, part_id;
|
2012-10-08 19:25:55 +00:00
|
|
|
int pictureid_present = 0, tl0picidx_present = 0, tid_present = 0,
|
|
|
|
keyidx_present = 0;
|
2013-01-07 20:02:07 +00:00
|
|
|
int pictureid = -1, pictureid_mask = 0;
|
2012-12-11 13:12:31 +00:00
|
|
|
int returned_old_frame = 0;
|
2013-03-02 11:54:50 +00:00
|
|
|
uint32_t old_timestamp = 0;
|
2012-12-11 13:12:31 +00:00
|
|
|
|
|
|
|
if (!buf) {
|
|
|
|
if (vp8->data) {
|
|
|
|
int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-01-07 19:42:40 +00:00
|
|
|
*timestamp = vp8->timestamp;
|
2013-01-08 21:21:15 +00:00
|
|
|
if (vp8->sequence_dirty)
|
|
|
|
pkt->flags |= AV_PKT_FLAG_CORRUPT;
|
2012-12-11 13:12:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
}
|
2012-10-08 19:25:55 +00:00
|
|
|
|
|
|
|
if (len < 1)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
extended_bits = buf[0] & 0x80;
|
|
|
|
start_partition = buf[0] & 0x10;
|
|
|
|
part_id = buf[0] & 0x0f;
|
|
|
|
end_packet = flags & RTP_FLAG_MARKER;
|
|
|
|
buf++;
|
|
|
|
len--;
|
|
|
|
if (extended_bits) {
|
|
|
|
if (len < 1)
|
2010-08-16 14:23:35 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2012-10-08 19:25:55 +00:00
|
|
|
pictureid_present = buf[0] & 0x80;
|
|
|
|
tl0picidx_present = buf[0] & 0x40;
|
|
|
|
tid_present = buf[0] & 0x20;
|
|
|
|
keyidx_present = buf[0] & 0x10;
|
|
|
|
buf++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
if (pictureid_present) {
|
|
|
|
if (len < 1)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-12-11 13:12:31 +00:00
|
|
|
if (buf[0] & 0x80) {
|
|
|
|
if (len < 2)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
pictureid = AV_RB16(buf) & 0x7fff;
|
|
|
|
pictureid_mask = 0x7fff;
|
|
|
|
buf += 2;
|
|
|
|
len -= 2;
|
|
|
|
} else {
|
|
|
|
pictureid = buf[0] & 0x7f;
|
|
|
|
pictureid_mask = 0x7f;
|
|
|
|
buf++;
|
|
|
|
len--;
|
|
|
|
}
|
2010-08-16 14:23:35 +00:00
|
|
|
}
|
2012-10-08 19:25:55 +00:00
|
|
|
if (tl0picidx_present) {
|
|
|
|
// Ignoring temporal level zero index
|
|
|
|
buf++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
if (tid_present || keyidx_present) {
|
2012-10-08 22:17:45 +00:00
|
|
|
// Ignoring temporal layer index, layer sync bit and keyframe index
|
2012-10-08 19:25:55 +00:00
|
|
|
buf++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
if (len < 1)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2010-08-16 14:23:35 +00:00
|
|
|
|
2012-12-11 13:12:31 +00:00
|
|
|
if (start_partition && part_id == 0 && len >= 3) {
|
2010-08-16 14:23:35 +00:00
|
|
|
int res;
|
2012-12-11 13:12:31 +00:00
|
|
|
int non_key = buf[0] & 0x01;
|
|
|
|
if (!non_key) {
|
2015-02-24 11:37:03 +00:00
|
|
|
ffio_free_dyn_buf(&vp8->data);
|
2012-12-11 13:12:31 +00:00
|
|
|
// Keyframe, decoding ok again
|
|
|
|
vp8->sequence_ok = 1;
|
2012-12-11 14:03:33 +00:00
|
|
|
vp8->sequence_dirty = 0;
|
|
|
|
vp8->got_keyframe = 1;
|
2012-12-11 13:12:31 +00:00
|
|
|
} else {
|
|
|
|
int can_continue = vp8->data && !vp8->is_keyframe &&
|
|
|
|
avio_tell(vp8->data) >= vp8->first_part_size;
|
|
|
|
if (!vp8->sequence_ok)
|
|
|
|
return AVERROR(EAGAIN);
|
2012-12-11 14:03:33 +00:00
|
|
|
if (!vp8->got_keyframe)
|
|
|
|
return vp8_broken_sequence(ctx, vp8, "Keyframe missing\n");
|
2012-12-11 13:12:31 +00:00
|
|
|
if (pictureid >= 0) {
|
|
|
|
if (pictureid != ((vp8->prev_pictureid + 1) & pictureid_mask)) {
|
|
|
|
return vp8_broken_sequence(ctx, vp8,
|
|
|
|
"Missed a picture, sequence broken\n");
|
|
|
|
} else {
|
|
|
|
if (vp8->data && !can_continue)
|
|
|
|
return vp8_broken_sequence(ctx, vp8,
|
|
|
|
"Missed a picture, sequence broken\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint16_t expected_seq = vp8->prev_seq + 1;
|
|
|
|
int16_t diff = seq - expected_seq;
|
|
|
|
if (vp8->data) {
|
|
|
|
// No picture id, so we can't know if missed packets
|
|
|
|
// contained any new frames. If diff == 0, we did get
|
|
|
|
// later packets from the same frame (matching timestamp),
|
|
|
|
// so we know we didn't miss any frame. If diff == 1 and
|
|
|
|
// we still have data (not flushed by the end of frame
|
|
|
|
// marker), the single missed packet must have been part
|
|
|
|
// of the same frame.
|
|
|
|
if ((diff == 0 || diff == 1) && can_continue) {
|
|
|
|
// Proceed with what we have
|
|
|
|
} else {
|
|
|
|
return vp8_broken_sequence(ctx, vp8,
|
|
|
|
"Missed too much, sequence broken\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (diff != 0)
|
|
|
|
return vp8_broken_sequence(ctx, vp8,
|
|
|
|
"Missed unknown data, sequence broken\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (vp8->data) {
|
2012-12-11 14:03:33 +00:00
|
|
|
vp8->sequence_dirty = 1;
|
2012-12-11 13:12:31 +00:00
|
|
|
if (avio_tell(vp8->data) >= vp8->first_part_size) {
|
|
|
|
int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-01-08 21:21:15 +00:00
|
|
|
pkt->flags |= AV_PKT_FLAG_CORRUPT;
|
2012-12-11 13:12:31 +00:00
|
|
|
returned_old_frame = 1;
|
|
|
|
old_timestamp = vp8->timestamp;
|
|
|
|
} else {
|
|
|
|
// Shouldn't happen
|
2015-02-24 11:37:03 +00:00
|
|
|
ffio_free_dyn_buf(&vp8->data);
|
2012-12-11 13:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-16 14:23:35 +00:00
|
|
|
}
|
2012-12-11 13:12:31 +00:00
|
|
|
vp8->first_part_size = (AV_RL16(&buf[1]) << 3 | buf[0] >> 5) + 3;
|
2011-03-17 07:13:34 +00:00
|
|
|
if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
|
2010-08-16 14:23:35 +00:00
|
|
|
return res;
|
2012-10-08 19:25:55 +00:00
|
|
|
vp8->timestamp = *timestamp;
|
2012-12-11 13:12:31 +00:00
|
|
|
vp8->broken_frame = 0;
|
|
|
|
vp8->prev_pictureid = pictureid;
|
|
|
|
vp8->is_keyframe = !non_key;
|
|
|
|
} else {
|
|
|
|
uint16_t expected_seq = vp8->prev_seq + 1;
|
2010-08-16 14:23:35 +00:00
|
|
|
|
2012-12-11 13:12:31 +00:00
|
|
|
if (!vp8->sequence_ok)
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
|
|
|
|
if (vp8->timestamp != *timestamp) {
|
|
|
|
// Missed the start of the new frame, sequence broken
|
2013-01-09 12:25:22 +00:00
|
|
|
return vp8_broken_sequence(ctx, vp8,
|
|
|
|
"Received no start marker; dropping frame\n");
|
2012-12-11 13:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (seq != expected_seq) {
|
|
|
|
if (vp8->is_keyframe) {
|
|
|
|
return vp8_broken_sequence(ctx, vp8,
|
|
|
|
"Missed part of a keyframe, sequence broken\n");
|
|
|
|
} else if (vp8->data && avio_tell(vp8->data) >= vp8->first_part_size) {
|
|
|
|
vp8->broken_frame = 1;
|
2012-12-11 14:03:33 +00:00
|
|
|
vp8->sequence_dirty = 1;
|
2012-12-11 13:12:31 +00:00
|
|
|
} else {
|
|
|
|
return vp8_broken_sequence(ctx, vp8,
|
|
|
|
"Missed part of the first partition, sequence broken\n");
|
|
|
|
}
|
|
|
|
}
|
2010-08-16 14:23:35 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 13:12:31 +00:00
|
|
|
if (!vp8->data)
|
|
|
|
return vp8_broken_sequence(ctx, vp8, "Received no start marker\n");
|
|
|
|
|
|
|
|
vp8->prev_seq = seq;
|
2013-01-09 12:25:59 +00:00
|
|
|
if (!vp8->broken_frame)
|
|
|
|
avio_write(vp8->data, buf, len);
|
2010-08-16 14:23:35 +00:00
|
|
|
|
2013-01-07 19:42:46 +00:00
|
|
|
if (returned_old_frame) {
|
|
|
|
*timestamp = old_timestamp;
|
|
|
|
return end_packet ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2010-08-16 14:23:35 +00:00
|
|
|
if (end_packet) {
|
2012-12-11 13:12:31 +00:00
|
|
|
int ret;
|
|
|
|
ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
|
2012-11-01 13:03:04 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-01-08 21:21:15 +00:00
|
|
|
if (vp8->sequence_dirty)
|
|
|
|
pkt->flags |= AV_PKT_FLAG_CORRUPT;
|
2015-02-20 14:17:44 +00:00
|
|
|
if (vp8->is_keyframe)
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2010-08-16 14:23:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
}
|
|
|
|
|
2015-02-24 10:43:07 +00:00
|
|
|
static av_cold int vp8_init(AVFormatContext *s, int st_index, PayloadContext *vp8)
|
2010-08-16 14:23:35 +00:00
|
|
|
{
|
2012-12-11 14:03:33 +00:00
|
|
|
vp8->sequence_ok = 1;
|
2015-02-24 10:43:07 +00:00
|
|
|
return 0;
|
2010-08-16 14:23:35 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 15:01:48 +00:00
|
|
|
static void vp8_close_context(PayloadContext *vp8)
|
2010-08-16 14:23:35 +00:00
|
|
|
{
|
2015-02-24 11:37:03 +00:00
|
|
|
ffio_free_dyn_buf(&vp8->data);
|
2010-08-16 14:23:35 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 14:03:33 +00:00
|
|
|
static int vp8_need_keyframe(PayloadContext *vp8)
|
|
|
|
{
|
|
|
|
return vp8->sequence_dirty || !vp8->sequence_ok;
|
|
|
|
}
|
|
|
|
|
2010-08-16 14:23:35 +00:00
|
|
|
RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
|
|
|
|
.enc_name = "VP8",
|
|
|
|
.codec_type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 09:11:04 +00:00
|
|
|
.codec_id = AV_CODEC_ID_VP8,
|
2015-02-24 10:43:07 +00:00
|
|
|
.priv_data_size = sizeof(PayloadContext),
|
|
|
|
.init = vp8_init,
|
2015-02-24 15:01:48 +00:00
|
|
|
.close = vp8_close_context,
|
2010-08-16 14:23:35 +00:00
|
|
|
.parse_packet = vp8_handle_packet,
|
2012-12-11 14:03:33 +00:00
|
|
|
.need_keyframe = vp8_need_keyframe,
|
2010-08-16 14:23:35 +00:00
|
|
|
};
|