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.

392 lines
14KB

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