godot/thirdparty/libwebp/src/enc/quant_enc.c

// Copyright 2011 Google Inc. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING 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.
// -----------------------------------------------------------------------------
//
//   Quantization
//
// Author: Skal ([email protected])

#include <assert.h>
#include <math.h>
#include <stdlib.h>  // for abs()

#include "src/dsp/quant.h"
#include "src/enc/vp8i_enc.h"
#include "src/enc/cost_enc.h"

#define DO_TRELLIS_I4
#define DO_TRELLIS_I16
#define DO_TRELLIS_UV
#define USE_TDISTO

#define MID_ALPHA
#define MIN_ALPHA
#define MAX_ALPHA

#define SNS_TO_DQ
                          // power-law modulation. Must be strictly less than 1.

// number of non-zero coeffs below which we consider the block very flat
// (and apply a penalty to complex predictions)
#define FLATNESS_LIMIT_I16
#define FLATNESS_LIMIT_I4
#define FLATNESS_LIMIT_UV
#define FLATNESS_PENALTY

#define MULT_8B(a, b)

#define RD_DISTO_MULT

// #define DEBUG_BLOCK

//------------------------------------------------------------------------------

#if defined(DEBUG_BLOCK)

#include <stdio.h>
#include <stdlib.h>

static void PrintBlockInfo(const VP8EncIterator* const it,
                           const VP8ModeScore* const rd) {
  int i, j;
  const int is_i16 = (it->mb_->type_ == 1);
  const uint8_t* const y_in = it->yuv_in_ + Y_OFF_ENC;
  const uint8_t* const y_out = it->yuv_out_ + Y_OFF_ENC;
  const uint8_t* const uv_in = it->yuv_in_ + U_OFF_ENC;
  const uint8_t* const uv_out = it->yuv_out_ + U_OFF_ENC;
  printf("SOURCE / OUTPUT / ABS DELTA\n");
  for (j = 0; j < 16; ++j) {
    for (i = 0; i < 16; ++i) printf("%3d ", y_in[i + j * BPS]);
    printf("     ");
    for (i = 0; i < 16; ++i) printf("%3d ", y_out[i + j * BPS]);
    printf("     ");
    for (i = 0; i < 16; ++i) {
      printf("%1d ", abs(y_in[i + j * BPS] - y_out[i + j * BPS]));
    }
    printf("\n");
  }
  printf("\n");   // newline before the U/V block
  for (j = 0; j < 8; ++j) {
    for (i = 0; i < 8; ++i) printf("%3d ", uv_in[i + j * BPS]);
    printf(" ");
    for (i = 8; i < 16; ++i) printf("%3d ", uv_in[i + j * BPS]);
    printf("    ");
    for (i = 0; i < 8; ++i) printf("%3d ", uv_out[i + j * BPS]);
    printf(" ");
    for (i = 8; i < 16; ++i) printf("%3d ", uv_out[i + j * BPS]);
    printf("   ");
    for (i = 0; i < 8; ++i) {
      printf("%1d ", abs(uv_out[i + j * BPS] - uv_in[i + j * BPS]));
    }
    printf(" ");
    for (i = 8; i < 16; ++i) {
      printf("%1d ", abs(uv_out[i + j * BPS] - uv_in[i + j * BPS]));
    }
    printf("\n");
  }
  printf("\nD:%d SD:%d R:%d H:%d nz:0x%x score:%d\n",
    (int)rd->D, (int)rd->SD, (int)rd->R, (int)rd->H, (int)rd->nz,
    (int)rd->score);
  if (is_i16) {
    printf("Mode: %d\n", rd->mode_i16);
    printf("y_dc_levels:");
    for (i = 0; i < 16; ++i) printf("%3d ", rd->y_dc_levels[i]);
    printf("\n");
  } else {
    printf("Modes[16]: ");
    for (i = 0; i < 16; ++i) printf("%d ", rd->modes_i4[i]);
    printf("\n");
  }
  printf("y_ac_levels:\n");
  for (j = 0; j < 16; ++j) {
    for (i = is_i16 ? 1 : 0; i < 16; ++i) {
      printf("%4d ", rd->y_ac_levels[j][i]);
    }
    printf("\n");
  }
  printf("\n");
  printf("uv_levels (mode=%d):\n", rd->mode_uv);
  for (j = 0; j < 8; ++j) {
    for (i = 0; i < 16; ++i) {
      printf("%4d ", rd->uv_levels[j][i]);
    }
    printf("\n");
  }
}

