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.

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