Skip to content

Commit

Permalink
Replace deprecated FFmpeg APIs to support FFmpeg 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeWang000000 committed Jun 23, 2022
1 parent 084ce47 commit 5d9b551
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions src/spek-audio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AudioFileImpl : public AudioFile
int channels;
double duration;

AVPacket packet;
AVPacket *packet;
int offset;
AVFrame *frame;
int buffer_len;
Expand Down Expand Up @@ -99,7 +99,7 @@ std::unique_ptr<AudioFile> Audio::open(const std::string& file_name, int stream)

AVStream *avstream = nullptr;
AVCodecParameters *codecpar = nullptr;
AVCodec *codec = nullptr;
const AVCodec *codec = nullptr;
if (!error) {
avstream = format_context->streams[audio_stream];
codecpar = avstream->codecpar;
Expand Down Expand Up @@ -156,7 +156,7 @@ std::unique_ptr<AudioFile> Audio::open(const std::string& file_name, int stream)
if (bits_per_sample) {
bit_rate = 0;
}
channels = codecpar->channels;
channels = codecpar->ch_layout.nb_channels;

if (avstream->duration != AV_NOPTS_VALUE) {
duration = avstream->duration * av_q2d(avstream->time_base);
Expand Down Expand Up @@ -214,9 +214,9 @@ AudioFileImpl::AudioFileImpl(
sample_rate(sample_rate),
bits_per_sample(bits_per_sample), streams(streams), channels(channels), duration(duration)
{
av_init_packet(&this->packet);
this->packet.data = nullptr;
this->packet.size = 0;
this->packet = av_packet_alloc();
this->packet->data = nullptr;
this->packet->size = 0;
this->offset = 0;
this->frame = av_frame_alloc();
this->buffer_len = 0;
Expand All @@ -234,11 +234,11 @@ AudioFileImpl::~AudioFileImpl()
if (this->frame) {
av_frame_free(&this->frame);
}
if (this->packet.data) {
this->packet.data -= this->offset;
this->packet.size += this->offset;
if (this->packet->data) {
this->packet->data -= this->offset;
this->packet->size += this->offset;
this->offset = 0;
av_packet_unref(&this->packet);
av_packet_unref(this->packet);
}
if (this->format_context) {
avformat_close_input(&this->format_context);
Expand All @@ -262,23 +262,23 @@ int AudioFileImpl::read()
}

for (;;) {
while (this->packet.size > 0) {
while (this->packet->size > 0) {
av_frame_unref(this->frame);
int got_frame = 0;
int len = avcodec_decode_audio4(
this->codec_context, this->frame, &got_frame, &this->packet
);
if (len < 0) {
// Error, skip the frame.
int ret;
ret = avcodec_send_packet(this->codec_context, this->packet);
if (ret < 0) {
// Error sending a packet for decoding, skip the frame.
break;
}
this->packet.data += len;
this->packet.size -= len;
this->offset += len;
if (!got_frame) {
// No data yet, get more frames.
continue;
ret = avcodec_receive_frame(this->codec_context, this->frame);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
// Error during decoding, skip the frame.
break;
}
int len = this->packet->size;
this->packet->data += len;
this->packet->size -= len;
this->offset += len;
// We have data, return it and come back for more later.
int samples = this->frame->nb_samples;
int channels = this->channels;
Expand Down Expand Up @@ -333,19 +333,19 @@ int AudioFileImpl::read()
}
return buffer_len;
}
if (this->packet.data) {
this->packet.data -= this->offset;
this->packet.size += this->offset;
if (this->packet->data) {
this->packet->data -= this->offset;
this->packet->size += this->offset;
this->offset = 0;
av_packet_unref(&this->packet);
av_packet_unref(this->packet);
}

int res = 0;
while ((res = av_read_frame(this->format_context, &this->packet)) >= 0) {
if (this->packet.stream_index == this->audio_stream) {
while ((res = av_read_frame(this->format_context, this->packet)) >= 0) {
if (this->packet->stream_index == this->audio_stream) {
break;
}
av_packet_unref(&this->packet);
av_packet_unref(this->packet);
}
if (res < 0) {
// End of file or error.
Expand Down

1 comment on commit 5d9b551

@MikeWang000000
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Fix alexkay/spek#171 (DSD Files can be decoded by FFmpeg now), alexkay/spek#218, withmorten/spek-alternative#15

Please sign in to comment.