vf_mix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (c) 2017 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avstring.h"
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "framesync.h"
  29. #include "video.h"
  30. typedef struct MixContext {
  31. const AVClass *class;
  32. const AVPixFmtDescriptor *desc;
  33. char *weights_str;
  34. int nb_inputs;
  35. int duration;
  36. float *weights;
  37. float scale;
  38. float wfactor;
  39. int tmix;
  40. int nb_frames;
  41. int depth;
  42. int max;
  43. int nb_planes;
  44. int linesize[4];
  45. int height[4];
  46. AVFrame **frames;
  47. FFFrameSync fs;
  48. } MixContext;
  49. static int query_formats(AVFilterContext *ctx)
  50. {
  51. AVFilterFormats *pix_fmts = NULL;
  52. int fmt, ret;
  53. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  54. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  55. if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
  56. desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  57. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
  58. (ret = ff_add_format(&pix_fmts, fmt)) < 0)
  59. return ret;
  60. }
  61. return ff_set_common_formats(ctx, pix_fmts);
  62. }
  63. static av_cold int init(AVFilterContext *ctx)
  64. {
  65. MixContext *s = ctx->priv;
  66. char *p, *arg, *saveptr = NULL;
  67. int i, ret, last = 0;
  68. s->tmix = !strcmp(ctx->filter->name, "tmix");
  69. s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
  70. if (!s->frames)
  71. return AVERROR(ENOMEM);
  72. s->weights = av_calloc(s->nb_inputs, sizeof(*s->weights));
  73. if (!s->weights)
  74. return AVERROR(ENOMEM);
  75. if (!s->tmix) {
  76. for (i = 0; i < s->nb_inputs; i++) {
  77. AVFilterPad pad = { 0 };
  78. pad.type = AVMEDIA_TYPE_VIDEO;
  79. pad.name = av_asprintf("input%d", i);
  80. if (!pad.name)
  81. return AVERROR(ENOMEM);
  82. if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
  83. av_freep(&pad.name);
  84. return ret;
  85. }
  86. }
  87. }
  88. p = s->weights_str;
  89. for (i = 0; i < s->nb_inputs; i++) {
  90. if (!(arg = av_strtok(p, " ", &saveptr)))
  91. break;
  92. p = NULL;
  93. av_sscanf(arg, "%f", &s->weights[i]);
  94. s->wfactor += s->weights[i];
  95. last = i;
  96. }
  97. for (; i < s->nb_inputs; i++) {
  98. s->weights[i] = s->weights[last];
  99. s->wfactor += s->weights[i];
  100. }
  101. if (s->scale == 0) {
  102. s->wfactor = 1 / s->wfactor;
  103. } else {
  104. s->wfactor = s->scale;
  105. }
  106. return 0;
  107. }
  108. typedef struct ThreadData {
  109. AVFrame **in, *out;
  110. } ThreadData;
  111. static int mix_frames(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  112. {
  113. MixContext *s = ctx->priv;
  114. ThreadData *td = arg;
  115. AVFrame **in = td->in;
  116. AVFrame *out = td->out;
  117. int i, p, x, y;
  118. if (s->depth <= 8) {
  119. for (p = 0; p < s->nb_planes; p++) {
  120. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  121. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  122. uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
  123. for (y = slice_start; y < slice_end; y++) {
  124. for (x = 0; x < s->linesize[p]; x++) {
  125. int val = 0;
  126. for (i = 0; i < s->nb_inputs; i++) {
  127. uint8_t src = in[i]->data[p][y * in[i]->linesize[p] + x];
  128. val += src * s->weights[i];
  129. }
  130. dst[x] = av_clip_uint8(val * s->wfactor);
  131. }
  132. dst += out->linesize[p];
  133. }
  134. }
  135. } else {
  136. for (p = 0; p < s->nb_planes; p++) {
  137. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  138. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  139. uint16_t *dst = (uint16_t *)(out->data[p] + slice_start * out->linesize[p]);
  140. for (y = slice_start; y < slice_end; y++) {
  141. for (x = 0; x < s->linesize[p] / 2; x++) {
  142. int val = 0;
  143. for (i = 0; i < s->nb_inputs; i++) {
  144. uint16_t src = AV_RN16(in[i]->data[p] + y * in[i]->linesize[p] + x * 2);
  145. val += src * s->weights[i];
  146. }
  147. dst[x] = av_clip(val * s->wfactor, 0, s->max);
  148. }
  149. dst += out->linesize[p] / 2;
  150. }
  151. }
  152. }
  153. return 0;
  154. }
  155. static int process_frame(FFFrameSync *fs)
  156. {
  157. AVFilterContext *ctx = fs->parent;
  158. AVFilterLink *outlink = ctx->outputs[0];
  159. MixContext *s = fs->opaque;
  160. AVFrame **in = s->frames;
  161. AVFrame *out;
  162. ThreadData td;
  163. int i, ret;
  164. for (i = 0; i < s->nb_inputs; i++) {
  165. if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
  166. return ret;
  167. }
  168. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  169. if (!out)
  170. return AVERROR(ENOMEM);
  171. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  172. td.in = in;
  173. td.out = out;
  174. ctx->internal->execute(ctx, mix_frames, &td, NULL, FFMIN(s->height[0], ff_filter_get_nb_threads(ctx)));
  175. return ff_filter_frame(outlink, out);
  176. }
  177. static int config_output(AVFilterLink *outlink)
  178. {
  179. AVFilterContext *ctx = outlink->src;
  180. MixContext *s = ctx->priv;
  181. AVRational frame_rate = ctx->inputs[0]->frame_rate;
  182. AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
  183. AVFilterLink *inlink = ctx->inputs[0];
  184. int height = ctx->inputs[0]->h;
  185. int width = ctx->inputs[0]->w;
  186. FFFrameSyncIn *in;
  187. int i, ret;
  188. if (!s->tmix) {
  189. for (i = 1; i < s->nb_inputs; i++) {
  190. if (ctx->inputs[i]->h != height || ctx->inputs[i]->w != width) {
  191. av_log(ctx, AV_LOG_ERROR, "Input %d size (%dx%d) does not match input %d size (%dx%d).\n", i, ctx->inputs[i]->w, ctx->inputs[i]->h, 0, width, height);
  192. return AVERROR(EINVAL);
  193. }
  194. }
  195. }
  196. s->desc = av_pix_fmt_desc_get(outlink->format);
  197. if (!s->desc)
  198. return AVERROR_BUG;
  199. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  200. s->depth = s->desc->comp[0].depth;
  201. s->max = (1 << s->depth) - 1;
  202. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  203. return ret;
  204. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  205. s->height[0] = s->height[3] = inlink->h;
  206. if (s->tmix)
  207. return 0;
  208. outlink->w = width;
  209. outlink->h = height;
  210. outlink->frame_rate = frame_rate;
  211. outlink->sample_aspect_ratio = sar;
  212. if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
  213. return ret;
  214. in = s->fs.in;
  215. s->fs.opaque = s;
  216. s->fs.on_event = process_frame;
  217. for (i = 0; i < s->nb_inputs; i++) {
  218. AVFilterLink *inlink = ctx->inputs[i];
  219. in[i].time_base = inlink->time_base;
  220. in[i].sync = 1;
  221. in[i].before = EXT_STOP;
  222. in[i].after = (s->duration == 1 || (s->duration == 2 && i == 0)) ? EXT_STOP : EXT_INFINITY;
  223. }
  224. ret = ff_framesync_configure(&s->fs);
  225. outlink->time_base = s->fs.time_base;
  226. return ret;
  227. }
  228. static av_cold void uninit(AVFilterContext *ctx)
  229. {
  230. MixContext *s = ctx->priv;
  231. int i;
  232. ff_framesync_uninit(&s->fs);
  233. av_freep(&s->weights);
  234. if (!s->tmix) {
  235. for (i = 0; i < ctx->nb_inputs; i++)
  236. av_freep(&ctx->input_pads[i].name);
  237. } else {
  238. for (i = 0; i < s->nb_frames; i++)
  239. av_frame_free(&s->frames[i]);
  240. }
  241. av_freep(&s->frames);
  242. }
  243. static int activate(AVFilterContext *ctx)
  244. {
  245. MixContext *s = ctx->priv;
  246. return ff_framesync_activate(&s->fs);
  247. }
  248. #define OFFSET(x) offsetof(MixContext, x)
  249. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  250. static const AVOption mix_options[] = {
  251. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
  252. { "weights", "set weight for each input", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, .flags = FLAGS },
  253. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = FLAGS },
  254. { "duration", "how to determine end of stream", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, .flags = FLAGS, "duration" },
  255. { "longest", "Duration of longest input", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "duration" },
  256. { "shortest", "Duration of shortest input", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "duration" },
  257. { "first", "Duration of first input", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "duration" },
  258. { NULL },
  259. };
  260. static const AVFilterPad outputs[] = {
  261. {
  262. .name = "default",
  263. .type = AVMEDIA_TYPE_VIDEO,
  264. .config_props = config_output,
  265. },
  266. { NULL }
  267. };
  268. #if CONFIG_MIX_FILTER
  269. AVFILTER_DEFINE_CLASS(mix);
  270. AVFilter ff_vf_mix = {
  271. .name = "mix",
  272. .description = NULL_IF_CONFIG_SMALL("Mix video inputs."),
  273. .priv_size = sizeof(MixContext),
  274. .priv_class = &mix_class,
  275. .query_formats = query_formats,
  276. .outputs = outputs,
  277. .init = init,
  278. .uninit = uninit,
  279. .activate = activate,
  280. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
  281. };
  282. #endif /* CONFIG_MIX_FILTER */
  283. #if CONFIG_TMIX_FILTER
  284. static int tmix_filter_frame(AVFilterLink *inlink, AVFrame *in)
  285. {
  286. AVFilterContext *ctx = inlink->dst;
  287. AVFilterLink *outlink = ctx->outputs[0];
  288. MixContext *s = ctx->priv;
  289. ThreadData td;
  290. AVFrame *out;
  291. if (s->nb_inputs == 1)
  292. return ff_filter_frame(outlink, in);
  293. if (s->nb_frames < s->nb_inputs) {
  294. s->frames[s->nb_frames] = in;
  295. s->nb_frames++;
  296. if (s->nb_frames < s->nb_inputs)
  297. return 0;
  298. } else {
  299. av_frame_free(&s->frames[0]);
  300. memmove(&s->frames[0], &s->frames[1], sizeof(*s->frames) * (s->nb_inputs - 1));
  301. s->frames[s->nb_inputs - 1] = in;
  302. }
  303. if (ctx->is_disabled) {
  304. out = av_frame_clone(s->frames[0]);
  305. if (!out)
  306. return AVERROR(ENOMEM);
  307. return ff_filter_frame(outlink, out);
  308. }
  309. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  310. if (!out)
  311. return AVERROR(ENOMEM);
  312. out->pts = s->frames[0]->pts;
  313. td.out = out;
  314. td.in = s->frames;
  315. ctx->internal->execute(ctx, mix_frames, &td, NULL, FFMIN(s->height[0], ff_filter_get_nb_threads(ctx)));
  316. return ff_filter_frame(outlink, out);
  317. }
  318. static const AVOption tmix_options[] = {
  319. { "frames", "set number of successive frames to mix", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 1, 128, .flags = FLAGS },
  320. { "weights", "set weight for each frame", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1 1"}, 0, 0, .flags = FLAGS },
  321. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = FLAGS },
  322. { NULL },
  323. };
  324. static const AVFilterPad inputs[] = {
  325. {
  326. .name = "default",
  327. .type = AVMEDIA_TYPE_VIDEO,
  328. .filter_frame = tmix_filter_frame,
  329. },
  330. { NULL }
  331. };
  332. AVFILTER_DEFINE_CLASS(tmix);
  333. AVFilter ff_vf_tmix = {
  334. .name = "tmix",
  335. .description = NULL_IF_CONFIG_SMALL("Mix successive video frames."),
  336. .priv_size = sizeof(MixContext),
  337. .priv_class = &tmix_class,
  338. .query_formats = query_formats,
  339. .outputs = outputs,
  340. .inputs = inputs,
  341. .init = init,
  342. .uninit = uninit,
  343. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  344. };
  345. #endif /* CONFIG_TMIX_FILTER */