scaler initial work
[nihav.git] / nihav-core / src / scale / colorcvt.rs
1 use super::*;
2 use super::kernel::Kernel;
3
4 const YUV_PARAMS: &[[f32; 2]] = &[
5 [ 0.333, 0.333 ], // RGB
6 [ 0.2126, 0.0722 ], // ITU-R BT709
7 [ 0.333, 0.333 ], // unspecified
8 [ 0.333, 0.333 ], // reserved
9 [ 0.299, 0.114 ], // ITU-R BT601
10 [ 0.299, 0.114 ], // ITU-R BT470
11 [ 0.299, 0.114 ], // SMPTE 170M
12 [ 0.212, 0.087 ], // SMPTE 240M
13 [ 0.333, 0.333 ], // YCoCg
14 [ 0.2627, 0.0593 ], // ITU-R BT2020
15 [ 0.2627, 0.0593 ], // ITU-R BT2020
16 ];
17
18 const BT_PAL_COEFFS: [f32; 2] = [ 0.493, 0.877 ];
19
20 const SMPTE_NTSC_COEFFS: &[f32; 4] = &[ -0.268, 0.7358, 0.4127, 0.4778 ];
21
22 /*const RGB2YCOCG: [[f32; 3]; 3] = [
23 [ 0.25, 0.5, 0.25 ],
24 [ -0.25, 0.5, -0.25 ],
25 [ 0.5, 0.0, -0.5 ]
26 ];
27 const YCOCG2RGB: [[f32; 3]; 3] = [
28 [ 1.0, -1.0, 1.0 ],
29 [ 1.0, 1.0, 0.0 ],
30 [ 1.0, -1.0, -1.0 ]
31 ];
32
33 const XYZ2RGB: [[f32; 3]; 3] = [
34 [ 0.49, 0.31, 0.2 ],
35 [ 0.17697, 0.8124, 0.01063 ],
36 [ 0.0, 0.01, 0.99 ]
37 ];
38 const RGB2XYZ: [[f32; 3]; 3] = [
39 [ 2.364613, -0.89654, -0.46807 ],
40 [ -0.515167, 1.42641, 0.08876 ],
41 [ 0.0052, -0.01441, 1.00920 ]
42 ];*/
43
44 fn make_rgb2yuv(kr: f32, kb: f32, mat: &mut [[f32; 3]; 3]) {
45 // Y
46 mat[0][0] = kr;
47 mat[0][1] = 1.0 - kr - kb;
48 mat[0][2] = kb;
49 // Cb
50 mat[1][0] = -mat[0][0] * 0.5 / (1.0 - kb);
51 mat[1][1] = -mat[0][1] * 0.5 / (1.0 - kb);
52 mat[1][2] = 0.5;
53 // Cr
54 mat[2][0] = 0.5;
55 mat[2][1] = -mat[0][1] * 0.5 / (1.0 - kr);
56 mat[2][2] = -mat[0][2] * 0.5 / (1.0 - kr);
57 }
58
59 fn make_yuv2rgb(kr: f32, kb: f32, mat: &mut [[f32; 3]; 3]) {
60 let kg = 1.0 - kr - kb;
61
62 // R
63 mat[0][0] = 1.0;
64 mat[0][1] = 0.0;
65 mat[0][2] = 2.0 * (1.0 - kr);
66 // G
67 mat[1][0] = 1.0;
68 mat[1][1] = -kb * 2.0 * (1.0 - kb) / kg;
69 mat[1][2] = -kr * 2.0 * (1.0 - kr) / kg;
70 // B
71 mat[2][0] = 1.0;
72 mat[2][1] = 2.0 * (1.0 - kb);
73 mat[2][2] = 0.0;
74 }
75
76 fn apply_pal_rgb2yuv(eu: f32, ev: f32, mat: &mut [[f32; 3]; 3]) {
77 let ufac = 2.0 * (1.0 - mat[0][2]) * eu;
78 let vfac = 2.0 * (1.0 - mat[0][0]) * ev;
79
80 // U
81 mat[1][0] *= ufac;
82 mat[1][1] *= ufac;
83 mat[1][2] = eu * (1.0 - mat[0][2]);
84 // V
85 mat[2][0] = ev * (1.0 - mat[0][0]);
86 mat[2][1] *= vfac;
87 mat[2][2] *= vfac;
88 }
89
90 fn apply_pal_yuv2rgb(eu: f32, ev: f32, mat: &mut [[f32; 3]; 3]) {
91 let ufac = 1.0 / (mat[2][1] * eu);
92 let vfac = 1.0 / (mat[0][2] * ev);
93
94 // R
95 mat[0][2] *= vfac;
96 // G
97 mat[1][1] *= ufac;
98 mat[1][2] *= vfac;
99 // B
100 mat[2][1] *= ufac;
101 }
102
103 fn apply_ntsc_rgb2yiq(params: &[f32; 4], mat: &mut [[f32; 3]; 3]) {
104 let ufac = 2.0 * (1.0 - mat[0][2]);
105 let vfac = 2.0 * (1.0 - mat[0][0]);
106 let mut tmp: [[f32; 3]; 2] = [[0.0; 3]; 2];
107
108 for i in 0..3 {
109 tmp[0][i] = mat[1][i] * ufac;
110 tmp[1][i] = mat[2][i] * vfac;
111 }
112 for i in 0..3 {
113 mat[1][i] = params[0] * tmp[0][i] + params[1] * tmp[1][i];
114 mat[2][i] = params[2] * tmp[0][i] + params[3] * tmp[1][i];
115 }
116 }
117
118 fn subm_det(mat: &[[f32; 3]; 3], col: usize, row: usize) -> f32 {
119 let row0 = if row == 0 { 1 } else { 0 };
120 let row1 = if (row == 1) || (row0 == 1) { 2 } else { 1 };
121 let col0 = if col == 0 { 1 } else { 0 };
122 let col1 = if (col == 1) || (col0 == 1) { 2 } else { 1 };
123
124 let det = mat[row0][col0] * mat[row1][col1] - mat[row0][col1] * mat[row1][col0];
125 if ((col ^ row) & 1) == 0 {
126 det
127 } else {
128 -det
129 }
130 }
131
132 fn invert_matrix(mat: &mut [[f32; 3]; 3]) {
133 let d00 = subm_det(mat, 0, 0);
134 let d01 = subm_det(mat, 0, 1);
135 let d02 = subm_det(mat, 0, 2);
136 let d10 = subm_det(mat, 1, 0);
137 let d11 = subm_det(mat, 1, 1);
138 let d12 = subm_det(mat, 1, 2);
139 let d20 = subm_det(mat, 2, 0);
140 let d21 = subm_det(mat, 2, 1);
141 let d22 = subm_det(mat, 2, 2);
142 let det = 1.0 / (mat[0][0] * d00 + mat[0][1] * d10 + mat[0][2] * d20).abs();
143
144 mat[0][0] = det * d00;
145 mat[0][1] = det * d01;
146 mat[0][2] = det * d02;
147 mat[1][0] = det * d10;
148 mat[1][1] = det * d11;
149 mat[1][2] = det * d12;
150 mat[2][0] = det * d20;
151 mat[2][1] = det * d21;
152 mat[2][2] = det * d22;
153 }
154
155 fn matrix_mul(mat: &[[f32; 3]; 3], a: f32, b: f32, c: f32) -> (f32, f32, f32) {
156 (a * mat[0][0] + b * mat[0][1] + c * mat[0][2],
157 a * mat[1][0] + b * mat[1][1] + c * mat[1][2],
158 a * mat[2][0] + b * mat[2][1] + c * mat[2][2] )
159 }
160
161 #[derive(Default)]
162 struct RgbToYuv {
163 matrix: [[f32; 3]; 3],
164 }
165
166 impl RgbToYuv {
167 fn new() -> Self { Self::default() }
168 }
169
170 impl Kernel for RgbToYuv {
171 fn init(&mut self, in_fmt: &ScaleInfo, dest_fmt: &ScaleInfo) -> ScaleResult<NABufferType> {
172 let mut df = dest_fmt.fmt;
173 //todo coeff selection
174 make_rgb2yuv(YUV_PARAMS[2][0], YUV_PARAMS[2][1], &mut self.matrix);
175 if let ColorModel::YUV(yuvsm) = df.get_model() {
176 match yuvsm {
177 YUVSubmodel::YCbCr => {},
178 YUVSubmodel::YIQ => { apply_ntsc_rgb2yiq(SMPTE_NTSC_COEFFS, &mut self.matrix); },
179 YUVSubmodel::YUVJ => { apply_pal_rgb2yuv(BT_PAL_COEFFS[0], BT_PAL_COEFFS[1], &mut self.matrix); },
180 };
181 } else {
182 return Err(ScaleError::InvalidArgument);
183 }
184 for i in 0..MAX_CHROMATONS {
185 if let Some(ref mut chr) = df.comp_info[i] {
186 chr.packed = false;
187 chr.comp_offs = i as u8;
188 chr.h_ss = 0;
189 chr.v_ss = 0;
190 }
191 }
192 println!(" [intermediate format {}]", df);
193 let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, df), 3);
194 if res.is_err() { return Err(ScaleError::AllocError); }
195 Ok(res.unwrap())
196 }
197 fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) {
198 if let (Some(ref sbuf), Some(ref mut dbuf)) = (pic_in.get_vbuf(), pic_out.get_vbuf()) {
199 let istrides = [sbuf.get_stride(0), sbuf.get_stride(1), sbuf.get_stride(2)];
200 let dstrides = [dbuf.get_stride(0), dbuf.get_stride(1), dbuf.get_stride(2)];
201 let (w, h) = sbuf.get_dimensions(0);
202
203 let mut roff = sbuf.get_offset(0);
204 let mut goff = sbuf.get_offset(1);
205 let mut boff = sbuf.get_offset(2);
206 let mut yoff = dbuf.get_offset(0);
207 let mut uoff = dbuf.get_offset(1);
208 let mut voff = dbuf.get_offset(2);
209 let src = sbuf.get_data();
210 let dst = dbuf.get_data_mut().unwrap();
211 for _y in 0..h {
212 for x in 0..w {
213 let r = src[roff + x] as f32;
214 let g = src[goff + x] as f32;
215 let b = src[boff + x] as f32;
216 let (y, u, v) = matrix_mul(&self.matrix, r, g, b);
217
218 dst[yoff + x] = (y as i16).max(0).min(255) as u8;
219 dst[uoff + x] = ((u as i16).max(-128).min(128) + 128) as u8;
220 dst[voff + x] = ((v as i16).max(-128).min(128) + 128) as u8;
221 }
222 roff += istrides[0];
223 goff += istrides[1];
224 boff += istrides[2];
225 yoff += dstrides[0];
226 uoff += dstrides[1];
227 voff += dstrides[2];
228 }
229 }
230 }
231 }
232
233 pub fn create_rgb2yuv() -> Box<Kernel> {
234 Box::new(RgbToYuv::new())
235 }
236
237 #[derive(Default)]
238 struct YuvToRgb {
239 matrix: [[f32; 3]; 3],
240 }
241
242 impl YuvToRgb {
243 fn new() -> Self { Self::default() }
244 }
245
246 impl Kernel for YuvToRgb {
247 fn init(&mut self, in_fmt: &ScaleInfo, dest_fmt: &ScaleInfo) -> ScaleResult<NABufferType> {
248 let mut df = dest_fmt.fmt;
249 //todo coeff selection
250 make_yuv2rgb(YUV_PARAMS[2][0], YUV_PARAMS[2][1], &mut self.matrix);
251 if let ColorModel::YUV(yuvsm) = in_fmt.fmt.get_model() {
252 match yuvsm {
253 YUVSubmodel::YCbCr => {},
254 YUVSubmodel::YIQ => {
255 make_rgb2yuv(YUV_PARAMS[2][0], YUV_PARAMS[2][1], &mut self.matrix);
256 apply_ntsc_rgb2yiq(SMPTE_NTSC_COEFFS, &mut self.matrix);
257 invert_matrix(&mut self.matrix);
258 },
259 YUVSubmodel::YUVJ => {
260 apply_pal_yuv2rgb(BT_PAL_COEFFS[0], BT_PAL_COEFFS[1], &mut self.matrix);
261 },
262 };
263 } else {
264 return Err(ScaleError::InvalidArgument);
265 }
266 for i in 0..MAX_CHROMATONS {
267 if let Some(ref mut chr) = df.comp_info[i] {
268 chr.packed = false;
269 chr.comp_offs = i as u8;
270 }
271 }
272 println!(" [intermediate format {}]", df);
273 let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, df), 3);
274 if res.is_err() { return Err(ScaleError::AllocError); }
275 Ok(res.unwrap())
276 }
277 fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) {
278 if let (Some(ref sbuf), Some(ref mut dbuf)) = (pic_in.get_vbuf(), pic_out.get_vbuf()) {
279 let istrides = [sbuf.get_stride(0), sbuf.get_stride(1), sbuf.get_stride(2)];
280 let dstrides = [dbuf.get_stride(0), dbuf.get_stride(1), dbuf.get_stride(2)];
281 let (w, h) = sbuf.get_dimensions(0);
282 let (sv0, sh0) = sbuf.get_info().get_format().get_chromaton(1).unwrap().get_subsampling();
283 let (sv1, sh1) = sbuf.get_info().get_format().get_chromaton(2).unwrap().get_subsampling();
284
285 let uhmask = (1 << sh0) - 1;
286 let vhmask = (1 << sh1) - 1;
287 let mut roff = dbuf.get_offset(0);
288 let mut goff = dbuf.get_offset(1);
289 let mut boff = dbuf.get_offset(2);
290 let mut yoff = sbuf.get_offset(0);
291 let mut uoff = sbuf.get_offset(1);
292 let mut voff = sbuf.get_offset(2);
293 let src = sbuf.get_data();
294 let dst = dbuf.get_data_mut().unwrap();
295 for y in 0..h {
296 for x in 0..w {
297 let y = src[yoff + x] as f32;
298 let u = ((src[uoff + (x >> sv0)] as i16) - 128) as f32;
299 let v = ((src[voff + (x >> sv1)] as i16) - 128) as f32;
300
301 let (r, g, b) = matrix_mul(&self.matrix, y, u, v);
302 dst[roff + x] = (r as i16).max(0).min(255) as u8;
303 dst[goff + x] = (g as i16).max(0).min(255) as u8;
304 dst[boff + x] = (b as i16).max(0).min(255) as u8;
305 }
306 roff += dstrides[0];
307 goff += dstrides[1];
308 boff += dstrides[2];
309 yoff += istrides[0];
310 if (y & uhmask) == uhmask {
311 uoff += istrides[1];
312 }
313 if (y & vhmask) == vhmask {
314 voff += istrides[2];
315 }
316 }
317 }
318 }
319 }
320
321 pub fn create_yuv2rgb() -> Box<Kernel> {
322 Box::new(YuvToRgb::new())
323 }