idct.c 99.1 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4 5 6 7 8 9
 * 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.
Jingning Han's avatar
Jingning Han committed
10 11 12 13
 */

#include <math.h>

Yaowu Xu's avatar
Yaowu Xu committed
14
#include "./aom_dsp_rtcd.h"
Geza Lore's avatar
Geza Lore committed
15 16 17
#include "./av1_rtcd.h"
#include "aom_dsp/inv_txfm.h"
#include "aom_ports/mem.h"
18
#include "av1/common/av1_inv_txfm1d_cfg.h"
19 20 21
#include "av1/common/blockd.h"
#include "av1/common/enums.h"
#include "av1/common/idct.h"
22 23 24 25
#if CONFIG_DAALA_DCT4 || CONFIG_DAALA_DCT8 || CONFIG_DAALA_DCT16 || \
    CONFIG_DAALA_DCT32 || CONFIG_DAALA_DCT64
#include "av1/common/daala_tx.h"
#endif
Jingning Han's avatar
Jingning Han committed
26

27
int av1_get_tx_scale(const TX_SIZE tx_size) {
28 29
  const int pels = tx_size_2d[tx_size];
  return (pels > 256) + (pels > 1024) + (pels > 4096);
30 31
}

32 33 34
// NOTE: The implementation of all inverses need to be aware of the fact
// that input and output could be the same buffer.

35
#if CONFIG_EXT_TX
Debargha Mukherjee's avatar
Debargha Mukherjee committed
36 37
static void iidtx4_c(const tran_low_t *input, tran_low_t *output) {
  int i;
38
  for (i = 0; i < 4; ++i) {
Debargha Mukherjee's avatar
Debargha Mukherjee committed
39
    output[i] = (tran_low_t)dct_const_round_shift(input[i] * Sqrt2);
40
  }
Debargha Mukherjee's avatar
Debargha Mukherjee committed
41 42 43 44
}

static void iidtx8_c(const tran_low_t *input, tran_low_t *output) {
  int i;
45 46 47
  for (i = 0; i < 8; ++i) {
    output[i] = input[i] * 2;
  }
Debargha Mukherjee's avatar
Debargha Mukherjee committed
48 49 50 51
}

static void iidtx16_c(const tran_low_t *input, tran_low_t *output) {
  int i;
52
  for (i = 0; i < 16; ++i) {
Debargha Mukherjee's avatar
Debargha Mukherjee committed
53
    output[i] = (tran_low_t)dct_const_round_shift(input[i] * 2 * Sqrt2);
54
  }
Debargha Mukherjee's avatar
Debargha Mukherjee committed
55 56 57 58
}

static void iidtx32_c(const tran_low_t *input, tran_low_t *output) {
  int i;
59 60 61
  for (i = 0; i < 32; ++i) {
    output[i] = input[i] * 4;
  }
Debargha Mukherjee's avatar
Debargha Mukherjee committed
62
}
63

64
#if CONFIG_TX64X64 && !CONFIG_DAALA_DCT64
65 66
static void iidtx64_c(const tran_low_t *input, tran_low_t *output) {
  int i;
67
  for (i = 0; i < 64; ++i) {
68
    output[i] = (tran_low_t)dct_const_round_shift(input[i] * 4 * Sqrt2);
69
  }
70 71
}
#endif  // CONFIG_TX64X64
72
#endif  // CONFIG_EXT_TX
Debargha Mukherjee's avatar
Debargha Mukherjee committed
73

74
// For use in lieu of ADST
75 76 77 78 79 80 81
static void ihalfright32_c(const tran_low_t *input, tran_low_t *output) {
  int i;
  tran_low_t inputhalf[16];
  // Multiply input by sqrt(2)
  for (i = 0; i < 16; ++i) {
    inputhalf[i] = (tran_low_t)dct_const_round_shift(input[i] * Sqrt2);
  }
82 83 84
  for (i = 0; i < 16; ++i) {
    output[i] = input[16 + i] * 4;
  }
Luca Barbato's avatar
Luca Barbato committed
85
  aom_idct16_c(inputhalf, output + 16);
86 87 88
  // Note overall scaling factor is 4 times orthogonal
}

89
#if CONFIG_TX64X64 && !CONFIG_DAALA_DCT64
90 91 92 93
static void idct64_col_c(const tran_low_t *input, tran_low_t *output) {
  int32_t in[64], out[64];
  int i;
  for (i = 0; i < 64; ++i) in[i] = (int32_t)input[i];
94
  av1_idct64_new(in, out, inv_cos_bit_col_dct_64, inv_stage_range_col_dct_64);
95 96 97 98 99 100 101
  for (i = 0; i < 64; ++i) output[i] = (tran_low_t)out[i];
}

static void idct64_row_c(const tran_low_t *input, tran_low_t *output) {
  int32_t in[64], out[64];
  int i;
  for (i = 0; i < 64; ++i) in[i] = (int32_t)input[i];
102
  av1_idct64_new(in, out, inv_cos_bit_row_dct_64, inv_stage_range_row_dct_64);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  for (i = 0; i < 64; ++i) output[i] = (tran_low_t)out[i];
}

// For use in lieu of ADST
static void ihalfright64_c(const tran_low_t *input, tran_low_t *output) {
  int i;
  tran_low_t inputhalf[32];
  // Multiply input by sqrt(2)
  for (i = 0; i < 32; ++i) {
    inputhalf[i] = (tran_low_t)dct_const_round_shift(input[i] * Sqrt2);
  }
  for (i = 0; i < 32; ++i) {
    output[i] = (tran_low_t)dct_const_round_shift(input[32 + i] * 4 * Sqrt2);
  }
  aom_idct32_c(inputhalf, output + 32);
  // Note overall scaling factor is 4 * sqrt(2)  times orthogonal
}
#endif  // CONFIG_TX64X64

Jingning Han's avatar
Jingning Han committed
122
// Inverse identity transform and add.
123
#if CONFIG_EXT_TX
124
static void inv_idtx_add_c(const tran_low_t *input, uint8_t *dest, int stride,
125
                           int bsx, int bsy, TX_TYPE tx_type) {
126
  int r, c;
127 128
  const int pels = bsx * bsy;
  const int shift = 3 - ((pels > 256) + (pels > 1024));
Debargha Mukherjee's avatar
Debargha Mukherjee committed
129
  if (tx_type == IDTX) {
130 131
    for (r = 0; r < bsy; ++r) {
      for (c = 0; c < bsx; ++c)
Debargha Mukherjee's avatar
Debargha Mukherjee committed
132 133
        dest[c] = clip_pixel_add(dest[c], input[c] >> shift);
      dest += stride;
134
      input += bsx;
Jingning Han's avatar
Jingning Han committed
135
    }
136 137
  }
}
138
#endif  // CONFIG_EXT_TX
139

