libvorbis: use AVFifoBuffer for output packet buffer
[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))) {
176 av_log(avctx, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n");
6f600ab3
JR
177 goto error;
178 }
eb35ef29 179 if ((ret = vorbis_analysis_init(&s->vd, &s->vi))) {
6f600ab3
JR
180 ret = vorbis_error_to_averror(ret);
181 goto error;
182 }
f15c4281 183 s->dsp_initialized = 1;
eb35ef29 184 if ((ret = vorbis_block_init(&s->vd, &s->vb))) {
6f600ab3
JR
185 ret = vorbis_error_to_averror(ret);
186 goto error;
81e0d0b4 187 }
81e0d0b4 188
eb35ef29
JR
189 vorbis_comment_init(&s->vc);
190 vorbis_comment_add_tag(&s->vc, "encoder", LIBAVCODEC_IDENT);
bbb77e7c 191
eb35ef29
JR
192 if ((ret = vorbis_analysis_headerout(&s->vd, &s->vc, &header, &header_comm,
193 &header_code))) {
6f600ab3
JR
194 ret = vorbis_error_to_averror(ret);
195 goto error;
196 }
115329f1 197
eb35ef29
JR
198 avctx->extradata_size = 1 + xiph_len(header.bytes) +
199 xiph_len(header_comm.bytes) +
200 header_code.bytes;
201 p = avctx->extradata = av_malloc(avctx->extradata_size +
202 FF_INPUT_BUFFER_PADDING_SIZE);
6f600ab3
JR
203 if (!p) {
204 ret = AVERROR(ENOMEM);
205 goto error;
206 }
ca5ab8cd
DB
207 p[0] = 2;
208 offset = 1;
ad2b531d
MR
209 offset += av_xiphlacing(&p[offset], header.bytes);
210 offset += av_xiphlacing(&p[offset], header_comm.bytes);
211 memcpy(&p[offset], header.packet, header.bytes);
212 offset += header.bytes;
213 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
214 offset += header_comm.bytes;
215 memcpy(&p[offset], header_code.packet, header_code.bytes);
216 offset += header_code.bytes;
eb35ef29 217 assert(offset == avctx->extradata_size);
115329f1 218
eb35ef29 219 vorbis_comment_clear(&s->vc);
115329f1 220
eb35ef29 221 avctx->frame_size = OGGVORBIS_FRAME_SIZE;
115329f1 222
592c4dbc
JR
223 s->pkt_fifo = av_fifo_alloc(BUFFER_SIZE);
224 if (!s->pkt_fifo) {
225 ret = AVERROR(ENOMEM);
226 goto error;
227 }
228
eb35ef29
JR
229 avctx->coded_frame = avcodec_alloc_frame();
230 if (!avctx->coded_frame) {
6f600ab3
JR
231 ret = AVERROR(ENOMEM);
232 goto error;
233 }
115329f1 234
ca5ab8cd 235 return 0;
6f600ab3 236error:
eb35ef29 237 oggvorbis_encode_close(avctx);
6f600ab3 238 return ret;
81e0d0b4
MH
239}
240
eb35ef29 241static int oggvorbis_encode_frame(AVCodecContext *avctx, unsigned char *packets,
ca5ab8cd 242 int buf_size, void *data)
81e0d0b4 243{
eb35ef29 244 OggVorbisContext *s = avctx->priv_data;
ca5ab8cd 245 ogg_packet op;
c5063e03 246 float *audio = data;
94025d8a 247 int pkt_size, ret;
9c8f0768 248
eb35ef29 249 /* send samples to libvorbis */
ca5ab8cd 250 if (data) {
eb35ef29 251 const int samples = avctx->frame_size;
ca5ab8cd 252 float **buffer;
eb35ef29 253 int c, channels = s->vi.channels;
81e0d0b4 254
eb35ef29 255 buffer = vorbis_analysis_buffer(&s->vd, samples);
9577838f 256 for (c = 0; c < channels; c++) {
eb35ef29 257 int i;
9577838f 258 int co = (channels > 8) ? c :
ca5ab8cd 259 ff_vorbis_encoding_channel_layout_offsets[channels - 1][c];
eb35ef29 260 for (i = 0; i < samples; i++)
c5063e03 261 buffer[c][i] = audio[i * channels + co];
bb270c08 262 }
94025d8a
JR
263 if ((ret = vorbis_analysis_wrote(&s->vd, samples)) < 0)
264 return vorbis_error_to_averror(ret);
9c8f0768 265 } else {
eb35ef29 266 if (!s->eof)
94025d8a
JR
267 if ((ret = vorbis_analysis_wrote(&s->vd, 0)) < 0)
268 return vorbis_error_to_averror(ret);
eb35ef29 269 s->eof = 1;
9c8f0768 270 }
81e0d0b4 271
eb35ef29 272 /* retrieve available packets from libvorbis */
94025d8a
JR
273 while ((ret = vorbis_analysis_blockout(&s->vd, &s->vb)) == 1) {
274 if ((ret = vorbis_analysis(&s->vb, NULL)) < 0)
275 break;
276 if ((ret = vorbis_bitrate_addblock(&s->vb)) < 0)
277 break;
81e0d0b4 278
eb35ef29 279 /* add any available packets to the output packet buffer */
94025d8a 280 while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) {
592c4dbc 281 if (av_fifo_space(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) {
eb35ef29 282 av_log(avctx, AV_LOG_ERROR, "libvorbis: buffer overflow.");
c426562c
PM
283 return -1;
284 }
592c4dbc
JR
285 av_fifo_generic_write(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
286 av_fifo_generic_write(s->pkt_fifo, op.packet, op.bytes, NULL);
bb270c08 287 }
94025d8a
JR
288 if (ret < 0)
289 break;
81e0d0b4 290 }
94025d8a
JR
291 if (ret < 0)
292 return vorbis_error_to_averror(ret);
81e0d0b4 293
eb35ef29
JR
294 /* output then next packet from the output buffer, if available */
295 pkt_size = 0;
592c4dbc
JR
296 if (av_fifo_size(s->pkt_fifo) >= sizeof(ogg_packet)) {
297 av_fifo_generic_read(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
298 pkt_size = op.bytes;
eb35ef29
JR
299 // FIXME: we should use the user-supplied pts and duration
300 avctx->coded_frame->pts = ff_samples_to_time_base(avctx,
592c4dbc 301 op.granulepos);
eb35ef29
JR
302 if (pkt_size > buf_size) {
303 av_log(avctx, AV_LOG_ERROR, "libvorbis: buffer overflow.");
c426562c
PM
304 return -1;
305 }
592c4dbc 306 av_fifo_generic_read(s->pkt_fifo, packets, pkt_size, NULL);
6d8f985e
MN
307 }
308
eb35ef29 309 return pkt_size;
81e0d0b4
MH
310}
311
d36beb3f 312AVCodec ff_libvorbis_encoder = {
86714887
DB
313 .name = "libvorbis",
314 .type = AVMEDIA_TYPE_AUDIO,
315 .id = CODEC_ID_VORBIS,
316 .priv_data_size = sizeof(OggVorbisContext),
317 .init = oggvorbis_encode_init,
318 .encode = oggvorbis_encode_frame,
319 .close = oggvorbis_encode_close,
320 .capabilities = CODEC_CAP_DELAY,
c5063e03 321 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
eb35ef29 322 AV_SAMPLE_FMT_NONE },
86714887
DB
323 .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
324 .priv_class = &class,
147ff24a 325 .defaults = defaults,
86714887 326};