Ejemplo n.º 1
0
//  Returns the index associated with a bitrate for
//  the specified version of MPEG.
//
//  Returns -1 for invalid bitrates
//
int twolame_get_bitrate_index(int bitrate, TWOLAME_MPEG_version version)
{
	int index = 0;
	int found = 0;

	// MFC sanity check.
	if (version!=0 && version!=1) {
		fprintf(stderr,"twolame_get_bitrate_index: invalid version index %i\n",version);
		return -1;
	}

	while (!found && index < 15) {
		if (bitrate_table[version][index] == bitrate)
			found = 1;
		else
			++index;
	}
	
	if (found)  return (index);
	else {
		fprintf (stderr,
			"twolame_get_bitrate_index: %d is not a legal bitrate for version '%s'\n",
			bitrate, twolame_mpeg_version_name(version));
		return -1;
	}
}
Ejemplo n.º 2
0
// Returns 0 if successful
// Returns -1 if unsuccessful
static int init_header_info(twolame_options * glopts)
{
    frame_header *header = &glopts->header;

    /* use the options to create header info structure */
    header->lay = 2;
    header->error_protection = glopts->error_protection;
    header->version = glopts->version;

    // Convert the sampling frequency to the required index
    header->samplerate_idx = twolame_get_samplerate_index(glopts->samplerate_out);
    if (header->samplerate_idx < 0) {
        fprintf(stderr, "Not a valid samplerate: %i\n", glopts->samplerate_out);
        return -1;
    }

    // Convert the bitrate to the an index
    if (glopts->freeformat)
        header->bitrate_index = 0;
    else
    {
        header->bitrate_index = twolame_get_bitrate_index(glopts->bitrate, header->version);
        if (header->bitrate_index < 0) {
            fprintf(stderr, "Not a valid bitrate (%i) for MPEG version '%s'\n", glopts->bitrate,
                    twolame_mpeg_version_name(glopts->version));
            return -1;
        }
    }
    if (glopts->vbr && glopts->vbr_max_bitrate > 0)
    {
        /* user required vbr and a specified max bitrate */
        /* convert max VBR bitrate to an index */
        glopts->vbr_upper_index = twolame_get_bitrate_index(glopts->vbr_max_bitrate, header->version);
        if (glopts->vbr_upper_index < 0) {
            fprintf(stderr, "Not a valid max VBR bitrate for this version: %i\n",
                    glopts->vbr_max_bitrate);
            return -1;
        }
    }
    // Copy accross the other settings
    header->padding = 0; /* when requested, padding will be evaluated later for this frame */
    header->private_extension = glopts->private_extension;
    header->mode = glopts->mode;
    header->mode_ext = 0;
    header->copyright = glopts->copyright;
    header->original = glopts->original;
    header->emphasis = glopts->emphasis;

    return 0;
}
Ejemplo n.º 3
0
// Returns 0 if successful
// Returns -1 if unsuccessful
static int init_header_info( twolame_options *glopts ) {
	frame_header *header = &glopts->header;
	
	/* use the options to create header info structure */
	header->lay = 2;
	header->error_protection = glopts->error_protection;
	header->version = glopts->version;
	
	// Convert the sampling frequency to the required index
	header->samplerate_idx = twolame_get_samplerate_index( glopts->samplerate_out ); 
	if (header->samplerate_idx < 0) {
		fprintf(stdout,"Not a valid samplerate: %i\n",glopts->samplerate_out );
		return -1;
	}
	
	// Convert the bitrate to the an index	
	header->bitrate_index = twolame_get_bitrate_index(glopts->bitrate, header->version);
	if (header->bitrate_index < 0) {
		fprintf(stdout,"Not a valid bitrate (%i) for MPEG version '%s'\n", glopts->bitrate, 
			twolame_mpeg_version_name(glopts->version));
		return -1;
	}
		
	// Convert the max VBR bitrate to the an index	
	glopts->vbr_upper_index = twolame_get_bitrate_index(glopts->vbr_max_bitrate, header->version);
	if (glopts->vbr_upper_index < 0) {
		fprintf(stdout,"Not a valid max VBR bitrate for this version: %i\n",glopts->vbr_max_bitrate);
		return -1;
	}
	
	// Copy accross the other settings	
	header->padding = glopts->padding;
	header->private_bit = glopts->private_bit;
	header->mode = glopts->mode;
	header->mode_ext = 0;
	header->copyright = glopts->copyright;
	header->original = glopts->original;
	header->emphasis = glopts->emphasis;
	
	return 0;
}
Ejemplo n.º 4
0
//  Returns the index associated with a bitrate for
//  the specified version of MPEG.
//
//  Returns -1 for invalid bitrates
//
int twolame_get_bitrate_index(int bitrate, TWOLAME_MPEG_version version)
{
    int index = 0;

    // MFC sanity check.
    if (version != 0 && version != 1) {
        fprintf(stderr, "twolame_get_bitrate_index: invalid version index %i\n", version);
        return -1;
    }

    while (++index < 15)
        if (bitrate_table[version][index] == bitrate)
            break;

    if (index == 15) {
        fprintf(stderr,
                "twolame_get_bitrate_index: %d is not a legal bitrate for version '%s'\n",
                bitrate, twolame_mpeg_version_name(version));
        return -1;
    }
    return (index);
}
Ejemplo n.º 5
0
const char *twolame_get_version_name(twolame_options * glopts)
{
    return twolame_mpeg_version_name(glopts->version);
}
Ejemplo n.º 6
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.º 7
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 != 1 && glopts->num_channels != 2) {
		fprintf(stderr,"twolame_init_params(): must specify number of channels in input samples using twolame_set_num_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(stdout,"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 == 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);
		}
	}

	// Choose the bitrate (if none chosen)
	if (glopts->bitrate <= 0) {
		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);
		}
	}
	
	

	/* 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,"Can't do DAB and Energy Levels at the same time\n");
		return -1;
	}

	
	/* Check that if we're doing energy levels, that there's enough space 
	   to put the information */
	if (glopts->do_energy_levels) {
		if ((glopts->mode==TWOLAME_MONO)&&(glopts->num_ancillary_bits<16)) {
			fprintf(stderr,"Too few ancillary bits: %i<16\n",glopts->num_ancillary_bits);
			glopts->num_ancillary_bits = 16;
		}    
		if ((glopts->mode!=TWOLAME_MONO)&&(glopts->num_ancillary_bits<40)) {
			fprintf(stderr,"Too few ancillary bits: %i<40\n",glopts->num_ancillary_bits);
		glopts->num_ancillary_bits = 40;
		}
	}

	/*
	 * 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) {
		// force stereo mode
		twolame_set_mode(glopts, TWOLAME_STEREO);
	}

	/* don't use padding for VBR */
	if (glopts->vbr) {
		twolame_set_padding(glopts, FALSE);
	}
	
	//MFC FIX:	Need to cross validate the number of ancillary_bits
	// with the energylevel setting.
	//
	// energylevel:
	// MFC FIX:		This option must be mutually exclusive with the
	// reservebits(-R) option *UNLESS * the number
	// of explicitly reserved bits > 5 bytes.


	// If energy information is required, see if we're in MONO mode in
	// which case, we only need 16 bits of ancillary data
	if (glopts->do_energy_levels) {
		if (glopts->mode==TWOLAME_MONO) {
			// only 2 bytes needed for energy level for mono channel
			twolame_set_num_ancillary_bits(glopts, 16);
		} else {
			// 5 bytes for the stereo energy info
			twolame_set_num_ancillary_bits(glopts, 40);
		}
	
	}
	


	// build mpeg header from parameters
	if (init_header_info( glopts )<0) {
		return -1;
	}
	
	// load the alloc tables and do some other stuff
	if (init_frame_info( glopts )<0) {
		return -1;
	}
	
	// initialise bitrate allocation
	if (init_bit_allocation( 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;
	glopts->crc = 0;

  
  	// Allocate memory to larger buffers 
    glopts->subband = (subband_t *) twolame_malloc (sizeof (subband_t), "subband");
	glopts->j_sample = (jsb_sample_t *) twolame_malloc (sizeof (jsb_sample_t), "j_sample");
    glopts->sb_sample = (sb_sample_t *) twolame_malloc (sizeof (sb_sample_t), "sb_sample");
	
	// 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 (init_subband(&glopts->smem)<0) {
		return -1;
	}

	// All initalised now :)
	glopts->twolame_init++;

	return(0);
}