-
Notifications
You must be signed in to change notification settings - Fork 54
/
encodetxb.c
2062 lines (1878 loc) · 76.1 KB
/
encodetxb.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) 2017, 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 "av1/encoder/encodetxb.h"
#include "aom_ports/mem.h"
#include "av1/common/blockd.h"
#include "av1/common/idct.h"
#include "av1/common/pred_common.h"
#include "av1/common/scan.h"
#include "av1/encoder/bitstream.h"
#include "av1/encoder/cost.h"
#include "av1/encoder/encodeframe.h"
#include "av1/encoder/hash.h"
#include "av1/encoder/rdopt.h"
#include "av1/encoder/tokenize.h"
static int hbt_needs_init = 1;
static CRC32C crc_calculator;
static const int HBT_EOB = 16; // also the length in opt_qcoeff
static const int HBT_TABLE_SIZE = 65536; // 16 bit: holds 65536 'arrays'
static const int HBT_ARRAY_LENGTH = 256; // 8 bit: 256 entries
// If removed in hbt_create_hashes or increased beyond int8_t, widen deltas type
static const int HBT_KICKOUT = 3;
typedef struct OptTxbQcoeff {
// Use larger type if larger/no kickout value is used in hbt_create_hashes
int8_t deltas[16];
uint32_t hbt_qc_hash;
uint32_t hbt_ctx_hash;
int init;
int rate_cost;
} OptTxbQcoeff;
OptTxbQcoeff *hbt_hash_table;
typedef struct LevelDownStats {
int update;
tran_low_t low_qc;
tran_low_t low_dqc;
int64_t dist0;
int rate;
int rate_low;
int64_t dist;
int64_t dist_low;
int64_t rd;
int64_t rd_low;
int64_t nz_rd;
int64_t rd_diff;
int cost_diff;
int64_t dist_diff;
int new_eob;
} LevelDownStats;
void av1_alloc_txb_buf(AV1_COMP *cpi) {
AV1_COMMON *cm = &cpi->common;
int size = ((cm->mi_rows >> cm->seq_params.mib_size_log2) + 1) *
((cm->mi_cols >> cm->seq_params.mib_size_log2) + 1);
av1_free_txb_buf(cpi);
// TODO(jingning): This should be further reduced.
CHECK_MEM_ERROR(cm, cpi->coeff_buffer_base,
aom_memalign(32, sizeof(*cpi->coeff_buffer_base) * size));
}
void av1_free_txb_buf(AV1_COMP *cpi) { aom_free(cpi->coeff_buffer_base); }
void av1_set_coeff_buffer(const AV1_COMP *const cpi, MACROBLOCK *const x,
int mi_row, int mi_col) {
const AV1_COMMON *const cm = &cpi->common;
const int num_planes = av1_num_planes(cm);
int mib_size_log2 = cm->seq_params.mib_size_log2;
int stride = (cm->mi_cols >> mib_size_log2) + 1;
int offset = (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2);
CB_COEFF_BUFFER *coeff_buf = &cpi->coeff_buffer_base[offset];
const int txb_offset = x->cb_offset / (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
assert(x->cb_offset < (1 << num_pels_log2_lookup[cm->seq_params.sb_size]));
for (int plane = 0; plane < num_planes; ++plane) {
x->mbmi_ext->tcoeff[plane] = coeff_buf->tcoeff[plane] + x->cb_offset;
x->mbmi_ext->eobs[plane] = coeff_buf->eobs[plane] + txb_offset;
x->mbmi_ext->txb_skip_ctx[plane] =
coeff_buf->txb_skip_ctx[plane] + txb_offset;
x->mbmi_ext->dc_sign_ctx[plane] =
coeff_buf->dc_sign_ctx[plane] + txb_offset;
}
}
static void write_golomb(aom_writer *w, int level) {
int x = level + 1;
int i = x;
int length = 0;
while (i) {
i >>= 1;
++length;
}
assert(length > 0);
for (i = 0; i < length - 1; ++i) aom_write_bit(w, 0);
for (i = length - 1; i >= 0; --i) aom_write_bit(w, (x >> i) & 0x01);
}
static INLINE tran_low_t get_lower_coeff(tran_low_t qc) {
if (qc == 0) {
return 0;
}
return qc > 0 ? qc - 1 : qc + 1;
}
static INLINE tran_low_t qcoeff_to_dqcoeff(tran_low_t qc, int coeff_idx,
int dqv, int shift,
const qm_val_t *iqmatrix) {
int sign = qc < 0 ? -1 : 1;
if (iqmatrix != NULL)
dqv =
((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
return sign * ((abs(qc) * dqv) >> shift);
}
static INLINE int64_t get_coeff_dist(tran_low_t tcoeff, tran_low_t dqcoeff,
int shift) {
const int64_t diff = (tcoeff - dqcoeff) * (1 << shift);
const int64_t error = diff * diff;
return error;
}
static const int8_t eob_to_pos_small[33] = {
0, 1, 2, // 0-2
3, 3, // 3-4
4, 4, 4, 4, // 5-8
5, 5, 5, 5, 5, 5, 5, 5, // 9-16
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 // 17-32
};
static const int8_t eob_to_pos_large[17] = {
6, // place holder
7, // 33-64
8, 8, // 65-128
9, 9, 9, 9, // 129-256
10, 10, 10, 10, 10, 10, 10, 10, // 257-512
11 // 513-
};
static INLINE int get_eob_pos_token(const int eob, int *const extra) {
int t;
if (eob < 33) {
t = eob_to_pos_small[eob];
} else {
const int e = AOMMIN((eob - 1) >> 5, 16);
t = eob_to_pos_large[e];
}
*extra = eob - k_eob_group_start[t];
return t;
}
#if CONFIG_ENTROPY_STATS
void av1_update_eob_context(int cdf_idx, int eob, TX_SIZE tx_size,
TX_CLASS tx_class, PLANE_TYPE plane,
FRAME_CONTEXT *ec_ctx, FRAME_COUNTS *counts,
uint8_t allow_update_cdf) {
#else
void av1_update_eob_context(int eob, TX_SIZE tx_size, TX_CLASS tx_class,
PLANE_TYPE plane, FRAME_CONTEXT *ec_ctx,
uint8_t allow_update_cdf) {
#endif
int eob_extra;
const int eob_pt = get_eob_pos_token(eob, &eob_extra);
TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
const int eob_multi_size = txsize_log2_minus4[tx_size];
const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
switch (eob_multi_size) {
case 0:
#if CONFIG_ENTROPY_STATS
++counts->eob_multi16[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
#endif
if (allow_update_cdf)
update_cdf(ec_ctx->eob_flag_cdf16[plane][eob_multi_ctx], eob_pt - 1, 5);
break;
case 1:
#if CONFIG_ENTROPY_STATS
++counts->eob_multi32[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
#endif
if (allow_update_cdf)
update_cdf(ec_ctx->eob_flag_cdf32[plane][eob_multi_ctx], eob_pt - 1, 6);
break;
case 2:
#if CONFIG_ENTROPY_STATS
++counts->eob_multi64[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
#endif
if (allow_update_cdf)
update_cdf(ec_ctx->eob_flag_cdf64[plane][eob_multi_ctx], eob_pt - 1, 7);
break;
case 3:
#if CONFIG_ENTROPY_STATS
++counts->eob_multi128[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
#endif
if (allow_update_cdf) {
update_cdf(ec_ctx->eob_flag_cdf128[plane][eob_multi_ctx], eob_pt - 1,
8);
}
break;
case 4:
#if CONFIG_ENTROPY_STATS
++counts->eob_multi256[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
#endif
if (allow_update_cdf) {
update_cdf(ec_ctx->eob_flag_cdf256[plane][eob_multi_ctx], eob_pt - 1,
9);
}
break;
case 5:
#if CONFIG_ENTROPY_STATS
++counts->eob_multi512[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
#endif
if (allow_update_cdf) {
update_cdf(ec_ctx->eob_flag_cdf512[plane][eob_multi_ctx], eob_pt - 1,
10);
}
break;
case 6:
default:
#if CONFIG_ENTROPY_STATS
++counts->eob_multi1024[cdf_idx][plane][eob_multi_ctx][eob_pt - 1];
#endif
if (allow_update_cdf) {
update_cdf(ec_ctx->eob_flag_cdf1024[plane][eob_multi_ctx], eob_pt - 1,
11);
}
break;
}
if (k_eob_offset_bits[eob_pt] > 0) {
int eob_ctx = eob_pt - 3;
int eob_shift = k_eob_offset_bits[eob_pt] - 1;
int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
#if CONFIG_ENTROPY_STATS
counts->eob_extra[cdf_idx][txs_ctx][plane][eob_pt][bit]++;
#endif // CONFIG_ENTROPY_STATS
if (allow_update_cdf)
update_cdf(ec_ctx->eob_extra_cdf[txs_ctx][plane][eob_ctx], bit, 2);
}
}
static int get_eob_cost(int eob, const LV_MAP_EOB_COST *txb_eob_costs,
const LV_MAP_COEFF_COST *txb_costs, TX_CLASS tx_class) {
int eob_extra;
const int eob_pt = get_eob_pos_token(eob, &eob_extra);
int eob_cost = 0;
const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
eob_cost = txb_eob_costs->eob_cost[eob_multi_ctx][eob_pt - 1];
if (k_eob_offset_bits[eob_pt] > 0) {
const int eob_ctx = eob_pt - 3;
const int eob_shift = k_eob_offset_bits[eob_pt] - 1;
const int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
eob_cost += txb_costs->eob_extra_cost[eob_ctx][bit];
const int offset_bits = k_eob_offset_bits[eob_pt];
if (offset_bits > 1) eob_cost += av1_cost_literal(offset_bits - 1);
}
return eob_cost;
}
static INLINE int get_sign_bit_cost(tran_low_t qc, int coeff_idx,
const int (*dc_sign_cost)[2],
int dc_sign_ctx) {
if (coeff_idx == 0) {
const int sign = (qc < 0) ? 1 : 0;
return dc_sign_cost[dc_sign_ctx][sign];
}
return av1_cost_literal(1);
}
static INLINE int get_br_cost(tran_low_t abs_qc, int ctx,
const int *coeff_lps) {
const tran_low_t min_level = 1 + NUM_BASE_LEVELS;
const tran_low_t max_level = 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE;
(void)ctx;
if (abs_qc >= min_level) {
if (abs_qc >= max_level) {
return coeff_lps[COEFF_BASE_RANGE]; // COEFF_BASE_RANGE * cost0;
} else {
return coeff_lps[(abs_qc - min_level)]; // * cost0 + cost1;
}
}
return 0;
}
static INLINE int get_golomb_cost(int abs_qc) {
if (abs_qc >= 1 + NUM_BASE_LEVELS + COEFF_BASE_RANGE) {
const int r = abs_qc - COEFF_BASE_RANGE - NUM_BASE_LEVELS;
const int length = get_msb(r) + 1;
return av1_cost_literal(2 * length - 1);
}
return 0;
}
static int get_coeff_cost(const tran_low_t qc, const int scan_idx,
const int is_eob, const TxbInfo *const txb_info,
const LV_MAP_COEFF_COST *const txb_costs,
const int coeff_ctx, const TX_CLASS tx_class) {
const TXB_CTX *const txb_ctx = txb_info->txb_ctx;
const int is_nz = (qc != 0);
const tran_low_t abs_qc = abs(qc);
int cost = 0;
const int16_t *const scan = txb_info->scan_order->scan;
const int pos = scan[scan_idx];
if (is_eob) {
cost += txb_costs->base_eob_cost[coeff_ctx][AOMMIN(abs_qc, 3) - 1];
} else {
cost += txb_costs->base_cost[coeff_ctx][AOMMIN(abs_qc, 3)];
}
if (is_nz) {
cost += get_sign_bit_cost(qc, scan_idx, txb_costs->dc_sign_cost,
txb_ctx->dc_sign_ctx);
if (abs_qc > NUM_BASE_LEVELS) {
const int ctx =
get_br_ctx(txb_info->levels, pos, txb_info->bwl, tx_class);
cost += get_br_cost(abs_qc, ctx, txb_costs->lps_cost[ctx]);
cost += get_golomb_cost(abs_qc);
}
}
return cost;
}
static INLINE int get_nz_map_ctx(const uint8_t *const levels,
const int coeff_idx, const int bwl,
const int height, const int scan_idx,
const int is_eob, const TX_SIZE tx_size,
const TX_CLASS tx_class) {
if (is_eob) {
if (scan_idx == 0) return 0;
if (scan_idx <= (height << bwl) / 8) return 1;
if (scan_idx <= (height << bwl) / 4) return 2;
return 3;
}
const int stats =
get_nz_mag(levels + get_padded_idx(coeff_idx, bwl), bwl, tx_class);
return get_nz_map_ctx_from_stats(stats, coeff_idx, bwl, tx_size, tx_class);
}
static void get_dist_cost_stats(LevelDownStats *const stats, const int scan_idx,
const int is_eob,
const LV_MAP_COEFF_COST *const txb_costs,
const TxbInfo *const txb_info,
const TX_CLASS tx_class) {
const int16_t *const scan = txb_info->scan_order->scan;
const int coeff_idx = scan[scan_idx];
const tran_low_t qc = txb_info->qcoeff[coeff_idx];
const uint8_t *const levels = txb_info->levels;
stats->new_eob = -1;
stats->update = 0;
stats->rd_low = 0;
stats->rd = 0;
stats->nz_rd = 0;
stats->dist_low = 0;
stats->rate_low = 0;
stats->low_qc = 0;
const tran_low_t tqc = txb_info->tcoeff[coeff_idx];
const int dqv = txb_info->dequant[coeff_idx != 0];
const int coeff_ctx =
get_nz_map_ctx(levels, coeff_idx, txb_info->bwl, txb_info->height,
scan_idx, is_eob, txb_info->tx_size, tx_class);
const int qc_cost = get_coeff_cost(qc, scan_idx, is_eob, txb_info, txb_costs,
coeff_ctx, tx_class);
assert(qc != 0);
const tran_low_t dqc = qcoeff_to_dqcoeff(qc, coeff_idx, dqv, txb_info->shift,
txb_info->iqmatrix);
const int64_t dqc_dist = get_coeff_dist(tqc, dqc, txb_info->shift);
// distortion difference when coefficient is quantized to 0
const tran_low_t dqc0 =
qcoeff_to_dqcoeff(0, coeff_idx, dqv, txb_info->shift, txb_info->iqmatrix);
stats->dist0 = get_coeff_dist(tqc, dqc0, txb_info->shift);
stats->dist = dqc_dist - stats->dist0;
stats->rate = qc_cost;
stats->rd = RDCOST(txb_info->rdmult, stats->rate, stats->dist);
stats->low_qc = get_lower_coeff(qc);
if (is_eob && stats->low_qc == 0) {
stats->rd_low = stats->rd; // disable selection of low_qc in this case.
} else {
if (stats->low_qc == 0) {
stats->dist_low = 0;
} else {
stats->low_dqc = qcoeff_to_dqcoeff(stats->low_qc, coeff_idx, dqv,
txb_info->shift, txb_info->iqmatrix);
const int64_t low_dqc_dist =
get_coeff_dist(tqc, stats->low_dqc, txb_info->shift);
stats->dist_low = low_dqc_dist - stats->dist0;
}
const int low_qc_cost =
get_coeff_cost(stats->low_qc, scan_idx, is_eob, txb_info, txb_costs,
coeff_ctx, tx_class);
stats->rate_low = low_qc_cost;
stats->rd_low = RDCOST(txb_info->rdmult, stats->rate_low, stats->dist_low);
}
}
static void get_dist_cost_stats_with_eob(
LevelDownStats *const stats, const int scan_idx,
const LV_MAP_COEFF_COST *const txb_costs, const TxbInfo *const txb_info,
const TX_CLASS tx_class) {
const int is_eob = 0;
get_dist_cost_stats(stats, scan_idx, is_eob, txb_costs, txb_info, tx_class);
const int16_t *const scan = txb_info->scan_order->scan;
const int coeff_idx = scan[scan_idx];
const tran_low_t qc = txb_info->qcoeff[coeff_idx];
const int coeff_ctx_temp = get_nz_map_ctx(
txb_info->levels, coeff_idx, txb_info->bwl, txb_info->height, scan_idx, 1,
txb_info->tx_size, tx_class);
const int qc_eob_cost = get_coeff_cost(qc, scan_idx, 1, txb_info, txb_costs,
coeff_ctx_temp, tx_class);
int64_t rd_eob = RDCOST(txb_info->rdmult, qc_eob_cost, stats->dist);
if (stats->low_qc != 0) {
const int low_qc_eob_cost =
get_coeff_cost(stats->low_qc, scan_idx, 1, txb_info, txb_costs,
coeff_ctx_temp, tx_class);
int64_t rd_eob_low =
RDCOST(txb_info->rdmult, low_qc_eob_cost, stats->dist_low);
rd_eob = (rd_eob > rd_eob_low) ? rd_eob_low : rd_eob;
}
stats->nz_rd = AOMMIN(stats->rd_low, stats->rd) - rd_eob;
}
static INLINE void update_qcoeff(const int coeff_idx, const tran_low_t qc,
const TxbInfo *const txb_info) {
txb_info->qcoeff[coeff_idx] = qc;
txb_info->levels[get_padded_idx(coeff_idx, txb_info->bwl)] =
(uint8_t)clamp(abs(qc), 0, INT8_MAX);
}
static INLINE void update_coeff(const int coeff_idx, const tran_low_t qc,
const TxbInfo *const txb_info) {
update_qcoeff(coeff_idx, qc, txb_info);
const int dqv = txb_info->dequant[coeff_idx != 0];
txb_info->dqcoeff[coeff_idx] = qcoeff_to_dqcoeff(
qc, coeff_idx, dqv, txb_info->shift, txb_info->iqmatrix);
}
void av1_txb_init_levels_c(const tran_low_t *const coeff, const int width,
const int height, uint8_t *const levels) {
const int stride = width + TX_PAD_HOR;
uint8_t *ls = levels;
memset(levels - TX_PAD_TOP * stride, 0,
sizeof(*levels) * TX_PAD_TOP * stride);
memset(levels + stride * height, 0,
sizeof(*levels) * (TX_PAD_BOTTOM * stride + TX_PAD_END));
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
*ls++ = (uint8_t)clamp(abs(coeff[i * width + j]), 0, INT8_MAX);
}
for (int j = 0; j < TX_PAD_HOR; j++) {
*ls++ = 0;
}
}
}
void av1_get_nz_map_contexts_c(const uint8_t *const levels,
const int16_t *const scan, const uint16_t eob,
const TX_SIZE tx_size, const TX_CLASS tx_class,
int8_t *const coeff_contexts) {
const int bwl = get_txb_bwl(tx_size);
const int height = get_txb_high(tx_size);
for (int i = 0; i < eob; ++i) {
const int pos = scan[i];
coeff_contexts[pos] = get_nz_map_ctx(levels, pos, bwl, height, i,
i == eob - 1, tx_size, tx_class);
}
}
void av1_write_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *xd,
aom_writer *w, int blk_row, int blk_col, int plane,
TX_SIZE tx_size, const tran_low_t *tcoeff,
uint16_t eob, TXB_CTX *txb_ctx) {
const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
aom_write_symbol(w, eob == 0,
ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2);
if (eob == 0) return;
const PLANE_TYPE plane_type = get_plane_type(plane);
const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col,
tx_size, cm->reduced_tx_set_used);
const TX_CLASS tx_class = tx_type_to_class[tx_type];
const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
const int16_t *const scan = scan_order->scan;
int c;
const int bwl = get_txb_bwl(tx_size);
const int width = get_txb_wide(tx_size);
const int height = get_txb_high(tx_size);
uint8_t levels_buf[TX_PAD_2D];
uint8_t *const levels = set_levels(levels_buf, width);
DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
av1_txb_init_levels(tcoeff, width, height, levels);
av1_write_tx_type(cm, xd, blk_row, blk_col, plane, tx_size, w);
int eob_extra;
const int eob_pt = get_eob_pos_token(eob, &eob_extra);
const int eob_multi_size = txsize_log2_minus4[tx_size];
const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
switch (eob_multi_size) {
case 0:
aom_write_symbol(w, eob_pt - 1,
ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx], 5);
break;
case 1:
aom_write_symbol(w, eob_pt - 1,
ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx], 6);
break;
case 2:
aom_write_symbol(w, eob_pt - 1,
ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx], 7);
break;
case 3:
aom_write_symbol(w, eob_pt - 1,
ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx], 8);
break;
case 4:
aom_write_symbol(w, eob_pt - 1,
ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx], 9);
break;
case 5:
aom_write_symbol(w, eob_pt - 1,
ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx], 10);
break;
default:
aom_write_symbol(w, eob_pt - 1,
ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11);
break;
}
if (k_eob_offset_bits[eob_pt] > 0) {
const int eob_ctx = eob_pt - 3;
int eob_shift = k_eob_offset_bits[eob_pt] - 1;
int bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
aom_write_symbol(w, bit,
ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2);
for (int i = 1; i < k_eob_offset_bits[eob_pt]; i++) {
eob_shift = k_eob_offset_bits[eob_pt] - 1 - i;
bit = (eob_extra & (1 << eob_shift)) ? 1 : 0;
aom_write_bit(w, bit);
}
}
av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
for (c = eob - 1; c >= 0; --c) {
const int pos = scan[c];
const int coeff_ctx = coeff_contexts[pos];
const tran_low_t v = tcoeff[pos];
const tran_low_t level = abs(v);
if (c == eob - 1) {
aom_write_symbol(
w, AOMMIN(level, 3) - 1,
ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx], 3);
} else {
aom_write_symbol(w, AOMMIN(level, 3),
ec_ctx->coeff_base_cdf[txs_ctx][plane_type][coeff_ctx],
4);
}
if (level > NUM_BASE_LEVELS) {
// level is above 1.
const int base_range = level - 1 - NUM_BASE_LEVELS;
const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
const int k = AOMMIN(base_range - idx, BR_CDF_SIZE - 1);
aom_write_symbol(
w, k,
ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx],
BR_CDF_SIZE);
if (k < BR_CDF_SIZE - 1) break;
}
}
}
// Loop to code all signs in the transform block,
// starting with the sign of DC (if applicable)
for (c = 0; c < eob; ++c) {
const tran_low_t v = tcoeff[scan[c]];
const tran_low_t level = abs(v);
const int sign = (v < 0) ? 1 : 0;
if (level) {
if (c == 0) {
aom_write_symbol(
w, sign, ec_ctx->dc_sign_cdf[plane_type][txb_ctx->dc_sign_ctx], 2);
} else {
aom_write_bit(w, sign);
}
if (level > COEFF_BASE_RANGE + NUM_BASE_LEVELS)
write_golomb(w, level - COEFF_BASE_RANGE - 1 - NUM_BASE_LEVELS);
}
}
}
typedef struct encode_txb_args {
const AV1_COMMON *cm;
MACROBLOCK *x;
aom_writer *w;
} ENCODE_TXB_ARGS;
static void write_coeffs_txb_wrap(const AV1_COMMON *cm, MACROBLOCK *x,
aom_writer *w, int plane, int block,
int blk_row, int blk_col, TX_SIZE tx_size) {
MACROBLOCKD *xd = &x->e_mbd;
tran_low_t *tcoeff = BLOCK_OFFSET(x->mbmi_ext->tcoeff[plane], block);
uint16_t eob = x->mbmi_ext->eobs[plane][block];
TXB_CTX txb_ctx = { x->mbmi_ext->txb_skip_ctx[plane][block],
x->mbmi_ext->dc_sign_ctx[plane][block] };
av1_write_coeffs_txb(cm, xd, w, blk_row, blk_col, plane, tx_size, tcoeff, eob,
&txb_ctx);
}
void av1_write_coeffs_mb(const AV1_COMMON *const cm, MACROBLOCK *x, int mi_row,
int mi_col, aom_writer *w, BLOCK_SIZE bsize) {
MACROBLOCKD *xd = &x->e_mbd;
const int num_planes = av1_num_planes(cm);
int block[MAX_MB_PLANE] = { 0 };
int row, col;
assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
xd->plane[0].subsampling_y));
const int max_blocks_wide = max_block_wide(xd, bsize, 0);
const int max_blocks_high = max_block_high(xd, bsize, 0);
const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
int mu_blocks_wide = block_size_wide[max_unit_bsize] >> tx_size_wide_log2[0];
int mu_blocks_high = block_size_high[max_unit_bsize] >> tx_size_high_log2[0];
mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
for (int plane = 0; plane < num_planes; ++plane) {
const struct macroblockd_plane *const pd = &xd->plane[plane];
if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
pd->subsampling_y))
continue;
const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
const int stepr = tx_size_high_unit[tx_size];
const int stepc = tx_size_wide_unit[tx_size];
const int step = stepr * stepc;
const int unit_height = ROUND_POWER_OF_TWO(
AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y);
const int unit_width = ROUND_POWER_OF_TWO(
AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x);
for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height;
blk_row += stepr) {
for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width;
blk_col += stepc) {
write_coeffs_txb_wrap(cm, x, w, plane, block[plane], blk_row,
blk_col, tx_size);
block[plane] += step;
}
}
}
}
}
}
// TODO(angiebird): use this function whenever it's possible
static int get_tx_type_cost(const AV1_COMMON *cm, const MACROBLOCK *x,
const MACROBLOCKD *xd, int plane, TX_SIZE tx_size,
TX_TYPE tx_type) {
if (plane > 0) return 0;
const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
const MB_MODE_INFO *mbmi = xd->mi[0];
const int is_inter = is_inter_block(mbmi);
if (get_ext_tx_types(tx_size, is_inter, cm->reduced_tx_set_used) > 1 &&
!xd->lossless[xd->mi[0]->segment_id]) {
const int ext_tx_set =
get_ext_tx_set(tx_size, is_inter, cm->reduced_tx_set_used);
if (is_inter) {
if (ext_tx_set > 0)
return x->inter_tx_type_costs[ext_tx_set][square_tx_size][tx_type];
} else {
if (ext_tx_set > 0) {
PREDICTION_MODE intra_dir;
if (mbmi->filter_intra_mode_info.use_filter_intra)
intra_dir = fimode_to_intradir[mbmi->filter_intra_mode_info
.filter_intra_mode];
else
intra_dir = mbmi->mode;
return x->intra_tx_type_costs[ext_tx_set][square_tx_size][intra_dir]
[tx_type];
}
}
}
return 0;
}
static AOM_FORCE_INLINE int warehouse_efficients_txb(
const AV1_COMMON *const cm, const MACROBLOCK *x, const int plane,
const int block, const TX_SIZE tx_size, const TXB_CTX *const txb_ctx,
const struct macroblock_plane *p, const int eob,
const PLANE_TYPE plane_type, const LV_MAP_COEFF_COST *const coeff_costs,
const MACROBLOCKD *const xd, const TX_TYPE tx_type,
const TX_CLASS tx_class) {
const tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
const int txb_skip_ctx = txb_ctx->txb_skip_ctx;
const int bwl = get_txb_bwl(tx_size);
const int width = get_txb_wide(tx_size);
const int height = get_txb_high(tx_size);
const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
const int16_t *const scan = scan_order->scan;
uint8_t levels_buf[TX_PAD_2D];
uint8_t *const levels = set_levels(levels_buf, width);
DECLARE_ALIGNED(16, int8_t, coeff_contexts[MAX_TX_SQUARE]);
const int eob_multi_size = txsize_log2_minus4[tx_size];
const LV_MAP_EOB_COST *const eob_costs =
&x->eob_costs[eob_multi_size][plane_type];
int cost = coeff_costs->txb_skip_cost[txb_skip_ctx][0];
av1_txb_init_levels(qcoeff, width, height, levels);
cost += get_tx_type_cost(cm, x, xd, plane, tx_size, tx_type);
cost += get_eob_cost(eob, eob_costs, coeff_costs, tx_class);
av1_get_nz_map_contexts(levels, scan, eob, tx_size, tx_class, coeff_contexts);
const int(*lps_cost)[COEFF_BASE_RANGE + 1] = coeff_costs->lps_cost;
int c = eob - 1;
{
const int pos = scan[c];
const tran_low_t v = qcoeff[pos];
const int sign = v >> 31;
const int level = (v ^ sign) - sign;
const int coeff_ctx = coeff_contexts[pos];
cost += coeff_costs->base_eob_cost[coeff_ctx][AOMMIN(level, 3) - 1];
if (v) {
// sign bit cost
if (level > NUM_BASE_LEVELS) {
const int ctx = get_br_ctx(levels, pos, bwl, tx_class);
const int base_range =
AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
cost += lps_cost[ctx][base_range];
cost += get_golomb_cost(level);
}
if (c) {
cost += av1_cost_literal(1);
} else {
const int sign01 = (sign ^ sign) - sign;
const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
return cost;
}
}
}
const int(*base_cost)[4] = coeff_costs->base_cost;
for (c = eob - 2; c >= 1; --c) {
const int pos = scan[c];
const int coeff_ctx = coeff_contexts[pos];
const tran_low_t v = qcoeff[pos];
const int level = abs(v);
const int cost0 = base_cost[coeff_ctx][AOMMIN(level, 3)];
if (v) {
// sign bit cost
cost += av1_cost_literal(1);
if (level > NUM_BASE_LEVELS) {
const int ctx = get_br_ctx(levels, pos, bwl, tx_class);
const int base_range =
AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
cost += lps_cost[ctx][base_range];
cost += get_golomb_cost(level);
}
}
cost += cost0;
}
if (c == 0) {
const int pos = scan[c];
const tran_low_t v = qcoeff[pos];
const int coeff_ctx = coeff_contexts[pos];
const int sign = v >> 31;
const int level = (v ^ sign) - sign;
cost += base_cost[coeff_ctx][AOMMIN(level, 3)];
if (v) {
// sign bit cost
const int sign01 = (sign ^ sign) - sign;
const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
cost += coeff_costs->dc_sign_cost[dc_sign_ctx][sign01];
if (level > NUM_BASE_LEVELS) {
const int ctx = get_br_ctx(levels, pos, bwl, tx_class);
const int base_range =
AOMMIN(level - 1 - NUM_BASE_LEVELS, COEFF_BASE_RANGE);
cost += lps_cost[ctx][base_range];
cost += get_golomb_cost(level);
}
}
}
return cost;
}
int av1_cost_coeffs_txb(const AV1_COMMON *const cm, const MACROBLOCK *x,
const int plane, const int block, const TX_SIZE tx_size,
const TX_TYPE tx_type, const TXB_CTX *const txb_ctx) {
const struct macroblock_plane *p = &x->plane[plane];
const int eob = p->eobs[block];
const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
const PLANE_TYPE plane_type = get_plane_type(plane);
const LV_MAP_COEFF_COST *const coeff_costs =
&x->coeff_costs[txs_ctx][plane_type];
if (eob == 0) {
return coeff_costs->txb_skip_cost[txb_ctx->txb_skip_ctx][1];
}
const MACROBLOCKD *const xd = &x->e_mbd;
const TX_CLASS tx_class = tx_type_to_class[tx_type];
#define WAREHOUSE_EFFICIENTS_TXB_CASE(tx_class_literal) \
case tx_class_literal: \
return warehouse_efficients_txb(cm, x, plane, block, tx_size, txb_ctx, p, \
eob, plane_type, coeff_costs, xd, tx_type, \
tx_class_literal);
switch (tx_class) {
WAREHOUSE_EFFICIENTS_TXB_CASE(TX_CLASS_2D);
WAREHOUSE_EFFICIENTS_TXB_CASE(TX_CLASS_HORIZ);
WAREHOUSE_EFFICIENTS_TXB_CASE(TX_CLASS_VERT);
#undef WAREHOUSE_EFFICIENTS_TXB_CASE
default: assert(false); return 0;
}
}
static int optimize_txb(TxbInfo *txb_info, const LV_MAP_COEFF_COST *txb_costs,
const LV_MAP_EOB_COST *txb_eob_costs, int *rate_cost) {
int update = 0;
if (txb_info->eob == 0) return update;
const int16_t *const scan = txb_info->scan_order->scan;
// forward optimize the nz_map`
const int init_eob = txb_info->eob;
const TX_CLASS tx_class = tx_type_to_class[txb_info->tx_type];
const int eob_cost =
get_eob_cost(init_eob, txb_eob_costs, txb_costs, tx_class);
// backward optimize the level-k map
int accu_rate = eob_cost;
int64_t accu_dist = 0;
int64_t prev_eob_rd_cost = INT64_MAX;
int64_t cur_eob_rd_cost = 0;
{
const int si = init_eob - 1;
const int coeff_idx = scan[si];
LevelDownStats stats;
get_dist_cost_stats(&stats, si, si == init_eob - 1, txb_costs, txb_info,
tx_class);
if ((stats.rd_low < stats.rd) && (stats.low_qc != 0)) {
update = 1;
update_coeff(coeff_idx, stats.low_qc, txb_info);
accu_rate += stats.rate_low;
accu_dist += stats.dist_low;
} else {
accu_rate += stats.rate;
accu_dist += stats.dist;
}
}
int si = init_eob - 2;
int8_t has_nz_tail = 0;
// eob is not fixed
for (; si >= 0 && has_nz_tail < 2; --si) {
assert(si != init_eob - 1);
const int coeff_idx = scan[si];
tran_low_t qc = txb_info->qcoeff[coeff_idx];
if (qc == 0) {
const int coeff_ctx =
get_lower_levels_ctx(txb_info->levels, coeff_idx, txb_info->bwl,
txb_info->tx_size, tx_class);
accu_rate += txb_costs->base_cost[coeff_ctx][0];
} else {
LevelDownStats stats;
get_dist_cost_stats_with_eob(&stats, si, txb_costs, txb_info, tx_class);
// check if it is better to make this the last significant coefficient
int cur_eob_rate =
get_eob_cost(si + 1, txb_eob_costs, txb_costs, tx_class);
cur_eob_rd_cost = RDCOST(txb_info->rdmult, cur_eob_rate, 0);
prev_eob_rd_cost =
RDCOST(txb_info->rdmult, accu_rate, accu_dist) + stats.nz_rd;
if (cur_eob_rd_cost <= prev_eob_rd_cost) {
update = 1;
for (int j = si + 1; j < txb_info->eob; j++) {
const int coeff_pos_j = scan[j];
update_coeff(coeff_pos_j, 0, txb_info);
}
txb_info->eob = si + 1;
// rerun cost calculation due to change of eob
accu_rate = cur_eob_rate;
accu_dist = 0;
get_dist_cost_stats(&stats, si, 1, txb_costs, txb_info, tx_class);
if ((stats.rd_low < stats.rd) && (stats.low_qc != 0)) {
update = 1;
update_coeff(coeff_idx, stats.low_qc, txb_info);
accu_rate += stats.rate_low;
accu_dist += stats.dist_low;
} else {
accu_rate += stats.rate;
accu_dist += stats.dist;
}
// reset non zero tail when new eob is found
has_nz_tail = 0;
} else {
int bUpdCoeff = 0;
if (stats.rd_low < stats.rd) {
if ((si < txb_info->eob - 1)) {
bUpdCoeff = 1;
update = 1;
}
} else {
++has_nz_tail;
}
if (bUpdCoeff) {
update_coeff(coeff_idx, stats.low_qc, txb_info);
accu_rate += stats.rate_low;
accu_dist += stats.dist_low;
} else {
accu_rate += stats.rate;
accu_dist += stats.dist;
}
}
}
} // for (si)
// eob is fixed
for (; si >= 0; --si) {
assert(si != init_eob - 1);
const int coeff_idx = scan[si];
tran_low_t qc = txb_info->qcoeff[coeff_idx];
if (qc == 0) {
const int coeff_ctx =
get_lower_levels_ctx(txb_info->levels, coeff_idx, txb_info->bwl,
txb_info->tx_size, tx_class);
accu_rate += txb_costs->base_cost[coeff_ctx][0];
} else {
LevelDownStats stats;
get_dist_cost_stats(&stats, si, 0, txb_costs, txb_info, tx_class);
int bUpdCoeff = 0;
if (stats.rd_low < stats.rd) {
if ((si < txb_info->eob - 1)) {
bUpdCoeff = 1;
update = 1;
}
}
if (bUpdCoeff) {
update_coeff(coeff_idx, stats.low_qc, txb_info);
accu_rate += stats.rate_low;
accu_dist += stats.dist_low;
} else {
accu_rate += stats.rate;
accu_dist += stats.dist;
}
}
} // for (si)
int non_zero_blk_rate =
txb_costs->txb_skip_cost[txb_info->txb_ctx->txb_skip_ctx][0];
prev_eob_rd_cost =
RDCOST(txb_info->rdmult, accu_rate + non_zero_blk_rate, accu_dist);
int zero_blk_rate =
txb_costs->txb_skip_cost[txb_info->txb_ctx->txb_skip_ctx][1];
int64_t zero_blk_rd_cost = RDCOST(txb_info->rdmult, zero_blk_rate, 0);
if (zero_blk_rd_cost <= prev_eob_rd_cost) {
update = 1;
for (int j = 0; j < txb_info->eob; j++) {
const int coeff_pos_j = scan[j];
update_coeff(coeff_pos_j, 0, txb_info);