Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/libopusenc.c
Line
Count
Source
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
25
#include "libavutil/channel_layout.h"
26
#include "libavutil/mem.h"
27
#include "libavutil/opt.h"
28
#include "avcodec.h"
29
#include "bytestream.h"
30
#include "codec_internal.h"
31
#include "encode.h"
32
#include "libopus.h"
33
#include "audio_frame_queue.h"
34
#include "vorbis_data.h"
35
36
typedef struct LibopusEncOpts {
37
    int vbr;
38
    int application;
39
    int packet_loss;
40
    int fec;
41
    int complexity;
42
    float frame_duration;
43
    int packet_size;
44
    int max_bandwidth;
45
    int mapping_family;
46
#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
47
    int apply_phase_inv;
48
#endif
49
} LibopusEncOpts;
50
51
typedef struct LibopusEncContext {
52
    AVClass *class;
53
    OpusMSEncoder *enc;
54
    int stream_count;
55
    uint8_t *samples;
56
    LibopusEncOpts opts;
57
    AudioFrameQueue afq;
58
    const uint8_t *encoder_channel_map;
59
} LibopusEncContext;
60
61
static const uint8_t opus_coupled_streams[8] = {
62
    0, 1, 1, 2, 2, 2, 2, 3
63
};
64
65
/* Opus internal to Vorbis channel order mapping written in the header */
66
static const uint8_t opus_vorbis_channel_map[8][8] = {
67
    { 0 },
68
    { 0, 1 },
69
    { 0, 2, 1 },
70
    { 0, 1, 2, 3 },
71
    { 0, 4, 1, 2, 3 },
72
    { 0, 4, 1, 2, 3, 5 },
73
    { 0, 4, 1, 2, 3, 5, 6 },
74
    { 0, 6, 1, 2, 3, 4, 5, 7 },
75
};
76
77
/* libavcodec to libopus channel order mapping, passed to libopus */
78
static const uint8_t libavcodec_libopus_channel_map[8][8] = {
79
    { 0 },
80
    { 0, 1 },
81
    { 0, 1, 2 },
82
    { 0, 1, 2, 3 },
83
    { 0, 1, 3, 4, 2 },
84
    { 0, 1, 4, 5, 2, 3 },
85
    { 0, 1, 5, 6, 2, 4, 3 },
86
    { 0, 1, 6, 7, 4, 5, 2, 3 },
87
};
88
89
static void libopus_write_header(AVCodecContext *avctx, int stream_count,
90
                                 int coupled_stream_count,
91
                                 int mapping_family,
92
                                 const uint8_t *channel_mapping)
93
0
{
94
0
    uint8_t *p   = avctx->extradata;
95
0
    int channels = avctx->ch_layout.nb_channels;
96
97
0
    bytestream_put_buffer(&p, "OpusHead", 8);
98
0
    bytestream_put_byte(&p, 1); /* Version */
99
0
    bytestream_put_byte(&p, channels);
100
0
    bytestream_put_le16(&p, avctx->initial_padding * 48000 / avctx->sample_rate); /* Lookahead samples at 48kHz */
101
0
    bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */
102
0
    bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
103
104
    /* Channel mapping */
105
0
    bytestream_put_byte(&p, mapping_family);
106
0
    if (mapping_family != 0) {
107
0
        bytestream_put_byte(&p, stream_count);
108
0
        bytestream_put_byte(&p, coupled_stream_count);
109
0
        bytestream_put_buffer(&p, channel_mapping, channels);
110
0
    }
111
0
}
112
113
static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc,
114
                                     LibopusEncOpts *opts)
115
0
{
116
0
    int ret;
117
118
0
    if (avctx->global_quality) {
119
0
        av_log(avctx, AV_LOG_ERROR,
120
0
               "Quality-based encoding not supported, "
121
0
               "please specify a bitrate and VBR setting.\n");
122
0
        return AVERROR(EINVAL);
123
0
    }
124
125
0
    ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->bit_rate));
126
0
    if (ret != OPUS_OK) {
127
0
        av_log(avctx, AV_LOG_ERROR,
128
0
               "Failed to set bitrate: %s\n", opus_strerror(ret));
129
0
        return ret;
130
0
    }
131
132
0
    ret = opus_multistream_encoder_ctl(enc,
133
0
                                       OPUS_SET_COMPLEXITY(opts->complexity));
