214 #define RE_MALLOC_OR_FAIL(field, field_size) \
217 field = av_malloc(field_size); \
219 yae_release_buffers(atempo); \
220 return AVERROR(ENOMEM); \
234 uint32_t nlevels = 0;
240 atempo->
stride = sample_size * channels;
243 atempo->
window = sample_rate / 24;
250 if (pot < atempo->window) {
288 for (i = 0; i < atempo->
window; i++) {
289 double t = (double)i / (
double)(atempo->
window - 1);
290 double h = 0.5 * (1.0 - cos(2.0 *
M_PI * t));
291 atempo->
hann[i] = (float)h;
302 double tempo =
av_strtod(arg_tempo, &tail);
309 if (tempo < 0.5 || tempo > 2.0) {
315 atempo->
tempo = tempo;
326 return &atempo->
frag[(atempo->
nfrag + 1) % 2];
333 #define yae_init_xdat(scalar_type, scalar_max) \
335 const uint8_t *src_end = src + \
336 frag->nsamples * atempo->channels * sizeof(scalar_type); \
338 FFTSample *xdat = frag->xdat; \
341 if (atempo->channels == 1) { \
342 for (; src < src_end; xdat++) { \
343 tmp = *(const scalar_type *)src; \
344 src += sizeof(scalar_type); \
346 *xdat = (FFTSample)tmp; \
349 FFTSample s, max, ti, si; \
352 for (; src < src_end; xdat++) { \
353 tmp = *(const scalar_type *)src; \
354 src += sizeof(scalar_type); \
356 max = (FFTSample)tmp; \
357 s = FFMIN((FFTSample)scalar_max, \
358 (FFTSample)fabsf(max)); \
360 for (i = 1; i < atempo->channels; i++) { \
361 tmp = *(const scalar_type *)src; \
362 src += sizeof(scalar_type); \
364 ti = (FFTSample)tmp; \
365 si = FFMIN((FFTSample)scalar_max, \
366 (FFTSample)fabsf(ti)); \
418 const int read_size = stop_here - atempo->
position[0];
420 if (stop_here <= atempo->position[0]) {
427 while (atempo->
position[0] < stop_here && src < src_end) {
428 int src_samples = (src_end - src) / atempo->
stride;
431 int nsamples =
FFMIN(read_size, src_samples);
435 nsamples =
FFMIN(nsamples, atempo->
ring);
441 memcpy(a, src, na * atempo->
stride);
443 src += na * atempo->
stride;
456 memcpy(b, src, nb * atempo->
stride);
458 src += nb * atempo->
stride;
493 int64_t missing, start, zeros;
496 int i0, i1, n0, n1, na, nb;
499 if (src_ref &&
yae_load_data(atempo, src_ref, src_end, stop_here) != 0) {
506 stop_here - atempo->
position[0] : 0;
509 missing < (int64_t)atempo->
window ?
510 (uint32_t)(atempo->
window - missing) : 0;
524 memset(dst, 0, zeros * atempo->
stride);
525 dst += zeros * atempo->
stride;
528 if (zeros == nsamples) {
545 i0 = frag->
position[0] + zeros - start;
546 i1 = i0 < na ? 0 : i0 - na;
548 n0 = i0 < na ?
FFMIN(na - i0, (
int)(nsamples - zeros)) : 0;
549 n1 = nsamples - zeros - n0;
552 memcpy(dst, a + i0 * atempo->
stride, n0 * atempo->
stride);
553 dst += n0 * atempo->
stride;
557 memcpy(dst, b + i1 * atempo->
stride, n1 * atempo->
stride);
568 const double fragment_step = atempo->
tempo * (double)(atempo->
window / 2);
607 for (i = 1; i < window; i++, xa++, xb++, xc++) {
608 xc->
re = (xa->re * xb->re + xa->im * xb->im);
609 xc->
im = (xa->im * xb->re - xa->re * xb->im);
630 int best_offset = -drift;
645 i0 =
FFMAX(window / 2 - delta_max - drift, 0);
646 i0 =
FFMIN(i0, window);
648 i1 =
FFMIN(window / 2 + delta_max - drift, window - window / 16);
652 xcorr = correlation + i0;
654 for (i = i0; i < i1; i++, xcorr++) {
661 if (metric > best_metric) {
662 best_metric = metric;
663 best_offset = i - window / 2;
681 const int delta_max = atempo->
window / 2;
698 atempo->
drift += correction;
708 #define yae_blend(scalar_type) \
710 const scalar_type *aaa = (const scalar_type *)a; \
711 const scalar_type *bbb = (const scalar_type *)b; \
713 scalar_type *out = (scalar_type *)dst; \
714 scalar_type *out_end = (scalar_type *)dst_end; \
717 for (i = 0; i < overlap && out < out_end; \
718 i++, atempo->position[1]++, wa++, wb++) { \
723 for (j = 0; j < atempo->channels; \
724 j++, aaa++, bbb++, out++) { \
725 float t0 = (float)*aaa; \
726 float t1 = (float)*bbb; \
729 frag->position[0] + i < 0 ? \
731 (scalar_type)(t0 * w0 + t1 * w1); \
734 dst = (uint8_t *)out; \
759 const int64_t overlap = stop_here - start_here;
761 const int64_t ia = start_here - prev->
position[1];
762 const int64_t ib = start_here - frag->
position[1];
764 const float *wa = atempo->
hann + ia;
765 const float *wb = atempo->
hann + ib;
774 overlap <= frag->nsamples);
820 if (!atempo->
nfrag) {
922 while (atempo->
position[1] < overlap_end) {
931 offset = start_here - frag->
position[1];
932 av_assert0(start_here <= stop_here && frag->position[1] <= start_here);
937 src_size = (int)(stop_here - start_here) * atempo->
stride;
938 dst_size = dst_end -
dst;
939 nbytes =
FFMIN(src_size, dst_size);
941 memcpy(dst, src, nbytes);
1020 return yae_reset(atempo, format, sample_rate, channels);
1034 outlink->time_base);
1037 atempo->dst_buffer =
NULL;
1039 atempo->dst_end =
NULL;
1041 atempo->nsamples_out += n_out;
1052 int n_out = (int)(0.5 + ((
double)n_in) / atempo->
tempo);
1057 while (src < src_end) {
1095 int n_max = atempo->
ring;
1099 while (err ==
AVERROR(EAGAIN)) {