]> git.sesse.net Git - ffmpeg/blob - libavfilter/formats.c
lavfi: handle NULL lists in avfilter_make_format_list
[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 "libavutil/pixdesc.h"
23 #include "avfilter.h"
24
25 /**
26  * Add all refs from a to ret and destroy a.
27  */
28 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
29 {
30     int i;
31
32     for(i = 0; i < a->refcount; i ++) {
33         ret->refs[ret->refcount] = a->refs[i];
34         *ret->refs[ret->refcount++] = ret;
35     }
36
37     av_free(a->refs);
38     av_free(a->formats);
39     av_free(a);
40 }
41
42 AVFilterFormats *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 */
50     ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
51                                                            b->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];
56
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);
61         av_free(ret);
62         return NULL;
63     }
64
65     ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
66
67     merge_ref(ret, a);
68     merge_ref(ret, b);
69
70     return ret;
71 }
72
73 AVFilterFormats *avfilter_make_format_list(const int *fmts)
74 {
75     AVFilterFormats *formats;
76     int count = 0;
77
78     if (fmts)
79         for (count = 0; fmts[count] != -1; count++)
80             ;
81
82     formats               = av_mallocz(sizeof(AVFilterFormats));
83     formats->format_count = count;
84     if (count) {
85         formats->formats  = av_malloc(sizeof(*formats->formats) * count);
86         memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
87     }
88
89     return formats;
90 }
91
92 int avfilter_add_format(AVFilterFormats **avff, int fmt)
93 {
94     int *fmts;
95
96     if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
97         return AVERROR(ENOMEM);
98
99     fmts = av_realloc((*avff)->formats,
100                       sizeof((*avff)->formats) * ((*avff)->format_count+1));
101     if (!fmts)
102         return AVERROR(ENOMEM);
103
104     (*avff)->formats = fmts;
105     (*avff)->formats[(*avff)->format_count++] = fmt;
106     return 0;
107 }
108
109 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
110 {
111     AVFilterFormats *ret = NULL;
112     int fmt;
113     int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB    :
114                       type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
115
116     for (fmt = 0; fmt < num_formats; fmt++)
117         if ((type != AVMEDIA_TYPE_VIDEO) ||
118             (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
119             avfilter_add_format(&ret, fmt);
120
121     return ret;
122 }
123
124 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
125 {
126     *ref = f;
127     f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
128     f->refs[f->refcount-1] = ref;
129 }
130
131 static int find_ref_index(AVFilterFormats **ref)
132 {
133     int i;
134     for(i = 0; i < (*ref)->refcount; i ++)
135         if((*ref)->refs[i] == ref)
136             return i;
137     return -1;
138 }
139
140 void avfilter_formats_unref(AVFilterFormats **ref)
141 {
142     int idx;
143
144     if (!*ref)
145         return;
146
147     idx = find_ref_index(ref);
148
149     if(idx >= 0)
150         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
151             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
152
153     if(!--(*ref)->refcount) {
154         av_free((*ref)->formats);
155         av_free((*ref)->refs);
156         av_free(*ref);
157     }
158     *ref = NULL;
159 }
160
161 void avfilter_formats_changeref(AVFilterFormats **oldref,
162                                 AVFilterFormats **newref)
163 {
164     int idx = find_ref_index(oldref);
165
166     if(idx >= 0) {
167         (*oldref)->refs[idx] = newref;
168         *newref = *oldref;
169         *oldref = NULL;
170     }
171 }
172