134
0
    if (ret != OPUS_OK)
135
0
        av_log(avctx, AV_LOG_WARNING,
136
0
               "Unable to set complexity: %s\n", opus_strerror(ret));
137
138
0
    ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->vbr));
139
0
    if (ret != OPUS_OK)
140
0
        av_log(avctx, AV_LOG_WARNING,
141
0
               "Unable to set VBR: %s\n", opus_strerror(ret));
142
143
0
    ret = opus_multistream_encoder_ctl(enc,
144
0
                                       OPUS_SET_VBR_CONSTRAINT(opts->vbr == 2));
145
0
    if (ret != OPUS_OK)
146
0
        av_log(avctx, AV_LOG_WARNING,
147
0
               "Unable to set constrained VBR: %s\n", opus_strerror(ret));
148
149
0
    ret = opus_multistream_encoder_ctl(enc,
150
0
                                       OPUS_SET_PACKET_LOSS_PERC(opts->packet_loss));
151
0
    if (ret != OPUS_OK)
152
0
        av_log(avctx, AV_LOG_WARNING,
153
0
               "Unable to set expected packet loss percentage: %s\n",
154
0
               opus_strerror(ret));
155
156
0
    ret = opus_multistream_encoder_ctl(enc,
157
0
                                       OPUS_SET_INBAND_FEC(opts->fec));
158
0
    if (ret != OPUS_OK)
159
0
        av_log(avctx, AV_LOG_WARNING,
160
0
               "Unable to set inband FEC: %s\n",
161
0
               opus_strerror(ret));
162
163
0
    if (avctx->cutoff) {
164
0
        ret = opus_multistream_encoder_ctl(enc,
165
0
                                           OPUS_SET_MAX_BANDWIDTH(opts->max_bandwidth));
166
0
        if (ret != OPUS_OK)
167
0
            av_log(avctx, AV_LOG_WARNING,
168
0
                   "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
169
0
    }
170
171
0
#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
172
0
    ret = opus_multistream_encoder_ctl(enc,
173
0
                                       OPUS_SET_PHASE_INVERSION_DISABLED(!opts->apply_phase_inv));
174
0
    if (ret != OPUS_OK)
175
0
        av_log(avctx, AV_LOG_WARNING,
176
0
               "Unable to set phase inversion: %s\n",
177
0
               opus_strerror(ret));
178
0
#endif
179
0
    return OPUS_OK;
180
0
}
181
182
static int libopus_check_max_channels(AVCodecContext *avctx,
183
0
                                      int max_channels) {
184
0
    if (avctx->ch_layout.nb_channels > max_channels) {
185
0
        av_log(avctx, AV_LOG_ERROR, "Opus mapping family undefined for %d channels.\n",
186
0
               avctx->ch_layout.nb_channels);
187
0
        return AVERROR(EINVAL);
188
0
    }
189
190
0
    return 0;
191
0
}
192
193
0
static int libopus_check_vorbis_layout(AVCodecContext *avctx, int mapping_family) {
194
0
    av_assert2(avctx->ch_layout.nb_channels < FF_ARRAY_ELEMS(ff_vorbis_ch_layouts));
195
196
0
    if (avctx->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
197
0
        av_log(avctx, AV_LOG_WARNING,
198
0
               "No channel layout specified. Opus encoder will use Vorbis "
199
0
               "channel layout for %d channels.\n", avctx->ch_layout.nb_channels);
200
0
    } else if (av_channel_layout_compare(&avctx->ch_layout, &ff_vorbis_ch_layouts[avctx->ch_layout.nb_channels - 1])) {
201
0
        char name[32];
202
203
0
        av_channel_layout_describe(&avctx->ch_layout, name, sizeof(name));
204
0
        av_log(avctx, AV_LOG_ERROR,
205
0
               "Invalid channel layout %s for specified mapping family %d.\n",
206
0
               name, mapping_family);
207
208
0
        return AVERROR(EINVAL);
209
0
    }
210
211
0
    return 0;
212
0
}
213
214
static int libopus_validate_layout_and_get_channel_map(
215
        AVCodecContext *avctx,
216
        int mapping_family,
217
        const uint8_t ** channel_map_result)
