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 "decode.h"
33 #include "internal.h"
34 #include "mathops.h"
35 #include "libopus.h"
36 #include "vorbis_data.h"
37 
39  AVClass *class;
40  OpusMSDecoder *dec;
41  int pre_skip;
42 #ifndef OPUS_SET_GAIN
43  union { int i; double d; } gain;
44 #endif
45 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
46  int apply_phase_inv;
47 #endif
48 };
49 
50 #define OPUS_HEAD_SIZE 19
51 
53 {
54  struct libopus_context *opus = avc->priv_data;
55  int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled, channels;
56  uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
57 
58  channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->ch_layout.nb_channels == 1) ? 1 : 2;
59  if (channels <= 0) {
61  "Invalid number of channels %d, defaulting to stereo\n", channels);
62  channels = 2;
63  }
64 
65  avc->sample_rate = 48000;
69  if (channels > 8) {
72  } else {
74  }
75 
76  if (avc->extradata_size >= OPUS_HEAD_SIZE) {
77  opus->pre_skip = AV_RL16(avc->extradata + 10);
78  gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
79  channel_map = AV_RL8 (avc->extradata + 18);
80  }
81  if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + channels) {
83  nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
84  if (nb_streams + nb_coupled != channels)
85  av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
86  mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
87  } else {
88  if (channels > 2 || channel_map) {
89  av_log(avc, AV_LOG_ERROR,
90  "No channel mapping for %d channels.\n", channels);
91  return AVERROR(EINVAL);
92  }
93  nb_streams = 1;
94  nb_coupled = channels > 1;
95  mapping = mapping_arr;
96  }
97 
98  if (channels > 2 && channels <= 8) {
99  const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[channels - 1];
100  int ch;
101 
102  /* Remap channels from Vorbis order to ffmpeg order */
103  for (ch = 0; ch < channels; ch++)
104  mapping_arr[ch] = mapping[vorbis_offset[ch]];
105  mapping = mapping_arr;
106  }
107 
108  opus->dec = opus_multistream_decoder_create(avc->sample_rate, channels,
109  nb_streams, nb_coupled,
110  mapping, &ret);
111  if (!opus->dec) {
112  av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
113  opus_strerror(ret));
115  }
116 
117 #ifdef OPUS_SET_GAIN
118  ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
119  if (ret != OPUS_OK)
120  av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
121  opus_strerror(ret));
122 #else
123  {
124  double gain_lin = ff_exp10(gain_db / (20.0 * 256));
125  if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
126  opus->gain.d = gain_lin;
127  else
128  opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
129  }
130 #endif
131 
132 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
133  ret = opus_multistream_decoder_ctl(opus->dec,
134  OPUS_SET_PHASE_INVERSION_DISABLED(!opus->apply_phase_inv));
135  if (ret != OPUS_OK)
136  av_log(avc, AV_LOG_WARNING,
137  "Unable to set phase inversion: %s\n",
138  opus_strerror(ret));
139 #endif
140 
141  /* Decoder delay (in samples) at 48kHz */
142  avc->delay = avc->internal->skip_samples = opus->pre_skip;
143 
144  return 0;
145 }
146 
148 {
149  struct libopus_context *opus = avc->priv_data;
150 
151  if (opus->dec) {
152  opus_multistream_decoder_destroy(opus->dec);
153  opus->dec = NULL;
154  }
155  return 0;
156 }
157 
158 #define MAX_FRAME_SIZE (960 * 6)
159 
161  int *got_frame_ptr, AVPacket *pkt)
162 {
163  struct libopus_context *opus = avc->priv_data;
164  int ret, nb_samples;
165 
166  frame->nb_samples = MAX_FRAME_SIZE;
167  if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
168  return ret;
169 
170  if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
171  nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
172  (opus_int16 *)frame->data[0],
173  frame->nb_samples, 0);
174  else
175  nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
176  (float *)frame->data[0],
177  frame->nb_samples, 0);
178 
179  if (nb_samples < 0) {
180  av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
181  opus_strerror(nb_samples));
182  return ff_opus_error_to_averror(nb_samples);
183  }
184 
185 #ifndef OPUS_SET_GAIN
186  {
187  int i = avc->ch_layout.nb_channels * nb_samples;
188  if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
189  float *pcm = (float *)frame->data[0];
190  for (; i > 0; i--, pcm++)
191  *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
192  } else {
193  int16_t *pcm = (int16_t *)frame->data[0];
194  for (; i > 0; i--, pcm++)
195  *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
196  }
197  }
198 #endif
199 
200  frame->nb_samples = nb_samples;
201  *got_frame_ptr = 1;
202 
203  return pkt->size;
204 }
205 
206 static void libopus_flush(AVCodecContext *avc)
207 {
208  struct libopus_context *opus = avc->priv_data;
209 
210  opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
211  /* The stream can have been extracted by a tool that is not Opus-aware.
212  Therefore, any packet can become the first of the stream. */
213  avc->internal->skip_samples = opus->pre_skip;
214 }
215 
216 
217 #define OFFSET(x) offsetof(struct libopus_context, x)
218 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
219 static const AVOption libopusdec_options[] = {
220 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
221  { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
222 #endif
223  { NULL },
224 };
225 
226 static const AVClass libopusdec_class = {
227  .class_name = "libopusdec",
228  .item_name = av_default_item_name,
229  .option = libopusdec_options,
230  .version = LIBAVUTIL_VERSION_INT,
231 };
232 
233 
235  .p.name = "libopus",
236  CODEC_LONG_NAME("libopus Opus"),
237  .p.type = AVMEDIA_TYPE_AUDIO,
238  .p.id = AV_CODEC_ID_OPUS,
239  .priv_data_size = sizeof(struct libopus_context),
241  .close = libopus_decode_close,
243  .flush = libopus_flush,
244  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
245  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
247  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
250  .p.priv_class = &libopusdec_class,
251  .p.wrapper_name = "libopus",
252 };
libopus_context::d
double d
Definition: libopusdec.c:43
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
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:43
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
libopus_context::gain
union libopus_context::@157 gain
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1064
libopus_flush
static void libopus_flush(AVCodecContext *avc)
Definition: libopusdec.c:206
AVCodecInternal::skip_samples
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition: internal.h:125
int64_t
long long int64_t
Definition: coverity.c:34
libopusdec_options
static const AVOption libopusdec_options[]
Definition: libopusdec.c:219
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:403
libopusdec_class
static const AVClass libopusdec_class
Definition: libopusdec.c:226
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVOption
AVOption.
Definition: opt.h:429
opus.h
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:35
FFCodec
Definition: codec_internal.h:127
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:323
libopus_decode_init
static av_cold int libopus_decode_init(AVCodecContext *avc)
Definition: libopusdec.c:52
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:328
AV_RL8
#define AV_RL8(x)
Definition: intreadwrite.h:394
libopus_context
Definition: libopusdec.c:38
vorbis_data.h
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:615
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1079
libopus_context::i
int i
Definition: libopusdec.c:43
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
libopus_decode_close
static av_cold int libopus_decode_close(AVCodecContext *avc)
Definition: libopusdec.c:147
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:538
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
intreadwrite.h
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
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:119
channels
channels
Definition: aptx.h:31
decode.h
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:385
channel_map
static const uint8_t channel_map[8][8]
Definition: atrac3plusdec.c:52
ff_opus_error_to_averror
int ff_opus_error_to_averror(int err)
Definition: libopus.c:27
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
FLAGS
#define FLAGS
Definition: libopusdec.c:218
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
av_clip_int16
#define av_clip_int16
Definition: common.h:115
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:486
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:50
mathops.h
av_clipf
av_clipf
Definition: af_crystalizer.c:122
libopus_context::dec
OpusMSDecoder * dec
Definition: libopusdec.c:40
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:106
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1697
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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:540
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:1071
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:507
OFFSET
#define OFFSET(x)
Definition: libopusdec.c:217
AVCodecContext::request_sample_fmt
enum AVSampleFormat request_sample_fmt
desired sample format
Definition: avcodec.h:1119
ff_vorbis_channel_layout_offsets
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition: vorbis_data.c:26
MAX_FRAME_SIZE
#define MAX_FRAME_SIZE
Definition: libopusdec.c:158
ff_libopus_decoder
const FFCodec ff_libopus_decoder
Definition: libopusdec.c:234
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:537
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:194
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:80
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:451
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:441
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:131
ffmath.h
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:448
ff_vorbis_ch_layouts
const AVChannelLayout ff_vorbis_ch_layouts[9]
Definition: vorbis_data.c:37
libopus_decode
static int libopus_decode(AVCodecContext *avc, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition: libopusdec.c:160
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
libopus_context::pre_skip
int pre_skip
Definition: libopusdec.c:41
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60