Mercurial > libavformat.hg
annotate ogg.c @ 2403:a7e0737b3477 libavformat
Identifiers starting with underscores are reserved.
author | diego |
---|---|
date | Wed, 22 Aug 2007 12:41:16 +0000 |
parents | b21c2af60bc9 |
children |
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 |
0 | 32 #define DECODER_BUFFER_SIZE 4096 |
33 | |
34 | |
35 typedef struct OggContext { | |
36 /* output */ | |
37 ogg_stream_state os ; | |
38 int header_handled ; | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
39 ogg_packet op; |
0 | 40 |
41 /* input */ | |
42 ogg_sync_state oy ; | |
43 } OggContext ; | |
44 | |
45 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
857
diff
changeset
|
46 #ifdef CONFIG_MUXERS |
885 | 47 static int ogg_write_header(AVFormatContext *avfcontext) |
35 | 48 { |
49 OggContext *context = avfcontext->priv_data; | |
885 | 50 ogg_packet *op= &context->op; |
857 | 51 int n; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
52 |
404 | 53 ogg_stream_init(&context->os, 31415); |
885 | 54 |
0 | 55 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
|
56 AVCodecContext *codec = avfcontext->streams[n]->codec; |
843 | 57 uint8_t *headers = codec->extradata; |
58 int headers_len = codec->extradata_size; | |
59 uint8_t *header_start[3]; | |
60 int header_len[3]; | |
857 | 61 int i, j; |
885 | 62 |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
454
diff
changeset
|
63 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
|
64 |
843 | 65 for(j=1,i=0;i<2;++i, ++j) { |
66 header_len[i]=0; | |
67 while(j<headers_len && headers[j]==0xff) { | |
68 header_len[i]+=0xff; | |
69 ++j; | |
70 } | |
71 header_len[i]+=headers[j]; | |
72 } | |
73 header_len[2]=headers_len-header_len[0]-header_len[1]-j; | |
74 headers+=j; | |
75 header_start[0] = headers; | |
76 header_start[1] = header_start[0] + header_len[0]; | |
77 header_start[2] = header_start[1] + header_len[1]; | |
0 | 78 |
843 | 79 for(i=0; i < 3; ++i){ |
80 op->bytes = header_len[i]; | |
81 | |
82 op->packet= header_start[i]; | |
407
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
83 op->b_o_s= op->packetno==0; |
0 | 84 |
407
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
85 ogg_stream_packetin(&context->os, op); |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
86 |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
87 op->packetno++; //FIXME multiple streams |
b9d16c18ee18
remove function call from muxer->encoder and cleanly pass global headers
michael
parents:
406
diff
changeset
|
88 } |
0 | 89 |
887 | 90 context->header_handled = 0 ; |
0 | 91 } |
885 | 92 |
0 | 93 return 0 ; |
94 } | |
95 | |
468 | 96 static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt) |
0 | 97 { |
98 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
|
99 AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
100 ogg_packet *op= &context->op; |
0 | 101 ogg_page og ; |
468 | 102 int64_t pts; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
103 |
468 | 104 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
|
105 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
106 // 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
|
107 |
0 | 108 /* flush header packets so audio starts on a new page */ |
109 | |
110 if(!context->header_handled) { | |
887 | 111 while(ogg_stream_flush(&context->os, &og)) { |
112 put_buffer(&avfcontext->pb, og.header, og.header_len) ; | |
113 put_buffer(&avfcontext->pb, og.body, og.body_len) ; | |
114 put_flush_packet(&avfcontext->pb); | |
115 } | |
116 context->header_handled = 1 ; | |
0 | 117 } |
118 | |
468 | 119 op->packet = (uint8_t*) pkt->data; |
120 op->bytes = pkt->size; | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
121 op->b_o_s = op->packetno == 0; |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
122 op->granulepos= pts; |
0 | 123 |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
124 /* correct the fields in the packet -- essential for streaming */ |
885 | 125 |
126 ogg_stream_packetin(&context->os, op); | |
127 | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
128 while(ogg_stream_pageout(&context->os, &og)) { |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
129 put_buffer(&avfcontext->pb, og.header, og.header_len); |
887 | 130 put_buffer(&avfcontext->pb, og.body, og.body_len); |
131 put_flush_packet(&avfcontext->pb); | |
885 | 132 } |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
405
diff
changeset
|
133 op->packetno++; |
0 | 134 |
135 return 0; | |
136 } | |
137 | |
138 | |
139 static int ogg_write_trailer(AVFormatContext *avfcontext) { | |
140 OggContext *context = avfcontext->priv_data ; | |
141 ogg_page og ; | |
142 | |
143 while(ogg_stream_flush(&context->os, &og)) { | |
887 | 144 put_buffer(&avfcontext->pb, og.header, og.header_len) ; |
145 put_buffer(&avfcontext->pb, og.body, og.body_len) ; | |
146 put_flush_packet(&avfcontext->pb); | |
0 | 147 } |
148 | |
149 ogg_stream_clear(&context->os) ; | |
150 return 0 ; | |
151 } | |
152 | |
153 | |
1169 | 154 AVOutputFormat ogg_muxer = { |
0 | 155 "ogg", |
1788 | 156 "Ogg format", |
157 "application/ogg", | |
0 | 158 "ogg", |
159 sizeof(OggContext), | |
160 CODEC_ID_VORBIS, | |
161 0, | |
162 ogg_write_header, | |
163 ogg_write_packet, | |
164 ogg_write_trailer, | |
165 } ; | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
857
diff
changeset
|
166 #endif //CONFIG_MUXERS |
0 | 167 |
726
17178af951b4
Ogg demuxer ported from tcvp by Luca Barbato <lu_zero at gentoo dot org>,
mru
parents:
639
diff
changeset
|
168 #if 0 |
0 | 169 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) { |
170 OggContext *context = avfcontext->priv_data ; | |
171 ogg_page og ; | |
172 char *buf ; | |
173 | |
174 while(ogg_stream_packetout(&context->os, op) != 1) { | |
175 | |
887 | 176 /* while no pages are available, read in more data to the sync */ |
177 while(ogg_sync_pageout(&context->oy, &og) != 1) { | |
178 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ; | |
179 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0) | |
180 return 1 ; | |
181 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; | |
182 } | |
885 | 183 |
887 | 184 /* got a page. Feed it into the stream and get the packet */ |
185 if(ogg_stream_pagein(&context->os, &og) != 0) | |
186 return 1 ; | |
0 | 187 } |
188 | |
189 return 0 ; | |
190 } | |
191 | |
192 | |
193 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap) | |
194 { | |
35 | 195 OggContext *context = avfcontext->priv_data; |
885 | 196 ogg_packet op ; |
0 | 197 char *buf ; |
198 ogg_page og ; | |
199 AVStream *ast ; | |
408 | 200 AVCodecContext *codec; |
201 uint8_t *p; | |
202 int i; | |
885 | 203 |
0 | 204 ogg_sync_init(&context->oy) ; |
205 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ; | |
206 | |
207 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0) | |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
208 return AVERROR(EIO) ; |
885 | 209 |
210 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ; | |
0 | 211 ogg_sync_pageout(&context->oy, &og) ; |
212 ogg_stream_init(&context->os, ogg_page_serialno(&og)) ; | |
213 ogg_stream_pagein(&context->os, &og) ; | |
885 | 214 |
0 | 215 /* currently only one vorbis stream supported */ |
216 | |
217 ast = av_new_stream(avfcontext, 0) ; | |
218 if(!ast) | |
2273
7eb456c4ed8a
Replace all occurrences of AVERROR_NOMEM with AVERROR(ENOMEM).
takis
parents:
2249
diff
changeset
|
219 return AVERROR(ENOMEM) ; |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
454
diff
changeset
|
220 av_set_pts_info(ast, 60, 1, AV_TIME_BASE); |
0 | 221 |
408 | 222 codec= &ast->codec; |
223 codec->codec_type = CODEC_TYPE_AUDIO; | |
224 codec->codec_id = CODEC_ID_VORBIS; | |
225 for(i=0; i<3; i++){ | |
226 if(next_packet(avfcontext, &op)){ | |
227 return -1; | |
228 } | |
639 | 229 if(op.bytes >= (1<<16) || op.bytes < 0) |
230 return -1; | |
408 | 231 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
|
232 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
|
233 memset(codec->extradata + codec->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
408 | 234 p= codec->extradata + codec->extradata_size - 2 - op.bytes; |
235 *(p++)= op.bytes>>8; | |
236 *(p++)= op.bytes&0xFF; | |
237 memcpy(p, op.packet, op.bytes); | |
238 } | |
239 | |
0 | 240 return 0 ; |
241 } | |
242 | |
243 | |
244 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) { | |
245 ogg_packet op ; | |
246 | |
885 | 247 if(next_packet(avfcontext, &op)) |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
248 return AVERROR(EIO) ; |
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
249 if(av_new_packet(pkt, op.bytes) < 0) |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2273
diff
changeset
|
250 return AVERROR(EIO) ; |
0 | 251 pkt->stream_index = 0 ; |
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
252 memcpy(pkt->data, op.packet, op.bytes); |
454 | 253 if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1) |
254 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
|
255 // printf("%"PRId64" %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate); |
0 | 256 |
405
04d7dda7ccd5
kill obnoxious ogg_packet passing from demuxer to decoder
michael
parents:
404
diff
changeset
|
257 return op.bytes; |
0 | 258 } |
259 | |
260 | |
261 static int ogg_read_close(AVFormatContext *avfcontext) { | |
262 OggContext *context = avfcontext->priv_data ; | |
263 | |
264 ogg_stream_clear(&context->os) ; | |
265 ogg_sync_clear(&context->oy) ; | |
266 | |
267 return 0 ; | |
268 } | |
269 | |
270 | |
1169 | 271 static AVInputFormat ogg_iformat = { |
0 | 272 "ogg", |
273 "Ogg Vorbis", | |
274 sizeof(OggContext), | |
275 NULL, | |
276 ogg_read_header, | |
277 ogg_read_packet, | |
278 ogg_read_close, | |
279 .extensions = "ogg", | |
280 } ; | |
726
17178af951b4
Ogg demuxer ported from tcvp by Luca Barbato <lu_zero at gentoo dot org>,
mru
parents:
639
diff
changeset
|
281 #endif |