* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-/* #define DEBUG */
-
+#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
+#include "libavutil/buffer.h"
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
+#include "libavutil/hwcontext.h"
#include "libavutil/imgutils.h"
+#include "libavutil/internal.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "libavutil/rational.h"
#include "internal.h"
#include "video.h"
-unsigned avfilter_version(void) {
+unsigned avfilter_version(void)
+{
return LIBAVFILTER_VERSION_INT;
}
*pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
*links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
- memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
- memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
- memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
+ memmove(*pads + idx + 1, *pads + idx, sizeof(AVFilterPad) * (*count - idx));
+ memmove(*links + idx + 1, *links + idx, sizeof(AVFilterLink*) * (*count - idx));
+ memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
(*links)[idx] = NULL;
(*count)++;
- for (i = idx+1; i < *count; i++)
+ for (i = idx + 1; i < *count; i++)
if (*links[i])
(*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
}
if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
src->outputs[srcpad] || dst->inputs[dstpad])
- return -1;
+ return AVERROR(EINVAL);
if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
av_log(src, AV_LOG_ERROR,
return AVERROR(EINVAL);
}
- src->outputs[srcpad] =
- dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
+ link = av_mallocz(sizeof(*link));
+ if (!link)
+ return AVERROR(ENOMEM);
+
+ src->outputs[srcpad] = dst->inputs[dstpad] = link;
link->src = src;
link->dst = dst;
}
/* re-hookup the link to the new destination filter we inserted */
- link->dst = filt;
- link->dstpad = &filt->input_pads[filt_srcpad_idx];
+ link->dst = filt;
+ link->dstpad = &filt->input_pads[filt_srcpad_idx];
filt->inputs[filt_srcpad_idx] = link;
/* if any information on supported media formats already exists on the
* link, we need to preserve that */
if (link->out_formats)
ff_formats_changeref(&link->out_formats,
- &filt->outputs[filt_dstpad_idx]->out_formats);
+ &filt->outputs[filt_dstpad_idx]->out_formats);
if (link->out_samplerates)
ff_formats_changeref(&link->out_samplerates,
- &filt->outputs[filt_dstpad_idx]->out_samplerates);
+ &filt->outputs[filt_dstpad_idx]->out_samplerates);
if (link->out_channel_layouts)
ff_channel_layouts_changeref(&link->out_channel_layouts,
&filt->outputs[filt_dstpad_idx]->out_channel_layouts);
AVFilterLink *link = filter->inputs[i];
if (!link) continue;
+ if (!link->src || !link->dst) {
+ av_log(filter, AV_LOG_ERROR,
+ "Not all input and output are properly linked (%d).\n", i);
+ return AVERROR(EINVAL);
+ }
switch (link->init_state) {
case AVLINK_INIT:
}
if (link->time_base.num == 0 && link->time_base.den == 0)
- link->time_base = link->src && link->src->nb_inputs ?
+ link->time_base = link->src->nb_inputs ?
link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
if (link->type == AVMEDIA_TYPE_VIDEO) {
link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
if (link->src->nb_inputs) {
+ if (!link->frame_rate.num && !link->frame_rate.den)
+ link->frame_rate = link->src->inputs[0]->frame_rate;
if (!link->w)
link->w = link->src->inputs[0]->w;
if (!link->h)
}
}
+ if (link->src->nb_inputs && link->src->inputs[0]->hw_frames_ctx &&
+ !(link->src->filter->flags_internal & FF_FILTER_FLAG_HWFRAME_AWARE)) {
+ av_assert0(!link->hw_frames_ctx &&
+ "should not be set by non-hwframe-aware filter");
+ link->hw_frames_ctx = av_buffer_ref(link->src->inputs[0]->hw_frames_ctx);
+ if (!link->hw_frames_ctx)
+ return AVERROR(ENOMEM);
+ }
+
if ((config_link = link->dstpad->config_props))
if ((ret = config_link(link)) < 0) {
- av_log(link->src, AV_LOG_ERROR,
+ av_log(link->dst, AV_LOG_ERROR,
"Failed to configure input pad on %s\n",
link->dst->name);
return ret;
void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
{
if (link->type == AVMEDIA_TYPE_VIDEO) {
- av_dlog(ctx,
+ av_log(ctx, AV_LOG_TRACE,
"link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
link, link->w, link->h,
av_get_pix_fmt_name(link->format),
char buf[128];
av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
- av_dlog(ctx,
+ av_log(ctx, AV_LOG_TRACE,
"link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
link, link->sample_rate, buf,
av_get_sample_fmt_name(link->format),
return link->srcpad->request_frame(link);
else if (link->src->inputs[0])
return ff_request_frame(link->src->inputs[0]);
- else return -1;
+ else
+ return AVERROR(EINVAL);
}
int ff_poll_frame(AVFilterLink *link)
for (i = 0; i < link->src->nb_inputs; i++) {
int val;
if (!link->src->inputs[i])
- return -1;
+ return AVERROR(EINVAL);
val = ff_poll_frame(link->src->inputs[i]);
min = FFMIN(min, val);
}
static AVFilter *first_filter;
+#if !FF_API_NOCONST_GET_NAME
+const
+#endif
AVFilter *avfilter_get_by_name(const char *name)
{
- AVFilter *f = NULL;
+ const AVFilter *f = NULL;
if (!name)
return NULL;
if (!pads)
return 0;
- for(count = 0; pads->name; count ++) pads ++;
+ for (count = 0; pads->name; count++)
+ pads++;
return count;
}
static const AVClass *filter_child_class_next(const AVClass *prev)
{
- AVFilter *f = NULL;
+ const AVFilter *f = NULL;
while (prev && (f = avfilter_next(f)))
if (f->priv_class == prev)
return NULL;
}
+#define OFFSET(x) offsetof(AVFilterContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption avfilter_options[] = {
+ { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
+ { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
+ { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
+ { NULL },
+};
+
static const AVClass avfilter_class = {
.class_name = "AVFilter",
.item_name = filter_name,
.version = LIBAVUTIL_VERSION_INT,
.child_next = filter_child_next,
.child_class_next = filter_child_class_next,
+ .option = avfilter_options,
};
+static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
+ int *ret, int nb_jobs)
+{
+ int i;
+
+ for (i = 0; i < nb_jobs; i++) {
+ int r = func(ctx, arg, i, nb_jobs);
+ if (ret)
+ ret[i] = r;
+ }
+ return 0;
+}
+
AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
{
AVFilterContext *ret;
goto err;
}
+ av_opt_set_defaults(ret);
if (filter->priv_class) {
*(const AVClass**)ret->priv = filter->priv_class;
av_opt_set_defaults(ret->priv);
}
+ ret->internal = av_mallocz(sizeof(*ret->internal));
+ if (!ret->internal)
+ goto err;
+ ret->internal->execute = default_execute;
+
ret->nb_inputs = avfilter_pad_count(filter->inputs);
if (ret->nb_inputs ) {
ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
if (!ret->outputs)
goto err;
}
-#if FF_API_FOO_COUNT
- ret->output_count = ret->nb_outputs;
- ret->input_count = ret->nb_inputs;
-#endif
return ret;
av_freep(&ret->output_pads);
ret->nb_outputs = 0;
av_freep(&ret->priv);
+ av_freep(&ret->internal);
av_free(ret);
return NULL;
}
}
#endif
+static void free_link(AVFilterLink *link)
+{
+ if (!link)
+ return;
+
+ if (link->src)
+ link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
+ if (link->dst)
+ link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
+
+ av_buffer_unref(&link->hw_frames_ctx);
+
+ ff_formats_unref(&link->in_formats);
+ ff_formats_unref(&link->out_formats);
+ ff_formats_unref(&link->in_samplerates);
+ ff_formats_unref(&link->out_samplerates);
+ ff_channel_layouts_unref(&link->in_channel_layouts);
+ ff_channel_layouts_unref(&link->out_channel_layouts);
+ av_freep(&link);
+}
+
void avfilter_free(AVFilterContext *filter)
{
int i;
- AVFilterLink *link;
if (filter->graph)
ff_filter_graph_remove_filter(filter->graph, filter);
filter->filter->uninit(filter);
for (i = 0; i < filter->nb_inputs; i++) {
- if ((link = filter->inputs[i])) {
- if (link->src)
- link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
- ff_formats_unref(&link->in_formats);
- ff_formats_unref(&link->out_formats);
- ff_formats_unref(&link->in_samplerates);
- ff_formats_unref(&link->out_samplerates);
- ff_channel_layouts_unref(&link->in_channel_layouts);
- ff_channel_layouts_unref(&link->out_channel_layouts);
- }
- av_freep(&link);
+ free_link(filter->inputs[i]);
}
for (i = 0; i < filter->nb_outputs; i++) {
- if ((link = filter->outputs[i])) {
- if (link->dst)
- link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
- ff_formats_unref(&link->in_formats);
- ff_formats_unref(&link->out_formats);
- ff_formats_unref(&link->in_samplerates);
- ff_formats_unref(&link->out_samplerates);
- ff_channel_layouts_unref(&link->in_channel_layouts);
- ff_channel_layouts_unref(&link->out_channel_layouts);
- }
- av_freep(&link);
+ free_link(filter->outputs[i]);
}
if (filter->filter->priv_class)
av_opt_free(filter->priv);
+ av_buffer_unref(&filter->hw_device_ctx);
+
av_freep(&filter->name);
av_freep(&filter->input_pads);
av_freep(&filter->output_pads);
av_freep(&filter->inputs);
av_freep(&filter->outputs);
av_freep(&filter->priv);
+ av_freep(&filter->internal);
av_free(filter);
}
{
int ret = 0;
+ ret = av_opt_set_dict(ctx, options);
+ if (ret < 0) {
+ av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
+ return ret;
+ }
+
+ if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
+ ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
+ ctx->graph->internal->thread_execute) {
+ ctx->thread_type = AVFILTER_THREAD_SLICE;
+ ctx->internal->execute = ctx->graph->internal->thread_execute;
+ } else {
+ ctx->thread_type = 0;
+ }
+
if (ctx->filter->priv_class) {
ret = av_opt_set_dict(ctx->priv, options);
if (ret < 0) {
{
AVDictionary *options = NULL;
AVDictionaryEntry *e;
- int ret=0;
+ int ret = 0;
if (args && *args) {
if (!filter->filter->priv_class) {
#if FF_API_OLD_FILTER_OPTS
if (!strcmp(filter->filter->name, "scale") &&
- strchr(args, ':') < strchr(args, '=')) {
+ strchr(args, ':') && strchr(args, ':') < strchr(args, '=')) {
/* old w:h:flags=<flags> syntax */
char *copy = av_strdup(args);
char *p;
{
int (*filter_frame)(AVFilterLink *, AVFrame *);
AVFilterPad *dst = link->dstpad;
- AVFrame *out;
+ AVFrame *out = NULL;
+ int ret;
FF_DPRINTF_START(NULL, filter_frame);
ff_dlog_link(NULL, link, 1);
case AVMEDIA_TYPE_AUDIO:
out = ff_get_audio_buffer(link, frame->nb_samples);
break;
- default: return AVERROR(EINVAL);
+ default:
+ ret = AVERROR(EINVAL);
+ goto fail;
}
if (!out) {
- av_frame_free(&frame);
- return AVERROR(ENOMEM);
+ ret = AVERROR(ENOMEM);
+ goto fail;
}
- av_frame_copy_props(out, frame);
+
+ ret = av_frame_copy_props(out, frame);
+ if (ret < 0)
+ goto fail;
switch (link->type) {
case AVMEDIA_TYPE_VIDEO:
av_get_channel_layout_nb_channels(frame->channel_layout),
frame->format);
break;
- default: return AVERROR(EINVAL);
+ default:
+ ret = AVERROR(EINVAL);
+ goto fail;
}
av_frame_free(&frame);
out = frame;
return filter_frame(link, out);
+
+fail:
+ av_frame_free(&out);
+ av_frame_free(&frame);
+ return ret;
}
const AVClass *avfilter_get_class(void)