218
0
{
219
0
    const uint8_t * channel_map = NULL;
220
0
    int ret;
221
222
0
    switch (mapping_family) {
223
0
    case -1:
224
0
        ret = libopus_check_max_channels(avctx, 8);
225
0
        if (ret == 0) {
226
0
            ret = libopus_check_vorbis_layout(avctx, mapping_family);
227
            /* Channels do not need to be reordered. */
228
0
        }
229
230
0
        break;
231
0
    case 0:
232
0
        ret = libopus_check_max_channels(avctx, 2);
233
0
        if (ret == 0) {
234
0
            ret = libopus_check_vorbis_layout(avctx, mapping_family);
235
0
        }
236
0
        break;
237
0
    case 1:
238
        /* Opus expects channels to be in Vorbis order. */
239
0
        ret = libopus_check_max_channels(avctx, 8);
240
0
        if (ret == 0) {
241
0
            ret = libopus_check_vorbis_layout(avctx, mapping_family);
242
0
            channel_map = ff_vorbis_channel_layout_offsets[avctx->ch_layout.nb_channels - 1];
243
0
        }
244
0
        break;
245
0
    case 255:
246
0
        ret = libopus_check_max_channels(avctx, 254);
247
0
        break;
248
0
    default:
249
0
        av_log(avctx, AV_LOG_WARNING,
250
0
               "Unknown channel mapping family %d. Output channel layout may be invalid.\n",
251
0
               mapping_family);
252
0
        ret = 0;
253
0
    }
254
255
0
    *channel_map_result = channel_map;
256
0
    return ret;
257
0
}
258
259
static av_cold int libopus_encode_init(AVCodecContext *avctx)
260
0
{
261
0
    LibopusEncContext *opus = avctx->priv_data;
262
0
    OpusMSEncoder *enc;
263
0
    uint8_t libopus_channel_mapping[255];
264
0
    int ret = OPUS_OK;
265
0
    int channels = avctx->ch_layout.nb_channels;
266
0
    int av_ret;
267
0
    int coupled_stream_count, header_size, frame_size;
268
0
    int mapping_family;
269
270
0
    frame_size = opus->opts.frame_duration * 48000 / 1000;
271
0
    switch (frame_size) {
272
0
    case 120:
273
0
    case 240:
274
0
        if (opus->opts.application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
275
0
            av_log(avctx, AV_LOG_WARNING,
276
0
                   "LPC mode cannot be used with a frame duration of less "
277
0
                   "than 10ms. Enabling restricted low-delay mode.\n"
278
0
                   "Use a longer frame duration if this is not what you want.\n");
279
        /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
280
         * RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
281
0
        opus->opts.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
282
0
    case 480:
283
0
    case 960:
284
0
    case 1920:
285
0
    case 2880:
286
0
#ifdef OPUS_FRAMESIZE_120_MS
287
0
    case 3840:
288
0
    case 4800:
289
0
    case 5760:
290
0
#endif
291
0
        opus->opts.packet_size =
292
0
        avctx->frame_size      = frame_size * avctx->sample_rate / 48000;
293
0
        break;
294
0
    default:
295
0
        av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
296
0
               "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40"
297
0
#ifdef OPUS_FRAMESIZE_120_MS
298
0
               ", 60, 80, 100 or 120.\n",
299
#else
300
               " or 60.\n",
301
#endif
302
0
               opus->opts.frame_duration);
303
0
        return AVERROR(EINVAL);
304
0
    }
305
306
0
    if (avctx->compression_level < 0 || avctx->compression_level > 10) {
307
0
        av_log(avctx, AV_LOG_WARNING,
308
0
               "Compression level must be in the range 0 to 10. "
309
0
               "Defaulting to 10.\n");
310
0
        opus->opts.complexity = 10;
311
0
    } else {
312
0
        opus->opts.complexity = avctx->compression_level;
313
0
    }
314
315
0
    if (avctx->cutoff) {
316
0
        switch (avctx->cutoff) {
317
0
        case  4000:
318
0
            opus->opts.max_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
319
0
            break;
320
0
        case  6000:
321
0
            opus->opts.max_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
322
0
            break;
323
0
        case  8000:
324
0
            opus->opts.max_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
325
0
            break;
326
0
        case 12000:
327
0
            opus->opts.max_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
328
0
            break;
329
0
        case 20000:
330
0
            opus->opts.max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
331
0
            break;
332
0
        default:
333
0
            av_log(avctx, AV_LOG_WARNING,
334
0
                   "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
335
0
                   "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
336
0
                   avctx->cutoff);
337
0
            avctx->cutoff = 0;
338
0
        }
339
0
    }
340
341
    /* Channels may need to be reordered to match opus mapping. */
342
0
    av_ret = libopus_validate_layout_and_get_channel_map(avctx, opus->opts.mapping_family,
343
0
                                                         &opus->encoder_channel_map);
344
0
    if (av_ret) {
345
0
        return av_ret;
346
0
    }
347
348
0
    if (opus->opts.mapping_family == -1) {
349
        /* By default, use mapping family 1 for the header but use the older
350
         * libopus multistream API to avoid surround masking. */
351
352
        /* Set the mapping family so that the value is correct in the header */
353
0
        mapping_family = channels > 2 ? 1 : 0;
354
0
        coupled_stream_count = opus_coupled_streams[channels - 1];
355
0
        opus->stream_count   = channels - coupled_stream_count;
356
0
        memcpy(libopus_channel_mapping,
357
0
               opus_vorbis_channel_map[channels - 1],
358
0
               channels * sizeof(*libopus_channel_mapping));
359
360
0
        enc = opus_multistream_encoder_create(
361
0
            avctx->sample_rate, channels, opus->stream_count,
362
0
            coupled_stream_count,
363
0
            libavcodec_libopus_channel_map[channels - 1],
364
0
            opus->opts.application, &ret);
365
0
    } else {
366
        /* Use the newer multistream API. The encoder will set the channel
367
         * mapping and coupled stream counts to its internal defaults and will
368
         * use surround masking analysis to save bits. */
369
0
        mapping_family = opus->opts.mapping_family;
370
0
        enc = opus_multistream_surround_encoder_create(
371
0
            avctx->sample_rate, channels, mapping_family,
372
0
            &opus->stream_count, &coupled_stream_count, libopus_channel_mapping,
373
0
            opus->opts.application, &ret);
374
0
    }
375
376
0
    if (ret != OPUS_OK) {
377
0
        av_log(avctx, AV_LOG_ERROR,
378
0
               "Failed to create encoder: %s\n", opus_strerror(ret));
379
0
        return ff_opus_error_to_averror(ret);
380
0
    }
381
382
0
    if (!avctx->bit_rate) {
383
        /* Sane default copied from opusenc */
384
0
        avctx->bit_rate = 64000 * opus->stream_count +
385
0
                          32000 * coupled_stream_count;
386
0
        av_log(avctx, AV_LOG_WARNING,
387
0
               "No bit rate set. Defaulting to %"PRId64" bps.\n", avctx->bit_rate);
388
0
    }
389
390
0
    if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * channels) {
391
0
        av_log(avctx, AV_LOG_ERROR, "The bit rate %"PRId64" bps is unsupported. "
392
0
               "Please choose a value between 500 and %d.\n", avctx->bit_rate,
393
0
               256000 * channels);
394
0
        ret = AVERROR(EINVAL);
395
0
        goto fail;
396
0
    }
