More accurate value for log2(10)
[libav.git] / libavfilter / formats.c
CommitLineData
39135465
VS
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
5ff84183 22#include "libavutil/pixdesc.h"
39135465
VS
23#include "avfilter.h"
24
4c4de9ca
MN
25/**
26 * Add all refs from a to ret and destroy a.
27 */
37e0b997
VS
28static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
29{
30 int i;
93faa9fa 31
37e0b997
VS
32 for(i = 0; i < a->refcount; i ++) {
33 ret->refs[ret->refcount] = a->refs[i];
34 *ret->refs[ret->refcount++] = ret;
35 }
93faa9fa
VS
36
37 av_free(a->refs);
38 av_free(a->formats);
39 av_free(a);
37e0b997
VS
40}
41
39135465
VS
42AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
43{
44 AVFilterFormats *ret;
45 unsigned i, j, k = 0;
46
47 ret = av_mallocz(sizeof(AVFilterFormats));
48
49 /* merge list of formats */
243370cb
VS
50 ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
51 b->format_count));
39135465
VS
52 for(i = 0; i < a->format_count; i ++)
53 for(j = 0; j < b->format_count; j ++)
54 if(a->formats[i] == b->formats[j])
55 ret->formats[k++] = a->formats[i];
56
9189411b 57 ret->format_count = k;
39135465 58 /* check that there was at least one common format */
9189411b 59 if(!ret->format_count) {
39135465
VS
60 av_free(ret->formats);
61 av_free(ret);
62 return NULL;
63 }
64
39135465 65 ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
37e0b997
VS
66
67 merge_ref(ret, a);
68 merge_ref(ret, b);
39135465 69
39135465
VS
70 return ret;
71}
72
9abba21a 73AVFilterFormats *avfilter_make_format_list(const enum PixelFormat *pix_fmts)
f6a1fa85
SS
74{
75 AVFilterFormats *formats;
76 int count;
77
9abba21a 78 for (count = 0; pix_fmts[count] != PIX_FMT_NONE; count++)
f6a1fa85
SS
79 ;
80
81 formats = av_mallocz(sizeof(AVFilterFormats));
82 formats->formats = av_malloc(sizeof(*formats->formats) * count);
83 formats->format_count = count;
9abba21a 84 memcpy(formats->formats, pix_fmts, sizeof(*formats->formats) * count);
f6a1fa85
SS
85
86 return formats;
87}
88
c1d662fd 89int avfilter_add_colorspace(AVFilterFormats **avff, enum PixelFormat pix_fmt)
4fd1f187 90{
c1d662fd 91 enum PixelFormat *pix_fmts;
4fd1f187 92
c1d662fd
SS
93 if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
94 return AVERROR(ENOMEM);
95
96 pix_fmts = av_realloc((*avff)->formats,
97 sizeof((*avff)->formats) * ((*avff)->format_count+1));
4fd1f187
SS
98 if (!pix_fmts)
99 return AVERROR(ENOMEM);
100
c1d662fd
SS
101 (*avff)->formats = pix_fmts;
102 (*avff)->formats[(*avff)->format_count++] = pix_fmt;
4fd1f187
SS
103 return 0;
104}
105
39135465
VS
106AVFilterFormats *avfilter_all_colorspaces(void)
107{
c1d662fd 108 AVFilterFormats *ret = NULL;
5ff84183 109 enum PixelFormat pix_fmt;
d3a4e41c 110
5ff84183
SS
111 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
112 if (!(av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL))
c1d662fd 113 avfilter_add_colorspace(&ret, pix_fmt);
d3a4e41c
VS
114
115 return ret;
39135465
VS
116}
117
118void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
119{
120 *ref = f;
121 f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
122 f->refs[f->refcount-1] = ref;
123}
124
30f4baeb 125static int find_ref_index(AVFilterFormats **ref)
88cfb804
VS
126{
127 int i;
128 for(i = 0; i < (*ref)->refcount; i ++)
129 if((*ref)->refs[i] == ref)
130 return i;
131 return -1;
132}
133
39135465
VS
134void avfilter_formats_unref(AVFilterFormats **ref)
135{
d3c01751
SS
136 int idx;
137
063e7692
SS
138 if (!*ref)
139 return;
140
d3c01751 141 idx = find_ref_index(ref);
88cfb804 142
9189411b 143 if(idx >= 0)
88cfb804
VS
144 memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
145 sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
146
39135465
VS
147 if(!--(*ref)->refcount) {
148 av_free((*ref)->formats);
149 av_free((*ref)->refs);
150 av_free(*ref);
151 }
152 *ref = NULL;
153}
154
eac24950
VS
155void avfilter_formats_changeref(AVFilterFormats **oldref,
156 AVFilterFormats **newref)
157{
9189411b 158 int idx = find_ref_index(oldref);
eac24950 159
9189411b 160 if(idx >= 0) {
eac24950
VS
161 (*oldref)->refs[idx] = newref;
162 *newref = *oldref;
163 *oldref = NULL;
164 }
165}
166