X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fcodec%2Favcodec%2Fencoder.c;h=0981c1b2458ffe227d5ffb987893971f34541b3f;hb=12ade3e3bc975d5426ba4af155b7372c31093b31;hp=14d7011c1b08a85bc7f81181d9d39fe62a3413f0;hpb=9efe5bff03ca4a0d218b98e0e697d51dea8d5b55;p=vlc diff --git a/modules/codec/avcodec/encoder.c b/modules/codec/avcodec/encoder.c index 14d7011c1b..0981c1b245 100644 --- a/modules/codec/avcodec/encoder.c +++ b/modules/codec/avcodec/encoder.c @@ -436,13 +436,7 @@ int OpenEncoder( vlc_object_t *p_this ) p_enc->fmt_in.video.i_sar_num, p_enc->fmt_in.video.i_sar_den, 1 << 30 ); - p_sys->i_buffer_out = p_context->height * p_context->width - * 3 /* Assume 24bpp maximum */ - + 200; /* some room for potential headers (such as BMP) */ - - if( p_sys->i_buffer_out < FF_MIN_BUFFER_SIZE ) - p_sys->i_buffer_out = FF_MIN_BUFFER_SIZE; - p_sys->p_buffer_out = malloc( p_sys->i_buffer_out ); + p_sys->p_buffer_out = NULL; p_enc->fmt_in.i_codec = VLC_CODEC_I420; p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec; @@ -600,10 +594,38 @@ int OpenEncoder( vlc_object_t *p_this ) libvpx-720p preset from libvpx ffmpeg-patch */ if( i_codec_id == CODEC_ID_VP8 ) { + /* Lets give bitrate tolerance */ p_context->bit_rate_tolerance = __MAX(2 * p_enc->fmt_out.i_bitrate, p_sys->i_vtolerance ); - /* I used Harrison-stetson method here to get there values */ - p_context->rc_max_rate = 3 * p_enc->fmt_out.i_bitrate; - p_context->rc_min_rate = p_enc->fmt_out.i_bitrate / 200; + /* default to 120 frames between keyframe */ + if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" ) ) + p_context->gop_size = 120; + /* Don't set rc-values atm, they were from time before + libvpx was officially in ffmpeg */ + //p_context->rc_max_rate = 24 * 1000 * 1000; //24M + //p_context->rc_min_rate = 40 * 1000; // 40k + /* seems that ffmpeg presets have 720p as divider for buffers */ + if( p_enc->fmt_out.video.i_height >= 720 ) + { + /* Check that we don't overrun users qmin/qmax values */ + if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) ) + { + p_context->mb_qmin = p_context->qmin = 10; + p_context->mb_lmin = p_context->lmin = 10 * FF_QP2LAMBDA; + } + + if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmax" ) ) + { + p_context->mb_qmax = p_context->qmax = 42; + p_context->mb_lmax = p_context->lmax = 42 * FF_QP2LAMBDA; + } + + } else { + if( !var_GetInteger( p_enc, ENC_CFG_PREFIX "qmin" ) ) + { + p_context->mb_qmin = p_context->qmin = 1; + p_context->mb_lmin = p_context->lmin = FF_QP2LAMBDA; + } + } #if 0 /* enable when/if vp8 encoder is accepted in libavcodec */ @@ -735,6 +757,10 @@ int OpenEncoder( vlc_object_t *p_this ) if( p_enc->fmt_out.i_extra ) { p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra ); + if ( p_enc->fmt_out.p_extra == NULL ) + { + goto error; + } memcpy( p_enc->fmt_out.p_extra, p_context->extradata, p_enc->fmt_out.i_extra ); } @@ -753,6 +779,10 @@ int OpenEncoder( vlc_object_t *p_this ) p_context->frame_size : RAW_AUDIO_FRAME_SIZE; p_sys->p_buffer = malloc( p_sys->i_frame_size * p_sys->i_sample_bytes ); + if ( p_sys->p_buffer == NULL ) + { + goto error; + } p_enc->fmt_out.audio.i_blockalign = p_context->block_align; p_enc->fmt_out.audio.i_bitspersample = aout_BitsPerSample( vlc_fourcc_GetCodec( AUDIO_ES, p_enc->fmt_out.i_codec ) ); @@ -761,11 +791,21 @@ int OpenEncoder( vlc_object_t *p_this ) else p_sys->i_buffer_out = p_sys->i_frame_size * p_sys->i_sample_bytes; p_sys->p_buffer_out = malloc( p_sys->i_buffer_out ); + if ( p_sys->p_buffer_out == NULL ) + { + goto error; + } } msg_Dbg( p_enc, "found encoder %s", psz_namecodec ); return VLC_SUCCESS; +error: + free( p_enc->fmt_out.p_extra ); + free( p_sys->p_buffer ); + free( p_sys->p_buffer_out ); + free( p_sys ); + return VLC_ENOMEM; } /**************************************************************************** @@ -777,6 +817,24 @@ static block_t *EncodeVideo( encoder_t *p_enc, picture_t *p_pict ) AVFrame frame; int i_out, i_plane; + /* Initialize the video output buffer the first time. + * This is done here instead of OpenEncoder() because we need the actual + * bits_per_pixel value, without having to assume anything. + */ + if ( p_sys->p_buffer_out == NULL ) + { + int bytesPerPixel = p_enc->fmt_out.video.i_bits_per_pixel ? + p_enc->fmt_out.video.i_bits_per_pixel / 8 : 3; + + p_sys->i_buffer_out = p_sys->p_context->height * p_sys->p_context->width + * bytesPerPixel + 200; /* some room for potential headers (such as BMP) */ + + if( p_sys->i_buffer_out < FF_MIN_BUFFER_SIZE ) + p_sys->i_buffer_out = FF_MIN_BUFFER_SIZE; + p_sys->p_buffer_out = malloc( p_sys->i_buffer_out ); + if ( p_sys->p_buffer_out == NULL ) + return NULL; + } memset( &frame, 0, sizeof( AVFrame ) ); if( likely(p_pict) ) {