clang-format's avatar
clang-format committed
140 141 142 143 144
#define FLIPUD_PTR(dest, stride, size)       \
  do {                                       \
    (dest) = (dest) + ((size)-1) * (stride); \
    (stride) = -(stride);                    \
  } while (0)
145

146
#if CONFIG_EXT_TX
clang-format's avatar
clang-format committed
147
static void maybe_flip_strides(uint8_t **dst, int *dstride, tran_low_t **src,
148
                               int *sstride, TX_TYPE tx_type, int sizey,
clang-format's avatar
clang-format committed
149
                               int sizex) {
150 151 152 153 154 155 156 157
  // Note that the transpose of src will be added to dst. In order to LR
  // flip the addends (in dst coordinates), we UD flip the src. To UD flip
  // the addends, we UD flip the dst.
  switch (tx_type) {
    case DCT_DCT:
    case ADST_DCT:
    case DCT_ADST:
    case ADST_ADST:
Debargha Mukherjee's avatar
Debargha Mukherjee committed
158
    case IDTX:
Jingning Han's avatar
Jingning Han committed
159 160
    case V_DCT:
    case H_DCT:
161
    case V_ADST:
clang-format's avatar
clang-format committed
162
    case H_ADST: break;
163 164
    case FLIPADST_DCT:
    case FLIPADST_ADST:
165
    case V_FLIPADST:
166
      // flip UD
167
      FLIPUD_PTR(*dst, *dstride, sizey);
168 169 170
      break;
    case DCT_FLIPADST:
    case ADST_FLIPADST:
171
    case H_FLIPADST:
172
      // flip LR
173
      FLIPUD_PTR(*src, *sstride, sizex);
174 175 176
      break;
    case FLIPADST_FLIPADST:
      // flip UD
177
      FLIPUD_PTR(*dst, *dstride, sizey);
178
      // flip LR
179
      FLIPUD_PTR(*src, *sstride, sizex);
180
      break;
clang-format's avatar
clang-format committed
181
    default: assert(0); break;
182 183
  }
}
184
#endif  // CONFIG_EXT_TX
185

186
#if CONFIG_HIGHBITDEPTH
187
#if CONFIG_EXT_TX && CONFIG_TX64X64
188
static void highbd_inv_idtx_add_c(const tran_low_t *input, uint8_t *dest8,
189
                                  int stride, int bsx, int bsy, TX_TYPE tx_type,
190
                                  int bd) {
191
  int r, c;
192 193
  const int pels = bsx * bsy;
  const int shift = 3 - ((pels > 256) + (pels > 1024));
194
  uint16_t *dest = CONVERT_TO_SHORTPTR(dest8);
195

Debargha Mukherjee's avatar
Debargha Mukherjee committed
196
  if (tx_type == IDTX) {
197 198
    for (r = 0; r < bsy; ++r) {
      for (c = 0; c < bsx; ++c)
Debargha Mukherjee's avatar
Debargha Mukherjee committed
199 200
        dest[c] = highbd_clip_pixel_add(dest[c], input[c] >> shift, bd);
      dest += stride;
201
      input += bsx;
202
    }
203 204
  }
}
205
#endif  // CONFIG_EXT_TX && CONFIG_TX64X64
206
#endif  // CONFIG_HIGHBITDEPTH
207

208
#if CONFIG_LGT || CONFIG_LGT_FROM_PRED
Lester Lu's avatar
Lester Lu committed
209 210
void ilgt4(const tran_low_t *input, tran_low_t *output,
           const tran_high_t *lgtmtx) {
Lester Lu's avatar
Lester Lu committed
211
  if (!lgtmtx) assert(0);
212 213 214 215 216 217 218 219 220 221 222
#if CONFIG_LGT_FROM_PRED
  // For DCT/ADST, use butterfly implementations
  if (lgtmtx[0] == DCT4) {
    aom_idct4_c(input, output);
    return;
  } else if (lgtmtx[0] == ADST4) {
    aom_iadst4_c(input, output);
    return;
  }
#endif  // CONFIG_LGT_FROM_PRED

Lester Lu's avatar
Lester Lu committed
223
  // evaluate s[j] = sum of all lgtmtx[j]*input[i] over i=1,...,4
Lester Lu's avatar
Lester Lu committed
224 225 226 227 228 229 230 231 232
  tran_high_t s[4] = { 0 };
  for (int i = 0; i < 4; ++i)
    for (int j = 0; j < 4; ++j) s[j] += lgtmtx[i * 4 + j] * input[i];

  for (int i = 0; i < 4; ++i) output[i] = WRAPLOW(dct_const_round_shift(s[i]));
}

void ilgt8(const tran_low_t *input, tran_low_t *output,
           const tran_high_t *lgtmtx) {
Lester Lu's avatar
Lester Lu committed
233
  if (!lgtmtx) assert(0);
234 235 236 237 238 239 240 241 242 243 244
#if CONFIG_LGT_FROM_PRED
  // For DCT/ADST, use butterfly implementations
  if (lgtmtx[0] == DCT8) {
    aom_idct8_c(input, output);
    return;
  } else if (lgtmtx[0] == ADST8) {
    aom_iadst8_c(input, output);
    return;
  }
#endif  // CONFIG_LGT_FROM_PRED

Lester Lu's avatar
Lester Lu committed
245
  // evaluate s[j] = sum of all lgtmtx[j]*input[i] over i=1,...,8
Lester Lu's avatar
Lester Lu committed
246 247 248 249 250 251
  tran_high_t s[8] = { 0 };
  for (int i = 0; i < 8; ++i)
    for (int j = 0; j < 8; ++j) s[j] += lgtmtx[i * 8 + j] * input[i];

  for (int i = 0; i < 8; ++i) output[i] = WRAPLOW(dct_const_round_shift(s[i]));
}
252
#endif  // CONFIG_LGT || CONFIG_LGT_FROM_PRED
Lester Lu's avatar
Lester Lu committed
253

254
#if CONFIG_LGT
Lester Lu's avatar
Lester Lu committed
255 256 257 258 259 260 261 262 263 264 265
// get_lgt4 and get_lgt8 return 1 and pick a lgt matrix if LGT is chosen to
// apply. Otherwise they return 0
int get_lgt4(const TxfmParam *txfm_param, int is_col,
             const tran_high_t **lgtmtx) {
  if (is_col && (vtx_tab[txfm_param->tx_type] == ADST_1D ||
                 vtx_tab[txfm_param->tx_type] == FLIPADST_1D)) {
    lgtmtx[0] = txfm_param->is_inter ? &lgt4_170[0][0] : &lgt4_140[0][0];
    return 1;
  } else if (!is_col && (htx_tab[txfm_param->tx_type] == ADST_1D ||
                         htx_tab[txfm_param->tx_type] == FLIPADST_1D)) {
    lgtmtx[0] = txfm_param->is_inter ? &lgt4_170[0][0] : &lgt4_140[0][0];
Lester Lu's avatar
Lester Lu committed
266 267
    return 1;
  }
Lester Lu's avatar
Lester Lu committed
268
  lgtmtx[0] = NULL;
Lester Lu's avatar
Lester Lu committed
269 270 271
  return 0;
}

