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.

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