realloc when next size is larger than current

dev
xufulong 3 years ago
parent 1410df401b
commit 28f3ae6aaf
  1. 10
      app/src/main/cpp/audio_player.cpp
  2. 15
      app/src/main/cpp/visualizer/execute_fft.c
  3. 3
      app/src/main/cpp/visualizer/execute_fft.h
  4. 4
      app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt

@ -286,6 +286,9 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr, jstring filter_jstr) {
jmethodID fft_method = env->GetMethodID(player_class, "fftCallbackFromJNI", "([S)V");
auto *fft_filter = static_cast<filter_sys_t *>(malloc(sizeof(filter_sys_t)));
fft_filter->nb_samples = 0;
fft_filter->data_size = 0;
fft_filter->data = nullptr;
init_visualizer(fft_filter);
//read audio frame
@ -309,11 +312,8 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr, jstring filter_jstr) {
}
if (got_frame > 0) {
if (!fft_filter->data && frame->nb_samples > fft_filter->out_samples) {
fft_filter->nb_samples = frame->nb_samples;
fft_filter->data = static_cast<uint8_t *>(malloc(frame->nb_samples * sizeof(uint8_t)));
}
if (fft_filter->nb_samples == frame->nb_samples) {
ret = ensure_memory(fft_filter, frame->nb_samples);
if (ret == 0 && fft_filter->nb_samples == frame->nb_samples) {
memcpy(fft_filter->data, frame->data[0], static_cast<size_t>(frame->nb_samples));
fft_once(fft_filter);
fft_callback(env, thiz, fft_method, fft_filter->output, fft_filter->out_samples);

@ -224,6 +224,21 @@ void release_visualizer(filter_sys_t *p_filter)
free(p_filter);
}
int ensure_memory(filter_sys_t *fft_filter, int nb_samples) {
if (nb_samples != fft_filter->nb_samples && nb_samples > fft_filter->out_samples) {
if (!fft_filter->data) {
fft_filter->data_size = nb_samples;
fft_filter->data = (uint8_t *) (malloc(nb_samples * sizeof(uint8_t)));
} else if (nb_samples > fft_filter->data_size) {
fft_filter->data_size = nb_samples;
fft_filter->data = (uint8_t *) (realloc(fft_filter->data, nb_samples * sizeof(uint8_t)));
}
if (!fft_filter->data) return -1;
fft_filter->nb_samples = nb_samples;
}
return 0;
}
void fft_once(filter_sys_t *p_sys)
{
int nb_samples = p_sys->nb_samples;

@ -30,6 +30,7 @@ typedef struct
window_param wind_param;
uint8_t *data;
int data_size;
int nb_samples;
int16_t * output;
int out_samples;
@ -49,4 +50,6 @@ int init_visualizer(filter_sys_t *p_sys);
void release_visualizer(filter_sys_t *p_sys);
int ensure_memory(filter_sys_t *fft_filter, int nb_samples);
#endif //EXECUTE_FFT_H

@ -34,7 +34,7 @@ class EqualizerActivity : BaseActivity(), OnSeeBarListener {
private val selectBandList = IntArray(bandsList.size)
private val minEQLevel = 0
private val fftData = ByteArray(/*20*/256)
private val fftData = ByteArray(256)
private var filterThread: Thread? = null
private var mAudioPlayer: AudioPlayer? = null
private var visualizerView: VisualizerView? = null
@ -92,12 +92,10 @@ class EqualizerActivity : BaseActivity(), OnSeeBarListener {
}
mAudioPlayer = AudioPlayer()
mAudioPlayer?.setOnFftCallback {
Log.e("Equalizer", "fft data[0]=" + it[0] + "--data[1]=" + it[1] + "--data[2]=" + it[2])
if (visualizerView != null && it != null) {
for (i in it.indices) {
fftData[i] = (it[i]/*10/4.2f*/).toByte()
}
Log.e("Equalizer", "data[0]=" + fftData[0] + "--data[1]=" + fftData[1] + "--data[2]=" + fftData[2])
visualizerView!!.post { visualizerView!!.setWaveData(fftData) }
}
}

Loading…
Cancel
Save