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.

262 lines
8.1KB

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