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.

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