Mercurial > libavformat.hg
annotate ogg.c @ 1681:23c911d09382 libavformat
set codec_tag from codec_id if its not set and can be set based on the AVCodecTag tables
author | michael |
---|---|
date | Sun, 21 Jan 2007 12:31:58 +0000 |
parents | 404048ea11bc |
children | 6dd0da50623a |
rev | line source |
---|---|
0 | 1 /* |
2 * Ogg bitstream support | |
3 * Mark Hills <mark@pogo.org.uk> | |
4 * | |
5 * Uses libogg, but requires libvorbisenc to construct correct headers | |
6 * when containing Vorbis stream -- currently the only format supported | |
1306
8bf9be9bb107
Add official LGPL license headers to the files that were missing them.
diego
parents:
1169
diff
changeset
|
7 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1306
diff
changeset
|
8 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1306
diff
changeset
|
9 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1306
diff
changeset
|
10 * FFmpeg is free software; you can redistribute it and/or |
1306
8bf9be9bb107
Add official LGPL license headers to the files that were missing them.
diego
parents:
1169
diff
changeset
|
11 * modify it under the terms of the GNU Lesser General Public |
8bf9be9bb107
Add official LGPL license headers to the files that were missing them.
diego
parents:
1169
diff
changeset
|
12 * License as published by the Free Software Foundation; either |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1306
diff
changeset
|
13 * version 2.1 of the License, or (at your option) any later version. |
1306
8bf9be9bb107
Add official LGPL license headers to the files that were missing them.
diego
parents:
1169
diff
changeset
|
14 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1306
diff
changeset
|
15 * FFmpeg is distributed in the hope that it will be useful, |
1306
8bf9be9bb107
Add official LGPL license headers to the files that were missing them.
diego
parents:
1169
diff
changeset
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8bf9be9bb107
Add official LGPL license headers to the files that were missing them.
diego
parents:
1169
diff
changeset
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8bf9be9bb107
Add official LGPL license headers to the files that were missing them.
diego
parents:
1169
diff
changeset
|
18 * Lesser General Public License for more details. |
8bf9be9bb107
Add official LGPL license headers to the files that were missing them.
diego
parents:
1169
diff
changeset
|
19 * |
8bf9be9bb107
Add official LGPL license headers to the files that were missing them.
diego
parents:
1169
diff
changeset
|
20 * You should have received a copy of the GNU Lesser General Public |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1306
diff
changeset
|
21 * License along with FFmpeg; if not, write to the Free Software |
1306
8bf9be9bb107
Add official LGPL license headers to the files that were missing them.
diego
parents:
1169
diff
changeset
|
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 23 */ |
24 | |
25 #include <stdio.h> | |
26 | |
27 #include <ogg/ogg.h> | |
28 | |
29 #include "avformat.h" | |
30 | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
31 #undef NDEBUG |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
32 #include <assert.h> |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
33 |
0 | 34 #define DECODER_BUFFER_SIZE 4096 |
35 | |
36 | |
37 typedef struct OggContext { | |
38 /* output */ | |
39 ogg_stream_state os ; | |
40 int header_handled ; | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
41 ogg_packet op; |
0 | 42 |
43 /* input */ | |
44 ogg_sync_state oy ; | |
45 } OggContext ; | |
46 | |
47 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
857
diff
changeset
|
48 #ifdef CONFIG_MUXERS |
885 | 49 static int ogg_write_header(AVFormatContext *avfcontext) |
35 | 50 { |
51 OggContext *context = avfcontext->priv_data; | |
885 | 52 ogg_packet *op= &context->op; |
857 | 53 int n; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
54 |
404 | 55 ogg_stream_init(&context->os, 31415); |
885 | 56 |
0 | 57 for(n = 0 ; n < avfcontext->nb_streams ; n++) { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
726
diff
changeset
|
58 AVCodecContext *codec = avfcontext->streams[n]->codec; |
843 | 59 uint8_t *headers = codec->extradata; |
60 int headers_len = codec->extradata_size; | |
61 uint8_t *header_start[3]; | |
62 int header_len[3]; | |
857 | 63 int i, j; |
885 | 64 |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
454
diff
changeset
|
65 av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE); |
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
454
diff
changeset
|
66 |
843 | 67 for(j=1,i=0;i<2;++i, ++j) { |
68 header_len[i]=0; | |
69 while(j<headers_len && headers[j]==0xff) { | |
70 header_len[i]+=0xff; | |
71 ++j; | |
72 } | |
73 header_len[i]+=headers[j]; | |
74 } | |
75 header_len[2]=headers_len-header_len[0]-header_len[1]-j; | |
76 headers+=j; | |
77 header_start[0] = headers; | |
78 header_start[1] = header_start[0] + header_len[0]; | |
79 header_start[2] = header_start[1] + header_len[1]; | |
0 | 80 |
843 | 81 for(i=0; i < 3; ++i){ |
82 op->bytes = header_len[i]; | |
83 | |
84 op->packet= header_start[i]; | |
407
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
85 op->b_o_s= op->packetno==0; |
0 | 86 |
407
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
87 ogg_stream_packetin(&context->os, op); |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
88 |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
89 op->packetno++; //FIXME multiple streams |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
90 } |
0 | 91 |
887 | 92 context->header_handled = 0 ; |
0 | 93 } |
885 | 94 |
0 | 95 return 0 ; |
96 } | |
97 | |
468 | 98 static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt) |
0 | 99 { |
100 OggContext *context = avfcontext->priv_data ; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
726
diff
changeset
|
101 AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
102 ogg_packet *op= &context->op; |
0 | 103 ogg_page og ; |
468 | 104 int64_t pts; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
105 |
468 | 106 pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE); |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
107 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
108 // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size); |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
109 |
0 | 110 /* flush header packets so audio starts on a new page */ |
111 | |
112 if(!context->header_handled) { | |
887 | 113 while(ogg_stream_flush(&context->os, &og)) { |
114 put_buffer(&avfcontext->pb, og.header, og.header_len) ; | |
115 put_buffer(&avfcontext->pb, og.body, og.body_len) ; | |
116 put_flush_packet(&avfcontext->pb); | |
117 } | |
118 context->header_handled = 1 ; | |
0 | 119 } |
120 | |
468 | 121 op->packet = (uint8_t*) pkt->data; |
122 op->bytes = pkt->size; | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
123 op->b_o_s = op->packetno == 0; |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
124 op->granulepos= pts; |
0 | 125 |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
126 /* correct the fields in the packet -- essential for streaming */ |
885 | 127 |
128 ogg_stream_packetin(&context->os, op); | |
129 | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
130 while(ogg_stream_pageout(&context->os, &og)) { |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
131 put_buffer(&avfcontext->pb, og.header, og.header_len); |
887 | 132 put_buffer(&avfcontext->pb, og.body, og.body_len); |
133 put_flush_packet(&avfcontext->pb); | |
885 | 134 } |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
135 op->packetno++; |
0 | 136 |
137 return 0; | |
138 } | |
139 | |
140 | |
141 static int ogg_write_trailer(AVFormatContext *avfcontext) { | |
142 OggContext *context = avfcontext->priv_data ; | |
143 ogg_page og ; | |
144 | |
145 while(ogg_stream_flush(&context->os, &og)) { | |
887 | 146 put_buffer(&avfcontext->pb, og.header, og.header_len) ; |
147 put_buffer(&avfcontext->pb, og.body, og.body_len) ; | |
148 put_flush_packet(&avfcontext->pb); | |
0 | 149 } |
150 | |
151 ogg_stream_clear(&context->os) ; | |
152 return 0 ; | |
153 } | |
154 | |
155 | |
1169 | 156 AVOutputFormat ogg_muxer = { |
0 | 157 "ogg", |
158 "Ogg Vorbis", | |
159 "audio/x-vorbis", | |
160 "ogg", | |
161 sizeof(OggContext), | |
162 CODEC_ID_VORBIS, | |
163 0, | |
164 ogg_write_header, | |
165 ogg_write_packet, | |
166 ogg_write_trailer, | |
167 } ; | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
857
diff
changeset
|
168 #endif //CONFIG_MUXERS |
0 | 169 |
726
17178af951b4
Ogg demuxer ported from tcvp by Luca Barbato <lu_zero at gentoo dot org>,
mru
parents:
639
diff
changeset
|
170 #if 0 |
0 | 171 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) { |
172 OggContext *context = avfcontext->priv_data ; | |
173 ogg_page og ; | |
174 char *buf ; | |
175 | |
176 while(ogg_stream_packetout(&context->os, op) != 1) { | |
177 | |
887 | 178 /* while no pages are available, read in more data to the sync */ |
179 while(ogg_sync_pageout(&context->oy, &og) != 1) { | |
180 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ; | |
181 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0) | |
182 return 1 ; | |
183 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; | |
184 } | |
885 | 185 |
887 | 186 /* got a page. Feed it into the stream and get the packet */ |
187 if(ogg_stream_pagein(&context->os, &og) != 0) | |
188 return 1 ; | |
0 | 189 } |
190 | |
191 return 0 ; | |
192 } | |
193 | |
194 | |
195 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap) | |
196 { | |
35 | 197 OggContext *context = avfcontext->priv_data; |
885 | 198 ogg_packet op ; |
0 | 199 char *buf ; |
200 ogg_page og ; | |
201 AVStream *ast ; | |
408 | 202 AVCodecContext *codec; |
203 uint8_t *p; | |
204 int i; | |
885 | 205 |
0 | 206 ogg_sync_init(&context->oy) ; |
207 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ; | |
208 | |
209 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0) | |
887 | 210 return AVERROR_IO ; |
885 | 211 |
212 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; | |
0 | 213 ogg_sync_pageout(&context->oy, &og) ; |
214 ogg_stream_init(&context->os, ogg_page_serialno(&og)) ; | |
215 ogg_stream_pagein(&context->os, &og) ; | |
885 | 216 |
0 | 217 /* currently only one vorbis stream supported */ |
218 | |
219 ast = av_new_stream(avfcontext, 0) ; | |
220 if(!ast) | |
887 | 221 return AVERROR_NOMEM ; |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
454
diff
changeset
|
222 av_set_pts_info(ast, 60, 1, AV_TIME_BASE); |
0 | 223 |
408 | 224 codec= &ast->codec; |
225 codec->codec_type = CODEC_TYPE_AUDIO; | |
226 codec->codec_id = CODEC_ID_VORBIS; | |
227 for(i=0; i<3; i++){ | |
228 if(next_packet(avfcontext, &op)){ | |
229 return -1; | |
230 } | |
639 | 231 if(op.bytes >= (1<<16) || op.bytes < 0) |
232 return -1; | |
408 | 233 codec->extradata_size+= 2 + op.bytes; |
587
fe24632a577b
allocate a few bytes more for extradata so the bitstream reader if its used by the codec for parsing extardata, doesnt read over the end
michael
parents:
486
diff
changeset
|
234 codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); |
884
2ece9c9dd94c
malloc padding to avoid reading past the malloc()ed area.
henry
parents:
858
diff
changeset
|
235 memset(codec->extradata + codec->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
408 | 236 p= codec->extradata + codec->extradata_size - 2 - op.bytes; |
237 *(p++)= op.bytes>>8; | |
238 *(p++)= op.bytes&0xFF; | |
239 memcpy(p, op.packet, op.bytes); | |
240 } | |
241 | |
0 | 242 return 0 ; |
243 } | |
244 | |
245 | |
246 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) { | |
247 ogg_packet op ; | |
248 | |
885 | 249 if(next_packet(avfcontext, &op)) |
887 | 250 return AVERROR_IO ; |
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
251 if(av_new_packet(pkt, op.bytes) < 0) |
887 | 252 return AVERROR_IO ; |
0 | 253 pkt->stream_index = 0 ; |
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
254 memcpy(pkt->data, op.packet, op.bytes); |
454 | 255 if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1) |
256 pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate); | |
1443
404048ea11bc
Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents:
1358
diff
changeset
|
257 // printf("%"PRId64" %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate); |
0 | 258 |
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
259 return op.bytes; |
0 | 260 } |
261 | |
262 | |
263 static int ogg_read_close(AVFormatContext *avfcontext) { | |
264 OggContext *context = avfcontext->priv_data ; | |
265 | |
266 ogg_stream_clear(&context->os) ; | |
267 ogg_sync_clear(&context->oy) ; | |
268 | |
269 return 0 ; | |
270 } | |
271 | |
272 | |
1169 | 273 static AVInputFormat ogg_iformat = { |
0 | 274 "ogg", |
275 "Ogg Vorbis", | |
276 sizeof(OggContext), | |
277 NULL, | |
278 ogg_read_header, | |
279 ogg_read_packet, | |
280 ogg_read_close, | |
281 .extensions = "ogg", | |
282 } ; | |
726
17178af951b4
Ogg demuxer ported from tcvp by Luca Barbato <lu_zero at gentoo dot org>,
mru
parents:
639
diff
changeset
|
283 #endif |