agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [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 | ef9f856 | 2020-06-25 16:43:25 -0400 | [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 | |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 21 | #ifndef FRAMESCALER_H |
| 22 | #define FRAMESCALER_H |
| 23 | |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 24 | extern "C" { |
| 25 | #include <libavutil/avutil.h> |
| 26 | #include <libavutil/frame.h> |
| 27 | #include <libavutil/pixfmt.h> |
| 28 | #include <libswscale/swscale.h> |
| 29 | } |
| 30 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 31 | class FrameScaler |
| 32 | { |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 33 | public: |
agsantos | 4bb4bc5 | 2021-03-08 14:21:45 -0500 | [diff] [blame] | 34 | FrameScaler() {} |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * @brief scaleConvert |
| 38 | * Scales an av frame accoding to the desired width height/height |
| 39 | * Converts the frame to another format if the desiredFromat is different from the input PixelFormat |
| 40 | * @param input |
| 41 | * @param desiredWidth |
| 42 | * @param desiredHeight |
| 43 | * @param desiredFormat |
| 44 | * @return |
| 45 | */ |
agsantos | 4bb4bc5 | 2021-03-08 14:21:45 -0500 | [diff] [blame] | 46 | static AVFrame* scaleConvert(const AVFrame* input, |
| 47 | const size_t desiredWidth, |
| 48 | const size_t desiredHeight, |
| 49 | const AVPixelFormat desiredFormat) |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 50 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 51 | if (input) { |
agsantos | 4bb4bc5 | 2021-03-08 14:21:45 -0500 | [diff] [blame] | 52 | SwsContext* ctx_ = nullptr; |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 53 | AVFrame* output = av_frame_alloc(); |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 54 | output->width = static_cast<int>(desiredWidth); |
| 55 | output->height = static_cast<int>(desiredHeight); |
| 56 | output->format = static_cast<int>(desiredFormat); |
| 57 | |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 58 | if (av_frame_get_buffer(output, 0)) { |
| 59 | av_frame_unref(output); |
| 60 | av_frame_free(&output); |
| 61 | return nullptr; |
| 62 | } |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 63 | |
| 64 | ctx_ = sws_getCachedContext(ctx_, |
| 65 | input->width, |
| 66 | input->height, |
| 67 | static_cast<AVPixelFormat>(input->format), |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 68 | output->width, |
| 69 | output->height, |
| 70 | static_cast<AVPixelFormat>(output->format), |
agsantos | 4bb4bc5 | 2021-03-08 14:21:45 -0500 | [diff] [blame] | 71 | SWS_FAST_BILINEAR, |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 72 | nullptr, |
| 73 | nullptr, |
| 74 | nullptr); |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 75 | if (!ctx_) { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 76 | av_frame_unref(output); |
| 77 | av_frame_free(&output); |
| 78 | return nullptr; |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 79 | } |
| 80 | |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 81 | if (sws_scale(ctx_, |
agsantos | 4bb4bc5 | 2021-03-08 14:21:45 -0500 | [diff] [blame] | 82 | input->data, |
| 83 | input->linesize, |
| 84 | 0, |
| 85 | input->height, |
| 86 | output->data, |
| 87 | output->linesize) |
| 88 | <= 0) { |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 89 | av_frame_unref(output); |
| 90 | av_frame_free(&output); |
agsantos | 4bb4bc5 | 2021-03-08 14:21:45 -0500 | [diff] [blame] | 91 | sws_freeContext(ctx_); |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 92 | return nullptr; |
| 93 | } |
agsantos | 4bb4bc5 | 2021-03-08 14:21:45 -0500 | [diff] [blame] | 94 | |
| 95 | sws_freeContext(ctx_); |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 96 | return output; |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 97 | } |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 98 | return nullptr; |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @brief convertFormat |
| 103 | * @param input |
| 104 | * @param pix |
| 105 | * @return |
| 106 | */ |
agsantos | 4bb4bc5 | 2021-03-08 14:21:45 -0500 | [diff] [blame] | 107 | static AVFrame* convertFormat(const AVFrame* input, AVPixelFormat pix) |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 108 | { |
| 109 | return input ? scaleConvert(input, |
| 110 | static_cast<size_t>(input->width), |
| 111 | static_cast<size_t>(input->height), |
| 112 | pix) |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 113 | : nullptr; |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 114 | } |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 115 | }; |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 116 | |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 117 | #endif // FRAMESCALER_H |