Coverage Report

Created: 2026-10-28 08:13

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