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);
}
Exemple #2
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) {
        fprintf(stderr,
                "twolame_init_params(): must specify number of input channels 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(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) {
        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, "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 = 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 = 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;
    }
    // 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;
    }
    // Select table number and sblimit
    if (encode_init(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;


    // 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));

    // 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);
}