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