]> 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 "libavutil/audioconvert.h"
24 #include "avfilter.h"
25
26 /**
27  * Add all refs from a to ret and destroy a.
28  */
29 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
30 {
31     int i;
32
33     for (i = 0; i < a->refcount; i++) {
34         ret->refs[ret->refcount] = a->refs[i];
35         *ret->refs[ret->refcount++] = ret;
36     }
37
38     av_free(a->refs);
39     av_free(a->formats);
40     av_free(a);
41 }
42
43 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
44 {
45     AVFilterFormats *ret;
46     unsigned i, j, k = 0;
47
48     if (a == b) return a;
49
50     ret = av_mallocz(sizeof(AVFilterFormats));
51
52     /* merge list of formats */
53     ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
54                                                            b->format_count));
55     for (i = 0; i < a->format_count; i++)
56         for (j = 0; j < b->format_count; j++)
57             if (a->formats[i] == b->formats[j])
58                 ret->formats[k++] = a->formats[i];
59
60     ret->format_count = k;
61     /* check that there was at least one common format */
62     if (!ret->format_count) {
63         av_free(ret->formats);
64         av_free(ret);
65         return NULL;
66     }
67
68     ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
69
70     merge_ref(ret, a);
71     merge_ref(ret, b);
72
73     return ret;
74 }
75
76 #define MAKE_FORMAT_LIST()                                              \
77     AVFilterFormats *formats;                                           \
78     int count = 0;                                                      \
79     if (fmts)                                                           \
80         for (count = 0; fmts[count] != -1; count++)                     \
81             ;                                                           \
82     formats = av_mallocz(sizeof(AVFilterFormats));                      \
83     if (!formats) return NULL;                                          \
84     formats->format_count = count;                                      \
85     if (count) {                                                        \
86         formats->formats = av_malloc(sizeof(*formats->formats)*count);  \
87         if (!formats->formats) {                                        \
88             av_free(formats);                                           \
89             return NULL;                                                \
90         }                                                               \
91     }
92
93 AVFilterFormats *avfilter_make_format_list(const int *fmts)
94 {
95     MAKE_FORMAT_LIST();
96     while (count--)
97         formats->formats[count] = fmts[count];
98
99     return formats;
100 }
101
102 AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
103 {
104     MAKE_FORMAT_LIST();
105     if (count)
106         memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
107
108     return formats;
109 }
110
111 int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
112 {
113     int64_t *fmts;
114
115     if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
116         return AVERROR(ENOMEM);
117
118     fmts = av_realloc((*avff)->formats,
119                       sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
120     if (!fmts)
121         return AVERROR(ENOMEM);
122
123     (*avff)->formats = fmts;
124     (*avff)->formats[(*avff)->format_count++] = fmt;
125     return 0;
126 }
127
128 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
129 {
130     AVFilterFormats *ret = NULL;
131     int fmt;
132     int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB    :
133                       type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
134
135     for (fmt = 0; fmt < num_formats; fmt++)
136         if ((type != AVMEDIA_TYPE_VIDEO) ||
137             (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
138             avfilter_add_format(&ret, fmt);
139
140     return ret;
141 }
142
143 AVFilterFormats *avfilter_all_channel_layouts(void)
144 {
145     static int64_t chlayouts[] = {
146         AV_CH_LAYOUT_MONO,
147         AV_CH_LAYOUT_STEREO,
148         AV_CH_LAYOUT_4POINT0,
149         AV_CH_LAYOUT_QUAD,
150         AV_CH_LAYOUT_5POINT0,
151         AV_CH_LAYOUT_5POINT0_BACK,
152         AV_CH_LAYOUT_5POINT1,
153         AV_CH_LAYOUT_5POINT1_BACK,
154         AV_CH_LAYOUT_5POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
155         AV_CH_LAYOUT_7POINT1,
156         AV_CH_LAYOUT_7POINT1_WIDE,
157         AV_CH_LAYOUT_7POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
158         -1,
159     };
160
161     return avfilter_make_format64_list(chlayouts);
162 }
163
164 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
165 {
166     *ref = f;
167     f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
168     f->refs[f->refcount-1] = ref;
169 }
170
171 static int find_ref_index(AVFilterFormats **ref)
172 {
173     int i;
174     for (i = 0; i < (*ref)->refcount; i++)
175         if ((*ref)->refs[i] == ref)
176             return i;
177     return -1;
178 }
179
180 void avfilter_formats_unref(AVFilterFormats **ref)
181 {
182     int idx;
183
184     if (!*ref)
185         return;
186
187     idx = find_ref_index(ref);
188
189     if (idx >= 0)
190         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
191             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
192
193     if (!--(*ref)->refcount) {
194         av_free((*ref)->formats);
195         av_free((*ref)->refs);
196         av_free(*ref);
197     }
198     *ref = NULL;
199 }
200
201 void avfilter_formats_changeref(AVFilterFormats **oldref,
202                                 AVFilterFormats **newref)
203 {
204     int idx = find_ref_index(oldref);
205
206     if (idx >= 0) {
207         (*oldref)->refs[idx] = newref;
208         *newref = *oldref;
209         *oldref = NULL;
210     }
211 }
212