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.

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