Skip to content

Commit

Permalink
Fix av_* API usage for deprecations (#220)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <michael@openrobotics.org>
Co-authored-by: Steve Peters <scpeters@openrobotics.org>
Co-authored-by: Louise Poubel <louise@openrobotics.org>
  • Loading branch information
3 people authored May 14, 2021
1 parent eb408f0 commit 153f320
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
20 changes: 13 additions & 7 deletions av/src/AudioDecoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 14 additions & 9 deletions av/src/Video.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,23 +245,28 @@ 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

// 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)
Expand All @@ -277,23 +282,23 @@ 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;
}
}

// 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)
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions av/src/VideoEncoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 153f320

Please sign in to comment.