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.

366 lines
13KB

  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. int ncomp;
  40. const uint8_t *bg_color;
  41. const uint8_t *fg_color;
  42. int level_height;
  43. int scale_height;
  44. int step;
  45. int waveform_mode;
  46. int display_mode;
  47. int levels_mode;
  48. const AVPixFmtDescriptor *desc;
  49. } HistogramContext;
  50. #define OFFSET(x) offsetof(HistogramContext, x)
  51. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  52. static const AVOption histogram_options[] = {
  53. { "mode", "set histogram mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_LEVELS}, 0, MODE_NB-1, FLAGS, "mode"},
  54. { "levels", "standard histogram", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LEVELS}, 0, 0, FLAGS, "mode" },
  55. { "waveform", "per row/column luminance graph", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WAVEFORM}, 0, 0, FLAGS, "mode" },
  56. { "color", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR}, 0, 0, FLAGS, "mode" },
  57. { "color2", "chroma values in vectorscope", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLOR2}, 0, 0, FLAGS, "mode" },
  58. { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
  59. { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
  60. { "step", "set waveform step value", OFFSET(step), AV_OPT_TYPE_INT, {.i64=10}, 1, 255, FLAGS},
  61. { "waveform_mode", "set waveform mode", OFFSET(waveform_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "waveform_mode"},
  62. { "row", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "waveform_mode" },
  63. { "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "waveform_mode" },
  64. { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"},
  65. { "parade", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" },
  66. { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" },
  67. { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
  68. { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" },
  69. { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" },
  70. { NULL }
  71. };
  72. AVFILTER_DEFINE_CLASS(histogram);
  73. static const enum AVPixelFormat color_pix_fmts[] = {
  74. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
  75. AV_PIX_FMT_NONE
  76. };
  77. static const enum AVPixelFormat levels_pix_fmts[] = {
  78. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
  79. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_NONE
  80. };
  81. static const enum AVPixelFormat waveform_pix_fmts[] = {
  82. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  83. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  84. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  85. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  86. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
  87. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
  88. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  89. AV_PIX_FMT_GRAY8,
  90. AV_PIX_FMT_NONE
  91. };
  92. static int query_formats(AVFilterContext *ctx)
  93. {
  94. HistogramContext *h = ctx->priv;
  95. const enum AVPixelFormat *pix_fmts;
  96. switch (h->mode) {
  97. case MODE_WAVEFORM:
  98. pix_fmts = waveform_pix_fmts;
  99. break;
  100. case MODE_LEVELS:
  101. pix_fmts = levels_pix_fmts;
  102. break;
  103. case MODE_COLOR:
  104. case MODE_COLOR2:
  105. pix_fmts = color_pix_fmts;
  106. break;
  107. default:
  108. av_assert0(0);
  109. }
  110. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  111. return 0;
  112. }
  113. static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
  114. static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
  115. static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
  116. static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
  117. static int config_input(AVFilterLink *inlink)
  118. {
  119. HistogramContext *h = inlink->dst->priv;
  120. h->desc = av_pix_fmt_desc_get(inlink->format);
  121. h->ncomp = h->desc->nb_components;
  122. switch (inlink->format) {
  123. case AV_PIX_FMT_GBRAP:
  124. case AV_PIX_FMT_GBRP:
  125. h->bg_color = black_gbrp_color;
  126. h->fg_color = white_gbrp_color;
  127. break;
  128. default:
  129. h->bg_color = black_yuva_color;
  130. h->fg_color = white_yuva_color;
  131. }
  132. return 0;
  133. }
  134. static int config_output(AVFilterLink *outlink)
  135. {
  136. AVFilterContext *ctx = outlink->src;
  137. HistogramContext *h = ctx->priv;
  138. switch (h->mode) {
  139. case MODE_LEVELS:
  140. outlink->w = 256;
  141. outlink->h = (h->level_height + h->scale_height) * FFMAX(h->ncomp * h->display_mode, 1);
  142. break;
  143. case MODE_WAVEFORM:
  144. if (h->waveform_mode)
  145. outlink->h = 256 * FFMAX(h->ncomp * h->display_mode, 1);
  146. else
  147. outlink->w = 256 * FFMAX(h->ncomp * h->display_mode, 1);
  148. break;
  149. case MODE_COLOR:
  150. case MODE_COLOR2:
  151. outlink->h = outlink->w = 256;
  152. break;
  153. default:
  154. av_assert0(0);
  155. }
  156. outlink->sample_aspect_ratio = (AVRational){1,1};
  157. return 0;
  158. }
  159. static void gen_waveform(HistogramContext *h, AVFrame *inpicref, AVFrame *outpicref,
  160. int component, int intensity, int offset, int col_mode)
  161. {
  162. const int plane = h->desc->comp[component].plane;
  163. const int src_linesize = inpicref->linesize[plane];
  164. const int dst_linesize = outpicref->linesize[plane];
  165. uint8_t *src_data = inpicref->data[plane];
  166. uint8_t *dst_data = outpicref->data[plane] + (col_mode ? offset * dst_linesize : offset);
  167. uint8_t * const dst_line = dst_data;
  168. const uint8_t max = 255 - intensity;
  169. const int is_chroma = (component == 1 || component == 2);
  170. const int shift_w = (is_chroma ? h->desc->log2_chroma_w : 0);
  171. const int shift_h = (is_chroma ? h->desc->log2_chroma_h : 0);
  172. const int src_h = FF_CEIL_RSHIFT(inpicref->height, shift_h);
  173. const int src_w = FF_CEIL_RSHIFT(inpicref->width, shift_w);
  174. uint8_t *dst, *p;
  175. int y;
  176. for (y = 0; y < src_h; y++) {
  177. const uint8_t *src_data_end = src_data + src_w;
  178. dst = dst_line;
  179. for (p = src_data; p < src_data_end; p++) {
  180. uint8_t *target;
  181. if (col_mode)
  182. target = dst++ + dst_linesize * (*p >> shift_h);
  183. else
  184. target = dst_data + (*p >> shift_w);
  185. if (*target <= max)
  186. *target += intensity;
  187. else
  188. *target = 255;
  189. }
  190. src_data += src_linesize;
  191. dst_data += dst_linesize;
  192. }
  193. }
  194. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  195. {
  196. HistogramContext *h = inlink->dst->priv;
  197. AVFilterContext *ctx = inlink->dst;
  198. AVFilterLink *outlink = ctx->outputs[0];
  199. AVFrame *out;
  200. const uint8_t *src;
  201. uint8_t *dst;
  202. int i, j, k, l;
  203. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  204. if (!out) {
  205. av_frame_free(&in);
  206. return AVERROR(ENOMEM);
  207. }
  208. out->pts = in->pts;
  209. for (k = 0; k < h->ncomp; k++) {
  210. int is_chroma = (k == 1 || k == 2);
  211. int dst_h = FF_CEIL_RSHIFT(outlink->h, (is_chroma ? h->desc->log2_chroma_h : 0));
  212. int dst_w = FF_CEIL_RSHIFT(outlink->w, (is_chroma ? h->desc->log2_chroma_w : 0));
  213. for (i = 0; i < dst_h ; i++)
  214. memset(out->data[h->desc->comp[k].plane] +
  215. i * out->linesize[h->desc->comp[k].plane],
  216. h->bg_color[k], dst_w);
  217. }
  218. switch (h->mode) {
  219. case MODE_LEVELS:
  220. for (k = 0; k < h->ncomp; k++) {
  221. const int p = h->desc->comp[k].plane;
  222. int start = k * (h->level_height + h->scale_height) * h->display_mode;
  223. double max_hval_log;
  224. unsigned max_hval = 0;
  225. for (i = 0; i < in->height; i++) {
  226. src = in->data[p] + i * in->linesize[p];
  227. for (j = 0; j < in->width; j++)
  228. h->histogram[src[j]]++;
  229. }
  230. for (i = 0; i < 256; i++)
  231. max_hval = FFMAX(max_hval, h->histogram[i]);
  232. max_hval_log = log2(max_hval + 1);
  233. for (i = 0; i < outlink->w; i++) {
  234. int col_height;
  235. if (h->levels_mode)
  236. col_height = round(h->level_height * (1. - (log2(h->histogram[i] + 1) / max_hval_log)));
  237. else
  238. col_height = h->level_height - (h->histogram[i] * (int64_t)h->level_height + max_hval - 1) / max_hval;
  239. for (j = h->level_height - 1; j >= col_height; j--) {
  240. if (h->display_mode) {
  241. for (l = 0; l < h->ncomp; l++)
  242. out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l];
  243. } else {
  244. out->data[p][(j + start) * out->linesize[p] + i] = 255;
  245. }
  246. }
  247. for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
  248. out->data[p][(j + start) * out->linesize[p] + i] = i;
  249. }
  250. memset(h->histogram, 0, 256 * sizeof(unsigned));
  251. }
  252. break;
  253. case MODE_WAVEFORM:
  254. for (k = 0; k < h->ncomp; k++) {
  255. int offset = k * 256 * h->display_mode;
  256. gen_waveform(h, in, out, k, h->step, offset, h->waveform_mode);
  257. }
  258. break;
  259. case MODE_COLOR:
  260. for (i = 0; i < inlink->h; i++) {
  261. int iw1 = i * in->linesize[1];
  262. int iw2 = i * in->linesize[2];
  263. for (j = 0; j < inlink->w; j++) {
  264. int pos = in->data[1][iw1 + j] * out->linesize[0] + in->data[2][iw2 + j];
  265. if (out->data[0][pos] < 255)
  266. out->data[0][pos]++;
  267. }
  268. }
  269. for (i = 0; i < 256; i++) {
  270. dst = out->data[0] + i * out->linesize[0];
  271. for (j = 0; j < 256; j++) {
  272. if (!dst[j]) {
  273. out->data[1][i * out->linesize[0] + j] = i;
  274. out->data[2][i * out->linesize[0] + j] = j;
  275. }
  276. }
  277. }
  278. break;
  279. case MODE_COLOR2:
  280. for (i = 0; i < inlink->h; i++) {
  281. int iw1 = i * in->linesize[1];
  282. int iw2 = i * in->linesize[2];
  283. for (j = 0; j < inlink->w; j++) {
  284. int u = in->data[1][iw1 + j];
  285. int v = in->data[2][iw2 + j];
  286. int pos = u * out->linesize[0] + v;
  287. if (!out->data[0][pos])
  288. out->data[0][pos] = FFABS(128 - u) + FFABS(128 - v);
  289. out->data[1][pos] = u;
  290. out->data[2][pos] = v;
  291. }
  292. }
  293. break;
  294. default:
  295. av_assert0(0);
  296. }
  297. av_frame_free(&in);
  298. return ff_filter_frame(outlink, out);
  299. }
  300. static const AVFilterPad inputs[] = {
  301. {
  302. .name = "default",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .filter_frame = filter_frame,
  305. .config_props = config_input,
  306. },
  307. { NULL }
  308. };
  309. static const AVFilterPad outputs[] = {
  310. {
  311. .name = "default",
  312. .type = AVMEDIA_TYPE_VIDEO,
  313. .config_props = config_output,
  314. },
  315. { NULL }
  316. };
  317. AVFilter avfilter_vf_histogram = {
  318. .name = "histogram",
  319. .description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
  320. .priv_size = sizeof(HistogramContext),
  321. .query_formats = query_formats,
  322. .inputs = inputs,
  323. .outputs = outputs,
  324. .priv_class = &histogram_class,
  325. };