2 * Copyright (c) 2002 Mark Hills <mark@pogo.org.uk>
4 * This file is part of FFmpeg.
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.
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.
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
21 #include <vorbis/vorbisenc.h>
23 #include "libavutil/avassert.h"
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/fifo.h"
26 #include "libavutil/opt.h"
28 #include "audio_frame_queue.h"
29 #include "codec_internal.h"
32 #include "vorbis_parser.h"
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
40 #define LIBVORBIS_FRAME_SIZE 64
42 #define BUFFER_SIZE (1024 * 64)
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;
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 },
63 static const FFCodecDefault defaults[] = {
68 static const AVClass vorbis_class = {
69 .class_name = "libvorbis",
70 .item_name = av_default_item_name,
72 .version = LIBAVUTIL_VERSION_INT,
75 static const uint8_t vorbis_encoding_channel_layout_offsets[8][8] = {
82 { 0, 2, 1, 5, 6, 4, 3 },
83 { 0, 2, 1, 6, 7, 4, 5, 3 },
86 static int vorbis_error_to_averror(int 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;
96 static av_cold int libvorbis_setup(vorbis_info *vi, AVCodecContext *avctx)
98 LibvorbisEncContext *s = avctx->priv_data;
99 int channels = avctx->ch_layout.nb_channels;
103 if (avctx->flags & AV_CODEC_FLAG_QSCALE || !avctx->bit_rate) {
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.
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))
112 if ((ret = vorbis_encode_setup_vbr(vi, channels,
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;
120 /* average bitrate */
121 if ((ret = vorbis_encode_setup_managed(vi, channels,
122 avctx->sample_rate, maxrate,
123 avctx->bit_rate, minrate)))
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 */
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 */
139 /* impulse block bias */
141 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->iblock)))
145 if ((channels == 3 &&
146 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_SURROUND)) ||
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)) ||
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)) ||
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)) ||
157 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_6POINT1)) ||
159 av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_7POINT1))) {
160 if (avctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
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);
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);
173 if ((ret = vorbis_encode_setup_init(vi)))
178 return vorbis_error_to_averror(ret);
181 /* How many bytes are needed for a buffer of length 'l' */
182 static int xiph_len(int l)
184 return 1 + l / 255 + l;
187 static av_cold int libvorbis_encode_close(AVCodecContext *avctx)
189 LibvorbisEncContext *s = avctx->priv_data;
191 /* notify vorbisenc this is EOF */
192 if (s->dsp_initialized)
193 vorbis_analysis_wrote(&s->vd, 0);
195 vorbis_block_clear(&s->vb);
196 vorbis_dsp_clear(&s->vd);
197 vorbis_info_clear(&s->vi);
199 av_fifo_freep2(&s->pkt_fifo);
200 ff_af_queue_close(&s->afq);
202 av_vorbis_parse_free(&s->vp);
207 static av_cold int libvorbis_encode_init(AVCodecContext *avctx)
209 LibvorbisEncContext *s = avctx->priv_data;
210 ogg_packet header, header_comm, header_code;
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");
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);
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);
232 vorbis_comment_init(&s->vc);
233 if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT))
234 vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
236 if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
238 ret = vorbis_error_to_averror(ret);
242 avctx->extradata_size = 1 + xiph_len(header.bytes) +
243 xiph_len(header_comm.bytes) +
245 p = avctx->extradata = av_malloc(avctx->extradata_size +
246 AV_INPUT_BUFFER_PADDING_SIZE);
248 ret = AVERROR(ENOMEM);
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);
263 s->vp = av_vorbis_parse_init(avctx->extradata, avctx->extradata_size);
265 av_log(avctx, AV_LOG_ERROR, "invalid extradata\n");
269 vorbis_comment_clear(&s->vc);
271 avctx->frame_size = LIBVORBIS_FRAME_SIZE;
272 ff_af_queue_init(avctx, &s->afq);
274 s->pkt_fifo = av_fifo_alloc2(BUFFER_SIZE, 1, 0);
276 ret = AVERROR(ENOMEM);
282 libvorbis_encode_close(avctx);
286 static int libvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
287 const AVFrame *frame, int *got_packet_ptr)
289 LibvorbisEncContext *s = avctx->priv_data;
293 /* send samples to libvorbis */
295 const int samples = frame->nb_samples;
297 int c, channels = s->vi.channels;
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]));
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);
310 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
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);
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)
325 if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0)
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");
334 av_fifo_write(s->pkt_fifo, &op, sizeof(ogg_packet));
335 av_fifo_write(s->pkt_fifo, op.packet, op.bytes);
338 av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
343 av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
344 return vorbis_error_to_averror(ret);
347 /* Read an available packet if possible */
348 if (av_fifo_read(s->pkt_fifo, &op, sizeof(ogg_packet)) < 0)
351 if ((ret = ff_get_encode_buffer(avctx, avpkt, op.bytes, 0)) < 0)
353 av_fifo_read(s->pkt_fifo, avpkt->data, op.bytes);
355 avpkt->pts = ff_samples_to_time_base(avctx, op.granulepos);
357 duration = av_vorbis_parse_frame(s->vp, avpkt->data, avpkt->size);
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;
369 ff_af_queue_remove(&s->afq, duration, &avpkt->pts, &avpkt->duration);
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",