libavfilter/formats.c
Go to the documentation of this file.
00001 /*
00002  * Filter layer - format negotiation
00003  * Copyright (c) 2007 Bobby Bingham
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/pixdesc.h"
00023 #include "avfilter.h"
00024 #include "internal.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, m_count;
00047 
00048     ret = av_mallocz(sizeof(AVFilterFormats));
00049 
00050     /* merge list of formats */
00051     m_count = FFMIN(a->format_count, b->format_count);
00052     if (m_count) {
00053         ret->formats = av_malloc(sizeof(*ret->formats) * m_count);
00054         for(i = 0; i < a->format_count; i ++)
00055             for(j = 0; j < b->format_count; j ++)
00056                 if(a->formats[i] == b->formats[j])
00057                     ret->formats[k++] = a->formats[i];
00058 
00059         ret->format_count = k;
00060     }
00061     /* check that there was at least one common format */
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 int ff_fmt_is_in(int fmt, const int *fmts)
00077 {
00078     const int *p;
00079 
00080     for (p = fmts; *p != PIX_FMT_NONE; p++) {
00081         if (fmt == *p)
00082             return 1;
00083     }
00084     return 0;
00085 }
00086 
00087 AVFilterFormats *avfilter_make_format_list(const int *fmts)
00088 {
00089     AVFilterFormats *formats;
00090     int count;
00091 
00092     for (count = 0; fmts[count] != -1; count++)
00093         ;
00094 
00095     formats               = av_mallocz(sizeof(AVFilterFormats));
00096     if (count)
00097         formats->formats  = av_malloc(sizeof(*formats->formats) * count);
00098     formats->format_count = count;
00099     memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
00100 
00101     return formats;
00102 }
00103 
00104 int avfilter_add_format(AVFilterFormats **avff, int fmt)
00105 {
00106     int *fmts;
00107 
00108     if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
00109         return AVERROR(ENOMEM);
00110 
00111     fmts = av_realloc((*avff)->formats,
00112                       sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
00113     if (!fmts)
00114         return AVERROR(ENOMEM);
00115 
00116     (*avff)->formats = fmts;
00117     (*avff)->formats[(*avff)->format_count++] = fmt;
00118     return 0;
00119 }
00120 
00121 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
00122 {
00123     AVFilterFormats *ret = NULL;
00124     int fmt;
00125     int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB    :
00126                       type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
00127 
00128     for (fmt = 0; fmt < num_formats; fmt++)
00129         if ((type != AVMEDIA_TYPE_VIDEO) ||
00130             (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
00131             avfilter_add_format(&ret, fmt);
00132 
00133     return ret;
00134 }
00135 
00136 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
00137 {
00138     *ref = f;
00139     f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
00140     f->refs[f->refcount-1] = ref;
00141 }
00142 
00143 static int find_ref_index(AVFilterFormats **ref)
00144 {
00145     int i;
00146     for(i = 0; i < (*ref)->refcount; i ++)
00147         if((*ref)->refs[i] == ref)
00148             return i;
00149     return -1;
00150 }
00151 
00152 void avfilter_formats_unref(AVFilterFormats **ref)
00153 {
00154     int idx;
00155 
00156     if (!*ref)
00157         return;
00158 
00159     idx = find_ref_index(ref);
00160 
00161     if(idx >= 0)
00162         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
00163             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
00164 
00165     if(!--(*ref)->refcount) {
00166         av_free((*ref)->formats);
00167         av_free((*ref)->refs);
00168         av_free(*ref);
00169     }
00170     *ref = NULL;
00171 }
00172 
00173 void avfilter_formats_changeref(AVFilterFormats **oldref,
00174                                 AVFilterFormats **newref)
00175 {
00176     int idx = find_ref_index(oldref);
00177 
00178     if(idx >= 0) {
00179         (*oldref)->refs[idx] = newref;
00180         *newref = *oldref;
00181         *oldref = NULL;
00182     }
00183 }
00184