1 /*****************************************************************************
2 
3         Resample.h
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 #pragma once
19 #if ! defined (fmtcavs_Resample_HEADER_INCLUDED)
20 #define fmtcavs_Resample_HEADER_INCLUDED
21 
22 
23 
24 /*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
25 
26 #include "fmtcavs/FmtAvs.h"
27 #include "fmtcl/ChromaPlacement.h"
28 #include "fmtcl/FilterResize.h"
29 #include "fmtcl/InterlacingType.h"
30 #include "fmtcl/KernelData.h"
31 #include "fmtcl/ResamplePlaneData.h"
32 #include "fmtcl/ResampleSpecPlane.h"
33 #include "fmtcl/ResampleUtil.h"
34 #include "avsutl/PlaneProcCbInterface.h"
35 #include "avsutl/PlaneProcessor.h"
36 #include "avsutl/VideoFilterBase.h"
37 
38 #include <array>
39 #include <map>
40 #include <memory>
41 #include <mutex>
42 #include <string>
43 
44 
45 
46 namespace fmtcavs
47 {
48 
49 
50 
51 class Resample
52 :	public avsutl::VideoFilterBase
53 ,	public avsutl::PlaneProcCbInterface
54 {
55 
56 /*\\\ PUBLIC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
57 
58 public:
59 
60 	typedef avsutl::VideoFilterBase Inherited;
61 
62 	enum Param
63 	{
64 		Param_CLIP_SRC = 0, // 0
65 		Param_W,
66 		Param_H,
67 		Param_SX,
68 		Param_SY,
69 		Param_SW,
70 		Param_SH,
71 		Param_SCALE,
72 		Param_SCALEH,
73 		Param_SCALEV,
74 		Param_KERNEL, // 10
75 		Param_KERNELH,
76 		Param_KERNELV,
77 		Param_IMPULSE,
78 		Param_IMPULSEH,
79 		Param_IMPULSEV,
80 		Param_TAPS,
81 		Param_TAPSH,
82 		Param_TAPSV,
83 		Param_A1,
84 		Param_A2, // 20
85 		Param_A3,
86 		Param_A1H,
87 		Param_A2H,
88 		Param_A3H,
89 		Param_A1V,
90 		Param_A2V,
91 		Param_A3V,
92 		Param_KOVRSPL,
93 		Param_FH,
94 		Param_FV, // 30
95 		Param_CNORM,
96 		Param_TOTAL,
97 		Param_TOTALH,
98 		Param_TOTALV,
99 		Param_INVKS,
100 		Param_INVKSH,
101 		Param_INVKSV,
102 		Param_INVKSTAPS,
103 		Param_INVKSTAPSH,
104 		Param_INVKSTAPSV, // 40
105 		Param_CSP,
106 		Param_CSS,
107 		Param_PLANES,
108 		Param_FULLS,
109 		Param_FULLD,
110 		Param_CENTER,
111 		Param_CPLACE,
112 		Param_CPLACES,
113 		Param_CPLACED,
114 		Param_INTERLACED, // 50
115 		Param_INTERLACEDD,
116 		Param_TFF,
117 		Param_TFFD,
118 		Param_FLT,
119 		Param_CPUOPT,
120 
121 		Param_NBR_ELT,
122 	};
123 
124 	explicit       Resample (::IScriptEnvironment &env, const ::AVSValue &args);
125 	virtual        ~Resample () = default;
126 
127 	// VideoFilterBase
128 	::PVideoFrame __stdcall
129 						GetFrame (int n, ::IScriptEnvironment *env_ptr) override;
130 
131 	static fmtcl::ChromaPlacement
132 	               conv_str_to_chroma_placement (::IScriptEnvironment &env, std::string cplace);
133 	static void    conv_str_to_chroma_subspl (::IScriptEnvironment &env, int &ssh, int &ssv, std::string css);
134 
135 
136 
137 /*\\\ PROTECTED \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
138 
139 protected:
140 
141 	// PlaneProcCbInterface
142 	void           do_process_plane (::PVideoFrame &dst_sptr, int n, ::IScriptEnvironment &env, int plane_index, int plane_id, void *ctx_ptr) override;
143 
144 
145 
146 /*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
147 
148 private:
149 
150 	using Ru = fmtcl::ResampleUtil;
151 
152 	static constexpr int _max_nbr_planes = 4;
153 
154 	typedef std::array <fmtcl::ResamplePlaneData, _max_nbr_planes> PlaneDataArray;
155 
156 	FmtAvs         get_output_colorspace (::IScriptEnvironment &env, const ::AVSValue &args, const FmtAvs &fmt_src);
157 	void           process_plane_proc (::PVideoFrame &dst_sptr, ::IScriptEnvironment &env, int n, int plane_index, const Ru::FrameInfo &frame_info);
158 	void           process_plane_copy (::PVideoFrame &dst_sptr, ::IScriptEnvironment &env, int n, int plane_index);
159 	fmtcl::FilterResize *
160 	               create_or_access_plane_filter (int plane_index, fmtcl::InterlacingType itl_d, fmtcl::InterlacingType itl_s);
161 	void           create_all_plane_specs (const FmtAvs &fmt_dst, const FmtAvs &fmt_src);
162 
163 	::PClip        _clip_src_sptr;
164 	const ::VideoInfo
165 	               _vi_src;
166 
167 	int            _src_width  = 0;
168 	int            _src_height = 0;
169 	FmtAvs         _fmt_src;
170 	fmtcl::SplFmt  _src_type   = fmtcl::SplFmt_ILLEGAL;
171 	int            _src_res    = 0;
172 	FmtAvs         _fmt_dst;
173 	fmtcl::SplFmt  _dst_type   = fmtcl::SplFmt_ILLEGAL;
174 	int            _dst_res    = 0;
175 	Ru::InterlacingParam
176 	               _interlaced_src  = Ru::InterlacingParam_INVALID;
177 	Ru::InterlacingParam
178 	               _interlaced_dst  = Ru::InterlacingParam_INVALID;
179 	Ru::FieldOrder _field_order_src = Ru::FieldOrder_INVALID;
180 	Ru::FieldOrder _field_order_dst = Ru::FieldOrder_INVALID;
181 	bool           _int_flag   = false;
182 	bool           _norm_flag  = false;
183 	bool           _range_s_def_flag  = false;
184 	bool           _range_d_def_flag  = false;
185 	bool           _fulls_flag = false;
186 	bool           _fulld_flag = false;
187 	bool           _cplace_d_set_flag = false;
188 	fmtcl::ChromaPlacement
189 	               _cplace_s   = fmtcl::ChromaPlacement_MPEG2;
190 	fmtcl::ChromaPlacement
191 	               _cplace_d   = fmtcl::ChromaPlacement_MPEG2;
192 
193 	bool           _sse2_flag  = false;
194 	bool           _avx2_flag  = false;
195 
196 	std::mutex     _filter_mutex;          // To access _filter_uptr_map.
197 	std::map <fmtcl::ResampleSpecPlane, std::unique_ptr <fmtcl::FilterResize> >
198 	               _filter_uptr_map;       // Created only on request.
199 
200 	PlaneDataArray _plane_data_arr;
201 
202 	std::unique_ptr <avsutl::PlaneProcessor>
203 	               _plane_proc_uptr;
204 
205 
206 
207 /*\\\ FORBIDDEN MEMBER FUNCTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
208 
209 private:
210 
211 	               Resample ()                               = delete;
212 	               Resample (const Resample &other)          = delete;
213 	               Resample (Resample &&other)               = delete;
214 	Resample &     operator = (const Resample &other)        = delete;
215 	Resample &     operator = (Resample &&other)             = delete;
216 	bool           operator == (const Resample &other) const = delete;
217 	bool           operator != (const Resample &other) const = delete;
218 
219 }; // class Resample
220 
221 
222 
223 }  // namespace fmtcavs
224 
225 
226 
227 //#include "fmtcavs/Resample.hpp"
228 
229 
230 
231 #endif   // fmtcavs_Resample_HEADER_INCLUDED
232 
233 
234 
235 /*\\\ EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
236