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.

365 lines
12KB

  1. /*
  2. * Copyright (c) 2015 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/avstring.h"
  21. #include "libavutil/channel_layout.h"
  22. #include "libavutil/eval.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/parseutils.h"
  26. #include "libavutil/xga_font_data.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "audio.h"
  30. #include "video.h"
  31. #include "internal.h"
  32. static const char *const var_names[] = { "VOLUME", "CHANNEL", "PEAK", NULL };
  33. enum { VAR_VOLUME, VAR_CHANNEL, VAR_PEAK, VAR_VARS_NB };
  34. typedef struct ShowVolumeContext {
  35. const AVClass *class;
  36. int w, h;
  37. int b;
  38. double f;
  39. AVRational frame_rate;
  40. char *color;
  41. int orientation;
  42. int step;
  43. AVFrame *out;
  44. AVExpr *c_expr;
  45. int draw_text;
  46. int draw_volume;
  47. double *values;
  48. uint32_t *color_lut;
  49. } ShowVolumeContext;
  50. #define OFFSET(x) offsetof(ShowVolumeContext, x)
  51. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  52. static const AVOption showvolume_options[] = {
  53. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  54. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  55. { "b", "set border width", OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
  56. { "w", "set channel width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 80, 8192, FLAGS },
  57. { "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 900, FLAGS },
  58. { "f", "set fade", OFFSET(f), AV_OPT_TYPE_DOUBLE, {.dbl=0.95}, 0.001, 1, FLAGS },
  59. { "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="PEAK*255+floor((1-PEAK)*255)*256+0xff000000"}, 0, 0, FLAGS },
  60. { "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  61. { "v", "display volume value", OFFSET(draw_volume), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  62. { "o", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "orientation" },
  63. { "h", "horizontal", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "orientation" },
  64. { "v", "vertical", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "orientation" },
  65. { "s", "set step size", OFFSET(step), AV_OPT_TYPE_INT, {.i64=0}, 0, 5, FLAGS },
  66. { NULL }
  67. };
  68. AVFILTER_DEFINE_CLASS(showvolume);
  69. static av_cold int init(AVFilterContext *ctx)
  70. {
  71. ShowVolumeContext *s = ctx->priv;
  72. int ret;
  73. if (s->color) {
  74. ret = av_expr_parse(&s->c_expr, s->color, var_names,
  75. NULL, NULL, NULL, NULL, 0, ctx);
  76. if (ret < 0)
  77. return ret;
  78. }
  79. return 0;
  80. }
  81. static int query_formats(AVFilterContext *ctx)
  82. {
  83. AVFilterFormats *formats = NULL;
  84. AVFilterChannelLayouts *layouts = NULL;
  85. AVFilterLink *inlink = ctx->inputs[0];
  86. AVFilterLink *outlink = ctx->outputs[0];
  87. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
  88. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
  89. int ret;
  90. formats = ff_make_format_list(sample_fmts);
  91. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  92. return ret;
  93. layouts = ff_all_channel_counts();
  94. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  95. return ret;
  96. formats = ff_all_samplerates();
  97. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  98. return ret;
  99. formats = ff_make_format_list(pix_fmts);
  100. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  101. return ret;
  102. return 0;
  103. }
  104. static int config_input(AVFilterLink *inlink)
  105. {
  106. AVFilterContext *ctx = inlink->dst;
  107. ShowVolumeContext *s = ctx->priv;
  108. int nb_samples;
  109. nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
  110. inlink->partial_buf_size =
  111. inlink->min_samples =
  112. inlink->max_samples = nb_samples;
  113. s->values = av_calloc(inlink->channels * VAR_VARS_NB, sizeof(double));
  114. if (!s->values)
  115. return AVERROR(ENOMEM);
  116. s->color_lut = av_calloc(s->w, sizeof(*s->color_lut) * inlink->channels);
  117. if (!s->color_lut)
  118. return AVERROR(ENOMEM);
  119. return 0;
  120. }
  121. static int config_output(AVFilterLink *outlink)
  122. {
  123. ShowVolumeContext *s = outlink->src->priv;
  124. AVFilterLink *inlink = outlink->src->inputs[0];
  125. int ch;
  126. if (s->orientation) {
  127. outlink->h = s->w;
  128. outlink->w = s->h * inlink->channels + (inlink->channels - 1) * s->b;
  129. } else {
  130. outlink->w = s->w;
  131. outlink->h = s->h * inlink->channels + (inlink->channels - 1) * s->b;
  132. }
  133. outlink->sample_aspect_ratio = (AVRational){1,1};
  134. outlink->frame_rate = s->frame_rate;
  135. for (ch = 0; ch < inlink->channels; ch++) {
  136. int i;
  137. for (i = 0; i < s->w; i++) {
  138. float max = i / (float)(s->w - 1);
  139. s->values[ch * VAR_VARS_NB + VAR_PEAK] = max;
  140. s->values[ch * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
  141. s->values[ch * VAR_VARS_NB + VAR_CHANNEL] = ch;
  142. s->color_lut[ch * s->w + i] = av_expr_eval(s->c_expr, &s->values[ch * VAR_VARS_NB], NULL);
  143. }
  144. }
  145. return 0;
  146. }
  147. static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
  148. {
  149. const uint8_t *font;
  150. int font_height;
  151. int i;
  152. font = avpriv_cga_font, font_height = 8;
  153. for (i = 0; txt[i]; i++) {
  154. int char_y, mask;
  155. if (o) {
  156. for (char_y = font_height - 1; char_y >= 0; char_y--) {
  157. uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x * 4;
  158. for (mask = 0x80; mask; mask >>= 1) {
  159. if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
  160. AV_WN32(&p[char_y * 4], ~AV_RN32(&p[char_y * 4]));
  161. p += pic->linesize[0];
  162. }
  163. }
  164. } else {
  165. uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
  166. for (char_y = 0; char_y < font_height; char_y++) {
  167. for (mask = 0x80; mask; mask >>= 1) {
  168. if (font[txt[i] * font_height + char_y] & mask)
  169. AV_WN32(p, ~AV_RN32(p));
  170. p += 4;
  171. }
  172. p += pic->linesize[0] - 8 * 4;
  173. }
  174. }
  175. }
  176. }
  177. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  178. {
  179. AVFilterContext *ctx = inlink->dst;
  180. AVFilterLink *outlink = ctx->outputs[0];
  181. ShowVolumeContext *s = ctx->priv;
  182. const int step = s->step;
  183. int c, i, j, k;
  184. AVFrame *out;
  185. if (!s->out || s->out->width != outlink->w ||
  186. s->out->height != outlink->h) {
  187. av_frame_free(&s->out);
  188. s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  189. if (!s->out) {
  190. av_frame_free(&insamples);
  191. return AVERROR(ENOMEM);
  192. }
  193. for (i = 0; i < outlink->h; i++)
  194. memset(s->out->data[0] + i * s->out->linesize[0], 0, outlink->w * 4);
  195. }
  196. s->out->pts = insamples->pts;
  197. for (j = 0; j < outlink->h; j++) {
  198. uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
  199. for (k = 0; k < outlink->w; k++) {
  200. dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] * s->f, 0);
  201. dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] * s->f, 0);
  202. dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] * s->f, 0);
  203. dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] * s->f, 0);
  204. }
  205. }
  206. if (s->orientation) {
  207. for (c = 0; c < inlink->channels; c++) {
  208. float *src = (float *)insamples->extended_data[c];
  209. uint32_t *lut = s->color_lut + s->w * c;
  210. float max = 0;
  211. for (i = 0; i < insamples->nb_samples; i++)
  212. max = FFMAX(max, src[i]);
  213. s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
  214. max = av_clipf(max, 0, 1);
  215. for (j = outlink->h - outlink->h * max; j < s->w; j++) {
  216. uint8_t *dst = s->out->data[0] + j * s->out->linesize[0] + c * (s->b + s->h) * 4;
  217. for (k = 0; k < s->h; k++) {
  218. AV_WN32A(&dst[k * 4], lut[s->w - j - 1]);
  219. if (j & step)
  220. j += step;
  221. }
  222. }
  223. if (s->h >= 8 && s->draw_text) {
  224. const char *channel_name = av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c));
  225. if (!channel_name)
  226. continue;
  227. drawtext(s->out, c * (s->h + s->b) + (s->h - 10) / 2, outlink->h - 35, channel_name, 1);
  228. }
  229. }
  230. } else {
  231. for (c = 0; c < inlink->channels; c++) {
  232. float *src = (float *)insamples->extended_data[c];
  233. uint32_t *lut = s->color_lut + s->w * c;
  234. float max = 0;
  235. for (i = 0; i < insamples->nb_samples; i++)
  236. max = FFMAX(max, src[i]);
  237. s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
  238. max = av_clipf(max, 0, 1);
  239. for (j = 0; j < s->h; j++) {
  240. uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
  241. for (k = 0; k < s->w * max; k++) {
  242. AV_WN32A(dst + k * 4, lut[k]);
  243. if (k & step)
  244. k += step;
  245. }
  246. }
  247. if (s->h >= 8 && s->draw_text) {
  248. const char *channel_name = av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c));
  249. if (!channel_name)
  250. continue;
  251. drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2, channel_name, 0);
  252. }
  253. }
  254. }
  255. av_frame_free(&insamples);
  256. out = av_frame_clone(s->out);
  257. if (!out)
  258. return AVERROR(ENOMEM);
  259. av_frame_make_writable(out);
  260. for (c = 0; c < inlink->channels && s->draw_volume; c++) {
  261. char buf[16];
  262. if (s->orientation) {
  263. if (s->h >= 8) {
  264. snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
  265. drawtext(out, c * (s->h + s->b) + (s->h - 8) / 2, 2, buf, 1);
  266. }
  267. } else {
  268. if (s->h >= 8) {
  269. snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
  270. drawtext(out, FFMAX(0, s->w - 8 * (int)strlen(buf)), c * (s->h + s->b) + (s->h - 8) / 2, buf, 0);
  271. }
  272. }
  273. }
  274. return ff_filter_frame(outlink, out);
  275. }
  276. static av_cold void uninit(AVFilterContext *ctx)
  277. {
  278. ShowVolumeContext *s = ctx->priv;
  279. av_frame_free(&s->out);
  280. av_expr_free(s->c_expr);
  281. av_freep(&s->values);
  282. av_freep(&s->color_lut);
  283. }
  284. static const AVFilterPad showvolume_inputs[] = {
  285. {
  286. .name = "default",
  287. .type = AVMEDIA_TYPE_AUDIO,
  288. .config_props = config_input,
  289. .filter_frame = filter_frame,
  290. },
  291. { NULL }
  292. };
  293. static const AVFilterPad showvolume_outputs[] = {
  294. {
  295. .name = "default",
  296. .type = AVMEDIA_TYPE_VIDEO,
  297. .config_props = config_output,
  298. },
  299. { NULL }
  300. };
  301. AVFilter ff_avf_showvolume = {
  302. .name = "showvolume",
  303. .description = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
  304. .init = init,
  305. .uninit = uninit,
  306. .query_formats = query_formats,
  307. .priv_size = sizeof(ShowVolumeContext),
  308. .inputs = showvolume_inputs,
  309. .outputs = showvolume_outputs,
  310. .priv_class = &showvolume_class,
  311. };