avf_showwaves.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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/avassert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/parseutils.h"
  29. #include "avfilter.h"
  30. #include "filters.h"
  31. #include "formats.h"
  32. #include "audio.h"
  33. #include "video.h"
  34. #include "internal.h"
  35. enum ShowWavesMode {
  36. MODE_POINT,
  37. MODE_LINE,
  38. MODE_P2P,
  39. MODE_CENTERED_LINE,
  40. MODE_NB,
  41. };
  42. enum ShowWavesScale {
  43. SCALE_LIN,
  44. SCALE_LOG,
  45. SCALE_SQRT,
  46. SCALE_CBRT,
  47. SCALE_NB,
  48. };
  49. enum ShowWavesDrawMode {
  50. DRAW_SCALE,
  51. DRAW_FULL,
  52. DRAW_NB,
  53. };
  54. struct frame_node {
  55. AVFrame *frame;
  56. struct frame_node *next;
  57. };
  58. typedef struct ShowWavesContext {
  59. const AVClass *class;
  60. int w, h;
  61. AVRational rate;
  62. char *colors;
  63. int buf_idx;
  64. int16_t *buf_idy; /* y coordinate of previous sample for each channel */
  65. AVFrame *outpicref;
  66. int n;
  67. int pixstep;
  68. int sample_count_mod;
  69. int mode; ///< ShowWavesMode
  70. int scale; ///< ShowWavesScale
  71. int draw_mode; ///< ShowWavesDrawMode
  72. int split_channels;
  73. uint8_t *fg;
  74. int (*get_h)(int16_t sample, int height);
  75. void (*draw_sample)(uint8_t *buf, int height, int linesize,
  76. int16_t *prev_y, const uint8_t color[4], int h);
  77. /* single picture */
  78. int single_pic;
  79. struct frame_node *audio_frames;
  80. struct frame_node *last_frame;
  81. int64_t total_samples;
  82. int64_t *sum; /* abs sum of the samples per channel */
  83. } ShowWavesContext;
  84. #define OFFSET(x) offsetof(ShowWavesContext, x)
  85. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  86. static const AVOption showwaves_options[] = {
  87. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
  88. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
  89. { "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
  90. { "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
  91. { "line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"},
  92. { "p2p", "draw a line between samples", 0, AV_OPT_TYPE_CONST, {.i64=MODE_P2P}, .flags=FLAGS, .unit="mode"},
  93. { "cline", "draw a centered line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CENTERED_LINE}, .flags=FLAGS, .unit="mode"},
  94. { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
  95. { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
  96. { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
  97. { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
  98. { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
  99. { "scale", "set amplitude scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SCALE_NB-1, FLAGS, .unit="scale" },
  100. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LIN}, .flags=FLAGS, .unit="scale"},
  101. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LOG}, .flags=FLAGS, .unit="scale"},
  102. { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_SQRT}, .flags=FLAGS, .unit="scale"},
  103. { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_CBRT}, .flags=FLAGS, .unit="scale"},
  104. { "draw", "set draw mode", OFFSET(draw_mode), AV_OPT_TYPE_INT, {.i64 = DRAW_SCALE}, 0, DRAW_NB-1, FLAGS, .unit="draw" },
  105. { "scale", "scale pixel values for each drawn sample", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_SCALE}, .flags=FLAGS, .unit="draw"},
  106. { "full", "draw every pixel for sample directly", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_FULL}, .flags=FLAGS, .unit="draw"},
  107. { NULL }
  108. };
  109. AVFILTER_DEFINE_CLASS(showwaves);
  110. static av_cold void uninit(AVFilterContext *ctx)
  111. {
  112. ShowWavesContext *showwaves = ctx->priv;
  113. av_frame_free(&showwaves->outpicref);
  114. av_freep(&showwaves->buf_idy);
  115. av_freep(&showwaves->fg);
  116. if (showwaves->single_pic) {
  117. struct frame_node *node = showwaves->audio_frames;
  118. while (node) {
  119. struct frame_node *tmp = node;
  120. node = node->next;
  121. av_frame_free(&tmp->frame);
  122. av_freep(&tmp);
  123. }
  124. av_freep(&showwaves->sum);
  125. showwaves->last_frame = NULL;
  126. }
  127. }
  128. static int query_formats(AVFilterContext *ctx)
  129. {
  130. AVFilterFormats *formats = NULL;
  131. AVFilterChannelLayouts *layouts = NULL;
  132. AVFilterLink *inlink = ctx->inputs[0];
  133. AVFilterLink *outlink = ctx->outputs[0];
  134. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
  135. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
  136. int ret;
  137. /* set input audio formats */
  138. formats = ff_make_format_list(sample_fmts);
  139. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  140. return ret;
  141. layouts = ff_all_channel_layouts();
  142. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  143. return ret;
  144. formats = ff_all_samplerates();
  145. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  146. return ret;
  147. /* set output video format */
  148. formats = ff_make_format_list(pix_fmts);
  149. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  150. return ret;
  151. return 0;
  152. }
  153. static int get_lin_h(int16_t sample, int height)
  154. {
  155. return height/2 - av_rescale(sample, height/2, INT16_MAX);
  156. }
  157. static int get_lin_h2(int16_t sample, int height)
  158. {
  159. return av_rescale(FFABS(sample), height, INT16_MAX);
  160. }
  161. static int get_log_h(int16_t sample, int height)
  162. {
  163. return height/2 - FFSIGN(sample) * (log10(1 + FFABS(sample)) * (height/2) / log10(1 + INT16_MAX));
  164. }
  165. static int get_log_h2(int16_t sample, int height)
  166. {
  167. return log10(1 + FFABS(sample)) * height / log10(1 + INT16_MAX);
  168. }
  169. static int get_sqrt_h(int16_t sample, int height)
  170. {
  171. return height/2 - FFSIGN(sample) * (sqrt(FFABS(sample)) * (height/2) / sqrt(INT16_MAX));
  172. }
  173. static int get_sqrt_h2(int16_t sample, int height)
  174. {
  175. return sqrt(FFABS(sample)) * height / sqrt(INT16_MAX);
  176. }
  177. static int get_cbrt_h(int16_t sample, int height)
  178. {
  179. return height/2 - FFSIGN(sample) * (cbrt(FFABS(sample)) * (height/2) / cbrt(INT16_MAX));
  180. }
  181. static int get_cbrt_h2(int16_t sample, int height)
  182. {
  183. return cbrt(FFABS(sample)) * height / cbrt(INT16_MAX);
  184. }
  185. static void draw_sample_point_rgba_scale(uint8_t *buf, int height, int linesize,
  186. int16_t *prev_y,
  187. const uint8_t color[4], int h)
  188. {
  189. if (h >= 0 && h < height) {
  190. buf[h * linesize + 0] += color[0];
  191. buf[h * linesize + 1] += color[1];
  192. buf[h * linesize + 2] += color[2];
  193. buf[h * linesize + 3] += color[3];
  194. }
  195. }
  196. static void draw_sample_point_rgba_full(uint8_t *buf, int height, int linesize,
  197. int16_t *prev_y,
  198. const uint8_t color[4], int h)
  199. {
  200. if (h >= 0 && h < height) {
  201. buf[h * linesize + 0] = color[0];
  202. buf[h * linesize + 1] = color[1];
  203. buf[h * linesize + 2] = color[2];
  204. buf[h * linesize + 3] = color[3];
  205. }
  206. }
  207. static void draw_sample_line_rgba_scale(uint8_t *buf, int height, int linesize,
  208. int16_t *prev_y,
  209. const uint8_t color[4], int h)
  210. {
  211. int k;
  212. int start = height/2;
  213. int end = av_clip(h, 0, height-1);
  214. if (start > end)
  215. FFSWAP(int16_t, start, end);
  216. for (k = start; k < end; k++) {
  217. buf[k * linesize + 0] += color[0];
  218. buf[k * linesize + 1] += color[1];
  219. buf[k * linesize + 2] += color[2];
  220. buf[k * linesize + 3] += color[3];
  221. }
  222. }
  223. static void draw_sample_line_rgba_full(uint8_t *buf, int height, int linesize,
  224. int16_t *prev_y,
  225. const uint8_t color[4], int h)
  226. {
  227. int k;
  228. int start = height/2;
  229. int end = av_clip(h, 0, height-1);
  230. if (start > end)
  231. FFSWAP(int16_t, start, end);
  232. for (k = start; k < end; k++) {
  233. buf[k * linesize + 0] = color[0];
  234. buf[k * linesize + 1] = color[1];
  235. buf[k * linesize + 2] = color[2];
  236. buf[k * linesize + 3] = color[3];
  237. }
  238. }
  239. static void draw_sample_p2p_rgba_scale(uint8_t *buf, int height, int linesize,
  240. int16_t *prev_y,
  241. const uint8_t color[4], int h)
  242. {
  243. int k;
  244. if (h >= 0 && h < height) {
  245. buf[h * linesize + 0] += color[0];
  246. buf[h * linesize + 1] += color[1];
  247. buf[h * linesize + 2] += color[2];
  248. buf[h * linesize + 3] += color[3];
  249. if (*prev_y && h != *prev_y) {
  250. int start = *prev_y;
  251. int end = av_clip(h, 0, height-1);
  252. if (start > end)
  253. FFSWAP(int16_t, start, end);
  254. for (k = start + 1; k < end; k++) {
  255. buf[k * linesize + 0] += color[0];
  256. buf[k * linesize + 1] += color[1];
  257. buf[k * linesize + 2] += color[2];
  258. buf[k * linesize + 3] += color[3];
  259. }
  260. }
  261. }
  262. *prev_y = h;
  263. }
  264. static void draw_sample_p2p_rgba_full(uint8_t *buf, int height, int linesize,
  265. int16_t *prev_y,
  266. const uint8_t color[4], int h)
  267. {
  268. int k;
  269. if (h >= 0 && h < height) {
  270. buf[h * linesize + 0] = color[0];
  271. buf[h * linesize + 1] = color[1];
  272. buf[h * linesize + 2] = color[2];
  273. buf[h * linesize + 3] = color[3];
  274. if (*prev_y && h != *prev_y) {
  275. int start = *prev_y;
  276. int end = av_clip(h, 0, height-1);
  277. if (start > end)
  278. FFSWAP(int16_t, start, end);
  279. for (k = start + 1; k < end; k++) {
  280. buf[k * linesize + 0] = color[0];
  281. buf[k * linesize + 1] = color[1];
  282. buf[k * linesize + 2] = color[2];
  283. buf[k * linesize + 3] = color[3];
  284. }
  285. }
  286. }
  287. *prev_y = h;
  288. }
  289. static void draw_sample_cline_rgba_scale(uint8_t *buf, int height, int linesize,
  290. int16_t *prev_y,
  291. const uint8_t color[4], int h)
  292. {
  293. int k;
  294. const int start = (height - h) / 2;
  295. const int end = start + h;
  296. for (k = start; k < end; k++) {
  297. buf[k * linesize + 0] += color[0];
  298. buf[k * linesize + 1] += color[1];
  299. buf[k * linesize + 2] += color[2];
  300. buf[k * linesize + 3] += color[3];
  301. }
  302. }
  303. static void draw_sample_cline_rgba_full(uint8_t *buf, int height, int linesize,
  304. int16_t *prev_y,
  305. const uint8_t color[4], int h)
  306. {
  307. int k;
  308. const int start = (height - h) / 2;
  309. const int end = start + h;
  310. for (k = start; k < end; k++) {
  311. buf[k * linesize + 0] = color[0];
  312. buf[k * linesize + 1] = color[1];
  313. buf[k * linesize + 2] = color[2];
  314. buf[k * linesize + 3] = color[3];
  315. }
  316. }
  317. static void draw_sample_point_gray(uint8_t *buf, int height, int linesize,
  318. int16_t *prev_y,
  319. const uint8_t color[4], int h)
  320. {
  321. if (h >= 0 && h < height)
  322. buf[h * linesize] += color[0];
  323. }
  324. static void draw_sample_line_gray(uint8_t *buf, int height, int linesize,
  325. int16_t *prev_y,
  326. const uint8_t color[4], int h)
  327. {
  328. int k;
  329. int start = height/2;
  330. int end = av_clip(h, 0, height-1);
  331. if (start > end)
  332. FFSWAP(int16_t, start, end);
  333. for (k = start; k < end; k++)
  334. buf[k * linesize] += color[0];
  335. }
  336. static void draw_sample_p2p_gray(uint8_t *buf, int height, int linesize,
  337. int16_t *prev_y,
  338. const uint8_t color[4], int h)
  339. {
  340. int k;
  341. if (h >= 0 && h < height) {
  342. buf[h * linesize] += color[0];
  343. if (*prev_y && h != *prev_y) {
  344. int start = *prev_y;
  345. int end = av_clip(h, 0, height-1);
  346. if (start > end)
  347. FFSWAP(int16_t, start, end);
  348. for (k = start + 1; k < end; k++)
  349. buf[k * linesize] += color[0];
  350. }
  351. }
  352. *prev_y = h;
  353. }
  354. static void draw_sample_cline_gray(uint8_t *buf, int height, int linesize,
  355. int16_t *prev_y,
  356. const uint8_t color[4], int h)
  357. {
  358. int k;
  359. const int start = (height - h) / 2;
  360. const int end = start + h;
  361. for (k = start; k < end; k++)
  362. buf[k * linesize] += color[0];
  363. }
  364. static int config_output(AVFilterLink *outlink)
  365. {
  366. AVFilterContext *ctx = outlink->src;
  367. AVFilterLink *inlink = ctx->inputs[0];
  368. ShowWavesContext *showwaves = ctx->priv;
  369. int nb_channels = inlink->channels;
  370. char *colors, *saveptr = NULL;
  371. uint8_t x;
  372. int ch;
  373. if (showwaves->single_pic)
  374. showwaves->n = 1;
  375. if (!showwaves->n)
  376. showwaves->n = FFMAX(1, av_rescale_q(inlink->sample_rate, av_make_q(1, showwaves->w), showwaves->rate));
  377. showwaves->buf_idx = 0;
  378. if (!(showwaves->buf_idy = av_mallocz_array(nb_channels, sizeof(*showwaves->buf_idy)))) {
  379. av_log(ctx, AV_LOG_ERROR, "Could not allocate showwaves buffer\n");
  380. return AVERROR(ENOMEM);
  381. }
  382. outlink->w = showwaves->w;
  383. outlink->h = showwaves->h;
  384. outlink->sample_aspect_ratio = (AVRational){1,1};
  385. outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
  386. (AVRational){showwaves->w,1});
  387. av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n",
  388. showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
  389. switch (outlink->format) {
  390. case AV_PIX_FMT_GRAY8:
  391. switch (showwaves->mode) {
  392. case MODE_POINT: showwaves->draw_sample = draw_sample_point_gray; break;
  393. case MODE_LINE: showwaves->draw_sample = draw_sample_line_gray; break;
  394. case MODE_P2P: showwaves->draw_sample = draw_sample_p2p_gray; break;
  395. case MODE_CENTERED_LINE: showwaves->draw_sample = draw_sample_cline_gray; break;
  396. default:
  397. return AVERROR_BUG;
  398. }
  399. showwaves->pixstep = 1;
  400. break;
  401. case AV_PIX_FMT_RGBA:
  402. switch (showwaves->mode) {
  403. case MODE_POINT: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_point_rgba_scale : draw_sample_point_rgba_full; break;
  404. case MODE_LINE: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_line_rgba_scale : draw_sample_line_rgba_full; break;
  405. case MODE_P2P: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_p2p_rgba_scale : draw_sample_p2p_rgba_full; break;
  406. case MODE_CENTERED_LINE: showwaves->draw_sample = showwaves->draw_mode == DRAW_SCALE ? draw_sample_cline_rgba_scale : draw_sample_cline_rgba_full; break;
  407. default:
  408. return AVERROR_BUG;
  409. }
  410. showwaves->pixstep = 4;
  411. break;
  412. }
  413. switch (showwaves->scale) {
  414. case SCALE_LIN:
  415. switch (showwaves->mode) {
  416. case MODE_POINT:
  417. case MODE_LINE:
  418. case MODE_P2P: showwaves->get_h = get_lin_h; break;
  419. case MODE_CENTERED_LINE: showwaves->get_h = get_lin_h2; break;
  420. default:
  421. return AVERROR_BUG;
  422. }
  423. break;
  424. case SCALE_LOG:
  425. switch (showwaves->mode) {
  426. case MODE_POINT:
  427. case MODE_LINE:
  428. case MODE_P2P: showwaves->get_h = get_log_h; break;
  429. case MODE_CENTERED_LINE: showwaves->get_h = get_log_h2; break;
  430. default:
  431. return AVERROR_BUG;
  432. }
  433. break;
  434. case SCALE_SQRT:
  435. switch (showwaves->mode) {
  436. case MODE_POINT:
  437. case MODE_LINE:
  438. case MODE_P2P: showwaves->get_h = get_sqrt_h; break;
  439. case MODE_CENTERED_LINE: showwaves->get_h = get_sqrt_h2; break;
  440. default:
  441. return AVERROR_BUG;
  442. }
  443. break;
  444. case SCALE_CBRT:
  445. switch (showwaves->mode) {
  446. case MODE_POINT:
  447. case MODE_LINE:
  448. case MODE_P2P: showwaves->get_h = get_cbrt_h; break;
  449. case MODE_CENTERED_LINE: showwaves->get_h = get_cbrt_h2; break;
  450. default:
  451. return AVERROR_BUG;
  452. }
  453. break;
  454. }
  455. showwaves->fg = av_malloc_array(nb_channels, 4 * sizeof(*showwaves->fg));
  456. if (!showwaves->fg)
  457. return AVERROR(ENOMEM);
  458. colors = av_strdup(showwaves->colors);
  459. if (!colors)
  460. return AVERROR(ENOMEM);
  461. if (showwaves->draw_mode == DRAW_SCALE) {
  462. /* multiplication factor, pre-computed to avoid in-loop divisions */
  463. x = 255 / ((showwaves->split_channels ? 1 : nb_channels) * showwaves->n);
  464. } else {
  465. x = 255;
  466. }
  467. if (outlink->format == AV_PIX_FMT_RGBA) {
  468. uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
  469. for (ch = 0; ch < nb_channels; ch++) {
  470. char *color;
  471. color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
  472. if (color)
  473. av_parse_color(fg, color, -1, ctx);
  474. showwaves->fg[4*ch + 0] = fg[0] * x / 255.;
  475. showwaves->fg[4*ch + 1] = fg[1] * x / 255.;
  476. showwaves->fg[4*ch + 2] = fg[2] * x / 255.;
  477. showwaves->fg[4*ch + 3] = fg[3] * x / 255.;
  478. }
  479. } else {
  480. for (ch = 0; ch < nb_channels; ch++)
  481. showwaves->fg[4 * ch + 0] = x;
  482. }
  483. av_free(colors);
  484. return 0;
  485. }
  486. inline static int push_frame(AVFilterLink *outlink)
  487. {
  488. AVFilterContext *ctx = outlink->src;
  489. AVFilterLink *inlink = ctx->inputs[0];
  490. ShowWavesContext *showwaves = outlink->src->priv;
  491. int nb_channels = inlink->channels;
  492. int ret, i;
  493. ret = ff_filter_frame(outlink, showwaves->outpicref);
  494. showwaves->outpicref = NULL;
  495. showwaves->buf_idx = 0;
  496. for (i = 0; i < nb_channels; i++)
  497. showwaves->buf_idy[i] = 0;
  498. return ret;
  499. }
  500. static int push_single_pic(AVFilterLink *outlink)
  501. {
  502. AVFilterContext *ctx = outlink->src;
  503. AVFilterLink *inlink = ctx->inputs[0];
  504. ShowWavesContext *showwaves = ctx->priv;
  505. int64_t n = 0, column_max_samples = showwaves->total_samples / outlink->w;
  506. int64_t remaining_samples = showwaves->total_samples - (column_max_samples * outlink->w);
  507. int64_t last_column_samples = column_max_samples + remaining_samples;
  508. AVFrame *out = showwaves->outpicref;
  509. struct frame_node *node;
  510. const int nb_channels = inlink->channels;
  511. const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
  512. const int linesize = out->linesize[0];
  513. const int pixstep = showwaves->pixstep;
  514. int col = 0;
  515. int64_t *sum = showwaves->sum;
  516. if (column_max_samples == 0) {
  517. av_log(ctx, AV_LOG_ERROR, "Too few samples\n");
  518. return AVERROR(EINVAL);
  519. }
  520. av_log(ctx, AV_LOG_DEBUG, "Create frame averaging %"PRId64" samples per column\n", column_max_samples);
  521. memset(sum, 0, nb_channels);
  522. for (node = showwaves->audio_frames; node; node = node->next) {
  523. int i;
  524. const AVFrame *frame = node->frame;
  525. const int16_t *p = (const int16_t *)frame->data[0];
  526. for (i = 0; i < frame->nb_samples; i++) {
  527. int64_t max_samples = col == outlink->w - 1 ? last_column_samples: column_max_samples;
  528. int ch;
  529. for (ch = 0; ch < nb_channels; ch++)
  530. sum[ch] += abs(p[ch + i*nb_channels]) << 1;
  531. n++;
  532. if (n == max_samples) {
  533. for (ch = 0; ch < nb_channels; ch++) {
  534. int16_t sample = sum[ch] / max_samples;
  535. uint8_t *buf = out->data[0] + col * pixstep;
  536. int h;
  537. if (showwaves->split_channels)
  538. buf += ch*ch_height*linesize;
  539. av_assert0(col < outlink->w);
  540. h = showwaves->get_h(sample, ch_height);
  541. showwaves->draw_sample(buf, ch_height, linesize, &showwaves->buf_idy[ch], &showwaves->fg[ch * 4], h);
  542. sum[ch] = 0;
  543. }
  544. col++;
  545. n = 0;
  546. }
  547. }
  548. }
  549. return push_frame(outlink);
  550. }
  551. static int request_frame(AVFilterLink *outlink)
  552. {
  553. ShowWavesContext *showwaves = outlink->src->priv;
  554. AVFilterLink *inlink = outlink->src->inputs[0];
  555. int ret;
  556. ret = ff_request_frame(inlink);
  557. if (ret == AVERROR_EOF && showwaves->outpicref) {
  558. if (showwaves->single_pic)
  559. push_single_pic(outlink);
  560. else
  561. push_frame(outlink);
  562. }
  563. return ret;
  564. }
  565. static int alloc_out_frame(ShowWavesContext *showwaves, const int16_t *p,
  566. const AVFilterLink *inlink, AVFilterLink *outlink,
  567. const AVFrame *in)
  568. {
  569. if (!showwaves->outpicref) {
  570. int j;
  571. AVFrame *out = showwaves->outpicref =
  572. ff_get_video_buffer(outlink, outlink->w, outlink->h);
  573. if (!out)
  574. return AVERROR(ENOMEM);
  575. out->width = outlink->w;
  576. out->height = outlink->h;
  577. out->pts = in->pts + av_rescale_q((p - (int16_t *)in->data[0]) / inlink->channels,
  578. av_make_q(1, inlink->sample_rate),
  579. outlink->time_base);
  580. for (j = 0; j < outlink->h; j++)
  581. memset(out->data[0] + j*out->linesize[0], 0, outlink->w * showwaves->pixstep);
  582. }
  583. return 0;
  584. }
  585. static av_cold int init(AVFilterContext *ctx)
  586. {
  587. ShowWavesContext *showwaves = ctx->priv;
  588. if (!strcmp(ctx->filter->name, "showwavespic")) {
  589. showwaves->single_pic = 1;
  590. showwaves->mode = MODE_CENTERED_LINE;
  591. }
  592. return 0;
  593. }
  594. #if CONFIG_SHOWWAVES_FILTER
  595. static int showwaves_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  596. {
  597. AVFilterContext *ctx = inlink->dst;
  598. AVFilterLink *outlink = ctx->outputs[0];
  599. ShowWavesContext *showwaves = ctx->priv;
  600. const int nb_samples = insamples->nb_samples;
  601. AVFrame *outpicref = showwaves->outpicref;
  602. int16_t *p = (int16_t *)insamples->data[0];
  603. int nb_channels = inlink->channels;
  604. int i, j, ret = 0;
  605. const int pixstep = showwaves->pixstep;
  606. const int n = showwaves->n;
  607. const int ch_height = showwaves->split_channels ? outlink->h / nb_channels : outlink->h;
  608. /* draw data in the buffer */
  609. for (i = 0; i < nb_samples; i++) {
  610. ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
  611. if (ret < 0)
  612. goto end;
  613. outpicref = showwaves->outpicref;
  614. for (j = 0; j < nb_channels; j++) {
  615. uint8_t *buf = outpicref->data[0] + showwaves->buf_idx * pixstep;
  616. const int linesize = outpicref->linesize[0];
  617. int h;
  618. if (showwaves->split_channels)
  619. buf += j*ch_height*linesize;
  620. h = showwaves->get_h(*p++, ch_height);
  621. showwaves->draw_sample(buf, ch_height, linesize,
  622. &showwaves->buf_idy[j], &showwaves->fg[j * 4], h);
  623. }
  624. showwaves->sample_count_mod++;
  625. if (showwaves->sample_count_mod == n) {
  626. showwaves->sample_count_mod = 0;
  627. showwaves->buf_idx++;
  628. }
  629. if (showwaves->buf_idx == showwaves->w ||
  630. (ff_outlink_get_status(inlink) && i == nb_samples - 1))
  631. if ((ret = push_frame(outlink)) < 0)
  632. break;
  633. outpicref = showwaves->outpicref;
  634. }
  635. end:
  636. av_frame_free(&insamples);
  637. return ret;
  638. }
  639. static int activate(AVFilterContext *ctx)
  640. {
  641. AVFilterLink *inlink = ctx->inputs[0];
  642. AVFilterLink *outlink = ctx->outputs[0];
  643. ShowWavesContext *showwaves = ctx->priv;
  644. AVFrame *in;
  645. const int nb_samples = showwaves->n * outlink->w;
  646. int ret;
  647. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  648. ret = ff_inlink_consume_samples(inlink, nb_samples, nb_samples, &in);
  649. if (ret < 0)
  650. return ret;
  651. if (ret > 0)
  652. return showwaves_filter_frame(inlink, in);
  653. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  654. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  655. return FFERROR_NOT_READY;
  656. }
  657. static const AVFilterPad showwaves_inputs[] = {
  658. {
  659. .name = "default",
  660. .type = AVMEDIA_TYPE_AUDIO,
  661. },
  662. { NULL }
  663. };
  664. static const AVFilterPad showwaves_outputs[] = {
  665. {
  666. .name = "default",
  667. .type = AVMEDIA_TYPE_VIDEO,
  668. .config_props = config_output,
  669. },
  670. { NULL }
  671. };
  672. AVFilter ff_avf_showwaves = {
  673. .name = "showwaves",
  674. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
  675. .init = init,
  676. .uninit = uninit,
  677. .query_formats = query_formats,
  678. .priv_size = sizeof(ShowWavesContext),
  679. .inputs = showwaves_inputs,
  680. .activate = activate,
  681. .outputs = showwaves_outputs,
  682. .priv_class = &showwaves_class,
  683. };
  684. #endif // CONFIG_SHOWWAVES_FILTER
  685. #if CONFIG_SHOWWAVESPIC_FILTER
  686. #define OFFSET(x) offsetof(ShowWavesContext, x)
  687. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  688. static const AVOption showwavespic_options[] = {
  689. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
  690. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
  691. { "split_channels", "draw channels separately", OFFSET(split_channels), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
  692. { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
  693. { "scale", "set amplitude scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, SCALE_NB-1, FLAGS, .unit="scale" },
  694. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LIN}, .flags=FLAGS, .unit="scale"},
  695. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_LOG}, .flags=FLAGS, .unit="scale"},
  696. { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_SQRT}, .flags=FLAGS, .unit="scale"},
  697. { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=SCALE_CBRT}, .flags=FLAGS, .unit="scale"},
  698. { "draw", "set draw mode", OFFSET(draw_mode), AV_OPT_TYPE_INT, {.i64 = DRAW_SCALE}, 0, DRAW_NB-1, FLAGS, .unit="draw" },
  699. { "scale", "scale pixel values for each drawn sample", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_SCALE}, .flags=FLAGS, .unit="draw"},
  700. { "full", "draw every pixel for sample directly", 0, AV_OPT_TYPE_CONST, {.i64=DRAW_FULL}, .flags=FLAGS, .unit="draw"},
  701. { NULL }
  702. };
  703. AVFILTER_DEFINE_CLASS(showwavespic);
  704. static int showwavespic_config_input(AVFilterLink *inlink)
  705. {
  706. AVFilterContext *ctx = inlink->dst;
  707. ShowWavesContext *showwaves = ctx->priv;
  708. if (showwaves->single_pic) {
  709. showwaves->sum = av_mallocz_array(inlink->channels, sizeof(*showwaves->sum));
  710. if (!showwaves->sum)
  711. return AVERROR(ENOMEM);
  712. }
  713. return 0;
  714. }
  715. static int showwavespic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  716. {
  717. AVFilterContext *ctx = inlink->dst;
  718. AVFilterLink *outlink = ctx->outputs[0];
  719. ShowWavesContext *showwaves = ctx->priv;
  720. int16_t *p = (int16_t *)insamples->data[0];
  721. int ret = 0;
  722. if (showwaves->single_pic) {
  723. struct frame_node *f;
  724. ret = alloc_out_frame(showwaves, p, inlink, outlink, insamples);
  725. if (ret < 0)
  726. goto end;
  727. /* queue the audio frame */
  728. f = av_malloc(sizeof(*f));
  729. if (!f) {
  730. ret = AVERROR(ENOMEM);
  731. goto end;
  732. }
  733. f->frame = insamples;
  734. f->next = NULL;
  735. if (!showwaves->last_frame) {
  736. showwaves->audio_frames =
  737. showwaves->last_frame = f;
  738. } else {
  739. showwaves->last_frame->next = f;
  740. showwaves->last_frame = f;
  741. }
  742. showwaves->total_samples += insamples->nb_samples;
  743. return 0;
  744. }
  745. end:
  746. av_frame_free(&insamples);
  747. return ret;
  748. }
  749. static const AVFilterPad showwavespic_inputs[] = {
  750. {
  751. .name = "default",
  752. .type = AVMEDIA_TYPE_AUDIO,
  753. .config_props = showwavespic_config_input,
  754. .filter_frame = showwavespic_filter_frame,
  755. },
  756. { NULL }
  757. };
  758. static const AVFilterPad showwavespic_outputs[] = {
  759. {
  760. .name = "default",
  761. .type = AVMEDIA_TYPE_VIDEO,
  762. .config_props = config_output,
  763. .request_frame = request_frame,
  764. },
  765. { NULL }
  766. };
  767. AVFilter ff_avf_showwavespic = {
  768. .name = "showwavespic",
  769. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output single picture."),
  770. .init = init,
  771. .uninit = uninit,
  772. .query_formats = query_formats,
  773. .priv_size = sizeof(ShowWavesContext),
  774. .inputs = showwavespic_inputs,
  775. .outputs = showwavespic_outputs,
  776. .priv_class = &showwavespic_class,
  777. };
  778. #endif // CONFIG_SHOWWAVESPIC_FILTER