/** \fn initialize */ bool AUDMEncoder_Twolame::initialize(void) { int ret; TWOLAME_MPEG_mode mmode; uint32_t frequence; int channels=wavheader.channels; _twolameOptions = twolame_init(); if (_twolameOptions == NULL) return 0; if(channels>2) { printf("[TwoLame]Too many channels\n"); return 0; } wavheader.byterate=(_config.bitrate*1000)>>3; _chunk = 1152*channels; printf("[TwoLame]Incoming :fq : %"PRIu32", channel : %"PRIu32" bitrate: %"PRIu32" \n", wavheader.frequency,channels,_config.bitrate); twolame_set_in_samplerate(OPTIONS, wavheader.frequency); twolame_set_out_samplerate (OPTIONS, wavheader.frequency); twolame_set_num_channels(OPTIONS, channels); if(channels==1) mmode=TWOLAME_MONO; else mmode = TWOLAME_STEREO; twolame_set_mode(OPTIONS,mmode); twolame_set_error_protection(OPTIONS,TRUE); //toolame_setPadding (options,TRUE); twolame_set_bitrate (OPTIONS,_config.bitrate); twolame_set_verbosity(OPTIONS, 2); if(twolame_init_params(OPTIONS)) { printf("[TwoLame]Twolame init failed\n"); return false; } printf("[TwoLame]Libtoolame successfully initialized\n"); return true; }
int MastCodec_MPA::set_param_internal( const char* name, const char* value ) { /* Parameters listed in RFC3555 for audio/MPA: layer: 1,2,3 samplerate: mode: "stereo", "joint_stereo", "single_channel", "dual_channel" bitrate: the data rate for the audio bit stream ptime: duration of each packet in milliseconds maxptime: maximum duration of each packet in milliseconds Example: audio/MPA;layer=2;mode=stereo;bitrate=160; */ if (strcmp(name, "layer")==0) { int layer = atoi(value); if (layer!=2) return -2; } else if (strcmp(name, "mode")==0) { TWOLAME_MPEG_mode mode; if (strcmp(value, "stereo")==0) mode = TWOLAME_STEREO; else if (strcmp(value, "joint_stereo")==0) mode = TWOLAME_JOINT_STEREO; else if (strcmp(value, "single_channel")==0) mode = TWOLAME_MONO; else if (strcmp(value, "dual_channel")==0) mode = TWOLAME_DUAL_CHANNEL; else return -2; if (twolame_set_mode( this->twolame, mode )) { MAST_WARNING("Failed to set mode"); } } else if (strcmp(name, "bitrate")==0) { int bitrate = atoi(value); if (twolame_set_bitrate( this->twolame, bitrate )) { MAST_WARNING("Failed to set bitrate"); } else { MAST_DEBUG("Set bitrate to %d", bitrate); } } else { // Unsupported parameter return -1; } // Success return 0; }
/* set up the encoder state */ static gboolean gst_two_lame_setup (GstTwoLame * twolame) { #define CHECK_ERROR(command) G_STMT_START {\ if ((command) < 0) { \ GST_ERROR_OBJECT (twolame, "setup failed: " G_STRINGIFY (command)); \ return FALSE; \ } \ }G_STMT_END int retval; GstCaps *allowed_caps; GST_DEBUG_OBJECT (twolame, "starting setup"); /* check if we're already setup; if we are, we might want to check * if this initialization is compatible with the previous one */ /* FIXME: do this */ if (twolame->setup) { GST_WARNING_OBJECT (twolame, "already setup"); twolame->setup = FALSE; } twolame->glopts = twolame_init (); if (twolame->glopts == NULL) return FALSE; /* copy the parameters over */ twolame_set_in_samplerate (twolame->glopts, twolame->samplerate); /* let twolame choose default samplerate unless outgoing sample rate is fixed */ allowed_caps = gst_pad_get_allowed_caps (twolame->srcpad); if (allowed_caps != NULL) { GstStructure *structure; gint samplerate; structure = gst_caps_get_structure (allowed_caps, 0); if (gst_structure_get_int (structure, "rate", &samplerate)) { GST_DEBUG_OBJECT (twolame, "Setting sample rate to %d as fixed in src caps", samplerate); twolame_set_out_samplerate (twolame->glopts, samplerate); } else { GST_DEBUG_OBJECT (twolame, "Letting twolame choose sample rate"); twolame_set_out_samplerate (twolame->glopts, 0); } gst_caps_unref (allowed_caps); allowed_caps = NULL; } else { GST_DEBUG_OBJECT (twolame, "No peer yet, letting twolame choose sample rate"); twolame_set_out_samplerate (twolame->glopts, 0); } /* force mono encoding if we only have one channel */ if (twolame->num_channels == 1) twolame->mode = 3; /* Fix bitrates and MPEG version */ CHECK_ERROR (twolame_set_num_channels (twolame->glopts, twolame->num_channels)); CHECK_ERROR (twolame_set_mode (twolame->glopts, twolame->mode)); CHECK_ERROR (twolame_set_psymodel (twolame->glopts, twolame->psymodel)); CHECK_AND_FIXUP_BITRATE (twolame, "bitrate", twolame->bitrate); CHECK_ERROR (twolame_set_bitrate (twolame->glopts, twolame->bitrate)); CHECK_ERROR (twolame_set_padding (twolame->glopts, twolame->padding)); CHECK_ERROR (twolame_set_energy_levels (twolame->glopts, twolame->energy_level_extension)); CHECK_ERROR (twolame_set_emphasis (twolame->glopts, twolame->emphasis)); CHECK_ERROR (twolame_set_error_protection (twolame->glopts, twolame->error_protection)); CHECK_ERROR (twolame_set_copyright (twolame->glopts, twolame->copyright)); CHECK_ERROR (twolame_set_original (twolame->glopts, twolame->original)); CHECK_ERROR (twolame_set_VBR (twolame->glopts, twolame->vbr)); CHECK_ERROR (twolame_set_VBR_level (twolame->glopts, twolame->vbr_level)); CHECK_ERROR (twolame_set_ATH_level (twolame->glopts, twolame->ath_level)); CHECK_AND_FIXUP_BITRATE (twolame, "vbr-max-bitrate", twolame->vbr_max_bitrate); CHECK_ERROR (twolame_set_VBR_max_bitrate_kbps (twolame->glopts, twolame->vbr_max_bitrate)); CHECK_ERROR (twolame_set_quick_mode (twolame->glopts, twolame->quick_mode)); CHECK_ERROR (twolame_set_quick_count (twolame->glopts, twolame->quick_mode_count)); /* initialize the twolame encoder */ if ((retval = twolame_init_params (twolame->glopts)) >= 0) { twolame->setup = TRUE; /* FIXME: it would be nice to print out the mode here */ GST_INFO ("twolame encoder setup (%d kbit/s, %d Hz, %d channels)", twolame->bitrate, twolame->samplerate, twolame->num_channels); } else { GST_ERROR_OBJECT (twolame, "twolame_init_params returned %d", retval); } GST_DEBUG_OBJECT (twolame, "done with setup"); return twolame->setup; #undef CHECK_ERROR }
/* parse_args() Parse the command line arguments */ void parse_args(int argc, char **argv, twolame_options * encopts ) { int ch=0; // process args struct option longopts[] = { // Input { "raw-input", no_argument, NULL, 'r' }, { "byte-swap", no_argument, NULL, 'x' }, { "samplerate", required_argument, NULL, 's' }, { "channels", required_argument, NULL, 'N' }, { "swap-channels", no_argument, NULL, 'g' }, { "scale", required_argument, NULL, 1000 }, { "scale-l", required_argument, NULL, 1001 }, { "scale-r", required_argument, NULL, 1002 }, // Output { "mode", required_argument, NULL, 'm' }, { "downmix", no_argument, NULL, 'a' }, { "bitrate", required_argument, NULL, 'b' }, { "psyc-mode", required_argument, NULL, 'P' }, { "vbr", no_argument, NULL, 'v' }, { "vbr-level", required_argument, NULL, 'V' }, { "max-bitrate", required_argument, NULL, 'B' }, { "ath", required_argument, NULL, 'l' }, { "quick", required_argument, NULL, 'q' }, { "single-frame", no_argument, NULL, 'S' }, // Misc { "copyright", no_argument, NULL, 'c' }, { "non-original", no_argument, NULL, 'o' }, { "original", no_argument, NULL, 1003 }, { "protect", no_argument, NULL, 'p' }, { "padding", no_argument, NULL, 'd' }, { "reserve-bits", required_argument, NULL, 'R' }, { "deemphasis", required_argument, NULL, 'e' }, { "energy", no_argument, NULL, 'E' }, // Verbosity { "talkativity", required_argument, NULL, 't' }, { "quiet", no_argument, NULL, 1004 }, { "brief", no_argument, NULL, 1005 }, { "verbose", no_argument, NULL, 1006 }, { "help", no_argument, NULL, 'h' }, { NULL, 0, NULL, 0 } }; // Create a short options structure from the long one char* shortopts = build_shortopt_string( longopts ); //printf("shortopts: %s\n", shortopts); while( (ch = getopt_long( argc, argv, shortopts, longopts, NULL )) != -1) { switch(ch) { // Input case 'r': sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16; break; case 'x': byteswap = TRUE; break; case 's': twolame_set_out_samplerate(encopts, atoi(optarg)); sfinfo.samplerate = atoi(optarg); break; case 'N': sfinfo.channels = atoi(optarg); break; case 'g': channelswap = TRUE; break; case 1000: // --scale twolame_set_scale( encopts, atof(optarg) ); break; case 1001: // --scale-l twolame_set_scale_left( encopts, atof(optarg) ); break; case 1002: // --scale-r twolame_set_scale_right( encopts, atof(optarg) ); break; // Output case 'm': if (*optarg == 's') { twolame_set_mode(encopts, TWOLAME_STEREO); } else if (*optarg == 'd') { twolame_set_mode(encopts, TWOLAME_DUAL_CHANNEL); } else if (*optarg == 'j') { twolame_set_mode(encopts, TWOLAME_JOINT_STEREO); } else if (*optarg == 'm') { twolame_set_mode(encopts, TWOLAME_MONO); } else if (*optarg == 'a') { twolame_set_mode(encopts, TWOLAME_AUTO_MODE); } else { fprintf(stderr, "Error: mode must be a/s/d/j/m not '%s'\n\n", optarg); usage_long(); } break; case 'a': // downmix twolame_set_mode(encopts, TWOLAME_MONO); break; case 'b': twolame_set_bitrate(encopts, atoi(optarg)); break; case 'P': twolame_set_psymodel(encopts, atoi(optarg)); break; case 'v': twolame_set_VBR(encopts, TRUE); break; case 'V': twolame_set_VBR(encopts, TRUE); twolame_set_VBR_level(encopts, atof(optarg)); break; case 'B': twolame_set_VBR_max_bitrate_kbps(encopts, atoi(optarg)); break; case 'l': twolame_set_ATH_level(encopts, atof(optarg)); break; case 'q': twolame_set_quick_mode(encopts, TRUE); twolame_set_quick_count(encopts, atoi(optarg)); break; case 'S': single_frame_mode = TRUE; break; // Miscellaneous case 'c': twolame_set_copyright(encopts, TRUE); break; case 'o': // --non-original twolame_set_original(encopts, FALSE); break; case 1003: // --original twolame_set_original(encopts, TRUE); break; case 'p': twolame_set_error_protection(encopts, TRUE); break; case 'd': twolame_set_padding(encopts, TWOLAME_PAD_ALL); break; case 'R': twolame_set_num_ancillary_bits(encopts, atoi(optarg)); break; case 'e': if (*optarg == 'n') twolame_set_emphasis(encopts, TWOLAME_EMPHASIS_N); else if (*optarg == '5') twolame_set_emphasis(encopts, TWOLAME_EMPHASIS_5); else if (*optarg == 'c') twolame_set_emphasis(encopts, TWOLAME_EMPHASIS_C); else { fprintf(stderr, "Error: emphasis must be n/5/c not '%s'\n\n", optarg); usage_long(); } break; case 'E': twolame_set_energy_levels(encopts, TRUE); break; // Verbosity case 't': twolame_set_verbosity(encopts, atoi(optarg)); break; case 1004: // --quiet twolame_set_verbosity(encopts, 0); break; case 1005: // --brief twolame_set_verbosity(encopts, 1); break; case 1006: // --verbose twolame_set_verbosity(encopts, 4); break; case 'h': usage_long(); break; default: usage_short(); break; } } // Look for the input and output file names argc -= optind; argv += optind; while( argc ) { if (inputfilename[0] == '\0') strncpy(inputfilename, *argv, MAX_NAME_SIZE); else if (outputfilename[0] == '\0') strncpy(outputfilename, *argv, MAX_NAME_SIZE); else { fprintf(stderr, "excess argument: %s\n", *argv); usage_short(); } argv++; argc--; } // Check that we now have input and output file names ok if ( inputfilename[0] == '\0') { fprintf(stderr, "Missing input filename.\n"); usage_short(); } if ( outputfilename[0] == '\0' && strcmp(inputfilename, "-")!=0 ) { // Create output filename from the inputfilename // and change the suffix new_extension( inputfilename, OUTPUT_SUFFIX, outputfilename ); } if ( outputfilename[0] == '\0') { fprintf(stderr, "Missing output filename.\n"); usage_short(); } }
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); }
static int OpenEncoder( vlc_object_t *p_this ) { encoder_t *p_enc = (encoder_t *)p_this; encoder_sys_t *p_sys; int i_frequency; if( p_enc->fmt_out.i_codec != VLC_CODEC_MP2 && p_enc->fmt_out.i_codec != VLC_CODEC_MPGA && p_enc->fmt_out.i_codec != VLC_FOURCC( 'm', 'p', '2', 'a' ) && !p_enc->obj.force ) { return VLC_EGENERIC; } if( p_enc->fmt_in.audio.i_channels > 2 ) { msg_Err( p_enc, "doesn't support > 2 channels" ); return VLC_EGENERIC; } for ( i_frequency = 0; i_frequency < 6; i_frequency++ ) { if ( p_enc->fmt_out.audio.i_rate == mpa_freq_tab[i_frequency] ) break; } if ( i_frequency == 6 ) { msg_Err( p_enc, "MPEG audio doesn't support frequency=%d", p_enc->fmt_out.audio.i_rate ); return VLC_EGENERIC; } /* Allocate the memory needed to store the decoder's structure */ if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL ) return VLC_ENOMEM; p_enc->p_sys = p_sys; p_enc->fmt_in.i_codec = VLC_CODEC_S16N; p_enc->fmt_out.i_cat = AUDIO_ES; p_enc->fmt_out.i_codec = VLC_CODEC_MPGA; config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg ); p_sys->p_twolame = twolame_init(); /* Set options */ twolame_set_in_samplerate( p_sys->p_twolame, p_enc->fmt_out.audio.i_rate ); twolame_set_out_samplerate( p_sys->p_twolame, p_enc->fmt_out.audio.i_rate ); if( var_GetBool( p_enc, ENC_CFG_PREFIX "vbr" ) ) { float f_quality = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" ); if ( f_quality > 50.f ) f_quality = 50.f; if ( f_quality < 0.f ) f_quality = 0.f; twolame_set_VBR( p_sys->p_twolame, 1 ); twolame_set_VBR_q( p_sys->p_twolame, f_quality ); } else { int i; for ( i = 1; i < 14; i++ ) { if ( p_enc->fmt_out.i_bitrate / 1000 <= mpa_bitrate_tab[i_frequency / 3][i] ) break; } if ( p_enc->fmt_out.i_bitrate / 1000 != mpa_bitrate_tab[i_frequency / 3][i] ) { msg_Warn( p_enc, "MPEG audio doesn't support bitrate=%d, using %d", p_enc->fmt_out.i_bitrate, mpa_bitrate_tab[i_frequency / 3][i] * 1000 ); p_enc->fmt_out.i_bitrate = mpa_bitrate_tab[i_frequency / 3][i] * 1000; } twolame_set_bitrate( p_sys->p_twolame, p_enc->fmt_out.i_bitrate / 1000 ); } if ( p_enc->fmt_in.audio.i_channels == 1 ) { twolame_set_num_channels( p_sys->p_twolame, 1 ); twolame_set_mode( p_sys->p_twolame, TWOLAME_MONO ); } else { twolame_set_num_channels( p_sys->p_twolame, 2 ); switch( var_GetInteger( p_enc, ENC_CFG_PREFIX "mode" ) ) { case 1: twolame_set_mode( p_sys->p_twolame, TWOLAME_DUAL_CHANNEL ); break; case 2: twolame_set_mode( p_sys->p_twolame, TWOLAME_JOINT_STEREO ); break; case 0: default: twolame_set_mode( p_sys->p_twolame, TWOLAME_STEREO ); break; } } twolame_set_psymodel( p_sys->p_twolame, var_GetInteger( p_enc, ENC_CFG_PREFIX "psy" ) ); if ( twolame_init_params( p_sys->p_twolame ) ) { msg_Err( p_enc, "twolame initialization failed" ); return -VLC_EGENERIC; } p_enc->pf_encode_audio = Encode; p_sys->i_nb_samples = 0; return VLC_SUCCESS; }
bool MpegL2Codec::startCodec() { #ifdef HAVE_TWOLAME // // Load Library // if((twolame_handle=dlopen("libtwolame.so.0",RTLD_LAZY))==NULL) { Log(LOG_ERR,"unsupported audio format (library not found)"); return false; } *(void **)(&twolame_init)=dlsym(twolame_handle,"twolame_init"); *(void **)(&twolame_set_mode)=dlsym(twolame_handle,"twolame_set_mode"); *(void **)(&twolame_set_num_channels)= dlsym(twolame_handle,"twolame_set_num_channels"); *(void **)(&twolame_set_in_samplerate)= dlsym(twolame_handle,"twolame_set_in_samplerate"); *(void **)(&twolame_set_out_samplerate)= dlsym(twolame_handle,"twolame_set_out_samplerate"); *(void **)(&twolame_set_bitrate)= dlsym(twolame_handle,"twolame_set_bitrate"); *(void **)(&twolame_init_params)= dlsym(twolame_handle,"twolame_init_params"); *(void **)(&twolame_close)=dlsym(twolame_handle,"twolame_close"); *(void **)(&twolame_encode_buffer_interleaved)= dlsym(twolame_handle,"twolame_encode_buffer_interleaved"); *(void **)(&twolame_encode_buffer_float32_interleaved)= dlsym(twolame_handle,"twolame_encode_buffer_float32_interleaved"); *(void **)(&twolame_encode_flush)= dlsym(twolame_handle,"twolame_encode_flush"); *(void **)(&twolame_set_energy_levels)= dlsym(twolame_handle,"twolame_set_energy_levels"); *(void **)(&twolame_set_VBR)=dlsym(twolame_handle,"twolame_set_VBR"); *(void **)(&twolame_set_VBR_level)= dlsym(twolame_handle,"twolame_set_VBR_level"); // // Initialize Encoder Instance // TWOLAME_MPEG_mode mpeg_mode=TWOLAME_STEREO; switch(channels()) { case 1: mpeg_mode=TWOLAME_MONO; break; case 2: mpeg_mode=TWOLAME_STEREO; break; } if((twolame_lameopts=twolame_init())==NULL) { Log(LOG_ERR,"unable to initialize MP2 encoder"); return false; } twolame_set_mode(twolame_lameopts,mpeg_mode); twolame_set_num_channels(twolame_lameopts,channels()); twolame_set_in_samplerate(twolame_lameopts,streamSamplerate()); twolame_set_out_samplerate(twolame_lameopts,streamSamplerate()); if(bitrate()==0) { twolame_set_VBR(twolame_lameopts,TRUE); twolame_set_VBR_level(twolame_lameopts,(int)(20.0*quality()-10.0)); } else { twolame_set_bitrate(twolame_lameopts,bitrate()); } twolame_set_energy_levels(twolame_lameopts,1); if(twolame_init_params(twolame_lameopts)!=0) { Log(LOG_ERR,"unable to start MP2 encoder"); return false; } return true; #else Log(LOG_ERR,"unsupported audio format (no build support)"); return false; #endif // HAVE_TWOLAME }
//________________________________________________ // Init lame encoder // frequence : Impose frequency , 0 means reuse the incoming fq // mode : ADM_STEREO etc... // bitrate : Bitrate in kbps (96,192...) // return 0 : init failed // 1 : init succeeded //_______________________________________________ uint8_t AUDMEncoder_Twolame::init(ADM_audioEncoderDescriptor *config) { int ret; TWOLAME_MPEG_mode mmode; uint32_t frequence; TWOLAME_encoderParam *lameConf=(TWOLAME_encoderParam *)config->param; ADM_assert(config->paramSize==sizeof(TWOLAME_encoderParam)); _twolameOptions = twolame_init(); if (_twolameOptions == NULL) return 0; if(_wavheader->channels>2) { printf("[TwoLame]Too many channels\n"); return 0; } _wavheader->byterate=(config->bitrate*1000)>>3; _chunk = 1152*_wavheader->channels; printf("[TwoLame]Incoming :fq : %lu, channel : %lu bitrate: %lu \n", _wavheader->frequency,_wavheader->channels,config->bitrate); twolame_set_in_samplerate(OPTIONS, _wavheader->frequency); twolame_set_out_samplerate (OPTIONS, _wavheader->frequency); twolame_set_num_channels(OPTIONS, _wavheader->channels); if(_wavheader->channels==1) mmode=TWOLAME_MONO; else switch (lameConf->mode) { case ADM_STEREO: mmode = TWOLAME_STEREO; break; case ADM_JSTEREO: mmode = TWOLAME_JOINT_STEREO; break; case ADM_MONO: mmode=TWOLAME_MONO; break; default: printf("\n **** unknown mode, going stereo ***\n"); mmode = TWOLAME_STEREO; break; } twolame_set_mode(OPTIONS,mmode); twolame_set_error_protection(OPTIONS,TRUE); //toolame_setPadding (options,TRUE); twolame_set_bitrate (OPTIONS,config->bitrate); twolame_set_verbosity(OPTIONS, 2); if(twolame_init_params(OPTIONS)) { printf("[TwoLame]Twolame init failed\n"); return 0; } printf("[TwoLame]Libtoolame successfully initialized\n"); return 1; }
int main(int argc, char **argv) { twolame_options *encodeOptions; char *inputfilename = argv[1]; char *outputfilename = argv[2]; FILE *outfile, *fpSrc = NULL; short int *pcmaudio; unsigned char *mp2buffer; int num_samples = 0; int mp2fill_size = 0; int frames = 0; wave_info_t *wave_info = NULL; int nReadSize = 0; if (argc != 3) usage(); /* Allocate some space for the PCM audio data */ if ((pcmaudio = (short *) calloc(AUDIOBUFSIZE, sizeof(short))) == NULL) { fprintf(stderr, "pcmaudio alloc failed\n"); exit(99); } /* Allocate some space for the encoded MP2 audio data */ if ((mp2buffer = (unsigned char *) calloc(MP2BUFSIZE, sizeof(unsigned char))) == NULL) { fprintf(stderr, "mp2buffer alloc failed\n"); exit(99); } /* grab a set of default encode options */ encodeOptions = twolame_init(); printf("Using libtwolame version %s.\n", get_twolame_version()); /* Open the wave file */ if ((wave_info = wave_init(inputfilename)) == NULL) { fprintf(stderr, "Not a recognised WAV file.\n"); exit(99); } // Use sound file to over-ride preferences for // mono/stereo and sampling-frequency twolame_set_num_channels(encodeOptions, wave_info->channels); if (wave_info->channels == 1) { twolame_set_mode(encodeOptions, TWOLAME_MONO); } else { twolame_set_mode(encodeOptions, TWOLAME_STEREO); } /* Set the input and output sample rate to the same */ twolame_set_in_samplerate(encodeOptions, wave_info->samplerate); twolame_set_out_samplerate(encodeOptions, wave_info->samplerate); /* Set the bitrate to 192 kbps */ twolame_set_bitrate(encodeOptions, 64); /* initialise twolame with this set of options */ if (twolame_init_params(encodeOptions) != 0) { fprintf(stderr, "Error: configuring libtwolame encoder failed.\n"); exit(99); } /* Open the output file for the encoded MP2 data */ if ((outfile = fopen(outputfilename, "wb")) == 0) { fprintf(stderr, "error opening output file %s\n", outputfilename); exit(99); } // Read num_samples of audio data *per channel* from the input file //while ((num_samples = wave_get_samples(wave_info, pcmaudio, AUDIOBUFSIZE/2)) != 0) { fpSrc = fopen( "a2002011001-e02.wav", "rb" ); while(1) { num_samples = fread( pcmaudio, sizeof( char ), AUDIOBUFSIZE/2, fpSrc ); // Encode the audio!2024*4 if( num_samples != AUDIOBUFSIZE/2 ) break; num_samples /= 2; mp2fill_size = twolame_encode_buffer_interleaved(encodeOptions, pcmaudio, num_samples/2, mp2buffer, MP2BUFSIZE); // Write the MPEG bitstream to the file fwrite(mp2buffer, sizeof(unsigned char), mp2fill_size, outfile); // Display the number of MPEG audio frames we have encoded frames += (num_samples / TWOLAME_SAMPLES_PER_FRAME); printf("[%04i]\r", frames); fflush(stdout); } /* flush any remaining audio. (don't send any new audio data) There should only ever be a max of 1 frame on a flush. There may be zero frames if the audio data was an exact multiple of 1152 */ mp2fill_size = twolame_encode_flush(encodeOptions, mp2buffer, MP2BUFSIZE); fwrite(mp2buffer, sizeof(unsigned char), mp2fill_size, outfile); twolame_close(&encodeOptions); free(pcmaudio); printf("\nFinished nicely.\n"); return (0); }
int mpae_init_twolame(audio_encoder_t *encoder) { int mode; mpae_twolame_ctx *ctx = NULL; if(encoder->params.channels == 1) { mp_msg(MSGT_MENCODER, MSGL_INFO, "ae_twolame, 1 audio channel, forcing mono mode\n"); mode = TWOLAME_MONO; } else if(encoder->params.channels == 2) { if(! strcasecmp(param_mode, "dual")) mode = TWOLAME_DUAL_CHANNEL; else if(! strcasecmp(param_mode, "jstereo")) mode = TWOLAME_JOINT_STEREO; else if(! strcasecmp(param_mode, "stereo")) mode = TWOLAME_STEREO; else { mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, unknown mode %s, exiting\n", param_mode); } } else mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, Twolame can't encode > 2 channels, exiting\n"); ctx = (mpae_twolame_ctx *) calloc(1, sizeof(mpae_twolame_ctx)); if(ctx == NULL) { mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, couldn't alloc a %d bytes context, exiting\n", sizeof(mpae_twolame_ctx)); return 0; } ctx->twolame_ctx = twolame_init(); if(ctx->twolame_ctx == NULL) { mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, couldn't initial parameters from libtwolame, exiting\n"); free(ctx); return 0; } ctx->vbr = 0; if(twolame_set_num_channels(ctx->twolame_ctx, encoder->params.channels) != 0) return 0; if(twolame_set_mode(ctx->twolame_ctx, mode) != 0) return 0; if(twolame_set_in_samplerate(ctx->twolame_ctx, encoder->params.sample_rate) != 0) return 0; if(twolame_set_out_samplerate(ctx->twolame_ctx, encoder->params.sample_rate) != 0) return 0; if(encoder->params.sample_rate < 32000) twolame_set_version(ctx->twolame_ctx, TWOLAME_MPEG2); else twolame_set_version(ctx->twolame_ctx, TWOLAME_MPEG1); if(twolame_set_psymodel(ctx->twolame_ctx, param_psy) != 0) return 0; if(twolame_set_bitrate(ctx->twolame_ctx, param_bitrate) != 0) return 0; if(param_errprot) if(twolame_set_error_protection(ctx->twolame_ctx, TRUE) != 0) return 0; if(param_vbr != 0) { if(twolame_set_VBR(ctx->twolame_ctx, TRUE) != 0) return 0; if(twolame_set_VBR_q(ctx->twolame_ctx, param_vbr) != 0) return 0; if(twolame_set_padding(ctx->twolame_ctx, FALSE) != 0) return 0; if(param_maxvbr) { if(twolame_set_VBR_max_bitrate_kbps(ctx->twolame_ctx, param_maxvbr) != 0) return 0; } ctx->vbr = 1; } if(twolame_set_verbosity(ctx->twolame_ctx, param_debug) != 0) return 0; if(twolame_init_params(ctx->twolame_ctx) != 0) return 0; encoder->params.bitrate = param_bitrate * 1000; encoder->params.samples_per_frame = 1152; encoder->priv = ctx; encoder->decode_buffer_size = 1152 * 2 * encoder->params.channels; encoder->bind = bind_twolame; encoder->get_frame_size = get_frame_size; encoder->encode = encode_twolame; encoder->close = close_twolame; return 1; }
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); }