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.

348 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) * FFMAX(h->ncomp * h->display_mode, 1);
  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) * h->display_mode;
  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. 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. h->max_hval = 0;
  196. }
  197. break;
  198. case MODE_WAVEFORM:
  199. if (h->waveform_mode) {
  200. for (k = 0; k < h->ncomp; k++) {
  201. int offset = k * 256 * h->display_mode;
  202. for (i = 0; i < inlink->w; i++) {
  203. for (j = 0; j < inlink->h; j++) {
  204. int pos = (offset +
  205. in->data[k][j * in->linesize[k] + i]) *
  206. out->linesize[k] + i;
  207. unsigned value = out->data[k][pos];
  208. value = FFMIN(value + h->step, 255);
  209. out->data[k][pos] = value;
  210. }
  211. }
  212. }
  213. } else {
  214. for (k = 0; k < h->ncomp; k++) {
  215. int offset = k * 256 * h->display_mode;
  216. for (i = 0; i < inlink->h; i++) {
  217. src = in ->data[k] + i * in ->linesize[k];
  218. dst = out->data[k] + i * out->linesize[k];
  219. for (j = 0; j < inlink->w; j++) {
  220. int pos = src[j] + offset;
  221. unsigned value = dst[pos];
  222. value = FFMIN(value + h->step, 255);
  223. dst[pos] = value;
  224. }
  225. }
  226. }
  227. }
  228. break;
  229. case MODE_COLOR:
  230. for (i = 0; i < inlink->h; i++) {
  231. int iw1 = i * in->linesize[1];
  232. int iw2 = i * in->linesize[2];
  233. for (j = 0; j < inlink->w; j++) {
  234. int pos = in->data[1][iw1 + j] * out->linesize[0] + in->data[2][iw2 + j];
  235. if (out->data[0][pos] < 255)
  236. out->data[0][pos]++;
  237. }
  238. }
  239. for (i = 0; i < 256; i++) {
  240. dst = out->data[0] + i * out->linesize[0];
  241. for (j = 0; j < 256; j++) {
  242. if (!dst[j]) {
  243. out->data[1][i * out->linesize[0] + j] = i;
  244. out->data[2][i * out->linesize[0] + j] = j;
  245. }
  246. }
  247. }
  248. break;
  249. case MODE_COLOR2:
  250. for (i = 0; i < inlink->h; i++) {
  251. int iw1 = i * in->linesize[1];
  252. int iw2 = i * in->linesize[2];
  253. for (j = 0; j < inlink->w; j++) {
  254. int u = in->data[1][iw1 + j];
  255. int v = in->data[2][iw2 + j];
  256. int pos = u * out->linesize[0] + v;
  257. if (!out->data[0][pos])
  258. out->data[0][pos] = FFABS(128 - u) + FFABS(128 - v);
  259. out->data[1][pos] = u;
  260. out->data[2][pos] = v;
  261. }
  262. }
  263. break;
  264. default:
  265. av_assert0(0);
  266. }
  267. ret = ff_filter_frame(outlink, out);
  268. avfilter_unref_bufferp(&in);
  269. if (ret < 0)
  270. return ret;
  271. return 0;
  272. }
  273. static av_cold void uninit(AVFilterContext *ctx)
  274. {
  275. HistogramContext *h = ctx->priv;
  276. av_opt_free(h);
  277. }
  278. static const AVFilterPad inputs[] = {
  279. {
  280. .name = "default",
  281. .type = AVMEDIA_TYPE_VIDEO,
  282. .filter_frame = filter_frame,
  283. .config_props = config_input,
  284. .min_perms = AV_PERM_READ,
  285. },
  286. { NULL }
  287. };
  288. static const AVFilterPad outputs[] = {
  289. {
  290. .name = "default",
  291. .type = AVMEDIA_TYPE_VIDEO,
  292. .config_props = config_output,
  293. },
  294. { NULL }
  295. };
  296. AVFilter avfilter_vf_histogram = {
  297. .name = "histogram",
  298. .description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
  299. .priv_size = sizeof(HistogramContext),
  300. .init = init,
  301. .uninit = uninit,
  302. .query_formats = query_formats,
  303. .inputs = inputs,
  304. .outputs = outputs,
  305. .priv_class = &histogram_class,
  306. };