avf_showwaves.c 24 KB

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