33
34
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);
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;
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;
79
int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
65
prepare_packet(pkt, vp8, st->index);
66
*timestamp = vp8->timestamp;
84
return AVERROR(EAGAIN);
70
start_packet = *buf & 1;
71
end_packet = flags & RTP_FLAG_MARKER;
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) {
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;
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;
88
195
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;
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");
114
avio_write(vp8->data, buf, au_len);
119
if (ret != AVERROR(EAGAIN)) // did we miss a end marker?
230
return vp8_broken_sequence(ctx, vp8, "Received no start marker\n");
233
avio_write(vp8->data, buf, len);
122
235
if (end_packet) {
123
prepare_packet(pkt, vp8, st->index);
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;