397
398
0
    ret = libopus_configure_encoder(avctx, enc, &opus->opts);
399
0
    if (ret != OPUS_OK) {
400
0
        ret = ff_opus_error_to_averror(ret);
401
0
        goto fail;
402
0
    }
403
404
    /* Header includes channel mapping table if and only if mapping family is NOT 0 */
405
0
    header_size = 19 + (mapping_family == 0 ? 0 : 2 + channels);
406
0
    avctx->extradata = av_malloc(header_size + AV_INPUT_BUFFER_PADDING_SIZE);
407
0
    if (!avctx->extradata) {
408
0
        av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
409
0
        ret = AVERROR(ENOMEM);
410
0
        goto fail;
411
0
    }
412
0
    avctx->extradata_size = header_size;
413
414
0
    opus->samples = av_calloc(frame_size, channels *
415
0
                               av_get_bytes_per_sample(avctx->sample_fmt));
416
0
    if (!opus->samples) {
417
0
        av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
418
0
        ret = AVERROR(ENOMEM);
419
0
        goto fail;
420
0
    }
421
422
0
    ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->initial_padding));
423
0
    if (ret != OPUS_OK)
424
0
        av_log(avctx, AV_LOG_WARNING,
425
0
               "Unable to get number of lookahead samples: %s\n",
426
0
               opus_strerror(ret));
