FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
28 #include "avcodec.h"
29 #include "internal.h"
30 #include "vorbis.h"
31 #include "mathops.h"
32 #include "libopus.h"
33 
35  OpusMSDecoder *dec;
36  int pre_skip;
37 #ifndef OPUS_SET_GAIN
38  union { int i; double d; } gain;
39 #endif
40 };
41 
42 #define OPUS_HEAD_SIZE 19
43 
45 {
46  struct libopus_context *opus = avc->priv_data;
47  int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
48  uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
49 
50  avc->sample_rate = 48000;
53  avc->channel_layout = avc->channels > 8 ? 0 :
55 
56  if (avc->extradata_size >= OPUS_HEAD_SIZE) {
57  opus->pre_skip = AV_RL16(avc->extradata + 10);
58  gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
59  channel_map = AV_RL8 (avc->extradata + 18);
60  }
61  if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
63  nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
64  if (nb_streams + nb_coupled != avc->channels)
65  av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
66  mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
67  } else {
68  if (avc->channels > 2 || channel_map) {
69  av_log(avc, AV_LOG_ERROR,
70  "No channel mapping for %d channels.\n", avc->channels);
71  return AVERROR(EINVAL);
72  }
73  nb_streams = 1;
74  nb_coupled = avc->channels > 1;
75  mapping = mapping_arr;
76  }
77 
78  if (avc->channels > 2 && avc->channels <= 8) {
79  const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
80  int ch;
81 
82  /* Remap channels from Vorbis order to ffmpeg order */
83  for (ch = 0; ch < avc->channels; ch++)
84  mapping_arr[ch] = mapping[vorbis_offset[ch]];
85  mapping = mapping_arr;
86  }
87 
88  opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
89  nb_streams, nb_coupled,
90  mapping, &ret);
91  if (!opus->dec) {
92  av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
93  opus_strerror(ret));
94  return ff_opus_error_to_averror(ret);
95  }
96 
97 #ifdef OPUS_SET_GAIN
98  ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
99  if (ret != OPUS_OK)
100  av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
101  opus_strerror(ret));
102 #else
103  {
104  double gain_lin = ff_exp10(gain_db / (20.0 * 256));
105  if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
106  opus->gain.d = gain_lin;
107  else
108  opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
109  }
110 #endif
111 
112  /* Decoder delay (in samples) at 48kHz */
113  avc->delay = avc->internal->skip_samples = opus->pre_skip;
114 
115  return 0;
116 }
117 
119 {
120  struct libopus_context *opus = avc->priv_data;
121 
122  opus_multistream_decoder_destroy(opus->dec);
123  return 0;
124 }
125 
126 #define MAX_FRAME_SIZE (960 * 6)
127 
128 static int libopus_decode(AVCodecContext *avc, void *data,
129  int *got_frame_ptr, AVPacket *pkt)
130 {
131  struct libopus_context *opus = avc->priv_data;
132  AVFrame *frame = data;
133  int ret, nb_samples;
134 
135  frame->nb_samples = MAX_FRAME_SIZE;
136  if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
137  return ret;
138 
139  if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
140  nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
141  (opus_int16 *)frame->data[0],
142  frame->nb_samples, 0);
143  else
144  nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
145  (float *)frame->data[0],
146  frame->nb_samples, 0);
147 
148  if (nb_samples < 0) {
149  av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
150  opus_strerror(nb_samples));
151  return ff_opus_error_to_averror(nb_samples);
152  }
153 
154 #ifndef OPUS_SET_GAIN
155  {
156  int i = avc->channels * nb_samples;
157  if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
158  float *pcm = (float *)frame->data[0];
159  for (; i > 0; i--, pcm++)
160  *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
161  } else {
162  int16_t *pcm = (int16_t *)frame->data[0];
163  for (; i > 0; i--, pcm++)
164  *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
165  }
166  }
167 #endif
168 
169  frame->nb_samples = nb_samples;
170  *got_frame_ptr = 1;
171 
172  return pkt->size;
173 }
174 
175 static void libopus_flush(AVCodecContext *avc)
176 {
177  struct libopus_context *opus = avc->priv_data;
178 
179  opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
180  /* The stream can have been extracted by a tool that is not Opus-aware.
181  Therefore, any packet can become the first of the stream. */
182  avc->internal->skip_samples = opus->pre_skip;
183 }
184 
186  .name = "libopus",
187  .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
188  .type = AVMEDIA_TYPE_AUDIO,
189  .id = AV_CODEC_ID_OPUS,
190  .priv_data_size = sizeof(struct libopus_context),
191  .init = libopus_decode_init,
192  .close = libopus_decode_close,
193  .decode = libopus_decode,
194  .flush = libopus_flush,
195  .capabilities = AV_CODEC_CAP_DR1,
196  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
199 };
static int libopus_decode(AVCodecContext *avc, void *data, int *got_frame_ptr, AVPacket *pkt)
Definition: libopusdec.c:128
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int size
Definition: avcodec.h:1589
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3559
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
AVCodec ff_libopus_decoder
Definition: libopusdec.c:185
int ff_opus_error_to_averror(int err)
Definition: libopus.c:28
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2426
uint8_t
static int nb_streams
Definition: ffprobe.c:254
#define av_cold
Definition: attributes.h:82
#define MAX_FRAME_SIZE
Definition: libopusdec.c:126
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1772
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1588
#define av_log(a,...)
#define AV_RL8(x)
Definition: intreadwrite.h:398
OpusMSDecoder * dec
Definition: libopusdec.c:35
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: ffmath.h:42
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
enum AVSampleFormat request_sample_fmt
desired sample format
Definition: avcodec.h:2491
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * name
Name of the codec implementation.
Definition: avcodec.h:3566
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2469
common internal API header
#define FFMIN(a, b)
Definition: common.h:96
static av_cold int libopus_decode_init(AVCodecContext *avc)
Definition: libopusdec.c:44
union libopus_context::@70 gain
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int sample_rate
samples per second
Definition: avcodec.h:2418
main external API structure.
Definition: avcodec.h:1657
const uint64_t ff_vorbis_channel_layouts[9]
Definition: vorbis_data.c:47
static void libopus_flush(AVCodecContext *avc)
Definition: libopusdec.c:175
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:928
int extradata_size
Definition: avcodec.h:1773
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition: internal.h:157
#define OPUS_HEAD_SIZE
Definition: libopusdec.c:42
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:139
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
common internal api header.
signed 16 bits
Definition: samplefmt.h:61
void * priv_data
Definition: avcodec.h:1699
int channels
number of audio channels
Definition: avcodec.h:2419
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1707
static av_cold int libopus_decode_close(AVCodecContext *avc)
Definition: libopusdec.c:118
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition: vorbis_data.c:25
This structure stores compressed data.
Definition: avcodec.h:1565
int delay
Codec delay.
Definition: avcodec.h:1827
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:956
for(j=16;j >0;--j)