annotate rtpdec_vp8.c @ 6449:3a28e6827095 libavformat

Solving memory leak and initialization problem with prev_pkt / pkt.
author bindhammer
date Tue, 31 Aug 2010 07:15:11 +0000
parents 63e7b0fb1616
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6380
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
1 /*
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
2 * RTP VP8 Depacketizer
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
3 * Copyright (c) 2010 Josh Allmann
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
4 *
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
5 * This file is part of FFmpeg.
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
6 *
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
11 *
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
15 * Lesser General Public License for more details.
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
16 *
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
20 */
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
21
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
22 /**
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
23 * @file
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
24 * @brief RTP support for the VP8 payload
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
25 * @author Josh Allmann <joshua.allmann@gmail.com>
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
26 * ( http://www.webmproject.org/code/specs/rtp/ )
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
27 */
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
28
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
29 #include "libavcodec/bytestream.h"
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
30
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
31 #include "rtpdec_formats.h"
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
32
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
33 struct PayloadContext {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
34 ByteIOContext *data;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
35 uint32_t timestamp;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
36 int is_keyframe;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
37 };
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
38
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
39 static void prepare_packet(AVPacket *pkt, PayloadContext *vp8, int stream)
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
40 {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
41 av_init_packet(pkt);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
42 pkt->stream_index = stream;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
43 pkt->flags = vp8->is_keyframe ? AV_PKT_FLAG_KEY : 0;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
44 pkt->size = url_close_dyn_buf(vp8->data, &pkt->data);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
45 pkt->destruct = av_destruct_packet;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
46 vp8->data = NULL;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
47 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
48
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
49 static int vp8_handle_packet(AVFormatContext *ctx,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
50 PayloadContext *vp8,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
51 AVStream *st,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
52 AVPacket *pkt,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
53 uint32_t *timestamp,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
54 const uint8_t *buf,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
55 int len, int flags)
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
56 {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
57 int start_packet, end_packet, has_au, ret = AVERROR(EAGAIN);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
58
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
59 if (!buf) {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
60 // only called when vp8_handle_packet returns 1
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
61 if (!vp8->data) {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
62 av_log(ctx, AV_LOG_ERROR, "Invalid VP8 data passed\n");
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
63 return AVERROR_INVALIDDATA;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
64 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
65 prepare_packet(pkt, vp8, st->index);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
66 *timestamp = vp8->timestamp;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
67 return 0;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
68 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
69
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
70 start_packet = *buf & 1;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
71 end_packet = flags & RTP_FLAG_MARKER;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
72 has_au = *buf & 2;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
73 buf++;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
74 len--;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
75
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
76 if (start_packet) {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
77 int res;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
78 uint32_t ts = *timestamp;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
79 if (vp8->data) {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
80 // missing end marker; return old frame anyway. untested
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
81 prepare_packet(pkt, vp8, st->index);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
82 *timestamp = vp8->timestamp; // reset timestamp from old frame
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
83
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
84 // if current frame fits into one rtp packet, need to hold
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
85 // that for the next av_get_packet call
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
86 ret = end_packet ? 1 : 0;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
87 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
88 if ((res = url_open_dyn_buf(&vp8->data)) < 0)
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
89 return res;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
90 vp8->is_keyframe = *buf & 1;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
91 vp8->timestamp = ts;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
92 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
93
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
94 if (!vp8->data || vp8->timestamp != *timestamp && ret == AVERROR(EAGAIN)) {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
95 av_log(ctx, AV_LOG_WARNING,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
96 "Received no start marker; dropping frame\n");
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
97 return AVERROR(EAGAIN);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
98 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
99
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
100 // cycle through VP8AU headers if needed
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
101 // not tested with actual VP8AUs
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
102 while (len) {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
103 int au_len = len;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
104 if (has_au && len > 2) {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
105 au_len = AV_RB16(buf);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
106 buf += 2;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
107 len -= 2;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
108 if (buf + au_len > buf + len) {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
109 av_log(ctx, AV_LOG_ERROR, "Invalid VP8AU length\n");
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
110 return AVERROR_INVALIDDATA;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
111 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
112 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
113
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
114 put_buffer(vp8->data, buf, au_len);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
115 buf += au_len;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
116 len -= au_len;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
117 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
118
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
119 if (ret != AVERROR(EAGAIN)) // did we miss a end marker?
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
120 return ret;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
121
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
122 if (end_packet) {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
123 prepare_packet(pkt, vp8, st->index);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
124 return 0;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
125 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
126
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
127 return AVERROR(EAGAIN);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
128 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
129
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
130 static PayloadContext *vp8_new_context(void)
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
131 {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
132 av_log(NULL, AV_LOG_WARNING, "RTP VP8 payload is still experimental\n");
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
133 return av_mallocz(sizeof(PayloadContext));
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
134 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
135
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
136 static void vp8_free_context(PayloadContext *vp8)
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
137 {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
138 if (vp8->data) {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
139 uint8_t *tmp;
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
140 url_close_dyn_buf(vp8->data, &tmp);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
141 av_free(tmp);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
142 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
143 av_free(vp8);
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
144 }
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
145
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
146 RTPDynamicProtocolHandler ff_vp8_dynamic_handler = {
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
147 .enc_name = "VP8",
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
148 .codec_type = AVMEDIA_TYPE_VIDEO,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
149 .codec_id = CODEC_ID_VP8,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
150 .open = vp8_new_context,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
151 .close = vp8_free_context,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
152 .parse_packet = vp8_handle_packet,
63e7b0fb1616 Add RTP depacketization of VP8
mstorsjo
parents:
diff changeset
153 };