New submission from Justin Ruggles <justin.rugg...@gmail.com>: copy/paste of attached SoundFragmentsAfterSeeking.txt:
Hi, while trying to use FFmpeg libs as a developer I found the following: When decoding sound from some compressed audio formats (namely i tested some mp4 files with aac audio codec and some wmv files with wma audio codec). After seeking the beginning of the first decoded packet is buggy. Looks like some leftovers from internal buffers - but I am using 'avcodec_flush_buffers'! That of course leads to nasty sound clicks. See sourcecode below. Basically it shows the first few bytes of the first decoded sound packet then seeks back and shows the first packets decoding again. If you use it with some wmv or mp4 file with sound you should see see effect that both outputs are different but should be equal. Just make sure the sound is not only silence at the first 10 packets. Or am I missing some other flush...whatever function here? I am developing under windows with the r26400 32bit developer compilation from http://ffmpeg.arrozcru.org/autobuilds/ffmpeg/mingw32/dev/shared/ It happens also with other releases and also the older 64 bit version. // *** Sourcecode from here *** AVFormatContext *pFormatCtx = NULL; AVPacket packet; AVStream *pSoundStream = NULL; AVCodec *pSoundCodec = NULL; unsigned char *pSoundDecodeBuff; const int SoundDecodeBuffSize = (AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2; // Decode next sound packet, print some data and return timestamp int64_t DecodeSoundPacket() { while (av_read_frame(pFormatCtx, &packet) >= 0) { if (packet.stream_index == pSoundStream->index) { int nBuff = SoundDecodeBuffSize; int used = avcodec_decode_audio3(pSoundStream->codec, (int16_t*)pSoundDecodeBuff, &nBuff, &packet); if (used != packet.size) printf("Problem while decoding!\n"); // This also indicates multi frame packets av_free_packet(&packet); break; } av_free_packet(&packet); } printf("Packet at %.2f:", (float)packet.dts * pSoundStream->time_base.num / pSoundStream->time_base.den); for (int i = 0; i < 20; i++) printf(" %.2x", (int)pSoundDecodeBuff[i]); printf("\n"); return packet.dts; } int main(int argc, char* argv[]) { // Open media file av_register_all(); if(av_open_input_file(&pFormatCtx, "D:\\Projekte\\VideoTest\\VideoTest_1280x720_29p_h264_Audio48k.mp4", NULL, 0, NULL) != 0) return -1; if(av_find_stream_info(pFormatCtx) < 0) return -1; // Find the first sound stream for(int i = 0; i < (int)pFormatCtx->nb_streams; i++) if(pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) { pSoundStream = pFormatCtx->streams[i]; break; } if(pSoundStream == NULL) return -1; // Open the decoder for the sound stream pSoundCodec = avcodec_find_decoder(pSoundStream->codec->codec_id); if (pSoundCodec==NULL) return -1; if (avcodec_open(pSoundStream->codec, pSoundCodec) < 0) return -1; pSoundDecodeBuff = (unsigned char*)av_malloc(SoundDecodeBuffSize); // Decode first packet and some others int64_t SeekTo = DecodeSoundPacket(); for (int i = 0; i < 10; i++) DecodeSoundPacket(); // Seek back av_seek_frame(pFormatCtx, pSoundStream->index, SeekTo, AVSEEK_FLAG_BACKWARD); avcodec_flush_buffers(pSoundStream->codec); // Enable this and the bug is gone //avcodec_close(pSoundStream->codec); //avcodec_open(pSoundStream->codec, pSoundCodec); // Decode first packet again printf("\n"); DecodeSoundPacket(); // No cleanup - I am soooo dirty } ---------- topic: -avcodec, avfilter, avformat ________________________________________________ FFmpeg issue tracker <iss...@roundup.ffmpeg.org> <https://roundup.ffmpeg.org/issue2581> ________________________________________________