1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
use nihav_core::frame::{NAVideoBuffer, NAVideoBufferRef, NASimpleVideoFrame};
use super::{BlockDSP, CBPInfo, MV};
use super::super::blockdsp;
use super::data::H263_CHROMA_ROUND;
/*const W1: i32 = 22725;
const W2: i32 = 21407;
const W3: i32 = 19266;
const W4: i32 = 16383;
const W5: i32 = 12873;
const W6: i32 = 8867;
const W7: i32 = 4520;
const ROW_SHIFT: u8 = 11;
const COL_SHIFT: u8 = 20;
fn idct_row(row: &mut [i16]) {
let in0 = row[0] as i32;
let in1 = row[1] as i32;
let in2 = row[2] as i32;
let in3 = row[3] as i32;
let in4 = row[4] as i32;
let in5 = row[5] as i32;
let in6 = row[6] as i32;
let in7 = row[7] as i32;
let mut a0 = in0 * W1 + (1 << (ROW_SHIFT - 1));
let mut a1 = a0;
let mut a2 = a0;
let mut a3 = a0;
a0 += W2 * in2;
a1 += W6 * in2;
a2 -= W6 * in2;
a3 -= W2 * in2;
let mut b0 = W1 * in1 + W3 * in3;
let mut b1 = W3 * in1 - W7 * in3;
let mut b2 = W5 * in1 - W1 * in3;
let mut b3 = W7 * in1 - W5 * in3;
a0 += W4 * in4 + W6 * in6;
a1 -= W4 * in4 + W2 * in6;
a2 -= W4 * in4 - W2 * in6;
a3 += W4 * in4 - W6 * in6;
b0 += W5 * in5 + W7 * in7;
b1 -= W1 * in5 + W5 * in7;
b2 += W7 * in5 + W3 * in7;
b3 += W3 * in5 - W1 * in7;
row[0] = ((a0 + b0) >> ROW_SHIFT) as i16;
row[7] = ((a0 - b0) >> ROW_SHIFT) as i16;
row[1] = ((a1 + b1) >> ROW_SHIFT) as i16;
row[6] = ((a1 - b1) >> ROW_SHIFT) as i16;
row[2] = ((a2 + b2) >> ROW_SHIFT) as i16;
row[5] = ((a2 - b2) >> ROW_SHIFT) as i16;
row[3] = ((a3 + b3) >> ROW_SHIFT) as i16;
row[4] = ((a3 - b3) >> ROW_SHIFT) as i16;
}
fn idct_col(blk: &mut [i16; 64], off: usize) {
let in0 = blk[off + 0*8] as i32;
let in1 = blk[off + 1*8] as i32;
let in2 = blk[off + 2*8] as i32;
let in3 = blk[off + 3*8] as i32;
let in4 = blk[off + 4*8] as i32;
let in5 = blk[off + 5*8] as i32;
let in6 = blk[off + 6*8] as i32;
let in7 = blk[off + 7*8] as i32;
let mut a0 = in0 * W1 + (1 << (COL_SHIFT - 1));
let mut a1 = a0;
let mut a2 = a0;
let mut a3 = a0;
a0 += W2 * in2;
a1 += W6 * in2;
a2 -= W6 * in2;
a3 -= W2 * in2;
let mut b0 = W1 * in1 + W3 * in3;
let mut b1 = W3 * in1 - W7 * in3;
let mut b2 = W5 * in1 - W1 * in3;
let mut b3 = W7 * in1 - W5 * in3;
a0 += W4 * in4 + W6 * in6;
a1 -= W4 * in4 + W2 * in6;
a2 -= W4 * in4 - W2 * in6;
a3 += W4 * in4 - W6 * in6;
b0 += W5 * in5 + W7 * in7;
b1 -= W1 * in5 + W5 * in7;
b2 += W7 * in5 + W3 * in7;
b3 += W3 * in5 - W1 * in7;
blk[off + 0*8] = ((a0 + b0) >> COL_SHIFT) as i16;
blk[off + 7*8] = ((a0 - b0) >> COL_SHIFT) as i16;
blk[off + 1*8] = ((a1 + b1) >> COL_SHIFT) as i16;
blk[off + 6*8] = ((a1 - b1) >> COL_SHIFT) as i16;
blk[off + 2*8] = ((a2 + b2) >> COL_SHIFT) as i16;
blk[off + 5*8] = ((a2 - b2) >> COL_SHIFT) as i16;
blk[off + 3*8] = ((a3 + b3) >> COL_SHIFT) as i16;
blk[off + 4*8] = ((a3 - b3) >> COL_SHIFT) as i16;
}
#[allow(dead_code)]
pub fn h263_idct(blk: &mut [i16; 64]) {
for i in 0..8 { idct_row(&mut blk[i*8..(i+1)*8]); }
for i in 0..8 { idct_col(blk, i); }
}*/
const W1: i32 = 2841;
const W2: i32 = 2676;
const W3: i32 = 2408;
const W5: i32 = 1609;
const W6: i32 = 1108;
const W7: i32 = 565;
const W8: i32 = 181;
const ROW_SHIFT: u8 = 8;
const COL_SHIFT: u8 = 14;
#[allow(clippy::erasing_op)]
fn idct_row(row: &mut [i16]) {
let in0 = ((i32::from(row[0])) << 11) + (1 << (ROW_SHIFT - 1));
let in1 = (i32::from(row[4])) << 11;
let in2 = i32::from(row[6]);
let in3 = i32::from(row[2]);
let in4 = i32::from(row[1]);
let in5 = i32::from(row[7]);
let in6 = i32::from(row[5]);
let in7 = i32::from(row[3]);
let tmp = W7 * (in4 + in5);
let a4 = tmp + (W1 - W7) * in4;
let a5 = tmp - (W1 + W7) * in5;
let tmp = W3 * (in6 + in7);
let a6 = tmp - (W3 - W5) * in6;
let a7 = tmp - (W3 + W5) * in7;
let tmp = in0 + in1;
let a0 = in0 - in1;
let t1 = W6 * (in2 + in3);
let a2 = t1 - (W2 + W6) * in2;
let a3 = t1 + (W2 - W6) * in3;
let b1 = a4 + a6;
let b4 = a4 - a6;
let t2 = a5 - a7;
let b6 = a5 + a7;
let b7 = tmp + a3;
let b5 = tmp - a3;
let b3 = a0 + a2;
let b0 = a0 - a2;
let b2 = (W8 * (b4 + t2) + 128) >> 8;
let b4 = (W8 * (b4 - t2) + 128) >> 8;
row[0] = ((b7 + b1) >> ROW_SHIFT) as i16;
row[7] = ((b7 - b1) >> ROW_SHIFT) as i16;
row[1] = ((b3 + b2) >> ROW_SHIFT) as i16;
row[6] = ((b3 - b2) >> ROW_SHIFT) as i16;
row[2] = ((b0 + b4) >> ROW_SHIFT) as i16;
row[5] = ((b0 - b4) >> ROW_SHIFT) as i16;
row[3] = ((b5 + b6) >> ROW_SHIFT) as i16;
row[4] = ((b5 - b6) >> ROW_SHIFT) as i16;
}
#[allow(clippy::erasing_op)]
fn idct_col(blk: &mut [i16; 64], off: usize) {
let in0 = ((i32::from(blk[off + 0*8])) << 8) + (1 << (COL_SHIFT - 1));
let in1 = (i32::from(blk[off + 4*8])) << 8;
let in2 = i32::from(blk[off + 6*8]);
let in3 = i32::from(blk[off + 2*8]);
let in4 = i32::from(blk[off + 1*8]);
let in5 = i32::from(blk[off + 7*8]);
let in6 = i32::from(blk[off + 5*8]);
let in7 = i32::from(blk[off + 3*8]);
let tmp = W7 * (in4 + in5);
let a4 = (tmp + (W1 - W7) * in4) >> 3;
let a5 = (tmp - (W1 + W7) * in5) >> 3;
let tmp = W3 * (in6 + in7);
let a6 = (tmp - (W3 - W5) * in6) >> 3;
let a7 = (tmp - (W3 + W5) * in7) >> 3;
let tmp = in0 + in1;
let a0 = in0 - in1;
let t1 = W6 * (in2 + in3);
let a2 = (t1 - (W2 + W6) * in2) >> 3;
let a3 = (t1 + (W2 - W6) * in3) >> 3;
let b1 = a4 + a6;
let b4 = a4 - a6;
let t2 = a5 - a7;
let b6 = a5 + a7;
let b7 = tmp + a3;
let b5 = tmp - a3;
let b3 = a0 + a2;
let b0 = a0 - a2;
let b2 = (W8 * (b4 + t2) + 128) >> 8;
let b4 = (W8 * (b4 - t2) + 128) >> 8;
blk[off + 0*8] = ((b7 + b1) >> COL_SHIFT) as i16;
blk[off + 7*8] = ((b7 - b1) >> COL_SHIFT) as i16;
blk[off + 1*8] = ((b3 + b2) >> COL_SHIFT) as i16;
blk[off + 6*8] = ((b3 - b2) >> COL_SHIFT) as i16;
blk[off + 2*8] = ((b0 + b4) >> COL_SHIFT) as i16;
blk[off + 5*8] = ((b0 - b4) >> COL_SHIFT) as i16;
blk[off + 3*8] = ((b5 + b6) >> COL_SHIFT) as i16;
blk[off + 4*8] = ((b5 - b6) >> COL_SHIFT) as i16;
}
#[allow(dead_code)]
pub fn h263_idct(blk: &mut [i16; 64]) {
for i in 0..8 { idct_row(&mut blk[i*8..(i+1)*8]); }
for i in 0..8 { idct_col(blk, i); }
}
fn h263_interp00(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
{
let mut didx = 0;
let mut sidx = 0;
for _ in 0..bh {
for x in 0..bw { dst[didx + x] = src[sidx + x]; }
didx += dstride;
sidx += sstride;
}
}
fn h263_interp01(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
{
let mut didx = 0;
let mut sidx = 0;
for _ in 0..bh {
for x in 0..bw { dst[didx + x] = (((src[sidx + x] as u16) + (src[sidx + x + 1] as u16) + 1) >> 1) as u8; }
didx += dstride;
sidx += sstride;
}
}
fn h263_interp10(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
{
let mut didx = 0;
let mut sidx = 0;
for _ in 0..bh {
for x in 0..bw { dst[didx + x] = (((src[sidx + x] as u16) + (src[sidx + x + sstride] as u16) + 1) >> 1) as u8; }
didx += dstride;
sidx += sstride;
}
}
fn h263_interp11(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
{
let mut didx = 0;
let mut sidx = 0;
for _ in 0..bh {
for x in 0..bw {
dst[didx + x] = (((src[sidx + x] as u16) +
(src[sidx + x + 1] as u16) +
(src[sidx + x + sstride] as u16) +
(src[sidx + x + sstride + 1] as u16) + 2) >> 2) as u8;
}
didx += dstride;
sidx += sstride;
}
}
pub const H263_INTERP_FUNCS: &[blockdsp::BlkInterpFunc] = &[
h263_interp00, h263_interp01, h263_interp10, h263_interp11 ];
fn h263_interp00_avg(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
{
let mut didx = 0;
let mut sidx = 0;
for _ in 0..bh {
for x in 0..bw {
let a = dst[didx + x] as u16;
let b = src[sidx + x] as u16;
dst[didx + x] = ((a + b + 1) >> 1) as u8;
}
didx += dstride;
sidx += sstride;
}
}
fn h263_interp01_avg(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
{
let mut didx = 0;
let mut sidx = 0;
for _ in 0..bh {
for x in 0..bw {
let a = dst[didx + x] as u16;
let b = ((src[sidx + x] as u16) + (src[sidx + x + 1] as u16) + 1) >> 1;
dst[didx + x] = ((a + b + 1) >> 1) as u8;
}
didx += dstride;
sidx += sstride;
}
}
fn h263_interp10_avg(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
{
let mut didx = 0;
let mut sidx = 0;
for _ in 0..bh {
for x in 0..bw {
let a = dst[didx + x] as u16;
let b = ((src[sidx + x] as u16) + (src[sidx + x + sstride] as u16) + 1) >> 1;
dst[didx + x] = ((a + b + 1) >> 1) as u8;
}
didx += dstride;
sidx += sstride;
}
}
fn h263_interp11_avg(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize, bw: usize, bh: usize)
{
let mut didx = 0;
let mut sidx = 0;
for _ in 0..bh {
for x in 0..bw {
let a = dst[didx + x] as u16;
let b = ((src[sidx + x] as u16) +
(src[sidx + x + 1] as u16) +
(src[sidx + x + sstride] as u16) +
(src[sidx + x + sstride + 1] as u16) + 2) >> 2;
dst[didx + x] = ((a + b + 1) >> 1) as u8;
}
didx += dstride;
sidx += sstride;
}
}
pub const H263_INTERP_AVG_FUNCS: &[blockdsp::BlkInterpFunc] = &[
h263_interp00_avg, h263_interp01_avg, h263_interp10_avg, h263_interp11_avg ];
#[derive(Default)]
pub struct H263BlockDSP { }
impl H263BlockDSP {
pub fn new() -> Self {
H263BlockDSP { }
}
}
#[allow(clippy::erasing_op)]
fn deblock_hor(buf: &mut NAVideoBuffer<u8>, comp: usize, strength: u8, off: usize) {
let stride = buf.get_stride(comp);
let dptr = buf.get_data_mut().unwrap();
let buf = dptr.as_mut_slice();
for x in 0..8 {
let a = buf[off - 2 * stride + x] as i16;
let b = buf[off - 1 * stride + x] as i16;
let c = buf[off + 0 * stride + x] as i16;
let d = buf[off + 1 * stride + x] as i16;
let diff = ((a - d) + (c - b) * 4) / 8;
if (diff != 0) && (diff > -24) && (diff < 24) {
let d1a = (diff.abs() - 2 * (diff.abs() - (strength as i16)).max(0)).max(0);
let d1 = if diff < 0 { -d1a } else { d1a };
let hd1 = d1a / 2;
let d2 = ((a - d) / 4).max(-hd1).min(hd1);
buf[off - 2 * stride + x] = (a - d2) as u8;
buf[off - 1 * stride + x] = (b + d1).max(0).min(255) as u8;
buf[off + 0 * stride + x] = (c - d1).max(0).min(255) as u8;
buf[off + 1 * stride + x] = (d + d2) as u8;
}
}
}
fn deblock_ver(buf: &mut NAVideoBuffer<u8>, comp: usize, strength: u8, off: usize) {
let stride = buf.get_stride(comp);
let dptr = buf.get_data_mut().unwrap();
let buf = dptr.as_mut_slice();
for y in 0..8 {
let a = buf[off - 2 + y * stride] as i16;
let b = buf[off - 1 + y * stride] as i16;
let c = buf[off + 0 + y * stride] as i16;
let d = buf[off + 1 + y * stride] as i16;
let diff = (a - d + (c - b) * 4) / 8;
if (diff != 0) && (diff > -24) && (diff < 24) {
let d1a = (diff.abs() - 2 * (diff.abs() - (strength as i16)).max(0)).max(0);
let d1 = if diff < 0 { -d1a } else { d1a };
let hd1 = d1a / 2;
let d2 = ((a - d) / 4).max(-hd1).min(hd1);
buf[off - 2 + y * stride] = (a - d2) as u8;
buf[off - 1 + y * stride] = (b + d1).max(0).min(255) as u8;
buf[off + y * stride] = (c - d1).max(0).min(255) as u8;
buf[off + 1 + y * stride] = (d + d2) as u8;
}
}
}
const FILTER_STRENGTH: [u8; 32] = [
1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7,
7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12
];
pub fn h263_filter_row(buf: &mut NAVideoBuffer<u8>, mb_y: usize, mb_w: usize, cbpi: &CBPInfo) {
let stride = buf.get_stride(0);
let mut off = buf.get_offset(0) + mb_y * 16 * stride;
for mb_x in 0..mb_w {
let coff = off;
let coded0 = cbpi.is_coded(mb_x, 0);
let coded1 = cbpi.is_coded(mb_x, 1);
let q = cbpi.get_q(mb_w + mb_x);
let str = if q < 32 { FILTER_STRENGTH[q as usize] } else { 0 };
if mb_y != 0 {
if coded0 && cbpi.is_coded_top(mb_x, 0) { deblock_hor(buf, 0, str, coff); }
if coded1 && cbpi.is_coded_top(mb_x, 1) { deblock_hor(buf, 0, str, coff + 8); }
}
let coff = off + 8 * stride;
if cbpi.is_coded(mb_x, 2) && coded0 { deblock_hor(buf, 0, q, coff); }
if cbpi.is_coded(mb_x, 3) && coded1 { deblock_hor(buf, 0, q, coff + 8); }
off += 16;
}
let mut leftt = false;
let mut leftc = false;
let mut off = buf.get_offset(0) + mb_y * 16 * stride;
for mb_x in 0..mb_w {
let ctop0 = cbpi.is_coded_top(mb_x, 0);
let ctop1 = cbpi.is_coded_top(mb_x, 0);
let ccur0 = cbpi.is_coded(mb_x, 0);
let ccur1 = cbpi.is_coded(mb_x, 1);
let q = cbpi.get_q(mb_w + mb_x);
let str = if q < 32 { FILTER_STRENGTH[q as usize] } else { 0 };
if mb_y != 0 {
let coff = off - 8 * stride;
let qtop = cbpi.get_q(mb_x);
let strtop = if qtop < 32 { FILTER_STRENGTH[qtop as usize] } else { 0 };
if leftt && ctop0 { deblock_ver(buf, 0, strtop, coff); }
if ctop0 && ctop1 { deblock_ver(buf, 0, strtop, coff + 8); }
}
if leftc && ccur0 { deblock_ver(buf, 0, str, off); }
if ccur0 && ccur1 { deblock_ver(buf, 0, str, off + 8); }
leftt = ctop1;
leftc = ccur1;
off += 16;
}
let strideu = buf.get_stride(1);
let stridev = buf.get_stride(2);
let offu = buf.get_offset(1) + mb_y * 8 * strideu;
let offv = buf.get_offset(2) + mb_y * 8 * stridev;
if mb_y != 0 {
for mb_x in 0..mb_w {
let ctu = cbpi.is_coded_top(mb_x, 4);
let ccu = cbpi.is_coded(mb_x, 4);
let ctv = cbpi.is_coded_top(mb_x, 5);
let ccv = cbpi.is_coded(mb_x, 5);
let q = cbpi.get_q(mb_w + mb_x);
let str = if q < 32 { FILTER_STRENGTH[q as usize] } else { 0 };
if ctu && ccu { deblock_hor(buf, 1, str, offu + mb_x * 8); }
if ctv && ccv { deblock_hor(buf, 2, str, offv + mb_x * 8); }
}
let mut leftu = false;
let mut leftv = false;
let offu = buf.get_offset(1) + (mb_y - 1) * 8 * strideu;
let offv = buf.get_offset(2) + (mb_y - 1) * 8 * stridev;
for mb_x in 0..mb_w {
let ctu = cbpi.is_coded_top(mb_x, 4);
let ctv = cbpi.is_coded_top(mb_x, 5);
let qt = cbpi.get_q(mb_x);
let strt = if qt < 32 { FILTER_STRENGTH[qt as usize] } else { 0 };
if leftu && ctu { deblock_ver(buf, 1, strt, offu + mb_x * 8); }
if leftv && ctv { deblock_ver(buf, 2, strt, offv + mb_x * 8); }
leftu = ctu;
leftv = ctv;
}
}
}
impl BlockDSP for H263BlockDSP {
fn idct(&self, blk: &mut [i16; 64]) {
h263_idct(blk)
}
fn copy_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mv: MV) {
let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize;
let cmode = (if (mv.x & 3) != 0 { 1 } else { 0 }) + (if (mv.y & 3) != 0 { 2 } else { 0 });
let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap();
blockdsp::copy_block(&mut dst, src.clone(), 0, xpos, ypos, mv.x >> 1, mv.y >> 1, 16, 16, 0, 1, mode, H263_INTERP_FUNCS);
blockdsp::copy_block(&mut dst, src.clone(), 1, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_FUNCS);
blockdsp::copy_block(&mut dst, src.clone(), 2, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_FUNCS);
}
fn copy_blocks8x8(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mvs: &[MV; 4]) {
let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap();
for i in 0..4 {
let xadd = (i & 1) * 8;
let yadd = (i & 2) * 4;
let mode = ((mvs[i].x & 1) + (mvs[i].y & 1) * 2) as usize;
blockdsp::copy_block(&mut dst, src.clone(), 0, xpos + xadd, ypos + yadd, mvs[i].x >> 1, mvs[i].y >> 1, 8, 8, 0, 1, mode, H263_INTERP_FUNCS);
}
let sum_mv = mvs[0] + mvs[1] + mvs[2] + mvs[3];
let cmx = (sum_mv.x >> 3) + H263_CHROMA_ROUND[(sum_mv.x & 0xF) as usize];
let cmy = (sum_mv.y >> 3) + H263_CHROMA_ROUND[(sum_mv.y & 0xF) as usize];
let mode = ((cmx & 1) + (cmy & 1) * 2) as usize;
for plane in 1..3 {
blockdsp::copy_block(&mut dst, src.clone(), plane, xpos >> 1, ypos >> 1, cmx >> 1, cmy >> 1, 8, 8, 0, 1, mode, H263_INTERP_FUNCS);
}
}
fn avg_blocks(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mv: MV) {
let mode = ((mv.x & 1) + (mv.y & 1) * 2) as usize;
let cmode = (if (mv.x & 3) != 0 { 1 } else { 0 }) + (if (mv.y & 3) != 0 { 2 } else { 0 });
let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap();
blockdsp::copy_block(&mut dst, src.clone(), 0, xpos, ypos, mv.x >> 1, mv.y >> 1, 16, 16, 0, 1, mode, H263_INTERP_AVG_FUNCS);
blockdsp::copy_block(&mut dst, src.clone(), 1, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_AVG_FUNCS);
blockdsp::copy_block(&mut dst, src.clone(), 2, xpos >> 1, ypos >> 1, mv.x >> 2, mv.y >> 2, 8, 8, 0, 1, cmode, H263_INTERP_AVG_FUNCS);
}
fn avg_blocks8x8(&self, dst: &mut NAVideoBuffer<u8>, src: NAVideoBufferRef<u8>, xpos: usize, ypos: usize, mvs: &[MV; 4]) {
let mut dst = NASimpleVideoFrame::from_video_buf(dst).unwrap();
for i in 0..4 {
let xadd = (i & 1) * 8;
let yadd = (i & 2) * 4;
let mode = ((mvs[i].x & 1) + (mvs[i].y & 1) * 2) as usize;
blockdsp::copy_block(&mut dst, src.clone(), 0, xpos + xadd, ypos + yadd, mvs[i].x >> 1, mvs[i].y >> 1, 8, 8, 0, 1, mode, H263_INTERP_AVG_FUNCS);
}
let sum_mv = mvs[0] + mvs[1] + mvs[2] + mvs[3];
let cmx = (sum_mv.x >> 3) + H263_CHROMA_ROUND[(sum_mv.x & 0xF) as usize];
let cmy = (sum_mv.y >> 3) + H263_CHROMA_ROUND[(sum_mv.y & 0xF) as usize];
let mode = ((cmx & 1) + (cmy & 1) * 2) as usize;
for plane in 1..3 {
blockdsp::copy_block(&mut dst, src.clone(), plane, xpos >> 1, ypos >> 1, cmx >> 1, cmy >> 1, 8, 8, 0, 1, mode, H263_INTERP_AVG_FUNCS);
}
}
fn filter_row(&self, buf: &mut NAVideoBuffer<u8>, mb_y: usize, mb_w: usize, cbpi: &CBPInfo) {
h263_filter_row(buf, mb_y, mb_w, cbpi)
}
}
macro_rules! obmc_filter {
($src: expr, $base_off: expr, $off0: expr, $w0: expr, $off1: expr, $w1: expr, $off2: expr, $w2: expr) => ({
let a = $src[$base_off + $off0] as u16;
let b = $src[$base_off + $off1] as u16;
let c = $src[$base_off + $off2] as u16;
((a * $w0 + b * $w1 + c * $w2 + 4) >> 3) as u8
})
}
pub fn obmc_filter(dst: &mut [u8], dstride: usize, src: &[u8], sstride: usize) {
let top_off = 8 + sstride * 0;
let left_off = 0 + sstride * 8;
let right_off = 16 + sstride * 8;
let bottom_off = 8 + sstride * 16;
let cur_off = 8 + sstride * 8;
let mut doff = 0;
let mut soff = 0;
dst[doff + 0] = obmc_filter!(src, soff + 0, left_off, 2, top_off, 2, cur_off, 4);
dst[doff + 1] = obmc_filter!(src, soff + 1, left_off, 1, top_off, 2, cur_off, 5);
dst[doff + 2] = obmc_filter!(src, soff + 2, left_off, 1, top_off, 2, cur_off, 5);
dst[doff + 3] = obmc_filter!(src, soff + 3, left_off, 1, top_off, 2, cur_off, 5);
dst[doff + 4] = obmc_filter!(src, soff + 4, right_off, 1, top_off, 2, cur_off, 5);
dst[doff + 5] = obmc_filter!(src, soff + 5, right_off, 1, top_off, 2, cur_off, 5);
dst[doff + 6] = obmc_filter!(src, soff + 6, right_off, 1, top_off, 2, cur_off, 5);
dst[doff + 7] = obmc_filter!(src, soff + 7, right_off, 2, top_off, 2, cur_off, 4);
doff += dstride;
soff += sstride;
dst[doff + 0] = obmc_filter!(src, soff + 0, left_off, 2, top_off, 1, cur_off, 5);
dst[doff + 1] = obmc_filter!(src, soff + 1, left_off, 2, top_off, 1, cur_off, 5);
dst[doff + 2] = obmc_filter!(src, soff + 2, left_off, 1, top_off, 2, cur_off, 5);
dst[doff + 3] = obmc_filter!(src, soff + 3, left_off, 1, top_off, 2, cur_off, 5);
dst[doff + 4] = obmc_filter!(src, soff + 4, right_off, 1, top_off, 2, cur_off, 5);
dst[doff + 5] = obmc_filter!(src, soff + 5, right_off, 1, top_off, 2, cur_off, 5);
dst[doff + 6] = obmc_filter!(src, soff + 6, right_off, 2, top_off, 1, cur_off, 5);
dst[doff + 7] = obmc_filter!(src, soff + 7, right_off, 2, top_off, 1, cur_off, 5);
doff += dstride;
soff += sstride;
dst[doff + 0] = obmc_filter!(src, soff + 0, left_off, 2, top_off, 1, cur_off, 5);
dst[doff + 1] = obmc_filter!(src, soff + 1, left_off, 2, top_off, 1, cur_off, 5);
dst[doff + 2] = obmc_filter!(src, soff + 2, left_off, 1, top_off, 1, cur_off, 6);
dst[doff + 3] = obmc_filter!(src, soff + 3, left_off, 1, top_off, 1, cur_off, 6);
dst[doff + 4] = obmc_filter!(src, soff + 4, right_off, 1, top_off, 1, cur_off, 6);
dst[doff + 5] = obmc_filter!(src, soff + 5, right_off, 1, top_off, 1, cur_off, 6);
dst[doff + 6] = obmc_filter!(src, soff + 6, right_off, 2, top_off, 1, cur_off, 5);
dst[doff + 7] = obmc_filter!(src, soff + 7, right_off, 2, top_off, 1, cur_off, 5);
doff += dstride;
soff += sstride;
dst[doff + 0] = obmc_filter!(src, soff + 0, left_off, 2, top_off, 1, cur_off, 5);
dst[doff + 1] = obmc_filter!(src, soff + 1, left_off, 2, top_off, 1, cur_off, 5);
dst[doff + 2] = obmc_filter!(src, soff + 2, left_off, 1, top_off, 1, cur_off, 6);
dst[doff + 3] = obmc_filter!(src, soff + 3, left_off, 1, top_off, 1, cur_off, 6);
dst[doff + 4] = obmc_filter!(src, soff + 4, right_off, 1, top_off, 1, cur_off, 6);
dst[doff + 5] = obmc_filter!(src, soff + 5, right_off, 1, top_off, 1, cur_off, 6);
dst[doff + 6] = obmc_filter!(src, soff + 6, right_off, 2, top_off, 1, cur_off, 5);
dst[doff + 7] = obmc_filter!(src, soff + 7, right_off, 2, top_off, 1, cur_off, 5);
doff += dstride;
soff += sstride;
dst[doff + 0] = obmc_filter!(src, soff + 0, left_off, 2, bottom_off, 1, cur_off, 5);
dst[doff + 1] = obmc_filter!(src, soff + 1, left_off, 2, bottom_off, 1, cur_off, 5);
dst[doff + 2] = obmc_filter!(src, soff + 2, left_off, 1, bottom_off, 1, cur_off, 6);
dst[doff + 3] = obmc_filter!(src, soff + 3, left_off, 1, bottom_off, 1, cur_off, 6);
dst[doff + 4] = obmc_filter!(src, soff + 4, right_off, 1, bottom_off, 1, cur_off, 6);
dst[doff + 5] = obmc_filter!(src, soff + 5, right_off, 1, bottom_off, 1, cur_off, 6);
dst[doff + 6] = obmc_filter!(src, soff + 6, right_off, 2, bottom_off, 1, cur_off, 5);
dst[doff + 7] = obmc_filter!(src, soff + 7, right_off, 2, bottom_off, 1, cur_off, 5);
doff += dstride;
soff += sstride;
dst[doff + 0] = obmc_filter!(src, soff + 0, left_off, 2, bottom_off, 1, cur_off, 5);
dst[doff + 1] = obmc_filter!(src, soff + 1, left_off, 2, bottom_off, 1, cur_off, 5);
dst[doff + 2] = obmc_filter!(src, soff + 2, left_off, 1, bottom_off, 1, cur_off, 6);
dst[doff + 3] = obmc_filter!(src, soff + 3, left_off, 1, bottom_off, 1, cur_off, 6);
dst[doff + 4] = obmc_filter!(src, soff + 4, right_off, 1, bottom_off, 1, cur_off, 6);
dst[doff + 5] = obmc_filter!(src, soff + 5, right_off, 1, bottom_off, 1, cur_off, 6);
dst[doff + 6] = obmc_filter!(src, soff + 6, right_off, 2, bottom_off, 1, cur_off, 5);
dst[doff + 7] = obmc_filter!(src, soff + 7, right_off, 2, bottom_off, 1, cur_off, 5);
doff += dstride;
soff += sstride;
dst[doff + 0] = obmc_filter!(src, soff + 0, left_off, 2, bottom_off, 1, cur_off, 5);
dst[doff + 1] = obmc_filter!(src, soff + 1, left_off, 2, bottom_off, 1, cur_off, 5);
dst[doff + 2] = obmc_filter!(src, soff + 2, left_off, 1, bottom_off, 2, cur_off, 5);
dst[doff + 3] = obmc_filter!(src, soff + 3, left_off, 1, bottom_off, 2, cur_off, 5);
dst[doff + 4] = obmc_filter!(src, soff + 4, right_off, 1, bottom_off, 2, cur_off, 5);
dst[doff + 5] = obmc_filter!(src, soff + 5, right_off, 1, bottom_off, 2, cur_off, 5);
dst[doff + 6] = obmc_filter!(src, soff + 6, right_off, 2, bottom_off, 1, cur_off, 5);
dst[doff + 7] = obmc_filter!(src, soff + 7, right_off, 2, bottom_off, 1, cur_off, 5);
doff += dstride;
soff += sstride;
dst[doff + 0] = obmc_filter!(src, soff + 0, left_off, 2, bottom_off, 2, cur_off, 4);
dst[doff + 1] = obmc_filter!(src, soff + 1, left_off, 1, bottom_off, 2, cur_off, 5);
dst[doff + 2] = obmc_filter!(src, soff + 2, left_off, 1, bottom_off, 2, cur_off, 5);
dst[doff + 3] = obmc_filter!(src, soff + 3, left_off, 1, bottom_off, 2, cur_off, 5);
dst[doff + 4] = obmc_filter!(src, soff + 4, right_off, 1, bottom_off, 2, cur_off, 5);
dst[doff + 5] = obmc_filter!(src, soff + 5, right_off, 1, bottom_off, 2, cur_off, 5);
dst[doff + 6] = obmc_filter!(src, soff + 6, right_off, 1, bottom_off, 2, cur_off, 5);
dst[doff + 7] = obmc_filter!(src, soff + 7, right_off, 2, bottom_off, 2, cur_off, 4);
}
|