chromium/third_party/libaom/source/libaom/av1/encoder/intra_mode_search_utils.h

/*
 * Copyright (c) 2020, 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.
 */

/*!\file
 * \brief Defines utility functions used in intra mode search.
 *
 * This includes rdcost estimations, histogram based pruning, etc.
 */
#ifndef AOM_AV1_ENCODER_INTRA_MODE_SEARCH_UTILS_H_
#define AOM_AV1_ENCODER_INTRA_MODE_SEARCH_UTILS_H_

#include "av1/common/enums.h"
#include "av1/common/pred_common.h"
#include "av1/common/reconintra.h"

#include "av1/encoder/encoder.h"
#include "av1/encoder/encodeframe.h"
#include "av1/encoder/model_rd.h"
#include "av1/encoder/palette.h"
#include "av1/encoder/hybrid_fwd_txfm.h"

#ifdef __cplusplus
extern "C" {
#endif

/*!\cond */
// Macro for computing the speed-preset dependent threshold which is used for
// deciding whether to enable/disable variance calculations in
// intra_rd_variance_factor().
#define INTRA_RD_VAR_THRESH(X)

#define BINS
static const float av1_intra_hog_model_bias[DIRECTIONAL_MODES] =;

static const float av1_intra_hog_model_weights[BINS * DIRECTIONAL_MODES] =;

static const NN_CONFIG av1_intra_hog_model_nnconfig =;

#define FIX_PREC_BITS
static inline int get_hist_bin_idx(int dx, int dy) {}
#undef FIX_PREC_BITS

// Normalizes the hog data.
static inline void normalize_hog(float total, float *hist) {}

static inline void lowbd_generate_hog(const uint8_t *src, int stride, int rows,
                                      int cols, float *hist) {}

// Computes and stores pixel level gradient information of a given superblock
// for LBD encode.
static inline void lowbd_compute_gradient_info_sb(MACROBLOCK *const x,
                                                  BLOCK_SIZE sb_size,
                                                  PLANE_TYPE plane) {}

#if CONFIG_AV1_HIGHBITDEPTH
static inline void highbd_generate_hog(const uint8_t *src8, int stride,
                                       int rows, int cols, float *hist) {
  float total = 0.1f;
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
  src += stride;
  for (int r = 1; r < rows - 1; ++r) {
    for (int c = 1; c < cols - 1; ++c) {
      const uint16_t *above = &src[c - stride];
      const uint16_t *below = &src[c + stride];
      const uint16_t *left = &src[c - 1];
      const uint16_t *right = &src[c + 1];
      // Calculate gradient using Sobel filters.
      const int dx = (right[-stride] + 2 * right[0] + right[stride]) -
                     (left[-stride] + 2 * left[0] + left[stride]);
      const int dy = (below[-1] + 2 * below[0] + below[1]) -
                     (above[-1] + 2 * above[0] + above[1]);
      if (dx == 0 && dy == 0) continue;
      const int temp = abs(dx) + abs(dy);
      if (!temp) continue;
      total += temp;
      if (dx == 0) {
        hist[0] += temp / 2;
        hist[BINS - 1] += temp / 2;
      } else {
        const int idx = get_hist_bin_idx(dx, dy);
        assert(idx >= 0 && idx < BINS);
        hist[idx] += temp;
      }
    }
    src += stride;
  }

  normalize_hog(total, hist);
}

// Computes and stores pixel level gradient information of a given superblock
// for HBD encode.
static inline void highbd_compute_gradient_info_sb(MACROBLOCK *const x,
                                                   BLOCK_SIZE sb_size,
                                                   PLANE_TYPE plane) {
  PixelLevelGradientInfo *const grad_info_sb =
      x->pixel_gradient_info + plane * MAX_SB_SQUARE;
  const uint16_t *src = CONVERT_TO_SHORTPTR(x->plane[plane].src.buf);
  const int stride = x->plane[plane].src.stride;
  const int ss_x = x->e_mbd.plane[plane].subsampling_x;
  const int ss_y = x->e_mbd.plane[plane].subsampling_y;
  const int sb_height = block_size_high[sb_size] >> ss_y;
  const int sb_width = block_size_wide[sb_size] >> ss_x;
  src += stride;
  for (int r = 1; r < sb_height - 1; ++r) {
    for (int c = 1; c < sb_width - 1; ++c) {
      const uint16_t *above = &src[c - stride];
      const uint16_t *below = &src[c + stride];
      const uint16_t *left = &src[c - 1];
      const uint16_t *right = &src[c + 1];
      // Calculate gradient using Sobel filters.
      const int dx = (right[-stride] + 2 * right[0] + right[stride]) -
                     (left[-stride] + 2 * left[0] + left[stride]);
      const int dy = (below[-1] + 2 * below[0] + below[1]) -
                     (above[-1] + 2 * above[0] + above[1]);
      grad_info_sb[r * sb_width + c].is_dx_zero = (dx == 0);
      grad_info_sb[r * sb_width + c].abs_dx_abs_dy_sum =
          (uint16_t)(abs(dx) + abs(dy));
      grad_info_sb[r * sb_width + c].hist_bin_idx =
          (dx != 0) ? get_hist_bin_idx(dx, dy) : -1;
    }
    src += stride;
  }
}
#endif  // CONFIG_AV1_HIGHBITDEPTH

static inline void generate_hog(const uint8_t *src8, int stride, int rows,
                                int cols, float *hist, int highbd) {}

static inline void compute_gradient_info_sb(MACROBLOCK *const x,
                                            BLOCK_SIZE sb_size,
                                            PLANE_TYPE plane) {}

// Gradient caching at superblock level is allowed only if all of the following
// conditions are satisfied:
// (1) The current frame is an intra only frame
// (2) Non-RD mode decisions are not enabled
// (3) The sf partition_search_type is set to SEARCH_PARTITION
// (4) Either intra_pruning_with_hog or chroma_intra_pruning_with_hog is enabled
//
// SB level caching of gradient data may not help in speedup for the following
// cases:
// (1) Inter frames (due to early intra gating)
// (2) When partition_search_type is not SEARCH_PARTITION
// Hence, gradient data is computed at block level in such cases.
static inline bool is_gradient_caching_for_hog_enabled(
    const AV1_COMP *const cpi) {}

// Function to generate pixel level gradient information for a given superblock.
// Sets the flags 'is_sb_gradient_cached' for the specific plane-type if
// gradient info is generated for the same.
static inline void produce_gradients_for_sb(AV1_COMP *cpi, MACROBLOCK *x,
                                            BLOCK_SIZE sb_size, int mi_row,
                                            int mi_col) {}

// Reuses the pixel level gradient data generated at superblock level for block
// level histogram computation.
static inline void generate_hog_using_gradient_cache(const MACROBLOCK *x,
                                                     int rows, int cols,
                                                     BLOCK_SIZE sb_size,
                                                     PLANE_TYPE plane,
                                                     float *hist) {}

static inline void collect_hog_data(const MACROBLOCK *x, BLOCK_SIZE bsize,
                                    BLOCK_SIZE sb_size, int plane, float *hog) {}

static inline void prune_intra_mode_with_hog(
    const MACROBLOCK *x, BLOCK_SIZE bsize, BLOCK_SIZE sb_size, float th,
    uint8_t *directional_mode_skip_mask, int is_chroma) {}
#undef BINS

int av1_calc_normalized_variance(aom_variance_fn_t vf, const uint8_t *const buf,
                                 const int stride, const int is_hbd);

// Returns whether caching of source variance for 4x4 sub-blocks is allowed.
static inline bool is_src_var_for_4x4_sub_blocks_caching_enabled(
    const AV1_COMP *const cpi) {}

// Initialize the members of Block4x4VarInfo structure to -1 at the start
// of every superblock.
static inline void init_src_var_info_of_4x4_sub_blocks(
    const AV1_COMP *const cpi, Block4x4VarInfo *src_var_info_of_4x4_sub_blocks,
    const BLOCK_SIZE sb_size) {}

// Returns the cost needed to send a uniformly distributed r.v.
static inline int write_uniform_cost(int n, int v) {}
/*!\endcond */

/*!\brief Returns the rate cost for luma prediction mode info of intra blocks.
 *
 * \callergraph
 */
static inline int intra_mode_info_cost_y(const AV1_COMP *cpi,
                                         const MACROBLOCK *x,
                                         const MB_MODE_INFO *mbmi,
                                         BLOCK_SIZE bsize, int mode_cost,
                                         int discount_color_cost) {}

/*!\brief Return the rate cost for chroma prediction mode info of intra blocks.
 *
 * \callergraph
 */
static inline int intra_mode_info_cost_uv(const AV1_COMP *cpi,
                                          const MACROBLOCK *x,
                                          const MB_MODE_INFO *mbmi,
                                          BLOCK_SIZE bsize, int mode_cost) {}

/*!\cond */
// Makes a quick intra prediction and estimate the rdcost with a model without
// going through the whole txfm/quantize/itxfm process.
static int64_t intra_model_rd(const AV1_COMMON *cm, MACROBLOCK *const x,
                              int plane, BLOCK_SIZE plane_bsize,
                              TX_SIZE tx_size, int use_hadamard) {}
/*!\endcond */

/*!\brief Estimate the luma rdcost of a given intra mode and try to prune it.
 *
 * \ingroup intra_mode_search
 * \callergraph
 * This function first makes a quick luma prediction and estimates the rdcost
 * with a model without going through the txfm, then try to prune the current
 * mode if the new estimate y_rd > 1.25 * best_model_rd.
 *
 * \return Returns 1 if the given mode is prune; 0 otherwise.
 */
static inline int model_intra_yrd_and_prune(const AV1_COMP *const cpi,
                                            MACROBLOCK *x, BLOCK_SIZE bsize,
                                            int64_t *best_model_rd) {}

#ifdef __cplusplus
}  // extern "C"
#endif

#endif  // AOM_AV1_ENCODER_INTRA_MODE_SEARCH_UTILS_H_