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

Side by Side Diff: src/opus_encoder.c

Issue 107243004: Updating Opus to release 1.1 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/opus
Patch Set: Created 7 years 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 | Annotate | Revision Log
« no previous file with comments | « src/opus_demo.c ('k') | src/opus_multistream_decoder.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited 1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */ 2 Written by Jean-Marc Valin and Koen Vos */
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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 int first; 97 int first;
98 opus_val16 * energy_masking; 98 opus_val16 * energy_masking;
99 StereoWidthState width_mem; 99 StereoWidthState width_mem;
100 opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2]; 100 opus_val16 delay_buffer[MAX_ENCODER_BUFFER*2];
101 #ifndef DISABLE_FLOAT_API 101 #ifndef DISABLE_FLOAT_API
102 TonalityAnalysisState analysis; 102 TonalityAnalysisState analysis;
103 int detected_bandwidth; 103 int detected_bandwidth;
104 int analysis_offset; 104 int analysis_offset;
105 #endif 105 #endif
106 opus_uint32 rangeFinal; 106 opus_uint32 rangeFinal;
107 int arch; 107 int arch;
108 }; 108 };
109 109
110 /* Transition tables for the voice and music. First column is the 110 /* Transition tables for the voice and music. First column is the
111 middle (memoriless) threshold. The second column is the hysteresis 111 middle (memoriless) threshold. The second column is the hysteresis
112 (difference with the middle) */ 112 (difference with the middle) */
113 static const opus_int32 mono_voice_bandwidth_thresholds[8] = { 113 static const opus_int32 mono_voice_bandwidth_thresholds[8] = {
114 11000, 1000, /* NB<->MB */ 114 11000, 1000, /* NB<->MB */
115 14000, 1000, /* MB<->WB */ 115 14000, 1000, /* MB<->WB */
116 17000, 1000, /* WB<->SWB */ 116 17000, 1000, /* WB<->SWB */
117 20000, 1000, /* SWB<->FB */ 117 21000, 2000, /* SWB<->FB */
118 }; 118 };
119 static const opus_int32 mono_music_bandwidth_thresholds[8] = { 119 static const opus_int32 mono_music_bandwidth_thresholds[8] = {
120 14000, 1000, /* MB not allowed */ 120 12000, 1000, /* NB<->MB */
121 18000, 2000, /* MB<->WB */ 121 15000, 1000, /* MB<->WB */
122 24000, 2000, /* WB<->SWB */ 122 18000, 2000, /* WB<->SWB */
123 33000, 2000, /* SWB<->FB */ 123 22000, 2000, /* SWB<->FB */
124 }; 124 };
125 static const opus_int32 stereo_voice_bandwidth_thresholds[8] = { 125 static const opus_int32 stereo_voice_bandwidth_thresholds[8] = {
126 11000, 1000, /* NB<->MB */ 126 11000, 1000, /* NB<->MB */
127 14000, 1000, /* MB<->WB */ 127 14000, 1000, /* MB<->WB */
128 21000, 2000, /* WB<->SWB */ 128 21000, 2000, /* WB<->SWB */
129 32000, 2000, /* SWB<->FB */ 129 28000, 2000, /* SWB<->FB */
130 }; 130 };
131 static const opus_int32 stereo_music_bandwidth_thresholds[8] = { 131 static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
132 14000, 1000, /* MB not allowed */ 132 12000, 1000, /* NB<->MB */
133 18000, 2000, /* MB<->WB */ 133 18000, 2000, /* MB<->WB */
134 24000, 2000, /* WB<->SWB */ 134 21000, 2000, /* WB<->SWB */
135 48000, 2000, /* SWB<->FB */ 135 30000, 2000, /* SWB<->FB */
136 }; 136 };
137 /* Threshold bit-rates for switching between mono and stereo */ 137 /* Threshold bit-rates for switching between mono and stereo */
138 static const opus_int32 stereo_voice_threshold = 31000; 138 static const opus_int32 stereo_voice_threshold = 30000;
139 static const opus_int32 stereo_music_threshold = 31000; 139 static const opus_int32 stereo_music_threshold = 30000;
140 140
141 /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */ 141 /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
142 static const opus_int32 mode_thresholds[2][2] = { 142 static const opus_int32 mode_thresholds[2][2] = {
143 /* voice */ /* music */ 143 /* voice */ /* music */
144 { 64000, 20000}, /* mono */ 144 { 64000, 16000}, /* mono */
145 { 36000, 20000}, /* stereo */ 145 { 36000, 16000}, /* stereo */
146 }; 146 };
147 147
148 int opus_encoder_get_size(int channels) 148 int opus_encoder_get_size(int channels)
149 { 149 {
150 int silkEncSizeBytes, celtEncSizeBytes; 150 int silkEncSizeBytes, celtEncSizeBytes;
151 int ret; 151 int ret;
152 if (channels<1 || channels > 2) 152 if (channels<1 || channels > 2)
153 return 0; 153 return 0;
154 ret = silk_Get_Encoder_Size( &silkEncSizeBytes ); 154 ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
155 if (ret) 155 if (ret)
(...skipping 25 matching lines...) Expand all
181 st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes; 181 st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes;
182 silk_enc = (char*)st+st->silk_enc_offset; 182 silk_enc = (char*)st+st->silk_enc_offset;
183 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 183 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
184 184
185 st->stream_channels = st->channels = channels; 185 st->stream_channels = st->channels = channels;
186 186
187 st->Fs = Fs; 187 st->Fs = Fs;
188 188
189 st->arch = opus_select_arch(); 189 st->arch = opus_select_arch();
190 190
191 ret = silk_InitEncoder( silk_enc, &st->silk_mode ); 191 ret = silk_InitEncoder( silk_enc, st->arch, &st->silk_mode );
192 if(ret)return OPUS_INTERNAL_ERROR; 192 if(ret)return OPUS_INTERNAL_ERROR;
193 193
194 /* default SILK parameters */ 194 /* default SILK parameters */
195 st->silk_mode.nChannelsAPI = channels; 195 st->silk_mode.nChannelsAPI = channels;
196 st->silk_mode.nChannelsInternal = channels; 196 st->silk_mode.nChannelsInternal = channels;
197 st->silk_mode.API_sampleRate = st->Fs; 197 st->silk_mode.API_sampleRate = st->Fs;
198 st->silk_mode.maxInternalSampleRate = 16000; 198 st->silk_mode.maxInternalSampleRate = 16000;
199 st->silk_mode.minInternalSampleRate = 8000; 199 st->silk_mode.minInternalSampleRate = 8000;
200 st->silk_mode.desiredInternalSampleRate = 16000; 200 st->silk_mode.desiredInternalSampleRate = 16000;
201 st->silk_mode.payloadSize_ms = 20; 201 st->silk_mode.payloadSize_ms = 20;
202 st->silk_mode.bitRate = 25000; 202 st->silk_mode.bitRate = 25000;
203 st->silk_mode.packetLossPercentage = 0; 203 st->silk_mode.packetLossPercentage = 0;
204 st->silk_mode.complexity = 9; 204 st->silk_mode.complexity = 9;
205 st->silk_mode.useInBandFEC = 0; 205 st->silk_mode.useInBandFEC = 0;
206 st->silk_mode.useDTX = 0; 206 st->silk_mode.useDTX = 0;
207 st->silk_mode.useCBR = 0; 207 st->silk_mode.useCBR = 0;
208 st->silk_mode.reducedDependency = 0;
208 209
209 /* Create CELT encoder */ 210 /* Create CELT encoder */
210 /* Initialize CELT encoder */ 211 /* Initialize CELT encoder */
211 err = celt_encoder_init(celt_enc, Fs, channels); 212 err = celt_encoder_init(celt_enc, Fs, channels, st->arch);
212 if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR; 213 if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR;
213 214
214 celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0)); 215 celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
215 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity)); 216 celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity));
216 217
217 st->use_vbr = 1; 218 st->use_vbr = 1;
218 /* Makes constrained VBR the default (safer for real-time use) */ 219 /* Makes constrained VBR the default (safer for real-time use) */
219 st->vbr_constraint = 1; 220 st->vbr_constraint = 1;
220 st->user_bitrate_bps = OPUS_AUTO; 221 st->user_bitrate_bps = OPUS_AUTO;
221 st->bitrate_bps = 3000+Fs*channels; 222 st->bitrate_bps = 3000+Fs*channels;
(...skipping 15 matching lines...) Expand all
237 st->hybrid_stereo_width_Q14 = 1 << 14; 238 st->hybrid_stereo_width_Q14 = 1 << 14;
238 st->prev_HB_gain = Q15ONE; 239 st->prev_HB_gain = Q15ONE;
239 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOF F_HZ ), 8 ); 240 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOF F_HZ ), 8 );
240 st->first = 1; 241 st->first = 1;
241 st->mode = MODE_HYBRID; 242 st->mode = MODE_HYBRID;
242 st->bandwidth = OPUS_BANDWIDTH_FULLBAND; 243 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
243 244
244 return OPUS_OK; 245 return OPUS_OK;
245 } 246 }
246 247
247 static int pad_frame(unsigned char *data, opus_int32 len, opus_int32 new_len)
248 {
249 if (len == new_len)
250 return 0;
251 if (len > new_len)
252 return 1;
253
254 if ((data[0]&0x3)==0)
255 {
256 int i;
257 int padding, nb_255s;
258
259 padding = new_len - len;
260 if (padding >= 2)
261 {
262 nb_255s = (padding-2)/255;
263
264 for (i=len-1;i>=1;i--)
265 data[i+nb_255s+2] = data[i];
266 data[0] |= 0x3;
267 data[1] = 0x41;
268 for (i=0;i<nb_255s;i++)
269 data[i+2] = 255;
270 data[nb_255s+2] = padding-255*nb_255s-2;
271 for (i=len+3+nb_255s;i<new_len;i++)
272 data[i] = 0;
273 } else {
274 for (i=len-1;i>=1;i--)
275 data[i+1] = data[i];
276 data[0] |= 0x3;
277 data[1] = 1;
278 }
279 return 0;
280 } else {
281 return 1;
282 }
283 }
284
285 static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channel s) 248 static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channel s)
286 { 249 {
287 int period; 250 int period;
288 unsigned char toc; 251 unsigned char toc;
289 period = 0; 252 period = 0;
290 while (framerate < 400) 253 while (framerate < 400)
291 { 254 {
292 framerate <<= 1; 255 framerate <<= 1;
293 period++; 256 period++;
294 } 257 }
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
679 for (i=N-1;i>=0;i--) 642 for (i=N-1;i>=0;i--)
680 { 643 {
681 /*printf("%d ", best_state);*/ 644 /*printf("%d ", best_state);*/
682 best_state = states[i][best_state]; 645 best_state = states[i][best_state];
683 } 646 }
684 /*printf("%d\n", best_state);*/ 647 /*printf("%d\n", best_state);*/
685 return best_state; 648 return best_state;
686 } 649 }
687 650
688 int optimize_framesize(const opus_val16 *x, int len, int C, opus_int32 Fs, 651 int optimize_framesize(const opus_val16 *x, int len, int C, opus_int32 Fs,
689 int bitrate, opus_val16 tonality, opus_val32 *mem, int buffering , 652 int bitrate, opus_val16 tonality, float *mem, int buffering,
690 downmix_func downmix) 653 downmix_func downmix)
691 { 654 {
692 int N; 655 int N;
693 int i; 656 int i;
694 float e[MAX_DYNAMIC_FRAMESIZE+4]; 657 float e[MAX_DYNAMIC_FRAMESIZE+4];
695 float e_1[MAX_DYNAMIC_FRAMESIZE+3]; 658 float e_1[MAX_DYNAMIC_FRAMESIZE+3];
696 float memx; 659 opus_val32 memx;
697 int bestLM=0; 660 int bestLM=0;
698 int subframe; 661 int subframe;
699 int pos; 662 int pos;
700 VARDECL(opus_val32, sub); 663 VARDECL(opus_val32, sub);
701 664
702 subframe = Fs/400; 665 subframe = Fs/400;
703 ALLOC(sub, subframe, opus_val32); 666 ALLOC(sub, subframe, opus_val32);
704 e[0]=mem[0]; 667 e[0]=mem[0];
705 e_1[0]=1.f/(EPSILON+mem[0]); 668 e_1[0]=1.f/(EPSILON+mem[0]);
706 if (buffering) 669 if (buffering)
707 { 670 {
708 /* Consider the CELT delay when not in restricted-lowdelay */ 671 /* Consider the CELT delay when not in restricted-lowdelay */
709 /* We assume the buffering is between 2.5 and 5 ms */ 672 /* We assume the buffering is between 2.5 and 5 ms */
710 int offset = 2*subframe - buffering; 673 int offset = 2*subframe - buffering;
711 celt_assert(offset>=0 && offset <= subframe); 674 celt_assert(offset>=0 && offset <= subframe);
712 x += C*offset; 675 x += C*offset;
713 len -= offset; 676 len -= offset;
714 e[1]=mem[1]; 677 e[1]=mem[1];
715 e_1[1]=1.f/(EPSILON+mem[1]); 678 e_1[1]=1.f/(EPSILON+mem[1]);
716 e[2]=mem[2]; 679 e[2]=mem[2];
717 e_1[2]=1.f/(EPSILON+mem[2]); 680 e_1[2]=1.f/(EPSILON+mem[2]);
718 pos = 3; 681 pos = 3;
719 } else { 682 } else {
720 pos=1; 683 pos=1;
721 } 684 }
722 N=IMIN(len/subframe, MAX_DYNAMIC_FRAMESIZE); 685 N=IMIN(len/subframe, MAX_DYNAMIC_FRAMESIZE);
723 memx = x[0]; 686 /* Just silencing a warning, it's really initialized later */
687 memx = 0;
724 for (i=0;i<N;i++) 688 for (i=0;i<N;i++)
725 { 689 {
726 float tmp; 690 float tmp;
727 float tmpx; 691 opus_val32 tmpx;
728 int j; 692 int j;
729 tmp=EPSILON; 693 tmp=EPSILON;
730 694
731 downmix(x, sub, subframe, i*subframe, 0, -2, C); 695 downmix(x, sub, subframe, i*subframe, 0, -2, C);
732 if (i==0) 696 if (i==0)
733 memx = sub[0]; 697 memx = sub[0];
734 for (j=0;j<subframe;j++) 698 for (j=0;j<subframe;j++)
735 { 699 {
736 tmpx = sub[j]; 700 tmpx = sub[j];
737 tmp += (tmpx-memx)*(tmpx-memx); 701 tmp += (tmpx-memx)*(float)(tmpx-memx);
738 memx = tmpx; 702 memx = tmpx;
739 } 703 }
740 e[i+pos] = tmp; 704 e[i+pos] = tmp;
741 e_1[i+pos] = 1.f/tmp; 705 e_1[i+pos] = 1.f/tmp;
742 } 706 }
743 /* Hack to get 20 ms working with APPLICATION_AUDIO 707 /* Hack to get 20 ms working with APPLICATION_AUDIO
744 The real problem is that the corresponding memory needs to use 1.5 ms 708 The real problem is that the corresponding memory needs to use 1.5 ms
745 from this frame and 1 ms from the next frame */ 709 from this frame and 1 ms from the next frame */
746 e[i+pos] = e[i+pos-1]; 710 e[i+pos] = e[i+pos-1];
747 if (buffering) 711 if (buffering)
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 if (new_size>frame_size) 813 if (new_size>frame_size)
850 return -1; 814 return -1;
851 if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs && 815 if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs &&
852 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs) 816 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs)
853 return -1; 817 return -1;
854 return new_size; 818 return new_size;
855 } 819 }
856 820
857 opus_int32 compute_frame_size(const void *analysis_pcm, int frame_size, 821 opus_int32 compute_frame_size(const void *analysis_pcm, int frame_size,
858 int variable_duration, int C, opus_int32 Fs, int bitrate_bps, 822 int variable_duration, int C, opus_int32 Fs, int bitrate_bps,
859 int delay_compensation, downmix_func downmix, opus_val32 *subframe_mem) 823 int delay_compensation, downmix_func downmix
824 #ifndef DISABLE_FLOAT_API
825 , float *subframe_mem
826 #endif
827 )
860 { 828 {
861 #ifndef DISABLE_FLOAT_API 829 #ifndef DISABLE_FLOAT_API
862 if (variable_duration == OPUS_FRAMESIZE_VARIABLE && frame_size >= Fs/200) 830 if (variable_duration == OPUS_FRAMESIZE_VARIABLE && frame_size >= Fs/200)
863 { 831 {
864 int LM = 3; 832 int LM = 3;
865 LM = optimize_framesize(analysis_pcm, frame_size, C, Fs, bitrate_bps, 833 LM = optimize_framesize(analysis_pcm, frame_size, C, Fs, bitrate_bps,
866 0, subframe_mem, delay_compensation, downmix); 834 0, subframe_mem, delay_compensation, downmix);
867 while ((Fs/400<<LM)>frame_size) 835 while ((Fs/400<<LM)>frame_size)
868 LM--; 836 LM--;
869 frame_size = (Fs/400<<LM); 837 frame_size = (Fs/400<<LM);
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
1088 if (!st->use_vbr) 1056 if (!st->use_vbr)
1089 { 1057 {
1090 int cbrBytes; 1058 int cbrBytes;
1091 cbrBytes = IMIN( (st->bitrate_bps + 4*frame_rate)/(8*frame_rate) , max_da ta_bytes); 1059 cbrBytes = IMIN( (st->bitrate_bps + 4*frame_rate)/(8*frame_rate) , max_da ta_bytes);
1092 st->bitrate_bps = cbrBytes * (8*frame_rate); 1060 st->bitrate_bps = cbrBytes * (8*frame_rate);
1093 max_data_bytes = cbrBytes; 1061 max_data_bytes = cbrBytes;
1094 } 1062 }
1095 max_rate = frame_rate*max_data_bytes*8; 1063 max_rate = frame_rate*max_data_bytes*8;
1096 1064
1097 /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */ 1065 /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
1098 equiv_rate = st->bitrate_bps - 60*(st->Fs/frame_size - 50); 1066 equiv_rate = st->bitrate_bps - (40*st->channels+20)*(st->Fs/frame_size - 50) ;
1099 1067
1100 if (st->signal_type == OPUS_SIGNAL_VOICE) 1068 if (st->signal_type == OPUS_SIGNAL_VOICE)
1101 voice_est = 127; 1069 voice_est = 127;
1102 else if (st->signal_type == OPUS_SIGNAL_MUSIC) 1070 else if (st->signal_type == OPUS_SIGNAL_MUSIC)
1103 voice_est = 0; 1071 voice_est = 0;
1104 else if (st->voice_ratio >= 0) 1072 else if (st->voice_ratio >= 0)
1105 { 1073 {
1106 voice_est = st->voice_ratio*327>>8; 1074 voice_est = st->voice_ratio*327>>8;
1107 /* For AUDIO, never be more than 90% confident of having speech */ 1075 /* For AUDIO, never be more than 90% confident of having speech */
1108 if (st->application == OPUS_APPLICATION_AUDIO) 1076 if (st->application == OPUS_APPLICATION_AUDIO)
(...skipping 20 matching lines...) Expand all
1129 if (st->stream_channels == 2) 1097 if (st->stream_channels == 2)
1130 stereo_threshold -= 1000; 1098 stereo_threshold -= 1000;
1131 else 1099 else
1132 stereo_threshold += 1000; 1100 stereo_threshold += 1000;
1133 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1; 1101 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1134 } else { 1102 } else {
1135 st->stream_channels = st->channels; 1103 st->stream_channels = st->channels;
1136 } 1104 }
1137 #endif 1105 #endif
1138 } 1106 }
1107 equiv_rate = st->bitrate_bps - (40*st->stream_channels+20)*(st->Fs/frame_siz e - 50);
1139 1108
1140 /* Mode selection depending on application and signal type */ 1109 /* Mode selection depending on application and signal type */
1141 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1110 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1142 { 1111 {
1143 st->mode = MODE_CELT_ONLY; 1112 st->mode = MODE_CELT_ONLY;
1144 } else if (st->user_forced_mode == OPUS_AUTO) 1113 } else if (st->user_forced_mode == OPUS_AUTO)
1145 { 1114 {
1146 #ifdef FUZZING 1115 #ifdef FUZZING
1147 /* Random mode switching */ 1116 /* Random mode switching */
1148 if ((rand()&0xF)==0) 1117 if ((rand()&0xF)==0)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1188 if (st->silk_mode.useDTX && voice_est > 100) 1157 if (st->silk_mode.useDTX && voice_est > 100)
1189 st->mode = MODE_SILK_ONLY; 1158 st->mode = MODE_SILK_ONLY;
1190 #endif 1159 #endif
1191 } else { 1160 } else {
1192 st->mode = st->user_forced_mode; 1161 st->mode = st->user_forced_mode;
1193 } 1162 }
1194 1163
1195 /* Override the chosen mode to make sure we meet the requested frame size */ 1164 /* Override the chosen mode to make sure we meet the requested frame size */
1196 if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100) 1165 if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
1197 st->mode = MODE_CELT_ONLY; 1166 st->mode = MODE_CELT_ONLY;
1167 if (st->lfe)
1168 st->mode = MODE_CELT_ONLY;
1169 /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode * /
1170 if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8))
1171 st->mode = MODE_CELT_ONLY;
1198 1172
1199 if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMon o==0 1173 if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMon o==0
1200 && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY) 1174 && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1201 { 1175 {
1202 /* Delay stereo->mono transition by two frames so that SILK can do a smoo th downmix */ 1176 /* Delay stereo->mono transition by two frames so that SILK can do a smoo th downmix */
1203 st->silk_mode.toMono = 1; 1177 st->silk_mode.toMono = 1;
1204 st->stream_channels = 2; 1178 st->stream_channels = 2;
1205 } else { 1179 } else {
1206 st->silk_mode.toMono = 0; 1180 st->silk_mode.toMono = 0;
1207 } 1181 }
(...skipping 30 matching lines...) Expand all
1238 /* Fair share of the max size allowed */ 1212 /* Fair share of the max size allowed */
1239 redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(fra me_size+st->Fs/200)); 1213 redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(fra me_size+st->Fs/200));
1240 /* For VBR, target the actual bitrate (subject to the limit above) */ 1214 /* For VBR, target the actual bitrate (subject to the limit above) */
1241 if (st->use_vbr) 1215 if (st->use_vbr)
1242 redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600); 1216 redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600);
1243 } 1217 }
1244 1218
1245 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) 1219 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
1246 { 1220 {
1247 silk_EncControlStruct dummy; 1221 silk_EncControlStruct dummy;
1248 silk_InitEncoder( silk_enc, &dummy); 1222 silk_InitEncoder( silk_enc, st->arch, &dummy);
1249 prefill=1; 1223 prefill=1;
1250 } 1224 }
1251 1225
1252 /* Automatic (rate-dependent) bandwidth selection */ 1226 /* Automatic (rate-dependent) bandwidth selection */
1253 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthS witch) 1227 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthS witch)
1254 { 1228 {
1255 const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_threshold s; 1229 const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_threshold s;
1256 opus_int32 bandwidth_thresholds[8]; 1230 opus_int32 bandwidth_thresholds[8];
1257 int bandwidth = OPUS_BANDWIDTH_FULLBAND; 1231 int bandwidth = OPUS_BANDWIDTH_FULLBAND;
1258 opus_int32 equiv_rate2; 1232 opus_int32 equiv_rate2;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1316 /* Prevents Opus from wasting bits on frequencies that are above 1290 /* Prevents Opus from wasting bits on frequencies that are above
1317 the Nyquist rate of the input signal */ 1291 the Nyquist rate of the input signal */
1318 if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND) 1292 if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
1319 st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; 1293 st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1320 if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND) 1294 if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1321 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1295 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1322 if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND) 1296 if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
1323 st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1297 st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1324 if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND) 1298 if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
1325 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1299 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1326 #ifndef FIXED_POINT 1300 #ifndef DISABLE_FLOAT_API
1327 /* Use detected bandwidth to reduce the encoded bandwidth. */ 1301 /* Use detected bandwidth to reduce the encoded bandwidth. */
1328 if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO) 1302 if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
1329 { 1303 {
1330 int min_detected_bandwidth; 1304 int min_detected_bandwidth;
1331 /* Makes bandwidth detection more conservative just in case the detector 1305 /* Makes bandwidth detection more conservative just in case the detector
1332 gets it wrong when we could have coded a high bandwidth transparently. 1306 gets it wrong when we could have coded a high bandwidth transparently.
1333 When operating in SILK/hybrid mode, we don't go below wideband to avoi d 1307 When operating in SILK/hybrid mode, we don't go below wideband to avoi d
1334 more complicated switches that require redundancy. */ 1308 more complicated switches that require redundancy. */
1335 if (st->bitrate_bps <= 18000*st->stream_channels && st->mode == MODE_CELT _ONLY) 1309 if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY )
1336 min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1310 min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1337 else if (st->bitrate_bps <= 24000*st->stream_channels && st->mode == MODE _CELT_ONLY) 1311 else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT _ONLY)
1338 min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; 1312 min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1339 else if (st->bitrate_bps <= 30000*st->stream_channels) 1313 else if (equiv_rate <= 30000*st->stream_channels)
1340 min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1314 min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1341 else if (st->bitrate_bps <= 44000*st->stream_channels) 1315 else if (equiv_rate <= 44000*st->stream_channels)
1342 min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; 1316 min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1343 else 1317 else
1344 min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND; 1318 min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1345 1319
1346 st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwi dth); 1320 st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwi dth);
1347 st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth); 1321 st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1348 } 1322 }
1349 #endif 1323 #endif
1350 celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth)); 1324 celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
1351 1325
1352 /* If max_data_bytes represents less than 8 kb/s, switch to CELT-only mode * /
1353 if (max_data_bytes < (frame_rate > 50 ? 12000 : 8000)*frame_size / (st->Fs * 8))
1354 st->mode = MODE_CELT_ONLY;
1355
1356 /* CELT mode doesn't support mediumband, use wideband instead */ 1326 /* CELT mode doesn't support mediumband, use wideband instead */
1357 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND ) 1327 if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND )
1358 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND; 1328 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1359 if (st->lfe) 1329 if (st->lfe)
1360 {
1361 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND; 1330 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1362 st->mode = MODE_CELT_ONLY;
1363 }
1364 1331
1365 /* Can't support higher than wideband for >20 ms frames */ 1332 /* Can't support higher than wideband for >20 ms frames */
1366 if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)) 1333 if (frame_size > st->Fs/50 && (st->mode == MODE_CELT_ONLY || st->bandwidth > OPUS_BANDWIDTH_WIDEBAND))
1367 { 1334 {
1368 VARDECL(unsigned char, tmp_data); 1335 VARDECL(unsigned char, tmp_data);
1369 int nb_frames; 1336 int nb_frames;
1370 int bak_mode, bak_bandwidth, bak_channels, bak_to_mono; 1337 int bak_mode, bak_bandwidth, bak_channels, bak_to_mono;
1371 VARDECL(OpusRepacketizer, rp); 1338 VARDECL(OpusRepacketizer, rp);
1372 opus_int32 bytes_per_frame; 1339 opus_int32 bytes_per_frame;
1340 opus_int32 repacketize_len;
1373 1341
1342 #ifndef DISABLE_FLOAT_API
1374 if (analysis_read_pos_bak!= -1) 1343 if (analysis_read_pos_bak!= -1)
1375 { 1344 {
1376 st->analysis.read_pos = analysis_read_pos_bak; 1345 st->analysis.read_pos = analysis_read_pos_bak;
1377 st->analysis.read_subframe = analysis_read_subframe_bak; 1346 st->analysis.read_subframe = analysis_read_subframe_bak;
1378 } 1347 }
1348 #endif
1379 1349
1380 nb_frames = frame_size > st->Fs/25 ? 3 : 2; 1350 nb_frames = frame_size > st->Fs/25 ? 3 : 2;
1381 bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames); 1351 bytes_per_frame = IMIN(1276,(out_data_bytes-3)/nb_frames);
1382 1352
1383 ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char); 1353 ALLOC(tmp_data, nb_frames*bytes_per_frame, unsigned char);
1384 1354
1385 ALLOC(rp, 1, OpusRepacketizer); 1355 ALLOC(rp, 1, OpusRepacketizer);
1386 opus_repacketizer_init(rp); 1356 opus_repacketizer_init(rp);
1387 1357
1388 bak_mode = st->user_forced_mode; 1358 bak_mode = st->user_forced_mode;
(...skipping 24 matching lines...) Expand all
1413 RESTORE_STACK; 1383 RESTORE_STACK;
1414 return OPUS_INTERNAL_ERROR; 1384 return OPUS_INTERNAL_ERROR;
1415 } 1385 }
1416 ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len); 1386 ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len);
1417 if (ret<0) 1387 if (ret<0)
1418 { 1388 {
1419 RESTORE_STACK; 1389 RESTORE_STACK;
1420 return OPUS_INTERNAL_ERROR; 1390 return OPUS_INTERNAL_ERROR;
1421 } 1391 }
1422 } 1392 }
1423 ret = opus_repacketizer_out(rp, data, out_data_bytes); 1393 if (st->use_vbr)
1394 repacketize_len = out_data_bytes;
1395 else
1396 repacketize_len = IMIN(3*st->bitrate_bps/(3*8*50/nb_frames), out_data_ bytes);
1397 ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketiz e_len, 0, !st->use_vbr);
1424 if (ret<0) 1398 if (ret<0)
1425 { 1399 {
1426 RESTORE_STACK; 1400 RESTORE_STACK;
1427 return OPUS_INTERNAL_ERROR; 1401 return OPUS_INTERNAL_ERROR;
1428 } 1402 }
1429 st->user_forced_mode = bak_mode; 1403 st->user_forced_mode = bak_mode;
1430 st->user_bandwidth = bak_bandwidth; 1404 st->user_bandwidth = bak_bandwidth;
1431 st->force_channels = bak_channels; 1405 st->force_channels = bak_channels;
1432 st->silk_mode.toMono = bak_to_mono; 1406 st->silk_mode.toMono = bak_to_mono;
1433 RESTORE_STACK; 1407 RESTORE_STACK;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 /* SILK gets 2/3 of the remaining bits */ 1469 /* SILK gets 2/3 of the remaining bits */
1496 st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 2 / 3; 1470 st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 2 / 3;
1497 } else { /* FULLBAND */ 1471 } else { /* FULLBAND */
1498 /* SILK gets 3/5 of the remaining bits */ 1472 /* SILK gets 3/5 of the remaining bits */
1499 st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 3 / 5; 1473 st->silk_mode.bitRate += ( total_bitRate - st->silk_mode.bitRate ) * 3 / 5;
1500 } 1474 }
1501 /* Don't let SILK use more than 80% */ 1475 /* Don't let SILK use more than 80% */
1502 if( st->silk_mode.bitRate > total_bitRate * 4/5 ) { 1476 if( st->silk_mode.bitRate > total_bitRate * 4/5 ) {
1503 st->silk_mode.bitRate = total_bitRate * 4/5; 1477 st->silk_mode.bitRate = total_bitRate * 4/5;
1504 } 1478 }
1505 /* Increasingly attenuate high band when it gets allocated fewer bit s */ 1479 if (!st->energy_masking)
1506 celt_rate = total_bitRate - st->silk_mode.bitRate; 1480 {
1507 HB_gain_ref = (curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND) ? 300 0 : 3600; 1481 /* Increasingly attenuate high band when it gets allocated fewer bits */
1508 HB_gain = SHL32((opus_val32)celt_rate, 9) / SHR32((opus_val32)celt_r ate + st->stream_channels * HB_gain_ref, 6); 1482 celt_rate = total_bitRate - st->silk_mode.bitRate;
1509 HB_gain = HB_gain < Q15ONE*6/7 ? HB_gain + Q15ONE/7 : Q15ONE; 1483 HB_gain_ref = (curr_bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND) ? 3000 : 3600;
1484 HB_gain = SHL32((opus_val32)celt_rate, 9) / SHR32((opus_val32)cel t_rate + st->stream_channels * HB_gain_ref, 6);
1485 HB_gain = HB_gain < Q15ONE*6/7 ? HB_gain + Q15ONE/7 : Q15ONE;
1486 }
1510 } else { 1487 } else {
1511 /* SILK gets all bits */ 1488 /* SILK gets all bits */
1512 st->silk_mode.bitRate = total_bitRate; 1489 st->silk_mode.bitRate = total_bitRate;
1513 } 1490 }
1514 1491
1515 /* Surround masking for SILK */ 1492 /* Surround masking for SILK */
1516 if (st->energy_masking && st->use_vbr && !st->lfe) 1493 if (st->energy_masking && st->use_vbr && !st->lfe)
1517 { 1494 {
1518 opus_val32 mask_sum=0; 1495 opus_val32 mask_sum=0;
1519 opus_val16 masking_depth; 1496 opus_val16 masking_depth;
1520 opus_int32 rate_offset; 1497 opus_int32 rate_offset;
1521 int c; 1498 int c;
1522 int end = 17; 1499 int end = 17;
1523 opus_int16 srate = 16000; 1500 opus_int16 srate = 16000;
1524 if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND) 1501 if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
1525 { 1502 {
1526 end = 13; 1503 end = 13;
1527 srate = 8000; 1504 srate = 8000;
1528 } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) 1505 } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1529 { 1506 {
1530 end = 15; 1507 end = 15;
1531 srate = 12000; 1508 srate = 12000;
1532 } 1509 }
1533 for (c=0;c<st->channels;c++) 1510 for (c=0;c<st->channels;c++)
1534 { 1511 {
1535 for(i=0;i<end;i++) 1512 for(i=0;i<end;i++)
1536 { 1513 {
1537 opus_val16 mask; 1514 opus_val16 mask;
1538 mask = MAX16(MIN16(st->energy_masking[21*c+i], 1515 mask = MAX16(MIN16(st->energy_masking[21*c+i],
1539 QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT)); 1516 QCONST16(.5f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT));
1540 if (mask > 0) 1517 if (mask > 0)
1541 mask = HALF16(mask); 1518 mask = HALF16(mask);
1542 mask_sum += mask; 1519 mask_sum += mask;
1543 } 1520 }
1544 } 1521 }
1545 /* Conservative rate reduction, we cut the masking in half */ 1522 /* Conservative rate reduction, we cut the masking in half */
1546 masking_depth = HALF16(mask_sum / end*st->channels); 1523 masking_depth = mask_sum / end*st->channels;
1547 rate_offset = PSHR32(MULT16_16(srate, masking_depth), DB_SHIFT); 1524 masking_depth += QCONST16(.2f, DB_SHIFT);
1525 rate_offset = (opus_int32)PSHR32(MULT16_16(srate, masking_depth), DB_ SHIFT);
1548 rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3); 1526 rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
1549 rate_offset += QCONST16(.4f, DB_SHIFT); 1527 /* Split the rate change between the SILK and CELT part for hybrid. * /
1550 st->silk_mode.bitRate += rate_offset; 1528 if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPU S_BANDWIDTH_FULLBAND)
1529 st->silk_mode.bitRate += 3*rate_offset/5;
1530 else
1531 st->silk_mode.bitRate += rate_offset;
1551 bytes_target += rate_offset * frame_size / (8 * st->Fs); 1532 bytes_target += rate_offset * frame_size / (8 * st->Fs);
1552 } 1533 }
1553 1534
1554 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs; 1535 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
1555 st->silk_mode.nChannelsAPI = st->channels; 1536 st->silk_mode.nChannelsAPI = st->channels;
1556 st->silk_mode.nChannelsInternal = st->stream_channels; 1537 st->silk_mode.nChannelsInternal = st->stream_channels;
1557 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 1538 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
1558 st->silk_mode.desiredInternalSampleRate = 8000; 1539 st->silk_mode.desiredInternalSampleRate = 8000;
1559 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 1540 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
1560 st->silk_mode.desiredInternalSampleRate = 12000; 1541 st->silk_mode.desiredInternalSampleRate = 12000;
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1691 case OPUS_BANDWIDTH_FULLBAND: 1672 case OPUS_BANDWIDTH_FULLBAND:
1692 endband = 21; 1673 endband = 21;
1693 break; 1674 break;
1694 } 1675 }
1695 celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband)); 1676 celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
1696 celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels)); 1677 celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
1697 } 1678 }
1698 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); 1679 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
1699 if (st->mode != MODE_SILK_ONLY) 1680 if (st->mode != MODE_SILK_ONLY)
1700 { 1681 {
1682 opus_val32 celt_pred=2;
1701 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0)); 1683 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
1702 /* Allow prediction unless we decide to disable it later */ 1684 /* We may still decide to disable prediction later */
1703 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(2)); 1685 if (st->silk_mode.reducedDependency)
1686 celt_pred = 0;
1687 celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred));
1704 1688
1705 if (st->mode == MODE_HYBRID) 1689 if (st->mode == MODE_HYBRID)
1706 { 1690 {
1707 int len; 1691 int len;
1708 1692
1709 len = (ec_tell(&enc)+7)>>3; 1693 len = (ec_tell(&enc)+7)>>3;
1710 if (redundancy) 1694 if (redundancy)
1711 len += st->mode == MODE_HYBRID ? 3 : 1; 1695 len += st->mode == MODE_HYBRID ? 3 : 1;
1712 if( st->use_vbr ) { 1696 if( st->use_vbr ) {
1713 nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * f rame_size) / (8 * st->Fs); 1697 nb_compr_bytes = len + bytes_target - (st->silk_mode.bitRate * f rame_size) / (8 * st->Fs);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1753 st->delay_buffer[i] = pcm_buf[(frame_size+total_buffer-st->encoder_buffe r)*st->channels+i]; 1737 st->delay_buffer[i] = pcm_buf[(frame_size+total_buffer-st->encoder_buffe r)*st->channels+i];
1754 1738
1755 /* gain_fade() and stereo_fade() need to be after the buffer copying 1739 /* gain_fade() and stereo_fade() need to be after the buffer copying
1756 because we don't want any of this to affect the SILK part */ 1740 because we don't want any of this to affect the SILK part */
1757 if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) { 1741 if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
1758 gain_fade(pcm_buf, pcm_buf, 1742 gain_fade(pcm_buf, pcm_buf,
1759 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->chan nels, celt_mode->window, st->Fs); 1743 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->chan nels, celt_mode->window, st->Fs);
1760 } 1744 }
1761 st->prev_HB_gain = HB_gain; 1745 st->prev_HB_gain = HB_gain;
1762 if (st->mode != MODE_HYBRID || st->stream_channels==1) 1746 if (st->mode != MODE_HYBRID || st->stream_channels==1)
1763 st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),IMAX(0,st->bitrate_bps-32000 )); 1747 st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),2*IMAX(0,equiv_rate-30000));
1764 if( !st->energy_masking && st->channels == 2 ) { 1748 if( !st->energy_masking && st->channels == 2 ) {
1765 /* Apply stereo width reduction (at low bitrates) */ 1749 /* Apply stereo width reduction (at low bitrates) */
1766 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth _Q14 < (1 << 14) ) { 1750 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth _Q14 < (1 << 14) ) {
1767 opus_val16 g1, g2; 1751 opus_val16 g1, g2;
1768 g1 = st->hybrid_stereo_width_Q14; 1752 g1 = st->hybrid_stereo_width_Q14;
1769 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14); 1753 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
1770 #ifdef FIXED_POINT 1754 #ifdef FIXED_POINT
1771 g1 = g1==16384 ? Q15ONE : SHL16(g1,1); 1755 g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
1772 g2 = g2==16384 ? Q15ONE : SHL16(g2,1); 1756 g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
1773 #else 1757 #else
1774 g1 *= (1.f/16384); 1758 g1 *= (1.f/16384);
1775 g2 *= (1.f/16384); 1759 g2 *= (1.f/16384);
1776 #endif 1760 #endif
1777 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap, 1761 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
1778 frame_size, st->channels, celt_mode->window, st->Fs); 1762 frame_size, st->channels, celt_mode->window, st->Fs);
1779 st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14; 1763 st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
1780 } 1764 }
1781 } 1765 }
1782 1766
1783 if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYB RID) <= 8*(max_data_bytes-1)) 1767 if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYB RID) <= 8*(max_data_bytes-1))
1784 { 1768 {
1785 /* For SILK mode, the redundancy is inferred from the length */ 1769 /* For SILK mode, the redundancy is inferred from the length */
1786 if (st->mode == MODE_HYBRID && (redundancy || ec_tell(&enc)+37 <= 8*nb_c ompr_bytes)) 1770 if (st->mode == MODE_HYBRID && (redundancy || ec_tell(&enc)+37 <= 8*nb_c ompr_bytes))
1787 ec_enc_bit_logp(&enc, redundancy, 12); 1771 ec_enc_bit_logp(&enc, redundancy, 12);
1788 if (redundancy) 1772 if (redundancy)
1789 { 1773 {
1790 int max_redundancy; 1774 int max_redundancy;
1791 ec_enc_bit_logp(&enc, celt_to_silk, 1); 1775 ec_enc_bit_logp(&enc, celt_to_silk, 1);
1792 if (st->mode == MODE_HYBRID) 1776 if (st->mode == MODE_HYBRID)
1793 max_redundancy = (max_data_bytes-1)-nb_compr_bytes-1; 1777 max_redundancy = (max_data_bytes-1)-nb_compr_bytes;
1794 else 1778 else
1795 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3); 1779 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
1796 /* Target the same bit-rate for redundancy as for the rest, 1780 /* Target the same bit-rate for redundancy as for the rest,
1797 up to a max of 257 bytes */ 1781 up to a max of 257 bytes */
1798 redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600); 1782 redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600);
1799 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes)); 1783 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
1800 if (st->mode == MODE_HYBRID) 1784 if (st->mode == MODE_HYBRID)
1801 ec_enc_uint(&enc, redundancy_bytes-2, 256); 1785 ec_enc_uint(&enc, redundancy_bytes-2, 256);
1802 } 1786 }
1803 } else { 1787 } else {
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
1926 /*When in LPC only mode it's perfectly 1910 /*When in LPC only mode it's perfectly
1927 reasonable to strip off trailing zero bytes as 1911 reasonable to strip off trailing zero bytes as
1928 the required range decoder behavior is to 1912 the required range decoder behavior is to
1929 fill these in. This can't be done when the MDCT 1913 fill these in. This can't be done when the MDCT
1930 modes are used because the decoder needs to know 1914 modes are used because the decoder needs to know
1931 the actual length for allocation purposes.*/ 1915 the actual length for allocation purposes.*/
1932 while(ret>2&&data[ret]==0)ret--; 1916 while(ret>2&&data[ret]==0)ret--;
1933 } 1917 }
1934 /* Count ToC and redundancy */ 1918 /* Count ToC and redundancy */
1935 ret += 1+redundancy_bytes; 1919 ret += 1+redundancy_bytes;
1936 if (!st->use_vbr && ret >= 3) 1920 if (!st->use_vbr)
1937 { 1921 {
1938 if (pad_frame(data, ret, max_data_bytes)) 1922 if (opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK)
1923
1939 { 1924 {
1940 RESTORE_STACK; 1925 RESTORE_STACK;
1941 return OPUS_INTERNAL_ERROR; 1926 return OPUS_INTERNAL_ERROR;
1942 } 1927 }
1943 ret = max_data_bytes; 1928 ret = max_data_bytes;
1944 } 1929 }
1945 RESTORE_STACK; 1930 RESTORE_STACK;
1946 return ret; 1931 return ret;
1947 } 1932 }
1948 1933
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1980 unsigned char *data, opus_int32 out_data_bytes) 1965 unsigned char *data, opus_int32 out_data_bytes)
1981 { 1966 {
1982 int frame_size; 1967 int frame_size;
1983 int delay_compensation; 1968 int delay_compensation;
1984 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1969 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1985 delay_compensation = 0; 1970 delay_compensation = 0;
1986 else 1971 else
1987 delay_compensation = st->delay_compensation; 1972 delay_compensation = st->delay_compensation;
1988 frame_size = compute_frame_size(pcm, analysis_frame_size, 1973 frame_size = compute_frame_size(pcm, analysis_frame_size,
1989 st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 1974 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
1990 delay_compensation, downmix_float, st->analysis.subframe_mem); 1975 delay_compensation, downmix_int
1976 #ifndef DISABLE_FLOAT_API
1977 , st->analysis.subframe_mem
1978 #endif
1979 );
1991 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int); 1980 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int);
1992 } 1981 }
1993 1982
1994 #else 1983 #else
1995 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size, 1984 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size,
1996 unsigned char *data, opus_int32 max_data_bytes) 1985 unsigned char *data, opus_int32 max_data_bytes)
1997 { 1986 {
1998 int i, ret; 1987 int i, ret;
1999 int frame_size; 1988 int frame_size;
2000 int delay_compensation; 1989 int delay_compensation;
2001 VARDECL(float, in); 1990 VARDECL(float, in);
2002 ALLOC_STACK; 1991 ALLOC_STACK;
2003 1992
2004 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1993 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2005 delay_compensation = 0; 1994 delay_compensation = 0;
2006 else 1995 else
2007 delay_compensation = st->delay_compensation; 1996 delay_compensation = st->delay_compensation;
2008 frame_size = compute_frame_size(pcm, analysis_frame_size, 1997 frame_size = compute_frame_size(pcm, analysis_frame_size,
2009 st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 1998 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
2010 delay_compensation, downmix_float, st->analysis.subframe_mem); 1999 delay_compensation, downmix_int, st->analysis.subframe_mem);
2011 2000
2012 ALLOC(in, frame_size*st->channels, float); 2001 ALLOC(in, frame_size*st->channels, float);
2013 2002
2014 for (i=0;i<frame_size*st->channels;i++) 2003 for (i=0;i<frame_size*st->channels;i++)
2015 in[i] = (1.0f/32768)*pcm[i]; 2004 in[i] = (1.0f/32768)*pcm[i];
2016 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, a nalysis_frame_size, 0, -2, st->channels, downmix_int); 2005 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, a nalysis_frame_size, 0, -2, st->channels, downmix_int);
2017 RESTORE_STACK; 2006 RESTORE_STACK;
2018 return ret; 2007 return ret;
2019 } 2008 }
2020 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_fra me_size, 2009 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_fra me_size,
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
2402 case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST: 2391 case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
2403 { 2392 {
2404 opus_int32 *value = va_arg(ap, opus_int32*); 2393 opus_int32 *value = va_arg(ap, opus_int32*);
2405 if (!value) 2394 if (!value)
2406 { 2395 {
2407 goto bad_arg; 2396 goto bad_arg;
2408 } 2397 }
2409 *value = st->variable_duration; 2398 *value = st->variable_duration;
2410 } 2399 }
2411 break; 2400 break;
2401 case OPUS_SET_PREDICTION_DISABLED_REQUEST:
2402 {
2403 opus_int32 value = va_arg(ap, opus_int32);
2404 if (value > 1 || value < 0)
2405 goto bad_arg;
2406 st->silk_mode.reducedDependency = value;
2407 }
2408 break;
2409 case OPUS_GET_PREDICTION_DISABLED_REQUEST:
2410 {
2411 opus_int32 *value = va_arg(ap, opus_int32*);
2412 if (!value)
2413 goto bad_arg;
2414 *value = st->silk_mode.reducedDependency;
2415 }
2416 break;
2412 case OPUS_RESET_STATE: 2417 case OPUS_RESET_STATE:
2413 { 2418 {
2414 void *silk_enc; 2419 void *silk_enc;
2415 silk_EncControlStruct dummy; 2420 silk_EncControlStruct dummy;
2416 silk_enc = (char*)st+st->silk_enc_offset; 2421 silk_enc = (char*)st+st->silk_enc_offset;
2417 2422
2418 OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START, 2423 OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START,
2419 sizeof(OpusEncoder)- 2424 sizeof(OpusEncoder)-
2420 ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st)); 2425 ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st));
2421 2426
2422 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE); 2427 celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2423 silk_InitEncoder( silk_enc, &dummy ); 2428 silk_InitEncoder( silk_enc, st->arch, &dummy );
2424 st->stream_channels = st->channels; 2429 st->stream_channels = st->channels;
2425 st->hybrid_stereo_width_Q14 = 1 << 14; 2430 st->hybrid_stereo_width_Q14 = 1 << 14;
2426 st->prev_HB_gain = Q15ONE; 2431 st->prev_HB_gain = Q15ONE;
2427 st->first = 1; 2432 st->first = 1;
2428 st->mode = MODE_HYBRID; 2433 st->mode = MODE_HYBRID;
2429 st->bandwidth = OPUS_BANDWIDTH_FULLBAND; 2434 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
2430 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MI N_CUTOFF_HZ ), 8 ); 2435 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MI N_CUTOFF_HZ ), 8 );
2431 } 2436 }
2432 break; 2437 break;
2433 case OPUS_SET_FORCE_MODE_REQUEST: 2438 case OPUS_SET_FORCE_MODE_REQUEST:
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
2474 return ret; 2479 return ret;
2475 bad_arg: 2480 bad_arg:
2476 va_end(ap); 2481 va_end(ap);
2477 return OPUS_BAD_ARG; 2482 return OPUS_BAD_ARG;
2478 } 2483 }
2479 2484
2480 void opus_encoder_destroy(OpusEncoder *st) 2485 void opus_encoder_destroy(OpusEncoder *st)
2481 { 2486 {
2482 opus_free(st); 2487 opus_free(st);
2483 } 2488 }
OLDNEW
« no previous file with comments | « src/opus_demo.c ('k') | src/opus_multistream_decoder.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698