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.

453 lines
13KB

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