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
10KB

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