avf_showwaves.c 8.5 KB

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