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 library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #define DECODER_BUFFER_SIZE 4096
35 typedef struct OggContext {
47 static int ogg_write_header(AVFormatContext *avfcontext)
49 OggContext *context = avfcontext->priv_data;
50 ogg_packet *op= &context->op;
53 ogg_stream_init(&context->os, 31415);
55 for(n = 0 ; n < avfcontext->nb_streams ; n++) {
56 AVCodecContext *codec = avfcontext->streams[n]->codec;
57 uint8_t *headers = codec->extradata;
58 int headers_len = codec->extradata_size;
59 uint8_t *header_start[3];
63 av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
65 for(j=1,i=0;i<2;++i, ++j) {
67 while(j<headers_len && headers[j]==0xff) {
71 header_len[i]+=headers[j];
73 header_len[2]=headers_len-header_len[0]-header_len[1]-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];
80 op->bytes = header_len[i];
82 op->packet= header_start[i];
83 op->b_o_s= op->packetno==0;
85 ogg_stream_packetin(&context->os, op);
87 op->packetno++; //FIXME multiple streams
90 context->header_handled = 0 ;
96 static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
98 OggContext *context = avfcontext->priv_data ;
99 AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec;
100 ogg_packet *op= &context->op;
104 pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
106 // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
108 /* flush header packets so audio starts on a new page */
110 if(!context->header_handled) {
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);
116 context->header_handled = 1 ;
119 op->packet = (uint8_t*) pkt->data;
120 op->bytes = pkt->size;
121 op->b_o_s = op->packetno == 0;
124 /* correct the fields in the packet -- essential for streaming */
126 ogg_stream_packetin(&context->os, op);
128 while(ogg_stream_pageout(&context->os, &og)) {
129 put_buffer(&avfcontext->pb, og.header, og.header_len);
130 put_buffer(&avfcontext->pb, og.body, og.body_len);
131 put_flush_packet(&avfcontext->pb);
139 static int ogg_write_trailer(AVFormatContext *avfcontext) {
140 OggContext *context = avfcontext->priv_data ;
143 while(ogg_stream_flush(&context->os, &og)) {
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);
149 ogg_stream_clear(&context->os) ;
154 AVOutputFormat ogg_muxer = {
166 #endif //CONFIG_MUXERS
169 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
170 OggContext *context = avfcontext->priv_data ;
174 while(ogg_stream_packetout(&context->os, op) != 1) {
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)
181 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
184 /* got a page. Feed it into the stream and get the packet */
185 if(ogg_stream_pagein(&context->os, &og) != 0)
193 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
195 OggContext *context = avfcontext->priv_data;
200 AVCodecContext *codec;
204 ogg_sync_init(&context->oy) ;
205 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
207 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
210 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
211 ogg_sync_pageout(&context->oy, &og) ;
212 ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
213 ogg_stream_pagein(&context->os, &og) ;
215 /* currently only one vorbis stream supported */
217 ast = av_new_stream(avfcontext, 0) ;
219 return AVERROR_NOMEM ;
220 av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
223 codec->codec_type = CODEC_TYPE_AUDIO;
224 codec->codec_id = CODEC_ID_VORBIS;
226 if(next_packet(avfcontext, &op)){
229 if(op.bytes >= (1<<16) || op.bytes < 0)
231 codec->extradata_size+= 2 + op.bytes;
232 codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
233 memset(codec->extradata + codec->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
234 p= codec->extradata + codec->extradata_size - 2 - op.bytes;
236 *(p++)= op.bytes&0xFF;
237 memcpy(p, op.packet, op.bytes);
244 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
247 if(next_packet(avfcontext, &op))
249 if(av_new_packet(pkt, op.bytes) < 0)
251 pkt->stream_index = 0 ;
252 memcpy(pkt->data, op.packet, op.bytes);
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);
255 // printf("%lld %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
261 static int ogg_read_close(AVFormatContext *avfcontext) {
262 OggContext *context = avfcontext->priv_data ;
264 ogg_stream_clear(&context->os) ;
265 ogg_sync_clear(&context->oy) ;
271 static AVInputFormat ogg_iformat = {