#endif   // DEBUG_BLOCK

//------------------------------------------------------------------------------

static WEBP_INLINE int clip(int v, int m, int M) {}

static const uint8_t kZigzag[16] =;

static const uint8_t kDcTable[128] =;

static const uint16_t kAcTable[128] =;

static const uint16_t kAcTable2[128] =;

static const uint8_t kBiasMatrices[3][2] =;

// Sharpening by (slightly) raising the hi-frequency coeffs.
// Hack-ish but helpful for mid-bitrate range. Use with care.
#define SHARPEN_BITS
static const uint8_t kFreqSharpening[16] =;

//------------------------------------------------------------------------------
// Initialize quantization parameters in VP8Matrix

// Returns the average quantizer
static int ExpandMatrix(VP8Matrix* const m, int type) {}

static void CheckLambdaValue(int* const v) {}

static void SetupMatrices(VP8Encoder* enc) {}

//------------------------------------------------------------------------------
// Initialize filtering parameters

// Very small filter-strength values have close to no visual effect. So we can
// save a little decoding-CPU by turning filtering off for these.
#define FSTRENGTH_CUTOFF

static void SetupFilterStrength(VP8Encoder* const enc) {}

//------------------------------------------------------------------------------

// Note: if you change the values below, remember that the max range
// allowed by the syntax for DQ_UV is [-16,16].
#define MAX_DQ_UV
#define MIN_DQ_UV

// We want to emulate jpeg-like behaviour where the expected "good" quality
// is around q=75. Internally, our "good" middle is around c=50. So we
// map accordingly using linear piece-wise function
static double QualityToCompression(double c) {}

static double QualityToJPEGCompression(double c, double alpha) {}

static int SegmentsAreEquivalent(const VP8SegmentInfo* const S1,
                                 const VP8SegmentInfo* const S2) {}

static void SimplifySegments(VP8Encoder* const enc) {}

void VP8SetSegmentParams(VP8Encoder* const enc, float quality) {}

//------------------------------------------------------------------------------
// Form the predictions in cache

// Must be ordered using {DC_PRED, TM_PRED, V_PRED, H_PRED} as index
const uint16_t VP8I16ModeOffsets[4] =;
const uint16_t VP8UVModeOffsets[4] =;

// Must be indexed using {B_DC_PRED -> B_HU_PRED} as index
const uint16_t VP8I4ModeOffsets[NUM_BMODES] =;

void VP8MakeLuma16Preds(const VP8EncIterator* const it) {}

void VP8MakeChroma8Preds(const VP8EncIterator* const it) {}

void VP8MakeIntra4Preds(const VP8EncIterator* const it) {}

//------------------------------------------------------------------------------
// Quantize

// Layout:
// +----+----+
// |YYYY|UUVV| 0
// |YYYY|UUVV| 4
// |YYYY|....| 8
// |YYYY|....| 12
// +----+----+

const uint16_t VP8Scan[16] =;

static const uint16_t VP8ScanUV[4 + 4] =;

//------------------------------------------------------------------------------
// Distortion measurement

static const uint16_t kWeightY[16] =;

static const uint16_t kWeightTrellis[16] =;

// Init/Copy the common fields in score.
static void InitScore(VP8ModeScore* const rd) {}

static void CopyScore(VP8ModeScore* WEBP_RESTRICT const dst,
                      const VP8ModeScore* WEBP_RESTRICT const src) {}

static void AddScore(VP8ModeScore* WEBP_RESTRICT const dst,
                     const VP8ModeScore* WEBP_RESTRICT const src) {}

//------------------------------------------------------------------------------
// Performs trellis-optimized quantization.

// -- GODOT start --
// Prevents Visual Studio debugger from using this Node struct in place of the Godot Node class.
#define Node
// -- GODOT end --

// Trellis node
Node_libwebp_quant;

// Score state
ScoreState;

// If a coefficient was quantized to a value Q (using a neutral bias),
// we test all alternate possibilities between [Q-MIN_DELTA, Q+MAX_DELTA]
// We don't test negative values though.
#define MIN_DELTA
#define MAX_DELTA
#define NUM_NODES
#define NODE
#define SCORE_STATE(n, l)

static WEBP_INLINE void SetRDScore(int lambda, VP8ModeScore* const rd) {}

static WEBP_INLINE score_t RDScoreTrellis(int lambda, score_t rate,
                                          score_t distortion) {}

