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