Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/libopusdec.c
Line
Count
Source
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
38
struct libopus_context {
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
15.3k
#define OPUS_HEAD_SIZE 19
51
52
static av_cold int libopus_decode_init(AVCodecContext *avc)
53
6.69k
{
54
6.69k
    struct libopus_context *opus = avc->priv_data;
55
6.69k
    int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled, channels;
56
6.69k
    uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
57
58
6.69k
    channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->ch_layout.nb_channels == 1) ? 1 : 2;
59
6.69k
    if (channels <= 0) {
60
138
        av_log(avc, AV_LOG_WARNING,
61
138
               "Invalid number of channels %d, defaulting to stereo\n", channels);
62
138
        channels = 2;
63
138
    }
64
65
6.69k
    avc->sample_rate    = 48000;
66
6.69k
    avc->sample_fmt     = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
67
6.69k
                          AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
68
6.69k
    av_channel_layout_uninit(&avc->ch_layout);
69
6.69k
    if (channels > 8) {
70
200
        avc->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
71
200
        avc->ch_layout.nb_channels = channels;
72
6.49k
    } else {
73
6.49k
        av_channel_layout_copy(&avc->ch_layout, &ff_vorbis_ch_layouts[channels - 1]);
74
6.49k
    }
75
76
6.69k
    if (avc->extradata_size >= OPUS_HEAD_SIZE) {
77
682
        opus->pre_skip = AV_RL16(avc->extradata + 10);
78
682
        gain_db     = sign_extend(AV_RL16(avc->extradata + 16), 16);
79
682
        channel_map = AV_RL8 (avc->extradata + 18);
80
682
    }
81
6.69k
    if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + channels) {
82
660
        nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
83
660
        nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
84
660
        if (nb_streams + nb_coupled != channels)
85
650
            av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
86
660
        mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
87
6.03k
    } else {
88
6.03k
        if (channels > 2 || channel_map) {
89
4
            av_log(avc, AV_LOG_ERROR,
90
4
                   "No channel mapping for %d channels.\n", channels);
91
4
            return AVERROR(EINVAL);
92
4
        }
93
6.03k
        nb_streams = 1;
94
6.03k
        nb_coupled = channels > 1;
95
6.03k
        mapping    = mapping_arr;
96
6.03k
    }
97
98
6.69k
    if (channels > 2 && channels <= 8) {
99
90
        const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[channels - 1];
100
90
        int ch;
101
102
        /* Remap channels from Vorbis order to ffmpeg order */
103
497
        for (ch = 0; ch < channels; ch++)
104
407
            mapping_arr[ch] = mapping[vorbis_offset[ch]];
105
90
        mapping = mapping_arr;
106
90
    }
107
108
6.69k
    opus->dec = opus_multistream_decoder_create(avc->sample_rate, channels,
109
6.69k
                                                nb_streams, nb_coupled,
110
6.69k
                                                mapping, &ret);
111
6.69k
    if (!opus->dec) {
112
29
        av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
113
29
               opus_strerror(ret));
114
29
        return ff_opus_error_to_averror(ret);
115
29
    }
116
117
6.66k
#ifdef OPUS_SET_GAIN
118
6.66k
    ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
119
6.66k
    if (ret != OPUS_OK)
120
0
        av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
121
0
               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
6.66k
#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
133
6.66k
    ret = opus_multistream_decoder_ctl(opus->dec,
134
6.66k
                                       OPUS_SET_PHASE_INVERSION_DISABLED(!opus->apply_phase_inv));
135
6.66k
    if (ret != OPUS_OK)
136
0
        av_log(avc, AV_LOG_WARNING,
137
0
               "Unable to set phase inversion: %s\n",
138
0
               opus_strerror(ret));
139
6.66k
#endif
140
141
    /* Decoder delay (in samples) at 48kHz */
142
6.66k
    avc->delay = avc->internal->skip_samples = opus->pre_skip;
143
144
6.66k
    return 0;
145
6.69k
}
146
147
static av_cold int libopus_decode_close(AVCodecContext *avc)
148
6.69k
{
149
6.69k
    struct libopus_context *opus = avc->priv_data;
150
151
6.69k
    if (opus->dec) {
152
6.66k
        opus_multistream_decoder_destroy(opus->dec);
153
6.66k
        opus->dec = NULL;
154
6.66k
    }
155
6.69k
    return 0;
156
6.69k
}
157
158
292k
#define MAX_FRAME_SIZE (960 * 6)
159
160
static int libopus_decode(AVCodecContext *avc, AVFrame *frame,
161
                          int *got_frame_ptr, AVPacket *pkt)
162
292k
{
163
292k
    struct libopus_context *opus = avc->priv_data;
164
292k
    int ret, nb_samples;
165
166
292k
    frame->nb_samples = MAX_FRAME_SIZE;
167
292k
    if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
168
0
        return ret;
169
170
292k
    if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
171
292k
        nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
172
292k
                                             (opus_int16 *)frame->data[0],
173
292k
                                             frame->nb_samples, 0);
174
0
    else
175
0
        nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
176
0
                                                   (float *)frame->data[0],
177
0
                                                   frame->nb_samples, 0);
178
179
292k
    if (nb_samples < 0) {
180
58.0k
        av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
181
58.0k
               opus_strerror(nb_samples));
182
58.0k
        return ff_opus_error_to_averror(nb_samples);
183
58.0k
    }
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
234k
    frame->nb_samples = nb_samples;
201
234k
    *got_frame_ptr    = 1;
202
203
234k
    return pkt->size;
204
292k
}
205
206
static void libopus_flush(AVCodecContext *avc)
207
115k
{
208
115k
    struct libopus_context *opus = avc->priv_data;
209
210
115k
    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
115k
    avc->internal->skip_samples = opus->pre_skip;
214
115k
}
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
234
const FFCodec ff_libopus_decoder = {
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),
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
    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16),
248
    .p.priv_class   = &libopusdec_class,
249
    .p.wrapper_name = "libopus",
250
};