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.

272 lines
8.7KB

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