agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 1 | /** |
Sébastien Blin | cb783e3 | 2021-02-12 11:34:10 -0500 | [diff] [blame] | 2 | * Copyright (C) 2020-2021 Savoir-faire Linux Inc. |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 3 | * |
| 4 | * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include "CoinCircleVideoSubscriber.h" |
| 22 | |
| 23 | extern "C" { |
| 24 | #include <libavutil/display.h> |
| 25 | } |
| 26 | #include <accel.h> |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 27 | #include <frameUtils.h> |
agsantos | 1bbc7cc | 2021-05-20 16:43:35 -0400 | [diff] [blame] | 28 | #include <frameScaler.h> |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 29 | #include <pluglog.h> |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <opencv2/imgproc.hpp> |
| 32 | |
| 33 | const std::string TAG = "CoinCircle"; |
| 34 | const char sep = separator(); |
| 35 | |
| 36 | namespace jami { |
| 37 | |
| 38 | CoinCircleVideoSubscriber::CoinCircleVideoSubscriber(const std::string& dataPath) |
| 39 | : path_ {dataPath} |
| 40 | {} |
| 41 | |
| 42 | CoinCircleVideoSubscriber::~CoinCircleVideoSubscriber() |
| 43 | { |
| 44 | std::ostringstream oss; |
| 45 | oss << "~CoinCircleMediaProcessor" << std::endl; |
| 46 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 47 | } |
| 48 | |
| 49 | void |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 50 | CoinCircleVideoSubscriber::update(jami::Observable<AVFrame*>*, AVFrame* const& pluginFrame) |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 51 | { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 52 | if (!pluginFrame) |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 53 | return; |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 54 | |
| 55 | //====================================================================================== |
| 56 | // GET FRAME ROTATION |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 57 | AVFrameSideData* side_data = av_frame_get_side_data(pluginFrame, AV_FRAME_DATA_DISPLAYMATRIX); |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 58 | |
| 59 | int angle {0}; |
| 60 | if (side_data) { |
| 61 | auto matrix_rotation = reinterpret_cast<int32_t*>(side_data->data); |
| 62 | angle = static_cast<int>(av_display_rotation_get(matrix_rotation)); |
| 63 | } |
| 64 | |
| 65 | //====================================================================================== |
| 66 | // GET RAW FRAME |
| 67 | // Use a non-const Frame |
| 68 | // Convert input frame to RGB |
agsantos | 1bbc7cc | 2021-05-20 16:43:35 -0400 | [diff] [blame] | 69 | uniqueFramePtr rgbFrame = {transferToMainMemory(pluginFrame, AV_PIX_FMT_NV12), frameFree}; |
| 70 | rgbFrame.reset(FrameScaler::convertFormat(rgbFrame.get(), AV_PIX_FMT_RGB24)); |
| 71 | if (!rgbFrame.get()) |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 72 | return; |
agsantos | 1bbc7cc | 2021-05-20 16:43:35 -0400 | [diff] [blame] | 73 | resultFrame = cv::Mat {rgbFrame->height, |
| 74 | rgbFrame->width, |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 75 | CV_8UC3, |
agsantos | 1bbc7cc | 2021-05-20 16:43:35 -0400 | [diff] [blame] | 76 | rgbFrame->data[0], |
| 77 | static_cast<size_t>(rgbFrame->linesize[0])}; |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 78 | |
| 79 | // First clone the frame as the original one is unusable because of |
| 80 | // linespace |
| 81 | processingFrame = resultFrame.clone(); |
| 82 | |
| 83 | if (firstRun) { |
| 84 | // we set were the circle will be draw. |
| 85 | int w = resultFrame.size().width; |
| 86 | int h = resultFrame.size().height; |
| 87 | radius = std::min(w, h) / 8; |
| 88 | circlePos.y = static_cast<int>(radius); |
| 89 | circlePos.x = static_cast<int>(radius); |
| 90 | firstRun = false; |
| 91 | } |
| 92 | |
| 93 | drawCoinCircle(angle); |
agsantos | 1bbc7cc | 2021-05-20 16:43:35 -0400 | [diff] [blame] | 94 | copyByLine(rgbFrame->linesize[0]); |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 95 | |
| 96 | //====================================================================================== |
| 97 | // REPLACE AVFRAME DATA WITH FRAME DATA |
agsantos | 1bbc7cc | 2021-05-20 16:43:35 -0400 | [diff] [blame] | 98 | rgbFrame.reset(FrameScaler::convertFormat(rgbFrame.get(), AV_PIX_FMT_YUV420P)); |
| 99 | moveFrom(pluginFrame, rgbFrame.get()); |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void |
| 103 | CoinCircleVideoSubscriber::drawCoinCircle(const int angle) |
| 104 | { |
| 105 | if (!processingFrame.empty()) { |
| 106 | rotateFrame(angle, processingFrame); |
| 107 | cv::circle(processingFrame, circlePos, radius, baseColor, cv::FILLED); |
| 108 | rotateFrame(-angle, processingFrame); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | CoinCircleVideoSubscriber::rotateFrame(const int angle, cv::Mat& frame) |
| 114 | { |
| 115 | if (angle == -90) |
| 116 | cv::rotate(frame, frame, cv::ROTATE_90_COUNTERCLOCKWISE); |
| 117 | else if (std::abs(angle) == 180) |
| 118 | cv::rotate(frame, frame, cv::ROTATE_180); |
| 119 | else if (angle == 90) |
| 120 | cv::rotate(frame, frame, cv::ROTATE_90_CLOCKWISE); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | CoinCircleVideoSubscriber::setColor(const std::string& color) |
| 125 | { |
| 126 | int r, g, b = 0; |
| 127 | std::sscanf(color.c_str(), "#%02x%02x%02x", &r, &g, &b); |
| 128 | baseColor = cv::Scalar(r, g, b); |
| 129 | Plog::log(Plog::LogPriority::INFO, TAG, "Color set to: " + color); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | CoinCircleVideoSubscriber::copyByLine(const int lineSize) |
| 134 | { |
| 135 | if (3 * processingFrame.cols == lineSize) { |
| 136 | std::memcpy(resultFrame.data, |
| 137 | processingFrame.data, |
| 138 | processingFrame.rows * processingFrame.cols * 3); |
| 139 | } else { |
| 140 | int rows = processingFrame.rows; |
| 141 | int offset = 0; |
| 142 | int frameOffset = 0; |
| 143 | for (int i = 0; i < rows; i++) { |
| 144 | std::memcpy(resultFrame.data + offset, processingFrame.data + frameOffset, lineSize); |
| 145 | offset += lineSize; |
| 146 | frameOffset += 3 * processingFrame.cols; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | CoinCircleVideoSubscriber::attached(jami::Observable<AVFrame*>* observable) |
| 153 | { |
| 154 | std::ostringstream oss; |
| 155 | oss << "::Attached ! " << std::endl; |
| 156 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 157 | observable_ = observable; |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | CoinCircleVideoSubscriber::detached(jami::Observable<AVFrame*>*) |
| 162 | { |
| 163 | firstRun = true; |
| 164 | observable_ = nullptr; |
| 165 | std::ostringstream oss; |
| 166 | oss << "::Detached()" << std::endl; |
| 167 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | CoinCircleVideoSubscriber::detach() |
| 172 | { |
| 173 | if (observable_) { |
| 174 | firstRun = true; |
| 175 | std::ostringstream oss; |
| 176 | oss << "::Calling detach()" << std::endl; |
| 177 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 178 | observable_->detach(this); |
| 179 | } |
| 180 | } |
| 181 | } // namespace jami |