2 * Filter layer - format negotiation
3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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"
27 * Add all refs from a to ret and destroy a.
29 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
33 for(i = 0; i < a->refcount; i ++) {
34 ret->refs[ret->refcount] = a->refs[i];
35 *ret->refs[ret->refcount++] = ret;
43 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
46 unsigned i, j, k = 0, m_count;
48 ret = av_mallocz(sizeof(AVFilterFormats));
50 /* merge list of formats */
51 m_count = FFMIN(a->format_count, b->format_count);
53 ret->formats = av_malloc(sizeof(*ret->formats) * m_count);
54 for(i = 0; i < a->format_count; i ++)
55 for(j = 0; j < b->format_count; j ++)
56 if(a->formats[i] == b->formats[j])
57 ret->formats[k++] = a->formats[i];
59 ret->format_count = k;
61 /* check that there was at least one common format */
62 if(!ret->format_count) {
63 av_free(ret->formats);
68 ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
76 int ff_fmt_is_in(int fmt, const int *fmts)
80 for (p = fmts; *p != PIX_FMT_NONE; p++) {
87 AVFilterFormats *avfilter_make_format_list(const int *fmts)
89 AVFilterFormats *formats;
92 for (count = 0; fmts[count] != -1; count++)
95 formats = av_mallocz(sizeof(AVFilterFormats));
97 formats->formats = av_malloc(sizeof(*formats->formats) * count);
98 formats->format_count = count;
99 memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
104 int avfilter_add_format(AVFilterFormats **avff, int fmt)
108 if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
109 return AVERROR(ENOMEM);
111 fmts = av_realloc((*avff)->formats,
112 sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
114 return AVERROR(ENOMEM);
116 (*avff)->formats = fmts;
117 (*avff)->formats[(*avff)->format_count++] = fmt;
121 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
123 AVFilterFormats *ret = NULL;
125 int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
126 type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
128 for (fmt = 0; fmt < num_formats; fmt++)
129 if ((type != AVMEDIA_TYPE_VIDEO) ||
130 (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
131 avfilter_add_format(&ret, fmt);
136 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
139 f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
140 f->refs[f->refcount-1] = ref;
143 static int find_ref_index(AVFilterFormats **ref)
146 for(i = 0; i < (*ref)->refcount; i ++)
147 if((*ref)->refs[i] == ref)
152 void avfilter_formats_unref(AVFilterFormats **ref)
159 idx = find_ref_index(ref);
162 memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
163 sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
165 if(!--(*ref)->refcount) {
166 av_free((*ref)->formats);
167 av_free((*ref)->refs);
173 void avfilter_formats_changeref(AVFilterFormats **oldref,
174 AVFilterFormats **newref)
176 int idx = find_ref_index(oldref);
179 (*oldref)->refs[idx] = newref;