]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpdec_vp8.c
Merge remote-tracking branch 'qatar/master'
[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 };
38
39 static void prepare_packet(AVPacket *pkt, PayloadContext *vp8, int stream)
40 {
41     av_init_packet(pkt);
42     pkt->stream_index = stream;
43     pkt->size         = avio_close_dyn_buf(vp8->data, &pkt->data);
44     pkt->destruct     = av_destruct_packet;
45     vp8->data         = NULL;
46 }
47
48 static int vp8_handle_packet(AVFormatContext *ctx,
49                              PayloadContext *vp8,
50                              AVStream *st,
51                              AVPacket *pkt,
52                              uint32_t *timestamp,
53                              const uint8_t *buf,
54                              int len, int flags)
55 {
56     int start_partition, end_packet;
57     int extended_bits, non_ref, part_id;
58     int pictureid_present = 0, tl0picidx_present = 0, tid_present = 0,
59         keyidx_present = 0;
60     int pictureid = -1, keyidx = -1;
61
62     if (len < 1)
63         return AVERROR_INVALIDDATA;
64
65     extended_bits   = buf[0] & 0x80;
66     non_ref         = buf[0] & 0x20;
67     start_partition = buf[0] & 0x10;
68     part_id         = buf[0] & 0x0f;
69     end_packet      = flags & RTP_FLAG_MARKER;
70     buf++;
71     len--;
72     if (extended_bits) {
73         if (len < 1)
74             return AVERROR_INVALIDDATA;
75         pictureid_present = buf[0] & 0x80;
76         tl0picidx_present = buf[0] & 0x40;
77         tid_present       = buf[0] & 0x20;
78         keyidx_present    = buf[0] & 0x10;
79         buf++;
80         len--;
81     }
82     if (pictureid_present) {
83         if (len < 1)
84             return AVERROR_INVALIDDATA;
85         if (buf[0] & 0x80) {
86             if (len < 2)
87                 return AVERROR_INVALIDDATA;
88             pictureid = AV_RB16(buf) & 0x7fff;
89             buf += 2;
90             len -= 2;
91         } else {
92             pictureid = buf[0] & 0x7f;
93             buf++;
94             len--;
95         }
96     }
97     if (tl0picidx_present) {
98         // Ignoring temporal level zero index
99         buf++;
100         len--;
101     }
102     if (tid_present || keyidx_present) {
103         // Ignoring temporal layer index and layer sync bit
104         if (len < 1)
105             return AVERROR_INVALIDDATA;
106         if (keyidx_present)
107             keyidx = buf[0] & 0x1f;
108         buf++;
109         len--;
110     }
111     if (len < 1)
112         return AVERROR_INVALIDDATA;
113
114     if (start_partition && part_id == 0) {
115         int res;
116         if (vp8->data) {
117             uint8_t *tmp;
118             avio_close_dyn_buf(vp8->data, &tmp);
119             av_free(tmp);
120             vp8->data = NULL;
121         }
122         if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
123             return res;
124         vp8->timestamp = *timestamp;
125      }
126
127     if (!vp8->data || vp8->timestamp != *timestamp) {
128         av_log(ctx, AV_LOG_WARNING,
129                "Received no start marker; dropping frame\n");
130         return AVERROR(EAGAIN);
131     }
132
133     avio_write(vp8->data, buf, len);
134
135     if (end_packet) {
136         prepare_packet(pkt, vp8, st->index);
137         return 0;
138     }
139
140     return AVERROR(EAGAIN);
141 }
142
143 static PayloadContext *vp8_new_context(void)
144 {
145     return av_mallocz(sizeof(PayloadContext));
146 }
147
148 static void vp8_free_context(PayloadContext *vp8)
149 {
150     if (vp8->data) {
151         uint8_t *tmp;
152         avio_close_dyn_buf(vp8->data, &tmp);
153         av_free(tmp);
154     }
155     av_free(vp8);
156 }
157
158 RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
159     .enc_name       = "VP8",
160     .codec_type     = AVMEDIA_TYPE_VIDEO,
161     .codec_id       = AV_CODEC_ID_VP8,
162     .alloc          = vp8_new_context,
163     .free           = vp8_free_context,
164     .parse_packet   = vp8_handle_packet,
165 };