Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Copyright (c) 2011 Xiph.Org Foundation |
michael@0 | 2 | Written by Jean-Marc Valin */ |
michael@0 | 3 | /* |
michael@0 | 4 | Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | modification, are permitted provided that the following conditions |
michael@0 | 6 | are met: |
michael@0 | 7 | |
michael@0 | 8 | - Redistributions of source code must retain the above copyright |
michael@0 | 9 | notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | |
michael@0 | 11 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 12 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 13 | documentation and/or other materials provided with the distribution. |
michael@0 | 14 | |
michael@0 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 16 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 17 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
michael@0 | 19 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | #ifdef HAVE_CONFIG_H |
michael@0 | 29 | #include "config.h" |
michael@0 | 30 | #endif |
michael@0 | 31 | |
michael@0 | 32 | #include "opus_multistream.h" |
michael@0 | 33 | #include "opus.h" |
michael@0 | 34 | #include "opus_private.h" |
michael@0 | 35 | #include "stack_alloc.h" |
michael@0 | 36 | #include <stdarg.h> |
michael@0 | 37 | #include "float_cast.h" |
michael@0 | 38 | #include "os_support.h" |
michael@0 | 39 | #include "mathops.h" |
michael@0 | 40 | #include "mdct.h" |
michael@0 | 41 | #include "modes.h" |
michael@0 | 42 | #include "bands.h" |
michael@0 | 43 | #include "quant_bands.h" |
michael@0 | 44 | |
michael@0 | 45 | typedef struct { |
michael@0 | 46 | int nb_streams; |
michael@0 | 47 | int nb_coupled_streams; |
michael@0 | 48 | unsigned char mapping[8]; |
michael@0 | 49 | } VorbisLayout; |
michael@0 | 50 | |
michael@0 | 51 | /* Index is nb_channel-1*/ |
michael@0 | 52 | static const VorbisLayout vorbis_mappings[8] = { |
michael@0 | 53 | {1, 0, {0}}, /* 1: mono */ |
michael@0 | 54 | {1, 1, {0, 1}}, /* 2: stereo */ |
michael@0 | 55 | {2, 1, {0, 2, 1}}, /* 3: 1-d surround */ |
michael@0 | 56 | {2, 2, {0, 1, 2, 3}}, /* 4: quadraphonic surround */ |
michael@0 | 57 | {3, 2, {0, 4, 1, 2, 3}}, /* 5: 5-channel surround */ |
michael@0 | 58 | {4, 2, {0, 4, 1, 2, 3, 5}}, /* 6: 5.1 surround */ |
michael@0 | 59 | {4, 3, {0, 4, 1, 2, 3, 5, 6}}, /* 7: 6.1 surround */ |
michael@0 | 60 | {5, 3, {0, 6, 1, 2, 3, 4, 5, 7}}, /* 8: 7.1 surround */ |
michael@0 | 61 | }; |
michael@0 | 62 | |
michael@0 | 63 | typedef void (*opus_copy_channel_in_func)( |
michael@0 | 64 | opus_val16 *dst, |
michael@0 | 65 | int dst_stride, |
michael@0 | 66 | const void *src, |
michael@0 | 67 | int src_stride, |
michael@0 | 68 | int src_channel, |
michael@0 | 69 | int frame_size |
michael@0 | 70 | ); |
michael@0 | 71 | |
michael@0 | 72 | struct OpusMSEncoder { |
michael@0 | 73 | ChannelLayout layout; |
michael@0 | 74 | int lfe_stream; |
michael@0 | 75 | int application; |
michael@0 | 76 | int variable_duration; |
michael@0 | 77 | int surround; |
michael@0 | 78 | opus_int32 bitrate_bps; |
michael@0 | 79 | float subframe_mem[3]; |
michael@0 | 80 | /* Encoder states go here */ |
michael@0 | 81 | /* then opus_val32 window_mem[channels*120]; */ |
michael@0 | 82 | /* then opus_val32 preemph_mem[channels]; */ |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | static opus_val32 *ms_get_preemph_mem(OpusMSEncoder *st) |
michael@0 | 86 | { |
michael@0 | 87 | int s; |
michael@0 | 88 | char *ptr; |
michael@0 | 89 | int coupled_size, mono_size; |
michael@0 | 90 | |
michael@0 | 91 | coupled_size = opus_encoder_get_size(2); |
michael@0 | 92 | mono_size = opus_encoder_get_size(1); |
michael@0 | 93 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
michael@0 | 94 | for (s=0;s<st->layout.nb_streams;s++) |
michael@0 | 95 | { |
michael@0 | 96 | if (s < st->layout.nb_coupled_streams) |
michael@0 | 97 | ptr += align(coupled_size); |
michael@0 | 98 | else |
michael@0 | 99 | ptr += align(mono_size); |
michael@0 | 100 | } |
michael@0 | 101 | return (opus_val32*)(ptr+st->layout.nb_channels*120*sizeof(opus_val32)); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | static opus_val32 *ms_get_window_mem(OpusMSEncoder *st) |
michael@0 | 105 | { |
michael@0 | 106 | int s; |
michael@0 | 107 | char *ptr; |
michael@0 | 108 | int coupled_size, mono_size; |
michael@0 | 109 | |
michael@0 | 110 | coupled_size = opus_encoder_get_size(2); |
michael@0 | 111 | mono_size = opus_encoder_get_size(1); |
michael@0 | 112 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
michael@0 | 113 | for (s=0;s<st->layout.nb_streams;s++) |
michael@0 | 114 | { |
michael@0 | 115 | if (s < st->layout.nb_coupled_streams) |
michael@0 | 116 | ptr += align(coupled_size); |
michael@0 | 117 | else |
michael@0 | 118 | ptr += align(mono_size); |
michael@0 | 119 | } |
michael@0 | 120 | return (opus_val32*)ptr; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | static int validate_encoder_layout(const ChannelLayout *layout) |
michael@0 | 124 | { |
michael@0 | 125 | int s; |
michael@0 | 126 | for (s=0;s<layout->nb_streams;s++) |
michael@0 | 127 | { |
michael@0 | 128 | if (s < layout->nb_coupled_streams) |
michael@0 | 129 | { |
michael@0 | 130 | if (get_left_channel(layout, s, -1)==-1) |
michael@0 | 131 | return 0; |
michael@0 | 132 | if (get_right_channel(layout, s, -1)==-1) |
michael@0 | 133 | return 0; |
michael@0 | 134 | } else { |
michael@0 | 135 | if (get_mono_channel(layout, s, -1)==-1) |
michael@0 | 136 | return 0; |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | return 1; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | static void channel_pos(int channels, int pos[8]) |
michael@0 | 143 | { |
michael@0 | 144 | /* Position in the mix: 0 don't mix, 1: left, 2: center, 3:right */ |
michael@0 | 145 | if (channels==4) |
michael@0 | 146 | { |
michael@0 | 147 | pos[0]=1; |
michael@0 | 148 | pos[1]=3; |
michael@0 | 149 | pos[2]=1; |
michael@0 | 150 | pos[3]=3; |
michael@0 | 151 | } else if (channels==3||channels==5||channels==6) |
michael@0 | 152 | { |
michael@0 | 153 | pos[0]=1; |
michael@0 | 154 | pos[1]=2; |
michael@0 | 155 | pos[2]=3; |
michael@0 | 156 | pos[3]=1; |
michael@0 | 157 | pos[4]=3; |
michael@0 | 158 | pos[5]=0; |
michael@0 | 159 | } else if (channels==7) |
michael@0 | 160 | { |
michael@0 | 161 | pos[0]=1; |
michael@0 | 162 | pos[1]=2; |
michael@0 | 163 | pos[2]=3; |
michael@0 | 164 | pos[3]=1; |
michael@0 | 165 | pos[4]=3; |
michael@0 | 166 | pos[5]=2; |
michael@0 | 167 | pos[6]=0; |
michael@0 | 168 | } else if (channels==8) |
michael@0 | 169 | { |
michael@0 | 170 | pos[0]=1; |
michael@0 | 171 | pos[1]=2; |
michael@0 | 172 | pos[2]=3; |
michael@0 | 173 | pos[3]=1; |
michael@0 | 174 | pos[4]=3; |
michael@0 | 175 | pos[5]=1; |
michael@0 | 176 | pos[6]=3; |
michael@0 | 177 | pos[7]=0; |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | #if 1 |
michael@0 | 182 | /* Computes a rough approximation of log2(2^a + 2^b) */ |
michael@0 | 183 | static opus_val16 logSum(opus_val16 a, opus_val16 b) |
michael@0 | 184 | { |
michael@0 | 185 | opus_val16 max; |
michael@0 | 186 | opus_val32 diff; |
michael@0 | 187 | opus_val16 frac; |
michael@0 | 188 | static const opus_val16 diff_table[17] = { |
michael@0 | 189 | QCONST16(0.5000000f, DB_SHIFT), QCONST16(0.2924813f, DB_SHIFT), QCONST16(0.1609640f, DB_SHIFT), QCONST16(0.0849625f, DB_SHIFT), |
michael@0 | 190 | QCONST16(0.0437314f, DB_SHIFT), QCONST16(0.0221971f, DB_SHIFT), QCONST16(0.0111839f, DB_SHIFT), QCONST16(0.0056136f, DB_SHIFT), |
michael@0 | 191 | QCONST16(0.0028123f, DB_SHIFT) |
michael@0 | 192 | }; |
michael@0 | 193 | int low; |
michael@0 | 194 | if (a>b) |
michael@0 | 195 | { |
michael@0 | 196 | max = a; |
michael@0 | 197 | diff = SUB32(EXTEND32(a),EXTEND32(b)); |
michael@0 | 198 | } else { |
michael@0 | 199 | max = b; |
michael@0 | 200 | diff = SUB32(EXTEND32(b),EXTEND32(a)); |
michael@0 | 201 | } |
michael@0 | 202 | if (diff >= QCONST16(8.f, DB_SHIFT)) |
michael@0 | 203 | return max; |
michael@0 | 204 | #ifdef FIXED_POINT |
michael@0 | 205 | low = SHR32(diff, DB_SHIFT-1); |
michael@0 | 206 | frac = SHL16(diff - SHL16(low, DB_SHIFT-1), 16-DB_SHIFT); |
michael@0 | 207 | #else |
michael@0 | 208 | low = (int)floor(2*diff); |
michael@0 | 209 | frac = 2*diff - low; |
michael@0 | 210 | #endif |
michael@0 | 211 | return max + diff_table[low] + MULT16_16_Q15(frac, SUB16(diff_table[low+1], diff_table[low])); |
michael@0 | 212 | } |
michael@0 | 213 | #else |
michael@0 | 214 | opus_val16 logSum(opus_val16 a, opus_val16 b) |
michael@0 | 215 | { |
michael@0 | 216 | return log2(pow(4, a)+ pow(4, b))/2; |
michael@0 | 217 | } |
michael@0 | 218 | #endif |
michael@0 | 219 | |
michael@0 | 220 | void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *bandLogE, opus_val32 *mem, opus_val32 *preemph_mem, |
michael@0 | 221 | int len, int overlap, int channels, int rate, opus_copy_channel_in_func copy_channel_in |
michael@0 | 222 | ) |
michael@0 | 223 | { |
michael@0 | 224 | int c; |
michael@0 | 225 | int i; |
michael@0 | 226 | int LM; |
michael@0 | 227 | int pos[8] = {0}; |
michael@0 | 228 | int upsample; |
michael@0 | 229 | int frame_size; |
michael@0 | 230 | opus_val16 channel_offset; |
michael@0 | 231 | opus_val32 bandE[21]; |
michael@0 | 232 | opus_val16 maskLogE[3][21]; |
michael@0 | 233 | VARDECL(opus_val32, in); |
michael@0 | 234 | VARDECL(opus_val16, x); |
michael@0 | 235 | VARDECL(opus_val32, freq); |
michael@0 | 236 | SAVE_STACK; |
michael@0 | 237 | |
michael@0 | 238 | upsample = resampling_factor(rate); |
michael@0 | 239 | frame_size = len*upsample; |
michael@0 | 240 | |
michael@0 | 241 | for (LM=0;LM<celt_mode->maxLM;LM++) |
michael@0 | 242 | if (celt_mode->shortMdctSize<<LM==frame_size) |
michael@0 | 243 | break; |
michael@0 | 244 | |
michael@0 | 245 | ALLOC(in, frame_size+overlap, opus_val32); |
michael@0 | 246 | ALLOC(x, len, opus_val16); |
michael@0 | 247 | ALLOC(freq, frame_size, opus_val32); |
michael@0 | 248 | |
michael@0 | 249 | channel_pos(channels, pos); |
michael@0 | 250 | |
michael@0 | 251 | for (c=0;c<3;c++) |
michael@0 | 252 | for (i=0;i<21;i++) |
michael@0 | 253 | maskLogE[c][i] = -QCONST16(28.f, DB_SHIFT); |
michael@0 | 254 | |
michael@0 | 255 | for (c=0;c<channels;c++) |
michael@0 | 256 | { |
michael@0 | 257 | OPUS_COPY(in, mem+c*overlap, overlap); |
michael@0 | 258 | (*copy_channel_in)(x, 1, pcm, channels, c, len); |
michael@0 | 259 | celt_preemphasis(x, in+overlap, frame_size, 1, upsample, celt_mode->preemph, preemph_mem+c, 0); |
michael@0 | 260 | clt_mdct_forward(&celt_mode->mdct, in, freq, celt_mode->window, overlap, celt_mode->maxLM-LM, 1); |
michael@0 | 261 | if (upsample != 1) |
michael@0 | 262 | { |
michael@0 | 263 | int bound = len; |
michael@0 | 264 | for (i=0;i<bound;i++) |
michael@0 | 265 | freq[i] *= upsample; |
michael@0 | 266 | for (;i<frame_size;i++) |
michael@0 | 267 | freq[i] = 0; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | compute_band_energies(celt_mode, freq, bandE, 21, 1, 1<<LM); |
michael@0 | 271 | amp2Log2(celt_mode, 21, 21, bandE, bandLogE+21*c, 1); |
michael@0 | 272 | /* Apply spreading function with -6 dB/band going up and -12 dB/band going down. */ |
michael@0 | 273 | for (i=1;i<21;i++) |
michael@0 | 274 | bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i-1]-QCONST16(1.f, DB_SHIFT)); |
michael@0 | 275 | for (i=19;i>=0;i--) |
michael@0 | 276 | bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i+1]-QCONST16(2.f, DB_SHIFT)); |
michael@0 | 277 | if (pos[c]==1) |
michael@0 | 278 | { |
michael@0 | 279 | for (i=0;i<21;i++) |
michael@0 | 280 | maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]); |
michael@0 | 281 | } else if (pos[c]==3) |
michael@0 | 282 | { |
michael@0 | 283 | for (i=0;i<21;i++) |
michael@0 | 284 | maskLogE[2][i] = logSum(maskLogE[2][i], bandLogE[21*c+i]); |
michael@0 | 285 | } else if (pos[c]==2) |
michael@0 | 286 | { |
michael@0 | 287 | for (i=0;i<21;i++) |
michael@0 | 288 | { |
michael@0 | 289 | maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]-QCONST16(.5f, DB_SHIFT)); |
michael@0 | 290 | maskLogE[2][i] = logSum(maskLogE[2][i], bandLogE[21*c+i]-QCONST16(.5f, DB_SHIFT)); |
michael@0 | 291 | } |
michael@0 | 292 | } |
michael@0 | 293 | #if 0 |
michael@0 | 294 | for (i=0;i<21;i++) |
michael@0 | 295 | printf("%f ", bandLogE[21*c+i]); |
michael@0 | 296 | float sum=0; |
michael@0 | 297 | for (i=0;i<21;i++) |
michael@0 | 298 | sum += bandLogE[21*c+i]; |
michael@0 | 299 | printf("%f ", sum/21); |
michael@0 | 300 | #endif |
michael@0 | 301 | OPUS_COPY(mem+c*overlap, in+frame_size, overlap); |
michael@0 | 302 | } |
michael@0 | 303 | for (i=0;i<21;i++) |
michael@0 | 304 | maskLogE[1][i] = MIN32(maskLogE[0][i],maskLogE[2][i]); |
michael@0 | 305 | channel_offset = HALF16(celt_log2(QCONST32(2.f,14)/(channels-1))); |
michael@0 | 306 | for (c=0;c<3;c++) |
michael@0 | 307 | for (i=0;i<21;i++) |
michael@0 | 308 | maskLogE[c][i] += channel_offset; |
michael@0 | 309 | #if 0 |
michael@0 | 310 | for (c=0;c<3;c++) |
michael@0 | 311 | { |
michael@0 | 312 | for (i=0;i<21;i++) |
michael@0 | 313 | printf("%f ", maskLogE[c][i]); |
michael@0 | 314 | } |
michael@0 | 315 | #endif |
michael@0 | 316 | for (c=0;c<channels;c++) |
michael@0 | 317 | { |
michael@0 | 318 | opus_val16 *mask; |
michael@0 | 319 | if (pos[c]!=0) |
michael@0 | 320 | { |
michael@0 | 321 | mask = &maskLogE[pos[c]-1][0]; |
michael@0 | 322 | for (i=0;i<21;i++) |
michael@0 | 323 | bandLogE[21*c+i] = bandLogE[21*c+i] - mask[i]; |
michael@0 | 324 | } else { |
michael@0 | 325 | for (i=0;i<21;i++) |
michael@0 | 326 | bandLogE[21*c+i] = 0; |
michael@0 | 327 | } |
michael@0 | 328 | #if 0 |
michael@0 | 329 | for (i=0;i<21;i++) |
michael@0 | 330 | printf("%f ", bandLogE[21*c+i]); |
michael@0 | 331 | printf("\n"); |
michael@0 | 332 | #endif |
michael@0 | 333 | #if 0 |
michael@0 | 334 | float sum=0; |
michael@0 | 335 | for (i=0;i<21;i++) |
michael@0 | 336 | sum += bandLogE[21*c+i]; |
michael@0 | 337 | printf("%f ", sum/(float)QCONST32(21.f, DB_SHIFT)); |
michael@0 | 338 | printf("\n"); |
michael@0 | 339 | #endif |
michael@0 | 340 | } |
michael@0 | 341 | RESTORE_STACK; |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | opus_int32 opus_multistream_encoder_get_size(int nb_streams, int nb_coupled_streams) |
michael@0 | 345 | { |
michael@0 | 346 | int coupled_size; |
michael@0 | 347 | int mono_size; |
michael@0 | 348 | |
michael@0 | 349 | if(nb_streams<1||nb_coupled_streams>nb_streams||nb_coupled_streams<0)return 0; |
michael@0 | 350 | coupled_size = opus_encoder_get_size(2); |
michael@0 | 351 | mono_size = opus_encoder_get_size(1); |
michael@0 | 352 | return align(sizeof(OpusMSEncoder)) |
michael@0 | 353 | + nb_coupled_streams * align(coupled_size) |
michael@0 | 354 | + (nb_streams-nb_coupled_streams) * align(mono_size); |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | opus_int32 opus_multistream_surround_encoder_get_size(int channels, int mapping_family) |
michael@0 | 358 | { |
michael@0 | 359 | int nb_streams; |
michael@0 | 360 | int nb_coupled_streams; |
michael@0 | 361 | opus_int32 size; |
michael@0 | 362 | |
michael@0 | 363 | if (mapping_family==0) |
michael@0 | 364 | { |
michael@0 | 365 | if (channels==1) |
michael@0 | 366 | { |
michael@0 | 367 | nb_streams=1; |
michael@0 | 368 | nb_coupled_streams=0; |
michael@0 | 369 | } else if (channels==2) |
michael@0 | 370 | { |
michael@0 | 371 | nb_streams=1; |
michael@0 | 372 | nb_coupled_streams=1; |
michael@0 | 373 | } else |
michael@0 | 374 | return 0; |
michael@0 | 375 | } else if (mapping_family==1 && channels<=8 && channels>=1) |
michael@0 | 376 | { |
michael@0 | 377 | nb_streams=vorbis_mappings[channels-1].nb_streams; |
michael@0 | 378 | nb_coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams; |
michael@0 | 379 | } else if (mapping_family==255) |
michael@0 | 380 | { |
michael@0 | 381 | nb_streams=channels; |
michael@0 | 382 | nb_coupled_streams=0; |
michael@0 | 383 | } else |
michael@0 | 384 | return 0; |
michael@0 | 385 | size = opus_multistream_encoder_get_size(nb_streams, nb_coupled_streams); |
michael@0 | 386 | if (channels>2) |
michael@0 | 387 | { |
michael@0 | 388 | size += channels*(120*sizeof(opus_val32) + sizeof(opus_val32)); |
michael@0 | 389 | } |
michael@0 | 390 | return size; |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | |
michael@0 | 394 | static int opus_multistream_encoder_init_impl( |
michael@0 | 395 | OpusMSEncoder *st, |
michael@0 | 396 | opus_int32 Fs, |
michael@0 | 397 | int channels, |
michael@0 | 398 | int streams, |
michael@0 | 399 | int coupled_streams, |
michael@0 | 400 | const unsigned char *mapping, |
michael@0 | 401 | int application, |
michael@0 | 402 | int surround |
michael@0 | 403 | ) |
michael@0 | 404 | { |
michael@0 | 405 | int coupled_size; |
michael@0 | 406 | int mono_size; |
michael@0 | 407 | int i, ret; |
michael@0 | 408 | char *ptr; |
michael@0 | 409 | |
michael@0 | 410 | if ((channels>255) || (channels<1) || (coupled_streams>streams) || |
michael@0 | 411 | (coupled_streams+streams>255) || (streams<1) || (coupled_streams<0)) |
michael@0 | 412 | return OPUS_BAD_ARG; |
michael@0 | 413 | |
michael@0 | 414 | st->layout.nb_channels = channels; |
michael@0 | 415 | st->layout.nb_streams = streams; |
michael@0 | 416 | st->layout.nb_coupled_streams = coupled_streams; |
michael@0 | 417 | st->subframe_mem[0]=st->subframe_mem[1]=st->subframe_mem[2]=0; |
michael@0 | 418 | if (!surround) |
michael@0 | 419 | st->lfe_stream = -1; |
michael@0 | 420 | st->bitrate_bps = OPUS_AUTO; |
michael@0 | 421 | st->application = application; |
michael@0 | 422 | st->variable_duration = OPUS_FRAMESIZE_ARG; |
michael@0 | 423 | for (i=0;i<st->layout.nb_channels;i++) |
michael@0 | 424 | st->layout.mapping[i] = mapping[i]; |
michael@0 | 425 | if (!validate_layout(&st->layout) || !validate_encoder_layout(&st->layout)) |
michael@0 | 426 | return OPUS_BAD_ARG; |
michael@0 | 427 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
michael@0 | 428 | coupled_size = opus_encoder_get_size(2); |
michael@0 | 429 | mono_size = opus_encoder_get_size(1); |
michael@0 | 430 | |
michael@0 | 431 | for (i=0;i<st->layout.nb_coupled_streams;i++) |
michael@0 | 432 | { |
michael@0 | 433 | ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 2, application); |
michael@0 | 434 | if(ret!=OPUS_OK)return ret; |
michael@0 | 435 | if (i==st->lfe_stream) |
michael@0 | 436 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1)); |
michael@0 | 437 | ptr += align(coupled_size); |
michael@0 | 438 | } |
michael@0 | 439 | for (;i<st->layout.nb_streams;i++) |
michael@0 | 440 | { |
michael@0 | 441 | ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 1, application); |
michael@0 | 442 | if (i==st->lfe_stream) |
michael@0 | 443 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1)); |
michael@0 | 444 | if(ret!=OPUS_OK)return ret; |
michael@0 | 445 | ptr += align(mono_size); |
michael@0 | 446 | } |
michael@0 | 447 | if (surround) |
michael@0 | 448 | { |
michael@0 | 449 | OPUS_CLEAR(ms_get_preemph_mem(st), channels); |
michael@0 | 450 | OPUS_CLEAR(ms_get_window_mem(st), channels*120); |
michael@0 | 451 | } |
michael@0 | 452 | st->surround = surround; |
michael@0 | 453 | return OPUS_OK; |
michael@0 | 454 | } |
michael@0 | 455 | |
michael@0 | 456 | int opus_multistream_encoder_init( |
michael@0 | 457 | OpusMSEncoder *st, |
michael@0 | 458 | opus_int32 Fs, |
michael@0 | 459 | int channels, |
michael@0 | 460 | int streams, |
michael@0 | 461 | int coupled_streams, |
michael@0 | 462 | const unsigned char *mapping, |
michael@0 | 463 | int application |
michael@0 | 464 | ) |
michael@0 | 465 | { |
michael@0 | 466 | return opus_multistream_encoder_init_impl(st, Fs, channels, streams, coupled_streams, mapping, application, 0); |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | int opus_multistream_surround_encoder_init( |
michael@0 | 470 | OpusMSEncoder *st, |
michael@0 | 471 | opus_int32 Fs, |
michael@0 | 472 | int channels, |
michael@0 | 473 | int mapping_family, |
michael@0 | 474 | int *streams, |
michael@0 | 475 | int *coupled_streams, |
michael@0 | 476 | unsigned char *mapping, |
michael@0 | 477 | int application |
michael@0 | 478 | ) |
michael@0 | 479 | { |
michael@0 | 480 | if ((channels>255) || (channels<1)) |
michael@0 | 481 | return OPUS_BAD_ARG; |
michael@0 | 482 | st->lfe_stream = -1; |
michael@0 | 483 | if (mapping_family==0) |
michael@0 | 484 | { |
michael@0 | 485 | if (channels==1) |
michael@0 | 486 | { |
michael@0 | 487 | *streams=1; |
michael@0 | 488 | *coupled_streams=0; |
michael@0 | 489 | mapping[0]=0; |
michael@0 | 490 | } else if (channels==2) |
michael@0 | 491 | { |
michael@0 | 492 | *streams=1; |
michael@0 | 493 | *coupled_streams=1; |
michael@0 | 494 | mapping[0]=0; |
michael@0 | 495 | mapping[1]=1; |
michael@0 | 496 | } else |
michael@0 | 497 | return OPUS_UNIMPLEMENTED; |
michael@0 | 498 | } else if (mapping_family==1 && channels<=8 && channels>=1) |
michael@0 | 499 | { |
michael@0 | 500 | int i; |
michael@0 | 501 | *streams=vorbis_mappings[channels-1].nb_streams; |
michael@0 | 502 | *coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams; |
michael@0 | 503 | for (i=0;i<channels;i++) |
michael@0 | 504 | mapping[i] = vorbis_mappings[channels-1].mapping[i]; |
michael@0 | 505 | if (channels>=6) |
michael@0 | 506 | st->lfe_stream = *streams-1; |
michael@0 | 507 | } else if (mapping_family==255) |
michael@0 | 508 | { |
michael@0 | 509 | int i; |
michael@0 | 510 | *streams=channels; |
michael@0 | 511 | *coupled_streams=0; |
michael@0 | 512 | for(i=0;i<channels;i++) |
michael@0 | 513 | mapping[i] = i; |
michael@0 | 514 | } else |
michael@0 | 515 | return OPUS_UNIMPLEMENTED; |
michael@0 | 516 | return opus_multistream_encoder_init_impl(st, Fs, channels, *streams, *coupled_streams, |
michael@0 | 517 | mapping, application, channels>2&&mapping_family==1); |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | OpusMSEncoder *opus_multistream_encoder_create( |
michael@0 | 521 | opus_int32 Fs, |
michael@0 | 522 | int channels, |
michael@0 | 523 | int streams, |
michael@0 | 524 | int coupled_streams, |
michael@0 | 525 | const unsigned char *mapping, |
michael@0 | 526 | int application, |
michael@0 | 527 | int *error |
michael@0 | 528 | ) |
michael@0 | 529 | { |
michael@0 | 530 | int ret; |
michael@0 | 531 | OpusMSEncoder *st; |
michael@0 | 532 | if ((channels>255) || (channels<1) || (coupled_streams>streams) || |
michael@0 | 533 | (coupled_streams+streams>255) || (streams<1) || (coupled_streams<0)) |
michael@0 | 534 | { |
michael@0 | 535 | if (error) |
michael@0 | 536 | *error = OPUS_BAD_ARG; |
michael@0 | 537 | return NULL; |
michael@0 | 538 | } |
michael@0 | 539 | st = (OpusMSEncoder *)opus_alloc(opus_multistream_encoder_get_size(streams, coupled_streams)); |
michael@0 | 540 | if (st==NULL) |
michael@0 | 541 | { |
michael@0 | 542 | if (error) |
michael@0 | 543 | *error = OPUS_ALLOC_FAIL; |
michael@0 | 544 | return NULL; |
michael@0 | 545 | } |
michael@0 | 546 | ret = opus_multistream_encoder_init(st, Fs, channels, streams, coupled_streams, mapping, application); |
michael@0 | 547 | if (ret != OPUS_OK) |
michael@0 | 548 | { |
michael@0 | 549 | opus_free(st); |
michael@0 | 550 | st = NULL; |
michael@0 | 551 | } |
michael@0 | 552 | if (error) |
michael@0 | 553 | *error = ret; |
michael@0 | 554 | return st; |
michael@0 | 555 | } |
michael@0 | 556 | |
michael@0 | 557 | OpusMSEncoder *opus_multistream_surround_encoder_create( |
michael@0 | 558 | opus_int32 Fs, |
michael@0 | 559 | int channels, |
michael@0 | 560 | int mapping_family, |
michael@0 | 561 | int *streams, |
michael@0 | 562 | int *coupled_streams, |
michael@0 | 563 | unsigned char *mapping, |
michael@0 | 564 | int application, |
michael@0 | 565 | int *error |
michael@0 | 566 | ) |
michael@0 | 567 | { |
michael@0 | 568 | int ret; |
michael@0 | 569 | OpusMSEncoder *st; |
michael@0 | 570 | if ((channels>255) || (channels<1)) |
michael@0 | 571 | { |
michael@0 | 572 | if (error) |
michael@0 | 573 | *error = OPUS_BAD_ARG; |
michael@0 | 574 | return NULL; |
michael@0 | 575 | } |
michael@0 | 576 | st = (OpusMSEncoder *)opus_alloc(opus_multistream_surround_encoder_get_size(channels, mapping_family)); |
michael@0 | 577 | if (st==NULL) |
michael@0 | 578 | { |
michael@0 | 579 | if (error) |
michael@0 | 580 | *error = OPUS_ALLOC_FAIL; |
michael@0 | 581 | return NULL; |
michael@0 | 582 | } |
michael@0 | 583 | ret = opus_multistream_surround_encoder_init(st, Fs, channels, mapping_family, streams, coupled_streams, mapping, application); |
michael@0 | 584 | if (ret != OPUS_OK) |
michael@0 | 585 | { |
michael@0 | 586 | opus_free(st); |
michael@0 | 587 | st = NULL; |
michael@0 | 588 | } |
michael@0 | 589 | if (error) |
michael@0 | 590 | *error = ret; |
michael@0 | 591 | return st; |
michael@0 | 592 | } |
michael@0 | 593 | |
michael@0 | 594 | static void surround_rate_allocation( |
michael@0 | 595 | OpusMSEncoder *st, |
michael@0 | 596 | opus_int32 *rate, |
michael@0 | 597 | int frame_size |
michael@0 | 598 | ) |
michael@0 | 599 | { |
michael@0 | 600 | int i; |
michael@0 | 601 | opus_int32 channel_rate; |
michael@0 | 602 | opus_int32 Fs; |
michael@0 | 603 | char *ptr; |
michael@0 | 604 | int stream_offset; |
michael@0 | 605 | int lfe_offset; |
michael@0 | 606 | int coupled_ratio; /* Q8 */ |
michael@0 | 607 | int lfe_ratio; /* Q8 */ |
michael@0 | 608 | |
michael@0 | 609 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
michael@0 | 610 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs)); |
michael@0 | 611 | |
michael@0 | 612 | if (st->bitrate_bps > st->layout.nb_channels*40000) |
michael@0 | 613 | stream_offset = 20000; |
michael@0 | 614 | else |
michael@0 | 615 | stream_offset = st->bitrate_bps/st->layout.nb_channels/2; |
michael@0 | 616 | stream_offset += 60*(Fs/frame_size-50); |
michael@0 | 617 | /* We start by giving each stream (coupled or uncoupled) the same bitrate. |
michael@0 | 618 | This models the main saving of coupled channels over uncoupled. */ |
michael@0 | 619 | /* The LFE stream is an exception to the above and gets fewer bits. */ |
michael@0 | 620 | lfe_offset = 3500 + 60*(Fs/frame_size-50); |
michael@0 | 621 | /* Coupled streams get twice the mono rate after the first 20 kb/s. */ |
michael@0 | 622 | coupled_ratio = 512; |
michael@0 | 623 | /* Should depend on the bitrate, for now we assume LFE gets 1/8 the bits of mono */ |
michael@0 | 624 | lfe_ratio = 32; |
michael@0 | 625 | |
michael@0 | 626 | /* Compute bitrate allocation between streams */ |
michael@0 | 627 | if (st->bitrate_bps==OPUS_AUTO) |
michael@0 | 628 | { |
michael@0 | 629 | channel_rate = Fs+60*Fs/frame_size; |
michael@0 | 630 | } else if (st->bitrate_bps==OPUS_BITRATE_MAX) |
michael@0 | 631 | { |
michael@0 | 632 | channel_rate = 300000; |
michael@0 | 633 | } else { |
michael@0 | 634 | int nb_lfe; |
michael@0 | 635 | int nb_uncoupled; |
michael@0 | 636 | int nb_coupled; |
michael@0 | 637 | int total; |
michael@0 | 638 | nb_lfe = (st->lfe_stream!=-1); |
michael@0 | 639 | nb_coupled = st->layout.nb_coupled_streams; |
michael@0 | 640 | nb_uncoupled = st->layout.nb_streams-nb_coupled-nb_lfe; |
michael@0 | 641 | total = (nb_uncoupled<<8) /* mono */ |
michael@0 | 642 | + coupled_ratio*nb_coupled /* stereo */ |
michael@0 | 643 | + nb_lfe*lfe_ratio; |
michael@0 | 644 | channel_rate = 256*(st->bitrate_bps-lfe_offset*nb_lfe-stream_offset*(nb_coupled+nb_uncoupled))/total; |
michael@0 | 645 | } |
michael@0 | 646 | #ifndef FIXED_POINT |
michael@0 | 647 | if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50) |
michael@0 | 648 | { |
michael@0 | 649 | opus_int32 bonus; |
michael@0 | 650 | bonus = 60*(Fs/frame_size-50); |
michael@0 | 651 | channel_rate += bonus; |
michael@0 | 652 | } |
michael@0 | 653 | #endif |
michael@0 | 654 | |
michael@0 | 655 | for (i=0;i<st->layout.nb_streams;i++) |
michael@0 | 656 | { |
michael@0 | 657 | if (i<st->layout.nb_coupled_streams) |
michael@0 | 658 | rate[i] = stream_offset+(channel_rate*coupled_ratio>>8); |
michael@0 | 659 | else if (i!=st->lfe_stream) |
michael@0 | 660 | rate[i] = stream_offset+channel_rate; |
michael@0 | 661 | else |
michael@0 | 662 | rate[i] = lfe_offset+(channel_rate*lfe_ratio>>8); |
michael@0 | 663 | } |
michael@0 | 664 | } |
michael@0 | 665 | |
michael@0 | 666 | /* Max size in case the encoder decides to return three frames */ |
michael@0 | 667 | #define MS_FRAME_TMP (3*1275+7) |
michael@0 | 668 | static int opus_multistream_encode_native |
michael@0 | 669 | ( |
michael@0 | 670 | OpusMSEncoder *st, |
michael@0 | 671 | opus_copy_channel_in_func copy_channel_in, |
michael@0 | 672 | const void *pcm, |
michael@0 | 673 | int analysis_frame_size, |
michael@0 | 674 | unsigned char *data, |
michael@0 | 675 | opus_int32 max_data_bytes, |
michael@0 | 676 | int lsb_depth, |
michael@0 | 677 | downmix_func downmix |
michael@0 | 678 | ) |
michael@0 | 679 | { |
michael@0 | 680 | opus_int32 Fs; |
michael@0 | 681 | int coupled_size; |
michael@0 | 682 | int mono_size; |
michael@0 | 683 | int s; |
michael@0 | 684 | char *ptr; |
michael@0 | 685 | int tot_size; |
michael@0 | 686 | VARDECL(opus_val16, buf); |
michael@0 | 687 | VARDECL(opus_val16, bandSMR); |
michael@0 | 688 | unsigned char tmp_data[MS_FRAME_TMP]; |
michael@0 | 689 | OpusRepacketizer rp; |
michael@0 | 690 | opus_int32 vbr; |
michael@0 | 691 | const CELTMode *celt_mode; |
michael@0 | 692 | opus_int32 bitrates[256]; |
michael@0 | 693 | opus_val16 bandLogE[42]; |
michael@0 | 694 | opus_val32 *mem = NULL; |
michael@0 | 695 | opus_val32 *preemph_mem=NULL; |
michael@0 | 696 | int frame_size; |
michael@0 | 697 | ALLOC_STACK; |
michael@0 | 698 | |
michael@0 | 699 | if (st->surround) |
michael@0 | 700 | { |
michael@0 | 701 | preemph_mem = ms_get_preemph_mem(st); |
michael@0 | 702 | mem = ms_get_window_mem(st); |
michael@0 | 703 | } |
michael@0 | 704 | |
michael@0 | 705 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
michael@0 | 706 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs)); |
michael@0 | 707 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_VBR(&vbr)); |
michael@0 | 708 | opus_encoder_ctl((OpusEncoder*)ptr, CELT_GET_MODE(&celt_mode)); |
michael@0 | 709 | |
michael@0 | 710 | { |
michael@0 | 711 | opus_int32 delay_compensation; |
michael@0 | 712 | int channels; |
michael@0 | 713 | |
michael@0 | 714 | channels = st->layout.nb_streams + st->layout.nb_coupled_streams; |
michael@0 | 715 | opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_LOOKAHEAD(&delay_compensation)); |
michael@0 | 716 | delay_compensation -= Fs/400; |
michael@0 | 717 | frame_size = compute_frame_size(pcm, analysis_frame_size, |
michael@0 | 718 | st->variable_duration, channels, Fs, st->bitrate_bps, |
michael@0 | 719 | delay_compensation, downmix |
michael@0 | 720 | #ifndef DISABLE_FLOAT_API |
michael@0 | 721 | , st->subframe_mem |
michael@0 | 722 | #endif |
michael@0 | 723 | ); |
michael@0 | 724 | } |
michael@0 | 725 | |
michael@0 | 726 | if (400*frame_size < Fs) |
michael@0 | 727 | { |
michael@0 | 728 | RESTORE_STACK; |
michael@0 | 729 | return OPUS_BAD_ARG; |
michael@0 | 730 | } |
michael@0 | 731 | /* Validate frame_size before using it to allocate stack space. |
michael@0 | 732 | This mirrors the checks in opus_encode[_float](). */ |
michael@0 | 733 | if (400*frame_size != Fs && 200*frame_size != Fs && |
michael@0 | 734 | 100*frame_size != Fs && 50*frame_size != Fs && |
michael@0 | 735 | 25*frame_size != Fs && 50*frame_size != 3*Fs) |
michael@0 | 736 | { |
michael@0 | 737 | RESTORE_STACK; |
michael@0 | 738 | return OPUS_BAD_ARG; |
michael@0 | 739 | } |
michael@0 | 740 | ALLOC(buf, 2*frame_size, opus_val16); |
michael@0 | 741 | coupled_size = opus_encoder_get_size(2); |
michael@0 | 742 | mono_size = opus_encoder_get_size(1); |
michael@0 | 743 | |
michael@0 | 744 | ALLOC(bandSMR, 21*st->layout.nb_channels, opus_val16); |
michael@0 | 745 | if (st->surround) |
michael@0 | 746 | { |
michael@0 | 747 | surround_analysis(celt_mode, pcm, bandSMR, mem, preemph_mem, frame_size, 120, st->layout.nb_channels, Fs, copy_channel_in); |
michael@0 | 748 | } |
michael@0 | 749 | |
michael@0 | 750 | if (max_data_bytes < 4*st->layout.nb_streams-1) |
michael@0 | 751 | { |
michael@0 | 752 | RESTORE_STACK; |
michael@0 | 753 | return OPUS_BUFFER_TOO_SMALL; |
michael@0 | 754 | } |
michael@0 | 755 | |
michael@0 | 756 | /* Compute bitrate allocation between streams (this could be a lot better) */ |
michael@0 | 757 | surround_rate_allocation(st, bitrates, frame_size); |
michael@0 | 758 | |
michael@0 | 759 | if (!vbr) |
michael@0 | 760 | max_data_bytes = IMIN(max_data_bytes, 3*st->bitrate_bps/(3*8*Fs/frame_size)); |
michael@0 | 761 | |
michael@0 | 762 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
michael@0 | 763 | for (s=0;s<st->layout.nb_streams;s++) |
michael@0 | 764 | { |
michael@0 | 765 | OpusEncoder *enc; |
michael@0 | 766 | enc = (OpusEncoder*)ptr; |
michael@0 | 767 | if (s < st->layout.nb_coupled_streams) |
michael@0 | 768 | ptr += align(coupled_size); |
michael@0 | 769 | else |
michael@0 | 770 | ptr += align(mono_size); |
michael@0 | 771 | opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrates[s])); |
michael@0 | 772 | if (st->surround) |
michael@0 | 773 | { |
michael@0 | 774 | opus_int32 equiv_rate; |
michael@0 | 775 | equiv_rate = st->bitrate_bps; |
michael@0 | 776 | if (frame_size*50 < Fs) |
michael@0 | 777 | equiv_rate -= 60*(Fs/frame_size - 50)*st->layout.nb_channels; |
michael@0 | 778 | if (equiv_rate > 10000*st->layout.nb_channels) |
michael@0 | 779 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND)); |
michael@0 | 780 | else if (equiv_rate > 7000*st->layout.nb_channels) |
michael@0 | 781 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND)); |
michael@0 | 782 | else if (equiv_rate > 5000*st->layout.nb_channels) |
michael@0 | 783 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND)); |
michael@0 | 784 | else |
michael@0 | 785 | opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND)); |
michael@0 | 786 | if (s < st->layout.nb_coupled_streams) |
michael@0 | 787 | { |
michael@0 | 788 | /* To preserve the spatial image, force stereo CELT on coupled streams */ |
michael@0 | 789 | opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_CELT_ONLY)); |
michael@0 | 790 | opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(2)); |
michael@0 | 791 | } |
michael@0 | 792 | } |
michael@0 | 793 | } |
michael@0 | 794 | |
michael@0 | 795 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
michael@0 | 796 | /* Counting ToC */ |
michael@0 | 797 | tot_size = 0; |
michael@0 | 798 | for (s=0;s<st->layout.nb_streams;s++) |
michael@0 | 799 | { |
michael@0 | 800 | OpusEncoder *enc; |
michael@0 | 801 | int len; |
michael@0 | 802 | int curr_max; |
michael@0 | 803 | int c1, c2; |
michael@0 | 804 | |
michael@0 | 805 | opus_repacketizer_init(&rp); |
michael@0 | 806 | enc = (OpusEncoder*)ptr; |
michael@0 | 807 | if (s < st->layout.nb_coupled_streams) |
michael@0 | 808 | { |
michael@0 | 809 | int i; |
michael@0 | 810 | int left, right; |
michael@0 | 811 | left = get_left_channel(&st->layout, s, -1); |
michael@0 | 812 | right = get_right_channel(&st->layout, s, -1); |
michael@0 | 813 | (*copy_channel_in)(buf, 2, |
michael@0 | 814 | pcm, st->layout.nb_channels, left, frame_size); |
michael@0 | 815 | (*copy_channel_in)(buf+1, 2, |
michael@0 | 816 | pcm, st->layout.nb_channels, right, frame_size); |
michael@0 | 817 | ptr += align(coupled_size); |
michael@0 | 818 | if (st->surround) |
michael@0 | 819 | { |
michael@0 | 820 | for (i=0;i<21;i++) |
michael@0 | 821 | { |
michael@0 | 822 | bandLogE[i] = bandSMR[21*left+i]; |
michael@0 | 823 | bandLogE[21+i] = bandSMR[21*right+i]; |
michael@0 | 824 | } |
michael@0 | 825 | } |
michael@0 | 826 | c1 = left; |
michael@0 | 827 | c2 = right; |
michael@0 | 828 | } else { |
michael@0 | 829 | int i; |
michael@0 | 830 | int chan = get_mono_channel(&st->layout, s, -1); |
michael@0 | 831 | (*copy_channel_in)(buf, 1, |
michael@0 | 832 | pcm, st->layout.nb_channels, chan, frame_size); |
michael@0 | 833 | ptr += align(mono_size); |
michael@0 | 834 | if (st->surround) |
michael@0 | 835 | { |
michael@0 | 836 | for (i=0;i<21;i++) |
michael@0 | 837 | bandLogE[i] = bandSMR[21*chan+i]; |
michael@0 | 838 | } |
michael@0 | 839 | c1 = chan; |
michael@0 | 840 | c2 = -1; |
michael@0 | 841 | } |
michael@0 | 842 | if (st->surround) |
michael@0 | 843 | opus_encoder_ctl(enc, OPUS_SET_ENERGY_MASK(bandLogE)); |
michael@0 | 844 | /* number of bytes left (+Toc) */ |
michael@0 | 845 | curr_max = max_data_bytes - tot_size; |
michael@0 | 846 | /* Reserve three bytes for the last stream and four for the others */ |
michael@0 | 847 | curr_max -= IMAX(0,4*(st->layout.nb_streams-s-1)-1); |
michael@0 | 848 | curr_max = IMIN(curr_max,MS_FRAME_TMP); |
michael@0 | 849 | if (!vbr && s == st->layout.nb_streams-1) |
michael@0 | 850 | opus_encoder_ctl(enc, OPUS_SET_BITRATE(curr_max*(8*Fs/frame_size))); |
michael@0 | 851 | len = opus_encode_native(enc, buf, frame_size, tmp_data, curr_max, lsb_depth, |
michael@0 | 852 | pcm, analysis_frame_size, c1, c2, st->layout.nb_channels, downmix); |
michael@0 | 853 | if (len<0) |
michael@0 | 854 | { |
michael@0 | 855 | RESTORE_STACK; |
michael@0 | 856 | return len; |
michael@0 | 857 | } |
michael@0 | 858 | /* We need to use the repacketizer to add the self-delimiting lengths |
michael@0 | 859 | while taking into account the fact that the encoder can now return |
michael@0 | 860 | more than one frame at a time (e.g. 60 ms CELT-only) */ |
michael@0 | 861 | opus_repacketizer_cat(&rp, tmp_data, len); |
michael@0 | 862 | len = opus_repacketizer_out_range_impl(&rp, 0, opus_repacketizer_get_nb_frames(&rp), |
michael@0 | 863 | data, max_data_bytes-tot_size, s != st->layout.nb_streams-1, !vbr && s == st->layout.nb_streams-1); |
michael@0 | 864 | data += len; |
michael@0 | 865 | tot_size += len; |
michael@0 | 866 | } |
michael@0 | 867 | /*printf("\n");*/ |
michael@0 | 868 | RESTORE_STACK; |
michael@0 | 869 | return tot_size; |
michael@0 | 870 | } |
michael@0 | 871 | |
michael@0 | 872 | #if !defined(DISABLE_FLOAT_API) |
michael@0 | 873 | static void opus_copy_channel_in_float( |
michael@0 | 874 | opus_val16 *dst, |
michael@0 | 875 | int dst_stride, |
michael@0 | 876 | const void *src, |
michael@0 | 877 | int src_stride, |
michael@0 | 878 | int src_channel, |
michael@0 | 879 | int frame_size |
michael@0 | 880 | ) |
michael@0 | 881 | { |
michael@0 | 882 | const float *float_src; |
michael@0 | 883 | opus_int32 i; |
michael@0 | 884 | float_src = (const float *)src; |
michael@0 | 885 | for (i=0;i<frame_size;i++) |
michael@0 | 886 | #if defined(FIXED_POINT) |
michael@0 | 887 | dst[i*dst_stride] = FLOAT2INT16(float_src[i*src_stride+src_channel]); |
michael@0 | 888 | #else |
michael@0 | 889 | dst[i*dst_stride] = float_src[i*src_stride+src_channel]; |
michael@0 | 890 | #endif |
michael@0 | 891 | } |
michael@0 | 892 | #endif |
michael@0 | 893 | |
michael@0 | 894 | static void opus_copy_channel_in_short( |
michael@0 | 895 | opus_val16 *dst, |
michael@0 | 896 | int dst_stride, |
michael@0 | 897 | const void *src, |
michael@0 | 898 | int src_stride, |
michael@0 | 899 | int src_channel, |
michael@0 | 900 | int frame_size |
michael@0 | 901 | ) |
michael@0 | 902 | { |
michael@0 | 903 | const opus_int16 *short_src; |
michael@0 | 904 | opus_int32 i; |
michael@0 | 905 | short_src = (const opus_int16 *)src; |
michael@0 | 906 | for (i=0;i<frame_size;i++) |
michael@0 | 907 | #if defined(FIXED_POINT) |
michael@0 | 908 | dst[i*dst_stride] = short_src[i*src_stride+src_channel]; |
michael@0 | 909 | #else |
michael@0 | 910 | dst[i*dst_stride] = (1/32768.f)*short_src[i*src_stride+src_channel]; |
michael@0 | 911 | #endif |
michael@0 | 912 | } |
michael@0 | 913 | |
michael@0 | 914 | |
michael@0 | 915 | #ifdef FIXED_POINT |
michael@0 | 916 | int opus_multistream_encode( |
michael@0 | 917 | OpusMSEncoder *st, |
michael@0 | 918 | const opus_val16 *pcm, |
michael@0 | 919 | int frame_size, |
michael@0 | 920 | unsigned char *data, |
michael@0 | 921 | opus_int32 max_data_bytes |
michael@0 | 922 | ) |
michael@0 | 923 | { |
michael@0 | 924 | return opus_multistream_encode_native(st, opus_copy_channel_in_short, |
michael@0 | 925 | pcm, frame_size, data, max_data_bytes, 16, downmix_int); |
michael@0 | 926 | } |
michael@0 | 927 | |
michael@0 | 928 | #ifndef DISABLE_FLOAT_API |
michael@0 | 929 | int opus_multistream_encode_float( |
michael@0 | 930 | OpusMSEncoder *st, |
michael@0 | 931 | const float *pcm, |
michael@0 | 932 | int frame_size, |
michael@0 | 933 | unsigned char *data, |
michael@0 | 934 | opus_int32 max_data_bytes |
michael@0 | 935 | ) |
michael@0 | 936 | { |
michael@0 | 937 | return opus_multistream_encode_native(st, opus_copy_channel_in_float, |
michael@0 | 938 | pcm, frame_size, data, max_data_bytes, 16, downmix_float); |
michael@0 | 939 | } |
michael@0 | 940 | #endif |
michael@0 | 941 | |
michael@0 | 942 | #else |
michael@0 | 943 | |
michael@0 | 944 | int opus_multistream_encode_float |
michael@0 | 945 | ( |
michael@0 | 946 | OpusMSEncoder *st, |
michael@0 | 947 | const opus_val16 *pcm, |
michael@0 | 948 | int frame_size, |
michael@0 | 949 | unsigned char *data, |
michael@0 | 950 | opus_int32 max_data_bytes |
michael@0 | 951 | ) |
michael@0 | 952 | { |
michael@0 | 953 | return opus_multistream_encode_native(st, opus_copy_channel_in_float, |
michael@0 | 954 | pcm, frame_size, data, max_data_bytes, 24, downmix_float); |
michael@0 | 955 | } |
michael@0 | 956 | |
michael@0 | 957 | int opus_multistream_encode( |
michael@0 | 958 | OpusMSEncoder *st, |
michael@0 | 959 | const opus_int16 *pcm, |
michael@0 | 960 | int frame_size, |
michael@0 | 961 | unsigned char *data, |
michael@0 | 962 | opus_int32 max_data_bytes |
michael@0 | 963 | ) |
michael@0 | 964 | { |
michael@0 | 965 | return opus_multistream_encode_native(st, opus_copy_channel_in_short, |
michael@0 | 966 | pcm, frame_size, data, max_data_bytes, 16, downmix_int); |
michael@0 | 967 | } |
michael@0 | 968 | #endif |
michael@0 | 969 | |
michael@0 | 970 | int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...) |
michael@0 | 971 | { |
michael@0 | 972 | va_list ap; |
michael@0 | 973 | int coupled_size, mono_size; |
michael@0 | 974 | char *ptr; |
michael@0 | 975 | int ret = OPUS_OK; |
michael@0 | 976 | |
michael@0 | 977 | va_start(ap, request); |
michael@0 | 978 | |
michael@0 | 979 | coupled_size = opus_encoder_get_size(2); |
michael@0 | 980 | mono_size = opus_encoder_get_size(1); |
michael@0 | 981 | ptr = (char*)st + align(sizeof(OpusMSEncoder)); |
michael@0 | 982 | switch (request) |
michael@0 | 983 | { |
michael@0 | 984 | case OPUS_SET_BITRATE_REQUEST: |
michael@0 | 985 | { |
michael@0 | 986 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 987 | if (value<0 && value!=OPUS_AUTO && value!=OPUS_BITRATE_MAX) |
michael@0 | 988 | { |
michael@0 | 989 | goto bad_arg; |
michael@0 | 990 | } |
michael@0 | 991 | st->bitrate_bps = value; |
michael@0 | 992 | } |
michael@0 | 993 | break; |
michael@0 | 994 | case OPUS_GET_BITRATE_REQUEST: |
michael@0 | 995 | { |
michael@0 | 996 | int s; |
michael@0 | 997 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 998 | if (!value) |
michael@0 | 999 | { |
michael@0 | 1000 | goto bad_arg; |
michael@0 | 1001 | } |
michael@0 | 1002 | *value = 0; |
michael@0 | 1003 | for (s=0;s<st->layout.nb_streams;s++) |
michael@0 | 1004 | { |
michael@0 | 1005 | opus_int32 rate; |
michael@0 | 1006 | OpusEncoder *enc; |
michael@0 | 1007 | enc = (OpusEncoder*)ptr; |
michael@0 | 1008 | if (s < st->layout.nb_coupled_streams) |
michael@0 | 1009 | ptr += align(coupled_size); |
michael@0 | 1010 | else |
michael@0 | 1011 | ptr += align(mono_size); |
michael@0 | 1012 | opus_encoder_ctl(enc, request, &rate); |
michael@0 | 1013 | *value += rate; |
michael@0 | 1014 | } |
michael@0 | 1015 | } |
michael@0 | 1016 | break; |
michael@0 | 1017 | case OPUS_GET_LSB_DEPTH_REQUEST: |
michael@0 | 1018 | case OPUS_GET_VBR_REQUEST: |
michael@0 | 1019 | case OPUS_GET_APPLICATION_REQUEST: |
michael@0 | 1020 | case OPUS_GET_BANDWIDTH_REQUEST: |
michael@0 | 1021 | case OPUS_GET_COMPLEXITY_REQUEST: |
michael@0 | 1022 | case OPUS_GET_PACKET_LOSS_PERC_REQUEST: |
michael@0 | 1023 | case OPUS_GET_DTX_REQUEST: |
michael@0 | 1024 | case OPUS_GET_VOICE_RATIO_REQUEST: |
michael@0 | 1025 | case OPUS_GET_VBR_CONSTRAINT_REQUEST: |
michael@0 | 1026 | case OPUS_GET_SIGNAL_REQUEST: |
michael@0 | 1027 | case OPUS_GET_LOOKAHEAD_REQUEST: |
michael@0 | 1028 | case OPUS_GET_SAMPLE_RATE_REQUEST: |
michael@0 | 1029 | case OPUS_GET_INBAND_FEC_REQUEST: |
michael@0 | 1030 | case OPUS_GET_FORCE_CHANNELS_REQUEST: |
michael@0 | 1031 | case OPUS_GET_PREDICTION_DISABLED_REQUEST: |
michael@0 | 1032 | { |
michael@0 | 1033 | OpusEncoder *enc; |
michael@0 | 1034 | /* For int32* GET params, just query the first stream */ |
michael@0 | 1035 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 1036 | enc = (OpusEncoder*)ptr; |
michael@0 | 1037 | ret = opus_encoder_ctl(enc, request, value); |
michael@0 | 1038 | } |
michael@0 | 1039 | break; |
michael@0 | 1040 | case OPUS_GET_FINAL_RANGE_REQUEST: |
michael@0 | 1041 | { |
michael@0 | 1042 | int s; |
michael@0 | 1043 | opus_uint32 *value = va_arg(ap, opus_uint32*); |
michael@0 | 1044 | opus_uint32 tmp; |
michael@0 | 1045 | if (!value) |
michael@0 | 1046 | { |
michael@0 | 1047 | goto bad_arg; |
michael@0 | 1048 | } |
michael@0 | 1049 | *value=0; |
michael@0 | 1050 | for (s=0;s<st->layout.nb_streams;s++) |
michael@0 | 1051 | { |
michael@0 | 1052 | OpusEncoder *enc; |
michael@0 | 1053 | enc = (OpusEncoder*)ptr; |
michael@0 | 1054 | if (s < st->layout.nb_coupled_streams) |
michael@0 | 1055 | ptr += align(coupled_size); |
michael@0 | 1056 | else |
michael@0 | 1057 | ptr += align(mono_size); |
michael@0 | 1058 | ret = opus_encoder_ctl(enc, request, &tmp); |
michael@0 | 1059 | if (ret != OPUS_OK) break; |
michael@0 | 1060 | *value ^= tmp; |
michael@0 | 1061 | } |
michael@0 | 1062 | } |
michael@0 | 1063 | break; |
michael@0 | 1064 | case OPUS_SET_LSB_DEPTH_REQUEST: |
michael@0 | 1065 | case OPUS_SET_COMPLEXITY_REQUEST: |
michael@0 | 1066 | case OPUS_SET_VBR_REQUEST: |
michael@0 | 1067 | case OPUS_SET_VBR_CONSTRAINT_REQUEST: |
michael@0 | 1068 | case OPUS_SET_MAX_BANDWIDTH_REQUEST: |
michael@0 | 1069 | case OPUS_SET_BANDWIDTH_REQUEST: |
michael@0 | 1070 | case OPUS_SET_SIGNAL_REQUEST: |
michael@0 | 1071 | case OPUS_SET_APPLICATION_REQUEST: |
michael@0 | 1072 | case OPUS_SET_INBAND_FEC_REQUEST: |
michael@0 | 1073 | case OPUS_SET_PACKET_LOSS_PERC_REQUEST: |
michael@0 | 1074 | case OPUS_SET_DTX_REQUEST: |
michael@0 | 1075 | case OPUS_SET_FORCE_MODE_REQUEST: |
michael@0 | 1076 | case OPUS_SET_FORCE_CHANNELS_REQUEST: |
michael@0 | 1077 | case OPUS_SET_PREDICTION_DISABLED_REQUEST: |
michael@0 | 1078 | { |
michael@0 | 1079 | int s; |
michael@0 | 1080 | /* This works for int32 params */ |
michael@0 | 1081 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 1082 | for (s=0;s<st->layout.nb_streams;s++) |
michael@0 | 1083 | { |
michael@0 | 1084 | OpusEncoder *enc; |
michael@0 | 1085 | |
michael@0 | 1086 | enc = (OpusEncoder*)ptr; |
michael@0 | 1087 | if (s < st->layout.nb_coupled_streams) |
michael@0 | 1088 | ptr += align(coupled_size); |
michael@0 | 1089 | else |
michael@0 | 1090 | ptr += align(mono_size); |
michael@0 | 1091 | ret = opus_encoder_ctl(enc, request, value); |
michael@0 | 1092 | if (ret != OPUS_OK) |
michael@0 | 1093 | break; |
michael@0 | 1094 | } |
michael@0 | 1095 | } |
michael@0 | 1096 | break; |
michael@0 | 1097 | case OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST: |
michael@0 | 1098 | { |
michael@0 | 1099 | int s; |
michael@0 | 1100 | opus_int32 stream_id; |
michael@0 | 1101 | OpusEncoder **value; |
michael@0 | 1102 | stream_id = va_arg(ap, opus_int32); |
michael@0 | 1103 | if (stream_id<0 || stream_id >= st->layout.nb_streams) |
michael@0 | 1104 | ret = OPUS_BAD_ARG; |
michael@0 | 1105 | value = va_arg(ap, OpusEncoder**); |
michael@0 | 1106 | if (!value) |
michael@0 | 1107 | { |
michael@0 | 1108 | goto bad_arg; |
michael@0 | 1109 | } |
michael@0 | 1110 | for (s=0;s<stream_id;s++) |
michael@0 | 1111 | { |
michael@0 | 1112 | if (s < st->layout.nb_coupled_streams) |
michael@0 | 1113 | ptr += align(coupled_size); |
michael@0 | 1114 | else |
michael@0 | 1115 | ptr += align(mono_size); |
michael@0 | 1116 | } |
michael@0 | 1117 | *value = (OpusEncoder*)ptr; |
michael@0 | 1118 | } |
michael@0 | 1119 | break; |
michael@0 | 1120 | case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: |
michael@0 | 1121 | { |
michael@0 | 1122 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 1123 | st->variable_duration = value; |
michael@0 | 1124 | } |
michael@0 | 1125 | break; |
michael@0 | 1126 | case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST: |
michael@0 | 1127 | { |
michael@0 | 1128 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 1129 | if (!value) |
michael@0 | 1130 | { |
michael@0 | 1131 | goto bad_arg; |
michael@0 | 1132 | } |
michael@0 | 1133 | *value = st->variable_duration; |
michael@0 | 1134 | } |
michael@0 | 1135 | break; |
michael@0 | 1136 | case OPUS_RESET_STATE: |
michael@0 | 1137 | { |
michael@0 | 1138 | int s; |
michael@0 | 1139 | st->subframe_mem[0] = st->subframe_mem[1] = st->subframe_mem[2] = 0; |
michael@0 | 1140 | if (st->surround) |
michael@0 | 1141 | { |
michael@0 | 1142 | OPUS_CLEAR(ms_get_preemph_mem(st), st->layout.nb_channels); |
michael@0 | 1143 | OPUS_CLEAR(ms_get_window_mem(st), st->layout.nb_channels*120); |
michael@0 | 1144 | } |
michael@0 | 1145 | for (s=0;s<st->layout.nb_streams;s++) |
michael@0 | 1146 | { |
michael@0 | 1147 | OpusEncoder *enc; |
michael@0 | 1148 | enc = (OpusEncoder*)ptr; |
michael@0 | 1149 | if (s < st->layout.nb_coupled_streams) |
michael@0 | 1150 | ptr += align(coupled_size); |
michael@0 | 1151 | else |
michael@0 | 1152 | ptr += align(mono_size); |
michael@0 | 1153 | ret = opus_encoder_ctl(enc, OPUS_RESET_STATE); |
michael@0 | 1154 | if (ret != OPUS_OK) |
michael@0 | 1155 | break; |
michael@0 | 1156 | } |
michael@0 | 1157 | } |
michael@0 | 1158 | break; |
michael@0 | 1159 | default: |
michael@0 | 1160 | ret = OPUS_UNIMPLEMENTED; |
michael@0 | 1161 | break; |
michael@0 | 1162 | } |
michael@0 | 1163 | |
michael@0 | 1164 | va_end(ap); |
michael@0 | 1165 | return ret; |
michael@0 | 1166 | bad_arg: |
michael@0 | 1167 | va_end(ap); |
michael@0 | 1168 | return OPUS_BAD_ARG; |
michael@0 | 1169 | } |
michael@0 | 1170 | |
michael@0 | 1171 | void opus_multistream_encoder_destroy(OpusMSEncoder *st) |
michael@0 | 1172 | { |
michael@0 | 1173 | opus_free(st); |
michael@0 | 1174 | } |