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);
1051 return yae_reset(atempo, format, sample_rate, channels);
1067 outlink->time_base);
1070 atempo->dst_buffer =
NULL;
1072 atempo->dst_end =
NULL;
1076 atempo->nsamples_out += n_out;
1088 int n_out = (int)(0.5 + ((
double)n_in) / atempo->
tempo);
1093 while (src < src_end) {
1131 int n_max = atempo->
ring;
1135 while (err ==
AVERROR(EAGAIN)) {
1202 .priv_class = &atempo_class,
#define RE_MALLOC_OR_FAIL(field, field_size)
static int push_samples(ATempoContext *atempo, AVFilterLink *outlink, int n_out)
static void yae_xcorr_via_rdft(FFTSample *xcorr, RDFTContext *complex_to_real, const FFTComplex *xa, const FFTComplex *xb, const int window)
Calculate cross-correlation via rDFT.
This structure describes decoded (raw) audio or video data.
RDFTContext * complex_to_real
FilterState
Filter state machine states.
RDFTContext * real_to_complex
static int yae_set_tempo(AVFilterContext *ctx, const char *arg_tempo)
static int config_props(AVFilterLink *inlink)
static const AVFilterPad outputs[]
Main libavfilter public API header.
enum AVSampleFormat format
#define AV_OPT_FLAG_AUDIO_PARAM
static void yae_apply(ATempoContext *atempo, const uint8_t **src_ref, const uint8_t *src_end, uint8_t **dst_ref, uint8_t *dst_end)
Feed as much data to the filter as it is able to consume and receive as much processed data in the de...
static const AVFilterPad atempo_outputs[]
static enum AVSampleFormat formats[]
static int request_frame(AVFilterLink *outlink)
AVFILTER_DEFINE_CLASS(atempo)
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
static void yae_advance_to_next_frag(ATempoContext *atempo)
Prepare for loading next audio fragment.
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
static AudioFragment * yae_prev_frag(ATempoContext *atempo)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static av_cold int end(AVCodecContext *avctx)
static AudioFragment * yae_curr_frag(ATempoContext *atempo)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
#define AVERROR_EOF
End of file.
static av_cold int init(AVFilterContext *ctx)
A filter pad used for either input or output.
static void yae_clear(ATempoContext *atempo)
Reset filter to initial state, do not deallocate existing local buffers.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
A link between two filters.
static int yae_load_data(ATempoContext *atempo, const uint8_t **src_ref, const uint8_t *src_end, int64_t stop_here)
Populate the internal data buffer on as-needed basis.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
unsigned flags
Link processing flags.
int sample_rate
samples per second
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
#define AV_OPT_FLAG_FILTERING_PARAM
a generic parameter which can be set by the user for filtering
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
A fragment of audio waveform.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
simple assert() macros that are a bit more flexible than ISO C assert().
static const uint8_t offset[127][2]
static int yae_flush(ATempoContext *atempo, uint8_t **dst_ref, uint8_t *dst_end)
Flush any buffered data from the filter.
void av_rdft_calc(RDFTContext *s, FFTSample *data)
static int yae_reset(ATempoContext *atempo, enum AVSampleFormat format, int sample_rate, int channels)
Prepare filter for processing audio data of given format, sample rate and number of channels...
static const AVFilterPad atempo_inputs[]
audio channel layout utility functions
static int yae_align(AudioFragment *frag, const AudioFragment *prev, const int window, const int delta_max, const int drift, FFTSample *correlation, RDFTContext *complex_to_real)
Calculate alignment offset for given fragment relative to the previous fragment.
static int yae_overlap_add(ATempoContext *atempo, uint8_t **dst_ref, uint8_t *dst_end)
Blend the overlap region of previous and current audio fragment and output the results to the given d...
void av_rdft_end(RDFTContext *s)
static int yae_adjust_position(ATempoContext *atempo)
Adjust current fragment position for better alignment with previous fragment.
AVFilterContext * src
source filter
RDFTContext * av_rdft_init(int nbits, enum RDFTransformType trans)
Set up a real FFT.
static int process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
#define yae_blend(scalar_type)
A helper macro for blending the overlap region of previous and current audio fragment.
int format
agreed upon media format
A list of supported channel layouts.
static int query_formats(AVFilterContext *ctx)
static void yae_release_buffers(ATempoContext *atempo)
Reset filter to initial state and deallocate all buffers.
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
AVSampleFormat
Audio sample formats.
static int filter_frame(AVFilterLink *inlink, AVFrame *src_buffer)
Describe the class of an AVClass context structure.
int sample_rate
Sample rate of the audio data.
static const AVFilterPad inputs[]
rational number numerator/denominator
const char * name
Filter name.
AVFilterLink ** outputs
array of pointers to output links
enum MovChannelLayoutTag * layouts
#define yae_init_xdat(scalar_type, scalar_max)
A helper macro for initializing complex data buffer with scalar data of a given type.
Frame requests may need to loop in order to be fulfilled.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
uint64_t channel_layout
channel layout of current buffer (see libavutil/channel_layout.h)
static av_cold void uninit(AVFilterContext *ctx)
AVFilterContext * dst
dest filter
static int yae_load_frag(ATempoContext *atempo, const uint8_t **src_ref, const uint8_t *src_end)
Populate current audio fragment data buffer.
static void yae_downmix(ATempoContext *atempo, AudioFragment *frag)
Initialize complex data buffer of a given audio fragment with down-mixed mono data of appropriate sca...
static enum AVSampleFormat sample_fmts[]
static const AVOption atempo_options[]
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
int nb_samples
number of audio samples (per channel) described by this frame
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
simple arithmetic expression evaluator