1 /*
2 * Copyright (c) 2002 Mark Hills <mark@pogo.org.uk>
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <vorbis/vorbisenc.h>
22
31
32
33 /* Number of samples the user should send in each call.
34 * This value is used because it is the LCD of all possible frame sizes, so
35 * an output packet will always start at the same point as one of the input
36 * packets.
37 */
38 #define OGGVORBIS_FRAME_SIZE 64
39
40 #define BUFFER_SIZE (1024 * 64)
41
44 vorbis_info
vi;
/**< vorbis_info used during init */
45 vorbis_dsp_state
vd;
/**< DSP state used for analysis */
46 vorbis_block
vb;
/**< vorbis_block used for analysis */
48 int eof;
/**< end-of-file flag */
50 vorbis_comment
vc;
/**< VorbisComment info */
51 double iblock;
/**< impulse block bias option */
55
58 { NULL }
59 };
60
62 { "b", "0" },
63 { NULL },
64 };
65
71 };
72
74 {
75 switch (ov_err) {
77 case OV_EINVAL:
return AVERROR(EINVAL);
78 case OV_EIMPL:
return AVERROR(EINVAL);
80 }
81 }
82
85 {
87 double cfreq;
89
91 /* variable bitrate
92 * NOTE: we use the oggenc range of -1 to 10 for global_quality for
93 * user convenience, but libvorbis uses -0.1 to 1.0.
94 */
96 /* default to 3 if the user did not set quality or bitrate */
98 q = 3.0;
99 if ((ret = vorbis_encode_setup_vbr(vi, avctx->
channels,
101 q / 10.0)))
102 goto error;
103 } else {
106
107 /* average bitrate */
108 if ((ret = vorbis_encode_setup_managed(vi, avctx->
channels,
111 goto error;
112
113 /* variable bitrate by estimate, disable slow rate management */
114 if (minrate == -1 && maxrate == -1)
115 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
116 goto error; /* should not happen */
117 }
118
119 /* cutoff frequency */
121 cfreq = avctx->
cutoff / 1000.0;
122 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
123 goto error; /* should not happen */
124 }
125
126 /* impulse block bias */
128 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->
iblock)))
129 goto error;
130 }
131
152 "output stream will have incorrect "
153 "channel layout.\n", name);
154 } else {
156 "will use Vorbis channel layout for "
158 }
159 }
160
161 if ((ret = vorbis_encode_setup_init(vi)))
162 goto error;
163
164 return 0;
165 error:
167 }
168
169 /* How many bytes are needed for a buffer of length 'l' */
171 {
172 return 1 + l / 255 + l;
173 }
174
176 {
178
179 /* notify vorbisenc this is EOF */
181 vorbis_analysis_wrote(&s->
vd, 0);
182
183 vorbis_block_clear(&s->
vb);
184 vorbis_dsp_clear(&s->
vd);
185 vorbis_info_clear(&s->
vi);
186
190
191 return 0;
192 }
193
195 {
201
202 vorbis_info_init(&s->
vi);
205 goto error;
206 }
207 if ((ret = vorbis_analysis_init(&s->
vd, &s->
vi))) {
210 goto error;
211 }
213 if ((ret = vorbis_block_init(&s->
vd, &s->
vb))) {
216 goto error;
217 }
218
219 vorbis_comment_init(&s->
vc);
222
223 if ((ret = vorbis_analysis_headerout(&s->
vd, &s->
vc, &header, &header_comm,
224 &header_code))) {
226 goto error;
227 }
228
231 header_code.bytes;
234 if (!p) {
236 goto error;
237 }
238 p[0] = 2;
239 offset = 1;
242 memcpy(&p[offset], header.packet, header.bytes);
243 offset += header.bytes;
244 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
245 offset += header_comm.bytes;
246 memcpy(&p[offset], header_code.packet, header_code.bytes);
247 offset += header_code.bytes;
249
253 }
254
255 vorbis_comment_clear(&s->
vc);
256
259
263 goto error;
264 }
265
266 return 0;
267 error:
270 }
271
274 {
278
279 /* send samples to libvorbis */
280 if (frame) {
283 int c, channels = s->
vi.channels;
284
285 buffer = vorbis_analysis_buffer(&s->
vd, samples);
286 for (c = 0; c < channels; c++) {
287 int co = (channels > 8) ? c :
290 samples * sizeof(*buffer[c]));
291 }
292 if ((ret = vorbis_analysis_wrote(&s->
vd, samples)) < 0) {
295 }
298 } else {
300 if ((ret = vorbis_analysis_wrote(&s->
vd, 0)) < 0) {
303 }
305 }
306
307 /* retrieve available packets from libvorbis */
308 while ((ret = vorbis_analysis_blockout(&s->
vd, &s->
vb)) == 1) {
309 if ((ret = vorbis_analysis(&s->
vb, NULL)) < 0)
310 break;
311 if ((ret = vorbis_bitrate_addblock(&s->
vb)) < 0)
312 break;
313
314 /* add any available packets to the output packet buffer */
315 while ((ret = vorbis_bitrate_flushpacket(&s->
vd, &op)) == 1) {
319 }
322 }
323 if (ret < 0) {
325 break;
326 }
327 }
328 if (ret < 0) {
331 }
332
333 /* check for available packets */
335 return 0;
336
338
342
344
346 if (duration > 0) {
347 /* we do not know encoder delay until we get the first packet from
348 * libvorbis, so we have to update the AudioFrameQueue counts */
355 }
357 }
358
359 *got_packet_ptr = 1;
360 return 0;
361 }
362
377 };