#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;
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;
}
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));
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;