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.

447 lines
16KB

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