From f6d001e3a3d3732b474225f2a0a45468cf96a100 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Mon, 27 Aug 2018 15:13:34 -0400
Subject: [PATCH] video: use ffmpeg' AVPixelFormat

Change-Id: I019e66f12cf7d704f832bce457b63ca9aaefa15d
Reviewed-by: Philippe Gorley <philippe.gorley@savoirfairelinux.com>
---
 src/media/libav_utils.cpp                     | 21 -------------------
 src/media/libav_utils.h                       |  3 ---
 src/media/media_buffer.cpp                    | 10 ++++-----
 src/media/media_decoder.cpp                   |  2 +-
 src/media/media_encoder.cpp                   |  2 +-
 .../video/androidvideo/video_device_impl.cpp  |  6 +++---
 .../video/iosvideo/video_device_impl.cpp      |  6 +++---
 src/media/video/sinkclient.cpp                |  8 +++----
 .../video/uwpvideo/video_device_impl.cpp      |  8 +++----
 src/media/video/video_base.h                  | 12 ++++-------
 src/media/video/video_mixer.cpp               |  4 ++--
 11 files changed, 26 insertions(+), 56 deletions(-)

diff --git a/src/media/libav_utils.cpp b/src/media/libav_utils.cpp
index 293ad7fe98..e963921f75 100644
--- a/src/media/libav_utils.cpp
+++ b/src/media/libav_utils.cpp
@@ -165,27 +165,6 @@ void ring_avcodec_init()
     std::call_once(already_called, init_once);
 }
 
-
-int libav_pixel_format(int fmt)
-{
-    switch (fmt) {
-        case video::VIDEO_PIXFMT_BGRA: return AV_PIX_FMT_BGRA;
-        case video::VIDEO_PIXFMT_RGBA: return AV_PIX_FMT_RGBA;
-        case video::VIDEO_PIXFMT_YUYV422: return AV_PIX_FMT_YUYV422;
-        case video::VIDEO_PIXFMT_YUV420P: return AV_PIX_FMT_YUV420P;
-        case video::VIDEO_PIXFMT_NV21: return AV_PIX_FMT_NV21;
-    }
-    return fmt;
-}
-
-int ring_pixel_format(int fmt)
-{
-    switch (fmt) {
-        case AV_PIX_FMT_YUYV422: return video::VIDEO_PIXFMT_YUYV422;
-    }
-    return fmt;
-}
-
 void ring_url_split(const char *url,
                    char *hostname, size_t hostname_size, int *port,
                    char *path, size_t path_size)
