]>
Commit | Line | Data |
---|---|---|
0cc75664 KS |
1 | use nihav_core::frame::*; |
2 | use nihav_codec_support::codecs::blockdsp::edge_emu; | |
3 | ||
4 | fn clip_u8(val: i16) -> u8 { | |
5 | val.max(0).min(255) as u8 | |
6 | } | |
7 | ||
8 | pub struct IPredContext { | |
9 | pub left: [u8; 16], | |
10 | pub has_left: bool, | |
11 | pub top: [u8; 16], | |
12 | pub has_top: bool, | |
13 | pub tl: u8, | |
14 | } | |
15 | ||
16 | impl IPredContext { | |
17 | pub fn fill(&mut self, src: &[u8], off: usize, stride: usize, tsize: usize, lsize: usize) { | |
18 | if self.has_top { | |
19 | for i in 0..tsize { | |
20 | self.top[i] = src[off - stride + i]; | |
21 | } | |
22 | for i in tsize..16 { | |
23 | self.top[i] = 0x80; | |
24 | } | |
25 | } else { | |
26 | self.top = [0x80; 16]; | |
27 | } | |
28 | if self.has_left { | |
29 | for i in 0..lsize { | |
30 | self.left[i] = src[off - 1 + i * stride]; | |
31 | } | |
32 | for i in lsize..16 { | |
33 | self.left[i] = 0x80; | |
34 | } | |
35 | } else { | |
36 | self.left = [0x80; 16]; | |
37 | } | |
38 | if self.has_top && self.has_left { | |
39 | self.tl = src[off - stride - 1]; | |
40 | } else { | |
41 | self.tl = 0x80; | |
42 | } | |
43 | } | |
44 | } | |
45 | ||
46 | impl Default for IPredContext { | |
47 | fn default() -> Self { | |
48 | Self { | |
49 | left: [0x80; 16], | |
50 | top: [0x80; 16], | |
51 | tl: 0x80, | |
52 | has_left: false, | |
53 | has_top: false, | |
54 | } | |
55 | } | |
56 | } | |
57 | ||
58 | pub fn add_coeffs4x4(dst: &mut [u8], off: usize, stride: usize, coeffs: &[i16; 16]) { | |
59 | let dst = &mut dst[off..]; | |
60 | for (out, src) in dst.chunks_mut(stride).zip(coeffs.chunks(4)) { | |
61 | for (oel, iel) in out.iter_mut().take(4).zip(src.iter()) { | |
62 | *oel = clip_u8(i16::from(*oel) + *iel); | |
63 | } | |
64 | } | |
65 | } | |
66 | pub fn add_coeffs16x1(dst: &mut [u8], off: usize, coeffs: &[i16; 16]) { | |
67 | let dst = &mut dst[off..]; | |
68 | for (oel, iel) in dst.iter_mut().take(16).zip(coeffs.iter()) { | |
69 | *oel = clip_u8(i16::from(*oel) + *iel); | |
70 | } | |
71 | } | |
72 | ||
73 | pub trait IntraPred { | |
74 | const SIZE: usize; | |
75 | fn ipred_dc(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
76 | let dc; | |
77 | if !ipred.has_left && !ipred.has_top { | |
78 | dc = 0x80; | |
79 | } else { | |
80 | let mut dcsum = 0; | |
81 | let mut dcshift = match Self::SIZE { | |
82 | 16 => 3, | |
83 | _ => 2, | |
84 | }; | |
85 | if ipred.has_left { | |
86 | for el in ipred.left.iter().take(Self::SIZE) { | |
87 | dcsum += u16::from(*el); | |
88 | } | |
89 | dcshift += 1; | |
90 | } | |
91 | if ipred.has_top { | |
92 | for el in ipred.top.iter().take(Self::SIZE) { | |
93 | dcsum += u16::from(*el); | |
94 | } | |
95 | dcshift += 1; | |
96 | } | |
97 | dc = ((dcsum + (1 << (dcshift - 1))) >> dcshift) as u8; | |
98 | } | |
99 | for _ in 0..Self::SIZE { | |
100 | let out = &mut dst[off..][..Self::SIZE]; | |
101 | for el in out.iter_mut() { | |
102 | *el = dc; | |
103 | } | |
104 | off += stride; | |
105 | } | |
106 | } | |
107 | fn ipred_v(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
108 | for _ in 0..Self::SIZE { | |
109 | let out = &mut dst[off..][..Self::SIZE]; | |
110 | out.copy_from_slice(&ipred.top[0..Self::SIZE]); | |
111 | off += stride; | |
112 | } | |
113 | } | |
114 | fn ipred_h(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
115 | for leftel in ipred.left.iter().take(Self::SIZE) { | |
116 | let out = &mut dst[off..][..Self::SIZE]; | |
117 | for el in out.iter_mut() { | |
118 | *el = *leftel; | |
119 | } | |
120 | off += stride; | |
121 | } | |
122 | } | |
123 | fn ipred_tm(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
124 | let tl = i16::from(ipred.tl); | |
125 | for m in 0..Self::SIZE { | |
126 | for n in 0..Self::SIZE { | |
127 | dst[off + n] = clip_u8(i16::from(ipred.left[m]) + i16::from(ipred.top[n]) - tl); | |
128 | } | |
129 | off += stride; | |
130 | } | |
131 | } | |
d0d21988 KS |
132 | fn ipred_const(dst: &mut [u8], off: usize, stride: usize, dc: u8) { |
133 | for row in dst[off..].chunks_mut(stride).take(Self::SIZE) { | |
134 | for el in row[..Self::SIZE].iter_mut() { | |
135 | *el = dc; | |
136 | } | |
137 | } | |
138 | } | |
0cc75664 KS |
139 | } |
140 | ||
141 | pub struct IPred16x16 {} | |
142 | impl IntraPred for IPred16x16 { const SIZE: usize = 16; } | |
143 | ||
144 | pub struct IPred8x8 {} | |
145 | impl IntraPred for IPred8x8 { const SIZE: usize = 8; } | |
146 | ||
147 | macro_rules! load_pred4 { | |
148 | (topleft; $ipred: expr) => {{ | |
149 | let tl = u16::from($ipred.tl); | |
150 | let a0 = u16::from($ipred.top[0]); | |
151 | let l0 = u16::from($ipred.left[0]); | |
152 | ((l0 + tl * 2 + a0 + 2) >> 2) as u8 | |
153 | }}; | |
154 | (top; $ipred: expr) => {{ | |
155 | let tl = u16::from($ipred.tl); | |
156 | let a0 = u16::from($ipred.top[0]); | |
157 | let a1 = u16::from($ipred.top[1]); | |
158 | let a2 = u16::from($ipred.top[2]); | |
159 | let a3 = u16::from($ipred.top[3]); | |
160 | let a4 = u16::from($ipred.top[4]); | |
161 | let p0 = ((tl + a0 * 2 + a1 + 2) >> 2) as u8; | |
162 | let p1 = ((a0 + a1 * 2 + a2 + 2) >> 2) as u8; | |
163 | let p2 = ((a1 + a2 * 2 + a3 + 2) >> 2) as u8; | |
164 | let p3 = ((a2 + a3 * 2 + a4 + 2) >> 2) as u8; | |
165 | (p0, p1, p2, p3) | |
166 | }}; | |
167 | (top8; $ipred: expr) => {{ | |
168 | let t3 = u16::from($ipred.top[3]); | |
169 | let t4 = u16::from($ipred.top[4]); | |
170 | let t5 = u16::from($ipred.top[5]); | |
171 | let t6 = u16::from($ipred.top[6]); | |
172 | let t7 = u16::from($ipred.top[7]); | |
173 | let p4 = ((t3 + t4 * 2 + t5 + 2) >> 2) as u8; | |
174 | let p5 = ((t4 + t5 * 2 + t6 + 2) >> 2) as u8; | |
175 | let p6 = ((t5 + t6 * 2 + t7 + 2) >> 2) as u8; | |
176 | let p7 = ((t6 + t7 * 2 + t7 + 2) >> 2) as u8; | |
177 | (p4, p5, p6, p7) | |
178 | }}; | |
179 | (topavg; $ipred: expr) => {{ | |
180 | let tl = u16::from($ipred.tl); | |
181 | let a0 = u16::from($ipred.top[0]); | |
182 | let a1 = u16::from($ipred.top[1]); | |
183 | let a2 = u16::from($ipred.top[2]); | |
184 | let a3 = u16::from($ipred.top[3]); | |
185 | let p0 = ((tl + a0 + 1) >> 1) as u8; | |
186 | let p1 = ((a0 + a1 + 1) >> 1) as u8; | |
187 | let p2 = ((a1 + a2 + 1) >> 1) as u8; | |
188 | let p3 = ((a2 + a3 + 1) >> 1) as u8; | |
189 | (p0, p1, p2, p3) | |
190 | }}; | |
191 | (left; $ipred: expr) => {{ | |
192 | let tl = u16::from($ipred.tl); | |
193 | let l0 = u16::from($ipred.left[0]); | |
194 | let l1 = u16::from($ipred.left[1]); | |
195 | let l2 = u16::from($ipred.left[2]); | |
196 | let l3 = u16::from($ipred.left[3]); | |
197 | let l4 = u16::from($ipred.left[4]); | |
198 | let p0 = ((tl + l0 * 2 + l1 + 2) >> 2) as u8; | |
199 | let p1 = ((l0 + l1 * 2 + l2 + 2) >> 2) as u8; | |
200 | let p2 = ((l1 + l2 * 2 + l3 + 2) >> 2) as u8; | |
201 | let p3 = ((l2 + l3 * 2 + l4 + 2) >> 2) as u8; | |
202 | (p0, p1, p2, p3) | |
203 | }}; | |
204 | (left8; $ipred: expr) => {{ | |
205 | let l3 = u16::from($ipred.left[3]); | |
206 | let l4 = u16::from($ipred.left[4]); | |
207 | let l5 = u16::from($ipred.left[5]); | |
208 | let l6 = u16::from($ipred.left[6]); | |
209 | let l7 = u16::from($ipred.left[7]); | |
210 | let p4 = ((l3 + l4 * 2 + l5 + 2) >> 2) as u8; | |
211 | let p5 = ((l4 + l5 * 2 + l6 + 2) >> 2) as u8; | |
212 | let p6 = ((l5 + l6 * 2 + l7 + 2) >> 2) as u8; | |
213 | let p7 = ((l6 + l7 * 2 + l7 + 2) >> 2) as u8; | |
214 | (p4, p5, p6, p7) | |
215 | }}; | |
216 | (leftavg; $ipred: expr) => {{ | |
217 | let tl = u16::from($ipred.tl); | |
218 | let l0 = u16::from($ipred.left[0]); | |
219 | let l1 = u16::from($ipred.left[1]); | |
220 | let l2 = u16::from($ipred.left[2]); | |
221 | let l3 = u16::from($ipred.left[3]); | |
222 | let p0 = ((tl + l0 + 1) >> 1) as u8; | |
223 | let p1 = ((l0 + l1 + 1) >> 1) as u8; | |
224 | let p2 = ((l1 + l2 + 1) >> 1) as u8; | |
225 | let p3 = ((l2 + l3 + 1) >> 1) as u8; | |
226 | (p0, p1, p2, p3) | |
227 | }}; | |
228 | } | |
229 | ||
230 | pub struct IPred4x4 {} | |
231 | impl IPred4x4 { | |
232 | pub fn ipred_dc(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
0cc75664 KS |
233 | let mut dcsum = 0; |
234 | for el in ipred.left.iter().take(4) { | |
235 | dcsum += u16::from(*el); | |
236 | } | |
237 | for el in ipred.top.iter().take(4) { | |
238 | dcsum += u16::from(*el); | |
239 | } | |
6f263099 | 240 | let dc = ((dcsum + (1 << 2)) >> 3) as u8; |
0cc75664 KS |
241 | for _ in 0..4 { |
242 | let out = &mut dst[off..][..4]; | |
243 | for el in out.iter_mut() { | |
244 | *el = dc; | |
245 | } | |
246 | off += stride; | |
247 | } | |
248 | } | |
249 | pub fn ipred_tm(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
250 | let tl = i16::from(ipred.tl); | |
251 | for m in 0..4 { | |
252 | for n in 0..4 { | |
253 | dst[off + n] = clip_u8(i16::from(ipred.left[m]) + i16::from(ipred.top[n]) - tl); | |
254 | } | |
255 | off += stride; | |
256 | } | |
257 | } | |
258 | pub fn ipred_ve(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
259 | let (v0, v1, v2, v3) = load_pred4!(top; ipred); | |
260 | let vert_pred = [v0, v1, v2, v3]; | |
261 | for _ in 0..4 { | |
262 | let out = &mut dst[off..][..4]; | |
263 | out.copy_from_slice(&vert_pred); | |
264 | off += stride; | |
265 | } | |
266 | } | |
267 | pub fn ipred_he(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
268 | let (p0, p1, p2, _) = load_pred4!(left; ipred); | |
269 | let p3 = ((u16::from(ipred.left[2]) + u16::from(ipred.left[3]) * 3 + 2) >> 2) as u8; | |
270 | let hor_pred = [p0, p1, p2, p3]; | |
271 | for m in 0..4 { | |
272 | for n in 0..4 { | |
273 | dst[off + n] = hor_pred[m]; | |
274 | } | |
275 | off += stride; | |
276 | } | |
277 | } | |
278 | pub fn ipred_ld(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
279 | let (_, p0, p1, p2) = load_pred4!(top; ipred); | |
280 | let (p3, p4, p5, p6) = load_pred4!(top8; ipred); | |
281 | ||
282 | dst[off + 0] = p0; dst[off + 1] = p1; dst[off + 2] = p2; dst[off + 3] = p3; | |
283 | off += stride; | |
284 | dst[off + 0] = p1; dst[off + 1] = p2; dst[off + 2] = p3; dst[off + 3] = p4; | |
285 | off += stride; | |
286 | dst[off + 0] = p2; dst[off + 1] = p3; dst[off + 2] = p4; dst[off + 3] = p5; | |
287 | off += stride; | |
288 | dst[off + 0] = p3; dst[off + 1] = p4; dst[off + 2] = p5; dst[off + 3] = p6; | |
289 | } | |
290 | pub fn ipred_rd(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
291 | let tl = load_pred4!(topleft; ipred); | |
292 | let (l0, l1, l2, _) = load_pred4!(left; ipred); | |
293 | let (t0, t1, t2, _) = load_pred4!(top; ipred); | |
294 | ||
295 | dst[off + 0] = tl; dst[off + 1] = t0; dst[off + 2] = t1; dst[off + 3] = t2; | |
296 | off += stride; | |
297 | dst[off + 0] = l0; dst[off + 1] = tl; dst[off + 2] = t0; dst[off + 3] = t1; | |
298 | off += stride; | |
299 | dst[off + 0] = l1; dst[off + 1] = l0; dst[off + 2] = tl; dst[off + 3] = t0; | |
300 | off += stride; | |
301 | dst[off + 0] = l2; dst[off + 1] = l1; dst[off + 2] = l0; dst[off + 3] = tl; | |
302 | } | |
303 | pub fn ipred_vr(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
304 | let tl = load_pred4!(topleft; ipred); | |
305 | let (l0, l1, _, _) = load_pred4!(left; ipred); | |
306 | let (t0, t1, t2, _) = load_pred4!(top; ipred); | |
307 | let (m0, m1, m2, m3) = load_pred4!(topavg; ipred); | |
308 | ||
309 | dst[off + 0] = m0; dst[off + 1] = m1; dst[off + 2] = m2; dst[off + 3] = m3; | |
310 | off += stride; | |
311 | dst[off + 0] = tl; dst[off + 1] = t0; dst[off + 2] = t1; dst[off + 3] = t2; | |
312 | off += stride; | |
313 | dst[off + 0] = l0; dst[off + 1] = m0; dst[off + 2] = m1; dst[off + 3] = m2; | |
314 | off += stride; | |
315 | dst[off + 0] = l1; dst[off + 1] = tl; dst[off + 2] = t0; dst[off + 3] = t1; | |
316 | } | |
317 | pub fn ipred_vl(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
318 | let (_, t1, t2, t3) = load_pred4!(top; ipred); | |
319 | let (t4, t5, t6, _) = load_pred4!(top8; ipred); | |
320 | let (_, m1, m2, m3) = load_pred4!(topavg; ipred); | |
321 | let m4 = ((u16::from(ipred.top[3]) + u16::from(ipred.top[4]) + 1) >> 1) as u8; | |
322 | ||
323 | dst[off + 0] = m1; dst[off + 1] = m2; dst[off + 2] = m3; dst[off + 3] = m4; | |
324 | off += stride; | |
325 | dst[off + 0] = t1; dst[off + 1] = t2; dst[off + 2] = t3; dst[off + 3] = t4; | |
326 | off += stride; | |
327 | dst[off + 0] = m2; dst[off + 1] = m3; dst[off + 2] = m4; dst[off + 3] = t5; | |
328 | off += stride; | |
329 | dst[off + 0] = t2; dst[off + 1] = t3; dst[off + 2] = t4; dst[off + 3] = t6; | |
330 | } | |
331 | pub fn ipred_hd(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
332 | let tl = load_pred4!(topleft; ipred); | |
333 | let (l0, l1, l2, _) = load_pred4!(left; ipred); | |
334 | let (m0, m1, m2, m3) = load_pred4!(leftavg; ipred); | |
335 | let (t0, t1, _, _) = load_pred4!(top; ipred); | |
336 | ||
337 | dst[off + 0] = m0; dst[off + 1] = tl; dst[off + 2] = t0; dst[off + 3] = t1; | |
338 | off += stride; | |
339 | dst[off + 0] = m1; dst[off + 1] = l0; dst[off + 2] = m0; dst[off + 3] = tl; | |
340 | off += stride; | |
341 | dst[off + 0] = m2; dst[off + 1] = l1; dst[off + 2] = m1; dst[off + 3] = l0; | |
342 | off += stride; | |
343 | dst[off + 0] = m3; dst[off + 1] = l2; dst[off + 2] = m2; dst[off + 3] = l1; | |
344 | } | |
345 | pub fn ipred_hu(dst: &mut [u8], mut off: usize, stride: usize, ipred: &IPredContext) { | |
346 | let (_, m1, m2, m3) = load_pred4!(leftavg; ipred); | |
347 | let (_, l1, l2, _) = load_pred4!(left; ipred); | |
348 | let l3 = ((u16::from(ipred.left[2]) + u16::from(ipred.left[3]) * 3 + 2) >> 2) as u8; | |
349 | let p3 = ipred.left[3]; | |
350 | ||
351 | dst[off + 0] = m1; dst[off + 1] = l1; dst[off + 2] = m2; dst[off + 3] = l2; | |
352 | off += stride; | |
353 | dst[off + 0] = m2; dst[off + 1] = l2; dst[off + 2] = m3; dst[off + 3] = l3; | |
354 | off += stride; | |
355 | dst[off + 0] = m3; dst[off + 1] = l3; dst[off + 2] = p3; dst[off + 3] = p3; | |
356 | off += stride; | |
357 | dst[off + 0] = p3; dst[off + 1] = p3; dst[off + 2] = p3; dst[off + 3] = p3; | |
358 | } | |
359 | } | |
360 | ||
361 | const VP7_BICUBIC_FILTERS: [[i16; 6]; 8] = [ | |
362 | [ 0, 0, 128, 0, 0, 0 ], | |
363 | [ 0, -6, 123, 12, -1, 0 ], | |
364 | [ 2, -11, 108, 36, -8, 1 ], | |
365 | [ 0, -9, 93, 50, -6, 0 ], | |
366 | [ 3, -16, 77, 77, -16, 3 ], | |
367 | [ 0, -6, 50, 93, -9, 0 ], | |
368 | [ 1, -8, 36, 108, -11, 2 ], | |
369 | [ 0, -1, 12, 123, -6, 0 ] | |
370 | ]; | |
371 | ||
372 | macro_rules! interpolate { | |
373 | ($src: expr, $off: expr, $step: expr, $mode: expr) => {{ | |
374 | let s0 = i32::from($src[$off + 0 * $step]); | |
375 | let s1 = i32::from($src[$off + 1 * $step]); | |
376 | let s2 = i32::from($src[$off + 2 * $step]); | |
377 | let s3 = i32::from($src[$off + 3 * $step]); | |
378 | let s4 = i32::from($src[$off + 4 * $step]); | |
379 | let s5 = i32::from($src[$off + 5 * $step]); | |
380 | let filt = &VP7_BICUBIC_FILTERS[$mode]; | |
381 | let src = [s0, s1, s2, s3, s4, s5]; | |
382 | let mut val = 64; | |
383 | for (s, c) in src.iter().zip(filt.iter()) { | |
384 | val += s * i32::from(*c); | |
385 | } | |
386 | clip_u8((val >> 7) as i16) | |
387 | }} | |
388 | } | |
389 | ||
390 | const EDGE_PRE: usize = 2; | |
391 | const EDGE_POST: usize = 4; | |
392 | const TMP_STRIDE: usize = 16; | |
393 | ||
394 | fn mc_block_common(dst: &mut [u8], mut doff: usize, dstride: usize, src: &[u8], sstride: usize, size: usize, mx: usize, my: usize) { | |
395 | if (mx == 0) && (my == 0) { | |
396 | let dst = &mut dst[doff..]; | |
397 | let src = &src[EDGE_PRE + EDGE_PRE * sstride..]; | |
398 | for (out, src) in dst.chunks_mut(dstride).take(size).zip(src.chunks(sstride)) { | |
e6aaad5c | 399 | out[..size].copy_from_slice(&src[..size]); |
0cc75664 KS |
400 | } |
401 | } else if my == 0 { | |
402 | let src = &src[EDGE_PRE * sstride..]; | |
403 | for src in src.chunks(sstride).take(size) { | |
404 | for x in 0..size { | |
405 | dst[doff + x] = interpolate!(src, x, 1, mx); | |
406 | } | |
407 | doff += dstride; | |
408 | } | |
409 | } else if mx == 0 { | |
410 | let src = &src[EDGE_PRE..]; | |
411 | for y in 0..size { | |
412 | for x in 0..size { | |
413 | dst[doff + x] = interpolate!(src, x + y * sstride, sstride, my); | |
414 | } | |
415 | doff += dstride; | |
416 | } | |
417 | } else { | |
418 | let mut tmp = [0u8; TMP_STRIDE * (16 + EDGE_PRE + EDGE_POST)]; | |
419 | for (y, dst) in tmp.chunks_mut(TMP_STRIDE).take(size + EDGE_PRE + EDGE_POST).enumerate() { | |
420 | for x in 0..size { | |
421 | dst[x] = interpolate!(src, x + y * sstride, 1, mx); | |
422 | } | |
423 | } | |
424 | for y in 0..size { | |
425 | for x in 0..size { | |
426 | dst[doff + x] = interpolate!(tmp, x + y * TMP_STRIDE, TMP_STRIDE, my); | |
427 | } | |
428 | doff += dstride; | |
429 | } | |
430 | } | |
431 | } | |
432 | fn mc_block(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize, | |
433 | mvx: i16, mvy: i16, reffrm: NAVideoBufferRef<u8>, plane: usize, | |
434 | mc_buf: &mut [u8], size: usize) { | |
435 | if (mvx == 0) && (mvy == 0) { | |
436 | let dst = &mut dst[doff..]; | |
437 | let sstride = reffrm.get_stride(plane); | |
438 | let srcoff = reffrm.get_offset(plane) + xpos + ypos * sstride; | |
439 | let src = &reffrm.get_data(); | |
440 | let src = &src[srcoff..]; | |
441 | for (out, src) in dst.chunks_mut(dstride).take(size).zip(src.chunks(sstride)) { | |
e6aaad5c | 442 | out[..size].copy_from_slice(&src[..size]); |
0cc75664 KS |
443 | } |
444 | return; | |
445 | } | |
446 | let (w, h) = reffrm.get_dimensions(plane); | |
447 | let wa = if plane == 0 { (w + 15) & !15 } else { (w + 7) & !7 } as isize; | |
448 | let ha = if plane == 0 { (h + 15) & !15 } else { (h + 7) & !7 } as isize; | |
449 | let bsize = (size as isize) + (EDGE_PRE as isize) + (EDGE_POST as isize); | |
450 | let ref_x = (xpos as isize) + ((mvx >> 3) as isize) - (EDGE_PRE as isize); | |
451 | let ref_y = (ypos as isize) + ((mvy >> 3) as isize) - (EDGE_PRE as isize); | |
452 | ||
453 | let (src, sstride) = if (ref_x < 0) || (ref_x + bsize > wa) || (ref_y < 0) || (ref_y + bsize > ha) { | |
454 | edge_emu(&reffrm, ref_x, ref_y, bsize as usize, bsize as usize, mc_buf, 32, plane, 4); | |
455 | (mc_buf as &[u8], 32) | |
456 | } else { | |
457 | let off = reffrm.get_offset(plane); | |
458 | let stride = reffrm.get_stride(plane); | |
459 | let data = reffrm.get_data(); | |
460 | (&data[off + (ref_x as usize) + (ref_y as usize) * stride..], stride) | |
461 | }; | |
462 | let mx = (mvx & 7) as usize; | |
463 | let my = (mvy & 7) as usize; | |
464 | mc_block_common(dst, doff, dstride, src, sstride, size, mx, my); | |
465 | } | |
466 | pub fn mc_block16x16(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize, | |
467 | mvx: i16, mvy: i16, src: NAVideoBufferRef<u8>, plane: usize, mc_buf: &mut [u8]) { | |
468 | mc_block(dst, doff, dstride, xpos, ypos, mvx, mvy, src, plane, mc_buf, 16); | |
469 | } | |
470 | pub fn mc_block8x8(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize, | |
471 | mvx: i16, mvy: i16, src: NAVideoBufferRef<u8>, plane: usize, mc_buf: &mut [u8]) { | |
472 | mc_block(dst, doff, dstride, xpos, ypos, mvx, mvy, src, plane, mc_buf, 8); | |
473 | } | |
474 | pub fn mc_block4x4(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize, | |
475 | mvx: i16, mvy: i16, src: NAVideoBufferRef<u8>, plane: usize, mc_buf: &mut [u8]) { | |
476 | mc_block(dst, doff, dstride, xpos, ypos, mvx, mvy, src, plane, mc_buf, 4); | |
477 | } | |
478 | pub fn mc_block_special(dst: &mut [u8], doff: usize, dstride: usize, xpos: usize, ypos: usize, | |
479 | mvx: i16, mvy: i16, reffrm: NAVideoBufferRef<u8>, plane: usize, | |
480 | mc_buf: &mut [u8], size: usize, pitch_mode: u8) { | |
481 | const Y_MUL: [isize; 8] = [ 1, 0, 2, 4, 1, 1, 2, 2 ]; | |
482 | const Y_OFF: [isize; 8] = [ 0, 4, 0, 0, 1, -1, 1, -1 ]; | |
483 | const ILACE_CHROMA: [bool; 8] = [ false, false, true, true, false, false, true, true ]; // mode&2 != 0 | |
484 | ||
485 | let pitch_mode = (pitch_mode & 7) as usize; | |
486 | let (xstep, ymul) = if plane == 0 { | |
487 | (Y_OFF[pitch_mode], Y_MUL[pitch_mode]) | |
488 | } else { | |
489 | (0, if ILACE_CHROMA[pitch_mode] { 2 } else { 1 }) | |
490 | }; | |
491 | ||
492 | let (w, h) = reffrm.get_dimensions(plane); | |
493 | let wa = if plane == 0 { (w + 15) & !15 } else { (w + 7) & !7 } as isize; | |
494 | let ha = if plane == 0 { (h + 15) & !15 } else { (h + 7) & !7 } as isize; | |
495 | let mut start_x = (xpos as isize) + ((mvx >> 3) as isize) - (EDGE_PRE as isize); | |
496 | let mut end_x = (xpos as isize) + ((mvx >> 3) as isize) + ((size + EDGE_POST) as isize); | |
497 | if xstep < 0 { | |
498 | start_x -= (size + EDGE_POST) as isize; | |
499 | } else if xstep > 0 { | |
500 | end_x += (size as isize) * xstep; | |
501 | } | |
502 | let mut start_y = (ypos as isize) + ((mvy >> 3) as isize) - (EDGE_PRE as isize) * ymul; | |
503 | let mut end_y = (ypos as isize) + ((mvy >> 3) as isize) + ((size + EDGE_POST) as isize) * ymul; | |
504 | if ymul == 0 { | |
505 | start_y -= EDGE_PRE as isize; | |
506 | end_y += (EDGE_POST + 1) as isize; | |
507 | } | |
508 | let off = reffrm.get_offset(plane); | |
509 | let stride = reffrm.get_stride(plane); | |
510 | let (src, sstride) = if (start_x >= 0) && (end_x <= wa) && (start_y >= 0) && (end_y <= ha) { | |
511 | let data = reffrm.get_data(); | |
512 | (&data[off + (start_x as usize) + (start_y as usize) * stride..], | |
513 | ((stride as isize) + xstep) as usize) | |
514 | } else { | |
e6aaad5c | 515 | let add = (size + EDGE_PRE + EDGE_POST) * xstep.unsigned_abs(); |
0cc75664 KS |
516 | let bw = size + EDGE_PRE + EDGE_POST + add; |
517 | let bh = (end_y - start_y) as usize; | |
518 | let bo = if xstep >= 0 { 0 } else { add }; | |
519 | edge_emu(&reffrm, start_x + (bo as isize), start_y, bw, bh, mc_buf, 128, plane, 0); | |
520 | (&mc_buf[bo..], (128 + xstep) as usize) | |
521 | }; | |
522 | let mx = (mvx & 7) as usize; | |
523 | let my = (mvy & 7) as usize; | |
524 | match ymul { | |
525 | 0 => unimplemented!(), | |
526 | 1 => mc_block_common(dst, doff, dstride, src, sstride, size, mx, my), | |
527 | 2 => { | |
528 | let hsize = size / 2; | |
529 | for y in 0..2 { | |
530 | for x in 0..2 { | |
531 | mc_block_common(dst, doff + x * hsize + y * hsize * dstride, dstride, | |
532 | &src[x * hsize + y * sstride..], sstride * 2, hsize, mx, my); | |
533 | } | |
534 | } | |
535 | }, | |
536 | 4 => { | |
537 | let qsize = size / 4; | |
538 | for y in 0..4 { | |
539 | for x in 0..4 { | |
540 | mc_block_common(dst, doff + x * qsize + y * qsize * dstride, dstride, | |
541 | &src[x * qsize + y * sstride..], sstride * 4, qsize, mx, my); | |
542 | } | |
543 | } | |
544 | }, | |
545 | _ => unreachable!(), | |
546 | }; | |
547 | } |