avf_showwaves.c 8.4 KB

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