FFmpeg
Loading...
Searching...
No Matches
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"
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;
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) {
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));
114 return ff_opus_error_to_averror(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)
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 = 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
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
219static 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
226static 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,
245 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
248 .p.priv_class = &libopusdec_class,
249 .p.wrapper_name = "libopus",
250};
#define MAX_FRAME_SIZE
Definition 8svx.c:64
const FFCodec ff_libopus_decoder
Definition libopusdec.c:234
channels
Definition aptx.h:31
static const uint8_t channel_map[8][8]
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FLAGS
Definition cmdutils.c:598
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
#define av_clip_int16
Definition common.h:115
#define av_clipf
Definition common.h:145
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVPacket * pkt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
internal math functions header
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition ffmath.h:42
static unsigned int nb_streams
Definition ffprobe.c:352
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_OPUS
Definition codec_id.h:513
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define AV_RL8(x)
#define AV_RL16(p)
common internal api header.
#define av_cold
Definition attributes.h:117
common internal API header
int ff_opus_error_to_averror(int err)
Definition libopus.c:27
static av_cold int libopus_decode_init(AVCodecContext *avc)
Definition libopusdec.c:52
static const AVClass libopusdec_class
Definition libopusdec.c:226
static int libopus_decode(AVCodecContext *avc, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition libopusdec.c:160
static av_cold int libopus_decode_close(AVCodecContext *avc)
Definition libopusdec.c:147
static const AVOption libopusdec_options[]
Definition libopusdec.c:219
static void libopus_flush(AVCodecContext *avc)
Definition libopusdec.c:206
#define OPUS_HEAD_SIZE
Definition libopusdec.c:50
#define OFFSET(x)
Definition libopusdec.c:217
#define FFMIN(a, b)
Definition macros.h:49
static av_const int sign_extend(int val, unsigned bits)
Definition mathops.h:135
AVOptions.
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
enum AVSampleFormat request_sample_fmt
desired sample format
Definition avcodec.h:1097
int sample_rate
samples per second
Definition avcodec.h:1040
int delay
Codec delay.
Definition avcodec.h:587
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition internal.h:125
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
union libopus_context::@247020046336201346334172224354210264025236321146 gain
OpusMSDecoder * dec
Definition libopusdec.c:40
#define av_log(a,...)
const AVChannelLayout ff_vorbis_ch_layouts[9]
Definition vorbis_data.c:37
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
Definition vorbis_data.c:26