libvorbis: fix use of minrate/maxrate AVOptions
[libav.git] / libavcodec / libvorbis.c
CommitLineData
04d7f601
DB
1/*
2 * copyright (c) 2002 Mark Hills <mark@pogo.org.uk>
3 *
2912e87a 4 * This file is part of Libav.
b78e7197 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
04d7f601
DB
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
b78e7197 9 * version 2.1 of the License, or (at your option) any later version.
04d7f601 10 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
04d7f601
DB
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
2912e87a 17 * License along with Libav; if not, write to the Free Software
e5a389a1 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
04d7f601
DB
19 */
20
983e3246 21/**
ba87f080 22 * @file
eb35ef29 23 * Vorbis encoding support via libvorbisenc.
983e3246 24 * @author Mark Hills <mark@pogo.org.uk>
81e0d0b4
MH
25 */
26
81e0d0b4
MH
27#include <vorbis/vorbisenc.h>
28
77336a5e 29#include "libavutil/opt.h"
81e0d0b4 30#include "avcodec.h"
2c124cb6 31#include "bytestream.h"
91a28b0e 32#include "internal.h"
9577838f 33#include "vorbis.h"
81e0d0b4 34
3f4993f1
MN
35#undef NDEBUG
36#include <assert.h>
37
eb35ef29
JR
38/* Number of samples the user should send in each call.
39 * This value is used because it is the LCD of all possible frame sizes, so
40 * an output packet will always start at the same point as one of the input
41 * packets.
42 */
6d8f985e 43#define OGGVORBIS_FRAME_SIZE 64
81e0d0b4 44
ca5ab8cd 45#define BUFFER_SIZE (1024 * 64)
81e0d0b4
MH
46
47typedef struct OggVorbisContext {
eb35ef29
JR
48 AVClass *av_class; /**< class for AVOptions */
49 vorbis_info vi; /**< vorbis_info used during init */
50 vorbis_dsp_state vd; /**< DSP state used for analysis */
51 vorbis_block vb; /**< vorbis_block used for analysis */
52 uint8_t buffer[BUFFER_SIZE]; /**< output packet buffer */
53 int buffer_index; /**< current buffer position */
54 int eof; /**< end-of-file flag */
55 vorbis_comment vc; /**< VorbisComment info */
56 ogg_packet op; /**< ogg packet */
57 double iblock; /**< impulse block bias option */
ca5ab8cd 58} OggVorbisContext;
81e0d0b4 59
ca5ab8cd
DB
60static const AVOption options[] = {
61 { "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 { NULL }
77336a5e 63};
da754858 64static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
81e0d0b4 65
eb35ef29 66
6f600ab3
JR
67static int vorbis_error_to_averror(int ov_err)
68{
69 switch (ov_err) {
70 case OV_EFAULT: return AVERROR_BUG;
71 case OV_EINVAL: return AVERROR(EINVAL);
72 case OV_EIMPL: return AVERROR(EINVAL);
73 default: return AVERROR_UNKNOWN;
74 }
75}
76
eb35ef29
JR
77static av_cold int oggvorbis_init_encoder(vorbis_info *vi,
78 AVCodecContext *avctx)
ca5ab8cd 79{
eb35ef29 80 OggVorbisContext *s = avctx->priv_data;
13c71451 81 double cfreq;
6f600ab3 82 int ret;
c55427f8 83
eb35ef29
JR
84 if (avctx->flags & CODEC_FLAG_QSCALE) {
85 /* variable bitrate
86 * NOTE: we use the oggenc range of -1 to 10 for global_quality for
87 * user convenience, but libvorbis uses -0.1 to 1.0
88 */
89 float q = avctx->global_quality / (float)FF_QP2LAMBDA;
90 if ((ret = vorbis_encode_setup_vbr(vi, avctx->channels,
91 avctx->sample_rate,
6f600ab3
JR
92 q / 10.0)))
93 goto error;
13c71451 94 } else {
eb35ef29 95 int minrate = avctx->rc_min_rate > 0 ? avctx->rc_min_rate : -1;
182d4f1f 96 int maxrate = avctx->rc_max_rate > 0 ? avctx->rc_max_rate : -1;
e5a5ea9e 97
eb35ef29
JR
98 /* average bitrate */
99 if ((ret = vorbis_encode_setup_managed(vi, avctx->channels,
182d4f1f
JR
100 avctx->sample_rate, maxrate,
101 avctx->bit_rate, minrate)))
6f600ab3 102 goto error;
13c71451 103
57ebbccf 104 /* variable bitrate by estimate, disable slow rate management */
ca5ab8cd 105 if (minrate == -1 && maxrate == -1)
6f600ab3
JR
106 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
107 goto error;
13c71451 108 }
c55427f8 109
13c71451 110 /* cutoff frequency */
eb35ef29
JR
111 if (avctx->cutoff > 0) {
112 cfreq = avctx->cutoff / 1000.0;
6f600ab3
JR
113 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
114 goto error;
13c71451 115 }
81e0d0b4 116
eb35ef29
JR
117 /* impulse block bias */
118 if (s->iblock) {
119 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->iblock)))
6f600ab3 120 goto error;
77336a5e
MN
121 }
122
6f600ab3
JR
123 if ((ret = vorbis_encode_setup_init(vi)))
124 goto error;
125
126 return 0;
127error:
128 return vorbis_error_to_averror(ret);
81e0d0b4
MH
129}
130
fd7242dd 131/* How many bytes are needed for a buffer of length 'l' */
ca5ab8cd
DB
132static int xiph_len(int l)
133{
d4b63054 134 return 1 + l / 255 + l;
ca5ab8cd 135}
fd7242dd 136
eb35ef29 137static av_cold int oggvorbis_encode_close(AVCodecContext *avctx)
6f600ab3 138{
eb35ef29 139 OggVorbisContext *s = avctx->priv_data;
6f600ab3 140
eb35ef29
JR
141 /* notify vorbisenc this is EOF */
142 vorbis_analysis_wrote(&s->vd, 0);
6f600ab3 143
eb35ef29
JR
144 vorbis_block_clear(&s->vb);
145 vorbis_dsp_clear(&s->vd);
146 vorbis_info_clear(&s->vi);
6f600ab3 147
eb35ef29
JR
148 av_freep(&avctx->coded_frame);
149 av_freep(&avctx->extradata);
6f600ab3
JR
150
151 return 0;
152}
153
eb35ef29 154static av_cold int oggvorbis_encode_init(AVCodecContext *avctx)
ca5ab8cd 155{
eb35ef29 156 OggVorbisContext *s = avctx->priv_data;
bbb77e7c
MN
157 ogg_packet header, header_comm, header_code;
158 uint8_t *p;
fd7242dd 159 unsigned int offset;
6f600ab3 160 int ret;
81e0d0b4 161
eb35ef29
JR
162 vorbis_info_init(&s->vi);
163 if ((ret = oggvorbis_init_encoder(&s->vi, avctx))) {
164 av_log(avctx, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n");
6f600ab3
JR
165 goto error;
166 }
eb35ef29 167 if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) {
6f600ab3
JR
168 ret = vorbis_error_to_averror(ret);
169 goto error;
170 }
eb35ef29 171 if ((ret = vorbis_block_init(&s->vd, &s->vb))) {
6f600ab3
JR
172 ret = vorbis_error_to_averror(ret);
173 goto error;
81e0d0b4 174 }
81e0d0b4 175
eb35ef29
JR
176 vorbis_comment_init(&s->vc);
177 vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
bbb77e7c 178
eb35ef29
JR
179 if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
180 &header_code))) {
6f600ab3
JR
181 ret = vorbis_error_to_averror(ret);
182 goto error;
183 }
115329f1 184
eb35ef29
JR
185 avctx->extradata_size = 1 + xiph_len(header.bytes) +
186 xiph_len(header_comm.bytes) +
187 header_code.bytes;
188 p = avctx->extradata = av_malloc(avctx->extradata_size +
189 FF_INPUT_BUFFER_PADDING_SIZE);
6f600ab3
JR
190 if (!p) {
191 ret = AVERROR(ENOMEM);
192 goto error;
193 }
ca5ab8cd
DB
194 p[0] = 2;
195 offset = 1;
ad2b531d
MR
196 offset += av_xiphlacing(&p[offset], header.bytes);
197 offset += av_xiphlacing(&p[offset], header_comm.bytes);
198 memcpy(&p[offset], header.packet, header.bytes);
199 offset += header.bytes;
200 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
201 offset += header_comm.bytes;
202 memcpy(&p[offset], header_code.packet, header_code.bytes);
203 offset += header_code.bytes;
eb35ef29 204 assert(offset == avctx->extradata_size);
115329f1 205
eb35ef29 206 vorbis_comment_clear(&s->vc);
115329f1 207
eb35ef29 208 avctx->frame_size = OGGVORBIS_FRAME_SIZE;
115329f1 209
eb35ef29
JR
210 avctx->coded_frame = avcodec_alloc_frame();
211 if (!avctx->coded_frame) {
6f600ab3
JR
212 ret = AVERROR(ENOMEM);
213 goto error;
214 }
115329f1 215
ca5ab8cd 216 return 0;
6f600ab3 217error:
eb35ef29 218 oggvorbis_encode_close(avctx);
6f600ab3 219 return ret;
81e0d0b4
MH
220}
221
eb35ef29 222static int oggvorbis_encode_frame(AVCodecContext *avctx, unsigned char *packets,
ca5ab8cd 223 int buf_size, void *data)
81e0d0b4 224{
eb35ef29 225 OggVorbisContext *s = avctx->priv_data;
ca5ab8cd
DB
226 ogg_packet op;
227 signed short *audio = data;
eb35ef29 228 int pkt_size;
9c8f0768 229
eb35ef29 230 /* send samples to libvorbis */
ca5ab8cd 231 if (data) {
eb35ef29 232 const int samples = avctx->frame_size;
ca5ab8cd 233 float **buffer;
eb35ef29 234 int c, channels = s->vi.channels;
81e0d0b4 235
eb35ef29 236 buffer = vorbis_analysis_buffer(&s->vd, samples);
9577838f 237 for (c = 0; c < channels; c++) {
eb35ef29 238 int i;
9577838f 239 int co = (channels > 8) ? c :
ca5ab8cd 240 ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
eb35ef29
JR
241 for (i = 0; i < samples; i++)
242 buffer[c][i] = audio[i * channels + co] / 32768.f;
bb270c08 243 }
eb35ef29 244 vorbis_analysis_wrote(&s->vd, samples);
9c8f0768 245 } else {
eb35ef29
JR
246 if (!s->eof)
247 vorbis_analysis_wrote(&s->vd, 0);
248 s->eof = 1;
9c8f0768 249 }
81e0d0b4 250
eb35ef29
JR
251 /* retrieve available packets from libvorbis */
252 while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) {
253 vorbis_analysis(&s->vb, NULL);
254 vorbis_bitrate_addblock(&s->vb);
81e0d0b4 255
eb35ef29
JR
256 /* add any available packets to the output packet buffer */
257 while (vorbis_bitrate_flushpacket(&s->vd, &op)) {
0afd2a92
DB
258 /* i'd love to say the following line is a hack, but sadly it's
259 * not, apparently the end of stream decision is in libogg. */
ca5ab8cd 260 if (op.bytes == 1 && op.e_o_s)
6f824977 261 continue;
eb35ef29
JR
262 if (s->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) {
263 av_log(avctx, AV_LOG_ERROR, "libvorbis: buffer overflow.");
c426562c
PM
264 return -1;
265 }
eb35ef29
JR
266 memcpy(s->buffer + s->buffer_index, &op, sizeof(ogg_packet));
267 s->buffer_index += sizeof(ogg_packet);
268 memcpy(s->buffer + s->buffer_index, op.packet, op.bytes);
269 s->buffer_index += op.bytes;
bb270c08 270 }
81e0d0b4
MH
271 }
272
eb35ef29
JR
273 /* output then next packet from the output buffer, if available */
274 pkt_size = 0;
275 if (s->buffer_index) {
276 ogg_packet *op2 = (ogg_packet *)s->buffer;
277 op2->packet = s->buffer + sizeof(ogg_packet);
278
279 pkt_size = op2->bytes;
280 // FIXME: we should use the user-supplied pts and duration
281 avctx->coded_frame->pts = ff_samples_to_time_base(avctx,
282 op2->granulepos);
283 if (pkt_size > buf_size) {
284 av_log(avctx, AV_LOG_ERROR, "libvorbis: buffer overflow.");
c426562c
PM
285 return -1;
286 }
287
eb35ef29
JR
288 memcpy(packets, op2->packet, pkt_size);
289 s->buffer_index -= pkt_size + sizeof(ogg_packet);
290 memmove(s->buffer, s->buffer + pkt_size + sizeof(ogg_packet),
291 s->buffer_index);
6d8f985e
MN
292 }
293
eb35ef29 294 return pkt_size;
81e0d0b4
MH
295}
296
d36beb3f 297AVCodec ff_libvorbis_encoder = {
86714887
DB
298 .name = "libvorbis",
299 .type = AVMEDIA_TYPE_AUDIO,
300 .id = CODEC_ID_VORBIS,
301 .priv_data_size = sizeof(OggVorbisContext),
302 .init = oggvorbis_encode_init,
303 .encode = oggvorbis_encode_frame,
304 .close = oggvorbis_encode_close,
305 .capabilities = CODEC_CAP_DELAY,
eb35ef29
JR
306 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
307 AV_SAMPLE_FMT_NONE },
86714887
DB
308 .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
309 .priv_class = &class,
310};