vf_histogram.c 14 KB

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