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

Side by Side Diff: celt/celt_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 | « celt/celt_decoder.c ('k') | celt/celt_lpc.h » ('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) 2007-2008 CSIRO 1 /* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2010 Xiph.Org Foundation 2 Copyright (c) 2007-2010 Xiph.Org Foundation
3 Copyright (c) 2008 Gregory Maxwell 3 Copyright (c) 2008 Gregory Maxwell
4 Written by Jean-Marc Valin and Gregory Maxwell */ 4 Written by Jean-Marc Valin and Gregory Maxwell */
5 /* 5 /*
6 Redistribution and use in source and binary forms, with or without 6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions 7 modification, are permitted provided that the following conditions
8 are met: 8 are met:
9 9
10 - Redistributions of source code must retain the above copyright 10 - Redistributions of source code must retain the above copyright
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 { 154 {
155 opus_custom_encoder_destroy(st); 155 opus_custom_encoder_destroy(st);
156 st = NULL; 156 st = NULL;
157 } 157 }
158 if (error) 158 if (error)
159 *error = ret; 159 *error = ret;
160 return st; 160 return st;
161 } 161 }
162 #endif /* CUSTOM_MODES */ 162 #endif /* CUSTOM_MODES */
163 163
164 int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels) 164 static int opus_custom_encoder_init_arch(CELTEncoder *st, const CELTMode *mode,
165 { 165 int channels, int arch)
166 int ret;
167 ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
168 if (ret != OPUS_OK)
169 return ret;
170 st->upsample = resampling_factor(sampling_rate);
171 return OPUS_OK;
172 }
173
174 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMod e *mode, int channels)
175 { 166 {
176 if (channels < 0 || channels > 2) 167 if (channels < 0 || channels > 2)
177 return OPUS_BAD_ARG; 168 return OPUS_BAD_ARG;
178 169
179 if (st==NULL || mode==NULL) 170 if (st==NULL || mode==NULL)
180 return OPUS_ALLOC_FAIL; 171 return OPUS_ALLOC_FAIL;
181 172
182 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels)); 173 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
183 174
184 st->mode = mode; 175 st->mode = mode;
185 st->overlap = mode->overlap; 176 st->overlap = mode->overlap;
186 st->stream_channels = st->channels = channels; 177 st->stream_channels = st->channels = channels;
187 178
188 st->upsample = 1; 179 st->upsample = 1;
189 st->start = 0; 180 st->start = 0;
190 st->end = st->mode->effEBands; 181 st->end = st->mode->effEBands;
191 st->signalling = 1; 182 st->signalling = 1;
192 183
193 st->arch = opus_select_arch(); 184 st->arch = arch;
194 185
195 st->constrained_vbr = 1; 186 st->constrained_vbr = 1;
196 st->clip = 1; 187 st->clip = 1;
197 188
198 st->bitrate = OPUS_BITRATE_MAX; 189 st->bitrate = OPUS_BITRATE_MAX;
199 st->vbr = 0; 190 st->vbr = 0;
200 st->force_intra = 0; 191 st->force_intra = 0;
201 st->complexity = 5; 192 st->complexity = 5;
202 st->lsb_depth=24; 193 st->lsb_depth=24;
203 194
204 opus_custom_encoder_ctl(st, OPUS_RESET_STATE); 195 opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
205 196
206 return OPUS_OK; 197 return OPUS_OK;
207 } 198 }
208 199
209 #ifdef CUSTOM_MODES 200 #ifdef CUSTOM_MODES
201 int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels )
202 {
203 return opus_custom_encoder_init_arch(st, mode, channels, opus_select_arch());
204 }
205 #endif
206
207 int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels,
208 int arch)
209 {
210 int ret;
211 ret = opus_custom_encoder_init_arch(st,
212 opus_custom_mode_create(48000, 960, NULL), channels, arch);
213 if (ret != OPUS_OK)
214 return ret;
215 st->upsample = resampling_factor(sampling_rate);
216 return OPUS_OK;
217 }
218
219 #ifdef CUSTOM_MODES
210 void opus_custom_encoder_destroy(CELTEncoder *st) 220 void opus_custom_encoder_destroy(CELTEncoder *st)
211 { 221 {
212 opus_free(st); 222 opus_free(st);
213 } 223 }
214 #endif /* CUSTOM_MODES */ 224 #endif /* CUSTOM_MODES */
215 225
216 226
217 static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C, 227 static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C,
218 opus_val16 *tf_estimate, int *tf_chan) 228 opus_val16 *tf_estimate, int *tf_chan)
219 { 229 {
(...skipping 13 matching lines...) Expand all
233 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 243 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6,
234 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 244 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5,
235 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 245 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
236 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 246 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3,
237 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 247 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
238 }; 248 };
239 SAVE_STACK; 249 SAVE_STACK;
240 ALLOC(tmp, len, opus_val16); 250 ALLOC(tmp, len, opus_val16);
241 251
242 len2=len/2; 252 len2=len/2;
243 tf_max = 0;
244 for (c=0;c<C;c++) 253 for (c=0;c<C;c++)
245 { 254 {
246 opus_val32 mean; 255 opus_val32 mean;
247 opus_int32 unmask=0; 256 opus_int32 unmask=0;
248 opus_val32 norm; 257 opus_val32 norm;
249 opus_val16 maxE; 258 opus_val16 maxE;
250 mem0=0; 259 mem0=0;
251 mem1=0; 260 mem1=0;
252 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */ 261 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
253 for (i=0;i<len;i++) 262 for (i=0;i<len;i++)
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 int bound = B*N/upsample; 453 int bound = B*N/upsample;
445 for (i=0;i<bound;i++) 454 for (i=0;i<bound;i++)
446 out[c*B*N+i] *= upsample; 455 out[c*B*N+i] *= upsample;
447 for (;i<B*N;i++) 456 for (;i<B*N;i++)
448 out[c*B*N+i] = 0; 457 out[c*B*N+i] = 0;
449 } while (++c<C); 458 } while (++c<C);
450 } 459 }
451 } 460 }
452 461
453 462
454 void preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp, 463 void celt_preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RES TRICT inp,
455 int N, int CC, int upsample, const opus_val16 *coef, cel t_sig *mem, int clip) 464 int N, int CC, int upsample, const opus_val16 *coef, cel t_sig *mem, int clip)
456 { 465 {
457 int i; 466 int i;
458 opus_val16 coef0; 467 opus_val16 coef0;
459 celt_sig m; 468 celt_sig m;
460 int Nu; 469 int Nu;
461 470
462 coef0 = coef[0]; 471 coef0 = coef[0];
463 472
464 473
(...skipping 16 matching lines...) Expand all
481 inp[i*upsample] = x; 490 inp[i*upsample] = x;
482 } 491 }
483 492
484 #ifndef FIXED_POINT 493 #ifndef FIXED_POINT
485 if (clip) 494 if (clip)
486 { 495 {
487 /* Clip input to avoid encoding non-portable files */ 496 /* Clip input to avoid encoding non-portable files */
488 for (i=0;i<Nu;i++) 497 for (i=0;i<Nu;i++)
489 inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample])); 498 inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]));
490 } 499 }
500 #else
501 (void)clip; /* Avoids a warning about clip being unused. */
491 #endif 502 #endif
492 m = *mem; 503 m = *mem;
493 #ifdef CUSTOM_MODES 504 #ifdef CUSTOM_MODES
494 if (coef[1] != 0) 505 if (coef[1] != 0)
495 { 506 {
496 opus_val16 coef1 = coef[1]; 507 opus_val16 coef1 = coef[1];
497 opus_val16 coef2 = coef[2]; 508 opus_val16 coef2 = coef[2];
498 for (i=0;i<N;i++) 509 for (i=0;i<N;i++)
499 { 510 {
500 opus_val16 x, tmp; 511 celt_sig x, tmp;
501 x = inp[i]; 512 x = inp[i];
502 /* Apply pre-emphasis */ 513 /* Apply pre-emphasis */
503 tmp = MULT16_16(coef2, x); 514 tmp = MULT16_16(coef2, x);
504 inp[i] = tmp + m; 515 inp[i] = tmp + m;
505 m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp); 516 m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp);
506 } 517 }
507 } else 518 } else
508 #endif 519 #endif
509 { 520 {
510 for (i=0;i<N;i++) 521 for (i=0;i<N;i++)
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
815 if (diff < -QCONST16(4.f, DB_SHIFT)) 826 if (diff < -QCONST16(4.f, DB_SHIFT))
816 trim_index++; 827 trim_index++;
817 if (diff < -QCONST16(10.f, DB_SHIFT)) 828 if (diff < -QCONST16(10.f, DB_SHIFT))
818 trim_index++; 829 trim_index++;
819 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), SHR16(diff+QCONST16( 1.f, DB_SHIFT),DB_SHIFT-8)/6 )); 830 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), SHR16(diff+QCONST16( 1.f, DB_SHIFT),DB_SHIFT-8)/6 ));
820 trim -= SHR16(surround_trim, DB_SHIFT-8); 831 trim -= SHR16(surround_trim, DB_SHIFT-8);
821 trim -= 2*SHR16(tf_estimate, 14-8); 832 trim -= 2*SHR16(tf_estimate, 14-8);
822 #ifndef DISABLE_FLOAT_API 833 #ifndef DISABLE_FLOAT_API
823 if (analysis->valid) 834 if (analysis->valid)
824 { 835 {
825 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), QCONST16(2.f, 8)* (analysis->tonality_slope+.05f))); 836 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8),
837 (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f))));
826 } 838 }
827 #endif 839 #endif
828 840
829 #ifdef FIXED_POINT 841 #ifdef FIXED_POINT
830 trim_index = PSHR32(trim, 8); 842 trim_index = PSHR32(trim, 8);
831 #else 843 #else
832 trim_index = (int)floor(.5f+trim); 844 trim_index = (int)floor(.5f+trim);
833 #endif 845 #endif
834 if (trim_index<0) 846 if (trim_index<0)
835 trim_index = 0; 847 trim_index = 0;
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 c=0; do { 1028 c=0; do {
1017 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERI OD); 1029 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERI OD);
1018 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N ); 1030 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N );
1019 } while (++c<CC); 1031 } while (++c<CC);
1020 1032
1021 if (enabled) 1033 if (enabled)
1022 { 1034 {
1023 VARDECL(opus_val16, pitch_buf); 1035 VARDECL(opus_val16, pitch_buf);
1024 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16); 1036 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1025 1037
1026 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC); 1038 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC, st->arch);
1027 /* Don't search for the fir last 1.5 octave of the range because 1039 /* Don't search for the fir last 1.5 octave of the range because
1028 there's too many false-positives due to short-term correlation */ 1040 there's too many false-positives due to short-term correlation */
1029 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N, 1041 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1030 COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index); 1042 COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index,
1043 st->arch);
1031 pitch_index = COMBFILTER_MAXPERIOD-pitch_index; 1044 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1032 1045
1033 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPER IOD, 1046 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPER IOD,
1034 N, &pitch_index, st->prefilter_period, st->prefilter_gain); 1047 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1035 if (pitch_index > COMBFILTER_MAXPERIOD-2) 1048 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1036 pitch_index = COMBFILTER_MAXPERIOD-2; 1049 pitch_index = COMBFILTER_MAXPERIOD-2;
1037 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1); 1050 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1038 /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.t onality);*/ 1051 /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.t onality);*/
1039 if (st->loss_rate>2) 1052 if (st->loss_rate>2)
1040 gain1 = HALF32(gain1); 1053 gain1 = HALF32(gain1);
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1149 /* Stereo savings */ 1162 /* Stereo savings */
1150 if (C==2) 1163 if (C==2)
1151 { 1164 {
1152 int coded_stereo_bands; 1165 int coded_stereo_bands;
1153 int coded_stereo_dof; 1166 int coded_stereo_dof;
1154 opus_val16 max_frac; 1167 opus_val16 max_frac;
1155 coded_stereo_bands = IMIN(intensity, coded_bands); 1168 coded_stereo_bands = IMIN(intensity, coded_bands);
1156 coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands; 1169 coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
1157 /* Maximum fraction of the bits we can save if the signal is mono. */ 1170 /* Maximum fraction of the bits we can save if the signal is mono. */
1158 max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded _bins); 1171 max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded _bins);
1172 stereo_saving = MIN16(stereo_saving, QCONST16(1.f, 8));
1159 /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/ 1173 /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
1160 target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target), 1174 target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target),
1161 SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_ster eo_dof<<BITRES)),8)); 1175 SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_ster eo_dof<<BITRES)),8));
1162 } 1176 }
1163 /* Boost the rate according to dynalloc (minus the dynalloc average for calib ration). */ 1177 /* Boost the rate according to dynalloc (minus the dynalloc average for calib ration). */
1164 target += tot_boost-(16<<LM); 1178 target += tot_boost-(16<<LM);
1165 /* Apply transient boost, compensating for average boost. */ 1179 /* Apply transient boost, compensating for average boost. */
1166 tf_calibration = variable_duration==OPUS_FRAMESIZE_VARIABLE ? 1180 tf_calibration = variable_duration==OPUS_FRAMESIZE_VARIABLE ?
1167 QCONST16(0.02f,14) : QCONST16(0.04f,14); 1181 QCONST16(0.02f,14) : QCONST16(0.04f,14);
1168 target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target) ,1); 1182 target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target) ,1);
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
1287 const OpusCustomMode *mode; 1301 const OpusCustomMode *mode;
1288 int nbEBands; 1302 int nbEBands;
1289 int overlap; 1303 int overlap;
1290 const opus_int16 *eBands; 1304 const opus_int16 *eBands;
1291 int secondMdct; 1305 int secondMdct;
1292 int signalBandwidth; 1306 int signalBandwidth;
1293 int transient_got_disabled=0; 1307 int transient_got_disabled=0;
1294 opus_val16 surround_masking=0; 1308 opus_val16 surround_masking=0;
1295 opus_val16 temporal_vbr=0; 1309 opus_val16 temporal_vbr=0;
1296 opus_val16 surround_trim = 0; 1310 opus_val16 surround_trim = 0;
1311 opus_int32 equiv_rate = 510000;
1297 VARDECL(opus_val16, surround_dynalloc); 1312 VARDECL(opus_val16, surround_dynalloc);
1298 ALLOC_STACK; 1313 ALLOC_STACK;
1299 1314
1300 mode = st->mode; 1315 mode = st->mode;
1301 nbEBands = mode->nbEBands; 1316 nbEBands = mode->nbEBands;
1302 overlap = mode->overlap; 1317 overlap = mode->overlap;
1303 eBands = mode->eBands; 1318 eBands = mode->eBands;
1304 tf_estimate = 0; 1319 tf_estimate = 0;
1305 if (nbCompressedBytes<2 || pcm==NULL) 1320 if (nbCompressedBytes<2 || pcm==NULL)
1306 return OPUS_BAD_ARG; 1321 {
1322 RESTORE_STACK;
1323 return OPUS_BAD_ARG;
1324 }
1307 1325
1308 frame_size *= st->upsample; 1326 frame_size *= st->upsample;
1309 for (LM=0;LM<=mode->maxLM;LM++) 1327 for (LM=0;LM<=mode->maxLM;LM++)
1310 if (mode->shortMdctSize<<LM==frame_size) 1328 if (mode->shortMdctSize<<LM==frame_size)
1311 break; 1329 break;
1312 if (LM>mode->maxLM) 1330 if (LM>mode->maxLM)
1331 {
1332 RESTORE_STACK;
1313 return OPUS_BAD_ARG; 1333 return OPUS_BAD_ARG;
1334 }
1314 M=1<<LM; 1335 M=1<<LM;
1315 N = M*mode->shortMdctSize; 1336 N = M*mode->shortMdctSize;
1316 1337
1317 prefilter_mem = st->in_mem+CC*(st->overlap); 1338 prefilter_mem = st->in_mem+CC*(st->overlap);
1318 oldBandE = (opus_val16*)(st->in_mem+CC*(st->overlap+COMBFILTER_MAXPERIOD)); 1339 oldBandE = (opus_val16*)(st->in_mem+CC*(st->overlap+COMBFILTER_MAXPERIOD));
1319 oldLogE = oldBandE + CC*nbEBands; 1340 oldLogE = oldBandE + CC*nbEBands;
1320 oldLogE2 = oldLogE + CC*nbEBands; 1341 oldLogE2 = oldLogE + CC*nbEBands;
1321 1342
1322 if (enc==NULL) 1343 if (enc==NULL)
1323 { 1344 {
(...skipping 10 matching lines...) Expand all
1334 int tmp = (mode->effEBands-st->end)>>1; 1355 int tmp = (mode->effEBands-st->end)>>1;
1335 st->end = IMAX(1, mode->effEBands-tmp); 1356 st->end = IMAX(1, mode->effEBands-tmp);
1336 compressed[0] = tmp<<5; 1357 compressed[0] = tmp<<5;
1337 compressed[0] |= LM<<3; 1358 compressed[0] |= LM<<3;
1338 compressed[0] |= (C==2)<<2; 1359 compressed[0] |= (C==2)<<2;
1339 /* Convert "standard mode" to Opus header */ 1360 /* Convert "standard mode" to Opus header */
1340 if (mode->Fs==48000 && mode->shortMdctSize==120) 1361 if (mode->Fs==48000 && mode->shortMdctSize==120)
1341 { 1362 {
1342 int c0 = toOpus(compressed[0]); 1363 int c0 = toOpus(compressed[0]);
1343 if (c0<0) 1364 if (c0<0)
1365 {
1366 RESTORE_STACK;
1344 return OPUS_BAD_ARG; 1367 return OPUS_BAD_ARG;
1368 }
1345 compressed[0] = c0; 1369 compressed[0] = c0;
1346 } 1370 }
1347 compressed++; 1371 compressed++;
1348 nbCompressedBytes--; 1372 nbCompressedBytes--;
1349 } 1373 }
1350 #else 1374 #else
1351 celt_assert(st->signalling==0); 1375 celt_assert(st->signalling==0);
1352 #endif 1376 #endif
1353 1377
1354 /* Can't produce more than 1275 output bytes */ 1378 /* Can't produce more than 1275 output bytes */
(...skipping 13 matching lines...) Expand all
1368 opus_int32 tmp; 1392 opus_int32 tmp;
1369 vbr_rate = 0; 1393 vbr_rate = 0;
1370 tmp = st->bitrate*frame_size; 1394 tmp = st->bitrate*frame_size;
1371 if (tell>1) 1395 if (tell>1)
1372 tmp += tell; 1396 tmp += tell;
1373 if (st->bitrate!=OPUS_BITRATE_MAX) 1397 if (st->bitrate!=OPUS_BITRATE_MAX)
1374 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes, 1398 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1375 (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling)); 1399 (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
1376 effectiveBytes = nbCompressedBytes; 1400 effectiveBytes = nbCompressedBytes;
1377 } 1401 }
1402 if (st->bitrate != OPUS_BITRATE_MAX)
1403 equiv_rate = st->bitrate - (40*C+20)*((400>>LM) - 50);
1378 1404
1379 if (enc==NULL) 1405 if (enc==NULL)
1380 { 1406 {
1381 ec_enc_init(&_enc, compressed, nbCompressedBytes); 1407 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1382 enc = &_enc; 1408 enc = &_enc;
1383 } 1409 }
1384 1410
1385 if (vbr_rate>0) 1411 if (vbr_rate>0)
1386 { 1412 {
1387 /* Computes the max bit-rate allowed in VBR mode to avoid violating the 1413 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1441 total_bits=nbCompressedBytes*8; 1467 total_bits=nbCompressedBytes*8;
1442 nbAvailableBytes=2; 1468 nbAvailableBytes=2;
1443 ec_enc_shrink(enc, nbCompressedBytes); 1469 ec_enc_shrink(enc, nbCompressedBytes);
1444 } 1470 }
1445 /* Pretend we've filled all the remaining bits with zeros 1471 /* Pretend we've filled all the remaining bits with zeros
1446 (that's what the initialiser did anyway) */ 1472 (that's what the initialiser did anyway) */
1447 tell = nbCompressedBytes*8; 1473 tell = nbCompressedBytes*8;
1448 enc->nbits_total+=tell-ec_tell(enc); 1474 enc->nbits_total+=tell-ec_tell(enc);
1449 } 1475 }
1450 c=0; do { 1476 c=0; do {
1451 preemphasis(pcm+c, in+c*(N+st->overlap)+st->overlap, N, CC, st->upsample, 1477 celt_preemphasis(pcm+c, in+c*(N+st->overlap)+st->overlap, N, CC, st->upsam ple,
1452 mode->preemph, st->preemph_memE+c, st->clip); 1478 mode->preemph, st->preemph_memE+c, st->clip);
1453 } while (++c<CC); 1479 } while (++c<CC);
1454 1480
1455 1481
1456 1482
1457 /* Find pitch period and gain */ 1483 /* Find pitch period and gain */
1458 { 1484 {
1459 int enabled; 1485 int enabled;
1460 int qg; 1486 int qg;
1461 enabled = (st->lfe || nbAvailableBytes>12*C) && st->start==0 && !silence & & !st->disable_pf 1487 enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && st-> start==0 && !silence && !st->disable_pf
1462 && st->complexity >= 5 && !(st->consec_transient && LM!=3 && st->var iable_duration==OPUS_FRAMESIZE_VARIABLE); 1488 && st->complexity >= 5 && !(st->consec_transient && LM!=3 && st->var iable_duration==OPUS_FRAMESIZE_VARIABLE);
1463 1489
1464 prefilter_tapset = st->tapset_decision; 1490 prefilter_tapset = st->tapset_decision;
1465 pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pit ch_index, &gain1, &qg, enabled, nbAvailableBytes); 1491 pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pit ch_index, &gain1, &qg, enabled, nbAvailableBytes);
1466 if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3) 1492 if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
1467 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st- >prefilter_period)) 1493 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st- >prefilter_period))
1468 pitch_change = 1; 1494 pitch_change = 1;
1469 if (pf_on==0) 1495 if (pf_on==0)
1470 { 1496 {
1471 if(st->start==0 && tell+16<=total_bits) 1497 if(st->start==0 && tell+16<=total_bits)
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
1605 mask_avg += QCONST16(.2f, DB_SHIFT); 1631 mask_avg += QCONST16(.2f, DB_SHIFT);
1606 /* Convert to 1/64th units used for the trim */ 1632 /* Convert to 1/64th units used for the trim */
1607 surround_trim = 64*diff; 1633 surround_trim = 64*diff;
1608 /*printf("%d %d ", mask_avg, surround_trim);*/ 1634 /*printf("%d %d ", mask_avg, surround_trim);*/
1609 surround_masking = mask_avg; 1635 surround_masking = mask_avg;
1610 } 1636 }
1611 /* Temporal VBR (but not for LFE) */ 1637 /* Temporal VBR (but not for LFE) */
1612 if (!st->lfe) 1638 if (!st->lfe)
1613 { 1639 {
1614 opus_val16 follow=-QCONST16(10.0f,DB_SHIFT); 1640 opus_val16 follow=-QCONST16(10.0f,DB_SHIFT);
1615 float frame_avg=0; 1641 opus_val32 frame_avg=0;
1616 opus_val16 offset = shortBlocks?HALF16(SHL16(LM, DB_SHIFT)):0; 1642 opus_val16 offset = shortBlocks?HALF16(SHL16(LM, DB_SHIFT)):0;
1617 for(i=st->start;i<st->end;i++) 1643 for(i=st->start;i<st->end;i++)
1618 { 1644 {
1619 follow = MAX16(follow-QCONST16(1.f, DB_SHIFT), bandLogE[i]-offset); 1645 follow = MAX16(follow-QCONST16(1.f, DB_SHIFT), bandLogE[i]-offset);
1620 if (C==2) 1646 if (C==2)
1621 follow = MAX16(follow, bandLogE[i+nbEBands]-offset); 1647 follow = MAX16(follow, bandLogE[i+nbEBands]-offset);
1622 frame_avg += follow; 1648 frame_avg += follow;
1623 } 1649 }
1624 frame_avg /= (st->end-st->start); 1650 frame_avg /= (st->end-st->start);
1625 temporal_vbr = SUB16(frame_avg,st->spec_avg); 1651 temporal_vbr = SUB16(frame_avg,st->spec_avg);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1703 } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C || st->start != 0) 1729 } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C || st->start != 0)
1704 { 1730 {
1705 if (st->complexity == 0) 1731 if (st->complexity == 0)
1706 st->spread_decision = SPREAD_NONE; 1732 st->spread_decision = SPREAD_NONE;
1707 else 1733 else
1708 st->spread_decision = SPREAD_NORMAL; 1734 st->spread_decision = SPREAD_NORMAL;
1709 } else { 1735 } else {
1710 /* Disable new spreading+tapset estimator until we can show it works 1736 /* Disable new spreading+tapset estimator until we can show it works
1711 better than the old one. So far it seems like spreading_decision() 1737 better than the old one. So far it seems like spreading_decision()
1712 works best. */ 1738 works best. */
1713 if (0&&st->analysis.valid) 1739 #if 0
1740 if (st->analysis.valid)
1714 { 1741 {
1715 static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)}; 1742 static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
1716 static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)}; 1743 static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
1717 static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), Q CONST16(.15f, 15)}; 1744 static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), Q CONST16(.15f, 15)};
1718 static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), Q CONST16(.05f, 15)}; 1745 static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), Q CONST16(.05f, 15)};
1719 st->spread_decision = hysteresis_decision(-st->analysis.tonality, sp read_thresholds, spread_histeresis, 3, st->spread_decision); 1746 st->spread_decision = hysteresis_decision(-st->analysis.tonality, sp read_thresholds, spread_histeresis, 3, st->spread_decision);
1720 st->tapset_decision = hysteresis_decision(st->analysis.tonality_slop e, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision); 1747 st->tapset_decision = hysteresis_decision(st->analysis.tonality_slop e, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
1721 } else { 1748 } else
1749 #endif
1750 {
1722 st->spread_decision = spreading_decision(mode, X, 1751 st->spread_decision = spreading_decision(mode, X,
1723 &st->tonal_average, st->spread_decision, &st->hf_average, 1752 &st->tonal_average, st->spread_decision, &st->hf_average,
1724 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M); 1753 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1725 } 1754 }
1726 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/ 1755 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
1727 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/ 1756 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
1728 } 1757 }
1729 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5); 1758 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1730 } 1759 }
1731 1760
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1770 dynalloc_loop_logp = 1; 1799 dynalloc_loop_logp = 1;
1771 } 1800 }
1772 /* Making dynalloc more likely */ 1801 /* Making dynalloc more likely */
1773 if (j) 1802 if (j)
1774 dynalloc_logp = IMAX(2, dynalloc_logp-1); 1803 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1775 offsets[i] = boost; 1804 offsets[i] = boost;
1776 } 1805 }
1777 1806
1778 if (C==2) 1807 if (C==2)
1779 { 1808 {
1780 int effectiveRate;
1781
1782 static const opus_val16 intensity_thresholds[21]= 1809 static const opus_val16 intensity_thresholds[21]=
1783 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/ 1810 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/
1784 { 16,21,23,25,27,29,31,33,35,38,42,46,50,54,58,63,68,75,84,102,130}; 1811 { 1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134};
1785 static const opus_val16 intensity_histeresis[21]= 1812 static const opus_val16 intensity_histeresis[21]=
1786 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 8, 12}; 1813 { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6, 8, 8};
1787 1814
1788 /* Always use MS for 2.5 ms frames until we can do a better analysis */ 1815 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1789 if (LM!=0) 1816 if (LM!=0)
1790 dual_stereo = stereo_analysis(mode, X, LM, N); 1817 dual_stereo = stereo_analysis(mode, X, LM, N);
1791 1818
1792 /* Account for coarse energy */ 1819 st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000),
1793 effectiveRate = (8*effectiveBytes - 80)>>LM; 1820 intensity_thresholds, intensity_histeresis, 21, st->intensity);
1794
1795 /* effectiveRate in kb/s */
1796 effectiveRate = 2*effectiveRate/5;
1797
1798 st->intensity = hysteresis_decision((opus_val16)effectiveRate, intensity_t hresholds, intensity_histeresis, 21, st->intensity);
1799 st->intensity = IMIN(st->end,IMAX(st->start, st->intensity)); 1821 st->intensity = IMIN(st->end,IMAX(st->start, st->intensity));
1800 } 1822 }
1801 1823
1802 alloc_trim = 5; 1824 alloc_trim = 5;
1803 if (tell+(6<<BITRES) <= total_bits - total_boost) 1825 if (tell+(6<<BITRES) <= total_bits - total_boost)
1804 { 1826 {
1805 if (st->lfe) 1827 if (st->lfe)
1806 alloc_trim = 5; 1828 alloc_trim = 5;
1807 else 1829 else
1808 alloc_trim = alloc_trim_analysis(mode, X, bandLogE, 1830 alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
(...skipping 13 matching lines...) Expand all
1822 int lm_diff = mode->maxLM - LM; 1844 int lm_diff = mode->maxLM - LM;
1823 1845
1824 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms. 1846 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1825 The CELT allocator will just not be able to use more than that anyway. * / 1847 The CELT allocator will just not be able to use more than that anyway. * /
1826 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM)); 1848 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1827 base_target = vbr_rate - ((40*C+20)<<BITRES); 1849 base_target = vbr_rate - ((40*C+20)<<BITRES);
1828 1850
1829 if (st->constrained_vbr) 1851 if (st->constrained_vbr)
1830 base_target += (st->vbr_offset>>lm_diff); 1852 base_target += (st->vbr_offset>>lm_diff);
1831 1853
1832 target = compute_vbr(mode, &st->analysis, base_target, LM, st->bitrate, 1854 target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate,
1833 st->lastCodedBands, C, st->intensity, st->constrained_vbr, 1855 st->lastCodedBands, C, st->intensity, st->constrained_vbr,
1834 st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth, 1856 st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
1835 st->variable_duration, st->lfe, st->energy_mask!=NULL, surround_maski ng, 1857 st->variable_duration, st->lfe, st->energy_mask!=NULL, surround_maski ng,
1836 temporal_vbr); 1858 temporal_vbr);
1837 1859
1838 /* The current offset is removed from the target and the space used 1860 /* The current offset is removed from the target and the space used
1839 so far is added*/ 1861 so far is added*/
1840 target=target+tell; 1862 target=target+tell;
1841 /* In VBR mode the frame size must not be reduced so much that it would 1863 /* In VBR mode the frame size must not be reduced so much that it would
1842 result in the encoder running out of bits. 1864 result in the encoder running out of bits.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1906 1928
1907 /* bits = packet size - where we are - safety*/ 1929 /* bits = packet size - where we are - safety*/
1908 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1; 1930 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1909 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; 1931 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1910 bits -= anti_collapse_rsv; 1932 bits -= anti_collapse_rsv;
1911 signalBandwidth = st->end-1; 1933 signalBandwidth = st->end-1;
1912 #ifndef DISABLE_FLOAT_API 1934 #ifndef DISABLE_FLOAT_API
1913 if (st->analysis.valid) 1935 if (st->analysis.valid)
1914 { 1936 {
1915 int min_bandwidth; 1937 int min_bandwidth;
1916 if (st->bitrate < (opus_int32)32000*C) 1938 if (equiv_rate < (opus_int32)32000*C)
1917 min_bandwidth = 13; 1939 min_bandwidth = 13;
1918 else if (st->bitrate < (opus_int32)48000*C) 1940 else if (equiv_rate < (opus_int32)48000*C)
1919 min_bandwidth = 16; 1941 min_bandwidth = 16;
1920 else if (st->bitrate < (opus_int32)60000*C) 1942 else if (equiv_rate < (opus_int32)60000*C)
1921 min_bandwidth = 18; 1943 min_bandwidth = 18;
1922 else if (st->bitrate < (opus_int32)80000*C) 1944 else if (equiv_rate < (opus_int32)80000*C)
1923 min_bandwidth = 19; 1945 min_bandwidth = 19;
1924 else 1946 else
1925 min_bandwidth = 20; 1947 min_bandwidth = 20;
1926 signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth); 1948 signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth);
1927 } 1949 }
1928 #endif 1950 #endif
1929 if (st->lfe) 1951 if (st->lfe)
1930 signalBandwidth = 1; 1952 signalBandwidth = 1;
1931 codedBands = compute_allocation(mode, st->start, st->end, offsets, cap, 1953 codedBands = compute_allocation(mode, st->start, st->end, offsets, cap,
1932 alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses, 1954 alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
2322 } 2344 }
2323 va_end(ap); 2345 va_end(ap);
2324 return OPUS_OK; 2346 return OPUS_OK;
2325 bad_arg: 2347 bad_arg:
2326 va_end(ap); 2348 va_end(ap);
2327 return OPUS_BAD_ARG; 2349 return OPUS_BAD_ARG;
2328 bad_request: 2350 bad_request:
2329 va_end(ap); 2351 va_end(ap);
2330 return OPUS_UNIMPLEMENTED; 2352 return OPUS_UNIMPLEMENTED;
2331 } 2353 }
OLDNEW
« no previous file with comments | « celt/celt_decoder.c ('k') | celt/celt_lpc.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698