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.

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