2 * Opus decoder using libopus
3 * Copyright (c) 2012 Nicolas George
5 * This file is part of FFmpeg.
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.
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.
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
23 #include <opus_multistream.h>
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/ffmath.h"
28 #include "libavutil/opt.h"
31 #include "codec_internal.h"
36 #include "vorbis_data.h"
38 struct libopus_context
{
43 union { int i
; double d
; } gain
;
45 #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
50 #define OPUS_HEAD_SIZE 19
52 static av_cold
int libopus_decode_init(AVCodecContext
*avc
)
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
;
58 channels
= avc
->extradata_size
>= 10 ? avc
->extradata
[9] : (avc
->ch_layout
.nb_channels
== 1) ? 1 : 2;
60 av_log(avc
, AV_LOG_WARNING
,
61 "Invalid number of channels %d, defaulting to stereo\n", channels
);
65 avc
->sample_rate
= 48000;
66 avc
->sample_fmt
= avc
->request_sample_fmt
== AV_SAMPLE_FMT_FLT
?
67 AV_SAMPLE_FMT_FLT
: AV_SAMPLE_FMT_S16
;
68 av_channel_layout_uninit(&avc
->ch_layout
);
70 avc
->ch_layout
.order
= AV_CHANNEL_ORDER_UNSPEC
;
71 avc
->ch_layout
.nb_channels
= channels
;
73 av_channel_layout_copy(&avc
->ch_layout
, &ff_vorbis_ch_layouts
[channels
- 1]);
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);
81 if (avc
->extradata_size
>= OPUS_HEAD_SIZE
+ 2 + channels
) {
82 nb_streams
= avc
->extradata
[OPUS_HEAD_SIZE
+ 0];
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;
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
);
94 nb_coupled
= channels
> 1;
95 mapping
= mapping_arr
;
98 if (channels
> 2 && channels
<= 8) {
99 const uint8_t *vorbis_offset
= ff_vorbis_channel_layout_offsets
[channels
- 1];
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
;
108 opus
->dec
= opus_multistream_decoder_create(avc
->sample_rate
, channels
,
109 nb_streams
, nb_coupled
,
112 av_log(avc
, AV_LOG_ERROR
, "Unable to create decoder: %s\n",
114 return ff_opus_error_to_averror(ret
);
118 ret
= opus_multistream_decoder_ctl(opus
->dec
, OPUS_SET_GAIN(gain_db
));
120 av_log(avc
, AV_LOG_WARNING
, "Failed to set gain: %s\n",
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
;
128 opus
->gain
.i
= FFMIN(gain_lin
* 65536, INT_MAX
);
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
));
136 av_log(avc
, AV_LOG_WARNING
,
137 "Unable to set phase inversion: %s\n",
141 /* Decoder delay (in samples) at 48kHz */
142 avc
->delay
= avc
->internal
->skip_samples
= opus
->pre_skip
;
147 static av_cold
int libopus_decode_close(AVCodecContext
*avc
)
149 struct libopus_context
*opus
= avc
->priv_data
;
152 opus_multistream_decoder_destroy(opus
->dec
);
158 #define MAX_FRAME_SIZE (960 * 6)
160 static int libopus_decode(AVCodecContext
*avc
, AVFrame
*frame
,
161 int *got_frame_ptr
, AVPacket
*pkt
)
163 struct libopus_context
*opus
= avc
->priv_data
;
166 frame
->nb_samples
= MAX_FRAME_SIZE
;
167 if ((ret
= ff_get_buffer(avc
, frame
, 0)) < 0)
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);
175 nb_samples
= opus_multistream_decode_float(opus
->dec
, pkt
->data
, pkt
->size
,
176 (float *)frame
->data
[0],
177 frame
->nb_samples
, 0);
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
);
185 #ifndef OPUS_SET_GAIN
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);
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);
200 frame
->nb_samples
= nb_samples
;
206 static void libopus_flush(AVCodecContext
*avc
)
208 struct libopus_context
*opus
= avc
->priv_data
;
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
;
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
},
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
,
234 const FFCodec ff_libopus_decoder
= {
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
),
240 .init
= libopus_decode_init
,
241 .close
= libopus_decode_close
,
242 FF_CODEC_DECODE_CB(libopus_decode
),
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
|
246 FF_CODEC_CAP_INIT_CLEANUP
,
247 .p
.sample_fmts
= (const enum AVSampleFormat
[]){ AV_SAMPLE_FMT_FLT
,
249 AV_SAMPLE_FMT_NONE
},
250 .p
.priv_class
= &libopusdec_class
,
251 .p
.wrapper_name
= "libopus",