Skip to content

Commit

Permalink
Added videotoolbox on macOS.
Browse files Browse the repository at this point in the history
Added using hardware encoding on macOS.
  • Loading branch information
ggarra13 committed Feb 19, 2024
1 parent a60bad5 commit 3d60559
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
7 changes: 5 additions & 2 deletions etc/SuperBuild/cmake/Modules/BuildFFmpeg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ else()
endif()
set(FFmpeg_CONFIGURE_ARGS
--prefix=${CMAKE_INSTALL_PREFIX}
--enable-pic
--pkg-config-flags=--static
--disable-programs
--disable-avfilter
Expand Down Expand Up @@ -105,12 +106,14 @@ else()
--disable-v4l2-m2m
--disable-vaapi
--disable-vdpau
--disable-videotoolbox
--enable-pic
${FFmpeg_CFLAGS}
${FFmpeg_CXXFLAGS}
${FFmpeg_OBJCFLAGS}
${FFmpeg_LDFLAGS})
if(NOT APPLE)
list(APPEND FFmpeg_CONFIGURE_ARGS
--disable-videotoolbox)
endif()
if(TLRENDER_FFMPEG_MINIMAL)
list(APPEND FFmpeg_CONFIGURE_ARGS
--disable-decoders
Expand Down
47 changes: 32 additions & 15 deletions lib/tlIO/FFmpegWrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,29 @@ namespace tl
{
AVPixelFormat o = AV_PIX_FMT_YUV420P;

// Most common formats
if (s == "YUV420P")
o = AV_PIX_FMT_YUV420P;
else if (s == "YUVA420P")
o = AV_PIX_FMT_YUVA420P;
else if (s == "YUV422P")
o = AV_PIX_FMT_YUV422P;
else if (s == "YUV444P")
o = AV_PIX_FMT_YUV444P;

// 10-bits pixel formats
else if (s == "YUV420P10LE")
o = AV_PIX_FMT_YUV420P10LE;
else if (s == "YUV422P10LE")
o = AV_PIX_FMT_YUV422P10LE;
else if (s == "YUV444P10LE")
o = AV_PIX_FMT_YUV444P10LE;
else if (s == "YUV420P10LE")
o = AV_PIX_FMT_YUV420P10LE;
else if (s == "YUV422P10LE")
o = AV_PIX_FMT_YUV422P10LE;
else if (s == "YUV444P10LE")
o = AV_PIX_FMT_YUV444P10LE;


// With alpha
else if (s == "YUVA420P")
o = AV_PIX_FMT_YUVA420P;
else if (s == "YUVA444P16LE")
o = AV_PIX_FMT_YUVA444P16LE;

// RGB formats
else if (s == "GBRP")
o = AV_PIX_FMT_GBRP;
else if (s == "GBRP10LE")
Expand Down Expand Up @@ -301,7 +302,7 @@ namespace tl
AVFrame* avFrame2 = nullptr;
SwsContext* swsContext = nullptr;
otime::RationalTime videoStartTime = time::invalidTime;
double speed = 24.F;
double avSpeed = 24.F;

// Audio
AVCodecContext* avAudioCodecContext = nullptr;
Expand Down Expand Up @@ -766,7 +767,7 @@ namespace tl
default: break;
}

p.speed = info.videoTime.duration().rate();
p.avSpeed = info.videoTime.duration().rate();

// Allow setting the speed if not saving audio
if (!info.audio.isValid() || avAudioCodecID == AV_CODEC_ID_NONE)
Expand All @@ -775,17 +776,31 @@ namespace tl
if (option != options.end())
{
std::stringstream ss(option->second);
ss >> p.speed;
ss >> p.avSpeed;
}
}

const AVCodec* avCodec = nullptr;
if (avCodecID == AV_CODEC_ID_VP9)
if (avCodecID == AV_CODEC_ID_H264)
{
avCodec = avcodec_find_encoder_by_name("h264_videotoolbox");
}
else if (avCodecID == AV_CODEC_ID_VP9)
{
#ifdef __APPLE__
avCodec = avcodec_find_encoder_by_name("videotoolbox_vp9");
#else
avCodec = avcodec_find_encoder_by_name("vp9_qsv");
#endif
if (!avCodec)
avCodec = avcodec_find_encoder_by_name("libvpx-vp9");
}
else if (avCodecID == AV_CODEC_ID_PRORES)
{
avCodec = avcodec_find_encoder_by_name("prores_videotoolbox");
if (!avCodec)
avCodec = avcodec_find_encoder_by_name("prores_ks");
}
if (!avCodec)
avCodec = avcodec_find_encoder(avCodecID);
if (!avCodec)
Expand Down Expand Up @@ -814,7 +829,7 @@ namespace tl
p.avCodecContext->width = videoInfo.size.w;
p.avCodecContext->height = videoInfo.size.h;
p.avCodecContext->sample_aspect_ratio = AVRational({ 1, 1 });
const auto rational = time::toRational(p.speed);
const auto rational = time::toRational(p.avSpeed);
p.avCodecContext->time_base = { rational.second, rational.first };
p.avCodecContext->framerate = { rational.first, rational.second };

Expand Down Expand Up @@ -863,6 +878,8 @@ namespace tl
}

p.avCodecContext->profile = avProfile;

// Get codec options from preset file
AVDictionary* codecOptions = NULL;

std::string presetFile;
Expand Down Expand Up @@ -1209,7 +1226,7 @@ namespace tl
p.avFrame->data,
p.avFrame->linesize);

const auto timeRational = time::toRational(p.speed);
const auto timeRational = time::toRational(p.avSpeed);
p.avFrame->pts = av_rescale_q(
time.value() - p.videoStartTime.value(),
{ timeRational.second, timeRational.first },
Expand Down

0 comments on commit 3d60559

Please sign in to comment.