2 * Ogg bitstream support
3 * Mark Hills <mark@pogo.org.uk>
5 * Uses libogg, but requires libvorbisenc to construct correct headers
6 * when containing Vorbis stream -- currently the only format supported
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #define DECODER_BUFFER_SIZE 4096
37 typedef struct OggContext {
49 static int ogg_write_header(AVFormatContext *avfcontext)
51 OggContext *context = avfcontext->priv_data;
52 ogg_packet *op= &context->op;
55 ogg_stream_init(&context->os, 31415);
57 for(n = 0 ; n < avfcontext->nb_streams ; n++) {
58 AVCodecContext *codec = avfcontext->streams[n]->codec;
59 uint8_t *headers = codec->extradata;
60 int headers_len = codec->extradata_size;
61 uint8_t *header_start[3];
65 av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
67 for(j=1,i=0;i<2;++i, ++j) {
69 while(j<headers_len && headers[j]==0xff) {
73 header_len[i]+=headers[j];
75 header_len[2]=headers_len-header_len[0]-header_len[1]-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];
82 op->bytes = header_len[i];
84 op->packet= header_start[i];
85 op->b_o_s= op->packetno==0;
87 ogg_stream_packetin(&context->os, op);
89 op->packetno++; //FIXME multiple streams
92 context->header_handled = 0 ;
98 static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
100 OggContext *context = avfcontext->priv_data ;
101 AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec;
102 ogg_packet *op= &context->op;
106 pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
108 // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
110 /* flush header packets so audio starts on a new page */
112 if(!context->header_handled) {
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);
118 context->header_handled = 1 ;
121 op->packet = (uint8_t*) pkt->data;
122 op->bytes = pkt->size;
123 op->b_o_s = op->packetno == 0;
126 /* correct the fields in the packet -- essential for streaming */
128 ogg_stream_packetin(&context->os, op);
130 while(ogg_stream_pageout(&context->os, &og)) {
131 put_buffer(&avfcontext->pb, og.header, og.header_len);
132 put_buffer(&avfcontext->pb, og.body, og.body_len);
133 put_flush_packet(&avfcontext->pb);
141 static int ogg_write_trailer(AVFormatContext *avfcontext) {
142 OggContext *context = avfcontext->priv_data ;
145 while(ogg_stream_flush(&context->os, &og)) {
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);
151 ogg_stream_clear(&context->os) ;
156 AVOutputFormat ogg_muxer = {
168 #endif //CONFIG_MUXERS
171 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
172 OggContext *context = avfcontext->priv_data ;
176 while(ogg_stream_packetout(&context->os, op) != 1) {
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)
183 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
186 /* got a page. Feed it into the stream and get the packet */
187 if(ogg_stream_pagein(&context->os, &og) != 0)
195 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
197 OggContext *context = avfcontext->priv_data;
202 AVCodecContext *codec;
206 ogg_sync_init(&context->oy) ;
207 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
209 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
212 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
213 ogg_sync_pageout(&context->oy, &og) ;
214 ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
215 ogg_stream_pagein(&context->os, &og) ;
217 /* currently only one vorbis stream supported */
219 ast = av_new_stream(avfcontext, 0) ;
221 return AVERROR_NOMEM ;
222 av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
225 codec->codec_type = CODEC_TYPE_AUDIO;
226 codec->codec_id = CODEC_ID_VORBIS;
228 if(next_packet(avfcontext, &op)){
231 if(op.bytes >= (1<<16) || op.bytes < 0)
233 codec->extradata_size+= 2 + op.bytes;
234 codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
235 memset(codec->extradata + codec->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
236 p= codec->extradata + codec->extradata_size - 2 - op.bytes;
238 *(p++)= op.bytes&0xFF;
239 memcpy(p, op.packet, op.bytes);
246 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
249 if(next_packet(avfcontext, &op))
251 if(av_new_packet(pkt, op.bytes) < 0)
253 pkt->stream_index = 0 ;
254 memcpy(pkt->data, op.packet, op.bytes);
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);
257 // printf("%lld %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
263 static int ogg_read_close(AVFormatContext *avfcontext) {
264 OggContext *context = avfcontext->priv_data ;
266 ogg_stream_clear(&context->os) ;
267 ogg_sync_clear(&context->oy) ;
273 static AVInputFormat ogg_iformat = {