diff --git a/av/src/AudioDecoder.cc b/av/src/AudioDecoder.cc index 307397f59..ad73e48b8 100644 --- a/av/src/AudioDecoder.cc +++ b/av/src/AudioDecoder.cc @@ -77,7 +77,7 @@ void AudioDecoder::Cleanup() ///////////////////////////////////////////////// bool AudioDecoder::Decode(uint8_t **_outBuffer, unsigned int *_outBufferSize) { - AVPacket packet, packet1; + AVPacket *packet, packet1; int bytesDecoded = 0; unsigned int maxBufferSize = 0; AVFrame *decodedFrame = nullptr; @@ -110,14 +110,20 @@ bool AudioDecoder::Decode(uint8_t **_outBuffer, unsigned int *_outBufferSize) result = false; } - av_init_packet(&packet); - while (av_read_frame(this->data->formatCtx, &packet) == 0) + packet = av_packet_alloc(); + if (!packet) { - if (packet.stream_index == this->data->audioStream) + ignerr << "Failed to allocate AVPacket" << std::endl; + return false; + } + + while (av_read_frame(this->data->formatCtx, packet) == 0) + { + if (packet->stream_index == this->data->audioStream) { int gotFrame = 0; - packet1 = packet; + packet1 = *packet; while (packet1.size) { // Some frames rely on multiple packets, so we have to make sure @@ -158,10 +164,10 @@ bool AudioDecoder::Decode(uint8_t **_outBuffer, unsigned int *_outBufferSize) packet1.size -= bytesDecoded; } } - AVPacketUnref(&packet); + av_packet_unref(packet); } - AVPacketUnref(&packet); + av_packet_unref(packet); // Seek to the beginning so that it can be decoded again, if necessary. av_seek_frame(this->data->formatCtx, this->data->audioStream, 0, 0); diff --git a/av/src/Video.cc b/av/src/Video.cc index 71d7aaf29..d53f56fe8 100644 --- a/av/src/Video.cc +++ b/av/src/Video.cc @@ -245,12 +245,19 @@ bool Video::Load(const std::string &_filename) ///////////////////////////////////////////////// bool Video::NextFrame(unsigned char **_buffer) { - AVPacket packet; + AVPacket* packet; int frameAvailable = 0; int ret; while (frameAvailable == 0) { + packet = av_packet_alloc(); + if (!packet) + { + ignerr << "Failed to allocate AVPacket" << std::endl; + return false; + } + // this loop will always exit because each call to AVCodecDecode() // reads from the input buffer and it has to either end at some time or // return a valid frame @@ -258,10 +265,8 @@ bool Video::NextFrame(unsigned char **_buffer) // in draining mode, we no longer read the input stream as it has ended if (!this->dataPtr->drainingMode) { - av_init_packet(&packet); - // read a frame from the input stream - ret = av_read_frame(this->dataPtr->formatCtx, &packet); + ret = av_read_frame(this->dataPtr->formatCtx, packet); if (ret < 0) { if (ret == AVERROR_EOF) @@ -277,10 +282,10 @@ bool Video::NextFrame(unsigned char **_buffer) return false; } } - else if (packet.stream_index != this->dataPtr->videoStream) + else if (packet->stream_index != this->dataPtr->videoStream) { // packet belongs to a stream we're not interested in (e.g. audio) - av_packet_unref(&packet); + av_packet_unref(packet); continue; } } @@ -288,12 +293,12 @@ bool Video::NextFrame(unsigned char **_buffer) // Process all the data in the frame ret = AVCodecDecode( this->dataPtr->codecCtx, this->dataPtr->avFrame, &frameAvailable, - this->dataPtr->drainingMode ? nullptr : &packet); + this->dataPtr->drainingMode ? nullptr : packet); if (ret == AVERROR_EOF) { if (!this->dataPtr->drainingMode) - AVPacketUnref(&packet); + av_packet_unref(packet); return false; } else if (ret < 0) @@ -303,7 +308,7 @@ bool Video::NextFrame(unsigned char **_buffer) // continue processing data } if (!this->dataPtr->drainingMode) - AVPacketUnref(&packet); + av_packet_unref(packet); } // processing the image if available diff --git a/av/src/VideoEncoder.cc b/av/src/VideoEncoder.cc index abbc2795e..cd6449df7 100644 --- a/av/src/VideoEncoder.cc +++ b/av/src/VideoEncoder.cc @@ -798,7 +798,6 @@ bool VideoEncoder::AddFrame(const unsigned char *_frame, #else AVPacket* avPacket = av_packet_alloc(); - av_init_packet(avPacket); avPacket->data = nullptr; avPacket->size = 0; @@ -892,7 +891,6 @@ bool VideoEncoder::Stop() if (ret >= 0) { AVPacket *avPacket = av_packet_alloc(); - av_init_packet(avPacket); avPacket->data = nullptr; avPacket->size = 0;