]> git.nihav.org Git - nihav.git/blob - nihav-core/src/scale/scale.rs
mov: fix track duration rescaling
[nihav.git] / nihav-core / src / scale / scale.rs
1 use super::*;
2 use super::kernel::Kernel;
3
4 struct NNResampler {}
5
6 impl NNResampler {
7 fn new() -> Self { Self{} }
8 }
9
10 fn scale_line<T:Copy>(src: &[T], dst: &mut [T], src_w: usize, dst_w: usize) {
11 if src_w == dst_w {
12 (&mut dst[..dst_w]).copy_from_slice(&src[..dst_w]);
13 } else if src_w < dst_w {
14 if dst_w % src_w == 0 {
15 let step = dst_w / src_w;
16 for (out, srcv) in dst.chunks_exact_mut(step).take(src_w).zip(src.iter()) {
17 for el in out.iter_mut() {
18 *el = *srcv;
19 }
20 }
21 } else {
22 let mut pos = 0;
23 for out in dst.iter_mut().take(dst_w) {
24 *out = src[pos / dst_w];
25 pos += src_w;
26 }
27 }
28 } else {
29 if dst_w % src_w == 0 {
30 let step = src_w / dst_w;
31 for (out, srcv) in dst.iter_mut().take(dst_w).zip(src.iter().step_by(step)) {
32 *out = *srcv;
33 }
34 } else {
35 let mut pos = 0;
36 for out in dst.iter_mut().take(dst_w) {
37 *out = src[pos / dst_w];
38 pos += src_w;
39 }
40 }
41 }
42 }
43
44 fn fill_plane<T: Copy>(dst: &mut [T], w: usize, h: usize, stride: usize, val: T) {
45 for row in dst.chunks_mut(stride).take(h) {
46 for el in row.iter_mut().take(w) {
47 *el = val;
48 }
49 }
50 }
51
52 macro_rules! scale_loop {
53 ($sbuf:expr, $dbuf:expr) => {
54 let fmt = $sbuf.get_info().get_format();
55 let ncomp = fmt.get_num_comp();
56 for comp in 0..ncomp {
57 let istride = $sbuf.get_stride(comp);
58 let dstride = $dbuf.get_stride(comp);
59 let (sw, sh) = $sbuf.get_dimensions(comp);
60 let (dw, dh) = $dbuf.get_dimensions(comp);
61 let ioff = $sbuf.get_offset(comp);
62 let mut doff = $dbuf.get_offset(comp);
63 let src = $sbuf.get_data();
64 let dst = $dbuf.get_data_mut().unwrap();
65 for y in 0..dh {
66 let sy = y * sh / dh;
67 let soff = ioff + sy * istride;
68 scale_line(&src[soff..], &mut dst[doff..], sw, dw);
69 doff += dstride;
70 }
71 }
72 let dfmt = $dbuf.get_info().get_format();
73 let ndcomp = dfmt.get_num_comp();
74 if ndcomp > ncomp {
75 if !fmt.alpha && dfmt.alpha {
76 let acomp = ndcomp - 1;
77 let dstride = $dbuf.get_stride(acomp);
78 let (dw, dh) = $dbuf.get_dimensions(acomp);
79 let doff = $dbuf.get_offset(acomp);
80 let dst = $dbuf.get_data_mut().unwrap();
81 fill_plane(&mut dst[doff..], dw, dh, dstride, 0);
82 }
83 if fmt.model.is_yuv() && ((!fmt.alpha && ncomp == 1) || (fmt.alpha && ncomp == 2)) && ndcomp >= 3 {
84 let uval = 1 << (dfmt.comp_info[1].unwrap().depth - 1);
85 let vval = 1 << (dfmt.comp_info[2].unwrap().depth - 1);
86
87 let ustride = $dbuf.get_stride(1);
88 let vstride = $dbuf.get_stride(2);
89 let (uw, uh) = $dbuf.get_dimensions(1);
90 let (vw, vh) = $dbuf.get_dimensions(2);
91 let uoff = $dbuf.get_offset(1);
92 let voff = $dbuf.get_offset(2);
93 let dst = $dbuf.get_data_mut().unwrap();
94 fill_plane(&mut dst[uoff..], uw, uh, ustride, uval);
95 fill_plane(&mut dst[voff..], vw, vh, vstride, vval);
96 }
97 }
98 };
99 }
100
101 impl Kernel for NNResampler {
102 fn init(&mut self, in_fmt: &ScaleInfo, dest_fmt: &ScaleInfo) -> ScaleResult<NABufferType> {
103 let res = alloc_video_buffer(NAVideoInfo::new(dest_fmt.width, dest_fmt.height, false, in_fmt.fmt), 3);
104 if res.is_err() { return Err(ScaleError::AllocError); }
105 Ok(res.unwrap())
106 }
107 fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) {
108 if let (Some(ref sbuf), Some(ref mut dbuf)) = (pic_in.get_vbuf(), pic_out.get_vbuf()) {
109 scale_loop!(sbuf, dbuf);
110 } else if let (Some(ref sbuf), Some(ref mut dbuf)) = (pic_in.get_vbuf16(), pic_out.get_vbuf16()) {
111 scale_loop!(sbuf, dbuf);
112 } else if let (Some(ref sbuf), Some(ref mut dbuf)) = (pic_in.get_vbuf32(), pic_out.get_vbuf32()) {
113 scale_loop!(sbuf, dbuf);
114 } else {
115 unreachable!();
116 }
117 }
118 }
119
120 pub fn create_scale() -> Box<dyn Kernel> {
121 Box::new(NNResampler::new())
122 }
123