Mercurial > libavformat.hg
annotate ogg.c @ 439:ef46119f67d1 libavformat
10l
author | michael |
---|---|
date | Mon, 12 Apr 2004 18:32:36 +0000 |
parents | b6949d80b7a9 |
children | 7fa377b2f533 |
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 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
39 av_set_pts_info(avfcontext, 60, 1, AV_TIME_BASE); |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
40 |
404 | 41 ogg_stream_init(&context->os, 31415); |
0 | 42 |
43 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
|
44 AVCodecContext *codec = &avfcontext->streams[n]->codec; |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
45 uint8_t *p= codec->extradata; |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
46 |
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 | |
65 static int ogg_write_packet(AVFormatContext *avfcontext, | |
66 int stream_index, | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
67 const uint8_t *buf, int size, int64_t pts) |
0 | 68 { |
69 OggContext *context = avfcontext->priv_data ; | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
70 AVCodecContext *avctx= &avfcontext->streams[stream_index]->codec; |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
71 ogg_packet *op= &context->op; |
0 | 72 ogg_page og ; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
73 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
74 pts= av_rescale(pts, avctx->sample_rate, AV_TIME_BASE); |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
75 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
76 // 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
|
77 |
0 | 78 /* flush header packets so audio starts on a new page */ |
79 | |
80 if(!context->header_handled) { | |
81 while(ogg_stream_flush(&context->os, &og)) { | |
82 put_buffer(&avfcontext->pb, og.header, og.header_len) ; | |
83 put_buffer(&avfcontext->pb, og.body, og.body_len) ; | |
84 put_flush_packet(&avfcontext->pb); | |
85 } | |
86 context->header_handled = 1 ; | |
87 } | |
88 | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
89 op->packet = (uint8_t*) buf; |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
90 op->bytes = size; |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
91 op->b_o_s = op->packetno == 0; |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
92 op->granulepos= pts; |
0 | 93 |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
94 /* correct the fields in the packet -- essential for streaming */ |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
95 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
96 ogg_stream_packetin(&context->os, op); |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
97 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
98 while(ogg_stream_pageout(&context->os, &og)) { |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
99 put_buffer(&avfcontext->pb, og.header, og.header_len); |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
100 put_buffer(&avfcontext->pb, og.body, og.body_len); |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
101 put_flush_packet(&avfcontext->pb); |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
102 } |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
103 op->packetno++; |
0 | 104 |
105 return 0; | |
106 } | |
107 | |
108 | |
109 static int ogg_write_trailer(AVFormatContext *avfcontext) { | |
110 OggContext *context = avfcontext->priv_data ; | |
111 ogg_page og ; | |
112 | |
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 | |
119 ogg_stream_clear(&context->os) ; | |
120 return 0 ; | |
121 } | |
122 | |
123 | |
124 static AVOutputFormat ogg_oformat = { | |
125 "ogg", | |
126 "Ogg Vorbis", | |
127 "audio/x-vorbis", | |
128 "ogg", | |
129 sizeof(OggContext), | |
130 CODEC_ID_VORBIS, | |
131 0, | |
132 ogg_write_header, | |
133 ogg_write_packet, | |
134 ogg_write_trailer, | |
135 } ; | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
68
diff
changeset
|
136 #endif //CONFIG_ENCODERS |
0 | 137 |
138 | |
139 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) { | |
140 OggContext *context = avfcontext->priv_data ; | |
141 ogg_page og ; | |
142 char *buf ; | |
143 | |
144 while(ogg_stream_packetout(&context->os, op) != 1) { | |
145 | |
146 /* while no pages are available, read in more data to the sync */ | |
147 while(ogg_sync_pageout(&context->oy, &og) != 1) { | |
148 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ; | |
149 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0) | |
150 return 1 ; | |
151 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; | |
152 } | |
153 | |
154 /* got a page. Feed it into the stream and get the packet */ | |
155 if(ogg_stream_pagein(&context->os, &og) != 0) | |
156 return 1 ; | |
157 } | |
158 | |
159 return 0 ; | |
160 } | |
161 | |
162 | |
163 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap) | |
164 { | |
35 | 165 OggContext *context = avfcontext->priv_data; |
408 | 166 ogg_packet op ; |
0 | 167 char *buf ; |
168 ogg_page og ; | |
169 AVStream *ast ; | |
408 | 170 AVCodecContext *codec; |
171 uint8_t *p; | |
172 int i; | |
404 | 173 |
174 avfcontext->ctx_flags |= AVFMTCTX_NOHEADER; | |
175 | |
0 | 176 ogg_sync_init(&context->oy) ; |
177 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ; | |
178 | |
179 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0) | |
180 return -EIO ; | |
181 | |
182 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; | |
183 ogg_sync_pageout(&context->oy, &og) ; | |
184 ogg_stream_init(&context->os, ogg_page_serialno(&og)) ; | |
185 ogg_stream_pagein(&context->os, &og) ; | |
408 | 186 |
0 | 187 /* currently only one vorbis stream supported */ |
188 | |
189 ast = av_new_stream(avfcontext, 0) ; | |
190 if(!ast) | |
191 return AVERROR_NOMEM ; | |
192 | |
408 | 193 codec= &ast->codec; |
194 codec->codec_type = CODEC_TYPE_AUDIO; | |
195 codec->codec_id = CODEC_ID_VORBIS; | |
196 for(i=0; i<3; i++){ | |
197 if(next_packet(avfcontext, &op)){ | |
198 return -1; | |
199 } | |
200 codec->extradata_size+= 2 + op.bytes; | |
201 codec->extradata= av_realloc(codec->extradata, codec->extradata_size); | |
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)) | |
216 return -EIO ; | |
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) |
0 | 218 return -EIO ; |
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); |
0 | 221 |
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
222 return op.bytes; |
0 | 223 } |
224 | |
225 | |
226 static int ogg_read_close(AVFormatContext *avfcontext) { | |
227 OggContext *context = avfcontext->priv_data ; | |
228 | |
229 ogg_stream_clear(&context->os) ; | |
230 ogg_sync_clear(&context->oy) ; | |
408 | 231 av_freep(&avfcontext->streams[0]->codec.extradata); |
0 | 232 |
233 return 0 ; | |
234 } | |
235 | |
236 | |
237 static AVInputFormat ogg_iformat = { | |
238 "ogg", | |
239 "Ogg Vorbis", | |
240 sizeof(OggContext), | |
241 NULL, | |
242 ogg_read_header, | |
243 ogg_read_packet, | |
244 ogg_read_close, | |
245 .extensions = "ogg", | |
246 } ; | |
247 | |
248 | |
249 int ogg_init(void) { | |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
68
diff
changeset
|
250 #ifdef CONFIG_ENCODERS |
0 | 251 av_register_output_format(&ogg_oformat) ; |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
68
diff
changeset
|
252 #endif |
0 | 253 av_register_input_format(&ogg_iformat); |
254 return 0 ; | |
255 } |