vf_histogram.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (c) 2012-2013 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/avassert.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/parseutils.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. typedef struct HistogramContext {
  31. const AVClass *class; ///< AVClass context for log and options purpose
  32. unsigned histogram[256*256];
  33. int histogram_size;
  34. int mult;
  35. int ncomp;
  36. const uint8_t *bg_color;
  37. const uint8_t *fg_color;
  38. int level_height;
  39. int scale_height;
  40. int display_mode;
  41. int levels_mode;
  42. const AVPixFmtDescriptor *desc, *odesc;
  43. int components;
  44. int planewidth[4];
  45. int planeheight[4];
  46. } HistogramContext;
  47. #define OFFSET(x) offsetof(HistogramContext, x)
  48. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  49. static const AVOption histogram_options[] = {
  50. { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
  51. { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
  52. { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"},
  53. { "parade", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" },
  54. { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" },
  55. { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
  56. { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" },
  57. { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" },
  58. { "components", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS},
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(histogram);
  62. static const enum AVPixelFormat levels_in_pix_fmts[] = {
  63. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  64. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  65. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P,
  66. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV410P,
  67. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  68. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  69. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  70. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  71. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  72. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
  73. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  74. AV_PIX_FMT_GRAY8,
  75. AV_PIX_FMT_NONE
  76. };
  77. static const enum AVPixelFormat levels_out_yuv8_pix_fmts[] = {
  78. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
  79. AV_PIX_FMT_NONE
  80. };
  81. static const enum AVPixelFormat levels_out_yuv9_pix_fmts[] = {
  82. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P9,
  83. AV_PIX_FMT_NONE
  84. };
  85. static const enum AVPixelFormat levels_out_yuv10_pix_fmts[] = {
  86. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUV444P10,
  87. AV_PIX_FMT_NONE
  88. };
  89. static const enum AVPixelFormat levels_out_rgb8_pix_fmts[] = {
  90. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
  91. AV_PIX_FMT_NONE
  92. };
  93. static const enum AVPixelFormat levels_out_rgb9_pix_fmts[] = {
  94. AV_PIX_FMT_GBRP9,
  95. AV_PIX_FMT_NONE
  96. };
  97. static const enum AVPixelFormat levels_out_rgb10_pix_fmts[] = {
  98. AV_PIX_FMT_GBRP10,
  99. AV_PIX_FMT_NONE
  100. };
  101. static int query_formats(AVFilterContext *ctx)
  102. {
  103. AVFilterFormats *avff;
  104. const AVPixFmtDescriptor *desc;
  105. const enum AVPixelFormat *out_pix_fmts;
  106. int rgb, i, bits;
  107. int ret;
  108. if (!ctx->inputs[0]->in_formats ||
  109. !ctx->inputs[0]->in_formats->nb_formats) {
  110. return AVERROR(EAGAIN);
  111. }
  112. if (!ctx->inputs[0]->out_formats)
  113. if ((ret = ff_formats_ref(ff_make_format_list(levels_in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0)
  114. return ret;
  115. avff = ctx->inputs[0]->in_formats;
  116. desc = av_pix_fmt_desc_get(avff->formats[0]);
  117. rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
  118. bits = desc->comp[0].depth;
  119. for (i = 1; i < avff->nb_formats; i++) {
  120. desc = av_pix_fmt_desc_get(avff->formats[i]);
  121. if ((rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB)) ||
  122. (bits != desc->comp[0].depth))
  123. return AVERROR(EAGAIN);
  124. }
  125. if (rgb && bits == 8)
  126. out_pix_fmts = levels_out_rgb8_pix_fmts;
  127. else if (rgb && bits == 9)
  128. out_pix_fmts = levels_out_rgb9_pix_fmts;
  129. else if (rgb && bits == 10)
  130. out_pix_fmts = levels_out_rgb10_pix_fmts;
  131. else if (bits == 8)
  132. out_pix_fmts = levels_out_yuv8_pix_fmts;
  133. else if (bits == 9)
  134. out_pix_fmts = levels_out_yuv9_pix_fmts;
  135. else // if (bits == 10)
  136. out_pix_fmts = levels_out_yuv10_pix_fmts;
  137. if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0)
  138. return ret;
  139. return 0;
  140. }
  141. static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
  142. static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
  143. static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
  144. static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
  145. static int config_input(AVFilterLink *inlink)
  146. {
  147. HistogramContext *h = inlink->dst->priv;
  148. h->desc = av_pix_fmt_desc_get(inlink->format);
  149. h->ncomp = h->desc->nb_components;
  150. h->histogram_size = 1 << h->desc->comp[0].depth;
  151. h->mult = h->histogram_size / 256;
  152. switch (inlink->format) {
  153. case AV_PIX_FMT_GBRP10:
  154. case AV_PIX_FMT_GBRP9:
  155. case AV_PIX_FMT_GBRAP:
  156. case AV_PIX_FMT_GBRP:
  157. h->bg_color = black_gbrp_color;
  158. h->fg_color = white_gbrp_color;
  159. break;
  160. default:
  161. h->bg_color = black_yuva_color;
  162. h->fg_color = white_yuva_color;
  163. }
  164. h->planeheight[1] = h->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, h->desc->log2_chroma_h);
  165. h->planeheight[0] = h->planeheight[3] = inlink->h;
  166. h->planewidth[1] = h->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, h->desc->log2_chroma_w);
  167. h->planewidth[0] = h->planewidth[3] = inlink->w;
  168. return 0;
  169. }
  170. static int config_output(AVFilterLink *outlink)
  171. {
  172. AVFilterContext *ctx = outlink->src;
  173. HistogramContext *h = ctx->priv;
  174. int ncomp = 0, i;
  175. for (i = 0; i < h->ncomp; i++) {
  176. if ((1 << i) & h->components)
  177. ncomp++;
  178. }
  179. outlink->w = h->histogram_size;
  180. outlink->h = (h->level_height + h->scale_height) * FFMAX(ncomp * h->display_mode, 1);
  181. h->odesc = av_pix_fmt_desc_get(outlink->format);
  182. outlink->sample_aspect_ratio = (AVRational){1,1};
  183. return 0;
  184. }
  185. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  186. {
  187. HistogramContext *h = inlink->dst->priv;
  188. AVFilterContext *ctx = inlink->dst;
  189. AVFilterLink *outlink = ctx->outputs[0];
  190. AVFrame *out;
  191. int i, j, k, l, m;
  192. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  193. if (!out) {
  194. av_frame_free(&in);
  195. return AVERROR(ENOMEM);
  196. }
  197. out->pts = in->pts;
  198. for (k = 0; k < 4 && out->data[k]; k++) {
  199. const int is_chroma = (k == 1 || k == 2);
  200. const int dst_h = AV_CEIL_RSHIFT(outlink->h, (is_chroma ? h->odesc->log2_chroma_h : 0));
  201. const int dst_w = AV_CEIL_RSHIFT(outlink->w, (is_chroma ? h->odesc->log2_chroma_w : 0));
  202. if (h->histogram_size <= 256) {
  203. for (i = 0; i < dst_h ; i++)
  204. memset(out->data[h->odesc->comp[k].plane] +
  205. i * out->linesize[h->odesc->comp[k].plane],
  206. h->bg_color[k], dst_w);
  207. } else {
  208. const int mult = h->mult;
  209. for (i = 0; i < dst_h ; i++)
  210. for (j = 0; j < dst_w; j++)
  211. AV_WN16(out->data[h->odesc->comp[k].plane] +
  212. i * out->linesize[h->odesc->comp[k].plane] + j * 2,
  213. h->bg_color[k] * mult);
  214. }
  215. }
  216. for (m = 0, k = 0; k < h->ncomp; k++) {
  217. const int p = h->desc->comp[k].plane;
  218. const int height = h->planeheight[p];
  219. const int width = h->planewidth[p];
  220. double max_hval_log;
  221. unsigned max_hval = 0;
  222. int start;
  223. if (!((1 << k) & h->components))
  224. continue;
  225. start = m++ * (h->level_height + h->scale_height) * h->display_mode;
  226. if (h->histogram_size <= 256) {
  227. for (i = 0; i < height; i++) {
  228. const uint8_t *src = in->data[p] + i * in->linesize[p];
  229. for (j = 0; j < width; j++)
  230. h->histogram[src[j]]++;
  231. }
  232. } else {
  233. for (i = 0; i < height; i++) {
  234. const uint16_t *src = (const uint16_t *)(in->data[p] + i * in->linesize[p]);
  235. for (j = 0; j < width; j++)
  236. h->histogram[src[j]]++;
  237. }
  238. }
  239. for (i = 0; i < h->histogram_size; i++)
  240. max_hval = FFMAX(max_hval, h->histogram[i]);
  241. max_hval_log = log2(max_hval + 1);
  242. for (i = 0; i < outlink->w; i++) {
  243. int col_height;
  244. if (h->levels_mode)
  245. col_height = lrint(h->level_height * (1. - (log2(h->histogram[i] + 1) / max_hval_log)));
  246. else
  247. col_height = h->level_height - (h->histogram[i] * (int64_t)h->level_height + max_hval - 1) / max_hval;
  248. if (h->histogram_size <= 256) {
  249. for (j = h->level_height - 1; j >= col_height; j--) {
  250. if (h->display_mode) {
  251. for (l = 0; l < h->ncomp; l++)
  252. out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l];
  253. } else {
  254. out->data[p][(j + start) * out->linesize[p] + i] = 255;
  255. }
  256. }
  257. for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
  258. out->data[p][(j + start) * out->linesize[p] + i] = i;
  259. } else {
  260. const int mult = h->mult;
  261. for (j = h->level_height - 1; j >= col_height; j--) {
  262. if (h->display_mode) {
  263. for (l = 0; l < h->ncomp; l++)
  264. AV_WN16(out->data[l] + (j + start) * out->linesize[l] + i * 2, h->fg_color[l] * mult);
  265. } else {
  266. AV_WN16(out->data[p] + (j + start) * out->linesize[p] + i * 2, 255 * mult);
  267. }
  268. }
  269. for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
  270. AV_WN16(out->data[p] + (j + start) * out->linesize[p] + i * 2, i);
  271. }
  272. }
  273. memset(h->histogram, 0, h->histogram_size * sizeof(unsigned));
  274. }
  275. av_frame_free(&in);
  276. return ff_filter_frame(outlink, out);
  277. }
  278. static const AVFilterPad inputs[] = {
  279. {
  280. .name = "default",
  281. .type = AVMEDIA_TYPE_VIDEO,
  282. .filter_frame = filter_frame,
  283. .config_props = config_input,
  284. },
  285. { NULL }
  286. };
  287. static const AVFilterPad outputs[] = {
  288. {
  289. .name = "default",
  290. .type = AVMEDIA_TYPE_VIDEO,
  291. .config_props = config_output,
  292. },
  293. { NULL }
  294. };
  295. AVFilter ff_vf_histogram = {
  296. .name = "histogram",
  297. .description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
  298. .priv_size = sizeof(HistogramContext),
  299. .query_formats = query_formats,
  300. .inputs = inputs,
  301. .outputs = outputs,
  302. .priv_class = &histogram_class,
  303. };