]> git.nihav.org Git - nihav.git/blame - nihav-itu/src/codecs/h264/dsp/mod.rs
h264: update test hashes after the changes
[nihav.git] / nihav-itu / src / codecs / h264 / dsp / mod.rs
CommitLineData
696e4e20
KS
1use nihav_core::frame::*;
2use nihav_codec_support::codecs::blockdsp::*;
3use nihav_codec_support::codecs::MV;
4
999fbb83
KS
5#[cfg(not(debug_assertions))]
6mod release;
7#[cfg(not(debug_assertions))]
8use release::*;
9#[cfg(debug_assertions)]
10mod debug;
11#[cfg(debug_assertions)]
12use debug::*;
13
696e4e20
KS
14pub const CHROMA_QUANTS: [u8; 52] = [
15 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 29, 30,
17 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38,
18 39, 39, 39, 39
19];
20
21pub const CHROMA_DC_SCAN: [usize; 4] = [ 0, 1, 2, 3];
22pub const ZIGZAG: [usize; 16] = [
23 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
24];
25pub const ZIGZAG1: [usize; 15] = [
26 0, 3, 7, 4, 1, 2, 5, 8, 11, 12, 9, 6, 10, 13, 14
27];
28/*pub const IL_SCAN: [usize; 16] = [
29 0, 4, 1, 8, 12, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15
30];*/
31pub const ZIGZAG8X8: [usize; 64] = [
32 0, 1, 8, 16, 9, 2, 3, 10,
33 17, 24, 32, 25, 18, 11, 4, 5,
34 12, 19, 26, 33, 40, 48, 41, 34,
35 27, 20, 13, 6, 7, 14, 21, 28,
36 35, 42, 49, 56, 57, 50, 43, 36,
37 29, 22, 15, 23, 30, 37, 44, 51,
38 58, 59, 52, 45, 38, 31, 39, 46,
39 53, 60, 61, 54, 47, 55, 62, 63
40];
41
42const LEVEL_SCALE: [[i16; 6]; 3] = [
43 [ 10, 11, 13, 14, 16, 18 ],
44 [ 16, 18, 20, 23, 25, 29 ],
45 [ 13, 14, 16, 18, 20, 23 ]
46];
47
48pub fn chroma_dc_transform(blk: &mut [i16; 4], qp: u8) {
49 let t0 = blk[0] + blk[2];
50 let t1 = blk[0] - blk[2];
51 let t2 = blk[1] + blk[3];
52 let t3 = blk[1] - blk[3];
53 blk[0] = t0 + t2;
54 blk[1] = t0 - t2;
55 blk[2] = t1 + t3;
56 blk[3] = t1 - t3;
57 if qp < 6 {
4a1ca15c 58 let mul = LEVEL_SCALE[0][qp as usize];
696e4e20
KS
59 for el in blk.iter_mut() {
60 *el = el.wrapping_mul(mul) >> 1;
61 }
62 } else {
4a1ca15c 63 let mul = LEVEL_SCALE[0][(qp % 6) as usize];
696e4e20
KS
64 let shift = qp / 6 - 1;
65 for el in blk.iter_mut() {
66 *el = el.wrapping_mul(mul) << shift;
67 }
68 }
69}
70
71macro_rules! transform {
72 (luma_dc; $a: expr, $b: expr, $c: expr, $d: expr) => ({
73 let t0 = $a.wrapping_add($c);
74 let t1 = $a.wrapping_sub($c);
75 let t2 = $b.wrapping_add($d);
76 let t3 = $b.wrapping_sub($d);
77 $a = t0.wrapping_add(t2);
78 $b = t1.wrapping_add(t3);
79 $c = t1.wrapping_sub(t3);
80 $d = t0.wrapping_sub(t2);
81 });
82 ($a: expr, $b: expr, $c: expr, $d: expr, $shift: expr) => ({
83 let t0 = $a.wrapping_add($c);
84 let t1 = $a.wrapping_sub($c);
85 let t2 = ($b >> 1).wrapping_sub($d);
86 let t3 = $b.wrapping_add($d >> 1);
87 let bias = 1 << $shift >> 1;
88 $a = t0.wrapping_add(t3).wrapping_add(bias) >> $shift;
89 $b = t1.wrapping_add(t2).wrapping_add(bias) >> $shift;
90 $c = t1.wrapping_sub(t2).wrapping_add(bias) >> $shift;
91 $d = t0.wrapping_sub(t3).wrapping_add(bias) >> $shift;
92 });
93 ($a: expr, $b: expr, $c: expr, $d: expr, $e: expr, $f: expr, $g: expr, $h: expr) => {
94 let e0 = $a + $e;
95 let e1 = -$d + $f - $h - ($h >> 1);
96 let e2 = $a - $e;
97 let e3 = $b + $h - $d - ($d >> 1);
98 let e4 = ($c >> 1) - $g;
99 let e5 = -$b + $h + $f + ($f >> 1);
100 let e6 = $c + ($g >> 1);
101 let e7 = $d + $f + $b + ($b >> 1);
102
103 let f0 = e0 + e6;
104 let f1 = e1 + (e7 >> 2);
105 let f2 = e2 + e4;
106 let f3 = e3 + (e5 >> 2);
107 let f4 = e2 - e4;
108 let f5 = (e3 >> 2) - e5;
109 let f6 = e0 - e6;
110 let f7 = e7 - (e1 >> 2);
111
112 $a = f0 + f7;
113 $b = f2 + f5;
114 $c = f4 + f3;
115 $d = f6 + f1;
116 $e = f6 - f1;
117 $f = f4 - f3;
118 $g = f2 - f5;
119 $h = f0 - f7;
120 };
121}
122
123pub fn idct_luma_dc(blk: &mut [i16; 16], qp: u8) {
124 if qp < 12 {
4a1ca15c 125 let mul = LEVEL_SCALE[0][(qp % 6) as usize];
696e4e20
KS
126 let shift = 2 - qp / 6;
127 let bias = 1 << shift >> 1;
128 for el in blk.iter_mut() {
129 *el = el.wrapping_mul(mul).wrapping_add(bias) >> shift;
130 }
131 } else {
4a1ca15c 132 let mul = LEVEL_SCALE[0][(qp % 6) as usize];
696e4e20
KS
133 let shift = qp / 6 - 2;
134 for el in blk.iter_mut() {
135 *el = el.wrapping_mul(mul) << shift;
136 }
137 }
138 for i in 0..4 {
139 transform!(luma_dc; blk[i], blk[i + 4], blk[i + 8], blk[i + 12]);
140 }
141 for row in blk.chunks_mut(4) {
142 transform!(luma_dc; row[0], row[1], row[2], row[3]);
143 }
144}
145
146pub fn idct(blk: &mut [i16; 16], qp: u8, quant_dc: bool) {
147 const BLK_INDEX: [usize; 16] = [
148 0, 2, 0, 2,
149 2, 1, 2, 1,
150 0, 2, 0, 2,
151 2, 1, 2, 1
152 ];
153 let qidx = (qp % 6) as usize;
154 let shift = qp / 6;
155 let start = if quant_dc { 0 } else { 1 };
156 for (el, &idx) in blk.iter_mut().zip(BLK_INDEX.iter()).skip(start) {
157 *el = (*el * LEVEL_SCALE[idx][qidx]) << shift;
158 }
696e4e20 159 for row in blk.chunks_mut(4) {
3ad9bf2b
KS
160 transform!(row[0], row[1], row[2], row[3], 0);
161 }
162 for i in 0..4 {
163 transform!(blk[i], blk[i + 4], blk[i + 8], blk[i + 12], 6);
696e4e20
KS
164 }
165}
166
167pub fn idct_dc(blk: &mut [i16; 16], qp: u8, quant_dc: bool) {
168 let dc = if quant_dc {
169 (blk[0] * LEVEL_SCALE[0][(qp % 6) as usize]) << (qp / 6)
170 } else {
171 blk[0]
172 };
173 *blk = [(dc + 0x20) >> 6; 16];
174}
175
176const QMAT_8X8: [[u8; 16]; 6] = [
177 [
2b6a8fdc 178 20, 19, 25, 19,
696e4e20
KS
179 19, 18, 24, 18,
180 25, 24, 32, 24,
2b6a8fdc 181 19, 18, 24, 18
696e4e20 182 ], [
2b6a8fdc 183 22, 21, 28, 21,
696e4e20
KS
184 21, 19, 26, 19,
185 28, 26, 35, 26,
2b6a8fdc 186 21, 19, 26, 19
696e4e20 187 ], [
2b6a8fdc 188 26, 24, 33, 24,
696e4e20
KS
189 24, 23, 31, 23,
190 33, 31, 42, 31,
2b6a8fdc 191 24, 23, 31, 23
696e4e20 192 ], [
2b6a8fdc 193 28, 26, 35, 26,
696e4e20
KS
194 26, 25, 33, 25,
195 35, 33, 45, 33,
2b6a8fdc 196 26, 25, 33, 25
696e4e20 197 ], [
2b6a8fdc 198 32, 30, 40, 30,
696e4e20
KS
199 30, 28, 38, 28,
200 40, 38, 51, 38,
2b6a8fdc 201 30, 28, 38, 28
696e4e20 202 ], [
2b6a8fdc 203 36, 34, 46, 34,
696e4e20
KS
204 34, 32, 43, 32,
205 46, 43, 58, 43,
2b6a8fdc 206 34, 32, 43, 32
696e4e20
KS
207 ]
208];
209
210pub fn dequant8x8(blk: &mut [i16; 64], slist: &[u8; 64]) {
211 for (el, &scan) in blk.iter_mut().zip(ZIGZAG8X8.iter()) {
212 if *el != 0 {
213 *el = el.wrapping_mul(i16::from(slist[scan]));
214 }
215 }
216}
217
218pub fn idct8x8(blk: &mut [i16; 64], qp: u8) {
219 let mut tmp = [0i32; 64];
220 let qmat = &QMAT_8X8[(qp % 6) as usize];
221 if qp >= 36 {
222 let shift = qp / 6 - 6;
223 for (i, (dst, &src)) in tmp.iter_mut().zip(blk.iter()).enumerate() {
224 let x = i & 7;
225 let y = i >> 3;
226 let idx = (x & 3) + (y & 3) * 4;
227 *dst = i32::from(src).wrapping_mul(i32::from(qmat[idx])) << shift;
228 }
229 } else {
230 let shift = 6 - qp / 6;
231 let bias = (1 << shift) >> 1;
232 for (i, (dst, &src)) in tmp.iter_mut().zip(blk.iter()).enumerate() {
233 let x = i & 7;
234 let y = i >> 3;
235 let idx = (x & 3) + (y & 3) * 4;
236 *dst = i32::from(src).wrapping_mul(i32::from(qmat[idx])).wrapping_add(bias) >> shift;
237 }
238 }
239 for row in tmp.chunks_mut(8) {
240 transform!(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]);
241 }
242 for col in 0..8 {
243 transform!(tmp[col], tmp[col + 8], tmp[col + 8 * 2], tmp[col + 8 * 3],
244 tmp[col + 8 * 4], tmp[col + 8 * 5], tmp[col + 8 * 6], tmp[col + 8 * 7]);
245 }
246 for (dst, &src) in blk.iter_mut().zip(tmp.iter()) {
247 *dst = ((src + 0x20) >> 6) as i16;
248 }
249}
250
251pub fn add_coeffs(dst: &mut [u8], offset: usize, stride: usize, coeffs: &[i16]) {
252 let out = &mut dst[offset..][..stride * 3 + 4];
253 for (line, src) in out.chunks_mut(stride).take(4).zip(coeffs.chunks(4)) {
254 for (dst, src) in line.iter_mut().take(4).zip(src.iter()) {
255 *dst = (i32::from(*dst) + i32::from(*src)).max(0).min(255) as u8;
256 }
257 }
258}
259
260pub fn add_coeffs8(dst: &mut [u8], offset: usize, stride: usize, coeffs: &[i16; 64]) {
261 let out = &mut dst[offset..];
262 for (line, src) in out.chunks_mut(stride).take(8).zip(coeffs.chunks(8)) {
263 for (dst, src) in line.iter_mut().take(8).zip(src.iter()) {
264 *dst = (i32::from(*dst) + i32::from(*src)).max(0).min(255) as u8;
265 }
266 }
267}
268
269pub fn avg(dst: &mut [u8], dstride: usize,
270 src: &[u8], sstride: usize, bw: usize, bh: usize) {
271 for (dline, sline) in dst.chunks_mut(dstride).zip(src.chunks(sstride)).take(bh) {
272 for (dst, src) in dline.iter_mut().zip(sline.iter()).take(bw) {
273 *dst = ((u16::from(*dst) + u16::from(*src) + 1) >> 1) as u8;
274 }
275 }
276}
277
278fn clip8(val: i16) -> u8 { val.max(0).min(255) as u8 }
279
22de733b
KS
280fn ipred_dc128(buf: &mut [u8], stride: usize, bsize: usize) {
281 for row in buf.chunks_mut(stride).take(bsize) {
282 for el in row[..bsize].iter_mut() {
283 *el = 128;
284 }
696e4e20
KS
285 }
286}
22de733b
KS
287fn ipred_ver(buf: &mut [u8], stride: usize, top: &[u8], bsize: usize) {
288 for row in buf.chunks_mut(stride).take(bsize) {
289 row[..bsize].copy_from_slice(&top[..bsize]);
696e4e20
KS
290 }
291}
22de733b
KS
292fn ipred_hor(buf: &mut [u8], stride: usize, left: &[u8], bsize: usize) {
293 for (row, &left) in buf.chunks_mut(stride).zip(left[1..].iter()).take(bsize) {
294 for el in row[..bsize].iter_mut() {
295 *el = left;
296 }
696e4e20
KS
297 }
298}
22de733b 299fn ipred_dc(buf: &mut [u8], stride: usize, top: &[u8], left: &[u8], bsize: usize, shift: u8) {
696e4e20 300 let mut adc: u16 = 0;
22de733b
KS
301 for i in 0..bsize { adc += u16::from(top[i]); }
302 for i in 0..bsize { adc += u16::from(left[i + 1]); }
696e4e20
KS
303 let dc = ((adc + (1 << (shift - 1))) >> shift) as u8;
304
22de733b
KS
305 for row in buf.chunks_mut(stride).take(bsize) {
306 for el in row[..bsize].iter_mut() {
307 *el = dc;
308 }
696e4e20
KS
309 }
310}
22de733b 311fn ipred_left_dc(buf: &mut [u8], stride: usize, left: &[u8], bsize: usize, shift: u8) {
696e4e20 312 let mut adc: u16 = 0;
22de733b 313 for i in 0..bsize { adc += u16::from(left[i + 1]); }
696e4e20
KS
314 let dc = ((adc + (1 << (shift - 1))) >> shift) as u8;
315
22de733b
KS
316 for row in buf.chunks_mut(stride).take(bsize) {
317 for el in row[..bsize].iter_mut() {
318 *el = dc;
319 }
696e4e20
KS
320 }
321}
22de733b 322fn ipred_top_dc(buf: &mut [u8], stride: usize, top: &[u8], bsize: usize, shift: u8) {
696e4e20 323 let mut adc: u16 = 0;
22de733b 324 for i in 0..bsize { adc += u16::from(top[i]); }
696e4e20
KS
325 let dc = ((adc + (1 << (shift - 1))) >> shift) as u8;
326
22de733b
KS
327 for row in buf.chunks_mut(stride).take(bsize) {
328 for el in row[..bsize].iter_mut() {
329 *el = dc;
330 }
696e4e20
KS
331 }
332}
333
22de733b
KS
334fn load(dst: &mut [u16], src: &[u8]) {
335 for (dst, &src) in dst.iter_mut().zip(src.iter()) {
336 *dst = u16::from(src);
337 }
696e4e20
KS
338}
339
22de733b
KS
340fn ipred_4x4_ver(buf: &mut [u8], stride: usize, top: &[u8], _left: &[u8], _tr: &[u8]) {
341 ipred_ver(buf, stride, top, 4);
696e4e20 342}
22de733b
KS
343fn ipred_4x4_hor(buf: &mut [u8], stride: usize, _top: &[u8], left: &[u8], _tr: &[u8]) {
344 ipred_hor(buf, stride, left, 4);
696e4e20 345}
22de733b 346fn ipred_4x4_diag_down_left(buf: &mut [u8], stride: usize, top: &[u8], _left: &[u8], tr: &[u8]) {
696e4e20 347 let mut t: [u16; 9] = [0; 9];
22de733b
KS
348 load(&mut t[..4], top);
349 load(&mut t[4..8], tr);
696e4e20
KS
350 t[8] = t[7];
351
696e4e20 352 for i in 0..4 {
22de733b 353 buf[i] = ((t[i] + 2 * t[i + 1] + t[i + 2] + 2) >> 2) as u8;
696e4e20 354 }
22de733b 355 let dst = &mut buf[stride..];
696e4e20
KS
356 for i in 0..4 {
357 dst[i] = ((t[i + 1] + 2 * t[i + 2] + t[i + 3] + 2) >> 2) as u8;
358 }
22de733b 359 let dst = &mut buf[stride * 2..];
696e4e20
KS
360 for i in 0..4 {
361 dst[i] = ((t[i + 2] + 2 * t[i + 3] + t[i + 4] + 2) >> 2) as u8;
362 }
22de733b 363 let dst = &mut buf[stride * 3..];
696e4e20
KS
364 for i in 0..4 {
365 dst[i] = ((t[i + 3] + 2 * t[i + 4] + t[i + 5] + 2) >> 2) as u8;
366 }
367}
22de733b 368fn ipred_4x4_diag_down_right(buf: &mut [u8], stride: usize, top: &[u8], left: &[u8], _tr: &[u8]) {
696e4e20 369 let mut t: [u16; 5] = [0; 5];
22de733b
KS
370 t[0] = u16::from(left[0]);
371 load(&mut t[1..], &top);
696e4e20 372 let mut l: [u16; 5] = [0; 5];
22de733b
KS
373 load(&mut l, left);
374 let dst = buf;
696e4e20
KS
375
376 for j in 0..4 {
377 for i in 0..j {
378 dst[i + j * stride] = ((l[j - i - 1] + 2 * l[j - i] + l[j - i + 1] + 2) >> 2) as u8;
379 }
380 dst[j + j * stride] = ((l[1] + 2 * l[0] + t[1] + 2) >> 2) as u8;
381 for i in (j+1)..4 {
382 dst[i + j * stride] = ((t[i - j - 1] + 2 * t[i - j] + t[i - j + 1] + 2) >> 2) as u8;
383 }
384 }
385}
22de733b 386fn ipred_4x4_ver_right(buf: &mut [u8], stride: usize, top: &[u8], left: &[u8], _tr: &[u8]) {
696e4e20 387 let mut t: [u16; 5] = [0; 5];
22de733b
KS
388 t[0] = u16::from(left[0]);
389 load(&mut t[1..], &top);
696e4e20 390 let mut l: [u16; 5] = [0; 5];
22de733b
KS
391 load(&mut l, left);
392 let dst = buf;
696e4e20
KS
393
394 for j in 0..4 {
395 for i in 0..4 {
396 let zvr = ((2 * i) as i8) - (j as i8);
397 let pix;
398 if zvr >= 0 {
399 if (zvr & 1) == 0 {
400 pix = (t[i - (j >> 1)] + t[i - (j >> 1) + 1] + 1) >> 1;
401 } else {
402 pix = (t[i - (j >> 1) - 1] + 2 * t[i - (j >> 1)] + t[i - (j >> 1) + 1] + 2) >> 2;
403 }
404 } else {
405 if zvr == -1 {
406 pix = (l[1] + 2 * l[0] + t[1] + 2) >> 2;
407 } else {
408 pix = (l[j] + 2 * l[j - 1] + l[j - 2] + 2) >> 2;
409 }
410 }
411 dst[i + j * stride] = pix as u8;
412 }
413 }
414}
22de733b 415fn ipred_4x4_ver_left(buf: &mut [u8], stride: usize, top: &[u8], _left: &[u8], tr: &[u8]) {
696e4e20 416 let mut t: [u16; 8] = [0; 8];
22de733b
KS
417 load(&mut t[..4], &top);
418 load(&mut t[4..], tr);
419 let dst = buf;
696e4e20
KS
420
421 dst[0 + 0 * stride] = ((t[0] + t[1] + 1) >> 1) as u8;
422 let pix = ((t[1] + t[2] + 1) >> 1) as u8;
423 dst[1 + 0 * stride] = pix;
424 dst[0 + 2 * stride] = pix;
425 let pix = ((t[2] + t[3] + 1) >> 1) as u8;
426 dst[2 + 0 * stride] = pix;
427 dst[1 + 2 * stride] = pix;
428 let pix = ((t[3] + t[4] + 1) >> 1) as u8;
429 dst[3 + 0 * stride] = pix;
430 dst[2 + 2 * stride] = pix;
431 dst[3 + 2 * stride] = ((t[4] + t[5] + 1) >> 1) as u8;
432 dst[0 + 1 * stride] = ((t[0] + 2*t[1] + t[2] + 2) >> 2) as u8;
433 let pix = ((t[1] + 2*t[2] + t[3] + 2) >> 2) as u8;
434 dst[1 + 1 * stride] = pix;
435 dst[0 + 3 * stride] = pix;
436 let pix = ((t[2] + 2*t[3] + t[4] + 2) >> 2) as u8;
437 dst[2 + 1 * stride] = pix;
438 dst[1 + 3 * stride] = pix;
439 let pix = ((t[3] + 2*t[4] + t[5] + 2) >> 2) as u8;
440 dst[3 + 1 * stride] = pix;
441 dst[2 + 3 * stride] = pix;
442 dst[3 + 3 * stride] = ((t[4] + 2*t[5] + t[6] + 2) >> 2) as u8;
443}
22de733b 444fn ipred_4x4_hor_down(buf: &mut [u8], stride: usize, top: &[u8], left: &[u8], _tr: &[u8]) {
696e4e20 445 let mut t: [u16; 5] = [0; 5];
22de733b
KS
446 t[0] = u16::from(left[0]);
447 load(&mut t[1..], &top);
696e4e20 448 let mut l: [u16; 5] = [0; 5];
22de733b
KS
449 load(&mut l, left);
450 let dst = buf;
696e4e20
KS
451
452 for j in 0..4 {
453 for i in 0..4 {
454 let zhd = ((2 * j) as i8) - (i as i8);
455 let pix;
456 if zhd >= 0 {
457 if (zhd & 1) == 0 {
458 pix = (l[j - (i >> 1)] + l[j - (i >> 1) + 1] + 1) >> 1;
459 } else {
460 pix = (l[j - (i >> 1) - 1] + 2 * l[j - (i >> 1)] + l[j - (i >> 1) + 1] + 2) >> 2;
461 }
462 } else {
463 if zhd == -1 {
464 pix = (l[1] + 2 * l[0] + t[1] + 2) >> 2;
465 } else {
466 pix = (t[i - 2] + 2 * t[i - 1] + t[i] + 2) >> 2;
467 }
468 }
469 dst[i + j * stride] = pix as u8;
470 }
471 }
472}
22de733b 473fn ipred_4x4_hor_up(buf: &mut [u8], stride: usize, _top: &[u8], left: &[u8], _tr: &[u8]) {
696e4e20 474 let mut l: [u16; 8] = [0; 8];
22de733b
KS
475 load(&mut l, &left[1..]);
476 let dst = buf;
696e4e20
KS
477
478 dst[0 + 0 * stride] = ((l[0] + l[1] + 1) >> 1) as u8;
479 dst[1 + 0 * stride] = ((l[0] + 2*l[1] + l[2] + 2) >> 2) as u8;
480 let pix = ((l[1] + l[2] + 1) >> 1) as u8;
481 dst[2 + 0 * stride] = pix;
482 dst[0 + 1 * stride] = pix;
483 let pix = ((l[1] + 2*l[2] + l[3] + 2) >> 2) as u8;
484 dst[3 + 0 * stride] = pix;
485 dst[1 + 1 * stride] = pix;
486 let pix = ((l[2] + l[3] + 1) >> 1) as u8;
487 dst[2 + 1 * stride] = pix;
488 dst[0 + 2 * stride] = pix;
489 let pix = ((l[2] + 3*l[3] + 2) >> 2) as u8;
490 dst[3 + 1 * stride] = pix;
491 dst[1 + 2 * stride] = pix;
492 dst[3 + 2 * stride] = l[3] as u8;
493 dst[1 + 3 * stride] = l[3] as u8;
494 dst[0 + 3 * stride] = l[3] as u8;
495 dst[2 + 2 * stride] = l[3] as u8;
496 dst[2 + 3 * stride] = l[3] as u8;
497 dst[3 + 3 * stride] = l[3] as u8;
498}
22de733b
KS
499fn ipred_4x4_dc(buf: &mut [u8], stride: usize, top: &[u8], left: &[u8], _tr: &[u8]) {
500 ipred_dc(buf, stride, top, left, 4, 3);
696e4e20 501}
22de733b
KS
502fn ipred_4x4_left_dc(buf: &mut [u8], stride: usize, _top: &[u8], left: &[u8], _tr: &[u8]) {
503 ipred_left_dc(buf, stride, left, 4, 2);
696e4e20 504}
22de733b
KS
505fn ipred_4x4_top_dc(buf: &mut [u8], stride: usize, top: &[u8], _left: &[u8], _tr: &[u8]) {
506 ipred_top_dc(buf, stride, top, 4, 2);
696e4e20 507}
22de733b
KS
508fn ipred_4x4_dc128(buf: &mut [u8], stride: usize, _top: &[u8], _left: &[u8], _tr: &[u8]) {
509 ipred_dc128(buf, stride, 4);
696e4e20
KS
510}
511
512pub struct IPred8Context {
513 pub t: [u8; 16],
514 pub l: [u8; 8],
515 pub tl: u8,
516}
517
518impl IPred8Context {
519 pub fn new() -> Self {
520 Self {
521 t: [128; 16],
522 l: [128; 8],
523 tl: 128,
524 }
525 }
22de733b 526 pub fn fill(&mut self, top: &[u8], left: &[u8], has_t: bool, has_tr: bool, has_l: bool, has_tl: bool) {
696e4e20
KS
527 let mut t = [0x80u8; 19];
528 let mut l = [0x80u8; 11];
529 if has_t {
22de733b 530 t[1..8 + 1].copy_from_slice(&top[..8]);
696e4e20
KS
531 }
532 if has_tr {
22de733b 533 t[8 + 1..16 + 1].copy_from_slice(&top[8..][..8]);
696e4e20
KS
534 t[16 + 1] = t[15 + 1];
535 t[17 + 1] = t[15 + 1];
536 } else {
537 let (t0, t1) = t.split_at_mut(8 + 1);
538 for el in t1.iter_mut() {
539 *el = t0[7 + 1];
540 }
541 }
542 if has_l {
22de733b 543 l[1..9].copy_from_slice(&left[1..9]);
696e4e20
KS
544 l[8 + 1] = l[7 + 1];
545 l[9 + 1] = l[7 + 1];
546 }
547 if has_tl {
22de733b
KS
548 t[0] = left[0];
549 l[0] = left[0];
696e4e20
KS
550 } else {
551 t[0] = t[1];
552 l[0] = l[1];
553 }
554
555 for i in 0..16 {
556 self.t[i] = ((u16::from(t[i]) + 2 * u16::from(t[i + 1]) + u16::from(t[i + 2]) + 2) >> 2) as u8;
557 }
558 for i in 0..8 {
559 self.l[i] = ((u16::from(l[i]) + 2 * u16::from(l[i + 1]) + u16::from(l[i + 2]) + 2) >> 2) as u8;
560 }
561 self.tl = if has_t && has_l {
562 ((u16::from(t[1]) + 2 * u16::from(t[0]) + u16::from(l[1]) + 2) >> 2) as u8
563 } else if has_t {
564 ((3 * u16::from(t[0]) + u16::from(t[1]) + 2) >> 2) as u8
565 } else if has_l {
566 ((3 * u16::from(l[0]) + u16::from(l[1]) + 2) >> 2) as u8
567 } else {
568 t[0]
569 };
570 }
571}
572
573fn ipred_y_8x8_ver(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
574 for row in buf.chunks_mut(stride).take(8) {
575 row[..8].copy_from_slice(&ctx.t[..8]);
576 }
577}
578fn ipred_y_8x8_hor(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
579 for (row, &l) in buf.chunks_mut(stride).zip(ctx.l.iter()).take(8) {
580 row[..8].copy_from_slice(&[l; 8]);
581 }
582}
583fn ipred_y_8x8_diag_down_left(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
584 let mut t = [0u16; 16];
22de733b 585 load(&mut t, &ctx.t);
696e4e20
KS
586
587 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
588 for (x, pix) in row.iter_mut().take(8).enumerate() {
589 *pix = ((if (x != 7) || (y != 7) {
590 t[x + y] + 2 * t[x + y + 1] + t[x + y + 2]
591 } else {
592 t[14] + 3 * t[15]
593 } + 2) >> 2) as u8;
594 }
595 }
596}
597fn ipred_y_8x8_diag_down_right(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
598 let mut t = [0u16; 9];
599 t[0] = u16::from(ctx.tl);
22de733b 600 load(&mut t[1..], &ctx.t);
696e4e20
KS
601 let mut l = [0u16; 9];
602 l[0] = u16::from(ctx.tl);
22de733b 603 load(&mut l[1..], &ctx.l);
696e4e20
KS
604 let diag = t[1] + 2 * t[0] + l[1];
605
606 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
607 for (x, pix) in row.iter_mut().take(8).enumerate() {
608 *pix = ((if x > y {
609 t[x - y - 1] + 2 * t[x - y] + t[x - y + 1]
610 } else if x < y {
611 l[y - x - 1] + 2 * l[y - x] + l[y - x + 1]
612 } else {
613 diag
614 } + 2) >> 2) as u8;
615 }
616 }
617}
618fn ipred_y_8x8_ver_right(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
619 let mut t = [0u16; 9];
620 t[0] = u16::from(ctx.tl);
22de733b 621 load(&mut t[1..], &ctx.t);
696e4e20
KS
622 let mut l = [0u16; 9];
623 l[0] = u16::from(ctx.tl);
22de733b 624 load(&mut l[1..], &ctx.l);
696e4e20
KS
625
626 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
627 for (x, pix) in row.iter_mut().take(8).enumerate() {
628 let zvr = 2 * (x as i8) - (y as i8);
629 *pix = if zvr >= 0 {
630 let ix = x - (y >> 1);
631 if (zvr & 1) == 0 {
632 (t[ix] + t[ix + 1] + 1) >> 1
633 } else {
634 (t[ix - 1] + 2 * t[ix] + t[ix + 1] + 2) >> 2
635 }
636 } else if zvr == -1 {
2b6a8fdc 637 (l[1] + 2 * l[0] + t[1] + 2) >> 2
696e4e20
KS
638 } else {
639 let ix = y - 2 * x;
640 (l[ix] + 2 * l[ix - 1] + l[ix - 2] + 2) >> 2
641 } as u8;
642 }
643 }
644}
645fn ipred_y_8x8_ver_left(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
646 let mut t = [0u16; 16];
22de733b 647 load(&mut t, &ctx.t);
696e4e20
KS
648
649 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
650 for (x, pix) in row.iter_mut().take(8).enumerate() {
651 let ix = x + (y >> 1);
652 *pix = if (y & 1) == 0 {
653 (t[ix] + t[ix + 1] + 1) >> 1
654 } else {
655 (t[ix] + 2 * t[ix + 1] + t[ix + 2] + 2) >> 2
656 } as u8;
657 }
658 }
659
660}
661fn ipred_y_8x8_hor_down(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
662 let mut t = [0u16; 9];
663 t[0] = u16::from(ctx.tl);
22de733b 664 load(&mut t[1..], &ctx.t);
696e4e20
KS
665 let mut l = [0u16; 9];
666 l[0] = u16::from(ctx.tl);
22de733b 667 load(&mut l[1..], &ctx.l);
696e4e20
KS
668
669 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
670 for (x, pix) in row.iter_mut().take(8).enumerate() {
671 let zhd = 2 * (y as i8) - (x as i8);
672 *pix = if zhd >= 0 {
673 let ix = y - (x >> 1);
674 if (zhd & 1) == 0 {
675 (l[ix] + l[ix + 1] + 1) >> 1
676 } else {
677 (l[ix - 1] + 2 * l[ix] + l[ix + 1] + 2) >> 2
678 }
679 } else if zhd == -1 {
2b6a8fdc 680 (l[1] + 2 * l[0] + t[1] + 2) >> 2
696e4e20
KS
681 } else {
682 let ix = x - 2 * y;
683 (t[ix] + 2 * t[ix - 1] + t[ix - 2] + 2) >> 2
684 } as u8;
685 }
686 }
687}
688fn ipred_y_8x8_hor_up(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
689 let mut l = [0u16; 8];
22de733b 690 load(&mut l, &ctx.l);
696e4e20
KS
691
692 for (y, row) in buf.chunks_mut(stride).take(8).enumerate() {
693 for (x, pix) in row.iter_mut().take(8).enumerate() {
694 let zhu = x + 2 * y;
695 let ix = y + (x >> 1);
696 *pix = if zhu > 13 {
697 l[7]
698 } else if zhu == 13 {
699 (l[6] + 3 * l[7] + 2) >> 2
700 } else if (zhu & 1) != 0 {
701 (l[ix] + 2 * l[ix + 1] + l[ix + 2] + 2) >> 2
702 } else {
703 (l[ix] + l[ix + 1] + 1) >> 1
704 } as u8;
705 }
706 }
707}
708fn ipred_y_8x8_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
709 let mut sum = 0u16;
710 for &t in ctx.t[..8].iter() {
711 sum += u16::from(t);
712 }
713 for &l in ctx.l[..8].iter() {
714 sum += u16::from(l);
715 }
716 let dc = ((sum + 8) >> 4) as u8;
717 for row in buf.chunks_mut(stride).take(8) {
718 for pix in row.iter_mut().take(8) {
719 *pix = dc;
720 }
721 }
722}
723fn ipred_y_8x8_left_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
724 let mut sum = 0u16;
725 for &l in ctx.l[..8].iter() {
726 sum += u16::from(l);
727 }
728 let dc = ((sum + 4) >> 3) as u8;
729 for row in buf.chunks_mut(stride).take(8) {
730 for pix in row.iter_mut().take(8) {
731 *pix = dc;
732 }
733 }
734}
735fn ipred_y_8x8_top_dc(buf: &mut [u8], stride: usize, ctx: &IPred8Context) {
736 let mut sum = 0u16;
737 for &t in ctx.t[..8].iter() {
738 sum += u16::from(t);
739 }
740 let dc = ((sum + 4) >> 3) as u8;
741 for row in buf.chunks_mut(stride).take(8) {
742 for pix in row.iter_mut().take(8) {
743 *pix = dc;
744 }
745 }
746}
747fn ipred_y_8x8_dc128(buf: &mut [u8], stride: usize, _ctx: &IPred8Context) {
22de733b 748 ipred_dc128(buf, stride, 8);
696e4e20
KS
749}
750
22de733b
KS
751fn ipred_8x8_ver(buf: &mut [u8], stride: usize, top: &[u8], _left: &[u8]) {
752 ipred_ver(buf, stride, top, 8);
696e4e20 753}
22de733b
KS
754fn ipred_8x8_hor(buf: &mut [u8], stride: usize, _top: &[u8], left: &[u8]) {
755 ipred_hor(buf, stride, left, 8);
696e4e20 756}
22de733b
KS
757fn ipred_8x8_dc(buf: &mut [u8], stride: usize, top: &[u8], left: &[u8]) {
758 let mut l = [0; 8];
759 load(&mut l, &left[1..]);
760 let mut t = [0; 8];
761 load(&mut t, &top);
696e4e20
KS
762
763 let dc0 = ((t[0] + t[1] + t[2] + t[3] + l[0] + l[1] + l[2] + l[3] + 4) >> 3) as u8;
764 let sum1 = t[4] + t[5] + t[6] + t[7];
765 let dc1 = ((sum1 + 2) >> 2) as u8;
766 let sum2 = l[4] + l[5] + l[6] + l[7];
767 let dc2 = ((sum2 + 2) >> 2) as u8;
768 let dc3 = ((sum1 + sum2 + 4) >> 3) as u8;
769
22de733b 770 for row in buf.chunks_mut(stride).take(4) {
696e4e20
KS
771 row[..4].copy_from_slice(&[dc0; 4]);
772 row[4..8].copy_from_slice(&[dc1; 4]);
773 }
22de733b 774 for row in buf.chunks_mut(stride).skip(4).take(4) {
696e4e20
KS
775 row[..4].copy_from_slice(&[dc2; 4]);
776 row[4..8].copy_from_slice(&[dc3; 4]);
777 }
778}
22de733b 779fn ipred_8x8_left_dc(buf: &mut [u8], stride: usize, _top: &[u8], left: &[u8]) {
696e4e20
KS
780 let mut left_dc0 = 0;
781 let mut left_dc1 = 0;
22de733b
KS
782 for &el in left[1..].iter().take(4) {
783 left_dc0 += u16::from(el);
696e4e20 784 }
22de733b
KS
785 for &el in left[1..].iter().skip(4).take(4) {
786 left_dc1 += u16::from(el);
696e4e20
KS
787 }
788 let dc0 = ((left_dc0 + 2) >> 2) as u8;
789 let dc2 = ((left_dc1 + 2) >> 2) as u8;
22de733b 790 for row in buf.chunks_mut(stride).take(4) {
696e4e20
KS
791 row[..8].copy_from_slice(&[dc0; 8]);
792 }
22de733b 793 for row in buf.chunks_mut(stride).skip(4).take(4) {
696e4e20
KS
794 row[..8].copy_from_slice(&[dc2; 8]);
795 }
796}
22de733b
KS
797fn ipred_8x8_top_dc(buf: &mut [u8], stride: usize, top: &[u8], _left: &[u8]) {
798 ipred_top_dc(buf, stride, top, 4, 2);
799 ipred_top_dc(&mut buf[4..], stride, &top[4..], 4, 2);
800 let mut top = [0; 8];
801 top.copy_from_slice(&buf[stride * 3..][..8]);
802 ipred_top_dc(&mut buf[4 * stride..], stride, &top, 4, 2);
803 ipred_top_dc(&mut buf[4 + 4 * stride..], stride, &top[4..], 4, 2);
696e4e20 804}
22de733b
KS
805fn ipred_8x8_dc128(buf: &mut [u8], stride: usize, _top: &[u8], _left: &[u8]) {
806 ipred_dc128(buf, stride, 8);
696e4e20 807}
22de733b
KS
808fn ipred_8x8_plane(buf: &mut [u8], stride: usize, top: &[u8], left: &[u8]) {
809 let mut h: i32 = 4 * (i32::from(top[7]) - i32::from(left[0]));
810 let mut v: i32 = 4 * (i32::from(left[8]) - i32::from(left[0]));
811 for i in 0..3 {
696e4e20 812 let i1 = (i + 1) as i32;
22de733b
KS
813 h += i1 * (i32::from(top[4 + i]) - i32::from(top[2 - i]));
814 v += i1 * (i32::from(left[5 + i]) - i32::from(left[3 - i]));
696e4e20
KS
815 }
816 let b = (17 * h + 16) >> 5;
817 let c = (17 * v + 16) >> 5;
22de733b
KS
818 let mut a = 16 * (i32::from(left[8]) + i32::from(top[7])) - 3 * (b + c) + 16;
819 for line in buf.chunks_mut(stride).take(8) {
696e4e20
KS
820 let mut acc = a;
821 for el in line.iter_mut().take(8) {
822 *el = clip8((acc >> 5) as i16);
823 acc += b;
824 }
825 a += c;
826 }
827}
828
22de733b
KS
829fn ipred_16x16_ver(buf: &mut [u8], stride: usize, top: &[u8], _left: &[u8]) {
830 ipred_ver(buf, stride, top, 16);
696e4e20 831}
22de733b
KS
832fn ipred_16x16_hor(buf: &mut [u8], stride: usize, _top: &[u8], left: &[u8]) {
833 ipred_hor(buf, stride, left, 16);
696e4e20 834}
22de733b
KS
835fn ipred_16x16_dc(buf: &mut [u8], stride: usize, top: &[u8], left: &[u8]) {
836 ipred_dc(buf, stride, top, left, 16, 5);
696e4e20 837}
22de733b
KS
838fn ipred_16x16_left_dc(buf: &mut [u8], stride: usize, _top: &[u8], left: &[u8]) {
839 ipred_left_dc(buf, stride, left, 16, 4);
696e4e20 840}
22de733b
KS
841fn ipred_16x16_top_dc(buf: &mut [u8], stride: usize, top: &[u8], _left: &[u8]) {
842 ipred_top_dc(buf, stride, top, 16, 4);
696e4e20 843}
22de733b
KS
844fn ipred_16x16_dc128(buf: &mut [u8], stride: usize, _top: &[u8], _left: &[u8]) {
845 ipred_dc128(buf, stride, 16);
696e4e20 846}
22de733b
KS
847fn ipred_16x16_plane(buf: &mut [u8], stride: usize, top: &[u8], left: &[u8]) {
848 let mut h = 8 * (i32::from(top[15]) - i32::from(left[0]));
849 let mut v = 8 * (i32::from(left[16]) - i32::from(left[0]));
850 for k in 0..7 {
851 h += ((k as i32) + 1) * (i32::from(top[8 + k]) - i32::from(top[6 - k]));
852 v += ((k as i32) + 1) * (i32::from(left[9 + k]) - i32::from(left[7 - k]));
696e4e20 853 }
22de733b 854
696e4e20
KS
855 h = (5 * h + 32) >> 6;
856 v = (5 * v + 32) >> 6;
857
22de733b 858 let mut a = 16 * (i32::from(left[16]) + i32::from(top[15]) + 1) - 7 * (v + h);
696e4e20 859
22de733b 860 for row in buf.chunks_mut(stride).take(16) {
696e4e20
KS
861 let mut b = a;
862 a += v;
863
864 for dst in row.chunks_exact_mut(4).take(4) {
865 dst[0] = clip8(((b ) >> 5) as i16);
866 dst[1] = clip8(((b + h) >> 5) as i16);
867 dst[2] = clip8(((b + 2*h) >> 5) as i16);
868 dst[3] = clip8(((b + 3*h) >> 5) as i16);
869 b += h * 4;
870 }
871 }
872}
873
22de733b
KS
874pub type IPred4x4Func = fn(buf: &mut [u8], stride: usize, top: &[u8], left: &[u8], tr: &[u8]);
875pub type IPred8x8Func = fn(buf: &mut [u8], stride: usize, top: &[u8], left: &[u8]);
696e4e20
KS
876pub type IPred8x8LumaFunc = fn(buf: &mut [u8], stride: usize, ctx: &IPred8Context);
877
878pub const IPRED4_DC128: usize = 11;
879pub const IPRED4_DC_TOP: usize = 10;
880pub const IPRED4_DC_LEFT: usize = 9;
881pub const IPRED8_DC128: usize = 6;
882pub const IPRED8_DC_TOP: usize = 5;
883pub const IPRED8_DC_LEFT: usize = 4;
884
885pub const IPRED_FUNCS4X4: [IPred4x4Func; 12] = [
886 ipred_4x4_ver, ipred_4x4_hor, ipred_4x4_dc,
887 ipred_4x4_diag_down_left, ipred_4x4_diag_down_right,
888 ipred_4x4_ver_right, ipred_4x4_hor_down, ipred_4x4_ver_left, ipred_4x4_hor_up,
889 ipred_4x4_left_dc, ipred_4x4_top_dc, ipred_4x4_dc128
890];
891
892pub const IPRED_FUNCS8X8_LUMA: [IPred8x8LumaFunc; 12] = [
893 ipred_y_8x8_ver, ipred_y_8x8_hor, ipred_y_8x8_dc,
894 ipred_y_8x8_diag_down_left, ipred_y_8x8_diag_down_right,
895 ipred_y_8x8_ver_right, ipred_y_8x8_hor_down,
896 ipred_y_8x8_ver_left, ipred_y_8x8_hor_up,
897 ipred_y_8x8_left_dc, ipred_y_8x8_top_dc, ipred_y_8x8_dc128
898];
899
900pub const IPRED_FUNCS8X8_CHROMA: [IPred8x8Func; 7] = [
901 ipred_8x8_dc, ipred_8x8_hor, ipred_8x8_ver, ipred_8x8_plane,
902 ipred_8x8_left_dc, ipred_8x8_top_dc, ipred_8x8_dc128
903];
904
905pub const IPRED_FUNCS16X16: [IPred8x8Func; 7] = [
906 ipred_16x16_ver, ipred_16x16_hor, ipred_16x16_dc, ipred_16x16_plane,
907 ipred_16x16_left_dc, ipred_16x16_top_dc, ipred_16x16_dc128
908];
909
910fn clip_u8(val: i16) -> u8 { val.max(0).min(255) as u8 }
911
696e4e20
KS
912pub fn do_mc(frm: &mut NASimpleVideoFrame<u8>, refpic: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV) {
913 let mode = ((mv.x & 3) + (mv.y & 3) * 4) as usize;
914 copy_block(frm, refpic.clone(), 0, xpos, ypos, mv.x >> 2, mv.y >> 2, w, h, 2, 3, mode, H264_LUMA_INTERP);
915
916 let (cw, ch) = refpic.get_dimensions(1);
917 let mvx = mv.x >> 3;
918 let mvy = mv.y >> 3;
919 let dx = (mv.x & 7) as u16;
920 let dy = (mv.y & 7) as u16;
921 let mut ebuf = [0u8; 18 * 9];
922 let src_x = ((xpos >> 1) as isize) + (mvx as isize);
923 let src_y = ((ypos >> 1) as isize) + (mvy as isize);
924 let suoff = refpic.get_offset(1);
925 let svoff = refpic.get_offset(2);
926 let sustride = refpic.get_stride(1);
927 let svstride = refpic.get_stride(2);
928 let src = refpic.get_data();
929 let cbw = w / 2;
930 let cbh = h / 2;
931 let (csrc, cstride) = if (src_x < 0) || (src_x + (cbw as isize) + 1 > (cw as isize)) || (src_y < 0) || (src_y + (cbh as isize) + 1 > (ch as isize)) {
932 edge_emu(&refpic, src_x, src_y, cbw+1, cbh+1, &mut ebuf, 18, 1, 4);
933 edge_emu(&refpic, src_x, src_y, cbw+1, cbh+1, &mut ebuf[9..], 18, 2, 4);
934 ([&ebuf, &ebuf[9..]], [18, 18])
935 } else {
936 ([&src[suoff + (src_x as usize) + (src_y as usize) * sustride..],
937 &src[svoff + (src_x as usize) + (src_y as usize) * svstride..]],
938 [sustride, svstride])
939 };
940 for chroma in 1..3 {
941 let off = frm.offset[chroma] + xpos / 2 + (ypos / 2) * frm.stride[chroma];
942 chroma_interp(&mut frm.data[off..], frm.stride[chroma], csrc[chroma - 1], cstride[chroma - 1], dx, dy, cbw, cbh);
943 }
944}
945
495b7ec0
KS
946pub fn mc_blocks(ydst: &mut [u8], udst: &mut [u8], vdst: &mut [u8], refpic: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV) {
947 let mode = ((mv.x & 3) + (mv.y & 3) * 4) as usize;
948
949 let pre = if mode != 0 { 2 } else { 0 };
950 let post = if mode != 0 { 3 } else { 0 };
951 let (width, height) = refpic.get_dimensions(0);
952 let sx = (xpos as isize) + ((mv.x >> 2) as isize);
953 let sy = (ypos as isize) + ((mv.y >> 2) as isize);
954
955 const EBUF_STRIDE: usize = 32;
956 let mut ebuf = [0u8; EBUF_STRIDE * (16 + 2 + 3)];
957
958 if (sx - pre < 0) || (sx + (w as isize) + post > (width as isize)) ||
959 (sy - pre < 0) || (sy + (h as isize) + post > (height as isize)) {
960 let edge = (pre + post) as usize;
961 edge_emu(&refpic, sx - pre, sy - pre, w + edge, h + edge,
962 &mut ebuf, EBUF_STRIDE, 0, 0);
963 (H264_LUMA_INTERP[mode])(ydst, 16, &ebuf, EBUF_STRIDE, w, h);
964 } else {
965 let sstride = refpic.get_stride(0);
966 let soff = refpic.get_offset(0);
967 let sdta = refpic.get_data();
968 let sbuf: &[u8] = sdta.as_slice();
969 let saddr = soff + ((sx - pre) as usize) + ((sy - pre) as usize) * sstride;
970 (H264_LUMA_INTERP[mode])(ydst, 16, &sbuf[saddr..], sstride, w, h);
971 }
972
973 let (cw, ch) = refpic.get_dimensions(1);
974 let mvx = mv.x >> 3;
975 let mvy = mv.y >> 3;
976 let dx = (mv.x & 7) as u16;
977 let dy = (mv.y & 7) as u16;
978 let src_x = ((xpos >> 1) as isize) + (mvx as isize);
979 let src_y = ((ypos >> 1) as isize) + (mvy as isize);
980 let suoff = refpic.get_offset(1);
981 let svoff = refpic.get_offset(2);
982 let sustride = refpic.get_stride(1);
983 let svstride = refpic.get_stride(2);
984 let src = refpic.get_data();
985 let cbw = w / 2;
986 let cbh = h / 2;
987 let (csrc, cstride) = if (src_x < 0) || (src_x + (cbw as isize) + 1 > (cw as isize)) || (src_y < 0) || (src_y + (cbh as isize) + 1 > (ch as isize)) {
988 edge_emu(&refpic, src_x, src_y, cbw+1, cbh+1, &mut ebuf, 18, 1, 4);
989 edge_emu(&refpic, src_x, src_y, cbw+1, cbh+1, &mut ebuf[9..], 18, 2, 4);
990 ([&ebuf, &ebuf[9..]], [18, 18])
991 } else {
992 ([&src[suoff + (src_x as usize) + (src_y as usize) * sustride..],
993 &src[svoff + (src_x as usize) + (src_y as usize) * svstride..]],
994 [sustride, svstride])
995 };
996 chroma_interp(udst, 16, csrc[0], cstride[0], dx, dy, cbw, cbh);
997 chroma_interp(vdst, 16, csrc[1], cstride[1], dx, dy, cbw, cbh);
998}
999
696e4e20
KS
1000pub fn gray_block(frm: &mut NASimpleVideoFrame<u8>, x: usize, y: usize, w: usize, h: usize) {
1001 let yoff = frm.offset[0] + x + y * frm.stride[0];
1002 let coff = [frm.offset[1] + x / 2 + y / 2 * frm.stride[1],
1003 frm.offset[2] + x / 2 + y / 2 * frm.stride[2]];
1004 if w == 16 && h == 16 {
22de733b 1005 IPRED_FUNCS16X16[IPRED8_DC128](&mut frm.data[yoff..], frm.stride[0], &[], &[]);
696e4e20 1006 for chroma in 1..2 {
22de733b 1007 IPRED_FUNCS8X8_CHROMA[IPRED8_DC128](&mut frm.data[coff[chroma - 1]..], frm.stride[chroma], &[], &[]);
696e4e20
KS
1008 }
1009 } else if w == 8 && h == 8 {
22de733b 1010 IPRED_FUNCS8X8_CHROMA[IPRED8_DC128](&mut frm.data[yoff..], frm.stride[0], &[], &[]);
696e4e20 1011 for chroma in 1..2 {
22de733b 1012 IPRED_FUNCS4X4[IPRED4_DC128](&mut frm.data[coff[chroma - 1]..], frm.stride[chroma], &[128; 4], &[128; 4], &[128; 4]);
696e4e20
KS
1013 }
1014 } else {
1015 for row in frm.data[yoff..].chunks_mut(frm.stride[0]).take(h) {
1016 for el in row[..w].iter_mut() {
1017 *el = 128;
1018 }
1019 }
1020 for chroma in 0..2 {
1021 for row in frm.data[coff[chroma]..].chunks_mut(frm.stride[chroma + 1]).take(h / 2) {
1022 for el in row[..w / 2].iter_mut() {
1023 *el = 128;
1024 }
1025 }
1026 }
1027 }
1028}
1029
1030pub fn do_mc_avg(frm: &mut NASimpleVideoFrame<u8>, refpic: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, w: usize, h: usize, mv: MV, avg_buf: &mut NAVideoBufferRef<u8>) {
1031 let mut afrm = NASimpleVideoFrame::from_video_buf(avg_buf).unwrap();
1032 let amv = MV { x: mv.x + (xpos as i16) * 4, y: mv.y + (ypos as i16) * 4 };
1033 do_mc(&mut afrm, refpic, 0, 0, w, h, amv);
1034 for comp in 0..3 {
1035 let shift = if comp == 0 { 0 } else { 1 };
1036 avg(&mut frm.data[frm.offset[comp] + (xpos >> shift) + (ypos >> shift) * frm.stride[comp]..], frm.stride[comp], &afrm.data[afrm.offset[comp]..], afrm.stride[comp], w >> shift, h >> shift);
1037 }
1038}
1039
495b7ec0
KS
1040pub fn put_block_weighted(dst: &mut [u8], stride: usize, src: &[u8], w: usize, h: usize, wparams: [i8; 3]) {
1041 let weight = i16::from(wparams[0]);
1042 let offset = i16::from(wparams[1]);
1043 let wshift = wparams[2] as u8;
1044 let bias = (1 << wshift) >> 1;
1045
1046 for (drow, srow) in dst.chunks_mut(stride).zip(src.chunks(16)).take(h) {
1047 for (dst, &src) in drow[..w].iter_mut().zip(srow.iter()) {
1048 *dst = clip8(((i16::from(src) * weight + bias) >> wshift) + offset);
1049 }
1050 }
1051}
1052
1053pub fn put_block_weighted2(dst: &mut [u8], stride: usize, src0: &[u8], src1: &[u8], w: usize, h: usize, wparams: [i8; 5]) {
1054 let weight0 = i16::from(wparams[0]);
1055 let offset0 = i16::from(wparams[1]);
1056 let weight1 = i16::from(wparams[2]);
1057 let offset1 = i16::from(wparams[3]);
1058 let wshift = (wparams[4] as u8) + 1;
1059 let offset = (offset0 + offset1 + 1) >> 1;
1060 let bias = (1 << wshift) >> 1;
1061
1062 for (drow, (srow0, srow1)) in dst.chunks_mut(stride).zip(src0.chunks(16).zip(src1.chunks(16))).take(h) {
1063 for (dst, (&src0, &src1)) in drow[..w].iter_mut().zip(srow0.iter().zip(srow1.iter())) {
1064 *dst = clip8(((i16::from(src0) * weight0 + i16::from(src1) * weight1 + bias) >> wshift) + offset);
1065 }
1066 }
1067}
1068
696e4e20
KS
1069macro_rules! loop_filter {
1070 (lumaedge; $buf: expr, $off: expr, $step: expr, $alpha: expr, $beta: expr) => {
1071 let p2 = i16::from($buf[$off - $step * 3]);
1072 let p1 = i16::from($buf[$off - $step * 2]);
1073 let p0 = i16::from($buf[$off - $step]);
1074 let q0 = i16::from($buf[$off]);
1075 let q1 = i16::from($buf[$off + $step]);
1076 let q2 = i16::from($buf[$off + $step * 2]);
1077 let a_p = (p2 - p0).abs() < $beta;
1078 let a_q = (q2 - q0).abs() < $beta;
1079 if a_p && (p0 - q0).abs() < (($alpha >> 2) + 2) {
1080 let p3 = i16::from($buf[$off - $step * 4]);
1081 $buf[$off - $step * 3] = ((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3) as u8;
1082 $buf[$off - $step * 2] = ((p2 + p1 + p0 + q0 + 2) >> 2) as u8;
1083 $buf[$off - $step] = ((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3) as u8;
1084 } else {
1085 $buf[$off - $step] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
1086 }
1087 if a_q && (p0 - q0).abs() < (($alpha >> 2) + 2) {
1088 let q3 = i16::from($buf[$off + $step * 3]);
1089 $buf[$off] = ((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3) as u8;
1090 $buf[$off + $step] = ((p0 + q0 + q1 + q2 + 2) >> 2) as u8;
1091 $buf[$off + $step * 2] = ((2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) >> 3) as u8;
1092 } else {
1093 $buf[$off] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
1094 }
1095 };
1096 (chromaedge; $buf: expr, $off: expr, $step: expr) => {
1097 let p1 = i16::from($buf[$off - $step * 2]);
1098 let p0 = i16::from($buf[$off - $step]);
1099 let q0 = i16::from($buf[$off]);
1100 let q1 = i16::from($buf[$off + $step]);
1101 $buf[$off - $step] = ((2 * p1 + p0 + q1 + 2) >> 2) as u8;
1102 $buf[$off] = ((2 * q1 + q0 + p1 + 2) >> 2) as u8;
1103 };
1104 (lumanormal; $buf: expr, $off: expr, $step: expr, $tc0: expr, $beta: expr) => {
1105 let p2 = i16::from($buf[$off - $step * 3]);
1106 let p1 = i16::from($buf[$off - $step * 2]);
1107 let p0 = i16::from($buf[$off - $step]);
1108 let q0 = i16::from($buf[$off]);
1109 let q1 = i16::from($buf[$off + $step]);
1110 let q2 = i16::from($buf[$off + $step * 2]);
1111 let a_p = (p2 - p0).abs() < $beta;
1112 let a_q = (q2 - q0).abs() < $beta;
1113 let tc = $tc0 + (a_p as i16) + (a_q as i16);
1114 let delta = (((q0 - p0) * 4 + (p1 - q1) + 4) >> 3).max(-tc).min(tc);
1115 if a_p && ($tc0 > 0) {
1116 $buf[$off - $step * 2] = clip8(p1 + ((p2 + ((p0 + q0 + 1) >> 1) - p1 * 2) >> 1).max(-$tc0).min($tc0));
1117 }
1118 $buf[$off - $step] = clip8(p0 + delta);
1119 $buf[$off] = clip8(q0 - delta);
1120 if a_q && ($tc0 > 0) {
1121 $buf[$off + $step] = clip8(q1 + ((q2 + ((p0 + q0 + 1) >> 1) - q1 * 2) >> 1).max(-$tc0).min($tc0));
1122 }
1123 };
1124 (chromanormal; $buf: expr, $off: expr, $step: expr, $tc0: expr) => {
1125 let p1 = i16::from($buf[$off - $step * 2]);
1126 let p0 = i16::from($buf[$off - $step]);
1127 let q0 = i16::from($buf[$off]);
1128 let q1 = i16::from($buf[$off + $step]);
1129 let tc = $tc0 + 1;
1130 let delta = (((q0 - p0) * 4 + (p1 - q1) + 4) >> 3).max(-tc).min(tc);
1131 $buf[$off - $step] = clip8(p0 + delta);
1132 $buf[$off] = clip8(q0 - delta);
1133 }
1134}
1135
1136fn check_filter(buf: &[u8], off: usize, step: usize, alpha: i16, beta: i16) -> bool {
1137 let p1 = i16::from(buf[off - step * 2]);
1138 let p0 = i16::from(buf[off - step]);
1139 let q0 = i16::from(buf[off]);
1140 let q1 = i16::from(buf[off + step]);
1141 (p0 - q0).abs() < alpha && (p1 - p0).abs() < beta && (q1 - q0).abs() < beta
1142}
1143
1144pub fn loop_filter_lumaedge_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16) {
1145 for _ in 0..4 {
1146 if check_filter(dst, off, 1, alpha, beta) {
1147 loop_filter!(lumaedge; dst, off, 1, alpha, beta);
1148 }
1149 off += stride;
1150 }
1151}
1152pub fn loop_filter_lumaedge_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16) {
1153 for x in 0..4 {
1154 if check_filter(dst, off + x, stride, alpha, beta) {
1155 loop_filter!(lumaedge; dst, off + x, stride, alpha, beta);
1156 }
1157 }
1158}
1159pub fn loop_filter_lumanormal_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1160 for _ in 0..4 {
1161 if check_filter(dst, off, 1, alpha, beta) {
1162 loop_filter!(lumanormal; dst, off, 1, tc0, beta);
1163 }
1164 off += stride;
1165 }
1166}
1167pub fn loop_filter_lumanormal_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
1168 for x in 0..4 {
1169 if check_filter(dst, off + x, stride, alpha, beta) {
1170 loop_filter!(lumanormal; dst, off + x, stride, tc0, beta);
1171 }
1172 }
1173}
1174pub fn loop_filter_chromaedge_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16) {
22de733b 1175 for _ in 0..2 {
696e4e20
KS
1176 if check_filter(dst, off, 1, alpha, beta) {
1177 loop_filter!(chromaedge; dst, off, 1);
1178 }
1179 off += stride;
1180 }
1181}
1182pub fn loop_filter_chromaedge_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16) {
22de733b 1183 for x in 0..2 {
696e4e20
KS
1184 if check_filter(dst, off + x, stride, alpha, beta) {
1185 loop_filter!(chromaedge; dst, off + x, stride);
1186 }
1187 }
1188}
1189pub fn loop_filter_chromanormal_v(dst: &mut [u8], mut off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
22de733b 1190 for _ in 0..2 {
696e4e20
KS
1191 if check_filter(dst, off, 1, alpha, beta) {
1192 loop_filter!(chromanormal; dst, off, 1, tc0);
1193 }
1194 off += stride;
1195 }
1196}
1197pub fn loop_filter_chromanormal_h(dst: &mut [u8], off: usize, stride: usize, alpha: i16, beta: i16, tc0: i16) {
22de733b 1198 for x in 0..2 {
696e4e20
KS
1199 if check_filter(dst, off + x, stride, alpha, beta) {
1200 loop_filter!(chromanormal; dst, off + x, stride, tc0);
1201 }
1202 }
1203}