Commit | Line | Data |
---|---|---|
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 |
983e3246 MN |
23 | * Ogg Vorbis codec support via libvorbisenc. |
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 | ||
6d8f985e | 38 | #define OGGVORBIS_FRAME_SIZE 64 |
81e0d0b4 | 39 | |
ca5ab8cd | 40 | #define BUFFER_SIZE (1024 * 64) |
81e0d0b4 MH |
41 | |
42 | typedef struct OggVorbisContext { | |
77336a5e | 43 | AVClass *av_class; |
ca5ab8cd DB |
44 | vorbis_info vi; |
45 | vorbis_dsp_state vd; | |
46 | vorbis_block vb; | |
6d8f985e MN |
47 | uint8_t buffer[BUFFER_SIZE]; |
48 | int buffer_index; | |
9c8f0768 | 49 | int eof; |
3aca208a MH |
50 | |
51 | /* decoder */ | |
ca5ab8cd | 52 | vorbis_comment vc; |
d76f581f | 53 | ogg_packet op; |
77336a5e MN |
54 | |
55 | double iblock; | |
ca5ab8cd | 56 | } OggVorbisContext; |
81e0d0b4 | 57 | |
ca5ab8cd DB |
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 }, | |
60 | { NULL } | |
77336a5e | 61 | }; |
da754858 | 62 | static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT }; |
81e0d0b4 | 63 | |
6f600ab3 JR |
64 | static int vorbis_error_to_averror(int ov_err) |
65 | { | |
66 | switch (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; | |
71 | } | |
72 | } | |
73 | ||
ca5ab8cd DB |
74 | static av_cold int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) |
75 | { | |
76 | OggVorbisContext *context = avccontext->priv_data; | |
13c71451 | 77 | double cfreq; |
6f600ab3 | 78 | int ret; |
c55427f8 | 79 | |
ca5ab8cd | 80 | if (avccontext->flags & CODEC_FLAG_QSCALE) { |
13c71451 | 81 | /* variable bitrate */ |
6f600ab3 JR |
82 | float q = avccontext->global_quality / (float)FF_QP2LAMBDA; |
83 | if ((ret = vorbis_encode_setup_vbr(vi, avccontext->channels, | |
84 | avccontext->sample_rate, | |
85 | q / 10.0))) | |
86 | goto error; | |
13c71451 | 87 | } else { |
e5a5ea9e DC |
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; | |
90 | ||
13c71451 | 91 | /* constant bitrate */ |
6f600ab3 JR |
92 | if ((ret = vorbis_encode_setup_managed(vi, avccontext->channels, |
93 | avccontext->sample_rate, minrate, | |
94 | avccontext->bit_rate, maxrate))) | |
95 | goto error; | |
13c71451 | 96 | |
57ebbccf | 97 | /* variable bitrate by estimate, disable slow rate management */ |
ca5ab8cd | 98 | if (minrate == -1 && maxrate == -1) |
6f600ab3 JR |
99 | if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL))) |
100 | goto error; | |
13c71451 | 101 | } |
c55427f8 | 102 | |
13c71451 | 103 | /* cutoff frequency */ |
ca5ab8cd | 104 | if (avccontext->cutoff > 0) { |
13c71451 | 105 | cfreq = avccontext->cutoff / 1000.0; |
6f600ab3 JR |
106 | if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq))) |
107 | goto error; | |
13c71451 | 108 | } |
81e0d0b4 | 109 | |
ca5ab8cd | 110 | if (context->iblock) { |
6f600ab3 JR |
111 | if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &context->iblock))) |
112 | goto error; | |
77336a5e MN |
113 | } |
114 | ||
6f600ab3 JR |
115 | if ((ret = vorbis_encode_setup_init(vi))) |
116 | goto error; | |
117 | ||
118 | return 0; | |
119 | error: | |
120 | return vorbis_error_to_averror(ret); | |
81e0d0b4 MH |
121 | } |
122 | ||
fd7242dd | 123 | /* How many bytes are needed for a buffer of length 'l' */ |
ca5ab8cd DB |
124 | static int xiph_len(int l) |
125 | { | |
d4b63054 | 126 | return 1 + l / 255 + l; |
ca5ab8cd | 127 | } |
fd7242dd | 128 | |
6f600ab3 JR |
129 | static av_cold int oggvorbis_encode_close(AVCodecContext *avccontext) |
130 | { | |
131 | OggVorbisContext *context = avccontext->priv_data; | |
132 | /* ogg_packet op ; */ | |
133 | ||
134 | vorbis_analysis_wrote(&context->vd, 0); /* notify vorbisenc this is EOF */ | |
135 | ||
136 | vorbis_block_clear(&context->vb); | |
137 | vorbis_dsp_clear(&context->vd); | |
138 | vorbis_info_clear(&context->vi); | |
139 | ||
140 | av_freep(&avccontext->coded_frame); | |
141 | av_freep(&avccontext->extradata); | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
ca5ab8cd DB |
146 | static av_cold int oggvorbis_encode_init(AVCodecContext *avccontext) |
147 | { | |
148 | OggVorbisContext *context = avccontext->priv_data; | |
bbb77e7c MN |
149 | ogg_packet header, header_comm, header_code; |
150 | uint8_t *p; | |
fd7242dd | 151 | unsigned int offset; |
6f600ab3 | 152 | int ret; |
81e0d0b4 | 153 | |
ca5ab8cd | 154 | vorbis_info_init(&context->vi); |
6f600ab3 | 155 | if ((ret = oggvorbis_init_encoder(&context->vi, avccontext))) { |
ca5ab8cd | 156 | av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n"); |
6f600ab3 JR |
157 | goto error; |
158 | } | |
159 | if ((ret = vorbis_analysis_init(&context->vd, &context->vi))) { | |
160 | ret = vorbis_error_to_averror(ret); | |
161 | goto error; | |
162 | } | |
163 | if ((ret = vorbis_block_init(&context->vd, &context->vb))) { | |
164 | ret = vorbis_error_to_averror(ret); | |
165 | goto error; | |
81e0d0b4 | 166 | } |
81e0d0b4 | 167 | |
bbb77e7c | 168 | vorbis_comment_init(&context->vc); |
ca5ab8cd | 169 | vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT); |
bbb77e7c | 170 | |
6f600ab3 JR |
171 | if ((ret = vorbis_analysis_headerout(&context->vd, &context->vc, &header, |
172 | &header_comm, &header_code))) { | |
173 | ret = vorbis_error_to_averror(ret); | |
174 | goto error; | |
175 | } | |
115329f1 | 176 | |
ca5ab8cd | 177 | avccontext->extradata_size = |
fd7242dd PM |
178 | 1 + xiph_len(header.bytes) + xiph_len(header_comm.bytes) + |
179 | header_code.bytes; | |
180 | p = avccontext->extradata = | |
ca5ab8cd | 181 | av_malloc(avccontext->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); |
6f600ab3 JR |
182 | if (!p) { |
183 | ret = AVERROR(ENOMEM); | |
184 | goto error; | |
185 | } | |
ca5ab8cd DB |
186 | p[0] = 2; |
187 | offset = 1; | |
ad2b531d MR |
188 | offset += av_xiphlacing(&p[offset], header.bytes); |
189 | offset += av_xiphlacing(&p[offset], header_comm.bytes); | |
190 | memcpy(&p[offset], header.packet, header.bytes); | |
191 | offset += header.bytes; | |
192 | memcpy(&p[offset], header_comm.packet, header_comm.bytes); | |
193 | offset += header_comm.bytes; | |
194 | memcpy(&p[offset], header_code.packet, header_code.bytes); | |
195 | offset += header_code.bytes; | |
fd7242dd | 196 | assert(offset == avccontext->extradata_size); |
115329f1 | 197 | |
ca5ab8cd DB |
198 | #if 0 |
199 | vorbis_block_clear(&context->vb); | |
bbb77e7c | 200 | vorbis_dsp_clear(&context->vd); |
ca5ab8cd DB |
201 | vorbis_info_clear(&context->vi); |
202 | #endif | |
bbb77e7c | 203 | vorbis_comment_clear(&context->vc); |
115329f1 | 204 | |
ca5ab8cd | 205 | avccontext->frame_size = OGGVORBIS_FRAME_SIZE; |
115329f1 | 206 | |
ca5ab8cd | 207 | avccontext->coded_frame = avcodec_alloc_frame(); |
6f600ab3 JR |
208 | if (!avccontext->coded_frame) { |
209 | ret = AVERROR(ENOMEM); | |
210 | goto error; | |
211 | } | |
115329f1 | 212 | |
ca5ab8cd | 213 | return 0; |
6f600ab3 JR |
214 | error: |
215 | oggvorbis_encode_close(avccontext); | |
216 | return ret; | |
81e0d0b4 MH |
217 | } |
218 | ||
3aca208a | 219 | static int oggvorbis_encode_frame(AVCodecContext *avccontext, |
bb270c08 | 220 | unsigned char *packets, |
ca5ab8cd | 221 | int buf_size, void *data) |
81e0d0b4 | 222 | { |
ca5ab8cd DB |
223 | OggVorbisContext *context = avccontext->priv_data; |
224 | ogg_packet op; | |
225 | signed short *audio = data; | |
9c8f0768 NG |
226 | int l; |
227 | ||
ca5ab8cd | 228 | if (data) { |
42859ddb | 229 | const int samples = avccontext->frame_size; |
ca5ab8cd | 230 | float **buffer; |
9577838f | 231 | int c, channels = context->vi.channels; |
81e0d0b4 | 232 | |
ca5ab8cd | 233 | buffer = vorbis_analysis_buffer(&context->vd, samples); |
9577838f JD |
234 | for (c = 0; c < channels; c++) { |
235 | int co = (channels > 8) ? c : | |
ca5ab8cd DB |
236 | ff_vorbis_encoding_channel_layout_offsets[channels - 1][c]; |
237 | for (l = 0; l < samples; l++) | |
238 | buffer[c][l] = audio[l * channels + co] / 32768.f; | |
bb270c08 | 239 | } |
ca5ab8cd | 240 | vorbis_analysis_wrote(&context->vd, samples); |
9c8f0768 | 241 | } else { |
ca5ab8cd DB |
242 | if (!context->eof) |
243 | vorbis_analysis_wrote(&context->vd, 0); | |
9c8f0768 NG |
244 | context->eof = 1; |
245 | } | |
81e0d0b4 | 246 | |
ca5ab8cd | 247 | while (vorbis_analysis_blockout(&context->vd, &context->vb) == 1) { |
bb270c08 | 248 | vorbis_analysis(&context->vb, NULL); |
ca5ab8cd | 249 | vorbis_bitrate_addblock(&context->vb); |
81e0d0b4 | 250 | |
ca5ab8cd | 251 | while (vorbis_bitrate_flushpacket(&context->vd, &op)) { |
0afd2a92 DB |
252 | /* i'd love to say the following line is a hack, but sadly it's |
253 | * not, apparently the end of stream decision is in libogg. */ | |
ca5ab8cd | 254 | if (op.bytes == 1 && op.e_o_s) |
6f824977 | 255 | continue; |
c426562c PM |
256 | if (context->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) { |
257 | av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow."); | |
258 | return -1; | |
259 | } | |
6d8f985e MN |
260 | memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet)); |
261 | context->buffer_index += sizeof(ogg_packet); | |
262 | memcpy(context->buffer + context->buffer_index, op.packet, op.bytes); | |
263 | context->buffer_index += op.bytes; | |
264 | // av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes); | |
bb270c08 | 265 | } |
81e0d0b4 MH |
266 | } |
267 | ||
ca5ab8cd DB |
268 | l = 0; |
269 | if (context->buffer_index) { | |
270 | ogg_packet *op2 = (ogg_packet *)context->buffer; | |
6d8f985e | 271 | op2->packet = context->buffer + sizeof(ogg_packet); |
3f4993f1 | 272 | |
ca5ab8cd | 273 | l = op2->bytes; |
91a28b0e JR |
274 | avccontext->coded_frame->pts = ff_samples_to_time_base(avccontext, |
275 | op2->granulepos); | |
3723f01c | 276 | //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate |
3f4993f1 | 277 | |
c426562c PM |
278 | if (l > buf_size) { |
279 | av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow."); | |
280 | return -1; | |
281 | } | |
282 | ||
6f824977 MN |
283 | memcpy(packets, op2->packet, l); |
284 | context->buffer_index -= l + sizeof(ogg_packet); | |
1204a13c | 285 | memmove(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index); |
6d8f985e | 286 | // av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l); |
6d8f985e MN |
287 | } |
288 | ||
3f4993f1 | 289 | return l; |
81e0d0b4 MH |
290 | } |
291 | ||
d36beb3f | 292 | AVCodec ff_libvorbis_encoder = { |
86714887 DB |
293 | .name = "libvorbis", |
294 | .type = AVMEDIA_TYPE_AUDIO, | |
295 | .id = CODEC_ID_VORBIS, | |
296 | .priv_data_size = sizeof(OggVorbisContext), | |
297 | .init = oggvorbis_encode_init, | |
298 | .encode = oggvorbis_encode_frame, | |
299 | .close = oggvorbis_encode_close, | |
300 | .capabilities = CODEC_CAP_DELAY, | |
ca5ab8cd | 301 | .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE }, |
86714887 DB |
302 | .long_name = NULL_IF_CONFIG_SMALL("libvorbis Vorbis"), |
303 | .priv_class = &class, | |
304 | }; |