]> git.sesse.net Git - ffmpeg/blob - libavcodec/oggvorbis.c
print a big warning if we mess up and run out of space ...
[ffmpeg] / libavcodec / oggvorbis.c
1 /**
2  * @file oggvorbis.c
3  * Ogg Vorbis codec support via libvorbisenc.
4  * @author Mark Hills <mark@pogo.org.uk>
5  */
6
7 #include <vorbis/vorbisenc.h>
8
9 #include "avcodec.h"
10
11 #undef NDEBUG
12 #include <assert.h>
13
14 #define OGGVORBIS_FRAME_SIZE 64
15
16 #define BUFFER_SIZE (1024*64)
17
18 typedef struct OggVorbisContext {
19     vorbis_info vi ;
20     vorbis_dsp_state vd ;
21     vorbis_block vb ;
22     uint8_t buffer[BUFFER_SIZE];
23     int buffer_index;
24
25     /* decoder */
26     vorbis_comment vc ;
27     ogg_packet op;
28 } OggVorbisContext ;
29
30
31 static int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
32     double cfreq;
33
34     if(avccontext->flags & CODEC_FLAG_QSCALE) {
35         /* variable bitrate */
36         if(vorbis_encode_setup_vbr(vi, avccontext->channels,
37                 avccontext->sample_rate,
38                 avccontext->global_quality / (float)FF_QP2LAMBDA))
39             return -1;
40     } else {
41         /* constant bitrate */
42         if(vorbis_encode_setup_managed(vi, avccontext->channels,
43                 avccontext->sample_rate, -1, avccontext->bit_rate, -1))
44             return -1;
45
46 #ifdef OGGVORBIS_VBR_BY_ESTIMATE
47         /* variable bitrate by estimate */
48         if(vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL))
49             return -1;
50 #endif
51     }
52
53     /* cutoff frequency */
54     if(avccontext->cutoff > 0) {
55         cfreq = avccontext->cutoff / 1000.0;
56         if(vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq))
57             return -1;
58     }
59
60     return vorbis_encode_setup_init(vi);
61 }
62
63 static int oggvorbis_encode_init(AVCodecContext *avccontext) {
64     OggVorbisContext *context = avccontext->priv_data ;
65     ogg_packet header, header_comm, header_code;
66     uint8_t *p;
67     unsigned int offset, len;
68
69     vorbis_info_init(&context->vi) ;
70     if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
71         av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed") ;
72         return -1 ;
73     }
74     vorbis_analysis_init(&context->vd, &context->vi) ;
75     vorbis_block_init(&context->vd, &context->vb) ;
76
77     vorbis_comment_init(&context->vc);
78     vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ;
79
80     vorbis_analysis_headerout(&context->vd, &context->vc, &header,
81                                 &header_comm, &header_code);
82
83     len = header.bytes + header_comm.bytes +  header_code.bytes;
84     avccontext->extradata_size= 64 + len + len/255;
85     p = avccontext->extradata= av_mallocz(avccontext->extradata_size);
86     p[0] = 2;
87     offset = 1;
88     offset += av_xiphlacing(&p[offset], header.bytes);
89     offset += av_xiphlacing(&p[offset], header_comm.bytes);
90     memcpy(&p[offset], header.packet, header.bytes);
91     offset += header.bytes;
92     memcpy(&p[offset], header_comm.packet, header_comm.bytes);
93     offset += header_comm.bytes;
94     memcpy(&p[offset], header_code.packet, header_code.bytes);
95     offset += header_code.bytes;
96     avccontext->extradata_size = offset;
97     avccontext->extradata= av_realloc(avccontext->extradata, avccontext->extradata_size);
98
99 /*    vorbis_block_clear(&context->vb);
100     vorbis_dsp_clear(&context->vd);
101     vorbis_info_clear(&context->vi);*/
102     vorbis_comment_clear(&context->vc);
103
104     avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
105
106     avccontext->coded_frame= avcodec_alloc_frame();
107     avccontext->coded_frame->key_frame= 1;
108
109     return 0 ;
110 }
111
112
113 static int oggvorbis_encode_frame(AVCodecContext *avccontext,
114                                   unsigned char *packets,
115                            int buf_size, void *data)
116 {
117     OggVorbisContext *context = avccontext->priv_data ;
118     float **buffer ;
119     ogg_packet op ;
120     signed short *audio = data ;
121     int l, samples = data ? OGGVORBIS_FRAME_SIZE : 0;
122
123     buffer = vorbis_analysis_buffer(&context->vd, samples) ;
124
125     if(context->vi.channels == 1) {
126         for(l = 0 ; l < samples ; l++)
127             buffer[0][l]=audio[l]/32768.f;
128     } else {
129         for(l = 0 ; l < samples ; l++){
130             buffer[0][l]=audio[l*2]/32768.f;
131             buffer[1][l]=audio[l*2+1]/32768.f;
132         }
133     }
134
135     vorbis_analysis_wrote(&context->vd, samples) ;
136
137     while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
138         vorbis_analysis(&context->vb, NULL);
139         vorbis_bitrate_addblock(&context->vb) ;
140
141         while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
142             if(op.bytes==1) //id love to say this is a hack, bad sadly its not, appearently the end of stream decission is in libogg
143                 continue;
144             memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
145             context->buffer_index += sizeof(ogg_packet);
146             memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
147             context->buffer_index += op.bytes;
148 //            av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
149         }
150     }
151
152     l=0;
153     if(context->buffer_index){
154         ogg_packet *op2= (ogg_packet*)context->buffer;
155         op2->packet = context->buffer + sizeof(ogg_packet);
156
157         l=  op2->bytes;
158         avccontext->coded_frame->pts= av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
159         //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate
160
161         memcpy(packets, op2->packet, l);
162         context->buffer_index -= l + sizeof(ogg_packet);
163         memcpy(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
164 //        av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
165     }
166
167     return l;
168 }
169
170
171 static int oggvorbis_encode_close(AVCodecContext *avccontext) {
172     OggVorbisContext *context = avccontext->priv_data ;
173 /*  ogg_packet op ; */
174
175     vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
176
177     vorbis_block_clear(&context->vb);
178     vorbis_dsp_clear(&context->vd);
179     vorbis_info_clear(&context->vi);
180
181     av_freep(&avccontext->coded_frame);
182     av_freep(&avccontext->extradata);
183
184     return 0 ;
185 }
186
187
188 AVCodec oggvorbis_encoder = {
189     "vorbis",
190     CODEC_TYPE_AUDIO,
191     CODEC_ID_VORBIS,
192     sizeof(OggVorbisContext),
193     oggvorbis_encode_init,
194     oggvorbis_encode_frame,
195     oggvorbis_encode_close,
196     .capabilities= CODEC_CAP_DELAY,
197 } ;
198
199 static int oggvorbis_decode_init(AVCodecContext *avccontext) {
200     OggVorbisContext *context = avccontext->priv_data ;
201     uint8_t *p= avccontext->extradata;
202     int i, hsizes[3];
203     unsigned char *headers[3], *extradata = avccontext->extradata;
204
205     vorbis_info_init(&context->vi) ;
206     vorbis_comment_init(&context->vc) ;
207
208     if(! avccontext->extradata_size || ! p) {
209         av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
210         return -1;
211     }
212
213     if(p[0] == 0 && p[1] == 30) {
214         for(i = 0; i < 3; i++){
215             hsizes[i] = *p++ << 8;
216             hsizes[i] += *p++;
217             headers[i] = p;
218             p += hsizes[i];
219         }
220     } else if(*p == 2) {
221         unsigned int offset = 1;
222         p++;
223         for(i=0; i<2; i++) {
224             hsizes[i] = 0;
225             while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
226                 hsizes[i] += 0xFF;
227                 offset++;
228                 p++;
229             }
230             if(offset >= avccontext->extradata_size - 1) {
231                 av_log(avccontext, AV_LOG_ERROR,
232                        "vorbis header sizes damaged\n");
233                 return -1;
234             }
235             hsizes[i] += *p;
236             offset++;
237             p++;
238         }
239         hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
240 #if 0
241         av_log(avccontext, AV_LOG_DEBUG,
242                "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
243                hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
244 #endif
245         headers[0] = extradata + offset;
246         headers[1] = extradata + offset + hsizes[0];
247         headers[2] = extradata + offset + hsizes[0] + hsizes[1];
248     } else {
249         av_log(avccontext, AV_LOG_ERROR,
250                "vorbis initial header len is wrong: %d\n", *p);
251         return -1;
252     }
253
254     for(i=0; i<3; i++){
255         context->op.b_o_s= i==0;
256         context->op.bytes = hsizes[i];
257         context->op.packet = headers[i];
258         if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
259             av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
260             return -1;
261         }
262     }
263
264     avccontext->channels = context->vi.channels;
265     avccontext->sample_rate = context->vi.rate;
266     avccontext->time_base= (AVRational){1, avccontext->sample_rate};
267
268     vorbis_synthesis_init(&context->vd, &context->vi);
269     vorbis_block_init(&context->vd, &context->vb);
270
271     return 0 ;
272 }
273
274
275 static inline int conv(int samples, float **pcm, char *buf, int channels) {
276     int i, j, val ;
277     ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
278     float *mono ;
279
280     for(i = 0 ; i < channels ; i++){
281         ptr = &data[i];
282         mono = pcm[i] ;
283
284         for(j = 0 ; j < samples ; j++) {
285
286             val = mono[j] * 32767.f;
287
288             if(val > 32767) val = 32767 ;
289             if(val < -32768) val = -32768 ;
290
291             *ptr = val ;
292             ptr += channels;
293         }
294     }
295
296     return 0 ;
297 }
298
299
300 static int oggvorbis_decode_frame(AVCodecContext *avccontext,
301                         void *data, int *data_size,
302                         uint8_t *buf, int buf_size)
303 {
304     OggVorbisContext *context = avccontext->priv_data ;
305     float **pcm ;
306     ogg_packet *op= &context->op;
307     int samples, total_samples, total_bytes;
308
309     if(!buf_size){
310     //FIXME flush
311         return 0;
312     }
313
314     op->packet = buf;
315     op->bytes  = buf_size;
316
317 //    av_log(avccontext, AV_LOG_DEBUG, "%d %d %d %lld %lld %d %d\n", op->bytes, op->b_o_s, op->e_o_s, op->granulepos, op->packetno, buf_size, context->vi.rate);
318
319 /*    for(i=0; i<op->bytes; i++)
320       av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
321     av_log(avccontext, AV_LOG_DEBUG, "\n");*/
322
323     if(vorbis_synthesis(&context->vb, op) == 0)
324         vorbis_synthesis_blockin(&context->vd, &context->vb) ;
325
326     total_samples = 0 ;
327     total_bytes = 0 ;
328
329     while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
330         conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ;
331         total_bytes += samples * 2 * context->vi.channels ;
332         total_samples += samples ;
333         vorbis_synthesis_read(&context->vd, samples) ;
334     }
335
336     *data_size = total_bytes ;
337     return buf_size ;
338 }
339
340
341 static int oggvorbis_decode_close(AVCodecContext *avccontext) {
342     OggVorbisContext *context = avccontext->priv_data ;
343
344     vorbis_info_clear(&context->vi) ;
345     vorbis_comment_clear(&context->vc) ;
346
347     return 0 ;
348 }
349
350
351 AVCodec oggvorbis_decoder = {
352     "vorbis",
353     CODEC_TYPE_AUDIO,
354     CODEC_ID_VORBIS,
355     sizeof(OggVorbisContext),
356     oggvorbis_decode_init,
357     NULL,
358     oggvorbis_decode_close,
359     oggvorbis_decode_frame,
360     .capabilities= CODEC_CAP_DELAY,
361 } ;