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