diff --git a/src/media/libav_utils.h b/src/media/libav_utils.h
index 0cce603692..d61c7b08d7 100644
--- a/src/media/libav_utils.h
+++ b/src/media/libav_utils.h
@@ -32,9 +32,6 @@ namespace ring { namespace libav_utils {
 
     void ring_avcodec_init();
 
-    int libav_pixel_format(int fmt);
-    int ring_pixel_format(int fmt);
-
     const char *const DEFAULT_H264_PROFILE_LEVEL_ID = "profile-level-id=428029";
     const char *const MAX_H264_PROFILE_LEVEL_ID = "profile-level-id=640034";
 
diff --git a/src/media/media_buffer.cpp b/src/media/media_buffer.cpp
index 9357b823d9..160c32c13f 100644
--- a/src/media/media_buffer.cpp
+++ b/src/media/media_buffer.cpp
@@ -70,7 +70,7 @@ VideoFrame::size() const noexcept
 int
 VideoFrame::format() const noexcept
 {
-    return libav_utils::ring_pixel_format(frame_->format);
+    return frame_->format;
 }
 
 int
@@ -88,7 +88,7 @@ VideoFrame::height() const noexcept
 void
 VideoFrame::setGeometry(int format, int width, int height) noexcept
 {
-    frame_->format = libav_utils::libav_pixel_format(format);
+    frame_->format = format;
     frame_->width = width;
     frame_->height = height;
 }
@@ -96,14 +96,13 @@ VideoFrame::setGeometry(int format, int width, int height) noexcept
 void
 VideoFrame::reserve(int format, int width, int height)
 {
-    auto libav_format = (AVPixelFormat)libav_utils::libav_pixel_format(format);
     auto libav_frame = frame_.get();
 
     if (allocated_) {
         // nothing to do if same properties
         if (width == libav_frame->width
             and height == libav_frame->height
-            and libav_format == libav_frame->format)
+            and format == libav_frame->format)
 #if USE_OLD_AVU
         avpicture_free((AVPicture *) libav_frame);
 #else
@@ -167,8 +166,7 @@ VideoFrame::operator =(const VideoFrame& src)
 std::size_t
 videoFrameSize(int format, int width, int height)
 {
-    return av_image_get_buffer_size((AVPixelFormat)libav_utils::libav_pixel_format(format),
-                                    width, height, 1);
+    return av_image_get_buffer_size((AVPixelFormat)format, width, height, 1);
 }
 
 #endif // RING_VIDEO
diff --git a/src/media/media_decoder.cpp b/src/media/media_decoder.cpp
index bbd39a6aa4..992b80051a 100644
--- a/src/media/media_decoder.cpp
+++ b/src/media/media_decoder.cpp
@@ -444,7 +444,7 @@ MediaDecoder::getTimeBase() const
 }
 
 int MediaDecoder::getPixelFormat() const
-{ return libav_utils::ring_pixel_format(decoderCtx_->pix_fmt); }
+{ return decoderCtx_->pix_fmt; }
 
 void
 MediaDecoder::writeToRingBuffer(const AudioFrame& decodedFrame,
diff --git a/src/media/media_encoder.cpp b/src/media/media_encoder.cpp
index 69155b9e6b..c00f9b8c9a 100644
--- a/src/media/media_encoder.cpp
+++ b/src/media/media_encoder.cpp
@@ -278,7 +278,7 @@ MediaEncoder::addStream(const SystemCodecInfo& systemCodecInfo, std::string para
         // allocate buffers for both scaled (pre-encoder) and encoded frames
         const int width = encoderCtx->width;
         const int height = encoderCtx->height;
-        const int format = libav_utils::ring_pixel_format((int)encoderCtx->pix_fmt);
+        const int format = encoderCtx->pix_fmt;
         scaledFrameBufferSize_ = videoFrameSize(format, width, height);
         if (scaledFrameBufferSize_ <= AV_INPUT_BUFFER_MIN_SIZE)
             throw MediaEncoderException("buffer too small");
diff --git a/src/media/video/androidvideo/video_device_impl.cpp b/src/media/video/androidvideo/video_device_impl.cpp
index 2dd11762d5..3f567cbad4 100644
--- a/src/media/video/androidvideo/video_device_impl.cpp
+++ b/src/media/video/androidvideo/video_device_impl.cpp
@@ -41,12 +41,12 @@ namespace ring { namespace video {
 struct android_fmt {
     int             code;
     std::string     name;
-    enum VideoPixelFormat ring_format;
+    int ring_format;
 };
 
 static const std::array<android_fmt, 2> and_formats {
-    android_fmt { 17,           "NV21",     VIDEO_PIXFMT_NV21    },
-    android_fmt { 842094169,    "YUV420",   VIDEO_PIXFMT_YUV420P },
+    android_fmt { 17,           "NV21",     AV_PIX_FMT_NV21    },
+    android_fmt { 842094169,    "YUV420",   AV_PIX_FMT_YUV420P },
 };
 
 class VideoDeviceImpl {
diff --git a/src/media/video/iosvideo/video_device_impl.cpp b/src/media/video/iosvideo/video_device_impl.cpp
index e52a342f9b..131df2de00 100644
--- a/src/media/video/iosvideo/video_device_impl.cpp
+++ b/src/media/video/iosvideo/video_device_impl.cpp
@@ -37,9 +37,9 @@ typedef struct
 
 static const std::array<ios_fmt, 4> ios_formats
 {
-    ios_fmt { "RGBA",       VIDEO_PIXFMT_RGBA       },
-    ios_fmt { "BGRA",       VIDEO_PIXFMT_BGRA       },
-    ios_fmt { "YUV420P",    VIDEO_PIXFMT_YUV420P    }
+    ios_fmt { "RGBA",       AV_PIX_FMT_RGBA       },
+    ios_fmt { "BGRA",       AV_PIX_FMT_BGRA       },
+    ios_fmt { "YUV420P",    AV_PIX_FMT_YUV420P    }
 };
 
 class VideoDeviceImpl
diff --git a/src/media/video/sinkclient.cpp b/src/media/video/sinkclient.cpp
index 809a1b9610..4f7724dbc6 100644
--- a/src/media/video/sinkclient.cpp
+++ b/src/media/video/sinkclient.cpp
@@ -224,7 +224,7 @@ ShmHolder::renderFrame(VideoFrame& src) noexcept
 {
     const auto width = src.width();
     const auto height = src.height();
-    const auto format = VIDEO_PIXFMT_BGRA;
+    const auto format = AV_PIX_FMT_BGRA;
     const auto frameSize = videoFrameSize(format, width, height);
 
     if (!resizeArea(frameSize)) {
@@ -344,16 +344,16 @@ SinkClient::update(Observable<std::shared_ptr<VideoFrame>>* /*obs*/,
         const int width = f.width();
         const int height = f.height();
 #if defined(__ANDROID__) || (defined(__APPLE__) && !TARGET_OS_IPHONE)
-        const int format = VIDEO_PIXFMT_RGBA;
+        const int format = AV_PIX_FMT_RGBA;
 #else
-        const int format = VIDEO_PIXFMT_BGRA;
+        const int format = AV_PIX_FMT_BGRA;
 #endif
 
         const auto bytes = videoFrameSize(format, width, height);
 
         if (bytes > 0) {
             if (auto buffer_ptr = target_.pull(bytes)) {
-                buffer_ptr->format = libav_utils::libav_pixel_format(format);
+                buffer_ptr->format = format;
                 buffer_ptr->width = width;
                 buffer_ptr->height = height;
                 dst.setFromMemory(buffer_ptr->ptr, format, width, height);
diff --git a/src/media/video/uwpvideo/video_device_impl.cpp b/src/media/video/uwpvideo/video_device_impl.cpp
index f22ac2ece7..6feaeea5a4 100644
--- a/src/media/video/uwpvideo/video_device_impl.cpp
+++ b/src/media/video/uwpvideo/video_device_impl.cpp
@@ -39,10 +39,10 @@ typedef struct
 // have all formats map to bgra
 static const std::array<uwp_fmt, 4> uwp_formats
 {
-    uwp_fmt { "MJPG",   VIDEO_PIXFMT_BGRA   },
-    uwp_fmt { "RGB24",  VIDEO_PIXFMT_BGRA   },
-    uwp_fmt { "NV12",   VIDEO_PIXFMT_BGRA   },
-    uwp_fmt { "YUY2",   VIDEO_PIXFMT_BGRA   }
+    uwp_fmt { "MJPG",   AV_PIX_FMT_BGRA   },
+    uwp_fmt { "RGB24",  AV_PIX_FMT_BGRA   },
+    uwp_fmt { "NV12",   AV_PIX_FMT_BGRA   },
+    uwp_fmt { "YUY2",   AV_PIX_FMT_BGRA   }
 };
 
 class VideoDeviceImpl
diff --git a/src/media/video/video_base.h b/src/media/video/video_base.h
index 1f85b298e5..5b39096c6a 100644
--- a/src/media/video/video_base.h
+++ b/src/media/video/video_base.h
@@ -36,6 +36,10 @@
 #include <mutex>
 #include <ciso646> // fix windows compiler bug
 
+extern "C" {
+#include <libavutil/pixfmt.h>
+}
+
 struct AVPacket;
 struct AVDictionary;
 
@@ -49,14 +53,6 @@ class VideoFrame;
 
 namespace ring { namespace video {
 
-enum VideoPixelFormat {
-    VIDEO_PIXFMT_BGRA = -1,
-    VIDEO_PIXFMT_YUV420P = -2,
-    VIDEO_PIXFMT_YUYV422 = -3,
-    VIDEO_PIXFMT_RGBA = -4,
-    VIDEO_PIXFMT_NV21 = -5,
-};
-
 template <typename T> class Observer;
 template <typename T> class Observable;
 
diff --git a/src/media/video/video_mixer.cpp b/src/media/video/video_mixer.cpp
index 4de6b17626..f864d9c849 100644
--- a/src/media/video/video_mixer.cpp
+++ b/src/media/video/video_mixer.cpp
@@ -130,7 +130,7 @@ VideoMixer::process()
 
     VideoFrame& output = getNewFrame();
     try {
-        output.reserve(VIDEO_PIXFMT_YUYV422, width_, height_);
+        output.reserve(AV_PIX_FMT_YUYV422, width_, height_);
     } catch (const std::bad_alloc& e) {
         RING_ERR("VideoFrame::allocBuffer() failed");
         return;
@@ -231,6 +231,6 @@ VideoMixer::getHeight() const
 
 int
 VideoMixer::getPixelFormat() const
-{ return VIDEO_PIXFMT_YUYV422; }
+{ return AV_PIX_FMT_YUYV422; }
 
 }} // namespace ring::video
-- 
GitLab