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.

369 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 "libavutil/imgutils.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. typedef struct HistogramContext {
  31. const AVClass *class; ///< AVClass context for log and options purpose
  32. unsigned histogram[256*256];
  33. int histogram_size;
  34. int mult;
  35. int ncomp;
  36. const uint8_t *bg_color;
  37. const uint8_t *fg_color;
  38. int level_height;
  39. int scale_height;
  40. int display_mode;
  41. int levels_mode;
  42. const AVPixFmtDescriptor *desc, *odesc;
  43. int components;
  44. int planewidth[4];
  45. int planeheight[4];
  46. } HistogramContext;
  47. #define OFFSET(x) offsetof(HistogramContext, x)
  48. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  49. static const AVOption histogram_options[] = {
  50. { "level_height", "set level height", OFFSET(level_height), AV_OPT_TYPE_INT, {.i64=200}, 50, 2048, FLAGS},
  51. { "scale_height", "set scale height", OFFSET(scale_height), AV_OPT_TYPE_INT, {.i64=12}, 0, 40, FLAGS},
  52. { "display_mode", "set display mode", OFFSET(display_mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display_mode"},
  53. { "parade", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display_mode" },
  54. { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display_mode" },
  55. { "levels_mode", "set levels mode", OFFSET(levels_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "levels_mode"},
  56. { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "levels_mode" },
  57. { "logarithmic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "levels_mode" },
  58. { "components", "set color components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 1, 15, FLAGS},
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(histogram);
  62. static const enum AVPixelFormat levels_in_pix_fmts[] = {
  63. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  64. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  65. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P,
  66. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV410P,
  67. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  68. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  69. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  70. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  71. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  72. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  73. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
  74. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  75. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
  76. AV_PIX_FMT_GRAY8,
  77. AV_PIX_FMT_NONE
  78. };
  79. static const enum AVPixelFormat levels_out_yuv8_pix_fmts[] = {
  80. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
  81. AV_PIX_FMT_NONE
  82. };
  83. static const enum AVPixelFormat levels_out_yuv9_pix_fmts[] = {
  84. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P9,
  85. AV_PIX_FMT_NONE
  86. };
  87. static const enum AVPixelFormat levels_out_yuv10_pix_fmts[] = {
  88. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUV444P10,
  89. AV_PIX_FMT_NONE
  90. };
  91. static const enum AVPixelFormat levels_out_yuv12_pix_fmts[] = {
  92. AV_PIX_FMT_YUV444P12,
  93. AV_PIX_FMT_NONE
  94. };
  95. static const enum AVPixelFormat levels_out_rgb8_pix_fmts[] = {
  96. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
  97. AV_PIX_FMT_NONE
  98. };
  99. static const enum AVPixelFormat levels_out_rgb9_pix_fmts[] = {
  100. AV_PIX_FMT_GBRP9,
  101. AV_PIX_FMT_NONE
  102. };
  103. static const enum AVPixelFormat levels_out_rgb10_pix_fmts[] = {
  104. AV_PIX_FMT_GBRP10,
  105. AV_PIX_FMT_NONE
  106. };
  107. static const enum AVPixelFormat levels_out_rgb12_pix_fmts[] = {
  108. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
  109. AV_PIX_FMT_NONE
  110. };
  111. static int query_formats(AVFilterContext *ctx)
  112. {
  113. AVFilterFormats *avff;
  114. const AVPixFmtDescriptor *desc;
  115. const enum AVPixelFormat *out_pix_fmts;
  116. int rgb, i, bits;
  117. int ret;
  118. if (!ctx->inputs[0]->in_formats ||
  119. !ctx->inputs[0]->in_formats->nb_formats) {
  120. return AVERROR(EAGAIN);
  121. }
  122. if (!ctx->inputs[0]->out_formats)
  123. if ((ret = ff_formats_ref(ff_make_format_list(levels_in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0)
  124. return ret;
  125. avff = ctx->inputs[0]->in_formats;
  126. desc = av_pix_fmt_desc_get(avff->formats[0]);
  127. rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
  128. bits = desc->comp[0].depth;
  129. for (i = 1; i < avff->nb_formats; i++) {
  130. desc = av_pix_fmt_desc_get(avff->formats[i]);
  131. if ((rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB)) ||
  132. (bits != desc->comp[0].depth))
  133. return AVERROR(EAGAIN);
  134. }
  135. if (rgb && bits == 8)
  136. out_pix_fmts = levels_out_rgb8_pix_fmts;
  137. else if (rgb && bits == 9)
  138. out_pix_fmts = levels_out_rgb9_pix_fmts;
  139. else if (rgb && bits == 10)
  140. out_pix_fmts = levels_out_rgb10_pix_fmts;
  141. else if (rgb && bits == 12)
  142. out_pix_fmts = levels_out_rgb12_pix_fmts;
  143. else if (bits == 8)
  144. out_pix_fmts = levels_out_yuv8_pix_fmts;
  145. else if (bits == 9)
  146. out_pix_fmts = levels_out_yuv9_pix_fmts;
  147. else if (bits == 10)
  148. out_pix_fmts = levels_out_yuv10_pix_fmts;
  149. else if (bits == 12)
  150. out_pix_fmts = levels_out_yuv12_pix_fmts;
  151. else
  152. return AVERROR(EAGAIN);
  153. if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0)
  154. return ret;
  155. return 0;
  156. }
  157. static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
  158. static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
  159. static const uint8_t white_yuva_color[4] = { 255, 127, 127, 255 };
  160. static const uint8_t white_gbrp_color[4] = { 255, 255, 255, 255 };
  161. static int config_input(AVFilterLink *inlink)
  162. {
  163. HistogramContext *h = inlink->dst->priv;
  164. h->desc = av_pix_fmt_desc_get(inlink->format);
  165. h->ncomp = h->desc->nb_components;
  166. h->histogram_size = 1 << h->desc->comp[0].depth;
  167. h->mult = h->histogram_size / 256;
  168. switch (inlink->format) {
  169. case AV_PIX_FMT_GBRP12:
  170. case AV_PIX_FMT_GBRP10:
  171. case AV_PIX_FMT_GBRP9:
  172. case AV_PIX_FMT_GBRAP:
  173. case AV_PIX_FMT_GBRP:
  174. h->bg_color = black_gbrp_color;
  175. h->fg_color = white_gbrp_color;
  176. break;
  177. default:
  178. h->bg_color = black_yuva_color;
  179. h->fg_color = white_yuva_color;
  180. }
  181. h->planeheight[1] = h->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, h->desc->log2_chroma_h);
  182. h->planeheight[0] = h->planeheight[3] = inlink->h;
  183. h->planewidth[1] = h->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, h->desc->log2_chroma_w);
  184. h->planewidth[0] = h->planewidth[3] = inlink->w;
  185. return 0;
  186. }
  187. static int config_output(AVFilterLink *outlink)
  188. {
  189. AVFilterContext *ctx = outlink->src;
  190. HistogramContext *h = ctx->priv;
  191. int ncomp = 0, i;
  192. for (i = 0; i < h->ncomp; i++) {
  193. if ((1 << i) & h->components)
  194. ncomp++;
  195. }
  196. outlink->w = h->histogram_size;
  197. outlink->h = (h->level_height + h->scale_height) * FFMAX(ncomp * h->display_mode, 1);
  198. h->odesc = av_pix_fmt_desc_get(outlink->format);
  199. outlink->sample_aspect_ratio = (AVRational){1,1};
  200. return 0;
  201. }
  202. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  203. {
  204. HistogramContext *h = inlink->dst->priv;
  205. AVFilterContext *ctx = inlink->dst;
  206. AVFilterLink *outlink = ctx->outputs[0];
  207. AVFrame *out;
  208. int i, j, k, l, m;
  209. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  210. if (!out) {
  211. av_frame_free(&in);
  212. return AVERROR(ENOMEM);
  213. }
  214. out->pts = in->pts;
  215. for (k = 0; k < 4 && out->data[k]; k++) {
  216. const int is_chroma = (k == 1 || k == 2);
  217. const int dst_h = AV_CEIL_RSHIFT(outlink->h, (is_chroma ? h->odesc->log2_chroma_h : 0));
  218. const int dst_w = AV_CEIL_RSHIFT(outlink->w, (is_chroma ? h->odesc->log2_chroma_w : 0));
  219. if (h->histogram_size <= 256) {
  220. for (i = 0; i < dst_h ; i++)
  221. memset(out->data[h->odesc->comp[k].plane] +
  222. i * out->linesize[h->odesc->comp[k].plane],
  223. h->bg_color[k], dst_w);
  224. } else {
  225. const int mult = h->mult;
  226. for (i = 0; i < dst_h ; i++)
  227. for (j = 0; j < dst_w; j++)
  228. AV_WN16(out->data[h->odesc->comp[k].plane] +
  229. i * out->linesize[h->odesc->comp[k].plane] + j * 2,
  230. h->bg_color[k] * mult);
  231. }
  232. }
  233. for (m = 0, k = 0; k < h->ncomp; k++) {
  234. const int p = h->desc->comp[k].plane;
  235. const int height = h->planeheight[p];
  236. const int width = h->planewidth[p];
  237. double max_hval_log;
  238. unsigned max_hval = 0;
  239. int start;
  240. if (!((1 << k) & h->components))
  241. continue;
  242. start = m++ * (h->level_height + h->scale_height) * h->display_mode;
  243. if (h->histogram_size <= 256) {
  244. for (i = 0; i < height; i++) {
  245. const uint8_t *src = in->data[p] + i * in->linesize[p];
  246. for (j = 0; j < width; j++)
  247. h->histogram[src[j]]++;
  248. }
  249. } else {
  250. for (i = 0; i < height; i++) {
  251. const uint16_t *src = (const uint16_t *)(in->data[p] + i * in->linesize[p]);
  252. for (j = 0; j < width; j++)
  253. h->histogram[src[j]]++;
  254. }
  255. }
  256. for (i = 0; i < h->histogram_size; 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 = lrint(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. if (h->histogram_size <= 256) {
  266. for (j = h->level_height - 1; j >= col_height; j--) {
  267. if (h->display_mode) {
  268. for (l = 0; l < h->ncomp; l++)
  269. out->data[l][(j + start) * out->linesize[l] + i] = h->fg_color[l];
  270. } else {
  271. out->data[p][(j + start) * out->linesize[p] + i] = 255;
  272. }
  273. }
  274. for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
  275. out->data[p][(j + start) * out->linesize[p] + i] = i;
  276. } else {
  277. const int mult = h->mult;
  278. for (j = h->level_height - 1; j >= col_height; j--) {
  279. if (h->display_mode) {
  280. for (l = 0; l < h->ncomp; l++)
  281. AV_WN16(out->data[l] + (j + start) * out->linesize[l] + i * 2, h->fg_color[l] * mult);
  282. } else {
  283. AV_WN16(out->data[p] + (j + start) * out->linesize[p] + i * 2, 255 * mult);
  284. }
  285. }
  286. for (j = h->level_height + h->scale_height - 1; j >= h->level_height; j--)
  287. AV_WN16(out->data[p] + (j + start) * out->linesize[p] + i * 2, i);
  288. }
  289. }
  290. memset(h->histogram, 0, h->histogram_size * sizeof(unsigned));
  291. }
  292. av_frame_free(&in);
  293. return ff_filter_frame(outlink, out);
  294. }
  295. static const AVFilterPad inputs[] = {
  296. {
  297. .name = "default",
  298. .type = AVMEDIA_TYPE_VIDEO,
  299. .filter_frame = filter_frame,
  300. .config_props = config_input,
  301. },
  302. { NULL }
  303. };
  304. static const AVFilterPad outputs[] = {
  305. {
  306. .name = "default",
  307. .type = AVMEDIA_TYPE_VIDEO,
  308. .config_props = config_output,
  309. },
  310. { NULL }
  311. };
  312. AVFilter ff_vf_histogram = {
  313. .name = "histogram",
  314. .description = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
  315. .priv_size = sizeof(HistogramContext),
  316. .query_formats = query_formats,
  317. .inputs = inputs,
  318. .outputs = outputs,
  319. .priv_class = &histogram_class,
  320. };