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