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
45 vorbis_info
vi;
/**< vorbis_info used during init */
46 vorbis_dsp_state
vd;
/**< DSP state used for analysis */
47 vorbis_block
vb;
/**< vorbis_block used for analysis */
49 int eof;
/**< end-of-file flag */
51 vorbis_comment
vc;
/**< VorbisComment info */
52 double iblock;
/**< impulse block bias option */
56
59 { NULL }
60 };
61
63 { "b", "0" },
64 { NULL },
65 };
66
72 };
73
75 {
76 switch (ov_err) {
78 case OV_EINVAL:
return AVERROR(EINVAL);
79 case OV_EIMPL:
return AVERROR(EINVAL);
81 }
82 }
83
86 {
88 double cfreq;
90
92 /* variable bitrate
93 * NOTE: we use the oggenc range of -1 to 10 for global_quality for
94 * user convenience, but libvorbis uses -0.1 to 1.0.
95 */
97 /* default to 3 if the user did not set quality or bitrate */
99 q = 3.0;
100 if ((ret = vorbis_encode_setup_vbr(vi, avctx->
channels,
102 q / 10.0)))
103 goto error;
104 } else {
107
108 /* average bitrate */
109 if ((ret = vorbis_encode_setup_managed(vi, avctx->
channels,
112 goto error;
113
114 /* variable bitrate by estimate, disable slow rate management */
115 if (minrate == -1 && maxrate == -1)
116 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL)))
117 goto error; /* should not happen */
118 }
119
120 /* cutoff frequency */
122 cfreq = avctx->
cutoff / 1000.0;
123 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)))
124 goto error; /* should not happen */
125 }
126
127 /* impulse block bias */
129 if ((ret = vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &s->
iblock)))
130 goto error;
131 }
132
153 "output stream will have incorrect "
154 "channel layout.\n", name);
155 } else {
157 "will use Vorbis channel layout for "
159 }
160 }
161
162 if ((ret = vorbis_encode_setup_init(vi)))
163 goto error;
164
165 return 0;
166 error:
168 }
169
170 /* How many bytes are needed for a buffer of length 'l' */
172 {
173 return 1 + l / 255 + l;
174 }
175
177 {
179
180 /* notify vorbisenc this is EOF */
182 vorbis_analysis_wrote(&s->
vd, 0);
183
184 vorbis_block_clear(&s->
vb);
185 vorbis_dsp_clear(&s->
vd);
186 vorbis_info_clear(&s->
vi);
187
191
192 return 0;
193 }
194
196 {
202
203 vorbis_info_init(&s->
vi);
206 goto error;
207 }
208 if ((ret = vorbis_analysis_init(&s->
vd, &s->
vi))) {
211 goto error;
212 }
214 if ((ret = vorbis_block_init(&s->
vd, &s->
vb))) {
217 goto error;
218 }
219
220 vorbis_comment_init(&s->
vc);
223
224 if ((ret = vorbis_analysis_headerout(&s->
vd, &s->
vc, &header, &header_comm,
225 &header_code))) {
227 goto error;
228 }
229
232 header_code.bytes;
235 if (!p) {
237 goto error;
238 }
239 p[0] = 2;
240 offset = 1;
243 memcpy(&p[offset], header.packet, header.bytes);
244 offset += header.bytes;
245 memcpy(&p[offset], header_comm.packet, header_comm.bytes);
246 offset += header_comm.bytes;
247 memcpy(&p[offset], header_code.packet, header_code.bytes);
248 offset += header_code.bytes;
250
254 }
255
256 vorbis_comment_clear(&s->
vc);
257
260
264 goto error;
265 }
266
267 return 0;
268 error:
271 }
272
275 {
279
280 /* send samples to libvorbis */
281 if (frame) {
284 int c, channels = s->
vi.channels;
285
286 buffer = vorbis_analysis_buffer(&s->
vd, samples);
287 for (c = 0; c < channels; c++) {
288 int co = (channels > 8) ? c :
291 samples * sizeof(*buffer[c]));
292 }
293 if ((ret = vorbis_analysis_wrote(&s->
vd, samples)) < 0) {
296 }
299 } else {
301 if ((ret = vorbis_analysis_wrote(&s->
vd, 0)) < 0) {
304 }
306 }
307
308 /* retrieve available packets from libvorbis */
309 while ((ret = vorbis_analysis_blockout(&s->
vd, &s->
vb)) == 1) {
310 if ((ret = vorbis_analysis(&s->
vb, NULL)) < 0)
311 break;
312 if ((ret = vorbis_bitrate_addblock(&s->
vb)) < 0)
313 break;
314
315 /* add any available packets to the output packet buffer */
316 while ((ret = vorbis_bitrate_flushpacket(&s->
vd, &op)) == 1) {
320 }
323 }
324 if (ret < 0) {
326 break;
327 }
328 }
329 if (ret < 0) {
332 }
333
334 /* check for available packets */
336 return 0;
337
339
343
345
347 if (duration > 0) {
348 /* we do not know encoder delay until we get the first packet from
349 * libvorbis, so we have to update the AudioFrameQueue counts */
356 }
358 }
359
360 *got_packet_ptr = 1;
361 return 0;
362 }
363
378 };