[FFmpeg-devel] [PATCH] avcodec_copy_context()

Ronald S. Bultje rsbultje
Tue Mar 30 16:22:26 CEST 2010


Hi,

as per $subj. Subsequent patches will use this function.

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-29 13:37:49.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
+ * @returns 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));
+    }
+
+    return 0;
+}



More information about the ffmpeg-devel mailing list