コード例 #1
0
ファイル: avfft.c プロジェクト: 0day-ci/FFmpeg
int main(int argc, char **argv)
{
    int i;
#define LEN 1024
    FFTSample *ref  = av_malloc_array(LEN, sizeof(*ref));
    FFTSample *data = av_malloc_array(LEN, sizeof(*data));
    RDFTContext *rdft_context  = av_rdft_init(10, DFT_R2C);
    RDFTContext *irdft_context = av_rdft_init(10, IDFT_C2R);

    if (!ref || !data || !rdft_context || !irdft_context)
        return 2;
    for (i=0; i<LEN; i++) {
        ref[i] = data[i] = i*456 + 123 + i*i;
    }
    av_rdft_calc(rdft_context, data);
    av_rdft_calc(irdft_context, data);

    for (i=0; i<LEN; i++) {
        if (fabs(ref[i] - data[i]/LEN*2) > 1) {
            fprintf(stderr, "Failed at %d (%f %f)\n", i, ref[i], data[i]/LEN*2);
            return 1;
        }
    }

    av_rdft_end(rdft_context);
    av_rdft_end(irdft_context);
    av_free(data);
    av_free(ref);

    return 0;
}
コード例 #2
0
ファイル: af_afir.c プロジェクト: DeHackEd/FFmpeg
static void uninit_segment(AVFilterContext *ctx, AudioFIRSegment *seg)
{
    AudioFIRContext *s = ctx->priv;

    if (seg->rdft) {
        for (int ch = 0; ch < s->nb_channels; ch++) {
            av_rdft_end(seg->rdft[ch]);
        }
    }
    av_freep(&seg->rdft);

    if (seg->irdft) {
        for (int ch = 0; ch < s->nb_channels; ch++) {
            av_rdft_end(seg->irdft[ch]);
        }
    }
    av_freep(&seg->irdft);

    av_freep(&seg->output_offset);
    av_freep(&seg->part_index);

    av_frame_free(&seg->block);
    av_frame_free(&seg->sum);
    av_frame_free(&seg->buffer);
    av_frame_free(&seg->coeff);
    av_frame_free(&seg->input);
    av_frame_free(&seg->output);
    seg->input_size = 0;
}
コード例 #3
0
ファイル: fft.c プロジェクト: 411697643/FFmpeg
static inline void rdft_end(RDFTContext *r)
{
#if AVFFT
    av_rdft_end(r);
#else
    ff_rdft_end(r);
#endif
}
コード例 #4
0
ファイル: af_atempo.c プロジェクト: chinshou/ffmpeg-vda
/**
 * Reset filter to initial state and deallocate all buffers.
 */
static void yae_release_buffers(ATempoContext *atempo)
{
    yae_clear(atempo);

    av_freep(&atempo->frag[0].data);
    av_freep(&atempo->frag[1].data);
    av_freep(&atempo->frag[0].xdat);
    av_freep(&atempo->frag[1].xdat);

    av_freep(&atempo->buffer);
    av_freep(&atempo->hann);
    av_freep(&atempo->correlation);

    av_rdft_end(atempo->real_to_complex);
    atempo->real_to_complex = NULL;

    av_rdft_end(atempo->complex_to_real);
    atempo->complex_to_real = NULL;
}
コード例 #5
0
ファイル: af_surround.c プロジェクト: USBhost/FFmpeg
static av_cold void uninit(AVFilterContext *ctx)
{
    AudioSurroundContext *s = ctx->priv;
    int ch;

    av_frame_free(&s->input);
    av_frame_free(&s->output);
    av_frame_free(&s->overlap_buffer);

    for (ch = 0; ch < s->nb_in_channels; ch++) {
        av_rdft_end(s->rdft[ch]);
    }
    for (ch = 0; ch < s->nb_out_channels; ch++) {
        av_rdft_end(s->irdft[ch]);
    }
    av_freep(&s->input_levels);
    av_freep(&s->output_levels);
    av_freep(&s->rdft);
    av_freep(&s->irdft);
    av_audio_fifo_free(s->fifo);
    av_freep(&s->window_func_lut);
}
コード例 #6
0
void DLL_EXPORT FFMPEG_API release()
{
	state->quit = 1;
	SDL_Quit();

	if (state->wave)
	{
		free(state->wave->tmp_buffer);
		free(state->wave);
	}

	if (state->fft) 
	{
		av_rdft_end(state->fft->ctx);
		free(state->fft);
	}

	av_free(state);
}
コード例 #7
0
ファイル: fft-lavc.c プロジェクト: alexkay/goperf
int main() {
    int N = 1000000; // 1M
    int nbits = 11;
    int input_size = 1 << nbits;
    int output_size = (1 << (nbits - 1)) + 1;

    float *input = malloc(input_size * sizeof(float));
    float *output = malloc(output_size * sizeof(float));

    struct RDFTContext *cx = av_rdft_init(nbits, DFT_R2C);

    float f = M_PI;
    for (int i = 0; i < input_size; ++i) {
        f = floorf(f * M_PI);
        input[i] = f;
    }

    for (int k = 0; k < N; k++ ) {
        av_rdft_calc(cx, input);
    }

    av_rdft_end(cx);
    return 0;
}
コード例 #8
0
ファイル: af_atempo.c プロジェクト: chinshou/ffmpeg-vda
/**
 * Prepare filter for processing audio data of given format,
 * sample rate and number of channels.
 */
