libopencore-amrnbenc: use AVCodec.encode2()
[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
592c4dbc 29#include "libavutil/fifo.h"
77336a5e 30#include "libavutil/opt.h"
81e0d0b4 31#include "avcodec.h"
2c124cb6 32#include "bytestream.h"
91a28b0e 33#include "internal.h"
9577838f 34#include "vorbis.h"
81e0d0b4 35
3f4993f1
MN
36#undef NDEBUG
37#include <assert.h>
38
eb35ef29
JR
39/* Number of samples the user should send in each call.
40 * This value is used because it is the LCD of all possible frame sizes, so
41 * an output packet will always start at the same point as one of the input
42 * packets.
43 */
6d8f985e 44#define OGGVORBIS_FRAME_SIZE 64
81e0d0b4 45
ca5ab8cd 46#define BUFFER_SIZE (1024 * 64)
81e0d0b4
MH
47
48typedef struct OggVorbisContext {
eb35ef29
JR
49 AVClass *av_class; /**< class for AVOptions */
50 vorbis_info vi; /**< vorbis_info used during init */
51 vorbis_dsp_state vd; /**< DSP state used for analysis */
52 vorbis_block vb; /**< vorbis_block used for analysis */
592c4dbc 53 AVFifoBuffer *pkt_fifo; /**< output packet buffer */
eb35ef29 54 int eof; /**< end-of-file flag */
f15c4281 55 int dsp_initialized; /**< vd has been initialized */
eb35ef29
JR
56 vorbis_comment vc; /**< VorbisComment info */
57 ogg_packet op; /**< ogg packet */
58 double iblock; /**< impulse block bias option */
ca5ab8cd 59} OggVorbisContext;
81e0d0b4 60
ca5ab8cd
DB
61static const AVOption options[] = {
62 { "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 },
63 { NULL }
77336a5e 64};
147ff24a
JR
65
66static const AVCodecDefault defaults[] = {
67 { "b", "0" },
68 { NULL },
69};
70
da754858 71static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
81e0d0b4 72
eb35ef29 73
6f600ab3
JR
74static int vorbis_error_to_averror(int ov_err)
75{
76 switch (ov_err) {
77 case OV_EFAULT: return AVERROR_BUG;
78 case OV_EINVAL: return AVERROR(EINVAL);
79 case OV_EIMPL: return AVERROR(EINVAL);
80 default: return AVERROR_UNKNOWN;
81 }
82}
83
eb35ef29
JR
84static av_cold int oggvorbis_init_encoder(vorbis_info *vi,
85 AVCodecContext *avctx)
ca5ab8cd 86{
eb35ef29 87 OggVorbisContext *s = avctx->priv_data;
13c71451 88 double cfreq;
6f600ab3 89 int ret;
c55427f8 90
147ff24a 91 if (avctx->flags & CODEC_FLAG_QSCALE || !avctx->bit_rate) {
eb35ef29
JR
92 /* variable bitrate
93 * NOTE: we use the oggenc range of -1 to 10 for global_quality for
147ff24a 94 * user convenience, but libvorbis uses -0.1 to 1.0.
eb35ef29
JR
95 */
96 float q = avctx->global_quality / (float)FF_QP2LAMBDA;
147ff24a
JR
97 /* default to 3 if the user did not set quality or bitrate */
98 if (!(avctx->flags & CODEC_FLAG_QSCALE))
99 q = 3.0;
eb35ef29
JR
100 if ((ret = vorbis_encode_setup_vbr(vi, avctx->channels,
101 avctx->sample_rate,
6f600ab3
JR
102 q / 10.0)))
103 goto error;
13c71451 104 } else {
eb35ef29 105 int minrate = avctx->rc_min_rate > 0 ? avctx->rc_min_rate : -1;
182d4f1f 106 int maxrate = avctx->rc_max_rate > 0 ? avctx->rc_max_rate : -1;
e5a5ea9e 107
eb35ef29
JR
108 /* average bitrate */
109 if ((ret = vorbis_encode_setup_managed(vi, avctx->channels,
182d4f1f
JR
110 avctx->sample_rate, maxrate,
111 avctx->bit_rate, minrate)))
6f600ab3 112 goto error;
13c71451 113
57ebbccf 114 /* variable bitrate by estimate, disable slow rate management */
ca5ab8cd 115 if (minrate == -1 && maxrate == -1)
6f600ab3
JR
116 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
117 goto error;
13c71451 118 }
c55427f8 119
13c71451 120 /* cutoff frequency */
eb35ef29
JR
121 if (avctx->cutoff > 0) {
122 cfreq = avctx->cutoff / 1000.0;
6f600ab3
JR
123 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
124 goto error;
13c71451 125 }
81e0d0b4 126
eb35ef29
JR
127 /* impulse block bias */
128 if (s->iblock) {
129 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->iblock)))
6f600ab3 130 goto error;
77336a5e
MN
131 }
132
6f600ab3
JR
133 if ((ret = vorbis_encode_setup_init(vi)))
134 goto error;
135
136 return 0;
137error:
138 return vorbis_error_to_averror(ret);
81e0d0b4
MH
139}
140
fd7242dd 141/* How many bytes are needed for a buffer of length 'l' */
ca5ab8cd
DB
142static int xiph_len(int l)
143{
d4b63054 144 return 1 + l / 255 + l;
ca5ab8cd 145}
fd7242dd 146
eb35ef29 147static av_cold int oggvorbis_encode_close(AVCodecContext *avctx)
6f600ab3 148{
eb35ef29 149 OggVorbisContext *s = avctx->priv_data;
6f600ab3 150
eb35ef29 151 /* notify vorbisenc this is EOF */
f15c4281
JR
152 if (s->dsp_initialized)
153 vorbis_analysis_wrote(&s->vd, 0);
6f600ab3 154
eb35ef29
JR
155 vorbis_block_clear(&s->vb);
156 vorbis_dsp_clear(&s->vd);
157 vorbis_info_clear(&s->vi);
6f600ab3 158
592c4dbc 159 av_fifo_free(s->pkt_fifo);
eb35ef29
JR
160 av_freep(&avctx->coded_frame);
161 av_freep(&avctx->extradata);
6f600ab3
JR
162
163 return 0;
164}
165
eb35ef29 166static av_cold int oggvorbis_encode_init(AVCodecContext *avctx)
ca5ab8cd 167{
eb35ef29 168 OggVorbisContext *s = avctx->priv_data;
bbb77e7c
MN
169 ogg_packet header, header_comm, header_code;
170 uint8_t *p;
fd7242dd 171 unsigned int offset;
6f600ab3 172 int ret;
81e0d0b4 173
eb35ef29
JR
174 vorbis_info_init(&s->vi);
175 if ((ret = oggvorbis_init_encoder(&s->vi, avctx))) {
a45a1ea5 176 av_log(avctx, AV_LOG_ERROR, "encoder setup failed\n");
6f600ab3
JR
177 goto error;
178 }
eb35ef29 179 if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) {
a45a1ea5 180 av_log(avctx, AV_LOG_ERROR, "analysis init failed\n");
6f600ab3
JR
181 ret = vorbis_error_to_averror(ret);
182 goto error;
183 }
f15c4281 184 s->dsp_initialized = 1;
eb35ef29 185 if ((ret = vorbis_block_init(&s->vd, &s->vb))) {
a45a1ea5 186 av_log(avctx, AV_LOG_ERROR, "dsp init failed\n");
6f600ab3
JR
187 ret = vorbis_error_to_averror(ret);
188 goto error;
81e0d0b4 189 }
81e0d0b4 190
eb35ef29
JR
191 vorbis_comment_init(&s->vc);
192 vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
bbb77e7c 193
eb35ef29
JR
194 if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
195 &header_code))) {
6f600ab3
JR
196 ret = vorbis_error_to_averror(ret);
197 goto error;
198 }
115329f1 199
eb35ef29
JR
200 avctx->extradata_size = 1 + xiph_len(header.bytes) +
201 xiph_len(header_comm.bytes) +
202 header_code.bytes;
203 p = avctx->extradata = av_malloc(avctx->extradata_size +
204 FF_INPUT_BUFFER_PADDING_SIZE);
6f600ab3
JR
205 if (!p) {
206 ret = AVERROR(ENOMEM);
207 goto error;
208 }
ca5ab8cd
DB
209 p[0] = 2;
210 offset = 1;
ad2b531d
MR
211 offset += av_xiphlacing(&p[offset], header.bytes);
212 offset += av_xiphlacing(&p[offset], header_comm.bytes);
213 memcpy(&p[offset], header.packet, header.bytes);
214 offset += header.bytes;
215 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
216 offset += header_comm.bytes;
217 memcpy(&p[offset], header_code.packet, header_code.bytes);
218 offset += header_code.bytes;
eb35ef29 219 assert(offset == avctx->extradata_size);
115329f1 220
eb35ef29 221 vorbis_comment_clear(&s->vc);
115329f1 222
eb35ef29 223 avctx->frame_size = OGGVORBIS_FRAME_SIZE;
115329f1 224
592c4dbc
JR
225 s->pkt_fifo = av_fifo_alloc(BUFFER_SIZE);
226 if (!s->pkt_fifo) {
227 ret = AVERROR(ENOMEM);
228 goto error;
229 }
230
eb35ef29
JR
231 avctx->coded_frame = avcodec_alloc_frame();
232 if (!avctx->coded_frame) {
6f600ab3
JR
233 ret = AVERROR(ENOMEM);
234 goto error;
235 }
115329f1 236
ca5ab8cd 237 return 0;
6f600ab3 238error:
eb35ef29 239 oggvorbis_encode_close(avctx);
6f600ab3 240 return ret;
81e0d0b4
MH
241}
242
eb35ef29 243static int oggvorbis_encode_frame(AVCodecContext *avctx, unsigned char *packets,
ca5ab8cd 244 int buf_size, void *data)
81e0d0b4 245{
eb35ef29 246 OggVorbisContext *s = avctx->priv_data;
ca5ab8cd 247 ogg_packet op;
c5063e03 248 float *audio = data;
94025d8a 249 int pkt_size, ret;
9c8f0768 250
eb35ef29 251 /* send samples to libvorbis */
ca5ab8cd 252 if (data) {
eb35ef29 253 const int samples = avctx->frame_size;
ca5ab8cd 254 float **buffer;
eb35ef29 255 int c, channels = s->vi.channels;
81e0d0b4 256
eb35ef29 257 buffer = vorbis_analysis_buffer(&s->vd, samples);
9577838f 258 for (c = 0; c < channels; c++) {
eb35ef29 259 int i;
9577838f 260 int co = (channels > 8) ? c :
ca5ab8cd 261 ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
eb35ef29 262 for (i = 0; i < samples; i++)
c5063e03 263 buffer[c][i] = audio[i * channels + co];
bb270c08 264 }
a45a1ea5
JR
265 if ((ret = vorbis_analysis_wrote(&s->vd, samples)) < 0) {
266 av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
94025d8a 267 return vorbis_error_to_averror(ret);
a45a1ea5 268 }
9c8f0768 269 } else {
eb35ef29 270 if (!s->eof)
a45a1ea5
JR
271 if ((ret = vorbis_analysis_wrote(&s->vd, 0)) < 0) {
272 av_log(avctx, AV_LOG_ERROR, "error in vorbis_analysis_wrote()\n");
94025d8a 273 return vorbis_error_to_averror(ret);
a45a1ea5 274 }
eb35ef29 275 s->eof = 1;
9c8f0768 276 }
81e0d0b4 277
eb35ef29 278 /* retrieve available packets from libvorbis */
94025d8a
JR
279 while ((ret = vorbis_analysis_blockout(&s->vd, &s->vb)) == 1) {
280 if ((ret = vorbis_analysis(&s->vb, NULL)) < 0)
281 break;
282 if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0)
283 break;
81e0d0b4 284
eb35ef29 285 /* add any available packets to the output packet buffer */
94025d8a 286 while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) {
592c4dbc 287 if (av_fifo_space(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) {
a45a1ea5
JR
288 av_log(avctx, AV_LOG_ERROR, "packet buffer is too small");
289 return AVERROR_BUG;
c426562c 290 }
592c4dbc
JR
291 av_fifo_generic_write(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
292 av_fifo_generic_write(s->pkt_fifo, op.packet, op.bytes, NULL);
bb270c08 293 }
a45a1ea5
JR
294 if (ret < 0) {
295 av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
94025d8a 296 break;
a45a1ea5 297 }
81e0d0b4 298 }
a45a1ea5
JR
299 if (ret < 0) {
300 av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
94025d8a 301 return vorbis_error_to_averror(ret);
a45a1ea5 302 }
81e0d0b4 303
eb35ef29
JR
304 /* output then next packet from the output buffer, if available */
305 pkt_size = 0;
592c4dbc
JR
306 if (av_fifo_size(s->pkt_fifo) >= sizeof(ogg_packet)) {
307 av_fifo_generic_read(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
308 pkt_size = op.bytes;
eb35ef29
JR
309 // FIXME: we should use the user-supplied pts and duration
310 avctx->coded_frame->pts = ff_samples_to_time_base(avctx,
592c4dbc 311 op.granulepos);
eb35ef29 312 if (pkt_size > buf_size) {
a45a1ea5
JR
313 av_log(avctx, AV_LOG_ERROR, "output buffer is too small");
314 return AVERROR(EINVAL);
c426562c 315 }
592c4dbc 316 av_fifo_generic_read(s->pkt_fifo, packets, pkt_size, NULL);
6d8f985e
MN
317 }
318
eb35ef29 319 return pkt_size;
81e0d0b4
MH
320}
321
d36beb3f 322AVCodec ff_libvorbis_encoder = {
86714887
DB
323 .name = "libvorbis",
324 .type = AVMEDIA_TYPE_AUDIO,
325 .id = CODEC_ID_VORBIS,
326 .priv_data_size = sizeof(OggVorbisContext),
327 .init = oggvorbis_encode_init,
328 .encode = oggvorbis_encode_frame,
329 .close = oggvorbis_encode_close,
330 .capabilities = CODEC_CAP_DELAY,
c5063e03 331 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
eb35ef29 332 AV_SAMPLE_FMT_NONE },
86714887
DB
333 .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
334 .priv_class = &class,
147ff24a 335 .defaults = defaults,
86714887 336};