vf_histogram.c 13 KB

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