Lester Lu's avatar
Lester Lu committed
272 273 274 275 276 277 278 279 280
int get_lgt8(const TxfmParam *txfm_param, int is_col,
             const tran_high_t **lgtmtx) {
  if (is_col && (vtx_tab[txfm_param->tx_type] == ADST_1D ||
                 vtx_tab[txfm_param->tx_type] == FLIPADST_1D)) {
    lgtmtx[0] = txfm_param->is_inter ? &lgt8_170[0][0] : &lgt8_150[0][0];
    return 1;
  } else if (!is_col && (htx_tab[txfm_param->tx_type] == ADST_1D ||
                         htx_tab[txfm_param->tx_type] == FLIPADST_1D)) {
    lgtmtx[0] = txfm_param->is_inter ? &lgt8_170[0][0] : &lgt8_150[0][0];
Lester Lu's avatar
Lester Lu committed
281 282
    return 1;
  }
Lester Lu's avatar
Lester Lu committed
283
  lgtmtx[0] = NULL;
Lester Lu's avatar
Lester Lu committed
284 285 286 287
  return 0;
}
#endif  // CONFIG_LGT

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
#if CONFIG_LGT_FROM_PRED
void ilgt16up(const tran_low_t *input, tran_low_t *output,
              const tran_high_t *lgtmtx) {
  if (lgtmtx[0] == DCT16) {
    aom_idct16_c(input, output);
    return;
  } else if (lgtmtx[0] == ADST16) {
    aom_iadst16_c(input, output);
    return;
  } else if (lgtmtx[0] == DCT32) {
    aom_idct32_c(input, output);
    return;
  } else if (lgtmtx[0] == ADST32) {
    ihalfright32_c(input, output);
    return;
  } else {
    assert(0);
  }
}

void get_discontinuity_1d(uint8_t *arr, int n, int *idx_max_diff) {
  *idx_max_diff = -1;

  int temp = 0, max_diff = 0, min_diff = INT_MAX;
  for (int i = 1; i < n; ++i) {
    temp = abs(arr[i] - arr[i - 1]);
    if (temp > max_diff) {
      max_diff = temp;
      *idx_max_diff = i;
    }
    if (temp < min_diff) min_diff = temp;
  }
}

void get_discontinuity_2d(uint8_t *dst, int stride, int n, int is_col,
                          int *idx_max_diff, int ntx) {
  *idx_max_diff = -1;

  int diff = 0, temp = 0, max_diff = 0, min_diff = INT_MAX;
  for (int i = 1; i < n; ++i) {
    temp = 0;
    for (int j = 0; j < ntx; ++j) {
      if (is_col)  // vertical diff
        diff = dst[i * stride + j] - dst[(i - 1) * stride + j];
      else  // horizontal diff
        diff = dst[j * stride + i] - dst[j * stride + i - 1];
      temp += diff * diff;
    }
    // temp/w is the i-th avg square diff
    if (temp > max_diff) {
      max_diff = temp;
      *idx_max_diff = i;
    }
    if (temp < min_diff) min_diff = temp;
  }
}

int idx_selfloop_wrt_mode(PREDICTION_MODE mode, int is_col) {
  // 0: no self-loop
  // 1: small self-loop
  // 2: medium self-loop
  // 3: large self-loop
  switch (mode) {
    case DC_PRED:
    case SMOOTH_PRED:
      // predition is good for both directions: large SLs for row and col
      return 3;
    case TM_PRED: return 0;
#if CONFIG_SMOOTH_HV
    case SMOOTH_H_PRED:
#endif
    case H_PRED:
      // prediction is good for H direction: large SL for row only
      return is_col ? 0 : 3;
#if CONFIG_SMOOTH_HV
    case SMOOTH_V_PRED:
#endif
    case V_PRED:
      // prediction is good for V direction: large SL for col only
      return is_col ? 3 : 0;
#if LGT_SL_INTRA
    // directional mode: choose SL based on the direction
    case D45_PRED: return is_col ? 2 : 0;
    case D63_PRED: return is_col ? 3 : 0;
    case D117_PRED: return is_col ? 3 : 1;
    case D135_PRED: return 2;
    case D153_PRED: return is_col ? 1 : 3;
    case D207_PRED: return is_col ? 0 : 3;
#else
    case D45_PRED:
    case D63_PRED:
    case D117_PRED: return is_col ? 3 : 0;
    case D135_PRED:
    case D153_PRED:
    case D207_PRED: return is_col ? 0 : 3;
#endif
    // inter: no SL
    default: return 0;
  }
}

void get_lgt4_from_pred(const TxfmParam *txfm_param, int is_col,
                        const tran_high_t **lgtmtx, int ntx) {
  PREDICTION_MODE mode = txfm_param->mode;
  int stride = txfm_param->stride;
  uint8_t *dst = txfm_param->dst;
  int bp = -1;
  uint8_t arr[4];

  // Each lgt4mtx_arr[k][i] corresponds to a line graph with a self-loop on
  // the first node, and possibly a weak edge within the line graph. i is
  // the index of the weak edge (between the i-th and (i+1)-th pixels, i=0
  // means no weak edge). k corresponds to the first self-loop's weight
  const tran_high_t *lgt4mtx_arr[4][4] = {
    { &lgt4_000[0][0], &lgt4_000w1[0][0], &lgt4_000w2[0][0],
      &lgt4_000w3[0][0] },
    { &lgt4_060[0][0], &lgt4_060_000w1[0][0], &lgt4_060_000w2[0][0],
      &lgt4_060_000w3[0][0] },
    { &lgt4_100[0][0], &lgt4_100_000w1[0][0], &lgt4_100_000w2[0][0],
      &lgt4_100_000w3[0][0] },
    { &lgt4_150[0][0], &lgt4_150_000w1[0][0], &lgt4_150_000w2[0][0],
      &lgt4_150_000w3[0][0] },
  };

  // initialize to DCT or some LGTs, and then change later if necessary
  int idx_sl = idx_selfloop_wrt_mode(mode, is_col);
  lgtmtx[0] = lgt4mtx_arr[idx_sl][0];

  // find the break point and replace the line graph by the one with a
  // break point
  if (mode == DC_PRED || mode == SMOOTH_PRED) {
    // Do not use break point, since 1) is_left_available and is_top_available
    // in DC_PRED are not known by txfm_param for now, so accessing
    // both boundaries anyway may cause a mismatch 2) DC prediciton
    // typically yields very smooth residues so having the break point
    // does not usually improve the RD result.
    return;
  } else if (mode == TM_PRED) {
    // TM_PRED: use both 1D top boundary and 1D left boundary
    if (is_col)
      for (int i = 0; i < 4; ++i) arr[i] = dst[i * stride];
    else
      for (int i = 0; i < 4; ++i) arr[i] = dst[i];
    get_discontinuity_1d(&arr[0], 4, &bp);
  } else if (mode == V_PRED) {
    // V_PRED: use 1D top boundary only
    if (is_col) return;
    for (int i = 0; i < 4; ++i) arr[i] = dst[i];
    get_discontinuity_1d(&arr[0], 4, &bp);
  } else if (mode == H_PRED) {
    // H_PRED: use 1D left boundary only
    if (!is_col) return;
    for (int i = 0; i < 4; ++i) arr[i] = dst[i * stride];
    get_discontinuity_1d(&arr[0], 4, &bp);
#if CONFIG_SMOOTH_HV
  } else if (mode == SMOOTH_V_PRED) {
    if (is_col) return;
    for (int i = 0; i < 4; ++i) arr[i] = dst[-stride + i];
    get_discontinuity_1d(&arr[0], 4, &bp);
  } else if (mode == SMOOTH_H_PRED) {
    if (!is_col) return;
    for (int i = 0; i < 4; ++i) arr[i] = dst[i * stride - 1];
    get_discontinuity_1d(&arr[0], 4, &bp);
#endif
  } else if (mode == D45_PRED || mode == D63_PRED || mode == D117_PRED) {
    // directional modes closer to vertical (maybe include D135 later)
    if (!is_col) get_discontinuity_2d(dst, stride, 4, 0, &bp, ntx);
  } else if (mode == D135_PRED || mode == D153_PRED || mode == D207_PRED) {
    // directional modes closer to horizontal
    if (is_col) get_discontinuity_2d(dst, stride, 4, 1, &bp, ntx);
  } else if (mode > TM_PRED) {
    // inter
    get_discontinuity_2d(dst, stride, 4, is_col, &bp, ntx);
  }

#if LGT_SL_INTRA
  if (bp != -1) lgtmtx[0] = lgt4mtx_arr[idx_sl][bp];
#else
  if (bp != -1) lgtmtx[0] = lgt4mtx_arr[0][bp];
#endif
}

