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.
This commit is contained in:
Sam Edwards 2018-03-27 21:25:55 -06:00
parent 9596095294
commit eb591674e0
2 changed files with 33 additions and 15 deletions

View File

@ -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;

View File

@ -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
}
}