vf_midequalizer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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_GBRAP10, AV_PIX_FMT_GBRAP12,
  71. AV_PIX_FMT_NONE
  72. };
  73. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  74. }
  75. static int process_frame(FFFrameSync *fs)
  76. {
  77. AVFilterContext *ctx = fs->parent;
  78. MidEqualizerContext *s = fs->opaque;
  79. AVFilterLink *outlink = ctx->outputs[0];
  80. AVFrame *out, *in0, *in1;
  81. int ret;
  82. if ((ret = ff_framesync_get_frame(&s->fs, 0, &in0, 0)) < 0 ||
  83. (ret = ff_framesync_get_frame(&s->fs, 1, &in1, 0)) < 0)
  84. return ret;
  85. if (ctx->is_disabled) {
  86. out = av_frame_clone(in0);
  87. if (!out)
  88. return AVERROR(ENOMEM);
  89. } else {
  90. int p;
  91. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  92. if (!out)
  93. return AVERROR(ENOMEM);
  94. av_frame_copy_props(out, in0);
  95. for (p = 0; p < s->nb_planes; p++) {
  96. if (!((1 << p) & s->planes)) {
  97. av_image_copy_plane(out->data[p], out->linesize[p], in0->data[p], in0->linesize[p],
  98. s->width[0][p] * (1 + (s->histogram_size > 256)), s->height[0][p]);
  99. continue;
  100. }
  101. s->midequalizer(in0->data[p], in1->data[p],
  102. out->data[p],
  103. in0->linesize[p], in1->linesize[p],
  104. out->linesize[p],
  105. s->width[0][p], s->height[0][p],
  106. s->width[1][p], s->height[1][p],
  107. s->histogram[0], s->histogram[1],
  108. s->cchange, s->histogram_size);
  109. }
  110. }
  111. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  112. return ff_filter_frame(outlink, out);
  113. }
  114. static void compute_histogram8(const uint8_t *src, ptrdiff_t linesize,
  115. int w, int h, float *histogram, size_t hsize)
  116. {
  117. int y, x;
  118. memset(histogram, 0, hsize * sizeof(*histogram));
  119. for (y = 0; y < h; y++) {
  120. for (x = 0; x < w; x++) {
  121. histogram[src[x]] += 1;
  122. }
  123. src += linesize;
  124. }
  125. for (x = 0; x < hsize - 1; x++) {
  126. histogram[x + 1] += histogram[x];
  127. histogram[x] /= hsize;
  128. }
  129. histogram[x] /= hsize;
  130. }
  131. static void compute_histogram16(const uint16_t *src, ptrdiff_t linesize,
  132. int w, int h, float *histogram, size_t hsize)
  133. {
  134. int y, x;
  135. memset(histogram, 0, hsize * sizeof(*histogram));
  136. for (y = 0; y < h; y++) {
  137. for (x = 0; x < w; x++) {
  138. histogram[src[x]] += 1;
  139. }
  140. src += linesize;
  141. }
  142. for (x = 0; x < hsize - 1; x++) {
  143. histogram[x + 1] += histogram[x];
  144. histogram[x] /= hsize;
  145. }
  146. histogram[x] /= hsize;
  147. }
  148. static void compute_contrast_change(float *histogram1, float *histogram2,
  149. unsigned *cchange, size_t hsize)
  150. {
  151. int i;
  152. for (i = 0; i < hsize; i++) {
  153. int j;
  154. for (j = 0; j < hsize && histogram2[j] < histogram1[i]; j++);
  155. cchange[i] = (i + j) / 2;
  156. }
  157. }
  158. static void midequalizer8(const uint8_t *in0, const uint8_t *in1,
  159. uint8_t *dst,
  160. ptrdiff_t linesize1, ptrdiff_t linesize2,
  161. ptrdiff_t dlinesize,
  162. int w0, int h0,
  163. int w1, int h1,
  164. float *histogram1, float *histogram2,
  165. unsigned *cchange,
  166. size_t hsize)
  167. {
  168. int x, y;
  169. compute_histogram8(in0, linesize1, w0, h0, histogram1, hsize);
  170. compute_histogram8(in1, linesize2, w1, h1, histogram2, hsize);
  171. compute_contrast_change(histogram1, histogram2, cchange, hsize);
  172. for (y = 0; y < h0; y++) {
  173. for (x = 0; x < w0; x++) {
  174. dst[x] = av_clip_uint8(cchange[in0[x]]);
  175. }
  176. dst += dlinesize;
  177. in0 += linesize1;
  178. }
  179. }
  180. static void midequalizer16(const uint8_t *in0, const uint8_t *in1,
  181. uint8_t *dst,
  182. ptrdiff_t linesize1, ptrdiff_t linesize2,
  183. ptrdiff_t dlinesize,
  184. int w0, int h0,
  185. int w1, int h1,
  186. float *histogram1, float *histogram2,
  187. unsigned *cchange,
  188. size_t hsize)
  189. {
  190. const uint16_t *i = (const uint16_t *)in0;
  191. uint16_t *d = (uint16_t *)dst;
  192. int x, y;
  193. compute_histogram16(i, linesize1 / 2, w0, h0, histogram1, hsize);
  194. compute_histogram16((const uint16_t *)in1, linesize2 / 2, w1, h1, histogram2, hsize);
  195. compute_contrast_change(histogram1, histogram2, cchange, hsize);
  196. for (y = 0; y < h0; y++) {
  197. for (x = 0; x < w0; x++) {
  198. d[x] = cchange[i[x]];
  199. }
  200. d += dlinesize / 2;
  201. i += linesize1 / 2;
  202. }
  203. }
  204. static int config_input0(AVFilterLink *inlink)
  205. {
  206. AVFilterContext *ctx = inlink->dst;
  207. MidEqualizerContext *s = ctx->priv;
  208. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  209. int vsub, hsub;
  210. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  211. hsub = desc->log2_chroma_w;
  212. vsub = desc->log2_chroma_h;
  213. s->height[0][0] = s->height[0][3] = inlink->h;
  214. s->width[0][0] = s->width[0][3] = inlink->w;
  215. s->height[0][1] = s->height[0][2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  216. s->width[0][1] = s->width[0][2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  217. s->histogram_size = 1 << desc->comp[0].depth;
  218. s->histogram[0] = av_calloc(s->histogram_size, sizeof(float));
  219. s->histogram[1] = av_calloc(s->histogram_size, sizeof(float));
  220. s->cchange = av_calloc(s->histogram_size, sizeof(unsigned));
  221. if (!s->histogram[0] || !s->histogram[1] || !s->cchange)
  222. return AVERROR(ENOMEM);
  223. if (s->histogram_size == 256) {
  224. s->midequalizer = midequalizer8;
  225. } else {
  226. s->midequalizer = midequalizer16;
  227. }
  228. return 0;
  229. }
  230. static int config_input1(AVFilterLink *inlink)
  231. {
  232. AVFilterContext *ctx = inlink->dst;
  233. MidEqualizerContext *s = ctx->priv;
  234. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  235. int vsub, hsub;
  236. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  237. hsub = desc->log2_chroma_w;
  238. vsub = desc->log2_chroma_h;
  239. s->height[1][0] = s->height[1][3] = inlink->h;
  240. s->width[1][0] = s->width[1][3] = inlink->w;
  241. s->height[1][1] = s->height[1][2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  242. s->width[1][1] = s->width[1][2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  243. return 0;
  244. }
  245. static int config_output(AVFilterLink *outlink)
  246. {
  247. AVFilterContext *ctx = outlink->src;
  248. MidEqualizerContext *s = ctx->priv;
  249. AVFilterLink *in0 = ctx->inputs[0];
  250. AVFilterLink *in1 = ctx->inputs[1];
  251. FFFrameSyncIn *in;
  252. int ret;
  253. if (in0->format != in1->format) {
  254. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  255. return AVERROR(EINVAL);
  256. }
  257. outlink->w = in0->w;
  258. outlink->h = in0->h;
  259. outlink->sample_aspect_ratio = in0->sample_aspect_ratio;
  260. outlink->frame_rate = in0->frame_rate;
  261. if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
  262. return ret;
  263. in = s->fs.in;
  264. in[0].time_base = in0->time_base;
  265. in[1].time_base = in1->time_base;
  266. in[0].sync = 1;
  267. in[0].before = EXT_STOP;
  268. in[0].after = EXT_INFINITY;
  269. in[1].sync = 1;
  270. in[1].before = EXT_STOP;
  271. in[1].after = EXT_INFINITY;
  272. s->fs.opaque = s;
  273. s->fs.on_event = process_frame;
  274. ret = ff_framesync_configure(&s->fs);
  275. outlink->time_base = s->fs.time_base;
  276. return ret;
  277. }
  278. static int activate(AVFilterContext *ctx)
  279. {
  280. MidEqualizerContext *s = ctx->priv;
  281. return ff_framesync_activate(&s->fs);
  282. }
  283. static av_cold void uninit(AVFilterContext *ctx)
  284. {
  285. MidEqualizerContext *s = ctx->priv;
  286. ff_framesync_uninit(&s->fs);
  287. av_freep(&s->histogram[0]);
  288. av_freep(&s->histogram[1]);
  289. av_freep(&s->cchange);
  290. }
  291. static const AVFilterPad midequalizer_inputs[] = {
  292. {
  293. .name = "in0",
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .config_props = config_input0,
  296. },
  297. {
  298. .name = "in1",
  299. .type = AVMEDIA_TYPE_VIDEO,
  300. .config_props = config_input1,
  301. },
  302. { NULL }
  303. };
  304. static const AVFilterPad midequalizer_outputs[] = {
  305. {
  306. .name = "default",
  307. .type = AVMEDIA_TYPE_VIDEO,
  308. .config_props = config_output,
  309. },
  310. { NULL }
  311. };
  312. AVFilter ff_vf_midequalizer = {
  313. .name = "midequalizer",
  314. .description = NULL_IF_CONFIG_SMALL("Apply Midway Equalization."),
  315. .priv_size = sizeof(MidEqualizerContext),
  316. .uninit = uninit,
  317. .query_formats = query_formats,
  318. .activate = activate,
  319. .inputs = midequalizer_inputs,
  320. .outputs = midequalizer_outputs,
  321. .priv_class = &midequalizer_class,
  322. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  323. };