Skip to content

Commit

Permalink
build: upgrade ffmpeg to 7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Feb 11, 2025
1 parent 922e808 commit 15e5679
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .github/actions/prepare_macos_tooling/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ runs:
# Install to separate staging paths for all architectures in
# preparation for fusing universal libraries in a follow-up step.
cd "$RUNNER_TEMP"
git clone --depth 1 --branch "n4.4.1" https://github.com/FFmpeg/FFmpeg
git clone --depth 1 --branch "n7.1" https://github.com/FFmpeg/FFmpeg
cd FFmpeg
# Common FFmpeg configure options
Expand Down
53 changes: 26 additions & 27 deletions src/libtrx/engine/audio_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,11 @@ static bool M_Convert(const int32_t sample_id)
};

struct {
int32_t src_format;
int32_t src_channels;
int32_t src_sample_rate;
int32_t dst_format;
int32_t dst_channels;
int32_t dst_sample_rate;
struct {
int32_t format;
AVChannelLayout ch_layout;
int32_t sample_rate;
} src, dst;
SwrContext *ctx;
} swr = {};

Expand Down Expand Up @@ -279,17 +278,18 @@ static bool M_Convert(const int32_t sample_id)
goto cleanup;
}

if (!swr.ctx) {
swr.src_sample_rate = av.codec_ctx->sample_rate;
swr.src_channels = av.codec_ctx->channels;
swr.src_format = av.codec_ctx->sample_fmt;
swr.dst_sample_rate = AUDIO_WORKING_RATE;
swr.dst_channels = 1;
swr.dst_format = Audio_GetAVAudioFormat(AUDIO_WORKING_FORMAT);
swr.ctx = swr_alloc_set_opts(
swr.ctx, swr.dst_channels, swr.dst_format, swr.dst_sample_rate,
swr.src_channels, swr.src_format, swr.src_sample_rate, 0, 0);
if (!swr.ctx) {
if (swr.ctx == nullptr) {
swr.src.sample_rate = av.codec_ctx->sample_rate;
swr.src.ch_layout = av.codec_ctx->ch_layout;
swr.src.format = av.codec_ctx->sample_fmt;
swr.dst.sample_rate = AUDIO_WORKING_RATE;
av_channel_layout_default(&swr.dst.ch_layout, 1);
swr.dst.format = Audio_GetAVAudioFormat(AUDIO_WORKING_FORMAT);
swr_alloc_set_opts2(
&swr.ctx, &swr.dst.ch_layout, swr.dst.format,
swr.dst.sample_rate, &swr.src.ch_layout, swr.src.format,
swr.src.sample_rate, 0, 0);
if (swr.ctx == nullptr) {
av_packet_unref(av.packet);
error_code = AVERROR(ENOMEM);
goto cleanup;
Expand Down Expand Up @@ -319,15 +319,15 @@ static bool M_Convert(const int32_t sample_id)
const int32_t out_samples =
swr_get_out_samples(swr.ctx, av.frame->nb_samples);
av_samples_alloc(
&out_buffer, nullptr, swr.dst_channels, out_samples,
swr.dst_format, 1);
&out_buffer, nullptr, swr.dst.ch_layout.nb_channels,
out_samples, swr.dst.format, 1);
int32_t resampled_size = swr_convert(
swr.ctx, &out_buffer, out_samples,
(const uint8_t **)av.frame->data, av.frame->nb_samples);
while (resampled_size > 0) {
int32_t out_buffer_size = av_samples_get_buffer_size(
nullptr, swr.dst_channels, resampled_size, swr.dst_format,
1);
nullptr, swr.dst.ch_layout.nb_channels, resampled_size,
swr.dst.format, 1);

if (out_buffer_size > 0) {
working_buffer = Memory_Realloc(
Expand All @@ -351,10 +351,10 @@ static bool M_Convert(const int32_t sample_id)
av_packet_unref(av.packet);
}

int32_t sample_format_bytes = av_get_bytes_per_sample(swr.dst_format);
sample->num_samples =
working_buffer_size / sample_format_bytes / swr.dst_channels;
sample->channels = swr.src_channels;
int32_t sample_format_bytes = av_get_bytes_per_sample(swr.dst.format);
sample->num_samples = working_buffer_size / sample_format_bytes
/ swr.dst.ch_layout.nb_channels;
sample->channels = swr.src.ch_layout.nb_channels;
sample->sample_data = working_buffer;
result = true;

Expand Down Expand Up @@ -396,8 +396,7 @@ static bool M_Convert(const int32_t sample_id)
}

if (av.codec_ctx) {
avcodec_close(av.codec_ctx);
av_freep(&av.codec_ctx);
avcodec_free_context(&av.codec_ctx);
}

if (av.format_ctx) {
Expand Down
61 changes: 30 additions & 31 deletions src/libtrx/engine/audio_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ typedef struct {
} av;

struct {
int32_t src_format;
int32_t src_channels;
int32_t src_sample_rate;
int32_t dst_format;
int32_t dst_channels;
int32_t dst_sample_rate;
struct {
int32_t format;
AVChannelLayout ch_layout;
int32_t sample_rate;
} src, dst;
SwrContext *ctx;
} swr;

Expand Down Expand Up @@ -147,17 +146,18 @@ static bool M_EnqueueFrame(AUDIO_STREAM_SOUND *stream)
int32_t error_code;

if (!stream->swr.ctx) {
stream->swr.src_sample_rate = stream->av.codec_ctx->sample_rate;
stream->swr.src_channels = stream->av.codec_ctx->channels;
stream->swr.src_format = stream->av.codec_ctx->sample_fmt;
stream->swr.dst_sample_rate = AUDIO_WORKING_RATE;
stream->swr.dst_channels = AUDIO_WORKING_CHANNELS;
stream->swr.dst_format = Audio_GetAVAudioFormat(AUDIO_WORKING_FORMAT);
stream->swr.ctx = swr_alloc_set_opts(
stream->swr.ctx, Audio_GetAVChannelLayout(stream->swr.dst_channels),
stream->swr.dst_format, stream->swr.dst_sample_rate,
Audio_GetAVChannelLayout(stream->swr.src_channels),
stream->swr.src_format, stream->swr.src_sample_rate, 0, 0);
stream->swr.src.sample_rate = stream->av.codec_ctx->sample_rate;
stream->swr.src.ch_layout = stream->av.codec_ctx->ch_layout;
stream->swr.src.format = stream->av.codec_ctx->sample_fmt;
stream->swr.dst.sample_rate = AUDIO_WORKING_RATE;
av_channel_layout_default(
&stream->swr.dst.ch_layout, AUDIO_WORKING_CHANNELS);
stream->swr.dst.format = Audio_GetAVAudioFormat(AUDIO_WORKING_FORMAT);
swr_alloc_set_opts2(
&stream->swr.ctx, &stream->swr.dst.ch_layout,
stream->swr.dst.format, stream->swr.dst.sample_rate,
&stream->swr.src.ch_layout, stream->swr.src.format,
stream->swr.src.sample_rate, 0, 0);
if (!stream->swr.ctx) {
av_packet_unref(stream->av.packet);
error_code = AVERROR(ENOMEM);
Expand Down Expand Up @@ -189,8 +189,8 @@ static bool M_EnqueueFrame(AUDIO_STREAM_SOUND *stream)
const int32_t out_samples =
swr_get_out_samples(stream->swr.ctx, stream->av.frame->nb_samples);
av_samples_alloc(
&out_buffer, nullptr, stream->swr.dst_channels, out_samples,
stream->swr.dst_format, 1);
&out_buffer, nullptr, stream->swr.dst.ch_layout.nb_channels,
out_samples, stream->swr.dst.format, 1);
int32_t resampled_size = swr_convert(
stream->swr.ctx, &out_buffer, out_samples,
(const uint8_t **)stream->av.frame->data,
Expand All @@ -199,8 +199,8 @@ static bool M_EnqueueFrame(AUDIO_STREAM_SOUND *stream)
size_t out_pos = 0;
while (resampled_size > 0) {
const size_t out_buffer_size = av_samples_get_buffer_size(
nullptr, stream->swr.dst_channels, resampled_size,
stream->swr.dst_format, 1);
nullptr, stream->swr.dst.ch_layout.nb_channels, resampled_size,
stream->swr.dst.format, 1);

if (out_pos + out_buffer_size > m_DecodeBufferCapacity) {
m_DecodeBufferCapacity = out_pos + out_buffer_size;
Expand Down Expand Up @@ -322,8 +322,8 @@ static bool M_InitialiseFromPath(int32_t sound_id, const char *file_path)

M_DecodeFrame(stream);

int32_t sdl_sample_rate = stream->av.codec_ctx->sample_rate;
int32_t sdl_channels = stream->av.codec_ctx->channels;
const int32_t sdl_sample_rate = stream->av.codec_ctx->sample_rate;
const int32_t sdl_channels = stream->av.codec_ctx->ch_layout.nb_channels;

stream->is_read_done = false;
stream->is_used = true;
Expand Down Expand Up @@ -474,14 +474,12 @@ bool Audio_Stream_Close(int32_t sound_id)
AUDIO_STREAM_SOUND *stream = &m_Streams[sound_id];

if (stream->av.codec_ctx) {
avcodec_close(stream->av.codec_ctx);

// XXX: potential libav bug - avcodec_close should free this info
if (stream->av.codec_ctx->extradata != nullptr) {
av_freep(&stream->av.codec_ctx->extradata);
}

av_free(stream->av.codec_ctx);
avcodec_free_context(&stream->av.codec_ctx);
stream->av.codec_ctx = nullptr;
}

Expand Down Expand Up @@ -613,13 +611,15 @@ void Audio_Stream_Mix(float *dst_buffer, size_t len)
const float *src_ptr = &m_MixBuffer[0];
float *dst_ptr = dst_buffer;

if (stream->av.codec_ctx->channels == AUDIO_WORKING_CHANNELS) {
const int32_t channels =
stream->av.codec_ctx->ch_layout.nb_channels;
if (channels == AUDIO_WORKING_CHANNELS) {
for (int32_t s = 0; s < samples_gotten; s++) {
for (int32_t c = 0; c < AUDIO_WORKING_CHANNELS; c++) {
*dst_ptr++ += *src_ptr++ * stream->volume;
}
}
} else if (stream->av.codec_ctx->channels == 1) {
} else if (channels == 1) {
for (int32_t s = 0; s < samples_gotten; s++) {
for (int32_t c = 0; c < AUDIO_WORKING_CHANNELS; c++) {
*dst_ptr++ += *src_ptr * stream->volume;
Expand All @@ -630,11 +630,10 @@ void Audio_Stream_Mix(float *dst_buffer, size_t len)
for (int32_t s = 0; s < samples_gotten; s++) {
// downmix to mono
float src_sample = 0.0f;
for (int32_t i = 0; i < stream->av.codec_ctx->channels;
i++) {
for (int32_t i = 0; i < channels; i++) {
src_sample += *src_ptr++;
}
src_sample /= (float)stream->av.codec_ctx->channels;
src_sample /= (float)channels;
for (int32_t c = 0; c < AUDIO_WORKING_CHANNELS; c++) {
*dst_ptr++ += src_sample * stream->volume;
}
Expand Down
11 changes: 5 additions & 6 deletions src/libtrx/engine/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ static bool M_Init(const char *const path, IMAGE_READER_CONTEXT *const ctx)
AVStream *video_stream = nullptr;
for (unsigned int i = 0; i < ctx->format_ctx->nb_streams; i++) {
AVStream *current_stream = ctx->format_ctx->streams[i];
if (current_stream->codecpar == nullptr) {
continue;
}
if (current_stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream = current_stream;
break;
Expand Down Expand Up @@ -165,9 +168,7 @@ static void M_Free(IMAGE_READER_CONTEXT *const ctx)
}

if (ctx->codec_ctx != nullptr) {
avcodec_close(ctx->codec_ctx);
av_free(ctx->codec_ctx);
ctx->codec_ctx = nullptr;
avcodec_free_context(&ctx->codec_ctx);
}

if (ctx->format_ctx != nullptr) {
Expand Down Expand Up @@ -490,9 +491,7 @@ bool Image_SaveToFile(const IMAGE *const image, const char *const path)
}

if (codec) {
avcodec_close(codec_ctx);
av_free(codec_ctx);
codec_ctx = nullptr;
avcodec_free_context(&codec_ctx);
}

if (frame) {
Expand Down
Loading

0 comments on commit 15e5679

Please sign in to comment.