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.

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