]> git.sesse.net Git - ffmpeg/blob - libavcodec/libvorbis.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / libvorbis.c
1 /*
2  * copyright (c) 2002 Mark Hills <mark@pogo.org.uk>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * Vorbis encoding support via libvorbisenc.
24  * @author Mark Hills <mark@pogo.org.uk>
25  */
26
27 #include <vorbis/vorbisenc.h>
28
29 #include "libavutil/fifo.h"
30 #include "libavutil/opt.h"
31 #include "avcodec.h"
32 #include "audio_frame_queue.h"
33 #include "bytestream.h"
34 #include "internal.h"
35 #include "vorbis.h"
36 #include "vorbis_parser.h"
37
38 #undef NDEBUG
39 #include <assert.h>
40
41 /* Number of samples the user should send in each call.
42  * This value is used because it is the LCD of all possible frame sizes, so
43  * an output packet will always start at the same point as one of the input
44  * packets.
45  */
46 #define OGGVORBIS_FRAME_SIZE 64
47
48 #define BUFFER_SIZE (1024 * 64)
49
50 typedef struct OggVorbisContext {
51     AVClass *av_class;                  /**< class for AVOptions            */
52     AVFrame frame;
53     vorbis_info vi;                     /**< vorbis_info used during init   */
54     vorbis_dsp_state vd;                /**< DSP state used for analysis    */
55     vorbis_block vb;                    /**< vorbis_block used for analysis */
56     AVFifoBuffer *pkt_fifo;             /**< output packet buffer           */
57     int eof;                            /**< end-of-file flag               */
58     int dsp_initialized;                /**< vd has been initialized        */
59     vorbis_comment vc;                  /**< VorbisComment info             */
60     ogg_packet op;                      /**< ogg packet                     */
61     double iblock;                      /**< impulse block bias option      */
62     VorbisParseContext vp;              /**< parse context to get durations */
63     AudioFrameQueue afq;                /**< frame queue for timestamps     */
64 } OggVorbisContext;
65
66 static const AVOption options[] = {
67     { "iblock", "Sets the impulse block bias", offsetof(OggVorbisContext, iblock), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -15, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
68     { NULL }
69 };
70
71 static const AVCodecDefault defaults[] = {
72     { "b",  "0" },
73     { NULL },
74 };
75
76 static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
77
78
79 static int vorbis_error_to_averror(int ov_err)
80 {
81     switch (ov_err) {
82     case OV_EFAULT: return AVERROR_BUG;
83     case OV_EINVAL: return AVERROR(EINVAL);
84     case OV_EIMPL:  return AVERROR(EINVAL);
85     default:        return AVERROR_UNKNOWN;
86     }
87 }
88
89 static av_cold int oggvorbis_init_encoder(vorbis_info *vi,
90                                           AVCodecContext *avctx)
91 {
92     OggVorbisContext *s = avctx->priv_data;
93     double cfreq;
94     int ret;
95
96     if (avctx->flags & CODEC_FLAG_QSCALE || !avctx->bit_rate) {
97         /* variable bitrate
98          * NOTE: we use the oggenc range of -1 to 10 for global_quality for
99          *       user convenience, but libvorbis uses -0.1 to 1.0.
100          */
101         float q = avctx->global_quality / (float)FF_QP2LAMBDA;
102         /* default to 3 if the user did not set quality or bitrate */
103         if (!(avctx->flags & CODEC_FLAG_QSCALE))
104             q = 3.0;
105         if ((ret = vorbis_encode_setup_vbr(vi, avctx->channels,
106                                            avctx->sample_rate,
107                                            q / 10.0)))
108             goto error;
109     } else {
110         int minrate = avctx->rc_min_rate > 0 ? avctx->rc_min_rate : -1;
111         int maxrate = avctx->rc_max_rate > 0 ? avctx->rc_max_rate : -1;
112
113         /* average bitrate */
114         if ((ret = vorbis_encode_setup_managed(vi, avctx->channels,
115                                                avctx->sample_rate, maxrate,
116                                                avctx->bit_rate, minrate)))
117             goto error;
118
119         /* variable bitrate by estimate, disable slow rate management */
120         if (minrate == -1 && maxrate == -1)
121             if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
122                 goto error; /* should not happen */
123     }
124
125     /* cutoff frequency */
126     if (avctx->cutoff > 0) {
127         cfreq = avctx->cutoff / 1000.0;
128         if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
129             goto error; /* should not happen */
130     }
131
132     /* impulse block bias */
133     if (s->iblock) {
134         if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->iblock)))
135             goto error;
136     }
137
138     if (avctx->channels == 3 &&
139             avctx->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
140         avctx->channels == 4 &&
141             avctx->channel_layout != AV_CH_LAYOUT_2_2 &&
142             avctx->channel_layout != AV_CH_LAYOUT_QUAD ||
143         avctx->channels == 5 &&
144             avctx->channel_layout != AV_CH_LAYOUT_5POINT0 &&
145             avctx->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
146         avctx->channels == 6 &&
147             avctx->channel_layout != AV_CH_LAYOUT_5POINT1 &&
148             avctx->channel_layout != AV_CH_LAYOUT_5POINT1_BACK ||
149         avctx->channels == 7 &&
150             avctx->channel_layout != (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER) ||
151         avctx->channels == 8 &&
152             avctx->channel_layout != AV_CH_LAYOUT_7POINT1) {
153         if (avctx->channel_layout) {
154             char name[32];
155             av_get_channel_layout_string(name, sizeof(name), avctx->channels,
156                                          avctx->channel_layout);
157             av_log(avctx, AV_LOG_ERROR, "%s not supported by Vorbis: "
158                                              "output stream will have incorrect "
159                                              "channel layout.\n", name);
160         } else {
161             av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
162                                                "will use Vorbis channel layout for "
163                                                "%d channels.\n", avctx->channels);
164         }
165     }
166
167     if ((ret = vorbis_encode_setup_init(vi)))
168         goto error;
169
170     return 0;
171 error:
172     return vorbis_error_to_averror(ret);
173 }
174
175 /* How many bytes are needed for a buffer of length 'l' */
176 static int xiph_len(int l)
177 {
178     return 1 + l / 255 + l;
179 }
180
181 static av_cold int oggvorbis_encode_close(AVCodecContext *avctx)
182 {
183     OggVorbisContext *s = avctx->priv_data;
184
185     /* notify vorbisenc this is EOF */
186     if (s->dsp_initialized)
187         vorbis_analysis_wrote(&s->vd, 0);
188
189     vorbis_block_clear(&s->vb);
190     vorbis_dsp_clear(&s->vd);
191     vorbis_info_clear(&s->vi);
192
193     av_fifo_free(s->pkt_fifo);
194     ff_af_queue_close(&s->afq);
195 #if FF_API_OLD_ENCODE_AUDIO
196     av_freep(&avctx->coded_frame);
197 #endif
198     av_freep(&avctx->extradata);
199
200     return 0;
201 }
202
203 static av_cold int oggvorbis_encode_init(AVCodecContext *avctx)
204 {
205     OggVorbisContext *s = avctx->priv_data;
206     ogg_packet header, header_comm, header_code;
207     uint8_t *p;
208     unsigned int offset;
209     int ret;
210
211     vorbis_info_init(&s->vi);
212     if ((ret = oggvorbis_init_encoder(&s->vi, avctx))) {
213         av_log(avctx, AV_LOG_ERROR, "encoder setup failed\n");
214         goto error;
215     }
216     if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) {
217         av_log(avctx, AV_LOG_ERROR, "analysis init failed\n");
218         ret = vorbis_error_to_averror(ret);
219         goto error;
220     }
221     s->dsp_initialized = 1;
222     if ((ret = vorbis_block_init(&s->vd, &s->vb))) {
223         av_log(avctx, AV_LOG_ERROR, "dsp init failed\n");
224         ret = vorbis_error_to_averror(ret);
225         goto error;
226     }
227
228     vorbis_comment_init(&s->vc);
229     vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
230
231     if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
232                                          &header_code))) {
233         ret = vorbis_error_to_averror(ret);
234         goto error;
235     }
236
237     avctx->extradata_size = 1 + xiph_len(header.bytes)      +
238                                 xiph_len(header_comm.bytes) +
239                                 header_code.bytes;
240     p = avctx->extradata = av_malloc(avctx->extradata_size +
241                                      FF_INPUT_BUFFER_PADDING_SIZE);
242     if (!p) {
243         ret = AVERROR(ENOMEM);
244         goto error;
245     }
246     p[0]    = 2;
247     offset  = 1;
248     offset += av_xiphlacing(&p[offset], header.bytes);
249     offset += av_xiphlacing(&p[offset], header_comm.bytes);
250     memcpy(&p[offset], header.packet, header.bytes);
251     offset += header.bytes;
252     memcpy(&p[offset], header_comm.packet, header_comm.bytes);
253     offset += header_comm.bytes;
254     memcpy(&p[offset], header_code.packet, header_code.bytes);
255     offset += header_code.bytes;
256     assert(offset == avctx->extradata_size);
257
258     if ((ret = avpriv_vorbis_parse_extradata(avctx, &s->vp)) < 0) {
259         av_log(avctx, AV_LOG_ERROR, "invalid extradata\n");
260         return ret;
261     }
262
263     vorbis_comment_clear(&s->vc);
264
265     avctx->frame_size = OGGVORBIS_FRAME_SIZE;
266     ff_af_queue_init(avctx, &s->afq);
267
268     s->pkt_fifo = av_fifo_alloc(BUFFER_SIZE);
269     if (!s->pkt_fifo) {
270         ret = AVERROR(ENOMEM);
271         goto error;
272     }
273
274 #if FF_API_OLD_ENCODE_AUDIO
275     avctx->coded_frame = avcodec_alloc_frame();
276     if (!avctx->coded_frame) {
277         ret = AVERROR(ENOMEM);
278         goto error;
279     }
280 #endif
281
282     return 0;
283 error:
284     oggvorbis_encode_close(avctx);
285     return ret;
286 }
287
288 static int oggvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
289                                   const AVFrame *frame, int *got_packet_ptr)
290 {
291     OggVorbisContext *s = avctx->priv_data;
292     ogg_packet op;
293     int ret, duration;
294
295     /* send samples to libvorbis */
296     if (frame) {
297         const float *audio = (const float *)frame->data[0];
298         const int samples = frame->nb_samples;
299         float **buffer;
300         int c, channels = s->vi.channels;
301
302         buffer = vorbis_analysis_buffer(&s->vd, samples);
303         for (c = 0; c < channels; c++) {
304             int i;
305             int co = (channels > 8) ? c :
306                      ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
307             for (i = 0; i < samples; i++)
308                 buffer[c][i] = audio[i * channels + co];
309         }
310         if ((ret = vorbis_analysis_wrote(&s->vd, samples)) < 0) {
311             av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
312             return vorbis_error_to_averror(ret);
313         }
314         if ((ret = ff_af_queue_add(&s->afq, frame) < 0))
315             return ret;
316     } else {
317         if (!s->eof)
318             if ((ret = vorbis_analysis_wrote(&s->vd, 0)) < 0) {
319                 av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
320                 return vorbis_error_to_averror(ret);
321             }
322         s->eof = 1;
323     }
324
325     /* retrieve available packets from libvorbis */
326     while ((ret = vorbis_analysis_blockout(&s->vd, &s->vb)) == 1) {
327         if ((ret = vorbis_analysis(&s->vb, NULL)) < 0)
328             break;
329         if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0)
330             break;
331
332         /* add any available packets to the output packet buffer */
333         while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) {
334             if (av_fifo_space(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) {
335                 av_log(avctx, AV_LOG_ERROR, "packet buffer is too small\n");
336                 return AVERROR_BUG;
337             }
338             av_fifo_generic_write(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
339             av_fifo_generic_write(s->pkt_fifo, op.packet, op.bytes, NULL);
340         }
341         if (ret < 0) {
342             av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
343             break;
344         }
345     }
346     if (ret < 0) {
347         av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
348         return vorbis_error_to_averror(ret);
349     }
350
351     /* check for available packets */
352     if (av_fifo_size(s->pkt_fifo) < sizeof(ogg_packet))
353         return 0;
354
355     av_fifo_generic_read(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
356
357     if ((ret = ff_alloc_packet2(avctx, avpkt, op.bytes)))
358         return ret;
359     av_fifo_generic_read(s->pkt_fifo, avpkt->data, op.bytes, NULL);
360
361     avpkt->pts = ff_samples_to_time_base(avctx, op.granulepos);
362
363     duration = avpriv_vorbis_parse_frame(&s->vp, avpkt->data, avpkt->size);
364     if (duration > 0) {
365         /* we do not know encoder delay until we get the first packet from
366          * libvorbis, so we have to update the AudioFrameQueue counts */
367         if (!avctx->delay) {
368             avctx->delay              = duration;
369             s->afq.remaining_delay   += duration;
370             s->afq.remaining_samples += duration;
371         }
372         ff_af_queue_remove(&s->afq, duration, &avpkt->pts, &avpkt->duration);
373     }
374
375     *got_packet_ptr = 1;
376     return 0;
377 }
378
379 AVCodec ff_libvorbis_encoder = {
380     .name           = "libvorbis",
381     .type           = AVMEDIA_TYPE_AUDIO,
382     .id             = CODEC_ID_VORBIS,
383     .priv_data_size = sizeof(OggVorbisContext),
384     .init           = oggvorbis_encode_init,
385     .encode2        = oggvorbis_encode_frame,
386     .close          = oggvorbis_encode_close,
387     .capabilities   = CODEC_CAP_DELAY,
388     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
389                                                       AV_SAMPLE_FMT_NONE },
390     .long_name      = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
391     .priv_class     = &class,
392     .defaults       = defaults,
393 };
394
395 static int oggvorbis_decode_init(AVCodecContext *avccontext) {
396     OggVorbisContext *context = avccontext->priv_data ;
397     uint8_t *p= avccontext->extradata;
398     int i, hsizes[3];
399     unsigned char *headers[3], *extradata = avccontext->extradata;
400
401     vorbis_info_init(&context->vi) ;
402     vorbis_comment_init(&context->vc) ;
403
404     if(! avccontext->extradata_size || ! p) {
405         av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n");
406         return -1;
407     }
408
409     if(p[0] == 0 && p[1] == 30) {
410         for(i = 0; i < 3; i++){
411             hsizes[i] = bytestream_get_be16(&p);
412             headers[i] = p;
413             p += hsizes[i];
414         }
415     } else if(*p == 2) {
416         unsigned int offset = 1;
417         p++;
418         for(i=0; i<2; i++) {
419             hsizes[i] = 0;
420             while((*p == 0xFF) && (offset < avccontext->extradata_size)) {
421                 hsizes[i] += 0xFF;
422                 offset++;
423                 p++;
424             }
425             if(offset >= avccontext->extradata_size - 1) {
426                 av_log(avccontext, AV_LOG_ERROR,
427                        "vorbis header sizes damaged\n");
428                 return -1;
429             }
430             hsizes[i] += *p;
431             offset++;
432             p++;
433         }
434         hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset;
435 #if 0
436         av_log(avccontext, AV_LOG_DEBUG,
437                "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n",
438                hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size);
439 #endif
440         headers[0] = extradata + offset;
441         headers[1] = extradata + offset + hsizes[0];
442         headers[2] = extradata + offset + hsizes[0] + hsizes[1];
443     } else {
444         av_log(avccontext, AV_LOG_ERROR,
445                "vorbis initial header len is wrong: %d\n", *p);
446         return -1;
447     }
448
449     for(i=0; i<3; i++){
450         context->op.b_o_s= i==0;
451         context->op.bytes = hsizes[i];
452         context->op.packet = headers[i];
453         if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){
454             av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1);
455             return -1;
456         }
457     }
458
459     avccontext->channels = context->vi.channels;
460     avccontext->sample_rate = context->vi.rate;
461     avccontext->time_base= (AVRational){1, avccontext->sample_rate};
462
463     vorbis_synthesis_init(&context->vd, &context->vi);
464     vorbis_block_init(&context->vd, &context->vb);
465
466     return 0 ;
467 }
468
469
470 static inline int conv(int samples, float **pcm, char *buf, int channels) {
471     int i, j;
472     ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ;
473     float *mono ;
474
475     for(i = 0 ; i < channels ; i++){
476         ptr = &data[i];
477         mono = pcm[i] ;
478
479         for(j = 0 ; j < samples ; j++) {
480             *ptr = av_clip_int16(mono[j] * 32767.f);
481             ptr += channels;
482         }
483     }
484
485     return 0 ;
486 }
487
488 static int oggvorbis_decode_frame(AVCodecContext *avccontext, void *data,
489                         int *got_frame_ptr, AVPacket *avpkt)
490 {
491     OggVorbisContext *context = avccontext->priv_data ;
492     float **pcm ;
493     ogg_packet *op= &context->op;
494     int samples, total_samples, total_bytes;
495     int ret;
496     int16_t *output;
497
498     if(!avpkt->size){
499     //FIXME flush
500         return 0;
501     }
502
503     context->frame.nb_samples = 8192*4;
504     if ((ret = avccontext->get_buffer(avccontext, &context->frame)) < 0) {
505         av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
506         return ret;
507     }
508     output = (int16_t *)context->frame.data[0];
509
510
511     op->packet = avpkt->data;
512     op->bytes  = avpkt->size;
513
514 //    av_log(avccontext, AV_LOG_DEBUG, "%d %d %d %"PRId64" %"PRId64" %d %d\n", op->bytes, op->b_o_s, op->e_o_s, op->granulepos, op->packetno, buf_size, context->vi.rate);
515
516 /*    for(i=0; i<op->bytes; i++)
517       av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]);
518     av_log(avccontext, AV_LOG_DEBUG, "\n");*/
519
520     if(vorbis_synthesis(&context->vb, op) == 0)
521         vorbis_synthesis_blockin(&context->vd, &context->vb) ;
522
523     total_samples = 0 ;
524     total_bytes = 0 ;
525
526     while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) {
527         conv(samples, pcm, (char*)output + total_bytes, context->vi.channels) ;
528         total_bytes += samples * 2 * context->vi.channels ;
529         total_samples += samples ;
530         vorbis_synthesis_read(&context->vd, samples) ;
531     }
532
533     context->frame.nb_samples = total_samples;
534     *got_frame_ptr   = 1;
535     *(AVFrame *)data = context->frame;
536     return avpkt->size;
537 }
538
539
540 static int oggvorbis_decode_close(AVCodecContext *avccontext) {
541     OggVorbisContext *context = avccontext->priv_data ;
542
543     vorbis_info_clear(&context->vi) ;
544     vorbis_comment_clear(&context->vc) ;
545
546     return 0 ;
547 }
548
549
550 AVCodec ff_libvorbis_decoder = {
551   .name           = "libvorbis",
552   .type           = AVMEDIA_TYPE_AUDIO,
553   .id             = CODEC_ID_VORBIS,
554   .priv_data_size = sizeof(OggVorbisContext),
555   .init           = oggvorbis_decode_init,
556   .decode         = oggvorbis_decode_frame,
557   .close          = oggvorbis_decode_close,
558   .capabilities   = CODEC_CAP_DELAY,
559 } ;
560