• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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     if (a == b)
00049         return a;
00050 
00051     ret = av_mallocz(sizeof(AVFilterFormats));
00052 
00053     /* merge list of formats */
00054     m_count = FFMIN(a->format_count, b->format_count);
00055     if (m_count) {
00056         ret->formats = av_malloc(sizeof(*ret->formats) * m_count);
00057         for(i = 0; i < a->format_count; i ++)
00058             for(j = 0; j < b->format_count; j ++)
00059                 if(a->formats[i] == b->formats[j])
00060                     ret->formats[k++] = a->formats[i];
00061 
00062         ret->format_count = k;
00063     }
00064     /* check that there was at least one common format */
00065     if(!ret->format_count) {
00066         av_free(ret->formats);
00067         av_free(ret);
00068         return NULL;
00069     }
00070 
00071     ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
00072 
00073     merge_ref(ret, a);
00074     merge_ref(ret, b);
00075 
00076     return ret;
00077 }
00078 
00079 int ff_fmt_is_in(int fmt, const int *fmts)
00080 {
00081     const int *p;
00082 
00083     for (p = fmts; *p != PIX_FMT_NONE; p++) {
00084         if (fmt == *p)
00085             return 1;
00086     }
00087     return 0;
00088 }
00089 
00090 AVFilterFormats *avfilter_make_format_list(const int *fmts)
00091 {
00092     AVFilterFormats *formats;
00093     int count;
00094 
00095     for (count = 0; fmts[count] != -1; count++)
00096         ;
00097 
00098     formats               = av_mallocz(sizeof(AVFilterFormats));
00099     if (count)
00100         formats->formats  = av_malloc(sizeof(*formats->formats) * count);
00101     formats->format_count = count;
00102     memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
00103 
00104     return formats;
00105 }
00106 
00107 int avfilter_add_format(AVFilterFormats **avff, int fmt)
00108 {
00109     int *fmts;
00110 
00111     if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
00112         return AVERROR(ENOMEM);
00113 
00114     fmts = av_realloc((*avff)->formats,
00115                       sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
00116     if (!fmts)
00117         return AVERROR(ENOMEM);
00118 
00119     (*avff)->formats = fmts;
00120     (*avff)->formats[(*avff)->format_count++] = fmt;
00121     return 0;
00122 }
00123 
00124 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
00125 {
00126     AVFilterFormats *ret = NULL;
00127     int fmt;
00128     int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB    :
00129                       type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
00130 
00131     for (fmt = 0; fmt < num_formats; fmt++)
00132         if ((type != AVMEDIA_TYPE_VIDEO) ||
00133             (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
00134             avfilter_add_format(&ret, fmt);
00135 
00136     return ret;
00137 }
00138 
00139 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
00140 {
00141     *ref = f;
00142     f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
00143     f->refs[f->refcount-1] = ref;
00144 }
00145 
00146 static int find_ref_index(AVFilterFormats **ref)
00147 {
00148     int i;
00149     for(i = 0; i < (*ref)->refcount; i ++)
00150         if((*ref)->refs[i] == ref)
00151             return i;
00152     return -1;
00153 }
00154 
00155 void avfilter_formats_unref(AVFilterFormats **ref)
00156 {
00157     int idx;
00158 
00159     if (!*ref)
00160         return;
00161 
00162     idx = find_ref_index(ref);
00163 
00164     if(idx >= 0)
00165         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
00166             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
00167 
00168     if(!--(*ref)->refcount) {
00169         av_free((*ref)->formats);
00170         av_free((*ref)->refs);
00171         av_free(*ref);
00172     }
00173     *ref = NULL;
00174 }
00175 
00176 void avfilter_formats_changeref(AVFilterFormats **oldref,
00177                                 AVFilterFormats **newref)
00178 {
00179     int idx = find_ref_index(oldref);
00180 
00181     if(idx >= 0) {
00182         (*oldref)->refs[idx] = newref;
00183         *newref = *oldref;
00184         *oldref = NULL;
00185     }
00186 }
00187 
Generated on Thu Jan 24 2013 17:08:54 for Libav by doxygen 1.7.1