libavcodec and libavformat are pretty awesome when it comes to decoding/demuxing audio streams. They provide a codec and format agnostic API that processes containers, extracts metadata, and and decodes streams as fixed-size packets one by one into PCM samples.

Unfortunately, libav is not very easy on beginners. It’s very difficult to find code examples or tutorials or even documentation that isn’t just API references that is up to date. So after playing with it for a while, I decided to blog about my audio decoding venture, maybe it would help someone get into programming with libav more easily.

I used the equally awesome libao to send the decoded PCM samples to the audio device. It is both cross-platform and very elegantly simple to use.

For the sake of simplicity, I have made the assumptions that the PCM samples are of 16bit size each, and that the sample rate is 44.1K samples/second, and channel count is 2. In order to extract these information from the container, you’ll have to read at least one packet first so they would become available. You could decode that read packet and then continue from the next one, or seek back to the beginning of the stream afterwards, and start from there. Like in the following code: