h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_frei0r.c
CommitLineData
47941088 1/*
3fa77bde 2 * Copyright (c) 2010 Stefano Sabatini
2912e87a 3 * This file is part of Libav.
47941088 4 *
2912e87a 5 * Libav is free software; you can redistribute it and/or
47941088
SS
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
2912e87a 10 * Libav is distributed in the hope that it will be useful,
47941088
SS
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
2912e87a 16 * License along with Libav; if not, write to the Free Software
47941088
SS
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/**
21 * @file
22 * frei0r wrapper
23 */
24
47941088
SS
25#include <dlfcn.h>
26#include <frei0r.h>
7ebe3962
MS
27#include <stdio.h>
28#include <string.h>
29#include <stdlib.h>
30#include "config.h"
372e2884 31#include "libavutil/avstring.h"
737eb597 32#include "libavutil/imgutils.h"
7ebe3962 33#include "libavutil/internal.h"
d3f751e6 34#include "libavutil/mathematics.h"
7ebe3962 35#include "libavutil/mem.h"
5aa1a668 36#include "libavutil/opt.h"
737eb597 37#include "libavutil/parseutils.h"
47941088 38#include "avfilter.h"
b74a1da4 39#include "formats.h"
9d0bfc50 40#include "internal.h"
803391f7 41#include "video.h"
47941088
SS
42
43typedef f0r_instance_t (*f0r_construct_f)(unsigned int width, unsigned int height);
44typedef void (*f0r_destruct_f)(f0r_instance_t instance);
45typedef void (*f0r_deinit_f)(void);
46typedef int (*f0r_init_f)(void);
47typedef void (*f0r_get_plugin_info_f)(f0r_plugin_info_t *info);
48typedef void (*f0r_get_param_info_f)(f0r_param_info_t *info, int param_index);
49typedef void (*f0r_update_f)(f0r_instance_t instance, double time, const uint32_t *inframe, uint32_t *outframe);
50typedef void (*f0r_update2_f)(f0r_instance_t instance, double time, const uint32_t *inframe1, const uint32_t *inframe2, const uint32_t *inframe3, uint32_t *outframe);
51typedef void (*f0r_set_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
52typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
53
54typedef struct Frei0rContext {
5aa1a668 55 const AVClass *class;
47941088
SS
56 f0r_update_f update;
57 void *dl_handle; /* dynamic library handle */
58 f0r_instance_t instance;
59 f0r_plugin_info_t plugin_info;
60
61 f0r_get_param_info_f get_param_info;
62 f0r_get_param_value_f get_param_value;
63 f0r_set_param_value_f set_param_value;
64 f0r_construct_f construct;
65 f0r_destruct_f destruct;
66 f0r_deinit_f deinit;
5aa1a668
AK
67
68 char *dl_name;
69 char *params;
70 char *size;
71 char *framerate;
f8608dca
SS
72
73 /* only used by the source */
74 int w, h;
75 AVRational time_base;
76 uint64_t pts;
47941088
SS
77} Frei0rContext;
78
79static void *load_sym(AVFilterContext *ctx, const char *sym_name)
80{
f6b6d6ac
AK
81 Frei0rContext *s = ctx->priv;
82 void *sym = dlsym(s->dl_handle, sym_name);
47941088 83 if (!sym)
8accddeb 84 av_log(ctx, AV_LOG_ERROR, "Could not find symbol '%s' in loaded module.\n", sym_name);
47941088
SS
85 return sym;
86}
87
88static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param)
89{
f6b6d6ac 90 Frei0rContext *s = ctx->priv;
47941088
SS
91 union {
92 double d;
93 f0r_param_color_t col;
94 f0r_param_position_t pos;
95 } val;
96 char *tail;
97 uint8_t rgba[4];
98
99 switch (info.type) {
100 case F0R_PARAM_BOOL:
101 if (!strcmp(param, "y")) val.d = 1.0;
102 else if (!strcmp(param, "n")) val.d = 0.0;
103 else goto fail;
104 break;
105
106 case F0R_PARAM_DOUBLE:
107 val.d = strtod(param, &tail);
108 if (*tail || val.d == HUGE_VAL)
109 goto fail;
110 break;
111
112 case F0R_PARAM_COLOR:
113 if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) {
f6c182a0 114 if (av_parse_color(rgba, param, -1, ctx) < 0)
47941088
SS
115 goto fail;
116 val.col.r = rgba[0] / 255.0;
117 val.col.g = rgba[1] / 255.0;
118 val.col.b = rgba[2] / 255.0;
119 }
120 break;
121
122 case F0R_PARAM_POSITION:
123 if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2)
124 goto fail;
125 break;
126 }
127
f6b6d6ac 128 s->set_param_value(s->instance, &val, index);
47941088
SS
129 return 0;
130
131fail:
8accddeb 132 av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'.\n",
47941088
SS
133 param, info.name);
134 return AVERROR(EINVAL);
135}
136
137static int set_params(AVFilterContext *ctx, const char *params)
138{
f6b6d6ac 139 Frei0rContext *s = ctx->priv;
47941088
SS
140 int i;
141
f6b6d6ac 142 for (i = 0; i < s->plugin_info.num_params; i++) {
47941088
SS
143 f0r_param_info_t info;
144 char *param;
145 int ret;
146
f6b6d6ac 147 s->get_param_info(&info, i);
47941088
SS
148
149 if (*params) {
5aa1a668 150 if (!(param = av_get_token(&params, "|")))
47941088 151 return AVERROR(ENOMEM);
d0b22405
VG
152 if (*params)
153 params++; /* skip ':' */
47941088
SS
154 ret = set_param(ctx, info, i, param);
155 av_free(param);
156 if (ret < 0)
157 return ret;
158 }
47941088
SS
159 }
160
161 return 0;
162}
163
164static void *load_path(AVFilterContext *ctx, const char *prefix, const char *name)
165{
166 char path[1024];
167
524aae86 168 snprintf(path, sizeof(path), "%s%s%s", prefix, name, SLIBSUF);
8accddeb 169 av_log(ctx, AV_LOG_DEBUG, "Looking for frei0r effect in '%s'.\n", path);
47941088
SS
170 return dlopen(path, RTLD_NOW|RTLD_LOCAL);
171}
172
f8608dca
SS
173static av_cold int frei0r_init(AVFilterContext *ctx,
174 const char *dl_name, int type)
47941088 175{
f6b6d6ac 176 Frei0rContext *s = ctx->priv;
47941088
SS
177 f0r_init_f f0r_init;
178 f0r_get_plugin_info_f f0r_get_plugin_info;
179 f0r_plugin_info_t *pi;
f8608dca 180 char *path;
47941088 181
5aa1a668
AK
182 if (!dl_name) {
183 av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
184 return AVERROR(EINVAL);
185 }
186
47941088 187 /* see: http://piksel.org/frei0r/1.2/spec/1.2/spec/group__pluglocations.html */
61b323ce
VG
188 if (path = getenv("FREI0R_PATH")) {
189 while(*path) {
190 char *ptr = av_get_token((const char **)&path, ":");
191 if (!ptr)
192 return AVERROR(ENOMEM);
193 s->dl_handle = load_path(ctx, ptr, dl_name);
194 av_freep(&ptr);
195 if (s->dl_handle)
196 break; /* found */
197 if (*path)
4c41a7a1 198 path++; /* skip ':' */
61b323ce 199 }
47941088 200 }
f6b6d6ac 201 if (!s->dl_handle && (path = getenv("HOME"))) {
47941088
SS
202 char prefix[1024];
203 snprintf(prefix, sizeof(prefix), "%s/.frei0r-1/lib/", path);
f6b6d6ac 204 s->dl_handle = load_path(ctx, prefix, dl_name);
47941088 205 }
f6b6d6ac
AK
206 if (!s->dl_handle)
207 s->dl_handle = load_path(ctx, "/usr/local/lib/frei0r-1/", dl_name);
208 if (!s->dl_handle)
209 s->dl_handle = load_path(ctx, "/usr/lib/frei0r-1/", dl_name);
210 if (!s->dl_handle) {
8accddeb 211 av_log(ctx, AV_LOG_ERROR, "Could not find module '%s'.\n", dl_name);
47941088
SS
212 return AVERROR(EINVAL);
213 }
214
215 if (!(f0r_init = load_sym(ctx, "f0r_init" )) ||
216 !(f0r_get_plugin_info = load_sym(ctx, "f0r_get_plugin_info")) ||
f6b6d6ac
AK
217 !(s->get_param_info = load_sym(ctx, "f0r_get_param_info" )) ||
218 !(s->get_param_value = load_sym(ctx, "f0r_get_param_value")) ||
219 !(s->set_param_value = load_sym(ctx, "f0r_set_param_value")) ||
220 !(s->update = load_sym(ctx, "f0r_update" )) ||
221 !(s->construct = load_sym(ctx, "f0r_construct" )) ||
222 !(s->destruct = load_sym(ctx, "f0r_destruct" )) ||
223 !(s->deinit = load_sym(ctx, "f0r_deinit" )))
47941088
SS
224 return AVERROR(EINVAL);
225
226 if (f0r_init() < 0) {
8accddeb 227 av_log(ctx, AV_LOG_ERROR, "Could not init the frei0r module.\n");
47941088
SS
228 return AVERROR(EINVAL);
229 }
230
f6b6d6ac
AK
231 f0r_get_plugin_info(&s->plugin_info);
232 pi = &s->plugin_info;
f8608dca 233 if (pi->plugin_type != type) {
47941088 234 av_log(ctx, AV_LOG_ERROR,
8accddeb 235 "Invalid type '%s' for this plugin\n",
f8608dca 236 pi->plugin_type == F0R_PLUGIN_TYPE_FILTER ? "filter" :
47941088
SS
237 pi->plugin_type == F0R_PLUGIN_TYPE_SOURCE ? "source" :
238 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER2 ? "mixer2" :
239 pi->plugin_type == F0R_PLUGIN_TYPE_MIXER3 ? "mixer3" : "unknown");
240 return AVERROR(EINVAL);
241 }
242
1a49a169 243 av_log(ctx, AV_LOG_VERBOSE,
47941088
SS
244 "name:%s author:'%s' explanation:'%s' color_model:%s "
245 "frei0r_version:%d version:%d.%d num_params:%d\n",
246 pi->name, pi->author, pi->explanation,
247 pi->color_model == F0R_COLOR_MODEL_BGRA8888 ? "bgra8888" :
248 pi->color_model == F0R_COLOR_MODEL_RGBA8888 ? "rgba8888" :
249 pi->color_model == F0R_COLOR_MODEL_PACKED32 ? "packed32" : "unknown",
250 pi->frei0r_version, pi->major_version, pi->minor_version, pi->num_params);
251
252 return 0;
253}
254
c43a7eca 255static av_cold int filter_init(AVFilterContext *ctx)
f8608dca 256{
f6b6d6ac 257 Frei0rContext *s = ctx->priv;
f8608dca 258
f6b6d6ac 259 return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_FILTER);
f8608dca
SS
260}
261
47941088
SS
262static av_cold void uninit(AVFilterContext *ctx)
263{
f6b6d6ac
AK
264 Frei0rContext *s = ctx->priv;
265
266 if (s->destruct && s->instance)
267 s->destruct(s->instance);
268 if (s->deinit)
269 s->deinit();
270 if (s->dl_handle)
271 dlclose(s->dl_handle);
47941088
SS
272}
273
274static int config_input_props(AVFilterLink *inlink)
275{
276 AVFilterContext *ctx = inlink->dst;
f6b6d6ac 277 Frei0rContext *s = ctx->priv;
47941088 278
d371c3c2
AK
279 if (s->destruct && s->instance)
280 s->destruct(s->instance);
f6b6d6ac 281 if (!(s->instance = s->construct(inlink->w, inlink->h))) {
8accddeb 282 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
47941088
SS
283 return AVERROR(EINVAL);
284 }
285
f6b6d6ac 286 return set_params(ctx, s->params);
47941088
SS
287}
288
289static int query_formats(AVFilterContext *ctx)
290{
f6b6d6ac 291 Frei0rContext *s = ctx->priv;
47941088
SS
292 AVFilterFormats *formats = NULL;
293
f6b6d6ac 294 if (s->plugin_info.color_model == F0R_COLOR_MODEL_BGRA8888) {
716d413c 295 ff_add_format(&formats, AV_PIX_FMT_BGRA);
f6b6d6ac 296 } else if (s->plugin_info.color_model == F0R_COLOR_MODEL_RGBA8888) {
716d413c 297 ff_add_format(&formats, AV_PIX_FMT_RGBA);
47941088 298 } else { /* F0R_COLOR_MODEL_PACKED32 */
716d413c
AK
299 static const enum AVPixelFormat pix_fmts[] = {
300 AV_PIX_FMT_BGRA, AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_ARGB, AV_PIX_FMT_NONE
47941088 301 };
b74a1da4 302 formats = ff_make_format_list(pix_fmts);
47941088
SS
303 }
304
305 if (!formats)
306 return AVERROR(ENOMEM);
307
b74a1da4 308 ff_set_common_formats(ctx, formats);
47941088
SS
309 return 0;
310}
311
7e350379 312static int filter_frame(AVFilterLink *inlink, AVFrame *in)
47941088 313{
f6b6d6ac 314 Frei0rContext *s = inlink->dst->priv;
47941088 315 AVFilterLink *outlink = inlink->dst->outputs[0];
7e350379 316 AVFrame *out;
565e4993 317
7e350379 318 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
565e4993 319 if (!out) {
7e350379 320 av_frame_free(&in);
565e4993
AK
321 return AVERROR(ENOMEM);
322 }
7e350379 323 av_frame_copy_props(out, in);
565e4993 324
f6b6d6ac 325 s->update(s->instance, in->pts * av_q2d(inlink->time_base) * 1000,
565e4993
AK
326 (const uint32_t *)in->data[0],
327 (uint32_t *)out->data[0]);
328
7e350379 329 av_frame_free(&in);
565e4993
AK
330
331 return ff_filter_frame(outlink, out);
47941088
SS
332}
333
5aa1a668
AK
334#define OFFSET(x) offsetof(Frei0rContext, x)
335#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
336static const AVOption filter_options[] = {
337 { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
338 { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
339 { NULL },
340};
341
342static const AVClass filter_class = {
343 .class_name = "frei0r",
344 .item_name = av_default_item_name,
345 .option = filter_options,
346 .version = LIBAVUTIL_VERSION_INT,
347};
348
568c70e7
MR
349static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
350 {
351 .name = "default",
352 .type = AVMEDIA_TYPE_VIDEO,
568c70e7 353 .config_props = config_input_props,
565e4993 354 .filter_frame = filter_frame,
568c70e7
MR
355 },
356 { NULL }
357};
358
359static const AVFilterPad avfilter_vf_frei0r_outputs[] = {
360 {
361 .name = "default",
362 .type = AVMEDIA_TYPE_VIDEO,
363 },
364 { NULL }
365};
366
cd43ca04 367AVFilter ff_vf_frei0r = {
47941088
SS
368 .name = "frei0r",
369 .description = NULL_IF_CONFIG_SMALL("Apply a frei0r effect."),
370
371 .query_formats = query_formats,
f8608dca 372 .init = filter_init,
47941088
SS
373 .uninit = uninit,
374
375 .priv_size = sizeof(Frei0rContext),
5aa1a668 376 .priv_class = &filter_class,
47941088 377
568c70e7
MR
378 .inputs = avfilter_vf_frei0r_inputs,
379
380 .outputs = avfilter_vf_frei0r_outputs,
47941088 381};
f8608dca 382
c43a7eca 383static av_cold int source_init(AVFilterContext *ctx)
f8608dca 384{
f6b6d6ac 385 Frei0rContext *s = ctx->priv;
f8608dca
SS
386 AVRational frame_rate_q;
387
f6b6d6ac 388 if (av_parse_video_size(&s->w, &s->h, s->size) < 0) {
8accddeb 389 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'.\n", s->size);
f8608dca
SS
390 return AVERROR(EINVAL);
391 }
392
f6b6d6ac 393 if (av_parse_video_rate(&frame_rate_q, s->framerate) < 0 ||
f8608dca 394 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
8accddeb 395 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'.\n", s->framerate);
f8608dca
SS
396 return AVERROR(EINVAL);
397 }
f6b6d6ac
AK
398 s->time_base.num = frame_rate_q.den;
399 s->time_base.den = frame_rate_q.num;
f8608dca 400
f6b6d6ac 401 return frei0r_init(ctx, s->dl_name, F0R_PLUGIN_TYPE_SOURCE);
f8608dca
SS
402}
403
404static int source_config_props(AVFilterLink *outlink)
405{
406 AVFilterContext *ctx = outlink->src;
f6b6d6ac 407 Frei0rContext *s = ctx->priv;
f8608dca 408
f6b6d6ac 409 if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
f8608dca 410 return AVERROR(EINVAL);
f6b6d6ac
AK
411 outlink->w = s->w;
412 outlink->h = s->h;
413 outlink->time_base = s->time_base;
11f87ca7 414 outlink->frame_rate = av_inv_q(s->time_base);
f8608dca 415
d371c3c2
AK
416 if (s->destruct && s->instance)
417 s->destruct(s->instance);
f6b6d6ac 418 if (!(s->instance = s->construct(outlink->w, outlink->h))) {
8accddeb 419 av_log(ctx, AV_LOG_ERROR, "Impossible to load frei0r instance.\n");
f8608dca
SS
420 return AVERROR(EINVAL);
421 }
4e0be9c8
VG
422 if (!s->params) {
423 av_log(ctx, AV_LOG_ERROR, "frei0r filter parameters not set.\n");
424 return AVERROR(EINVAL);
425 }
f8608dca 426
f6b6d6ac 427 return set_params(ctx, s->params);
f8608dca
SS
428}
429
430static int source_request_frame(AVFilterLink *outlink)
431{
f6b6d6ac 432 Frei0rContext *s = outlink->src->priv;
7e350379 433 AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
3825b526 434
7e350379 435 if (!frame)
8f3a3ce7
AK
436 return AVERROR(ENOMEM);
437
7e350379 438 frame->sample_aspect_ratio = (AVRational) {1, 1};
f6b6d6ac 439 frame->pts = s->pts++;
f8608dca 440
f6b6d6ac 441 s->update(s->instance, av_rescale_q(frame->pts, s->time_base, (AVRational){1,1000}),
7e350379 442 NULL, (uint32_t *)frame->data[0]);
f8608dca 443
7e350379 444 return ff_filter_frame(outlink, frame);
f8608dca
SS
445}
446
5aa1a668
AK
447static const AVOption src_options[] = {
448 { "size", "Dimensions of the generated video.", OFFSET(size), AV_OPT_TYPE_STRING, { .str = "" }, .flags = FLAGS },
449 { "framerate", NULL, OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = FLAGS },
450 { "filter_name", NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
451 { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
452 { NULL },
453};
454
455static const AVClass src_class = {
456 .class_name = "frei0r_src",
457 .item_name = av_default_item_name,
458 .option = src_options,
459 .version = LIBAVUTIL_VERSION_INT,
460};
461
568c70e7
MR
462static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
463 {
464 .name = "default",
465 .type = AVMEDIA_TYPE_VIDEO,
466 .request_frame = source_request_frame,
467 .config_props = source_config_props
468 },
469 { NULL }
470};
471
cd43ca04 472AVFilter ff_vsrc_frei0r_src = {
f8608dca
SS
473 .name = "frei0r_src",
474 .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
475
476 .priv_size = sizeof(Frei0rContext),
5aa1a668 477 .priv_class = &src_class,
f8608dca
SS
478 .init = source_init,
479 .uninit = uninit,
480
481 .query_formats = query_formats,
482
1fce361d 483 .inputs = NULL,
f8608dca 484
568c70e7 485 .outputs = avfilter_vsrc_frei0r_src_outputs,
f8608dca 486};