2008-02-10 19:02:11 +01:00
|
|
|
/*
|
|
|
|
* Filter layer - format negotiation
|
2010-11-28 11:22:58 +01:00
|
|
|
* Copyright (c) 2007 Bobby Bingham
|
2008-02-10 19:02:11 +01:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* This file is part of Libav.
|
2008-02-10 19:02:11 +01:00
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2008-02-10 19:02:11 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 18:35:10 +01:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2008-02-10 19:02:11 +01:00
|
|
|
* 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
|
2011-03-18 18:35:10 +01:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2008-02-10 19:02:11 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2010-01-04 01:08:11 +01:00
|
|
|
#include "libavutil/pixdesc.h"
|
2008-02-10 19:02:11 +01:00
|
|
|
#include "avfilter.h"
|
2011-05-28 21:51:03 +02:00
|
|
|
#include "internal.h"
|
2008-02-10 19:02:11 +01:00
|
|
|
|
2008-02-21 23:10:11 +01:00
|
|
|
/**
|
|
|
|
* Add all refs from a to ret and destroy a.
|
|
|
|
*/
|
2008-02-10 19:04:52 +01:00
|
|
|
static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
|
|
|
|
{
|
|
|
|
int i;
|
2008-02-10 19:04:55 +01:00
|
|
|
|
2008-02-10 19:04:52 +01:00
|
|
|
for(i = 0; i < a->refcount; i ++) {
|
|
|
|
ret->refs[ret->refcount] = a->refs[i];
|
|
|
|
*ret->refs[ret->refcount++] = ret;
|
|
|
|
}
|
2008-02-10 19:04:55 +01:00
|
|
|
|
|
|
|
av_free(a->refs);
|
|
|
|
av_free(a->formats);
|
|
|
|
av_free(a);
|
2008-02-10 19:04:52 +01:00
|
|
|
}
|
|
|
|
|
2008-02-10 19:02:11 +01:00
|
|
|
AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
|
|
|
|
{
|
|
|
|
AVFilterFormats *ret;
|
2011-10-30 01:17:27 +02:00
|
|
|
unsigned i, j, k = 0, m_count;
|
2008-02-10 19:02:11 +01:00
|
|
|
|
2012-05-04 09:49:28 +02:00
|
|
|
ret = av_mallocz(sizeof(*ret));
|
2008-02-10 19:02:11 +01:00
|
|
|
|
|
|
|
/* merge list of formats */
|
2011-10-30 01:17:27 +02:00
|
|
|
m_count = FFMIN(a->format_count, b->format_count);
|
|
|
|
if (m_count) {
|
2011-11-05 15:01:39 +01:00
|
|
|
ret->formats = av_malloc(sizeof(*ret->formats) * m_count);
|
|
|
|
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 19:02:11 +01:00
|
|
|
|
2011-11-05 15:01:39 +01:00
|
|
|
ret->format_count = k;
|
2011-10-30 01:17:27 +02:00
|
|
|
}
|
2008-02-10 19:02:11 +01:00
|
|
|
/* check that there was at least one common format */
|
2008-02-10 19:05:02 +01:00
|
|
|
if(!ret->format_count) {
|
2008-02-10 19:02:11 +01:00
|
|
|
av_free(ret->formats);
|
|
|
|
av_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-04 09:49:28 +02:00
|
|
|
ret->refs = av_malloc(sizeof(*ret->refs) * (a->refcount + b->refcount));
|
2008-02-10 19:04:52 +01:00
|
|
|
|
|
|
|
merge_ref(ret, a);
|
|
|
|
merge_ref(ret, b);
|
2008-02-10 19:02:11 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-28 21:51:03 +02:00
|
|
|
int ff_fmt_is_in(int fmt, const int *fmts)
|
|
|
|
{
|
|
|
|
const int *p;
|
|
|
|
|
|
|
|
for (p = fmts; *p != PIX_FMT_NONE; p++) {
|
|
|
|
if (fmt == *p)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-22 13:12:47 +02:00
|
|
|
AVFilterFormats *avfilter_make_format_list(const int *fmts)
|
2009-10-19 01:06:52 +02:00
|
|
|
{
|
|
|
|
AVFilterFormats *formats;
|
|
|
|
int count;
|
|
|
|
|
2010-07-22 13:12:47 +02:00
|
|
|
for (count = 0; fmts[count] != -1; count++)
|
2009-10-19 01:06:52 +02:00
|
|
|
;
|
|
|
|
|
2012-05-04 09:49:28 +02:00
|
|
|
formats = av_mallocz(sizeof(*formats));
|
2011-10-30 01:17:27 +02:00
|
|
|
if (count)
|
2011-11-05 15:01:39 +01:00
|
|
|
formats->formats = av_malloc(sizeof(*formats->formats) * count);
|
2009-10-19 01:06:52 +02:00
|
|
|
formats->format_count = count;
|
2010-07-22 13:12:47 +02:00
|
|
|
memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
|
2009-10-19 01:06:52 +02:00
|
|
|
|
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2010-07-22 13:12:47 +02:00
|
|
|
int avfilter_add_format(AVFilterFormats **avff, int fmt)
|
2010-01-03 23:26:59 +01:00
|
|
|
{
|
2010-07-22 13:12:47 +02:00
|
|
|
int *fmts;
|
2010-01-03 23:26:59 +01:00
|
|
|
|
2012-05-04 09:49:28 +02:00
|
|
|
if (!(*avff) && !(*avff = av_mallocz(sizeof(**avff))))
|
2010-01-06 17:19:13 +01:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2010-07-22 13:12:47 +02:00
|
|
|
fmts = av_realloc((*avff)->formats,
|
2011-06-19 22:07:18 +02:00
|
|
|
sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
|
2010-07-22 13:12:47 +02:00
|
|
|
if (!fmts)
|
2010-01-03 23:26:59 +01:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2010-07-22 13:12:47 +02:00
|
|
|
(*avff)->formats = fmts;
|
|
|
|
(*avff)->formats[(*avff)->format_count++] = fmt;
|
2010-01-03 23:26:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-22 13:12:47 +02:00
|
|
|
AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
|
2008-02-10 19:02:11 +01:00
|
|
|
{
|
2010-01-06 17:19:13 +01:00
|
|
|
AVFilterFormats *ret = NULL;
|
2010-07-22 13:12:47 +02:00
|
|
|
int fmt;
|
2010-08-17 20:08:03 +02:00
|
|
|
int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
|
2010-11-12 12:04:40 +01:00
|
|
|
type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
|
2008-02-10 19:04:49 +01:00
|
|
|
|
2010-07-22 13:12:47 +02: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 19:04:49 +01:00
|
|
|
|
|
|
|
return ret;
|
2008-02-10 19:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
|
|
|
|
{
|
|
|
|
*ref = f;
|
2012-05-04 09:49:28 +02:00
|
|
|
f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount);
|
2008-02-10 19:02:11 +01:00
|
|
|
f->refs[f->refcount-1] = ref;
|
|
|
|
}
|
|
|
|
|
2008-02-10 19:04:27 +01:00
|
|
|
static int find_ref_index(AVFilterFormats **ref)
|
2008-02-10 19:03:18 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < (*ref)->refcount; i ++)
|
|
|
|
if((*ref)->refs[i] == ref)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-10 19:02:11 +01:00
|
|
|
void avfilter_formats_unref(AVFilterFormats **ref)
|
|
|
|
{
|
2010-01-08 09:19:18 +01:00
|
|
|
int idx;
|
|
|
|
|
2010-01-07 23:59:22 +01:00
|
|
|
if (!*ref)
|
|
|
|
return;
|
|
|
|
|
2010-01-08 09:19:18 +01:00
|
|
|
idx = find_ref_index(ref);
|
2008-02-10 19:03:18 +01:00
|
|
|
|
2008-02-10 19:05:02 +01:00
|
|
|
if(idx >= 0)
|
2012-05-04 09:49:28 +02:00
|
|
|
memmove((*ref)->refs + idx, (*ref)->refs + idx + 1,
|
|
|
|
sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1));
|
2008-02-10 19:03:18 +01:00
|
|
|
|
2008-02-10 19:02:11 +01:00
|
|
|
if(!--(*ref)->refcount) {
|
|
|
|
av_free((*ref)->formats);
|
|
|
|
av_free((*ref)->refs);
|
|
|
|
av_free(*ref);
|
|
|
|
}
|
|
|
|
*ref = NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-10 19:04:32 +01:00
|
|
|
void avfilter_formats_changeref(AVFilterFormats **oldref,
|
|
|
|
AVFilterFormats **newref)
|
|
|
|
{
|
2008-02-10 19:05:02 +01:00
|
|
|
int idx = find_ref_index(oldref);
|
2008-02-10 19:04:32 +01:00
|
|
|
|
2008-02-10 19:05:02 +01:00
|
|
|
if(idx >= 0) {
|
2008-02-10 19:04:32 +01:00
|
|
|
(*oldref)->refs[idx] = newref;
|
|
|
|
*newref = *oldref;
|
|
|
|
*oldref = NULL;
|
|
|
|
}
|
|
|
|
}
|