blob: 7ee271cd16b8f7e9cb83bd8d8339abae8399d85b [file] [log] [blame]
agsantos5aa39652020-08-11 18:18:04 -04001/**
2 * Copyright (C) 2020 Savoir-faire Linux Inc.
agsantosef9f8562020-06-25 16:43:25 -04003 *
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
AGS555248cd2020-05-13 11:15:19 -040021#pragma once
22extern "C" {
23#include <libavutil/avutil.h>
24#include <libavutil/frame.h>
25#include <libavutil/pixfmt.h>
26#include <libswscale/swscale.h>
27}
28
agsantosac1940d2020-09-17 10:18:40 -040029// STL
AGS555248cd2020-05-13 11:15:19 -040030#include <memory>
31#include <functional>
32
agsantosac1940d2020-09-17 10:18:40 -040033using FrameUniquePtr = std::unique_ptr<AVFrame, void (*)(AVFrame*)>;
AGS555248cd2020-05-13 11:15:19 -040034
agsantosac1940d2020-09-17 10:18:40 -040035class FrameScaler
36{
AGS555248cd2020-05-13 11:15:19 -040037public:
agsantosac1940d2020-09-17 10:18:40 -040038 FrameScaler()
39 : ctx_(nullptr)
40 , mode_(SWS_FAST_BILINEAR)
41 {}
AGS555248cd2020-05-13 11:15:19 -040042
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 */
agsantosac1940d2020-09-17 10:18:40 -040053 FrameUniquePtr scaleConvert(const AVFrame* input,
54 const size_t desiredWidth,
55 const size_t desiredHeight,
agsantos5aa39652020-08-11 18:18:04 -040056 const AVPixelFormat desiredFormat)
57 {
agsantosac1940d2020-09-17 10:18:40 -040058 FrameUniquePtr output {av_frame_alloc(), [](AVFrame* frame) {
59 if (frame) {
60 av_frame_free(&frame);
61 }
62 }};
63 if (input) {
AGS555248cd2020-05-13 11:15:19 -040064 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_,
agsantosac1940d2020-09-17 10:18:40 -040081 nullptr,
82 nullptr,
83 nullptr);
AGS555248cd2020-05-13 11:15:19 -040084 if (!ctx_) {
85 throw std::bad_alloc();
86 }
87
agsantosac1940d2020-09-17 10:18:40 -040088 sws_scale(ctx_,
89 input->data,
90 input->linesize,
91 0,
92 input->height,
93 output_frame->data,
AGS555248cd2020-05-13 11:15:19 -040094 output_frame->linesize);
95 }
96
97 return output;
98 }
99
100 /**
101 * @brief convertFormat
102 * @param input
103 * @param pix
104 * @return
105 */
agsantosac1940d2020-09-17 10:18:40 -0400106 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 }};
AGS555248cd2020-05-13 11:15:19 -0400115 }
116
117 /**
118 * @brief moveFrom
119 * @param dst
120 * @param src
121 */
agsantosac1940d2020-09-17 10:18:40 -0400122 void moveFrom(AVFrame* dst, AVFrame* src)
123 {
124 if (dst && src) {
AGS555248cd2020-05-13 11:15:19 -0400125 av_frame_unref(dst);
126 av_frame_move_ref(dst, src);
127 }
128 }
129
130protected:
agsantosac1940d2020-09-17 10:18:40 -0400131 SwsContext* ctx_;
AGS555248cd2020-05-13 11:15:19 -0400132 int mode_;
133};