void get_lgt8_from_pred(const TxfmParam *txfm_param, int is_col,
                        const tran_high_t **lgtmtx, int ntx) {
  PREDICTION_MODE mode = txfm_param->mode;
  int stride = txfm_param->stride;
  uint8_t *dst = txfm_param->dst;
  int bp = -1;
  uint8_t arr[8];

  const tran_high_t *lgt8mtx_arr[4][8] = {
    { &lgt8_000[0][0], &lgt8_000w1[0][0], &lgt8_000w2[0][0], &lgt8_000w3[0][0],
      &lgt8_000w4[0][0], &lgt8_000w5[0][0], &lgt8_000w6[0][0],
      &lgt8_000w7[0][0] },
    { &lgt8_060[0][0], &lgt8_060_000w1[0][0], &lgt8_060_000w2[0][0],
      &lgt8_060_000w3[0][0], &lgt8_060_000w4[0][0], &lgt8_060_000w5[0][0],
      &lgt8_060_000w6[0][0], &lgt8_060_000w7[0][0] },
    { &lgt8_100[0][0], &lgt8_100_000w1[0][0], &lgt8_100_000w2[0][0],
      &lgt8_100_000w3[0][0], &lgt8_100_000w4[0][0], &lgt8_100_000w5[0][0],
      &lgt8_100_000w6[0][0], &lgt8_100_000w7[0][0] },
    { &lgt8_150[0][0], &lgt8_150_000w1[0][0], &lgt8_150_000w2[0][0],
      &lgt8_150_000w3[0][0], &lgt8_150_000w4[0][0], &lgt8_150_000w5[0][0],
      &lgt8_150_000w6[0][0], &lgt8_150_000w7[0][0] },
  };

  int idx_sl = idx_selfloop_wrt_mode(mode, is_col);
  lgtmtx[0] = lgt8mtx_arr[idx_sl][0];

  if (mode == DC_PRED || mode == SMOOTH_PRED) {
    return;
  } else if (mode == TM_PRED) {
    if (is_col)
      for (int i = 0; i < 8; ++i) arr[i] = dst[i * stride];
    else
      for (int i = 0; i < 8; ++i) arr[i] = dst[i];
    get_discontinuity_1d(&arr[0], 8, &bp);
  } else if (mode == V_PRED) {
    if (is_col) return;
    for (int i = 0; i < 8; ++i) arr[i] = dst[i];
    get_discontinuity_1d(&arr[0], 8, &bp);
  } else if (mode == H_PRED) {
    if (!is_col) return;
    for (int i = 0; i < 8; ++i) arr[i] = dst[i * stride];
    get_discontinuity_1d(&arr[0], 8, &bp);
#if CONFIG_SMOOTH_HV
  } else if (mode == SMOOTH_V_PRED) {
    if (is_col) return;
    for (int i = 0; i < 8; ++i) arr[i] = dst[-stride + i];
    get_discontinuity_1d(&arr[0], 8, &bp);
  } else if (mode == SMOOTH_H_PRED) {
    if (!is_col) return;
    for (int i = 0; i < 8; ++i) arr[i] = dst[i * stride - 1];
    get_discontinuity_1d(&arr[0], 8, &bp);
#endif
  } else if (mode == D45_PRED || mode == D63_PRED || mode == D117_PRED) {
    if (!is_col) get_discontinuity_2d(dst, stride, 8, 0, &bp, ntx);
  } else if (mode == D135_PRED || mode == D153_PRED || mode == D207_PRED) {
    if (is_col) get_discontinuity_2d(dst, stride, 8, 1, &bp, ntx);
  } else if (mode > TM_PRED) {
    get_discontinuity_2d(dst, stride, 8, is_col, &bp, ntx);
  }

#if LGT_SL_INTRA
  if (bp != -1) lgtmtx[0] = lgt8mtx_arr[idx_sl][bp];
#else
  if (bp != -1) lgtmtx[0] = lgt8mtx_arr[0][bp];
#endif
}

// Since LGTs with length >8 are not implemented now, the following function
// will just call DCT or ADST
void get_lgt16up_from_pred(const TxfmParam *txfm_param, int is_col,
                           const tran_high_t **lgtmtx, int ntx) {
  int tx_length = is_col ? tx_size_high[txfm_param->tx_size]
                         : tx_size_wide[txfm_param->tx_size];
  assert(tx_length == 16 || tx_length == 32);
  PREDICTION_MODE mode = txfm_param->mode;

  (void)ntx;
  const tran_high_t *dctmtx =
      tx_length == 16 ? &lgt16_000[0][0] : &lgt32_000[0][0];
  const tran_high_t *adstmtx =
      tx_length == 16 ? &lgt16_200[0][0] : &lgt32_200[0][0];

  switch (mode) {
    case DC_PRED:
    case TM_PRED:
    case SMOOTH_PRED:
      // prediction from both top and left -> ADST
      lgtmtx[0] = adstmtx;
      break;
    case V_PRED:
    case D45_PRED:
    case D63_PRED:
    case D117_PRED:
#if CONFIG_SMOOTH_HV
    case SMOOTH_V_PRED:
#endif
      // prediction from the top more than from the left -> ADST
      lgtmtx[0] = is_col ? adstmtx : dctmtx;
      break;
    case H_PRED:
    case D135_PRED:
    case D153_PRED:
    case D207_PRED:
#if CONFIG_SMOOTH_HV
    case SMOOTH_H_PRED:
#endif
      // prediction from the left more than from the top -> DCT
      lgtmtx[0] = is_col ? dctmtx : adstmtx;
      break;
    default: lgtmtx[0] = dctmtx; break;
  }
}

