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.

374 lines
11KB

  1. /*
  2. * Copyright (c) 2016 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/attributes.h"
  21. #include "libavutil/common.h"
  22. #include "libavutil/eval.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "drawutils.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #include "framesync.h"
  31. static const char *const var_names[] = {
  32. "w", ///< width of the input video
  33. "h", ///< height of the input video
  34. "x", ///< input value for the pixel from input #1
  35. "y", ///< input value for the pixel from input #2
  36. NULL
  37. };
  38. enum var_name {
  39. VAR_W,
  40. VAR_H,
  41. VAR_X,
  42. VAR_Y,
  43. VAR_VARS_NB
  44. };
  45. typedef struct LUT2Context {
  46. const AVClass *class;
  47. char *comp_expr_str[4];
  48. AVExpr *comp_expr[4];
  49. double var_values[VAR_VARS_NB];
  50. uint16_t *lut[4]; ///< lookup table for each component
  51. int width[4], height[4];
  52. int nb_planes;
  53. int depth, depthx, depthy;
  54. void (*lut2)(struct LUT2Context *s, AVFrame *dst, AVFrame *srcx, AVFrame *srcy);
  55. FFFrameSync fs;
  56. } LUT2Context;
  57. #define OFFSET(x) offsetof(LUT2Context, x)
  58. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  59. static const AVOption lut2_options[] = {
  60. { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  61. { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  62. { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  63. { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
  64. { NULL }
  65. };
  66. static av_cold void uninit(AVFilterContext *ctx)
  67. {
  68. LUT2Context *s = ctx->priv;
  69. int i;
  70. for (i = 0; i < 4; i++) {
  71. av_expr_free(s->comp_expr[i]);
  72. s->comp_expr[i] = NULL;
  73. av_freep(&s->comp_expr_str[i]);
  74. av_freep(&s->lut[i]);
  75. }
  76. }
  77. static int query_formats(AVFilterContext *ctx)
  78. {
  79. static const enum AVPixelFormat pix_fmts[] = {
  80. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  81. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  82. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  83. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  84. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  85. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  86. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  87. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  88. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  89. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  90. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  91. AV_PIX_FMT_GBRP12,
  92. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP12,
  93. AV_PIX_FMT_GRAY8,
  94. AV_PIX_FMT_NONE
  95. };
  96. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  97. }
  98. static int config_inputx(AVFilterLink *inlink)
  99. {
  100. AVFilterContext *ctx = inlink->dst;
  101. LUT2Context *s = ctx->priv;
  102. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  103. int hsub = desc->log2_chroma_w;
  104. int vsub = desc->log2_chroma_h;
  105. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  106. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  107. s->height[0] = s->height[3] = inlink->h;
  108. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  109. s->width[0] = s->width[3] = inlink->w;
  110. s->var_values[VAR_W] = inlink->w;
  111. s->var_values[VAR_H] = inlink->h;
  112. s->depthx = desc->comp[0].depth;
  113. return 0;
  114. }
  115. static int config_inputy(AVFilterLink *inlink)
  116. {
  117. AVFilterContext *ctx = inlink->dst;
  118. LUT2Context *s = ctx->priv;
  119. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  120. s->depthy = desc->comp[0].depth;
  121. return 0;
  122. }
  123. static void lut2_8bit(struct LUT2Context *s, AVFrame *out, AVFrame *srcx, AVFrame *srcy)
  124. {
  125. int p, y, x;
  126. for (p = 0; p < s->nb_planes; p++) {
  127. const uint16_t *lut = s->lut[p];
  128. const uint8_t *srcxx, *srcyy;
  129. uint8_t *dst;
  130. dst = out->data[p];
  131. srcxx = srcx->data[p];
  132. srcyy = srcy->data[p];
  133. for (y = 0; y < s->height[p]; y++) {
  134. for (x = 0; x < s->width[p]; x++) {
  135. dst[x] = lut[(srcyy[x] << s->depthx) | srcxx[x]];
  136. }
  137. dst += out->linesize[p];
  138. srcxx += srcx->linesize[p];
  139. srcyy += srcy->linesize[p];
  140. }
  141. }
  142. }
  143. static void lut2_16bit(struct LUT2Context *s, AVFrame *out, AVFrame *srcx, AVFrame *srcy)
  144. {
  145. int p, y, x;
  146. for (p = 0; p < s->nb_planes; p++) {
  147. const uint16_t *lut = s->lut[p];
  148. const uint16_t *srcxx, *srcyy;
  149. uint16_t *dst;
  150. dst = (uint16_t *)out->data[p];
  151. srcxx = (uint16_t *)srcx->data[p];
  152. srcyy = (uint16_t *)srcy->data[p];
  153. for (y = 0; y < s->height[p]; y++) {
  154. for (x = 0; x < s->width[p]; x++) {
  155. dst[x] = lut[(srcyy[x] << s->depthx) | srcxx[x]];
  156. }
  157. dst += out->linesize[p] / 2;
  158. srcxx += srcx->linesize[p] / 2;
  159. srcyy += srcy->linesize[p] / 2;
  160. }
  161. }
  162. }
  163. static int process_frame(FFFrameSync *fs)
  164. {
  165. AVFilterContext *ctx = fs->parent;
  166. LUT2Context *s = fs->opaque;
  167. AVFilterLink *outlink = ctx->outputs[0];
  168. AVFrame *out, *srcx, *srcy;
  169. int ret;
  170. if ((ret = ff_framesync_get_frame(&s->fs, 0, &srcx, 0)) < 0 ||
  171. (ret = ff_framesync_get_frame(&s->fs, 1, &srcy, 0)) < 0)
  172. return ret;
  173. if (ctx->is_disabled) {
  174. out = av_frame_clone(srcx);
  175. if (!out)
  176. return AVERROR(ENOMEM);
  177. } else {
  178. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  179. if (!out)
  180. return AVERROR(ENOMEM);
  181. av_frame_copy_props(out, srcx);
  182. s->lut2(s, out, srcx, srcy);
  183. }
  184. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  185. return ff_filter_frame(outlink, out);
  186. }
  187. static int config_output(AVFilterLink *outlink)
  188. {
  189. AVFilterContext *ctx = outlink->src;
  190. LUT2Context *s = ctx->priv;
  191. AVFilterLink *srcx = ctx->inputs[0];
  192. AVFilterLink *srcy = ctx->inputs[1];
  193. FFFrameSyncIn *in;
  194. int p, ret;
  195. s->depth = s->depthx + s->depthy;
  196. if (srcx->format != srcy->format) {
  197. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  198. return AVERROR(EINVAL);
  199. }
  200. if (srcx->w != srcy->w ||
  201. srcx->h != srcy->h ||
  202. srcx->sample_aspect_ratio.num != srcy->sample_aspect_ratio.num ||
  203. srcx->sample_aspect_ratio.den != srcy->sample_aspect_ratio.den) {
  204. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  205. "(size %dx%d, SAR %d:%d) do not match the corresponding "
  206. "second input link %s parameters (%dx%d, SAR %d:%d)\n",
  207. ctx->input_pads[0].name, srcx->w, srcx->h,
  208. srcx->sample_aspect_ratio.num,
  209. srcx->sample_aspect_ratio.den,
  210. ctx->input_pads[1].name,
  211. srcy->w, srcy->h,
  212. srcy->sample_aspect_ratio.num,
  213. srcy->sample_aspect_ratio.den);
  214. return AVERROR(EINVAL);
  215. }
  216. outlink->w = srcx->w;
  217. outlink->h = srcx->h;
  218. outlink->time_base = srcx->time_base;
  219. outlink->sample_aspect_ratio = srcx->sample_aspect_ratio;
  220. outlink->frame_rate = srcx->frame_rate;
  221. if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
  222. return ret;
  223. in = s->fs.in;
  224. in[0].time_base = srcx->time_base;
  225. in[1].time_base = srcy->time_base;
  226. in[0].sync = 1;
  227. in[0].before = EXT_STOP;
  228. in[0].after = EXT_INFINITY;
  229. in[1].sync = 1;
  230. in[1].before = EXT_STOP;
  231. in[1].after = EXT_INFINITY;
  232. s->fs.opaque = s;
  233. s->fs.on_event = process_frame;
  234. s->lut2 = s->depth > 16 ? lut2_16bit : lut2_8bit;
  235. for (p = 0; p < s->nb_planes; p++) {
  236. s->lut[p] = av_malloc_array(1 << s->depth, sizeof(uint16_t));
  237. if (!s->lut[p])
  238. return AVERROR(ENOMEM);
  239. }
  240. for (p = 0; p < s->nb_planes; p++) {
  241. double res;
  242. int x, y;
  243. /* create the parsed expression */
  244. av_expr_free(s->comp_expr[p]);
  245. s->comp_expr[p] = NULL;
  246. ret = av_expr_parse(&s->comp_expr[p], s->comp_expr_str[p],
  247. var_names, NULL, NULL, NULL, NULL, 0, ctx);
  248. if (ret < 0) {
  249. av_log(ctx, AV_LOG_ERROR,
  250. "Error when parsing the expression '%s' for the component %d.\n",
  251. s->comp_expr_str[p], p);
  252. return AVERROR(EINVAL);
  253. }
  254. /* compute the lut */
  255. for (y = 0; y < (1 << s->depthx); y++) {
  256. s->var_values[VAR_Y] = y;
  257. for (x = 0; x < (1 << s->depthx); x++) {
  258. s->var_values[VAR_X] = x;
  259. res = av_expr_eval(s->comp_expr[p], s->var_values, s);
  260. if (isnan(res)) {
  261. av_log(ctx, AV_LOG_ERROR,
  262. "Error when evaluating the expression '%s' for the values %d and %d for the component %d.\n",
  263. s->comp_expr_str[p], x, y, p);
  264. return AVERROR(EINVAL);
  265. }
  266. s->lut[p][(y << s->depthx) + x] = res;
  267. }
  268. }
  269. }
  270. return ff_framesync_configure(&s->fs);
  271. }
  272. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  273. {
  274. LUT2Context *s = inlink->dst->priv;
  275. return ff_framesync_filter_frame(&s->fs, inlink, buf);
  276. }
  277. static int request_frame(AVFilterLink *outlink)
  278. {
  279. LUT2Context *s = outlink->src->priv;
  280. return ff_framesync_request_frame(&s->fs, outlink);
  281. }
  282. static const AVFilterPad inputs[] = {
  283. {
  284. .name = "srcx",
  285. .type = AVMEDIA_TYPE_VIDEO,
  286. .filter_frame = filter_frame,
  287. .config_props = config_inputx,
  288. },
  289. {
  290. .name = "srcy",
  291. .type = AVMEDIA_TYPE_VIDEO,
  292. .filter_frame = filter_frame,
  293. .config_props = config_inputy,
  294. },
  295. { NULL }
  296. };
  297. static const AVFilterPad outputs[] = {
  298. {
  299. .name = "default",
  300. .type = AVMEDIA_TYPE_VIDEO,
  301. .config_props = config_output,
  302. .request_frame = request_frame,
  303. },
  304. { NULL }
  305. };
  306. AVFILTER_DEFINE_CLASS(lut2);
  307. AVFilter ff_vf_lut2 = {
  308. .name = "lut2",
  309. .description = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two video inputs."),
  310. .priv_size = sizeof(LUT2Context),
  311. .priv_class = &lut2_class,
  312. .uninit = uninit,
  313. .query_formats = query_formats,
  314. .inputs = inputs,
  315. .outputs = outputs,
  316. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  317. };