vf_deflicker.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/qsort.h"
  24. #include "avfilter.h"
  25. #define FF_BUFQUEUE_SIZE 129
  26. #include "bufferqueue.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #define SIZE FF_BUFQUEUE_SIZE
  31. enum smooth_mode {
  32. ARITHMETIC_MEAN,
  33. GEOMETRIC_MEAN,
  34. HARMONIC_MEAN,
  35. QUADRATIC_MEAN,
  36. CUBIC_MEAN,
  37. POWER_MEAN,
  38. MEDIAN,
  39. NB_SMOOTH_MODE,
  40. };
  41. typedef struct DeflickerContext {
  42. const AVClass *class;
  43. int size;
  44. int mode;
  45. int bypass;
  46. int eof;
  47. int depth;
  48. int nb_planes;
  49. int planewidth[4];
  50. int planeheight[4];
  51. uint64_t *histogram;
  52. float luminance[SIZE];
  53. float sorted[SIZE];
  54. struct FFBufQueue q;
  55. int available;
  56. void (*get_factor)(AVFilterContext *ctx, float *f);
  57. float (*calc_avgy)(AVFilterContext *ctx, AVFrame *in);
  58. int (*deflicker)(AVFilterContext *ctx, const uint8_t *src, ptrdiff_t src_linesize,
  59. uint8_t *dst, ptrdiff_t dst_linesize, int w, int h, float f);
  60. } DeflickerContext;
  61. #define OFFSET(x) offsetof(DeflickerContext, x)
  62. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  63. static const AVOption deflicker_options[] = {
  64. { "size", "set how many frames to use", OFFSET(size), AV_OPT_TYPE_INT, {.i64=5}, 2, SIZE, FLAGS },
  65. { "s", "set how many frames to use", OFFSET(size), AV_OPT_TYPE_INT, {.i64=5}, 2, SIZE, FLAGS },
  66. { "mode", "set how to smooth luminance", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SMOOTH_MODE-1, FLAGS, "mode" },
  67. { "m", "set how to smooth luminance", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SMOOTH_MODE-1, FLAGS, "mode" },
  68. { "am", "arithmetic mean", 0, AV_OPT_TYPE_CONST, {.i64=ARITHMETIC_MEAN}, 0, 0, FLAGS, "mode" },
  69. { "gm", "geometric mean", 0, AV_OPT_TYPE_CONST, {.i64=GEOMETRIC_MEAN}, 0, 0, FLAGS, "mode" },
  70. { "hm", "harmonic mean", 0, AV_OPT_TYPE_CONST, {.i64=HARMONIC_MEAN}, 0, 0, FLAGS, "mode" },
  71. { "qm", "quadratic mean", 0, AV_OPT_TYPE_CONST, {.i64=QUADRATIC_MEAN}, 0, 0, FLAGS, "mode" },
  72. { "cm", "cubic mean", 0, AV_OPT_TYPE_CONST, {.i64=CUBIC_MEAN}, 0, 0, FLAGS, "mode" },
  73. { "pm", "power mean", 0, AV_OPT_TYPE_CONST, {.i64=POWER_MEAN}, 0, 0, FLAGS, "mode" },
  74. { "median", "median", 0, AV_OPT_TYPE_CONST, {.i64=MEDIAN}, 0, 0, FLAGS, "mode" },
  75. { "bypass", "leave frames unchanged", OFFSET(bypass), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  76. { NULL }
  77. };
  78. AVFILTER_DEFINE_CLASS(deflicker);
  79. static int query_formats(AVFilterContext *ctx)
  80. {
  81. static const enum AVPixelFormat pixel_fmts[] = {
  82. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
  83. AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  84. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  85. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  86. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  87. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  88. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  89. AV_PIX_FMT_YUVJ411P,
  90. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  91. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  92. AV_PIX_FMT_YUV440P10,
  93. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  94. AV_PIX_FMT_YUV440P12,
  95. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  96. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  97. AV_PIX_FMT_NONE
  98. };
  99. AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
  100. if (!formats)
  101. return AVERROR(ENOMEM);
  102. return ff_set_common_formats(ctx, formats);
  103. }
  104. static int deflicker8(AVFilterContext *ctx,
  105. const uint8_t *src, ptrdiff_t src_linesize,
  106. uint8_t *dst, ptrdiff_t dst_linesize,
  107. int w, int h, float f)
  108. {
  109. int x, y;
  110. for (y = 0; y < h; y++) {
  111. for (x = 0; x < w; x++) {
  112. dst[x] = av_clip_uint8(src[x] * f);
  113. }
  114. dst += dst_linesize;
  115. src += src_linesize;
  116. }
  117. return 0;
  118. }
  119. static int deflicker16(AVFilterContext *ctx,
  120. const uint8_t *ssrc, ptrdiff_t src_linesize,
  121. uint8_t *ddst, ptrdiff_t dst_linesize,
  122. int w, int h, float f)
  123. {
  124. DeflickerContext *s = ctx->priv;
  125. const uint16_t *src = (const uint16_t *)ssrc;
  126. uint16_t *dst = (uint16_t *)ddst;
  127. const int max = (1 << s->depth) - 1;
  128. int x, y;
  129. for (y = 0; y < h; y++) {
  130. for (x = 0; x < w; x++) {
  131. dst[x] = av_clip(src[x] * f, 0, max);
  132. }
  133. dst += dst_linesize / 2;
  134. src += src_linesize / 2;
  135. }
  136. return 0;
  137. }
  138. static float calc_avgy8(AVFilterContext *ctx, AVFrame *in)
  139. {
  140. DeflickerContext *s = ctx->priv;
  141. const uint8_t *src = in->data[0];
  142. int64_t sum = 0;
  143. int y, x;
  144. memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram));
  145. for (y = 0; y < s->planeheight[0]; y++) {
  146. for (x = 0; x < s->planewidth[0]; x++) {
  147. s->histogram[src[x]]++;
  148. }
  149. src += in->linesize[0];
  150. }
  151. for (y = 0; y < 1 << s->depth; y++) {
  152. sum += s->histogram[y] * y;
  153. }
  154. return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]);
  155. }
  156. static float calc_avgy16(AVFilterContext *ctx, AVFrame *in)
  157. {
  158. DeflickerContext *s = ctx->priv;
  159. const uint16_t *src = (const uint16_t *)in->data[0];
  160. int64_t sum = 0;
  161. int y, x;
  162. memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram));
  163. for (y = 0; y < s->planeheight[0]; y++) {
  164. for (x = 0; x < s->planewidth[0]; x++) {
  165. s->histogram[src[x]]++;
  166. }
  167. src += in->linesize[0] / 2;
  168. }
  169. for (y = 0; y < 1 << s->depth; y++) {
  170. sum += s->histogram[y] * y;
  171. }
  172. return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]);
  173. }
  174. static void get_am_factor(AVFilterContext *ctx, float *f)
  175. {
  176. DeflickerContext *s = ctx->priv;
  177. int y;
  178. *f = 0.0f;
  179. for (y = 0; y < s->size; y++) {
  180. *f += s->luminance[y];
  181. }
  182. *f /= s->size;
  183. *f /= s->luminance[0];
  184. }
  185. static void get_gm_factor(AVFilterContext *ctx, float *f)
  186. {
  187. DeflickerContext *s = ctx->priv;
  188. int y;
  189. *f = 1;
  190. for (y = 0; y < s->size; y++) {
  191. *f *= s->luminance[y];
  192. }
  193. *f = pow(*f, 1.0f / s->size);
  194. *f /= s->luminance[0];
  195. }
  196. static void get_hm_factor(AVFilterContext *ctx, float *f)
  197. {
  198. DeflickerContext *s = ctx->priv;
  199. int y;
  200. *f = 0.0f;
  201. for (y = 0; y < s->size; y++) {
  202. *f += 1.0f / s->luminance[y];
  203. }
  204. *f = s->size / *f;
  205. *f /= s->luminance[0];
  206. }
  207. static void get_qm_factor(AVFilterContext *ctx, float *f)
  208. {
  209. DeflickerContext *s = ctx->priv;
  210. int y;
  211. *f = 0.0f;
  212. for (y = 0; y < s->size; y++) {
  213. *f += s->luminance[y] * s->luminance[y];
  214. }
  215. *f /= s->size;
  216. *f = sqrtf(*f);
  217. *f /= s->luminance[0];
  218. }
  219. static void get_cm_factor(AVFilterContext *ctx, float *f)
  220. {
  221. DeflickerContext *s = ctx->priv;
  222. int y;
  223. *f = 0.0f;
  224. for (y = 0; y < s->size; y++) {
  225. *f += s->luminance[y] * s->luminance[y] * s->luminance[y];
  226. }
  227. *f /= s->size;
  228. *f = cbrtf(*f);
  229. *f /= s->luminance[0];
  230. }
  231. static void get_pm_factor(AVFilterContext *ctx, float *f)
  232. {
  233. DeflickerContext *s = ctx->priv;
  234. int y;
  235. *f = 0.0f;
  236. for (y = 0; y < s->size; y++) {
  237. *f += powf(s->luminance[y], s->size);
  238. }
  239. *f /= s->size;
  240. *f = powf(*f, 1.0f / s->size);
  241. *f /= s->luminance[0];
  242. }
  243. static int comparef(const void *a, const void *b)
  244. {
  245. const float *aa = a, *bb = b;
  246. return round(aa - bb);
  247. }
  248. static void get_median_factor(AVFilterContext *ctx, float *f)
  249. {
  250. DeflickerContext *s = ctx->priv;
  251. memcpy(s->sorted, s->luminance, sizeof(s->sorted));
  252. AV_QSORT(s->sorted, s->size, float, comparef);
  253. *f = s->sorted[s->size >> 1] / s->luminance[0];
  254. }
  255. static int config_input(AVFilterLink *inlink)
  256. {
  257. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  258. AVFilterContext *ctx = inlink->dst;
  259. DeflickerContext *s = ctx->priv;
  260. s->nb_planes = desc->nb_components;
  261. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  262. s->planeheight[0] = s->planeheight[3] = inlink->h;
  263. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  264. s->planewidth[0] = s->planewidth[3] = inlink->w;
  265. s->depth = desc->comp[0].depth;
  266. if (s->depth == 8) {
  267. s->deflicker = deflicker8;
  268. s->calc_avgy = calc_avgy8;
  269. } else {
  270. s->deflicker = deflicker16;
  271. s->calc_avgy = calc_avgy16;
  272. }
  273. s->histogram = av_calloc(1 << s->depth, sizeof(*s->histogram));
  274. if (!s->histogram)
  275. return AVERROR(ENOMEM);
  276. switch (s->mode) {
  277. case MEDIAN: s->get_factor = get_median_factor; break;
  278. case ARITHMETIC_MEAN: s->get_factor = get_am_factor; break;
  279. case GEOMETRIC_MEAN: s->get_factor = get_gm_factor; break;
  280. case HARMONIC_MEAN: s->get_factor = get_hm_factor; break;
  281. case QUADRATIC_MEAN: s->get_factor = get_qm_factor; break;
  282. case CUBIC_MEAN: s->get_factor = get_cm_factor; break;
  283. case POWER_MEAN: s->get_factor = get_pm_factor; break;
  284. }
  285. return 0;
  286. }
  287. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  288. {
  289. AVFilterContext *ctx = inlink->dst;
  290. AVFilterLink *outlink = ctx->outputs[0];
  291. DeflickerContext *s = ctx->priv;
  292. AVDictionary **metadata;
  293. AVFrame *out, *in;
  294. float f;
  295. int y;
  296. if (s->q.available < s->size && !s->eof) {
  297. s->luminance[s->available] = s->calc_avgy(ctx, buf);
  298. ff_bufqueue_add(ctx, &s->q, buf);
  299. s->available++;
  300. return 0;
  301. }
  302. in = ff_bufqueue_peek(&s->q, 0);
  303. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  304. if (!out) {
  305. av_frame_free(&buf);
  306. return AVERROR(ENOMEM);
  307. }
  308. s->get_factor(ctx, &f);
  309. if (!s->bypass)
  310. s->deflicker(ctx, in->data[0], in->linesize[0], out->data[0], out->linesize[0],
  311. outlink->w, outlink->h, f);
  312. for (y = 1 - s->bypass; y < s->nb_planes; y++) {
  313. av_image_copy_plane(out->data[y], out->linesize[y],
  314. in->data[y], in->linesize[y],
  315. s->planewidth[y] * (1 + (s->depth > 8)), s->planeheight[y]);
  316. }
  317. av_frame_copy_props(out, in);
  318. metadata = &out->metadata;
  319. if (metadata) {
  320. uint8_t value[128];
  321. snprintf(value, sizeof(value), "%f", s->luminance[0]);
  322. av_dict_set(metadata, "lavfi.deflicker.luminance", value, 0);
  323. snprintf(value, sizeof(value), "%f", s->luminance[0] * f);
  324. av_dict_set(metadata, "lavfi.deflicker.new_luminance", value, 0);
  325. snprintf(value, sizeof(value), "%f", f - 1.0f);
  326. av_dict_set(metadata, "lavfi.deflicker.relative_change", value, 0);
  327. }
  328. in = ff_bufqueue_get(&s->q);
  329. av_frame_free(&in);
  330. memmove(&s->luminance[0], &s->luminance[1], sizeof(*s->luminance) * (s->size - 1));
  331. s->luminance[s->available - 1] = s->calc_avgy(ctx, buf);
  332. ff_bufqueue_add(ctx, &s->q, buf);
  333. return ff_filter_frame(outlink, out);
  334. }
  335. static int request_frame(AVFilterLink *outlink)
  336. {
  337. AVFilterContext *ctx = outlink->src;
  338. DeflickerContext *s = ctx->priv;
  339. int ret;
  340. ret = ff_request_frame(ctx->inputs[0]);
  341. if (ret == AVERROR_EOF && s->available > 0) {
  342. AVFrame *buf = av_frame_clone(ff_bufqueue_peek(&s->q, s->size - 1));
  343. if (!buf)
  344. return AVERROR(ENOMEM);
  345. s->eof = 1;
  346. ret = filter_frame(ctx->inputs[0], buf);
  347. s->available--;
  348. }
  349. return ret;
  350. }
  351. static av_cold void uninit(AVFilterContext *ctx)
  352. {
  353. DeflickerContext *s = ctx->priv;
  354. ff_bufqueue_discard_all(&s->q);
  355. av_freep(&s->histogram);
  356. }
  357. static const AVFilterPad inputs[] = {
  358. {
  359. .name = "default",
  360. .type = AVMEDIA_TYPE_VIDEO,
  361. .filter_frame = filter_frame,
  362. .config_props = config_input,
  363. },
  364. { NULL }
  365. };
  366. static const AVFilterPad outputs[] = {
  367. {
  368. .name = "default",
  369. .type = AVMEDIA_TYPE_VIDEO,
  370. .request_frame = request_frame,
  371. },
  372. { NULL }
  373. };
  374. AVFilter ff_vf_deflicker = {
  375. .name = "deflicker",
  376. .description = NULL_IF_CONFIG_SMALL("Remove temporal frame luminance variations."),
  377. .priv_size = sizeof(DeflickerContext),
  378. .priv_class = &deflicker_class,
  379. .uninit = uninit,
  380. .query_formats = query_formats,
  381. .inputs = inputs,
  382. .outputs = outputs,
  383. };