]> git.sesse.net Git - ffmpeg/blob - libavfilter/formats.c
Add an entry for the avfilter_add_colorspace() API addition.
[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 /**
25  * Add all refs from a to ret and destroy a.
26  */
27 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
28 {
29     int i;
30
31     for(i = 0; i < a->refcount; i ++) {
32         ret->refs[ret->refcount] = a->refs[i];
33         *ret->refs[ret->refcount++] = ret;
34     }
35
36     av_free(a->refs);
37     av_free(a->formats);
38     av_free(a);
39 }
40
41 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
42 {
43     AVFilterFormats *ret;
44     unsigned i, j, k = 0;
45
46     ret = av_mallocz(sizeof(AVFilterFormats));
47
48     /* merge list of formats */
49     ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
50                                                            b->format_count));
51     for(i = 0; i < a->format_count; i ++)
52         for(j = 0; j < b->format_count; j ++)
53             if(a->formats[i] == b->formats[j])
54                 ret->formats[k++] = a->formats[i];
55
56     ret->format_count = k;
57     /* check that there was at least one common format */
58     if(!ret->format_count) {
59         av_free(ret->formats);
60         av_free(ret);
61         return NULL;
62     }
63
64     ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
65
66     merge_ref(ret, a);
67     merge_ref(ret, b);
68
69     return ret;
70 }
71
72 AVFilterFormats *avfilter_make_format_list(const enum PixelFormat *pix_fmts)
73 {
74     AVFilterFormats *formats;
75     int count;
76
77     for (count = 0; pix_fmts[count] != PIX_FMT_NONE; count++)
78         ;
79
80     formats               = av_mallocz(sizeof(AVFilterFormats));
81     formats->formats      = av_malloc(sizeof(*formats->formats) * count);
82     formats->format_count = count;
83     memcpy(formats->formats, pix_fmts, sizeof(*formats->formats) * count);
84
85     return formats;
86 }
87
88 int avfilter_add_colorspace(AVFilterFormats *avff, enum PixelFormat pix_fmt)
89 {
90     enum PixelFormat *pix_fmts =
91         av_realloc(avff->formats, sizeof(avff->formats) * (avff->format_count+1));
92
93     if (!pix_fmts)
94         return AVERROR(ENOMEM);
95
96     avff->formats = pix_fmts;
97     avff->formats[avff->format_count++] = pix_fmt;
98     return 0;
99 }
100
101 AVFilterFormats *avfilter_all_colorspaces(void)
102 {
103     AVFilterFormats *ret;
104     int i;
105
106     ret = av_mallocz(sizeof(AVFilterFormats));
107     ret->formats = av_malloc(sizeof(*ret->formats) * PIX_FMT_NB);
108     ret->format_count = PIX_FMT_NB;
109
110     for(i = 0; i < PIX_FMT_NB; i ++)
111         ret->formats[i] = i;
112
113     return ret;
114 }
115
116 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
117 {
118     *ref = f;
119     f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
120     f->refs[f->refcount-1] = ref;
121 }
122
123 static int find_ref_index(AVFilterFormats **ref)
124 {
125     int i;
126     for(i = 0; i < (*ref)->refcount; i ++)
127         if((*ref)->refs[i] == ref)
128             return i;
129     return -1;
130 }
131
132 void avfilter_formats_unref(AVFilterFormats **ref)
133 {
134     int idx = find_ref_index(ref);
135
136     if(idx >= 0)
137         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
138             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
139
140     if(!--(*ref)->refcount) {
141         av_free((*ref)->formats);
142         av_free((*ref)->refs);
143         av_free(*ref);
144     }
145     *ref = NULL;
146 }
147
148 void avfilter_formats_changeref(AVFilterFormats **oldref,
149                                 AVFilterFormats **newref)
150 {
151     int idx = find_ref_index(oldref);
152
153     if(idx >= 0) {
154         (*oldref)->refs[idx] = newref;
155         *newref = *oldref;
156         *oldref = NULL;
157     }
158 }
159