I am building a project to view a video feed from an IP camera in Android using FFmpeg 1.1.
I’m attempting to use swresample in an Android project and getting a floating point exception when calling swr_convert. I stepped through the swresample code and found one line in libswresample/swresample.c function swri_realloc_audio where the variables a->bps and a->ch_count are zero causing the FPE.
int swri_realloc_audio(AudioData *a, int count){
int i, countb;
AudioData old;
LOGD("in swri_realloc_audio - bps[%d], ch_count[%d]", a->bps, a->ch_count);
if(count < 0 || count > INT_MAX/2/a->bps/a->ch_count)
return AVERROR(EINVAL);
01-21 17:29:09.612: D/swresample.c(18789): in swri_realloc_audio – bps[0], ch_count[0]
I found bug ticket #1834 in the FFmpeg project that sounds like the exact same issue, but it was resolved by calling swr_init. However, my code does call this function and still crashes. Here is my JNI code:
SwrContext* resampleCtx = swr_alloc_set_opts(NULL,
AV_CH_LAYOUT_MONO, AV_SAMPLE_FMT_S16, pAudioCodecCtx->sample_rate,
pAudioCodecCtx->channel_layout, pAudioCodecCtx->sample_fmt,
pAudioCodecCtx->sample_rate, 0, 0);
swr_init(resampleCtx);
LOGD("Resample context initialized");
int dataSize = swr_convert(resampleCtx,
&pAudioOutBuffer, AVCODEC_MAX_AUDIO_FRAME_SIZE / 2,
(const uint8_t**) &(pFrame->data[0]), pFrame->nb_samples);
LOGD("Resample conversion complete");
swr_free(&resampleCtx);
LOGD("Obtained data size - dataSize[%d]", dataSize);
I’m confused because I don’t seem to have any control over the variable a in the swri_realloc_audio function. I stepped through the code and noticed that it is from the variable resampleCtx->postin. This variable is copied from resampleCtx->in in the swr_init function, but I don’t see where in is ever set to anything.
What am I doing wrong? Is it in my code or is there a problem in swresample?
The answer here is I made a mistake in the input. AV_SAMPLE_FMT_S16 is not supported by swr_convert and the function call to swr_init was failing. I just wasn’t checking the result to know this.