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 | 942fc81 | 2013-10-01 19:27:30 -0400 | [diff] [blame] | 243 | opus_val16 channel_offset; |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 244 | opus_val32 bandE[21]; |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 245 | opus_val16 maskLogE[3][21]; |
| 246 | VARDECL(opus_val32, in); |
| 247 | VARDECL(opus_val16, x); |
Jean-Marc Valin | e8e5ecb | 2013-10-01 17:16:33 -0400 | [diff] [blame] | 248 | VARDECL(opus_val32, freq); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 249 | SAVE_STACK; |
Jean-Marc Valin | 978e4cb | 2013-09-11 00:51:22 -0400 | [diff] [blame] | 250 | |
| 251 | upsample = resampling_factor(rate); |
| 252 | frame_size = len*upsample; |
| 253 | |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 254 | /* LM = log2(frame_size / 120) */ |
Jean-Marc Valin | 2e653a7 | 2013-10-14 17:47:18 -0400 | [diff] [blame] | 255 | for (LM=0;LM<celt_mode->maxLM;LM++) |
Jean-Marc Valin | 54bddf0 | 2013-09-11 23:34:51 -0400 | [diff] [blame] | 256 | if (celt_mode->shortMdctSize<<LM==frame_size) |
| 257 | break; |
| 258 | |
Jean-Marc Valin | 978e4cb | 2013-09-11 00:51:22 -0400 | [diff] [blame] | 259 | ALLOC(in, frame_size+overlap, opus_val32); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 260 | ALLOC(x, len, opus_val16); |
Jean-Marc Valin | 978e4cb | 2013-09-11 00:51:22 -0400 | [diff] [blame] | 261 | ALLOC(freq, frame_size, opus_val32); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 262 | |
| 263 | channel_pos(channels, pos); |
| 264 | |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 265 | for (c=0;c<3;c++) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 266 | for (i=0;i<21;i++) |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 267 | maskLogE[c][i] = -QCONST16(28.f, DB_SHIFT); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 268 | |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 269 | for (c=0;c<channels;c++) |
| 270 | { |
| 271 | OPUS_COPY(in, mem+c*overlap, overlap); |
| 272 | (*copy_channel_in)(x, 1, pcm, channels, c, len); |
Jean-Marc Valin | 2dc27df | 2013-11-13 19:35:43 -0500 | [diff] [blame] | 273 | 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] | 274 | #ifndef FIXED_POINT |
| 275 | { |
| 276 | opus_val32 sum; |
| 277 | sum = celt_inner_prod(in, in, frame_size+overlap, 0); |
| 278 | /* This should filter out both NaNs and ridiculous signals that could |
| 279 | cause NaNs further down. */ |
| 280 | if (!(sum < 1e9f) || celt_isnan(sum)) |
| 281 | { |
| 282 | OPUS_CLEAR(in, frame_size+overlap); |
| 283 | preemph_mem[c] = 0; |
| 284 | } |
| 285 | } |
| 286 | #endif |
Viswanath Puttagunta | f48abe8 | 2015-05-15 12:42:19 -0500 | [diff] [blame] | 287 | clt_mdct_forward(&celt_mode->mdct, in, freq, celt_mode->window, |
| 288 | overlap, celt_mode->maxLM-LM, 1, arch); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 289 | if (upsample != 1) |
| 290 | { |
Jean-Marc Valin | 978e4cb | 2013-09-11 00:51:22 -0400 | [diff] [blame] | 291 | int bound = len; |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 292 | for (i=0;i<bound;i++) |
| 293 | freq[i] *= upsample; |
Jean-Marc Valin | 978e4cb | 2013-09-11 00:51:22 -0400 | [diff] [blame] | 294 | for (;i<frame_size;i++) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 295 | freq[i] = 0; |
| 296 | } |
| 297 | |
Mark Harris | 58107d8 | 2014-01-25 16:32:18 -0800 | [diff] [blame] | 298 | compute_band_energies(celt_mode, freq, bandE, 21, 1, LM); |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 299 | amp2Log2(celt_mode, 21, 21, bandE, bandLogE+21*c, 1); |
Jean-Marc Valin | 0f68696 | 2013-09-05 12:49:55 -0400 | [diff] [blame] | 300 | /* Apply spreading function with -6 dB/band going up and -12 dB/band going down. */ |
| 301 | for (i=1;i<21;i++) |
| 302 | bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i-1]-QCONST16(1.f, DB_SHIFT)); |
| 303 | for (i=19;i>=0;i--) |
| 304 | 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] | 305 | if (pos[c]==1) |
| 306 | { |
| 307 | for (i=0;i<21;i++) |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 308 | maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 309 | } else if (pos[c]==3) |
| 310 | { |
| 311 | for (i=0;i<21;i++) |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 312 | maskLogE[2][i] = logSum(maskLogE[2][i], bandLogE[21*c+i]); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 313 | } else if (pos[c]==2) |
| 314 | { |
| 315 | for (i=0;i<21;i++) |
| 316 | { |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 317 | maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]-QCONST16(.5f, DB_SHIFT)); |
| 318 | 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] | 319 | } |
| 320 | } |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 321 | #if 0 |
| 322 | for (i=0;i<21;i++) |
| 323 | printf("%f ", bandLogE[21*c+i]); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 324 | float sum=0; |
| 325 | for (i=0;i<21;i++) |
| 326 | sum += bandLogE[21*c+i]; |
| 327 | printf("%f ", sum/21); |
| 328 | #endif |
Jean-Marc Valin | 978e4cb | 2013-09-11 00:51:22 -0400 | [diff] [blame] | 329 | OPUS_COPY(mem+c*overlap, in+frame_size, overlap); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 330 | } |
| 331 | for (i=0;i<21;i++) |
Jean-Marc Valin | a714ae9 | 2013-08-31 02:05:32 -0400 | [diff] [blame] | 332 | maskLogE[1][i] = MIN32(maskLogE[0][i],maskLogE[2][i]); |
Jean-Marc Valin | 942fc81 | 2013-10-01 19:27:30 -0400 | [diff] [blame] | 333 | channel_offset = HALF16(celt_log2(QCONST32(2.f,14)/(channels-1))); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 334 | for (c=0;c<3;c++) |
| 335 | for (i=0;i<21;i++) |
Jean-Marc Valin | 942fc81 | 2013-10-01 19:27:30 -0400 | [diff] [blame] | 336 | maskLogE[c][i] += channel_offset; |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 337 | #if 0 |
| 338 | for (c=0;c<3;c++) |
| 339 | { |
| 340 | for (i=0;i<21;i++) |
| 341 | printf("%f ", maskLogE[c][i]); |
| 342 | } |
| 343 | #endif |
| 344 | for (c=0;c<channels;c++) |
| 345 | { |
| 346 | opus_val16 *mask; |
| 347 | if (pos[c]!=0) |
| 348 | { |
| 349 | mask = &maskLogE[pos[c]-1][0]; |
| 350 | for (i=0;i<21;i++) |
| 351 | bandLogE[21*c+i] = bandLogE[21*c+i] - mask[i]; |
| 352 | } else { |
| 353 | for (i=0;i<21;i++) |
| 354 | bandLogE[21*c+i] = 0; |
| 355 | } |
| 356 | #if 0 |
| 357 | for (i=0;i<21;i++) |
| 358 | printf("%f ", bandLogE[21*c+i]); |
| 359 | printf("\n"); |
| 360 | #endif |
| 361 | #if 0 |
| 362 | float sum=0; |
| 363 | for (i=0;i<21;i++) |
| 364 | sum += bandLogE[21*c+i]; |
Jean-Marc Valin | 942fc81 | 2013-10-01 19:27:30 -0400 | [diff] [blame] | 365 | printf("%f ", sum/(float)QCONST32(21.f, DB_SHIFT)); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 366 | printf("\n"); |
| 367 | #endif |
| 368 | } |
| 369 | RESTORE_STACK; |
| 370 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 371 | |
| 372 | opus_int32 opus_multistream_encoder_get_size(int nb_streams, int nb_coupled_streams) |
| 373 | { |
| 374 | int coupled_size; |
| 375 | int mono_size; |
| 376 | |
| 377 | if(nb_streams<1||nb_coupled_streams>nb_streams||nb_coupled_streams<0)return 0; |
| 378 | coupled_size = opus_encoder_get_size(2); |
| 379 | mono_size = opus_encoder_get_size(1); |
| 380 | return align(sizeof(OpusMSEncoder)) |
| 381 | + nb_coupled_streams * align(coupled_size) |
| 382 | + (nb_streams-nb_coupled_streams) * align(mono_size); |
| 383 | } |
| 384 | |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 385 | opus_int32 opus_multistream_surround_encoder_get_size(int channels, int mapping_family) |
| 386 | { |
| 387 | int nb_streams; |
| 388 | int nb_coupled_streams; |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 389 | opus_int32 size; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 390 | |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 391 | if (mapping_family==0) |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 392 | { |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 393 | if (channels==1) |
| 394 | { |
| 395 | nb_streams=1; |
| 396 | nb_coupled_streams=0; |
| 397 | } else if (channels==2) |
| 398 | { |
| 399 | nb_streams=1; |
| 400 | nb_coupled_streams=1; |
| 401 | } else |
| 402 | return 0; |
| 403 | } else if (mapping_family==1 && channels<=8 && channels>=1) |
| 404 | { |
| 405 | nb_streams=vorbis_mappings[channels-1].nb_streams; |
| 406 | nb_coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams; |
| 407 | } else if (mapping_family==255) |
| 408 | { |
| 409 | nb_streams=channels; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 410 | nb_coupled_streams=0; |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 411 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 412 | } else if (mapping_family==254) |
| 413 | { |
| 414 | nb_streams=channels; |
| 415 | nb_coupled_streams=0; |
| 416 | #endif |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 417 | } else |
| 418 | return 0; |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 419 | size = opus_multistream_encoder_get_size(nb_streams, nb_coupled_streams); |
| 420 | if (channels>2) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 421 | { |
| 422 | size += channels*(120*sizeof(opus_val32) + sizeof(opus_val32)); |
| 423 | } |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 424 | return size; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 425 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 426 | |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 427 | static int opus_multistream_encoder_init_impl( |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 428 | OpusMSEncoder *st, |
| 429 | opus_int32 Fs, |
| 430 | int channels, |
| 431 | int streams, |
| 432 | int coupled_streams, |
| 433 | const unsigned char *mapping, |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 434 | int application, |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 435 | MappingType mapping_type |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 436 | ) |
| 437 | { |
| 438 | int coupled_size; |
| 439 | int mono_size; |
| 440 | int i, ret; |
| 441 | char *ptr; |
| 442 | |
| 443 | if ((channels>255) || (channels<1) || (coupled_streams>streams) || |
Mark Harris | 25b27a9 | 2014-11-27 08:48:09 -0800 | [diff] [blame] | 444 | (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams)) |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 445 | return OPUS_BAD_ARG; |
| 446 | |
Viswanath Puttagunta | f48abe8 | 2015-05-15 12:42:19 -0500 | [diff] [blame] | 447 | st->arch = opus_select_arch(); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 448 | st->layout.nb_channels = channels; |
| 449 | st->layout.nb_streams = streams; |
| 450 | st->layout.nb_coupled_streams = coupled_streams; |
Jean-Marc Valin | b042935 | 2013-05-05 02:22:06 -0400 | [diff] [blame] | 451 | 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] | 452 | if (mapping_type != MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 453 | st->lfe_stream = -1; |
Jean-Marc Valin | 95561be | 2012-12-17 17:54:01 -0500 | [diff] [blame] | 454 | st->bitrate_bps = OPUS_AUTO; |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 455 | st->application = application; |
Jean-Marc Valin | 51f4a32 | 2013-02-20 04:08:04 -0500 | [diff] [blame] | 456 | st->variable_duration = OPUS_FRAMESIZE_ARG; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 457 | for (i=0;i<st->layout.nb_channels;i++) |
| 458 | st->layout.mapping[i] = mapping[i]; |
| 459 | if (!validate_layout(&st->layout) || !validate_encoder_layout(&st->layout)) |
| 460 | return OPUS_BAD_ARG; |
| 461 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 462 | coupled_size = opus_encoder_get_size(2); |
| 463 | mono_size = opus_encoder_get_size(1); |
| 464 | |
| 465 | for (i=0;i<st->layout.nb_coupled_streams;i++) |
| 466 | { |
| 467 | ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 2, application); |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 468 | if(ret!=OPUS_OK)return ret; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 469 | if (i==st->lfe_stream) |
| 470 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1)); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 471 | ptr += align(coupled_size); |
| 472 | } |
| 473 | for (;i<st->layout.nb_streams;i++) |
| 474 | { |
| 475 | ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 1, application); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 476 | if (i==st->lfe_stream) |
| 477 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1)); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 478 | if(ret!=OPUS_OK)return ret; |
| 479 | ptr += align(mono_size); |
| 480 | } |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 481 | if (mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 482 | { |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 483 | OPUS_CLEAR(ms_get_preemph_mem(st), channels); |
| 484 | OPUS_CLEAR(ms_get_window_mem(st), channels*120); |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 485 | } |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 486 | st->mapping_type = mapping_type; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 487 | return OPUS_OK; |
| 488 | } |
| 489 | |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 490 | int opus_multistream_encoder_init( |
| 491 | OpusMSEncoder *st, |
| 492 | opus_int32 Fs, |
| 493 | int channels, |
| 494 | int streams, |
| 495 | int coupled_streams, |
| 496 | const unsigned char *mapping, |
| 497 | int application |
| 498 | ) |
| 499 | { |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 500 | return opus_multistream_encoder_init_impl(st, Fs, channels, streams, |
| 501 | coupled_streams, mapping, |
| 502 | application, MAPPING_TYPE_NONE); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | int opus_multistream_surround_encoder_init( |
| 506 | OpusMSEncoder *st, |
| 507 | opus_int32 Fs, |
| 508 | int channels, |
| 509 | int mapping_family, |
| 510 | int *streams, |
| 511 | int *coupled_streams, |
| 512 | unsigned char *mapping, |
| 513 | int application |
| 514 | ) |
| 515 | { |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 516 | MappingType mapping_type; |
| 517 | |
Jean-Marc Valin | 337f34c | 2013-07-01 16:17:01 -0400 | [diff] [blame] | 518 | if ((channels>255) || (channels<1)) |
| 519 | return OPUS_BAD_ARG; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 520 | st->lfe_stream = -1; |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 521 | if (mapping_family==0) |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 522 | { |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 523 | if (channels==1) |
| 524 | { |
| 525 | *streams=1; |
| 526 | *coupled_streams=0; |
| 527 | mapping[0]=0; |
| 528 | } else if (channels==2) |
| 529 | { |
| 530 | *streams=1; |
| 531 | *coupled_streams=1; |
| 532 | mapping[0]=0; |
| 533 | mapping[1]=1; |
| 534 | } else |
| 535 | return OPUS_UNIMPLEMENTED; |
| 536 | } else if (mapping_family==1 && channels<=8 && channels>=1) |
| 537 | { |
| 538 | int i; |
| 539 | *streams=vorbis_mappings[channels-1].nb_streams; |
| 540 | *coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams; |
| 541 | for (i=0;i<channels;i++) |
| 542 | mapping[i] = vorbis_mappings[channels-1].mapping[i]; |
| 543 | if (channels>=6) |
| 544 | st->lfe_stream = *streams-1; |
| 545 | } else if (mapping_family==255) |
| 546 | { |
| 547 | int i; |
| 548 | *streams=channels; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 549 | *coupled_streams=0; |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 550 | for(i=0;i<channels;i++) |
| 551 | mapping[i] = i; |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 552 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 553 | } else if (mapping_family==254) |
| 554 | { |
| 555 | int i; |
| 556 | *streams=channels; |
| 557 | *coupled_streams=0; |
| 558 | for(i=0;i<channels;i++) |
| 559 | mapping[i] = i; |
| 560 | #endif |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 561 | } else |
Jean-Marc Valin | 7a8b139 | 2013-04-29 18:32:27 -0400 | [diff] [blame] | 562 | return OPUS_UNIMPLEMENTED; |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 563 | |
| 564 | if (channels>2 && mapping_family==1) { |
| 565 | mapping_type = MAPPING_TYPE_SURROUND; |
| 566 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 567 | } else if (mapping_family==254) |
| 568 | { |
| 569 | mapping_type = MAPPING_TYPE_AMBISONICS; |
| 570 | #endif |
| 571 | } else |
| 572 | { |
| 573 | mapping_type = MAPPING_TYPE_NONE; |
| 574 | } |
| 575 | return opus_multistream_encoder_init_impl(st, Fs, channels, *streams, |
| 576 | *coupled_streams, mapping, |
| 577 | application, mapping_type); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 578 | } |
| 579 | |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 580 | OpusMSEncoder *opus_multistream_encoder_create( |
| 581 | opus_int32 Fs, |
| 582 | int channels, |
| 583 | int streams, |
| 584 | int coupled_streams, |
| 585 | const unsigned char *mapping, |
| 586 | int application, |
| 587 | int *error |
| 588 | ) |
| 589 | { |
| 590 | int ret; |
| 591 | OpusMSEncoder *st; |
| 592 | if ((channels>255) || (channels<1) || (coupled_streams>streams) || |
Mark Harris | 25b27a9 | 2014-11-27 08:48:09 -0800 | [diff] [blame] | 593 | (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams)) |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 594 | { |
| 595 | if (error) |
| 596 | *error = OPUS_BAD_ARG; |
| 597 | return NULL; |
| 598 | } |
| 599 | st = (OpusMSEncoder *)opus_alloc(opus_multistream_encoder_get_size(streams, coupled_streams)); |
| 600 | if (st==NULL) |
| 601 | { |
| 602 | if (error) |
| 603 | *error = OPUS_ALLOC_FAIL; |
| 604 | return NULL; |
| 605 | } |
| 606 | ret = opus_multistream_encoder_init(st, Fs, channels, streams, coupled_streams, mapping, application); |
| 607 | if (ret != OPUS_OK) |
| 608 | { |
| 609 | opus_free(st); |
| 610 | st = NULL; |
| 611 | } |
| 612 | if (error) |
| 613 | *error = ret; |
| 614 | return st; |
| 615 | } |
| 616 | |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 617 | OpusMSEncoder *opus_multistream_surround_encoder_create( |
| 618 | opus_int32 Fs, |
| 619 | int channels, |
| 620 | int mapping_family, |
| 621 | int *streams, |
| 622 | int *coupled_streams, |
| 623 | unsigned char *mapping, |
| 624 | int application, |
| 625 | int *error |
| 626 | ) |
| 627 | { |
| 628 | int ret; |
Mark Harris | 25b27a9 | 2014-11-27 08:48:09 -0800 | [diff] [blame] | 629 | opus_int32 size; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 630 | OpusMSEncoder *st; |
| 631 | if ((channels>255) || (channels<1)) |
| 632 | { |
| 633 | if (error) |
| 634 | *error = OPUS_BAD_ARG; |
| 635 | return NULL; |
| 636 | } |
Mark Harris | 25b27a9 | 2014-11-27 08:48:09 -0800 | [diff] [blame] | 637 | size = opus_multistream_surround_encoder_get_size(channels, mapping_family); |
| 638 | if (!size) |
| 639 | { |
| 640 | if (error) |
| 641 | *error = OPUS_UNIMPLEMENTED; |
| 642 | return NULL; |
| 643 | } |
| 644 | st = (OpusMSEncoder *)opus_alloc(size); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 645 | if (st==NULL) |
| 646 | { |
| 647 | if (error) |
| 648 | *error = OPUS_ALLOC_FAIL; |
| 649 | return NULL; |
| 650 | } |
| 651 | ret = opus_multistream_surround_encoder_init(st, Fs, channels, mapping_family, streams, coupled_streams, mapping, application); |
| 652 | if (ret != OPUS_OK) |
| 653 | { |
| 654 | opus_free(st); |
| 655 | st = NULL; |
| 656 | } |
| 657 | if (error) |
| 658 | *error = ret; |
| 659 | return st; |
| 660 | } |
| 661 | |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 662 | static void surround_rate_allocation( |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 663 | OpusMSEncoder *st, |
| 664 | opus_int32 *rate, |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 665 | int frame_size, |
| 666 | opus_int32 Fs |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 667 | ) |
| 668 | { |
| 669 | int i; |
| 670 | opus_int32 channel_rate; |
Jean-Marc Valin | d66bdc7 | 2013-05-06 16:03:39 -0400 | [diff] [blame] | 671 | int stream_offset; |
| 672 | int lfe_offset; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 673 | int coupled_ratio; /* Q8 */ |
| 674 | int lfe_ratio; /* Q8 */ |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 675 | |
Jean-Marc Valin | 4958751 | 2013-07-07 02:50:18 -0400 | [diff] [blame] | 676 | if (st->bitrate_bps > st->layout.nb_channels*40000) |
| 677 | stream_offset = 20000; |
| 678 | else |
| 679 | stream_offset = st->bitrate_bps/st->layout.nb_channels/2; |
Jean-Marc Valin | 87ca6c0 | 2013-11-13 22:58:10 -0500 | [diff] [blame] | 680 | stream_offset += 60*(Fs/frame_size-50); |
Jean-Marc Valin | d66bdc7 | 2013-05-06 16:03:39 -0400 | [diff] [blame] | 681 | /* We start by giving each stream (coupled or uncoupled) the same bitrate. |
| 682 | This models the main saving of coupled channels over uncoupled. */ |
Jean-Marc Valin | d66bdc7 | 2013-05-06 16:03:39 -0400 | [diff] [blame] | 683 | /* The LFE stream is an exception to the above and gets fewer bits. */ |
Jean-Marc Valin | 87ca6c0 | 2013-11-13 22:58:10 -0500 | [diff] [blame] | 684 | lfe_offset = 3500 + 60*(Fs/frame_size-50); |
Jean-Marc Valin | d66bdc7 | 2013-05-06 16:03:39 -0400 | [diff] [blame] | 685 | /* Coupled streams get twice the mono rate after the first 20 kb/s. */ |
| 686 | coupled_ratio = 512; |
| 687 | /* 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] | 688 | lfe_ratio = 32; |
| 689 | |
| 690 | /* Compute bitrate allocation between streams */ |
| 691 | if (st->bitrate_bps==OPUS_AUTO) |
| 692 | { |
| 693 | channel_rate = Fs+60*Fs/frame_size; |
| 694 | } else if (st->bitrate_bps==OPUS_BITRATE_MAX) |
| 695 | { |
| 696 | channel_rate = 300000; |
| 697 | } else { |
Jean-Marc Valin | d66bdc7 | 2013-05-06 16:03:39 -0400 | [diff] [blame] | 698 | int nb_lfe; |
| 699 | int nb_uncoupled; |
| 700 | int nb_coupled; |
| 701 | int total; |
| 702 | nb_lfe = (st->lfe_stream!=-1); |
| 703 | nb_coupled = st->layout.nb_coupled_streams; |
| 704 | nb_uncoupled = st->layout.nb_streams-nb_coupled-nb_lfe; |
| 705 | total = (nb_uncoupled<<8) /* mono */ |
| 706 | + coupled_ratio*nb_coupled /* stereo */ |
| 707 | + nb_lfe*lfe_ratio; |
Jean-Marc Valin | f4f5a6a | 2016-09-06 22:32:22 -0400 | [diff] [blame] | 708 | channel_rate = 256*(opus_int64)(st->bitrate_bps-lfe_offset*nb_lfe-stream_offset*(nb_coupled+nb_uncoupled))/total; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 709 | } |
| 710 | #ifndef FIXED_POINT |
| 711 | if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50) |
| 712 | { |
| 713 | opus_int32 bonus; |
| 714 | bonus = 60*(Fs/frame_size-50); |
| 715 | channel_rate += bonus; |
| 716 | } |
| 717 | #endif |
| 718 | |
| 719 | for (i=0;i<st->layout.nb_streams;i++) |
| 720 | { |
| 721 | if (i<st->layout.nb_coupled_streams) |
Jean-Marc Valin | d66bdc7 | 2013-05-06 16:03:39 -0400 | [diff] [blame] | 722 | rate[i] = stream_offset+(channel_rate*coupled_ratio>>8); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 723 | else if (i!=st->lfe_stream) |
Jean-Marc Valin | d66bdc7 | 2013-05-06 16:03:39 -0400 | [diff] [blame] | 724 | rate[i] = stream_offset+channel_rate; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 725 | else |
Jean-Marc Valin | d66bdc7 | 2013-05-06 16:03:39 -0400 | [diff] [blame] | 726 | rate[i] = lfe_offset+(channel_rate*lfe_ratio>>8); |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
| 730 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 731 | static void ambisonics_rate_allocation( |
| 732 | OpusMSEncoder *st, |
| 733 | opus_int32 *rate, |
| 734 | int frame_size, |
| 735 | opus_int32 Fs |
| 736 | ) |
| 737 | { |
| 738 | int i; |
| 739 | int non_mono_rate; |
| 740 | int total_rate; |
| 741 | |
| 742 | /* The mono channel gets (rate_ratio_num / rate_ratio_den) times as many bits |
| 743 | * as all other channels */ |
| 744 | const int rate_ratio_num = 4; |
| 745 | const int rate_ratio_den = 3; |
| 746 | const int num_channels = st->layout.nb_streams; |
| 747 | |
| 748 | if (st->bitrate_bps==OPUS_AUTO) |
| 749 | { |
| 750 | total_rate = num_channels * (20000 + st->layout.nb_streams*(Fs+60*Fs/frame_size)); |
| 751 | } else if (st->bitrate_bps==OPUS_BITRATE_MAX) |
| 752 | { |
| 753 | total_rate = num_channels * 320000; |
| 754 | } else { |
| 755 | total_rate = st->bitrate_bps; |
| 756 | } |
| 757 | |
| 758 | /* Let y be the non-mono rate and let p, q be integers such that the mono |
| 759 | * channel rate is (p/q) * y. |
| 760 | * Also let T be the total bitrate to allocate. Then |
| 761 | * (n - 1) y + (p/q) y = T |
| 762 | * y = (T q) / (qn - q + p) |
| 763 | */ |
| 764 | non_mono_rate = |
| 765 | total_rate * rate_ratio_den |
| 766 | / (rate_ratio_den*num_channels + rate_ratio_num - rate_ratio_den); |
| 767 | |
| 768 | #ifndef FIXED_POINT |
| 769 | if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50) |
| 770 | { |
| 771 | opus_int32 bonus = 60*(Fs/frame_size-50); |
| 772 | non_mono_rate += bonus; |
| 773 | } |
| 774 | #endif |
| 775 | |
| 776 | rate[0] = total_rate - (num_channels - 1) * non_mono_rate; |
| 777 | for (i=1;i<st->layout.nb_streams;i++) |
| 778 | { |
| 779 | rate[i] = non_mono_rate; |
| 780 | } |
| 781 | } |
| 782 | #endif /* ENABLE_EXPERIMENTAL_AMBISONICS */ |
| 783 | |
| 784 | static opus_int32 rate_allocation( |
| 785 | OpusMSEncoder *st, |
| 786 | opus_int32 *rate, |
| 787 | int frame_size |
| 788 | ) |
| 789 | { |
| 790 | int i; |
| 791 | opus_int32 rate_sum=0; |
| 792 | opus_int32 Fs; |
| 793 | char *ptr; |
| 794 | |
| 795 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 796 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs)); |
| 797 | |
| 798 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 799 | if (st->mapping_type == MAPPING_TYPE_AMBISONICS) { |
| 800 | ambisonics_rate_allocation(st, rate, frame_size, Fs); |
| 801 | } else |
| 802 | #endif |
| 803 | { |
| 804 | surround_rate_allocation(st, rate, frame_size, Fs); |
| 805 | } |
| 806 | |
| 807 | for (i=0;i<st->layout.nb_streams;i++) |
| 808 | { |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 809 | rate[i] = IMAX(rate[i], 500); |
| 810 | rate_sum += rate[i]; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 811 | } |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 812 | return rate_sum; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 813 | } |
| 814 | |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 815 | /* Max size in case the encoder decides to return three frames */ |
| 816 | #define MS_FRAME_TMP (3*1275+7) |
| 817 | static int opus_multistream_encode_native |
| 818 | ( |
| 819 | OpusMSEncoder *st, |
| 820 | opus_copy_channel_in_func copy_channel_in, |
| 821 | const void *pcm, |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 822 | int analysis_frame_size, |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 823 | unsigned char *data, |
Jean-Marc Valin | b3eba24 | 2012-12-20 23:11:53 -0500 | [diff] [blame] | 824 | opus_int32 max_data_bytes, |
Jean-Marc Valin | 91904a4 | 2013-09-05 21:34:43 -0400 | [diff] [blame] | 825 | int lsb_depth, |
Jean-Marc Valin | c94e4bb | 2013-12-08 03:31:50 -0500 | [diff] [blame] | 826 | downmix_func downmix, |
| 827 | int float_api |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 828 | ) |
| 829 | { |
| 830 | opus_int32 Fs; |
| 831 | int coupled_size; |
| 832 | int mono_size; |
| 833 | int s; |
| 834 | char *ptr; |
| 835 | int tot_size; |
| 836 | VARDECL(opus_val16, buf); |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 837 | VARDECL(opus_val16, bandSMR); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 838 | unsigned char tmp_data[MS_FRAME_TMP]; |
| 839 | OpusRepacketizer rp; |
Jean-Marc Valin | eab134c | 2013-10-14 15:01:36 -0400 | [diff] [blame] | 840 | opus_int32 vbr; |
Jean-Marc Valin | 51f4a32 | 2013-02-20 04:08:04 -0500 | [diff] [blame] | 841 | const CELTMode *celt_mode; |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 842 | opus_int32 bitrates[256]; |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 843 | opus_val16 bandLogE[42]; |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 844 | opus_val32 *mem = NULL; |
| 845 | opus_val32 *preemph_mem=NULL; |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 846 | int frame_size; |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 847 | opus_int32 rate_sum; |
| 848 | opus_int32 smallest_packet; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 849 | ALLOC_STACK; |
| 850 | |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 851 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 852 | { |
| 853 | preemph_mem = ms_get_preemph_mem(st); |
| 854 | mem = ms_get_window_mem(st); |
| 855 | } |
| 856 | |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 857 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 858 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs)); |
Jean-Marc Valin | eab134c | 2013-10-14 15:01:36 -0400 | [diff] [blame] | 859 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_VBR(&vbr)); |
Jean-Marc Valin | 51f4a32 | 2013-02-20 04:08:04 -0500 | [diff] [blame] | 860 | opus_encoder_ctl((OpusEncoder*)ptr, CELT_GET_MODE(&celt_mode)); |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 861 | |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 862 | { |
| 863 | opus_int32 delay_compensation; |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 864 | |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 865 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_LOOKAHEAD(&delay_compensation)); |
| 866 | delay_compensation -= Fs/400; |
| 867 | frame_size = compute_frame_size(pcm, analysis_frame_size, |
Mark Harris | 7d0e33f | 2016-08-27 07:10:15 -0700 | [diff] [blame] | 868 | st->variable_duration, st->layout.nb_channels, Fs, st->bitrate_bps, |
Jean-Marc Valin | c2b3441 | 2013-10-28 21:48:50 -0400 | [diff] [blame] | 869 | delay_compensation, downmix |
| 870 | #ifndef DISABLE_FLOAT_API |
| 871 | , st->subframe_mem |
| 872 | #endif |
| 873 | ); |
Jean-Marc Valin | b90e63b | 2013-09-16 13:08:52 -0400 | [diff] [blame] | 874 | } |
| 875 | |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 876 | if (400*frame_size < Fs) |
| 877 | { |
| 878 | RESTORE_STACK; |
| 879 | return OPUS_BAD_ARG; |
| 880 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 881 | /* Validate frame_size before using it to allocate stack space. |
| 882 | This mirrors the checks in opus_encode[_float](). */ |
| 883 | if (400*frame_size != Fs && 200*frame_size != Fs && |
| 884 | 100*frame_size != Fs && 50*frame_size != Fs && |
| 885 | 25*frame_size != Fs && 50*frame_size != 3*Fs) |
| 886 | { |
| 887 | RESTORE_STACK; |
| 888 | return OPUS_BAD_ARG; |
| 889 | } |
Jean-Marc Valin | e1326fe | 2014-09-04 01:48:46 -0400 | [diff] [blame] | 890 | |
| 891 | /* Smallest packet the encoder can produce. */ |
| 892 | smallest_packet = st->layout.nb_streams*2-1; |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 893 | if (max_data_bytes < smallest_packet) |
| 894 | { |
| 895 | RESTORE_STACK; |
Jean-Marc Valin | e1326fe | 2014-09-04 01:48:46 -0400 | [diff] [blame] | 896 | return OPUS_BUFFER_TOO_SMALL; |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 897 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 898 | ALLOC(buf, 2*frame_size, opus_val16); |
| 899 | coupled_size = opus_encoder_get_size(2); |
| 900 | mono_size = opus_encoder_get_size(1); |
| 901 | |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 902 | ALLOC(bandSMR, 21*st->layout.nb_channels, opus_val16); |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 903 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 904 | { |
Viswanath Puttagunta | f48abe8 | 2015-05-15 12:42:19 -0500 | [diff] [blame] | 905 | 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] | 906 | } |
| 907 | |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 908 | /* Compute bitrate allocation between streams (this could be a lot better) */ |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 909 | rate_sum = rate_allocation(st, bitrates, frame_size); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 910 | |
Jean-Marc Valin | eab134c | 2013-10-14 15:01:36 -0400 | [diff] [blame] | 911 | if (!vbr) |
Jean-Marc Valin | 8dc91de | 2014-06-21 01:16:46 -0400 | [diff] [blame] | 912 | { |
| 913 | if (st->bitrate_bps == OPUS_AUTO) |
| 914 | { |
| 915 | max_data_bytes = IMIN(max_data_bytes, 3*rate_sum/(3*8*Fs/frame_size)); |
| 916 | } else if (st->bitrate_bps != OPUS_BITRATE_MAX) |
| 917 | { |
| 918 | max_data_bytes = IMIN(max_data_bytes, IMAX(smallest_packet, |
| 919 | 3*st->bitrate_bps/(3*8*Fs/frame_size))); |
| 920 | } |
| 921 | } |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 922 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 923 | for (s=0;s<st->layout.nb_streams;s++) |
| 924 | { |
| 925 | OpusEncoder *enc; |
| 926 | enc = (OpusEncoder*)ptr; |
| 927 | if (s < st->layout.nb_coupled_streams) |
| 928 | ptr += align(coupled_size); |
| 929 | else |
| 930 | ptr += align(mono_size); |
Jean-Marc Valin | 1b72386 | 2013-04-25 21:34:04 -0400 | [diff] [blame] | 931 | opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrates[s])); |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 932 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 933 | { |
Jean-Marc Valin | 6fbfed6 | 2013-09-13 17:16:38 -0400 | [diff] [blame] | 934 | opus_int32 equiv_rate; |
| 935 | equiv_rate = st->bitrate_bps; |
| 936 | if (frame_size*50 < Fs) |
| 937 | equiv_rate -= 60*(Fs/frame_size - 50)*st->layout.nb_channels; |
Jean-Marc Valin | ccf7d9b | 2013-11-24 01:57:52 -0500 | [diff] [blame] | 938 | if (equiv_rate > 10000*st->layout.nb_channels) |
Jean-Marc Valin | 6fbfed6 | 2013-09-13 17:16:38 -0400 | [diff] [blame] | 939 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND)); |
Jean-Marc Valin | ccf7d9b | 2013-11-24 01:57:52 -0500 | [diff] [blame] | 940 | else if (equiv_rate > 7000*st->layout.nb_channels) |
Jean-Marc Valin | 6fbfed6 | 2013-09-13 17:16:38 -0400 | [diff] [blame] | 941 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND)); |
Jean-Marc Valin | ccf7d9b | 2013-11-24 01:57:52 -0500 | [diff] [blame] | 942 | else if (equiv_rate > 5000*st->layout.nb_channels) |
Jean-Marc Valin | 6fbfed6 | 2013-09-13 17:16:38 -0400 | [diff] [blame] | 943 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND)); |
| 944 | else |
| 945 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND)); |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 946 | if (s < st->layout.nb_coupled_streams) |
Jean-Marc Valin | 8824fdb | 2013-09-10 01:15:19 -0400 | [diff] [blame] | 947 | { |
| 948 | /* To preserve the spatial image, force stereo CELT on coupled streams */ |
| 949 | opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_CELT_ONLY)); |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 950 | opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(2)); |
Jean-Marc Valin | 8824fdb | 2013-09-10 01:15:19 -0400 | [diff] [blame] | 951 | } |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 952 | } |
Michael Graczyk | 1ac3a56 | 2016-04-04 19:31:04 -0700 | [diff] [blame] | 953 | #ifdef ENABLE_EXPERIMENTAL_AMBISONICS |
| 954 | else if (st->mapping_type == MAPPING_TYPE_AMBISONICS) { |
| 955 | opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_CELT_ONLY)); |
| 956 | } |
| 957 | #endif |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 961 | /* Counting ToC */ |
| 962 | tot_size = 0; |
| 963 | for (s=0;s<st->layout.nb_streams;s++) |
| 964 | { |
| 965 | OpusEncoder *enc; |
| 966 | int len; |
| 967 | int curr_max; |
Jean-Marc Valin | 91904a4 | 2013-09-05 21:34:43 -0400 | [diff] [blame] | 968 | int c1, c2; |
Jean-Marc Valin | 328953e | 2016-07-04 01:29:23 -0400 | [diff] [blame] | 969 | int ret; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 970 | |
| 971 | opus_repacketizer_init(&rp); |
| 972 | enc = (OpusEncoder*)ptr; |
| 973 | if (s < st->layout.nb_coupled_streams) |
| 974 | { |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 975 | int i; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 976 | int left, right; |
| 977 | left = get_left_channel(&st->layout, s, -1); |
| 978 | right = get_right_channel(&st->layout, s, -1); |
| 979 | (*copy_channel_in)(buf, 2, |
| 980 | pcm, st->layout.nb_channels, left, frame_size); |
| 981 | (*copy_channel_in)(buf+1, 2, |
| 982 | pcm, st->layout.nb_channels, right, frame_size); |
| 983 | ptr += align(coupled_size); |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 984 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 985 | { |
| 986 | for (i=0;i<21;i++) |
| 987 | { |
| 988 | bandLogE[i] = bandSMR[21*left+i]; |
| 989 | bandLogE[21+i] = bandSMR[21*right+i]; |
| 990 | } |
| 991 | } |
Jean-Marc Valin | 91904a4 | 2013-09-05 21:34:43 -0400 | [diff] [blame] | 992 | c1 = left; |
| 993 | c2 = right; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 994 | } else { |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 995 | int i; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 996 | int chan = get_mono_channel(&st->layout, s, -1); |
| 997 | (*copy_channel_in)(buf, 1, |
| 998 | pcm, st->layout.nb_channels, chan, frame_size); |
| 999 | ptr += align(mono_size); |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 1000 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 1001 | { |
| 1002 | for (i=0;i<21;i++) |
| 1003 | bandLogE[i] = bandSMR[21*chan+i]; |
| 1004 | } |
Jean-Marc Valin | 91904a4 | 2013-09-05 21:34:43 -0400 | [diff] [blame] | 1005 | c1 = chan; |
| 1006 | c2 = -1; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1007 | } |
Michael Graczyk | 3925668 | 2016-05-02 21:42:18 -0700 | [diff] [blame] | 1008 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 1009 | opus_encoder_ctl(enc, OPUS_SET_ENERGY_MASK(bandLogE)); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1010 | /* number of bytes left (+Toc) */ |
| 1011 | curr_max = max_data_bytes - tot_size; |
Jean-Marc Valin | da97db1 | 2014-09-04 02:48:21 -0400 | [diff] [blame] | 1012 | /* 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] | 1013 | curr_max -= IMAX(0,2*(st->layout.nb_streams-s-1)-1); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1014 | curr_max = IMIN(curr_max,MS_FRAME_TMP); |
Jean-Marc Valin | da97db1 | 2014-09-04 02:48:21 -0400 | [diff] [blame] | 1015 | /* Repacketizer will add one or two bytes for self-delimited frames */ |
Jean-Marc Valin | 648eb9a | 2014-09-04 02:44:09 -0400 | [diff] [blame] | 1016 | 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] | 1017 | if (!vbr && s == st->layout.nb_streams-1) |
| 1018 | 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] | 1019 | 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] | 1020 | 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] | 1021 | if (len<0) |
| 1022 | { |
| 1023 | RESTORE_STACK; |
| 1024 | return len; |
| 1025 | } |
| 1026 | /* We need to use the repacketizer to add the self-delimiting lengths |
| 1027 | while taking into account the fact that the encoder can now return |
| 1028 | 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] | 1029 | ret = opus_repacketizer_cat(&rp, tmp_data, len); |
| 1030 | /* If the opus_repacketizer_cat() fails, then something's seriously wrong |
| 1031 | with the encoder. */ |
| 1032 | if (ret != OPUS_OK) |
Jean-Marc Valin | 9203565 | 2016-07-04 10:45:15 -0400 | [diff] [blame] | 1033 | { |
| 1034 | RESTORE_STACK; |
Jean-Marc Valin | 328953e | 2016-07-04 01:29:23 -0400 | [diff] [blame] | 1035 | return OPUS_INTERNAL_ERROR; |
Jean-Marc Valin | 9203565 | 2016-07-04 10:45:15 -0400 | [diff] [blame] | 1036 | } |
Jean-Marc Valin | c5635d2 | 2013-11-13 14:08:22 -0500 | [diff] [blame] | 1037 | len = opus_repacketizer_out_range_impl(&rp, 0, opus_repacketizer_get_nb_frames(&rp), |
| 1038 | 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] | 1039 | data += len; |
| 1040 | tot_size += len; |
| 1041 | } |
Jean-Marc Valin | fdceae8 | 2013-08-30 21:58:02 -0400 | [diff] [blame] | 1042 | /*printf("\n");*/ |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1043 | RESTORE_STACK; |
| 1044 | return tot_size; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1045 | } |
| 1046 | |
Pedro Becerra | 1af7f95 | 2013-07-11 00:00:47 -0400 | [diff] [blame] | 1047 | #if !defined(DISABLE_FLOAT_API) |
| 1048 | static void opus_copy_channel_in_float( |
| 1049 | opus_val16 *dst, |
| 1050 | int dst_stride, |
| 1051 | const void *src, |
| 1052 | int src_stride, |
| 1053 | int src_channel, |
| 1054 | int frame_size |
| 1055 | ) |
| 1056 | { |
| 1057 | const float *float_src; |
| 1058 | opus_int32 i; |
| 1059 | float_src = (const float *)src; |
| 1060 | for (i=0;i<frame_size;i++) |
| 1061 | #if defined(FIXED_POINT) |
| 1062 | dst[i*dst_stride] = FLOAT2INT16(float_src[i*src_stride+src_channel]); |
| 1063 | #else |
| 1064 | dst[i*dst_stride] = float_src[i*src_stride+src_channel]; |
| 1065 | #endif |
| 1066 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1067 | #endif |
| 1068 | |
| 1069 | static void opus_copy_channel_in_short( |
| 1070 | opus_val16 *dst, |
| 1071 | int dst_stride, |
| 1072 | const void *src, |
| 1073 | int src_stride, |
| 1074 | int src_channel, |
| 1075 | int frame_size |
| 1076 | ) |
| 1077 | { |
| 1078 | const opus_int16 *short_src; |
Timothy B. Terriberry | a8f04b2 | 2013-03-18 14:42:44 -0700 | [diff] [blame] | 1079 | opus_int32 i; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1080 | short_src = (const opus_int16 *)src; |
| 1081 | for (i=0;i<frame_size;i++) |
| 1082 | #if defined(FIXED_POINT) |
| 1083 | dst[i*dst_stride] = short_src[i*src_stride+src_channel]; |
| 1084 | #else |
| 1085 | dst[i*dst_stride] = (1/32768.f)*short_src[i*src_stride+src_channel]; |
| 1086 | #endif |
| 1087 | } |
| 1088 | |
Jean-Marc Valin | a4dccd3 | 2013-05-04 23:54:20 -0400 | [diff] [blame] | 1089 | |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1090 | #ifdef FIXED_POINT |
| 1091 | int opus_multistream_encode( |
| 1092 | OpusMSEncoder *st, |
| 1093 | const opus_val16 *pcm, |
| 1094 | int frame_size, |
| 1095 | unsigned char *data, |
| 1096 | opus_int32 max_data_bytes |
| 1097 | ) |
| 1098 | { |
| 1099 | return opus_multistream_encode_native(st, opus_copy_channel_in_short, |
Jean-Marc Valin | c94e4bb | 2013-12-08 03:31:50 -0500 | [diff] [blame] | 1100 | pcm, frame_size, data, max_data_bytes, 16, downmix_int, 0); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | #ifndef DISABLE_FLOAT_API |
| 1104 | int opus_multistream_encode_float( |
| 1105 | OpusMSEncoder *st, |
| 1106 | const float *pcm, |
| 1107 | int frame_size, |
| 1108 | unsigned char *data, |
| 1109 | opus_int32 max_data_bytes |
| 1110 | ) |
| 1111 | { |
| 1112 | return opus_multistream_encode_native(st, opus_copy_channel_in_float, |
Jean-Marc Valin | c94e4bb | 2013-12-08 03:31:50 -0500 | [diff] [blame] | 1113 | pcm, frame_size, data, max_data_bytes, 16, downmix_float, 1); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1114 | } |
| 1115 | #endif |
| 1116 | |
| 1117 | #else |
| 1118 | |
| 1119 | int opus_multistream_encode_float |
| 1120 | ( |
| 1121 | OpusMSEncoder *st, |
| 1122 | const opus_val16 *pcm, |
| 1123 | int frame_size, |
| 1124 | unsigned char *data, |
| 1125 | opus_int32 max_data_bytes |
| 1126 | ) |
| 1127 | { |
| 1128 | return opus_multistream_encode_native(st, opus_copy_channel_in_float, |
Jean-Marc Valin | c94e4bb | 2013-12-08 03:31:50 -0500 | [diff] [blame] | 1129 | pcm, frame_size, data, max_data_bytes, 24, downmix_float, 1); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | int opus_multistream_encode( |
| 1133 | OpusMSEncoder *st, |
| 1134 | const opus_int16 *pcm, |
| 1135 | int frame_size, |
| 1136 | unsigned char *data, |
| 1137 | opus_int32 max_data_bytes |
| 1138 | ) |
| 1139 | { |
| 1140 | return opus_multistream_encode_native(st, opus_copy_channel_in_short, |
Jean-Marc Valin | c94e4bb | 2013-12-08 03:31:50 -0500 | [diff] [blame] | 1141 | pcm, frame_size, data, max_data_bytes, 16, downmix_int, 0); |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1142 | } |
| 1143 | #endif |
| 1144 | |
| 1145 | int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...) |
| 1146 | { |
| 1147 | va_list ap; |
| 1148 | int coupled_size, mono_size; |
| 1149 | char *ptr; |
| 1150 | int ret = OPUS_OK; |
| 1151 | |
| 1152 | va_start(ap, request); |
| 1153 | |
| 1154 | coupled_size = opus_encoder_get_size(2); |
| 1155 | mono_size = opus_encoder_get_size(1); |
| 1156 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
| 1157 | switch (request) |
| 1158 | { |
| 1159 | case OPUS_SET_BITRATE_REQUEST: |
| 1160 | { |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1161 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | f4f5a6a | 2016-09-06 22:32:22 -0400 | [diff] [blame] | 1162 | if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX) |
Gregory Maxwell | a0d096f | 2013-06-29 20:33:32 -0700 | [diff] [blame] | 1163 | { |
Jean-Marc Valin | f4f5a6a | 2016-09-06 22:32:22 -0400 | [diff] [blame] | 1164 | if (value <= 0) |
| 1165 | goto bad_arg; |
| 1166 | 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] | 1167 | } |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1168 | st->bitrate_bps = value; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1169 | } |
| 1170 | break; |
| 1171 | case OPUS_GET_BITRATE_REQUEST: |
| 1172 | { |
| 1173 | int s; |
| 1174 | opus_int32 *value = va_arg(ap, opus_int32*); |
Gregory Maxwell | a0d096f | 2013-06-29 20:33:32 -0700 | [diff] [blame] | 1175 | if (!value) |
| 1176 | { |
Gregory Maxwell | b271dae | 2013-06-29 20:25:55 -0700 | [diff] [blame] | 1177 | goto bad_arg; |
| 1178 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1179 | *value = 0; |
| 1180 | for (s=0;s<st->layout.nb_streams;s++) |
| 1181 | { |
| 1182 | opus_int32 rate; |
| 1183 | OpusEncoder *enc; |
| 1184 | enc = (OpusEncoder*)ptr; |
| 1185 | if (s < st->layout.nb_coupled_streams) |
| 1186 | ptr += align(coupled_size); |
| 1187 | else |
| 1188 | ptr += align(mono_size); |
| 1189 | opus_encoder_ctl(enc, request, &rate); |
| 1190 | *value += rate; |
| 1191 | } |
| 1192 | } |
| 1193 | break; |
| 1194 | case OPUS_GET_LSB_DEPTH_REQUEST: |
| 1195 | case OPUS_GET_VBR_REQUEST: |
| 1196 | case OPUS_GET_APPLICATION_REQUEST: |
| 1197 | case OPUS_GET_BANDWIDTH_REQUEST: |
| 1198 | case OPUS_GET_COMPLEXITY_REQUEST: |
| 1199 | case OPUS_GET_PACKET_LOSS_PERC_REQUEST: |
| 1200 | case OPUS_GET_DTX_REQUEST: |
| 1201 | case OPUS_GET_VOICE_RATIO_REQUEST: |
| 1202 | case OPUS_GET_VBR_CONSTRAINT_REQUEST: |
| 1203 | case OPUS_GET_SIGNAL_REQUEST: |
| 1204 | case OPUS_GET_LOOKAHEAD_REQUEST: |
| 1205 | case OPUS_GET_SAMPLE_RATE_REQUEST: |
| 1206 | case OPUS_GET_INBAND_FEC_REQUEST: |
| 1207 | case OPUS_GET_FORCE_CHANNELS_REQUEST: |
Jean-Marc Valin | cbe93e2 | 2013-11-15 13:50:38 -0500 | [diff] [blame] | 1208 | case OPUS_GET_PREDICTION_DISABLED_REQUEST: |
Mark Harris | 691d493 | 2016-09-03 10:42:19 -0700 | [diff] [blame] | 1209 | case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST: |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1210 | { |
| 1211 | OpusEncoder *enc; |
| 1212 | /* For int32* GET params, just query the first stream */ |
| 1213 | opus_int32 *value = va_arg(ap, opus_int32*); |
| 1214 | enc = (OpusEncoder*)ptr; |
| 1215 | ret = opus_encoder_ctl(enc, request, value); |
| 1216 | } |
| 1217 | break; |
| 1218 | case OPUS_GET_FINAL_RANGE_REQUEST: |
| 1219 | { |
| 1220 | int s; |
| 1221 | opus_uint32 *value = va_arg(ap, opus_uint32*); |
| 1222 | opus_uint32 tmp; |
Gregory Maxwell | a0d096f | 2013-06-29 20:33:32 -0700 | [diff] [blame] | 1223 | if (!value) |
| 1224 | { |
Gregory Maxwell | b271dae | 2013-06-29 20:25:55 -0700 | [diff] [blame] | 1225 | goto bad_arg; |
| 1226 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1227 | *value=0; |
| 1228 | for (s=0;s<st->layout.nb_streams;s++) |
| 1229 | { |
| 1230 | OpusEncoder *enc; |
| 1231 | enc = (OpusEncoder*)ptr; |
| 1232 | if (s < st->layout.nb_coupled_streams) |
| 1233 | ptr += align(coupled_size); |
| 1234 | else |
| 1235 | ptr += align(mono_size); |
| 1236 | ret = opus_encoder_ctl(enc, request, &tmp); |
| 1237 | if (ret != OPUS_OK) break; |
| 1238 | *value ^= tmp; |
| 1239 | } |
| 1240 | } |
| 1241 | break; |
| 1242 | case OPUS_SET_LSB_DEPTH_REQUEST: |
| 1243 | case OPUS_SET_COMPLEXITY_REQUEST: |
| 1244 | case OPUS_SET_VBR_REQUEST: |
| 1245 | case OPUS_SET_VBR_CONSTRAINT_REQUEST: |
Daniel Jensen | c0c0ef6 | 2013-07-22 16:31:31 -0600 | [diff] [blame] | 1246 | case OPUS_SET_MAX_BANDWIDTH_REQUEST: |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1247 | case OPUS_SET_BANDWIDTH_REQUEST: |
| 1248 | case OPUS_SET_SIGNAL_REQUEST: |
| 1249 | case OPUS_SET_APPLICATION_REQUEST: |
| 1250 | case OPUS_SET_INBAND_FEC_REQUEST: |
| 1251 | case OPUS_SET_PACKET_LOSS_PERC_REQUEST: |
| 1252 | case OPUS_SET_DTX_REQUEST: |
| 1253 | case OPUS_SET_FORCE_MODE_REQUEST: |
| 1254 | case OPUS_SET_FORCE_CHANNELS_REQUEST: |
Jean-Marc Valin | cbe93e2 | 2013-11-15 13:50:38 -0500 | [diff] [blame] | 1255 | case OPUS_SET_PREDICTION_DISABLED_REQUEST: |
Mark Harris | 691d493 | 2016-09-03 10:42:19 -0700 | [diff] [blame] | 1256 | case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST: |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1257 | { |
| 1258 | int s; |
| 1259 | /* This works for int32 params */ |
| 1260 | opus_int32 value = va_arg(ap, opus_int32); |
| 1261 | for (s=0;s<st->layout.nb_streams;s++) |
| 1262 | { |
| 1263 | OpusEncoder *enc; |
| 1264 | |
| 1265 | enc = (OpusEncoder*)ptr; |
| 1266 | if (s < st->layout.nb_coupled_streams) |
| 1267 | ptr += align(coupled_size); |
| 1268 | else |
| 1269 | ptr += align(mono_size); |
| 1270 | ret = opus_encoder_ctl(enc, request, value); |
| 1271 | if (ret != OPUS_OK) |
| 1272 | break; |
| 1273 | } |
| 1274 | } |
| 1275 | break; |
| 1276 | case OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST: |
| 1277 | { |
| 1278 | int s; |
| 1279 | opus_int32 stream_id; |
| 1280 | OpusEncoder **value; |
| 1281 | stream_id = va_arg(ap, opus_int32); |
| 1282 | if (stream_id<0 || stream_id >= st->layout.nb_streams) |
| 1283 | ret = OPUS_BAD_ARG; |
| 1284 | value = va_arg(ap, OpusEncoder**); |
Gregory Maxwell | a0d096f | 2013-06-29 20:33:32 -0700 | [diff] [blame] | 1285 | if (!value) |
| 1286 | { |
Gregory Maxwell | b271dae | 2013-06-29 20:25:55 -0700 | [diff] [blame] | 1287 | goto bad_arg; |
| 1288 | } |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1289 | for (s=0;s<stream_id;s++) |
| 1290 | { |
| 1291 | if (s < st->layout.nb_coupled_streams) |
| 1292 | ptr += align(coupled_size); |
| 1293 | else |
| 1294 | ptr += align(mono_size); |
| 1295 | } |
| 1296 | *value = (OpusEncoder*)ptr; |
| 1297 | } |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1298 | break; |
Jean-Marc Valin | 51f4a32 | 2013-02-20 04:08:04 -0500 | [diff] [blame] | 1299 | case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1300 | { |
| 1301 | opus_int32 value = va_arg(ap, opus_int32); |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1302 | st->variable_duration = value; |
| 1303 | } |
| 1304 | break; |
Jean-Marc Valin | 51f4a32 | 2013-02-20 04:08:04 -0500 | [diff] [blame] | 1305 | case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST: |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1306 | { |
| 1307 | opus_int32 *value = va_arg(ap, opus_int32*); |
Gregory Maxwell | a0d096f | 2013-06-29 20:33:32 -0700 | [diff] [blame] | 1308 | if (!value) |
| 1309 | { |
Gregory Maxwell | b271dae | 2013-06-29 20:25:55 -0700 | [diff] [blame] | 1310 | goto bad_arg; |
| 1311 | } |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1312 | *value = st->variable_duration; |
| 1313 | } |
| 1314 | break; |
Jean-Marc Valin | 811db62 | 2013-10-28 16:11:53 -0400 | [diff] [blame] | 1315 | case OPUS_RESET_STATE: |
| 1316 | { |
| 1317 | int s; |
| 1318 | 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] | 1319 | if (st->mapping_type == MAPPING_TYPE_SURROUND) |
Jean-Marc Valin | 811db62 | 2013-10-28 16:11:53 -0400 | [diff] [blame] | 1320 | { |
| 1321 | OPUS_CLEAR(ms_get_preemph_mem(st), st->layout.nb_channels); |
| 1322 | OPUS_CLEAR(ms_get_window_mem(st), st->layout.nb_channels*120); |
| 1323 | } |
| 1324 | for (s=0;s<st->layout.nb_streams;s++) |
| 1325 | { |
| 1326 | OpusEncoder *enc; |
| 1327 | enc = (OpusEncoder*)ptr; |
| 1328 | if (s < st->layout.nb_coupled_streams) |
| 1329 | ptr += align(coupled_size); |
| 1330 | else |
| 1331 | ptr += align(mono_size); |
| 1332 | ret = opus_encoder_ctl(enc, OPUS_RESET_STATE); |
| 1333 | if (ret != OPUS_OK) |
| 1334 | break; |
| 1335 | } |
| 1336 | } |
| 1337 | break; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1338 | default: |
| 1339 | ret = OPUS_UNIMPLEMENTED; |
| 1340 | break; |
| 1341 | } |
| 1342 | |
| 1343 | va_end(ap); |
| 1344 | return ret; |
Jean-Marc Valin | 7448366 | 2012-12-17 16:23:42 -0500 | [diff] [blame] | 1345 | bad_arg: |
| 1346 | va_end(ap); |
| 1347 | return OPUS_BAD_ARG; |
Jean-Marc Valin | ae0e2ca | 2012-11-07 19:57:33 -0500 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | void opus_multistream_encoder_destroy(OpusMSEncoder *st) |
| 1351 | { |
| 1352 | opus_free(st); |
| 1353 | } |