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