Skip to content

Commit

Permalink
Update to FFmpeg 7 (#578)
Browse files Browse the repository at this point in the history
### Summarize your change.
Upgrade ffmpeg to 7.1.
Fix deprecation errors and warnings when building with FFmpeg 7.1.

### Linked issues
n/a

### Summarize your change.
Maintenance Update

### Describe what you have tested and on which operating system.
MacOS

Signed-off-by: Will Wira <[email protected]>
  • Loading branch information
williamwira committed Oct 4, 2024
1 parent e551a0d commit aaf8b26
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
24 changes: 12 additions & 12 deletions cmake/dependencies/ffmpeg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ SET(_target
)

SET(_version
"n6.1.1"
"n7.1"
)
SET(_download_url
"https://github.com/FFmpeg/FFmpeg/archive/refs/tags/${_version}.zip"
)

SET(_download_hash
"6dfc27fcb6da6f653c6ec025c2cd9b00"
"a7a85ec05c9bc3aeefee12743899d8ab"
)

SET(_base_dir
Expand Down Expand Up @@ -67,19 +67,19 @@ ENDIF()

IF(RV_TARGET_DARWIN)
SET(_ffmpeg_avutil_lib_name
${CMAKE_SHARED_LIBRARY_PREFIX}avutil.58${CMAKE_SHARED_LIBRARY_SUFFIX}
${CMAKE_SHARED_LIBRARY_PREFIX}avutil.59${CMAKE_SHARED_LIBRARY_SUFFIX}
)
SET(_ffmpeg_swresample_lib_name
${CMAKE_SHARED_LIBRARY_PREFIX}swresample.4${CMAKE_SHARED_LIBRARY_SUFFIX}
${CMAKE_SHARED_LIBRARY_PREFIX}swresample.5${CMAKE_SHARED_LIBRARY_SUFFIX}
)
SET(_ffmpeg_swscale_lib_name
${CMAKE_SHARED_LIBRARY_PREFIX}swscale.7${CMAKE_SHARED_LIBRARY_SUFFIX}
${CMAKE_SHARED_LIBRARY_PREFIX}swscale.8${CMAKE_SHARED_LIBRARY_SUFFIX}
)
SET(_ffmpeg_avcodec_lib_name
${CMAKE_SHARED_LIBRARY_PREFIX}avcodec.60${CMAKE_SHARED_LIBRARY_SUFFIX}
${CMAKE_SHARED_LIBRARY_PREFIX}avcodec.61${CMAKE_SHARED_LIBRARY_SUFFIX}
)
SET(_ffmpeg_avformat_lib_name
${CMAKE_SHARED_LIBRARY_PREFIX}avformat.60${CMAKE_SHARED_LIBRARY_SUFFIX}
${CMAKE_SHARED_LIBRARY_PREFIX}avformat.61${CMAKE_SHARED_LIBRARY_SUFFIX}
)
ELSEIF(RV_TARGET_LINUX)
SET(_ffmpeg_avutil_lib_name
Expand All @@ -99,19 +99,19 @@ ELSEIF(RV_TARGET_LINUX)
)
ELSEIF(RV_TARGET_WINDOWS)
SET(_ffmpeg_avutil_lib_name
${CMAKE_SHARED_LIBRARY_PREFIX}avutil-58${CMAKE_SHARED_LIBRARY_SUFFIX}
${CMAKE_SHARED_LIBRARY_PREFIX}avutil-59${CMAKE_SHARED_LIBRARY_SUFFIX}
)
SET(_ffmpeg_swresample_lib_name
${CMAKE_SHARED_LIBRARY_PREFIX}swresample-4${CMAKE_SHARED_LIBRARY_SUFFIX}
${CMAKE_SHARED_LIBRARY_PREFIX}swresample-5${CMAKE_SHARED_LIBRARY_SUFFIX}
)
SET(_ffmpeg_swscale_lib_name
${CMAKE_SHARED_LIBRARY_PREFIX}swscale-7${CMAKE_SHARED_LIBRARY_SUFFIX}
${CMAKE_SHARED_LIBRARY_PREFIX}swscale-8${CMAKE_SHARED_LIBRARY_SUFFIX}
)
SET(_ffmpeg_avcodec_lib_name
${CMAKE_SHARED_LIBRARY_PREFIX}avcodec-60${CMAKE_SHARED_LIBRARY_SUFFIX}
${CMAKE_SHARED_LIBRARY_PREFIX}avcodec-61${CMAKE_SHARED_LIBRARY_SUFFIX}
)
SET(_ffmpeg_avformat_lib_name
${CMAKE_SHARED_LIBRARY_PREFIX}avformat-60${CMAKE_SHARED_LIBRARY_SUFFIX}
${CMAKE_SHARED_LIBRARY_PREFIX}avformat-61${CMAKE_SHARED_LIBRARY_SUFFIX}
)
ENDIF()

Expand Down
65 changes: 34 additions & 31 deletions src/lib/image/MovieFFMpeg/MovieFFMpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1518,12 +1518,12 @@ MovieFFMpegReader::snagOrientation(VideoTrack* track)
rotation = (rotEntry) ? atoi(rotEntry->value) : 0;

// If rotation metadata not in metadata, try to get it from side data
if (!rotEntry && videoStream->nb_side_data > 0)
if (!rotEntry && videoCodecContext->nb_coded_side_data > 0)
{
double rotationFromSideData = 0;
for (int i = 0; i < videoStream->nb_side_data; ++i)
for (int i = 0; i < videoCodecContext->nb_coded_side_data; ++i)
{
const AVPacketSideData *sd = &videoStream->side_data[i];
const AVPacketSideData *sd = &videoStream->codecpar->coded_side_data[i];
if (sd->type == AV_PKT_DATA_DISPLAYMATRIX)
{
rotationFromSideData = av_display_rotation_get((int32_t *)sd->data);
Expand All @@ -1535,7 +1535,7 @@ MovieFFMpegReader::snagOrientation(VideoTrack* track)

// Setting rotation
char charRotation[5]; // Expecting a number between -360 and 360 (inclusive)
sprintf(charRotation, "%lu", rotation);
sprintf(charRotation, "%d", rotation);
if (av_dict_set(
&videoStream->metadata, "rotate", charRotation, 0) < 0)
{
Expand Down Expand Up @@ -1989,7 +1989,7 @@ MovieFFMpegReader::initializeAll()
else if (tsStream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
{
string tsLang = streamLang(i);
int numChans = tsStream->codecpar->channels;
int numChans = tsStream->codecpar->ch_layout.nb_channels;

// Keep a sorted set of channel counts for each language
chLangMap[tsLang].insert(numChans);
Expand Down Expand Up @@ -2094,6 +2094,7 @@ MovieFFMpegReader::initializeAll()
m_info.views.push_back(trackName.str());
ostringstream trk;
trk << "Track" << i;

snagMetadata(tsStream->metadata, trk.str(), &m_info.proxy);
}
else
Expand All @@ -2115,6 +2116,7 @@ MovieFFMpegReader::initializeAll()
m_audioTracks.push_back(track);
ostringstream trk;
trk << "Track" << i;

snagMetadata(tsStream->metadata, trk.str(), &m_info.proxy);
}
else
Expand Down Expand Up @@ -2385,11 +2387,11 @@ MovieFFMpegReader::initializeAudio()
TWK_THROW_EXC_STREAM("Audio sample rate must match for each track!");
}
audioSampleRate = audioCodecContext->sample_rate;
if (numChannels != 0 && numChannels != audioCodecContext->channels)
if (numChannels != 0 && numChannels != audioCodecContext->ch_layout.nb_channels)
{
TWK_THROW_EXC_STREAM("Audio channel count must match for each track!");
}
numChannels = audioCodecContext->channels;
numChannels = audioCodecContext->ch_layout.nb_channels;
track->numChannels = numChannels;

double timebase = av_q2d(audioStream->time_base);
Expand All @@ -2413,7 +2415,7 @@ MovieFFMpegReader::initializeAudio()
<< " timebase: " << timebase);

// Get the input source channels
uint64_t layout = audioCodecContext->channel_layout;
AVChannelLayout layout = audioCodecContext->ch_layout;
audioChannels = idAudioChannels(layout, numChannels);

// XXX Assume this thread will never decode audio
Expand Down Expand Up @@ -2657,15 +2659,15 @@ MovieFFMpegReader::isImageFormat(const char* format)
}

ChannelsVector
MovieFFMpegReader::idAudioChannels(uint64_t layout, int numChannels)
MovieFFMpegReader::idAudioChannels(AVChannelLayout layout, int numChannels)
{
uint64_t unmasked;
ChannelsVector chans;

// Layout can have max 64 channels; one bit per channel. See avutil/channel_layout.h
for (int i = 0; i < 64 && chans.size() < numChannels; i++)
{
unmasked = layout & ((uint64_t) 1 << i);
unmasked = layout.u.mask & (1ULL << i);

if (!unmasked) continue;

Expand Down Expand Up @@ -2699,13 +2701,15 @@ MovieFFMpegReader::idAudioChannels(uint64_t layout, int numChannels)
default: continue;
}

const char *chName, *chDesc;
chName = av_get_channel_name(unmasked);
chDesc = av_get_channel_description(unmasked);

DBL (DB_AUDIO, "Audio ch " << (chans.size()-1)
<< " is: '" << chName
<< "-" << chDesc);
AVChannel ch = av_channel_layout_channel_from_index(&layout, i);
char chName[64], chDesc[256];
if (av_channel_name(chName, sizeof(chName), ch) == 0 &&
av_channel_description(chDesc, sizeof(chDesc), ch) == 0)
{
DBL (DB_AUDIO, "Audio ch " << (chans.size()-1)
<< " is: '" << chName
<< "-" << chDesc);
}
}

// If we don't know any of the channels then assume the most common layout
Expand Down Expand Up @@ -4079,16 +4083,15 @@ MovieFFMpegWriter::addTrack(bool isVideo, string codec,
}
else
{
avStream->time_base.num = 1;
avStream->time_base.den = m_info.audioSampleRate;
avCodecContext->time_base.num = 1;
avCodecContext->time_base.den = m_info.audioSampleRate;
avCodecContext->codec_id = avCodec->id;
avCodecContext->codec_type = AVMEDIA_TYPE_AUDIO;
avCodecContext->sample_rate = m_info.audioSampleRate;
avCodecContext->channels = m_info.audioChannels.size();
avCodecContext->channel_layout =
av_get_default_channel_layout(avCodecContext->channels);
avStream->time_base.num = 1;
avStream->time_base.den = m_info.audioSampleRate;
avCodecContext->time_base.num = 1;
avCodecContext->time_base.den = m_info.audioSampleRate;
avCodecContext->codec_id = avCodec->id;
avCodecContext->codec_type = AVMEDIA_TYPE_AUDIO;
avCodecContext->sample_rate = m_info.audioSampleRate;
avCodecContext->ch_layout.nb_channels = m_info.audioChannels.size();
av_channel_layout_default(&(avCodecContext->ch_layout), avCodecContext->ch_layout.nb_channels);

if (m_parameters.find("sample_fmt") != m_parameters.end())
{
Expand Down Expand Up @@ -4147,7 +4150,7 @@ MovieFFMpegWriter::addTrack(bool isVideo, string codec,
m_audioFrameSize = (avCodecContext->frame_size) ?
avCodecContext->frame_size : 2048;
if (m_audioSamples) av_freep(&m_audioSamples);
m_audioSamples = av_malloc(avCodecContext->channels * m_audioFrameSize *
m_audioSamples = av_malloc(avCodecContext->ch_layout.nb_channels * m_audioFrameSize *
av_get_bytes_per_sample(avCodecContext->sample_fmt));
}
}
Expand Down Expand Up @@ -4553,7 +4556,7 @@ MovieFFMpegWriter::fillAudio(Movie* inMovie, double overflow, bool lastPass)
AVStream* audioStream = m_avFormatContext->streams[track->number];
AVCodecContext* audioCodecContext = track->avCodecContext;
AVSampleFormat audioFormat = audioCodecContext->sample_fmt;
int audioChannels = audioCodecContext->channels;
int audioChannels = audioCodecContext->ch_layout.nb_channels;
int sampleSize = av_get_bytes_per_sample(audioFormat);
int totalOutputSize = audioBuffer.size() * audioChannels * sampleSize;
bool planar = av_sample_fmt_is_planar(audioFormat);
Expand Down Expand Up @@ -4612,8 +4615,8 @@ MovieFFMpegWriter::fillAudio(Movie* inMovie, double overflow, bool lastPass)

track->audioFrame->nb_samples = nsamps;
track->audioFrame->format = audioFormat;
track->audioFrame->channels = audioCodecContext->channels;
track->audioFrame->channel_layout = audioCodecContext->channel_layout;
track->audioFrame->ch_layout.nb_channels = audioCodecContext->ch_layout.nb_channels;
track->audioFrame->ch_layout = audioCodecContext->ch_layout;
avcodec_fill_audio_frame(track->audioFrame, audioChannels,
audioFormat, (uint8_t*)m_audioSamples, totalOutputSize, 0);

Expand Down
4 changes: 3 additions & 1 deletion src/lib/image/MovieFFMpeg/MovieFFMpeg/MovieFFMpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// AVClass Forward Declaration Placeholders
//


class AVChannelLayout;
class AVClass;
class AVCodecContext;
class AVDictionary;
Expand Down Expand Up @@ -212,7 +214,7 @@ class MovieFFMpegReader : public MovieReader
// Audio Methods
//

ChannelsVector idAudioChannels(uint64_t layout, int numChannels);
ChannelsVector idAudioChannels(AVChannelLayout layout, int numChannels);
SampleTime oneTrackAudioFillBuffer(AudioTrack* track);
int decodeAudioForBuffer(AudioTrack* track);
template <typename T> int translateAVAudio(AudioTrack* track, double max, int offset);
Expand Down

0 comments on commit aaf8b26

Please sign in to comment.