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.

289 lines
9.5KB

  1. /*
  2. * Copyright (c) 2012 Stefano Sabatini
  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. /**
  21. * @file
  22. * audio to video multimedia filter
  23. */
  24. #include "libavutil/channel_layout.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/parseutils.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "audio.h"
  30. #include "video.h"
  31. #include "internal.h"
  32. enum ShowWavesMode {
  33. MODE_POINT,
  34. MODE_LINE,
  35. MODE_NB,
  36. };
  37. typedef struct {
  38. const AVClass *class;
  39. int w, h;
  40. char *rate_str;
  41. AVRational rate;
  42. int buf_idx;
  43. AVFilterBufferRef *outpicref;
  44. int req_fullfilled;
  45. int n;
  46. int sample_count_mod;
  47. enum ShowWavesMode mode;
  48. } ShowWavesContext;
  49. #define OFFSET(x) offsetof(ShowWavesContext, x)
  50. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  51. static const AVOption showwaves_options[] = {
  52. { "rate", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  53. { "r", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  54. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
  55. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
  56. { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  57. {"mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
  58. {"point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
  59. {"line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
  60. { NULL },
  61. };
  62. AVFILTER_DEFINE_CLASS(showwaves);
  63. static av_cold int init(AVFilterContext *ctx, const char *args)
  64. {
  65. ShowWavesContext *showwaves = ctx->priv;
  66. int err;
  67. showwaves->class = &showwaves_class;
  68. av_opt_set_defaults(showwaves);
  69. showwaves->buf_idx = 0;
  70. if ((err = av_set_options_string(showwaves, args, "=", ":")) < 0)
  71. return err;
  72. return 0;
  73. }
  74. static av_cold void uninit(AVFilterContext *ctx)
  75. {
  76. ShowWavesContext *showwaves = ctx->priv;
  77. av_freep(&showwaves->rate_str);
  78. avfilter_unref_bufferp(&showwaves->outpicref);
  79. }
  80. static int query_formats(AVFilterContext *ctx)
  81. {
  82. AVFilterFormats *formats = NULL;
  83. AVFilterChannelLayouts *layouts = NULL;
  84. AVFilterLink *inlink = ctx->inputs[0];
  85. AVFilterLink *outlink = ctx->outputs[0];
  86. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
  87. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  88. /* set input audio formats */
  89. formats = ff_make_format_list(sample_fmts);
  90. if (!formats)
  91. return AVERROR(ENOMEM);
  92. ff_formats_ref(formats, &inlink->out_formats);
  93. layouts = ff_all_channel_layouts();
  94. if (!layouts)
  95. return AVERROR(ENOMEM);
  96. ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
  97. formats = ff_all_samplerates();
  98. if (!formats)
  99. return AVERROR(ENOMEM);
  100. ff_formats_ref(formats, &inlink->out_samplerates);
  101. /* set output video format */
  102. formats = ff_make_format_list(pix_fmts);
  103. if (!formats)
  104. return AVERROR(ENOMEM);
  105. ff_formats_ref(formats, &outlink->in_formats);
  106. return 0;
  107. }
  108. static int config_output(AVFilterLink *outlink)
  109. {
  110. AVFilterContext *ctx = outlink->src;
  111. AVFilterLink *inlink = ctx->inputs[0];
  112. ShowWavesContext *showwaves = ctx->priv;
  113. int err;
  114. if (showwaves->n && showwaves->rate_str) {
  115. av_log(ctx, AV_LOG_ERROR, "Options 'n' and 'rate' cannot be set at the same time\n");
  116. return AVERROR(EINVAL);
  117. }
  118. if (!showwaves->n) {
  119. if (!showwaves->rate_str)
  120. showwaves->rate = (AVRational){25,1}; /* set default value */
  121. else if ((err = av_parse_video_rate(&showwaves->rate, showwaves->rate_str)) < 0) {
  122. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", showwaves->rate_str);
  123. return err;
  124. }
  125. showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
  126. }
  127. outlink->w = showwaves->w;
  128. outlink->h = showwaves->h;
  129. outlink->sample_aspect_ratio = (AVRational){1,1};
  130. outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
  131. (AVRational){showwaves->w,1});
  132. av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
  133. showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
  134. return 0;
  135. }
  136. inline static int push_frame(AVFilterLink *outlink)
  137. {
  138. ShowWavesContext *showwaves = outlink->src->priv;
  139. int ret;
  140. if ((ret = ff_filter_frame(outlink, showwaves->outpicref)) >= 0)
  141. showwaves->req_fullfilled = 1;
  142. showwaves->outpicref = NULL;
  143. showwaves->buf_idx = 0;
  144. return ret;
  145. }
  146. static int request_frame(AVFilterLink *outlink)
  147. {
  148. ShowWavesContext *showwaves = outlink->src->priv;
  149. AVFilterLink *inlink = outlink->src->inputs[0];
  150. int ret;
  151. showwaves->req_fullfilled = 0;
  152. do {
  153. ret = ff_request_frame(inlink);
  154. } while (!showwaves->req_fullfilled && ret >= 0);
  155. if (ret == AVERROR_EOF && showwaves->outpicref)
  156. push_frame(outlink);
  157. return ret;
  158. }
  159. #define MAX_INT16 ((1<<15) -1)
  160. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  161. {
  162. AVFilterContext *ctx = inlink->dst;
  163. AVFilterLink *outlink = ctx->outputs[0];
  164. ShowWavesContext *showwaves = ctx->priv;
  165. const int nb_samples = insamples->audio->nb_samples;
  166. AVFilterBufferRef *outpicref = showwaves->outpicref;
  167. int linesize = outpicref ? outpicref->linesize[0] : 0;
  168. int16_t *p = (int16_t *)insamples->data[0];
  169. int nb_channels = av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
  170. int i, j, k, h, ret = 0;
  171. const int n = showwaves->n;
  172. const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
  173. /* draw data in the buffer */
  174. for (i = 0; i < nb_samples; i++) {
  175. if (!showwaves->outpicref) {
  176. showwaves->outpicref = outpicref =
  177. ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN,
  178. outlink->w, outlink->h);
  179. if (!outpicref)
  180. return AVERROR(ENOMEM);
  181. outpicref->video->w = outlink->w;
  182. outpicref->video->h = outlink->h;
  183. outpicref->pts = insamples->pts +
  184. av_rescale_q((p - (int16_t *)insamples->data[0]) / nb_channels,
  185. (AVRational){ 1, inlink->sample_rate },
  186. outlink->time_base);
  187. linesize = outpicref->linesize[0];
  188. memset(outpicref->data[0], 0, showwaves->h*linesize);
  189. }
  190. for (j = 0; j < nb_channels; j++) {
  191. h = showwaves->h/2 - av_rescale(*p++, showwaves->h/2, MAX_INT16);
  192. switch (showwaves->mode) {
  193. case MODE_POINT:
  194. if (h >= 0 && h < outlink->h)
  195. *(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x;
  196. break;
  197. case MODE_LINE:
  198. {
  199. int start = showwaves->h/2, end = av_clip(h, 0, outlink->h-1);
  200. if (start > end) FFSWAP(int16_t, start, end);
  201. for (k = start; k < end; k++)
  202. *(outpicref->data[0] + showwaves->buf_idx + k * linesize) += x;
  203. break;
  204. }
  205. }
  206. }
  207. showwaves->sample_count_mod++;
  208. if (showwaves->sample_count_mod == n) {
  209. showwaves->sample_count_mod = 0;
  210. showwaves->buf_idx++;
  211. }
  212. if (showwaves->buf_idx == showwaves->w)
  213. if ((ret = push_frame(outlink)) < 0)
  214. break;
  215. outpicref = showwaves->outpicref;
  216. }
  217. avfilter_unref_buffer(insamples);
  218. return ret;
  219. }
  220. static const AVFilterPad showwaves_inputs[] = {
  221. {
  222. .name = "default",
  223. .type = AVMEDIA_TYPE_AUDIO,
  224. .filter_frame = filter_frame,
  225. .min_perms = AV_PERM_READ,
  226. },
  227. { NULL }
  228. };
  229. static const AVFilterPad showwaves_outputs[] = {
  230. {
  231. .name = "default",
  232. .type = AVMEDIA_TYPE_VIDEO,
  233. .config_props = config_output,
  234. .request_frame = request_frame,
  235. },
  236. { NULL }
  237. };
  238. AVFilter avfilter_avf_showwaves = {
  239. .name = "showwaves",
  240. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
  241. .init = init,
  242. .uninit = uninit,
  243. .query_formats = query_formats,
  244. .priv_size = sizeof(ShowWavesContext),
  245. .inputs = showwaves_inputs,
  246. .outputs = showwaves_outputs,
  247. .priv_class = &showwaves_class,
  248. };