vf_histogram.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. enum HistogramMode {
  29. MODE_LEVELS,
  30. MODE_WAVEFORM,
  31. MODE_COLOR,
  32. MODE_COLOR2,
  33. MODE_NB
  34. };
  35. typedef struct HistogramContext {
  36. const AVClass *class; ///< AVClass context for log and options purpose
  37. enum HistogramMode mode;
  38. unsigned histogram[256];
  39. int ncomp;
  40. const uint8_t *bg_color;
  41. const uint8_t *fg_color;
  42. int level_height;
  43. int scale_height;
  44. int step;
  45. int waveform_mode;
  46. int waveform_mirror;
  47. int display_mode;
  48. int levels_mode;
  49. const AVPixFmtDescriptor *desc;
  50. } HistogramContext;
  51. #define OFFSET(x) offsetof(HistogramContext, x)
  52. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  53. static const AVOption histogram_options[] = {
  54. { "mode", "set histogram mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_LEVELS}, 0, MODE_NB-1, FLAGS, "mode"},
  55. { "levels", "standard histogram", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LEVELS}, 0, 0, FLAGS, "mode" },
  56. { "waveform", "per row/column luminance graph", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WAVEFORM}, 0, 0, FLAGS, "mode" },
  57. { "color", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR}, 0, 0, FLAGS, "mode" },
  58. { "color2", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR2}, 0, 0, FLAGS, "mode" },
  59. { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
  60. { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
  61. { "step", "set waveform step value", OFFSET(step), AV_OPT_TYPE_INT, {.i64=10}, 1, 255, FLAGS},
  62. { "waveform_mode", "set waveform mode", OFFSET(waveform_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "waveform_mode"},
  63. { "row", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "waveform_mode" },
  64. { "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "waveform_mode" },
  65. { "waveform_mirror", "set waveform mirroring", OFFSET(waveform_mirror), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "waveform_mirror"},
  66. { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"},
  67. { "parade", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" },
  68. { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" },
  69. { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
  70. { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" },
  71. { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" },
  72. { NULL }
  73. };
  74. AVFILTER_DEFINE_CLASS(histogram);
  75. static const enum AVPixelFormat color_pix_fmts[] = {
  76. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
  77. AV_PIX_FMT_NONE
  78. };
  79. static const enum AVPixelFormat levels_pix_fmts[] = {
  80. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
  81. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  82. };
  83. static const enum AVPixelFormat waveform_pix_fmts[] = {
  84. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  85. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  86. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  87. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  88. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
  89. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
  90. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  91. AV_PIX_FMT_GRAY8,
  92. AV_PIX_FMT_NONE
  93. };
  94. static int query_formats(AVFilterContext *ctx)
  95. {
  96. HistogramContext *h = ctx->priv;
  97. const enum AVPixelFormat *pix_fmts;
  98. switch (h->mode) {
  99. case MODE_WAVEFORM:
  100. pix_fmts = waveform_pix_fmts;
  101. break;
  102. case MODE_LEVELS:
  103. pix_fmts = levels_pix_fmts;
  104. break;
  105. case MODE_COLOR:
  106. case MODE_COLOR2:
  107. pix_fmts = color_pix_fmts;
  108. break;
  109. default:
  110. av_assert0(0);
  111. }
  112. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  113. return 0;
  114. }
  115. static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
  116. static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
  117. static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
  118. static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
  119. static int config_input(AVFilterLink *inlink)
  120. {
  121. HistogramContext *h = inlink->dst->priv;
  122. h->desc = av_pix_fmt_desc_get(inlink->format);
  123. h->ncomp = h->desc->nb_components;
  124. switch (inlink->format) {
  125. case AV_PIX_FMT_GBRAP:
  126. case AV_PIX_FMT_GBRP:
  127. h->bg_color = black_gbrp_color;
  128. h->fg_color = white_gbrp_color;
  129. break;
  130. default:
  131. h->bg_color = black_yuva_color;
  132. h->fg_color = white_yuva_color;
  133. }
  134. return 0;
  135. }
  136. static int config_output(AVFilterLink *outlink)
  137. {
  138. AVFilterContext *ctx = outlink->src;
  139. HistogramContext *h = ctx->priv;
  140. switch (h->mode) {
  141. case MODE_LEVELS:
  142. outlink->w = 256;
  143. outlink->h = (h->level_height + h->scale_height) * FFMAX(h->ncomp * h->display_mode, 1);
  144. break;
  145. case MODE_WAVEFORM:
  146. if (h->waveform_mode)
  147. outlink->h = 256 * FFMAX(h->ncomp * h->display_mode, 1);
  148. else
  149. outlink->w = 256 * FFMAX(h->ncomp * h->display_mode, 1);
  150. break;
  151. case MODE_COLOR:
  152. case MODE_COLOR2:
  153. outlink->h = outlink->w = 256;
  154. break;
  155. default:
  156. av_assert0(0);
  157. }
  158. outlink->sample_aspect_ratio = (AVRational){1,1};
  159. return 0;
  160. }
  161. static void gen_waveform(HistogramContext *h, AVFrame *inpicref, AVFrame *outpicref,
  162. int component, int intensity, int offset, int col_mode)
  163. {
  164. const int plane = h->desc->comp[component].plane;
  165. const int mirror = h->waveform_mirror;
  166. const int is_chroma = (component == 1 || component == 2);
  167. const int shift_w = (is_chroma ? h->desc->log2_chroma_w : 0);
  168. const int shift_h = (is_chroma ? h->desc->log2_chroma_h : 0);
  169. const int src_linesize = inpicref->linesize[plane];
  170. const int dst_linesize = outpicref->linesize[plane];
  171. const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
  172. uint8_t *src_data = inpicref->data[plane];
  173. uint8_t *dst_data = outpicref->data[plane] + (col_mode ? (offset >> shift_h) * dst_linesize : offset >> shift_w);
  174. uint8_t * const dst_bottom_line = dst_data + dst_linesize * ((256 >> shift_h) - 1);
  175. uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
  176. const uint8_t max = 255 - intensity;
  177. const int src_h = FF_CEIL_RSHIFT(inpicref->height, shift_h);
  178. const int src_w = FF_CEIL_RSHIFT(inpicref->width, shift_w);
  179. uint8_t *dst, *p;
  180. int y;
  181. if (!col_mode && mirror)
  182. dst_data += 256 >> shift_w;
  183. for (y = 0; y < src_h; y++) {
  184. const uint8_t *src_data_end = src_data + src_w;
  185. dst = dst_line;
  186. for (p = src_data; p < src_data_end; p++) {
  187. uint8_t *target;
  188. if (col_mode) {
  189. target = dst++ + dst_signed_linesize * (*p >> shift_h);
  190. } else {
  191. if (mirror)
  192. target = dst_data - (*p >> shift_w);
  193. else
  194. target = dst_data + (*p >> shift_w);
  195. }
  196. if (*target <= max)
  197. *target += intensity;
  198. else
  199. *target = 255;
  200. }
  201. src_data += src_linesize;
  202. dst_data += dst_linesize;
  203. }
  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. const uint8_t *src;
  212. uint8_t *dst;
  213. int i, j, k, l;
  214. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  215. if (!out) {
  216. av_frame_free(&in);
  217. return AVERROR(ENOMEM);
  218. }
  219. out->pts = in->pts;
  220. for (k = 0; k < h->ncomp; k++) {
  221. const int is_chroma = (k == 1 || k == 2);
  222. const int dst_h = FF_CEIL_RSHIFT(outlink->h, (is_chroma ? h->desc->log2_chroma_h : 0));
  223. const int dst_w = FF_CEIL_RSHIFT(outlink->w, (is_chroma ? h->desc->log2_chroma_w : 0));
  224. for (i = 0; i < dst_h ; i++)
  225. memset(out->data[h->desc->comp[k].plane] +
  226. i * out->linesize[h->desc->comp[k].plane],
  227. h->bg_color[k], dst_w);
  228. }
  229. switch (h->mode) {
  230. case MODE_LEVELS:
  231. for (k = 0; k < h->ncomp; k++) {
  232. const int p = h->desc->comp[k].plane;
  233. const int start = k * (h->level_height + h->scale_height) * h->display_mode;
  234. double max_hval_log;
  235. unsigned max_hval = 0;
  236. for (i = 0; i < in->height; i++) {
  237. src = in->data[p] + i * in->linesize[p];
  238. for (j = 0; j < in->width; j++)
  239. h->histogram[src[j]]++;
  240. }
  241. for (i = 0; i < 256; i++)
  242. max_hval = FFMAX(max_hval, h->histogram[i]);
  243. max_hval_log = log2(max_hval + 1);
  244. for (i = 0; i < outlink->w; i++) {
  245. int col_height;
  246. if (h->levels_mode)
  247. col_height = round(h->level_height * (1. - (log2(h->histogram[i] + 1) / max_hval_log)));
  248. else
  249. col_height = h->level_height - (h->histogram[i] * (int64_t)h->level_height + max_hval - 1) / max_hval;
  250. for (j = h->level_height - 1; j >= col_height; j--) {
  251. if (h->display_mode) {
  252. for (l = 0; l < h->ncomp; l++)
  253. out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l];
  254. } else {
  255. out->data[p][(j + start) * out->linesize[p] + i] = 255;
  256. }
  257. }
  258. for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
  259. out->data[p][(j + start) * out->linesize[p] + i] = i;
  260. }
  261. memset(h->histogram, 0, 256 * sizeof(unsigned));
  262. }
  263. break;
  264. case MODE_WAVEFORM:
  265. for (k = 0; k < h->ncomp; k++) {
  266. const int offset = k * 256 * h->display_mode;
  267. gen_waveform(h, in, out, k, h->step, offset, h->waveform_mode);
  268. }
  269. break;
  270. case MODE_COLOR:
  271. for (i = 0; i < inlink->h; i++) {
  272. const int iw1 = i * in->linesize[1];
  273. const int iw2 = i * in->linesize[2];
  274. for (j = 0; j < inlink->w; j++) {
  275. const int pos = in->data[1][iw1 + j] * out->linesize[0] + in->data[2][iw2 + j];
  276. if (out->data[0][pos] < 255)
  277. out->data[0][pos]++;
  278. }
  279. }
  280. for (i = 0; i < 256; i++) {
  281. dst = out->data[0] + i * out->linesize[0];
  282. for (j = 0; j < 256; j++) {
  283. if (!dst[j]) {
  284. out->data[1][i * out->linesize[0] + j] = i;
  285. out->data[2][i * out->linesize[0] + j] = j;
  286. }
  287. }
  288. }
  289. break;
  290. case MODE_COLOR2:
  291. for (i = 0; i < inlink->h; i++) {
  292. const int iw1 = i * in->linesize[1];
  293. const int iw2 = i * in->linesize[2];
  294. for (j = 0; j < inlink->w; j++) {
  295. const int u = in->data[1][iw1 + j];
  296. const int v = in->data[2][iw2 + j];
  297. const int pos = u * out->linesize[0] + v;
  298. if (!out->data[0][pos])
  299. out->data[0][pos] = FFABS(128 - u) + FFABS(128 - v);
  300. out->data[1][pos] = u;
  301. out->data[2][pos] = v;
  302. }
  303. }
  304. break;
  305. default:
  306. av_assert0(0);
  307. }
  308. av_frame_free(&in);
  309. return ff_filter_frame(outlink, out);
  310. }
  311. static const AVFilterPad inputs[] = {
  312. {
  313. .name = "default",
  314. .type = AVMEDIA_TYPE_VIDEO,
  315. .filter_frame = filter_frame,
  316. .config_props = config_input,
  317. },
  318. { NULL }
  319. };
  320. static const AVFilterPad outputs[] = {
  321. {
  322. .name = "default",
  323. .type = AVMEDIA_TYPE_VIDEO,
  324. .config_props = config_output,
  325. },
  326. { NULL }
  327. };
  328. AVFilter ff_vf_histogram = {
  329. .name = "histogram",
  330. .description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
  331. .priv_size = sizeof(HistogramContext),
  332. .query_formats = query_formats,
  333. .inputs = inputs,
  334. .outputs = outputs,
  335. .priv_class = &histogram_class,
  336. };