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
23 * Ogg Vorbis codec support via libvorbisenc.
24 * @author Mark Hills <mark@pogo.org.uk>
27 #include <vorbis/vorbisenc.h>
29 #include "libavutil/opt.h"
31 #include "bytestream.h"
38 #define OGGVORBIS_FRAME_SIZE 64
40 #define BUFFER_SIZE (1024 * 64)
42 typedef struct OggVorbisContext {
47 uint8_t buffer[BUFFER_SIZE];
58 static const AVOption options[] = {
59 { "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 },
62 static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
64 static int vorbis_error_to_averror(int ov_err)
67 case OV_EFAULT: return AVERROR_BUG;
68 case OV_EINVAL: return AVERROR(EINVAL);
69 case OV_EIMPL: return AVERROR(EINVAL);
70 default: return AVERROR_UNKNOWN;
74 static av_cold int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext)
76 OggVorbisContext *context = avccontext->priv_data;
80 if (avccontext->flags & CODEC_FLAG_QSCALE) {
81 /* variable bitrate */
82 float q = avccontext->global_quality / (float)FF_QP2LAMBDA;
83 if ((ret = vorbis_encode_setup_vbr(vi, avccontext->channels,
84 avccontext->sample_rate,
88 int minrate = avccontext->rc_min_rate > 0 ? avccontext->rc_min_rate : -1;
89 int maxrate = avccontext->rc_min_rate > 0 ? avccontext->rc_max_rate : -1;
91 /* constant bitrate */
92 if ((ret = vorbis_encode_setup_managed(vi, avccontext->channels,
93 avccontext->sample_rate, minrate,
94 avccontext->bit_rate, maxrate)))
97 /* variable bitrate by estimate, disable slow rate management */
98 if (minrate == -1 && maxrate == -1)
99 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
100 goto error; /* should not happen */
103 /* cutoff frequency */
104 if (avccontext->cutoff > 0) {
105 cfreq = avccontext->cutoff / 1000.0;
106 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
107 goto error; /* should not happen */
110 if (context->iblock) {
111 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &context->iblock)))
115 if (avccontext->channels == 3 &&
116 avccontext->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
117 avccontext->channels == 4 &&
118 avccontext->channel_layout != AV_CH_LAYOUT_2_2 &&
119 avccontext->channel_layout != AV_CH_LAYOUT_QUAD ||
120 avccontext->channels == 5 &&
121 avccontext->channel_layout != AV_CH_LAYOUT_5POINT0 &&
122 avccontext->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
123 avccontext->channels == 6 &&
124 avccontext->channel_layout != AV_CH_LAYOUT_5POINT1 &&
125 avccontext->channel_layout != AV_CH_LAYOUT_5POINT1_BACK ||
126 avccontext->channels == 7 &&
127 avccontext->channel_layout != (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER) ||
128 avccontext->channels == 8 &&
129 avccontext->channel_layout != AV_CH_LAYOUT_7POINT1) {
130 if (avccontext->channel_layout) {
132 av_get_channel_layout_string(name, sizeof(name), avccontext->channels,
133 avccontext->channel_layout);
134 av_log(avccontext, AV_LOG_ERROR, "%s not supported by Vorbis: "
135 "output stream will have incorrect "
136 "channel layout.\n", name);
138 av_log(avccontext, AV_LOG_WARNING, "No channel layout specified. The encoder "
139 "will use Vorbis channel layout for "
140 "%d channels.\n", avccontext->channels);
144 if ((ret = vorbis_encode_setup_init(vi)))
149 return vorbis_error_to_averror(ret);
152 /* How many bytes are needed for a buffer of length 'l' */
153 static int xiph_len(int l)
155 return 1 + l / 255 + l;
158 static av_cold int oggvorbis_encode_close(AVCodecContext *avccontext)
160 OggVorbisContext *context = avccontext->priv_data;
161 /* ogg_packet op ; */
163 vorbis_analysis_wrote(&context->vd, 0); /* notify vorbisenc this is EOF */
165 vorbis_block_clear(&context->vb);
166 vorbis_dsp_clear(&context->vd);
167 vorbis_info_clear(&context->vi);
169 av_freep(&avccontext->coded_frame);
170 av_freep(&avccontext->extradata);
175 static av_cold int oggvorbis_encode_init(AVCodecContext *avccontext)
177 OggVorbisContext *context = avccontext->priv_data;
178 ogg_packet header, header_comm, header_code;
183 vorbis_info_init(&context->vi);
184 if ((ret = oggvorbis_init_encoder(&context->vi, avccontext))) {
185 av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n");
188 if ((ret = vorbis_analysis_init(&context->vd, &context->vi))) {
189 ret = vorbis_error_to_averror(ret);
192 if ((ret = vorbis_block_init(&context->vd, &context->vb))) {
193 ret = vorbis_error_to_averror(ret);
197 vorbis_comment_init(&context->vc);
198 vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT);
200 if ((ret = vorbis_analysis_headerout(&context->vd, &context->vc, &header,
201 &header_comm, &header_code))) {
202 ret = vorbis_error_to_averror(ret);
206 avccontext->extradata_size =
207 1 + xiph_len(header.bytes) + xiph_len(header_comm.bytes) +
209 p = avccontext->extradata =
210 av_malloc(avccontext->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
212 ret = AVERROR(ENOMEM);
217 offset += av_xiphlacing(&p[offset], header.bytes);
218 offset += av_xiphlacing(&p[offset], header_comm.bytes);
219 memcpy(&p[offset], header.packet, header.bytes);
220 offset += header.bytes;
221 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
222 offset += header_comm.bytes;
223 memcpy(&p[offset], header_code.packet, header_code.bytes);
224 offset += header_code.bytes;
225 assert(offset == avccontext->extradata_size);
228 vorbis_block_clear(&context->vb);
229 vorbis_dsp_clear(&context->vd);
230 vorbis_info_clear(&context->vi);
232 vorbis_comment_clear(&context->vc);
234 avccontext->frame_size = OGGVORBIS_FRAME_SIZE;
236 avccontext->coded_frame = avcodec_alloc_frame();
237 if (!avccontext->coded_frame) {
238 ret = AVERROR(ENOMEM);
244 oggvorbis_encode_close(avccontext);
248 static int oggvorbis_encode_frame(AVCodecContext *avccontext,
249 unsigned char *packets,
250 int buf_size, void *data)
252 OggVorbisContext *context = avccontext->priv_data;
254 signed short *audio = data;
258 const int samples = avccontext->frame_size;
260 int c, channels = context->vi.channels;
262 buffer = vorbis_analysis_buffer(&context->vd, samples);
263 for (c = 0; c < channels; c++) {
264 int co = (channels > 8) ? c :
265 ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
266 for (l = 0; l < samples; l++)
267 buffer[c][l] = audio[l * channels + co] / 32768.f;
269 vorbis_analysis_wrote(&context->vd, samples);
272 vorbis_analysis_wrote(&context->vd, 0);
276 while (vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
277 vorbis_analysis(&context->vb, NULL);
278 vorbis_bitrate_addblock(&context->vb);
280 while (vorbis_bitrate_flushpacket(&context->vd, &op)) {
281 /* i'd love to say the following line is a hack, but sadly it's
282 * not, apparently the end of stream decision is in libogg. */
283 if (op.bytes == 1 && op.e_o_s)
285 if (context->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) {
286 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.\n");
287 return AVERROR(EINVAL);
289 memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
290 context->buffer_index += sizeof(ogg_packet);
291 memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
292 context->buffer_index += op.bytes;
293 // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
298 if (context->buffer_index) {
299 ogg_packet *op2 = (ogg_packet *)context->buffer;
300 op2->packet = context->buffer + sizeof(ogg_packet);
303 avccontext->coded_frame->pts = ff_samples_to_time_base(avccontext,
305 //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate
308 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.\n");
309 return AVERROR(EINVAL);
312 memcpy(packets, op2->packet, l);
313 context->buffer_index -= l + sizeof(ogg_packet);
314 memmove(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
315 // av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
321 AVCodec ff_libvorbis_encoder = {
323 .type = AVMEDIA_TYPE_AUDIO,
324 .id = CODEC_ID_VORBIS,
325 .priv_data_size = sizeof(OggVorbisContext),
326 .init = oggvorbis_encode_init,
327 .encode = oggvorbis_encode_frame,
328 .close = oggvorbis_encode_close,
329 .capabilities = CODEC_CAP_DELAY,
330 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
331 .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
332 .priv_class = &class,