2 use super::kernel::Kernel;
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
18 const BT_PAL_COEFFS: [f32; 2] = [ 0.493, 0.877 ];
20 const SMPTE_NTSC_COEFFS: &[f32; 4] = &[ -0.268, 0.7358, 0.4127, 0.4778 ];
22 /*const RGB2YCOCG: [[f32; 3]; 3] = [
24 [ -0.25, 0.5, -0.25 ],
27 const YCOCG2RGB: [[f32; 3]; 3] = [
33 const XYZ2RGB: [[f32; 3]; 3] = [
35 [ 0.17697, 0.8124, 0.01063 ],
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 ]
44 fn make_rgb2yuv(kr: f32, kb: f32, mat: &mut [[f32; 3]; 3]) {
47 mat[0][1] = 1.0 - kr - kb;
50 mat[1][0] = -mat[0][0] * 0.5 / (1.0 - kb);
51 mat[1][1] = -mat[0][1] * 0.5 / (1.0 - kb);
55 mat[2][1] = -mat[0][1] * 0.5 / (1.0 - kr);
56 mat[2][2] = -mat[0][2] * 0.5 / (1.0 - kr);
59 fn make_yuv2rgb(kr: f32, kb: f32, mat: &mut [[f32; 3]; 3]) {
60 let kg = 1.0 - kr - kb;
65 mat[0][2] = 2.0 * (1.0 - kr);
68 mat[1][1] = -kb * 2.0 * (1.0 - kb) / kg;
69 mat[1][2] = -kr * 2.0 * (1.0 - kr) / kg;
72 mat[2][1] = 2.0 * (1.0 - kb);
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;
83 mat[1][2] = eu * (1.0 - mat[0][2]);
85 mat[2][0] = ev * (1.0 - mat[0][0]);
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);
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];
109 tmp[0][i] = mat[1][i] * ufac;
110 tmp[1][i] = mat[2][i] * vfac;
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];
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 };
124 let det = mat[row0][col0] * mat[row1][col1] - mat[row0][col1] * mat[row1][col0];
125 if ((col ^ row) & 1) == 0 {
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();
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;
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] )
163 matrix: [[f32; 3]; 3],
167 fn new() -> Self { Self::default() }
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() {
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); },
182 return Err(ScaleError::InvalidArgument);
184 for i in 0..MAX_CHROMATONS {
185 if let Some(ref mut chr) = df.comp_info[i] {
187 chr.comp_offs = i as u8;
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); }
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);
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();
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);
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;
233 pub fn create_rgb2yuv() -> Box<Kernel> {
234 Box::new(RgbToYuv::new())
239 matrix: [[f32; 3]; 3],
243 fn new() -> Self { Self::default() }
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() {
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);
259 YUVSubmodel::YUVJ => {
260 apply_pal_yuv2rgb(BT_PAL_COEFFS[0], BT_PAL_COEFFS[1], &mut self.matrix);
264 return Err(ScaleError::InvalidArgument);
266 for i in 0..MAX_CHROMATONS {
267 if let Some(ref mut chr) = df.comp_info[i] {
269 chr.comp_offs = i as u8;
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); }
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();
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();
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;
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;
310 if (y & uhmask) == uhmask {
313 if (y & vhmask) == vhmask {
321 pub fn create_yuv2rgb() -> Box<Kernel> {
322 Box::new(YuvToRgb::new())