[Libav-user] How to read in planar data?

Mark McKay mark at kitfox.com
Fri Dec 6 22:55:47 EET 2019


I'm having trouble trying to figure out how to read in planar data.  I
have a file that has data in the AV_SAMPLE_FMT_FLTP format with two
channels.  Below is my attempt to read in the data.  This will crash
with a memory problem if I try to read in the second channel (but it
will complete if I bypass the second and just read from channel 0).  Am
I doing this right? 

void AudioReader::loadAudioFrame(AVPacket& packet)
{
int err = avcodec_send_packet(aCodecCtx, &packet);
if (err < 0)
{
qDebug("Error sending packet to decoder");
return;
} 

int numChannels = aCodecCtx->channels;
while (err >= 0)
{
err = avcodec_receive_frame(aCodecCtx, aFrame);
if (err == AVERROR(EAGAIN) || err == AVERROR_EOF)
return; 

if (err < 0)
{
qDebug() << "Error decoding packet: " << err;
return;
} 

int bytesPerSample = av_get_bytes_per_sample(aCodecCtx->sample_fmt);
if (bytesPerSample < 0) {
/* This should not occur, checking just for paranoia */
fprintf(stderr, "Failed to calculate data size\n");
exit(1);
} 

int bufferSize = av_samples_get_buffer_size(NULL,
aCodecCtx->channels,
aFrame->nb_samples,
aCodecCtx->sample_fmt,
1); 

switch(aCodecCtx->sample_fmt)
{
...
case AV_SAMPLE_FMT_FLTP:
{
//Serial format - change to interleaved order
for (int i = 0; i < aFrame->nb_samples; i++)
{
for (int ch = 0; ch < numChannels; ch++)
{
_audioBytes.append((const char *)aFrame->data[ch] + i * sizeof(float),
sizeof(float));
}
}
break;
}
}
} 

---
http://www.kitfox.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20191206/98ad32b3/attachment.html>


More information about the Libav-user mailing list