avf_showwaves.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_P2P,
  36. MODE_CENTERED_LINE,
  37. MODE_NB,
  38. };
  39. typedef struct {
  40. const AVClass *class;
  41. int w, h;
  42. AVRational rate;
  43. int buf_idx;
  44. int16_t *buf_idy; /* y coordinate of previous sample for each channel */
  45. AVFrame *outpicref;
  46. int req_fullfilled;
  47. int n;
  48. int sample_count_mod;
  49. enum ShowWavesMode mode;
  50. int split_channels;
  51. void (*draw_sample)(uint8_t *buf, int height, int linesize,
  52. int16_t sample, int16_t *prev_y, int intensity);
  53. } ShowWavesContext;
  54. #define OFFSET(x) offsetof(ShowWavesContext, x)
  55. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  56. static const AVOption showwaves_options[] = {
  57. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
  58. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
  59. { "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
  60. { "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
  61. { "line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
  62. { "p2p", "draw a line between samples", 0, AV_OPT_TYPE_CONST, {.i64=MODE_P2P}, .flags=FLAGS, .unit="mode"},
  63. { "cline", "draw a centered line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CENTERED_LINE}, .flags=FLAGS, .unit="mode"},
  64. { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  65. { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
  66. { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
  67. { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
  68. { NULL }
  69. };
  70. AVFILTER_DEFINE_CLASS(showwaves);
  71. static av_cold void uninit(AVFilterContext *ctx)
  72. {
  73. ShowWavesContext *showwaves = ctx->priv;
  74. av_frame_free(&showwaves->outpicref);
  75. av_freep(&showwaves->buf_idy);
  76. }
  77. static int query_formats(AVFilterContext *ctx)
  78. {
  79. AVFilterFormats *formats = NULL;
  80. AVFilterChannelLayouts *layouts = NULL;
  81. AVFilterLink *inlink = ctx->inputs[0];
  82. AVFilterLink *outlink = ctx->outputs[0];
  83. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
  84. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  85. /* set input audio formats */
  86. formats = ff_make_format_list(sample_fmts);
  87. if (!formats)
  88. return AVERROR(ENOMEM);
  89. ff_formats_ref(formats, &inlink->out_formats);
  90. layouts = ff_all_channel_layouts();
  91. if (!layouts)
  92. return AVERROR(ENOMEM);
  93. ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
  94. formats = ff_all_samplerates();
  95. if (!formats)
  96. return AVERROR(ENOMEM);
  97. ff_formats_ref(formats, &inlink->out_samplerates);
  98. /* set output video format */
  99. formats = ff_make_format_list(pix_fmts);
  100. if (!formats)
  101. return AVERROR(ENOMEM);
  102. ff_formats_ref(formats, &outlink->in_formats);
  103. return 0;
  104. }
  105. static int config_output(AVFilterLink *outlink)
  106. {
  107. AVFilterContext *ctx = outlink->src;
  108. AVFilterLink *inlink = ctx->inputs[0];
  109. ShowWavesContext *showwaves = ctx->priv;
  110. int nb_channels = inlink->channels;
  111. if (!showwaves->n)
  112. showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
  113. showwaves->buf_idx = 0;
  114. if (!(showwaves->buf_idy = av_mallocz_array(nb_channels, sizeof(*showwaves->buf_idy)))) {
  115. av_log(ctx, AV_LOG_ERROR, "Could not allocate showwaves buffer\n");
  116. return AVERROR(ENOMEM);
  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 int push_frame(AVFilterLink *outlink)
  128. {
  129. AVFilterContext *ctx = outlink->src;
  130. AVFilterLink *inlink = ctx->inputs[0];
  131. ShowWavesContext *showwaves = outlink->src->priv;
  132. int nb_channels = inlink->channels;
  133. int ret, i;
  134. if ((ret = ff_filter_frame(outlink, showwaves->outpicref)) >= 0)
  135. showwaves->req_fullfilled = 1;
  136. showwaves->outpicref = NULL;
  137. showwaves->buf_idx = 0;
  138. for (i = 0; i < nb_channels; i++)
  139. showwaves->buf_idy[i] = 0;
  140. return ret;
  141. }
  142. static int request_frame(AVFilterLink *outlink)
  143. {
  144. ShowWavesContext *showwaves = outlink->src->priv;
  145. AVFilterLink *inlink = outlink->src->inputs[0];
  146. int ret;
  147. showwaves->req_fullfilled = 0;
  148. do {
  149. ret = ff_request_frame(inlink);
  150. } while (!showwaves->req_fullfilled && ret >= 0);
  151. if (ret == AVERROR_EOF && showwaves->outpicref)
  152. push_frame(outlink);
  153. return ret;
  154. }
  155. static void draw_sample_point(uint8_t *buf, int height, int linesize,
  156. int16_t sample, int16_t *prev_y, int intensity)
  157. {
  158. const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
  159. if (h >= 0 && h < height)
  160. buf[h * linesize] += intensity;
  161. }
  162. static void draw_sample_line(uint8_t *buf, int height, int linesize,
  163. int16_t sample, int16_t *prev_y, int intensity)
  164. {
  165. int k;
  166. const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
  167. int start = height/2;
  168. int end = av_clip(h, 0, height-1);
  169. if (start > end)
  170. FFSWAP(int16_t, start, end);
  171. for (k = start; k < end; k++)
  172. buf[k * linesize] += intensity;
  173. }
  174. static void draw_sample_p2p(uint8_t *buf, int height, int linesize,
  175. int16_t sample, int16_t *prev_y, int intensity)
  176. {
  177. int k;
  178. const int h = height/2 - av_rescale(sample, height/2, INT16_MAX);
  179. if (h >= 0 && h < height) {
  180. buf[h * linesize] += intensity;
  181. if (*prev_y && h != *prev_y) {
  182. int start = *prev_y;
  183. int end = av_clip(h, 0, height-1);
  184. if (start > end)
  185. FFSWAP(int16_t, start, end);
  186. for (k = start + 1; k < end; k++)
  187. buf[k * linesize] += intensity;
  188. }
  189. }
  190. *prev_y = h;
  191. }
  192. static void draw_sample_cline(uint8_t *buf, int height, int linesize,
  193. int16_t sample, int16_t *prev_y, int intensity)
  194. {
  195. int k;
  196. const int h = av_rescale(abs(sample), height, INT16_MAX);
  197. const int start = (height - h) / 2;
  198. const int end = start + h;
  199. for (k = start; k < end; k++)
  200. buf[k * linesize] += intensity;
  201. }
  202. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  203. {
  204. AVFilterContext *ctx = inlink->dst;
  205. AVFilterLink *outlink = ctx->outputs[0];
  206. ShowWavesContext *showwaves = ctx->priv;
  207. const int nb_samples = insamples->nb_samples;
  208. AVFrame *outpicref = showwaves->outpicref;
  209. int linesize = outpicref ? outpicref->linesize[0] : 0;
  210. int16_t *p = (int16_t *)insamples->data[0];
  211. int nb_channels = inlink->channels;
  212. int i, j, ret = 0;
  213. const int n = showwaves->n;
  214. const int x = 255 / ((showwaves->split_channels ? 1 : nb_channels) * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
  215. const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
  216. /* draw data in the buffer */
  217. for (i = 0; i < nb_samples; i++) {
  218. if (!showwaves->outpicref) {
  219. showwaves->outpicref = outpicref =
  220. ff_get_video_buffer(outlink, outlink->w, outlink->h);
  221. if (!outpicref)
  222. return AVERROR(ENOMEM);
  223. outpicref->width = outlink->w;
  224. outpicref->height = outlink->h;
  225. outpicref->pts = insamples->pts +
  226. av_rescale_q((p - (int16_t *)insamples->data[0]) / nb_channels,
  227. (AVRational){ 1, inlink->sample_rate },
  228. outlink->time_base);
  229. linesize = outpicref->linesize[0];
  230. for (j = 0; j < outlink->h; j++)
  231. memset(outpicref->data[0] + j * linesize, 0, outlink->w);
  232. }
  233. for (j = 0; j < nb_channels; j++) {
  234. uint8_t *buf = outpicref->data[0] + showwaves->buf_idx;
  235. if (showwaves->split_channels)
  236. buf += j*ch_height*linesize;
  237. showwaves->draw_sample(buf, ch_height, linesize, *p++,
  238. &showwaves->buf_idy[j], x);
  239. }
  240. showwaves->sample_count_mod++;
  241. if (showwaves->sample_count_mod == n) {
  242. showwaves->sample_count_mod = 0;
  243. showwaves->buf_idx++;
  244. }
  245. if (showwaves->buf_idx == showwaves->w)
  246. if ((ret = push_frame(outlink)) < 0)
  247. break;
  248. outpicref = showwaves->outpicref;
  249. }
  250. av_frame_free(&insamples);
  251. return ret;
  252. }
  253. static av_cold int init(AVFilterContext *ctx)
  254. {
  255. ShowWavesContext *showwaves = ctx->priv;
  256. switch (showwaves->mode) {
  257. case MODE_POINT: showwaves->draw_sample = draw_sample_point; break;
  258. case MODE_LINE: showwaves->draw_sample = draw_sample_line; break;
  259. case MODE_P2P: showwaves->draw_sample = draw_sample_p2p; break;
  260. case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline; break;
  261. default:
  262. return AVERROR_BUG;
  263. }
  264. return 0;
  265. }
  266. static const AVFilterPad showwaves_inputs[] = {
  267. {
  268. .name = "default",
  269. .type = AVMEDIA_TYPE_AUDIO,
  270. .filter_frame = filter_frame,
  271. },
  272. { NULL }
  273. };
  274. static const AVFilterPad showwaves_outputs[] = {
  275. {
  276. .name = "default",
  277. .type = AVMEDIA_TYPE_VIDEO,
  278. .config_props = config_output,
  279. .request_frame = request_frame,
  280. },
  281. { NULL }
  282. };
  283. AVFilter ff_avf_showwaves = {
  284. .name = "showwaves",
  285. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
  286. .init = init,
  287. .uninit = uninit,
  288. .query_formats = query_formats,
  289. .priv_size = sizeof(ShowWavesContext),
  290. .inputs = showwaves_inputs,
  291. .outputs = showwaves_outputs,
  292. .priv_class = &showwaves_class,
  293. };