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"
33 #include "libavutil/mathematics.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), FF_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 av_cold int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
65 OggVorbisContext *context = avccontext->priv_data ;
68 if(avccontext->flags & CODEC_FLAG_QSCALE) {
69 /* variable bitrate */
70 if(vorbis_encode_setup_vbr(vi, avccontext->channels,
71 avccontext->sample_rate,
72 avccontext->global_quality / (float)FF_QP2LAMBDA / 10.0))
75 int minrate = avccontext->rc_min_rate > 0 ? avccontext->rc_min_rate : -1;
76 int maxrate = avccontext->rc_min_rate > 0 ? avccontext->rc_max_rate : -1;
78 /* constant bitrate */
79 if(vorbis_encode_setup_managed(vi, avccontext->channels,
80 avccontext->sample_rate, minrate, avccontext->bit_rate, maxrate))
83 /* variable bitrate by estimate, disable slow rate management */
84 if(minrate == -1 && maxrate == -1)
85 if(vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL))
89 /* cutoff frequency */
90 if(avccontext->cutoff > 0) {
91 cfreq = avccontext->cutoff / 1000.0;
92 if(vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq))
97 vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &context->iblock);
100 if (avccontext->channels == 3 &&
101 avccontext->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
102 avccontext->channels == 4 &&
103 avccontext->channel_layout != AV_CH_LAYOUT_2_2 &&
104 avccontext->channel_layout != AV_CH_LAYOUT_QUAD ||
105 avccontext->channels == 5 &&
106 avccontext->channel_layout != AV_CH_LAYOUT_5POINT0 &&
107 avccontext->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
108 avccontext->channels == 6 &&
109 avccontext->channel_layout != AV_CH_LAYOUT_5POINT1 &&
110 avccontext->channel_layout != AV_CH_LAYOUT_5POINT1_BACK ||
111 avccontext->channels == 7 &&
112 avccontext->channel_layout != (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER) ||
113 avccontext->channels == 8 &&
114 avccontext->channel_layout != AV_CH_LAYOUT_7POINT1) {
115 if (avccontext->channel_layout) {
117 av_get_channel_layout_string(name, sizeof(name), avccontext->channels,
118 avccontext->channel_layout);
119 av_log(avccontext, AV_LOG_ERROR, "%s not supported by Vorbis: "
120 "output stream will have incorrect "
121 "channel layout.\n", name);
123 av_log(avccontext, AV_LOG_WARNING, "No channel layout specified. The encoder "
124 "will use Vorbis channel layout for "
125 "%d channels.\n", avccontext->channels);
129 return vorbis_encode_setup_init(vi);
132 /* How many bytes are needed for a buffer of length 'l' */
133 static int xiph_len(int l) { return (1 + l / 255 + l); }
135 static av_cold int oggvorbis_encode_init(AVCodecContext *avccontext) {
136 OggVorbisContext *context = avccontext->priv_data ;
137 ogg_packet header, header_comm, header_code;
141 vorbis_info_init(&context->vi) ;
142 if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
143 av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n") ;
146 vorbis_analysis_init(&context->vd, &context->vi) ;
147 vorbis_block_init(&context->vd, &context->vb) ;
149 vorbis_comment_init(&context->vc);
150 vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ;
152 vorbis_analysis_headerout(&context->vd, &context->vc, &header,
153 &header_comm, &header_code);
155 avccontext->extradata_size=
156 1 + xiph_len(header.bytes) + xiph_len(header_comm.bytes) +
158 p = avccontext->extradata =
159 av_malloc(avccontext->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
162 offset += av_xiphlacing(&p[offset], header.bytes);
163 offset += av_xiphlacing(&p[offset], header_comm.bytes);
164 memcpy(&p[offset], header.packet, header.bytes);
165 offset += header.bytes;
166 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
167 offset += header_comm.bytes;
168 memcpy(&p[offset], header_code.packet, header_code.bytes);
169 offset += header_code.bytes;
170 assert(offset == avccontext->extradata_size);
172 /* vorbis_block_clear(&context->vb);
173 vorbis_dsp_clear(&context->vd);
174 vorbis_info_clear(&context->vi);*/
175 vorbis_comment_clear(&context->vc);
177 avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
179 avccontext->coded_frame= avcodec_alloc_frame();
180 avccontext->coded_frame->key_frame= 1;
186 static int oggvorbis_encode_frame(AVCodecContext *avccontext,
187 unsigned char *packets,
188 int buf_size, void *data)
190 OggVorbisContext *context = avccontext->priv_data ;
192 signed short *audio = data ;
196 const int samples = avccontext->frame_size;
198 int c, channels = context->vi.channels;
200 buffer = vorbis_analysis_buffer(&context->vd, samples) ;
201 for (c = 0; c < channels; c++) {
202 int co = (channels > 8) ? c :
203 ff_vorbis_encoding_channel_layout_offsets[channels-1][c];
204 for(l = 0 ; l < samples ; l++)
205 buffer[c][l]=audio[l*channels+co]/32768.f;
207 vorbis_analysis_wrote(&context->vd, samples) ;
210 vorbis_analysis_wrote(&context->vd, 0) ;
214 while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
215 vorbis_analysis(&context->vb, NULL);
216 vorbis_bitrate_addblock(&context->vb) ;
218 while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
219 /* i'd love to say the following line is a hack, but sadly it's
220 * not, apparently the end of stream decision is in libogg. */
221 if(op.bytes==1 && op.e_o_s)
223 if (context->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) {
224 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
227 memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
228 context->buffer_index += sizeof(ogg_packet);
229 memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
230 context->buffer_index += op.bytes;
231 // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
236 if(context->buffer_index){
237 ogg_packet *op2= (ogg_packet*)context->buffer;
238 op2->packet = context->buffer + sizeof(ogg_packet);
241 avccontext->coded_frame->pts= av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
242 //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate
245 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
249 memcpy(packets, op2->packet, l);
250 context->buffer_index -= l + sizeof(ogg_packet);
251 memmove(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
252 // av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
259 static av_cold int oggvorbis_encode_close(AVCodecContext *avccontext) {
260 OggVorbisContext *context = avccontext->priv_data ;
261 /* ogg_packet op ; */
263 vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
265 vorbis_block_clear(&context->vb);
266 vorbis_dsp_clear(&context->vd);
267 vorbis_info_clear(&context->vi);
269 av_freep(&avccontext->coded_frame);
270 av_freep(&avccontext->extradata);
276 AVCodec ff_libvorbis_encoder = {
278 .type = AVMEDIA_TYPE_AUDIO,
279 .id = CODEC_ID_VORBIS,
280 .priv_data_size = sizeof(OggVorbisContext),
281 .init = oggvorbis_encode_init,
282 .encode = oggvorbis_encode_frame,
283 .close = oggvorbis_encode_close,
284 .capabilities = CODEC_CAP_DELAY,
285 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
286 .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
287 .priv_class = &class,