427
428
0
    libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
429
0
                         mapping_family, libopus_channel_mapping);
430
431
0
    ff_af_queue_init(avctx, &opus->afq);
432
433
0
    opus->enc = enc;
434
435
0
    return 0;
436
437
0
fail:
438
0
    opus_multistream_encoder_destroy(enc);
439
0
    return ret;
440
0
}
441
442
static void libopus_copy_samples_with_channel_map(
443
    uint8_t *dst, const uint8_t *src, const uint8_t *channel_map,
444
0
    int nb_channels, int nb_samples, int bytes_per_sample) {
445
0
    int sample, channel;
446
0
    for (sample = 0; sample < nb_samples; ++sample) {
447
0
        for (channel = 0; channel < nb_channels; ++channel) {
448
0
            const size_t src_pos = bytes_per_sample * (nb_channels * sample + channel);
449
0
            const size_t dst_pos = bytes_per_sample * (nb_channels * sample + channel_map[channel]);
450
451
0
            memcpy(&dst[dst_pos], &src[src_pos], bytes_per_sample);
452
0
        }
453
0
    }
454
0
}
455
456
static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
457
                          const AVFrame *frame, int *got_packet_ptr)
458
0
{
459
0
    LibopusEncContext *opus = avctx->priv_data;
460
0
    const int bytes_per_sample = av_get_bytes_per_sample(avctx->sample_fmt);
461
0
    const int channels         = avctx->ch_layout.nb_channels;
462
0
    const int sample_size      = channels * bytes_per_sample;
463
0
    const uint8_t *audio;
464
0
    int ret;
465
0
    int discard_padding;
466
467
0
    if (frame) {
468
0
        ret = ff_af_queue_add(&opus->afq, frame);
469
0
        if (ret < 0)
470
0
            return ret;
471
0
        if (opus->encoder_channel_map != NULL) {
472
0
            audio = opus->samples;
473
0
            libopus_copy_samples_with_channel_map(
474
0
                opus->samples, frame->data[0], opus->encoder_channel_map,
475
0
                channels, frame->nb_samples, bytes_per_sample);
476
0
        } else if (frame->nb_samples < opus->opts.packet_size) {
477
0
            audio = opus->samples;
478
0
            memcpy(opus->samples, frame->data[0], frame->nb_samples * sample_size);
479
0
        } else
480
0
            audio = frame->data[0];
481
0
    } else {
482
0
        if (!opus->afq.remaining_samples || (!opus->afq.frame_alloc && !opus->afq.frame_count))
483
0
            return 0;
484
0
        audio = opus->samples;
485
0
        memset(opus->samples, 0, opus->opts.packet_size * sample_size);
486
0
    }
487
488
    /* Maximum packet size taken from opusenc in opus-tools. 120ms packets
489
     * consist of 6 frames in one packet. The maximum frame size is 1275
490
     * bytes along with the largest possible packet header of 7 bytes. */
491
0
    if ((ret = ff_alloc_packet(avctx, avpkt, (1275 * 6 + 7) * opus->stream_count)) < 0)
492
0
        return ret;
493
494
0
    if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
495
0
        ret = opus_multistream_encode_float(opus->enc, (const float *)audio,
496
0
                                            opus->opts.packet_size,
497
0
                                            avpkt->data, avpkt->size);
498
0
    else
499
0
        ret = opus_multistream_encode(opus->enc, (const opus_int16 *)audio,
500
0
                                      opus->opts.packet_size,
501
0
                                      avpkt->data, avpkt->size);
502
503
0
    if (ret < 0) {
504
0
        av_log(avctx, AV_LOG_ERROR,
505
0
               "Error encoding frame: %s\n", opus_strerror(ret));
506
0
        return ff_opus_error_to_averror(ret);
507
0
    }
508
509
0
    av_shrink_packet(avpkt, ret);
510
511
0
    ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
512
0
                       &avpkt->pts, &avpkt->duration);
513
514
0
    discard_padding = opus->opts.packet_size - avpkt->duration;
515
    // Check if subtraction resulted in an overflow
