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
32
36 #ifndef OPUS_SET_GAIN
38 #endif
39 };
40
41 #define OPUS_HEAD_SIZE 19
42
44 {
46 int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
47 uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
48
54
59 }
63 if (nb_streams + nb_coupled != avc->
channels)
66 } else {
67 if (avc->
channels > 2 || channel_map) {
69 "No channel mapping for %d channels.\n", avc->
channels);
71 }
72 nb_streams = 1;
74 mapping = mapping_arr;
75 }
76
79 int ch;
80
81 /* Remap channels from vorbis order to ffmpeg order */
82 for (ch = 0; ch < avc->
channels; ch++)
83 mapping_arr[ch] = mapping[vorbis_offset[ch]];
84 mapping = mapping_arr;
85 }
86
88 nb_streams, nb_coupled,
89 mapping, &ret);
92 opus_strerror(ret));
94 }
95
96 #ifdef OPUS_SET_GAIN
97 ret = opus_multistream_decoder_ctl(opus->
dec, OPUS_SET_GAIN(gain_db));
98 if (ret != OPUS_OK)
100 opus_strerror(ret));
101 #else
102 {
103 double gain_lin = pow(10, gain_db / (20.0 * 256));
105 opus->
gain.
d = gain_lin;
106 else
107 opus->
gain.
i =
FFMIN(gain_lin * 65536, INT_MAX);
108 }
109 #endif
110
112 avc->
delay = 3840;
/* Decoder delay (in samples) at 48kHz */
113
114 return 0;
115 }
116
118 {
120
121 opus_multistream_decoder_destroy(opus->
dec);
122 return 0;
123 }
124
125 #define MAX_FRAME_SIZE (960 * 6)
126
129 {
133
137
139 nb_samples = opus_multistream_decode(opus->
dec, pkt->
data, pkt->
size,
140 (opus_int16 *)frame->
data[0],
142 else
143 nb_samples = opus_multistream_decode_float(opus->
dec, pkt->
data, pkt->
size,
144 (
float *)frame->
data[0],
146
147 if (nb_samples < 0) {
149 opus_strerror(nb_samples));
151 }
152
153 #ifndef OPUS_SET_GAIN
154 {
157 float *pcm = (
float *)frame->
data[0];
158 for (; i > 0; i--, pcm++)
159 *pcm = av_clipf(*pcm * opus->
gain.
d, -1, 1);
160 } else {
161 int16_t *pcm = (int16_t *)frame->
data[0];
162 for (; i > 0; i--, pcm++)
163 *pcm = av_clip_int16(((int64_t)opus->
gain.
i * *pcm) >> 16);
164 }
165 }
166 #endif
167
169 *got_frame_ptr = 1;
170
172 }
173
175 {
177
178 opus_multistream_decoder_ctl(opus->
dec, OPUS_RESET_STATE);
179 /* The stream can have been extracted by a tool that is not Opus-aware.
180 Therefore, any packet can become the first of the stream. */
182 }
183
198 };