Ejemplo n.º 1
0
/* reads in the frequency bands and bark values */
static void psycho_1_read_freq_band(g_ptr * ltg, int lay, int freq, int *sub_size)
{

#include "psycho_1_freqtable.h"

    int i, k;

    if ((freq < 0) || (freq > 6) || (freq == 3)) {
        fprintf(stderr, "Internal error (read_freq_band())\n");
        return;
    }

    /* read input for freq. subbands */

    *sub_size = SecondFreqEntries[freq] + 1;
    *ltg = (g_ptr) TWOLAME_MALLOC(sizeof(g_thres) * *sub_size);
    (*ltg)[0].line = 0;         /* initialize global masking threshold */
    (*ltg)[0].bark = 0.0;
    (*ltg)[0].hear = 0.0;
    for (i = 1; i < *sub_size; i++) {
        k = SecondFreqSubband[freq][i - 1].line;
        if (k != 0) {
            (*ltg)[i].line = k;
            (*ltg)[i].bark = SecondFreqSubband[freq][i - 1].bark;
            (*ltg)[i].hear = SecondFreqSubband[freq][i - 1].hear;
        } else {
            fprintf(stderr, "Internal error (read_freq_band())\n");
            return;
        }
    }
}
Ejemplo n.º 2
0
static int *psycho_1_read_cbound(int lay, int freq, int *crit_band)
/* this function reads in critical    band boundaries */
{

#include "psycho_1_critband.h"
    int *cbound;
    int i, k;

    if ((lay < 1) || (lay > 2)) {
        fprintf(stderr, "Internal error (read_cbound())\n");
        return (NULL);
    }
    if ((freq < 0) || (freq > 6) || (freq == 3)) {
        fprintf(stderr, "Internal error (read_cbound())\n");
        return (NULL);
    }

    *crit_band = SecondCriticalBand[freq][0];
    cbound = (int *) TWOLAME_MALLOC(sizeof(int) * *crit_band);
    for (i = 0; i < *crit_band; i++) {
        k = SecondCriticalBand[freq][i + 1];
        if (k != 0) {
            cbound[i] = k;
        } else {
            fprintf(stderr, "Internal error (read_cbound())\n");
            return (NULL);
        }
    }
    return (cbound);
}
Ejemplo n.º 3
0
/*
  twolame_init
  Create a set of encoding options and return a pointer to this structure

  Returns NULL if unsuccessful (can't allocate memory)
  Otherwise returns pointer to memory block
*/
twolame_options *twolame_init(void)
{
    twolame_options *newoptions = NULL;

    newoptions = (twolame_options *) TWOLAME_MALLOC(sizeof(twolame_options));
    if (newoptions == NULL) {
        return NULL;
    }

    memset(newoptions, 0, sizeof(twolame_options));

    newoptions->version = -1;
    newoptions->num_channels_in = 0;
    newoptions->num_channels_out = 0;
    newoptions->samplerate_in = 0;
    newoptions->samplerate_out = 0;

    newoptions->mode = TWOLAME_AUTO_MODE;   // Choose a proper mode later
    newoptions->psymodel = 3;
    newoptions->bitrate = -1;   // Default bitrate is set in init_params
    newoptions->vbr = FALSE;
    newoptions->freeformat = FALSE;
    newoptions->vbrlevel = 5.0;
    newoptions->athlevel = 0.0;

    newoptions->quickmode = FALSE;
    newoptions->quickcount = 10;
    newoptions->emphasis = TWOLAME_EMPHASIS_N;
    newoptions->private_extension = 0;
    newoptions->copyright = FALSE;
    newoptions->original = TRUE;
    newoptions->error_protection = FALSE;
    newoptions->padding = TWOLAME_PAD_NO;
    newoptions->do_dab = FALSE;
    newoptions->dab_crc_len = 2;
    newoptions->dab_xpad_len = 0;
    newoptions->verbosity = 2;
    newoptions->vbr_upper_index = 0;

    newoptions->slots_lag = 0.0;

    newoptions->scale = 1.0;    // scaling disabled
    newoptions->scale_left = 1.0;   // scaling disabled
    newoptions->scale_right = 1.0;  // scaling disabled

    newoptions->do_energy_levels = FALSE;
    newoptions->num_ancillary_bits = -1;

    newoptions->vbr_frame_count = 0;    // only used for debugging
    newoptions->tablenum = 0;

    newoptions->twolame_init = 0;
    newoptions->subband = NULL;
    newoptions->j_sample = NULL;
    newoptions->sb_sample = NULL;
    newoptions->psycount = 0;

    newoptions->p0mem = NULL;
    newoptions->p1mem = NULL;
    newoptions->p2mem = NULL;
    newoptions->p3mem = NULL;
    newoptions->p4mem = NULL;

    return (newoptions);
}
Ejemplo n.º 4
0
int twolame_init_params(twolame_options * glopts)
{

    if (glopts->twolame_init) {
        fprintf(stderr, "Already called twolame_init_params() once.\n");
        return 1;
    }
    // Check the number of channels
    if (glopts->num_channels_in != 1 && glopts->num_channels_in != 2) {
        if (glopts->num_channels_in == 0) {
            /* this error is due to the caller frontend */
            fprintf(stderr,
                    "twolame_init_params(): must specify number of input channels using twolame_set_num_channels().\n");
        }
        else {
            /* this error is due to the user feeding a wrong file */
            fprintf(stderr,
                    "Error: twolame cannot encode files with more than 2 channels.\n");
        }
        return -1;
    }
    // If not output samplerate has been set, then set it to the input sample rate
    if (glopts->samplerate_out < 1) {
        glopts->samplerate_out = glopts->samplerate_in;
    }
    // If the MPEG version has not been set, then choose automatically
    if (glopts->version == -1) {
        // Get the MPEG version for the chosen samplerate
        glopts->version = twolame_get_version_for_samplerate(glopts->samplerate_out);
        if (glopts->version < 0) {
            fprintf(stderr, "twolame_init_params(): invalid samplerate: %i\n",
                    glopts->samplerate_out);
            return -1;
        } else if (glopts->verbosity >= 3) {
            fprintf(stderr, "Chosen version '%s' for samplerate of %d Hz.\n",
                    twolame_mpeg_version_name(glopts->version), glopts->samplerate_out);
        }
    }
    // Choose mode (if none chosen)
    if (glopts->mode == TWOLAME_AUTO_MODE) {
        if (glopts->num_channels_in == 2)
            glopts->mode = TWOLAME_STEREO;
        else
            glopts->mode = TWOLAME_MONO;
        if (glopts->verbosity >= 3) {
            fprintf(stderr, "Chosen mode to be '%s' because of %d input channels.\n",
                    twolame_get_mode_name(glopts), glopts->num_channels_in);
        }
    }
    // Choose the bitrate (if none chosen)
    if (glopts->bitrate < 0 && !glopts->vbr) {
        if (glopts->mode == TWOLAME_MONO) {
            switch (glopts->samplerate_out) {
            case 48000:
                glopts->bitrate = 96;
                break;          // (LAME=64)
            case 44100:
                glopts->bitrate = 96;
                break;          // (LAME=64)
            case 32000:
                glopts->bitrate = 80;
                break;          // (LAME=48)
            case 24000:
                glopts->bitrate = 48;
                break;          // (LAME=32)
            case 22050:
                glopts->bitrate = 48;
                break;          // (LAME=32)
            case 16000:
                glopts->bitrate = 32;
                break;          // (LAME=24)
            }
        } else {
            switch (glopts->samplerate_out) {
            case 48000:
                glopts->bitrate = 192;
                break;          // (LAME=128)
            case 44100:
                glopts->bitrate = 192;
                break;          // (LAME=128)
            case 32000:
                glopts->bitrate = 160;
                break;          // (LAME=96)
            case 24000:
                glopts->bitrate = 96;
                break;          // (LAME=64)
            case 22050:
                glopts->bitrate = 96;
                break;          // (LAME=64)
            case 16000:
                glopts->bitrate = 64;
                break;          // (LAME=48)
            }
        }
        if (glopts->verbosity >= 3) {
            fprintf(stderr, "Chosen bitrate of %dkbps for samplerate of %d Hz.\n",
                    glopts->bitrate, glopts->samplerate_out);
        }
        glopts->freeformat = FALSE;   // no sense in requiring freeformat encoding without setting a bitrate
    }
    if (glopts->bitrate < 0 && glopts->vbr) {
        /* set the minimum bitrate - 'init_bit_allocation' will fix it if needed */
        glopts->bitrate = twolame_index_bitrate((int)glopts->version, 1);
    }

    /* Check for bitrate validity */
    if (glopts->version == TWOLAME_MPEG1
            &&
            !glopts->freeformat
            &&
            !glopts->vbr) {
        /* some limitation apply for MPEG1 when CBR and freeformat is not selected */
        if (glopts->mode == TWOLAME_MONO) {
            if (glopts->bitrate > 192) {
                fprintf(stderr, "twolame_init_params(): %dkbps is an invalid bitrate for mono encoding.\n",
                        glopts->bitrate);
                return -1;
            }
        }
        else {
            if (glopts->bitrate < 64 || glopts->bitrate == 80) {
                fprintf(stderr, "twolame_init_params(): %dkbps is an invalid bitrate for 2ch encoding.\n",
                        glopts->bitrate);
                return -1;
            }
        }
    }

    /* Can't do DAB and energylevel extensions at the same time Because both of them think they're
       the only ones inserting information into the ancillary section of the frame */
    if (glopts->do_dab && glopts->do_energy_levels) {
        fprintf(stderr, "Error: Can't do DAB and Energy Levels at the same time\n");
        return -1;
    }

    /* Set the number of ancillary bits automatically, if none set */
    if (glopts->num_ancillary_bits < 0) {
        if (glopts->do_energy_levels) {
            glopts->num_ancillary_bits = twolame_get_required_energy_bits(glopts);
        } else {
            glopts->num_ancillary_bits = 0;
        }
    }

    /* Check that if we're doing energy levels, that there's enough space to put the information */
    if (glopts->do_energy_levels) {
        int required = twolame_get_required_energy_bits(glopts);
        if (glopts->num_ancillary_bits < required) {
            fprintf(stderr, "Warning: Too few ancillary bits to store energy levels: %i<%i\n",
                    glopts->num_ancillary_bits, required);
            return -1;
        }
    }

    /*
     * MFC Feb 2003: in VBR mode, joint
     * stereo doesn't make any sense at
     * the moment, as there are no noisy
     * subbands according to
     * bits_for_nonoise in vbr mode
     */
    if (glopts->vbr && glopts->mode == TWOLAME_JOINT_STEREO) {
        fprintf(stderr, "Warning: Can't do Joint Stereo with VBR, switching to normal stereo.\n");

        // force stereo mode
        twolame_set_mode(glopts, TWOLAME_STEREO);
    }

    /* Can't do padding and VBR at same time */
    if (glopts->vbr && glopts->padding == TRUE) {
        fprintf(stderr, "Error: Can't do padding and VBR at same time\n");
        return -1;
    }

    /* Simple patch for the `bit_stream buffer needs to be bigger' warning */
    /* Fix FREEFORMAT_MAX_BITRATE definition when github issue #51 will be closed */
    if (glopts->freeformat && glopts->bitrate > FREEFORMAT_MAX_BITRATE) {
        fprintf(stderr, "twolame_init_params(): cannot encode freeformat stream at %d kbps\n", glopts->bitrate);
        return -1;
    }

    // Set the Number of output channels
    glopts->num_channels_out = (glopts->mode == TWOLAME_MONO) ? 1 : 2;



    // build mpeg header from parameters
    if (init_header_info(glopts) < 0) {
        return -1;
    }
    // initialise bitrate allocation
    if (twolame_init_bit_allocation(glopts) < 0) {
        return -1;
    }
    // Select table number and sblimit
    if (twolame_encode_init(glopts) < 0) {
        return -1;
    }
    // Check input samplerate is same as output samplerate
    if (glopts->samplerate_out != glopts->samplerate_in) {
        fprintf(stderr,
                "twolame_init_params(): sorry, twolame doesn't support resampling (yet).\n");
        return -1;
    }

    // Initialise interal variables
    glopts->samples_in_buffer = 0;
    glopts->psycount = 0;


    // Allocate memory to larger buffers
    glopts->subband = (subband_t *) TWOLAME_MALLOC(sizeof(subband_t));
    glopts->j_sample = (jsb_sample_t *) TWOLAME_MALLOC(sizeof(jsb_sample_t));
    glopts->sb_sample = (sb_sample_t *) TWOLAME_MALLOC(sizeof(sb_sample_t));
    if (glopts->subband == NULL
            ||
            glopts->j_sample == NULL
            ||
            glopts->sb_sample == NULL)
    {
        TWOLAME_FREE(glopts->subband);
        TWOLAME_FREE(glopts->j_sample);
        TWOLAME_FREE(glopts->sb_sample);
        return -1;
    }

    // clear buffers
    memset((char *) glopts->buffer, 0, sizeof(glopts->buffer));
    memset((char *) glopts->bit_alloc, 0, sizeof(glopts->bit_alloc));
    memset((char *) glopts->scfsi, 0, sizeof(glopts->scfsi));
    memset((char *) glopts->scalar, 0, sizeof(glopts->scalar));
    memset((char *) glopts->j_scale, 0, sizeof(glopts->j_scale));
    memset((char *) glopts->smrdef, 0, sizeof(glopts->smrdef));
    memset((char *) glopts->smr, 0, sizeof(glopts->smr));
    memset((char *) glopts->max_sc, 0, sizeof(glopts->max_sc));

    // Initialise subband windowfilter
    if (twolame_init_subband(&glopts->smem) < 0) {
        return -1;
    }
    // All initalised now :)
    glopts->twolame_init++;

    return (0);
}
Ejemplo n.º 5
0
void psycho_1(twolame_options * glopts, short buffer[2][1152], FLOAT scale[2][SBLIMIT],
              FLOAT ltmin[2][SBLIMIT])
{
    psycho_1_mem *mem;
    frame_header *header = &glopts->header;
    int nch = glopts->num_channels_out;
    int sblimit = glopts->sblimit;
    int k, i, tone = 0, noise = 0;
    FLOAT sample[FFT_SIZE];
    FLOAT spike[2][SBLIMIT];
    FLOAT *fft_buf[2];
    FLOAT energy[FFT_SIZE];

    /* call functions for critical boundaries, freq. */
    if (!glopts->p1mem) {       /* bands, bark values, and mapping */
        mem = (psycho_1_mem *) TWOLAME_MALLOC(sizeof(psycho_1_mem));

        mem->power = (mask_ptr) TWOLAME_MALLOC(sizeof(mask) * HAN_SIZE);
        if (header->version == TWOLAME_MPEG1) {
            mem->cbound =
                psycho_1_read_cbound(header->lay, header->samplerate_idx, &mem->crit_band);
            psycho_1_read_freq_band(&mem->ltg, header->lay, header->samplerate_idx, &mem->sub_size);
        } else {
            mem->cbound =
                psycho_1_read_cbound(header->lay, header->samplerate_idx + 4, &mem->crit_band);
            psycho_1_read_freq_band(&mem->ltg, header->lay, header->samplerate_idx + 4,
                                    &mem->sub_size);
        }
        psycho_1_make_map(mem->sub_size, mem->power, mem->ltg);
        for (i = 0; i < 1408; i++)
            mem->fft_buf[0][i] = mem->fft_buf[1][i] = 0;

        psycho_1_init_add_db(mem);  /* create the add_db table */

        mem->off[0] = 256;
        mem->off[1] = 256;

        glopts->p1mem = mem;
    }
    {
        mem = glopts->p1mem;

        fft_buf[0] = mem->fft_buf[0];
        fft_buf[1] = mem->fft_buf[1];
    }


    for (k = 0; k < nch; k++) {
        /* check pcm input for 3 blocks of 384 samples */
        /* sami's speedup, added in 02j saves about 4% overall during an encode */
        int ok = mem->off[k] % 1408;
        for (i = 0; i < 1152; i++) {
            fft_buf[k][ok++] = (FLOAT) buffer[k][i] / SCALE;
            if (ok >= 1408)
                ok = 0;
        }
        ok = (mem->off[k] + 1216) % 1408;
        for (i = 0; i < FFT_SIZE; i++) {
            sample[i] = fft_buf[k][ok++];
            if (ok >= 1408)
                ok = 0;
        }
        mem->off[k] += 1152;
        mem->off[k] %= 1408;

        psycho_1_hann_fft_pickmax(sample, mem->power, &spike[k][0], energy);
        psycho_1_tonal_label(mem, &tone);
        psycho_1_noise_label(mem, &noise, energy);
        // psycho_1_dump(power, &tone, &noise) ;
        psycho_1_subsampling(mem->power, mem->ltg, &tone, &noise);
        psycho_1_threshold(mem, &tone, &noise, glopts->bitrate / nch);
        psycho_1_minimum_mask(mem->sub_size, mem->ltg, &ltmin[k][0], sblimit);
        psycho_1_smr(&ltmin[k][0], &spike[k][0], &scale[k][0], sblimit);
    }

}
Ejemplo n.º 6
0
static psycho_3_mem *psycho_3_init(twolame_options * glopts)
{
    int i;
    int cbase = 0;              /* current base index for the bark range calculation */
    FLOAT sfreq;
    psycho_3_mem *mem;
    int numlines[HBLKSIZE];
    FLOAT cbval[HBLKSIZE];
    int partition[HBLKSIZE];
    int *freq_subset;
    FLOAT *bark, *ath;
    int cbands = 0;
    int *cbandindex;

    mem = (psycho_3_mem *) TWOLAME_MALLOC(sizeof(psycho_3_mem));
    mem->off[0] = mem->off[1] = 256;
    freq_subset = mem->freq_subset;
    bark = mem->bark;
    ath = mem->ath;
    cbandindex = mem->cbandindex;

    /* Initialise the tables for the adding dB */
    psycho_3_init_add_db(mem);

    /* For each spectral line calculate the bark and the ATH (in dB) */
    sfreq = (FLOAT) glopts->samplerate_out;
    for (i = 1; i < HBLKSIZE; i++) {
        FLOAT freq = i * sfreq / BLKSIZE;
        bark[i] = ath_freq2bark(freq);
        ath[i] = ath_db(freq, glopts->athlevel);
    }

    {                           /* Work out the critical bands Starting from line 0, all lines
                                   within 1 bark of the starting bark are added to the same
                                   critical band. When a line is greater by 1.0 of a bark, start a
                                   new critical band.  */

        cbandindex[0] = 1;
        for (i = 1; i < HBLKSIZE; i++) {
            if ((bark[i] - bark[cbase]) > 1.0) {    /* 1 critical band? 1 bark? */
                /* this frequency line is too different from the starting line, (in terms of the
                   bark distance) so make this spectral line the first member of the next critical
                   band */
                cbase = i;      /* Start the new critical band from this frequency line */
                cbands++;
                cbandindex[cbands] = cbase;
            }
            /* partition[i] tells us which critical band the i'th frequency line is in */
            partition[i] = cbands;
            /* keep a count of how many frequency lines are in each partition */
            numlines[cbands]++;
        }

        cbands++;
        cbandindex[cbands] = 513;   /* Set the top of the last critical band */
        mem->cbands = cbands;   // make a not of the number of cbands

        /* For each crtical band calculate the average bark value cbval [central bark value] */
        for (i = 1; i < HBLKSIZE; i++)
            cbval[partition[i]] += bark[i]; /* sum up all the bark values */
        for (i = 1; i < CBANDS; i++) {
            if (numlines[i] != 0)
                cbval[i] /= numlines[i];    /* divide by the number of values */
            else {
                cbval[i] = 0;   /* this isn't a partition */
            }
        }
    }

    {
        /* For Step6 - For the calculation of individual masking thresholds the spectral lines are
           subsampled i.e. no need to work out the masking for every single spectral line.
           Depending upon which subband the calculation is for, you can skip a number of lines
           There are 16 lines per subband -> 32 * 16 = 512 Subband 0-2 : Every line (3 * 16 = 48
           lines) Subband 3-5 : Every Second line (3 * 16/2 = 24 lines) Subband 6-11 : Every 4th
           line (6 * 16/4 = 24 lines) Subband 12-31 : Every 12th line (20 * 16/8 = 40 lines)

           create this subset of frequencies (freq_subset) */
        int freq_index = 0;
        for (i = 1; i < (3 * 16) + 1; i++)
            freq_subset[freq_index++] = i;
        for (; i < (6 * 16) + 1; i += 2)
            freq_subset[freq_index++] = i;
        for (; i < (12 * 16) + 1; i += 4)
            freq_subset[freq_index++] = i;
        for (; i < (32 * 16) + 1; i += 8)
            freq_subset[freq_index++] = i;
    }

    if (glopts->verbosity > 4) {
        fprintf(stderr, "%i critical bands\n", cbands);
        for (i = 0; i < cbands; i++)
            fprintf(stderr, "cband %i spectral line index %i\n", i, cbandindex[i]);
        fprintf(stderr, "%i Subsampled spectral lines\n", SUBSIZE);
        for (i = 0; i < SUBSIZE; i++)
            fprintf(stderr, "%i Spectral line %i Bark %.2f\n", i, freq_subset[i],
                    bark[freq_subset[i]]);
    }

    return (mem);
}