Mercurial > libavformat.hg
annotate oggenc.c @ 5053:037f1082ecd6 libavformat
oggenc: check for failed av_mallocz()
author | jbr |
---|---|
date | Sun, 21 Jun 2009 20:13:54 +0000 |
parents | c05d167a4fe2 |
children | 6c7d0a04ae96 |
rev | line source |
---|---|
2731 | 1 /* |
2 * Ogg muxer | |
3 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at free dot fr> | |
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 | |
3286 | 22 #include "libavutil/crc.h" |
23 #include "libavcodec/xiph.h" | |
24 #include "libavcodec/bytestream.h" | |
4578 | 25 #include "libavcodec/flac.h" |
2731 | 26 #include "avformat.h" |
4418
d5119d75439d
Move declaration of ff_interleave_add_packet to internal.h.
bcoudurier
parents:
4311
diff
changeset
|
27 #include "internal.h" |
2731 | 28 |
29 typedef struct { | |
30 int64_t duration; | |
31 unsigned page_counter; | |
32 uint8_t *header[3]; | |
33 int header_len[3]; | |
34 /** for theora granule */ | |
35 int kfgshift; | |
36 int64_t last_kf_pts; | |
37 int vrev; | |
3021 | 38 int eos; |
2731 | 39 } OGGStreamContext; |
40 | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3593
diff
changeset
|
41 static void ogg_update_checksum(AVFormatContext *s, int64_t crc_offset) |
2731 | 42 { |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3593
diff
changeset
|
43 int64_t pos = url_ftell(s->pb); |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
44 uint32_t checksum = get_checksum(s->pb); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
45 url_fseek(s->pb, crc_offset, SEEK_SET); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
46 put_be32(s->pb, checksum); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
47 url_fseek(s->pb, pos, SEEK_SET); |
2731 | 48 } |
49 | |
50 static int ogg_write_page(AVFormatContext *s, const uint8_t *data, int size, | |
51 int64_t granule, int stream_index, int flags) | |
52 { | |
53 OGGStreamContext *oggstream = s->streams[stream_index]->priv_data; | |
3973
549a09cf23fe
Remove offset_t typedef and use int64_t directly instead.
diego
parents:
3593
diff
changeset
|
54 int64_t crc_offset; |
2731 | 55 int page_segments, i; |
56 | |
3020
38777f77320e
it seems ogg requires granule to be -1 on unfinished packets
bcoudurier
parents:
2771
diff
changeset
|
57 if (size >= 255*255) { |
38777f77320e
it seems ogg requires granule to be -1 on unfinished packets
bcoudurier
parents:
2771
diff
changeset
|
58 granule = -1; |
38777f77320e
it seems ogg requires granule to be -1 on unfinished packets
bcoudurier
parents:
2771
diff
changeset
|
59 size = 255*255; |
3021 | 60 } else if (oggstream->eos) |
61 flags |= 4; | |
3020
38777f77320e
it seems ogg requires granule to be -1 on unfinished packets
bcoudurier
parents:
2771
diff
changeset
|
62 |
2731 | 63 page_segments = FFMIN((size/255)+!!size, 255); |
64 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
65 init_checksum(s->pb, ff_crc04C11DB7_update, 0); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
66 put_tag(s->pb, "OggS"); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
67 put_byte(s->pb, 0); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
68 put_byte(s->pb, flags); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
69 put_le64(s->pb, granule); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
70 put_le32(s->pb, stream_index); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
71 put_le32(s->pb, oggstream->page_counter++); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
72 crc_offset = url_ftell(s->pb); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
73 put_le32(s->pb, 0); // crc |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
74 put_byte(s->pb, page_segments); |
2731 | 75 for (i = 0; i < page_segments-1; i++) |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
76 put_byte(s->pb, 255); |
2731 | 77 if (size) { |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
78 put_byte(s->pb, size - (page_segments-1)*255); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
79 put_buffer(s->pb, data, size); |
2731 | 80 } |
81 ogg_update_checksum(s, crc_offset); | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2731
diff
changeset
|
82 put_flush_packet(s->pb); |
2731 | 83 return size; |
84 } | |
85 | |
4578 | 86 static int ogg_build_flac_headers(AVCodecContext *avctx, |
2731 | 87 OGGStreamContext *oggstream, int bitexact) |
88 { | |
89 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT; | |
4578 | 90 enum FLACExtradataFormat format; |
91 uint8_t *streaminfo; | |
2731 | 92 uint8_t *p; |
4578 | 93 if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo)) |
2731 | 94 return -1; |
3188 | 95 oggstream->header_len[0] = 51; |
96 oggstream->header[0] = av_mallocz(51); // per ogg flac specs | |
2731 | 97 p = oggstream->header[0]; |
5053 | 98 if (!p) |
99 return AVERROR_NOMEM; | |
2731 | 100 bytestream_put_byte(&p, 0x7F); |
101 bytestream_put_buffer(&p, "FLAC", 4); | |
102 bytestream_put_byte(&p, 1); // major version | |
103 bytestream_put_byte(&p, 0); // minor version | |
104 bytestream_put_be16(&p, 1); // headers packets without this one | |
105 bytestream_put_buffer(&p, "fLaC", 4); | |
106 bytestream_put_byte(&p, 0x00); // streaminfo | |
107 bytestream_put_be24(&p, 34); | |
4578 | 108 bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE); |
2731 | 109 oggstream->header_len[1] = 1+3+4+strlen(vendor)+4; |
110 oggstream->header[1] = av_mallocz(oggstream->header_len[1]); | |
111 p = oggstream->header[1]; | |
5053 | 112 if (!p) |
113 return AVERROR_NOMEM; | |
2731 | 114 bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment |
115 bytestream_put_be24(&p, oggstream->header_len[1] - 4); | |
116 bytestream_put_le32(&p, strlen(vendor)); | |
117 bytestream_put_buffer(&p, vendor, strlen(vendor)); | |
118 bytestream_put_le32(&p, 0); // user comment list length | |
119 return 0; | |
120 } | |
121 | |
122 static int ogg_write_header(AVFormatContext *s) | |
123 { | |
124 OGGStreamContext *oggstream; | |
125 int i, j; | |
126 for (i = 0; i < s->nb_streams; i++) { | |
127 AVStream *st = s->streams[i]; | |
128 if (st->codec->codec_type == CODEC_TYPE_AUDIO) | |
129 av_set_pts_info(st, 64, 1, st->codec->sample_rate); | |
130 else if (st->codec->codec_type == CODEC_TYPE_VIDEO) | |
131 av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den); | |
132 if (st->codec->codec_id != CODEC_ID_VORBIS && | |
133 st->codec->codec_id != CODEC_ID_THEORA && | |
134 st->codec->codec_id != CODEC_ID_FLAC) { | |
135 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i); | |
136 return -1; | |
137 } | |
138 | |
139 if (!st->codec->extradata || !st->codec->extradata_size) { | |
140 av_log(s, AV_LOG_ERROR, "No extradata present\n"); | |
141 return -1; | |
142 } | |
143 oggstream = av_mallocz(sizeof(*oggstream)); | |
144 st->priv_data = oggstream; | |
145 if (st->codec->codec_id == CODEC_ID_FLAC) { | |
4578 | 146 if (ogg_build_flac_headers(st->codec, |
2731 | 147 oggstream, st->codec->flags & CODEC_FLAG_BITEXACT) < 0) { |
148 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n"); | |
149 av_freep(&st->priv_data); | |
150 } | |
151 } else { | |
152 if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size, | |
153 st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42, | |
154 oggstream->header, oggstream->header_len) < 0) { | |
155 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n"); | |
156 av_freep(&st->priv_data); | |
157 return -1; | |
158 } | |
159 if (st->codec->codec_id == CODEC_ID_THEORA) { | |
160 /** KFGSHIFT is the width of the less significant section of the granule position | |
161 The less significant section is the frame count since the last keyframe */ | |
162 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5); | |
163 oggstream->vrev = oggstream->header[0][9]; | |
164 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n", | |
165 oggstream->kfgshift, oggstream->vrev); | |
166 } | |
167 } | |
168 } | |
169 for (i = 0; i < 3; i++) { | |
170 for (j = 0; j < s->nb_streams; j++) { | |
171 AVStream *st = s->streams[j]; | |
172 OGGStreamContext *oggstream = st->priv_data; | |
173 if (oggstream && oggstream->header_len[i]) { | |
174 ogg_write_page(s, oggstream->header[i], oggstream->header_len[i], | |
175 0, st->index, i ? 0 : 2); // bos | |
176 } | |
177 } | |
178 } | |
179 return 0; | |
180 } | |
181 | |
182 static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt) | |
183 { | |
184 AVStream *st = s->streams[pkt->stream_index]; | |
185 OGGStreamContext *oggstream = st->priv_data; | |
186 uint8_t *ptr = pkt->data; | |
187 int ret, size = pkt->size; | |
188 int64_t granule; | |
189 | |
190 if (st->codec->codec_id == CODEC_ID_THEORA) { | |
191 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration; | |
192 int pframe_count; | |
193 if (pkt->flags & PKT_FLAG_KEY) | |
194 oggstream->last_kf_pts = pts; | |
195 pframe_count = pts - oggstream->last_kf_pts; | |
196 // prevent frame count from overflow if key frame flag is not set | |
197 if (pframe_count >= (1<<oggstream->kfgshift)) { | |
198 oggstream->last_kf_pts += pframe_count; | |
199 pframe_count = 0; | |
200 } | |
201 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count; | |
202 } else | |
203 granule = pkt->pts + pkt->duration; | |
204 oggstream->duration = granule; | |
205 do { | |
206 ret = ogg_write_page(s, ptr, size, granule, pkt->stream_index, ptr != pkt->data); | |
207 ptr += ret; size -= ret; | |
208 } while (size > 0 || ret == 255*255); // need to output a last nil page | |
209 | |
210 return 0; | |
211 } | |
212 | |
4310 | 213 static int ogg_compare_granule(AVFormatContext *s, AVPacket *next, AVPacket *pkt) |
214 { | |
215 AVStream *st2 = s->streams[next->stream_index]; | |
216 AVStream *st = s->streams[pkt ->stream_index]; | |
217 | |
218 int64_t next_granule = av_rescale_q(next->pts + next->duration, | |
219 st2->time_base, AV_TIME_BASE_Q); | |
220 int64_t cur_granule = av_rescale_q(pkt ->pts + pkt ->duration, | |
221 st ->time_base, AV_TIME_BASE_Q); | |
222 return next_granule > cur_granule; | |
223 } | |
224 | |
4311 | 225 static int ogg_interleave_per_granule(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) |
3021 | 226 { |
4310 | 227 AVPacketList *pktl; |
3021 | 228 int stream_count = 0; |
229 int streams[MAX_STREAMS] = {0}; | |
230 int interleaved = 0; | |
231 | |
232 if (pkt) { | |
4310 | 233 ff_interleave_add_packet(s, pkt, ogg_compare_granule); |
3021 | 234 } |
235 | |
236 pktl = s->packet_buffer; | |
237 while (pktl) { | |
238 if (streams[pktl->pkt.stream_index] == 0) | |
239 stream_count++; | |
240 streams[pktl->pkt.stream_index]++; | |
241 // need to buffer at least one packet to set eos flag | |
242 if (streams[pktl->pkt.stream_index] == 2) | |
243 interleaved++; | |
244 pktl = pktl->next; | |
245 } | |
246 | |
247 if ((s->nb_streams == stream_count && interleaved == stream_count) || | |
248 (flush && stream_count)) { | |
249 pktl= s->packet_buffer; | |
250 *out= pktl->pkt; | |
251 s->packet_buffer = pktl->next; | |
252 if (flush && streams[out->stream_index] == 1) { | |
253 OGGStreamContext *ogg = s->streams[out->stream_index]->priv_data; | |
254 ogg->eos = 1; | |
255 } | |
256 av_freep(&pktl); | |
257 return 1; | |
258 } else { | |
259 av_init_packet(out); | |
260 return 0; | |
261 } | |
262 } | |
2731 | 263 |
264 static int ogg_write_trailer(AVFormatContext *s) | |
265 { | |
266 int i; | |
267 for (i = 0; i < s->nb_streams; i++) { | |
268 AVStream *st = s->streams[i]; | |
269 OGGStreamContext *oggstream = st->priv_data; | |
270 if (st->codec->codec_id == CODEC_ID_FLAC) { | |
271 av_free(oggstream->header[0]); | |
272 av_free(oggstream->header[1]); | |
273 } | |
274 av_freep(&st->priv_data); | |
275 } | |
276 return 0; | |
277 } | |
278 | |
279 AVOutputFormat ogg_muxer = { | |
280 "ogg", | |
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3286
diff
changeset
|
281 NULL_IF_CONFIG_SMALL("Ogg"), |
2731 | 282 "application/ogg", |
3593 | 283 "ogg,ogv", |
2731 | 284 0, |
285 CODEC_ID_FLAC, | |
286 CODEC_ID_THEORA, | |
287 ogg_write_header, | |
288 ogg_write_packet, | |
289 ogg_write_trailer, | |
3021 | 290 .interleave_packet = ogg_interleave_per_granule, |
2731 | 291 }; |