Fixes limitation of input duration that showspectrumpic can process.
Showing the changes to libavfilter (1 of 1 file). Show all
Unified · Split
| @@ -30,7 +30,6 @@ · 29 unmodified lines | ||
| 30 | 30 | #include <math.h> |
| 31 | 31 | |
| 32 | 32 | #include "libavutil/tx.h" |
| 33 | −#include "libavutil/audio_fifo.h" | |
| 34 | 33 | #include "libavutil/avassert.h" |
| 35 | 34 | #include "libavutil/avstring.h" |
| 36 | 35 | #include "libavutil/channel_layout.h" |
| @@ -53,6 +52,8 @@ enum ColorMode { CHANNEL, INTENSITY, RAINBOW, MORELAND, NEBULAE, FIRE, FIERY, · 16 unmodified lines | ||
| 53 | 52 | enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, LREPLACE, NB_SLIDES }; |
| 54 | 53 | enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS }; |
| 55 | 54 | |
| 55 | +#define DEFAULT_LENGTH 300 | |
| 56 | + | |
| 56 | 57 | typedef struct ShowSpectrumContext { |
| 57 | 58 | const AVClass *class; |
| 58 | 59 | int w, h; |
| @@ -60,6 +61,7 @@ typedef struct ShowSpectrumContext { · 1 unmodified line | ||
| 60 | 61 | AVRational auto_frame_rate; |
| 61 | 62 | AVRational frame_rate; |
| 62 | 63 | AVFrame *outpicref; |
| 64 | + AVFrame *in_frame; | |
| 63 | 65 | int nb_display_channels; |
| 64 | 66 | int orientation; |
| 65 | 67 | int channel_width; |
| @@ -95,7 +97,6 @@ typedef struct ShowSpectrumContext { · 29 unmodified lines | ||
| 95 | 97 | int hop_size; |
| 96 | 98 | float *combine_buffer; ///< color combining buffer (3 * h items) |
| 97 | 99 | float **color_buffer; ///< color buffer (3 * h * ch items) |
| 98 | − AVAudioFifo *fifo; | |
| 99 | 100 | int64_t pts; |
| 100 | 101 | int64_t old_pts; |
| 101 | 102 | int old_len; |
| @@ -104,7 +105,12 @@ typedef struct ShowSpectrumContext { · 2 unmodified lines | ||
| 104 | 105 | int start_x, start_y; |
| 105 | 106 | float drange, limit; |
| 106 | 107 | float dmin, dmax; |
| 108 | + uint64_t samples; | |
| 107 | 109 | int (*plot_channel)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); |
| 110 | + | |
| 111 | + AVFrame **frames; | |
| 112 | + unsigned int nb_frames; | |
| 113 | + unsigned int frames_size; | |
| 108 | 114 | } ShowSpectrumContext; |
| 109 | 115 | |
| 110 | 116 | #define OFFSET(x) offsetof(ShowSpectrumContext, x) |
| @@ -330,12 +336,19 @@ static av_cold void uninit(AVFilterContext *ctx) · 219 unmodified lines | ||
| 330 | 336 | } |
| 331 | 337 | av_freep(&s->magnitudes); |
| 332 | 338 | av_frame_free(&s->outpicref); |
| 333 | − av_audio_fifo_free(s->fifo); | |
| 339 | + av_frame_free(&s->in_frame); | |
| 334 | 340 | if (s->phases) { |
| 335 | 341 | for (i = 0; i < s->nb_display_channels; i++) |
| 336 | 342 | av_freep(&s->phases[i]); |
| 337 | 343 | } |
| 338 | 344 | av_freep(&s->phases); |
| 345 | + | |
| 346 | + while (s->nb_frames > 0) { | |
| 347 | + av_frame_free(&s->frames[s->nb_frames - 1]); | |
| 348 | + s->nb_frames--; | |
| 349 | + } | |
| 350 | + | |
| 351 | + av_freep(&s->frames); | |
| 339 | 352 | } |
| 340 | 353 | |
| 341 | 354 | static int query_formats(AVFilterContext *ctx) |
| @@ -380,6 +393,13 @@ static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jo · 38 unmodified lines | ||
| 380 | 393 | |
| 381 | 394 | /* fill FFT input with the number of samples available */ |
| 382 | 395 | const float *p = (float *)fin->extended_data[ch]; |
| 396 | + float *in_frame = (float *)s->in_frame->extended_data[ch]; | |
| 397 | + | |
| 398 | + memmove(in_frame, in_frame + s->hop_size, (s->fft_size - s->hop_size) * sizeof(float)); | |
| 399 | + memcpy(in_frame + s->fft_size - s->hop_size, p, fin->nb_samples * sizeof(float)); | |
| 400 | + | |
| 401 | + for (int i = fin->nb_samples; i < s->hop_size; i++) | |
| 402 | + in_frame[i + s->fft_size - s->hop_size] = 0.f; | |
| 383 | 403 | |
| 384 | 404 | if (s->stop) { |
| 385 | 405 | float theta, phi, psi, a, b, S, c; |
| @@ -391,7 +411,7 @@ static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jo · 5 unmodified lines | ||
| 391 | 411 | int M = s->win_size / 2; |
| 392 | 412 | |
| 393 | 413 | for (n = 0; n < s->win_size; n++) { |
| 394 | − s->fft_data[ch][n].re = p[n] * window_func_lut[n]; | |
| 414 | + s->fft_data[ch][n].re = in_frame[n] * window_func_lut[n]; | |
| 395 | 415 | s->fft_data[ch][n].im = 0; |
| 396 | 416 | } |
| 397 | 417 | |
| @@ -458,7 +478,7 @@ static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jo · 60 unmodified lines | ||
| 458 | 478 | } |
| 459 | 479 | } else { |
| 460 | 480 | for (n = 0; n < s->win_size; n++) { |
| 461 | − s->fft_in[ch][n].re = p[n] * window_func_lut[n]; | |
| 481 | + s->fft_in[ch][n].re = in_frame[n] * window_func_lut[n]; | |
| 462 | 482 | s->fft_in[ch][n].im = 0; |
| 463 | 483 | } |
| 464 | 484 | |
| @@ -726,7 +746,7 @@ static float get_iscale(AVFilterContext *ctx, int scale, float a) · 261 unmodified lines | ||
| 726 | 746 | return a; |
| 727 | 747 | } |
| 728 | 748 | |
| 729 | −static int draw_legend(AVFilterContext *ctx, int samples) | |
| 749 | +static int draw_legend(AVFilterContext *ctx, uint64_t samples) | |
| 730 | 750 | { |
| 731 | 751 | ShowSpectrumContext *s = ctx->priv; |
| 732 | 752 | AVFilterLink *inlink = ctx->inputs[0]; |
| @@ -1239,10 +1259,15 @@ static int config_output(AVFilterLink *outlink) · 506 unmodified lines | ||
| 1239 | 1259 | av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d FFT window size:%d\n", |
| 1240 | 1260 | s->w, s->h, s->win_size); |
| 1241 | 1261 | |
| 1242 | − av_audio_fifo_free(s->fifo); | |
| 1243 | − s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size); | |
| 1244 | − if (!s->fifo) | |
| 1262 | + s->in_frame = ff_get_audio_buffer(inlink, s->win_size); | |
| 1263 | + if (!s->in_frame) | |
| 1264 | + return AVERROR(ENOMEM); | |
| 1265 | + | |
| 1266 | + s->frames = av_fast_realloc(NULL, &s->frames_size, | |
| 1267 | + DEFAULT_LENGTH * sizeof(*(s->frames))); | |
| 1268 | + if (!s->frames) | |
| 1245 | 1269 | return AVERROR(ENOMEM); |
| 1270 | + | |
| 1246 | 1271 | return 0; |
| 1247 | 1272 | } |
| 1248 | 1273 | |
| @@ -1439,7 +1464,7 @@ static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples) · 190 unmodified lines | ||
| 1439 | 1464 | } |
| 1440 | 1465 | |
| 1441 | 1466 | if (s->sliding != FULLFRAME || s->xpos == 0) |
| 1442 | − outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base); | |
| 1467 | + s->pts = outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base); | |
| 1443 | 1468 | |
| 1444 | 1469 | if (s->sliding == LREPLACE) { |
| 1445 | 1470 | s->xpos--; |
| @@ -1507,65 +1532,40 @@ static int activate(AVFilterContext *ctx) · 61 unmodified lines | ||
| 1507 | 1532 | AVFilterLink *inlink = ctx->inputs[0]; |
| 1508 | 1533 | AVFilterLink *outlink = ctx->outputs[0]; |
| 1509 | 1534 | ShowSpectrumContext *s = ctx->priv; |
| 1510 | − int ret; | |
| 1535 | + int ret, status; | |
| 1536 | + int64_t pts; | |
| 1511 | 1537 | |
| 1512 | 1538 | FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
| 1513 | 1539 | |
| 1514 | − if (av_audio_fifo_size(s->fifo) < s->win_size) { | |
| 1515 | − AVFrame *frame = NULL; | |
| 1540 | + if (s->outpicref) { | |
| 1541 | + AVFrame *fin; | |
| 1516 | 1542 | |
| 1517 | − ret = ff_inlink_consume_frame(inlink, &frame); | |
| 1543 | + ret = ff_inlink_consume_samples(inlink, s->hop_size, s->hop_size, &fin); | |
| 1518 | 1544 | if (ret < 0) |
| 1519 | 1545 | return ret; |
| 1520 | 1546 | if (ret > 0) { |
| 1521 | − s->pts = frame->pts; | |
| 1522 | − s->consumed = 0; | |
| 1547 | + s->consumed += fin->nb_samples; | |
| 1548 | + ff_filter_execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels); | |
| 1523 | 1549 | |
| 1524 | − av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples); | |
| 1525 | − av_frame_free(&frame); | |
| 1526 | − } | |
| 1527 | − } | |
| 1550 | + if (s->data == D_MAGNITUDE) | |
| 1551 | + ff_filter_execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels); | |
| 1528 | 1552 | |
| 1529 | − if (s->outpicref && (av_audio_fifo_size(s->fifo) >= s->win_size || | |
| 1530 | − ff_outlink_get_status(inlink))) { | |
| 1531 | − AVFrame *fin = ff_get_audio_buffer(inlink, s->win_size); | |
| 1532 | − if (!fin) | |
| 1533 | − return AVERROR(ENOMEM); | |
| 1553 | + if (s->data == D_PHASE) | |
| 1554 | + ff_filter_execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels); | |
| 1534 | 1555 | |
| 1535 | − fin->pts = s->pts + s->consumed; | |
| 1536 | − s->consumed += s->hop_size; | |
| 1537 | − ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, | |
| 1538 | − FFMIN(s->win_size, av_audio_fifo_size(s->fifo))); | |
| 1539 | − if (ret < 0) { | |
| 1556 | + if (s->data == D_UPHASE) | |
| 1557 | + ff_filter_execute(ctx, calc_channel_uphases, NULL, NULL, s->nb_display_channels); | |
| 1558 | + | |
| 1559 | + ret = plot_spectrum_column(inlink, fin); | |
| 1540 | 1560 | av_frame_free(&fin); |
| 1541 | − return ret; | |
| 1561 | + if (ret <= 0) | |
| 1562 | + return ret; | |
| 1542 | 1563 | } |
| 1543 | − | |
| 1544 | − av_assert0(fin->nb_samples == s->win_size); | |
| 1545 | − | |
| 1546 | − ff_filter_execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels); | |
| 1547 | − | |
| 1548 | − if (s->data == D_MAGNITUDE) | |
| 1549 | − ff_filter_execute(ctx, calc_channel_magnitudes, NULL, NULL, s->nb_display_channels); | |
| 1550 | − | |
| 1551 | − if (s->data == D_PHASE) | |
| 1552 | − ff_filter_execute(ctx, calc_channel_phases, NULL, NULL, s->nb_display_channels); | |
| 1553 | − | |
| 1554 | − if (s->data == D_UPHASE) | |
| 1555 | − ff_filter_execute(ctx, calc_channel_uphases, NULL, NULL, s->nb_display_channels); | |
| 1556 | − | |
| 1557 | − ret = plot_spectrum_column(inlink, fin); | |
| 1558 | − | |
| 1559 | − av_frame_free(&fin); | |
| 1560 | − av_audio_fifo_drain(s->fifo, s->hop_size); | |
| 1561 | − if (ret <= 0 && !ff_outlink_get_status(inlink)) | |
| 1562 | − return ret; | |
| 1563 | 1564 | } |
| 1564 | 1565 | |
| 1565 | 1566 | if (ff_outlink_get_status(inlink) == AVERROR_EOF && |
| 1566 | 1567 | s->sliding == FULLFRAME && |
| 1567 | 1568 | s->xpos > 0 && s->outpicref) { |
| 1568 | − int64_t pts; | |
| 1569 | 1569 | |
| 1570 | 1570 | if (s->orientation == VERTICAL) { |
| 1571 | 1571 | for (int i = 0; i < outlink->h; i++) { |
| @@ -1588,17 +1588,19 @@ static int activate(AVFilterContext *ctx) · 16 unmodified lines | ||
| 1588 | 1588 | return 0; |
| 1589 | 1589 | } |
| 1590 | 1590 | |
| 1591 | − FF_FILTER_FORWARD_STATUS(inlink, outlink); | |
| 1592 | − if (av_audio_fifo_size(s->fifo) >= s->win_size || | |
| 1593 | − ff_inlink_queued_frames(inlink) > 0 || | |
| 1594 | − ff_outlink_get_status(inlink) == AVERROR_EOF) { | |
| 1591 | + if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { | |
| 1592 | + if (status == AVERROR_EOF) { | |
| 1593 | + ff_outlink_set_status(outlink, status, s->pts); | |
| 1594 | + return 0; | |
| 1595 | + } | |
| 1596 | + } | |
| 1597 | + | |
| 1598 | + if (ff_inlink_queued_samples(inlink) >= s->hop_size) { | |
| 1595 | 1599 | ff_filter_set_ready(ctx, 10); |
| 1596 | 1600 | return 0; |
| 1597 | 1601 | } |
| 1598 | 1602 | |
| 1599 | − if (ff_outlink_frame_wanted(outlink) && av_audio_fifo_size(s->fifo) < s->win_size && | |
| 1600 | − ff_inlink_queued_frames(inlink) == 0 && | |
| 1601 | − ff_outlink_get_status(inlink) != AVERROR_EOF) { | |
| 1603 | + if (ff_outlink_frame_wanted(outlink)) { | |
| 1602 | 1604 | ff_inlink_request_frame(inlink); |
| 1603 | 1605 | return 0; |
| 1604 | 1606 | } |
| @@ -1691,39 +1693,53 @@ static int showspectrumpic_request_frame(AVFilterLink *outlink) · 86 unmodified lines | ||
| 1691 | 1693 | AVFilterContext *ctx = outlink->src; |
| 1692 | 1694 | ShowSpectrumContext *s = ctx->priv; |
| 1693 | 1695 | AVFilterLink *inlink = ctx->inputs[0]; |
| 1694 | − int ret, samples; | |
| 1696 | + int ret; | |
| 1695 | 1697 | |
| 1696 | 1698 | ret = ff_request_frame(inlink); |
| 1697 | − samples = av_audio_fifo_size(s->fifo); | |
| 1698 | − if (ret == AVERROR_EOF && s->outpicref && samples > 0) { | |
| 1699 | + if (ret == AVERROR_EOF && s->outpicref && s->samples > 0) { | |
| 1699 | 1700 | int consumed = 0; |
| 1700 | 1701 | int x = 0, sz = s->orientation == VERTICAL ? s->w : s->h; |
| 1702 | + unsigned int nb_frame = 0; | |
| 1701 | 1703 | int ch, spf, spb; |
| 1704 | + int src_offset = 0; | |
| 1702 | 1705 | AVFrame *fin; |
| 1703 | 1706 | |
| 1704 | − spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz)))); | |
| 1707 | + spf = s->win_size * (s->samples / ((s->win_size * sz) * ceil(s->samples / (float)(s->win_size * sz)))); | |
| 1705 | 1708 | spf = FFMAX(1, spf); |
| 1706 | 1709 | |
| 1707 | − spb = (samples / (spf * sz)) * spf; | |
| 1710 | + spb = (s->samples / (spf * sz)) * spf; | |
| 1708 | 1711 | |
| 1709 | − fin = ff_get_audio_buffer(inlink, s->win_size); | |
| 1712 | + fin = ff_get_audio_buffer(inlink, spf); | |
| 1710 | 1713 | if (!fin) |
| 1711 | 1714 | return AVERROR(ENOMEM); |
| 1712 | 1715 | |
| 1713 | 1716 | while (x < sz) { |
| 1714 | − ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size); | |
| 1715 | − if (ret < 0) { | |
| 1716 | − av_frame_free(&fin); | |
| 1717 | − return ret; | |
| 1718 | − } | |
| 1719 | − | |
| 1720 | − av_audio_fifo_drain(s->fifo, spf); | |
| 1717 | + int acc_samples = 0; | |
| 1718 | + int dst_offset = 0; | |
| 1719 | + | |
| 1720 | + while (nb_frame <= s->nb_frames) { | |
| 1721 | + AVFrame *cur_frame = s->frames[nb_frame]; | |
| 1722 | + int cur_frame_samples = cur_frame->nb_samples; | |
| 1723 | + int nb_samples = 0; | |
| 1724 | + | |
| 1725 | + if (acc_samples < spf) { | |
| 1726 | + nb_samples = FFMIN(spf - acc_samples, cur_frame_samples - src_offset); | |
| 1727 | + acc_samples += nb_samples; | |
| 1728 | + av_samples_copy(fin->extended_data, cur_frame->extended_data, | |
| 1729 | + dst_offset, src_offset, nb_samples, | |
| 1730 | + cur_frame->channels, AV_SAMPLE_FMT_FLTP); | |
| 1731 | + } | |
| 1721 | 1732 | |
| 1722 | − if (ret < s->win_size) { | |
| 1723 | − for (ch = 0; ch < s->nb_display_channels; ch++) { | |
| 1724 | − memset(fin->extended_data[ch] + ret * sizeof(float), 0, | |
| 1725 | − (s->win_size - ret) * sizeof(float)); | |
| 1733 | + src_offset += nb_samples; | |
| 1734 | + dst_offset += nb_samples; | |
| 1735 | + if (cur_frame_samples <= src_offset) { | |
| 1736 | + av_frame_free(&s->frames[nb_frame]); | |
| 1737 | + nb_frame++; | |
| 1738 | + src_offset = 0; | |
| 1726 | 1739 | } |
| 1740 | + | |
| 1741 | + if (acc_samples == spf) | |
| 1742 | + break; | |
| 1727 | 1743 | } |
| 1728 | 1744 | |
| 1729 | 1745 | ff_filter_execute(ctx, run_channel_fft, fin, NULL, s->nb_display_channels); |
| @@ -1746,7 +1762,7 @@ static int showspectrumpic_request_frame(AVFilterLink *outlink) · 16 unmodified lines | ||
| 1746 | 1762 | s->outpicref->pts = 0; |
| 1747 | 1763 | |
| 1748 | 1764 | if (s->legend) |
| 1749 | − draw_legend(ctx, samples); | |
| 1765 | + draw_legend(ctx, s->samples); | |
| 1750 | 1766 | |
| 1751 | 1767 | ret = ff_filter_frame(outlink, s->outpicref); |
| 1752 | 1768 | s->outpicref = NULL; |
| @@ -1759,11 +1775,20 @@ static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples · 6 unmodified lines | ||
| 1759 | 1775 | { |
| 1760 | 1776 | AVFilterContext *ctx = inlink->dst; |
| 1761 | 1777 | ShowSpectrumContext *s = ctx->priv; |
| 1762 | − int ret; | |
| 1778 | + void *ptr; | |
| 1763 | 1779 | |
| 1764 | − ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples); | |
| 1765 | − av_frame_free(&insamples); | |
| 1766 | − return ret; | |
| 1780 | + if (s->nb_frames + 1ULL > s->frames_size / sizeof(*(s->frames))) { | |
| 1781 | + ptr = av_fast_realloc(s->frames, &s->frames_size, s->frames_size * 2); | |
| 1782 | + if (!ptr) | |
| 1783 | + return AVERROR(ENOMEM); | |
| 1784 | + s->frames = ptr; | |
| 1785 | + } | |
| 1786 | + | |
| 1787 | + s->frames[s->nb_frames] = insamples; | |
| 1788 | + s->samples += insamples->nb_samples; | |
| 1789 | + s->nb_frames++; | |
| 1790 | + | |
| 1791 | + return 0; | |
| 1767 | 1792 | } |
| 1768 | 1793 | |
| 1769 | 1794 | static const AVFilterPad showspectrumpic_inputs[] = { |