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

Generated on Fri Feb 22 2013 07:24:30 for FFmpeg by  doxygen 1.7.1