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
23 #include <opus_multistream.h>
24
34
45 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
46 int apply_phase_inv;
47 #endif
49
59
61 0, 1, 1, 2, 2, 2, 2, 3
62 };
63
64 /* Opus internal to Vorbis channel order mapping written in the header */
66 { 0 },
67 { 0, 1 },
68 { 0, 2, 1 },
69 { 0, 1, 2, 3 },
70 { 0, 4, 1, 2, 3 },
71 { 0, 4, 1, 2, 3, 5 },
72 { 0, 4, 1, 2, 3, 5, 6 },
73 { 0, 6, 1, 2, 3, 4, 5, 7 },
74 };
75
76 /* libavcodec to libopus channel order mapping, passed to libopus */
78 { 0 },
79 { 0, 1 },
80 { 0, 1, 2 },
81 { 0, 1, 2, 3 },
82 { 0, 1, 3, 4, 2 },
83 { 0, 1, 4, 5, 2, 3 },
84 { 0, 1, 5, 6, 2, 4, 3 },
85 { 0, 1, 6, 7, 4, 5, 2, 3 },
86 };
87
89 int coupled_stream_count,
90 int mapping_family,
91 const uint8_t *channel_mapping)
92 {
95
97 bytestream_put_byte(&p, 1); /* Version */
100 bytestream_put_le32(&p, avctx->
sample_rate);
/* Original sample rate */
101 bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
102
103 /* Channel mapping */
104 bytestream_put_byte(&p, mapping_family);
105 if (mapping_family != 0) {
106 bytestream_put_byte(&p, stream_count);
107 bytestream_put_byte(&p, coupled_stream_count);
109 }
110 }
111
114 {
116
119 "Quality-based encoding not supported, "
120 "please specify a bitrate and VBR setting.\n");
122 }
123
124 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->
bit_rate));
125 if (
ret != OPUS_OK) {
127 "Failed to set bitrate: %s\n", opus_strerror(
ret));
129 }
130
131 ret = opus_multistream_encoder_ctl(enc,
132 OPUS_SET_COMPLEXITY(
opts->complexity));
135 "Unable to set complexity: %s\n", opus_strerror(
ret));
136
137 ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!
opts->vbr));
140 "Unable to set VBR: %s\n", opus_strerror(
ret));
141
142 ret = opus_multistream_encoder_ctl(enc,
143 OPUS_SET_VBR_CONSTRAINT(
opts->vbr == 2));
146 "Unable to set constrained VBR: %s\n", opus_strerror(
ret));
147
148 ret = opus_multistream_encoder_ctl(enc,
149 OPUS_SET_PACKET_LOSS_PERC(
opts->packet_loss));
152 "Unable to set expected packet loss percentage: %s\n",
154
155 ret = opus_multistream_encoder_ctl(enc,
156 OPUS_SET_INBAND_FEC(
opts->fec));
159 "Unable to set inband FEC: %s\n",
161
163 ret = opus_multistream_encoder_ctl(enc,
164 OPUS_SET_MAX_BANDWIDTH(
opts->max_bandwidth));
167 "Unable to set maximum bandwidth: %s\n", opus_strerror(
ret));
168 }
169
170 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
171 ret = opus_multistream_encoder_ctl(enc,
172 OPUS_SET_PHASE_INVERSION_DISABLED(!
opts->apply_phase_inv));
175 "Unable to set phase inversion: %s\n",
177 #endif
178 return OPUS_OK;
179 }
180
182 int max_channels) {
187 }
188
189 return 0;
190 }
191
194
197 "No channel layout specified. Opus encoder will use Vorbis "
201
204 "Invalid channel layout %s for specified mapping family %d.\n",
205 name, mapping_family);
206
208 }
209
210 return 0;
211 }
212
215 int mapping_family,
216 const uint8_t ** channel_map_result)
217 {
218 const uint8_t * channel_map =
NULL;
220
221 switch (mapping_family) {
222 case -1:
226 /* Channels do not need to be reordered. */
227 }
228
229 break;
230 case 0:
234 }
235 break;
236 case 1:
237 /* Opus expects channels to be in Vorbis order. */
242 }
243 break;
244 case 255:
246 break;
247 default:
249 "Unknown channel mapping family %d. Output channel layout may be invalid.\n",
250 mapping_family);
252 }
253
254 *channel_map_result = channel_map;
256 }
257
259 {
261 OpusMSEncoder *enc;
262 uint8_t libopus_channel_mapping[255];
265 int av_ret;
266 int coupled_stream_count, header_size,
frame_size;
267 int mapping_family;
268
271 case 120:
272 case 240:
275 "LPC mode cannot be used with a frame duration of less "
276 "than 10ms. Enabling restricted low-delay mode.\n"
277 "Use a longer frame duration if this is not what you want.\n");
278 /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
279 * RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
281 case 480:
282 case 960:
283 case 1920:
284 case 2880:
285 #ifdef OPUS_FRAMESIZE_120_MS
286 case 3840:
287 case 4800:
288 case 5760:
289 #endif
292 break;
293 default:
295 "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40"
296 #ifdef OPUS_FRAMESIZE_120_MS
297 ", 60, 80, 100 or 120.\n",
298 #else
299 " or 60.\n",
300 #endif
303 }
304
307 "Compression level must be in the range 0 to 10. "
308 "Defaulting to 10.\n");
310 } else {
312 }
313
316 case 4000:
318 break;
319 case 6000:
321 break;
322 case 8000:
324 break;
325 case 12000:
327 break;
328 case 20000:
330 break;
331 default:
333 "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
334 "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
337 }
338 }
339
340 /* Channels may need to be reordered to match opus mapping. */
343 if (av_ret) {
344 return av_ret;
345 }
346
348 /* By default, use mapping family 1 for the header but use the older
349 * libopus multistream API to avoid surround masking. */
350
351 /* Set the mapping family so that the value is correct in the header */
352 mapping_family =
channels > 2 ? 1 : 0;
355 memcpy(libopus_channel_mapping,
357 channels *
sizeof(*libopus_channel_mapping));
358
359 enc = opus_multistream_encoder_create(
361 coupled_stream_count,
364 } else {
365 /* Use the newer multistream API. The encoder will set the channel
366 * mapping and coupled stream counts to its internal defaults and will
367 * use surround masking analysis to save bits. */
369 enc = opus_multistream_surround_encoder_create(
371 &opus->
stream_count, &coupled_stream_count, libopus_channel_mapping,
373 }
374
375 if (
ret != OPUS_OK) {
377 "Failed to create encoder: %s\n", opus_strerror(
ret));
379 }
380
382 /* Sane default copied from opusenc */
384 32000 * coupled_stream_count;
386 "No bit rate set. Defaulting to %"PRId64
" bps.\n", avctx->
bit_rate);
387 }
388
391 "Please choose a value between 500 and %d.\n", avctx->
bit_rate,
395 }
396
398 if (
ret != OPUS_OK) {
401 }
402
403 /* Header includes channel mapping table if and only if mapping family is NOT 0 */
404 header_size = 19 + (mapping_family == 0 ? 0 : 2 +
channels);
410 }
412
419 }
420
424 "Unable to get number of lookahead samples: %s\n",
426
428 mapping_family, libopus_channel_mapping);
429
431
433
434 return 0;
435
437 opus_multistream_encoder_destroy(enc);
439 }
440
442 uint8_t *dst,
const uint8_t *
src,
const uint8_t *channel_map,
443 int nb_channels, int nb_samples, int bytes_per_sample) {
447 const size_t src_pos = bytes_per_sample * (nb_channels *
sample +
channel);
448 const size_t dst_pos = bytes_per_sample * (nb_channels *
sample + channel_map[
channel]);
449
450 memcpy(&dst[dst_pos], &
src[src_pos], bytes_per_sample);
451 }
452 }
453 }
454
457 {
461 const int sample_size =
channels * bytes_per_sample;
462 uint8_t *audio;
464 int discard_padding;
465
477 memcpy(audio,
frame->data[0],
frame->nb_samples * sample_size);
478 } else
479 audio =
frame->data[0];
480 } else {
482 return 0;
485 }
486
487 /* Maximum packet size taken from opusenc in opus-tools. 120ms packets
488 * consist of 6 frames in one packet. The maximum frame size is 1275
489 * bytes along with the largest possible packet header of 7 bytes. */
492
494 ret = opus_multistream_encode_float(opus->
enc, (
float *)audio,
497 else
498 ret = opus_multistream_encode(opus->
enc, (opus_int16 *)audio,
501
504 "Error encoding frame: %s\n", opus_strerror(
ret));
506 }
507
509
512
514 // Check if subtraction resulted in an overflow
515 if ((discard_padding < opus->
opts.packet_size) != (avpkt->
duration > 0)) {
518 }
519 if (discard_padding > 0) {
522 10);
523 if(!side_data) {
526 }
527 AV_WL32(side_data + 4, discard_padding);
528 }
529
530 *got_packet_ptr = 1;
531
532 return 0;
533 }
534
536 {
538
539 opus_multistream_encoder_destroy(opus->
enc);
540
542
544
545 return 0;
546 }
547
548 #define OFFSET(x) offsetof(LibopusEncContext, opts.x)
549 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
551 {
"application",
"Intended application type",
OFFSET(application),
AV_OPT_TYPE_INT, { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY,
FLAGS,
"application" },
552 {
"voip",
"Favor improved speech intelligibility", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP }, 0, 0,
FLAGS,
"application" },
553 {
"audio",
"Favor faithfulness to the input", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO }, 0, 0,
FLAGS,
"application" },
554 {
"lowdelay",
"Restrict to only the lowest delay modes", 0,
AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0,
FLAGS,
"application" },
555 {
"frame_duration",
"Duration of a frame in milliseconds",
OFFSET(frame_duration),
AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 120.0,
FLAGS },
562 {
"mapping_family",
"Channel Mapping Family",
OFFSET(mapping_family),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255,
FLAGS,
"mapping_family" },
563 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
564 {
"apply_phase_inv",
"Apply intensity stereo phase inversion",
OFFSET(apply_phase_inv),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
FLAGS },
565 #endif
567 };
568
574 };
575
577 { "b", "0" },
578 { "compression_level", "10" },
580 };
581
583 48000, 24000, 16000, 12000, 8000, 0,
584 };
585
602 .p.wrapper_name = "libopus",
603 };