FFmpeg
libopusdec.c
Go to the documentation of this file.
1 /*
2  * Opus decoder using libopus
3  * Copyright (c) 2012 Nicolas George
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/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/ffmath.h"
28 #include "libavutil/opt.h"
29 
30 #include "avcodec.h"
31 #include "codec_internal.h"
32 #include "internal.h"
33 #include "vorbis.h"
34 #include "mathops.h"
35 #include "libopus.h"
36 
38  AVClass *class;
39  OpusMSDecoder *dec;
40  int pre_skip;
41 #ifndef OPUS_SET_GAIN
42  union { int i; double d; } gain;
43 #endif
44 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
45  int apply_phase_inv;
46 #endif
47 };
48 
49 #define OPUS_HEAD_SIZE 19
50 
52 {
53  struct libopus_context *opus = avc->priv_data;
54  int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled, channels;
55  uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
56 
57  channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->ch_layout.nb_channels == 1) ? 1 : 2;
58  if (channels <= 0) {
60  "Invalid number of channels %d, defaulting to stereo\n", channels);
61  channels = 2;
62  }
63 
64  avc->sample_rate = 48000;
68  if (channels > 8) {
71  } else {
73  }
74 
75  if (avc->extradata_size >= OPUS_HEAD_SIZE) {
76  opus->pre_skip = AV_RL16(avc->extradata + 10);
77  gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
78  channel_map = AV_RL8 (avc->extradata + 18);
79  }
80  if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + channels) {
82  nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
83  if (nb_streams + nb_coupled != channels)
84  av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
85  mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
86  } else {
87  if (channels > 2 || channel_map) {
88  av_log(avc, AV_LOG_ERROR,
89  "No channel mapping for %d channels.\n", channels);
90  return AVERROR(EINVAL);
91  }
92  nb_streams = 1;
93  nb_coupled = channels > 1;
94  mapping = mapping_arr;
95  }
96 
97  if (channels > 2 && channels <= 8) {
98  const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[channels - 1];
99  int ch;
100 
101  /* Remap channels from Vorbis order to ffmpeg order */
102  for (ch = 0; ch < channels; ch++)
103  mapping_arr[ch] = mapping[vorbis_offset[ch]];
104  mapping = mapping_arr;
105  }
106 
107  opus->dec = opus_multistream_decoder_create(avc->sample_rate, channels,
108  nb_streams, nb_coupled,
109  mapping, &ret);
110  if (!opus->dec) {
111  av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
112  opus_strerror(ret));
114  }
115 
116 #ifdef OPUS_SET_GAIN
117  ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
118  if (ret != OPUS_OK)
119  av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
120  opus_strerror(ret));
121 #else
122  {
123  double gain_lin = ff_exp10(gain_db / (20.0 * 256));
124  if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
125  opus->gain.d = gain_lin;
126  else
127  opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
128  }
129 #endif
130 
131 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
132  ret = opus_multistream_decoder_ctl(opus->dec,
133  OPUS_SET_PHASE_INVERSION_DISABLED(!opus->apply_phase_inv));
134  if (ret != OPUS_OK)
135  av_log(avc, AV_LOG_WARNING,
136  "Unable to set phase inversion: %s\n",
137  opus_strerror(ret));
138 #endif
139 
140  /* Decoder delay (in samples) at 48kHz */
141  avc->delay = avc->internal->skip_samples = opus->pre_skip;
142 
143  return 0;
144 }
145 
147 {
148  struct libopus_context *opus = avc->priv_data;
149 
150  if (opus->dec) {
151  opus_multistream_decoder_destroy(opus->dec);
152  opus->dec = NULL;
153  }
154  return 0;
155 }
156 
157 #define MAX_FRAME_SIZE (960 * 6)
158 
160  int *got_frame_ptr, AVPacket *pkt)
161 {
162  struct libopus_context *opus = avc->priv_data;
163  int ret, nb_samples;
164 
165  frame->nb_samples = MAX_FRAME_SIZE;
166  if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
167  return ret;
168 
169  if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
170  nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
171  (opus_int16 *)frame->data[0],
172  frame->nb_samples, 0);
173  else
174  nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
175  (float *)frame->data[0],
176  frame->nb_samples, 0);
177 
178  if (nb_samples < 0) {
179  av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
180  opus_strerror(nb_samples));
181  return ff_opus_error_to_averror(nb_samples);
182  }
183 
184 #ifndef OPUS_SET_GAIN
185  {
186  int i = avc->ch_layout.nb_channels * nb_samples;
187  if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
188  float *pcm = (float *)frame->data[0];
189  for (; i > 0; i--, pcm++)
190  *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
191  } else {
192  int16_t *pcm = (int16_t *)frame->data[0];
193  for (; i > 0; i--, pcm++)
194  *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
195  }
196  }
197 #endif
198 
199  frame->nb_samples = nb_samples;
200  *got_frame_ptr = 1;
201 
202  return pkt->size;
203 }
204 
205 static void libopus_flush(AVCodecContext *avc)
206 {
207  struct libopus_context *opus = avc->priv_data;
208 
209  opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
210  /* The stream can have been extracted by a tool that is not Opus-aware.
211  Therefore, any packet can become the first of the stream. */
212  avc->internal->skip_samples = opus->pre_skip;
213 }
214 
215 
216 #define OFFSET(x) offsetof(struct libopus_context, x)
217 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
218 static const AVOption libopusdec_options[] = {
219 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
220  { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
221 #endif
222  { NULL },
223 };
224 
225 static const AVClass libopusdec_class = {
226  .class_name = "libopusdec",
227  .item_name = av_default_item_name,
228  .option = libopusdec_options,
229  .version = LIBAVUTIL_VERSION_INT,
230 };
231 
232 
234  .p.name = "libopus",
235  .p.long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
236  .p.type = AVMEDIA_TYPE_AUDIO,
237  .p.id = AV_CODEC_ID_OPUS,
238  .priv_data_size = sizeof(struct libopus_context),
240  .close = libopus_decode_close,
242  .flush = libopus_flush,
243  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
244  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
245  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
248  .p.priv_class = &libopusdec_class,
249  .p.wrapper_name = "libopus",
250 };
libopus_context::d
double d
Definition: libopusdec.c:42
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
libopus.h
ff_exp10
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: ffmath.h:42
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:39
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:998
libopus_flush
static void libopus_flush(AVCodecContext *avc)
Definition: libopusdec.c:205
AVCodecInternal::skip_samples
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition: internal.h:115
libopusdec_options
static const AVOption libopusdec_options[]
Definition: libopusdec.c:218
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
libopusdec_class
static const AVClass libopusdec_class
Definition: libopusdec.c:225
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
opus.h
FFCodec
Definition: codec_internal.h:112
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:295
libopus_decode_init
static av_cold int libopus_decode_init(AVCodecContext *avc)
Definition: libopusdec.c:51
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
AV_RL8
#define AV_RL8(x)
Definition: intreadwrite.h:398
libopus_context
Definition: libopusdec.c:37
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:545
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:637
init
static int init
Definition: av_tx.c:47
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2056
libopus_context::gain
union libopus_context::@85 gain
libopus_context::i
int i
Definition: libopusdec.c:42
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
libopus_decode_close
static av_cold int libopus_decode_close(AVCodecContext *avc)
Definition: libopusdec.c:146
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:491
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
intreadwrite.h
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:106
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
channels
channels
Definition: aptx.h:32
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
nb_streams
static int nb_streams
Definition: ffprobe.c:307
ff_opus_error_to_averror
int ff_opus_error_to_averror(int err)
Definition: libopus.c:27
FLAGS
#define FLAGS
Definition: libopusdec.c:217
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
av_clip_int16
#define av_clip_int16
Definition: common.h:110
NULL
#define NULL
Definition: coverity.c:32
ff_vorbis_ch_layouts
const AVChannelLayout ff_vorbis_ch_layouts[9]
Definition: vorbis_data.c:51
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:424
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
OPUS_HEAD_SIZE
#define OPUS_HEAD_SIZE
Definition: libopusdec.c:49
mathops.h
ff_vorbis_channel_layout_offsets
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition: vorbis_data.c:26
av_clipf
av_clipf
Definition: af_crystalizer.c:122
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:630
libopus_context::dec
OpusMSDecoder * dec
Definition: libopusdec.c:39
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:109
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1403
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
codec_internal.h
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1014
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:487
OFFSET
#define OFFSET(x)
Definition: libopusdec.c:216
AVCodecContext::request_sample_fmt
enum AVSampleFormat request_sample_fmt
desired sample format
Definition: avcodec.h:1085
MAX_FRAME_SIZE
#define MAX_FRAME_SIZE
Definition: libopusdec.c:157
ff_libopus_decoder
const FFCodec ff_libopus_decoder
Definition: libopusdec.c:233
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
vorbis.h
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:490
internal.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:389
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:132
ffmath.h
libopus_decode
static int libopus_decode(AVCodecContext *avc, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition: libopusdec.c:159
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
libopus_context::pre_skip
int pre_skip
Definition: libopusdec.c:40
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60