]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpdec_vp8.c
Merge commit '1fb8f6a44f06e48386450fe0363aefc02583d24a'
[ffmpeg] / libavformat / rtpdec_vp8.c
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 {
35     AVIOContext *data;
36     uint32_t     timestamp;
37     int          is_keyframe;
38     /* If sequence_ok is set, we keep returning data (even if we might have
39      * lost some data, but we haven't lost any too critical data that would
40      * cause the decoder to desynchronize and output random garbage).
41      */
42     int          sequence_ok;
43     int          first_part_size;
44     uint16_t     prev_seq;
45     int          prev_pictureid;
46     int          broken_frame;
47     /* If sequence_dirty is set, we have lost some data (critical or
48      * non-critical) and decoding will have some sort of artefacts, and
49      * we thus should request a new keyframe.
50      */
51     int          sequence_dirty;
52     int          got_keyframe;
53 };
54
55 static void vp8_free_buffer(PayloadContext *vp8)
56 {
57     uint8_t *tmp;
58     if (!vp8->data)
59         return;
60     avio_close_dyn_buf(vp8->data, &tmp);
61     av_free(tmp);
62     vp8->data = NULL;
63 }
64
65 static int vp8_broken_sequence(AVFormatContext *ctx, PayloadContext *vp8,
66                                const char *msg)
67 {
68     vp8->sequence_ok = 0;
69     av_log(ctx, AV_LOG_WARNING, "%s", msg);
70     vp8_free_buffer(vp8);
71     return AVERROR(EAGAIN);
72 }
73
74 static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8,
75                              AVStream *st, AVPacket *pkt, uint32_t *timestamp,
76                              const uint8_t *buf, int len, uint16_t seq,
77                              int flags)
78 {
79     int start_partition, end_packet;
80     int extended_bits, part_id;
81     int pictureid_present = 0, tl0picidx_present = 0, tid_present = 0,
82         keyidx_present = 0;
83     int pictureid = -1, pictureid_mask = 0;
84     int returned_old_frame = 0;
85     uint32_t old_timestamp;
86
87     if (!buf) {
88         if (vp8->data) {
89             int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
90             if (ret < 0)
91                 return ret;
92             *timestamp = vp8->timestamp;
93             return 0;
94         }
95         return AVERROR(EAGAIN);
96     }
97
98     if (len < 1)
99         return AVERROR_INVALIDDATA;
100
101     extended_bits   = buf[0] & 0x80;
102     start_partition = buf[0] & 0x10;
103     part_id         = buf[0] & 0x0f;
104     end_packet      = flags & RTP_FLAG_MARKER;
105     buf++;
106     len--;
107     if (extended_bits) {
108         if (len < 1)
109             return AVERROR_INVALIDDATA;
110         pictureid_present = buf[0] & 0x80;
111         tl0picidx_present = buf[0] & 0x40;
112         tid_present       = buf[0] & 0x20;
113         keyidx_present    = buf[0] & 0x10;
114         buf++;
115         len--;
116     }
117     if (pictureid_present) {
118         if (len < 1)
119             return AVERROR_INVALIDDATA;
120         if (buf[0] & 0x80) {
121             if (len < 2)
122                 return AVERROR_INVALIDDATA;
123             pictureid = AV_RB16(buf) & 0x7fff;
124             pictureid_mask = 0x7fff;
125             buf += 2;
126             len -= 2;
127         } else {
128             pictureid = buf[0] & 0x7f;
129             pictureid_mask = 0x7f;
130             buf++;
131             len--;
132         }
133     }
134     if (tl0picidx_present) {
135         // Ignoring temporal level zero index
136         buf++;
137         len--;
138     }
139     if (tid_present || keyidx_present) {
140         // Ignoring temporal layer index, layer sync bit and keyframe index
141         buf++;
142         len--;
143     }
144     if (len < 1)
145         return AVERROR_INVALIDDATA;
146
147     if (start_partition && part_id == 0 && len >= 3) {
148         int res;
149         int non_key = buf[0] & 0x01;
150         if (!non_key) {
151             vp8_free_buffer(vp8);
152             // Keyframe, decoding ok again
153             vp8->sequence_ok = 1;
154             vp8->sequence_dirty = 0;
155             vp8->got_keyframe = 1;
156         } else {
157             int can_continue = vp8->data && !vp8->is_keyframe &&
158                                avio_tell(vp8->data) >= vp8->first_part_size;
159             if (!vp8->sequence_ok)
160                 return AVERROR(EAGAIN);
161             if (!vp8->got_keyframe)
162                 return vp8_broken_sequence(ctx, vp8, "Keyframe missing\n");
163             if (pictureid >= 0) {
164                 if (pictureid != ((vp8->prev_pictureid + 1) & pictureid_mask)) {
165                     return vp8_broken_sequence(ctx, vp8,
166                                                "Missed a picture, sequence broken\n");
167                 } else {
168                     if (vp8->data && !can_continue)
169                         return vp8_broken_sequence(ctx, vp8,
170                                                    "Missed a picture, sequence broken\n");
171                 }
172             } else {
173                 uint16_t expected_seq = vp8->prev_seq + 1;
174                 int16_t diff = seq - expected_seq;
175                 if (vp8->data) {
176                     // No picture id, so we can't know if missed packets
177                     // contained any new frames. If diff == 0, we did get
178                     // later packets from the same frame (matching timestamp),
179                     // so we know we didn't miss any frame. If diff == 1 and
180                     // we still have data (not flushed by the end of frame
181                     // marker), the single missed packet must have been part
182                     // of the same frame.
183                     if ((diff == 0 || diff == 1) && can_continue) {
184                         // Proceed with what we have
185                     } else {
186                         return vp8_broken_sequence(ctx, vp8,
187                                                    "Missed too much, sequence broken\n");
188                     }
189                 } else {
190                     if (diff != 0)
191                         return vp8_broken_sequence(ctx, vp8,
192                                                    "Missed unknown data, sequence broken\n");
193                 }
194             }
195             if (vp8->data) {
196                 vp8->sequence_dirty = 1;
197                 if (avio_tell(vp8->data) >= vp8->first_part_size) {
198                     int ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
199                     if (ret < 0)
200                         return ret;
201                     pkt->size = vp8->first_part_size;
202                     returned_old_frame = 1;
203                     old_timestamp = vp8->timestamp;
204                 } else {
205                     // Shouldn't happen
206                     vp8_free_buffer(vp8);
207                 }
208             }
209         }
210         vp8->first_part_size = (AV_RL16(&buf[1]) << 3 | buf[0] >> 5) + 3;
211         if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
212             return res;
213         vp8->timestamp = *timestamp;
214         vp8->broken_frame = 0;
215         vp8->prev_pictureid = pictureid;
216         vp8->is_keyframe = !non_key;
217     } else {
218         uint16_t expected_seq = vp8->prev_seq + 1;
219
220         if (!vp8->sequence_ok)
221             return AVERROR(EAGAIN);
222
223         if (vp8->timestamp != *timestamp) {
224             // Missed the start of the new frame, sequence broken
225             vp8->sequence_ok = 0;
226             av_log(ctx, AV_LOG_WARNING,
227                    "Received no start marker; dropping frame\n");
228             vp8_free_buffer(vp8);
229             return AVERROR(EAGAIN);
230         }
231
232         if (seq != expected_seq) {
233             if (vp8->is_keyframe) {
234                 return vp8_broken_sequence(ctx, vp8,
235                                            "Missed part of a keyframe, sequence broken\n");
236             } else if (vp8->data && avio_tell(vp8->data) >= vp8->first_part_size) {
237                 vp8->broken_frame = 1;
238                 vp8->sequence_dirty = 1;
239             } else {
240                 return vp8_broken_sequence(ctx, vp8,
241                                            "Missed part of the first partition, sequence broken\n");
242             }
243         }
244     }
245
246     if (!vp8->data)
247         return vp8_broken_sequence(ctx, vp8, "Received no start marker\n");
248
249     vp8->prev_seq = seq;
250     avio_write(vp8->data, buf, len);
251
252     if (returned_old_frame) {
253         *timestamp = old_timestamp;
254         return end_packet ? 1 : 0;
255     }
256
257     if (end_packet) {
258         int ret;
259         ret = ff_rtp_finalize_packet(pkt, &vp8->data, st->index);
260         if (ret < 0)
261             return ret;
262         if (vp8->broken_frame)
263             pkt->size = vp8->first_part_size;
264         return 0;
265     }
266
267     return AVERROR(EAGAIN);
268 }
269
270 static PayloadContext *vp8_new_context(void)
271 {
272     PayloadContext *vp8 = av_mallocz(sizeof(PayloadContext));
273     if (!vp8)
274         return NULL;
275     vp8->sequence_ok = 1;
276     return vp8;
277 }
278
279 static void vp8_free_context(PayloadContext *vp8)
280 {
281     vp8_free_buffer(vp8);
282     av_free(vp8);
283 }
284
285 static int vp8_need_keyframe(PayloadContext *vp8)
286 {
287     return vp8->sequence_dirty || !vp8->sequence_ok;
288 }
289
290 RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
291     .enc_name       = "VP8",
292     .codec_type     = AVMEDIA_TYPE_VIDEO,
293     .codec_id       = AV_CODEC_ID_VP8,
294     .alloc          = vp8_new_context,
295     .free           = vp8_free_context,
296     .parse_packet   = vp8_handle_packet,
297     .need_keyframe  = vp8_need_keyframe,
298 };