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