[FFmpeg-devel] [PATCH] avcodec_copy_context()

Michael Niedermayer michaelni
Wed Mar 31 12:00:28 CEST 2010


On Tue, Mar 30, 2010 at 02:23:03PM -0400, Ronald S. Bultje wrote:
> Hi,
> 
> 2010/3/30 M?ns Rullg?rd <mans at mansr.com>:
> > Stefano Sabatini <stefano.sabatini-lala at poste.it> writes:
> >> On date Tuesday 2010-03-30 10:22:26 -0400, Ronald S. Bultje encoded:
> >>> + * Copy the settings of the source AVCodecContext into the destination
> >>
> >> Nit: "Copies" (but I won't fight the Third Person Holy War so do as
> >> you prefer).
> 
> This will get so funny. ;-). Left as is for now, I don't think it matters.
> 
> >>> + * AVCodecContext. The resulting destination codec context will be
> >>> + * unopened, i.e. you are required to call avcodec_open() before you
> >>> + * can use this AVCodecContext to decode/encode video/audio data.
> >>> + *
> >>> + * @param dest target codec context, should be initialized with
> >>> + * ? ? ? ? ? ? #avcodec_alloc_context(), but otherwise uninitialized
> >>> + * @param src source codec context
> >>> + * @returns AVERROR() on error (e.g. memory allocation error), 0 on success.
> >>
> >> The final "." is not required.
> >
> > @return
> 
> Both fixed.
> 
> Ronald

>  avcodec.h |   13 +++++++++++++
>  options.c |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 65 insertions(+)
> e20e4b0904567737a11dc454a20f4c823ae17de7  avcodec_copy_context.patch
> Index: ffmpeg-svn/libavcodec/avcodec.h
> ===================================================================
> --- ffmpeg-svn.orig/libavcodec/avcodec.h	2010-03-29 13:37:31.000000000 -0400
> +++ ffmpeg-svn/libavcodec/avcodec.h	2010-03-30 12:00:24.000000000 -0400
> @@ -3256,6 +3256,19 @@
>  AVCodecContext *avcodec_alloc_context2(enum CodecType);
>  
>  /**
> + * Copy the settings of the source AVCodecContext into the destination
> + * AVCodecContext. The resulting destination codec context will be
> + * unopened, i.e. you are required to call avcodec_open() before you
> + * can use this AVCodecContext to decode/encode video/audio data.
> + *

> + * @param dest target codec context, should be allocated with
> + *             #avcodec_alloc_context(), but otherwise uninitialized

this should be checked if(->codec) or so maybe


> + * @param src source codec context
> + * @return AVERROR() on error (e.g. memory allocation error), 0 on success
> + */
> +int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src);
> +
> +/**
>   * Sets the fields of the given AVFrame to default values.
>   *
>   * @param pic The AVFrame of which the fields should be set to default values.
> Index: ffmpeg-svn/libavcodec/options.c
> ===================================================================
> --- ffmpeg-svn.orig/libavcodec/options.c	2010-03-29 13:37:31.000000000 -0400
> +++ ffmpeg-svn/libavcodec/options.c	2010-03-29 13:37:49.000000000 -0400
> @@ -471,3 +471,55 @@
>      return avcodec_alloc_context2(CODEC_TYPE_UNKNOWN);
>  }
>  
> +int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
> +{
> +    memcpy(dest, src, sizeof(*dest));
> +
> +    /* set values specific to opened codecs back to their default state */
> +    dest->priv_data       = NULL;
> +    dest->codec           = NULL;
> +    dest->palctrl         = NULL;
> +    dest->slice_offset    = NULL;
> +    dest->internal_buffer = NULL;
> +    dest->hwaccel         = NULL;
> +    dest->execute         = NULL;
> +    dest->execute2        = NULL;
> +    dest->reget_buffer    = NULL;
> +    dest->thread_opaque   = NULL;
> +
> +    /* reallocate values that should be allocated separately */
> +    if (dest->rc_eq) {
> +        dest->rc_eq = av_strdup(src->rc_eq);
> +        if (!dest->rc_eq)
> +            return AVERROR(ENOMEM);
> +    }
> +    if (src->extradata && src->extradata_size > 0) {
> +        dest->extradata = av_malloc(src->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
> +        if (!dest->extradata)
> +            return AVERROR(ENOMEM);
> +        memcpy(dest->extradata, src->extradata, src->extradata_size);
> +        memset(dest->extradata + dest->extradata_size, 0,
> +               FF_INPUT_BUFFER_PADDING_SIZE);
> +    }
> +    if (src->intra_matrix) {
> +        dest->intra_matrix = av_malloc(64 * sizeof(int16_t));
> +        if (!dest->intra_matrix)
> +            return AVERROR(ENOMEM);
> +        memcpy(dest->intra_matrix, src->intra_matrix, 64 * sizeof(int16_t));
> +    }
> +    if (src->inter_matrix) {
> +        dest->inter_matrix = av_malloc(64 * sizeof(int16_t));
> +        if (!dest->inter_matrix)
> +            return AVERROR(ENOMEM);
> +        memcpy(dest->inter_matrix, src->inter_matrix, 64 * sizeof(int16_t));
> +    }
> +    if (src->rc_override_count > 0 && src->rc_override) {
> +        dest->rc_override = av_malloc(sizeof(*src->rc_override) * src->rc_override_count);
> +        if (!dest->rc_override)
> +            return AVERROR(ENOMEM);
> +        memcpy(dest->rc_override, src->rc_override,
> +               dest->rc_override_count * sizeof(*src->rc_override));
> +    }

you might want to have a macro for the if(x) alloc,goto error,cpy


[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Many that live deserve death. And some that die deserve life. Can you give
it to them? Then do not be too eager to deal out death in judgement. For
even the very wise cannot see all ends. -- Gandalf
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20100331/4e997a17/attachment.pgp>



More information about the ffmpeg-devel mailing list