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.

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