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.

280 lines
8.9KB

  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", NULL };
  33. enum { VAR_VOLUME, VAR_CHANNEL, 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. AVFrame *out;
  42. AVExpr *c_expr;
  43. int draw_text;
  44. int draw_volume;
  45. double *values;
  46. } ShowVolumeContext;
  47. #define OFFSET(x) offsetof(ShowVolumeContext, x)
  48. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  49. static const AVOption showvolume_options[] = {
  50. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
  51. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
  52. { "b", "set border width", OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
  53. { "w", "set channel width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 80, 1080, FLAGS },
  54. { "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 100, FLAGS },
  55. { "f", "set fade", OFFSET(f), AV_OPT_TYPE_DOUBLE, {.dbl=0.95}, 0.001, 1, FLAGS },
  56. { "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="if(gte(VOLUME,-6), if(gte(VOLUME,-2), if(gte(VOLUME,-1), if(gt(VOLUME,0), 0xff0000ff, 0xff0066ff), 0xff00ffff),0xff00ff00),0xffff0000)"}, 0, 0, FLAGS },
  57. { "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  58. { "v", "display volume value", OFFSET(draw_volume), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(showvolume);
  62. static av_cold int init(AVFilterContext *ctx)
  63. {
  64. ShowVolumeContext *s = ctx->priv;
  65. int ret;
  66. if (s->color) {
  67. ret = av_expr_parse(&s->c_expr, s->color, var_names,
  68. NULL, NULL, NULL, NULL, 0, ctx);
  69. if (ret < 0)
  70. return ret;
  71. }
  72. return 0;
  73. }
  74. static int query_formats(AVFilterContext *ctx)
  75. {
  76. AVFilterFormats *formats = NULL;
  77. AVFilterChannelLayouts *layouts = NULL;
  78. AVFilterLink *inlink = ctx->inputs[0];
  79. AVFilterLink *outlink = ctx->outputs[0];
  80. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
  81. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
  82. int ret;
  83. formats = ff_make_format_list(sample_fmts);
  84. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  85. return ret;
  86. layouts = ff_all_channel_layouts();
  87. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  88. return ret;
  89. formats = ff_all_samplerates();
  90. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  91. return ret;
  92. formats = ff_make_format_list(pix_fmts);
  93. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  94. return ret;
  95. return 0;
  96. }
  97. static int config_input(AVFilterLink *inlink)
  98. {
  99. AVFilterContext *ctx = inlink->dst;
  100. ShowVolumeContext *s = ctx->priv;
  101. int nb_samples;
  102. nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
  103. inlink->partial_buf_size =
  104. inlink->min_samples =
  105. inlink->max_samples = nb_samples;
  106. s->values = av_calloc(inlink->channels * VAR_VARS_NB, sizeof(double));
  107. if (!s->values)
  108. return AVERROR(ENOMEM);
  109. return 0;
  110. }
  111. static int config_output(AVFilterLink *outlink)
  112. {
  113. ShowVolumeContext *s = outlink->src->priv;
  114. AVFilterLink *inlink = outlink->src->inputs[0];
  115. outlink->w = s->w;
  116. outlink->h = s->h * inlink->channels + (inlink->channels - 1) * s->b;
  117. outlink->sample_aspect_ratio = (AVRational){1,1};
  118. outlink->frame_rate = s->frame_rate;
  119. return 0;
  120. }
  121. static void drawtext(AVFrame *pic, int x, int y, const char *txt)
  122. {
  123. const uint8_t *font;
  124. int font_height;
  125. int i;
  126. font = avpriv_cga_font, font_height = 8;
  127. for (i = 0; txt[i]; i++) {
  128. int char_y, mask;
  129. uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*4;
  130. for (char_y = 0; char_y < font_height; char_y++) {
  131. for (mask = 0x80; mask; mask >>= 1) {
  132. if (font[txt[i] * font_height + char_y] & mask)
  133. AV_WN32(p, ~AV_RN32(p));
  134. p += 4;
  135. }
  136. p += pic->linesize[0] - 8*4;
  137. }
  138. }
  139. }
  140. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  141. {
  142. AVFilterContext *ctx = inlink->dst;
  143. AVFilterLink *outlink = ctx->outputs[0];
  144. ShowVolumeContext *s = ctx->priv;
  145. int c, i, j, k;
  146. AVFrame *out;
  147. if (!s->out || s->out->width != outlink->w ||
  148. s->out->height != outlink->h) {
  149. av_frame_free(&s->out);
  150. s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  151. if (!s->out) {
  152. av_frame_free(&insamples);
  153. return AVERROR(ENOMEM);
  154. }
  155. for (i = 0; i < outlink->h; i++)
  156. memset(s->out->data[0] + i * s->out->linesize[0], 0, outlink->w * 4);
  157. }
  158. s->out->pts = insamples->pts;
  159. for (j = 0; j < outlink->h; j++) {
  160. uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
  161. for (k = 0; k < s->w; k++) {
  162. dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] * s->f, 0);
  163. dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] * s->f, 0);
  164. dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] * s->f, 0);
  165. dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] * s->f, 0);
  166. }
  167. }
  168. for (c = 0; c < inlink->channels; c++) {
  169. float *src = (float *)insamples->extended_data[c];
  170. float max = 0;
  171. uint32_t color;
  172. for (i = 0; i < insamples->nb_samples; i++)
  173. max = FFMAX(max, src[i]);
  174. s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
  175. max = av_clipf(max, 0, 1);
  176. s->values[c * VAR_VARS_NB + VAR_CHANNEL] = c;
  177. color = av_expr_eval(s->c_expr, &s->values[c * VAR_VARS_NB], NULL);
  178. for (j = 0; j < s->h; j++) {
  179. uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
  180. for (k = 0; k < s->w * max; k++)
  181. AV_WN32A(dst + k * 4, color);
  182. }
  183. if (s->h >= 8 && s->draw_text)
  184. drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2,
  185. av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c)));
  186. }
  187. av_frame_free(&insamples);
  188. out = av_frame_clone(s->out);
  189. if (!out)
  190. return AVERROR(ENOMEM);
  191. av_frame_make_writable(out);
  192. for (c = 0; c < inlink->channels && s->draw_volume; c++) {
  193. if (s->h >= 8) {
  194. char buf[16];
  195. snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
  196. drawtext(out, FFMAX(0, s->w - 8 * (int)strlen(buf)), c * (s->h + s->b) + (s->h - 8) / 2, buf);
  197. }
  198. }
  199. return ff_filter_frame(outlink, out);
  200. }
  201. static av_cold void uninit(AVFilterContext *ctx)
  202. {
  203. ShowVolumeContext *s = ctx->priv;
  204. av_frame_free(&s->out);
  205. av_expr_free(s->c_expr);
  206. av_freep(&s->values);
  207. }
  208. static const AVFilterPad showvolume_inputs[] = {
  209. {
  210. .name = "default",
  211. .type = AVMEDIA_TYPE_AUDIO,
  212. .config_props = config_input,
  213. .filter_frame = filter_frame,
  214. },
  215. { NULL }
  216. };
  217. static const AVFilterPad showvolume_outputs[] = {
  218. {
  219. .name = "default",
  220. .type = AVMEDIA_TYPE_VIDEO,
  221. .config_props = config_output,
  222. },
  223. { NULL }
  224. };
  225. AVFilter ff_avf_showvolume = {
  226. .name = "showvolume",
  227. .description = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
  228. .init = init,
  229. .uninit = uninit,
  230. .query_formats = query_formats,
  231. .priv_size = sizeof(ShowVolumeContext),
  232. .inputs = showvolume_inputs,
  233. .outputs = showvolume_outputs,
  234. .priv_class = &showvolume_class,
  235. };