Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup in psymodel call #87

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions libtwolame/psycho_0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
}


Expand Down
2 changes: 1 addition & 1 deletion libtwolame/psycho_0.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions libtwolame/psycho_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down
3 changes: 1 addition & 2 deletions libtwolame/psycho_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions libtwolame/psycho_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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++) {
Expand Down
3 changes: 1 addition & 2 deletions libtwolame/psycho_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions libtwolame/psycho_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions libtwolame/psycho_3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions libtwolame/psycho_4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions libtwolame/psycho_4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion libtwolame/psycho_n1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
2 changes: 1 addition & 1 deletion libtwolame/psycho_n1.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
60 changes: 23 additions & 37 deletions libtwolame/twolame.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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];
}
Expand Down