typedef void (*IlgtFunc)(const tran_low_t *input, tran_low_t *output,
                         const tran_high_t *lgtmtx);

static IlgtFunc ilgt_func[4] = { ilgt4, ilgt8, ilgt16up, ilgt16up };

typedef void (*GetLgtFunc)(const TxfmParam *txfm_param, int is_col,
                           const tran_high_t **lgtmtx, int ntx);

static GetLgtFunc get_lgt_func[4] = { get_lgt4_from_pred, get_lgt8_from_pred,
                                      get_lgt16up_from_pred,
                                      get_lgt16up_from_pred };

// this inline function corresponds to the up scaling before the transpose
// operation in the av1_iht* functions
static INLINE tran_low_t inv_upscale_wrt_txsize(const tran_high_t val,
                                                const TX_SIZE tx_size) {
  switch (tx_size) {
    case TX_4X4:
    case TX_8X8:
    case TX_4X16:
    case TX_16X4:
    case TX_8X32:
    case TX_32X8: return (tran_low_t)val;
    case TX_4X8:
    case TX_8X4:
    case TX_8X16:
    case TX_16X8: return (tran_low_t)dct_const_round_shift(val * Sqrt2);
    default: assert(0); break;
  }
  return 0;
}

// This inline function corresponds to the bit shift before summing with the
// destination in the av1_iht* functions
static INLINE tran_low_t inv_downscale_wrt_txsize(const tran_low_t val,
                                                  const TX_SIZE tx_size) {
  switch (tx_size) {
    case TX_4X4: return ROUND_POWER_OF_TWO(val, 4);
    case TX_4X8:
    case TX_8X4:
    case TX_8X8:
    case TX_4X16:
    case TX_16X4: return ROUND_POWER_OF_TWO(val, 5);
    case TX_8X16:
    case TX_16X8:
    case TX_8X32:
    case TX_32X8: return ROUND_POWER_OF_TWO(val, 6);
    default: assert(0); break;
  }
  return 0;
}

void ilgt2d_from_pred_add(const tran_low_t *input, uint8_t *dest, int stride,
                          const TxfmParam *txfm_param) {
  const TX_SIZE tx_size = txfm_param->tx_size;
  const int w = tx_size_wide[tx_size];
  const int h = tx_size_high[tx_size];
  const int wlog2 = tx_size_wide_log2[tx_size];
  const int hlog2 = tx_size_high_log2[tx_size];
  assert(w <= 8 || h <= 8);

  int i, j;
  // largest 1D size allowed for LGT: 32
  // largest 2D size allowed for LGT: 8x32=256
  tran_low_t tmp[256], out[256], temp1d[32];
  const tran_high_t *lgtmtx_col[1];
  const tran_high_t *lgtmtx_row[1];
  get_lgt_func[hlog2 - 2](txfm_param, 1, lgtmtx_col, w);
  get_lgt_func[wlog2 - 2](txfm_param, 0, lgtmtx_row, h);

// for inverse transform, to be consistent with av1_iht functions, we always
// apply row transforms first and column transforms second, but both
// row-first and column-first versions are implemented here for future
// tests (use different lgtmtx_col[i], and choose row or column tx first
// depending on transforms).
#if 1
  // inverse column transforms
  for (i = 0; i < w; ++i) {
    // transpose
    for (j = 0; j < h; ++j) tmp[i * h + j] = input[j * w + i];
    ilgt_func[hlog2 - 2](&tmp[i * h], temp1d, lgtmtx_col[0]);
    // upscale, and store in place
    for (j = 0; j < h; ++j)
      tmp[i * h + j] = inv_upscale_wrt_txsize(temp1d[j], tx_size);
  }
  // inverse row transforms
  for (i = 0; i < h; ++i) {
    for (j = 0; j < w; ++j) temp1d[j] = tmp[j * h + i];
    ilgt_func[wlog2 - 2](temp1d, &out[i * w], lgtmtx_row[0]);
  }
  // downscale + sum with the destination
  for (i = 0; i < h; ++i) {
    for (j = 0; j < w; ++j) {
      int d = i * stride + j;
      int s = i * w + j;
      dest[d] =
          clip_pixel_add(dest[d], inv_downscale_wrt_txsize(out[s], tx_size));
    }
  }
#else
  // inverse row transforms
  for (i = 0; i < h; ++i) {
    ilgt_func[wlog2 - 2](input, temp1d, lgtmtx_row[0]);
    // upscale and transpose (tmp[j*h+i] <--> tmp[j][i])
    for (j = 0; j < w; ++j)
      tmp[j * h + i] = inv_upscale_wrt_txsize(temp1d[j], tx_size);
    input += w;
  }
  // inverse column transforms
  for (i = 0; i < w; ++i)
    ilgt_func[hlog2 - 2](&tmp[i * h], &out[i * h], lgtmtx_col[0]);
  // here, out[] is the transpose of 2D block of transform coefficients

  // downscale + transform + sum with dest
  for (i = 0; i < h; ++i) {
    for (j = 0; j < w; ++j) {
      int d = i * stride + j;
      int s = j * h + i;
      dest[d] =
          clip_pixel_add(dest[d], inv_downscale_wrt_txsize(out[s], tx_size));
    }
  }
#endif
}
#endif  // CONFIG_LGT_FROM_PRED

