From 37f48417066169f4cbf179755c31bfea839cf255 Mon Sep 17 00:00:00 2001 From: eblanca Date: Fri, 14 Jun 2019 17:17:52 +0200 Subject: [PATCH] speedup in psymodel call --- libtwolame/psycho_0.c | 6 +++-- libtwolame/psycho_0.h | 2 +- libtwolame/psycho_1.c | 6 +++-- libtwolame/psycho_1.h | 3 +-- libtwolame/psycho_2.c | 7 +++-- libtwolame/psycho_2.h | 3 +-- libtwolame/psycho_3.c | 6 +++-- libtwolame/psycho_3.h | 3 +-- libtwolame/psycho_4.c | 8 ++++-- libtwolame/psycho_4.h | 3 +-- libtwolame/psycho_n1.c | 4 ++- libtwolame/psycho_n1.h | 2 +- libtwolame/twolame.c | 60 ++++++++++++++++-------------------------- 13 files changed, 55 insertions(+), 58 deletions(-) diff --git a/libtwolame/psycho_0.c b/libtwolame/psycho_0.c index af39b6d..47ac2b3 100644 --- a/libtwolame/psycho_0.c +++ b/libtwolame/psycho_0.c @@ -73,13 +73,15 @@ static psycho_0_mem *twolame_psycho_0_init(twolame_options * glopts, int sfreq) -void twolame_psycho_0(twolame_options * glopts, FLOAT SMR[2][SBLIMIT], unsigned int scalar[2][3][SBLIMIT]) +void twolame_psycho_0(twolame_options * glopts) { psycho_0_mem *mem; int nch = glopts->num_channels_out; int sfreq = glopts->samplerate_out; int ch, sb, gr; unsigned int minscaleindex[2][SBLIMIT]; /* Smaller scale indexes mean bigger scalefactors */ + unsigned int (*scalar)[3][SBLIMIT] = glopts->scalar; + FLOAT (*smr)[SBLIMIT] = glopts->smr; if (!glopts->p0mem) { glopts->p0mem = twolame_psycho_0_init(glopts, sfreq); @@ -113,7 +115,7 @@ void twolame_psycho_0(twolame_options * glopts, FLOAT SMR[2][SBLIMIT], unsigned these values, but who cares? It works pretty well MFC Mar 03 */ for (ch = 0; ch < nch; ch++) for (sb = 0; sb < SBLIMIT; sb++) - SMR[ch][sb] = 2.0 * (30.0 - minscaleindex[ch][sb]) - mem->ath_min[sb]; + smr[ch][sb] = 2.0 * (30.0 - minscaleindex[ch][sb]) - mem->ath_min[sb]; } diff --git a/libtwolame/psycho_0.h b/libtwolame/psycho_0.h index 25ada84..920334a 100644 --- a/libtwolame/psycho_0.h +++ b/libtwolame/psycho_0.h @@ -23,7 +23,7 @@ #ifndef TWOLAME_PSYCHO_0_H #define TWOLAME_PSYCHO_0_H -void twolame_psycho_0(twolame_options * glopts, FLOAT SMR[2][SBLIMIT], unsigned int scalar[2][3][SBLIMIT]); +void twolame_psycho_0(twolame_options * glopts); void twolame_psycho_0_deinit(psycho_0_mem ** mem); #endif diff --git a/libtwolame/psycho_1.c b/libtwolame/psycho_1.c index 9daba24..418fda8 100644 --- a/libtwolame/psycho_1.c +++ b/libtwolame/psycho_1.c @@ -554,8 +554,7 @@ static void psycho_1_dump(mask power[HAN_SIZE], int *tone, int *noise) { */ -void twolame_psycho_1(twolame_options * glopts, short buffer[2][1152], FLOAT scale[2][SBLIMIT], - FLOAT ltmin[2][SBLIMIT]) +void twolame_psycho_1(twolame_options * glopts) { psycho_1_mem *mem; frame_header *header = &glopts->header; @@ -566,6 +565,9 @@ void twolame_psycho_1(twolame_options * glopts, short buffer[2][1152], FLOAT sca FLOAT spike[2][SBLIMIT]; FLOAT *fft_buf[2]; FLOAT energy[FFT_SIZE]; + FLOAT (*ltmin)[SBLIMIT] = glopts->smr; + FLOAT (*scale)[SBLIMIT] = glopts->max_sc; + short (*buffer)[1152] = glopts->buffer; /* call functions for critical boundaries, freq. */ if (!glopts->p1mem) { /* bands, bark values, and mapping */ diff --git a/libtwolame/psycho_1.h b/libtwolame/psycho_1.h index e4a77e5..4adae14 100644 --- a/libtwolame/psycho_1.h +++ b/libtwolame/psycho_1.h @@ -23,8 +23,7 @@ #ifndef TWOLAME_PSYCHO_1_H #define TWOLAME_PSYCHO_1_H -void twolame_psycho_1(twolame_options * glopts, short int buffer[2][1152], FLOAT scale[2][32], - FLOAT ltmin[2][32]); +void twolame_psycho_1(twolame_options * glopts); void twolame_psycho_1_deinit(psycho_1_mem ** mem); #endif diff --git a/libtwolame/psycho_2.c b/libtwolame/psycho_2.c index 07363d1..40bee1b 100644 --- a/libtwolame/psycho_2.c +++ b/libtwolame/psycho_2.c @@ -249,8 +249,7 @@ psycho_2_mem *twolame_psycho_2_init(twolame_options * glopts, int sfreq) return (mem); } -void twolame_psycho_2(twolame_options * glopts, short int buffer[2][1152], - short int savebuf[2][1056], FLOAT smr[2][32]) +void twolame_psycho_2(twolame_options * glopts) { psycho_2_mem *mem; unsigned int i, j, k, ch; @@ -273,9 +272,12 @@ void twolame_psycho_2(twolame_options * glopts, short int buffer[2][1152], FHBLK *lthr; F2HBLK *r, *phi_sav; FLOAT *absthr; + short int savebuf[2][1056]; int nch = glopts->num_channels_out; int sfreq = glopts->samplerate_out; + FLOAT (*smr)[32] = glopts->smr; + short int (*buffer)[1152] = glopts->buffer; if (!glopts->p2mem) { @@ -311,6 +313,7 @@ void twolame_psycho_2(twolame_options * glopts, short int buffer[2][1152], absthr = mem->absthr; } + memset(savebuf, 0, sizeof(savebuf)); for (ch = 0; ch < nch; ch++) { for (i = 0; i < 2; i++) { diff --git a/libtwolame/psycho_2.h b/libtwolame/psycho_2.h index 77563f2..5652fc3 100644 --- a/libtwolame/psycho_2.h +++ b/libtwolame/psycho_2.h @@ -24,8 +24,7 @@ #define TWOLAME_PSYCHO_2_H psycho_2_mem *twolame_psycho_2_init(twolame_options * glopts, int sfreq); -void twolame_psycho_2(twolame_options * glopts, short int buffer[2][1152], short int savebuf[2][1056], - FLOAT smr[2][32]); +void twolame_psycho_2(twolame_options * glopts); void twolame_psycho_2_deinit(psycho_2_mem ** mem); #endif diff --git a/libtwolame/psycho_3.c b/libtwolame/psycho_3.c index 4fac2fa..46f5bfe 100644 --- a/libtwolame/psycho_3.c +++ b/libtwolame/psycho_3.c @@ -521,8 +521,7 @@ static void psycho_3_dump(int *tonelabel, FLOAT * Xtm, int *noiselabel, FLOAT * } -void twolame_psycho_3(twolame_options * glopts, short int buffer[2][1152], FLOAT scale[2][32], - FLOAT ltmin[2][32]) +void twolame_psycho_3(twolame_options * glopts) { psycho_3_mem *mem; int nch = glopts->num_channels_out; @@ -535,6 +534,9 @@ void twolame_psycho_3(twolame_options * glopts, short int buffer[2][1152], FLOAT int tonelabel[HBLKSIZE], noiselabel[HBLKSIZE] = {0}; FLOAT LTg[HBLKSIZE]; FLOAT Lsb[SBLIMIT]; + FLOAT (*ltmin)[32] = glopts->smr; + FLOAT (*scale)[32] = glopts->max_sc; + short int (*buffer)[1152] = glopts->buffer; if (!glopts->p3mem) { glopts->p3mem = twolame_psycho_3_init(glopts); diff --git a/libtwolame/psycho_3.h b/libtwolame/psycho_3.h index ce8281c..eea86a0 100644 --- a/libtwolame/psycho_3.h +++ b/libtwolame/psycho_3.h @@ -23,8 +23,7 @@ #ifndef TWOLAME_PSYCHO_3_H #define TWOLAME_PSYCHO_3_H -void twolame_psycho_3(twolame_options * glopts, short int buffer[2][1152], FLOAT scale[2][32], - FLOAT ltmin[2][32]); +void twolame_psycho_3(twolame_options * glopts); void twolame_psycho_3_deinit(psycho_3_mem ** mem); #endif diff --git a/libtwolame/psycho_4.c b/libtwolame/psycho_4.c index 21e51f3..0e733c1 100644 --- a/libtwolame/psycho_4.c +++ b/libtwolame/psycho_4.c @@ -294,8 +294,7 @@ static psycho_4_mem *twolame_psycho_4_init(twolame_options * glopts, int sfreq) } -void twolame_psycho_4(twolame_options * glopts, - short int buffer[2][1152], short int savebuf[2][1056], FLOAT smr[2][32]) +void twolame_psycho_4(twolame_options * glopts) /* to match prototype : FLOAT args are always FLOAT */ { psycho_4_mem *mem; @@ -315,9 +314,12 @@ void twolame_psycho_4(twolame_options * glopts, FLOAT *tmn; FCB *s; F2HBLK *r, *phi_sav; + short int savebuf[2][1056]; int nch = glopts->num_channels_out; int sfreq = glopts->samplerate_out; + FLOAT (*smr)[32] = glopts->smr; + short int (*buffer)[1152] = glopts->buffer; if (!glopts->p4mem) { glopts->p4mem = twolame_psycho_4_init(glopts, sfreq); @@ -353,6 +355,8 @@ void twolame_psycho_4(twolame_options * glopts, phi_sav = mem->phi_sav; } + memset(savebuf, 0, sizeof(savebuf)); + for (ch = 0; ch < nch; ch++) { for (run = 0; run < 2; run++) { /* Net offset is 480 samples (1056-576) for layer 2; this is because one must stagger diff --git a/libtwolame/psycho_4.h b/libtwolame/psycho_4.h index 624fd16..47fc935 100644 --- a/libtwolame/psycho_4.h +++ b/libtwolame/psycho_4.h @@ -23,8 +23,7 @@ #ifndef TWOLAME_PSYCHO_4_H #define TWOLAME_PSYCHO_4_H -void twolame_psycho_4(twolame_options * glopts, short int buffer[2][1152], short int savebuf[2][1056], - FLOAT smr[2][32]); +void twolame_psycho_4(twolame_options * glopts); void twolame_psycho_4_deinit(psycho_4_mem ** mem); #endif diff --git a/libtwolame/psycho_n1.c b/libtwolame/psycho_n1.c index 0fa2764..ca8ebfe 100644 --- a/libtwolame/psycho_n1.c +++ b/libtwolame/psycho_n1.c @@ -60,9 +60,11 @@ static const FLOAT snrdefc[32] = { }; #endif -void twolame_psycho_n1(twolame_options * glopts, FLOAT ltmin[2][SBLIMIT], int stereo) +void twolame_psycho_n1(twolame_options * glopts) { int i, k; + int stereo = glopts->num_channels_out; + FLOAT (*ltmin)[SBLIMIT] = glopts->smr; for (k = 0; k < stereo; k++) for (i = 0; i < SBLIMIT; i++) diff --git a/libtwolame/psycho_n1.h b/libtwolame/psycho_n1.h index f011556..ff71735 100644 --- a/libtwolame/psycho_n1.h +++ b/libtwolame/psycho_n1.h @@ -23,7 +23,7 @@ #ifndef TWOLAME_PSYCHO_N1_H #define TWOLAME_PSYCHO_N1_H -void twolame_psycho_n1(twolame_options * glopts, FLOAT ltmin[2][SBLIMIT], int stereo); +void twolame_psycho_n1(twolame_options * glopts); #endif diff --git a/libtwolame/twolame.c b/libtwolame/twolame.c index 874ca75..e4e5a25 100644 --- a/libtwolame/twolame.c +++ b/libtwolame/twolame.c @@ -47,6 +47,17 @@ #include "bitbuffer_inline.h" +typedef void (*psycho_model_t)(twolame_options *); + +psycho_model_t psycho_model_array[] = +{ + &twolame_psycho_n1, + &twolame_psycho_0, + &twolame_psycho_1, + &twolame_psycho_2, + &twolame_psycho_3, + &twolame_psycho_4 +}; /* twolame_init @@ -388,6 +399,12 @@ int twolame_init_params(twolame_options * glopts) "twolame_init_params(): sorry, twolame doesn't support resampling (yet).\n"); return -1; } + // Check requested psychoacoustic model + if (glopts->psymodel < -1 || glopts->psymodel > 4) { + fprintf(stderr, + "twolame_init_params(): Invalid psy model specification %d, valid values are into [-1, .. 4].\n", glopts->psymodel); + return -1; + } // Initialise interal variables glopts->samples_in_buffer = 0; @@ -490,10 +507,8 @@ static void scale_and_mix_samples(twolame_options * glopts) */ static int encode_frame(twolame_options * glopts, bit_stream * bs) { - int nch = glopts->num_channels_out; int sb, ch, adb, i; unsigned long frameBits, initial_bits; - short sam[2][1056]; if (!glopts->twolame_init) { fprintf(stderr, "Please call twolame_init_params() before starting encoding.\n"); @@ -502,10 +517,6 @@ static int encode_frame(twolame_options * glopts, bit_stream * bs) // Scale and mix the input buffer scale_and_mix_samples(glopts); - - // Clear the saved audio buffer - memset((char *) sam, 0, sizeof(sam)); - // Number of bits to calculate CRC on glopts->num_crc_bits = 0; @@ -537,13 +548,13 @@ static int encode_frame(twolame_options * glopts, bit_stream * bs) /* New polyphase filter Combines windowing and filtering. Ricardo Feb'03 */ for (gr = 0; gr < 3; gr++) for (bl = 0; bl < 12; bl++) - for (ch = 0; ch < nch; ch++) + for (ch = 0; ch < glopts->num_channels_out; ch++) twolame_window_filter_subband(&glopts->smem, &glopts->buffer[ch][gr * 12 * 32 + 32 * bl], ch, &(*glopts->sb_sample)[ch][gr][bl][0]); } - twolame_scalefactor_calc(*glopts->sb_sample, glopts->scalar, nch, glopts->sblimit); + twolame_scalefactor_calc(*glopts->sb_sample, glopts->scalar, glopts->num_channels_out, glopts->sblimit); twolame_find_sf_max(glopts, glopts->scalar, glopts->max_sc); if (glopts->mode == TWOLAME_JOINT_STEREO) { // this way we calculate more mono than we need but it is cheap @@ -554,43 +565,18 @@ static int encode_frame(twolame_options * glopts, bit_stream * bs) if ((glopts->quickmode == TRUE) && (++glopts->psycount % glopts->quickcount != 0)) { /* We're using quick mode, so we're only calculating the model every 'quickcount' frames. Otherwise, just copy the old ones across */ - for (ch = 0; ch < nch; ch++) { + for (ch = 0; ch < glopts->num_channels_out; ch++) { for (sb = 0; sb < SBLIMIT; sb++) { glopts->smr[ch][sb] = glopts->smrdef[ch][sb]; } } } else { - // calculate the psymodel - switch (glopts->psymodel) { - case -1: - twolame_psycho_n1(glopts, glopts->smr, nch); - break; - case 0: // Psy Model A - twolame_psycho_0(glopts, glopts->smr, glopts->scalar); - break; - case 1: - twolame_psycho_1(glopts, glopts->buffer, glopts->max_sc, glopts->smr); - break; - case 2: - twolame_psycho_2(glopts, glopts->buffer, sam, glopts->smr); - break; - case 3: - // Modified psy model 1 - twolame_psycho_3(glopts, glopts->buffer, glopts->max_sc, glopts->smr); - break; - case 4: - // Modified psy model 2 - twolame_psycho_4(glopts, glopts->buffer, sam, glopts->smr); - break; - default: - fprintf(stderr, "Invalid psy model specification: %i\n", glopts->psymodel); - return -1; - break; - } + + psycho_model_array[glopts->psymodel+1](glopts); if (glopts->quickmode == TRUE) { // copy the smr values and reuse them later - for (ch = 0; ch < nch; ch++) { + for (ch = 0; ch < glopts->num_channels_out; ch++) { for (sb = 0; sb < SBLIMIT; sb++) glopts->smrdef[ch][sb] = glopts->smr[ch][sb]; }