b3873d7acbee815f1dcd3174ba60946aa303478c
[libav.git] / 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
29 for(i = 0; i < a->refcount; i ++) {
30 ret->refs[ret->refcount] = a->refs[i];
31 *ret->refs[ret->refcount++] = ret;
32 }
33
34 av_free(a->refs);
35 av_free(a->formats);
36 av_free(a);
37 }
38
39 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
40 {
41 AVFilterFormats *ret;
42 unsigned i, j, k = 0;
43
44 ret = av_mallocz(sizeof(AVFilterFormats));
45
46 /* merge list of formats */
47 ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
48 b->format_count));
49 for(i = 0; i < a->format_count; i ++)
50 for(j = 0; j < b->format_count; j ++)
51 if(a->formats[i] == b->formats[j])
52 ret->formats[k++] = a->formats[i];
53
54 /* check that there was at least one common format */
55 if(!(ret->format_count = k)) {
56 av_free(ret->formats);
57 av_free(ret);
58 return NULL;
59 }
60
61 ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
62
63 merge_ref(ret, a);
64 merge_ref(ret, b);
65
66 return ret;
67 }
68
69 AVFilterFormats *avfilter_make_format_list(int len, ...)
70 {
71 AVFilterFormats *ret;
72 int i;
73 va_list vl;
74
75 ret = av_mallocz(sizeof(AVFilterFormats));
76 ret->formats = av_malloc(sizeof(*ret->formats) * len);
77 ret->format_count = len;
78
79 va_start(vl, len);
80 for(i = 0; i < len; i ++)
81 ret->formats[i] = va_arg(vl, int);
82 va_end(vl);
83
84 return ret;
85 }
86
87 AVFilterFormats *avfilter_all_colorspaces(void)
88 {
89 AVFilterFormats *ret;
90 int i;
91
92 ret = av_mallocz(sizeof(AVFilterFormats));
93 ret->formats = av_malloc(sizeof(*ret->formats) * PIX_FMT_NB);
94 ret->format_count = PIX_FMT_NB;
95
96 for(i = 0; i < PIX_FMT_NB; i ++)
97 ret->formats[i] = i;
98
99 return ret;
100 }
101
102 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
103 {
104 *ref = f;
105 f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
106 f->refs[f->refcount-1] = ref;
107 }
108
109 static int find_ref_index(AVFilterFormats **ref)
110 {
111 int i;
112 for(i = 0; i < (*ref)->refcount; i ++)
113 if((*ref)->refs[i] == ref)
114 return i;
115 return -1;
116 }
117
118 void avfilter_formats_unref(AVFilterFormats **ref)
119 {
120 int idx;
121
122 if((idx = find_ref_index(ref)) >= 0)
123 memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
124 sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
125
126 if(!--(*ref)->refcount) {
127 av_free((*ref)->formats);
128 av_free((*ref)->refs);
129 av_free(*ref);
130 }
131 *ref = NULL;
132 }
133
134 void avfilter_formats_changeref(AVFilterFormats **oldref,
135 AVFilterFormats **newref)
136 {
137 int idx;
138
139 if((idx = find_ref_index(oldref)) >= 0) {
140 (*oldref)->refs[idx] = newref;
141 *newref = *oldref;
142 *oldref = NULL;
143 }
144 }
145