summaryrefslogtreecommitdiffstats
path: root/third_party/aom/aom_dsp/x86/highbd_adaptive_quantize_avx2.c
blob: 05c87bcff947043c54e9b132d45747073d8dbd30 (plain)
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
/*
 * Copyright (c) 2019, 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 <immintrin.h>

#include "config/aom_dsp_rtcd.h"

#include "aom/aom_integer.h"
#include "aom_dsp/quantize.h"
#include "aom_dsp/x86/quantize_x86.h"

static INLINE void highbd_load_b_values_avx2(
    const int16_t *zbin_ptr, __m256i *zbin, const int16_t *round_ptr,
    __m256i *round, const int16_t *quant_ptr, __m256i *quant,
    const int16_t *dequant_ptr, __m256i *dequant, const int16_t *shift_ptr,
    __m256i *shift) {
  *zbin = _mm256_cvtepi16_epi32(_mm_load_si128((const __m128i *)zbin_ptr));
  *zbin = _mm256_sub_epi32(*zbin, _mm256_set1_epi32(1));
  *round = _mm256_cvtepi16_epi32(_mm_load_si128((const __m128i *)round_ptr));
  *quant = _mm256_cvtepi16_epi32(_mm_load_si128((const __m128i *)quant_ptr));
  *dequant =
      _mm256_cvtepi16_epi32(_mm_load_si128((const __m128i *)dequant_ptr));
  *shift = _mm256_cvtepi16_epi32(_mm_load_si128((const __m128i *)shift_ptr));
}

static INLINE void highbd_update_mask1_avx2(__m256i *cmp_mask,
                                            const int16_t *iscan_ptr,
                                            int *is_found, __m256i *mask) {
  __m256i temp_mask = _mm256_setzero_si256();
  if (_mm256_movemask_epi8(*cmp_mask)) {
    __m256i iscan = _mm256_loadu_si256((const __m256i *)(iscan_ptr));
    temp_mask = _mm256_and_si256(*cmp_mask, iscan);
    *is_found = 1;
  }
  *mask = _mm256_max_epi16(temp_mask, *mask);
}

static INLINE void highbd_update_mask0_avx2(__m256i *qcoeff0, __m256i *qcoeff1,
                                            __m256i *threshold,
                                            const int16_t *iscan_ptr,
                                            int *is_found, __m256i *mask) {
  __m256i coeff[2], cmp_mask0, cmp_mask1;
  coeff[0] = _mm256_slli_epi32(*qcoeff0, AOM_QM_BITS);
  cmp_mask0 = _mm256_cmpgt_epi32(coeff[0], threshold[0]);
  coeff[1] = _mm256_slli_epi32(*qcoeff1, AOM_QM_BITS);
  cmp_mask1 = _mm256_cmpgt_epi32(coeff[1], threshold[1]);
  cmp_mask0 =
      _mm256_permute4x64_epi64(_mm256_packs_epi32(cmp_mask0, cmp_mask1), 0xd8);
  highbd_update_mask1_avx2(&cmp_mask0, iscan_ptr, is_found, mask);
}

static INLINE void highbd_mul_shift_avx2(const __m256i *x, const __m256i *y,
                                         __m256i *p, const int shift) {
  __m256i prod_lo = _mm256_mul_epi32(*x, *y);
  __m256i prod_hi = _mm256_srli_epi64(*x, 32);
  const __m256i mult_hi = _mm256_srli_epi64(*y, 32);
  prod_hi = _mm256_mul_epi32(prod_hi, mult_hi);

  prod_lo = _mm256_srli_epi64(prod_lo, shift);
  prod_hi = _mm256_srli_epi64(prod_hi, shift);

  prod_hi = _mm256_slli_epi64(prod_hi, 32);
  *p = _mm256_blend_epi32(prod_lo, prod_hi, 0xaa);
}

static INLINE void highbd_calculate_qcoeff_avx2(__m256i *coeff,
                                                const __m256i *round,
                                                const __m256i *quant,
                                                const __m256i *shift,
                                                const int *log_scale) {
  __m256i tmp, qcoeff;
  qcoeff = _mm256_add_epi32(*coeff, *round);
  highbd_mul_shift_avx2(&qcoeff, quant, &tmp, 16);
  qcoeff = _mm256_add_epi32(tmp, qcoeff);
  highbd_mul_shift_avx2(&qcoeff, shift, coeff, 16 - *log_scale);
}

static INLINE __m256i highbd_calculate_dqcoeff_avx2(__m256i qcoeff,
                                                    __m256i dequant) {
  return _mm256_mullo_epi32(qcoeff, dequant);
}

static INLINE __m256i highbd_calculate_dqcoeff_log_scale_avx2(
    __m256i qcoeff, __m256i dequant, const int log_scale) {
  __m256i abs_coeff = _mm256_abs_epi32(qcoeff);
  highbd_mul_shift_avx2(&abs_coeff, &dequant, &abs_coeff, log_scale);
  return _mm256_sign_epi32(abs_coeff, qcoeff);
}

static INLINE void highbd_store_coefficients_avx2(__m256i coeff0,
                                                  __m256i coeff1,
                                                  tran_low_t *coeff_ptr) {
  _mm256_store_si256((__m256i *)(coeff_ptr), coeff0);
  _mm256_store_si256((__m256i *)(coeff_ptr + 8), coeff1);
}

void aom_highbd_quantize_b_adaptive_avx2(
    const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr,
    const int16_t *round_ptr, const int16_t *quant_ptr,
    const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
    tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
    const int16_t *scan, const int16_t *iscan) {
  int index = 16;
  int non_zero_count = 0;
  int non_zero_count_prescan_add_zero = 0;
  int is_found0 = 0, is_found1 = 0;
  int eob = -1;
  const __m256i zero = _mm256_setzero_si256();
  __m256i zbin, round, quant, dequant, shift;
  __m256i coeff0, qcoeff0, coeff1, qcoeff1;
  __m256i cmp_mask, mask0 = zero, mask1 = zero;
  __m128i temp_mask0, temp_mask1;
  int prescan_add[2];
  int thresh[2];
  const int log_scale = 0;
  const qm_val_t wt = (1 << AOM_QM_BITS);
  for (int i = 0; i < 2; ++i) {
    prescan_add[i] = ROUND_POWER_OF_TWO(dequant_ptr[i] * EOB_FACTOR, 7);
    thresh[i] = (zbin_ptr[i] * wt + prescan_add[i]) - 1;
  }
  __m256i threshold[2];
  threshold[0] = _mm256_set1_epi32(thresh[0]);
  threshold[1] = _mm256_set1_epi32(thresh[1]);
  threshold[0] = _mm256_blend_epi32(threshold[0], threshold[1], 0xfe);

#if SKIP_EOB_FACTOR_ADJUST
  int first = -1;
#endif

  // Setup global values.
  highbd_load_b_values_avx2(zbin_ptr, &zbin, round_ptr, &round, quant_ptr,
                            &quant, dequant_ptr, &dequant, quant_shift_ptr,
                            &shift);

  // Do DC and first 15 AC.
  coeff0 = _mm256_load_si256((__m256i *)(coeff_ptr));
  qcoeff0 = _mm256_abs_epi32(coeff0);
  coeff1 = _mm256_load_si256((__m256i *)(coeff_ptr + 8));
  qcoeff1 = _mm256_abs_epi32(coeff1);
  highbd_update_mask0_avx2(&qcoeff0, &qcoeff1, threshold, iscan, &is_found0,
                           &mask0);
  __m256i temp0 = _mm256_cmpgt_epi32(qcoeff0, zbin);
  zbin = _mm256_unpackhi_epi64(zbin, zbin);
  __m256i temp1 = _mm256_cmpgt_epi32(qcoeff1, zbin);
  cmp_mask = _mm256_permute4x64_epi64(_mm256_packs_epi32(temp0, temp1), 0xd8);
  highbd_update_mask1_avx2(&cmp_mask, iscan, &is_found1, &mask1);
  threshold[0] = threshold[1];
  if (_mm256_movemask_epi8(cmp_mask) == 0) {
    _mm256_store_si256((__m256i *)(qcoeff_ptr), zero);
    _mm256_store_si256((__m256i *)(qcoeff_ptr + 8), zero);
    _mm256_store_si256((__m256i *)(dqcoeff_ptr), zero);
    _mm256_store_si256((__m256i *)(dqcoeff_ptr + 8), zero);
    round = _mm256_unpackhi_epi64(round, round);
    quant = _mm256_unpackhi_epi64(quant, quant);
    shift = _mm256_unpackhi_epi64(shift, shift);
    dequant = _mm256_unpackhi_epi64(dequant, dequant);
  } else {
    highbd_calculate_qcoeff_avx2(&qcoeff0, &round, &quant, &shift, &log_scale);
    round = _mm256_unpackhi_epi64(round, round);
    quant = _mm256_unpackhi_epi64(quant, quant);
    shift = _mm256_unpackhi_epi64(shift, shift);
    highbd_calculate_qcoeff_avx2(&qcoeff1, &round, &quant, &shift, &log_scale);
    // Reinsert signs
    qcoeff0 = _mm256_sign_epi32(qcoeff0, coeff0);
    qcoeff1 = _mm256_sign_epi32(qcoeff1, coeff1);
    // Mask out zbin threshold coeffs
    qcoeff0 = _mm256_and_si256(qcoeff0, temp0);
    qcoeff1 = _mm256_and_si256(qcoeff1, temp1);
    highbd_store_coefficients_avx2(qcoeff0, qcoeff1, qcoeff_ptr);
    coeff0 = highbd_calculate_dqcoeff_avx2(qcoeff0, dequant);
    dequant = _mm256_unpackhi_epi64(dequant, dequant);
    coeff1 = highbd_calculate_dqcoeff_avx2(qcoeff1, dequant);
    highbd_store_coefficients_avx2(coeff0, coeff1, dqcoeff_ptr);
  }

  // AC only loop.
  while (index < n_coeffs) {
    coeff0 = _mm256_load_si256((__m256i *)(coeff_ptr + index));
    qcoeff0 = _mm256_abs_epi32(coeff0);
    coeff1 = _mm256_load_si256((__m256i *)(coeff_ptr + index + 8));
    qcoeff1 = _mm256_abs_epi32(coeff1);
    highbd_update_mask0_avx2(&qcoeff0, &qcoeff1, threshold, iscan + index,
                             &is_found0, &mask0);
    temp0 = _mm256_cmpgt_epi32(qcoeff0, zbin);
    temp1 = _mm256_cmpgt_epi32(qcoeff1, zbin);
    cmp_mask = _mm256_permute4x64_epi64(_mm256_packs_epi32(temp0, temp1), 0xd8);
    highbd_update_mask1_avx2(&cmp_mask, iscan + index, &is_found1, &mask1);
    if (_mm256_movemask_epi8(cmp_mask) == 0) {
      _mm256_store_si256((__m256i *)(qcoeff_ptr + index), zero);
      _mm256_store_si256((__m256i *)(qcoeff_ptr + index + 8), zero);
      _mm256_store_si256((__m256i *)(dqcoeff_ptr + index), zero);
      _mm256_store_si256((__m256i *)(dqcoeff_ptr + index + 8), zero);
      index += 16;
      continue;
    }
    highbd_calculate_qcoeff_avx2(&qcoeff0, &round, &quant, &shift, &log_scale);
    highbd_calculate_qcoeff_avx2(&qcoeff1, &round, &quant, &shift, &log_scale);
    qcoeff0 = _mm256_sign_epi32(qcoeff0, coeff0);
    qcoeff1 = _mm256_sign_epi32(qcoeff1, coeff1);
    qcoeff0 = _mm256_and_si256(qcoeff0, temp0);
    qcoeff1 = _mm256_and_si256(qcoeff1, temp1);
    highbd_store_coefficients_avx2(qcoeff0, qcoeff1, qcoeff_ptr + index);
    coeff0 = highbd_calculate_dqcoeff_avx2(qcoeff0, dequant);
    coeff1 = highbd_calculate_dqcoeff_avx2(qcoeff1, dequant);
    highbd_store_coefficients_avx2(coeff0, coeff1, dqcoeff_ptr + index);
    index += 16;
  }
  if (is_found0) {
    temp_mask0 = _mm_max_epi16(_mm256_castsi256_si128(mask0),
                               _mm256_extracti128_si256(mask0, 1));
    non_zero_count = calculate_non_zero_count(temp_mask0);
  }
  if (is_found1) {
    temp_mask1 = _mm_max_epi16(_mm256_castsi256_si128(mask1),
                               _mm256_extracti128_si256(mask1, 1));
    non_zero_count_prescan_add_zero = calculate_non_zero_count(temp_mask1);
  }

  for (int i = non_zero_count_prescan_add_zero - 1; i >= non_zero_count; i--) {
    const int rc = scan[i];
    qcoeff_ptr[rc] = 0;
    dqcoeff_ptr[rc] = 0;
  }

  for (int i = non_zero_count - 1; i >= 0; i--) {
    const int rc = scan[i];
    if (qcoeff_ptr[rc]) {
      eob = i;
      break;
    }
  }

  *eob_ptr = eob + 1;
#if SKIP_EOB_FACTOR_ADJUST
  // TODO(Aniket): Experiment the following loop with intrinsic by combining
  // with the quantization loop above
  for (int i = 0; i < non_zero_count; i++) {
    const int rc = scan[i];
    const int qcoeff = qcoeff_ptr[rc];
    if (qcoeff) {
      first = i;
      break;
    }
  }
  if ((*eob_ptr - 1) >= 0 && first == (*eob_ptr - 1)) {
    const int rc = scan[(*eob_ptr - 1)];
    if (qcoeff_ptr[rc] == 1 || qcoeff_ptr[rc] == -1) {
      const int coeff = coeff_ptr[rc] * wt;
      const int coeff_sign = AOMSIGN(coeff);
      const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
      const int factor = EOB_FACTOR + SKIP_EOB_FACTOR_ADJUST;
      const int prescan_add_val =
          ROUND_POWER_OF_TWO(dequant_ptr[rc != 0] * factor, 7);
      if (abs_coeff <
          (zbin_ptr[rc != 0] * (1 << AOM_QM_BITS) + prescan_add_val)) {
        qcoeff_ptr[rc] = 0;
        dqcoeff_ptr[rc] = 0;
        *eob_ptr = 0;
      }
    }
  }
#endif
}

void aom_highbd_quantize_b_32x32_adaptive_avx2(
    const tran_low_t *coeff_ptr, intptr_t n_coeffs, const int16_t *zbin_ptr,
    const int16_t *round_ptr, const int16_t *quant_ptr,
    const int16_t *quant_shift_ptr, tran_low_t *qcoeff_ptr,
    tran_low_t *dqcoeff_ptr, const int16_t *dequant_ptr, uint16_t *eob_ptr,
    const int16_t *scan, const int16_t *iscan) {
  int index = 16;
  int non_zero_count = 0;
  int non_zero_count_prescan_add_zero = 0;
  int is_found0 = 0, is_found1 = 0;
  int eob = -1;
  const int log_scale = 1;
  const __m256i zero = _mm256_setzero_si256();
  __m256i zbin, round, quant, dequant, shift;
  __m256i coeff0, qcoeff0, coeff1, qcoeff1;
  __m256i cmp_mask, mask0 = zero, mask1 = zero;
  __m128i temp_mask0, temp_mask1;
  const __m256i one = _mm256_set1_epi32(1);
  const __m256i log_scale_vec = _mm256_set1_epi32(log_scale);
  int prescan_add[2];
  int thresh[2];
  const int zbins[2] = { ROUND_POWER_OF_TWO(zbin_ptr[0], log_scale),
                         ROUND_POWER_OF_TWO(zbin_ptr[1], log_scale) };
  const qm_val_t wt = (1 << AOM_QM_BITS);
  for (int i = 0; i < 2; ++i) {
    prescan_add[i] = ROUND_POWER_OF_TWO(dequant_ptr[i] * EOB_FACTOR, 7);
    thresh[i] = (zbins[i] * wt + prescan_add[i]) - 1;
  }
  __m256i threshold[2];
  threshold[0] = _mm256_set1_epi32(thresh[0]);
  threshold[1] = _mm256_set1_epi32(thresh[1]);
  threshold[0] = _mm256_blend_epi32(threshold[0], threshold[1], 0xfe);

#if SKIP_EOB_FACTOR_ADJUST
  int first = -1;
#endif

  // Setup global values.
  zbin = _mm256_cvtepi16_epi32(_mm_load_si128((const __m128i *)zbin_ptr));
  round = _mm256_cvtepi16_epi32(_mm_load_si128((const __m128i *)round_ptr));
  quant = _mm256_cvtepi16_epi32(_mm_load_si128((const __m128i *)quant_ptr));
  dequant = _mm256_cvtepi16_epi32(_mm_load_si128((const __m128i *)dequant_ptr));
  shift =
      _mm256_cvtepi16_epi32(_mm_load_si128((const __m128i *)quant_shift_ptr));

  // Shift with rounding.
  zbin = _mm256_add_epi32(zbin, log_scale_vec);
  round = _mm256_add_epi32(round, log_scale_vec);
  zbin = _mm256_srli_epi32(zbin, log_scale);
  round = _mm256_srli_epi32(round, log_scale);
  zbin = _mm256_sub_epi32(zbin, one);

  // Do DC and first 15 AC.
  coeff0 = _mm256_load_si256((__m256i *)(coeff_ptr));
  qcoeff0 = _mm256_abs_epi32(coeff0);
  coeff1 = _mm256_load_si256((__m256i *)(coeff_ptr + 8));
  qcoeff1 = _mm256_abs_epi32(coeff1);
  highbd_update_mask0_avx2(&qcoeff0, &qcoeff1, threshold, iscan, &is_found0,
                           &mask0);
  __m256i temp0 = _mm256_cmpgt_epi32(qcoeff0, zbin);
  zbin = _mm256_permute2x128_si256(zbin, zbin, 0x11);
  __m256i temp1 = _mm256_cmpgt_epi32(qcoeff1, zbin);
  cmp_mask = _mm256_permute4x64_epi64(_mm256_packs_epi32(temp0, temp1), 0xd8);
  highbd_update_mask1_avx2(&cmp_mask, iscan, &is_found1, &mask1);
  threshold[0] = threshold[1];
  if (_mm256_movemask_epi8(cmp_mask) == 0) {
    _mm256_store_si256((__m256i *)(qcoeff_ptr), zero);
    _mm256_store_si256((__m256i *)(qcoeff_ptr + 8), zero);
    _mm256_store_si256((__m256i *)(dqcoeff_ptr), zero);
    _mm256_store_si256((__m256i *)(dqcoeff_ptr + 8), zero);
    round = _mm256_permute2x128_si256(round, round, 0x11);
    quant = _mm256_permute2x128_si256(quant, quant, 0x11);
    shift = _mm256_permute2x128_si256(shift, shift, 0x11);
    dequant = _mm256_permute2x128_si256(dequant, dequant, 0x11);
  } else {
    highbd_calculate_qcoeff_avx2(&qcoeff0, &round, &quant, &shift, &log_scale);
    round = _mm256_permute2x128_si256(round, round, 0x11);
    quant = _mm256_permute2x128_si256(quant, quant, 0x11);
    shift = _mm256_permute2x128_si256(shift, shift, 0x11);
    highbd_calculate_qcoeff_avx2(&qcoeff1, &round, &quant, &shift, &log_scale);
    // Reinsert signs
    qcoeff0 = _mm256_sign_epi32(qcoeff0, coeff0);
    qcoeff1 = _mm256_sign_epi32(qcoeff1, coeff1);
    // Mask out zbin threshold coeffs
    qcoeff0 = _mm256_and_si256(qcoeff0, temp0);
    qcoeff1 = _mm256_and_si256(qcoeff1, temp1);
    highbd_store_coefficients_avx2(qcoeff0, qcoeff1, qcoeff_ptr);
    coeff0 =
        highbd_calculate_dqcoeff_log_scale_avx2(qcoeff0, dequant, log_scale);
    dequant = _mm256_permute2x128_si256(dequant, dequant, 0x11);
    coeff1 =
        highbd_calculate_dqcoeff_log_scale_avx2(qcoeff1, dequant, log_scale);
    highbd_store_coefficients_avx2(coeff0, coeff1, dqcoeff_ptr);
  }

  // AC only loop.
  while (index < n_coeffs) {
    coeff0 = _mm256_load_si256((__m256i *)(coeff_ptr + index));
    qcoeff0 = _mm256_abs_epi32(coeff0);
    coeff1 = _mm256_load_si256((__m256i *)(coeff_ptr + index + 8));
    qcoeff1 = _mm256_abs_epi32(coeff1);
    highbd_update_mask0_avx2(&qcoeff0, &qcoeff1, threshold, iscan + index,
                             &is_found0, &mask0);
    temp0 = _mm256_cmpgt_epi32(qcoeff0, zbin);
    temp1 = _mm256_cmpgt_epi32(qcoeff1, zbin);
    cmp_mask = _mm256_permute4x64_epi64(_mm256_packs_epi32(temp0, temp1), 0xd8);
    highbd_update_mask1_avx2(&cmp_mask, iscan + index, &is_found1, &mask1);
    if (_mm256_movemask_epi8(cmp_mask) == 0) {
      _mm256_store_si256((__m256i *)(qcoeff_ptr + index), zero);
      _mm256_store_si256((__m256i *)(qcoeff_ptr + index + 8), zero);
      _mm256_store_si256((__m256i *)(dqcoeff_ptr + index), zero);
      _mm256_store_si256((__m256i *)(dqcoeff_ptr + index + 8), zero);
      index += 16;
      continue;
    }
    highbd_calculate_qcoeff_avx2(&qcoeff0, &round, &quant, &shift, &log_scale);
    highbd_calculate_qcoeff_avx2(&qcoeff1, &round, &quant, &shift, &log_scale);
    qcoeff0 = _mm256_sign_epi32(qcoeff0, coeff0);
    qcoeff1 = _mm256_sign_epi32(qcoeff1, coeff1);
    qcoeff0 = _mm256_and_si256(qcoeff0, temp0);
    qcoeff1 = _mm256_and_si256(qcoeff1, temp1);
    highbd_store_coefficients_avx2(qcoeff0, qcoeff1, qcoeff_ptr + index);
    coeff0 =
        highbd_calculate_dqcoeff_log_scale_avx2(qcoeff0, dequant, log_scale);
    coeff1 =
        highbd_calculate_dqcoeff_log_scale_avx2(qcoeff1, dequant, log_scale);
    highbd_store_coefficients_avx2(coeff0, coeff1, dqcoeff_ptr + index);
    index += 16;
  }
  if (is_found0) {
    temp_mask0 = _mm_max_epi16(_mm256_castsi256_si128(mask0),
                               _mm256_extracti128_si256(mask0, 1));
    non_zero_count = calculate_non_zero_count(temp_mask0);
  }
  if (is_found1) {
    temp_mask1 = _mm_max_epi16(_mm256_castsi256_si128(mask1),
                               _mm256_extracti128_si256(mask1, 1));
    non_zero_count_prescan_add_zero = calculate_non_zero_count(temp_mask1);
  }

  for (int i = non_zero_count_prescan_add_zero - 1; i >= non_zero_count; i--) {
    const int rc = scan[i];
    qcoeff_ptr[rc] = 0;
    dqcoeff_ptr[rc] = 0;
  }

  for (int i = non_zero_count - 1; i >= 0; i--) {
    const int rc = scan[i];
    if (qcoeff_ptr[rc]) {
      eob = i;
      break;
    }
  }

  *eob_ptr = eob + 1;
#if SKIP_EOB_FACTOR_ADJUST
  // TODO(Aniket): Experiment the following loop with intrinsic by combining
  // with the quantization loop above
  for (int i = 0; i < non_zero_count; i++) {
    const int rc = scan[i];
    const int qcoeff = qcoeff_ptr[rc];
    if (qcoeff) {
      first = i;
      break;
    }
  }
  if ((*eob_ptr - 1) >= 0 && first == (*eob_ptr - 1)) {
    const int rc = scan[(*eob_ptr - 1)];
    if (qcoeff_ptr[rc] == 1 || qcoeff_ptr[rc] == -1) {
      const int coeff = coeff_ptr[rc] * wt;
      const int coeff_sign = AOMSIGN(coeff);
      const int abs_coeff = (coeff ^ coeff_sign) - coeff_sign;
      const int factor = EOB_FACTOR + SKIP_EOB_FACTOR_ADJUST;
      const int prescan_add_val =
          ROUND_POWER_OF_TWO(dequant_ptr[rc != 0] * factor, 7);
      if (abs_coeff < (zbins[rc != 0] * (1 << AOM_QM_BITS) + prescan_add_val)) {
        qcoeff_ptr[rc] = 0;
        dqcoeff_ptr[rc] = 0;
        *eob_ptr = 0;
      }
    }
  }
#endif
}