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.

418 lines
14KB

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