From eb591674e09bb0a7a39b9b3b36f43c54d820756f Mon Sep 17 00:00:00 2001 From: Sam Edwards Date: Tue, 27 Mar 2018 21:25:55 -0600 Subject: [PATCH] ffmpeg: Prefer av_packet_alloc over allocating AVPacket ourselves av_packet_alloc/av_packet_free will know the correct size of AVPacket, even if the ABI should change. So, we use it when it's available. --- panda/src/ffmpeg/ffmpegAudioCursor.cxx | 36 ++++++++++++++++++-------- panda/src/ffmpeg/ffmpegVideoCursor.cxx | 12 ++++++--- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/panda/src/ffmpeg/ffmpegAudioCursor.cxx b/panda/src/ffmpeg/ffmpegAudioCursor.cxx index 2a94dd25b9..dafcc8f2c5 100644 --- a/panda/src/ffmpeg/ffmpegAudioCursor.cxx +++ b/panda/src/ffmpeg/ffmpegAudioCursor.cxx @@ -164,7 +164,12 @@ FfmpegAudioCursor(FfmpegAudio *src) : _frame = avcodec_alloc_frame(); #endif +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100) + _packet = av_packet_alloc(); +#else _packet = new AVPacket; +#endif + _buffer_size = AVCODEC_MAX_AUDIO_FRAME_SIZE / 2; _buffer_alloc = new int16_t[_buffer_size + 64]; @@ -213,15 +218,15 @@ cleanup() { } if (_packet) { - if (_packet->data) { #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100) - av_packet_unref(_packet); + av_packet_free(&_packet); #else + if (_packet->data) { av_free_packet(_packet); -#endif } delete _packet; _packet = NULL; +#endif } if (_buffer_alloc) { @@ -303,16 +308,25 @@ reload_buffer() { } else if (_packet_size > 0) { int bufsize = _buffer_size * 2; int got_frame; - AVPacket pkt; - av_init_packet(&pkt); - pkt.data = _packet_data; - pkt.size = _packet_size; - int len = avcodec_decode_audio4(_audio_ctx, _frame, &got_frame, &pkt); - movies_debug("avcodec_decode_audio4 returned " << len); + + AVPacket *pkt; #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100) - av_packet_unref(&pkt); + pkt = av_packet_alloc(); #else - av_free_packet(&pkt); + AVPacket _pkt; + pkt = &_pkt; + av_init_packet(pkt); +#endif + pkt->data = _packet_data; + pkt->size = _packet_size; + + int len = avcodec_decode_audio4(_audio_ctx, _frame, &got_frame, pkt); + movies_debug("avcodec_decode_audio4 returned " << len); + +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100) + av_packet_free(&pkt); +#else + av_free_packet(pkt); #endif bufsize = 0; diff --git a/panda/src/ffmpeg/ffmpegVideoCursor.cxx b/panda/src/ffmpeg/ffmpegVideoCursor.cxx index 33d0a1d411..cf2b01bf5c 100644 --- a/panda/src/ffmpeg/ffmpegVideoCursor.cxx +++ b/panda/src/ffmpeg/ffmpegVideoCursor.cxx @@ -94,8 +94,12 @@ init_from(FfmpegVideo *source) { return; } +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100) + _packet = av_packet_alloc(); +#else _packet = new AVPacket; - memset(_packet, 0, sizeof(AVPacket)); + av_init_packet(_packet); +#endif fetch_packet(0); fetch_frame(-1); @@ -624,15 +628,15 @@ cleanup() { } if (_packet) { - if (_packet->data) { #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100) - av_packet_unref(_packet); + av_packet_free(&_packet); #else + if (_packet->data) { av_free_packet(_packet); -#endif } delete _packet; _packet = NULL; +#endif } }