Mercurial > libavformat.hg
annotate ogg.c @ 662:549e594a13c2 libavformat
support colon-separated rates patch by Roine Gustafsson <roine AT users DOT sourceforge DOT net>
author | mmu_man |
---|---|
date | Thu, 27 Jan 2005 16:11:31 +0000 |
parents | 0b52743104ac |
children | 17178af951b4 |
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 | |
7 */ | |
8 | |
9 #include <stdio.h> | |
10 | |
11 #include <ogg/ogg.h> | |
12 | |
13 #include "avformat.h" | |
14 | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
15 #undef NDEBUG |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
16 #include <assert.h> |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
17 |
0 | 18 #define DECODER_BUFFER_SIZE 4096 |
19 | |
20 | |
21 typedef struct OggContext { | |
22 /* output */ | |
23 ogg_stream_state os ; | |
24 int header_handled ; | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
25 ogg_packet op; |
0 | 26 |
27 /* input */ | |
28 ogg_sync_state oy ; | |
29 } OggContext ; | |
30 | |
31 | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
68
diff
changeset
|
32 #ifdef CONFIG_ENCODERS |
35 | 33 static int ogg_write_header(AVFormatContext *avfcontext) |
34 { | |
35 OggContext *context = avfcontext->priv_data; | |
407
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
36 ogg_packet *op= &context->op; |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
37 int n, i; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
38 |
404 | 39 ogg_stream_init(&context->os, 31415); |
0 | 40 |
41 for(n = 0 ; n < avfcontext->nb_streams ; n++) { | |
407
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
42 AVCodecContext *codec = &avfcontext->streams[n]->codec; |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
43 uint8_t *p= codec->extradata; |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
44 |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
454
diff
changeset
|
45 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
|
46 |
407
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
47 for(i=0; i < codec->extradata_size; i+= op->bytes){ |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
48 op->bytes = p[i++]<<8; |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
49 op->bytes+= p[i++]; |
0 | 50 |
407
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
51 op->packet= &p[i]; |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
52 op->b_o_s= op->packetno==0; |
0 | 53 |
407
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
54 ogg_stream_packetin(&context->os, op); |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
55 |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
56 op->packetno++; //FIXME multiple streams |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
57 } |
0 | 58 |
59 context->header_handled = 0 ; | |
60 } | |
61 | |
62 return 0 ; | |
63 } | |
64 | |
468 | 65 static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt) |
0 | 66 { |
67 OggContext *context = avfcontext->priv_data ; | |
468 | 68 AVCodecContext *avctx= &avfcontext->streams[pkt->stream_index]->codec; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
69 ogg_packet *op= &context->op; |
0 | 70 ogg_page og ; |
468 | 71 int64_t pts; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
72 |
468 | 73 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
|
74 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
75 // 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
|
76 |
0 | 77 /* flush header packets so audio starts on a new page */ |
78 | |
79 if(!context->header_handled) { | |
80 while(ogg_stream_flush(&context->os, &og)) { | |
81 put_buffer(&avfcontext->pb, og.header, og.header_len) ; | |
82 put_buffer(&avfcontext->pb, og.body, og.body_len) ; | |
83 put_flush_packet(&avfcontext->pb); | |
84 } | |
85 context->header_handled = 1 ; | |
86 } | |
87 | |
468 | 88 op->packet = (uint8_t*) pkt->data; |
89 op->bytes = pkt->size; | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
90 op->b_o_s = op->packetno == 0; |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
91 op->granulepos= pts; |
0 | 92 |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
93 /* correct the fields in the packet -- essential for streaming */ |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
94 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
95 ogg_stream_packetin(&context->os, op); |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
96 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
97 while(ogg_stream_pageout(&context->os, &og)) { |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
98 put_buffer(&avfcontext->pb, og.header, og.header_len); |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
99 put_buffer(&avfcontext->pb, og.body, og.body_len); |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
100 put_flush_packet(&avfcontext->pb); |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
101 } |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
102 op->packetno++; |
0 | 103 |
104 return 0; | |
105 } | |
106 | |
107 | |
108 static int ogg_write_trailer(AVFormatContext *avfcontext) { | |
109 OggContext *context = avfcontext->priv_data ; | |
110 ogg_page og ; | |
111 | |
112 while(ogg_stream_flush(&context->os, &og)) { | |
113 put_buffer(&avfcontext->pb, og.header, og.header_len) ; | |
114 put_buffer(&avfcontext->pb, og.body, og.body_len) ; | |
115 put_flush_packet(&avfcontext->pb); | |
116 } | |
117 | |
118 ogg_stream_clear(&context->os) ; | |
119 return 0 ; | |
120 } | |
121 | |
122 | |
123 static AVOutputFormat ogg_oformat = { | |
124 "ogg", | |
125 "Ogg Vorbis", | |
126 "audio/x-vorbis", | |
127 "ogg", | |
128 sizeof(OggContext), | |
129 CODEC_ID_VORBIS, | |
130 0, | |
131 ogg_write_header, | |
132 ogg_write_packet, | |
133 ogg_write_trailer, | |
134 } ; | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
68
diff
changeset
|
135 #endif //CONFIG_ENCODERS |
0 | 136 |
137 | |
138 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) { | |
139 OggContext *context = avfcontext->priv_data ; | |
140 ogg_page og ; | |
141 char *buf ; | |
142 | |
143 while(ogg_stream_packetout(&context->os, op) != 1) { | |
144 | |
145 /* while no pages are available, read in more data to the sync */ | |
146 while(ogg_sync_pageout(&context->oy, &og) != 1) { | |
147 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ; | |
148 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0) | |
149 return 1 ; | |
150 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; | |
151 } | |
152 | |
153 /* got a page. Feed it into the stream and get the packet */ | |
154 if(ogg_stream_pagein(&context->os, &og) != 0) | |
155 return 1 ; | |
156 } | |
157 | |
158 return 0 ; | |
159 } | |
160 | |
161 | |
162 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap) | |
163 { | |
35 | 164 OggContext *context = avfcontext->priv_data; |
408 | 165 ogg_packet op ; |
0 | 166 char *buf ; |
167 ogg_page og ; | |
168 AVStream *ast ; | |
408 | 169 AVCodecContext *codec; |
170 uint8_t *p; | |
171 int i; | |
404 | 172 |
0 | 173 ogg_sync_init(&context->oy) ; |
174 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ; | |
175 | |
176 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0) | |
482 | 177 return AVERROR_IO ; |
0 | 178 |
179 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; | |
180 ogg_sync_pageout(&context->oy, &og) ; | |
181 ogg_stream_init(&context->os, ogg_page_serialno(&og)) ; | |
182 ogg_stream_pagein(&context->os, &og) ; | |
408 | 183 |
0 | 184 /* currently only one vorbis stream supported */ |
185 | |
186 ast = av_new_stream(avfcontext, 0) ; | |
187 if(!ast) | |
188 return AVERROR_NOMEM ; | |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
454
diff
changeset
|
189 av_set_pts_info(ast, 60, 1, AV_TIME_BASE); |
0 | 190 |
408 | 191 codec= &ast->codec; |
192 codec->codec_type = CODEC_TYPE_AUDIO; | |
193 codec->codec_id = CODEC_ID_VORBIS; | |
194 for(i=0; i<3; i++){ | |
195 if(next_packet(avfcontext, &op)){ | |
196 return -1; | |
197 } | |
639 | 198 if(op.bytes >= (1<<16) || op.bytes < 0) |
199 return -1; | |
408 | 200 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
|
201 codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); |
408 | 202 p= codec->extradata + codec->extradata_size - 2 - op.bytes; |
203 *(p++)= op.bytes>>8; | |
204 *(p++)= op.bytes&0xFF; | |
205 memcpy(p, op.packet, op.bytes); | |
206 } | |
207 | |
0 | 208 return 0 ; |
209 } | |
210 | |
211 | |
212 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) { | |
213 ogg_packet op ; | |
214 | |
215 if(next_packet(avfcontext, &op)) | |
482 | 216 return AVERROR_IO ; |
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
217 if(av_new_packet(pkt, op.bytes) < 0) |
482 | 218 return AVERROR_IO ; |
0 | 219 pkt->stream_index = 0 ; |
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
220 memcpy(pkt->data, op.packet, op.bytes); |
454 | 221 if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1) |
222 pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate); | |
223 // printf("%lld %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate); | |
0 | 224 |
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
225 return op.bytes; |
0 | 226 } |
227 | |
228 | |
229 static int ogg_read_close(AVFormatContext *avfcontext) { | |
230 OggContext *context = avfcontext->priv_data ; | |
231 | |
232 ogg_stream_clear(&context->os) ; | |
233 ogg_sync_clear(&context->oy) ; | |
408 | 234 av_freep(&avfcontext->streams[0]->codec.extradata); |
0 | 235 |
236 return 0 ; | |
237 } | |
238 | |
239 | |
240 static AVInputFormat ogg_iformat = { | |
241 "ogg", | |
242 "Ogg Vorbis", | |
243 sizeof(OggContext), | |
244 NULL, | |
245 ogg_read_header, | |
246 ogg_read_packet, | |
247 ogg_read_close, | |
248 .extensions = "ogg", | |
249 } ; | |
250 | |
251 | |
252 int ogg_init(void) { | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
68
diff
changeset
|
253 #ifdef CONFIG_ENCODERS |
0 | 254 av_register_output_format(&ogg_oformat) ; |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
68
diff
changeset
|
255 #endif |
0 | 256 av_register_input_format(&ogg_iformat); |
257 return 0 ; | |
258 } |