Yaowu Xu's avatar
Yaowu Xu committed
709
void av1_iht4x4_16_add_c(const tran_low_t *input, uint8_t *dest, int stride,
710
                         const TxfmParam *txfm_param) {
711
  const TX_TYPE tx_type = txfm_param->tx_type;
Sarah Parker's avatar
Sarah Parker committed
712 713 714
#if CONFIG_MRC_TX
  assert(tx_type != MRC_DCT && "Invalid tx type for tx size");
#endif  // CONFIG_MRC_TX
715
#if !CONFIG_DAALA_DCT4
716 717 718 719
  if (tx_type == DCT_DCT) {
    aom_idct4x4_16_add(input, dest, stride);
    return;
  }
720
#endif
721
  static const transform_2d IHT_4[] = {
722
#if CONFIG_DAALA_DCT4
723 724 725 726
    { daala_idct4, daala_idct4 },  // DCT_DCT  = 0
    { daala_idst4, daala_idct4 },  // ADST_DCT = 1
    { daala_idct4, daala_idst4 },  // DCT_ADST = 2
    { daala_idst4, daala_idst4 },  // ADST_ADST = 3
727
#if CONFIG_EXT_TX
728 729 730 731 732
    { daala_idst4, daala_idct4 },  // FLIPADST_DCT
    { daala_idct4, daala_idst4 },  // DCT_FLIPADST
    { daala_idst4, daala_idst4 },  // FLIPADST_FLIPADST
    { daala_idst4, daala_idst4 },  // ADST_FLIPADST
    { daala_idst4, daala_idst4 },  // FLIPADST_ADST
733 734 735 736 737 738 739
    { daala_idtx4, daala_idtx4 },  // IDTX
    { daala_idct4, daala_idtx4 },  // V_DCT
    { daala_idtx4, daala_idct4 },  // H_DCT
    { daala_idst4, daala_idtx4 },  // V_ADST
    { daala_idtx4, daala_idst4 },  // H_ADST
    { daala_idst4, daala_idtx4 },  // V_FLIPADST
    { daala_idtx4, daala_idst4 },  // H_FLIPADST
740 741
#endif
#else
Luca Barbato's avatar
Luca Barbato committed
742 743 744 745
    { aom_idct4_c, aom_idct4_c },    // DCT_DCT  = 0
    { aom_iadst4_c, aom_idct4_c },   // ADST_DCT = 1
    { aom_idct4_c, aom_iadst4_c },   // DCT_ADST = 2
    { aom_iadst4_c, aom_iadst4_c },  // ADST_ADST = 3
746
#if CONFIG_EXT_TX
Luca Barbato's avatar
Luca Barbato committed
747 748 749 750 751 752 753 754 755 756 757 758
    { aom_iadst4_c, aom_idct4_c },   // FLIPADST_DCT
    { aom_idct4_c, aom_iadst4_c },   // DCT_FLIPADST
    { aom_iadst4_c, aom_iadst4_c },  // FLIPADST_FLIPADST
    { aom_iadst4_c, aom_iadst4_c },  // ADST_FLIPADST
    { aom_iadst4_c, aom_iadst4_c },  // FLIPADST_ADST
    { iidtx4_c, iidtx4_c },          // IDTX
    { aom_idct4_c, iidtx4_c },       // V_DCT
    { iidtx4_c, aom_idct4_c },       // H_DCT
    { aom_iadst4_c, iidtx4_c },      // V_ADST
    { iidtx4_c, aom_iadst4_c },      // H_ADST
    { aom_iadst4_c, iidtx4_c },      // V_FLIPADST
    { iidtx4_c, aom_iadst4_c },      // H_FLIPADST
759
#endif
760
#endif
761 762 763
  };

  int i, j;
764
  tran_low_t tmp[4][4];
765 766 767
  tran_low_t out[4][4];
  tran_low_t *outp = &out[0][0];
  int outstride = 4;
768

769 770 771 772
#if CONFIG_DCT_ONLY
  assert(tx_type == DCT_DCT);
#endif

Lester Lu's avatar
Lester Lu committed
773
#if CONFIG_LGT
Lester Lu's avatar
Lester Lu committed
774 775 776 777
  const tran_high_t *lgtmtx_col[1];
  const tran_high_t *lgtmtx_row[1];
  int use_lgt_col = get_lgt4(txfm_param, 1, lgtmtx_col);
  int use_lgt_row = get_lgt4(txfm_param, 0, lgtmtx_row);
Lester Lu's avatar
Lester Lu committed
778 779
#endif

780 781
  // inverse transform row vectors
  for (i = 0; i < 4; ++i) {
782 783
#if CONFIG_DAALA_DCT4
    tran_low_t temp_in[4];
784
    for (j = 0; j < 4; j++) temp_in[j] = input[j] * 2;
785 786
    IHT_4[tx_type].rows(temp_in, out[i]);
#else
Lester Lu's avatar
Lester Lu committed
787 788
#if CONFIG_LGT
    if (use_lgt_row)
Lester Lu's avatar
Lester Lu committed
789
      ilgt4(input, out[i], lgtmtx_row[0]);
Lester Lu's avatar
Lester Lu committed
790 791 792
    else
#endif
      IHT_4[tx_type].rows(input, out[i]);
793
#endif
clang-format's avatar
clang-format committed
794
    input += 4;
795 796 797
  }

  // transpose
798 799 800
  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      tmp[j][i] = out[i][j];
801
    }
802 803 804 805
  }

  // inverse transform column vectors
  for (i = 0; i < 4; ++i) {
Lester Lu's avatar
Lester Lu committed
806 807
#if CONFIG_LGT
    if (use_lgt_col)
Lester Lu's avatar
Lester Lu committed
808
      ilgt4(tmp[i], out[i], lgtmtx_col[0]);
Lester Lu's avatar
Lester Lu committed
809 810 811
    else
#endif
      IHT_4[tx_type].cols(tmp[i], out[i]);
812 813 814
  }

#if CONFIG_EXT_TX
815
  maybe_flip_strides(&dest, &stride, &outp, &outstride, tx_type, 4, 4);
