<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body style='font-size: 10pt; font-family: Verdana,Geneva,sans-serif'>
<p>I'm trying to use libav to read in audio PCM data.  I've got it to read in my .wav sample data correctly (the byte format is <span style="color: #800080;">AV_SAMPLE_FMT_S16</span>). However, my .mp3 sample is getting a lot of strange spikes in the data.  The sample format for the .mp3 is <span style="color: #800080;">AV_SAMPLE_FMT_FLTP and while most of the samples appear to be correct, about 10% of them have very large magnitudes.  I'm wondering what's going wrong, and if this might have something to do with the exponent of the floating point number being translated incorrectly.  It's not clear to me what the difference between AV_SAMPLE_FMT_FLT and AV_SAMPLE_FMT_FLTP is.<br /></span></p>
<p><span style="color: #800080;">This is a sample of the bit of my code that reads the data from the frame.  Am I reading it correctly?</span></p>
<p><br /></p>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">        </span><span style="color: #808000;">int</span><span style="color: #c0c0c0;"> </span><span style="color: #092e64;">bufferSize</span><span style="color: #c0c0c0;"> </span>=<span style="color: #c0c0c0;"> </span><span style="color: #00677c;">av_samples_get_buffer_size</span>(<span style="color: #000080;">NULL</span>,</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                                   </span><span style="color: #800000;">aCodecCtx</span>-><span style="color: #800000;">channels</span>,</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                                   </span><span style="color: #800000;">aFrame</span>-><span style="color: #800000;">nb_samples</span>,</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                                   </span><span style="color: #800000;">aCodecCtx</span>-><span style="color: #800000;">sample_fmt</span>,</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                                   </span><span style="color: #000080;">1</span>);</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">        </span><span style="color: #800080;">uint8_t</span><span style="color: #c0c0c0;"> </span>*<span style="color: #092e64;">dataBuf</span><span style="color: #c0c0c0;"> </span>=<span style="color: #c0c0c0;"> </span><span style="color: #800000;">aFrame</span>-><span style="color: #800000;">data</span>[<span style="color: #000080;">0</span>];</pre>
<pre style="-qt-paragraph-type: empty; -qt-block-indent: 0; text-indent: 0px; margin: 0px;"> </pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">        </span><span style="color: #808000;">for</span><span style="color: #c0c0c0;"> </span>(<span style="color: #808000;">int</span><span style="color: #c0c0c0;"> </span><span style="color: #092e64;">i</span><span style="color: #c0c0c0;"> </span>=<span style="color: #c0c0c0;"> </span><span style="color: #000080;">0</span>;<span style="color: #c0c0c0;"> </span><span style="color: #092e64;">i</span><span style="color: #c0c0c0;"> </span><<span style="color: #c0c0c0;"> </span><span style="color: #800000;">aFrame</span>-><span style="color: #800000;">nb_samples</span>;<span style="color: #c0c0c0;"> </span><span style="color: #092e64;">i</span>++)</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">        </span>{</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">            </span><span style="color: #808000;">for</span><span style="color: #c0c0c0;"> </span>(<span style="color: #808000;">int</span><span style="color: #c0c0c0;"> </span><span style="color: #092e64;">ch</span><span style="color: #c0c0c0;"> </span>=<span style="color: #c0c0c0;"> </span><span style="color: #000080;">0</span>;<span style="color: #c0c0c0;"> </span><span style="color: #092e64;">ch</span><span style="color: #c0c0c0;"> </span><<span style="color: #c0c0c0;"> </span><span style="color: #092e64;">numChannels</span>;<span style="color: #c0c0c0;"> </span><span style="color: #092e64;">ch</span>++)</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">            </span>{</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                </span><span style="color: #808000;">float</span><span style="color: #c0c0c0;"> </span><span style="color: #092e64;">val</span><span style="color: #c0c0c0;"> </span>=<span style="color: #c0c0c0;"> </span><span style="color: #000080;">0</span>;</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                </span><span style="color: #808000;">switch</span><span style="color: #c0c0c0;"> </span>(<span style="color: #800000;">aCodecCtx</span>-><span style="color: #800000;">sample_fmt</span>)</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                </span>{<br />                 ...</pre>
<pre style="-qt-paragraph-type: empty; -qt-block-indent: 0; text-indent: 0px; margin: 0px;"> </pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                </span><span style="color: #808000;">case</span><span style="color: #c0c0c0;"> </span><span style="color: #800080;">AV_SAMPLE_FMT_FLT</span>:</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                </span><span style="color: #808000;">case</span><span style="color: #c0c0c0;"> </span><span style="color: #800080;">AV_SAMPLE_FMT_FLTP</span>:</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                </span>{</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                    </span><span style="color: #092e64;">val</span><span style="color: #c0c0c0;"> </span>=<span style="color: #c0c0c0;"> </span>((<span style="color: #808000;">float</span>*)<span style="color: #092e64;">dataBuf</span>)[<span style="color: #092e64;">ch</span><span style="color: #c0c0c0;"> </span>+<span style="color: #c0c0c0;"> </span><span style="color: #092e64;">i</span><span style="color: #c0c0c0;"> </span>*<span style="color: #c0c0c0;"> </span><span style="color: #092e64;">numChannels</span>];</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #008000;"></span><span style="color: #808000;">if</span><span style="color: #c0c0c0;"> </span>(<span style="color: #092e64;">val</span><span style="color: #c0c0c0;"> </span>><span style="color: #c0c0c0;"> </span><span style="color: #000080;">1</span><span style="color: #c0c0c0;"> </span>||<span style="color: #c0c0c0;"> </span><span style="color: #092e64;">val</span><span style="color: #c0c0c0;"> </span><<span style="color: #c0c0c0;"> </span>-<span style="color: #000080;">1</span>)</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;">{</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">    qDebug("Sample out of range %f", val);<br /></span><span style="color: #008000;"></span></pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">    </span><span style="color: #092e64;">val</span><span style="color: #c0c0c0;"> </span>=<span style="color: #c0c0c0;"> </span><span style="color: #000080;">0</span>;</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;">}</pre>
<pre style="-qt-paragraph-type: empty; -qt-block-indent: 0; text-indent: 0px; margin: 0px;"> </pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                    </span><span style="color: #808000;">break</span>;</pre>
<pre style="-qt-block-indent: 0; text-indent: 0px; margin: 0px;"><span style="color: #c0c0c0;">                </span>}</pre>
<pre style="-qt-paragraph-type: empty; -qt-block-indent: 0; text-indent: 0px; margin: 0px;"> </pre>
<p><br /></p>
<div id="signature">
<div class="pre" style="margin: 0; padding: 0; font-family: monospace">---<br /><a href="http://www.kitfox.com" target="_blank" rel="noopener noreferrer">http://www.kitfox.com</a></div>
<div class="pre" style="margin: 0; padding: 0; font-family: monospace"> </div>
</div>
</body></html>