2 * Copyright (c) 2017, Alliance for Open Media. All rights reserved
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
12 #include "av1/decoder/decodetxb.h"
14 #include "aom_ports/mem.h"
15 #include "av1/common/idct.h"
16 #include "av1/common/scan.h"
17 #include "av1/common/txb_common.h"
18 #include "av1/decoder/decodemv.h"
20 #define ACCT_STR __func__
22 static int read_golomb(MACROBLOCKD
*xd
, aom_reader
*r
) {
28 i
= aom_read_bit(r
, ACCT_STR
);
31 aom_internal_error(xd
->error_info
, AOM_CODEC_CORRUPT_FRAME
,
32 "Invalid length in read_golomb");
37 for (i
= 0; i
< length
- 1; ++i
) {
39 x
+= aom_read_bit(r
, ACCT_STR
);
45 static INLINE
int rec_eob_pos(const int eob_token
, const int extra
) {
46 int eob
= k_eob_group_start
[eob_token
];
53 static INLINE
int get_dqv(const int16_t *dequant
, int coeff_idx
,
54 const qm_val_t
*iqmatrix
) {
55 int dqv
= dequant
[!!coeff_idx
];
58 ((iqmatrix
[coeff_idx
] * dqv
) + (1 << (AOM_QM_BITS
- 1))) >> AOM_QM_BITS
;
62 static INLINE
void read_coeffs_reverse_2d(aom_reader
*r
, TX_SIZE tx_size
,
63 int start_si
, int end_si
,
64 const int16_t *scan
, int bwl
,
66 base_cdf_arr base_cdf
,
68 for (int c
= end_si
; c
>= start_si
; --c
) {
69 const int pos
= scan
[c
];
70 const int coeff_ctx
= get_lower_levels_ctx_2d(levels
, pos
, bwl
, tx_size
);
72 int level
= aom_read_symbol(r
, base_cdf
[coeff_ctx
], nsymbs
, ACCT_STR
);
73 if (level
> NUM_BASE_LEVELS
) {
74 const int br_ctx
= get_br_ctx_2d(levels
, pos
, bwl
);
75 aom_cdf_prob
*cdf
= br_cdf
[br_ctx
];
76 for (int idx
= 0; idx
< COEFF_BASE_RANGE
; idx
+= BR_CDF_SIZE
- 1) {
77 const int k
= aom_read_symbol(r
, cdf
, BR_CDF_SIZE
, ACCT_STR
);
79 if (k
< BR_CDF_SIZE
- 1) break;
82 levels
[get_padded_idx(pos
, bwl
)] = level
;
86 static INLINE
void read_coeffs_reverse(aom_reader
*r
, TX_SIZE tx_size
,
87 TX_TYPE tx_type
, int start_si
,
88 int end_si
, const int16_t *scan
, int bwl
,
89 uint8_t *levels
, base_cdf_arr base_cdf
,
91 for (int c
= end_si
; c
>= start_si
; --c
) {
92 const int pos
= scan
[c
];
94 get_lower_levels_ctx(levels
, pos
, bwl
, tx_size
, tx_type
);
96 int level
= aom_read_symbol(r
, base_cdf
[coeff_ctx
], nsymbs
, ACCT_STR
);
97 if (level
> NUM_BASE_LEVELS
) {
98 const int br_ctx
= get_br_ctx(levels
, pos
, bwl
, tx_type
);
99 aom_cdf_prob
*cdf
= br_cdf
[br_ctx
];
100 for (int idx
= 0; idx
< COEFF_BASE_RANGE
; idx
+= BR_CDF_SIZE
- 1) {
101 const int k
= aom_read_symbol(r
, cdf
, BR_CDF_SIZE
, ACCT_STR
);
103 if (k
< BR_CDF_SIZE
- 1) break;
106 levels
[get_padded_idx(pos
, bwl
)] = level
;
110 uint8_t av1_read_coeffs_txb(const AV1_COMMON
*const cm
, MACROBLOCKD
*const xd
,
111 aom_reader
*const r
, const int blk_row
,
112 const int blk_col
, const int plane
,
113 const TXB_CTX
*const txb_ctx
, const TX_SIZE tx_size
,
114 int16_t *const max_scan_line
, int *const eob
) {
115 FRAME_CONTEXT
*const ec_ctx
= xd
->tile_ctx
;
116 const int32_t max_value
= (1 << (7 + xd
->bd
)) - 1;
117 const int32_t min_value
= -(1 << (7 + xd
->bd
));
118 const TX_SIZE txs_ctx
= get_txsize_entropy_ctx(tx_size
);
119 const PLANE_TYPE plane_type
= get_plane_type(plane
);
120 MB_MODE_INFO
*const mbmi
= &xd
->mi
[0]->mbmi
;
121 struct macroblockd_plane
*const pd
= &xd
->plane
[plane
];
122 const int16_t *const dequant
= pd
->seg_dequant_QTX
[mbmi
->segment_id
];
123 tran_low_t
*const tcoeffs
= pd
->dqcoeff
;
124 const int shift
= av1_get_tx_scale(tx_size
);
125 const int bwl
= get_txb_bwl(tx_size
);
126 const int width
= get_txb_wide(tx_size
);
127 const int height
= get_txb_high(tx_size
);
129 uint8_t levels_buf
[TX_PAD_2D
];
130 uint8_t *const levels
= set_levels(levels_buf
, width
);
131 const int all_zero
= aom_read_symbol(
132 r
, ec_ctx
->txb_skip_cdf
[txs_ctx
][txb_ctx
->txb_skip_ctx
], 2, ACCT_STR
);
137 const int txk_type_idx
=
138 av1_get_txk_type_index(mbmi
->sb_type
, blk_row
, blk_col
);
139 mbmi
->txk_type
[txk_type_idx
] = DCT_DCT
;
144 memset(levels_buf
, 0,
145 sizeof(*levels_buf
) *
146 ((width
+ TX_PAD_HOR
) * (height
+ TX_PAD_VER
) + TX_PAD_END
));
147 av1_read_tx_type(cm
, xd
, blk_row
, blk_col
, plane
, tx_size
, r
);
148 const TX_TYPE tx_type
= av1_get_tx_type(plane_type
, xd
, blk_row
, blk_col
,
149 tx_size
, cm
->reduced_tx_set_used
);
150 const TX_SIZE qm_tx_size
= av1_get_adjusted_tx_size(tx_size
);
151 const qm_val_t
*iqmatrix
=
152 IS_2D_TRANSFORM(tx_type
)
153 ? pd
->seg_iqmatrix
[mbmi
->segment_id
][qm_tx_size
]
154 : cm
->giqmatrix
[NUM_QM_LEVELS
- 1][0][qm_tx_size
];
155 const SCAN_ORDER
*const scan_order
= get_scan(tx_size
, tx_type
);
156 const int16_t *const scan
= scan_order
->scan
;
160 const int eob_multi_size
= txsize_log2_minus4
[tx_size
];
161 const int eob_multi_ctx
= (tx_type_to_class
[tx_type
] == TX_CLASS_2D
) ? 0 : 1;
162 switch (eob_multi_size
) {
165 aom_read_symbol(r
, ec_ctx
->eob_flag_cdf16
[plane_type
][eob_multi_ctx
],
171 aom_read_symbol(r
, ec_ctx
->eob_flag_cdf32
[plane_type
][eob_multi_ctx
],
177 aom_read_symbol(r
, ec_ctx
->eob_flag_cdf64
[plane_type
][eob_multi_ctx
],
183 aom_read_symbol(r
, ec_ctx
->eob_flag_cdf128
[plane_type
][eob_multi_ctx
],
189 aom_read_symbol(r
, ec_ctx
->eob_flag_cdf256
[plane_type
][eob_multi_ctx
],
195 aom_read_symbol(r
, ec_ctx
->eob_flag_cdf512
[plane_type
][eob_multi_ctx
],
201 eob_pt
= aom_read_symbol(
202 r
, ec_ctx
->eob_flag_cdf1024
[plane_type
][eob_multi_ctx
], 11,
208 if (k_eob_offset_bits
[eob_pt
] > 0) {
209 int bit
= aom_read_symbol(
210 r
, ec_ctx
->eob_extra_cdf
[txs_ctx
][plane_type
][eob_pt
], 2, ACCT_STR
);
212 eob_extra
+= (1 << (k_eob_offset_bits
[eob_pt
] - 1));
215 for (int i
= 1; i
< k_eob_offset_bits
[eob_pt
]; i
++) {
216 bit
= aom_read_bit(r
, ACCT_STR
);
218 eob_extra
+= (1 << (k_eob_offset_bits
[eob_pt
] - 1 - i
));
222 *eob
= rec_eob_pos(eob_pt
, eob_extra
);
225 // Read the non-zero coefficient with scan index eob-1
226 // TODO(angiebird): Put this into a function
227 const int c
= *eob
- 1;
228 const int pos
= scan
[c
];
229 const int coeff_ctx
= get_lower_levels_ctx_eob(bwl
, height
, c
);
230 const int nsymbs
= 3;
232 ec_ctx
->coeff_base_eob_cdf
[txs_ctx
][plane_type
][coeff_ctx
];
233 int level
= aom_read_symbol(r
, cdf
, nsymbs
, ACCT_STR
) + 1;
234 if (level
> NUM_BASE_LEVELS
) {
235 const int br_ctx
= get_br_ctx(levels
, pos
, bwl
, tx_type
);
236 for (int idx
= 0; idx
< COEFF_BASE_RANGE
; idx
+= BR_CDF_SIZE
- 1) {
237 const int k
= aom_read_symbol(
239 ec_ctx
->coeff_br_cdf
[AOMMIN(txs_ctx
, TX_32X32
)][plane_type
][br_ctx
],
240 BR_CDF_SIZE
, ACCT_STR
);
242 if (k
< BR_CDF_SIZE
- 1) break;
245 levels
[get_padded_idx(pos
, bwl
)] = level
;
248 base_cdf_arr base_cdf
= ec_ctx
->coeff_base_cdf
[txs_ctx
][plane_type
];
250 ec_ctx
->coeff_br_cdf
[AOMMIN(txs_ctx
, TX_32X32
)][plane_type
];
251 const TX_CLASS tx_class
= tx_type_to_class
[tx_type
];
252 if (tx_class
== TX_CLASS_2D
) {
253 read_coeffs_reverse_2d(r
, tx_size
, 1, *eob
- 1 - 1, scan
, bwl
, levels
,
255 read_coeffs_reverse(r
, tx_size
, tx_type
, 0, 0, scan
, bwl
, levels
,
258 read_coeffs_reverse(r
, tx_size
, tx_type
, 0, *eob
- 1 - 1, scan
, bwl
,
259 levels
, base_cdf
, br_cdf
);
263 for (int c
= 0; c
< *eob
; ++c
) {
264 const int pos
= scan
[c
];
266 tran_low_t level
= levels
[get_padded_idx(pos
, bwl
)];
268 *max_scan_line
= AOMMAX(*max_scan_line
, pos
);
270 const int dc_sign_ctx
= txb_ctx
->dc_sign_ctx
;
271 sign
= aom_read_symbol(r
, ec_ctx
->dc_sign_cdf
[plane_type
][dc_sign_ctx
],
274 sign
= aom_read_bit(r
, ACCT_STR
);
276 if (level
>= MAX_BASE_BR_RANGE
) {
277 level
+= read_golomb(xd
, r
);
279 // Bitmasking to clamp level to valid range:
280 // The valid range for 8/10/12 bit vdieo is at most 14/16/18 bit
284 // Bitmasking to clamp dq_coeff to valid range:
285 // The valid range for 8/10/12 bit video is at most 17/19/21 bit
286 dq_coeff
= level
* get_dqv(dequant
, scan
[c
], iqmatrix
) & 0xffffff;
287 dq_coeff
= dq_coeff
>> shift
;
289 dq_coeff
= -dq_coeff
;
291 tcoeffs
[pos
] = clamp(dq_coeff
, min_value
, max_value
);
295 cul_level
= AOMMIN(63, cul_level
);
298 set_dc_sign(&cul_level
, tcoeffs
[0]);
303 uint8_t av1_read_coeffs_txb_facade(const AV1_COMMON
*const cm
,
304 MACROBLOCKD
*const xd
, aom_reader
*const r
,
305 const int row
, const int col
,
306 const int plane
, const TX_SIZE tx_size
,
307 int16_t *const max_scan_line
,
309 MB_MODE_INFO
*const mbmi
= &xd
->mi
[0]->mbmi
;
310 struct macroblockd_plane
*const pd
= &xd
->plane
[plane
];
312 const BLOCK_SIZE bsize
= mbmi
->sb_type
;
313 const BLOCK_SIZE plane_bsize
= get_plane_block_size(bsize
, pd
);
316 get_txb_ctx(plane_bsize
, tx_size
, plane
, pd
->above_context
+ col
,
317 pd
->left_context
+ row
, &txb_ctx
);
318 uint8_t cul_level
= av1_read_coeffs_txb(cm
, xd
, r
, row
, col
, plane
, &txb_ctx
,
319 tx_size
, max_scan_line
, eob
);
320 av1_set_contexts(xd
, pd
, plane
, tx_size
, cul_level
, col
, row
);