816 817 818 819
#endif

  // Sum with the destination
  for (i = 0; i < 4; ++i) {
820
    for (j = 0; j < 4; ++j) {
821 822
      int d = i * stride + j;
      int s = j * outstride + i;
823
#if CONFIG_DAALA_DCT4
824
      dest[d] = clip_pixel_add(dest[d], ROUND_POWER_OF_TWO(outp[s], 4));
825 826 827
#else
      dest[d] = clip_pixel_add(dest[d], ROUND_POWER_OF_TWO(outp[s], 4));
#endif
828 829 830 831
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
832
void av1_iht4x8_32_add_c(const tran_low_t *input, uint8_t *dest, int stride,
833
                         const TxfmParam *txfm_param) {
834
  const TX_TYPE tx_type = txfm_param->tx_type;
Sarah Parker's avatar
Sarah Parker committed
835 836 837
#if CONFIG_MRC_TX
  assert(tx_type != MRC_DCT && "Invalid tx type for tx size");
#endif  // CONFIG_MRC_TX
838 839 840
#if CONFIG_DCT_ONLY
  assert(tx_type == DCT_DCT);
#endif
841
  static const transform_2d IHT_4x8[] = {
Luca Barbato's avatar
Luca Barbato committed
842 843 844 845
    { aom_idct8_c, aom_idct4_c },    // DCT_DCT
    { aom_iadst8_c, aom_idct4_c },   // ADST_DCT
    { aom_idct8_c, aom_iadst4_c },   // DCT_ADST
    { aom_iadst8_c, aom_iadst4_c },  // ADST_ADST
846
#if CONFIG_EXT_TX
Luca Barbato's avatar
Luca Barbato committed
847 848 849 850 851 852 853 854 855 856 857 858
    { aom_iadst8_c, aom_idct4_c },   // FLIPADST_DCT
    { aom_idct8_c, aom_iadst4_c },   // DCT_FLIPADST
    { aom_iadst8_c, aom_iadst4_c },  // FLIPADST_FLIPADST
    { aom_iadst8_c, aom_iadst4_c },  // ADST_FLIPADST
    { aom_iadst8_c, aom_iadst4_c },  // FLIPADST_ADST
    { iidtx8_c, iidtx4_c },          // IDTX
    { aom_idct8_c, iidtx4_c },       // V_DCT
    { iidtx8_c, aom_idct4_c },       // H_DCT
    { aom_iadst8_c, iidtx4_c },      // V_ADST
    { iidtx8_c, aom_iadst4_c },      // H_ADST
    { aom_iadst8_c, iidtx4_c },      // V_FLIPADST
    { iidtx8_c, aom_iadst4_c },      // H_FLIPADST
859
#endif
860 861
  };

862 863
  const int n = 4;
  const int n2 = 8;
864
  int i, j;
865
  tran_low_t out[4][8], tmp[4][8], outtmp[4];
866
  tran_low_t *outp = &out[0][0];
867
  int outstride = n2;
868

Lester Lu's avatar
Lester Lu committed
869
#if CONFIG_LGT
Lester Lu's avatar
Lester Lu committed
870 871 872 873
  const tran_high_t *lgtmtx_col[1];
  const tran_high_t *lgtmtx_row[1];
  int use_lgt_col = get_lgt8(txfm_param, 1, lgtmtx_col);
  int use_lgt_row = get_lgt4(txfm_param, 0, lgtmtx_row);
Lester Lu's avatar
Lester Lu committed
874 875
#endif

876
  // inverse transform row vectors and transpose
877
  for (i = 0; i < n2; ++i) {
Lester Lu's avatar
Lester Lu committed
878 879
#if CONFIG_LGT
    if (use_lgt_row)
Lester Lu's avatar
Lester Lu committed
880
      ilgt4(input, outtmp, lgtmtx_row[0]);
Lester Lu's avatar
Lester Lu committed
881 882 883
    else
#endif
      IHT_4x8[tx_type].rows(input, outtmp);
884
    for (j = 0; j < n; ++j)
885
      tmp[j][i] = (tran_low_t)dct_const_round_shift(outtmp[j] * Sqrt2);
886
    input += n;
887 888 889
  }

  // inverse transform column vectors
890
  for (i = 0; i < n; ++i) {
Lester Lu's avatar
Lester Lu committed
891 892
#if CONFIG_LGT
    if (use_lgt_col)
Lester Lu's avatar
Lester Lu committed
893
      ilgt8(tmp[i], out[i], lgtmtx_col[0]);
Lester Lu's avatar
Lester Lu committed
894 895 896
    else
#endif
      IHT_4x8[tx_type].cols(tmp[i], out[i]);
897 898
  }

899
#if CONFIG_EXT_TX
900
  maybe_flip_strides(&dest, &stride, &outp, &outstride, tx_type, n2, n);
901
#endif
902 903

  // Sum with the destination
904 905
  for (i = 0; i < n2; ++i) {
    for (j = 0; j < n; ++j) {
906 907 908 909 910 911 912
      int d = i * stride + j;
      int s = j * outstride + i;
      dest[d] = clip_pixel_add(dest[d], ROUND_POWER_OF_TWO(outp[s], 5));
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
913
void av1_iht8x4_32_add_c(const tran_low_t *input, uint8_t *dest, int stride,
914
                         const TxfmParam *txfm_param) {
915
  const TX_TYPE tx_type = txfm_param->tx_type;
Sarah Parker's avatar
Sarah Parker committed
916 917 918
#if CONFIG_MRC_TX
  assert(tx_type != MRC_DCT && "Invalid tx type for tx size");
#endif  // CONFIG_MRC_TX
919 920 921
#if CONFIG_DCT_ONLY
  assert(tx_type == DCT_DCT);
#endif
922
  static const transform_2d IHT_8x4[] = {
Luca Barbato's avatar
Luca Barbato committed
923 924 925 926
    { aom_idct4_c, aom_idct8_c },    // DCT_DCT
    { aom_iadst4_c, aom_idct8_c },   // ADST_DCT
    { aom_idct4_c, aom_iadst8_c },   // DCT_ADST
    { aom_iadst4_c, aom_iadst8_c },  // ADST_ADST
927
#if CONFIG_EXT_TX
Luca Barbato's avatar
Luca Barbato committed
928 929 930 931 932 933 934 935 936 937 938 939
    { aom_iadst4_c, aom_idct8_c },   // FLIPADST_DCT
    { aom_idct4_c, aom_iadst8_c },   // DCT_FLIPADST
    { aom_iadst4_c, aom_iadst8_c },  // FLIPADST_FLIPADST
    { aom_iadst4_c, aom_iadst8_c },  // ADST_FLIPADST
    { aom_iadst4_c, aom_iadst8_c },  // FLIPADST_ADST
    { iidtx4_c, iidtx8_c },          // IDTX
    { aom_idct4_c, iidtx8_c },       // V_DCT
    { iidtx4_c, aom_idct8_c },       // H_DCT
    { aom_iadst4_c, iidtx8_c },      // V_ADST
    { iidtx4_c, aom_iadst8_c },      // H_ADST
    { aom_iadst4_c, iidtx8_c },      // V_FLIPADST
    { iidtx4_c, aom_iadst8_c },      // H_FLIPADST
940
#endif
941
  };
942

943 944
  const int n = 4;
  const int n2 = 8;
945 946

  int i, j;
947
  tran_low_t out[8][4], tmp[8][4], outtmp[8];
948
  tran_low_t *outp = &out[0][0];
949
  int outstride = n;
950

Lester Lu's avatar
Lester Lu committed
951
#if CONFIG_LGT
Lester Lu's avatar
Lester Lu committed
952 953 954 955
  const tran_high_t *lgtmtx_col[1];
  const tran_high_t *lgtmtx_row[1];
  int use_lgt_col = get_lgt4(txfm_param, 1, lgtmtx_col);
  int use_lgt_row = get_lgt8(txfm_param, 0, lgtmtx_row);
Lester Lu's avatar
Lester Lu committed
956 957
#endif

958
  // inverse transform row vectors and transpose
959
  for (i = 0; i < n; ++i) {
Lester Lu's avatar
Lester Lu committed
960 961
#if CONFIG_LGT
    if (use_lgt_row)
Lester Lu's avatar
Lester Lu committed
962
      ilgt8(input, outtmp, lgtmtx_row[0]);
Lester Lu's avatar
Lester Lu committed
963 964 965
    else
#endif
      IHT_8x4[tx_type].rows(input, outtmp);
966
    for (j = 0; j < n2; ++j)
967
      tmp[j][i] = (tran_low_t)dct_const_round_shift(outtmp[j] * Sqrt2);
968
    input += n2;
969 970 971
  }

  // inverse transform column vectors
972
  for (i = 0; i < n2; ++i) {
Lester Lu's avatar
Lester Lu committed
973 974
#if CONFIG_LGT
    if (use_lgt_col)
Lester Lu's avatar
Lester Lu committed
975
      ilgt4(tmp[i], out[i], lgtmtx_col[0]);
Lester Lu's avatar
Lester Lu committed
976 977 978
    else
#endif
      IHT_8x4[tx_type].cols(tmp[i], out[i]);
979 980
  }

981
#if CONFIG_EXT_TX
982
  maybe_flip_strides(&dest, &stride, &outp, &outstride, tx_type, n, n2);
983
#endif
984 985

  // Sum with the destination
986 987
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n2; ++j) {
988 989 990 991 992 993 994
      int d = i * stride + j;
      int s = j * outstride + i;
      dest[d] = clip_pixel_add(dest[d], ROUND_POWER_OF_TWO(outp[s], 5));
    }
  }
}

995
void av1_iht4x16_64_add_c(const tran_low_t *input, uint8_t *dest, int stride,
996
                          const TxfmParam *txfm_param) {
997
  const TX_TYPE tx_type = txfm_param->tx_type;
Sarah Parker's avatar
Sarah Parker committed
998 999 1000
#if CONFIG_MRC_TX
  assert(tx_type != MRC_DCT && "Invalid tx type for tx size");
#endif  // CONFIG_MRC_TX
1001 1002 1003
#if CONFIG_DCT_ONLY
  assert(tx_type == DCT_DCT);
#endif
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
  static const transform_2d IHT_4x16[] = {
    { aom_idct16_c, aom_idct4_c },    // DCT_DCT
    { aom_iadst16_c, aom_idct4_c },   // ADST_DCT
    { aom_idct16_c, aom_iadst4_c },   // DCT_ADST
    { aom_iadst16_c, aom_iadst4_c },  // ADST_ADST
#if CONFIG_EXT_TX
    { aom_iadst16_c, aom_idct4_c },   // FLIPADST_DCT
    { aom_idct16_c, aom_iadst4_c },   // DCT_FLIPADST
    { aom_iadst16_c, aom_iadst4_c },  // FLIPADST_FLIPADST
    { aom_iadst16_c, aom_iadst4_c },  // ADST_FLIPADST
    { aom_iadst16_c, aom_iadst4_c },  // FLIPADST_ADST
    { iidtx16_c, iidtx4_c },          // IDTX
    { aom_idct16_c, iidtx4_c },       // V_DCT
    { iidtx16_c, aom_idct4_c },       // H_DCT
    { aom_iadst16_c, iidtx4_c },      // V_ADST
    { iidtx16_c, aom_iadst4_c },      // H_ADST
    { aom_iadst16_c, iidtx4_c },      // V_FLIPADST
    { iidtx16_c, aom_iadst4_c },      // H_FLIPADST
#endif
  };

  const int n = 4;
  const int n4 = 16;
  int i, j;
1028
  tran_low_t out[4][16], tmp[4][16], outtmp[4];
1029 1030 1031
  tran_low_t *outp = &out[0][0];
  int outstride = n4;

Lester Lu's avatar
Lester Lu committed
1032
#if CONFIG_LGT
Lester Lu's avatar
Lester Lu committed
1033 1034
  const tran_high_t *lgtmtx_row[1];
  int use_lgt_row = get_lgt4(txfm_param, 0, lgtmtx_row);
Lester Lu's avatar
Lester Lu committed
1035 1036
#endif

1037 1038
  // inverse transform row vectors and transpose
  for (i = 0; i < n4; ++i) {
Lester Lu's avatar
Lester Lu committed
1039 1040
#if CONFIG_LGT
    if (use_lgt_row)
Lester Lu's avatar
Lester Lu committed
1041
      ilgt4(input, outtmp, lgtmtx_row[0]);
Lester Lu's avatar
Lester Lu committed
1042 1043 1044
    else
#endif
      IHT_4x16[tx_type].rows(input, outtmp);
1045
    for (j = 0; j < n; ++j) tmp[j][i] = outtmp[j];
1046 1047 1048 1049
    input += n;
  }

  // inverse transform column vectors
Lester Lu's avatar
Lester Lu committed
1050 1051 1052
  for (i = 0; i < n; ++i) {
    IHT_4x16[tx_type].cols(tmp[i], out[i]);
  }
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

#if CONFIG_EXT_TX
  maybe_flip_strides(&dest, &stride, &outp, &outstride, tx_type, n4, n);
#endif

  // Sum with the destination
  for (i = 0; i < n4; ++i) {
    for (j = 0; j < n; ++j) {
      int d = i * stride + j;
      int s = j * outstride + i;
      dest[d] = clip_pixel_add(dest[d], ROUND_POWER_OF_TWO(outp[s], 5));
    }
  }
}

void av1_iht16x4_64_add_c(const tran_low_t *input, uint8_t *dest, int stride,
1069
                          const TxfmParam *txfm_param) {
1070
  const TX_TYPE tx_type = txfm_param->tx_type;
Sarah Parker's avatar
Sarah Parker committed
1071 1072 1073
#if CONFIG_MRC_TX
  assert(tx_type != MRC_DCT && "Invalid tx type for tx size");
#endif  // CONFIG_MRC_TX
1074 1075 1076
#if CONFIG_DCT_ONLY
  assert(tx_type == DCT_DCT);
#endif
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
  static const transform_2d IHT_16x4[] = {
    { aom_idct4_c, aom_idct16_c },    // DCT_DCT
    { aom_iadst4_c, aom_idct16_c },   // ADST_DCT
    { aom_idct4_c, aom_iadst16_c },   // DCT_ADST
    { aom_iadst4_c, aom_iadst16_c },  // ADST_ADST
#if CONFIG_EXT_TX
    { aom_iadst4_c, aom_idct16_c },   // FLIPADST_DCT
    { aom_idct4_c, aom_iadst16_c },   // DCT_FLIPADST
    { aom_iadst4_c, aom_iadst16_c },  // FLIPADST_FLIPADST
    { aom_iadst4_c, aom_iadst16_c },  // ADST_FLIPADST
    { aom_iadst4_c, aom_iadst16_c },  // FLIPADST_ADST
    { iidtx4_c, iidtx16_c },          // IDTX
    { aom_idct4_c, iidtx16_c },       // V_DCT
    { iidtx4_c, aom_idct16_c },       // H_DCT
    { aom_iadst4_c, iidtx16_c },      // V_ADST
    { iidtx4_c, aom_iadst16_c },      // H_ADST
    { aom_iadst4_c, iidtx16_c },      // V_FLIPADST
    { iidtx4_c, aom_iadst16_c },      // H_FLIPADST
#endif
  };