]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/formats.c
Enable PIC unconditionally on OpenBSD
[ffmpeg] / libavfilter / formats.c
index 4ce1ff70db14031f6dbcfa0619711c2ff4b265c3..33fec163a52a4e34587590be992a5f60622ca02e 100644 (file)
 
 #include "avfilter.h"
 
+/**
+ * Add all refs from a to ret and destroy a.
+ */
+static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
+{
+    int i;
+
+    for(i = 0; i < a->refcount; i ++) {
+        ret->refs[ret->refcount] = a->refs[i];
+        *ret->refs[ret->refcount++] = ret;
+    }
+
+    av_free(a->refs);
+    av_free(a->formats);
+    av_free(a);
+}
+
 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
 {
     AVFilterFormats *ret;
@@ -36,31 +53,18 @@ AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
             if(a->formats[i] == b->formats[j])
                 ret->formats[k++] = a->formats[i];
 
+    ret->format_count = k;
     /* check that there was at least one common format */
-    if(!(ret->format_count = k)) {
+    if(!ret->format_count) {
         av_free(ret->formats);
         av_free(ret);
         return NULL;
     }
 
-    /* merge and update all the references */
     ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
-    for(i = 0; i < a->refcount; i ++) {
-        ret->refs[ret->refcount] = a->refs[i];
-        *ret->refs[ret->refcount++] = ret;
-    }
-    for(i = 0; i < b->refcount; i ++) {
-        ret->refs[ret->refcount] = b->refs[i];
-        *ret->refs[ret->refcount++] = ret;
-    }
-
-    av_free(a->refs);
-    av_free(a->formats);
-    av_free(a);
 
-    av_free(b->refs);
-    av_free(b->formats);
-    av_free(b);
+    merge_ref(ret, a);
+    merge_ref(ret, b);
 
     return ret;
 }
@@ -116,9 +120,9 @@ static int find_ref_index(AVFilterFormats **ref)
 
 void avfilter_formats_unref(AVFilterFormats **ref)
 {
-    int idx;
+    int idx = find_ref_index(ref);
 
-    if((idx = find_ref_index(ref)) >= 0)
+    if(idx >= 0)
         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
 
@@ -133,9 +137,9 @@ void avfilter_formats_unref(AVFilterFormats **ref)
 void avfilter_formats_changeref(AVFilterFormats **oldref,
                                 AVFilterFormats **newref)
 {
-    int idx;
+    int idx = find_ref_index(oldref);
 
-    if((idx = find_ref_index(oldref)) >= 0) {
+    if(idx >= 0) {
         (*oldref)->refs[idx] = newref;
         *newref = *oldref;
         *oldref = NULL;