<commit_before>/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <assert.h>
#include <math.h>
#include <limits.h>

#include "vp9/common/vp9_alloccommon.h"
#include "vp9/common/vp9_common.h"
#include "vp9/common/vp9_onyxc_int.h"
#include "vp9/common/vp9_quant_common.h"
#include "vp9/common/vp9_reconinter.h"
#include "vp9/encoder/vp9_encodeframe.h"
#include "vp9/encoder/vp9_ethread.h"
#include "vp9/encoder/vp9_extend.h"
#include "vp9/encoder/vp9_firstpass.h"
#include "vp9/encoder/vp9_mcomp.h"
#include "vp9/encoder/vp9_encoder.h"
#include "vp9/encoder/vp9_quantize.h"
#include "vp9/encoder/vp9_ratectrl.h"
#include "vp9/encoder/vp9_segmentation.h"
#include "vp9/encoder/vp9_temporal_filter.h"
#include "vpx_dsp/vpx_dsp_common.h"
#include "vpx_mem/vpx_mem.h"
#include "vpx_ports/mem.h"
#include "vpx_ports/vpx_timer.h"
#include "vpx_scale/vpx_scale.h"

static int fixed_divide[512];

static void temporal_filter_predictors_mb_c(
    MACROBLOCKD *xd, uint8_t *y_mb_ptr, uint8_t *u_mb_ptr, uint8_t *v_mb_ptr,
    int stride, int uv_block_width, int uv_block_height, int mv_row, int mv_col,
    uint8_t *pred, struct scale_factors *scale, int x, int y, MV *blk_mvs,
    int use_32x32) {
  const int which_mv = 0;
  const InterpKernel *const kernel = vp9_filter_kernels[EIGHTTAP_SHARP];
  int i, j, k = 0, ys = (BH >> 1), xs = (BW >> 1);

  enum mv_precision mv_precision_uv;
  int uv_stride;
  if (uv_block_width == (BW >> 1)) {
    uv_stride = (stride + 1) >> 1;
    mv_precision_uv = MV_PRECISION_Q4;
  } else {
    uv_stride = stride;
    mv_precision_uv = MV_PRECISION_Q3;
  }
#if !CONFIG_VP9_HIGHBITDEPTH
  (void)xd;
#endif

  if (use_32x32) {
    const MV mv = { mv_row, mv_col };
#if CONFIG_VP9_HIGHBITDEPTH
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      vp9_highbd_build_inter_predictor(CONVERT_TO_SHORTPTR(y_mb_ptr), stride,
                                       CONVERT_TO_SHORTPTR(&pred[0]), BW, &mv,
                                       scale, BW, BH, which_mv, kernel,
                                       MV_PRECISION_Q3, x, y, xd->bd);

      vp9_highbd_build_inter_predictor(
          CONVERT_TO_SHORTPTR(u_mb_ptr), uv_stride,
          CONVERT_TO_SHORTPTR(&pred[BLK_PELS]), uv_block_width, &mv, scale,
          uv_block_width, uv_block_height, which_mv, kernel, mv_precision_uv, x,
          y, xd->bd);

      vp9_highbd_build_inter_predictor(
          CONVERT_TO_SHORTPTR(v_mb_ptr), uv_stride,
          CONVERT_TO_SHORTPTR(&pred[(BLK_PELS << 1)]), uv_block_width, &mv,
          scale, uv_block_width, uv_block_height, which_mv, kernel,
          mv_precision_uv, x, y, xd->bd);
      return;
    }
#endif  // CONFIG_VP9_HIGHBITDEPTH
    vp9_build_inter_predictor(y_mb_ptr, stride, &pred[0], BW, &mv, scale, BW,
                              BH, which_mv, kernel, MV_PRECISION_Q3, x, y);

    vp9_build_inter_predictor(u_mb_ptr, uv_stride, &pred[BLK_PELS],
                              uv_block_width, &mv, scale, uv_block_width,
                              uv_block_height, which_mv, kernel,
                              mv_precision_uv, x, y);

    vp9_build_inter_predictor(v_mb_ptr, uv_stride, &pred[(BLK_PELS << 1)],
                              uv_block_width, &mv, scale, uv_block_width,
                              uv_block_height, which_mv, kernel,
                              mv_precision_uv, x, y);
    return;
  }

  // While use_32x32 = 0, construct the 32x32 predictor using 4 16x16
  // predictors.
  // Y predictor
  for (i = 0; i < BH; i += ys) {
    for (j = 0; j < BW; j += xs) {
      const MV mv = blk_mvs[k];
      const int y_offset = i * stride + j;
      const int p_offset = i * BW + j;

#if CONFIG_VP9_HIGHBITDEPTH
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        vp9_highbd_build_inter_predictor(
            CONVERT_TO_SHORTPTR(y_mb_ptr + y_offset), stride,
            CONVERT_TO_SHORTPTR(&pred[p_offset]), BW, &mv, scale, xs, ys,
            which_mv, kernel, MV_PRECISION_Q3, x, y, xd->bd);
      }
#endif  // CONFIG_VP9_HIGHBITDEPTH
      vp9_build_inter_predictor(y_mb_ptr + y_offset, stride, &pred[p_offset],
                                BW, &mv, scale, xs, ys, which_mv, kernel,
                                MV_PRECISION_Q3, x, y);
      k++;
    }
  }

  // U and V predictors
  ys = (uv_block_height >> 1);
  xs = (uv_block_width >> 1);
  k = 0;

  for (i = 0; i < uv_block_height; i += ys) {
    for (j = 0; j < uv_block_width; j += xs) {
      const MV mv = blk_mvs[k];
      const int uv_offset = i * uv_stride + j;
      const int p_offset = i * uv_block_width + j;

#if CONFIG_VP9_HIGHBITDEPTH
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        vp9_highbd_build_inter_predictor(
            CONVERT_TO_SHORTPTR(u_mb_ptr + uv_offset), uv_stride,
            CONVERT_TO_SHORTPTR(&pred[BLK_PELS + p_offset]), uv_block_width,
            &mv, scale, xs, ys, which_mv, kernel, mv_precision_uv, x, y,
            xd->bd);

        vp9_highbd_build_inter_predictor(
            CONVERT_TO_SHORTPTR(v_mb_ptr + uv_offset), uv_stride,
            CONVERT_TO_SHORTPTR(&pred[(BLK_PELS << 1) + p_offset]),
            uv_block_width, &mv, scale, xs, ys, which_mv, kernel,
            mv_precision_uv, x, y, xd->bd);
      }
#endif  // CONFIG_VP9_HIGHBITDEPTH
      vp9_build_inter_predictor(u_mb_ptr + uv_offset, uv_stride,
                                &pred[BLK_PELS + p_offset], uv_block_width, &mv,
                                scale, xs, ys, which_mv, kernel,
                                mv_precision_uv, x, y);

      vp9_build_inter_predictor(v_mb_ptr + uv_offset, uv_stride,
                                &pred[(BLK_PELS << 1) + p_offset],
                                uv_block_width, &mv, scale, xs, ys, which_mv,
                                kernel, mv_precision_uv, x, y);
      k++;
    }
  }
}

