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
13 #include <vorbis/vorbisenc.h>
16 #include "oggvorbis.h"
18 #define DECODER_BUFFER_SIZE 4096
21 typedef struct OggContext {
25 ogg_int64_t base_packet_no ;
26 ogg_int64_t base_granule_pos ;
33 static int ogg_write_header(AVFormatContext *avfcontext)
35 OggContext *context = avfcontext->priv_data;
36 AVCodecContext *avccontext ;
41 ogg_packet header, header_comm, header_code ;
45 ogg_stream_init(&context->os, rand());
47 for(n = 0 ; n < avfcontext->nb_streams ; n++) {
48 avccontext = &avfcontext->streams[n]->codec ;
50 /* begin vorbis specific code */
52 vorbis_info_init(&vi) ;
54 /* code copied from libavcodec/oggvorbis.c */
56 if(oggvorbis_init_encoder(&vi, avccontext) < 0) {
57 fprintf(stderr, "ogg_write_header: init_encoder failed") ;
61 vorbis_analysis_init(&vd, &vi) ;
62 vorbis_block_init(&vd, &vb) ;
64 vorbis_comment_init(&vc) ;
65 vorbis_comment_add_tag(&vc, "encoder", "ffmpeg") ;
66 if(*avfcontext->title)
67 vorbis_comment_add_tag(&vc, "title", avfcontext->title) ;
69 vorbis_analysis_headerout(&vd, &vc, &header,
70 &header_comm, &header_code) ;
71 ogg_stream_packetin(&context->os, &header) ;
72 ogg_stream_packetin(&context->os, &header_comm) ;
73 ogg_stream_packetin(&context->os, &header_code) ;
75 vorbis_block_clear(&vb) ;
76 vorbis_dsp_clear(&vd) ;
77 vorbis_info_clear(&vi) ;
78 vorbis_comment_clear(&vc) ;
80 /* end of vorbis specific code */
82 context->header_handled = 0 ;
83 context->base_packet_no = 0 ;
90 static int ogg_write_packet(AVFormatContext *avfcontext,
92 unsigned char *buf, int size, int force_pts)
94 OggContext *context = avfcontext->priv_data ;
99 /* flush header packets so audio starts on a new page */
101 if(!context->header_handled) {
102 while(ogg_stream_flush(&context->os, &og)) {
103 put_buffer(&avfcontext->pb, og.header, og.header_len) ;
104 put_buffer(&avfcontext->pb, og.body, og.body_len) ;
105 put_flush_packet(&avfcontext->pb);
107 context->header_handled = 1 ;
111 op = (ogg_packet*)(buf + l) ;
112 op->packet = buf + l + sizeof(ogg_packet) ; /* fix data pointer */
114 if(!context->base_packet_no) { /* this is the first packet */
115 context->base_packet_no = op->packetno ;
116 context->base_granule_pos = op->granulepos ;
119 /* correct the fields in the packet -- essential for streaming */
121 op->packetno -= context->base_packet_no ;
122 op->granulepos -= context->base_granule_pos ;
124 ogg_stream_packetin(&context->os, op) ;
125 l += sizeof(ogg_packet) + op->bytes ;
127 while(ogg_stream_pageout(&context->os, &og)) {
128 put_buffer(&avfcontext->pb, og.header, og.header_len) ;
129 put_buffer(&avfcontext->pb, og.body, og.body_len) ;
130 put_flush_packet(&avfcontext->pb);
138 static int ogg_write_trailer(AVFormatContext *avfcontext) {
139 OggContext *context = avfcontext->priv_data ;
142 while(ogg_stream_flush(&context->os, &og)) {
143 put_buffer(&avfcontext->pb, og.header, og.header_len) ;
144 put_buffer(&avfcontext->pb, og.body, og.body_len) ;
145 put_flush_packet(&avfcontext->pb);
148 ogg_stream_clear(&context->os) ;
153 static AVOutputFormat ogg_oformat = {
167 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
168 OggContext *context = avfcontext->priv_data ;
172 while(ogg_stream_packetout(&context->os, op) != 1) {
174 /* while no pages are available, read in more data to the sync */
175 while(ogg_sync_pageout(&context->oy, &og) != 1) {
176 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
177 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
179 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
182 /* got a page. Feed it into the stream and get the packet */
183 if(ogg_stream_pagein(&context->os, &og) != 0)
191 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
193 OggContext *context = avfcontext->priv_data;
198 ogg_sync_init(&context->oy) ;
199 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
201 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
204 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
205 ogg_sync_pageout(&context->oy, &og) ;
206 ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
207 ogg_stream_pagein(&context->os, &og) ;
209 /* currently only one vorbis stream supported */
211 ast = av_new_stream(avfcontext, 0) ;
213 return AVERROR_NOMEM ;
215 ast->codec.codec_type = CODEC_TYPE_AUDIO ;
216 ast->codec.codec_id = CODEC_ID_VORBIS ;
222 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
225 if(next_packet(avfcontext, &op))
227 if(av_new_packet(pkt, sizeof(ogg_packet) + op.bytes) < 0)
229 pkt->stream_index = 0 ;
230 memcpy(pkt->data, &op, sizeof(ogg_packet)) ;
231 memcpy(pkt->data + sizeof(ogg_packet), op.packet, op.bytes) ;
233 return sizeof(ogg_packet) + op.bytes ;
237 static int ogg_read_close(AVFormatContext *avfcontext) {
238 OggContext *context = avfcontext->priv_data ;
240 ogg_stream_clear(&context->os) ;
241 ogg_sync_clear(&context->oy) ;
247 static AVInputFormat ogg_iformat = {
260 av_register_output_format(&ogg_oformat) ;
261 av_register_input_format(&ogg_iformat);