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.

331 lines
11KB

  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 display_mode;
  47. int levels_mode;
  48. } HistogramContext;
  49. #define OFFSET(x) offsetof(HistogramContext, x)
  50. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  51. static const AVOption histogram_options[] = {
  52. { "mode", "set histogram mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_LEVELS}, 0, MODE_NB-1, FLAGS, "mode"},
  53. { "levels", "standard histogram", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LEVELS}, 0, 0, FLAGS, "mode" },
  54. { "waveform", "per row/column luminance graph", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WAVEFORM}, 0, 0, FLAGS, "mode" },
  55. { "color", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR}, 0, 0, FLAGS, "mode" },
  56. { "color2", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR2}, 0, 0, FLAGS, "mode" },
  57. { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
  58. { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
  59. { "step", "set waveform step value", OFFSET(step), AV_OPT_TYPE_INT, {.i64=10}, 1, 255, FLAGS},
  60. { "waveform_mode", "set waveform mode", OFFSET(waveform_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "waveform_mode"},
  61. { "row", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "waveform_mode" },
  62. { "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "waveform_mode" },
  63. { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"},
  64. { "parade", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" },
  65. { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" },
  66. { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
  67. { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" },
  68. { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" },
  69. { NULL },
  70. };
  71. AVFILTER_DEFINE_CLASS(histogram);
  72. static const enum AVPixelFormat color_pix_fmts[] = {
  73. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
  74. AV_PIX_FMT_NONE
  75. };
  76. static const enum AVPixelFormat levels_pix_fmts[] = {
  77. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
  78. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  79. };
  80. static int query_formats(AVFilterContext *ctx)
  81. {
  82. HistogramContext *h = ctx->priv;
  83. const enum AVPixelFormat *pix_fmts;
  84. switch (h->mode) {
  85. case MODE_WAVEFORM:
  86. case MODE_LEVELS:
  87. pix_fmts = levels_pix_fmts;
  88. break;
  89. case MODE_COLOR:
  90. case MODE_COLOR2:
  91. pix_fmts = color_pix_fmts;
  92. break;
  93. default:
  94. av_assert0(0);
  95. }
  96. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  97. return 0;
  98. }
  99. static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
  100. static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
  101. static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
  102. static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
  103. static int config_input(AVFilterLink *inlink)
  104. {
  105. HistogramContext *h = inlink->dst->priv;
  106. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  107. h->ncomp = desc->nb_components;
  108. switch (inlink->format) {
  109. case AV_PIX_FMT_GBRAP:
  110. case AV_PIX_FMT_GBRP:
  111. h->bg_color = black_gbrp_color;
  112. h->fg_color = white_gbrp_color;
  113. break;
  114. default:
  115. h->bg_color = black_yuva_color;
  116. h->fg_color = white_yuva_color;
  117. }
  118. return 0;
  119. }
  120. static int config_output(AVFilterLink *outlink)
  121. {
  122. AVFilterContext *ctx = outlink->src;
  123. HistogramContext *h = ctx->priv;
  124. switch (h->mode) {
  125. case MODE_LEVELS:
  126. outlink->w = 256;
  127. outlink->h = (h->level_height + h->scale_height) * FFMAX(h->ncomp * h->display_mode, 1);
  128. break;
  129. case MODE_WAVEFORM:
  130. if (h->waveform_mode)
  131. outlink->h = 256 * FFMAX(h->ncomp * h->display_mode, 1);
  132. else
  133. outlink->w = 256 * FFMAX(h->ncomp * h->display_mode, 1);
  134. break;
  135. case MODE_COLOR:
  136. case MODE_COLOR2:
  137. outlink->h = outlink->w = 256;
  138. break;
  139. default:
  140. av_assert0(0);
  141. }
  142. outlink->sample_aspect_ratio = (AVRational){1,1};
  143. return 0;
  144. }
  145. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  146. {
  147. HistogramContext *h = inlink->dst->priv;
  148. AVFilterContext *ctx = inlink->dst;
  149. AVFilterLink *outlink = ctx->outputs[0];
  150. AVFrame *out;
  151. const uint8_t *src;
  152. uint8_t *dst;
  153. int i, j, k, l;
  154. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  155. if (!out) {
  156. av_frame_free(&in);
  157. return AVERROR(ENOMEM);
  158. }
  159. out->pts = in->pts;
  160. for (k = 0; k < h->ncomp; k++)
  161. for (i = 0; i < outlink->h; i++)
  162. memset(out->data[k] + i * out->linesize[k], h->bg_color[k], outlink->w);
  163. switch (h->mode) {
  164. case MODE_LEVELS:
  165. for (k = 0; k < h->ncomp; k++) {
  166. int start = k * (h->level_height + h->scale_height) * h->display_mode;
  167. double max_hval_log;
  168. unsigned max_hval = 0;
  169. for (i = 0; i < in->height; i++) {
  170. src = in->data[k] + i * in->linesize[k];
  171. for (j = 0; j < in->width; j++)
  172. h->histogram[src[j]]++;
  173. }
  174. for (i = 0; i < 256; i++)
  175. max_hval = FFMAX(max_hval, h->histogram[i]);
  176. max_hval_log = log2(max_hval + 1);
  177. for (i = 0; i < outlink->w; i++) {
  178. int col_height;
  179. if (h->levels_mode)
  180. col_height = round(h->level_height * (1. - (log2(h->histogram[i] + 1) / max_hval_log)));
  181. else
  182. col_height = h->level_height - (h->histogram[i] * (int64_t)h->level_height + max_hval - 1) / max_hval;
  183. for (j = h->level_height - 1; j >= col_height; j--) {
  184. if (h->display_mode) {
  185. for (l = 0; l < h->ncomp; l++)
  186. out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l];
  187. } else {
  188. out->data[k][(j + start) * out->linesize[k] + i] = 255;
  189. }
  190. }
  191. for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
  192. out->data[k][(j + start) * out->linesize[k] + i] = i;
  193. }
  194. memset(h->histogram, 0, 256 * sizeof(unsigned));
  195. }
  196. break;
  197. case MODE_WAVEFORM:
  198. if (h->waveform_mode) {
  199. for (k = 0; k < h->ncomp; k++) {
  200. int offset = k * 256 * h->display_mode;
  201. for (i = 0; i < inlink->w; i++) {
  202. for (j = 0; j < inlink->h; j++) {
  203. int pos = (offset +
  204. in->data[k][j * in->linesize[k] + i]) *
  205. out->linesize[k] + i;
  206. unsigned value = out->data[k][pos];
  207. value = FFMIN(value + h->step, 255);
  208. out->data[k][pos] = value;
  209. }
  210. }
  211. }
  212. } else {
  213. for (k = 0; k < h->ncomp; k++) {
  214. int offset = k * 256 * h->display_mode;
  215. for (i = 0; i < inlink->h; i++) {
  216. src = in ->data[k] + i * in ->linesize[k];
  217. dst = out->data[k] + i * out->linesize[k];
  218. for (j = 0; j < inlink->w; j++) {
  219. int pos = src[j] + offset;
  220. unsigned value = dst[pos];
  221. value = FFMIN(value + h->step, 255);
  222. dst[pos] = value;
  223. }
  224. }
  225. }
  226. }
  227. break;
  228. case MODE_COLOR:
  229. for (i = 0; i < inlink->h; i++) {
  230. int iw1 = i * in->linesize[1];
  231. int iw2 = i * in->linesize[2];
  232. for (j = 0; j < inlink->w; j++) {
  233. int pos = in->data[1][iw1 + j] * out->linesize[0] + in->data[2][iw2 + j];
  234. if (out->data[0][pos] < 255)
  235. out->data[0][pos]++;
  236. }
  237. }
  238. for (i = 0; i < 256; i++) {
  239. dst = out->data[0] + i * out->linesize[0];
  240. for (j = 0; j < 256; j++) {
  241. if (!dst[j]) {
  242. out->data[1][i * out->linesize[0] + j] = i;
  243. out->data[2][i * out->linesize[0] + j] = j;
  244. }
  245. }
  246. }
  247. break;
  248. case MODE_COLOR2:
  249. for (i = 0; i < inlink->h; i++) {
  250. int iw1 = i * in->linesize[1];
  251. int iw2 = i * in->linesize[2];
  252. for (j = 0; j < inlink->w; j++) {
  253. int u = in->data[1][iw1 + j];
  254. int v = in->data[2][iw2 + j];
  255. int pos = u * out->linesize[0] + v;
  256. if (!out->data[0][pos])
  257. out->data[0][pos] = FFABS(128 - u) + FFABS(128 - v);
  258. out->data[1][pos] = u;
  259. out->data[2][pos] = v;
  260. }
  261. }
  262. break;
  263. default:
  264. av_assert0(0);
  265. }
  266. av_frame_free(&in);
  267. return ff_filter_frame(outlink, out);
  268. }
  269. static const AVFilterPad inputs[] = {
  270. {
  271. .name = "default",
  272. .type = AVMEDIA_TYPE_VIDEO,
  273. .filter_frame = filter_frame,
  274. .config_props = config_input,
  275. },
  276. { NULL }
  277. };
  278. static const AVFilterPad outputs[] = {
  279. {
  280. .name = "default",
  281. .type = AVMEDIA_TYPE_VIDEO,
  282. .config_props = config_output,
  283. },
  284. { NULL }
  285. };
  286. AVFilter avfilter_vf_histogram = {
  287. .name = "histogram",
  288. .description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
  289. .priv_size = sizeof(HistogramContext),
  290. .query_formats = query_formats,
  291. .inputs = inputs,
  292. .outputs = outputs,
  293. .priv_class = &histogram_class,
  294. };