1 /*
2 * Opus encoder using libopus
3 * Copyright (c) 2012 Nathan Caldwell
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <opus.h>
23 #include <opus_multistream.h>
24
32
42
51
53 0, 1, 1, 2, 2, 2, 2, 3
54 };
55
56 /* Opus internal to Vorbis channel order mapping written in the header */
58 { 0 },
59 { 0, 1 },
60 { 0, 2, 1 },
61 { 0, 1, 2, 3 },
62 { 0, 4, 1, 2, 3 },
63 { 0, 4, 1, 2, 3, 5 },
64 { 0, 4, 1, 2, 3, 5, 6 },
65 { 0, 6, 1, 2, 3, 4, 5, 7 },
66 };
67
68 /* libavcodec to libopus channel order mapping, passed to libopus */
70 { 0 },
71 { 0, 1 },
72 { 0, 1, 2 },
73 { 0, 1, 2, 3 },
74 { 0, 1, 3, 4, 2 },
75 { 0, 1, 4, 5, 2, 3 },
76 { 0, 1, 5, 6, 2, 4, 3 },
77 { 0, 1, 6, 7, 4, 5, 2, 3 },
78 };
79
81 int coupled_stream_count,
83 {
86
88 bytestream_put_byte(&p, 1); /* Version */
89 bytestream_put_byte(&p, channels);
90 bytestream_put_le16(&p, avctx->
delay);
/* Lookahead samples at 48kHz */
91 bytestream_put_le32(&p, avctx->
sample_rate);
/* Original sample rate */
92 bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
93
94 /* Channel mapping */
95 if (channels > 2) {
96 bytestream_put_byte(&p, channels <= 8 ? 1 : 255);
97 bytestream_put_byte(&p, stream_count);
98 bytestream_put_byte(&p, coupled_stream_count);
100 } else {
101 bytestream_put_byte(&p, 0);
102 }
103 }
104
107 {
109
112 "Quality-based encoding not supported, "
113 "please specify a bitrate and VBR setting.\n");
115 }
116
117 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->
bit_rate));
118 if (ret != OPUS_OK) {
120 "Failed to set bitrate: %s\n", opus_strerror(ret));
122 }
123
124 ret = opus_multistream_encoder_ctl(enc,
126 if (ret != OPUS_OK)
128 "Unable to set complexity: %s\n", opus_strerror(ret));
129
130 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->
vbr));
131 if (ret != OPUS_OK)
133 "Unable to set VBR: %s\n", opus_strerror(ret));
134
135 ret = opus_multistream_encoder_ctl(enc,
136 OPUS_SET_VBR_CONSTRAINT(opts->
vbr == 2));
137 if (ret != OPUS_OK)
139 "Unable to set constrained VBR: %s\n", opus_strerror(ret));
140
141 ret = opus_multistream_encoder_ctl(enc,
143 if (ret != OPUS_OK)
145 "Unable to set expected packet loss percentage: %s\n",
146 opus_strerror(ret));
147
149 ret = opus_multistream_encoder_ctl(enc,
151 if (ret != OPUS_OK)
153 "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
154 }
155
156 return OPUS_OK;
157 }
158
160 {
162 const uint8_t *channel_mapping;
163 OpusMSEncoder *enc;
165 int coupled_stream_count, header_size,
frame_size;
166
170
171 /* FIXME: Opus can handle up to 255 channels. However, the mapping for
172 * anything greater than 8 is undefined. */
175 "Channel layout undefined for %d channels.\n", avctx->
channels);
176
178 /* Sane default copied from opusenc */
180 32000 * coupled_stream_count;
182 "No bit rate set. Defaulting to %d bps.\n", avctx->
bit_rate);
183 }
184
187 "Please choose a value between 500 and %d.\n", avctx->
bit_rate,
190 }
191
193 switch (frame_size) {
194 case 120:
195 case 240:
198 "LPC mode cannot be used with a frame duration of less "
199 "than 10ms. Enabling restricted low-delay mode.\n"
200 "Use a longer frame duration if this is not what you want.\n");
201 /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
202 * RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
204 case 480:
205 case 960:
206 case 1920:
207 case 2880:
210 break;
211 default:
213 "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40 or 60.\n",
216 }
217
220 "Compression level must be in the range 0 to 10. "
221 "Defaulting to 10.\n");
223 } else {
225 }
226
229 case 4000:
231 break;
232 case 6000:
234 break;
235 case 8000:
237 break;
238 case 12000:
240 break;
241 case 20000:
243 break;
244 default:
246 "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
247 "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
250 }
251 }
252
255 coupled_stream_count,
256 channel_mapping,
258 if (ret != OPUS_OK) {
260 "Failed to create encoder: %s\n", opus_strerror(ret));
262 }
263
265 if (ret != OPUS_OK) {
267 goto fail;
268 }
269
275 goto fail;
276 }
278
284 goto fail;
285 }
286
287 ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->
delay));
288 if (ret != OPUS_OK)
290 "Unable to get number of lookahead samples: %s\n",
291 opus_strerror(ret));
292
295
297
299
300 return 0;
301
302 fail:
303 opus_multistream_encoder_destroy(enc);
306 }
307
310 {
312 const int sample_size = avctx->
channels *
316 int discard_padding;
317
318 if (frame) {
323 } else
324 audio = frame->
data[0];
325 } else {
327 return 0;
330 }
331
332 /* Maximum packet size taken from opusenc in opus-tools. 60ms packets
333 * consist of 3 frames in one packet. The maximum frame size is 1275
334 * bytes along with the largest possible packet header of 7 bytes. */
337
339 ret = opus_multistream_encode_float(opus->
enc, (
float *)audio,
342 else
343 ret = opus_multistream_encode(opus->
enc, (opus_int16 *)audio,
346
347 if (ret < 0) {
349 "Error encoding frame: %s\n", opus_strerror(ret));
351 }
352
354
357
359 // Check if subtraction resulted in an overflow
360 if ((discard_padding < opus->opts.packet_size) != (avpkt->
duration > 0)) {
364 }
365 if (discard_padding > 0) {
368 10);
369 if(side_data == NULL) {
373 }
374 AV_WL32(side_data + 4, discard_padding);
375 }
376
377 *got_packet_ptr = 1;
378
379 return 0;
380 }
381
383 {
385
386 opus_multistream_encoder_destroy(opus->
enc);
387
389
392
393 return 0;
394 }
395
396 #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
397 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
399 {
"application",
"Intended application type",
OFFSET(application),
AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY,
FLAGS,
"application" },
400 {
"voip",
"Favor improved speech intelligibility", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0,
FLAGS,
"application" },
401 {
"audio",
"Favor faithfulness to the input", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0,
FLAGS,
"application" },
402 {
"lowdelay",
"Restrict to only the lowest delay modes", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0,
FLAGS,
"application" },
403 {
"frame_duration",
"Duration of a frame in milliseconds",
OFFSET(frame_duration),
AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 60.0,
FLAGS },
409 { NULL },
410 };
411
417 };
418
420 { "b", "0" },
421 { "compression_level", "10" },
422 { NULL },
423 };
424
426 48000, 24000, 16000, 12000, 8000, 0,
427 };
428
446 };