From 64322318dc7a02d74da3962eab8b48079a3e3343 Mon Sep 17 00:00:00 2001
From: philippegorley <philippe.gorley@savoirfairelinux.com>
Date: Fri, 15 Dec 2017 14:54:20 -0500
Subject: [PATCH] osx: bump ffmpeg

Bumps FFmpeg to match the other platforms. Modifies the acceleration
code to work under the new API.

Enables bitstream filters, as some of libavcodec's components, such as
VideoToolbox, use them.

Change-Id: I3a6cee2cf06881bba4602c0ed635ff45908e7b85
Reviewed-by: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
---
 configure.ac                             | 25 ++++++------------------
 contrib/src/ffmpeg/rules.mak             |  7 ++-----
 src/media/video/osxvideo/videotoolbox.h  |  5 +++++
 src/media/video/osxvideo/videotoolbox.mm | 15 +++++++++++---
 4 files changed, 25 insertions(+), 27 deletions(-)

diff --git a/configure.ac b/configure.ac
index b117399a59..62a55ef5a7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -425,29 +425,16 @@ AS_IF([test "x$with_restcpp" = "xyes"], [
 );
 
 dnl Check for libav
-AS_IF([test "${SYS}" = "darwin"], [
-    PKG_CHECK_MODULES(LIBAVUTIL, libavutil >= 55.58.100,, AC_MSG_ERROR([Missing libavutil development files]))
+PKG_CHECK_MODULES(LIBAVUTIL, libavutil >= 55.75.100,, AC_MSG_ERROR([Missing libavutil development files]))
 
-    PKG_CHECK_MODULES(LIBAVCODEC, libavcodec >= 53.89.100,, AC_MSG_ERROR([Missing libavcodec development files]))
-    LIBAVCODEC_CFLAGS="${LIBAVCODEC_CFLAGS} -D__STDC_CONSTANT_MACROS"
+PKG_CHECK_MODULES(LIBAVCODEC, libavcodec >= 53.106.101,, AC_MSG_ERROR([Missing libavcodec development files]))
+LIBAVCODEC_CFLAGS="${LIBAVCODEC_CFLAGS} -D__STDC_CONSTANT_MACROS"
 
-    PKG_CHECK_MODULES(LIBAVFORMAT, libavformat >= 57.71.100,, AC_MSG_ERROR([Missing libavformat development files]))
+PKG_CHECK_MODULES(LIBAVFORMAT, libavformat >= 57.81.100,, AC_MSG_ERROR([Missing libavformat development files]))
 
-    PKG_CHECK_MODULES(LIBAVDEVICE, libavdevice >= 57.6.100,, AC_MSG_ERROR([Missing libavdevice development files]))
+PKG_CHECK_MODULES(LIBAVDEVICE, libavdevice >= 57.8.101,, AC_MSG_ERROR([Missing libavdevice development files]))
 
-    PKG_CHECK_MODULES(LIBSWSCALE, libswscale >= 4.6.100,, AC_MSG_ERROR([Missing libswscale development files]))
-],[
-    PKG_CHECK_MODULES(LIBAVUTIL, libavutil >= 55.75.100,, AC_MSG_ERROR([Missing libavutil development files]))
-
-    PKG_CHECK_MODULES(LIBAVCODEC, libavcodec >= 53.106.101,, AC_MSG_ERROR([Missing libavcodec development files]))
-    LIBAVCODEC_CFLAGS="${LIBAVCODEC_CFLAGS} -D__STDC_CONSTANT_MACROS"
-
-    PKG_CHECK_MODULES(LIBAVFORMAT, libavformat >= 57.81.100,, AC_MSG_ERROR([Missing libavformat development files]))
-
-    PKG_CHECK_MODULES(LIBAVDEVICE, libavdevice >= 57.8.101,, AC_MSG_ERROR([Missing libavdevice development files]))
-
-    PKG_CHECK_MODULES(LIBSWSCALE, libswscale >= 4.7.103,, AC_MSG_ERROR([Missing libswscale development files]))
-]);
+PKG_CHECK_MODULES(LIBSWSCALE, libswscale >= 4.7.103,, AC_MSG_ERROR([Missing libswscale development files]))
 
 dnl Video is default-enabled
 AC_ARG_ENABLE([video], AS_HELP_STRING([--disable-video], [Disable video]))
diff --git a/contrib/src/ffmpeg/rules.mak b/contrib/src/ffmpeg/rules.mak
index e86403e256..fbe1e581e8 100644
--- a/contrib/src/ffmpeg/rules.mak
+++ b/contrib/src/ffmpeg/rules.mak
@@ -1,8 +1,4 @@
-ifndef HAVE_MACOSX
 FFMPEG_HASH := 18516d3e695980525bd9758dc7b8a8e36cd3f09e
-else
-FFMPEG_HASH := n3.3.3
-endif
 FFMPEG_URL := https://git.ffmpeg.org/gitweb/ffmpeg.git/snapshot/$(FFMPEG_HASH).tar.gz
 
 PKGS+=ffmpeg
@@ -24,6 +20,7 @@ FFMPEGCONF += \
 	--enable-gpl \
 	--enable-swscale \
 	--enable-protocols \
+	--enable-bsfs \
 	--disable-programs
 
 #enable muxers/demuxers
@@ -136,7 +133,7 @@ endif
 
 ifdef HAVE_MACOSX
 FFMPEGCONF += \
-	--disable-sdl \
+	--enable-avfoundation \
 	--enable-indev=avfoundation \
 	--enable-videotoolbox \
 	--enable-hwaccel=h263_videotoolbox \
diff --git a/src/media/video/osxvideo/videotoolbox.h b/src/media/video/osxvideo/videotoolbox.h
index a006f70abb..90ea360310 100644
--- a/src/media/video/osxvideo/videotoolbox.h
+++ b/src/media/video/osxvideo/videotoolbox.h
@@ -28,6 +28,7 @@
 
 extern "C" {
 #include <libavcodec/avcodec.h>
+#include <libavutil/hwcontext.h>
 #include <libavcodec/videotoolbox.h>
 #include <libavutil/imgutils.h>
 }
@@ -48,6 +49,10 @@ class VideoToolboxAccel : public HardwareAccel {
         bool init() override;
         int allocateBuffer(AVFrame* frame, int flags) override;
         void extractData(VideoFrame& input, VideoFrame& output) override;
+
+    private:
+        using AVBufferRefPtr = std::unique_ptr<AVBufferRef, std::function<void(AVBufferRef*)>>;
+        AVBufferRefPtr deviceBufferRef_;
 };
 
 }} // namespace ring::video
