1 /*****************************************************************************
2
3 ProcAlpha.cpp
4 Author: Laurent de Soras, 2021
5
6 --- Legal stuff ---
7
8 This program is free software. It comes without any warranty, to
9 the extent permitted by applicable law. You can redistribute it
10 and/or modify it under the terms of the Do What The Fuck You Want
11 To Public License, Version 2, as published by Sam Hocevar. See
12 http://www.wtfpl.net/ for more details.
13
14 *Tab=3***********************************************************************/
15
16
17
18 #if defined (_MSC_VER)
19 #pragma warning (1 : 4130 4223 4705 4706)
20 #pragma warning (4 : 4355 4786 4800)
21 #endif
22
23
24
25 /*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
26
27 #include "avsutl/CsPlane.h"
28 #include "avsutl/fnc.h"
29 #include "fmtcavs/CpuOpt.h"
30 #include "fmtcavs/fnc.h"
31 #include "fmtcavs/ProcAlpha.h"
32 #include "fmtcl/fnc.h"
33 #include "avisynth.h"
34
35 #include <cassert>
36
37
38
39 namespace fmtcavs
40 {
41
42
43
44 /*\\\ PUBLIC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
45
46
47
48 // w and h in pixels
ProcAlpha(FmtAvs fmt_dst,FmtAvs fmt_src,int w,int h,const CpuOpt & cpu_opt)49 ProcAlpha::ProcAlpha (FmtAvs fmt_dst, FmtAvs fmt_src, int w, int h, const CpuOpt &cpu_opt)
50 : _dst_a_flag (fmt_dst.has_alpha ())
51 , _src_a_flag (fmt_src.has_alpha ())
52 , _dst_res (fmt_dst.get_bitdepth ())
53 , _src_res (fmt_dst.get_bitdepth ())
54 , _splfmt_dst (conv_bitdepth_to_splfmt (_dst_res))
55 , _splfmt_src (conv_bitdepth_to_splfmt (_src_res))
56 , _w (w)
57 , _h (h)
58 , _scale_info ()
59 , _sse2_flag (cpu_opt.has_sse2 ())
60 , _avx2_flag (cpu_opt.has_avx2 ())
61 {
62 if (_dst_a_flag && _src_a_flag)
63 {
64 const auto col_fam_dst = fmt_dst.get_col_fam ();
65 const auto col_fam_src = fmt_src.get_col_fam ();
66 fmtcl::compute_fmt_mac_cst (
67 _scale_info._gain,
68 _scale_info._add_cst,
69 fmtcl::PicFmt { _splfmt_dst, _dst_res, col_fam_dst, true },
70 fmtcl::PicFmt { _splfmt_src, _src_res, col_fam_src, true },
71 avsutl::CsPlane::_plane_index_alpha
72 );
73 }
74 }
75
76
77
process_plane(::PVideoFrame & dst_sptr,::PVideoFrame & src_sptr) const78 void ProcAlpha::process_plane (::PVideoFrame &dst_sptr, ::PVideoFrame &src_sptr) const
79 {
80 if (_dst_a_flag)
81 {
82 uint8_t * dst_ptr = dst_sptr->GetWritePtr (::PLANAR_A);
83 const int dst_stride = dst_sptr->GetPitch (::PLANAR_A);
84
85 // Copy
86 if (_src_a_flag)
87 {
88 const uint8_t* src_ptr = src_sptr->GetReadPtr (::PLANAR_A);
89 const int src_stride = src_sptr->GetPitch (::PLANAR_A);
90
91 fmtcl::BitBltConv blitter (_sse2_flag, _avx2_flag);
92 blitter.bitblt (
93 _splfmt_dst, _dst_res, dst_ptr, dst_stride,
94 _splfmt_src, _src_res, src_ptr, src_stride,
95 _w, _h,
96 &_scale_info
97 );
98 }
99
100 // Fill with full opacity
101 else
102 {
103 switch (_splfmt_dst)
104 {
105 case fmtcl::SplFmt_FLOAT:
106 avsutl::fill_block (dst_ptr, 1.f, dst_stride, _w, _h);
107 break;
108 case fmtcl::SplFmt_INT16:
109 {
110 const auto v = uint16_t ((1 << _dst_res) - 1);
111 avsutl::fill_block (dst_ptr, v, dst_stride, _w, _h);
112 }
113 break;
114 case fmtcl::SplFmt_INT8:
115 avsutl::fill_block (dst_ptr, uint8_t (0xFF), dst_stride, _w, _h);
116 break;
117 default:
118 // Other formats not supported
119 assert (false);
120 break;
121 }
122 }
123 }
124 }
125
126
127
128 /*\\\ PROTECTED \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
129
130
131
132 /*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
133
134
135
136 } // namespace fmtcavs
137
138
139
140 /*\\\ EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
141