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.

492 lines
17KB

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