changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext...
[libav.git] / 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_ENCODERS
33 static int ogg_write_header(AVFormatContext *avfcontext)
34 {
35 OggContext *context = avfcontext->priv_data;
36 ogg_packet *op= &context->op;
37 int n, i;
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 *p= codec->extradata;
44
45 av_set_pts_info(avfcontext->streams[n], 60, 1, AV_TIME_BASE);
46
47 for(i=0; i < codec->extradata_size; i+= op->bytes){
48 op->bytes = p[i++]<<8;
49 op->bytes+= p[i++];
50
51 op->packet= &p[i];
52 op->b_o_s= op->packetno==0;
53
54 ogg_stream_packetin(&context->os, op);
55
56 op->packetno++; //FIXME multiple streams
57 }
58
59 context->header_handled = 0 ;
60 }
61
62 return 0 ;
63 }
64
65 static int ogg_write_packet(AVFormatContext *avfcontext, AVPacket *pkt)
66 {
67 OggContext *context = avfcontext->priv_data ;
68 AVCodecContext *avctx= avfcontext->streams[pkt->stream_index]->codec;
69 ogg_packet *op= &context->op;
70 ogg_page og ;
71 int64_t pts;
72
73 pts= av_rescale(pkt->pts, avctx->sample_rate, AV_TIME_BASE);
74
75 // av_log(avfcontext, AV_LOG_DEBUG, "M%d\n", size);
76
77 /* flush header packets so audio starts on a new page */
78
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);
84 }
85 context->header_handled = 1 ;
86 }
87
88 op->packet = (uint8_t*) pkt->data;
89 op->bytes = pkt->size;
90 op->b_o_s = op->packetno == 0;
91 op->granulepos= pts;
92
93 /* correct the fields in the packet -- essential for streaming */
94
95 ogg_stream_packetin(&context->os, op);
96
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);
101 }
102 op->packetno++;
103
104 return 0;
105 }
106
107
108 static int ogg_write_trailer(AVFormatContext *avfcontext) {
109 OggContext *context = avfcontext->priv_data ;
110 ogg_page og ;
111
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);
116 }
117
118 ogg_stream_clear(&context->os) ;
119 return 0 ;
120 }
121
122
123 static AVOutputFormat ogg_oformat = {
124 "ogg",
125 "Ogg Vorbis",
126 "audio/x-vorbis",
127 "ogg",
128 sizeof(OggContext),
129 CODEC_ID_VORBIS,
130 0,
131 ogg_write_header,
132 ogg_write_packet,
133 ogg_write_trailer,
134 } ;
135 #endif //CONFIG_ENCODERS
136
137 #if 0
138 static int next_packet(AVFormatContext *avfcontext, ogg_packet *op) {
139 OggContext *context = avfcontext->priv_data ;
140 ogg_page og ;
141 char *buf ;
142
143 while(ogg_stream_packetout(&context->os, op) != 1) {
144
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)
149 return 1 ;
150 ogg_sync_wrote(&context->oy, DECODER_BUFFER_SIZE) ;
151 }
152
153 /* got a page. Feed it into the stream and get the packet */
154 if(ogg_stream_pagein(&context->os, &og) != 0)
155 return 1 ;
156 }
157
158 return 0 ;
159 }
160
161
162 static int ogg_read_header(AVFormatContext *avfcontext, AVFormatParameters *ap)
163 {
164 OggContext *context = avfcontext->priv_data;
165 ogg_packet op ;
166 char *buf ;
167 ogg_page og ;
168 AVStream *ast ;
169 AVCodecContext *codec;
170 uint8_t *p;
171 int i;
172
173 ogg_sync_init(&context->oy) ;
174 buf = ogg_sync_buffer(&context->oy, DECODER_BUFFER_SIZE) ;
175
176 if(get_buffer(&avfcontext->pb, buf, DECODER_BUFFER_SIZE) <= 0)
177 return AVERROR_IO ;
178
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) ;
183
184 /* currently only one vorbis stream supported */
185
186 ast = av_new_stream(avfcontext, 0) ;
187 if(!ast)
188 return AVERROR_NOMEM ;
189 av_set_pts_info(ast, 60, 1, AV_TIME_BASE);
190
191 codec= &ast->codec;
192 codec->codec_type = CODEC_TYPE_AUDIO;
193 codec->codec_id = CODEC_ID_VORBIS;
194 for(i=0; i<3; i++){
195 if(next_packet(avfcontext, &op)){
196 return -1;
197 }
198 if(op.bytes >= (1<<16) || op.bytes < 0)
199 return -1;
200 codec->extradata_size+= 2 + op.bytes;
201 codec->extradata= av_realloc(codec->extradata, codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
202 p= codec->extradata + codec->extradata_size - 2 - op.bytes;
203 *(p++)= op.bytes>>8;
204 *(p++)= op.bytes&0xFF;
205 memcpy(p, op.packet, op.bytes);
206 }
207
208 return 0 ;
209 }
210
211
212 static int ogg_read_packet(AVFormatContext *avfcontext, AVPacket *pkt) {
213 ogg_packet op ;
214
215 if(next_packet(avfcontext, &op))
216 return AVERROR_IO ;
217 if(av_new_packet(pkt, op.bytes) < 0)
218 return AVERROR_IO ;
219 pkt->stream_index = 0 ;
220 memcpy(pkt->data, op.packet, op.bytes);
221 if(avfcontext->streams[0]->codec.sample_rate && op.granulepos!=-1)
222 pkt->pts= av_rescale(op.granulepos, AV_TIME_BASE, avfcontext->streams[0]->codec.sample_rate);
223 // printf("%lld %d %d\n", pkt->pts, (int)op.granulepos, avfcontext->streams[0]->codec.sample_rate);
224
225 return op.bytes;
226 }
227
228
229 static int ogg_read_close(AVFormatContext *avfcontext) {
230 OggContext *context = avfcontext->priv_data ;
231
232 ogg_stream_clear(&context->os) ;
233 ogg_sync_clear(&context->oy) ;
234 av_freep(&avfcontext->streams[0]->codec.extradata);
235
236 return 0 ;
237 }
238
239
240 static AVInputFormat ogg_iformat = {
241 "ogg",
242 "Ogg Vorbis",
243 sizeof(OggContext),
244 NULL,
245 ogg_read_header,
246 ogg_read_packet,
247 ogg_read_close,
248 .extensions = "ogg",
249 } ;
250 #endif
251
252 int libogg_init(void) {
253 #ifdef CONFIG_ENCODERS
254 av_register_output_format(&ogg_oformat) ;
255 #endif
256 /* av_register_input_format(&ogg_iformat); */
257 return 0 ;
258 }