samedi 27 décembre 2014

Converting audio data with ffmpeg to libextract compatible array of doubles

Using ffmpeg libraries in c++ I am decoding live audio streams to signed 16 bit arrays easily using the codec defined within the stream itself. I now want to encode this input into arrays that are compatible with the libextract library that requires arrays of doubles, with values in the range of -1.0 to 1.0.


So I am using avcodec_encode_audio2 to convert to arrays of doubles. But I must not be doing it correctly as the AVPacket data member variable contains doubles that are outside the acceptable range.


Can anyone advise me on my mistake during the encoding?



_audioCodec = avcodec_find_encoder(CODEC_ID_PCM_F64BE);
if (_audioCodec)
{
_audioCodecContext = avcodec_alloc_context3(_audioCodec);
_audioCodecContext->bit_rate = 128000;
_audioCodecContext->sample_rate = 22050;
_audioCodecContext->channels = 1;
_audioCodecContext->sample_fmt = AV_SAMPLE_FMT_DBLP;

if (avcodec_open2(_audioCodecContext, _audioCodec, nullptr) >= 0)
{
int got_output = 0;
AVPacket outPacket = {0};

av_init_packet(&outPacket);

int err = avcodec_encode_audio2(_audioCodecContext, &outPacket, &frame, &got_output);
if (!err && got_output)
{
double * dbls = (double*)outPacket.data;
//
// When casting outPacket.data to double * the data contains values outside
// -1.0 to 1.0 and lots of NAN's. I'm obviously doing something wrong.
//


...


I have used variations of the codec such as CODEC_ID_PCM_F32LE, CODEC_ID_PCM_F64LE, CODEC_ID_PCM_F32BE, CODEC_ID_PCM_F64BE and I have used variations of AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT and AV_SAMPLE_FMT_FLTP but to no avail.


Thanks in advance.


James


Aucun commentaire:

Enregistrer un commentaire