agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2020 Savoir-faire Linux Inc. |
| 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 "CenterCircleVideoSubscriber.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 | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 28 | #include <pluglog.h> |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <opencv2/imgproc.hpp> |
| 31 | |
| 32 | const std::string TAG = "CenterCircle"; |
| 33 | const char sep = separator(); |
| 34 | |
| 35 | namespace jami { |
| 36 | |
| 37 | CenterCircleVideoSubscriber::CenterCircleVideoSubscriber(const std::string& dataPath) |
| 38 | : path_ {dataPath} |
| 39 | {} |
| 40 | |
| 41 | CenterCircleVideoSubscriber::~CenterCircleVideoSubscriber() |
| 42 | { |
| 43 | std::ostringstream oss; |
| 44 | oss << "~CenterCircleMediaProcessor" << std::endl; |
| 45 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 46 | } |
| 47 | |
| 48 | void |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 49 | CenterCircleVideoSubscriber::update(jami::Observable<AVFrame*>*, AVFrame* const& pluginFrame) |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 50 | { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 51 | if (!pluginFrame) |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 52 | return; |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 53 | |
| 54 | //====================================================================================== |
| 55 | // GET FRAME ROTATION |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 56 | AVFrameSideData* side_data = av_frame_get_side_data(pluginFrame, AV_FRAME_DATA_DISPLAYMATRIX); |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 57 | |
| 58 | int angle {0}; |
| 59 | if (side_data) { |
| 60 | auto matrix_rotation = reinterpret_cast<int32_t*>(side_data->data); |
| 61 | angle = static_cast<int>(av_display_rotation_get(matrix_rotation)); |
| 62 | } |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 63 | delete side_data; |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 64 | |
| 65 | //====================================================================================== |
| 66 | // GET RAW FRAME |
| 67 | // Use a non-const Frame |
| 68 | // Convert input frame to RGB |
| 69 | int inputHeight = pluginFrame->height; |
| 70 | int inputWidth = pluginFrame->width; |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 71 | AVFrame* temp = transferToMainMemory(pluginFrame, AV_PIX_FMT_NV12); |
| 72 | AVFrame* bgrFrame = scaler.convertFormat(temp, AV_PIX_FMT_RGB24); |
| 73 | av_frame_unref(temp); |
| 74 | av_frame_free(&temp); |
| 75 | if (!bgrFrame) |
| 76 | return; |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 77 | resultFrame = cv::Mat {bgrFrame->height, |
| 78 | bgrFrame->width, |
| 79 | CV_8UC3, |
| 80 | bgrFrame->data[0], |
| 81 | static_cast<size_t>(bgrFrame->linesize[0])}; |
| 82 | |
| 83 | // First clone the frame as the original one is unusable because of |
| 84 | // linespace |
| 85 | processingFrame = resultFrame.clone(); |
| 86 | |
| 87 | if (firstRun) { |
| 88 | // we set were the circle will be draw. |
| 89 | circlePos.y = static_cast<int>(inputHeight / 2); |
| 90 | circlePos.x = static_cast<int>(inputWidth / 2); |
| 91 | int w = resultFrame.size().width; |
| 92 | int h = resultFrame.size().height; |
| 93 | radius = std::min(w, h) / 8; |
| 94 | firstRun = false; |
| 95 | } |
| 96 | |
| 97 | drawCenterCircle(); |
| 98 | copyByLine(bgrFrame->linesize[0]); |
| 99 | |
| 100 | //====================================================================================== |
| 101 | // REPLACE AVFRAME DATA WITH FRAME DATA |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 102 | if (bgrFrame->data[0]) { |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 103 | uint8_t* frameData = bgrFrame->data[0]; |
| 104 | if (angle == 90 || angle == -90) { |
| 105 | std::memmove(frameData, |
| 106 | resultFrame.data, |
| 107 | static_cast<size_t>(pluginFrame->width * pluginFrame->height * 3) |
| 108 | * sizeof(uint8_t)); |
| 109 | } |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 110 | |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 111 | av_frame_copy_props(bgrFrame, pluginFrame); |
| 112 | moveFrom(pluginFrame, bgrFrame); |
| 113 | } |
| 114 | av_frame_unref(bgrFrame); |
| 115 | av_frame_free(&bgrFrame); |
agsantos | d09cc6d | 2020-11-06 17:34:46 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
| 119 | CenterCircleVideoSubscriber::setColor(const std::string& color) |
| 120 | { |
| 121 | int r, g, b = 0; |
| 122 | std::sscanf(color.c_str(), "#%02x%02x%02x", &r, &g, &b); |
| 123 | baseColor = cv::Scalar(r, g, b); |
| 124 | Plog::log(Plog::LogPriority::INFO, TAG, "Color set to: " + color); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | CenterCircleVideoSubscriber::copyByLine(const int lineSize) |
| 129 | { |
| 130 | if (3 * processingFrame.cols == lineSize) { |
| 131 | std::memcpy(resultFrame.data, |
| 132 | processingFrame.data, |
| 133 | processingFrame.rows * processingFrame.cols * 3); |
| 134 | } else { |
| 135 | int rows = processingFrame.rows; |
| 136 | int offset = 0; |
| 137 | int frameOffset = 0; |
| 138 | for (int i = 0; i < rows; i++) { |
| 139 | std::memcpy(resultFrame.data + offset, processingFrame.data + frameOffset, lineSize); |
| 140 | offset += lineSize; |
| 141 | frameOffset += 3 * processingFrame.cols; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | CenterCircleVideoSubscriber::drawCenterCircle() |
| 148 | { |
| 149 | if (!processingFrame.empty()) { |
| 150 | cv::circle(processingFrame, circlePos, radius, baseColor, cv::FILLED); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | CenterCircleVideoSubscriber::attached(jami::Observable<AVFrame*>* observable) |
| 156 | { |
| 157 | std::ostringstream oss; |
| 158 | oss << "::Attached ! " << std::endl; |
| 159 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 160 | observable_ = observable; |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | CenterCircleVideoSubscriber::detached(jami::Observable<AVFrame*>*) |
| 165 | { |
| 166 | firstRun = true; |
| 167 | observable_ = nullptr; |
| 168 | std::ostringstream oss; |
| 169 | oss << "::Detached()" << std::endl; |
| 170 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | CenterCircleVideoSubscriber::detach() |
| 175 | { |
| 176 | if (observable_) { |
| 177 | firstRun = true; |
| 178 | std::ostringstream oss; |
| 179 | oss << "::Calling detach()" << std::endl; |
| 180 | Plog::log(Plog::LogPriority::INFO, TAG, oss.str()); |
| 181 | observable_->detach(this); |
| 182 | } |
| 183 | } |
| 184 | } // namespace jami |