I want to encode and decode the sound from my Android app to the Opus format using FFmpeg 4.2.2.
The problem is that my Android app provides a raw PCM sound in AV_SAMPLE_FMT_S16
format, but the FFmpeg opus encoder requires only AV_SAMPLE_FMT_FLTP
. So, I decided to resample the sound using FFmpeg swr_convert()
function but it crashes with SIGSEGV
error and I can't understand why.
My code looks like this:
swrContext = swr_alloc();
av_opt_set_int(swrContext, "in_channel_layout", (int64_t) codecContext->channel_layouts, 0);
av_opt_set_int(swrContext, "out_channel_layout", (int64_t) codecContext->channel_layouts, 0);
av_opt_set_int(swrContext, "in_sample_rate", 8000, 0);
av_opt_set_int(swrContext, "out_sample_rate", 48000, 0);
av_opt_set_sample_fmt(swrContext, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
av_opt_set_sample_fmt(swrContext, "out_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
swr_init(swrContext);
memcpy(frame->data[0], data, dataSize);
uint8_t *outBuffer = (uint8_t *) malloc(sizeof(uint8_t) * frame->nb_samples);
swr_convert(swrContext, &outBuffer, frame->nb_samples, (const uint8_t **)frame->data, frame->nb_samples);
I am new to C++ so sorry for some mistakes if I made them.