Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1 | /* Copyright (c) 2011 Xiph.Org Foundation |
| 2 | Written by Jean-Marc Valin */ |
| 3 | /* |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions |
| 6 | are met: |
| 7 | |
| 8 | - Redistributions of source code must retain the above copyright |
| 9 | notice, this list of conditions and the following disclaimer. |
| 10 | |
| 11 | - Redistributions in binary form must reproduce the above copyright |
| 12 | notice, this list of conditions and the following disclaimer in the |
| 13 | documentation and/or other materials provided with the distribution. |
| 14 | |
| 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
| 19 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include "config.h" |
| 30 | #endif |
| 31 | |
| 32 | #include "opus_multistream.h" |
| 33 | #include "opus.h" |
| 34 | #include "opus_private.h" |
| 35 | #include "stack_alloc.h" |
| 36 | #include <stdarg.h> |
| 37 | #include "float_cast.h" |
| 38 | #include "os_support.h" |
Jean-Marc Valin | 4958751 | 2013-07-07 02:50:18 -0400 | [diff] [blame] | 39 | #include "mathops.h" |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 40 | #include "mdct.h" |
| 41 | #include "modes.h" |
| 42 | #include "bands.h" |
| 43 | #include "quant_bands.h" |
Jean-Marc Valin | 7c49ad0 | 2015-10-07 09:17:50 -0400 | [diff] [blame] | 44 | #include "pitch.h" |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 45 | |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 46 | typedef struct { |
| 47 | int nb_streams; |
| 48 | int nb_coupled_streams; |
| 49 | unsigned char mapping[8]; |
| 50 | } VorbisLayout; |
| 51 | |
| 52 | /* Index is nb_channel-1*/ |
| 53 | static const VorbisLayout vorbis_mappings[8] = { |
| 54 | {1, 0, {0}}, /* 1: mono */ |
| 55 | {1, 1, {0, 1}}, /* 2: stereo */ |
| 56 | {2, 1, {0, 2, 1}}, /* 3: 1-d surround */ |
| 57 | {2, 2, {0, 1, 2, 3}}, /* 4: quadraphonic surround */ |
| 58 | {3, 2, {0, 4, 1, 2, 3}}, /* 5: 5-channel surround */ |
| 59 | {4, 2, {0, 4, 1, 2, 3, 5}}, /* 6: 5.1 surround */ |
| 60 | {4, 3, {0, 4, 1, 2, 3, 5, 6}}, /* 7: 6.1 surround */ |
| 61 | {5, 3, {0, 6, 1, 2, 3, 4, 5, 7}}, /* 8: 7.1 surround */ |
| 62 | }; |
| 63 | |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 64 | typedef void (*opus_copy_channel_in_func)( |
| 65 | opus_val16 *dst, |
| 66 | int dst_stride, |
| 67 | const void *src, |
| 68 | int src_stride, |
| 69 | int src_channel, |
| 70 | int frame_size |
| 71 | ); |
| 72 | |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 73 | typedef enum { |
| 74 | MAPPING_TYPE_NONE, |
Michael Graczyk | d6642d6 | 2016-06-29 20:30:19 -0700 | [diff] [blame] | 75 | MAPPING_TYPE_SURROUND |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 76 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
Michael Graczyk | d6642d6 | 2016-06-29 20:30:19 -0700 | [diff] [blame] | 77 | , /* Do not include comma at end of enumerator list */ |
| 78 | MAPPING_TYPE_AMBISONICS |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 79 | #endif |
| 80 | } MappingType; |
| 81 | |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 82 | struct OpusMSEncoder { |
| 83 | ChannelLayout layout; |
Viswanath Puttagunta | f48abe8 | 2015-05-15 12:42:19 -0500 | [diff] [blame] | 84 | int arch; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 85 | int lfe_stream; |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 86 | int application; |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 87 | int variable_duration; |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 88 | MappingType mapping_type; |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 89 | opus_int32 bitrate_bps; |
Jean-Marc Valin | ab86a9c | 2013-11-13 23:06:25 -0500 | [diff] [blame] | 90 | float subframe_mem[3]; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 91 | /* Encoder states go here */ |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 92 | /* then opus_val32 window_mem[channels*120]; */ |
| 93 | /* then opus_val32 preemph_mem[channels]; */ |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 94 | }; |
| 95 | |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 96 | static opus_val32 *ms_get_preemph_mem(OpusMSEncoder *st) |
| 97 | { |
| 98 | int s; |
| 99 | char *ptr; |
| 100 | int coupled_size, mono_size; |
| 101 | |
| 102 | coupled_size = opus_encoder_get_size(2); |
| 103 | mono_size = opus_encoder_get_size(1); |
| 104 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 105 | for (s=0;s<st->layout.nb_streams;s++) |
| 106 | { |
| 107 | if (s < st->layout.nb_coupled_streams) |
| 108 | ptr += align(coupled_size); |
| 109 | else |
| 110 | ptr += align(mono_size); |
| 111 | } |
Mark Harris | 4feb0df | 2015-08-07 01:24:48 -0700 | [diff] [blame] | 112 | /* void* cast avoids clang -Wcast-align warning */ |
| 113 | return (opus_val32*)(void*)(ptr+st->layout.nb_channels*120*sizeof(opus_val32)); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static opus_val32 *ms_get_window_mem(OpusMSEncoder *st) |
| 117 | { |
| 118 | int s; |
| 119 | char *ptr; |
| 120 | int coupled_size, mono_size; |
| 121 | |
| 122 | coupled_size = opus_encoder_get_size(2); |
| 123 | mono_size = opus_encoder_get_size(1); |
| 124 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 125 | for (s=0;s<st->layout.nb_streams;s++) |
| 126 | { |
| 127 | if (s < st->layout.nb_coupled_streams) |
| 128 | ptr += align(coupled_size); |
| 129 | else |
| 130 | ptr += align(mono_size); |
| 131 | } |
Mark Harris | 4feb0df | 2015-08-07 01:24:48 -0700 | [diff] [blame] | 132 | /* void* cast avoids clang -Wcast-align warning */ |
| 133 | return (opus_val32*)(void*)ptr; |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 134 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 135 | |
| 136 | static int validate_encoder_layout(const ChannelLayout *layout) |
| 137 | { |
| 138 | int s; |
| 139 | for (s=0;s<layout->nb_streams;s++) |
| 140 | { |
| 141 | if (s < layout->nb_coupled_streams) |
| 142 | { |
| 143 | if (get_left_channel(layout, s, -1)==-1) |
| 144 | return 0; |
| 145 | if (get_right_channel(layout, s, -1)==-1) |
| 146 | return 0; |
| 147 | } else { |
| 148 | if (get_mono_channel(layout, s, -1)==-1) |
| 149 | return 0; |
| 150 | } |
| 151 | } |
| 152 | return 1; |
| 153 | } |
| 154 | |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 155 | static void channel_pos(int channels, int pos[8]) |
| 156 | { |
| 157 | /* Position in the mix: 0 don't mix, 1: left, 2: center, 3:right */ |
| 158 | if (channels==4) |
| 159 | { |
| 160 | pos[0]=1; |
| 161 | pos[1]=3; |
| 162 | pos[2]=1; |
| 163 | pos[3]=3; |
| 164 | } else if (channels==3||channels==5||channels==6) |
| 165 | { |
| 166 | pos[0]=1; |
| 167 | pos[1]=2; |
| 168 | pos[2]=3; |
| 169 | pos[3]=1; |
| 170 | pos[4]=3; |
| 171 | pos[5]=0; |
| 172 | } else if (channels==7) |
| 173 | { |
| 174 | pos[0]=1; |
| 175 | pos[1]=2; |
| 176 | pos[2]=3; |
| 177 | pos[3]=1; |
| 178 | pos[4]=3; |
| 179 | pos[5]=2; |
| 180 | pos[6]=0; |
| 181 | } else if (channels==8) |
| 182 | { |
| 183 | pos[0]=1; |
| 184 | pos[1]=2; |
| 185 | pos[2]=3; |
| 186 | pos[3]=1; |
| 187 | pos[4]=3; |
| 188 | pos[5]=1; |
| 189 | pos[6]=3; |
| 190 | pos[7]=0; |
| 191 | } |
| 192 | } |
| 193 | |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 194 | #if 1 |
| 195 | /* Computes a rough approximation of log2(2^a + 2^b) */ |
| 196 | static opus_val16 logSum(opus_val16 a, opus_val16 b) |
| 197 | { |
| 198 | opus_val16 max; |
| 199 | opus_val32 diff; |
| 200 | opus_val16 frac; |
| 201 | static const opus_val16 diff_table[17] = { |
| 202 | QCONST16(0.5000000f, DB_SHIFT), QCONST16(0.2924813f, DB_SHIFT), QCONST16(0.1609640f, DB_SHIFT), QCONST16(0.0849625f, DB_SHIFT), |
| 203 | QCONST16(0.0437314f, DB_SHIFT), QCONST16(0.0221971f, DB_SHIFT), QCONST16(0.0111839f, DB_SHIFT), QCONST16(0.0056136f, DB_SHIFT), |
| 204 | QCONST16(0.0028123f, DB_SHIFT) |
| 205 | }; |
| 206 | int low; |
| 207 | if (a>b) |
| 208 | { |
| 209 | max = a; |
| 210 | diff = SUB32(EXTEND32(a),EXTEND32(b)); |
| 211 | } else { |
| 212 | max = b; |
| 213 | diff = SUB32(EXTEND32(b),EXTEND32(a)); |
| 214 | } |
Mark Harris | a6595e6 | 2015-10-07 09:21:23 -0400 | [diff] [blame] | 215 | if (!(diff < QCONST16(8.f, DB_SHIFT))) /* inverted to catch NaNs */ |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 216 | return max; |
| 217 | #ifdef FIXED_POINT |
| 218 | low = SHR32(diff, DB_SHIFT-1); |
| 219 | frac = SHL16(diff - SHL16(low, DB_SHIFT-1), 16-DB_SHIFT); |
| 220 | #else |
Jean-Marc Valin | a71c9ad | 2013-11-13 12:07:01 -0500 | [diff] [blame] | 221 | low = (int)floor(2*diff); |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 222 | frac = 2*diff - low; |
| 223 | #endif |
| 224 | return max + diff_table[low] + MULT16_16_Q15(frac, SUB16(diff_table[low+1], diff_table[low])); |
| 225 | } |
| 226 | #else |
| 227 | opus_val16 logSum(opus_val16 a, opus_val16 b) |
| 228 | { |
| 229 | return log2(pow(4, a)+ pow(4, b))/2; |
| 230 | } |
| 231 | #endif |
| 232 | |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 233 | void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *bandLogE, opus_val32 *mem, opus_val32 *preemph_mem, |
Viswanath Puttagunta | f48abe8 | 2015-05-15 12:42:19 -0500 | [diff] [blame] | 234 | int len, int overlap, int channels, int rate, opus_copy_channel_in_func copy_channel_in, int arch |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 235 | ) |
| 236 | { |
| 237 | int c; |
| 238 | int i; |
Jean-Marc Valin | 54bddf0 | 2013-09-11 23:34:51 -0400 | [diff] [blame] | 239 | int LM; |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 240 | int pos[8] = {0}; |
| 241 | int upsample; |
Jean-Marc Valin | 978e4cb | 2013-09-11 00:51:22 -0400 | [diff] [blame] | 242 | int frame_size; |
Jean-Marc Valin | a3fadcb | 2016-09-09 16:13:43 -0400 | [diff] [blame] | 243 | int freq_size; |
Jean-Marc Valin | 942fc81 | 2013-10-01 19:27:30 -0400 | [diff] [blame] | 244 | opus_val16 channel_offset; |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 245 | opus_val32 bandE[21]; |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 246 | opus_val16 maskLogE[3][21]; |
| 247 | VARDECL(opus_val32, in); |
| 248 | VARDECL(opus_val16, x); |
Jean-Marc Valin | e8e5ecb | 2013-10-01 17:16:33 -0400 | [diff] [blame] | 249 | VARDECL(opus_val32, freq); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 250 | SAVE_STACK; |
Jean-Marc Valin | 978e4cb | 2013-09-11 00:51:22 -0400 | [diff] [blame] | 251 | |
| 252 | upsample = resampling_factor(rate); |
| 253 | frame_size = len*upsample; |
Jean-Marc Valin | a3fadcb | 2016-09-09 16:13:43 -0400 | [diff] [blame] | 254 | freq_size = IMIN(960, frame_size); |
Jean-Marc Valin | 978e4cb | 2013-09-11 00:51:22 -0400 | [diff] [blame] | 255 | |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 256 | /* LM = log2(frame_size / 120) */ |
Jean-Marc Valin | 2e653a7 | 2013-10-14 17:47:18 -0400 | [diff] [blame] | 257 | for (LM=0;LM<celt_mode->maxLM;LM++) |
Jean-Marc Valin | 54bddf0 | 2013-09-11 23:34:51 -0400 | [diff] [blame] | 258 | if (celt_mode->shortMdctSize<<LM==frame_size) |
| 259 | break; |
| 260 | |
Jean-Marc Valin | 978e4cb | 2013-09-11 00:51:22 -0400 | [diff] [blame] | 261 | ALLOC(in, frame_size+overlap, opus_val32); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 262 | ALLOC(x, len, opus_val16); |
Jean-Marc Valin | a3fadcb | 2016-09-09 16:13:43 -0400 | [diff] [blame] | 263 | ALLOC(freq, freq_size, opus_val32); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 264 | |
| 265 | channel_pos(channels, pos); |
| 266 | |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 267 | for (c=0;c<3;c++) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 268 | for (i=0;i<21;i++) |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 269 | maskLogE[c][i] = -QCONST16(28.f, DB_SHIFT); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 270 | |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 271 | for (c=0;c<channels;c++) |
| 272 | { |
Jean-Marc Valin | a3fadcb | 2016-09-09 16:13:43 -0400 | [diff] [blame] | 273 | int frame; |
| 274 | int nb_frames = frame_size/freq_size; |
| 275 | celt_assert(nb_frames*freq_size == frame_size); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 276 | OPUS_COPY(in, mem+c*overlap, overlap); |
| 277 | (*copy_channel_in)(x, 1, pcm, channels, c, len); |
Jean-Marc Valin | 2dc27df | 2013-11-13 19:35:43 -0500 | [diff] [blame] | 278 | celt_preemphasis(x, in+overlap, frame_size, 1, upsample, celt_mode->preemph, preemph_mem+c, 0); |
Jean-Marc Valin | 7c49ad0 | 2015-10-07 09:17:50 -0400 | [diff] [blame] | 279 | #ifndef FIXED_POINT |
| 280 | { |
| 281 | opus_val32 sum; |
| 282 | sum = celt_inner_prod(in, in, frame_size+overlap, 0); |
| 283 | /* This should filter out both NaNs and ridiculous signals that could |
| 284 | cause NaNs further down. */ |
| 285 | if (!(sum < 1e9f) || celt_isnan(sum)) |
| 286 | { |
| 287 | OPUS_CLEAR(in, frame_size+overlap); |
| 288 | preemph_mem[c] = 0; |
| 289 | } |
| 290 | } |
| 291 | #endif |
Jean-Marc Valin | a3fadcb | 2016-09-09 16:13:43 -0400 | [diff] [blame] | 292 | OPUS_CLEAR(bandE, 21); |
| 293 | for (frame=0;frame<nb_frames;frame++) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 294 | { |
Jean-Marc Valin | a3fadcb | 2016-09-09 16:13:43 -0400 | [diff] [blame] | 295 | opus_val32 tmpE[21]; |
| 296 | clt_mdct_forward(&celt_mode->mdct, in+960*frame, freq, celt_mode->window, |
| 297 | overlap, celt_mode->maxLM-LM, 1, arch); |
| 298 | if (upsample != 1) |
| 299 | { |
| 300 | int bound = freq_size/upsample; |
| 301 | for (i=0;i<bound;i++) |
| 302 | freq[i] *= upsample; |
| 303 | for (;i<freq_size;i++) |
| 304 | freq[i] = 0; |
| 305 | } |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 306 | |
Jean-Marc Valin | a3fadcb | 2016-09-09 16:13:43 -0400 | [diff] [blame] | 307 | compute_band_energies(celt_mode, freq, tmpE, 21, 1, LM); |
| 308 | /* If we have multiple frames, take the max energy. */ |
| 309 | for (i=0;i<21;i++) |
| 310 | bandE[i] = MAX32(bandE[i], tmpE[i]); |
| 311 | } |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 312 | amp2Log2(celt_mode, 21, 21, bandE, bandLogE+21*c, 1); |
Jean-Marc Valin | 0f68696 | 2013-09-05 12:49:55 -0400 | [diff] [blame] | 313 | /* Apply spreading function with -6 dB/band going up and -12 dB/band going down. */ |
| 314 | for (i=1;i<21;i++) |
| 315 | bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i-1]-QCONST16(1.f, DB_SHIFT)); |
| 316 | for (i=19;i>=0;i--) |
| 317 | bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i+1]-QCONST16(2.f, DB_SHIFT)); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 318 | if (pos[c]==1) |
| 319 | { |
| 320 | for (i=0;i<21;i++) |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 321 | maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 322 | } else if (pos[c]==3) |
| 323 | { |
| 324 | for (i=0;i<21;i++) |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 325 | maskLogE[2][i] = logSum(maskLogE[2][i], bandLogE[21*c+i]); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 326 | } else if (pos[c]==2) |
| 327 | { |
| 328 | for (i=0;i<21;i++) |
| 329 | { |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 330 | maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]-QCONST16(.5f, DB_SHIFT)); |
| 331 | maskLogE[2][i] = logSum(maskLogE[2][i], bandLogE[21*c+i]-QCONST16(.5f, DB_SHIFT)); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 332 | } |
| 333 | } |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 334 | #if 0 |
| 335 | for (i=0;i<21;i++) |
| 336 | printf("%f ", bandLogE[21*c+i]); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 337 | float sum=0; |
| 338 | for (i=0;i<21;i++) |
| 339 | sum += bandLogE[21*c+i]; |
| 340 | printf("%f ", sum/21); |
| 341 | #endif |
Jean-Marc Valin | 978e4cb | 2013-09-11 00:51:22 -0400 | [diff] [blame] | 342 | OPUS_COPY(mem+c*overlap, in+frame_size, overlap); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 343 | } |
| 344 | for (i=0;i<21;i++) |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 345 | maskLogE[1][i] = MIN32(maskLogE[0][i],maskLogE[2][i]); |
Jean-Marc Valin | 942fc81 | 2013-10-01 19:27:30 -0400 | [diff] [blame] | 346 | channel_offset = HALF16(celt_log2(QCONST32(2.f,14)/(channels-1))); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 347 | for (c=0;c<3;c++) |
| 348 | for (i=0;i<21;i++) |
Jean-Marc Valin | 942fc81 | 2013-10-01 19:27:30 -0400 | [diff] [blame] | 349 | maskLogE[c][i] += channel_offset; |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 350 | #if 0 |
| 351 | for (c=0;c<3;c++) |
| 352 | { |
| 353 | for (i=0;i<21;i++) |
| 354 | printf("%f ", maskLogE[c][i]); |
| 355 | } |
| 356 | #endif |
| 357 | for (c=0;c<channels;c++) |
| 358 | { |
| 359 | opus_val16 *mask; |
| 360 | if (pos[c]!=0) |
| 361 | { |
| 362 | mask = &maskLogE[pos[c]-1][0]; |
| 363 | for (i=0;i<21;i++) |
| 364 | bandLogE[21*c+i] = bandLogE[21*c+i] - mask[i]; |
| 365 | } else { |
| 366 | for (i=0;i<21;i++) |
| 367 | bandLogE[21*c+i] = 0; |
| 368 | } |
| 369 | #if 0 |
| 370 | for (i=0;i<21;i++) |
| 371 | printf("%f ", bandLogE[21*c+i]); |
| 372 | printf("\n"); |
| 373 | #endif |
| 374 | #if 0 |
| 375 | float sum=0; |
| 376 | for (i=0;i<21;i++) |
| 377 | sum += bandLogE[21*c+i]; |
Jean-Marc Valin | 942fc81 | 2013-10-01 19:27:30 -0400 | [diff] [blame] | 378 | printf("%f ", sum/(float)QCONST32(21.f, DB_SHIFT)); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 379 | printf("\n"); |
| 380 | #endif |
| 381 | } |
| 382 | RESTORE_STACK; |
| 383 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 384 | |
| 385 | opus_int32 opus_multistream_encoder_get_size(int nb_streams, int nb_coupled_streams) |
| 386 | { |
| 387 | int coupled_size; |
| 388 | int mono_size; |
| 389 | |
| 390 | if(nb_streams<1||nb_coupled_streams>nb_streams||nb_coupled_streams<0)return 0; |
| 391 | coupled_size = opus_encoder_get_size(2); |
| 392 | mono_size = opus_encoder_get_size(1); |
| 393 | return align(sizeof(OpusMSEncoder)) |
| 394 | + nb_coupled_streams * align(coupled_size) |
| 395 | + (nb_streams-nb_coupled_streams) * align(mono_size); |
| 396 | } |
| 397 | |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 398 | opus_int32 opus_multistream_surround_encoder_get_size(int channels, int mapping_family) |
| 399 | { |
| 400 | int nb_streams; |
| 401 | int nb_coupled_streams; |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 402 | opus_int32 size; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 403 | |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 404 | if (mapping_family==0) |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 405 | { |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 406 | if (channels==1) |
| 407 | { |
| 408 | nb_streams=1; |
| 409 | nb_coupled_streams=0; |
| 410 | } else if (channels==2) |
| 411 | { |
| 412 | nb_streams=1; |
| 413 | nb_coupled_streams=1; |
| 414 | } else |
| 415 | return 0; |
| 416 | } else if (mapping_family==1 && channels<=8 && channels>=1) |
| 417 | { |
| 418 | nb_streams=vorbis_mappings[channels-1].nb_streams; |
| 419 | nb_coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams; |
| 420 | } else if (mapping_family==255) |
| 421 | { |
| 422 | nb_streams=channels; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 423 | nb_coupled_streams=0; |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 424 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 425 | } else if (mapping_family==254) |
| 426 | { |
| 427 | nb_streams=channels; |
| 428 | nb_coupled_streams=0; |
| 429 | #endif |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 430 | } else |
| 431 | return 0; |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 432 | size = opus_multistream_encoder_get_size(nb_streams, nb_coupled_streams); |
| 433 | if (channels>2) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 434 | { |
| 435 | size += channels*(120*sizeof(opus_val32) + sizeof(opus_val32)); |
| 436 | } |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 437 | return size; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 438 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 439 | |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 440 | static int opus_multistream_encoder_init_impl( |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 441 | OpusMSEncoder *st, |
| 442 | opus_int32 Fs, |
| 443 | int channels, |
| 444 | int streams, |
| 445 | int coupled_streams, |
| 446 | const unsigned char *mapping, |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 447 | int application, |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 448 | MappingType mapping_type |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 449 | ) |
| 450 | { |
| 451 | int coupled_size; |
| 452 | int mono_size; |
| 453 | int i, ret; |
| 454 | char *ptr; |
| 455 | |
| 456 | if ((channels>255) || (channels<1) || (coupled_streams>streams) || |
Mark Harris | 25b27a9 | 2014-11-27 08:48:09 -0800 | [diff] [blame] | 457 | (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams)) |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 458 | return OPUS_BAD_ARG; |
| 459 | |
Viswanath Puttagunta | f48abe8 | 2015-05-15 12:42:19 -0500 | [diff] [blame] | 460 | st->arch = opus_select_arch(); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 461 | st->layout.nb_channels = channels; |
| 462 | st->layout.nb_streams = streams; |
| 463 | st->layout.nb_coupled_streams = coupled_streams; |
Jean-Marc Valin | b042935 | 2013-05-05 02:22:06 -0400 | [diff] [blame] | 464 | st->subframe_mem[0]=st->subframe_mem[1]=st->subframe_mem[2]=0; |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 465 | if (mapping_type != MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 466 | st->lfe_stream = -1; |
Jean-Marc Valin | 95561be | 2012-12-17 17:54:01 -0500 | [diff] [blame] | 467 | st->bitrate_bps = OPUS_AUTO; |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 468 | st->application = application; |
Jean-Marc Valin | 51f4a32 | 2013-02-20 04:08:04 -0500 | [diff] [blame] | 469 | st->variable_duration = OPUS_FRAMESIZE_ARG; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 470 | for (i=0;i<st->layout.nb_channels;i++) |
| 471 | st->layout.mapping[i] = mapping[i]; |
| 472 | if (!validate_layout(&st->layout) || !validate_encoder_layout(&st->layout)) |
| 473 | return OPUS_BAD_ARG; |
| 474 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 475 | coupled_size = opus_encoder_get_size(2); |
| 476 | mono_size = opus_encoder_get_size(1); |
| 477 | |
| 478 | for (i=0;i<st->layout.nb_coupled_streams;i++) |
| 479 | { |
| 480 | ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 2, application); |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 481 | if(ret!=OPUS_OK)return ret; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 482 | if (i==st->lfe_stream) |
| 483 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1)); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 484 | ptr += align(coupled_size); |
| 485 | } |
| 486 | for (;i<st->layout.nb_streams;i++) |
| 487 | { |
| 488 | ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 1, application); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 489 | if (i==st->lfe_stream) |
| 490 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1)); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 491 | if(ret!=OPUS_OK)return ret; |
| 492 | ptr += align(mono_size); |
| 493 | } |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 494 | if (mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 495 | { |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 496 | OPUS_CLEAR(ms_get_preemph_mem(st), channels); |
| 497 | OPUS_CLEAR(ms_get_window_mem(st), channels*120); |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 498 | } |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 499 | st->mapping_type = mapping_type; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 500 | return OPUS_OK; |
| 501 | } |
| 502 | |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 503 | int opus_multistream_encoder_init( |
| 504 | OpusMSEncoder *st, |
| 505 | opus_int32 Fs, |
| 506 | int channels, |
| 507 | int streams, |
| 508 | int coupled_streams, |
| 509 | const unsigned char *mapping, |
| 510 | int application |
| 511 | ) |
| 512 | { |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 513 | return opus_multistream_encoder_init_impl(st, Fs, channels, streams, |
| 514 | coupled_streams, mapping, |
| 515 | application, MAPPING_TYPE_NONE); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | int opus_multistream_surround_encoder_init( |
| 519 | OpusMSEncoder *st, |
| 520 | opus_int32 Fs, |
| 521 | int channels, |
| 522 | int mapping_family, |
| 523 | int *streams, |
| 524 | int *coupled_streams, |
| 525 | unsigned char *mapping, |
| 526 | int application |
| 527 | ) |
| 528 | { |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 529 | MappingType mapping_type; |
| 530 | |
Jean-Marc Valin | 337f34c | 2013-07-01 16:17:01 -0400 | [diff] [blame] | 531 | if ((channels>255) || (channels<1)) |
| 532 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 533 | st->lfe_stream = -1; |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 534 | if (mapping_family==0) |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 535 | { |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 536 | if (channels==1) |
| 537 | { |
| 538 | *streams=1; |
| 539 | *coupled_streams=0; |
| 540 | mapping[0]=0; |
| 541 | } else if (channels==2) |
| 542 | { |
| 543 | *streams=1; |
| 544 | *coupled_streams=1; |
| 545 | mapping[0]=0; |
| 546 | mapping[1]=1; |
| 547 | } else |
| 548 | return OPUS_UNIMPLEMENTED; |
| 549 | } else if (mapping_family==1 && channels<=8 && channels>=1) |
| 550 | { |
| 551 | int i; |
| 552 | *streams=vorbis_mappings[channels-1].nb_streams; |
| 553 | *coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams; |
| 554 | for (i=0;i<channels;i++) |
| 555 | mapping[i] = vorbis_mappings[channels-1].mapping[i]; |
| 556 | if (channels>=6) |
| 557 | st->lfe_stream = *streams-1; |
| 558 | } else if (mapping_family==255) |
| 559 | { |
| 560 | int i; |
| 561 | *streams=channels; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 562 | *coupled_streams=0; |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 563 | for(i=0;i<channels;i++) |
| 564 | mapping[i] = i; |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 565 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 566 | } else if (mapping_family==254) |
| 567 | { |
| 568 | int i; |
| 569 | *streams=channels; |
| 570 | *coupled_streams=0; |
| 571 | for(i=0;i<channels;i++) |
| 572 | mapping[i] = i; |
| 573 | #endif |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 574 | } else |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 575 | return OPUS_UNIMPLEMENTED; |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 576 | |
| 577 | if (channels>2 && mapping_family==1) { |
| 578 | mapping_type = MAPPING_TYPE_SURROUND; |
| 579 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 580 | } else if (mapping_family==254) |
| 581 | { |
| 582 | mapping_type = MAPPING_TYPE_AMBISONICS; |
| 583 | #endif |
| 584 | } else |
| 585 | { |
| 586 | mapping_type = MAPPING_TYPE_NONE; |
| 587 | } |
| 588 | return opus_multistream_encoder_init_impl(st, Fs, channels, *streams, |
| 589 | *coupled_streams, mapping, |
| 590 | application, mapping_type); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 591 | } |
| 592 | |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 593 | OpusMSEncoder *opus_multistream_encoder_create( |
| 594 | opus_int32 Fs, |
| 595 | int channels, |
| 596 | int streams, |
| 597 | int coupled_streams, |
| 598 | const unsigned char *mapping, |
| 599 | int application, |
| 600 | int *error |
| 601 | ) |
| 602 | { |
| 603 | int ret; |
| 604 | OpusMSEncoder *st; |
| 605 | if ((channels>255) || (channels<1) || (coupled_streams>streams) || |
Mark Harris | 25b27a9 | 2014-11-27 08:48:09 -0800 | [diff] [blame] | 606 | (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams)) |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 607 | { |
| 608 | if (error) |
| 609 | *error = OPUS_BAD_ARG; |
| 610 | return NULL; |
| 611 | } |
| 612 | st = (OpusMSEncoder *)opus_alloc(opus_multistream_encoder_get_size(streams, coupled_streams)); |
| 613 | if (st==NULL) |
| 614 | { |
| 615 | if (error) |
| 616 | *error = OPUS_ALLOC_FAIL; |
| 617 | return NULL; |
| 618 | } |
| 619 | ret = opus_multistream_encoder_init(st, Fs, channels, streams, coupled_streams, mapping, application); |
| 620 | if (ret != OPUS_OK) |
| 621 | { |
| 622 | opus_free(st); |
| 623 | st = NULL; |
| 624 | } |
| 625 | if (error) |
| 626 | *error = ret; |
| 627 | return st; |
| 628 | } |
| 629 | |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 630 | OpusMSEncoder *opus_multistream_surround_encoder_create( |
| 631 | opus_int32 Fs, |
| 632 | int channels, |
| 633 | int mapping_family, |
| 634 | int *streams, |
| 635 | int *coupled_streams, |
| 636 | unsigned char *mapping, |
| 637 | int application, |
| 638 | int *error |
| 639 | ) |
| 640 | { |
| 641 | int ret; |
Mark Harris | 25b27a9 | 2014-11-27 08:48:09 -0800 | [diff] [blame] | 642 | opus_int32 size; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 643 | OpusMSEncoder *st; |
| 644 | if ((channels>255) || (channels<1)) |
| 645 | { |
| 646 | if (error) |
| 647 | *error = OPUS_BAD_ARG; |
| 648 | return NULL; |
| 649 | } |
Mark Harris | 25b27a9 | 2014-11-27 08:48:09 -0800 | [diff] [blame] | 650 | size = opus_multistream_surround_encoder_get_size(channels, mapping_family); |
| 651 | if (!size) |
| 652 | { |
| 653 | if (error) |
| 654 | *error = OPUS_UNIMPLEMENTED; |
| 655 | return NULL; |
| 656 | } |
| 657 | st = (OpusMSEncoder *)opus_alloc(size); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 658 | if (st==NULL) |
| 659 | { |
| 660 | if (error) |
| 661 | *error = OPUS_ALLOC_FAIL; |
| 662 | return NULL; |
| 663 | } |
| 664 | ret = opus_multistream_surround_encoder_init(st, Fs, channels, mapping_family, streams, coupled_streams, mapping, application); |
| 665 | if (ret != OPUS_OK) |
| 666 | { |
| 667 | opus_free(st); |
| 668 | st = NULL; |
| 669 | } |
| 670 | if (error) |
| 671 | *error = ret; |
| 672 | return st; |
| 673 | } |
| 674 | |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 675 | static void surround_rate_allocation( |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 676 | OpusMSEncoder *st, |
| 677 | opus_int32 *rate, |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 678 | int frame_size, |
| 679 | opus_int32 Fs |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 680 | ) |
| 681 | { |
| 682 | int i; |
| 683 | opus_int32 channel_rate; |
Jean-Marc Valin | d66bdc7 | 2013-05-06 16:03:39 -0400 | [diff] [blame] | 684 | int stream_offset; |
| 685 | int lfe_offset; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 686 | int coupled_ratio; /* Q8 */ |
| 687 | int lfe_ratio; /* Q8 */ |
Jean-Marc Valin | 8ac5d89 | 2016-09-19 17:18:58 -0400 | [diff] [blame] | 688 | int nb_lfe; |
| 689 | int nb_uncoupled; |
| 690 | int nb_coupled; |
| 691 | int nb_normal; |
| 692 | opus_int32 channel_offset; |
| 693 | opus_int32 bitrate; |
| 694 | int total; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 695 | |
Jean-Marc Valin | 8ac5d89 | 2016-09-19 17:18:58 -0400 | [diff] [blame] | 696 | nb_lfe = (st->lfe_stream!=-1); |
| 697 | nb_coupled = st->layout.nb_coupled_streams; |
| 698 | nb_uncoupled = st->layout.nb_streams-nb_coupled-nb_lfe; |
| 699 | nb_normal = 2*nb_coupled + nb_uncoupled; |
| 700 | |
| 701 | /* Give each non-LFE channel enough bits per channel for coding band energy. */ |
| 702 | channel_offset = 40*IMAX(50, Fs/frame_size); |
| 703 | |
| 704 | if (st->bitrate_bps==OPUS_AUTO) |
| 705 | { |
| 706 | bitrate = nb_normal*(channel_offset + Fs + 10000) + 8000*nb_lfe; |
| 707 | } else if (st->bitrate_bps==OPUS_BITRATE_MAX) |
| 708 | { |
| 709 | bitrate = nb_normal*300000 + nb_lfe*128000; |
| 710 | } else { |
| 711 | bitrate = st->bitrate_bps; |
| 712 | } |
| 713 | |
| 714 | /* Give LFE some basic stream_channel allocation but never exceed 1/20 of the |
| 715 | total rate for the non-energy part to avoid problems at really low rate. */ |
| 716 | lfe_offset = IMIN(bitrate/20, 3000) + 15*IMAX(50, Fs/frame_size); |
| 717 | |
| 718 | /* We give each stream (coupled or uncoupled) a starting bitrate. |
Jean-Marc Valin | d66bdc7 | 2013-05-06 16:03:39 -0400 | [diff] [blame] | 719 | This models the main saving of coupled channels over uncoupled. */ |
Jean-Marc Valin | 8ac5d89 | 2016-09-19 17:18:58 -0400 | [diff] [blame] | 720 | stream_offset = (bitrate - channel_offset*nb_normal - lfe_offset*nb_lfe)/nb_normal/2; |
| 721 | stream_offset = IMAX(0, IMIN(20000, stream_offset)); |
| 722 | |
| 723 | /* Coupled streams get twice the mono rate after the offset is allocated. */ |
Jean-Marc Valin | d66bdc7 | 2013-05-06 16:03:39 -0400 | [diff] [blame] | 724 | coupled_ratio = 512; |
| 725 | /* Should depend on the bitrate, for now we assume LFE gets 1/8 the bits of mono */ |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 726 | lfe_ratio = 32; |
| 727 | |
Jean-Marc Valin | 8ac5d89 | 2016-09-19 17:18:58 -0400 | [diff] [blame] | 728 | total = (nb_uncoupled<<8) /* mono */ |
| 729 | + coupled_ratio*nb_coupled /* stereo */ |
| 730 | + nb_lfe*lfe_ratio; |
| 731 | channel_rate = 256*(opus_int64)(bitrate - lfe_offset*nb_lfe - stream_offset*(nb_coupled+nb_uncoupled) - channel_offset*nb_normal)/total; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 732 | #ifndef FIXED_POINT |
| 733 | if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50) |
| 734 | { |
| 735 | opus_int32 bonus; |
| 736 | bonus = 60*(Fs/frame_size-50); |
| 737 | channel_rate += bonus; |
| 738 | } |
| 739 | #endif |
| 740 | |
| 741 | for (i=0;i<st->layout.nb_streams;i++) |
| 742 | { |
| 743 | if (i<st->layout.nb_coupled_streams) |
Jean-Marc Valin | 8ac5d89 | 2016-09-19 17:18:58 -0400 | [diff] [blame] | 744 | rate[i] = 2*channel_offset + IMAX(0, stream_offset+(channel_rate*coupled_ratio>>8)); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 745 | else if (i!=st->lfe_stream) |
Jean-Marc Valin | 8ac5d89 | 2016-09-19 17:18:58 -0400 | [diff] [blame] | 746 | rate[i] = channel_offset + IMAX(0, stream_offset + channel_rate); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 747 | else |
Jean-Marc Valin | 8ac5d89 | 2016-09-19 17:18:58 -0400 | [diff] [blame] | 748 | rate[i] = IMAX(0, lfe_offset+(channel_rate*lfe_ratio>>8)); |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
| 752 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 753 | static void ambisonics_rate_allocation( |
| 754 | OpusMSEncoder *st, |
| 755 | opus_int32 *rate, |
| 756 | int frame_size, |
| 757 | opus_int32 Fs |
| 758 | ) |
| 759 | { |
| 760 | int i; |
| 761 | int non_mono_rate; |
| 762 | int total_rate; |
| 763 | |
| 764 | /* The mono channel gets (rate_ratio_num / rate_ratio_den) times as many bits |
| 765 | * as all other channels */ |
| 766 | const int rate_ratio_num = 4; |
| 767 | const int rate_ratio_den = 3; |
| 768 | const int num_channels = st->layout.nb_streams; |
| 769 | |
| 770 | if (st->bitrate_bps==OPUS_AUTO) |
| 771 | { |
Michael Graczyk | fa2578b | 2016-09-07 15:26:51 -0700 | [diff] [blame] | 772 | total_rate = (st->layout.nb_coupled_streams + st->layout.nb_streams) * |
| 773 | (Fs+60*Fs/frame_size) + st->layout.nb_streams * 15000; |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 774 | } else if (st->bitrate_bps==OPUS_BITRATE_MAX) |
| 775 | { |
| 776 | total_rate = num_channels * 320000; |
| 777 | } else { |
| 778 | total_rate = st->bitrate_bps; |
| 779 | } |
| 780 | |
| 781 | /* Let y be the non-mono rate and let p, q be integers such that the mono |
| 782 | * channel rate is (p/q) * y. |
| 783 | * Also let T be the total bitrate to allocate. Then |
| 784 | * (n - 1) y + (p/q) y = T |
| 785 | * y = (T q) / (qn - q + p) |
| 786 | */ |
| 787 | non_mono_rate = |
| 788 | total_rate * rate_ratio_den |
| 789 | / (rate_ratio_den*num_channels + rate_ratio_num - rate_ratio_den); |
| 790 | |
| 791 | #ifndef FIXED_POINT |
| 792 | if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50) |
| 793 | { |
| 794 | opus_int32 bonus = 60*(Fs/frame_size-50); |
| 795 | non_mono_rate += bonus; |
| 796 | } |
| 797 | #endif |
| 798 | |
| 799 | rate[0] = total_rate - (num_channels - 1) * non_mono_rate; |
| 800 | for (i=1;i<st->layout.nb_streams;i++) |
| 801 | { |
| 802 | rate[i] = non_mono_rate; |
| 803 | } |
| 804 | } |
| 805 | #endif /* ENABLE_EXPERIMENTAL_AMBISONICS */ |
| 806 | |
| 807 | static opus_int32 rate_allocation( |
| 808 | OpusMSEncoder *st, |
| 809 | opus_int32 *rate, |
| 810 | int frame_size |
| 811 | ) |
| 812 | { |
| 813 | int i; |
| 814 | opus_int32 rate_sum=0; |
| 815 | opus_int32 Fs; |
| 816 | char *ptr; |
| 817 | |
| 818 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 819 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs)); |
| 820 | |
| 821 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 822 | if (st->mapping_type == MAPPING_TYPE_AMBISONICS) { |
| 823 | ambisonics_rate_allocation(st, rate, frame_size, Fs); |
| 824 | } else |
| 825 | #endif |
| 826 | { |
| 827 | surround_rate_allocation(st, rate, frame_size, Fs); |
| 828 | } |
| 829 | |
| 830 | for (i=0;i<st->layout.nb_streams;i++) |
| 831 | { |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 832 | rate[i] = IMAX(rate[i], 500); |
| 833 | rate_sum += rate[i]; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 834 | } |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 835 | return rate_sum; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 836 | } |
| 837 | |
Felicia Lim | 0962cbe | 2016-10-27 17:03:36 -0700 | [diff] [blame] | 838 | /* Max size in case the encoder decides to return six frames (6 x 20 ms = 120 ms) */ |
| 839 | #define MS_FRAME_TMP (6*1275+12) |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 840 | static int opus_multistream_encode_native |
| 841 | ( |
| 842 | OpusMSEncoder *st, |
| 843 | opus_copy_channel_in_func copy_channel_in, |
| 844 | const void *pcm, |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 845 | int analysis_frame_size, |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 846 | unsigned char *data, |
Jean-Marc Valin | b3eba24 | 2012-12-20 23:11:53 -0500 | [diff] [blame] | 847 | opus_int32 max_data_bytes, |
Jean-Marc Valin | 91904a4 | 2013-09-05 21:34:43 -0400 | [diff] [blame] | 848 | int lsb_depth, |
Jean-Marc Valin | c94e4bb | 2013-12-08 03:31:50 -0500 | [diff] [blame] | 849 | downmix_func downmix, |
| 850 | int float_api |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 851 | ) |
| 852 | { |
| 853 | opus_int32 Fs; |
| 854 | int coupled_size; |
| 855 | int mono_size; |
| 856 | int s; |
| 857 | char *ptr; |
| 858 | int tot_size; |
| 859 | VARDECL(opus_val16, buf); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 860 | VARDECL(opus_val16, bandSMR); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 861 | unsigned char tmp_data[MS_FRAME_TMP]; |
| 862 | OpusRepacketizer rp; |
Jean-Marc Valin | eab134c | 2013-10-14 15:01:36 -0400 | [diff] [blame] | 863 | opus_int32 vbr; |
Jean-Marc Valin | 51f4a32 | 2013-02-20 04:08:04 -0500 | [diff] [blame] | 864 | const CELTMode *celt_mode; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 865 | opus_int32 bitrates[256]; |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 866 | opus_val16 bandLogE[42]; |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 867 | opus_val32 *mem = NULL; |
| 868 | opus_val32 *preemph_mem=NULL; |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 869 | int frame_size; |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 870 | opus_int32 rate_sum; |
| 871 | opus_int32 smallest_packet; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 872 | ALLOC_STACK; |
| 873 | |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 874 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 875 | { |
| 876 | preemph_mem = ms_get_preemph_mem(st); |
| 877 | mem = ms_get_window_mem(st); |
| 878 | } |
| 879 | |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 880 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 881 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs)); |
Jean-Marc Valin | eab134c | 2013-10-14 15:01:36 -0400 | [diff] [blame] | 882 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_VBR(&vbr)); |
Jean-Marc Valin | 51f4a32 | 2013-02-20 04:08:04 -0500 | [diff] [blame] | 883 | opus_encoder_ctl((OpusEncoder*)ptr, CELT_GET_MODE(&celt_mode)); |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 884 | |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 885 | { |
| 886 | opus_int32 delay_compensation; |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 887 | |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 888 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_LOOKAHEAD(&delay_compensation)); |
| 889 | delay_compensation -= Fs/400; |
| 890 | frame_size = compute_frame_size(pcm, analysis_frame_size, |
Mark Harris | 7d0e33f | 2016-08-27 07:10:15 -0700 | [diff] [blame] | 891 | st->variable_duration, st->layout.nb_channels, Fs, st->bitrate_bps, |
Jean-Marc Valin | c2b3441 | 2013-10-28 21:48:50 -0400 | [diff] [blame] | 892 | delay_compensation, downmix |
| 893 | #ifndef DISABLE_FLOAT_API |
| 894 | , st->subframe_mem |
| 895 | #endif |
| 896 | ); |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 897 | } |
| 898 | |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 899 | if (400*frame_size < Fs) |
| 900 | { |
| 901 | RESTORE_STACK; |
| 902 | return OPUS_BAD_ARG; |
| 903 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 904 | /* Validate frame_size before using it to allocate stack space. |
| 905 | This mirrors the checks in opus_encode[_float](). */ |
Felicia Lim | 0962cbe | 2016-10-27 17:03:36 -0700 | [diff] [blame] | 906 | if (400*frame_size != Fs && 200*frame_size != Fs && |
| 907 | 100*frame_size != Fs && 50*frame_size != Fs && |
| 908 | 25*frame_size != Fs && 50*frame_size != 3*Fs && |
| 909 | 50*frame_size != 4*Fs && 50*frame_size != 5*Fs && |
| 910 | 50*frame_size != 6*Fs) |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 911 | { |
| 912 | RESTORE_STACK; |
| 913 | return OPUS_BAD_ARG; |
| 914 | } |
Jean-Marc Valin | e1326fe | 2014-09-04 01:48:46 -0400 | [diff] [blame] | 915 | |
| 916 | /* Smallest packet the encoder can produce. */ |
| 917 | smallest_packet = st->layout.nb_streams*2-1; |
Felicia Lim | bcfe0f6 | 2016-10-28 11:11:18 -0700 | [diff] [blame] | 918 | /* 100 ms needs an extra byte per stream for the ToC. */ |
| 919 | if (Fs/frame_size == 10) |
| 920 | smallest_packet += st->layout.nb_streams; |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 921 | if (max_data_bytes < smallest_packet) |
| 922 | { |
| 923 | RESTORE_STACK; |
Jean-Marc Valin | e1326fe | 2014-09-04 01:48:46 -0400 | [diff] [blame] | 924 | return OPUS_BUFFER_TOO_SMALL; |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 925 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 926 | ALLOC(buf, 2*frame_size, opus_val16); |
| 927 | coupled_size = opus_encoder_get_size(2); |
| 928 | mono_size = opus_encoder_get_size(1); |
| 929 | |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 930 | ALLOC(bandSMR, 21*st->layout.nb_channels, opus_val16); |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 931 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 932 | { |
Viswanath Puttagunta | f48abe8 | 2015-05-15 12:42:19 -0500 | [diff] [blame] | 933 | surround_analysis(celt_mode, pcm, bandSMR, mem, preemph_mem, frame_size, 120, st->layout.nb_channels, Fs, copy_channel_in, st->arch); |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 934 | } |
| 935 | |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 936 | /* Compute bitrate allocation between streams (this could be a lot better) */ |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 937 | rate_sum = rate_allocation(st, bitrates, frame_size); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 938 | |
Jean-Marc Valin | eab134c | 2013-10-14 15:01:36 -0400 | [diff] [blame] | 939 | if (!vbr) |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 940 | { |
| 941 | if (st->bitrate_bps == OPUS_AUTO) |
| 942 | { |
| 943 | max_data_bytes = IMIN(max_data_bytes, 3*rate_sum/(3*8*Fs/frame_size)); |
| 944 | } else if (st->bitrate_bps != OPUS_BITRATE_MAX) |
| 945 | { |
| 946 | max_data_bytes = IMIN(max_data_bytes, IMAX(smallest_packet, |
| 947 | 3*st->bitrate_bps/(3*8*Fs/frame_size))); |
| 948 | } |
| 949 | } |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 950 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 951 | for (s=0;s<st->layout.nb_streams;s++) |
| 952 | { |
| 953 | OpusEncoder *enc; |
| 954 | enc = (OpusEncoder*)ptr; |
| 955 | if (s < st->layout.nb_coupled_streams) |
| 956 | ptr += align(coupled_size); |
| 957 | else |
| 958 | ptr += align(mono_size); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 959 | opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrates[s])); |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 960 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 961 | { |
Jean-Marc Valin | 6fbfed6 | 2013-09-13 17:16:38 -0400 | [diff] [blame] | 962 | opus_int32 equiv_rate; |
| 963 | equiv_rate = st->bitrate_bps; |
| 964 | if (frame_size*50 < Fs) |
| 965 | equiv_rate -= 60*(Fs/frame_size - 50)*st->layout.nb_channels; |
Jean-Marc Valin | ccf7d9b | 2013-11-24 01:57:52 -0500 | [diff] [blame] | 966 | if (equiv_rate > 10000*st->layout.nb_channels) |
Jean-Marc Valin | 6fbfed6 | 2013-09-13 17:16:38 -0400 | [diff] [blame] | 967 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND)); |
Jean-Marc Valin | ccf7d9b | 2013-11-24 01:57:52 -0500 | [diff] [blame] | 968 | else if (equiv_rate > 7000*st->layout.nb_channels) |
Jean-Marc Valin | 6fbfed6 | 2013-09-13 17:16:38 -0400 | [diff] [blame] | 969 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND)); |
Jean-Marc Valin | ccf7d9b | 2013-11-24 01:57:52 -0500 | [diff] [blame] | 970 | else if (equiv_rate > 5000*st->layout.nb_channels) |
Jean-Marc Valin | 6fbfed6 | 2013-09-13 17:16:38 -0400 | [diff] [blame] | 971 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND)); |
| 972 | else |
| 973 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND)); |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 974 | if (s < st->layout.nb_coupled_streams) |
Jean-Marc Valin | 8824fdb | 2013-09-10 01:15:19 -0400 | [diff] [blame] | 975 | { |
| 976 | /* To preserve the spatial image, force stereo CELT on coupled streams */ |
| 977 | opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_CELT_ONLY)); |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 978 | opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(2)); |
Jean-Marc Valin | 8824fdb | 2013-09-10 01:15:19 -0400 | [diff] [blame] | 979 | } |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 980 | } |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 981 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 982 | else if (st->mapping_type == MAPPING_TYPE_AMBISONICS) { |
| 983 | opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_CELT_ONLY)); |
| 984 | } |
| 985 | #endif |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 989 | /* Counting ToC */ |
| 990 | tot_size = 0; |
| 991 | for (s=0;s<st->layout.nb_streams;s++) |
| 992 | { |
| 993 | OpusEncoder *enc; |
| 994 | int len; |
| 995 | int curr_max; |
Jean-Marc Valin | 91904a4 | 2013-09-05 21:34:43 -0400 | [diff] [blame] | 996 | int c1, c2; |
Jean-Marc Valin | 328953e | 2016-07-04 01:29:23 -0400 | [diff] [blame] | 997 | int ret; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 998 | |
| 999 | opus_repacketizer_init(&rp); |
| 1000 | enc = (OpusEncoder*)ptr; |
| 1001 | if (s < st->layout.nb_coupled_streams) |
| 1002 | { |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 1003 | int i; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1004 | int left, right; |
| 1005 | left = get_left_channel(&st->layout, s, -1); |
| 1006 | right = get_right_channel(&st->layout, s, -1); |
| 1007 | (*copy_channel_in)(buf, 2, |
| 1008 | pcm, st->layout.nb_channels, left, frame_size); |
| 1009 | (*copy_channel_in)(buf+1, 2, |
| 1010 | pcm, st->layout.nb_channels, right, frame_size); |
| 1011 | ptr += align(coupled_size); |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 1012 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 1013 | { |
| 1014 | for (i=0;i<21;i++) |
| 1015 | { |
| 1016 | bandLogE[i] = bandSMR[21*left+i]; |
| 1017 | bandLogE[21+i] = bandSMR[21*right+i]; |
| 1018 | } |
| 1019 | } |
Jean-Marc Valin | 91904a4 | 2013-09-05 21:34:43 -0400 | [diff] [blame] | 1020 | c1 = left; |
| 1021 | c2 = right; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1022 | } else { |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 1023 | int i; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1024 | int chan = get_mono_channel(&st->layout, s, -1); |
| 1025 | (*copy_channel_in)(buf, 1, |
| 1026 | pcm, st->layout.nb_channels, chan, frame_size); |
| 1027 | ptr += align(mono_size); |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 1028 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 1029 | { |
| 1030 | for (i=0;i<21;i++) |
| 1031 | bandLogE[i] = bandSMR[21*chan+i]; |
| 1032 | } |
Jean-Marc Valin | 91904a4 | 2013-09-05 21:34:43 -0400 | [diff] [blame] | 1033 | c1 = chan; |
| 1034 | c2 = -1; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1035 | } |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 1036 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 1037 | opus_encoder_ctl(enc, OPUS_SET_ENERGY_MASK(bandLogE)); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1038 | /* number of bytes left (+Toc) */ |
| 1039 | curr_max = max_data_bytes - tot_size; |
Jean-Marc Valin | da97db1 | 2014-09-04 02:48:21 -0400 | [diff] [blame] | 1040 | /* Reserve one byte for the last stream and two for the others */ |
Jean-Marc Valin | e1326fe | 2014-09-04 01:48:46 -0400 | [diff] [blame] | 1041 | curr_max -= IMAX(0,2*(st->layout.nb_streams-s-1)-1); |
Felicia Lim | bcfe0f6 | 2016-10-28 11:11:18 -0700 | [diff] [blame] | 1042 | /* For 100 ms, reserve an extra byte per stream for the Toc */ |
| 1043 | if (Fs/frame_size == 10) |
| 1044 | curr_max -= st->layout.nb_streams-s; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1045 | curr_max = IMIN(curr_max,MS_FRAME_TMP); |
Jean-Marc Valin | da97db1 | 2014-09-04 02:48:21 -0400 | [diff] [blame] | 1046 | /* Repacketizer will add one or two bytes for self-delimited frames */ |
Jean-Marc Valin | 648eb9a | 2014-09-04 02:44:09 -0400 | [diff] [blame] | 1047 | if (s != st->layout.nb_streams-1) curr_max -= curr_max>253 ? 2 : 1; |
Jean-Marc Valin | 2e653a7 | 2013-10-14 17:47:18 -0400 | [diff] [blame] | 1048 | if (!vbr && s == st->layout.nb_streams-1) |
| 1049 | opus_encoder_ctl(enc, OPUS_SET_BITRATE(curr_max*(8*Fs/frame_size))); |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 1050 | len = opus_encode_native(enc, buf, frame_size, tmp_data, curr_max, lsb_depth, |
Jean-Marc Valin | c94e4bb | 2013-12-08 03:31:50 -0500 | [diff] [blame] | 1051 | pcm, analysis_frame_size, c1, c2, st->layout.nb_channels, downmix, float_api); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1052 | if (len<0) |
| 1053 | { |
| 1054 | RESTORE_STACK; |
| 1055 | return len; |
| 1056 | } |
| 1057 | /* We need to use the repacketizer to add the self-delimiting lengths |
| 1058 | while taking into account the fact that the encoder can now return |
| 1059 | more than one frame at a time (e.g. 60 ms CELT-only) */ |
Jean-Marc Valin | 328953e | 2016-07-04 01:29:23 -0400 | [diff] [blame] | 1060 | ret = opus_repacketizer_cat(&rp, tmp_data, len); |
| 1061 | /* If the opus_repacketizer_cat() fails, then something's seriously wrong |
| 1062 | with the encoder. */ |
| 1063 | if (ret != OPUS_OK) |
Jean-Marc Valin | 9203565 | 2016-07-04 10:45:15 -0400 | [diff] [blame] | 1064 | { |
| 1065 | RESTORE_STACK; |
Jean-Marc Valin | 328953e | 2016-07-04 01:29:23 -0400 | [diff] [blame] | 1066 | return OPUS_INTERNAL_ERROR; |
Jean-Marc Valin | 9203565 | 2016-07-04 10:45:15 -0400 | [diff] [blame] | 1067 | } |
Jean-Marc Valin | c5635d2 | 2013-11-13 14:08:22 -0500 | [diff] [blame] | 1068 | len = opus_repacketizer_out_range_impl(&rp, 0, opus_repacketizer_get_nb_frames(&rp), |
| 1069 | data, max_data_bytes-tot_size, s != st->layout.nb_streams-1, !vbr && s == st->layout.nb_streams-1); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1070 | data += len; |
| 1071 | tot_size += len; |
| 1072 | } |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 1073 | /*printf("\n");*/ |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1074 | RESTORE_STACK; |
| 1075 | return tot_size; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1076 | } |
| 1077 | |
Pedro Becerra | 1af7f95 | 2013-07-11 00:00:47 -0400 | [diff] [blame] | 1078 | #if !defined(DISABLE_FLOAT_API) |
| 1079 | static void opus_copy_channel_in_float( |
| 1080 | opus_val16 *dst, |
| 1081 | int dst_stride, |
| 1082 | const void *src, |
| 1083 | int src_stride, |
| 1084 | int src_channel, |
| 1085 | int frame_size |
| 1086 | ) |
| 1087 | { |
| 1088 | const float *float_src; |
| 1089 | opus_int32 i; |
| 1090 | float_src = (const float *)src; |
| 1091 | for (i=0;i<frame_size;i++) |
| 1092 | #if defined(FIXED_POINT) |
| 1093 | dst[i*dst_stride] = FLOAT2INT16(float_src[i*src_stride+src_channel]); |
| 1094 | #else |
| 1095 | dst[i*dst_stride] = float_src[i*src_stride+src_channel]; |
| 1096 | #endif |
| 1097 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1098 | #endif |
| 1099 | |
| 1100 | static void opus_copy_channel_in_short( |
| 1101 | opus_val16 *dst, |
| 1102 | int dst_stride, |
| 1103 | const void *src, |
| 1104 | int src_stride, |
| 1105 | int src_channel, |
| 1106 | int frame_size |
| 1107 | ) |
| 1108 | { |
| 1109 | const opus_int16 *short_src; |
Timothy B. Terriberry | a8f04b2 | 2013-03-18 14:42:44 -0700 | [diff] [blame] | 1110 | opus_int32 i; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1111 | short_src = (const opus_int16 *)src; |
| 1112 | for (i=0;i<frame_size;i++) |
| 1113 | #if defined(FIXED_POINT) |
| 1114 | dst[i*dst_stride] = short_src[i*src_stride+src_channel]; |
| 1115 | #else |
| 1116 | dst[i*dst_stride] = (1/32768.f)*short_src[i*src_stride+src_channel]; |
| 1117 | #endif |
| 1118 | } |
| 1119 | |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 1120 | |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1121 | #ifdef FIXED_POINT |
| 1122 | int opus_multistream_encode( |
| 1123 | OpusMSEncoder *st, |
| 1124 | const opus_val16 *pcm, |
| 1125 | int frame_size, |
| 1126 | unsigned char *data, |
| 1127 | opus_int32 max_data_bytes |
| 1128 | ) |
| 1129 | { |
| 1130 | return opus_multistream_encode_native(st, opus_copy_channel_in_short, |
Jean-Marc Valin | c94e4bb | 2013-12-08 03:31:50 -0500 | [diff] [blame] | 1131 | pcm, frame_size, data, max_data_bytes, 16, downmix_int, 0); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | #ifndef DISABLE_FLOAT_API |
| 1135 | int opus_multistream_encode_float( |
| 1136 | OpusMSEncoder *st, |
| 1137 | const float *pcm, |
| 1138 | int frame_size, |
| 1139 | unsigned char *data, |
| 1140 | opus_int32 max_data_bytes |
| 1141 | ) |
| 1142 | { |
| 1143 | return opus_multistream_encode_native(st, opus_copy_channel_in_float, |
Jean-Marc Valin | c94e4bb | 2013-12-08 03:31:50 -0500 | [diff] [blame] | 1144 | pcm, frame_size, data, max_data_bytes, 16, downmix_float, 1); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1145 | } |
| 1146 | #endif |
| 1147 | |
| 1148 | #else |
| 1149 | |
| 1150 | int opus_multistream_encode_float |
| 1151 | ( |
| 1152 | OpusMSEncoder *st, |
| 1153 | const opus_val16 *pcm, |
| 1154 | int frame_size, |
| 1155 | unsigned char *data, |
| 1156 | opus_int32 max_data_bytes |
| 1157 | ) |
| 1158 | { |
| 1159 | return opus_multistream_encode_native(st, opus_copy_channel_in_float, |
Jean-Marc Valin | c94e4bb | 2013-12-08 03:31:50 -0500 | [diff] [blame] | 1160 | pcm, frame_size, data, max_data_bytes, 24, downmix_float, 1); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | int opus_multistream_encode( |
| 1164 | OpusMSEncoder *st, |
| 1165 | const opus_int16 *pcm, |
| 1166 | int frame_size, |
| 1167 | unsigned char *data, |
| 1168 | opus_int32 max_data_bytes |
| 1169 | ) |
| 1170 | { |
| 1171 | return opus_multistream_encode_native(st, opus_copy_channel_in_short, |
Jean-Marc Valin | c94e4bb | 2013-12-08 03:31:50 -0500 | [diff] [blame] | 1172 | pcm, frame_size, data, max_data_bytes, 16, downmix_int, 0); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1173 | } |
| 1174 | #endif |
| 1175 | |
| 1176 | int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...) |
| 1177 | { |
| 1178 | va_list ap; |
| 1179 | int coupled_size, mono_size; |
| 1180 | char *ptr; |
| 1181 | int ret = OPUS_OK; |
| 1182 | |
| 1183 | va_start(ap, request); |
| 1184 | |
| 1185 | coupled_size = opus_encoder_get_size(2); |
| 1186 | mono_size = opus_encoder_get_size(1); |
| 1187 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 1188 | switch (request) |
| 1189 | { |
| 1190 | case OPUS_SET_BITRATE_REQUEST: |
| 1191 | { |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1192 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | f4f5a6a | 2016-09-06 22:32:22 -0400 | [diff] [blame] | 1193 | if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX) |
Gregory Maxwell | a0d096f | 2013-06-29 20:33:32 -0700 | [diff] [blame] | 1194 | { |
Jean-Marc Valin | f4f5a6a | 2016-09-06 22:32:22 -0400 | [diff] [blame] | 1195 | if (value <= 0) |
| 1196 | goto bad_arg; |
| 1197 | value = IMIN(300000*st->layout.nb_channels, IMAX(500*st->layout.nb_channels, value)); |
Gregory Maxwell | b271dae | 2013-06-29 20:25:55 -0700 | [diff] [blame] | 1198 | } |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1199 | st->bitrate_bps = value; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1200 | } |
| 1201 | break; |
| 1202 | case OPUS_GET_BITRATE_REQUEST: |
| 1203 | { |
| 1204 | int s; |
| 1205 | opus_int32 *value = va_arg(ap, opus_int32*); |
Gregory Maxwell | a0d096f | 2013-06-29 20:33:32 -0700 | [diff] [blame] | 1206 | if (!value) |
| 1207 | { |
Gregory Maxwell | b271dae | 2013-06-29 20:25:55 -0700 | [diff] [blame] | 1208 | goto bad_arg; |
| 1209 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1210 | *value = 0; |
| 1211 | for (s=0;s<st->layout.nb_streams;s++) |
| 1212 | { |
| 1213 | opus_int32 rate; |
| 1214 | OpusEncoder *enc; |
| 1215 | enc = (OpusEncoder*)ptr; |
| 1216 | if (s < st->layout.nb_coupled_streams) |
| 1217 | ptr += align(coupled_size); |
| 1218 | else |
| 1219 | ptr += align(mono_size); |
| 1220 | opus_encoder_ctl(enc, request, &rate); |
| 1221 | *value += rate; |
| 1222 | } |
| 1223 | } |
| 1224 | break; |
| 1225 | case OPUS_GET_LSB_DEPTH_REQUEST: |
| 1226 | case OPUS_GET_VBR_REQUEST: |
| 1227 | case OPUS_GET_APPLICATION_REQUEST: |
| 1228 | case OPUS_GET_BANDWIDTH_REQUEST: |
| 1229 | case OPUS_GET_COMPLEXITY_REQUEST: |
| 1230 | case OPUS_GET_PACKET_LOSS_PERC_REQUEST: |
| 1231 | case OPUS_GET_DTX_REQUEST: |
| 1232 | case OPUS_GET_VOICE_RATIO_REQUEST: |
| 1233 | case OPUS_GET_VBR_CONSTRAINT_REQUEST: |
| 1234 | case OPUS_GET_SIGNAL_REQUEST: |
| 1235 | case OPUS_GET_LOOKAHEAD_REQUEST: |
| 1236 | case OPUS_GET_SAMPLE_RATE_REQUEST: |
| 1237 | case OPUS_GET_INBAND_FEC_REQUEST: |
| 1238 | case OPUS_GET_FORCE_CHANNELS_REQUEST: |
Jean-Marc Valin | cbe93e2 | 2013-11-15 13:50:38 -0500 | [diff] [blame] | 1239 | case OPUS_GET_PREDICTION_DISABLED_REQUEST: |
Mark Harris | 691d493 | 2016-09-03 10:42:19 -0700 | [diff] [blame] | 1240 | case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST: |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1241 | { |
| 1242 | OpusEncoder *enc; |
| 1243 | /* For int32* GET params, just query the first stream */ |
| 1244 | opus_int32 *value = va_arg(ap, opus_int32*); |
| 1245 | enc = (OpusEncoder*)ptr; |
| 1246 | ret = opus_encoder_ctl(enc, request, value); |
| 1247 | } |
| 1248 | break; |
| 1249 | case OPUS_GET_FINAL_RANGE_REQUEST: |
| 1250 | { |
| 1251 | int s; |
| 1252 | opus_uint32 *value = va_arg(ap, opus_uint32*); |
| 1253 | opus_uint32 tmp; |
Gregory Maxwell | a0d096f | 2013-06-29 20:33:32 -0700 | [diff] [blame] | 1254 | if (!value) |
| 1255 | { |
Gregory Maxwell | b271dae | 2013-06-29 20:25:55 -0700 | [diff] [blame] | 1256 | goto bad_arg; |
| 1257 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1258 | *value=0; |
| 1259 | for (s=0;s<st->layout.nb_streams;s++) |
| 1260 | { |
| 1261 | OpusEncoder *enc; |
| 1262 | enc = (OpusEncoder*)ptr; |
| 1263 | if (s < st->layout.nb_coupled_streams) |
| 1264 | ptr += align(coupled_size); |
| 1265 | else |
| 1266 | ptr += align(mono_size); |
| 1267 | ret = opus_encoder_ctl(enc, request, &tmp); |
| 1268 | if (ret != OPUS_OK) break; |
| 1269 | *value ^= tmp; |
| 1270 | } |
| 1271 | } |
| 1272 | break; |
| 1273 | case OPUS_SET_LSB_DEPTH_REQUEST: |
| 1274 | case OPUS_SET_COMPLEXITY_REQUEST: |
| 1275 | case OPUS_SET_VBR_REQUEST: |
| 1276 | case OPUS_SET_VBR_CONSTRAINT_REQUEST: |
Daniel Jensen | c0c0ef6 | 2013-07-22 16:31:31 -0600 | [diff] [blame] | 1277 | case OPUS_SET_MAX_BANDWIDTH_REQUEST: |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1278 | case OPUS_SET_BANDWIDTH_REQUEST: |
| 1279 | case OPUS_SET_SIGNAL_REQUEST: |
| 1280 | case OPUS_SET_APPLICATION_REQUEST: |
| 1281 | case OPUS_SET_INBAND_FEC_REQUEST: |
| 1282 | case OPUS_SET_PACKET_LOSS_PERC_REQUEST: |
| 1283 | case OPUS_SET_DTX_REQUEST: |
| 1284 | case OPUS_SET_FORCE_MODE_REQUEST: |
| 1285 | case OPUS_SET_FORCE_CHANNELS_REQUEST: |
Jean-Marc Valin | cbe93e2 | 2013-11-15 13:50:38 -0500 | [diff] [blame] | 1286 | case OPUS_SET_PREDICTION_DISABLED_REQUEST: |
Mark Harris | 691d493 | 2016-09-03 10:42:19 -0700 | [diff] [blame] | 1287 | case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST: |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1288 | { |
| 1289 | int s; |
| 1290 | /* This works for int32 params */ |
| 1291 | opus_int32 value = va_arg(ap, opus_int32); |
| 1292 | for (s=0;s<st->layout.nb_streams;s++) |
| 1293 | { |
| 1294 | OpusEncoder *enc; |
| 1295 | |
| 1296 | enc = (OpusEncoder*)ptr; |
| 1297 | if (s < st->layout.nb_coupled_streams) |
| 1298 | ptr += align(coupled_size); |
| 1299 | else |
| 1300 | ptr += align(mono_size); |
| 1301 | ret = opus_encoder_ctl(enc, request, value); |
| 1302 | if (ret != OPUS_OK) |
| 1303 | break; |
| 1304 | } |
| 1305 | } |
| 1306 | break; |
| 1307 | case OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST: |
| 1308 | { |
| 1309 | int s; |
| 1310 | opus_int32 stream_id; |
| 1311 | OpusEncoder **value; |
| 1312 | stream_id = va_arg(ap, opus_int32); |
| 1313 | if (stream_id<0 || stream_id >= st->layout.nb_streams) |
| 1314 | ret = OPUS_BAD_ARG; |
| 1315 | value = va_arg(ap, OpusEncoder**); |
Gregory Maxwell | a0d096f | 2013-06-29 20:33:32 -0700 | [diff] [blame] | 1316 | if (!value) |
| 1317 | { |
Gregory Maxwell | b271dae | 2013-06-29 20:25:55 -0700 | [diff] [blame] | 1318 | goto bad_arg; |
| 1319 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1320 | for (s=0;s<stream_id;s++) |
| 1321 | { |
| 1322 | if (s < st->layout.nb_coupled_streams) |
| 1323 | ptr += align(coupled_size); |
| 1324 | else |
| 1325 | ptr += align(mono_size); |
| 1326 | } |
| 1327 | *value = (OpusEncoder*)ptr; |
| 1328 | } |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1329 | break; |
Jean-Marc Valin | 51f4a32 | 2013-02-20 04:08:04 -0500 | [diff] [blame] | 1330 | case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1331 | { |
| 1332 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1333 | st->variable_duration = value; |
| 1334 | } |
| 1335 | break; |
Jean-Marc Valin | 51f4a32 | 2013-02-20 04:08:04 -0500 | [diff] [blame] | 1336 | case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST: |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1337 | { |
| 1338 | opus_int32 *value = va_arg(ap, opus_int32*); |
Gregory Maxwell | a0d096f | 2013-06-29 20:33:32 -0700 | [diff] [blame] | 1339 | if (!value) |
| 1340 | { |
Gregory Maxwell | b271dae | 2013-06-29 20:25:55 -0700 | [diff] [blame] | 1341 | goto bad_arg; |
| 1342 | } |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1343 | *value = st->variable_duration; |
| 1344 | } |
| 1345 | break; |
Jean-Marc Valin | 811db62 | 2013-10-28 16:11:53 -0400 | [diff] [blame] | 1346 | case OPUS_RESET_STATE: |
| 1347 | { |
| 1348 | int s; |
| 1349 | st->subframe_mem[0] = st->subframe_mem[1] = st->subframe_mem[2] = 0; |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 1350 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | 811db62 | 2013-10-28 16:11:53 -0400 | [diff] [blame] | 1351 | { |
| 1352 | OPUS_CLEAR(ms_get_preemph_mem(st), st->layout.nb_channels); |
| 1353 | OPUS_CLEAR(ms_get_window_mem(st), st->layout.nb_channels*120); |
| 1354 | } |
| 1355 | for (s=0;s<st->layout.nb_streams;s++) |
| 1356 | { |
| 1357 | OpusEncoder *enc; |
| 1358 | enc = (OpusEncoder*)ptr; |
| 1359 | if (s < st->layout.nb_coupled_streams) |
| 1360 | ptr += align(coupled_size); |
| 1361 | else |
| 1362 | ptr += align(mono_size); |
| 1363 | ret = opus_encoder_ctl(enc, OPUS_RESET_STATE); |
| 1364 | if (ret != OPUS_OK) |
| 1365 | break; |
| 1366 | } |
| 1367 | } |
| 1368 | break; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1369 | default: |
| 1370 | ret = OPUS_UNIMPLEMENTED; |
| 1371 | break; |
| 1372 | } |
| 1373 | |
| 1374 | va_end(ap); |
| 1375 | return ret; |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1376 | bad_arg: |
| 1377 | va_end(ap); |
| 1378 | return OPUS_BAD_ARG; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | void opus_multistream_encoder_destroy(OpusMSEncoder *st) |
| 1382 | { |
| 1383 | opus_free(st); |
| 1384 | } |