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
18 #define DECODER_BUFFER_SIZE 4096
21 typedef struct OggContext {
32 #ifdef CONFIG_ENCODERS
33 static int ogg_write_header(AVFormatContext *avfcontext)
35 OggContext *context = avfcontext->priv_data;
36 ogg_packet *op= &context->op;
39 ogg_stream_init(&context->os, 31415);
41 for(n = 0 ; n < avfcontext->nb_streams ; n++) {
42 AVCodecContext *codec = &avfcontext->streams[n]->codec;
43 uint8_t *p= codec->extradata;
45 av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
47 for(i=0; i < codec->extradata_size; i+= op->bytes){
48 op->bytes = p[i++]<<8;
52 op->b_o_s= op->packetno==0;
54 ogg_stream_packetin(&context->os, op);
56 op->packetno++; //FIXME multiple streams
59 context->header_handled = 0 ;
65 static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
67 OggContext *context = avfcontext->priv_data ;
68 AVCodecContext *avctx= &avfcontext->streams[pkt->stream_index]->codec;
69 ogg_packet *op= &context->op;
73 pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
75 // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
77 /* flush header packets so audio starts on a new page */
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);
85 context->header_handled = 1 ;
88 op->packet = (uint8_t*) pkt->data;
89 op->bytes = pkt->size;
90 op->b_o_s = op->packetno == 0;
93 /* correct the fields in the packet -- essential for streaming */
95 ogg_stream_packetin(&context->os, op);
97 while(ogg_stream_pageout(&context->os, &og)) {
98 put_buffer(&avfcontext->pb, og.header, og.header_len);
99 put_buffer(&avfcontext->pb, og.body, og.body_len);
100 put_flush_packet(&avfcontext->pb);
108 static int ogg_write_trailer(AVFormatContext *avfcontext) {
109 OggContext *context = avfcontext->priv_data ;
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);
118 ogg_stream_clear(&context->os) ;
123 static AVOutputFormat ogg_oformat = {
135 #endif //CONFIG_ENCODERS
138 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
139 OggContext *context = avfcontext->priv_data ;
143 while(ogg_stream_packetout(&context->os, op) != 1) {
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)
150 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
153 /* got a page. Feed it into the stream and get the packet */
154 if(ogg_stream_pagein(&context->os, &og) != 0)
162 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
164 OggContext *context = avfcontext->priv_data;
169 AVCodecContext *codec;
173 ogg_sync_init(&context->oy) ;
174 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
176 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
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) ;
184 /* currently only one vorbis stream supported */
186 ast = av_new_stream(avfcontext, 0) ;
188 return AVERROR_NOMEM ;
189 av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
192 codec->codec_type = CODEC_TYPE_AUDIO;
193 codec->codec_id = CODEC_ID_VORBIS;
195 if(next_packet(avfcontext, &op)){
198 codec->extradata_size+= 2 + op.bytes;
199 codec->extradata= av_realloc(codec->extradata, codec->extradata_size);
200 p= codec->extradata + codec->extradata_size - 2 - op.bytes;
202 *(p++)= op.bytes&0xFF;
203 memcpy(p, op.packet, op.bytes);
210 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
213 if(next_packet(avfcontext, &op))
215 if(av_new_packet(pkt, op.bytes) < 0)
217 pkt->stream_index = 0 ;
218 memcpy(pkt->data, op.packet, op.bytes);
219 if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1)
220 pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate);
221 // printf("%lld %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
227 static int ogg_read_close(AVFormatContext *avfcontext) {
228 OggContext *context = avfcontext->priv_data ;
230 ogg_stream_clear(&context->os) ;
231 ogg_sync_clear(&context->oy) ;
232 av_freep(&avfcontext->streams[0]->codec.extradata);
238 static AVInputFormat ogg_iformat = {
251 #ifdef CONFIG_ENCODERS
252 av_register_output_format(&ogg_oformat) ;
254 av_register_input_format(&ogg_iformat);