libavfilter/formats.c
39135465
 /*
  * Filter layer - format negotiation
3fa77bde
  * Copyright (c) 2007 Bobby Bingham
39135465
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
5ff84183
 #include "libavutil/pixdesc.h"
fd2c0a5d
 #include "libavutil/audioconvert.h"
39135465
 #include "avfilter.h"
 
4c4de9ca
 /**
  * Add all refs from a to ret and destroy a.
  */
37e0b997
 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
 {
     int i;
93faa9fa
 
37e0b997
     for(i = 0; i < a->refcount; i ++) {
         ret->refs[ret->refcount] = a->refs[i];
         *ret->refs[ret->refcount++] = ret;
     }
93faa9fa
 
     av_free(a->refs);
     av_free(a->formats);
     av_free(a);
37e0b997
 }
 
39135465
 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
 {
     AVFilterFormats *ret;
     unsigned i, j, k = 0;
 
79a0ec1a
     if (a == b) return a;
 
39135465
     ret = av_mallocz(sizeof(AVFilterFormats));
 
     /* merge list of formats */
243370cb
     ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
                                                            b->format_count));
39135465
     for(i = 0; i < a->format_count; i ++)
         for(j = 0; j < b->format_count; j ++)
             if(a->formats[i] == b->formats[j])
                 ret->formats[k++] = a->formats[i];
 
9189411b
     ret->format_count = k;
39135465
     /* check that there was at least one common format */
9189411b
     if(!ret->format_count) {
39135465
         av_free(ret->formats);
         av_free(ret);
         return NULL;
     }
 
     ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
37e0b997
 
     merge_ref(ret, a);
     merge_ref(ret, b);
39135465
 
     return ret;
 }
 
527ca398
 #define MAKE_FORMAT_LIST()                                              \
     AVFilterFormats *formats;                                           \
     int count = 0;                                                      \
     if (fmts)                                                           \
         for (count = 0; fmts[count] != -1; count++)                     \
             ;                                                           \
     formats = av_mallocz(sizeof(AVFilterFormats));                      \
     if (!formats) return NULL;                                          \
     formats->format_count = count;                                      \
     if (count) {                                                        \
         formats->formats  = av_malloc(sizeof(*formats->formats)*count); \
         if (!formats->formats) {                                        \
             av_free(formats);                                           \
             return NULL;                                                \
         }                                                               \
     }
 
bdab614b
 AVFilterFormats *avfilter_make_format_list(const int *fmts)
f6a1fa85
 {
527ca398
     MAKE_FORMAT_LIST();
     while (count--)
         formats->formats[count] = fmts[count];
f6a1fa85
 
527ca398
     return formats;
 }
f6a1fa85
 
527ca398
 AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
 {
     MAKE_FORMAT_LIST();
     if (count)
47d2ca32
         memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
f6a1fa85
 
     return formats;
 }
 
527ca398
 int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
4fd1f187
 {
527ca398
     int64_t *fmts;
4fd1f187
 
c1d662fd
     if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
         return AVERROR(ENOMEM);
 
bdab614b
     fmts = av_realloc((*avff)->formats,
8f349b64
                       sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
bdab614b
     if (!fmts)
4fd1f187
         return AVERROR(ENOMEM);
 
bdab614b
     (*avff)->formats = fmts;
     (*avff)->formats[(*avff)->format_count++] = fmt;
4fd1f187
     return 0;
 }
 
bdab614b
 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
39135465
 {
c1d662fd
     AVFilterFormats *ret = NULL;
bdab614b
     int fmt;
ad2c9501
     int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB    :
5d6e4c16
                       type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
d3a4e41c
 
bdab614b
     for (fmt = 0; fmt < num_formats; fmt++)
         if ((type != AVMEDIA_TYPE_VIDEO) ||
             (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
             avfilter_add_format(&ret, fmt);
d3a4e41c
 
     return ret;
39135465
 }
 
fd2c0a5d
 AVFilterFormats *avfilter_all_channel_layouts(void)
 {
     static int64_t chlayouts[] = {
         AV_CH_LAYOUT_MONO,
         AV_CH_LAYOUT_STEREO,
         AV_CH_LAYOUT_4POINT0,
         AV_CH_LAYOUT_QUAD,
         AV_CH_LAYOUT_5POINT0,
         AV_CH_LAYOUT_5POINT0_BACK,
         AV_CH_LAYOUT_5POINT1,
         AV_CH_LAYOUT_5POINT1_BACK,
         AV_CH_LAYOUT_5POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
         AV_CH_LAYOUT_7POINT1,
         AV_CH_LAYOUT_7POINT1_WIDE,
         AV_CH_LAYOUT_7POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
         -1,
     };
 
     return avfilter_make_format64_list(chlayouts);
 }
 
39135465
 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
 {
     *ref = f;
     f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
     f->refs[f->refcount-1] = ref;
 }
 
30f4baeb
 static int find_ref_index(AVFilterFormats **ref)
88cfb804
 {
     int i;
     for(i = 0; i < (*ref)->refcount; i ++)
         if((*ref)->refs[i] == ref)
             return i;
     return -1;
 }
 
39135465
 void avfilter_formats_unref(AVFilterFormats **ref)
 {
d3c01751
     int idx;
 
063e7692
     if (!*ref)
         return;
 
d3c01751
     idx = find_ref_index(ref);
88cfb804
 
9189411b
     if(idx >= 0)
88cfb804
         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
 
39135465
     if(!--(*ref)->refcount) {
         av_free((*ref)->formats);
         av_free((*ref)->refs);
         av_free(*ref);
     }
     *ref = NULL;
 }
 
eac24950
 void avfilter_formats_changeref(AVFilterFormats **oldref,
                                 AVFilterFormats **newref)
 {
9189411b
     int idx = find_ref_index(oldref);
eac24950
 
9189411b
     if(idx >= 0) {
eac24950
         (*oldref)->refs[idx] = newref;
         *newref = *oldref;
         *oldref = NULL;
     }
 }