]> git.sesse.net Git - ffmpeg/blob - libavfilter/formats.c
Merge remote-tracking branch 'qatar/master'
[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     if (a == b) return a;
48
49     ret = av_mallocz(sizeof(AVFilterFormats));
50
51     /* merge list of formats */
52     ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
53                                                            b->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];
58
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);
63         av_free(ret);
64         return NULL;
65     }
66
67     ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
68
69     merge_ref(ret, a);
70     merge_ref(ret, b);
71
72     return ret;
73 }
74
75 AVFilterFormats *avfilter_make_format_list(const int *fmts)
76 {
77     AVFilterFormats *formats;
78     int count = 0;
79
80     if (fmts)
81         for (count = 0; fmts[count] != -1; count++)
82             ;
83
84     formats               = av_mallocz(sizeof(AVFilterFormats));
85     formats->format_count = count;
86     if (count) {
87         formats->formats  = av_malloc(sizeof(*formats->formats) * count);
88         memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
89     }
90
91     return formats;
92 }
93
94 int avfilter_add_format(AVFilterFormats **avff, int fmt)
95 {
96     int *fmts;
97
98     if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
99         return AVERROR(ENOMEM);
100
101     fmts = av_realloc((*avff)->formats,
102                       sizeof((*avff)->formats) * ((*avff)->format_count+1));
103     if (!fmts)
104         return AVERROR(ENOMEM);
105
106     (*avff)->formats = fmts;
107     (*avff)->formats[(*avff)->format_count++] = fmt;
108     return 0;
109 }
110
111 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
112 {
113     AVFilterFormats *ret = NULL;
114     int fmt;
115     int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB    :
116                       type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
117
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);
122
123     return ret;
124 }
125
126 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
127 {
128     *ref = f;
129     f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
130     f->refs[f->refcount-1] = ref;
131 }
132
133 static int find_ref_index(AVFilterFormats **ref)
134 {
135     int i;
136     for(i = 0; i < (*ref)->refcount; i ++)
137         if((*ref)->refs[i] == ref)
138             return i;
139     return -1;
140 }
141
142 void avfilter_formats_unref(AVFilterFormats **ref)
143 {
144     int idx;
145
146     if (!*ref)
147         return;
148
149     idx = find_ref_index(ref);
150
151     if(idx >= 0)
152         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
153             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
154
155     if(!--(*ref)->refcount) {
156         av_free((*ref)->formats);
157         av_free((*ref)->refs);
158         av_free(*ref);
159     }
160     *ref = NULL;
161 }
162
163 void avfilter_formats_changeref(AVFilterFormats **oldref,
164                                 AVFilterFormats **newref)
165 {
166     int idx = find_ref_index(oldref);
167
168     if(idx >= 0) {
169         (*oldref)->refs[idx] = newref;
170         *newref = *oldref;
171         *oldref = NULL;
172     }
173 }
174