X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Favf_showcqt.c;h=023924fd4c5dbb802c31b7d1dbd27514855ba5bc;hb=954f865c8ee291d7c4c83a03815711321e931ada;hp=712a9997fe670ece882485b28911b6083159e770;hpb=9938697c1c119a675759029ead74b91c529cdf2e;p=ffmpeg diff --git a/libavfilter/avf_showcqt.c b/libavfilter/avf_showcqt.c index 712a9997fe6..023924fd4c5 100644 --- a/libavfilter/avf_showcqt.c +++ b/libavfilter/avf_showcqt.c @@ -18,9 +18,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include -#include - #include "config.h" #include "libavcodec/avfft.h" #include "libavutil/avassert.h" @@ -48,6 +45,8 @@ #define FONTCOLOR "st(0, (midi(f)-59.5)/12);" \ "st(1, if(between(ld(0),0,1), 0.5-0.5*cos(2*PI*ld(0)), 0));" \ "r(1-ld(1)) + b(ld(1))" +#define PTS_STEP 10 +#define PTS_TOLERANCE 1 #define OFFSET(x) offsetof(ShowCQTContext, x) #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM) @@ -442,19 +441,19 @@ static double midi(void *p, double f) static double r_func(void *p, double x) { x = av_clipd(x, 0.0, 1.0); - return (int)(x*255.0+0.5) << 16; + return lrint(x*255.0) << 16; } static double g_func(void *p, double x) { x = av_clipd(x, 0.0, 1.0); - return (int)(x*255.0+0.5) << 8; + return lrint(x*255.0) << 8; } static double b_func(void *p, double x) { x = av_clipd(x, 0.0, 1.0); - return (int)(x*255.0+0.5); + return lrint(x*255.0); } static int init_axis_color(ShowCQTContext *s, AVFrame *tmp) @@ -703,9 +702,9 @@ static void draw_bar_rgb(AVFrame *out, const float *h, const float *rcp_h, *lp++ = 0; } else { mul = (h[x] - ht) * rcp_h[x]; - *lp++ = mul * c[x].rgb.r + 0.5f; - *lp++ = mul * c[x].rgb.g + 0.5f; - *lp++ = mul * c[x].rgb.b + 0.5f; + *lp++ = lrintf(mul * c[x].rgb.r); + *lp++ = lrintf(mul * c[x].rgb.g); + *lp++ = lrintf(mul * c[x].rgb.b); } } } @@ -734,9 +733,9 @@ static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h, *lpv++ = 128; } else { mul = (h[x] - ht) * rcp_h[x]; - *lpy++ = mul * c[x].yuv.y + 16.5f; - *lpu++ = mul * c[x].yuv.u + 128.5f; - *lpv++ = mul * c[x].yuv.v + 128.5f; + *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f); + *lpu++ = lrintf(mul * c[x].yuv.u + 128.0f); + *lpv++ = lrintf(mul * c[x].yuv.v + 128.0f); } /* u and v are skipped on yuv422p and yuv420p */ if (fmt == AV_PIX_FMT_YUV444P) { @@ -746,16 +745,16 @@ static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h, *lpv++ = 128; } else { mul = (h[x+1] - ht) * rcp_h[x+1]; - *lpy++ = mul * c[x+1].yuv.y + 16.5f; - *lpu++ = mul * c[x+1].yuv.u + 128.5f; - *lpv++ = mul * c[x+1].yuv.v + 128.5f; + *lpy++ = lrintf(mul * c[x+1].yuv.y + 16.0f); + *lpu++ = lrintf(mul * c[x+1].yuv.u + 128.0f); + *lpv++ = lrintf(mul * c[x+1].yuv.v + 128.0f); } } else { if (h[x+1] <= ht) { *lpy++ = 16; } else { mul = (h[x+1] - ht) * rcp_h[x+1]; - *lpy++ = mul * c[x+1].yuv.y + 16.5f; + *lpy++ = lrintf(mul * c[x+1].yuv.y + 16.0f); } } } @@ -773,16 +772,16 @@ static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h, *lpv++ = 128; } else { mul = (h[x] - ht) * rcp_h[x]; - *lpy++ = mul * c[x].yuv.y + 16.5f; - *lpu++ = mul * c[x].yuv.u + 128.5f; - *lpv++ = mul * c[x].yuv.v + 128.5f; + *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f); + *lpu++ = lrintf(mul * c[x].yuv.u + 128.0f); + *lpv++ = lrintf(mul * c[x].yuv.v + 128.0f); } } else { if (h[x] <= ht) { *lpy++ = 16; } else { mul = (h[x] - ht) * rcp_h[x]; - *lpy++ = mul * c[x].yuv.y + 16.5f; + *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f); } } /* u and v are skipped on yuv422p and yuv420p */ @@ -793,16 +792,16 @@ static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h, *lpv++ = 128; } else { mul = (h[x+1] - ht) * rcp_h[x+1]; - *lpy++ = mul * c[x+1].yuv.y + 16.5f; - *lpu++ = mul * c[x+1].yuv.u + 128.5f; - *lpv++ = mul * c[x+1].yuv.v + 128.5f; + *lpy++ = lrintf(mul * c[x+1].yuv.y + 16.0f); + *lpu++ = lrintf(mul * c[x+1].yuv.u + 128.0f); + *lpv++ = lrintf(mul * c[x+1].yuv.v + 128.0f); } } else { if (h[x+1] <= ht) { *lpy++ = 16; } else { mul = (h[x+1] - ht) * rcp_h[x+1]; - *lpy++ = mul * c[x+1].yuv.y + 16.5f; + *lpy++ = lrintf(mul * c[x+1].yuv.y + 16.0f); } } } @@ -820,9 +819,9 @@ static void draw_axis_rgb(AVFrame *out, AVFrame *axis, const ColorFloat *c, int lpa = axis->data[0] + y * axis->linesize[0]; for (x = 0; x < w; x++) { a = rcp_255 * lpa[3]; - *lp++ = a * lpa[0] + (1.0f - a) * c[x].rgb.r + 0.5f; - *lp++ = a * lpa[1] + (1.0f - a) * c[x].rgb.g + 0.5f; - *lp++ = a * lpa[2] + (1.0f - a) * c[x].rgb.b + 0.5f; + *lp++ = lrintf(a * lpa[0] + (1.0f - a) * c[x].rgb.r); + *lp++ = lrintf(a * lpa[1] + (1.0f - a) * c[x].rgb.g); + *lp++ = lrintf(a * lpa[2] + (1.0f - a) * c[x].rgb.b); lpa += 4; } } @@ -850,15 +849,15 @@ static void draw_axis_yuv(AVFrame *out, AVFrame *axis, const ColorFloat *c, int lpaa = vaa + y * lsaa; for (x = 0; x < w; x += 2) { a = rcp_255 * (*lpaa++); - *lpy++ = a * (*lpay++) + (1.0f - a) * (c[x].yuv.y + 16.0f) + 0.5f; - *lpu++ = a * (*lpau++) + (1.0f - a) * (c[x].yuv.u + 128.0f) + 0.5f; - *lpv++ = a * (*lpav++) + (1.0f - a) * (c[x].yuv.v + 128.0f) + 0.5f; + *lpy++ = lrintf(a * (*lpay++) + (1.0f - a) * (c[x].yuv.y + 16.0f)); + *lpu++ = lrintf(a * (*lpau++) + (1.0f - a) * (c[x].yuv.u + 128.0f)); + *lpv++ = lrintf(a * (*lpav++) + (1.0f - a) * (c[x].yuv.v + 128.0f)); /* u and v are skipped on yuv422p and yuv420p */ a = rcp_255 * (*lpaa++); - *lpy++ = a * (*lpay++) + (1.0f - a) * (c[x+1].yuv.y + 16.0f) + 0.5f; + *lpy++ = lrintf(a * (*lpay++) + (1.0f - a) * (c[x+1].yuv.y + 16.0f)); if (fmt == AV_PIX_FMT_YUV444P) { - *lpu++ = a * (*lpau++) + (1.0f - a) * (c[x+1].yuv.u + 128.0f) + 0.5f; - *lpv++ = a * (*lpav++) + (1.0f - a) * (c[x+1].yuv.v + 128.0f) + 0.5f; + *lpu++ = lrintf(a * (*lpau++) + (1.0f - a) * (c[x+1].yuv.u + 128.0f)); + *lpv++ = lrintf(a * (*lpav++) + (1.0f - a) * (c[x+1].yuv.v + 128.0f)); } } @@ -872,17 +871,17 @@ static void draw_axis_yuv(AVFrame *out, AVFrame *axis, const ColorFloat *c, int for (x = 0; x < out->width; x += 2) { /* u and v are skipped on yuv420p */ a = rcp_255 * (*lpaa++); - *lpy++ = a * (*lpay++) + (1.0f - a) * (c[x].yuv.y + 16.0f) + 0.5f; + *lpy++ = lrintf(a * (*lpay++) + (1.0f - a) * (c[x].yuv.y + 16.0f)); if (fmt != AV_PIX_FMT_YUV420P) { - *lpu++ = a * (*lpau++) + (1.0f - a) * (c[x].yuv.u + 128.0f) + 0.5f; - *lpv++ = a * (*lpav++) + (1.0f - a) * (c[x].yuv.v + 128.0f) + 0.5f; + *lpu++ = lrintf(a * (*lpau++) + (1.0f - a) * (c[x].yuv.u + 128.0f)); + *lpv++ = lrintf(a * (*lpav++) + (1.0f - a) * (c[x].yuv.v + 128.0f)); } /* u and v are skipped on yuv422p and yuv420p */ a = rcp_255 * (*lpaa++); - *lpy++ = a * (*lpay++) + (1.0f - a) * (c[x+1].yuv.y + 16.0f) + 0.5f; + *lpy++ = lrintf(a * (*lpay++) + (1.0f - a) * (c[x+1].yuv.y + 16.0f)); if (fmt == AV_PIX_FMT_YUV444P) { - *lpu++ = a * (*lpau++) + (1.0f - a) * (c[x+1].yuv.u + 128.0f) + 0.5f; - *lpv++ = a * (*lpav++) + (1.0f - a) * (c[x+1].yuv.v + 128.0f) + 0.5f; + *lpu++ = lrintf(a * (*lpau++) + (1.0f - a) * (c[x+1].yuv.u + 128.0f)); + *lpv++ = lrintf(a * (*lpav++) + (1.0f - a) * (c[x+1].yuv.v + 128.0f)); } } } @@ -918,9 +917,9 @@ static void update_sono_rgb(AVFrame *sono, const ColorFloat *c, int idx) uint8_t *lp = sono->data[0] + idx * sono->linesize[0]; for (x = 0; x < w; x++) { - *lp++ = c[x].rgb.r + 0.5f; - *lp++ = c[x].rgb.g + 0.5f; - *lp++ = c[x].rgb.b + 0.5f; + *lp++ = lrintf(c[x].rgb.r); + *lp++ = lrintf(c[x].rgb.g); + *lp++ = lrintf(c[x].rgb.b); } } @@ -932,13 +931,13 @@ static void update_sono_yuv(AVFrame *sono, const ColorFloat *c, int idx) uint8_t *lpv = sono->data[2] + idx * sono->linesize[2]; for (x = 0; x < w; x += 2) { - *lpy++ = c[x].yuv.y + 16.5f; - *lpu++ = c[x].yuv.u + 128.5f; - *lpv++ = c[x].yuv.v + 128.5f; - *lpy++ = c[x+1].yuv.y + 16.5f; + *lpy++ = lrintf(c[x].yuv.y + 16.0f); + *lpu++ = lrintf(c[x].yuv.u + 128.0f); + *lpv++ = lrintf(c[x].yuv.v + 128.0f); + *lpy++ = lrintf(c[x+1].yuv.y + 16.0f); if (fmt == AV_PIX_FMT_YUV444P) { - *lpu++ = c[x+1].yuv.u + 128.5f; - *lpv++ = c[x+1].yuv.v + 128.5f; + *lpu++ = lrintf(c[x+1].yuv.u + 128.0f); + *lpv++ = lrintf(c[x+1].yuv.v + 128.0f); } } } @@ -989,11 +988,10 @@ static void process_cqt(ShowCQTContext *s) yuv_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width); } -static int plot_cqt(AVFilterContext *ctx) +static int plot_cqt(AVFilterContext *ctx, AVFrame **frameout) { AVFilterLink *outlink = ctx->outputs[0]; ShowCQTContext *s = ctx->priv; - int ret = 0; memcpy(s->fft_result, s->fft_data, s->fft_len * sizeof(*s->fft_data)); av_fft_permute(s->fft_ctx, s->fft_result); @@ -1004,7 +1002,7 @@ static int plot_cqt(AVFilterContext *ctx) if (s->sono_h) s->update_sono(s->sono_frame, s->c_buf, s->sono_idx); if (!s->sono_count) { - AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + AVFrame *out = *frameout = ff_get_video_buffer(outlink, outlink->w, outlink->h); if (!out) return AVERROR(ENOMEM); if (s->bar_h) @@ -1013,14 +1011,13 @@ static int plot_cqt(AVFilterContext *ctx) s->draw_axis(out, s->axis_frame, s->c_buf, s->bar_h); if (s->sono_h) s->draw_sono(out, s->sono_frame, s->bar_h + s->axis_h, s->sono_idx); - out->pts = s->frame_count; - ret = ff_filter_frame(outlink, out); - s->frame_count++; + out->pts = s->next_pts; + s->next_pts += PTS_STEP; } s->sono_count = (s->sono_count + 1) % s->count; if (s->sono_h) s->sono_idx = (s->sono_idx + s->sono_h - 1) % s->sono_h; - return ret; + return 0; } /* main filter control */ @@ -1133,7 +1130,7 @@ static int config_output(AVFilterLink *outlink) s->format = outlink->format; outlink->sample_aspect_ratio = av_make_q(1, 1); outlink->frame_rate = s->rate; - outlink->time_base = av_inv_q(s->rate); + outlink->time_base = av_mul_q(av_inv_q(s->rate), av_make_q(1, PTS_STEP)); av_log(ctx, AV_LOG_INFO, "video: %dx%d %s %d/%d fps, bar_h = %d, axis_h = %d, sono_h = %d.\n", s->width, s->height, av_get_pix_fmt_name(s->format), s->rate.num, s->rate.den, s->bar_h, s->axis_h, s->sono_h); @@ -1209,7 +1206,7 @@ static int config_output(AVFilterLink *outlink) return AVERROR(ENOMEM); s->sono_count = 0; - s->frame_count = 0; + s->next_pts = 0; s->sono_idx = 0; s->remaining_fill = s->fft_len / 2; s->remaining_frac = 0; @@ -1232,14 +1229,16 @@ static int config_output(AVFilterLink *outlink) static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) { AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; ShowCQTContext *s = ctx->priv; int remaining, step, ret, x, i, j, m; float *audio_data; + AVFrame *out = NULL; if (!insamples) { while (s->remaining_fill < s->fft_len / 2) { memset(&s->fft_data[s->fft_len - s->remaining_fill], 0, sizeof(*s->fft_data) * s->remaining_fill); - ret = plot_cqt(ctx); + ret = plot_cqt(ctx, &out); if (ret < 0) return ret; @@ -1248,6 +1247,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) for (x = 0; x < (s->fft_len-step); x++) s->fft_data[x] = s->fft_data[x+step]; s->remaining_fill += step; + + if (out) + return ff_filter_frame(outlink, out); } return AVERROR_EOF; } @@ -1263,12 +1265,30 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) s->fft_data[j+m].re = audio_data[2*(i+m)]; s->fft_data[j+m].im = audio_data[2*(i+m)+1]; } - ret = plot_cqt(ctx); + ret = plot_cqt(ctx, &out); if (ret < 0) { av_frame_free(&insamples); return ret; } remaining -= s->remaining_fill; + if (out) { + int64_t pts = av_rescale_q(insamples->pts, inlink->time_base, av_make_q(1, inlink->sample_rate)); + pts += insamples->nb_samples - remaining - s->fft_len/2; + pts = av_rescale_q(pts, av_make_q(1, inlink->sample_rate), outlink->time_base); + if (FFABS(pts - out->pts) > PTS_TOLERANCE) { + av_log(ctx, AV_LOG_DEBUG, "changing pts from %"PRId64" (%.3f) to %"PRId64" (%.3f).\n", + out->pts, out->pts * av_q2d(outlink->time_base), + pts, pts * av_q2d(outlink->time_base)); + out->pts = pts; + s->next_pts = pts + PTS_STEP; + } + ret = ff_filter_frame(outlink, out); + if (ret < 0) { + av_frame_free(&insamples); + return ret; + } + out = NULL; + } step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den; s->remaining_frac = (s->step_frac.num + s->remaining_frac) % s->step_frac.den; for (m = 0; m < s->fft_len-step; m++) @@ -1294,7 +1314,7 @@ static int request_frame(AVFilterLink *outlink) ret = ff_request_frame(inlink); if (ret == AVERROR_EOF) - filter_frame(inlink, NULL); + ret = filter_frame(inlink, NULL); return ret; }