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 AVCodecContext *avccontext ;
40 ogg_packet header, header_comm, header_code ;
43 if(!(context = malloc(sizeof(OggContext))))
45 avfcontext->priv_data = context ;
48 ogg_stream_init(&context->os, rand());
50 for(n = 0 ; n < avfcontext->nb_streams ; n++) {
51 avccontext = &avfcontext->streams[n]->codec ;
53 /* begin vorbis specific code */
55 vorbis_info_init(&vi) ;
57 /* code copied from libavcodec/oggvorbis.c */
59 if(oggvorbis_init_encoder(&vi, avccontext) < 0) {
60 fprintf(stderr, "ogg_write_header: init_encoder failed") ;
64 vorbis_analysis_init(&vd, &vi) ;
65 vorbis_block_init(&vd, &vb) ;
67 vorbis_comment_init(&vc) ;
68 vorbis_comment_add_tag(&vc, "encoder", "ffmpeg") ;
69 if(*avfcontext->title)
70 vorbis_comment_add_tag(&vc, "title", avfcontext->title) ;
72 vorbis_analysis_headerout(&vd, &vc, &header,
73 &header_comm, &header_code) ;
74 ogg_stream_packetin(&context->os, &header) ;
75 ogg_stream_packetin(&context->os, &header_comm) ;
76 ogg_stream_packetin(&context->os, &header_code) ;
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 ;
198 if(!(context = malloc(sizeof(OggContext)))) {
202 avfcontext->priv_data = context ;
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 ;
221 ast->codec.codec_type = CODEC_TYPE_AUDIO ;
222 ast->codec.codec_id = CODEC_ID_VORBIS ;
228 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
231 if(next_packet(avfcontext, &op))
233 if(av_new_packet(pkt, sizeof(ogg_packet) + op.bytes) < 0)
235 pkt->stream_index = 0 ;
236 memcpy(pkt->data, &op, sizeof(ogg_packet)) ;
237 memcpy(pkt->data + sizeof(ogg_packet), op.packet, op.bytes) ;
239 return sizeof(ogg_packet) + op.bytes ;
243 static int ogg_read_close(AVFormatContext *avfcontext) {
244 OggContext *context = avfcontext->priv_data ;
246 ogg_stream_clear(&context->os) ;
247 ogg_sync_clear(&context->oy) ;
253 static AVInputFormat ogg_iformat = {
266 av_register_output_format(&ogg_oformat) ;
267 av_register_input_format(&ogg_iformat);