// Print the library version and // encoder parameter settings to STDERR void twolame_print_config(twolame_options *glopts) { FILE* fd = stderr; // Are we being silent ? if (glopts->verbosity<=0) return; // Are we being brief ? if (glopts->verbosity==1) { fprintf(fd, "LibTwoLame version %s (%s)\n", get_twolame_version(), get_twolame_url()); fprintf (fd, "Encoding as %dHz, ", twolame_get_out_samplerate(glopts)); fprintf (fd, "%d kbps, ", twolame_get_bitrate(glopts) ); if (twolame_get_VBR(glopts)) fprintf (fd, "VBR, " ); else fprintf (fd, "CBR, " ); fprintf (fd, "%s Layer II\n", twolame_get_version_name(glopts)); } else { fprintf (fd, "---------------------------------------------------------\n"); fprintf (fd, "LibTwoLame %s (%s)\n", get_twolame_version(), get_twolame_url()); fprintf (fd, "Input : %d Hz, %d channels\n", twolame_get_in_samplerate(glopts), twolame_get_num_channels(glopts)); fprintf (fd, "Output: %d Hz, %s\n", twolame_get_out_samplerate(glopts), twolame_get_mode_name(glopts)); fprintf (fd, "%d kbps ", twolame_get_bitrate(glopts) ); if (twolame_get_VBR(glopts)) fprintf (fd, "VBR " ); else fprintf (fd, "CBR " ); fprintf (fd, "%s Layer II ", twolame_get_version_name(glopts)); fprintf (fd, "psycho model=%d \n", twolame_get_psymodel(glopts)); fprintf (fd, "[De-emph:%s Copyright:%s Original:%s]\n", ((twolame_get_emphasis(glopts)) ? "On " : "Off"), ((twolame_get_copyright(glopts)) ? "Yes" : "No "), ((twolame_get_original(glopts)) ? "Yes" : "No ")); fprintf (fd, "[Padding:%s CRC:%s DAB:%s ]\n", ((twolame_get_padding(glopts)) ? "Normal" : "Off "), ((twolame_get_error_protection(glopts)) ? "On " : "Off"), ((twolame_get_DAB(glopts)) ? "On " : "Off")); if (glopts->verbosity>=3) { if (twolame_get_VBR(glopts)) { fprintf (fd, " - VBR Enabled. Using MNR boost of %f\n", twolame_get_VBR_level(glopts)); fprintf (fd, " - VBR bitrate index limits [%i -> %i]\n", glopts->lower_index, glopts->upper_index); } fprintf(fd," - ATH adjustment %f\n", twolame_get_ATH_level(glopts)); fprintf(fd," - Reserving %i Ancillary bits\n", twolame_get_num_ancillary_bits(glopts)); if (twolame_get_scale(glopts)!=1.0f) fprintf(fd," - Scaling audio by %f\n", twolame_get_scale(glopts)); if (twolame_get_scale_left(glopts)!=1.0f) fprintf(fd," - Scaling left channel by %f\n", twolame_get_scale_left(glopts)); if (twolame_get_scale_right(glopts)!=1.0f) fprintf(fd," - Scaling right channel by %f\n", twolame_get_scale_right(glopts)); //if (glopts->num_channels_in == 2 && glopts->num_channels_out == 1 ) { // fprintf(fd, " - Downmixing from stereo to mono.\n"); //} else if (glopts->num_channels_in == 1 && glopts->num_channels_out == 2 ) { // fprintf(fd, " - Upmixing from mono to stereo.\n"); //} } fprintf (fd, "---------------------------------------------------------\n"); } }
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); }
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); }