]> git.sesse.net Git - ffmpeg/blob - libavfilter/formats.c
Factor duplicated loop
[ffmpeg] / libavfilter / formats.c
1 /*
2  * Filter layer - format negotiation
3  * copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "avfilter.h"
23
24 /** merge and update all the references */
25 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
26 {
27     int i;
28     for(i = 0; i < a->refcount; i ++) {
29         ret->refs[ret->refcount] = a->refs[i];
30         *ret->refs[ret->refcount++] = ret;
31     }
32 }
33
34 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
35 {
36     AVFilterFormats *ret;
37     unsigned i, j, k = 0;
38
39     ret = av_mallocz(sizeof(AVFilterFormats));
40
41     /* merge list of formats */
42     ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
43                                                            b->format_count));
44     for(i = 0; i < a->format_count; i ++)
45         for(j = 0; j < b->format_count; j ++)
46             if(a->formats[i] == b->formats[j])
47                 ret->formats[k++] = a->formats[i];
48
49     /* check that there was at least one common format */
50     if(!(ret->format_count = k)) {
51         av_free(ret->formats);
52         av_free(ret);
53         return NULL;
54     }
55
56     ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
57
58     merge_ref(ret, a);
59     merge_ref(ret, b);
60
61     av_free(a->refs);
62     av_free(a->formats);
63     av_free(a);
64
65     av_free(b->refs);
66     av_free(b->formats);
67     av_free(b);
68
69     return ret;
70 }
71
72 AVFilterFormats *avfilter_make_format_list(int len, ...)
73 {
74     AVFilterFormats *ret;
75     int i;
76     va_list vl;
77
78     ret = av_mallocz(sizeof(AVFilterFormats));
79     ret->formats = av_malloc(sizeof(*ret->formats) * len);
80     ret->format_count = len;
81
82     va_start(vl, len);
83     for(i = 0; i < len; i ++)
84         ret->formats[i] = va_arg(vl, int);
85     va_end(vl);
86
87     return ret;
88 }
89
90 AVFilterFormats *avfilter_all_colorspaces(void)
91 {
92     AVFilterFormats *ret;
93     int i;
94
95     ret = av_mallocz(sizeof(AVFilterFormats));
96     ret->formats = av_malloc(sizeof(*ret->formats) * PIX_FMT_NB);
97     ret->format_count = PIX_FMT_NB;
98
99     for(i = 0; i < PIX_FMT_NB; i ++)
100         ret->formats[i] = i;
101
102     return ret;
103 }
104
105 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
106 {
107     *ref = f;
108     f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
109     f->refs[f->refcount-1] = ref;
110 }
111
112 static int find_ref_index(AVFilterFormats **ref)
113 {
114     int i;
115     for(i = 0; i < (*ref)->refcount; i ++)
116         if((*ref)->refs[i] == ref)
117             return i;
118     return -1;
119 }
120
121 void avfilter_formats_unref(AVFilterFormats **ref)
122 {
123     int idx;
124
125     if((idx = find_ref_index(ref)) >= 0)
126         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
127             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
128
129     if(!--(*ref)->refcount) {
130         av_free((*ref)->formats);
131         av_free((*ref)->refs);
132         av_free(*ref);
133     }
134     *ref = NULL;
135 }
136
137 void avfilter_formats_changeref(AVFilterFormats **oldref,
138                                 AVFilterFormats **newref)
139 {
140     int idx;
141
142     if((idx = find_ref_index(oldref)) >= 0) {
143         (*oldref)->refs[idx] = newref;
144         *newref = *oldref;
145         *oldref = NULL;
146     }
147 }
148