diff --git a/src/media/video/osxvideo/videotoolbox.mm b/src/media/video/osxvideo/videotoolbox.mm
index 2316154ec2..ab058d772a 100644
--- a/src/media/video/osxvideo/videotoolbox.mm
+++ b/src/media/video/osxvideo/videotoolbox.mm
@@ -35,8 +35,11 @@
 
 namespace ring { namespace video {
 
+static auto avBufferRefDeleter = [](AVBufferRef* buf){ av_buffer_unref(&buf); };
+
 VideoToolboxAccel::VideoToolboxAccel(const std::string name, const AVPixelFormat format)
     : HardwareAccel(name, format)
+    , deviceBufferRef_(nullptr, avBufferRefDeleter)
 {
 }
 
@@ -127,9 +130,14 @@ VideoToolboxAccel::extractData(VideoFrame& input, VideoFrame& output)
 bool
 VideoToolboxAccel::checkAvailability()
 {
-    // VideoToolbox is always present on Mac 10.8+ and iOS 8+
-    // VDA is always present on Mac 10.6.3+
-    return true;
+    AVBufferRef* hardwareDeviceCtx;
+    if (av_hwdevice_ctx_create(&hardwareDeviceCtx, AV_HWDEVICE_TYPE_VIDEOTOOLBOX, nullptr, nullptr, 0) == 0) {
+        deviceBufferRef_.reset(hardwareDeviceCtx);
+        return true;
+    }
+
+    av_buffer_unref(&hardwareDeviceCtx);
+    return false;
 }
 
 bool
@@ -137,6 +145,7 @@ VideoToolboxAccel::init()
 {
     if (av_videotoolbox_default_init(codecCtx_) >= 0) {
         RING_DBG("VideoToolbox decoder initialized");
+        codecCtx_->hw_device_ctx = av_buffer_ref(deviceBufferRef_.get());
         return true;
     } else {
         RING_ERR("Failed to initialize VideoToolbox decoder");
-- 
GitLab