+ if( val.psz_string ) free( val.psz_string );
+
+ var_Get( p_enc, ENC_CFG_PREFIX "qmin", &val );
+ p_sys->i_qmin = val.i_int;
+ var_Get( p_enc, ENC_CFG_PREFIX "qmax", &val );
+ p_sys->i_qmax = val.i_int;
+ var_Get( p_enc, ENC_CFG_PREFIX "trellis", &val );
+ p_sys->b_trellis = val.b_bool;
+
+ var_Get( p_enc, ENC_CFG_PREFIX "strict", &val );
+ if( val.i_int < - 1 || val.i_int > 1 ) val.i_int = 0;
+ p_context->strict_std_compliance = val.i_int;
+
+ var_Get( p_enc, ENC_CFG_PREFIX "lumi-masking", &val );
+ p_sys->f_lumi_masking = val.f_float;
+ var_Get( p_enc, ENC_CFG_PREFIX "dark-masking", &val );
+ p_sys->f_dark_masking = val.f_float;
+ var_Get( p_enc, ENC_CFG_PREFIX "p-masking", &val );
+ p_sys->f_p_masking = val.f_float;
+ var_Get( p_enc, ENC_CFG_PREFIX "border-masking", &val );
+ p_sys->f_border_masking = val.f_float;
+ var_Get( p_enc, ENC_CFG_PREFIX "luma-elim-threshold", &val );
+ p_sys->i_luma_elim = val.i_int;
+ var_Get( p_enc, ENC_CFG_PREFIX "chroma-elim-threshold", &val );
+ p_sys->i_chroma_elim = val.i_int;
+
+ if( p_enc->fmt_in.i_cat == VIDEO_ES )
+ {
+ int i_aspect_num, i_aspect_den;
+
+ if( !p_enc->fmt_in.video.i_width || !p_enc->fmt_in.video.i_height )
+ {
+ msg_Warn( p_enc, "invalid size %ix%i", p_enc->fmt_in.video.i_width,
+ p_enc->fmt_in.video.i_height );
+ free( p_sys );
+ return VLC_EGENERIC;
+ }
+
+ p_context->width = p_enc->fmt_in.video.i_width;
+ p_context->height = p_enc->fmt_in.video.i_height;
+
+#if LIBAVCODEC_BUILD >= 4754
+ p_context->time_base.num = p_enc->fmt_in.video.i_frame_rate_base;
+ p_context->time_base.den = p_enc->fmt_in.video.i_frame_rate;
+#else
+ p_context->frame_rate = p_enc->fmt_in.video.i_frame_rate;
+ p_context->frame_rate_base= p_enc->fmt_in.video.i_frame_rate_base;
+#endif
+
+ /* Defaults from ffmpeg.c */
+ p_context->qblur = 0.5;
+ p_context->qcompress = 0.5;
+ p_context->b_quant_offset = 1.25;
+ p_context->b_quant_factor = 1.25;
+ p_context->i_quant_offset = 0.0;
+ p_context->i_quant_factor = -0.8;
+
+ p_context->lumi_masking = p_sys->f_lumi_masking;
+ p_context->dark_masking = p_sys->f_dark_masking;
+ p_context->p_masking = p_sys->f_p_masking;
+#if LIBAVCODEC_BUILD >= 4741
+ p_context->border_masking = p_sys->f_border_masking;
+#endif
+ p_context->luma_elim_threshold = p_sys->i_luma_elim;
+ p_context->chroma_elim_threshold = p_sys->i_chroma_elim;
+
+ if( p_sys->i_key_int > 0 )
+ p_context->gop_size = p_sys->i_key_int;
+ p_context->max_b_frames =
+ __MAX( __MIN( p_sys->i_b_frames, FF_MAX_B_FRAMES ), 0 );
+ p_context->b_frame_strategy = 0;
+
+#if LIBAVCODEC_BUILD >= 4687
+ av_reduce( &i_aspect_num, &i_aspect_den,
+ p_enc->fmt_in.video.i_aspect,
+ VOUT_ASPECT_FACTOR, 1 << 30 /* something big */ );
+ av_reduce( &p_context->sample_aspect_ratio.num,
+ &p_context->sample_aspect_ratio.den,
+ i_aspect_num * (int64_t)p_context->height,
+ i_aspect_den * (int64_t)p_context->width, 1 << 30 );
+#else
+ p_context->aspect_ratio = ((float)p_enc->fmt_in.video.i_aspect) /
+ VOUT_ASPECT_FACTOR;
+#endif
+
+ p_sys->p_buffer_out = malloc( AVCODEC_MAX_VIDEO_FRAME_SIZE );
+
+ p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
+ p_context->pix_fmt = E_(GetFfmpegChroma)( p_enc->fmt_in.i_codec );
+#if LIBAVCODEC_BUILD >= 4714
+ if( p_codec->pix_fmts )
+ {
+ const enum PixelFormat *p = p_codec->pix_fmts;
+ for( ; *p != -1; p++ )
+ {
+ if( *p == p_context->pix_fmt ) break;
+ }
+ if( *p == -1 ) p_context->pix_fmt = p_codec->pix_fmts[0];
+ p_enc->fmt_in.i_codec = E_(GetVlcChroma)( p_context->pix_fmt );
+ }
+#else
+ p_enc->fmt_in.i_codec = E_(GetVlcChroma)( p_context->pix_fmt );
+#endif
+
+ if ( p_sys->b_strict_rc )
+ {
+ p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
+ p_context->rc_buffer_size = p_sys->i_rc_buffer_size;
+ /* This is from ffmpeg's ffmpeg.c : */
+ p_context->rc_initial_buffer_occupancy
+ = p_sys->i_rc_buffer_size * 3/4;
+ p_context->rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
+ }
+
+ if ( p_sys->f_i_quant_factor != 0.0 )
+ p_context->i_quant_factor = p_sys->f_i_quant_factor;
+
+#if LIBAVCODEC_BUILD >= 4690
+ p_context->noise_reduction = p_sys->i_noise_reduction;
+#endif
+
+ if ( p_sys->b_mpeg4_matrix )
+ {
+ p_context->intra_matrix = ff_mpeg4_default_intra_matrix;
+ p_context->inter_matrix = ff_mpeg4_default_non_intra_matrix;
+ }
+
+ if ( p_sys->b_pre_me )
+ {
+ p_context->pre_me = 1;
+ p_context->me_pre_cmp = FF_CMP_CHROMA;
+ }
+
+ if ( p_sys->b_interlace )
+ {
+ if ( p_context->height <= 280 )
+ {
+ if ( p_context->height != 16 || p_context->width != 16 )
+ msg_Warn( p_enc,
+ "disabling interlaced video because height=%d <= 280",
+ p_context->height );
+ }
+ else
+ {
+ p_context->flags |= CODEC_FLAG_INTERLACED_DCT;
+#if LIBAVCODEC_BUILD >= 4698
+ if ( p_sys->b_interlace_me )
+ p_context->flags |= CODEC_FLAG_INTERLACED_ME;
+ }
+#endif
+ }
+
+ if ( p_sys->b_trellis )
+ p_context->flags |= CODEC_FLAG_TRELLIS_QUANT;
+
+ if ( p_sys->i_qmin > 0 && p_sys->i_qmin == p_sys->i_qmax )
+ p_context->flags |= CODEC_FLAG_QSCALE;
+
+#if LIBAVCODEC_BUILD >= 4702
+ if ( p_enc->i_threads >= 1 )
+ p_context->thread_count = p_enc->i_threads;