]> git.sesse.net Git - ffmpeg/blob - libavformat/ogg.c
COSMETICS: tabs --> spaces, some prettyprinting
[ffmpeg] / libavformat / ogg.c
1 /*
2  * Ogg bitstream support
3  * Mark Hills <mark@pogo.org.uk>
4  *
5  * Uses libogg, but requires libvorbisenc to construct correct headers
6  * when containing Vorbis stream -- currently the only format supported
7  */
8
9 #include <stdio.h>
10
11 #include <ogg/ogg.h>
12
13 #include "avformat.h"
14
15 #undef NDEBUG
16 #include <assert.h>
17
18 #define DECODER_BUFFER_SIZE 4096
19
20
21 typedef struct OggContext {
22     /* output */
23     ogg_stream_state os ;
24     int header_handled ;
25     ogg_packet op;
26
27     /* input */
28     ogg_sync_state oy ;
29 } OggContext ;
30
31
32 #ifdef CONFIG_MUXERS
33 static int ogg_write_header(AVFormatContext *avfcontext)
34 {
35     OggContext *context = avfcontext->priv_data;
36     ogg_packet *op= &context->op;
37     int n;
38
39     ogg_stream_init(&context->os, 31415);
40
41     for(n = 0 ; n < avfcontext->nb_streams ; n++) {
42         AVCodecContext *codec = avfcontext->streams[n]->codec;
43         uint8_t *headers = codec->extradata;
44         int headers_len = codec->extradata_size;
45         uint8_t *header_start[3];
46         int header_len[3];
47         int i, j;
48
49         av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
50
51         for(j=1,i=0;i<2;++i, ++j) {
52             header_len[i]=0;
53             while(j<headers_len && headers[j]==0xff) {
54                 header_len[i]+=0xff;
55                 ++j;
56             }
57             header_len[i]+=headers[j];
58         }
59         header_len[2]=headers_len-header_len[0]-header_len[1]-j;
60         headers+=j;
61         header_start[0] = headers;
62         header_start[1] = header_start[0] + header_len[0];
63         header_start[2] = header_start[1] + header_len[1];
64
65         for(i=0; i < 3; ++i){
66             op->bytes = header_len[i];
67
68             op->packet= header_start[i];
69             op->b_o_s= op->packetno==0;
70
71             ogg_stream_packetin(&context->os, op);
72
73             op->packetno++; //FIXME multiple streams
74         }
75
76         context->header_handled = 0 ;
77     }
78
79     return 0 ;
80 }
81
82 static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
83 {
84     OggContext *context = avfcontext->priv_data ;
85     AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec;
86     ogg_packet *op= &context->op;
87     ogg_page og ;
88     int64_t pts;
89
90     pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
91
92 //    av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
93
94     /* flush header packets so audio starts on a new page */
95
96     if(!context->header_handled) {
97         while(ogg_stream_flush(&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);
101         }
102         context->header_handled = 1 ;
103     }
104
105     op->packet = (uint8_t*) pkt->data;
106     op->bytes  = pkt->size;
107     op->b_o_s  = op->packetno == 0;
108     op->granulepos= pts;
109
110     /* correct the fields in the packet -- essential for streaming */
111
112     ogg_stream_packetin(&context->os, op);
113
114     while(ogg_stream_pageout(&context->os, &og)) {
115         put_buffer(&avfcontext->pb, og.header, og.header_len);
116         put_buffer(&avfcontext->pb, og.body, og.body_len);
117         put_flush_packet(&avfcontext->pb);
118     }
119     op->packetno++;
120
121     return 0;
122 }
123
124
125 static int ogg_write_trailer(AVFormatContext *avfcontext) {
126     OggContext *context = avfcontext->priv_data ;
127     ogg_page og ;
128
129     while(ogg_stream_flush(&context->os, &og)) {
130         put_buffer(&avfcontext->pb, og.header, og.header_len) ;
131         put_buffer(&avfcontext->pb, og.body, og.body_len) ;
132         put_flush_packet(&avfcontext->pb);
133     }
134
135     ogg_stream_clear(&context->os) ;
136     return 0 ;
137 }
138
139
140 static AVOutputFormat ogg_oformat = {
141     "ogg",
142     "Ogg Vorbis",
143     "audio/x-vorbis",
144     "ogg",
145     sizeof(OggContext),
146     CODEC_ID_VORBIS,
147     0,
148     ogg_write_header,
149     ogg_write_packet,
150     ogg_write_trailer,
151 } ;
152 #endif //CONFIG_MUXERS
153
154 #if 0
155 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
156     OggContext *context = avfcontext->priv_data ;
157     ogg_page og ;
158     char *buf ;
159
160     while(ogg_stream_packetout(&context->os, op) != 1) {
161
162         /* while no pages are available, read in more data to the sync */
163         while(ogg_sync_pageout(&context->oy, &og) != 1) {
164             buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
165             if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
166                 return 1 ;
167             ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
168         }
169
170         /* got a page. Feed it into the stream and get the packet */
171         if(ogg_stream_pagein(&context->os, &og) != 0)
172             return 1 ;
173     }
174
175     return 0 ;
176 }
177
178
179 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
180 {
181     OggContext *context = avfcontext->priv_data;
182     ogg_packet op ;
183     char *buf ;
184     ogg_page og ;
185     AVStream *ast ;
186     AVCodecContext *codec;
187     uint8_t *p;
188     int i;
189
190     ogg_sync_init(&context->oy) ;
191     buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
192
193     if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
194         return AVERROR_IO ;
195
196     ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
197     ogg_sync_pageout(&context->oy, &og) ;
198     ogg_stream_init(&context->os, ogg_page_serialno(&og)) ;
199     ogg_stream_pagein(&context->os, &og) ;
200
201     /* currently only one vorbis stream supported */
202
203     ast = av_new_stream(avfcontext, 0) ;
204     if(!ast)
205         return AVERROR_NOMEM ;
206     av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
207
208     codec= &ast->codec;
209     codec->codec_type = CODEC_TYPE_AUDIO;
210     codec->codec_id = CODEC_ID_VORBIS;
211     for(i=0; i<3; i++){
212         if(next_packet(avfcontext, &op)){
213             return -1;
214         }
215         if(op.bytes >= (1<<16) || op.bytes < 0)
216             return -1;
217         codec->extradata_size+= 2 + op.bytes;
218         codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
219         memset(codec->extradata + codec->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
220         p= codec->extradata + codec->extradata_size - 2 - op.bytes;
221         *(p++)= op.bytes>>8;
222         *(p++)= op.bytes&0xFF;
223         memcpy(p, op.packet, op.bytes);
224     }
225
226     return 0 ;
227 }
228
229
230 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
231     ogg_packet op ;
232
233     if(next_packet(avfcontext, &op))
234         return AVERROR_IO ;
235     if(av_new_packet(pkt, op.bytes) < 0)
236         return AVERROR_IO ;
237     pkt->stream_index = 0 ;
238     memcpy(pkt->data, op.packet, op.bytes);
239     if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1)
240         pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate);
241 //    printf("%lld %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
242
243     return op.bytes;
244 }
245
246
247 static int ogg_read_close(AVFormatContext *avfcontext) {
248     OggContext *context = avfcontext->priv_data ;
249
250     ogg_stream_clear(&context->os) ;
251     ogg_sync_clear(&context->oy) ;
252     av_freep(&avfcontext->streams[0]->codec.extradata);
253
254     return 0 ;
255 }
256
257
258 static AVInputFormat ogg_iformat = {
259     "ogg",
260     "Ogg Vorbis",
261     sizeof(OggContext),
262     NULL,
263     ogg_read_header,
264     ogg_read_packet,
265     ogg_read_close,
266     .extensions = "ogg",
267 } ;
268 #endif
269
270 int libogg_init(void) {
271 #ifdef CONFIG_MUXERS
272     av_register_output_format(&ogg_oformat) ;
273 #endif
274 /*     av_register_input_format(&ogg_iformat); */
275     return 0 ;
276 }