FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rtpdec_vp8.c
Go to the documentation of this file.
1 /*
2  * RTP VP8 Depacketizer
3  * Copyright (c) 2010 Josh Allmann
4  * Copyright (c) 2012 Martin Storsjo
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 /**
24  * @file
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
28  */
29 
30 #include "libavcodec/bytestream.h"
31 
32 #include "rtpdec_formats.h"
33 
34 struct PayloadContext {
36  uint32_t timestamp;
40  uint16_t prev_seq;
43 };
44 
46 {
47  uint8_t *tmp;
48  if (!vp8->data)
49  return;
50  avio_close_dyn_buf(vp8->data, &tmp);
51  av_free(tmp);
52  vp8->data = NULL;
53 }
54 
56  const char *msg)
57 {
58  vp8->sequence_ok = 0;
59  av_log(ctx, AV_LOG_WARNING, "%s", msg);
60  vp8_free_buffer(vp8);
61  return AVERROR(EAGAIN);
62 }
63 
65  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
66  const uint8_t *buf, int len, uint16_t seq,
67  int flags)
68 {
69  int start_partition, end_packet;
70  int extended_bits, part_id;
71  int pictureid_present = 0, tl0picidx_present = 0, tid_present = 0,
72  keyidx_present = 0;
73  int pictureid = -1, pictureid_mask;
74  int returned_old_frame = 0;
75  uint32_t old_timestamp;
76 
77  if (!buf) {
78  if (vp8->data) {
79  int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
80  if (ret < 0)
81  return ret;
82  return 0;
83  }
84  return AVERROR(EAGAIN);
85  }
86 
87  if (len < 1)
88  return AVERROR_INVALIDDATA;
89 
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;
94  buf++;
95  len--;
96  if (extended_bits) {
97  if (len < 1)
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;
103  buf++;
104  len--;
105  }
106  if (pictureid_present) {
107  if (len < 1)
108  return AVERROR_INVALIDDATA;
109  if (buf[0] & 0x80) {
110  if (len < 2)
111  return AVERROR_INVALIDDATA;
112  pictureid = AV_RB16(buf) & 0x7fff;
113  pictureid_mask = 0x7fff;
114  buf += 2;
115  len -= 2;
116  } else {
117  pictureid = buf[0] & 0x7f;
118  pictureid_mask = 0x7f;
119  buf++;
120  len--;
121  }
122  }
123  if (tl0picidx_present) {
124  // Ignoring temporal level zero index
125  buf++;
126  len--;
127  }
128  if (tid_present || keyidx_present) {
129  // Ignoring temporal layer index, layer sync bit and keyframe index
130  buf++;
131  len--;
132  }
133  if (len < 1)
134  return AVERROR_INVALIDDATA;
135 
136  if (start_partition && part_id == 0 && len >= 3) {
137  int res;
138  int non_key = buf[0] & 0x01;
139  if (!non_key) {
140  vp8_free_buffer(vp8);
141  // Keyframe, decoding ok again
142  vp8->sequence_ok = 1;
143  } else {
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");
152  } else {
153  if (vp8->data && !can_continue)
154  return vp8_broken_sequence(ctx, vp8,
155  "Missed a picture, sequence broken\n");
156  }
157  } else {
158  uint16_t expected_seq = vp8->prev_seq + 1;
159  int16_t diff = seq - expected_seq;
160  if (vp8->data) {
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
170  } else {
171  return vp8_broken_sequence(ctx, vp8,
172  "Missed too much, sequence broken\n");
173  }
174  } else {
175  if (diff != 0)
176  return vp8_broken_sequence(ctx, vp8,
177  "Missed unknown data, sequence broken\n");
178  }
179  }
180  if (vp8->data) {
181  if (avio_tell(vp8->data) >= vp8->first_part_size) {
182  int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
183  if (ret < 0)
184  return ret;
185  pkt->size = vp8->first_part_size;
186  returned_old_frame = 1;
187  old_timestamp = vp8->timestamp;
188  } else {
189  // Shouldn't happen
190  vp8_free_buffer(vp8);
191  }
192  }
193  }
194  vp8->first_part_size = (AV_RL16(&buf[1]) << 3 | buf[0] >> 5) + 3;
195  if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
196  return res;
197  vp8->timestamp = *timestamp;
198  vp8->broken_frame = 0;
199  vp8->prev_pictureid = pictureid;
200  vp8->is_keyframe = !non_key;
201  } else {
202  uint16_t expected_seq = vp8->prev_seq + 1;
203 
204  if (!vp8->sequence_ok)
205  return AVERROR(EAGAIN);
206 
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);
214  }
215 
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;
222  } else {
223  return vp8_broken_sequence(ctx, vp8,
224  "Missed part of the first partition, sequence broken\n");
225  }
226  }
227  }
228 
229  if (!vp8->data)
230  return vp8_broken_sequence(ctx, vp8, "Received no start marker\n");
231 
232  vp8->prev_seq = seq;
233  avio_write(vp8->data, buf, len);
234 
235  if (end_packet) {
236  int ret;
237  if (returned_old_frame) {
238  *timestamp = old_timestamp;
239  return 1;
240  }
241  ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
242  if (ret < 0)
243  return ret;
244  if (vp8->broken_frame)
245  pkt->size = vp8->first_part_size;
246  return 0;
247  }
248 
249  return AVERROR(EAGAIN);
250 }
251 
253 {
254  return av_mallocz(sizeof(PayloadContext));
255 }
256 
258 {
259  vp8_free_buffer(vp8);
260  av_free(vp8);
261 }
262 
264  .enc_name = "VP8",
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,
270 };