00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/pixdesc.h"
00023 #include "libavutil/audioconvert.h"
00024 #include "avfilter.h"
00025
00029 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
00030 {
00031 int i;
00032
00033 for(i = 0; i < a->refcount; i ++) {
00034 ret->refs[ret->refcount] = a->refs[i];
00035 *ret->refs[ret->refcount++] = ret;
00036 }
00037
00038 av_free(a->refs);
00039 av_free(a->formats);
00040 av_free(a);
00041 }
00042
00043 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
00044 {
00045 AVFilterFormats *ret;
00046 unsigned i, j, k = 0;
00047
00048 if (a == b)
00049 return a;
00050
00051 ret = av_mallocz(sizeof(AVFilterFormats));
00052
00053
00054 ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
00055 b->format_count));
00056 for(i = 0; i < a->format_count; i ++)
00057 for(j = 0; j < b->format_count; j ++)
00058 if(a->formats[i] == b->formats[j])
00059 ret->formats[k++] = a->formats[i];
00060
00061 ret->format_count = k;
00062
00063 if(!ret->format_count) {
00064 av_free(ret->formats);
00065 av_free(ret);
00066 return NULL;
00067 }
00068
00069 ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
00070
00071 merge_ref(ret, a);
00072 merge_ref(ret, b);
00073
00074 return ret;
00075 }
00076
00077 #define MAKE_FORMAT_LIST() \
00078 AVFilterFormats *formats; \
00079 int count = 0; \
00080 if (fmts) \
00081 for (count = 0; fmts[count] != -1; count++) \
00082 ; \
00083 formats = av_mallocz(sizeof(AVFilterFormats)); \
00084 if (!formats) return NULL; \
00085 formats->format_count = count; \
00086 if (count) { \
00087 formats->formats = av_malloc(sizeof(*formats->formats)*count); \
00088 if (!formats->formats) { \
00089 av_free(formats); \
00090 return NULL; \
00091 } \
00092 }
00093
00094 AVFilterFormats *avfilter_make_format_list(const int *fmts)
00095 {
00096 MAKE_FORMAT_LIST();
00097 while (count--)
00098 formats->formats[count] = fmts[count];
00099
00100 return formats;
00101 }
00102
00103 AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
00104 {
00105 MAKE_FORMAT_LIST();
00106 if (count)
00107 memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
00108
00109 return formats;
00110 }
00111
00112 int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
00113 {
00114 int64_t *fmts;
00115
00116 if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
00117 return AVERROR(ENOMEM);
00118
00119 fmts = av_realloc((*avff)->formats,
00120 sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
00121 if (!fmts)
00122 return AVERROR(ENOMEM);
00123
00124 (*avff)->formats = fmts;
00125 (*avff)->formats[(*avff)->format_count++] = fmt;
00126 return 0;
00127 }
00128
00129 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
00130 {
00131 AVFilterFormats *ret = NULL;
00132 int fmt;
00133 int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
00134 type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
00135
00136 for (fmt = 0; fmt < num_formats; fmt++)
00137 if ((type != AVMEDIA_TYPE_VIDEO) ||
00138 (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
00139 avfilter_add_format(&ret, fmt);
00140
00141 return ret;
00142 }
00143
00144 AVFilterFormats *avfilter_all_channel_layouts(void)
00145 {
00146 static int64_t chlayouts[] = {
00147 AV_CH_LAYOUT_MONO,
00148 AV_CH_LAYOUT_STEREO,
00149 AV_CH_LAYOUT_4POINT0,
00150 AV_CH_LAYOUT_QUAD,
00151 AV_CH_LAYOUT_5POINT0,
00152 AV_CH_LAYOUT_5POINT0_BACK,
00153 AV_CH_LAYOUT_5POINT1,
00154 AV_CH_LAYOUT_5POINT1_BACK,
00155 AV_CH_LAYOUT_5POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
00156 AV_CH_LAYOUT_7POINT1,
00157 AV_CH_LAYOUT_7POINT1_WIDE,
00158 AV_CH_LAYOUT_7POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
00159 -1,
00160 };
00161
00162 return avfilter_make_format64_list(chlayouts);
00163 }
00164
00165 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
00166 {
00167 *ref = f;
00168 f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
00169 f->refs[f->refcount-1] = ref;
00170 }
00171
00172 static int find_ref_index(AVFilterFormats **ref)
00173 {
00174 int i;
00175 for(i = 0; i < (*ref)->refcount; i ++)
00176 if((*ref)->refs[i] == ref)
00177 return i;
00178 return -1;
00179 }
00180
00181 void avfilter_formats_unref(AVFilterFormats **ref)
00182 {
00183 int idx;
00184
00185 if (!*ref)
00186 return;
00187
00188 idx = find_ref_index(ref);
00189
00190 if(idx >= 0)
00191 memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
00192 sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
00193
00194 if(!--(*ref)->refcount) {
00195 av_free((*ref)->formats);
00196 av_free((*ref)->refs);
00197 av_free(*ref);
00198 }
00199 *ref = NULL;
00200 }
00201
00202 void avfilter_formats_changeref(AVFilterFormats **oldref,
00203 AVFilterFormats **newref)
00204 {
00205 int idx = find_ref_index(oldref);
00206
00207 if(idx >= 0) {
00208 (*oldref)->refs[idx] = newref;
00209 *newref = *oldref;
00210 *oldref = NULL;
00211 }
00212 }
00213