2 * Filter layer - format negotiation
3 * copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/pixdesc.h"
26 * Add all refs from a to ret and destroy a.
28 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
32 for(i = 0; i < a->refcount; i ++) {
33 ret->refs[ret->refcount] = a->refs[i];
34 *ret->refs[ret->refcount++] = ret;
42 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
47 ret = av_mallocz(sizeof(AVFilterFormats));
49 /* merge list of formats */
50 ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
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];
57 ret->format_count = k;
58 /* check that there was at least one common format */
59 if(!ret->format_count) {
60 av_free(ret->formats);
65 ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
73 AVFilterFormats *avfilter_make_format_list(const enum PixelFormat *pix_fmts)
75 AVFilterFormats *formats;
78 for (count = 0; pix_fmts[count] != PIX_FMT_NONE; count++)
81 formats = av_mallocz(sizeof(AVFilterFormats));
82 formats->formats = av_malloc(sizeof(*formats->formats) * count);
83 formats->format_count = count;
84 memcpy(formats->formats, pix_fmts, sizeof(*formats->formats) * count);
89 int avfilter_add_colorspace(AVFilterFormats **avff, enum PixelFormat pix_fmt)
91 enum PixelFormat *pix_fmts;
93 if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
94 return AVERROR(ENOMEM);
96 pix_fmts = av_realloc((*avff)->formats,
97 sizeof((*avff)->formats) * ((*avff)->format_count+1));
99 return AVERROR(ENOMEM);
101 (*avff)->formats = pix_fmts;
102 (*avff)->formats[(*avff)->format_count++] = pix_fmt;
106 AVFilterFormats *avfilter_all_colorspaces(void)
108 AVFilterFormats *ret = NULL;
109 enum PixelFormat pix_fmt;
111 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
112 if (!(av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL))
113 avfilter_add_colorspace(&ret, pix_fmt);
118 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
121 f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
122 f->refs[f->refcount-1] = ref;
125 static int find_ref_index(AVFilterFormats **ref)
128 for(i = 0; i < (*ref)->refcount; i ++)
129 if((*ref)->refs[i] == ref)
134 void avfilter_formats_unref(AVFilterFormats **ref)
139 int idx = find_ref_index(ref);
142 memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
143 sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
145 if(!--(*ref)->refcount) {
146 av_free((*ref)->formats);
147 av_free((*ref)->refs);
153 void avfilter_formats_changeref(AVFilterFormats **oldref,
154 AVFilterFormats **newref)
156 int idx = find_ref_index(oldref);
159 (*oldref)->refs[idx] = newref;