-
Notifications
You must be signed in to change notification settings - Fork 54
/
decodeframe.c
5567 lines (4917 loc) · 211 KB
/
decodeframe.c
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 2 Clause License and
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
* was not distributed with this source code in the LICENSE file, you can
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
* Media Patent License 1.0 was not distributed with this source code in the
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
#include <assert.h>
#include <stddef.h>
#include "config/aom_config.h"
#include "config/aom_dsp_rtcd.h"
#include "config/aom_scale_rtcd.h"
#include "config/av1_rtcd.h"
#include "aom/aom_codec.h"
#include "aom_dsp/aom_dsp_common.h"
#include "aom_dsp/binary_codes_reader.h"
#include "aom_dsp/bitreader.h"
#include "aom_dsp/bitreader_buffer.h"
#include "aom_mem/aom_mem.h"
#include "aom_ports/aom_timer.h"
#include "aom_ports/mem.h"
#include "aom_ports/mem_ops.h"
#include "aom_scale/aom_scale.h"
#include "aom_util/aom_thread.h"
#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
#include "aom_util/debug_util.h"
#endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
#include "av1/common/alloccommon.h"
#include "av1/common/cdef.h"
#include "av1/common/cfl.h"
#if CONFIG_INSPECTION
#include "av1/decoder/inspection.h"
#endif
#include "av1/common/common.h"
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/entropymv.h"
#include "av1/common/frame_buffers.h"
#include "av1/common/idct.h"
#include "av1/common/mvref_common.h"
#include "av1/common/pred_common.h"
#include "av1/common/quant_common.h"
#include "av1/common/reconinter.h"
#include "av1/common/reconintra.h"
#include "av1/common/resize.h"
#include "av1/common/seg_common.h"
#include "av1/common/thread_common.h"
#include "av1/common/tile_common.h"
#include "av1/common/warped_motion.h"
#include "av1/common/obmc.h"
#include "av1/decoder/decodeframe.h"
#include "av1/decoder/decodemv.h"
#include "av1/decoder/decoder.h"
#include "av1/decoder/decodetxb.h"
#include "av1/decoder/detokenize.h"
#define ACCT_STR __func__
// This is needed by ext_tile related unit tests.
#define EXT_TILE_DEBUG 1
#define MC_TEMP_BUF_PELS \
(((MAX_SB_SIZE)*2 + (AOM_INTERP_EXTEND)*2) * \
((MAX_SB_SIZE)*2 + (AOM_INTERP_EXTEND)*2))
// Checks that the remaining bits start with a 1 and ends with 0s.
// It consumes an additional byte, if already byte aligned before the check.
int av1_check_trailing_bits(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
AV1_COMMON *const cm = &pbi->common;
// bit_offset is set to 0 (mod 8) when the reader is already byte aligned
int bits_before_alignment = 8 - rb->bit_offset % 8;
int trailing = aom_rb_read_literal(rb, bits_before_alignment);
if (trailing != (1 << (bits_before_alignment - 1))) {
cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
return -1;
}
return 0;
}
// Use only_chroma = 1 to only set the chroma planes
static void set_planes_to_neutral_grey(const SequenceHeader *const seq_params,
const YV12_BUFFER_CONFIG *const buf,
int only_chroma) {
if (seq_params->use_highbitdepth) {
const int val = 1 << (seq_params->bit_depth - 1);
for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
const int is_uv = plane > 0;
uint16_t *const base = CONVERT_TO_SHORTPTR(buf->buffers[plane]);
// Set the first row to neutral grey. Then copy the first row to all
// subsequent rows.
if (buf->crop_heights[is_uv] > 0) {
aom_memset16(base, val, buf->crop_widths[is_uv]);
for (int row_idx = 1; row_idx < buf->crop_heights[is_uv]; row_idx++) {
memcpy(&base[row_idx * buf->strides[is_uv]], base,
sizeof(*base) * buf->crop_widths[is_uv]);
}
}
}
} else {
for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
const int is_uv = plane > 0;
for (int row_idx = 0; row_idx < buf->crop_heights[is_uv]; row_idx++) {
memset(&buf->buffers[plane][row_idx * buf->uv_stride], 1 << 7,
buf->crop_widths[is_uv]);
}
}
}
}
static void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
MACROBLOCKD *xd,
aom_reader *const r, int plane,
int runit_idx);
static void setup_compound_reference_mode(AV1_COMMON *cm) {
cm->comp_fwd_ref[0] = LAST_FRAME;
cm->comp_fwd_ref[1] = LAST2_FRAME;
cm->comp_fwd_ref[2] = LAST3_FRAME;
cm->comp_fwd_ref[3] = GOLDEN_FRAME;
cm->comp_bwd_ref[0] = BWDREF_FRAME;
cm->comp_bwd_ref[1] = ALTREF2_FRAME;
cm->comp_bwd_ref[2] = ALTREF_FRAME;
}
static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
return len != 0 && len <= (size_t)(end - start);
}
static TX_MODE read_tx_mode(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
if (cm->coded_lossless) return ONLY_4X4;
return aom_rb_read_bit(rb) ? TX_MODE_SELECT : TX_MODE_LARGEST;
}
static REFERENCE_MODE read_frame_reference_mode(
const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
if (frame_is_intra_only(cm)) {
return SINGLE_REFERENCE;
} else {
return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
}
}
static void inverse_transform_block(MACROBLOCKD *xd, int plane,
const TX_TYPE tx_type,
const TX_SIZE tx_size, uint8_t *dst,
int stride, int reduced_tx_set) {
struct macroblockd_plane *const pd = &xd->plane[plane];
tran_low_t *const dqcoeff = pd->dqcoeff;
eob_info *eob_data = pd->eob_data + xd->txb_offset[plane];
uint16_t scan_line = eob_data->max_scan_line;
uint16_t eob = eob_data->eob;
memcpy(dqcoeff, pd->dqcoeff_block + xd->cb_offset[plane],
(scan_line + 1) * sizeof(dqcoeff[0]));
av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst, stride,
eob, reduced_tx_set);
memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
}
static void read_coeffs_tx_intra_block(const AV1_COMMON *const cm,
MACROBLOCKD *const xd,
aom_reader *const r, const int plane,
const int row, const int col,
const TX_SIZE tx_size) {
MB_MODE_INFO *mbmi = xd->mi[0];
if (!mbmi->skip) {
#if TXCOEFF_TIMER
struct aom_usec_timer timer;
aom_usec_timer_start(&timer);
#endif
av1_read_coeffs_txb_facade(cm, xd, r, plane, row, col, tx_size);
#if TXCOEFF_TIMER
aom_usec_timer_mark(&timer);
const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);
cm->txcoeff_timer += elapsed_time;
++cm->txb_count;
#endif
}
}
static void decode_block_void(const AV1_COMMON *const cm, MACROBLOCKD *const xd,
aom_reader *const r, const int plane,
const int row, const int col,
const TX_SIZE tx_size) {
(void)cm;
(void)xd;
(void)r;
(void)plane;
(void)row;
(void)col;
(void)tx_size;
}
static void predict_inter_block_void(AV1_COMMON *const cm,
MACROBLOCKD *const xd, int mi_row,
int mi_col, BLOCK_SIZE bsize) {
(void)cm;
(void)xd;
(void)mi_row;
(void)mi_col;
(void)bsize;
}
static void cfl_store_inter_block_void(AV1_COMMON *const cm,
MACROBLOCKD *const xd) {
(void)cm;
(void)xd;
}
static void predict_and_reconstruct_intra_block(
const AV1_COMMON *const cm, MACROBLOCKD *const xd, aom_reader *const r,
const int plane, const int row, const int col, const TX_SIZE tx_size) {
(void)r;
MB_MODE_INFO *mbmi = xd->mi[0];
PLANE_TYPE plane_type = get_plane_type(plane);
av1_predict_intra_block_facade(cm, xd, plane, col, row, tx_size);
if (!mbmi->skip) {
struct macroblockd_plane *const pd = &xd->plane[plane];
// tx_type will be read out in av1_read_coeffs_txb_facade
const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, row, col, tx_size,
cm->reduced_tx_set_used);
eob_info *eob_data = pd->eob_data + xd->txb_offset[plane];
if (eob_data->eob) {
uint8_t *dst =
&pd->dst.buf[(row * pd->dst.stride + col) << tx_size_wide_log2[0]];
inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
cm->reduced_tx_set_used);
}
}
if (plane == AOM_PLANE_Y && store_cfl_required(cm, xd)) {
cfl_store_tx(xd, row, col, tx_size, mbmi->sb_type);
}
}
static void inverse_transform_inter_block(const AV1_COMMON *const cm,
MACROBLOCKD *const xd,
aom_reader *const r, const int plane,
const int blk_row, const int blk_col,
const TX_SIZE tx_size) {
(void)r;
PLANE_TYPE plane_type = get_plane_type(plane);
const struct macroblockd_plane *const pd = &xd->plane[plane];
// tx_type will be read out in av1_read_coeffs_txb_facade
const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col,
tx_size, cm->reduced_tx_set_used);
uint8_t *dst =
&pd->dst
.buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
inverse_transform_block(xd, plane, tx_type, tx_size, dst, pd->dst.stride,
cm->reduced_tx_set_used);
#if CONFIG_MISMATCH_DEBUG
int pixel_c, pixel_r;
BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
int blk_w = block_size_wide[bsize];
int blk_h = block_size_high[bsize];
mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, blk_col, blk_row,
pd->subsampling_x, pd->subsampling_y);
mismatch_check_block_tx(dst, pd->dst.stride, cm->frame_offset, plane, pixel_c,
pixel_r, blk_w, blk_h,
xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
#endif
}
static void set_cb_buffer_offsets(MACROBLOCKD *const xd, TX_SIZE tx_size,
int plane) {
xd->cb_offset[plane] += tx_size_wide[tx_size] * tx_size_high[tx_size];
xd->txb_offset[plane] =
xd->cb_offset[plane] / (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
}
static void decode_reconstruct_tx(AV1_COMMON *cm, ThreadData *const td,
aom_reader *r, MB_MODE_INFO *const mbmi,
int plane, BLOCK_SIZE plane_bsize,
int blk_row, int blk_col, int block,
TX_SIZE tx_size, int *eob_total) {
MACROBLOCKD *const xd = &td->xd;
const struct macroblockd_plane *const pd = &xd->plane[plane];
const TX_SIZE plane_tx_size =
plane ? av1_get_max_uv_txsize(mbmi->sb_type, pd->subsampling_x,
pd->subsampling_y)
: mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
blk_col)];
// Scale to match transform block unit.
const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
if (tx_size == plane_tx_size || plane) {
td->read_coeffs_tx_inter_block_visit(cm, xd, r, plane, blk_row, blk_col,
tx_size);
td->inverse_tx_inter_block_visit(cm, xd, r, plane, blk_row, blk_col,
tx_size);
eob_info *eob_data = pd->eob_data + xd->txb_offset[plane];
*eob_total += eob_data->eob;
set_cb_buffer_offsets(xd, tx_size, plane);
} else {
const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
const int bsw = tx_size_wide_unit[sub_txs];
const int bsh = tx_size_high_unit[sub_txs];
const int sub_step = bsw * bsh;
assert(bsw > 0 && bsh > 0);
for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
const int offsetr = blk_row + row;
const int offsetc = blk_col + col;
if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
decode_reconstruct_tx(cm, td, r, mbmi, plane, plane_bsize, offsetr,
offsetc, block, sub_txs, eob_total);
block += sub_step;
}
}
}
}
static void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
int bh, int x_mis, int y_mis) {
const int num_planes = av1_num_planes(cm);
const int offset = mi_row * cm->mi_stride + mi_col;
const TileInfo *const tile = &xd->tile;
xd->mi = cm->mi_grid_visible + offset;
xd->mi[0] = &cm->mi[offset];
// TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
// passing bsize from decode_partition().
xd->mi[0]->sb_type = bsize;
#if CONFIG_RD_DEBUG
xd->mi[0]->mi_row = mi_row;
xd->mi[0]->mi_col = mi_col;
#endif
xd->cfl.mi_row = mi_row;
xd->cfl.mi_col = mi_col;
assert(x_mis && y_mis);
for (int x = 1; x < x_mis; ++x) xd->mi[x] = xd->mi[0];
int idx = cm->mi_stride;
for (int y = 1; y < y_mis; ++y) {
memcpy(&xd->mi[idx], &xd->mi[0], x_mis * sizeof(xd->mi[0]));
idx += cm->mi_stride;
}
set_plane_n4(xd, bw, bh, num_planes);
set_skip_context(xd, mi_row, mi_col, num_planes);
// Distance of Mb to the various image edges. These are specified to 8th pel
// as they are always compared to values that are in 1/8th pel units
set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
mi_col, 0, num_planes);
}
static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
int mi_row, int mi_col, aom_reader *r,
PARTITION_TYPE partition, BLOCK_SIZE bsize) {
AV1_COMMON *const cm = &pbi->common;
const SequenceHeader *const seq_params = &cm->seq_params;
const int bw = mi_size_wide[bsize];
const int bh = mi_size_high[bsize];
const int x_mis = AOMMIN(bw, cm->mi_cols - mi_col);
const int y_mis = AOMMIN(bh, cm->mi_rows - mi_row);
#if CONFIG_ACCOUNTING
aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
#endif
set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
xd->mi[0]->partition = partition;
av1_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
if (bsize >= BLOCK_8X8 &&
(seq_params->subsampling_x || seq_params->subsampling_y)) {
const BLOCK_SIZE uv_subsize =
ss_size_lookup[bsize][seq_params->subsampling_x]
[seq_params->subsampling_y];
if (uv_subsize == BLOCK_INVALID)
aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
"Invalid block size.");
}
int reader_corrupted_flag = aom_reader_has_error(r);
aom_merge_corrupted_flag(&xd->corrupted, reader_corrupted_flag);
}
typedef struct PadBlock {
int x0;
int x1;
int y0;
int y1;
} PadBlock;
static void highbd_build_mc_border(const uint8_t *src8, int src_stride,
uint8_t *dst8, int dst_stride, int x, int y,
int b_w, int b_h, int w, int h) {
// Get a pointer to the start of the real data for this row.
const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
const uint16_t *ref_row = src - x - y * src_stride;
if (y >= h)
ref_row += (h - 1) * src_stride;
else if (y > 0)
ref_row += y * src_stride;
do {
int right = 0, copy;
int left = x < 0 ? -x : 0;
if (left > b_w) left = b_w;
if (x + b_w > w) right = x + b_w - w;
if (right > b_w) right = b_w;
copy = b_w - left - right;
if (left) aom_memset16(dst, ref_row[0], left);
if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
if (right) aom_memset16(dst + left + copy, ref_row[w - 1], right);
dst += dst_stride;
++y;
if (y > 0 && y < h) ref_row += src_stride;
} while (--b_h);
}
static void build_mc_border(const uint8_t *src, int src_stride, uint8_t *dst,
int dst_stride, int x, int y, int b_w, int b_h,
int w, int h) {
// Get a pointer to the start of the real data for this row.
const uint8_t *ref_row = src - x - y * src_stride;
if (y >= h)
ref_row += (h - 1) * src_stride;
else if (y > 0)
ref_row += y * src_stride;
do {
int right = 0, copy;
int left = x < 0 ? -x : 0;
if (left > b_w) left = b_w;
if (x + b_w > w) right = x + b_w - w;
if (right > b_w) right = b_w;
copy = b_w - left - right;
if (left) memset(dst, ref_row[0], left);
if (copy) memcpy(dst + left, ref_row + x + left, copy);
if (right) memset(dst + left + copy, ref_row[w - 1], right);
dst += dst_stride;
++y;
if (y > 0 && y < h) ref_row += src_stride;
} while (--b_h);
}
static INLINE int update_extend_mc_border_params(
const struct scale_factors *const sf, struct buf_2d *const pre_buf,
MV32 scaled_mv, PadBlock *block, int subpel_x_mv, int subpel_y_mv,
int do_warp, int is_intrabc, int *x_pad, int *y_pad) {
const int is_scaled = av1_is_scaled(sf);
// Get reference width and height.
int frame_width = pre_buf->width;
int frame_height = pre_buf->height;
// Do border extension if there is motion or
// width/height is not a multiple of 8 pixels.
if ((!is_intrabc) && (!do_warp) &&
(is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) ||
(frame_height & 0x7))) {
if (subpel_x_mv || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
block->x0 -= AOM_INTERP_EXTEND - 1;
block->x1 += AOM_INTERP_EXTEND;
*x_pad = 1;
}
if (subpel_y_mv || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
block->y0 -= AOM_INTERP_EXTEND - 1;
block->y1 += AOM_INTERP_EXTEND;
*y_pad = 1;
}
// Skip border extension if block is inside the frame.
if (block->x0 < 0 || block->x1 > frame_width - 1 || block->y0 < 0 ||
block->y1 > frame_height - 1) {
return 1;
}
}
return 0;
}
static INLINE void extend_mc_border(const struct scale_factors *const sf,
struct buf_2d *const pre_buf,
MV32 scaled_mv, PadBlock block,
int subpel_x_mv, int subpel_y_mv,
int do_warp, int is_intrabc, int highbd,
uint8_t *mc_buf, uint8_t **pre,
int *src_stride) {
int x_pad = 0, y_pad = 0;
if (update_extend_mc_border_params(sf, pre_buf, scaled_mv, &block,
subpel_x_mv, subpel_y_mv, do_warp,
is_intrabc, &x_pad, &y_pad)) {
// Get reference block pointer.
const uint8_t *const buf_ptr =
pre_buf->buf0 + block.y0 * pre_buf->stride + block.x0;
int buf_stride = pre_buf->stride;
const int b_w = block.x1 - block.x0;
const int b_h = block.y1 - block.y0;
// Extend the border.
if (highbd) {
highbd_build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0,
block.y0, b_w, b_h, pre_buf->width,
pre_buf->height);
} else {
build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0, block.y0, b_w,
b_h, pre_buf->width, pre_buf->height);
}
*src_stride = b_w;
*pre = mc_buf + y_pad * (AOM_INTERP_EXTEND - 1) * b_w +
x_pad * (AOM_INTERP_EXTEND - 1);
}
}
static INLINE void dec_calc_subpel_params(
MACROBLOCKD *xd, const struct scale_factors *const sf, const MV mv,
int plane, const int pre_x, const int pre_y, int x, int y,
struct buf_2d *const pre_buf, SubpelParams *subpel_params, int bw, int bh,
PadBlock *block, int mi_x, int mi_y, MV32 *scaled_mv, int *subpel_x_mv,
int *subpel_y_mv) {
struct macroblockd_plane *const pd = &xd->plane[plane];
const int is_scaled = av1_is_scaled(sf);
if (is_scaled) {
int ssx = pd->subsampling_x;
int ssy = pd->subsampling_y;
int orig_pos_y = (pre_y + y) << SUBPEL_BITS;
orig_pos_y += mv.row * (1 << (1 - ssy));
int orig_pos_x = (pre_x + x) << SUBPEL_BITS;
orig_pos_x += mv.col * (1 << (1 - ssx));
int pos_y = sf->scale_value_y(orig_pos_y, sf);
int pos_x = sf->scale_value_x(orig_pos_x, sf);
pos_x += SCALE_EXTRA_OFF;
pos_y += SCALE_EXTRA_OFF;
const int top = -AOM_LEFT_TOP_MARGIN_SCALED(ssy);
const int left = -AOM_LEFT_TOP_MARGIN_SCALED(ssx);
const int bottom = (pre_buf->height + AOM_INTERP_EXTEND)
<< SCALE_SUBPEL_BITS;
const int right = (pre_buf->width + AOM_INTERP_EXTEND) << SCALE_SUBPEL_BITS;
pos_y = clamp(pos_y, top, bottom);
pos_x = clamp(pos_x, left, right);
subpel_params->subpel_x = pos_x & SCALE_SUBPEL_MASK;
subpel_params->subpel_y = pos_y & SCALE_SUBPEL_MASK;
subpel_params->xs = sf->x_step_q4;
subpel_params->ys = sf->y_step_q4;
// Get reference block top left coordinate.
block->x0 = pos_x >> SCALE_SUBPEL_BITS;
block->y0 = pos_y >> SCALE_SUBPEL_BITS;
// Get reference block bottom right coordinate.
block->x1 =
((pos_x + (bw - 1) * subpel_params->xs) >> SCALE_SUBPEL_BITS) + 1;
block->y1 =
((pos_y + (bh - 1) * subpel_params->ys) >> SCALE_SUBPEL_BITS) + 1;
MV temp_mv;
temp_mv = clamp_mv_to_umv_border_sb(xd, &mv, bw, bh, pd->subsampling_x,
pd->subsampling_y);
*scaled_mv = av1_scale_mv(&temp_mv, (mi_x + x), (mi_y + y), sf);
scaled_mv->row += SCALE_EXTRA_OFF;
scaled_mv->col += SCALE_EXTRA_OFF;
*subpel_x_mv = scaled_mv->col & SCALE_SUBPEL_MASK;
*subpel_y_mv = scaled_mv->row & SCALE_SUBPEL_MASK;
} else {
// Get block position in current frame.
int pos_x = (pre_x + x) << SUBPEL_BITS;
int pos_y = (pre_y + y) << SUBPEL_BITS;
const MV mv_q4 = clamp_mv_to_umv_border_sb(
xd, &mv, bw, bh, pd->subsampling_x, pd->subsampling_y);
subpel_params->xs = subpel_params->ys = SCALE_SUBPEL_SHIFTS;
subpel_params->subpel_x = (mv_q4.col & SUBPEL_MASK) << SCALE_EXTRA_BITS;
subpel_params->subpel_y = (mv_q4.row & SUBPEL_MASK) << SCALE_EXTRA_BITS;
// Get reference block top left coordinate.
pos_x += mv_q4.col;
pos_y += mv_q4.row;
block->x0 = pos_x >> SUBPEL_BITS;
block->y0 = pos_y >> SUBPEL_BITS;
// Get reference block bottom right coordinate.
block->x1 = (pos_x >> SUBPEL_BITS) + (bw - 1) + 1;
block->y1 = (pos_y >> SUBPEL_BITS) + (bh - 1) + 1;
scaled_mv->row = mv_q4.row;
scaled_mv->col = mv_q4.col;
*subpel_x_mv = scaled_mv->col & SUBPEL_MASK;
*subpel_y_mv = scaled_mv->row & SUBPEL_MASK;
}
}
static INLINE void dec_build_inter_predictors(const AV1_COMMON *cm,
MACROBLOCKD *xd, int plane,
const MB_MODE_INFO *mi,
int build_for_obmc, int bw,
int bh, int mi_x, int mi_y) {
struct macroblockd_plane *const pd = &xd->plane[plane];
int is_compound = has_second_ref(mi);
int ref;
const int is_intrabc = is_intrabc_block(mi);
assert(IMPLIES(is_intrabc, !is_compound));
int is_global[2] = { 0, 0 };
for (ref = 0; ref < 1 + is_compound; ++ref) {
const WarpedMotionParams *const wm = &xd->global_motion[mi->ref_frame[ref]];
is_global[ref] = is_global_mv_block(mi, wm->wmtype);
}
const BLOCK_SIZE bsize = mi->sb_type;
const int ss_x = pd->subsampling_x;
const int ss_y = pd->subsampling_y;
int sub8x8_inter = (block_size_wide[bsize] < 8 && ss_x) ||
(block_size_high[bsize] < 8 && ss_y);
if (is_intrabc) sub8x8_inter = 0;
// For sub8x8 chroma blocks, we may be covering more than one luma block's
// worth of pixels. Thus (mi_x, mi_y) may not be the correct coordinates for
// the top-left corner of the prediction source - the correct top-left corner
// is at (pre_x, pre_y).
const int row_start =
(block_size_high[bsize] == 4) && ss_y && !build_for_obmc ? -1 : 0;
const int col_start =
(block_size_wide[bsize] == 4) && ss_x && !build_for_obmc ? -1 : 0;
const int pre_x = (mi_x + MI_SIZE * col_start) >> ss_x;
const int pre_y = (mi_y + MI_SIZE * row_start) >> ss_y;
sub8x8_inter = sub8x8_inter && !build_for_obmc;
if (sub8x8_inter) {
for (int row = row_start; row <= 0 && sub8x8_inter; ++row) {
for (int col = col_start; col <= 0; ++col) {
const MB_MODE_INFO *this_mbmi = xd->mi[row * xd->mi_stride + col];
if (!is_inter_block(this_mbmi)) sub8x8_inter = 0;
if (is_intrabc_block(this_mbmi)) sub8x8_inter = 0;
}
}
}
if (sub8x8_inter) {
// block size
const int b4_w = block_size_wide[bsize] >> ss_x;
const int b4_h = block_size_high[bsize] >> ss_y;
const BLOCK_SIZE plane_bsize = scale_chroma_bsize(bsize, ss_x, ss_y);
const int b8_w = block_size_wide[plane_bsize] >> ss_x;
const int b8_h = block_size_high[plane_bsize] >> ss_y;
assert(!is_compound);
const struct buf_2d orig_pred_buf[2] = { pd->pre[0], pd->pre[1] };
int row = row_start;
int src_stride;
for (int y = 0; y < b8_h; y += b4_h) {
int col = col_start;
for (int x = 0; x < b8_w; x += b4_w) {
MB_MODE_INFO *this_mbmi = xd->mi[row * xd->mi_stride + col];
is_compound = has_second_ref(this_mbmi);
int tmp_dst_stride = 8;
assert(bw < 8 || bh < 8);
ConvolveParams conv_params = get_conv_params_no_round(
0, plane, xd->tmp_conv_dst, tmp_dst_stride, is_compound, xd->bd);
conv_params.use_jnt_comp_avg = 0;
struct buf_2d *const dst_buf = &pd->dst;
uint8_t *dst = dst_buf->buf + dst_buf->stride * y + x;
ref = 0;
const RefBuffer *ref_buf =
&cm->frame_refs[this_mbmi->ref_frame[ref] - LAST_FRAME];
pd->pre[ref].buf0 =
(plane == 1) ? ref_buf->buf->u_buffer : ref_buf->buf->v_buffer;
pd->pre[ref].buf =
pd->pre[ref].buf0 + scaled_buffer_offset(pre_x, pre_y,
ref_buf->buf->uv_stride,
&ref_buf->sf);
pd->pre[ref].width = ref_buf->buf->uv_crop_width;
pd->pre[ref].height = ref_buf->buf->uv_crop_height;
pd->pre[ref].stride = ref_buf->buf->uv_stride;
const struct scale_factors *const sf =
is_intrabc ? &cm->sf_identity : &ref_buf->sf;
struct buf_2d *const pre_buf = is_intrabc ? dst_buf : &pd->pre[ref];
const MV mv = this_mbmi->mv[ref].as_mv;
uint8_t *pre;
SubpelParams subpel_params;
PadBlock block;
MV32 scaled_mv;
int subpel_x_mv, subpel_y_mv;
int highbd;
WarpTypesAllowed warp_types;
warp_types.global_warp_allowed = is_global[ref];
warp_types.local_warp_allowed = this_mbmi->motion_mode == WARPED_CAUSAL;
dec_calc_subpel_params(xd, sf, mv, plane, pre_x, pre_y, x, y, pre_buf,
&subpel_params, bw, bh, &block, mi_x, mi_y,
&scaled_mv, &subpel_x_mv, &subpel_y_mv);
pre = pre_buf->buf0 + block.y0 * pre_buf->stride + block.x0;
src_stride = pre_buf->stride;
highbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
extend_mc_border(sf, pre_buf, scaled_mv, block, subpel_x_mv,
subpel_y_mv, 0, is_intrabc, highbd, xd->mc_buf[ref],
&pre, &src_stride);
conv_params.do_average = ref;
if (is_masked_compound_type(mi->interinter_comp.type)) {
// masked compound type has its own average mechanism
conv_params.do_average = 0;
}
av1_make_inter_predictor(
pre, src_stride, dst, dst_buf->stride, &subpel_params, sf, b4_w,
b4_h, &conv_params, this_mbmi->interp_filters, &warp_types,
(mi_x >> pd->subsampling_x) + x, (mi_y >> pd->subsampling_y) + y,
plane, ref, mi, build_for_obmc, xd, cm->allow_warped_motion);
++col;
}
++row;
}
for (ref = 0; ref < 2; ++ref) pd->pre[ref] = orig_pred_buf[ref];
return;
}
{
struct buf_2d *const dst_buf = &pd->dst;
uint8_t *const dst = dst_buf->buf;
uint8_t *pre[2];
SubpelParams subpel_params[2];
int src_stride[2];
for (ref = 0; ref < 1 + is_compound; ++ref) {
const struct scale_factors *const sf =
is_intrabc ? &cm->sf_identity : &xd->block_refs[ref]->sf;
struct buf_2d *const pre_buf = is_intrabc ? dst_buf : &pd->pre[ref];
const MV mv = mi->mv[ref].as_mv;
PadBlock block;
MV32 scaled_mv;
int subpel_x_mv, subpel_y_mv;
int highbd;
dec_calc_subpel_params(xd, sf, mv, plane, pre_x, pre_y, 0, 0, pre_buf,
&subpel_params[ref], bw, bh, &block, mi_x, mi_y,
&scaled_mv, &subpel_x_mv, &subpel_y_mv);
pre[ref] = pre_buf->buf0 + block.y0 * pre_buf->stride + block.x0;
src_stride[ref] = pre_buf->stride;
highbd = xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH;
WarpTypesAllowed warp_types;
warp_types.global_warp_allowed = is_global[ref];
warp_types.local_warp_allowed = mi->motion_mode == WARPED_CAUSAL;
int do_warp = (bw >= 8 && bh >= 8 &&
av1_allow_warp(mi, &warp_types,
&xd->global_motion[mi->ref_frame[ref]],
build_for_obmc, subpel_params[ref].xs,
subpel_params[ref].ys, NULL));
do_warp = (do_warp && xd->cur_frame_force_integer_mv == 0);
extend_mc_border(sf, pre_buf, scaled_mv, block, subpel_x_mv, subpel_y_mv,
do_warp, is_intrabc, highbd, xd->mc_buf[ref], &pre[ref],
&src_stride[ref]);
}
ConvolveParams conv_params = get_conv_params_no_round(
0, plane, xd->tmp_conv_dst, MAX_SB_SIZE, is_compound, xd->bd);
av1_jnt_comp_weight_assign(cm, mi, 0, &conv_params.fwd_offset,
&conv_params.bck_offset,
&conv_params.use_jnt_comp_avg, is_compound);
for (ref = 0; ref < 1 + is_compound; ++ref) {
const struct scale_factors *const sf =
is_intrabc ? &cm->sf_identity : &xd->block_refs[ref]->sf;
WarpTypesAllowed warp_types;
warp_types.global_warp_allowed = is_global[ref];
warp_types.local_warp_allowed = mi->motion_mode == WARPED_CAUSAL;
conv_params.do_average = ref;
if (is_masked_compound_type(mi->interinter_comp.type)) {
// masked compound type has its own average mechanism
conv_params.do_average = 0;
}
if (ref && is_masked_compound_type(mi->interinter_comp.type))
av1_make_masked_inter_predictor(
pre[ref], src_stride[ref], dst, dst_buf->stride,
&subpel_params[ref], sf, bw, bh, &conv_params, mi->interp_filters,
plane, &warp_types, mi_x >> pd->subsampling_x,
mi_y >> pd->subsampling_y, ref, xd, cm->allow_warped_motion);
else
av1_make_inter_predictor(
pre[ref], src_stride[ref], dst, dst_buf->stride,
&subpel_params[ref], sf, bw, bh, &conv_params, mi->interp_filters,
&warp_types, mi_x >> pd->subsampling_x, mi_y >> pd->subsampling_y,
plane, ref, mi, build_for_obmc, xd, cm->allow_warped_motion);
}
}
}
static void dec_build_inter_predictors_for_planes(const AV1_COMMON *cm,
MACROBLOCKD *xd,
BLOCK_SIZE bsize, int mi_row,
int mi_col, int plane_from,
int plane_to) {
int plane;
const int mi_x = mi_col * MI_SIZE;
const int mi_y = mi_row * MI_SIZE;
for (plane = plane_from; plane <= plane_to; ++plane) {
const struct macroblockd_plane *pd = &xd->plane[plane];
const int bw = pd->width;
const int bh = pd->height;
if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
pd->subsampling_y))
continue;
dec_build_inter_predictors(cm, xd, plane, xd->mi[0], 0, bw, bh, mi_x, mi_y);
}
}
static void dec_build_inter_predictors_sby(const AV1_COMMON *cm,
MACROBLOCKD *xd, int mi_row,
int mi_col, BUFFER_SET *ctx,
BLOCK_SIZE bsize) {
dec_build_inter_predictors_for_planes(cm, xd, bsize, mi_row, mi_col, 0, 0);
if (is_interintra_pred(xd->mi[0])) {
BUFFER_SET default_ctx = { { xd->plane[0].dst.buf, NULL, NULL },
{ xd->plane[0].dst.stride, 0, 0 } };
if (!ctx) ctx = &default_ctx;
av1_build_interintra_predictors_sbp(cm, xd, xd->plane[0].dst.buf,
xd->plane[0].dst.stride, ctx, 0, bsize);
}
}
static void dec_build_inter_predictors_sbuv(const AV1_COMMON *cm,
MACROBLOCKD *xd, int mi_row,
int mi_col, BUFFER_SET *ctx,
BLOCK_SIZE bsize) {
dec_build_inter_predictors_for_planes(cm, xd, bsize, mi_row, mi_col, 1,
MAX_MB_PLANE - 1);
if (is_interintra_pred(xd->mi[0])) {
BUFFER_SET default_ctx = {
{ NULL, xd->plane[1].dst.buf, xd->plane[2].dst.buf },
{ 0, xd->plane[1].dst.stride, xd->plane[2].dst.stride }
};
if (!ctx) ctx = &default_ctx;
av1_build_interintra_predictors_sbuv(
cm, xd, xd->plane[1].dst.buf, xd->plane[2].dst.buf,
xd->plane[1].dst.stride, xd->plane[2].dst.stride, ctx, bsize);
}
}
static void dec_build_inter_predictors_sb(const AV1_COMMON *cm, MACROBLOCKD *xd,
int mi_row, int mi_col,
BUFFER_SET *ctx, BLOCK_SIZE bsize) {
const int num_planes = av1_num_planes(cm);
dec_build_inter_predictors_sby(cm, xd, mi_row, mi_col, ctx, bsize);
if (num_planes > 1)
dec_build_inter_predictors_sbuv(cm, xd, mi_row, mi_col, ctx, bsize);
}
static INLINE void dec_build_prediction_by_above_pred(
MACROBLOCKD *xd, int rel_mi_col, uint8_t above_mi_width,
MB_MODE_INFO *above_mbmi, void *fun_ctxt, const int num_planes) {
struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
const int above_mi_col = ctxt->mi_col + rel_mi_col;
int mi_x, mi_y;
MB_MODE_INFO backup_mbmi = *above_mbmi;
av1_setup_build_prediction_by_above_pred(xd, rel_mi_col, above_mi_width,
&backup_mbmi, ctxt, num_planes);
mi_x = above_mi_col << MI_SIZE_LOG2;
mi_y = ctxt->mi_row << MI_SIZE_LOG2;
const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
for (int j = 0; j < num_planes; ++j) {
const struct macroblockd_plane *pd = &xd->plane[j];
int bw = (above_mi_width * MI_SIZE) >> pd->subsampling_x;
int bh = clamp(block_size_high[bsize] >> (pd->subsampling_y + 1), 4,
block_size_high[BLOCK_64X64] >> (pd->subsampling_y + 1));
if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 0)) continue;
dec_build_inter_predictors(ctxt->cm, xd, j, &backup_mbmi, 1, bw, bh, mi_x,
mi_y);
}
}
static void dec_build_prediction_by_above_preds(
const AV1_COMMON *cm, MACROBLOCKD *xd, int mi_row, int mi_col,
uint8_t *tmp_buf[MAX_MB_PLANE], int tmp_width[MAX_MB_PLANE],
int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
if (!xd->up_available) return;
// Adjust mb_to_bottom_edge to have the correct value for the OBMC
// prediction block. This is half the height of the original block,
// except for 128-wide blocks, where we only use a height of 32.
int this_height = xd->n4_h * MI_SIZE;
int pred_height = AOMMIN(this_height / 2, 32);
xd->mb_to_bottom_edge += (this_height - pred_height) * 8;
struct build_prediction_ctxt ctxt = { cm, mi_row,
mi_col, tmp_buf,
tmp_width, tmp_height,
tmp_stride, xd->mb_to_right_edge };
BLOCK_SIZE bsize = xd->mi[0]->sb_type;
foreach_overlappable_nb_above(cm, xd, mi_col,
max_neighbor_obmc[mi_size_wide_log2[bsize]],
dec_build_prediction_by_above_pred, &ctxt);
xd->mb_to_left_edge = -((mi_col * MI_SIZE) * 8);
xd->mb_to_right_edge = ctxt.mb_to_far_edge;
xd->mb_to_bottom_edge -= (this_height - pred_height) * 8;
}
static INLINE void dec_build_prediction_by_left_pred(
MACROBLOCKD *xd, int rel_mi_row, uint8_t left_mi_height,
MB_MODE_INFO *left_mbmi, void *fun_ctxt, const int num_planes) {
struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
const int left_mi_row = ctxt->mi_row + rel_mi_row;
int mi_x, mi_y;
MB_MODE_INFO backup_mbmi = *left_mbmi;
av1_setup_build_prediction_by_left_pred(xd, rel_mi_row, left_mi_height,
&backup_mbmi, ctxt, num_planes);
mi_x = ctxt->mi_col << MI_SIZE_LOG2;
mi_y = left_mi_row << MI_SIZE_LOG2;
const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
for (int j = 0; j < num_planes; ++j) {
const struct macroblockd_plane *pd = &xd->plane[j];
int bw = clamp(block_size_wide[bsize] >> (pd->subsampling_x + 1), 4,
block_size_wide[BLOCK_64X64] >> (pd->subsampling_x + 1));
int bh = (left_mi_height << MI_SIZE_LOG2) >> pd->subsampling_y;
if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 1)) continue;
dec_build_inter_predictors(ctxt->cm, xd, j, &backup_mbmi, 1, bw, bh, mi_x,
mi_y);
}
}
static void dec_build_prediction_by_left_preds(
const AV1_COMMON *cm, MACROBLOCKD *xd, int mi_row, int mi_col,
uint8_t *tmp_buf[MAX_MB_PLANE], int tmp_width[MAX_MB_PLANE],
int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
if (!xd->left_available) return;
// Adjust mb_to_right_edge to have the correct value for the OBMC
// prediction block. This is half the width of the original block,
// except for 128-wide blocks, where we only use a width of 32.
int this_width = xd->n4_w * MI_SIZE;
int pred_width = AOMMIN(this_width / 2, 32);
xd->mb_to_right_edge += (this_width - pred_width) * 8;
struct build_prediction_ctxt ctxt = { cm, mi_row,
mi_col, tmp_buf,
tmp_width, tmp_height,
tmp_stride, xd->mb_to_bottom_edge };
BLOCK_SIZE bsize = xd->mi[0]->sb_type;
foreach_overlappable_nb_left(cm, xd, mi_row,