]> git.nihav.org Git - nihav.git/blame - nihav-core/src/scale/repack.rs
vp6: switch to MD5-based tests
[nihav.git] / nihav-core / src / scale / repack.rs
CommitLineData
03accf76
KS
1use crate::formats::*;
2use super::*;
3use super::kernel::Kernel;
4
5fn convert_depth(val: u32, indepth: u8, outdepth: u8) -> u32 {
6 if indepth >= outdepth {
7 val >> (indepth - outdepth)
8 } else {
9 let mut val2 = val << (outdepth - indepth);
10 let mut shift = outdepth - indepth;
11 while shift >= indepth {
12 shift -= indepth;
13 val2 |= val << shift;
14 }
15 if shift > 0 {
16 val2 |= val >> (indepth - shift);
17 }
18 val2
19 }
20}
21
22#[derive(Default)]
23struct PackKernel {
24 shifts: [u8; MAX_CHROMATONS],
25 depths: [u8; MAX_CHROMATONS],
26 ncomps: usize,
27 osize: [u8; MAX_CHROMATONS],
28 ooff: [usize; MAX_CHROMATONS],
29}
30
31impl PackKernel {
32 fn new() -> Self { Self::default() }
33}
34
35impl Kernel for PackKernel {
36 fn init(&mut self, in_fmt: &ScaleInfo, dest_fmt: &ScaleInfo) -> ScaleResult<NABufferType> {
37 self.ncomps = in_fmt.fmt.components as usize;
38 for i in 0..self.ncomps {
39 let ichr = in_fmt.fmt.comp_info[i].unwrap();
40 let ochr = dest_fmt.fmt.comp_info[i].unwrap();
41 self.shifts[i] = ochr.shift;
42 self.osize[i] = ochr.depth;
43 self.depths[i] = ichr.depth;
44 self.ooff[i] = ochr.comp_offs as usize;
45 }
46 let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, dest_fmt.fmt), 3);
47 if res.is_err() { return Err(ScaleError::AllocError); }
48 Ok(res.unwrap())
49 }
50 fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) {
51 if let Some(ref buf) = pic_in.get_vbuf() {
52 if let Some(ref mut dbuf) = pic_out.get_vbuf() {
53 let dstride = dbuf.get_stride(0);
54 for comp in 0..self.ncomps {
55 let ioff = buf.get_offset(comp);
56 let istride = buf.get_stride(comp);
57 let step = dbuf.get_info().get_format().get_chromaton(comp).unwrap().get_step() as usize;
58 let (w, h) = dbuf.get_dimensions(comp);
59 let sdata = buf.get_data();
60 let sdata = &sdata[ioff..];
61 let ddata = dbuf.get_data_mut().unwrap();
62 for (src, dst) in sdata.chunks(istride).zip(ddata.chunks_mut(dstride)).take(h) {
63 for x in 0..w {
e243ceb4 64 dst[x * step + self.ooff[comp]] = convert_depth(u32::from(src[x]), self.depths[comp], self.osize[comp]) as u8;
03accf76
KS
65 }
66 }
67 }
68 } else if let Some(ref mut dbuf) = pic_out.get_vbuf16() {
69 let (w, h) = dbuf.get_dimensions(0);
70 let dstride = dbuf.get_stride(0);
71 let ddata = dbuf.get_data_mut().unwrap();
72 let src = buf.get_data();
73 let mut ioff: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
74 let mut istride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
75 for comp in 0..self.ncomps {
76 ioff[comp] = buf.get_offset(comp);
77 istride[comp] = buf.get_stride(comp);
78 }
79 for dst in ddata.chunks_mut(dstride).take(h) {
80 for x in 0..w {
81 let mut elem: u32 = 0;
82 for comp in 0..self.ncomps {
e243ceb4 83 let c = u32::from(src[ioff[comp] + x]);
03accf76
KS
84 elem |= convert_depth(c, self.depths[comp], self.osize[comp]) << self.shifts[comp];
85 }
86 dst[x] = elem as u16;
87 }
88 for comp in 0..self.ncomps {
89 ioff[comp] += istride[comp];
90 }
91 }
92 } else {
93unimplemented!();
94 }
95 } else if let Some(ref _buf) = pic_in.get_vbuf16() {
96unimplemented!();
97 } else if let Some(ref _buf) = pic_in.get_vbuf32() {
98unimplemented!();
99 } else {
100 unreachable!();
101 }
102 }
103}
104
6011e201 105pub fn create_pack() -> Box<dyn Kernel> {
03accf76
KS
106 Box::new(PackKernel::new())
107}
108
109#[derive(Default)]
110struct UnpackKernel {
111 shifts: [u8; MAX_CHROMATONS],
112 masks: [u32; MAX_CHROMATONS],
113 depths: [u8; MAX_CHROMATONS],
114 ncomps: usize,
115 osize: [u8; MAX_CHROMATONS],
116}
117
118impl UnpackKernel {
119 fn new() -> Self { Self::default() }
120}
121
122impl Kernel for UnpackKernel {
123 fn init(&mut self, in_fmt: &ScaleInfo, dest_fmt: &ScaleInfo) -> ScaleResult<NABufferType> {
124 self.ncomps = in_fmt.fmt.components as usize;
125 let mut chr: Vec<Option<NAPixelChromaton>> = Vec::with_capacity(MAX_CHROMATONS);
126 for i in 0..self.ncomps {
127 let ichr = in_fmt.fmt.comp_info[i].unwrap();
128 let ochr = dest_fmt.fmt.comp_info[i].unwrap();
129 self.shifts[i] = ichr.shift;
130 self.masks[i] = (1 << ichr.depth) - 1;
131 if ochr.depth > ichr.depth {
132 self.osize[i] = ochr.depth;
133 } else {
134 self.osize[i] = (ichr.depth + 7) & !7;
135 }
136 self.depths[i] = ichr.depth;
137 let mut dchr = ichr;
138 dchr.packed = false;
139 dchr.depth = self.osize[i];
140 dchr.shift = 0;
141 dchr.comp_offs = 0;
142 dchr.next_elem = 0;
143 chr.push(Some(dchr));
144 }
145 let mut df = in_fmt.fmt;
146 for i in 0..self.ncomps {
147 df.comp_info[i] = chr[i];
148 }
149println!(" [intermediate format {}]", df);
150 let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, df), 3);
151 if res.is_err() { return Err(ScaleError::AllocError); }
152 Ok(res.unwrap())
153 }
154 fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) {
155 if let Some(ref buf) = pic_in.get_vbuf() {
156 let step = buf.get_info().get_format().get_chromaton(0).unwrap().get_step() as usize;
157 let mut soff: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
158 for i in 0..self.ncomps {
159 soff[i] = buf.get_info().get_format().get_chromaton(i).unwrap().get_offset() as usize;
160 }
161 let (w, h) = buf.get_dimensions(0);
162 let ioff = buf.get_offset(0);
163 let istride = buf.get_stride(0);
164 let sdata1 = buf.get_data();
165 let sdata = &sdata1[ioff..];
166 if let Some(ref mut dbuf) = pic_out.get_vbuf() {
167 let mut ostride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
168 let mut offs: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
169 for i in 0..self.ncomps {
170 ostride[i] = dbuf.get_stride(i);
171 offs[i] = dbuf.get_offset(i);
172 }
173 let dst = dbuf.get_data_mut().unwrap();
174 for src in sdata.chunks(istride).take(h) {
175 for x in 0..w {
176 for i in 0..self.ncomps {
177 dst[offs[i] + x] = src[x * step + soff[i]];
178 }
179 }
180 for i in 0..self.ncomps { offs[i] += ostride[i]; }
181 }
182 } else {
183unimplemented!();
184 }
185 } else if let Some(ref buf) = pic_in.get_vbuf16() {
186 let (w, h) = buf.get_dimensions(0);
187 let ioff = buf.get_offset(0);
188 let istride = buf.get_stride(0);
189 let sdata1 = buf.get_data();
190 let sdata = &sdata1[ioff..];
191 if let Some(ref mut dbuf) = pic_out.get_vbuf() {
192 let mut ostride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
193 let mut offs: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
194 for i in 0..self.ncomps {
195 ostride[i] = dbuf.get_stride(i);
196 offs[i] = dbuf.get_offset(i);
197 }
198 let dst = dbuf.get_data_mut().unwrap();
199 for src in sdata.chunks(istride).take(h) {
200 for x in 0..w {
e243ceb4 201 let elem = u32::from(src[x]);
03accf76
KS
202 for i in 0..self.ncomps {
203 dst[offs[i] + x] = convert_depth((elem >> self.shifts[i]) & self.masks[i], self.depths[i], self.osize[i]) as u8;
204 }
205 }
206 for i in 0..self.ncomps { offs[i] += ostride[i]; }
207 }
208 } else {
209unimplemented!();
210 }
211 } else if let Some(ref _buf) = pic_in.get_vbuf32() {
212unimplemented!();
213 } else {
214 unreachable!();
215 }
216 }
217}
218
6011e201 219pub fn create_unpack() -> Box<dyn Kernel> {
03accf76
KS
220 Box::new(UnpackKernel::new())
221}
222
223#[derive(Default)]
224struct DepalKernel {
225 depths: [u8; MAX_CHROMATONS],
226 coffs: [usize; MAX_CHROMATONS],
227 ncomps: usize,
228 palstep: usize,
229}
230
231impl DepalKernel {
232 fn new() -> Self { Self::default() }
233}
234
235impl Kernel for DepalKernel {
236 fn init(&mut self, in_fmt: &ScaleInfo, _dest_fmt: &ScaleInfo) -> ScaleResult<NABufferType> {
237//todo select output more fitting for dest_fmt if possible
238 self.ncomps = in_fmt.fmt.components as usize;
239 let mut chr: Vec<Option<NAPixelChromaton>> = Vec::with_capacity(MAX_CHROMATONS);
240 self.palstep = in_fmt.fmt.elem_size as usize;
241 for i in 0..self.ncomps {
242 let ichr = in_fmt.fmt.comp_info[i].unwrap();
243 self.coffs[i] = ichr.comp_offs as usize;
244 self.depths[i] = ichr.depth;
245 let mut dchr = ichr;
246 dchr.packed = false;
247 dchr.depth = 8;
248 dchr.shift = 0;
249 dchr.comp_offs = i as u8;
250 dchr.next_elem = 0;
251 chr.push(Some(dchr));
252 }
253 let mut df = in_fmt.fmt;
254 df.palette = false;
255 for i in 0..self.ncomps {
256 df.comp_info[i] = chr[i];
257 }
258println!(" [intermediate format {}]", df);
259 let res = alloc_video_buffer(NAVideoInfo::new(in_fmt.width, in_fmt.height, false, df), 3);
260 if res.is_err() { return Err(ScaleError::AllocError); }
261 Ok(res.unwrap())
262 }
263 fn process(&mut self, pic_in: &NABufferType, pic_out: &mut NABufferType) {
264 if let (Some(ref sbuf), Some(ref mut dbuf)) = (pic_in.get_vbuf(), pic_out.get_vbuf()) {
265 let ioff = sbuf.get_offset(0);
266 let paloff = sbuf.get_offset(1);
267 let (w, h) = sbuf.get_dimensions(0);
268 let istride = sbuf.get_stride(0);
269 let sdata1 = sbuf.get_data();
270 let sdata = &sdata1[ioff..];
271 let pal = &sdata1[paloff..];
272
273 let mut ostride: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
274 let mut offs: [usize; MAX_CHROMATONS] = [0; MAX_CHROMATONS];
275 for i in 0..self.ncomps {
276 ostride[i] = dbuf.get_stride(i);
277 offs[i] = dbuf.get_offset(i);
278 }
279 let dst = dbuf.get_data_mut().unwrap();
280 for src in sdata.chunks(istride).take(h) {
281 for x in 0..w {
282 let palidx = src[x] as usize;
283 for i in 0..self.ncomps {
e243ceb4 284 let elem = u32::from(pal[palidx * self.palstep + self.coffs[i]]);
03accf76
KS
285 dst[offs[i] + x] = convert_depth(elem, self.depths[i], 8) as u8;
286 }
287 }
288 for i in 0..self.ncomps { offs[i] += ostride[i]; }
289 }
290 } else {
291 unreachable!();
292 }
293 }
294}
295
6011e201 296pub fn create_depal() -> Box<dyn Kernel> {
03accf76
KS
297 Box::new(DepalKernel::new())
298}