You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

405 lines
12KB

  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. 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 time_base = ctx->inputs[0]->time_base;
  182. AVRational frame_rate = ctx->inputs[0]->frame_rate;
  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->time_base = time_base;
  211. outlink->frame_rate = frame_rate;
  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. return ff_framesync_configure(&s->fs);
  225. }
  226. static av_cold void uninit(AVFilterContext *ctx)
  227. {
  228. MixContext *s = ctx->priv;
  229. int i;
  230. ff_framesync_uninit(&s->fs);
  231. av_freep(&s->weights);
  232. if (!s->tmix) {
  233. for (i = 0; i < ctx->nb_inputs; i++)
  234. av_freep(&ctx->input_pads[i].name);
  235. } else {
  236. for (i = 0; i < s->nb_frames; i++)
  237. av_frame_free(&s->frames[i]);
  238. }
  239. av_freep(&s->frames);
  240. }
  241. static int activate(AVFilterContext *ctx)
  242. {
  243. MixContext *s = ctx->priv;
  244. return ff_framesync_activate(&s->fs);
  245. }
  246. #define OFFSET(x) offsetof(MixContext, x)
  247. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  248. static const AVOption mix_options[] = {
  249. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT_MAX, .flags = FLAGS },
  250. { "weights", "set weight for each input", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, .flags = FLAGS },
  251. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = FLAGS },
  252. { "duration", "how to determine end of stream", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, .flags = FLAGS, "duration" },
  253. { "longest", "Duration of longest input", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "duration" },
  254. { "shortest", "Duration of shortest input", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "duration" },
  255. { "first", "Duration of first input", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "duration" },
  256. { NULL },
  257. };
  258. static const AVFilterPad outputs[] = {
  259. {
  260. .name = "default",
  261. .type = AVMEDIA_TYPE_VIDEO,
  262. .config_props = config_output,
  263. },
  264. { NULL }
  265. };
  266. #if CONFIG_MIX_FILTER
  267. AVFILTER_DEFINE_CLASS(mix);
  268. AVFilter ff_vf_mix = {
  269. .name = "mix",
  270. .description = NULL_IF_CONFIG_SMALL("Mix video inputs."),
  271. .priv_size = sizeof(MixContext),
  272. .priv_class = &mix_class,
  273. .query_formats = query_formats,
  274. .outputs = outputs,
  275. .init = init,
  276. .uninit = uninit,
  277. .activate = activate,
  278. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
  279. };
  280. #endif /* CONFIG_MIX_FILTER */
  281. #if CONFIG_TMIX_FILTER
  282. static int tmix_filter_frame(AVFilterLink *inlink, AVFrame *in)
  283. {
  284. AVFilterContext *ctx = inlink->dst;
  285. AVFilterLink *outlink = ctx->outputs[0];
  286. MixContext *s = ctx->priv;
  287. ThreadData td;
  288. AVFrame *out;
  289. if (s->nb_frames < s->nb_inputs) {
  290. s->frames[s->nb_frames] = in;
  291. s->nb_frames++;
  292. return 0;
  293. } else {
  294. av_frame_free(&s->frames[0]);
  295. memmove(&s->frames[0], &s->frames[1], sizeof(*s->frames) * (s->nb_inputs - 1));
  296. s->frames[s->nb_inputs - 1] = in;
  297. }
  298. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  299. if (!out)
  300. return AVERROR(ENOMEM);
  301. out->pts = s->frames[0]->pts;
  302. td.out = out;
  303. td.in = s->frames;
  304. ctx->internal->execute(ctx, mix_frames, &td, NULL, FFMIN(s->height[0], ff_filter_get_nb_threads(ctx)));
  305. return ff_filter_frame(outlink, out);
  306. }
  307. static const AVOption tmix_options[] = {
  308. { "frames", "set number of successive frames to mix", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 2, 128, .flags = FLAGS },
  309. { "weights", "set weight for each frame", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1 1"}, 0, 0, .flags = FLAGS },
  310. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = FLAGS },
  311. { NULL },
  312. };
  313. static const AVFilterPad inputs[] = {
  314. {
  315. .name = "default",
  316. .type = AVMEDIA_TYPE_VIDEO,
  317. .filter_frame = tmix_filter_frame,
  318. },
  319. { NULL }
  320. };
  321. AVFILTER_DEFINE_CLASS(tmix);
  322. AVFilter ff_vf_tmix = {
  323. .name = "tmix",
  324. .description = NULL_IF_CONFIG_SMALL("Mix successive video frames."),
  325. .priv_size = sizeof(MixContext),
  326. .priv_class = &tmix_class,
  327. .query_formats = query_formats,
  328. .outputs = outputs,
  329. .inputs = inputs,
  330. .init = init,
  331. .uninit = uninit,
  332. .flags = AVFILTER_FLAG_SLICE_THREADS,
  333. };
  334. #endif /* CONFIG_TMIX_FILTER */