[FFmpeg-devel] [PATCH] avcodec_copy_context()

Ronald S. Bultje rsbultje
Wed Mar 31 16:11:11 CEST 2010


Hi,

On Wed, Mar 31, 2010 at 9:59 AM, Ronald S. Bultje <rsbultje at gmail.com> wrote:
> Hi Michael,
>
> On Wed, Mar 31, 2010 at 6:00 AM, Michael Niedermayer <michaelni at gmx.at> wrote:
>> On Tue, Mar 30, 2010 at 02:23:03PM -0400, Ronald S. Bultje wrote:
>>> + * @param dest target codec context, should be allocated with
>>> + * ? ? ? ? ? ? #avcodec_alloc_context(), but otherwise uninitialized
>>
>> this should be checked if(->codec) or so maybe
>
> Coding error, so should be assert(), will add.
>
>>> + ? ?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
>
> OK.
>
> Also changed dest->rc_eq to rc_eq as Benoit suggested.

This one actually compiles...

Ronald
-------------- next part --------------
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 initialized with
+ *             #avcodec_alloc_context(), but otherwise uninitialized
+ * @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-31 10:09:38.000000000 -0400
@@ -471,3 +471,58 @@
     return avcodec_alloc_context2(CODEC_TYPE_UNKNOWN);
 }
 
+int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
+{
+    assert(!dest->codec); // check that the dest context is uninitialized
+    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 */
+    dest->rc_eq           = NULL;
+    dest->extradata       = NULL;
+    dest->intra_matrix    = NULL;
+    dest->inter_matrix    = NULL;
+    dest->rc_override     = NULL;
+    if (src->rc_eq) {
+        dest->rc_eq = av_strdup(src->rc_eq);
+        if (!dest->rc_eq)
+            return AVERROR(ENOMEM);
+    }
+
+#define alloc_and_cpy_or_fail(obj, size, pad) \
+    if (src->obj && size > 0) { \
+        dest->obj = av_malloc(size + pad); \
+        if (!dest->obj) \
+            goto fail; \
+        memcpy(dest->obj, src->obj, size); \
+        if (pad) \
+            memset(((uint8_t *) dest->obj) + size, 0, pad); \
+    }
+    alloc_and_cpy_or_fail(extradata,    src->extradata_size,
+                          FF_INPUT_BUFFER_PADDING_SIZE);
+    alloc_and_cpy_or_fail(intra_matrix, 64 * sizeof(int16_t), 0);
+    alloc_and_cpy_or_fail(inter_matrix, 64 * sizeof(int16_t), 0);
+    alloc_and_cpy_or_fail(rc_override,  src->rc_override_count * sizeof(*src->rc_override), 0);
+#undef alloc_and_cpy_or_fail
+
+    return 0;
+
+fail:
+    av_freep(&dest->rc_override);
+    av_freep(&dest->intra_matrix);
+    av_freep(&dest->inter_matrix);
+    av_freep(&dest->extradata);
+    av_freep(&dest->rc_eq);
+    return AVERROR(ENOMEM);
+}



More information about the ffmpeg-devel mailing list