2 * RTP parser for VP9 payload format (draft version 02) - experimental
3 * Copyright (c) 2015 Thomas Volkert <thomas@homer-conferencing.com>
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
22 #include "libavutil/intreadwrite.h"
24 #include "avio_internal.h"
25 #include "rtpdec_formats.h"
27 #define RTP_VP9_DESC_REQUIRED_SIZE 1
29 struct PayloadContext {
34 static av_cold int vp9_init(AVFormatContext *ctx, int st_index,
37 av_log(ctx, AV_LOG_WARNING,
38 "RTP/VP9 support is still experimental\n");
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,
48 int has_pic_id, has_layer_idc, has_ref_idc, has_ss_data;
49 av_unused int pic_id = 0, non_key_frame = 0, inter_picture_layer_frame;
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;
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);
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;
67 * decode the required VP9 payload descriptor according to section 4.2 of the spec.:
71 * |I|P|L|F|B|E|V|-| (REQUIRED)
74 * I: PictureID present
75 * P: Inter-picture predicted layer frame
76 * L: Layer indices present
78 * B: Start of VP9 frame
80 * V: Scalability Structure (SS) present
82 has_pic_id = !!(buf[0] & 0x80);
83 inter_picture_layer_frame = !!(buf[0] & 0x40);
84 has_layer_idc = !!(buf[0] & 0x20);
85 has_ref_idc = !!(buf[0] & 0x10);
86 first_fragment = !!(buf[0] & 0x08);
87 last_fragment = !!(buf[0] & 0x04);
88 has_ss_data = !!(buf[0] & 0x02);
90 rtp_m = !!(flags & RTP_FLAG_MARKER);
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;
98 /* pass the extensions field */
99 buf += RTP_VP9_DESC_REQUIRED_SIZE;
100 len -= RTP_VP9_DESC_REQUIRED_SIZE;
103 * decode the 1-byte/2-byte picture ID:
107 * I: |M|PICTURE ID | (RECOMMENDED)
109 * M: | EXTENDED PID | (RECOMMENDED)
112 * M: The most significant bit of the first octet is an extension flag.
113 * PictureID: 8 or 16 bits including the M bit.
116 /* check for 1-byte or 2-byte picture index */
119 av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
120 return AVERROR_INVALIDDATA;
122 pic_id = AV_RB16(buf) & 0x7fff;
126 pic_id = buf[0] & 0x7f;
133 * decode layer indices
137 * L: | T | S | Q | R | (CONDITIONALLY RECOMMENDED)
140 * T, S and Q are 2-bit indices for temporal, spatial, and quality layers.
141 * If "F" is set in the initial octet, R is 2 bits representing the number
142 * of reference fields this frame refers to.
146 av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
147 return AVERROR_INVALIDDATA;
149 layer_temporal = buf[0] & 0xC0;
150 layer_spatial = buf[0] & 0x30;
151 layer_quality = buf[0] & 0x0C;
153 ref_fields = buf[0] & 0x03;
162 * decode the reference fields
165 * +-+-+-+-+-+-+-+-+ -\
166 * F: | PID |X| RS| RQ| (OPTIONAL) .
167 * +-+-+-+-+-+-+-+-+ . - R times
168 * X: | EXTENDED PID | (OPTIONAL) .
169 * +-+-+-+-+-+-+-+-+ -/
171 * PID: The relative Picture ID referred to by this frame.
172 * RS and RQ: The spatial and quality layer IDs.
173 * X: 1 if this layer index has an extended relative Picture ID.
178 av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
179 return AVERROR_INVALIDDATA;
182 has_ref_field_ext_pic_id = buf[0] & 0x10;
184 /* pass ref. field */
185 if (has_ref_field_ext_pic_id) {
187 av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
188 return AVERROR_INVALIDDATA;
191 /* ignore ref. data */
197 /* ignore ref. data */
207 * decode the scalability structure (SS)
211 * V: | PATTERN LENGTH|
212 * +-+-+-+-+-+-+-+-+ -\
213 * | T | S | Q | R | (OPTIONAL) .
214 * +-+-+-+-+-+-+-+-+ -\ .
215 * | PID |X| RS| RQ| (OPTIONAL) . . - PAT. LEN. times
216 * +-+-+-+-+-+-+-+-+ . - R times .
217 * X: | EXTENDED PID | (OPTIONAL) . .
218 * +-+-+-+-+-+-+-+-+ -/ -/
220 * PID: The relative Picture ID referred to by this frame.
221 * RS and RQ: The spatial and quality layer IDs.
222 * X: 1 if this layer index has an extended relative Picture ID.
227 av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
228 return AVERROR_INVALIDDATA;
231 y = !!(buf[0] & 0x10);
232 g = !!(buf[0] & 0x08);
236 avpriv_report_missing_feature(ctx, "VP9 scalability structure with multiple layers");
237 return AVERROR_PATCHWELCOME;
240 if (len < 4 * (n_s + 1)) {
241 av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
242 return AVERROR_INVALIDDATA;
244 for (i = 0; i < n_s + 1; i++) {
247 h = AV_RB16(buf + 2);
255 av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
256 return AVERROR_INVALIDDATA;
261 for (i = 0; i < n_g; i++) {
262 av_unused int t, u, r, j;
264 av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
265 return AVERROR_INVALIDDATA;
268 u = !!(buf[0] & 0x10);
269 r = (buf[0] >> 2) & 0x03;
273 av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
274 return AVERROR_INVALIDDATA;
276 for (j = 0; j < r; j++) {
277 av_unused int p_diff = buf[0];
286 * decode the VP9 payload header
290 //XXX: implement when specified
292 /* sanity check: 1 byte payload as minimum */
294 av_log(ctx, AV_LOG_ERROR, "Too short RTP/VP9 packet\n");
295 return AVERROR_INVALIDDATA;
298 /* start frame buffering with new dynamic buffer */
299 if (!rtp_vp9_ctx->buf) {
300 /* sanity check: a new frame should have started */
301 if (first_fragment) {
302 res = avio_open_dyn_buf(&rtp_vp9_ctx->buf);
305 /* update the timestamp in the frame packet with the one from the RTP packet */
306 rtp_vp9_ctx->timestamp = *timestamp;
308 /* frame not started yet, need more packets */
309 return AVERROR(EAGAIN);
313 /* write the fragment to the dyn. buffer */
314 avio_write(rtp_vp9_ctx->buf, buf, len);
316 /* do we need more fragments? */
318 return AVERROR(EAGAIN);
320 /* close frame buffering and create resulting A/V packet */
321 res = ff_rtp_finalize_packet(pkt, &rtp_vp9_ctx->buf, st->index);
328 static void vp9_close_context(PayloadContext *vp9)
330 ffio_free_dyn_buf(&vp9->buf);
333 RTPDynamicProtocolHandler ff_vp9_dynamic_handler = {
335 .codec_type = AVMEDIA_TYPE_VIDEO,
336 .codec_id = AV_CODEC_ID_VP9,
337 .priv_data_size = sizeof(PayloadContext),
339 .close = vp9_close_context,
340 .parse_packet = vp9_handle_packet