FFmpeg  2.8.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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/avassert.h"
26 #include "libavutil/intreadwrite.h"
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "vorbis.h"
30 #include "mathops.h"
31 #include "libopus.h"
32 
34  OpusMSDecoder *dec;
35  int pre_skip;
36 #ifndef OPUS_SET_GAIN
37  union { int i; double d; } gain;
38 #endif
39 };
40 
41 #define OPUS_HEAD_SIZE 19
42 
44 {
45  struct libopus_context *opus = avc->priv_data;
46  int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
47  uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
48 
49  avc->channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->channels == 1) ? 1 : 2;
50  if (avc->channels <= 0) {
52  "Invalid number of channels %d, defaulting to stereo\n", avc->channels);
53  avc->channels = 2;
54  }
55 
56  avc->sample_rate = 48000;
59  avc->channel_layout = avc->channels > 8 ? 0 :
61 
62  if (avc->extradata_size >= OPUS_HEAD_SIZE) {
63  opus->pre_skip = AV_RL16(avc->extradata + 10);
64  gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
65  channel_map = AV_RL8 (avc->extradata + 18);
66  }
67  if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
69  nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
70  if (nb_streams + nb_coupled != avc->channels)
71  av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
72  mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
73  } else {
74  if (avc->channels > 2 || channel_map) {
75  av_log(avc, AV_LOG_ERROR,
76  "No channel mapping for %d channels.\n", avc->channels);
77  return AVERROR(EINVAL);
78  }
79  nb_streams = 1;
80  nb_coupled = avc->channels > 1;
81  mapping = mapping_arr;
82  }
83 
84  if (avc->channels > 2 && avc->channels <= 8) {
85  const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
86  int ch;
87 
88  /* Remap channels from vorbis order to ffmpeg order */
89  for (ch = 0; ch < avc->channels; ch++)
90  mapping_arr[ch] = mapping[vorbis_offset[ch]];
91  mapping = mapping_arr;
92  }
93 
94  opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
95  nb_streams, nb_coupled,
96  mapping, &ret);
97  if (!opus->dec) {
98  av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
99  opus_strerror(ret));
100  return ff_opus_error_to_averror(ret);
101  }
102 
103 #ifdef OPUS_SET_GAIN
104  ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
105  if (ret != OPUS_OK)
106  av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
107  opus_strerror(ret));
108 #else
109  {
110  double gain_lin = pow(10, gain_db / (20.0 * 256));
111  if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
112  opus->gain.d = gain_lin;
113  else
114  opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
115  }
116 #endif
117 
118  /* Decoder delay (in samples) at 48kHz */
119  avc->delay = avc->internal->skip_samples = opus->pre_skip;
120 
121  return 0;
122 }
123 
125 {
126  struct libopus_context *opus = avc->priv_data;
127 
128  if (opus->dec) {
129  opus_multistream_decoder_destroy(opus->dec);
130  opus->dec = NULL;
131  }
132  return 0;
133 }
134 
135 #define MAX_FRAME_SIZE (960 * 6)
136 
137 static int libopus_decode(AVCodecContext *avc, void *data,
138  int *got_frame_ptr, AVPacket *pkt)
139 {
140  struct libopus_context *opus = avc->priv_data;
141  AVFrame *frame = data;
142  int ret, nb_samples;
143 
144  frame->nb_samples = MAX_FRAME_SIZE;
145  if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
146  return ret;
147 
148  if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
149  nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
150  (opus_int16 *)frame->data[0],
151  frame->nb_samples, 0);
152  else
153  nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
154  (float *)frame->data[0],
155  frame->nb_samples, 0);
156 
157  if (nb_samples < 0) {
158  av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
159  opus_strerror(nb_samples));
160  return ff_opus_error_to_averror(nb_samples);
161  }
162 
163 #ifndef OPUS_SET_GAIN
164  {
165  int i = avc->channels * nb_samples;
166  if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
167  float *pcm = (float *)frame->data[0];
168  for (; i > 0; i--, pcm++)
169  *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
170  } else {
171  int16_t *pcm = (int16_t *)frame->data[0];
172  for (; i > 0; i--, pcm++)
173  *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
174  }
175  }
176 #endif
177 
178  frame->nb_samples = nb_samples;
179  *got_frame_ptr = 1;
180 
181  return pkt->size;
182 }
183 
184 static void libopus_flush(AVCodecContext *avc)
185 {
186  struct libopus_context *opus = avc->priv_data;
187 
188  opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
189  /* The stream can have been extracted by a tool that is not Opus-aware.
190  Therefore, any packet can become the first of the stream. */
191  avc->internal->skip_samples = opus->pre_skip;
192 }
193 
195  .name = "libopus",
196  .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
197  .type = AVMEDIA_TYPE_AUDIO,
198  .id = AV_CODEC_ID_OPUS,
199  .priv_data_size = sizeof(struct libopus_context),
200  .init = libopus_decode_init,
201  .close = libopus_decode_close,
202  .decode = libopus_decode,
203  .flush = libopus_flush,
204  .capabilities = AV_CODEC_CAP_DR1,
205  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
206  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
209 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
static int libopus_decode(AVCodecContext *avc, void *data, int *got_frame_ptr, AVPacket *pkt)
Definition: libopusdec.c:137
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
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:1434
#define AV_RL16
Definition: intreadwrite.h:42
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3482
AVCodec ff_libopus_decoder
Definition: libopusdec.c:194
int ff_opus_error_to_averror(int err)
Definition: libopus.c:28
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2280
uint8_t
static int nb_streams
Definition: ffprobe.c:226
#define av_cold
Definition: attributes.h:74
#define MAX_FRAME_SIZE
Definition: libopusdec.c:135
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1627
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1433
#define av_log(a,...)
#define AV_RL8(x)
Definition: intreadwrite.h:398
OpusMSDecoder * dec
Definition: libopusdec.c:34
#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:2355
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3489
Libavcodec external API header.
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2333
#define FFMIN(a, b)
Definition: common.h:92
static av_cold int libopus_decode_init(AVCodecContext *avc)
Definition: libopusdec.c:43
union libopus_context::@65 gain
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
int sample_rate
samples per second
Definition: avcodec.h:2272
main external API structure.
Definition: avcodec.h:1512
const uint64_t ff_vorbis_channel_layouts[9]
Definition: vorbis_data.c:47
static void libopus_flush(AVCodecContext *avc)
Definition: libopusdec.c:184
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1048
int extradata_size
Definition: avcodec.h:1628
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition: internal.h:154
#define OPUS_HEAD_SIZE
Definition: libopusdec.c:41
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:138
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
common internal api header.
signed 16 bits
Definition: samplefmt.h:62
void * priv_data
Definition: avcodec.h:1554
int channels
number of audio channels
Definition: avcodec.h:2273
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1562
static av_cold int libopus_decode_close(AVCodecContext *avc)
Definition: libopusdec.c:124
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition: vorbis_data.c:25
This structure stores compressed data.
Definition: avcodec.h:1410
int delay
Codec delay.
Definition: avcodec.h:1674
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:857
for(j=16;j >0;--j)