2008-02-10 20:02:11 +02:00
|
|
|
/*
|
|
|
|
* Filter layer - format negotiation
|
|
|
|
* copyright (c) 2007 Bobby Bingham
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2010-01-04 02:08:11 +02:00
|
|
|
#include "libavutil/pixdesc.h"
|
2008-02-10 20:02:11 +02:00
|
|
|
#include "avfilter.h"
|
|
|
|
|
2008-02-22 00:10:11 +02:00
|
|
|
/**
|
|
|
|
* Add all refs from a to ret and destroy a.
|
|
|
|
*/
|
2008-02-10 20:04:52 +02:00
|
|
|
static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
|
|
|
|
{
|
|
|
|
int i;
|
2008-02-10 20:04:55 +02:00
|
|
|
|
2008-02-10 20:04:52 +02:00
|
|
|
for(i = 0; i < a->refcount; i ++) {
|
|
|
|
ret->refs[ret->refcount] = a->refs[i];
|
|
|
|
*ret->refs[ret->refcount++] = ret;
|
|
|
|
}
|
2008-02-10 20:04:55 +02:00
|
|
|
|
|
|
|
av_free(a->refs);
|
|
|
|
av_free(a->formats);
|
|
|
|
av_free(a);
|
2008-02-10 20:04:52 +02:00
|
|
|
}
|
|
|
|
|
2008-02-10 20:02:11 +02:00
|
|
|
AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
|
|
|
|
{
|
|
|
|
AVFilterFormats *ret;
|
|
|
|
unsigned i, j, k = 0;
|
|
|
|
|
|
|
|
ret = av_mallocz(sizeof(AVFilterFormats));
|
|
|
|
|
|
|
|
/* merge list of formats */
|
2008-02-10 20:04:43 +02:00
|
|
|
ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
|
|
|
|
b->format_count));
|
2008-02-10 20:02:11 +02:00
|
|
|
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];
|
|
|
|
|
2008-02-10 20:05:02 +02:00
|
|
|
ret->format_count = k;
|
2008-02-10 20:02:11 +02:00
|
|
|
/* check that there was at least one common format */
|
2008-02-10 20:05:02 +02:00
|
|
|
if(!ret->format_count) {
|
2008-02-10 20:02:11 +02:00
|
|
|
av_free(ret->formats);
|
|
|
|
av_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
|
2008-02-10 20:04:52 +02:00
|
|
|
|
|
|
|
merge_ref(ret, a);
|
|
|
|
merge_ref(ret, b);
|
2008-02-10 20:02:11 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-07-22 14:12:47 +03:00
|
|
|
AVFilterFormats *avfilter_make_format_list(const int *fmts)
|
2009-10-19 02:06:52 +03:00
|
|
|
{
|
|
|
|
AVFilterFormats *formats;
|
|
|
|
int count;
|
|
|
|
|
2010-07-22 14:12:47 +03:00
|
|
|
for (count = 0; fmts[count] != -1; count++)
|
2009-10-19 02:06:52 +03:00
|
|
|
;
|
|
|
|
|
|
|
|
formats = av_mallocz(sizeof(AVFilterFormats));
|
|
|
|
formats->formats = av_malloc(sizeof(*formats->formats) * count);
|
|
|
|
formats->format_count = count;
|
2010-07-22 14:12:47 +03:00
|
|
|
memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
|
2009-10-19 02:06:52 +03:00
|
|
|
|
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2010-07-22 14:12:47 +03:00
|
|
|
int avfilter_add_format(AVFilterFormats **avff, int fmt)
|
2010-01-04 00:26:59 +02:00
|
|
|
{
|
2010-07-22 14:12:47 +03:00
|
|
|
int *fmts;
|
2010-01-04 00:26:59 +02:00
|
|
|
|
2010-01-06 18:19:13 +02:00
|
|
|
if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2010-07-22 14:12:47 +03:00
|
|
|
fmts = av_realloc((*avff)->formats,
|
|
|
|
sizeof((*avff)->formats) * ((*avff)->format_count+1));
|
|
|
|
if (!fmts)
|
2010-01-04 00:26:59 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2010-07-22 14:12:47 +03:00
|
|
|
(*avff)->formats = fmts;
|
|
|
|
(*avff)->formats[(*avff)->format_count++] = fmt;
|
2010-01-04 00:26:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-22 14:12:47 +03:00
|
|
|
AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
|
2008-02-10 20:02:11 +02:00
|
|
|
{
|
2010-01-06 18:19:13 +02:00
|
|
|
AVFilterFormats *ret = NULL;
|
2010-07-22 14:12:47 +03:00
|
|
|
int fmt;
|
|
|
|
int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB : 0;
|
2008-02-10 20:04:49 +02:00
|
|
|
|
2010-07-22 14:12:47 +03:00
|
|
|
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);
|
2008-02-10 20:04:49 +02:00
|
|
|
|
|
|
|
return ret;
|
2008-02-10 20:02:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-02-10 20:04:27 +02:00
|
|
|
static int find_ref_index(AVFilterFormats **ref)
|
2008-02-10 20:03:18 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < (*ref)->refcount; i ++)
|
|
|
|
if((*ref)->refs[i] == ref)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-10 20:02:11 +02:00
|
|
|
void avfilter_formats_unref(AVFilterFormats **ref)
|
|
|
|
{
|
2010-01-08 10:19:18 +02:00
|
|
|
int idx;
|
|
|
|
|
2010-01-08 00:59:22 +02:00
|
|
|
if (!*ref)
|
|
|
|
return;
|
|
|
|
|
2010-01-08 10:19:18 +02:00
|
|
|
idx = find_ref_index(ref);
|
2008-02-10 20:03:18 +02:00
|
|
|
|
2008-02-10 20:05:02 +02:00
|
|
|
if(idx >= 0)
|
2008-02-10 20:03:18 +02:00
|
|
|
memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
|
|
|
|
sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
|
|
|
|
|
2008-02-10 20:02:11 +02:00
|
|
|
if(!--(*ref)->refcount) {
|
|
|
|
av_free((*ref)->formats);
|
|
|
|
av_free((*ref)->refs);
|
|
|
|
av_free(*ref);
|
|
|
|
}
|
|
|
|
*ref = NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-10 20:04:32 +02:00
|
|
|
void avfilter_formats_changeref(AVFilterFormats **oldref,
|
|
|
|
AVFilterFormats **newref)
|
|
|
|
{
|
2008-02-10 20:05:02 +02:00
|
|
|
int idx = find_ref_index(oldref);
|
2008-02-10 20:04:32 +02:00
|
|
|
|
2008-02-10 20:05:02 +02:00
|
|
|
if(idx >= 0) {
|
2008-02-10 20:04:32 +02:00
|
|
|
(*oldref)->refs[idx] = newref;
|
|
|
|
*newref = *oldref;
|
|
|
|
*oldref = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|