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.

207 lines
7.0KB

  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 "avfilter.h"
  24. #include "drawutils.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct EntropyContext {
  29. const AVClass *class;
  30. int mode;
  31. int nb_planes;
  32. int planeheight[4];
  33. int planewidth[4];
  34. int depth;
  35. int is_rgb;
  36. uint8_t rgba_map[4];
  37. char planenames[4];
  38. int64_t *histogram;
  39. } EntropyContext;
  40. #define OFFSET(x) offsetof(EntropyContext, x)
  41. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  42. static const AVOption entropy_options[] = {
  43. { "mode", "set kind of histogram entropy measurement", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
  44. { "normal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
  45. { "diff", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
  46. { NULL }
  47. };
  48. AVFILTER_DEFINE_CLASS(entropy);
  49. static int query_formats(AVFilterContext *ctx)
  50. {
  51. static const enum AVPixelFormat pixfmts[] = {
  52. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  53. AV_PIX_FMT_YUV440P,
  54. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
  55. AV_PIX_FMT_YUVJ440P,
  56. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
  57. AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
  58. AV_PIX_FMT_YUV440P10,
  59. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  60. AV_PIX_FMT_YUV440P12,
  61. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  62. AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV420P16,
  63. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  64. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  65. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  66. AV_PIX_FMT_NONE
  67. };
  68. AVFilterFormats *formats = ff_make_format_list(pixfmts);
  69. if (!formats)
  70. return AVERROR(ENOMEM);
  71. return ff_set_common_formats(ctx, formats);
  72. }
  73. static int config_input(AVFilterLink *inlink)
  74. {
  75. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  76. AVFilterContext *ctx = inlink->dst;
  77. EntropyContext *s = ctx->priv;
  78. s->nb_planes = desc->nb_components;
  79. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  80. s->planeheight[0] = s->planeheight[3] = inlink->h;
  81. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  82. s->planewidth[0] = s->planewidth[3] = inlink->w;
  83. s->depth = desc->comp[0].depth;
  84. s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0;
  85. s->planenames[0] = s->is_rgb ? 'R' : 'Y';
  86. s->planenames[1] = s->is_rgb ? 'G' : 'U';
  87. s->planenames[2] = s->is_rgb ? 'B' : 'V';
  88. s->planenames[3] = 'A';
  89. s->histogram = av_malloc_array(1 << s->depth, sizeof(*s->histogram));
  90. if (!s->histogram)
  91. return AVERROR(ENOMEM);
  92. return 0;
  93. }
  94. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  95. {
  96. AVFilterContext *ctx = inlink->dst;
  97. AVFilterLink *outlink = ctx->outputs[0];
  98. EntropyContext *s = ctx->priv;
  99. int plane, y, x;
  100. for (plane = 0; plane < s->nb_planes; plane++) {
  101. int cidx = s->is_rgb ? s->rgba_map[plane] : plane;
  102. const uint8_t *src8 = in->data[plane];
  103. const uint16_t *src16 = (const uint16_t *)in->data[plane];
  104. float total = s->planewidth[plane] * s->planeheight[plane];
  105. float entropy = 0;
  106. char metabuf[128];
  107. char key[128];
  108. memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram));
  109. if (s->depth <= 8) {
  110. for (y = 0; y < s->planeheight[plane]; y++) {
  111. for (x = 0; x < s->planewidth[plane]; x++) {
  112. s->histogram[src8[x]]++;
  113. }
  114. src8 += in->linesize[plane];
  115. }
  116. } else {
  117. for (y = 0; y < s->planeheight[plane]; y++) {
  118. for (x = 0; x < s->planewidth[plane]; x++) {
  119. s->histogram[src16[x]]++;
  120. }
  121. src16 += in->linesize[plane] / 2;
  122. }
  123. }
  124. for (y = 0; y < 1 << s->depth; y++) {
  125. if (s->mode == 0) {
  126. if (s->histogram[y]) {
  127. float p = s->histogram[y] / total;
  128. entropy += -log2(p) * p;
  129. }
  130. } else if (s->mode == 1) {
  131. if (y && (s->histogram[y] - s->histogram[y - 1]) != 0) {
  132. float p = FFABS(s->histogram[y] - s->histogram[y - 1]) / total;
  133. entropy += -log2(p) * p;
  134. }
  135. }
  136. }
  137. snprintf(key, sizeof(key), "lavfi.entropy.entropy.%s.%c", s->mode ? "diff" : "normal", s->planenames[cidx]);
  138. snprintf(metabuf, sizeof(metabuf), "%f", entropy);
  139. av_dict_set(&in->metadata, key, metabuf, 0);
  140. snprintf(key, sizeof(key), "lavfi.entropy.normalized_entropy.%s.%c", s->mode ? "diff" : "normal", s->planenames[cidx]);
  141. snprintf(metabuf, sizeof(metabuf), "%f", entropy / log2(1 << s->depth));
  142. av_dict_set(&in->metadata, key, metabuf, 0);
  143. }
  144. return ff_filter_frame(outlink, in);
  145. }
  146. static av_cold void uninit(AVFilterContext *ctx)
  147. {
  148. EntropyContext *s = ctx->priv;
  149. av_freep(&s->histogram);
  150. }
  151. static const AVFilterPad inputs[] = {
  152. {
  153. .name = "default",
  154. .type = AVMEDIA_TYPE_VIDEO,
  155. .filter_frame = filter_frame,
  156. .config_props = config_input,
  157. },
  158. { NULL }
  159. };
  160. static const AVFilterPad outputs[] = {
  161. {
  162. .name = "default",
  163. .type = AVMEDIA_TYPE_VIDEO,
  164. },
  165. { NULL }
  166. };
  167. AVFilter ff_vf_entropy = {
  168. .name = "entropy",
  169. .description = NULL_IF_CONFIG_SMALL("Measure video frames entropy."),
  170. .priv_size = sizeof(EntropyContext),
  171. .uninit = uninit,
  172. .query_formats = query_formats,
  173. .inputs = inputs,
  174. .outputs = outputs,
  175. .priv_class = &entropy_class,
  176. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  177. };