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.

390 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/imgutils.h"
  21. #include "libavutil/pixdesc.h"
  22. #include "libavutil/opt.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. #include "framesync.h"
  28. typedef struct MidEqualizerContext {
  29. const AVClass *class;
  30. int width[2][4], height[2][4];
  31. int nb_planes;
  32. int planes;
  33. int histogram_size;
  34. float *histogram[2];
  35. unsigned *cchange;
  36. FFFrameSync fs;
  37. void (*midequalizer)(const uint8_t *in0, const uint8_t *in1,
  38. uint8_t *dst,
  39. ptrdiff_t linesize1, ptrdiff_t linesize2,
  40. ptrdiff_t dlinesize,
  41. int w0, int h0,
  42. int w1, int h1,
  43. float *histogram1, float *histogram2,
  44. unsigned *cchange, size_t hsize);
  45. } MidEqualizerContext;
  46. #define OFFSET(x) offsetof(MidEqualizerContext, x)
  47. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  48. static const AVOption midequalizer_options[] = {
  49. { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  50. { NULL }
  51. };
  52. AVFILTER_DEFINE_CLASS(midequalizer);
  53. static int query_formats(AVFilterContext *ctx)
  54. {
  55. static const enum AVPixelFormat pix_fmts[] = {
  56. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  57. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  58. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  59. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  60. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  61. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  62. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
  63. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  64. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  65. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  66. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  67. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
  68. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  69. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  70. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  71. AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12,
  72. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  73. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  74. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
  75. AV_PIX_FMT_GRAY16,
  76. AV_PIX_FMT_NONE
  77. };
  78. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  79. }
  80. static int process_frame(FFFrameSync *fs)
  81. {
  82. AVFilterContext *ctx = fs->parent;
  83. MidEqualizerContext *s = fs->opaque;
  84. AVFilterLink *outlink = ctx->outputs[0];
  85. AVFrame *out, *in0, *in1;
  86. int ret;
  87. if ((ret = ff_framesync_get_frame(&s->fs, 0, &in0, 0)) < 0 ||
  88. (ret = ff_framesync_get_frame(&s->fs, 1, &in1, 0)) < 0)
  89. return ret;
  90. if (ctx->is_disabled) {
  91. out = av_frame_clone(in0);
  92. if (!out)
  93. return AVERROR(ENOMEM);
  94. } else {
  95. int p;
  96. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  97. if (!out)
  98. return AVERROR(ENOMEM);
  99. av_frame_copy_props(out, in0);
  100. for (p = 0; p < s->nb_planes; p++) {
  101. if (!((1 << p) & s->planes)) {
  102. av_image_copy_plane(out->data[p], out->linesize[p], in0->data[p], in0->linesize[p],
  103. s->width[0][p] * (1 + (s->histogram_size > 256)), s->height[0][p]);
  104. continue;
  105. }
  106. s->midequalizer(in0->data[p], in1->data[p],
  107. out->data[p],
  108. in0->linesize[p], in1->linesize[p],
  109. out->linesize[p],
  110. s->width[0][p], s->height[0][p],
  111. s->width[1][p], s->height[1][p],
  112. s->histogram[0], s->histogram[1],
  113. s->cchange, s->histogram_size);
  114. }
  115. }
  116. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  117. return ff_filter_frame(outlink, out);
  118. }
  119. static void compute_histogram8(const uint8_t *src, ptrdiff_t linesize,
  120. int w, int h, float *histogram, size_t hsize)
  121. {
  122. int y, x;
  123. memset(histogram, 0, hsize * sizeof(*histogram));
  124. for (y = 0; y < h; y++) {
  125. for (x = 0; x < w; x++) {
  126. histogram[src[x]] += 1;
  127. }
  128. src += linesize;
  129. }
  130. for (x = 0; x < hsize - 1; x++) {
  131. histogram[x + 1] += histogram[x];
  132. histogram[x] /= hsize;
  133. }
  134. histogram[x] /= hsize;
  135. }
  136. static void compute_histogram16(const uint16_t *src, ptrdiff_t linesize,
  137. int w, int h, float *histogram, size_t hsize)
  138. {
  139. int y, x;
  140. memset(histogram, 0, hsize * sizeof(*histogram));
  141. for (y = 0; y < h; y++) {
  142. for (x = 0; x < w; x++) {
  143. histogram[src[x]] += 1;
  144. }
  145. src += linesize;
  146. }
  147. for (x = 0; x < hsize - 1; x++) {
  148. histogram[x + 1] += histogram[x];
  149. histogram[x] /= hsize;
  150. }
  151. histogram[x] /= hsize;
  152. }
  153. static void compute_contrast_change(float *histogram1, float *histogram2,
  154. unsigned *cchange, size_t hsize)
  155. {
  156. int i;
  157. for (i = 0; i < hsize; i++) {
  158. int j;
  159. for (j = 0; j < hsize && histogram2[j] < histogram1[i]; j++);
  160. cchange[i] = (i + j) / 2;
  161. }
  162. }
  163. static void midequalizer8(const uint8_t *in0, const uint8_t *in1,
  164. uint8_t *dst,
  165. ptrdiff_t linesize1, ptrdiff_t linesize2,
  166. ptrdiff_t dlinesize,
  167. int w0, int h0,
  168. int w1, int h1,
  169. float *histogram1, float *histogram2,
  170. unsigned *cchange,
  171. size_t hsize)
  172. {
  173. int x, y;
  174. compute_histogram8(in0, linesize1, w0, h0, histogram1, hsize);
  175. compute_histogram8(in1, linesize2, w1, h1, histogram2, hsize);
  176. compute_contrast_change(histogram1, histogram2, cchange, hsize);
  177. for (y = 0; y < h0; y++) {
  178. for (x = 0; x < w0; x++) {
  179. dst[x] = av_clip_uint8(cchange[in0[x]]);
  180. }
  181. dst += dlinesize;
  182. in0 += linesize1;
  183. }
  184. }
  185. static void midequalizer16(const uint8_t *in0, const uint8_t *in1,
  186. uint8_t *dst,
  187. ptrdiff_t linesize1, ptrdiff_t linesize2,
  188. ptrdiff_t dlinesize,
  189. int w0, int h0,
  190. int w1, int h1,
  191. float *histogram1, float *histogram2,
  192. unsigned *cchange,
  193. size_t hsize)
  194. {
  195. const uint16_t *i = (const uint16_t *)in0;
  196. uint16_t *d = (uint16_t *)dst;
  197. int x, y;
  198. compute_histogram16(i, linesize1 / 2, w0, h0, histogram1, hsize);
  199. compute_histogram16((const uint16_t *)in1, linesize2 / 2, w1, h1, histogram2, hsize);
  200. compute_contrast_change(histogram1, histogram2, cchange, hsize);
  201. for (y = 0; y < h0; y++) {
  202. for (x = 0; x < w0; x++) {
  203. d[x] = cchange[i[x]];
  204. }
  205. d += dlinesize / 2;
  206. i += linesize1 / 2;
  207. }
  208. }
  209. static int config_input0(AVFilterLink *inlink)
  210. {
  211. AVFilterContext *ctx = inlink->dst;
  212. MidEqualizerContext *s = ctx->priv;
  213. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  214. int vsub, hsub;
  215. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  216. hsub = desc->log2_chroma_w;
  217. vsub = desc->log2_chroma_h;
  218. s->height[0][0] = s->height[0][3] = inlink->h;
  219. s->width[0][0] = s->width[0][3] = inlink->w;
  220. s->height[0][1] = s->height[0][2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  221. s->width[0][1] = s->width[0][2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  222. s->histogram_size = 1 << desc->comp[0].depth;
  223. s->histogram[0] = av_calloc(s->histogram_size, sizeof(float));
  224. s->histogram[1] = av_calloc(s->histogram_size, sizeof(float));
  225. s->cchange = av_calloc(s->histogram_size, sizeof(unsigned));
  226. if (!s->histogram[0] || !s->histogram[1] || !s->cchange)
  227. return AVERROR(ENOMEM);
  228. if (s->histogram_size == 256) {
  229. s->midequalizer = midequalizer8;
  230. } else {
  231. s->midequalizer = midequalizer16;
  232. }
  233. return 0;
  234. }
  235. static int config_input1(AVFilterLink *inlink)
  236. {
  237. AVFilterContext *ctx = inlink->dst;
  238. MidEqualizerContext *s = ctx->priv;
  239. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  240. int vsub, hsub;
  241. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  242. hsub = desc->log2_chroma_w;
  243. vsub = desc->log2_chroma_h;
  244. s->height[1][0] = s->height[1][3] = inlink->h;
  245. s->width[1][0] = s->width[1][3] = inlink->w;
  246. s->height[1][1] = s->height[1][2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  247. s->width[1][1] = s->width[1][2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  248. return 0;
  249. }
  250. static int config_output(AVFilterLink *outlink)
  251. {
  252. AVFilterContext *ctx = outlink->src;
  253. MidEqualizerContext *s = ctx->priv;
  254. AVFilterLink *in0 = ctx->inputs[0];
  255. AVFilterLink *in1 = ctx->inputs[1];
  256. FFFrameSyncIn *in;
  257. int ret;
  258. if (in0->format != in1->format) {
  259. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  260. return AVERROR(EINVAL);
  261. }
  262. outlink->w = in0->w;
  263. outlink->h = in0->h;
  264. outlink->sample_aspect_ratio = in0->sample_aspect_ratio;
  265. outlink->frame_rate = in0->frame_rate;
  266. if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
  267. return ret;
  268. in = s->fs.in;
  269. in[0].time_base = in0->time_base;
  270. in[1].time_base = in1->time_base;
  271. in[0].sync = 1;
  272. in[0].before = EXT_STOP;
  273. in[0].after = EXT_INFINITY;
  274. in[1].sync = 1;
  275. in[1].before = EXT_STOP;
  276. in[1].after = EXT_INFINITY;
  277. s->fs.opaque = s;
  278. s->fs.on_event = process_frame;
  279. ret = ff_framesync_configure(&s->fs);
  280. outlink->time_base = s->fs.time_base;
  281. return ret;
  282. }
  283. static int activate(AVFilterContext *ctx)
  284. {
  285. MidEqualizerContext *s = ctx->priv;
  286. return ff_framesync_activate(&s->fs);
  287. }
  288. static av_cold void uninit(AVFilterContext *ctx)
  289. {
  290. MidEqualizerContext *s = ctx->priv;
  291. ff_framesync_uninit(&s->fs);
  292. av_freep(&s->histogram[0]);
  293. av_freep(&s->histogram[1]);
  294. av_freep(&s->cchange);
  295. }
  296. static const AVFilterPad midequalizer_inputs[] = {
  297. {
  298. .name = "in0",
  299. .type = AVMEDIA_TYPE_VIDEO,
  300. .config_props = config_input0,
  301. },
  302. {
  303. .name = "in1",
  304. .type = AVMEDIA_TYPE_VIDEO,
  305. .config_props = config_input1,
  306. },
  307. { NULL }
  308. };
  309. static const AVFilterPad midequalizer_outputs[] = {
  310. {
  311. .name = "default",
  312. .type = AVMEDIA_TYPE_VIDEO,
  313. .config_props = config_output,
  314. },
  315. { NULL }
  316. };
  317. AVFilter ff_vf_midequalizer = {
  318. .name = "midequalizer",
  319. .description = NULL_IF_CONFIG_SMALL("Apply Midway Equalization."),
  320. .priv_size = sizeof(MidEqualizerContext),
  321. .uninit = uninit,
  322. .query_formats = query_formats,
  323. .activate = activate,
  324. .inputs = midequalizer_inputs,
  325. .outputs = midequalizer_outputs,
  326. .priv_class = &midequalizer_class,
  327. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  328. };