<div dir="ltr"><div>I added some code to check if it was crashing because dataSize is negative. It's not. Also, I found out that dtSize in this instance is 4608, and no matter what I do, sizeof(frame->data[0]) is always 8. Also, I tried sizeof(frame->data) which returned 64.</div><div><br></div><div><div>int dtSize = av_samples_get_buffer_size(nullptr, codecCtx->channels, frame->nb_samples, codecCtx->sample_fmt, 1);</div><div><span class="" style="white-space:pre">                        </span>wxGetApp().popUpErrorDialog("dtSize: " + std::to_string(dtSize));</div><div><span class="" style="white-space:pre">                        </span>dataSize = dtSize;</div><div><br></div><div><span class="" style="white-space:pre">                        </span>while(totalBufferSize + dataSize > estimatedBuffSize)</div><div><span class="" style="white-space:pre">                   </span>{</div><div><span class="" style="white-space:pre">                          </span>estimatedBuffSize *= 1.1;</div><div><span class="" style="white-space:pre">                          </span>sampleBuffer = (uint8_t*)std::realloc(sampleBuffer, estimatedBuffSize);</div><div><span class="" style="white-space:pre">                    </span>}</div><div><span class="" style="white-space:pre">                  </span></div><div><span class="" style="white-space:pre">                   </span>wxGetApp().popUpErrorDialog("sizeof(frame->data[0]): " + std::to_string(sizeof(frame->data[0])));</div><div><br></div><div><span class="" style="white-space:pre">                 </span>std::memcpy(sampleBuffer + totalBufferSize, frame->data[0], dataSize);</div><div><br></div><div><span class="" style="white-space:pre">                 </span>totalBufferSize += dataSize;</div><div><span class="" style="white-space:pre">                       </span>totalSamples += frame->nb_samples;</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jun 20, 2016 at 5:47 PM, Bill Messenger <span dir="ltr"><<a href="mailto:apothemmusic@gmail.com" target="_blank">apothemmusic@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">This is the function I'm using to decode the audio file. My guess is that it's crashing because "dataSize" is larger than "frame->data[0]", but I'm pretty sure my calculation of dataSize is correct. Am I copying the frame data to "sampleBuffer" the wrong way?<div><br></div><div><div>bool AudioDecoder::decodeFile(std::string* filename)</div><div>{</div><div><span style="white-space:pre-wrap">  </span>reset(); // if a file has already been decoded, free it from memory and reset</div><div><br></div><div><span style="white-space:pre-wrap">   </span>AVFormatContext* formatCtx = avformat_alloc_context();</div><div><span style="white-space:pre-wrap">   </span>if(avformat_open_input(&formatCtx, filename->c_str(), nullptr, nullptr) < 0)</div><div><span style="white-space:pre-wrap">   </span>{</div><div><span style="white-space:pre-wrap">                </span>wxGetApp().popUpErrorDialog("Couldn't open \"" + *filename + "\".");</div><div><span style="white-space:pre-wrap">           </span>avformat_close_input(&formatCtx);</div><div><span style="white-space:pre-wrap">            </span>return false;</div><div><span style="white-space:pre-wrap">    </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>if(avformat_find_stream_info(formatCtx, nullptr) < 0)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap">                </span>wxGetApp().popUpErrorDialog("Couldn't find file info for \"" + *filename + "\".");</div><div><span style="white-space:pre-wrap">             </span>avformat_close_input(&formatCtx);</div><div><span style="white-space:pre-wrap">            </span>return false;</div><div><span style="white-space:pre-wrap">    </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>av_dump_format(formatCtx, 0, filename->c_str(), false);</div><div><br></div><div><span style="white-space:pre-wrap">      </span>int streamID = -1;</div><div><span style="white-space:pre-wrap">       </span>for(int i = 0; i < formatCtx->nb_streams; i++)</div><div><span style="white-space:pre-wrap">     </span>{</div><div><span style="white-space:pre-wrap">                </span>if(formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)</div><div><span style="white-space:pre-wrap">              </span>{</div><div><span style="white-space:pre-wrap">                        </span>streamID = i;</div><div><span style="white-space:pre-wrap">                    </span>break;</div><div><span style="white-space:pre-wrap">           </span>}</div><div><span style="white-space:pre-wrap">        </span>}</div><div><span style="white-space:pre-wrap">        </span>if(streamID == -1)</div><div><span style="white-space:pre-wrap">       </span>{</div><div><span style="white-space:pre-wrap">                </span>wxGetApp().popUpErrorDialog("\"" + *filename + "\" does not contain audio.");</div><div><span style="white-space:pre-wrap">              </span>avformat_close_input(&formatCtx);</div><div><span style="white-space:pre-wrap">            </span>return false;</div><div><span style="white-space:pre-wrap">    </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>AVCodecContext* codecCtx = formatCtx->streams[streamID]->codec;</div><div><span style="white-space:pre-wrap">    </span>AVCodec* codec = avcodec_find_decoder(codecCtx->codec_id);</div><div><span style="white-space:pre-wrap">    </span>if(!codec)</div><div><span style="white-space:pre-wrap">       </span>{</div><div><span style="white-space:pre-wrap">                </span>wxGetApp().popUpErrorDialog("Couldn't find the codec for \"" + *filename + "\".");</div><div><span style="white-space:pre-wrap">             </span>avformat_close_input(&formatCtx);</div><div><span style="white-space:pre-wrap">            </span>return false;</div><div><span style="white-space:pre-wrap">    </span>}</div><div><span style="white-space:pre-wrap">        </span>if(avcodec_open2(codecCtx, codec, nullptr) < 0)</div><div><span style="white-space:pre-wrap">       </span>{</div><div><span style="white-space:pre-wrap">                </span>wxGetApp().popUpErrorDialog("Couldn't open the codec for\"" + *filename + "\".");</div><div><span style="white-space:pre-wrap">              </span>avformat_close_input(&formatCtx);</div><div><span style="white-space:pre-wrap">            </span>return false;</div><div><span style="white-space:pre-wrap">    </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>sampleFormat = codecCtx->sample_fmt;</div><div><span style="white-space:pre-wrap">  </span>if(!(sampleFormat == AV_SAMPLE_FMT_U8 || sampleFormat == AV_SAMPLE_FMT_U8P ||</div><div><span style="white-space:pre-wrap">            </span>sampleFormat == AV_SAMPLE_FMT_S16 || sampleFormat == AV_SAMPLE_FMT_S16P ||</div><div><span style="white-space:pre-wrap">               </span>sampleFormat == AV_SAMPLE_FMT_S32 || sampleFormat == AV_SAMPLE_FMT_S32P ||</div><div><span style="white-space:pre-wrap">               </span>sampleFormat == AV_SAMPLE_FMT_FLT || sampleFormat == AV_SAMPLE_FMT_FLTP ||</div><div><span style="white-space:pre-wrap">               </span>sampleFormat == AV_SAMPLE_FMT_DBL || sampleFormat == AV_SAMPLE_FMT_DBLP))</div><div><span style="white-space:pre-wrap">        </span>{</div><div><span style="white-space:pre-wrap">                </span>wxGetApp().popUpErrorDialog("\"" + *filename + "\" uses an unsupported format.");</div><div><span style="white-space:pre-wrap">          </span>avcodec_close(codecCtx);</div><div><span style="white-space:pre-wrap">         </span>avformat_close_input(&formatCtx);</div><div><span style="white-space:pre-wrap">            </span>return false;</div><div><span style="white-space:pre-wrap">    </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>planar = false;</div><div><span style="white-space:pre-wrap">  </span>if(sampleFormat == AV_SAMPLE_FMT_U8P || sampleFormat == AV_SAMPLE_FMT_S16P ||</div><div><span style="white-space:pre-wrap">    </span>   sampleFormat == AV_SAMPLE_FMT_S32P || sampleFormat == AV_SAMPLE_FMT_FLTP || sampleFormat == AV_SAMPLE_FMT_DBLP)</div><div><span style="white-space:pre-wrap">      </span>{</div><div><span style="white-space:pre-wrap">                </span>planar = true;</div><div><span style="white-space:pre-wrap">   </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>AVFrame* frame = av_frame_alloc();</div><div><span style="white-space:pre-wrap">       </span>if(!frame)</div><div><span style="white-space:pre-wrap">       </span>{</div><div><span style="white-space:pre-wrap">                </span>wxGetApp().popUpErrorDialog("Failed to allocate an audio frame.");</div><div><span style="white-space:pre-wrap">             </span>avcodec_close(codecCtx);</div><div><span style="white-space:pre-wrap">         </span>avformat_close_input(&formatCtx);</div><div><span style="white-space:pre-wrap">            </span>return false;</div><div><span style="white-space:pre-wrap">    </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>AVPacket packet;</div><div><span style="white-space:pre-wrap"> </span>av_init_packet(&packet);</div><div><br></div><div><span style="white-space:pre-wrap">    </span>duration = formatCtx->duration / (double)AV_TIME_BASE; // duration is defined in AudioDecoder.h as "double duration = 0;"</div><div><br></div><div><span style="white-space:pre-wrap">  </span>uint64_t estimatedBuffSize = std::ceil(duration * codecCtx->sample_rate * av_get_bytes_per_sample(codecCtx->sample_fmt) * codecCtx->channels);</div><div><br></div><div><span style="white-space:pre-wrap"> </span>sampleBuffer = (uint8_t*)std::malloc(estimatedBuffSize); // sampleBuffer is defined in AudioDecoder.h as "uint8_t* sampleBuffer = nullptr;"</div><div><span style="white-space:pre-wrap">    </span>sampleBufferSet = true;</div><div><br></div><div><span style="white-space:pre-wrap"> </span>int len;</div><div><span style="white-space:pre-wrap"> </span>int gotFrame = 0;</div><div><span style="white-space:pre-wrap">        </span>uint64_t dataSize;</div><div><span style="white-space:pre-wrap">       </span>uint64_t totalBufferSize = 0;</div><div><span style="white-space:pre-wrap">    </span>uint64_t totalSamples = 0;</div><div><span style="white-space:pre-wrap">       </span>while(av_read_frame(formatCtx, &packet) == 0)</div><div><span style="white-space:pre-wrap">        </span>{</div><div><span style="white-space:pre-wrap">                </span>len = avcodec_decode_audio4(codecCtx, frame, &gotFrame, &packet);</div><div><span style="white-space:pre-wrap">                </span>if(len < 0)</div><div><span style="white-space:pre-wrap">           </span>{</div><div><span style="white-space:pre-wrap">                        </span>wxGetApp().popUpErrorDialog("Error while decoding.");</div><div><span style="white-space:pre-wrap">                  </span>std::free(sampleBuffer);</div><div><span style="white-space:pre-wrap">                 </span>sampleBufferSet = false;</div><div><span style="white-space:pre-wrap">                 </span>av_packet_unref(&packet);</div><div><span style="white-space:pre-wrap">                    </span>av_frame_free(&frame);</div><div><span style="white-space:pre-wrap">                       </span>avcodec_close(codecCtx);</div><div><span style="white-space:pre-wrap">                 </span>avformat_close_input(&formatCtx);</div><div><span style="white-space:pre-wrap">                    </span>return false;</div><div><span style="white-space:pre-wrap">            </span>}</div><div><br></div><div><span style="white-space:pre-wrap">               </span>if(gotFrame)</div><div><span style="white-space:pre-wrap">             </span>{</div><div><span style="white-space:pre-wrap">                        </span>dataSize = av_samples_get_buffer_size(nullptr, codecCtx->channels, frame->nb_samples, codecCtx->sample_fmt, 1);</div><div><br></div><div><span style="white-space:pre-wrap">                        </span>while(totalBufferSize + dataSize > estimatedBuffSize)</div><div><span style="white-space:pre-wrap">                 </span>{</div><div><span style="white-space:pre-wrap">                                </span>estimatedBuffSize *= 1.1;</div><div><span style="white-space:pre-wrap">                                </span>sampleBuffer = (uint8_t*)std::realloc(sampleBuffer, estimatedBuffSize);</div><div><span style="white-space:pre-wrap">                  </span>}</div><div><br></div><div><span style="white-space:pre-wrap">                       </span>std::memcpy(sampleBuffer + totalBufferSize, frame->data[0], dataSize);</div><div><br></div><div><span style="white-space:pre-wrap">                       </span>totalBufferSize += dataSize;</div><div><span style="white-space:pre-wrap">                     </span>totalSamples += frame->nb_samples;</div><div><span style="white-space:pre-wrap">            </span>}</div><div><br></div><div><span style="white-space:pre-wrap">               </span>av_packet_unref(&packet);</div><div><span style="white-space:pre-wrap">    </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>sampleBuffer = (uint8_t*)std::realloc(sampleBuffer, totalBufferSize);</div><div><br></div><div><span style="white-space:pre-wrap">   </span>numChannels = codecCtx->channels;</div><div><span style="white-space:pre-wrap">     </span>sampleRate = codecCtx->sample_rate;</div><div><span style="white-space:pre-wrap">   </span>numSamples = totalSamples;</div><div><span style="white-space:pre-wrap">       </span>bufferSize = totalBufferSize;</div><div><br></div><div><span style="white-space:pre-wrap">   </span>av_packet_unref(&packet);</div><div><span style="white-space:pre-wrap">    </span>av_frame_free(&frame);</div><div><span style="white-space:pre-wrap">       </span>avcodec_close(codecCtx);</div><div><span style="white-space:pre-wrap"> </span>avformat_close_input(&formatCtx);</div><div><br></div><div><span style="white-space:pre-wrap">   </span>didInit = true;</div><div><br></div><div><span style="white-space:pre-wrap"> </span>return true;</div><div>}</div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 17, 2016 at 4:20 PM, Bill Messenger <span dir="ltr"><<a href="mailto:apothemmusic@gmail.com" target="_blank">apothemmusic@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Update: I found out that it only crashes in debug mode. When I build it in release mode, it doesn't crash. It must be a bug in MSVC 2015 or something.</div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 17, 2016 at 4:06 PM, Bill Messenger <span dir="ltr"><<a href="mailto:apothemmusic@gmail.com" target="_blank">apothemmusic@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I'm trying to create a class that uses FFmpeg to decode any audio file and store it into memory. Then it has a function that returns a float value of any sample in that buffer. The code I wrote works perfectly for wav and flac files, produces weird audio for mp3 and ogg files, and crashes on certain mp3 files. I spent days trying to figure out why it isn't working, but I can't come up with anything.<div><br></div><div>I think the reason why the audio is weird for mp3 and ogg files is that it uses planar audio instead of interleaved audio, but I don't see what's wrong with the code I wrote. I may be missing something though. For example, to get a sample for 16 bit interleaved audio I use:</div><div><br></div><div><div>int16_t tmp = ((int16_t*)sampleBuffer)[numChannels*sample + channel];</div><div>rv = (float)tmp / 32767.0f;</div></div><div><br></div><div>and to get a sample for 16 bit planar audio I use:</div><div><br></div><div><div>int16_t tmp = ((int16_t*)sampleBuffer)[sample + numSamples*channel];</div><div>rv = (float)tmp / 32767.0f;</div></div><div><br></div><div>And I have no clue why it crashes on certain mp3 files. I paid close attention to make sure there is enough memory allocated in the buffer. What's even weirder is that the file I created "Chiptune 15 2.mp3" didn't crash, but when I renamed it to "test.mp3", it crashed! These crashes happen on line 139 of "AudioDecoder.cpp":</div><div><br></div><div>std::memcpy(sampleBuffer + totalBufferSize, frame->extended_data[0], dataSize);<br></div><div><br></div><div>with an "Access violation reading location" error in vcruntime140d.dll. It says it isn't with location 0x0000000000000000 or 0xFFFFFFFFFFFFFFFF though, it's a different random location.</div><div><br></div><div>I attached a zip file with the c++ code and two mp3's. Oh yeah, I should also mention that I'm using MSVC 2015 Community in Windows 10.</div></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>