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
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2011-08-04 11:28:14 +02:00
|
|
|
#include "libavutil/eval.h"
|
2010-01-04 01:08:11 +01:00
|
|
|
#include "libavutil/pixdesc.h"
|
2011-06-19 20:31:24 +02:00
|
|
|
#include "libavutil/audioconvert.h"
|
2008-02-10 19:02:11 +01:00
|
|
|
#include "avfilter.h"
|
2011-06-30 09:51:17 +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
|
|
|
|
2011-06-21 10:30:41 +02:00
|
|
|
for (i = 0; i < a->refcount; i++) {
|
2008-02-10 19:04:52 +01:00
|
|
|
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;
|
|
|
|
unsigned i, j, k = 0;
|
|
|
|
|
2011-06-08 18:24:25 +02:00
|
|
|
if (a == b) return a;
|
|
|
|
|
2008-02-10 19:02:11 +01:00
|
|
|
ret = av_mallocz(sizeof(AVFilterFormats));
|
|
|
|
|
|
|
|
/* merge list of formats */
|
2008-02-10 19:04:43 +01:00
|
|
|
ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
|
|
|
|
b->format_count));
|
2011-06-21 10:30:41 +02:00
|
|
|
for (i = 0; i < a->format_count; i++)
|
|
|
|
for (j = 0; j < b->format_count; j++)
|
2011-11-23 13:18:11 +01:00
|
|
|
if (a->formats[i] == b->formats[j]){
|
2011-11-23 14:14:01 +01:00
|
|
|
if(k >= FFMIN(a->format_count, b->format_count)){
|
|
|
|
av_log(0, AV_LOG_ERROR, "Duplicate formats in avfilter_merge_formats() detected\n");
|
|
|
|
av_free(ret->formats);
|
|
|
|
av_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-02-10 19:02:11 +01:00
|
|
|
ret->formats[k++] = a->formats[i];
|
2011-11-23 13:18:11 +01:00
|
|
|
}
|
2008-02-10 19:02:11 +01:00
|
|
|
|
2008-02-10 19:05:02 +01:00
|
|
|
ret->format_count = k;
|
2008-02-10 19:02:11 +01:00
|
|
|
/* check that there was at least one common format */
|
2011-06-21 10:30:41 +02:00
|
|
|
if (!ret->format_count) {
|
2008-02-10 19:02:11 +01:00
|
|
|
av_free(ret->formats);
|
|
|
|
av_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->refs = av_malloc(sizeof(AVFilterFormats**)*(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-06-30 09:51:17 +02:00
|
|
|
int ff_fmt_is_in(int fmt, const int *fmts)
|
|
|
|
{
|
|
|
|
const int *p;
|
|
|
|
|
|
|
|
for (p = fmts; *p != -1; p++) {
|
|
|
|
if (fmt == *p)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-07 20:17:23 +02:00
|
|
|
#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) { \
|
2011-06-21 10:30:41 +02:00
|
|
|
formats->formats = av_malloc(sizeof(*formats->formats)*count); \
|
2011-06-07 20:17:23 +02:00
|
|
|
if (!formats->formats) { \
|
|
|
|
av_free(formats); \
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2010-07-22 13:12:47 +02:00
|
|
|
AVFilterFormats *avfilter_make_format_list(const int *fmts)
|
2009-10-19 01:06:52 +02:00
|
|
|
{
|
2011-06-07 20:17:23 +02:00
|
|
|
MAKE_FORMAT_LIST();
|
|
|
|
while (count--)
|
|
|
|
formats->formats[count] = fmts[count];
|
2009-10-19 01:06:52 +02:00
|
|
|
|
2011-06-07 20:17:23 +02:00
|
|
|
return formats;
|
|
|
|
}
|
2009-10-19 01:06:52 +02:00
|
|
|
|
2011-06-07 20:17:23 +02:00
|
|
|
AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
|
|
|
|
{
|
|
|
|
MAKE_FORMAT_LIST();
|
|
|
|
if (count)
|
2011-06-07 16:42:32 +02:00
|
|
|
memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
|
2009-10-19 01:06:52 +02:00
|
|
|
|
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2011-06-07 20:17:23 +02:00
|
|
|
int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
|
2010-01-03 23:26:59 +01:00
|
|
|
{
|
2011-06-07 20:17:23 +02:00
|
|
|
int64_t *fmts;
|
2010-01-03 23:26:59 +01:00
|
|
|
|
2010-01-06 17:19:13 +01:00
|
|
|
if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-13 01:46:29 +02:00
|
|
|
#if FF_API_OLD_ALL_FORMATS_API
|
2010-07-22 13:12:47 +02:00
|
|
|
AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
|
2011-09-13 01:46:29 +02:00
|
|
|
{
|
|
|
|
return avfilter_make_all_formats(type);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
AVFilterFormats *avfilter_make_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
|
|
|
}
|
|
|
|
|
2011-08-18 15:26:21 +02:00
|
|
|
const int64_t avfilter_all_channel_layouts[] = {
|
|
|
|
#include "all_channel_layouts.h"
|
|
|
|
-1
|
|
|
|
};
|
|
|
|
|
2011-09-13 01:46:29 +02:00
|
|
|
AVFilterFormats *avfilter_make_all_channel_layouts(void)
|
2011-06-19 20:31:24 +02:00
|
|
|
{
|
2011-08-18 15:26:21 +02:00
|
|
|
return avfilter_make_format64_list(avfilter_all_channel_layouts);
|
2011-06-19 20:31:24 +02:00
|
|
|
}
|
|
|
|
|
2011-09-13 01:46:29 +02:00
|
|
|
AVFilterFormats *avfilter_make_all_packing_formats(void)
|
2011-06-28 07:56:19 +02:00
|
|
|
{
|
2011-11-05 15:27:13 +01:00
|
|
|
static const int packing[] = {
|
2011-06-28 07:56:19 +02:00
|
|
|
AVFILTER_PACKED,
|
|
|
|
AVFILTER_PLANAR,
|
|
|
|
-1,
|
|
|
|
};
|
|
|
|
|
|
|
|
return avfilter_make_format_list(packing);
|
|
|
|
}
|
|
|
|
|
2008-02-10 19:02:11 +01: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 19:04:27 +01:00
|
|
|
static int find_ref_index(AVFilterFormats **ref)
|
2008-02-10 19:03:18 +01:00
|
|
|
{
|
|
|
|
int i;
|
2011-06-21 10:30:41 +02:00
|
|
|
for (i = 0; i < (*ref)->refcount; i++)
|
|
|
|
if ((*ref)->refs[i] == ref)
|
2008-02-10 19:03:18 +01:00
|
|
|
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
|
|
|
|
2011-06-21 10:30:41 +02:00
|
|
|
if (idx >= 0)
|
2008-02-10 19:03:18 +01:00
|
|
|
memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
|
|
|
|
sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
|
|
|
|
|
2011-06-21 10:30:41 +02:00
|
|
|
if (!--(*ref)->refcount) {
|
2008-02-10 19:02:11 +01:00
|
|
|
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
|
|
|
|
2011-06-21 10:30:41 +02:00
|
|
|
if (idx >= 0) {
|
2008-02-10 19:04:32 +01:00
|
|
|
(*oldref)->refs[idx] = newref;
|
|
|
|
*newref = *oldref;
|
|
|
|
*oldref = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-04 11:28:14 +02:00
|
|
|
/* internal functions for parsing audio format arguments */
|
|
|
|
|
2011-08-22 14:56:23 +02:00
|
|
|
int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
|
|
|
|
{
|
|
|
|
char *tail;
|
|
|
|
int pix_fmt = av_get_pix_fmt(arg);
|
|
|
|
if (pix_fmt == PIX_FMT_NONE) {
|
|
|
|
pix_fmt = strtol(arg, &tail, 0);
|
|
|
|
if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ret = pix_fmt;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-04 11:28:14 +02:00
|
|
|
int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
|
|
|
|
{
|
|
|
|
char *tail;
|
|
|
|
int sfmt = av_get_sample_fmt(arg);
|
|
|
|
if (sfmt == AV_SAMPLE_FMT_NONE) {
|
|
|
|
sfmt = strtol(arg, &tail, 0);
|
|
|
|
if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ret = sfmt;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-21 19:17:24 +02:00
|
|
|
int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
|
2011-08-04 11:28:14 +02:00
|
|
|
{
|
|
|
|
char *tail;
|
|
|
|
double srate = av_strtod(arg, &tail);
|
2011-08-21 19:17:24 +02:00
|
|
|
if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
|
2011-08-04 11:28:14 +02:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
*ret = srate;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
|
|
|
|
{
|
|
|
|
char *tail;
|
|
|
|
int64_t chlayout = av_get_channel_layout(arg);
|
|
|
|
if (chlayout == 0) {
|
|
|
|
chlayout = strtol(arg, &tail, 10);
|
|
|
|
if (*tail || chlayout == 0) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ret = chlayout;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_parse_packing_format(int *ret, const char *arg, void *log_ctx)
|
|
|
|
{
|
|
|
|
char *tail;
|
|
|
|
int planar = strtol(arg, &tail, 10);
|
|
|
|
if (*tail) {
|
2011-08-16 01:59:50 +02:00
|
|
|
planar = !strcmp(arg, "packed") ? 0:
|
|
|
|
!strcmp(arg, "planar") ? 1: -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (planar != 0 && planar != 1) {
|
2011-08-04 11:28:14 +02:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "Invalid packing format '%s'\n", arg);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
*ret = planar;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-18 15:26:21 +02:00
|
|
|
#ifdef TEST
|
|
|
|
|
|
|
|
#undef printf
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
const int64_t *cl;
|
|
|
|
char buf[512];
|
|
|
|
|
2011-09-16 06:07:07 +02:00
|
|
|
for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
|
2011-08-18 15:26:21 +02:00
|
|
|
av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
|
|
|
|
printf("%s\n", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|