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