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.

422 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. if (av_sscanf(arg, "%f", &s->weights[i]) != 1) {
  94. av_log(ctx, AV_LOG_ERROR, "Invalid syntax for weights[%d].\n", i);
  95. return AVERROR(EINVAL);
  96. }
  97. s->wfactor += s->weights[i];
  98. last = i;
  99. }
  100. for (; i < s->nb_inputs; i++) {
  101. s->weights[i] = s->weights[last];
  102. s->wfactor += s->weights[i];
  103. }
  104. if (s->scale == 0) {
  105. s->wfactor = 1 / s->wfactor;
  106. } else {
  107. s->wfactor = s->scale;
  108. }
  109. return 0;
  110. }
  111. typedef struct ThreadData {
  112. AVFrame **in, *out;
  113. } ThreadData;
  114. static int mix_frames(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  115. {
  116. MixContext *s = ctx->priv;
  117. ThreadData *td = arg;
  118. AVFrame **in = td->in;
  119. AVFrame *out = td->out;
  120. int i, p, x, y;
  121. if (s->depth <= 8) {
  122. for (p = 0; p < s->nb_planes; p++) {
  123. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  124. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  125. uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
  126. for (y = slice_start; y < slice_end; y++) {
  127. for (x = 0; x < s->linesize[p]; x++) {
  128. int val = 0;
  129. for (i = 0; i < s->nb_inputs; i++) {
  130. uint8_t src = in[i]->data[p][y * in[i]->linesize[p] + x];
  131. val += src * s->weights[i];
  132. }
  133. dst[x] = av_clip_uint8(val * s->wfactor);
  134. }
  135. dst += out->linesize[p];
  136. }
  137. }
  138. } else {
  139. for (p = 0; p < s->nb_planes; p++) {
  140. const int slice_start = (s->height[p] * jobnr) / nb_jobs;
  141. const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
  142. uint16_t *dst = (uint16_t *)(out->data[p] + slice_start * out->linesize[p]);
  143. for (y = slice_start; y < slice_end; y++) {
  144. for (x = 0; x < s->linesize[p] / 2; x++) {
  145. int val = 0;
  146. for (i = 0; i < s->nb_inputs; i++) {
  147. uint16_t src = AV_RN16(in[i]->data[p] + y * in[i]->linesize[p] + x * 2);
  148. val += src * s->weights[i];
  149. }
  150. dst[x] = av_clip(val * s->wfactor, 0, s->max);
  151. }
  152. dst += out->linesize[p] / 2;
  153. }
  154. }
  155. }
  156. return 0;
  157. }
  158. static int process_frame(FFFrameSync *fs)
  159. {
  160. AVFilterContext *ctx = fs->parent;
  161. AVFilterLink *outlink = ctx->outputs[0];
  162. MixContext *s = fs->opaque;
  163. AVFrame **in = s->frames;
  164. AVFrame *out;
  165. ThreadData td;
  166. int i, ret;
  167. for (i = 0; i < s->nb_inputs; i++) {
  168. if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
  169. return ret;
  170. }
  171. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  172. if (!out)
  173. return AVERROR(ENOMEM);
  174. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  175. td.in = in;
  176. td.out = out;
  177. ctx->internal->execute(ctx, mix_frames, &td, NULL, FFMIN(s->height[0], ff_filter_get_nb_threads(ctx)));
  178. return ff_filter_frame(outlink, out);
  179. }
  180. static int config_output(AVFilterLink *outlink)
  181. {
  182. AVFilterContext *ctx = outlink->src;
  183. MixContext *s = ctx->priv;
  184. AVRational frame_rate = ctx->inputs[0]->frame_rate;
  185. AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
  186. AVFilterLink *inlink = ctx->inputs[0];
  187. int height = ctx->inputs[0]->h;
  188. int width = ctx->inputs[0]->w;
  189. FFFrameSyncIn *in;
  190. int i, ret;
  191. if (!s->tmix) {
  192. for (i = 1; i < s->nb_inputs; i++) {
  193. if (ctx->inputs[i]->h != height || ctx->inputs[i]->w != width) {
  194. 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);
  195. return AVERROR(EINVAL);
  196. }
  197. }
  198. }
  199. s->desc = av_pix_fmt_desc_get(outlink->format);
  200. if (!s->desc)
  201. return AVERROR_BUG;
  202. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  203. s->depth = s->desc->comp[0].depth;
  204. s->max = (1 << s->depth) - 1;
  205. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  206. return ret;
  207. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  208. s->height[0] = s->height[3] = inlink->h;
  209. if (s->tmix)
  210. return 0;
  211. outlink->w = width;
  212. outlink->h = height;
  213. outlink->frame_rate = frame_rate;
  214. outlink->sample_aspect_ratio = sar;
  215. if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
  216. return ret;
  217. in = s->fs.in;
  218. s->fs.opaque = s;
  219. s->fs.on_event = process_frame;
  220. for (i = 0; i < s->nb_inputs; i++) {
  221. AVFilterLink *inlink = ctx->inputs[i];
  222. in[i].time_base = inlink->time_base;
  223. in[i].sync = 1;
  224. in[i].before = EXT_STOP;
  225. in[i].after = (s->duration == 1 || (s->duration == 2 && i == 0)) ? EXT_STOP : EXT_INFINITY;
  226. }
  227. ret = ff_framesync_configure(&s->fs);
  228. outlink->time_base = s->fs.time_base;
  229. return ret;
  230. }
  231. static av_cold void uninit(AVFilterContext *ctx)
  232. {
  233. MixContext *s = ctx->priv;
  234. int i;
  235. ff_framesync_uninit(&s->fs);
  236. av_freep(&s->weights);
  237. if (!s->tmix) {
  238. for (i = 0; i < ctx->nb_inputs; i++)
  239. av_freep(&ctx->input_pads[i].name);
  240. } else {
  241. for (i = 0; i < s->nb_frames && s->frames; i++)
  242. av_frame_free(&s->frames[i]);
  243. }
  244. av_freep(&s->frames);
  245. }
  246. static int activate(AVFilterContext *ctx)
  247. {
  248. MixContext *s = ctx->priv;
  249. return ff_framesync_activate(&s->fs);
  250. }
  251. #define OFFSET(x) offsetof(MixContext, x)
  252. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  253. static const AVOption mix_options[] = {
  254. { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=2}, 2, INT16_MAX, .flags = FLAGS },
  255. { "weights", "set weight for each input", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, .flags = FLAGS },
  256. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = FLAGS },
  257. { "duration", "how to determine end of stream", OFFSET(duration), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, .flags = FLAGS, "duration" },
  258. { "longest", "Duration of longest input", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "duration" },
  259. { "shortest", "Duration of shortest input", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "duration" },
  260. { "first", "Duration of first input", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "duration" },
  261. { NULL },
  262. };
  263. static const AVFilterPad outputs[] = {
  264. {
  265. .name = "default",
  266. .type = AVMEDIA_TYPE_VIDEO,
  267. .config_props = config_output,
  268. },
  269. { NULL }
  270. };
  271. #if CONFIG_MIX_FILTER
  272. AVFILTER_DEFINE_CLASS(mix);
  273. AVFilter ff_vf_mix = {
  274. .name = "mix",
  275. .description = NULL_IF_CONFIG_SMALL("Mix video inputs."),
  276. .priv_size = sizeof(MixContext),
  277. .priv_class = &mix_class,
  278. .query_formats = query_formats,
  279. .outputs = outputs,
  280. .init = init,
  281. .uninit = uninit,
  282. .activate = activate,
  283. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,
  284. };
  285. #endif /* CONFIG_MIX_FILTER */
  286. #if CONFIG_TMIX_FILTER
  287. static int tmix_filter_frame(AVFilterLink *inlink, AVFrame *in)
  288. {
  289. AVFilterContext *ctx = inlink->dst;
  290. AVFilterLink *outlink = ctx->outputs[0];
  291. MixContext *s = ctx->priv;
  292. ThreadData td;
  293. AVFrame *out;
  294. if (s->nb_inputs == 1)
  295. return ff_filter_frame(outlink, in);
  296. if (s->nb_frames < s->nb_inputs) {
  297. s->frames[s->nb_frames] = in;
  298. s->nb_frames++;
  299. if (s->nb_frames < s->nb_inputs)
  300. return 0;
  301. } else {
  302. av_frame_free(&s->frames[0]);
  303. memmove(&s->frames[0], &s->frames[1], sizeof(*s->frames) * (s->nb_inputs - 1));
  304. s->frames[s->nb_inputs - 1] = in;
  305. }
  306. if (ctx->is_disabled) {
  307. out = av_frame_clone(s->frames[0]);
  308. if (!out)
  309. return AVERROR(ENOMEM);
  310. return ff_filter_frame(outlink, out);
  311. }
  312. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  313. if (!out)
  314. return AVERROR(ENOMEM);
  315. out->pts = s->frames[0]->pts;
  316. td.out = out;
  317. td.in = s->frames;
  318. ctx->internal->execute(ctx, mix_frames, &td, NULL, FFMIN(s->height[0], ff_filter_get_nb_threads(ctx)));
  319. return ff_filter_frame(outlink, out);
  320. }
  321. static const AVOption tmix_options[] = {
  322. { "frames", "set number of successive frames to mix", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 1, 128, .flags = FLAGS },
  323. { "weights", "set weight for each frame", OFFSET(weights_str), AV_OPT_TYPE_STRING, {.str="1 1 1"}, 0, 0, .flags = FLAGS },
  324. { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, INT16_MAX, .flags = FLAGS },
  325. { NULL },
  326. };
  327. static const AVFilterPad inputs[] = {
  328. {
  329. .name = "default",
  330. .type = AVMEDIA_TYPE_VIDEO,
  331. .filter_frame = tmix_filter_frame,
  332. },
  333. { NULL }
  334. };
  335. AVFILTER_DEFINE_CLASS(tmix);
  336. AVFilter ff_vf_tmix = {
  337. .name = "tmix",
  338. .description = NULL_IF_CONFIG_SMALL("Mix successive video frames."),
  339. .priv_size = sizeof(MixContext),
  340. .priv_class = &tmix_class,
  341. .query_formats = query_formats,
  342. .outputs = outputs,
  343. .inputs = inputs,
  344. .init = init,
  345. .uninit = uninit,
  346. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  347. };
  348. #endif /* CONFIG_TMIX_FILTER */