agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2020 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 | |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 21 | #pragma once |
| 22 | extern "C" { |
| 23 | #include <libavutil/avutil.h> |
| 24 | #include <libavutil/frame.h> |
| 25 | #include <libavutil/pixfmt.h> |
| 26 | #include <libswscale/swscale.h> |
| 27 | } |
| 28 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 29 | // STL |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 30 | #include <memory> |
| 31 | #include <functional> |
| 32 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 33 | using FrameUniquePtr = std::unique_ptr<AVFrame, void (*)(AVFrame*)>; |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 34 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 35 | class FrameScaler |
| 36 | { |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 37 | public: |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 38 | FrameScaler() |
| 39 | : ctx_(nullptr) |
| 40 | , mode_(SWS_FAST_BILINEAR) |
| 41 | {} |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * @brief scaleConvert |
| 45 | * Scales an av frame accoding to the desired width height/height |
| 46 | * Converts the frame to another format if the desiredFromat is different from the input PixelFormat |
| 47 | * @param input |
| 48 | * @param desiredWidth |
| 49 | * @param desiredHeight |
| 50 | * @param desiredFormat |
| 51 | * @return |
| 52 | */ |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 53 | FrameUniquePtr scaleConvert(const AVFrame* input, |
| 54 | const size_t desiredWidth, |
| 55 | const size_t desiredHeight, |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 56 | const AVPixelFormat desiredFormat) |
| 57 | { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 58 | FrameUniquePtr output {av_frame_alloc(), [](AVFrame* frame) { |
| 59 | if (frame) { |
| 60 | av_frame_free(&frame); |
| 61 | } |
| 62 | }}; |
| 63 | if (input) { |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 64 | output->width = static_cast<int>(desiredWidth); |
| 65 | output->height = static_cast<int>(desiredHeight); |
| 66 | output->format = static_cast<int>(desiredFormat); |
| 67 | |
| 68 | auto output_frame = output.get(); |
| 69 | |
| 70 | if (av_frame_get_buffer(output_frame, 0)) |
| 71 | throw std::bad_alloc(); |
| 72 | |
| 73 | ctx_ = sws_getCachedContext(ctx_, |
| 74 | input->width, |
| 75 | input->height, |
| 76 | static_cast<AVPixelFormat>(input->format), |
| 77 | output_frame->width, |
| 78 | output_frame->height, |
| 79 | static_cast<AVPixelFormat>(output_frame->format), |
| 80 | mode_, |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 81 | nullptr, |
| 82 | nullptr, |
| 83 | nullptr); |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 84 | if (!ctx_) { |
| 85 | throw std::bad_alloc(); |
| 86 | } |
| 87 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 88 | sws_scale(ctx_, |
| 89 | input->data, |
| 90 | input->linesize, |
| 91 | 0, |
| 92 | input->height, |
| 93 | output_frame->data, |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 94 | output_frame->linesize); |
| 95 | } |
| 96 | |
| 97 | return output; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @brief convertFormat |
| 102 | * @param input |
| 103 | * @param pix |
| 104 | * @return |
| 105 | */ |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 106 | FrameUniquePtr convertFormat(const AVFrame* input, AVPixelFormat pix) |
| 107 | { |
| 108 | return input ? scaleConvert(input, |
| 109 | static_cast<size_t>(input->width), |
| 110 | static_cast<size_t>(input->height), |
| 111 | pix) |
| 112 | : std::unique_ptr<AVFrame, void (*)(AVFrame*)> {nullptr, [](AVFrame* frame) { |
| 113 | (void) frame; |
| 114 | }}; |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @brief moveFrom |
| 119 | * @param dst |
| 120 | * @param src |
| 121 | */ |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 122 | void moveFrom(AVFrame* dst, AVFrame* src) |
| 123 | { |
| 124 | if (dst && src) { |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 125 | av_frame_unref(dst); |
| 126 | av_frame_move_ref(dst, src); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | protected: |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame^] | 131 | SwsContext* ctx_; |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 132 | int mode_; |
| 133 | }; |