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 "avfilter.h"
00024
00028 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
00029 {
00030 int i;
00031
00032 for(i = 0; i < a->refcount; i ++) {
00033 ret->refs[ret->refcount] = a->refs[i];
00034 *ret->refs[ret->refcount++] = ret;
00035 }
00036
00037 av_free(a->refs);
00038 av_free(a->formats);
00039 av_free(a);
00040 }
00041
00042 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
00043 {
00044 AVFilterFormats *ret;
00045 unsigned i, j, k = 0;
00046
00047 if (a == b)
00048 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 AVFilterFormats *avfilter_make_format_list(const enum PixelFormat *pix_fmts)
00077 {
00078 AVFilterFormats *formats;
00079 int count;
00080
00081 for (count = 0; pix_fmts[count] != PIX_FMT_NONE; count++)
00082 ;
00083
00084 formats = av_mallocz(sizeof(AVFilterFormats));
00085 formats->formats = av_malloc(sizeof(*formats->formats) * count);
00086 formats->format_count = count;
00087 memcpy(formats->formats, pix_fmts, sizeof(*formats->formats) * count);
00088
00089 return formats;
00090 }
00091
00092 int avfilter_add_colorspace(AVFilterFormats **avff, enum PixelFormat pix_fmt)
00093 {
00094 enum PixelFormat *pix_fmts;
00095
00096 if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
00097 return AVERROR(ENOMEM);
00098
00099 pix_fmts = av_realloc((*avff)->formats,
00100 sizeof((*avff)->formats) * ((*avff)->format_count+1));
00101 if (!pix_fmts)
00102 return AVERROR(ENOMEM);
00103
00104 (*avff)->formats = pix_fmts;
00105 (*avff)->formats[(*avff)->format_count++] = pix_fmt;
00106 return 0;
00107 }
00108
00109 AVFilterFormats *avfilter_all_colorspaces(void)
00110 {
00111 AVFilterFormats *ret = NULL;
00112 enum PixelFormat pix_fmt;
00113
00114 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
00115 if (!(av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL))
00116 avfilter_add_colorspace(&ret, pix_fmt);
00117
00118 return ret;
00119 }
00120
00121 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
00122 {
00123 *ref = f;
00124 f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
00125 f->refs[f->refcount-1] = ref;
00126 }
00127
00128 static int find_ref_index(AVFilterFormats **ref)
00129 {
00130 int i;
00131 for(i = 0; i < (*ref)->refcount; i ++)
00132 if((*ref)->refs[i] == ref)
00133 return i;
00134 return -1;
00135 }
00136
00137 void avfilter_formats_unref(AVFilterFormats **ref)
00138 {
00139 int idx;
00140
00141 if (!*ref)
00142 return;
00143
00144 idx = find_ref_index(ref);
00145
00146 if(idx >= 0)
00147 memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
00148 sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
00149
00150 if(!--(*ref)->refcount) {
00151 av_free((*ref)->formats);
00152 av_free((*ref)->refs);
00153 av_free(*ref);
00154 }
00155 *ref = NULL;
00156 }
00157
00158 void avfilter_formats_changeref(AVFilterFormats **oldref,
00159 AVFilterFormats **newref)
00160 {
00161 int idx = find_ref_index(oldref);
00162
00163 if(idx >= 0) {
00164 (*oldref)->refs[idx] = newref;
00165 *newref = *oldref;
00166 *oldref = NULL;
00167 }
00168 }
00169