152 #define OFFSET(x) offsetof(ATempoContext, x)
155 {
"tempo",
"set tempo scale factor",
170 return &atempo->
frag[(atempo->
nfrag + 1) % 2];
238 #define RE_MALLOC_OR_FAIL(field, field_size) \
241 field = av_malloc(field_size); \
243 yae_release_buffers(atempo); \
244 return AVERROR(ENOMEM); \
258 uint32_t nlevels = 0;
264 atempo->
stride = sample_size * channels;
267 atempo->
window = sample_rate / 24;
274 if (pot < atempo->window) {
312 for (i = 0; i < atempo->
window; i++) {
313 double t = (double)i / (
double)(atempo->
window - 1);
314 double h = 0.5 * (1.0 - cos(2.0 *
M_PI * t));
315 atempo->
hann[i] = (float)h;
327 double tempo =
av_strtod(arg_tempo, &tail);
334 if (tempo < 0.5 || tempo > 2.0) {
343 atempo->
tempo = tempo;
351 #define yae_init_xdat(scalar_type, scalar_max) \
353 const uint8_t *src_end = src + \
354 frag->nsamples * atempo->channels * sizeof(scalar_type); \
356 FFTSample *xdat = frag->xdat; \
359 if (atempo->channels == 1) { \
360 for (; src < src_end; xdat++) { \
361 tmp = *(const scalar_type *)src; \
362 src += sizeof(scalar_type); \
364 *xdat = (FFTSample)tmp; \
367 FFTSample s, max, ti, si; \
370 for (; src < src_end; xdat++) { \
371 tmp = *(const scalar_type *)src; \
372 src += sizeof(scalar_type); \
374 max = (FFTSample)tmp; \
375 s = FFMIN((FFTSample)scalar_max, \
376 (FFTSample)fabsf(max)); \
378 for (i = 1; i < atempo->channels; i++) { \
379 tmp = *(const scalar_type *)src; \
380 src += sizeof(scalar_type); \
382 ti = (FFTSample)tmp; \
383 si = FFMIN((FFTSample)scalar_max, \
384 (FFTSample)fabsf(ti)); \
436 const int read_size = stop_here - atempo->
position[0];
438 if (stop_here <= atempo->position[0]) {
445 while (atempo->
position[0] < stop_here && src < src_end) {
446 int src_samples = (src_end -
src) / atempo->
stride;
449 int nsamples =
FFMIN(read_size, src_samples);
453 nsamples =
FFMIN(nsamples, atempo->
ring);
459 memcpy(a, src, na * atempo->
stride);
461 src += na * atempo->
stride;
474 memcpy(b, src, nb * atempo->
stride);
476 src += nb * atempo->
stride;
511 int64_t missing,
start, zeros;
514 int i0, i1, n0, n1, na, nb;
517 if (src_ref &&
yae_load_data(atempo, src_ref, src_end, stop_here) != 0) {
524 stop_here - atempo->
position[0] : 0;
527 missing < (int64_t)atempo->
window ?
528 (uint32_t)(atempo->
window - missing) : 0;
542 memset(dst, 0, zeros * atempo->
stride);
543 dst += zeros * atempo->
stride;
546 if (zeros == nsamples) {
564 i1 = i0 < na ? 0 : i0 - na;
566 n0 = i0 < na ?
FFMIN(na - i0, (
int)(nsamples - zeros)) : 0;
567 n1 = nsamples - zeros - n0;
570 memcpy(dst, a + i0 * atempo->
stride, n0 * atempo->
stride);
571 dst += n0 * atempo->
stride;
575 memcpy(dst, b + i1 * atempo->
stride, n1 * atempo->
stride);
586 const double fragment_step = atempo->
tempo * (double)(atempo->
window / 2);
625 for (i = 1; i < window; i++, xa++, xb++, xc++) {
626 xc->
re = (xa->re * xb->re + xa->im * xb->im);
627 xc->
im = (xa->im * xb->re - xa->re * xb->im);
648 int best_offset = -drift;
663 i0 =
FFMAX(window / 2 - delta_max - drift, 0);
664 i0 =
FFMIN(i0, window);
666 i1 =
FFMIN(window / 2 + delta_max - drift, window - window / 16);
670 xcorr = correlation + i0;
672 for (i = i0; i < i1; i++, xcorr++) {
679 if (metric > best_metric) {
680 best_metric = metric;
681 best_offset = i - window / 2;
699 const double prev_output_position =
702 const double ideal_output_position =
706 const int drift = (int)(prev_output_position - ideal_output_position);
708 const int delta_max = atempo->
window / 2;
732 #define yae_blend(scalar_type) \
734 const scalar_type *aaa = (const scalar_type *)a; \
735 const scalar_type *bbb = (const scalar_type *)b; \
737 scalar_type *out = (scalar_type *)dst; \
738 scalar_type *out_end = (scalar_type *)dst_end; \
741 for (i = 0; i < overlap && out < out_end; \
742 i++, atempo->position[1]++, wa++, wb++) { \
747 for (j = 0; j < atempo->channels; \
748 j++, aaa++, bbb++, out++) { \
749 float t0 = (float)*aaa; \
750 float t1 = (float)*bbb; \
753 frag->position[0] + i < 0 ? \
755 (scalar_type)(t0 * w0 + t1 * w1); \
758 dst = (uint8_t *)out; \
783 const int64_t overlap = stop_here - start_here;
785 const int64_t ia = start_here - prev->
position[1];
786 const int64_t ib = start_here - frag->
position[1];
788 const float *wa = atempo->
hann + ia;
789 const float *wb = atempo->
hann + ib;
798 overlap <= frag->nsamples);
844 if (!atempo->
nfrag) {
946 while (atempo->
position[1] < overlap_end) {
961 offset = start_here - frag->
position[1];
962 av_assert0(start_here <= stop_here && frag->position[1] <= start_here);
967 src_size = (int)(stop_here - start_here) * atempo->
stride;
968 dst_size = dst_end - dst;
969 nbytes =
FFMIN(src_size, dst_size);
971 memcpy(dst, src, nbytes);
1048 return yae_reset(atempo, format, sample_rate, channels);
1064 outlink->time_base);
1067 atempo->dst_buffer =
NULL;
1069 atempo->dst_end =
NULL;
1073 atempo->nsamples_out += n_out;
1085 int n_out = (int)(0.5 + ((
double)n_in) / atempo->
tempo);
1090 while (src < src_end) {
1128 int n_max = atempo->
ring;
1132 while (err ==
AVERROR(EAGAIN)) {
1199 .priv_class = &atempo_class,