void vp9_temporal_filter_init(void) {
  int i;

  fixed_divide[0] = 0;
  for (i = 1; i < 512; ++i) fixed_divide[i] = 0x80000 / i;
}

static INLINE int mod_index(int sum_dist, int index, int rounding, int strength,
                            int filter_weight) {
  int mod = (sum_dist * 3) / index;
  mod += rounding;
  mod >>= strength;

  mod = VPXMIN(16, mod);

  mod = 16 - mod;
  mod *= filter_weight;

  return mod;
}

static INLINE int get_filter_weight(unsigned int i, unsigned int j,
                                    unsigned int block_height,
                                    unsigned int block_width, int *blk_fw,<commit_msg>Fix segmentation fault in hbd path<commit_after>/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <assert.h>
#include <math.h>
#include <limits.h>

#include "vp9/common/vp9_alloccommon.h"
#include "vp9/common/vp9_common.h"
#include "vp9/common/vp9_onyxc_int.h"
#include "vp9/common/vp9_quant_common.h"
#include "vp9/common/vp9_reconinter.h"
#include "vp9/encoder/vp9_encodeframe.h"
#include "vp9/encoder/vp9_ethread.h"
#include "vp9/encoder/vp9_extend.h"
#include "vp9/encoder/vp9_firstpass.h"
#include "vp9/encoder/vp9_mcomp.h"
#include "vp9/encoder/vp9_encoder.h"
#include "vp9/encoder/vp9_quantize.h"
#include "vp9/encoder/vp9_ratectrl.h"
#include "vp9/encoder/vp9_segmentation.h"
#include "vp9/encoder/vp9_temporal_filter.h"
#include "vpx_dsp/vpx_dsp_common.h"
#include "vpx_mem/vpx_mem.h"
#include "vpx_ports/mem.h"
#include "vpx_ports/vpx_timer.h"
#include "vpx_scale/vpx_scale.h"

static int fixed_divide[512];

static void temporal_filter_predictors_mb_c(
    MACROBLOCKD *xd, uint8_t *y_mb_ptr, uint8_t *u_mb_ptr, uint8_t *v_mb_ptr,
    int stride, int uv_block_width, int uv_block_height, int mv_row, int mv_col,
    uint8_t *pred, struct scale_factors *scale, int x, int y, MV *blk_mvs,
    int use_32x32) {
  const int which_mv = 0;
  const InterpKernel *const kernel = vp9_filter_kernels[EIGHTTAP_SHARP];
  int i, j, k = 0, ys = (BH >> 1), xs = (BW >> 1);

  enum mv_precision mv_precision_uv;
  int uv_stride;
  if (uv_block_width == (BW >> 1)) {
    uv_stride = (stride + 1) >> 1;
    mv_precision_uv = MV_PRECISION_Q4;
  } else {
    uv_stride = stride;
    mv_precision_uv = MV_PRECISION_Q3;
  }
#if !CONFIG_VP9_HIGHBITDEPTH
  (void)xd;
#endif

  if (use_32x32) {
    const MV mv = { mv_row, mv_col };
#if CONFIG_VP9_HIGHBITDEPTH
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      vp9_highbd_build_inter_predictor(CONVERT_TO_SHORTPTR(y_mb_ptr), stride,
                                       CONVERT_TO_SHORTPTR(&pred[0]), BW, &mv,
                                       scale, BW, BH, which_mv, kernel,
                                       MV_PRECISION_Q3, x, y, xd->bd);

      vp9_highbd_build_inter_predictor(
          CONVERT_TO_SHORTPTR(u_mb_ptr), uv_stride,
          CONVERT_TO_SHORTPTR(&pred[BLK_PELS]), uv_block_width, &mv, scale,
          uv_block_width, uv_block_height, which_mv, kernel, mv_precision_uv, x,
          y, xd->bd);

      vp9_highbd_build_inter_predictor(
          CONVERT_TO_SHORTPTR(v_mb_ptr), uv_stride,
          CONVERT_TO_SHORTPTR(&pred[(BLK_PELS << 1)]), uv_block_width, &mv,
          scale, uv_block_width, uv_block_height, which_mv, kernel,
          mv_precision_uv, x, y, xd->bd);
      return;
    }
#endif  // CONFIG_VP9_HIGHBITDEPTH
    vp9_build_inter_predictor(y_mb_ptr, stride, &pred[0], BW, &mv, scale, BW,
                              BH, which_mv, kernel, MV_PRECISION_Q3, x, y);

    vp9_build_inter_predictor(u_mb_ptr, uv_stride, &pred[BLK_PELS],
                              uv_block_width, &mv, scale, uv_block_width,
                              uv_block_height, which_mv, kernel,
                              mv_precision_uv, x, y);

    vp9_build_inter_predictor(v_mb_ptr, uv_stride, &pred[(BLK_PELS << 1)],
                              uv_block_width, &mv, scale, uv_block_width,
                              uv_block_height, which_mv, kernel,
                              mv_precision_uv, x, y);
    return;
  }

  // While use_32x32 = 0, construct the 32x32 predictor using 4 16x16
  // predictors.
  // Y predictor
  for (i = 0; i < BH; i += ys) {
    for (j = 0; j < BW; j += xs) {
      const MV mv = blk_mvs[k];
      const int y_offset = i * stride + j;
      const int p_offset = i * BW + j;

#if CONFIG_VP9_HIGHBITDEPTH
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        vp9_highbd_build_inter_predictor(
            CONVERT_TO_SHORTPTR(y_mb_ptr + y_offset), stride,
            CONVERT_TO_SHORTPTR(&pred[p_offset]), BW, &mv, scale, xs, ys,
            which_mv, kernel, MV_PRECISION_Q3, x, y, xd->bd);
      } else {
        vp9_build_inter_predictor(y_mb_ptr + y_offset, stride, &pred[p_offset],
                                  BW, &mv, scale, xs, ys, which_mv, kernel,
                                  MV_PRECISION_Q3, x, y);
      }
#else
      vp9_build_inter_predictor(y_mb_ptr + y_offset, stride, &pred[p_offset],
                                BW, &mv, scale, xs, ys, which_mv, kernel,
                                MV_PRECISION_Q3, x, y);
#endif  // CONFIG_VP9_HIGHBITDEPTH
      k++;
    }
  }

  // U and V predictors
  ys = (uv_block_height >> 1);
  xs = (uv_block_width >> 1);
  k = 0;

  for (i = 0; i < uv_block_height; i += ys) {
    for (j = 0; j < uv_block_width; j += xs) {
      const MV mv = blk_mvs[k];
      const int uv_offset = i * uv_stride + j;
      const int p_offset = i * uv_block_width + j;

#if CONFIG_VP9_HIGHBITDEPTH
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        vp9_highbd_build_inter_predictor(
            CONVERT_TO_SHORTPTR(u_mb_ptr + uv_offset), uv_stride,
            CONVERT_TO_SHORTPTR(&pred[BLK_PELS + p_offset]), uv_block_width,
            &mv, scale, xs, ys, which_mv, kernel, mv_precision_uv, x, y,
            xd->bd);

        vp9_highbd_build_inter_predictor(
            CONVERT_TO_SHORTPTR(v_mb_ptr + uv_offset), uv_stride,
            CONVERT_TO_SHORTPTR(&pred[(BLK_PELS << 1) + p_offset]),
            uv_block_width, &mv, scale, xs, ys, which_mv, kernel,
            mv_precision_uv, x, y, xd->bd);
      } else {
        vp9_build_inter_predictor(u_mb_ptr + uv_offset, uv_stride,
                                  &pred[BLK_PELS + p_offset], uv_block_width,
                                  &mv, scale, xs, ys, which_mv, kernel,
                                  mv_precision_uv, x, y);

        vp9_build_inter_predictor(v_mb_ptr + uv_offset, uv_stride,
                                  &pred[(BLK_PELS << 1) + p_offset],
                                  uv_block_width, &mv, scale, xs, ys, which_mv,
                                  kernel, mv_precision_uv, x, y);
      }
#else
      vp9_build_inter_predictor(u_mb_ptr + uv_offset, uv_stride,
                                &pred[BLK_PELS + p_offset], uv_block_width, &mv,
                                scale, xs, ys, which_mv, kernel,
                                mv_precision_uv, x, y);

      vp9_build_inter_predictor(v_mb_ptr + uv_offset, uv_stride,
                                &pred[(BLK_PELS << 1) + p_offset],
                                uv_block_width, &mv, scale, xs, ys, which_mv,
                                kernel, mv_precision_uv, x, y);
#endif  // CONFIG_VP9_HIGHBITDEPTH
      k++;
    }
  }
}

void vp9_temporal_filter_init(void) {
  int i;

  fixed_divide[0] = 0;
  for (i = 1; i < 512; ++i) fixed_divide[i] = 0x80000 / i;
}

static INLINE int mod_index(int sum_dist, int index, int rounding, int strength,
                            int filter_weight) {
  int mod = (sum_dist * 3) / index;
  mod += rounding;
  mod >>= strength;

  mod = VPXMIN(16, mod);

  mod = 16 - mod;
  mod *= filter_weight;

  return mod;
}

static INLINE int get_filter_weight(unsigned int i, unsigned int j,
                                    unsigned int block_height,
                                    unsigned int block_width, int *blk_fw,
{'fraction_non_alphanumeric': 0.1061679158357973, 'fraction_numerical': 0.018634394554081002, 'mean_word_length': 2.3419117647058822, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4208342', 'n_tokens_mistral': 5768, 'n_tokens_neox': 5300, 'n_words': 1167}