516
0
    if ((discard_padding < opus->opts.packet_size) != (avpkt->duration > 0))
517
0
        return AVERROR(EINVAL);
518
0
    if (discard_padding > 0) {
519
0
        uint8_t* side_data = av_packet_new_side_data(avpkt,
520
0
                                                     AV_PKT_DATA_SKIP_SAMPLES,
521
0
                                                     10);
522
0
        if (!side_data)
523
0
            return AVERROR(ENOMEM);
524
0
        AV_WL32(side_data + 4, discard_padding);
525
0
    }
526
527
0
    *got_packet_ptr = 1;
528
529
0
    return 0;
530
0
}
531
532
static av_cold int libopus_encode_close(AVCodecContext *avctx)
533
0
{
534
0
    LibopusEncContext *opus = avctx->priv_data;
535
536
0
    opus_multistream_encoder_destroy(opus->enc);
537
538
0
    ff_af_queue_close(&opus->afq);
539
540
0
    av_freep(&opus->samples);
541
542
0
    return 0;
543
0
}
544
545
#define OFFSET(x) offsetof(LibopusEncContext, opts.x)
546
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
547
static const AVOption libopus_options[] = {
548
    { "application",    "Intended application type",           OFFSET(application),    AV_OPT_TYPE_INT,   { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, .unit = "application" },
549
        { "voip",           "Favor improved speech intelligibility",   0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP },                0, 0, FLAGS, .unit = "application" },
550
        { "audio",          "Favor faithfulness to the input",         0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO },               0, 0, FLAGS, .unit = "application" },
551
        { "lowdelay",       "Restrict to only the lowest delay modes, disable voice-optimized modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, .unit = "application" },
552
    { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 20.0 }, 2.5, 120.0, FLAGS },
553
    { "packet_loss",    "Expected packet loss percentage",     OFFSET(packet_loss),    AV_OPT_TYPE_INT,   { .i64 = 0 },    0,   100,  FLAGS },
554
    { "fec",             "Enable inband FEC. Expected packet loss must be non-zero",     OFFSET(fec),    AV_OPT_TYPE_BOOL,   { .i64 = 0 }, 0, 1, FLAGS },
555
    { "vbr",            "Variable bit rate mode",              OFFSET(vbr),            AV_OPT_TYPE_INT,   { .i64 = 1 },    0,   2,    FLAGS, .unit = "vbr" },
556
        { "off",            "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, .unit = "vbr" },
557
        { "on",             "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, .unit = "vbr" },
558
        { "constrained",    "Use constrained VBR",   0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, .unit = "vbr" },
559
    { "mapping_family", "Channel Mapping Family",              OFFSET(mapping_family), AV_OPT_TYPE_INT,   { .i64 = -1 },   -1,  255,  FLAGS, .unit = "mapping_family" },
560
#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
561
    { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
562
#endif
563
    { NULL },
564
};
565
566
static const AVClass libopus_class = {
567
    .class_name = "libopus",
568
    .item_name  = av_default_item_name,
569
    .option     = libopus_options,
570
    .version    = LIBAVUTIL_VERSION_INT,
571
};
572
573
static const FFCodecDefault libopus_defaults[] = {
574
    { "b",                 "0" },
575
    { "compression_level", "10" },
576
    { NULL },
577
};
578
579
static const int libopus_sample_rates[] = {
580
    48000, 24000, 16000, 12000, 8000, 0,
581
};
582
583
const FFCodec ff_libopus_encoder = {
584
    .p.name          = "libopus",
585
    CODEC_LONG_NAME("libopus Opus"),
586
    .p.type          = AVMEDIA_TYPE_AUDIO,
587
    .p.id            = AV_CODEC_ID_OPUS,
588
    .p.capabilities  = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
589
                       AV_CODEC_CAP_SMALL_LAST_FRAME,
590
    .caps_internal   = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
591
    .priv_data_size  = sizeof(LibopusEncContext),
592
    .init            = libopus_encode_init,
593
    FF_CODEC_ENCODE_CB(libopus_encode),
594
    .close           = libopus_encode_close,
595
    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT),
596
    CODEC_SAMPLERATES_ARRAY(libopus_sample_rates),
597
    .p.priv_class    = &libopus_class,
598
    .defaults        = libopus_defaults,
599
    .p.wrapper_name  = "libopus",
600
};