FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rtpdec_vp9.c
Go to the documentation of this file.
1 /*
2  * RTP parser for VP9 payload format (draft version 0) - experimental
3  * Copyright (c) 2015 Thomas Volkert <thomas@homer-conferencing.com>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 
24 #include "avio_internal.h"
25 #include "rtpdec_formats.h"
26 
27 #define RTP_VP9_DESC_REQUIRED_SIZE 1
28 
29 struct PayloadContext {
31  uint32_t timestamp;
32 };
33 
34 static av_cold int vp9_init(AVFormatContext *ctx, int st_index,
36 {
38  "RTP/VP9 support is still experimental\n");
39 
40  return 0;
41 }
42 
43 static int vp9_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_vp9_ctx,
44  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
45  const uint8_t *buf, int len, uint16_t seq,
46  int flags)
47 {
48  int has_pic_id, has_layer_idc, has_ref_idc, has_ss_data, has_su_data;
49  av_unused int pic_id = 0, non_key_frame = 0;
50  av_unused int layer_temporal = -1, layer_spatial = -1, layer_quality = -1;
51  int ref_fields = 0, has_ref_field_ext_pic_id = 0;
52  int first_fragment, last_fragment;
53  int rtp_m;
54  int res = 0;
55 
56  /* drop data of previous packets in case of non-continuous (lossy) packet stream */
57  if (rtp_vp9_ctx->buf && rtp_vp9_ctx->timestamp != *timestamp)
58  ffio_free_dyn_buf(&rtp_vp9_ctx->buf);
59 
60  /* sanity check for size of input packet: 1 byte payload at least */
61  if (len < RTP_VP9_DESC_REQUIRED_SIZE + 1) {
62  av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet, got %d bytes\n", len);
63  return AVERROR_INVALIDDATA;
64  }
65 
66  /*
67  * decode the required VP9 payload descriptor according to section 4.2 of the spec.:
68  *
69  * 0 1 2 3 4 5 6 7
70  * +-+-+-+-+-+-+-+-+
71  * |I|L|F|B|E|V|U|-| (REQUIRED)
72  * +-+-+-+-+-+-+-+-+
73  *
74  * I: PictureID present
75  * L: Layer indices present
76  * F: Reference indices present
77  * B: Start of VP9 frame
78  * E: End of picture
79  * V: Scalability Structure (SS) present
80  * U: Scalability Structure Update (SU) present
81  */
82  has_pic_id = !!(buf[0] & 0x80);
83  has_layer_idc = !!(buf[0] & 0x40);
84  has_ref_idc = !!(buf[0] & 0x20);
85  first_fragment = !!(buf[0] & 0x10);
86  last_fragment = !!(buf[0] & 0x08);
87  has_ss_data = !!(buf[0] & 0x04);
88  has_su_data = !!(buf[0] & 0x02);
89 
90  rtp_m = !!(flags & RTP_FLAG_MARKER);
91 
92  /* sanity check for markers: B should always be equal to the RTP M marker */
93  if (last_fragment != rtp_m) {
94  av_log(ctx, AV_LOG_ERROR, "Invalid combination of B and M marker (%d != %d)\n", last_fragment, rtp_m);
95  return AVERROR_INVALIDDATA;
96  }
97 
98  /* pass the extensions field */
101 
102  /*
103  * decode the 1-byte/2-byte picture ID:
104  *
105  * 0 1 2 3 4 5 6 7
106  * +-+-+-+-+-+-+-+-+
107  * I: |M|PICTURE ID | (RECOMMENDED)
108  * +-+-+-+-+-+-+-+-+
109  * M: | EXTENDED PID | (RECOMMENDED)
110  * +-+-+-+-+-+-+-+-+
111  *
112  * M: The most significant bit of the first octet is an extension flag.
113  * PictureID: 8 or 16 bits including the M bit.
114  */
115  if (has_pic_id) {
116  if (len < 1) {
117  av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
118  return AVERROR_INVALIDDATA;
119  }
120 
121  /* check for 1-byte or 2-byte picture index */
122  if (buf[0] & 0x80) {
123  if (len < 2) {
124  av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
125  return AVERROR_INVALIDDATA;
126  }
127  pic_id = AV_RB16(buf) & 0x7fff;
128  buf += 2;
129  len -= 2;
130  } else {
131  pic_id = buf[0] & 0x7f;
132  buf++;
133  len--;
134  }
135  }
136 
137  /*
138  * decode layer indices
139  *
140  * 0 1 2 3 4 5 6 7
141  * +-+-+-+-+-+-+-+-+
142  * L: | T | S | Q | R | (CONDITIONALLY RECOMMENDED)
143  * +-+-+-+-+-+-+-+-+
144  *
145  * T, S and Q are 2-bit indices for temporal, spatial, and quality layers.
146  * If "F" is set in the initial octet, R is 2 bits representing the number
147  * of reference fields this frame refers to.
148  */
149  if (has_layer_idc) {
150  if (len < 1) {
151  av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
152  return AVERROR_INVALIDDATA;
153  }
154  layer_temporal = buf[0] & 0xC0;
155  layer_spatial = buf[0] & 0x30;
156  layer_quality = buf[0] & 0x0C;
157  if (has_ref_idc) {
158  ref_fields = buf[0] & 0x03;
159  if (ref_fields)
160  non_key_frame = 1;
161  }
162  buf++;
163  len--;
164  }
165 
166  /*
167  * decode the reference fields
168  *
169  * 0 1 2 3 4 5 6 7
170  * +-+-+-+-+-+-+-+-+ -\
171  * F: | PID |X| RS| RQ| (OPTIONAL) .
172  * +-+-+-+-+-+-+-+-+ . - R times
173  * X: | EXTENDED PID | (OPTIONAL) .
174  * +-+-+-+-+-+-+-+-+ -/
175  *
176  * PID: The relative Picture ID referred to by this frame.
177  * RS and RQ: The spatial and quality layer IDs.
178  * X: 1 if this layer index has an extended relative Picture ID.
179  */
180  if (has_ref_idc) {
181  while (ref_fields) {
182  if (len < 1) {
183  av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
184  return AVERROR_INVALIDDATA;
185  }
186 
187  has_ref_field_ext_pic_id = buf[0] & 0x10;
188 
189  /* pass ref. field */
190  if (has_ref_field_ext_pic_id) {
191  if (len < 2) {
192  av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
193  return AVERROR_INVALIDDATA;
194  }
195 
196  /* ignore ref. data */
197 
198  buf += 2;
199  len -= 2;
200  } else {
201 
202  /* ignore ref. data */
203 
204  buf++;
205  len--;
206  }
207  ref_fields--;
208  }
209  }
210 
211  /*
212  * decode the scalability structure (SS)
213  *
214  * 0 1 2 3 4 5 6 7
215  * +-+-+-+-+-+-+-+-+
216  * V: | PATTERN LENGTH|
217  * +-+-+-+-+-+-+-+-+ -\
218  * | T | S | Q | R | (OPTIONAL) .
219  * +-+-+-+-+-+-+-+-+ -\ .
220  * | PID |X| RS| RQ| (OPTIONAL) . . - PAT. LEN. times
221  * +-+-+-+-+-+-+-+-+ . - R times .
222  * X: | EXTENDED PID | (OPTIONAL) . .
223  * +-+-+-+-+-+-+-+-+ -/ -/
224  *
225  * PID: The relative Picture ID referred to by this frame.
226  * RS and RQ: The spatial and quality layer IDs.
227  * X: 1 if this layer index has an extended relative Picture ID.
228  */
229  if (has_ss_data) {
230  avpriv_report_missing_feature(ctx, "VP9 scalability structure data");
231  return AVERROR(ENOSYS);
232  }
233 
234  /*
235  * decode the scalability update structure (SU)
236  *
237  * spec. is tbd
238  */
239  if (has_su_data) {
240  avpriv_report_missing_feature(ctx, "VP9 scalability update structure data");
241  return AVERROR(ENOSYS);
242  }
243 
244  /*
245  * decode the VP9 payload header
246  *
247  * spec. is tbd
248  */
249  //XXX: implement when specified
250 
251  /* sanity check: 1 byte payload as minimum */
252  if (len < 1) {
253  av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
254  return AVERROR_INVALIDDATA;
255  }
256 
257  /* start frame buffering with new dynamic buffer */
258  if (!rtp_vp9_ctx->buf) {
259  /* sanity check: a new frame should have started */
260  if (first_fragment) {
261  res = avio_open_dyn_buf(&rtp_vp9_ctx->buf);
262  if (res < 0)
263  return res;
264  /* update the timestamp in the frame packet with the one from the RTP packet */
265  rtp_vp9_ctx->timestamp = *timestamp;
266  } else {
267  /* frame not started yet, need more packets */
268  return AVERROR(EAGAIN);
269  }
270  }
271 
272  /* write the fragment to the dyn. buffer */
273  avio_write(rtp_vp9_ctx->buf, buf, len);
274 
275  /* do we need more fragments? */
276  if (!last_fragment)
277  return AVERROR(EAGAIN);
278 
279  /* close frame buffering and create resulting A/V packet */
280  res = ff_rtp_finalize_packet(pkt, &rtp_vp9_ctx->buf, st->index);
281  if (res < 0)
282  return res;
283 
284  return 0;
285 }
286 
288  .enc_name = "VP9",
289  .codec_type = AVMEDIA_TYPE_VIDEO,
290  .codec_id = AV_CODEC_ID_VP9,
291  .priv_data_size = sizeof(PayloadContext),
292  .init = vp9_init,
294 };