Mercurial > libavformat.hg
annotate oggenc.c @ 5452:075674a33641 libavformat
Debug av_log() about stream probing from ffmbc.
author | michael |
---|---|
date | Sun, 13 Dec 2009 22:15:51 +0000 |
parents | 4292c6f121dc |
children | 0e84f356ca5f |
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 | |
5280 | 86 static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact, |
87 int *header_len) | |
88 { | |
89 const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT; | |
90 int size; | |
91 uint8_t *p, *p0; | |
92 | |
93 size = offset + 4 + strlen(vendor) + 4; | |
94 p = av_mallocz(size); | |
95 if (!p) | |
96 return NULL; | |
97 p0 = p; | |
98 | |
99 p += offset; | |
100 bytestream_put_le32(&p, strlen(vendor)); | |
101 bytestream_put_buffer(&p, vendor, strlen(vendor)); | |
102 bytestream_put_le32(&p, 0); // user comment list length | |
103 | |
104 *header_len = size; | |
105 return p0; | |
106 } | |
107 | |
4578 | 108 static int ogg_build_flac_headers(AVCodecContext *avctx, |
2731 | 109 OGGStreamContext *oggstream, int bitexact) |
110 { | |
4578 | 111 enum FLACExtradataFormat format; |
112 uint8_t *streaminfo; | |
2731 | 113 uint8_t *p; |
5054 | 114 |
4578 | 115 if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo)) |
2731 | 116 return -1; |
5054 | 117 |
118 // first packet: STREAMINFO | |
3188 | 119 oggstream->header_len[0] = 51; |
120 oggstream->header[0] = av_mallocz(51); // per ogg flac specs | |
2731 | 121 p = oggstream->header[0]; |
5053 | 122 if (!p) |
123 return AVERROR_NOMEM; | |
2731 | 124 bytestream_put_byte(&p, 0x7F); |
125 bytestream_put_buffer(&p, "FLAC", 4); | |
126 bytestream_put_byte(&p, 1); // major version | |
127 bytestream_put_byte(&p, 0); // minor version | |
128 bytestream_put_be16(&p, 1); // headers packets without this one | |
129 bytestream_put_buffer(&p, "fLaC", 4); | |
130 bytestream_put_byte(&p, 0x00); // streaminfo | |
131 bytestream_put_be24(&p, 34); | |
4578 | 132 bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE); |
5054 | 133 |
134 // second packet: VorbisComment | |
5280 | 135 p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1]); |
5053 | 136 if (!p) |
137 return AVERROR_NOMEM; | |
5280 | 138 oggstream->header[1] = p; |
2731 | 139 bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment |
140 bytestream_put_be24(&p, oggstream->header_len[1] - 4); | |
5280 | 141 |
142 return 0; | |
143 } | |
144 | |
145 #define SPEEX_HEADER_SIZE 80 | |
146 | |
147 static int ogg_build_speex_headers(AVCodecContext *avctx, | |
148 OGGStreamContext *oggstream, int bitexact) | |
149 { | |
150 uint8_t *p; | |
151 | |
152 if (avctx->extradata_size < SPEEX_HEADER_SIZE) | |
153 return -1; | |
154 | |
155 // first packet: Speex header | |
156 p = av_mallocz(SPEEX_HEADER_SIZE); | |
157 if (!p) | |
158 return AVERROR_NOMEM; | |
159 oggstream->header[0] = p; | |
160 oggstream->header_len[0] = SPEEX_HEADER_SIZE; | |
161 bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE); | |
162 AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0 | |
163 | |
164 // second packet: VorbisComment | |
165 p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1]); | |
166 if (!p) | |
167 return AVERROR_NOMEM; | |
168 oggstream->header[1] = p; | |
5054 | 169 |
2731 | 170 return 0; |
171 } | |
172 | |
173 static int ogg_write_header(AVFormatContext *s) | |
174 { | |
175 OGGStreamContext *oggstream; | |
176 int i, j; | |
177 for (i = 0; i < s->nb_streams; i++) { | |
178 AVStream *st = s->streams[i]; | |
179 if (st->codec->codec_type == CODEC_TYPE_AUDIO) | |
180 av_set_pts_info(st, 64, 1, st->codec->sample_rate); | |
181 else if (st->codec->codec_type == CODEC_TYPE_VIDEO) | |
182 av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den); | |
183 if (st->codec->codec_id != CODEC_ID_VORBIS && | |
184 st->codec->codec_id != CODEC_ID_THEORA && | |
5280 | 185 st->codec->codec_id != CODEC_ID_SPEEX && |
2731 | 186 st->codec->codec_id != CODEC_ID_FLAC) { |
187 av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i); | |
188 return -1; | |
189 } | |
190 | |
191 if (!st->codec->extradata || !st->codec->extradata_size) { | |
192 av_log(s, AV_LOG_ERROR, "No extradata present\n"); | |
193 return -1; | |
194 } | |
195 oggstream = av_mallocz(sizeof(*oggstream)); | |
196 st->priv_data = oggstream; | |
197 if (st->codec->codec_id == CODEC_ID_FLAC) { | |
5055
5a6bbdd352fb
oggenc: return error value from ogg_build_flac_headers()
jbr
parents:
5054
diff
changeset
|
198 int err = ogg_build_flac_headers(st->codec, oggstream, |
5a6bbdd352fb
oggenc: return error value from ogg_build_flac_headers()
jbr
parents:
5054
diff
changeset
|
199 st->codec->flags & CODEC_FLAG_BITEXACT); |
5a6bbdd352fb
oggenc: return error value from ogg_build_flac_headers()
jbr
parents:
5054
diff
changeset
|
200 if (err) { |
5056
f67fad61077e
oggenc: Change error log text. An error here does not necessarily mean
jbr
parents:
5055
diff
changeset
|
201 av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n"); |
2731 | 202 av_freep(&st->priv_data); |
5055
5a6bbdd352fb
oggenc: return error value from ogg_build_flac_headers()
jbr
parents:
5054
diff
changeset
|
203 return err; |
2731 | 204 } |
5280 | 205 } else if (st->codec->codec_id == CODEC_ID_SPEEX) { |
206 int err = ogg_build_speex_headers(st->codec, oggstream, | |
207 st->codec->flags & CODEC_FLAG_BITEXACT); | |
208 if (err) { | |
209 av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n"); | |
210 av_freep(&st->priv_data); | |
211 return err; | |
212 } | |
2731 | 213 } else { |
214 if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size, | |
215 st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42, | |
216 oggstream->header, oggstream->header_len) < 0) { | |
217 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n"); | |
218 av_freep(&st->priv_data); | |
219 return -1; | |
220 } | |
221 if (st->codec->codec_id == CODEC_ID_THEORA) { | |
222 /** KFGSHIFT is the width of the less significant section of the granule position | |
223 The less significant section is the frame count since the last keyframe */ | |
224 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5); | |
225 oggstream->vrev = oggstream->header[0][9]; | |
226 av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n", | |
227 oggstream->kfgshift, oggstream->vrev); | |
228 } | |
229 } | |
230 } | |
231 for (i = 0; i < 3; i++) { | |
232 for (j = 0; j < s->nb_streams; j++) { | |
233 AVStream *st = s->streams[j]; | |
234 OGGStreamContext *oggstream = st->priv_data; | |
235 if (oggstream && oggstream->header_len[i]) { | |
236 ogg_write_page(s, oggstream->header[i], oggstream->header_len[i], | |
237 0, st->index, i ? 0 : 2); // bos | |
238 } | |
239 } | |
240 } | |
241 return 0; | |
242 } | |
243 | |
244 static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt) | |
245 { | |
246 AVStream *st = s->streams[pkt->stream_index]; | |
247 OGGStreamContext *oggstream = st->priv_data; | |
248 uint8_t *ptr = pkt->data; | |
249 int ret, size = pkt->size; | |
250 int64_t granule; | |
251 | |
252 if (st->codec->codec_id == CODEC_ID_THEORA) { | |
253 int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration; | |
254 int pframe_count; | |
255 if (pkt->flags & PKT_FLAG_KEY) | |
256 oggstream->last_kf_pts = pts; | |
257 pframe_count = pts - oggstream->last_kf_pts; | |
258 // prevent frame count from overflow if key frame flag is not set | |
259 if (pframe_count >= (1<<oggstream->kfgshift)) { | |
260 oggstream->last_kf_pts += pframe_count; | |
261 pframe_count = 0; | |
262 } | |
263 granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count; | |
264 } else | |
265 granule = pkt->pts + pkt->duration; | |
266 oggstream->duration = granule; | |
267 do { | |
268 ret = ogg_write_page(s, ptr, size, granule, pkt->stream_index, ptr != pkt->data); | |
269 ptr += ret; size -= ret; | |
270 } while (size > 0 || ret == 255*255); // need to output a last nil page | |
271 | |
272 return 0; | |
273 } | |
274 | |
4310 | 275 static int ogg_compare_granule(AVFormatContext *s, AVPacket *next, AVPacket *pkt) |
276 { | |
277 AVStream *st2 = s->streams[next->stream_index]; | |
278 AVStream *st = s->streams[pkt ->stream_index]; | |
279 | |
280 int64_t next_granule = av_rescale_q(next->pts + next->duration, | |
281 st2->time_base, AV_TIME_BASE_Q); | |
282 int64_t cur_granule = av_rescale_q(pkt ->pts + pkt ->duration, | |
283 st ->time_base, AV_TIME_BASE_Q); | |
284 return next_granule > cur_granule; | |
285 } | |
286 | |
4311 | 287 static int ogg_interleave_per_granule(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) |
3021 | 288 { |
4310 | 289 AVPacketList *pktl; |
3021 | 290 int stream_count = 0; |
291 int streams[MAX_STREAMS] = {0}; | |
292 int interleaved = 0; | |
293 | |
294 if (pkt) { | |
4310 | 295 ff_interleave_add_packet(s, pkt, ogg_compare_granule); |
3021 | 296 } |
297 | |
298 pktl = s->packet_buffer; | |
299 while (pktl) { | |
300 if (streams[pktl->pkt.stream_index] == 0) | |
301 stream_count++; | |
302 streams[pktl->pkt.stream_index]++; | |
303 // need to buffer at least one packet to set eos flag | |
304 if (streams[pktl->pkt.stream_index] == 2) | |
305 interleaved++; | |
306 pktl = pktl->next; | |
307 } | |
308 | |
309 if ((s->nb_streams == stream_count && interleaved == stream_count) || | |
310 (flush && stream_count)) { | |
311 pktl= s->packet_buffer; | |
312 *out= pktl->pkt; | |
313 s->packet_buffer = pktl->next; | |
314 if (flush && streams[out->stream_index] == 1) { | |
315 OGGStreamContext *ogg = s->streams[out->stream_index]->priv_data; | |
316 ogg->eos = 1; | |
317 } | |
5210
36d130853c9b
Improve amortized worst case speed of the muxers packet interleaving code
michael
parents:
5056
diff
changeset
|
318 if(!s->packet_buffer) |
36d130853c9b
Improve amortized worst case speed of the muxers packet interleaving code
michael
parents:
5056
diff
changeset
|
319 s->packet_buffer_end= NULL; |
36d130853c9b
Improve amortized worst case speed of the muxers packet interleaving code
michael
parents:
5056
diff
changeset
|
320 |
36d130853c9b
Improve amortized worst case speed of the muxers packet interleaving code
michael
parents:
5056
diff
changeset
|
321 if(s->streams[out->stream_index]->last_in_packet_buffer == pktl) |
36d130853c9b
Improve amortized worst case speed of the muxers packet interleaving code
michael
parents:
5056
diff
changeset
|
322 s->streams[out->stream_index]->last_in_packet_buffer= NULL; |
36d130853c9b
Improve amortized worst case speed of the muxers packet interleaving code
michael
parents:
5056
diff
changeset
|
323 |
3021 | 324 av_freep(&pktl); |
325 return 1; | |
326 } else { | |
327 av_init_packet(out); | |
328 return 0; | |
329 } | |
330 } | |
2731 | 331 |
332 static int ogg_write_trailer(AVFormatContext *s) | |
333 { | |
334 int i; | |
335 for (i = 0; i < s->nb_streams; i++) { | |
336 AVStream *st = s->streams[i]; | |
337 OGGStreamContext *oggstream = st->priv_data; | |
5280 | 338 if (st->codec->codec_id == CODEC_ID_FLAC || |
339 st->codec->codec_id == CODEC_ID_SPEEX) { | |
2731 | 340 av_free(oggstream->header[0]); |
341 av_free(oggstream->header[1]); | |
342 } | |
343 av_freep(&st->priv_data); | |
344 } | |
345 return 0; | |
346 } | |
347 | |
348 AVOutputFormat ogg_muxer = { | |
349 "ogg", | |
3424
7a0230981402
Make long_names in lavf/lavdev optional depending on CONFIG_SMALL.
diego
parents:
3286
diff
changeset
|
350 NULL_IF_CONFIG_SMALL("Ogg"), |
2731 | 351 "application/ogg", |
5280 | 352 "ogg,ogv,spx", |
2731 | 353 0, |
354 CODEC_ID_FLAC, | |
355 CODEC_ID_THEORA, | |
356 ogg_write_header, | |
357 ogg_write_packet, | |
358 ogg_write_trailer, | |
3021 | 359 .interleave_packet = ogg_interleave_per_granule, |
2731 | 360 }; |