[vlc-devel] [PATCH 2/2] avcodec: initialize buffers when first frame comes in

Ilkka Ollakka ileoo at videolan.org
Fri Sep 20 10:35:37 CEST 2013


On Fri, Sep 20, 2013 at 11:33:17AM +0300, Ilkka Ollakka wrote:

I'm not sure what is the best way to handle error in EncodeAudio block
if those mallocs fail in there?

> ---
>  modules/codec/avcodec/encoder.c | 37 ++++++++++++++++++++++++-------------
>  1 file changed, 24 insertions(+), 13 deletions(-)

> diff --git a/modules/codec/avcodec/encoder.c b/modules/codec/avcodec/encoder.c
> index 977c2ba..b318340 100644
> --- a/modules/codec/avcodec/encoder.c
> +++ b/modules/codec/avcodec/encoder.c
> @@ -873,24 +873,12 @@ int OpenEncoder( vlc_object_t *p_this )
>          p_sys->i_frame_size = p_context->frame_size > 1 ?
>                                      p_context->frame_size :
>                                      FF_MIN_BUFFER_SIZE;
> -        p_sys->p_buffer = malloc( p_sys->i_frame_size * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels);
> -        if ( unlikely( 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( p_enc->fmt_out.i_codec );
>          //b_variable tells if we can feed any size frames to encoder
>          p_sys->b_variable = p_context->frame_size ? false : true;
> +        p_sys->i_buffer_out = 0;

> -        p_sys->i_buffer_out = p_sys->i_frame_size * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels;
> -
> -        if( p_sys->b_planar )
> -        {
> -            p_sys->p_interleave_buf = malloc( p_sys->i_buffer_out );
> -            if( unlikely( p_sys->p_interleave_buf == NULL ) )
> -                goto error;
> -        }
>      }

>      p_sys->frame = avcodec_alloc_frame();
> @@ -1122,6 +1110,29 @@ static block_t *EncodeAudio( encoder_t *p_enc, block_t *p_aout_buf )
>      int got_packet,i_out;
>      size_t buffer_delay = 0, i_samples_left = 0;

> +    /* Take buffers only when first frame arrives, as for example
> +     * faad sometimes has channels count only at this point*/
> +    if( unlikely( p_sys->i_buffer_out == 0 ) )
> +    {
> +        p_sys->i_buffer_out = p_sys->i_frame_size * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels;
> +        p_sys->p_buffer = malloc( p_sys->i_buffer_out );
> +        if ( unlikely( p_sys->p_buffer == NULL ) )
> +        {
> +            p_sys->i_buffer_out = 0;
> +            return NULL;
> +        }
> +
> +        if( p_sys->b_planar )
> +        {
> +            p_sys->p_interleave_buf = malloc( p_sys->i_buffer_out );
> +            if( unlikely( p_sys->p_interleave_buf == NULL ) )
> +            {
> +                p_sys->i_buffer_out = 0;
> +                return NULL;
> +            }
> +        }
> +    }
> +
>      //i_bytes_left is amount of bytes we get
>      i_samples_left = p_aout_buf ? p_aout_buf->i_nb_samples : 0;
>      buffer_delay = p_sys->i_samples_delay * p_sys->i_sample_bytes * p_enc->fmt_in.audio.i_channels;
-- 
Ilkka Ollakka
When a fellow says, "It ain't the money but the principle of the thing,"
it's the money.
		-- Kim Hubbard
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20130920/3dbec5e2/attachment.sig>


More information about the vlc-devel mailing list