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)
49 ret = av_mallocz(sizeof(AVFilterFormats));
51 /* merge list of formats */
52 ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_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;
60 /* check that there was at least one common format */
61 if(!ret->format_count) {
62 av_free(ret->formats);
67 ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
75 AVFilterFormats *avfilter_make_format_list(const int *fmts)
77 AVFilterFormats *formats;
81 for (count = 0; fmts[count] != -1; count++)
84 formats = av_mallocz(sizeof(AVFilterFormats));
85 formats->format_count = count;
87 formats->formats = av_malloc(sizeof(*formats->formats) * count);
88 memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
94 int avfilter_add_format(AVFilterFormats **avff, int fmt)
98 if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
99 return AVERROR(ENOMEM);
101 fmts = av_realloc((*avff)->formats,
102 sizeof((*avff)->formats) * ((*avff)->format_count+1));
104 return AVERROR(ENOMEM);
106 (*avff)->formats = fmts;
107 (*avff)->formats[(*avff)->format_count++] = fmt;
111 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
113 AVFilterFormats *ret = NULL;
115 int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
116 type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
118 for (fmt = 0; fmt < num_formats; fmt++)
119 if ((type != AVMEDIA_TYPE_VIDEO) ||
120 (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
121 avfilter_add_format(&ret, fmt);
126 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
129 f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
130 f->refs[f->refcount-1] = ref;
133 static int find_ref_index(AVFilterFormats **ref)
136 for(i = 0; i < (*ref)->refcount; i ++)
137 if((*ref)->refs[i] == ref)
142 void avfilter_formats_unref(AVFilterFormats **ref)
149 idx = find_ref_index(ref);
152 memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
153 sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
155 if(!--(*ref)->refcount) {
156 av_free((*ref)->formats);
157 av_free((*ref)->refs);
163 void avfilter_formats_changeref(AVFilterFormats **oldref,
164 AVFilterFormats **newref)
166 int idx = find_ref_index(oldref);
169 (*oldref)->refs[idx] = newref;