Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / libavcodec / libvorbisenc.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 #include <vorbis/vorbisenc.h>
22
23 #include "libavutil/avassert.h"
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/fifo.h"
26 #include "libavutil/opt.h"
27 #include "avcodec.h"
28 #include "audio_frame_queue.h"
29 #include "codec_internal.h"
30 #include "encode.h"
31 #include "version.h"
32 #include "vorbis_parser.h"
33
34
35 /* Number of samples the user should send in each call.
36  * This value is used because it is the LCD of all possible frame sizes, so
37  * an output packet will always start at the same point as one of the input
38  * packets.
39  */
40 #define LIBVORBIS_FRAME_SIZE 64
41
42 #define BUFFER_SIZE (1024 * 64)
43
44 typedef struct LibvorbisEncContext {
45     AVClass *av_class;                  /**< class for AVOptions            */
46     vorbis_info vi;                     /**< vorbis_info used during init   */
47     vorbis_dsp_state vd;                /**< DSP state used for analysis    */
48     vorbis_block vb;                    /**< vorbis_block used for analysis */
49     AVFifo *pkt_fifo;                   /**< output packet buffer           */
50     int eof;                            /**< end-of-file flag               */
51     int dsp_initialized;                /**< vd has been initialized        */
52     vorbis_comment vc;                  /**< VorbisComment info             */
53     double iblock;                      /**< impulse block bias option      */
54     AVVorbisParseContext *vp;           /**< parse context to get durations */
55     AudioFrameQueue afq;                /**< frame queue for timestamps     */
56 } LibvorbisEncContext;
57
58 static const AVOption options[] = {
59     { "iblock", "Sets the impulse block bias", offsetof(LibvorbisEncContext, iblock), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, -15, 0, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
60     { NULL }
61 };
62
63 static const FFCodecDefault defaults[] = {
64     { "b",  "0" },
65     { NULL },
66 };
67
68 static const AVClass vorbis_class = {
69     .class_name = "libvorbis",
70     .item_name  = av_default_item_name,
71     .option     = options,
72     .version    = LIBAVUTIL_VERSION_INT,
73 };
74
75 static const uint8_t vorbis_encoding_channel_layout_offsets[8][8] = {
76     { 0 },
77     { 0, 1 },
78     { 0, 2, 1 },
79     { 0, 1, 2, 3 },
80     { 0, 2, 1, 3, 4 },
81     { 0, 2, 1, 4, 5, 3 },
82     { 0, 2, 1, 5, 6, 4, 3 },
83     { 0, 2, 1, 6, 7, 4, 5, 3 },
84 };
85
86 static int vorbis_error_to_averror(int ov_err)
87 {
88     switch (ov_err) {
89     case OV_EFAULT: return AVERROR_BUG;
90     case OV_EINVAL: return AVERROR(EINVAL);
91     case OV_EIMPL:  return AVERROR(EINVAL);
92     default:        return AVERROR_UNKNOWN;
93     }
94 }
95
96 static av_cold int libvorbis_setup(vorbis_info *vi, AVCodecContext *avctx)
97 {
98     LibvorbisEncContext *s = avctx->priv_data;
99     int channels = avctx->ch_layout.nb_channels;
100     double cfreq;
101     int ret;
102
103     if (avctx->flags & AV_CODEC_FLAG_QSCALE || !avctx->bit_rate) {
104         /* variable bitrate
105          * NOTE: we use the oggenc range of -1 to 10 for global_quality for
106          *       user convenience, but libvorbis uses -0.1 to 1.0.
107          */
108         float q = avctx->global_quality / (float)FF_QP2LAMBDA;
109         /* default to 3 if the user did not set quality or bitrate */
110         if (!(avctx->flags & AV_CODEC_FLAG_QSCALE))
111             q = 3.0;
112         if ((ret = vorbis_encode_setup_vbr(vi, channels,
113                                            avctx->sample_rate,
114                                            q / 10.0)))
115             goto error;
116     } else {
117         int minrate = avctx->rc_min_rate > 0 ? avctx->rc_min_rate : -1;
118         int maxrate = avctx->rc_max_rate > 0 ? avctx->rc_max_rate : -1;
119
120         /* average bitrate */
121         if ((ret = vorbis_encode_setup_managed(vi, channels,
122                                                avctx->sample_rate, maxrate,
123                                                avctx->bit_rate, minrate)))
124             goto error;
125
126         /* variable bitrate by estimate, disable slow rate management */
127         if (minrate == -1 && maxrate == -1)
128             if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
129                 goto error; /* should not happen */
130     }
131
132     /* cutoff frequency */
133     if (avctx->cutoff > 0) {
134         cfreq = avctx->cutoff / 1000.0;
135         if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
136             goto error; /* should not happen */
137     }
138
139     /* impulse block bias */
140     if (s->iblock) {
141         if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->iblock)))
142             goto error;
143     }
144
145     if ((channels == 3 &&
146          av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_SURROUND)) ||
147         (channels == 4 &&
148          av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2_2) &&
149          av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD)) ||
150         (channels == 5 &&
151          av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0) &&
152          av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0_BACK)) ||
153         (channels == 6 &&
154          av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1) &&
155          av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK)) ||
156         (channels == 7 &&
157          av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_6POINT1)) ||
158         (channels == 8 &&
159          av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_7POINT1))) {
160         if (avctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
161             char name[32];
162             av_channel_layout_describe(&avctx->ch_layout, name, sizeof(name));
163             av_log(avctx, AV_LOG_ERROR, "%s not supported by Vorbis: "
164                                              "output stream will have incorrect "
165                                              "channel layout.\n", name);
166         } else {
167             av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
168                                                "will use Vorbis channel layout for "
169                                                "%d channels.\n", channels);
170         }
171     }
172
173     if ((ret = vorbis_encode_setup_init(vi)))
174         goto error;
175
176     return 0;
177 error:
178     return vorbis_error_to_averror(ret);
179 }
180
181 /* How many bytes are needed for a buffer of length 'l' */
182 static int xiph_len(int l)
183 {
184     return 1 + l / 255 + l;
185 }
186
187 static av_cold int libvorbis_encode_close(AVCodecContext *avctx)
188 {
189     LibvorbisEncContext *s = avctx->priv_data;
190
191     /* notify vorbisenc this is EOF */
192     if (s->dsp_initialized)
193         vorbis_analysis_wrote(&s->vd, 0);
194
195     vorbis_block_clear(&s->vb);
196     vorbis_dsp_clear(&s->vd);
197     vorbis_info_clear(&s->vi);
198
199     av_fifo_freep2(&s->pkt_fifo);
200     ff_af_queue_close(&s->afq);
201
202     av_vorbis_parse_free(&s->vp);
203
204     return 0;
205 }
206
207 static av_cold int libvorbis_encode_init(AVCodecContext *avctx)
208 {
209     LibvorbisEncContext *s = avctx->priv_data;
210     ogg_packet header, header_comm, header_code;
211     uint8_t *p;
212     unsigned int offset;
213     int ret;
214
215     vorbis_info_init(&s->vi);
216     if ((ret = libvorbis_setup(&s->vi, avctx))) {
217         av_log(avctx, AV_LOG_ERROR, "encoder setup failed\n");
218         goto error;
219     }
220     if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) {
221         av_log(avctx, AV_LOG_ERROR, "analysis init failed\n");
222         ret = vorbis_error_to_averror(ret);
223         goto error;
224     }
225     s->dsp_initialized = 1;
226     if ((ret = vorbis_block_init(&s->vd, &s->vb))) {
227         av_log(avctx, AV_LOG_ERROR, "dsp init failed\n");
228         ret = vorbis_error_to_averror(ret);
229         goto error;
230     }
231
232     vorbis_comment_init(&s->vc);
233     if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
234         vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
235
236     if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
237                                          &header_code))) {
238         ret = vorbis_error_to_averror(ret);
239         goto error;
240     }
241
242     avctx->extradata_size = 1 + xiph_len(header.bytes)      +
243                                 xiph_len(header_comm.bytes) +
244                                 header_code.bytes;
245     p = avctx->extradata = av_malloc(avctx->extradata_size +
246                                      AV_INPUT_BUFFER_PADDING_SIZE);
247     if (!p) {
248         ret = AVERROR(ENOMEM);
249         goto error;
250     }
251     p[0]    = 2;
252     offset  = 1;
253     offset += av_xiphlacing(&p[offset], header.bytes);
254     offset += av_xiphlacing(&p[offset], header_comm.bytes);
255     memcpy(&p[offset], header.packet, header.bytes);
256     offset += header.bytes;
257     memcpy(&p[offset], header_comm.packet, header_comm.bytes);
258     offset += header_comm.bytes;
259     memcpy(&p[offset], header_code.packet, header_code.bytes);
260     offset += header_code.bytes;
261     av_assert0(offset == avctx->extradata_size);
262
263     s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size);
264     if (!s->vp) {
265         av_log(avctx, AV_LOG_ERROR, "invalid extradata\n");
266         return ret;
267     }
268
269     vorbis_comment_clear(&s->vc);
270
271     avctx->frame_size = LIBVORBIS_FRAME_SIZE;
272     ff_af_queue_init(avctx, &s->afq);
273
274     s->pkt_fifo = av_fifo_alloc2(BUFFER_SIZE, 1, 0);
275     if (!s->pkt_fifo) {
276         ret = AVERROR(ENOMEM);
277         goto error;
278     }
279
280     return 0;
281 error:
282     libvorbis_encode_close(avctx);
283     return ret;
284 }
285
286 static int libvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
287                                   const AVFrame *frame, int *got_packet_ptr)
288 {
289     LibvorbisEncContext *s = avctx->priv_data;
290     ogg_packet op;
291     int ret, duration;
292
293     /* send samples to libvorbis */
294     if (frame) {
295         const int samples = frame->nb_samples;
296         float **buffer;
297         int c, channels = s->vi.channels;
298
299         buffer = vorbis_analysis_buffer(&s->vd, samples);
300         for (c = 0; c < channels; c++) {
301             int co = (channels > 8) ? c :
302                      vorbis_encoding_channel_layout_offsets[channels - 1][c];
303             memcpy(buffer[c], frame->extended_data[co],
304                    samples * sizeof(*buffer[c]));
305         }
306         if ((ret = vorbis_analysis_wrote(&s->vd, samples)) < 0) {
307             av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
308             return vorbis_error_to_averror(ret);
309         }
310         if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
311             return ret;
312     } else {
313         if (!s->eof && s->afq.frame_alloc)
314             if ((ret = vorbis_analysis_wrote(&s->vd, 0)) < 0) {
315                 av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
316                 return vorbis_error_to_averror(ret);
317             }
318         s->eof = 1;
319     }
320
321     /* retrieve available packets from libvorbis */
322     while ((ret = vorbis_analysis_blockout(&s->vd, &s->vb)) == 1) {
323         if ((ret = vorbis_analysis(&s->vb, NULL)) < 0)
324             break;
325         if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0)
326             break;
327
328         /* add any available packets to the output packet buffer */
329         while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) {
330             if (av_fifo_can_write(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) {
331                 av_log(avctx, AV_LOG_ERROR, "packet buffer is too small\n");
332                 return AVERROR_BUG;
333             }
334             av_fifo_write(s->pkt_fifo, &op, sizeof(ogg_packet));
335             av_fifo_write(s->pkt_fifo, op.packet, op.bytes);
336         }
337         if (ret < 0) {
338             av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
339             break;
340         }
341     }
342     if (ret < 0) {
343         av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
344         return vorbis_error_to_averror(ret);
345     }
346
347     /* Read an available packet if possible */
348     if (av_fifo_read(s->pkt_fifo, &op, sizeof(ogg_packet)) < 0)
349         return 0;
350
351     if ((ret = ff_get_encode_buffer(avctx, avpkt, op.bytes, 0)) < 0)
352         return ret;
353     av_fifo_read(s->pkt_fifo, avpkt->data, op.bytes);
354
355     avpkt->pts = ff_samples_to_time_base(avctx, op.granulepos);
356
357     duration = av_vorbis_parse_frame(s->vp, avpkt->data, avpkt->size);
358     if (duration > 0) {
359         /* we do not know encoder delay until we get the first packet from
360          * libvorbis, so we have to update the AudioFrameQueue counts */
361         if (!avctx->initial_padding && s->afq.frames) {
362             avctx->initial_padding    = duration;
363             av_assert0(!s->afq.remaining_delay);
364             s->afq.frames->duration  += duration;
365             if (s->afq.frames->pts != AV_NOPTS_VALUE)
366                 s->afq.frames->pts       -= duration;
367             s->afq.remaining_samples += duration;
368         }
369         ff_af_queue_remove(&s->afq, duration, &avpkt->pts, &avpkt->duration);
370     }
371
372     *got_packet_ptr = 1;
373     return 0;
374 }
375
376 const FFCodec ff_libvorbis_encoder = {
377     .p.name         = "libvorbis",
378     CODEC_LONG_NAME("libvorbis"),
379     .p.type         = AVMEDIA_TYPE_AUDIO,
380     .p.id           = AV_CODEC_ID_VORBIS,
381     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
382                       AV_CODEC_CAP_SMALL_LAST_FRAME,
383     .caps_internal  = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
384     .priv_data_size = sizeof(LibvorbisEncContext),
385     .init           = libvorbis_encode_init,
386     FF_CODEC_ENCODE_CB(libvorbis_encode_frame),
387     .close          = libvorbis_encode_close,
388     .p.sample_fmts  = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
389                                                       AV_SAMPLE_FMT_NONE },
390     .p.priv_class   = &vorbis_class,
391     .defaults       = defaults,
392     .p.wrapper_name = "libvorbis",
393 };