static bool twolame_encoder_setup(struct twolame_encoder *encoder, GError **error) { if (encoder->quality >= -1.0) { /* a quality was configured (VBR) */ if (0 != twolame_set_VBR(encoder->options, true)) { g_set_error(error, twolame_encoder_quark(), 0, "error setting twolame VBR mode"); return false; } if (0 != twolame_set_VBR_q(encoder->options, encoder->quality)) { g_set_error(error, twolame_encoder_quark(), 0, "error setting twolame VBR quality"); return false; } } else { /* a bit rate was configured */ if (0 != twolame_set_brate(encoder->options, encoder->bitrate)) { g_set_error(error, twolame_encoder_quark(), 0, "error setting twolame bitrate"); return false; } } if (0 != twolame_set_num_channels(encoder->options, encoder->audio_format.channels)) { g_set_error(error, twolame_encoder_quark(), 0, "error setting twolame num channels"); return false; } if (0 != twolame_set_in_samplerate(encoder->options, encoder->audio_format.sample_rate)) { g_set_error(error, twolame_encoder_quark(), 0, "error setting twolame sample rate"); return false; } if (0 > twolame_init_params(encoder->options)) { g_set_error(error, twolame_encoder_quark(), 0, "error initializing twolame params"); return false; } return true; }
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; }
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; }