Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: third_party/opus/src/src/opus_multistream_encoder.c

Issue 2962373002: [Opus] Update to v1.2.1 (Closed)
Patch Set: Pre-increment instead of post-increment Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* Copyright (c) 2011 Xiph.Org Foundation 1 /* Copyright (c) 2011 Xiph.Org Foundation
2 Written by Jean-Marc Valin */ 2 Written by Jean-Marc Valin */
3 /* 3 /*
4 Redistribution and use in source and binary forms, with or without 4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions 5 modification, are permitted provided that the following conditions
6 are met: 6 are met:
7 7
8 - Redistributions of source code must retain the above copyright 8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer. 9 notice, this list of conditions and the following disclaimer.
10 10
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 } MappingType; 80 } MappingType;
81 81
82 struct OpusMSEncoder { 82 struct OpusMSEncoder {
83 ChannelLayout layout; 83 ChannelLayout layout;
84 int arch; 84 int arch;
85 int lfe_stream; 85 int lfe_stream;
86 int application; 86 int application;
87 int variable_duration; 87 int variable_duration;
88 MappingType mapping_type; 88 MappingType mapping_type;
89 opus_int32 bitrate_bps; 89 opus_int32 bitrate_bps;
90 float subframe_mem[3];
91 /* Encoder states go here */ 90 /* Encoder states go here */
92 /* then opus_val32 window_mem[channels*120]; */ 91 /* then opus_val32 window_mem[channels*120]; */
93 /* then opus_val32 preemph_mem[channels]; */ 92 /* then opus_val32 preemph_mem[channels]; */
94 }; 93 };
95 94
96 static opus_val32 *ms_get_preemph_mem(OpusMSEncoder *st) 95 static opus_val32 *ms_get_preemph_mem(OpusMSEncoder *st)
97 { 96 {
98 int s; 97 int s;
99 char *ptr; 98 char *ptr;
100 int coupled_size, mono_size; 99 int coupled_size, mono_size;
(...skipping 25 matching lines...) Expand all
126 { 125 {
127 if (s < st->layout.nb_coupled_streams) 126 if (s < st->layout.nb_coupled_streams)
128 ptr += align(coupled_size); 127 ptr += align(coupled_size);
129 else 128 else
130 ptr += align(mono_size); 129 ptr += align(mono_size);
131 } 130 }
132 /* void* cast avoids clang -Wcast-align warning */ 131 /* void* cast avoids clang -Wcast-align warning */
133 return (opus_val32*)(void*)ptr; 132 return (opus_val32*)(void*)ptr;
134 } 133 }
135 134
135 #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
136 static int validate_ambisonics(int nb_channels, int *nb_streams, int *nb_coupled _streams)
137 {
138 int order_plus_one;
139 int acn_channels;
140 int nondiegetic_channels;
141
142 order_plus_one = isqrt32(nb_channels);
143 acn_channels = order_plus_one * order_plus_one;
144 nondiegetic_channels = nb_channels - acn_channels;
145
146 if (order_plus_one < 1 || order_plus_one > 15 ||
147 (nondiegetic_channels != 0 && nondiegetic_channels != 2))
148 return 0;
149
150 if (nb_streams)
151 *nb_streams = acn_channels + (nondiegetic_channels != 0);
152 if (nb_coupled_streams)
153 *nb_coupled_streams = nondiegetic_channels != 0;
154 return 1;
155 }
156 #endif
157
136 static int validate_encoder_layout(const ChannelLayout *layout) 158 static int validate_encoder_layout(const ChannelLayout *layout)
137 { 159 {
138 int s; 160 int s;
139 for (s=0;s<layout->nb_streams;s++) 161 for (s=0;s<layout->nb_streams;s++)
140 { 162 {
141 if (s < layout->nb_coupled_streams) 163 if (s < layout->nb_coupled_streams)
142 { 164 {
143 if (get_left_channel(layout, s, -1)==-1) 165 if (get_left_channel(layout, s, -1)==-1)
144 return 0; 166 return 0;
145 if (get_right_channel(layout, s, -1)==-1) 167 if (get_right_channel(layout, s, -1)==-1)
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *b andLogE, opus_val32 *mem, opus_val32 *preemph_mem, 255 void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *b andLogE, opus_val32 *mem, opus_val32 *preemph_mem,
234 int len, int overlap, int channels, int rate, opus_copy_channel_in_func co py_channel_in, int arch 256 int len, int overlap, int channels, int rate, opus_copy_channel_in_func co py_channel_in, int arch
235 ) 257 )
236 { 258 {
237 int c; 259 int c;
238 int i; 260 int i;
239 int LM; 261 int LM;
240 int pos[8] = {0}; 262 int pos[8] = {0};
241 int upsample; 263 int upsample;
242 int frame_size; 264 int frame_size;
265 int freq_size;
243 opus_val16 channel_offset; 266 opus_val16 channel_offset;
244 opus_val32 bandE[21]; 267 opus_val32 bandE[21];
245 opus_val16 maskLogE[3][21]; 268 opus_val16 maskLogE[3][21];
246 VARDECL(opus_val32, in); 269 VARDECL(opus_val32, in);
247 VARDECL(opus_val16, x); 270 VARDECL(opus_val16, x);
248 VARDECL(opus_val32, freq); 271 VARDECL(opus_val32, freq);
249 SAVE_STACK; 272 SAVE_STACK;
250 273
251 upsample = resampling_factor(rate); 274 upsample = resampling_factor(rate);
252 frame_size = len*upsample; 275 frame_size = len*upsample;
276 freq_size = IMIN(960, frame_size);
253 277
254 /* LM = log2(frame_size / 120) */ 278 /* LM = log2(frame_size / 120) */
255 for (LM=0;LM<celt_mode->maxLM;LM++) 279 for (LM=0;LM<celt_mode->maxLM;LM++)
256 if (celt_mode->shortMdctSize<<LM==frame_size) 280 if (celt_mode->shortMdctSize<<LM==frame_size)
257 break; 281 break;
258 282
259 ALLOC(in, frame_size+overlap, opus_val32); 283 ALLOC(in, frame_size+overlap, opus_val32);
260 ALLOC(x, len, opus_val16); 284 ALLOC(x, len, opus_val16);
261 ALLOC(freq, frame_size, opus_val32); 285 ALLOC(freq, freq_size, opus_val32);
262 286
263 channel_pos(channels, pos); 287 channel_pos(channels, pos);
264 288
265 for (c=0;c<3;c++) 289 for (c=0;c<3;c++)
266 for (i=0;i<21;i++) 290 for (i=0;i<21;i++)
267 maskLogE[c][i] = -QCONST16(28.f, DB_SHIFT); 291 maskLogE[c][i] = -QCONST16(28.f, DB_SHIFT);
268 292
269 for (c=0;c<channels;c++) 293 for (c=0;c<channels;c++)
270 { 294 {
295 int frame;
296 int nb_frames = frame_size/freq_size;
297 celt_assert(nb_frames*freq_size == frame_size);
271 OPUS_COPY(in, mem+c*overlap, overlap); 298 OPUS_COPY(in, mem+c*overlap, overlap);
272 (*copy_channel_in)(x, 1, pcm, channels, c, len); 299 (*copy_channel_in)(x, 1, pcm, channels, c, len);
273 celt_preemphasis(x, in+overlap, frame_size, 1, upsample, celt_mode->preemp h, preemph_mem+c, 0); 300 celt_preemphasis(x, in+overlap, frame_size, 1, upsample, celt_mode->preemp h, preemph_mem+c, 0);
274 #ifndef FIXED_POINT 301 #ifndef FIXED_POINT
275 { 302 {
276 opus_val32 sum; 303 opus_val32 sum;
277 sum = celt_inner_prod(in, in, frame_size+overlap, 0); 304 sum = celt_inner_prod(in, in, frame_size+overlap, 0);
278 /* This should filter out both NaNs and ridiculous signals that could 305 /* This should filter out both NaNs and ridiculous signals that could
279 cause NaNs further down. */ 306 cause NaNs further down. */
280 if (!(sum < 1e18f) || celt_isnan(sum)) 307 if (!(sum < 1e18f) || celt_isnan(sum))
281 { 308 {
282 OPUS_CLEAR(in, frame_size+overlap); 309 OPUS_CLEAR(in, frame_size+overlap);
283 preemph_mem[c] = 0; 310 preemph_mem[c] = 0;
284 } 311 }
285 } 312 }
286 #endif 313 #endif
287 clt_mdct_forward(&celt_mode->mdct, in, freq, celt_mode->window, 314 OPUS_CLEAR(bandE, 21);
288 overlap, celt_mode->maxLM-LM, 1, arch); 315 for (frame=0;frame<nb_frames;frame++)
289 if (upsample != 1)
290 { 316 {
291 int bound = len; 317 opus_val32 tmpE[21];
292 for (i=0;i<bound;i++) 318 clt_mdct_forward(&celt_mode->mdct, in+960*frame, freq, celt_mode->windo w,
293 freq[i] *= upsample; 319 overlap, celt_mode->maxLM-LM, 1, arch);
294 for (;i<frame_size;i++) 320 if (upsample != 1)
295 freq[i] = 0; 321 {
322 int bound = freq_size/upsample;
323 for (i=0;i<bound;i++)
324 freq[i] *= upsample;
325 for (;i<freq_size;i++)
326 freq[i] = 0;
327 }
328
329 compute_band_energies(celt_mode, freq, tmpE, 21, 1, LM, arch);
330 /* If we have multiple frames, take the max energy. */
331 for (i=0;i<21;i++)
332 bandE[i] = MAX32(bandE[i], tmpE[i]);
296 } 333 }
297
298 compute_band_energies(celt_mode, freq, bandE, 21, 1, LM);
299 amp2Log2(celt_mode, 21, 21, bandE, bandLogE+21*c, 1); 334 amp2Log2(celt_mode, 21, 21, bandE, bandLogE+21*c, 1);
300 /* Apply spreading function with -6 dB/band going up and -12 dB/band going down. */ 335 /* Apply spreading function with -6 dB/band going up and -12 dB/band going down. */
301 for (i=1;i<21;i++) 336 for (i=1;i<21;i++)
302 bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i-1]-QCONST16( 1.f, DB_SHIFT)); 337 bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i-1]-QCONST16( 1.f, DB_SHIFT));
303 for (i=19;i>=0;i--) 338 for (i=19;i>=0;i--)
304 bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i+1]-QCONST16( 2.f, DB_SHIFT)); 339 bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i+1]-QCONST16( 2.f, DB_SHIFT));
305 if (pos[c]==1) 340 if (pos[c]==1)
306 { 341 {
307 for (i=0;i<21;i++) 342 for (i=0;i<21;i++)
308 maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]); 343 maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]);
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 { 439 {
405 nb_streams=vorbis_mappings[channels-1].nb_streams; 440 nb_streams=vorbis_mappings[channels-1].nb_streams;
406 nb_coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams; 441 nb_coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams;
407 } else if (mapping_family==255) 442 } else if (mapping_family==255)
408 { 443 {
409 nb_streams=channels; 444 nb_streams=channels;
410 nb_coupled_streams=0; 445 nb_coupled_streams=0;
411 #ifdef ENABLE_EXPERIMENTAL_AMBISONICS 446 #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
412 } else if (mapping_family==254) 447 } else if (mapping_family==254)
413 { 448 {
414 nb_streams=channels; 449 if (!validate_ambisonics(channels, &nb_streams, &nb_coupled_streams))
415 nb_coupled_streams=0; 450 return 0;
416 #endif 451 #endif
417 } else 452 } else
418 return 0; 453 return 0;
419 size = opus_multistream_encoder_get_size(nb_streams, nb_coupled_streams); 454 size = opus_multistream_encoder_get_size(nb_streams, nb_coupled_streams);
420 if (channels>2) 455 if (channels>2)
421 { 456 {
422 size += channels*(120*sizeof(opus_val32) + sizeof(opus_val32)); 457 size += channels*(120*sizeof(opus_val32) + sizeof(opus_val32));
423 } 458 }
424 return size; 459 return size;
425 } 460 }
(...skipping 15 matching lines...) Expand all
441 char *ptr; 476 char *ptr;
442 477
443 if ((channels>255) || (channels<1) || (coupled_streams>streams) || 478 if ((channels>255) || (channels<1) || (coupled_streams>streams) ||
444 (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams)) 479 (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams))
445 return OPUS_BAD_ARG; 480 return OPUS_BAD_ARG;
446 481
447 st->arch = opus_select_arch(); 482 st->arch = opus_select_arch();
448 st->layout.nb_channels = channels; 483 st->layout.nb_channels = channels;
449 st->layout.nb_streams = streams; 484 st->layout.nb_streams = streams;
450 st->layout.nb_coupled_streams = coupled_streams; 485 st->layout.nb_coupled_streams = coupled_streams;
451 st->subframe_mem[0]=st->subframe_mem[1]=st->subframe_mem[2]=0;
452 if (mapping_type != MAPPING_TYPE_SURROUND) 486 if (mapping_type != MAPPING_TYPE_SURROUND)
453 st->lfe_stream = -1; 487 st->lfe_stream = -1;
454 st->bitrate_bps = OPUS_AUTO; 488 st->bitrate_bps = OPUS_AUTO;
455 st->application = application; 489 st->application = application;
456 st->variable_duration = OPUS_FRAMESIZE_ARG; 490 st->variable_duration = OPUS_FRAMESIZE_ARG;
457 for (i=0;i<st->layout.nb_channels;i++) 491 for (i=0;i<st->layout.nb_channels;i++)
458 st->layout.mapping[i] = mapping[i]; 492 st->layout.mapping[i] = mapping[i];
459 if (!validate_layout(&st->layout) || !validate_encoder_layout(&st->layout)) 493 if (!validate_layout(&st->layout))
460 return OPUS_BAD_ARG; 494 return OPUS_BAD_ARG;
495 if (mapping_type == MAPPING_TYPE_SURROUND &&
496 !validate_encoder_layout(&st->layout))
497 return OPUS_BAD_ARG;
498 #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
499 if (mapping_type == MAPPING_TYPE_AMBISONICS &&
500 !validate_ambisonics(st->layout.nb_channels, NULL, NULL))
501 return OPUS_BAD_ARG;
502 #endif
461 ptr = (char*)st + align(sizeof(OpusMSEncoder)); 503 ptr = (char*)st + align(sizeof(OpusMSEncoder));
462 coupled_size = opus_encoder_get_size(2); 504 coupled_size = opus_encoder_get_size(2);
463 mono_size = opus_encoder_get_size(1); 505 mono_size = opus_encoder_get_size(1);
464 506
465 for (i=0;i<st->layout.nb_coupled_streams;i++) 507 for (i=0;i<st->layout.nb_coupled_streams;i++)
466 { 508 {
467 ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 2, application); 509 ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 2, application);
468 if(ret!=OPUS_OK)return ret; 510 if(ret!=OPUS_OK)return ret;
469 if (i==st->lfe_stream) 511 if (i==st->lfe_stream)
470 opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1)); 512 opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1));
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 { 588 {
547 int i; 589 int i;
548 *streams=channels; 590 *streams=channels;
549 *coupled_streams=0; 591 *coupled_streams=0;
550 for(i=0;i<channels;i++) 592 for(i=0;i<channels;i++)
551 mapping[i] = i; 593 mapping[i] = i;
552 #ifdef ENABLE_EXPERIMENTAL_AMBISONICS 594 #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
553 } else if (mapping_family==254) 595 } else if (mapping_family==254)
554 { 596 {
555 int i; 597 int i;
556 *streams=channels; 598 if (!validate_ambisonics(channels, streams, coupled_streams))
557 *coupled_streams=0; 599 return OPUS_BAD_ARG;
558 for(i=0;i<channels;i++) 600 for(i = 0; i < (*streams - *coupled_streams); i++)
559 mapping[i] = i; 601 mapping[i] = i + (*coupled_streams * 2);
602 for(i = 0; i < *coupled_streams * 2; i++)
603 mapping[i + (*streams - *coupled_streams)] = i;
560 #endif 604 #endif
561 } else 605 } else
562 return OPUS_UNIMPLEMENTED; 606 return OPUS_UNIMPLEMENTED;
563 607
564 if (channels>2 && mapping_family==1) { 608 if (channels>2 && mapping_family==1) {
565 mapping_type = MAPPING_TYPE_SURROUND; 609 mapping_type = MAPPING_TYPE_SURROUND;
566 #ifdef ENABLE_EXPERIMENTAL_AMBISONICS 610 #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
567 } else if (mapping_family==254) 611 } else if (mapping_family==254)
568 { 612 {
569 mapping_type = MAPPING_TYPE_AMBISONICS; 613 mapping_type = MAPPING_TYPE_AMBISONICS;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
665 int frame_size, 709 int frame_size,
666 opus_int32 Fs 710 opus_int32 Fs
667 ) 711 )
668 { 712 {
669 int i; 713 int i;
670 opus_int32 channel_rate; 714 opus_int32 channel_rate;
671 int stream_offset; 715 int stream_offset;
672 int lfe_offset; 716 int lfe_offset;
673 int coupled_ratio; /* Q8 */ 717 int coupled_ratio; /* Q8 */
674 int lfe_ratio; /* Q8 */ 718 int lfe_ratio; /* Q8 */
719 int nb_lfe;
720 int nb_uncoupled;
721 int nb_coupled;
722 int nb_normal;
723 opus_int32 channel_offset;
724 opus_int32 bitrate;
725 int total;
675 726
676 if (st->bitrate_bps > st->layout.nb_channels*40000) 727 nb_lfe = (st->lfe_stream!=-1);
677 stream_offset = 20000; 728 nb_coupled = st->layout.nb_coupled_streams;
678 else 729 nb_uncoupled = st->layout.nb_streams-nb_coupled-nb_lfe;
679 stream_offset = st->bitrate_bps/st->layout.nb_channels/2; 730 nb_normal = 2*nb_coupled + nb_uncoupled;
680 stream_offset += 60*(Fs/frame_size-50); 731
681 /* We start by giving each stream (coupled or uncoupled) the same bitrate. 732 /* Give each non-LFE channel enough bits per channel for coding band energy. */
733 channel_offset = 40*IMAX(50, Fs/frame_size);
734
735 if (st->bitrate_bps==OPUS_AUTO)
736 {
737 bitrate = nb_normal*(channel_offset + Fs + 10000) + 8000*nb_lfe;
738 } else if (st->bitrate_bps==OPUS_BITRATE_MAX)
739 {
740 bitrate = nb_normal*300000 + nb_lfe*128000;
741 } else {
742 bitrate = st->bitrate_bps;
743 }
744
745 /* Give LFE some basic stream_channel allocation but never exceed 1/20 of the
746 total rate for the non-energy part to avoid problems at really low rate. * /
747 lfe_offset = IMIN(bitrate/20, 3000) + 15*IMAX(50, Fs/frame_size);
748
749 /* We give each stream (coupled or uncoupled) a starting bitrate.
682 This models the main saving of coupled channels over uncoupled. */ 750 This models the main saving of coupled channels over uncoupled. */
683 /* The LFE stream is an exception to the above and gets fewer bits. */ 751 stream_offset = (bitrate - channel_offset*nb_normal - lfe_offset*nb_lfe)/nb_n ormal/2;
684 lfe_offset = 3500 + 60*(Fs/frame_size-50); 752 stream_offset = IMAX(0, IMIN(20000, stream_offset));
685 /* Coupled streams get twice the mono rate after the first 20 kb/s. */ 753
754 /* Coupled streams get twice the mono rate after the offset is allocated. */
686 coupled_ratio = 512; 755 coupled_ratio = 512;
687 /* Should depend on the bitrate, for now we assume LFE gets 1/8 the bits of m ono */ 756 /* Should depend on the bitrate, for now we assume LFE gets 1/8 the bits of m ono */
688 lfe_ratio = 32; 757 lfe_ratio = 32;
689 758
690 /* Compute bitrate allocation between streams */ 759 total = (nb_uncoupled<<8) /* mono */
691 if (st->bitrate_bps==OPUS_AUTO) 760 + coupled_ratio*nb_coupled /* stereo */
692 { 761 + nb_lfe*lfe_ratio;
693 channel_rate = Fs+60*Fs/frame_size; 762 channel_rate = 256*(opus_int64)(bitrate - lfe_offset*nb_lfe - stream_offset*( nb_coupled+nb_uncoupled) - channel_offset*nb_normal)/total;
694 } else if (st->bitrate_bps==OPUS_BITRATE_MAX)
695 {
696 channel_rate = 300000;
697 } else {
698 int nb_lfe;
699 int nb_uncoupled;
700 int nb_coupled;
701 int total;
702 nb_lfe = (st->lfe_stream!=-1);
703 nb_coupled = st->layout.nb_coupled_streams;
704 nb_uncoupled = st->layout.nb_streams-nb_coupled-nb_lfe;
705 total = (nb_uncoupled<<8) /* mono */
706 + coupled_ratio*nb_coupled /* stereo */
707 + nb_lfe*lfe_ratio;
708 channel_rate = 256*(st->bitrate_bps-lfe_offset*nb_lfe-stream_offset*(nb_co upled+nb_uncoupled))/total;
709 }
710 #ifndef FIXED_POINT
711 if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50)
712 {
713 opus_int32 bonus;
714 bonus = 60*(Fs/frame_size-50);
715 channel_rate += bonus;
716 }
717 #endif
718 763
719 for (i=0;i<st->layout.nb_streams;i++) 764 for (i=0;i<st->layout.nb_streams;i++)
720 { 765 {
721 if (i<st->layout.nb_coupled_streams) 766 if (i<st->layout.nb_coupled_streams)
722 rate[i] = stream_offset+(channel_rate*coupled_ratio>>8); 767 rate[i] = 2*channel_offset + IMAX(0, stream_offset+(channel_rate*couple d_ratio>>8));
723 else if (i!=st->lfe_stream) 768 else if (i!=st->lfe_stream)
724 rate[i] = stream_offset+channel_rate; 769 rate[i] = channel_offset + IMAX(0, stream_offset + channel_rate);
725 else 770 else
726 rate[i] = lfe_offset+(channel_rate*lfe_ratio>>8); 771 rate[i] = IMAX(0, lfe_offset+(channel_rate*lfe_ratio>>8));
727 } 772 }
728 } 773 }
729 774
730 #ifdef ENABLE_EXPERIMENTAL_AMBISONICS 775 #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
731 static void ambisonics_rate_allocation( 776 static void ambisonics_rate_allocation(
732 OpusMSEncoder *st, 777 OpusMSEncoder *st,
733 opus_int32 *rate, 778 opus_int32 *rate,
734 int frame_size, 779 int frame_size,
735 opus_int32 Fs 780 opus_int32 Fs
736 ) 781 )
737 { 782 {
738 int i; 783 int i;
739 int non_mono_rate;
740 int total_rate; 784 int total_rate;
785 int directional_rate;
786 int nondirectional_rate;
787 int leftover_bits;
741 788
742 /* The mono channel gets (rate_ratio_num / rate_ratio_den) times as many bits 789 /* Each nondirectional channel gets (rate_ratio_num / rate_ratio_den) times
743 * as all other channels */ 790 * as many bits as all other ambisonics channels.
791 */
744 const int rate_ratio_num = 4; 792 const int rate_ratio_num = 4;
745 const int rate_ratio_den = 3; 793 const int rate_ratio_den = 3;
746 const int num_channels = st->layout.nb_streams; 794 const int nb_channels = st->layout.nb_streams + st->layout.nb_coupled_streams ;
795 const int nb_nondirectional_channels = st->layout.nb_coupled_streams * 2 + 1;
796 const int nb_directional_channels = st->layout.nb_streams - 1;
747 797
748 if (st->bitrate_bps==OPUS_AUTO) 798 if (st->bitrate_bps==OPUS_AUTO)
749 { 799 {
750 total_rate = num_channels * (20000 + st->layout.nb_streams*(Fs+60*Fs/frame _size)); 800 total_rate = (st->layout.nb_coupled_streams + st->layout.nb_streams) *
801 (Fs+60*Fs/frame_size) + st->layout.nb_streams * 15000;
751 } else if (st->bitrate_bps==OPUS_BITRATE_MAX) 802 } else if (st->bitrate_bps==OPUS_BITRATE_MAX)
752 { 803 {
753 total_rate = num_channels * 320000; 804 total_rate = nb_channels * 320000;
754 } else { 805 } else
806 {
755 total_rate = st->bitrate_bps; 807 total_rate = st->bitrate_bps;
756 } 808 }
757 809
758 /* Let y be the non-mono rate and let p, q be integers such that the mono 810 /* Let y be the directional rate, m be the num of nondirectional channels
759 * channel rate is (p/q) * y. 811 * m = (s + 1)
812 * and let p, q be integers such that the nondirectional rate is
813 * m_rate = (p / q) * y
760 * Also let T be the total bitrate to allocate. Then 814 * Also let T be the total bitrate to allocate. Then
761 * (n - 1) y + (p/q) y = T 815 * T = (n - m) * y + m * m_rate
762 * y = (T q) / (qn - q + p) 816 * Solving for y,
817 * y = (q * T) / (m * (p - q) + n * q)
763 */ 818 */
764 non_mono_rate = 819 directional_rate =
765 total_rate * rate_ratio_den 820 total_rate * rate_ratio_den
766 / (rate_ratio_den*num_channels + rate_ratio_num - rate_ratio_den); 821 / (nb_nondirectional_channels * (rate_ratio_num - rate_ratio_den)
822 + nb_channels * rate_ratio_den);
767 823
768 #ifndef FIXED_POINT 824 /* Calculate the nondirectional rate.
769 if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50) 825 * m_rate = y * (p / q)
826 */
827 nondirectional_rate = directional_rate * rate_ratio_num / rate_ratio_den;
828
829 /* Calculate the leftover from truncation error.
830 * leftover = T - y * (n - m) - m_rate * m
831 * Place leftover bits in omnidirectional channel.
832 */
833 leftover_bits = total_rate
834 - directional_rate * nb_directional_channels
835 - nondirectional_rate * nb_nondirectional_channels;
836
837 /* Calculate rates for each channel */
838 for (i = 0; i < st->layout.nb_streams; i++)
770 { 839 {
771 opus_int32 bonus = 60*(Fs/frame_size-50); 840 if (i < st->layout.nb_coupled_streams)
772 non_mono_rate += bonus; 841 {
773 } 842 rate[i] = nondirectional_rate * 2;
774 #endif 843 } else if (i == st->layout.nb_coupled_streams)
775 844 {
776 rate[0] = total_rate - (num_channels - 1) * non_mono_rate; 845 rate[i] = nondirectional_rate + leftover_bits;
777 for (i=1;i<st->layout.nb_streams;i++) 846 } else
778 { 847 {
779 rate[i] = non_mono_rate; 848 rate[i] = directional_rate;
849 }
780 } 850 }
781 } 851 }
782 #endif /* ENABLE_EXPERIMENTAL_AMBISONICS */ 852 #endif /* ENABLE_EXPERIMENTAL_AMBISONICS */
783 853
784 static opus_int32 rate_allocation( 854 static opus_int32 rate_allocation(
785 OpusMSEncoder *st, 855 OpusMSEncoder *st,
786 opus_int32 *rate, 856 opus_int32 *rate,
787 int frame_size 857 int frame_size
788 ) 858 )
789 { 859 {
(...skipping 15 matching lines...) Expand all
805 } 875 }
806 876
807 for (i=0;i<st->layout.nb_streams;i++) 877 for (i=0;i<st->layout.nb_streams;i++)
808 { 878 {
809 rate[i] = IMAX(rate[i], 500); 879 rate[i] = IMAX(rate[i], 500);
810 rate_sum += rate[i]; 880 rate_sum += rate[i];
811 } 881 }
812 return rate_sum; 882 return rate_sum;
813 } 883 }
814 884
815 /* Max size in case the encoder decides to return three frames */ 885 /* Max size in case the encoder decides to return six frames (6 x 20 ms = 120 ms ) */
816 #define MS_FRAME_TMP (3*1275+7) 886 #define MS_FRAME_TMP (6*1275+12)
817 static int opus_multistream_encode_native 887 static int opus_multistream_encode_native
818 ( 888 (
819 OpusMSEncoder *st, 889 OpusMSEncoder *st,
820 opus_copy_channel_in_func copy_channel_in, 890 opus_copy_channel_in_func copy_channel_in,
821 const void *pcm, 891 const void *pcm,
822 int analysis_frame_size, 892 int analysis_frame_size,
823 unsigned char *data, 893 unsigned char *data,
824 opus_int32 max_data_bytes, 894 opus_int32 max_data_bytes,
825 int lsb_depth, 895 int lsb_depth,
826 downmix_func downmix, 896 downmix_func downmix,
(...skipping 25 matching lines...) Expand all
852 { 922 {
853 preemph_mem = ms_get_preemph_mem(st); 923 preemph_mem = ms_get_preemph_mem(st);
854 mem = ms_get_window_mem(st); 924 mem = ms_get_window_mem(st);
855 } 925 }
856 926
857 ptr = (char*)st + align(sizeof(OpusMSEncoder)); 927 ptr = (char*)st + align(sizeof(OpusMSEncoder));
858 opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs)); 928 opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs));
859 opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_VBR(&vbr)); 929 opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_VBR(&vbr));
860 opus_encoder_ctl((OpusEncoder*)ptr, CELT_GET_MODE(&celt_mode)); 930 opus_encoder_ctl((OpusEncoder*)ptr, CELT_GET_MODE(&celt_mode));
861 931
862 { 932 frame_size = frame_size_select(analysis_frame_size, st->variable_duration, Fs );
863 opus_int32 delay_compensation; 933 if (frame_size <= 0)
864 int channels;
865
866 channels = st->layout.nb_streams + st->layout.nb_coupled_streams;
867 opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_LOOKAHEAD(&delay_compensation ));
868 delay_compensation -= Fs/400;
869 frame_size = compute_frame_size(pcm, analysis_frame_size,
870 st->variable_duration, channels, Fs, st->bitrate_bps,
871 delay_compensation, downmix
872 #ifndef DISABLE_FLOAT_API
873 , st->subframe_mem
874 #endif
875 );
876 }
877
878 if (400*frame_size < Fs)
879 { 934 {
880 RESTORE_STACK; 935 RESTORE_STACK;
881 return OPUS_BAD_ARG; 936 return OPUS_BAD_ARG;
882 }
883 /* Validate frame_size before using it to allocate stack space.
884 This mirrors the checks in opus_encode[_float](). */
885 if (400*frame_size != Fs && 200*frame_size != Fs &&
886 100*frame_size != Fs && 50*frame_size != Fs &&
887 25*frame_size != Fs && 50*frame_size != 3*Fs)
888 {
889 RESTORE_STACK;
890 return OPUS_BAD_ARG;
891 } 937 }
892 938
893 /* Smallest packet the encoder can produce. */ 939 /* Smallest packet the encoder can produce. */
894 smallest_packet = st->layout.nb_streams*2-1; 940 smallest_packet = st->layout.nb_streams*2-1;
941 /* 100 ms needs an extra byte per stream for the ToC. */
942 if (Fs/frame_size == 10)
943 smallest_packet += st->layout.nb_streams;
895 if (max_data_bytes < smallest_packet) 944 if (max_data_bytes < smallest_packet)
896 { 945 {
897 RESTORE_STACK; 946 RESTORE_STACK;
898 return OPUS_BUFFER_TOO_SMALL; 947 return OPUS_BUFFER_TOO_SMALL;
899 } 948 }
900 ALLOC(buf, 2*frame_size, opus_val16); 949 ALLOC(buf, 2*frame_size, opus_val16);
901 coupled_size = opus_encoder_get_size(2); 950 coupled_size = opus_encoder_get_size(2);
902 mono_size = opus_encoder_get_size(1); 951 mono_size = opus_encoder_get_size(1);
903 952
904 ALLOC(bandSMR, 21*st->layout.nb_channels, opus_val16); 953 ALLOC(bandSMR, 21*st->layout.nb_channels, opus_val16);
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
1006 } 1055 }
1007 c1 = chan; 1056 c1 = chan;
1008 c2 = -1; 1057 c2 = -1;
1009 } 1058 }
1010 if (st->mapping_type == MAPPING_TYPE_SURROUND) 1059 if (st->mapping_type == MAPPING_TYPE_SURROUND)
1011 opus_encoder_ctl(enc, OPUS_SET_ENERGY_MASK(bandLogE)); 1060 opus_encoder_ctl(enc, OPUS_SET_ENERGY_MASK(bandLogE));
1012 /* number of bytes left (+Toc) */ 1061 /* number of bytes left (+Toc) */
1013 curr_max = max_data_bytes - tot_size; 1062 curr_max = max_data_bytes - tot_size;
1014 /* Reserve one byte for the last stream and two for the others */ 1063 /* Reserve one byte for the last stream and two for the others */
1015 curr_max -= IMAX(0,2*(st->layout.nb_streams-s-1)-1); 1064 curr_max -= IMAX(0,2*(st->layout.nb_streams-s-1)-1);
1065 /* For 100 ms, reserve an extra byte per stream for the ToC */
1066 if (Fs/frame_size == 10)
1067 curr_max -= st->layout.nb_streams-s-1;
1016 curr_max = IMIN(curr_max,MS_FRAME_TMP); 1068 curr_max = IMIN(curr_max,MS_FRAME_TMP);
1017 /* Repacketizer will add one or two bytes for self-delimited frames */ 1069 /* Repacketizer will add one or two bytes for self-delimited frames */
1018 if (s != st->layout.nb_streams-1) curr_max -= curr_max>253 ? 2 : 1; 1070 if (s != st->layout.nb_streams-1) curr_max -= curr_max>253 ? 2 : 1;
1019 if (!vbr && s == st->layout.nb_streams-1) 1071 if (!vbr && s == st->layout.nb_streams-1)
1020 opus_encoder_ctl(enc, OPUS_SET_BITRATE(curr_max*(8*Fs/frame_size))); 1072 opus_encoder_ctl(enc, OPUS_SET_BITRATE(curr_max*(8*Fs/frame_size)));
1021 len = opus_encode_native(enc, buf, frame_size, tmp_data, curr_max, lsb_dep th, 1073 len = opus_encode_native(enc, buf, frame_size, tmp_data, curr_max, lsb_dep th,
1022 pcm, analysis_frame_size, c1, c2, st->layout.nb_channels, downmix, f loat_api); 1074 pcm, analysis_frame_size, c1, c2, st->layout.nb_channels, downmix, f loat_api);
1023 if (len<0) 1075 if (len<0)
1024 { 1076 {
1025 RESTORE_STACK; 1077 RESTORE_STACK;
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
1154 va_start(ap, request); 1206 va_start(ap, request);
1155 1207
1156 coupled_size = opus_encoder_get_size(2); 1208 coupled_size = opus_encoder_get_size(2);
1157 mono_size = opus_encoder_get_size(1); 1209 mono_size = opus_encoder_get_size(1);
1158 ptr = (char*)st + align(sizeof(OpusMSEncoder)); 1210 ptr = (char*)st + align(sizeof(OpusMSEncoder));
1159 switch (request) 1211 switch (request)
1160 { 1212 {
1161 case OPUS_SET_BITRATE_REQUEST: 1213 case OPUS_SET_BITRATE_REQUEST:
1162 { 1214 {
1163 opus_int32 value = va_arg(ap, opus_int32); 1215 opus_int32 value = va_arg(ap, opus_int32);
1164 if (value<0 && value!=OPUS_AUTO && value!=OPUS_BITRATE_MAX) 1216 if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
1165 { 1217 {
1166 goto bad_arg; 1218 if (value <= 0)
1219 goto bad_arg;
1220 value = IMIN(300000*st->layout.nb_channels, IMAX(500*st->layout.nb_chan nels, value));
1167 } 1221 }
1168 st->bitrate_bps = value; 1222 st->bitrate_bps = value;
1169 } 1223 }
1170 break; 1224 break;
1171 case OPUS_GET_BITRATE_REQUEST: 1225 case OPUS_GET_BITRATE_REQUEST:
1172 { 1226 {
1173 int s; 1227 int s;
1174 opus_int32 *value = va_arg(ap, opus_int32*); 1228 opus_int32 *value = va_arg(ap, opus_int32*);
1175 if (!value) 1229 if (!value)
1176 { 1230 {
(...skipping 22 matching lines...) Expand all
1199 case OPUS_GET_PACKET_LOSS_PERC_REQUEST: 1253 case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
1200 case OPUS_GET_DTX_REQUEST: 1254 case OPUS_GET_DTX_REQUEST:
1201 case OPUS_GET_VOICE_RATIO_REQUEST: 1255 case OPUS_GET_VOICE_RATIO_REQUEST:
1202 case OPUS_GET_VBR_CONSTRAINT_REQUEST: 1256 case OPUS_GET_VBR_CONSTRAINT_REQUEST:
1203 case OPUS_GET_SIGNAL_REQUEST: 1257 case OPUS_GET_SIGNAL_REQUEST:
1204 case OPUS_GET_LOOKAHEAD_REQUEST: 1258 case OPUS_GET_LOOKAHEAD_REQUEST:
1205 case OPUS_GET_SAMPLE_RATE_REQUEST: 1259 case OPUS_GET_SAMPLE_RATE_REQUEST:
1206 case OPUS_GET_INBAND_FEC_REQUEST: 1260 case OPUS_GET_INBAND_FEC_REQUEST:
1207 case OPUS_GET_FORCE_CHANNELS_REQUEST: 1261 case OPUS_GET_FORCE_CHANNELS_REQUEST:
1208 case OPUS_GET_PREDICTION_DISABLED_REQUEST: 1262 case OPUS_GET_PREDICTION_DISABLED_REQUEST:
1263 case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
1209 { 1264 {
1210 OpusEncoder *enc; 1265 OpusEncoder *enc;
1211 /* For int32* GET params, just query the first stream */ 1266 /* For int32* GET params, just query the first stream */
1212 opus_int32 *value = va_arg(ap, opus_int32*); 1267 opus_int32 *value = va_arg(ap, opus_int32*);
1213 enc = (OpusEncoder*)ptr; 1268 enc = (OpusEncoder*)ptr;
1214 ret = opus_encoder_ctl(enc, request, value); 1269 ret = opus_encoder_ctl(enc, request, value);
1215 } 1270 }
1216 break; 1271 break;
1217 case OPUS_GET_FINAL_RANGE_REQUEST: 1272 case OPUS_GET_FINAL_RANGE_REQUEST:
1218 { 1273 {
(...skipping 26 matching lines...) Expand all
1245 case OPUS_SET_MAX_BANDWIDTH_REQUEST: 1300 case OPUS_SET_MAX_BANDWIDTH_REQUEST:
1246 case OPUS_SET_BANDWIDTH_REQUEST: 1301 case OPUS_SET_BANDWIDTH_REQUEST:
1247 case OPUS_SET_SIGNAL_REQUEST: 1302 case OPUS_SET_SIGNAL_REQUEST:
1248 case OPUS_SET_APPLICATION_REQUEST: 1303 case OPUS_SET_APPLICATION_REQUEST:
1249 case OPUS_SET_INBAND_FEC_REQUEST: 1304 case OPUS_SET_INBAND_FEC_REQUEST:
1250 case OPUS_SET_PACKET_LOSS_PERC_REQUEST: 1305 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
1251 case OPUS_SET_DTX_REQUEST: 1306 case OPUS_SET_DTX_REQUEST:
1252 case OPUS_SET_FORCE_MODE_REQUEST: 1307 case OPUS_SET_FORCE_MODE_REQUEST:
1253 case OPUS_SET_FORCE_CHANNELS_REQUEST: 1308 case OPUS_SET_FORCE_CHANNELS_REQUEST:
1254 case OPUS_SET_PREDICTION_DISABLED_REQUEST: 1309 case OPUS_SET_PREDICTION_DISABLED_REQUEST:
1310 case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
1255 { 1311 {
1256 int s; 1312 int s;
1257 /* This works for int32 params */ 1313 /* This works for int32 params */
1258 opus_int32 value = va_arg(ap, opus_int32); 1314 opus_int32 value = va_arg(ap, opus_int32);
1259 for (s=0;s<st->layout.nb_streams;s++) 1315 for (s=0;s<st->layout.nb_streams;s++)
1260 { 1316 {
1261 OpusEncoder *enc; 1317 OpusEncoder *enc;
1262 1318
1263 enc = (OpusEncoder*)ptr; 1319 enc = (OpusEncoder*)ptr;
1264 if (s < st->layout.nb_coupled_streams) 1320 if (s < st->layout.nb_coupled_streams)
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1306 if (!value) 1362 if (!value)
1307 { 1363 {
1308 goto bad_arg; 1364 goto bad_arg;
1309 } 1365 }
1310 *value = st->variable_duration; 1366 *value = st->variable_duration;
1311 } 1367 }
1312 break; 1368 break;
1313 case OPUS_RESET_STATE: 1369 case OPUS_RESET_STATE:
1314 { 1370 {
1315 int s; 1371 int s;
1316 st->subframe_mem[0] = st->subframe_mem[1] = st->subframe_mem[2] = 0;
1317 if (st->mapping_type == MAPPING_TYPE_SURROUND) 1372 if (st->mapping_type == MAPPING_TYPE_SURROUND)
1318 { 1373 {
1319 OPUS_CLEAR(ms_get_preemph_mem(st), st->layout.nb_channels); 1374 OPUS_CLEAR(ms_get_preemph_mem(st), st->layout.nb_channels);
1320 OPUS_CLEAR(ms_get_window_mem(st), st->layout.nb_channels*120); 1375 OPUS_CLEAR(ms_get_window_mem(st), st->layout.nb_channels*120);
1321 } 1376 }
1322 for (s=0;s<st->layout.nb_streams;s++) 1377 for (s=0;s<st->layout.nb_streams;s++)
1323 { 1378 {
1324 OpusEncoder *enc; 1379 OpusEncoder *enc;
1325 enc = (OpusEncoder*)ptr; 1380 enc = (OpusEncoder*)ptr;
1326 if (s < st->layout.nb_coupled_streams) 1381 if (s < st->layout.nb_coupled_streams)
(...skipping 15 matching lines...) Expand all
1342 return ret; 1397 return ret;
1343 bad_arg: 1398 bad_arg:
1344 va_end(ap); 1399 va_end(ap);
1345 return OPUS_BAD_ARG; 1400 return OPUS_BAD_ARG;
1346 } 1401 }
1347 1402
1348 void opus_multistream_encoder_destroy(OpusMSEncoder *st) 1403 void opus_multistream_encoder_destroy(OpusMSEncoder *st)
1349 { 1404 {
1350 opus_free(st); 1405 opus_free(st);
1351 } 1406 }
OLDNEW
« no previous file with comments | « third_party/opus/src/src/opus_multistream_decoder.c ('k') | third_party/opus/src/src/opus_private.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698