static int yae_reset(ATempoContext *atempo,
                     enum AVSampleFormat format,
                     int sample_rate,
                     int channels)
{
    const int sample_size = av_get_bytes_per_sample(format);
    uint32_t nlevels  = 0;
    uint32_t pot;
    int i;

    atempo->format   = format;
    atempo->channels = channels;
    atempo->stride   = sample_size * channels;

    // pick a segment window size:
    atempo->window = sample_rate / 24;

    // adjust window size to be a power-of-two integer:
    nlevels = av_log2(atempo->window);
    pot = 1 << nlevels;
    av_assert0(pot <= atempo->window);

    if (pot < atempo->window) {
        atempo->window = pot * 2;
        nlevels++;
    }

    // initialize audio fragment buffers:
    REALLOC_OR_FAIL(atempo->frag[0].data, atempo->window * atempo->stride);
    REALLOC_OR_FAIL(atempo->frag[1].data, atempo->window * atempo->stride);
    REALLOC_OR_FAIL(atempo->frag[0].xdat, atempo->window * sizeof(FFTComplex));
    REALLOC_OR_FAIL(atempo->frag[1].xdat, atempo->window * sizeof(FFTComplex));

    // initialize rDFT contexts:
    av_rdft_end(atempo->real_to_complex);
    atempo->real_to_complex = NULL;

    av_rdft_end(atempo->complex_to_real);
    atempo->complex_to_real = NULL;

    atempo->real_to_complex = av_rdft_init(nlevels + 1, DFT_R2C);
    if (!atempo->real_to_complex) {
        yae_release_buffers(atempo);
        return AVERROR(ENOMEM);
    }

    atempo->complex_to_real = av_rdft_init(nlevels + 1, IDFT_C2R);
    if (!atempo->complex_to_real) {
        yae_release_buffers(atempo);
        return AVERROR(ENOMEM);
    }

    REALLOC_OR_FAIL(atempo->correlation, atempo->window * sizeof(FFTComplex));

    atempo->ring = atempo->window * 3;
    REALLOC_OR_FAIL(atempo->buffer, atempo->ring * atempo->stride);

    // initialize the Hann window function:
    REALLOC_OR_FAIL(atempo->hann, atempo->window * sizeof(float));

    for (i = 0; i < atempo->window; i++) {
        double t = (double)i / (double)(atempo->window - 1);
        double h = 0.5 * (1.0 - cos(2.0 * M_PI * t));
        atempo->hann[i] = (float)h;
    }

    yae_clear(atempo);
    return 0;
}
コード例 #9
0
FFTFrame::~FFTFrame()
{
    av_rdft_end(m_forwardContext);
    av_rdft_end(m_inverseContext);
}
コード例 #10
0
float bl_frequency_sort(struct bl_song const * const song) {
	// FFT transform context
	RDFTContext* fft;
	// Hann window values
	float hann_window[WINDOW_SIZE];
	// Number of frames, that is number of juxtaposed windows in the music
	int n_frames;
	// Complex DFT of input
	FFTSample* x;
	// Hold FFT power spectrum
	FFTSample *power_spectrum;
	// Power maximum value
	float peak = 0;

	// Array containing frequency mean of different bands 
	float bands[5];
	// Weighted sum of frequency bands
	float bands_sum;

	// Initialize Hann window
	for(int i = 0; i < WINDOW_SIZE; ++i) {
		hann_window[i] = .5f * (1.0f - cos(2 * M_PI * i / (WINDOW_SIZE - 1)));
    }

	// Initialize band array
	for(int i = 0; i < 5; ++i) {
		bands[i] = 0.0f;
	}

	// Get the number of frames in one channel
	n_frames = floor((song->nSamples / song->channels) / WINDOW_SIZE);

	// Allocate memory for x vector
	x = (FFTSample*)av_malloc(WINDOW_SIZE * sizeof(FFTSample));

	// Zero-initialize power spectrum
	power_spectrum = (FFTSample*) av_malloc((WINDOW_SIZE * sizeof(FFTSample)) / 2 + 1*sizeof(FFTSample));
	for(int i = 0; i <= WINDOW_SIZE / 2; ++i) {  // 2 factor due to x's complex nature and power_spectrum's real nature.
		power_spectrum[i] = 0.0f;
	}

	// Initialize fft
	fft = av_rdft_init(WIN_BITS, DFT_R2C);

	for(int i = 0; i < n_frames * WINDOW_SIZE * song->channels; i += song->channels * WINDOW_SIZE) {
		if(2 == song->channels) {  // Stereo sound
			for(int d = 0; d < WINDOW_SIZE; ++d) {
				x[d] = (float)((((int16_t*)song->sample_array)[i+2*d] + ((int16_t*)song->sample_array)[i+2*d+1])/2) * hann_window[d];
			}
		}
		else {  // Mono sound
			for(int d = 0; d < WINDOW_SIZE; ++d) {
				x[d] = (float)(((int16_t*)song->sample_array)[i+d])*hann_window[d];
			}
		}

		// Compute FFT
		av_rdft_calc(fft, x);

		// Fill-in power spectrum
		power_spectrum[0] = x[0] * x[0];  // Ignore x[1] due to ffmpeg's fft specifity
		for(int d = 1; d < WINDOW_SIZE / 2; ++d) {
			float re = x[d * 2];
			float im = x[d * 2 + 1];
			float raw = (re * re) + (im * im);
			power_spectrum[d] += raw;
		}
	}

	// Normalize it and compute real power in dB
	for(int d = 1; d <= WINDOW_SIZE / 2; ++d) {
		power_spectrum[d] = sqrt(power_spectrum[d] / WINDOW_SIZE);
	
		// Get power spectrum peak
		peak = fmax(power_spectrum[d], peak);
	}

	// Compute power spectrum in dB with 3dB attenuation
	for(int d = 1; d <= WINDOW_SIZE / 2; ++d) {
		power_spectrum[d] = 20 * log10(power_spectrum[d] / peak) - 3;
	}
	// Sum power in frequency bands
	// Arbitrary separation in frequency bands
	bands[0] = (power_spectrum[1] + power_spectrum[2]) / 2;

	bands[1] = (power_spectrum[3] + power_spectrum[4]) / 2;

	for(int i = LOW_INF; i <= LOW_SUP; ++i) {
		bands[2] += power_spectrum[i];
	}
	bands[2] /= (LOW_SUP - LOW_INF);

	for(int i = LOW_SUP + 1; i <= HIGH_INF; ++i) {
		bands[3] += power_spectrum[i];
	}
	bands[3] /= (HIGH_INF - (LOW_SUP + 1));

	for(int i = HIGH_INF + 1; i <= HIGH_SUP; ++i) {
		bands[4] += power_spectrum[i];
	}
	bands[4] /= (HIGH_SUP - (HIGH_INF + 1));

	bands_sum = bands[4] + bands[3] + bands[2] - bands[0] - bands[1];

	// Clean everything
	av_free(x);
	av_free(power_spectrum);
	av_rdft_end(fft);

	// Return final score, weighted by coefficients in order to have -4 for a panel of calm songs,
	// and 4 for a panel of loud songs. (only useful if you want an absolute « Loud » and « Calm » result
	return ((1. / 3.) * bands_sum + 68. / 3.);
}
コード例 #11
0
FFTLib::~FFTLib() {
	av_rdft_end(m_rdft_ctx);
	av_free(m_input);
	av_free(m_window);
}