// Coefficient type.
enum {};

static int TrellisQuantizeBlock(const VP8Encoder* WEBP_RESTRICT const enc,
                                int16_t in[16], int16_t out[16],
                                int ctx0, int coeff_type,
                                const VP8Matrix* WEBP_RESTRICT const mtx,
                                int lambda) {}

#undef NODE

//------------------------------------------------------------------------------
// Performs: difference, transform, quantize, back-transform, add
// all at once. Output is the reconstructed block in *yuv_out, and the
// quantized levels in *levels.

static int ReconstructIntra16(VP8EncIterator* WEBP_RESTRICT const it,
                              VP8ModeScore* WEBP_RESTRICT const rd,
                              uint8_t* WEBP_RESTRICT const yuv_out,
                              int mode) {}

static int ReconstructIntra4(VP8EncIterator* WEBP_RESTRICT const it,
                             int16_t levels[16],
                             const uint8_t* WEBP_RESTRICT const src,
                             uint8_t* WEBP_RESTRICT const yuv_out,
                             int mode) {}

//------------------------------------------------------------------------------
// DC-error diffusion

// Diffusion weights. We under-correct a bit (15/16th of the error is actually
// diffused) to avoid 'rainbow' chessboard pattern of blocks at q~=0.
#define C1
#define C2
#define DSHIFT
#define DSCALE

// Quantize as usual, but also compute and return the quantization error.
// Error is already divided by DSHIFT.
static int QuantizeSingle(int16_t* WEBP_RESTRICT const v,
                          const VP8Matrix* WEBP_RESTRICT const mtx) {}

static void CorrectDCValues(const VP8EncIterator* WEBP_RESTRICT const it,
                            const VP8Matrix* WEBP_RESTRICT const mtx,
                            int16_t tmp[][16],
                            VP8ModeScore* WEBP_RESTRICT const rd) {}

static void StoreDiffusionErrors(VP8EncIterator* WEBP_RESTRICT const it,
                                 const VP8ModeScore* WEBP_RESTRICT const rd) {}

#undef C1
#undef C2
#undef DSHIFT
#undef DSCALE

//------------------------------------------------------------------------------

static int ReconstructUV(VP8EncIterator* WEBP_RESTRICT const it,
                         VP8ModeScore* WEBP_RESTRICT const rd,
                         uint8_t* WEBP_RESTRICT const yuv_out, int mode) {}

//------------------------------------------------------------------------------
// RD-opt decision. Reconstruct each modes, evalue distortion and bit-cost.
// Pick the mode is lower RD-cost = Rate + lambda * Distortion.

static void StoreMaxDelta(VP8SegmentInfo* const dqm, const int16_t DCs[16]) {}

static void SwapModeScore(VP8ModeScore** a, VP8ModeScore** b) {}

static void SwapPtr(uint8_t** a, uint8_t** b) {}

static void SwapOut(VP8EncIterator* const it) {}

static void PickBestIntra16(VP8EncIterator* WEBP_RESTRICT const it,
                            VP8ModeScore* WEBP_RESTRICT rd) {}

//------------------------------------------------------------------------------

// return the cost array corresponding to the surrounding prediction modes.
static const uint16_t* GetCostModeI4(VP8EncIterator* WEBP_RESTRICT const it,
                                     const uint8_t modes[16]) {}

static int PickBestIntra4(VP8EncIterator* WEBP_RESTRICT const it,
                          VP8ModeScore* WEBP_RESTRICT const rd) {}

//------------------------------------------------------------------------------

static void PickBestUV(VP8EncIterator* WEBP_RESTRICT const it,
                       VP8ModeScore* WEBP_RESTRICT const rd) {}

//------------------------------------------------------------------------------
// Final reconstruction and quantization.

static void SimpleQuantize(VP8EncIterator* WEBP_RESTRICT const it,
                           VP8ModeScore* WEBP_RESTRICT const rd) {}

// Refine intra16/intra4 sub-modes based on distortion only (not rate).
static void RefineUsingDistortion(VP8EncIterator* WEBP_RESTRICT const it,
                                  int try_both_modes, int refine_uv_mode,
                                  VP8ModeScore* WEBP_RESTRICT const rd) {}

//------------------------------------------------------------------------------
// Entry point

int VP8Decimate(VP8EncIterator* WEBP_RESTRICT const it,
                VP8ModeScore* WEBP_RESTRICT const rd,
                VP8RDLevel rd_opt) {}