tokenize.h 4.72 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
 */

Yaowu Xu's avatar
Yaowu Xu committed
12 13
#ifndef AV1_ENCODER_TOKENIZE_H_
#define AV1_ENCODER_TOKENIZE_H_
Jingning Han's avatar
Jingning Han committed
14

15
#include "av1/common/entropy.h"
Jingning Han's avatar
Jingning Han committed
16

17 18
#include "av1/encoder/block.h"
#include "av1/encoder/treewriter.h"
Jingning Han's avatar
Jingning Han committed
19 20 21 22 23

#ifdef __cplusplus
extern "C" {
#endif

24
#define EOSB_TOKEN 127  // Not signalled, encoder only
Jingning Han's avatar
Jingning Han committed
25

26
#if CONFIG_HIGHBITDEPTH
27
typedef int32_t EXTRABIT;
Jingning Han's avatar
Jingning Han committed
28
#else
29
typedef int16_t EXTRABIT;
Jingning Han's avatar
Jingning Han committed
30 31 32 33 34 35 36 37
#endif

typedef struct {
  int16_t token;
  EXTRABIT extra;
} TOKENVALUE;

typedef struct {
38 39
  aom_cdf_prob (*tail_cdf)[CDF_SIZE(ENTROPY_TOKENS)];
  aom_cdf_prob (*head_cdf)[CDF_SIZE(ENTROPY_TOKENS)];
40
#if CONFIG_PALETTE
41
  aom_cdf_prob *palette_cdf;
42
#endif  // CONFIG_PALETTE
43
  int eob_val;
44
  int first_val;
45
  const aom_prob *context_tree;
Jingning Han's avatar
Jingning Han committed
46 47 48 49
  EXTRABIT extra;
  uint8_t token;
} TOKENEXTRA;

Yaowu Xu's avatar
Yaowu Xu committed
50 51
extern const aom_tree_index av1_coef_tree[];
extern const aom_tree_index av1_coef_con_tree[];
Jingning Han's avatar
Jingning Han committed
52

Yaowu Xu's avatar
Yaowu Xu committed
53
int av1_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane);
Jingning Han's avatar
Jingning Han committed
54

Yaowu Xu's avatar
Yaowu Xu committed
55
struct AV1_COMP;
Jingning Han's avatar
Jingning Han committed
56
struct ThreadData;
57

Jingning Han's avatar
Jingning Han committed
58 59 60 61 62 63 64
struct tokenize_b_args {
  const struct AV1_COMP *cpi;
  struct ThreadData *td;
  TOKENEXTRA **tp;
  int this_rate;
};

65 66 67 68 69 70 71 72 73
typedef enum {
  OUTPUT_ENABLED = 0,
  DRY_RUN_NORMAL,
  DRY_RUN_COSTCOEFFS,
} RUN_TYPE;

// Note in all the tokenize functions rate if non NULL is incremented
// with the coefficient token cost only if dry_run = DRY_RUN_COSTCOEFS,
// otherwise rate is not incremented.
74
#if CONFIG_VAR_TX
75
void av1_tokenize_sb_vartx(const struct AV1_COMP *cpi, struct ThreadData *td,
76 77
                           TOKENEXTRA **t, RUN_TYPE dry_run, int mi_row,
                           int mi_col, BLOCK_SIZE bsize, int *rate);
78
#endif
79
#if CONFIG_PALETTE
80
void av1_tokenize_palette_sb(const struct ThreadData *const td, int plane,
81 82
                             TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
                             int *rate);
83
#endif  // CONFIG_PALETTE
84
void av1_tokenize_sb(const struct AV1_COMP *cpi, struct ThreadData *td,
85
                     TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
86
                     int *rate, const int mi_row, const int mi_col);
87
#if CONFIG_SUPERTX
88
void av1_tokenize_sb_supertx(const struct AV1_COMP *cpi, struct ThreadData *td,
89 90
                             TOKENEXTRA **t, RUN_TYPE dry_run, int mi_row,
                             int mi_col, BLOCK_SIZE bsize, int *rate);
91
#endif
Jingning Han's avatar
Jingning Han committed
92

Yaowu Xu's avatar
Yaowu Xu committed
93
extern const int16_t *av1_dct_value_cost_ptr;
Jingning Han's avatar
Jingning Han committed
94 95 96 97
/* TODO: The Token field should be broken out into a separate char array to
 *  improve cache locality, since it's needed for costing when the rest of the
 *  fields are not.
 */
Yaowu Xu's avatar
Yaowu Xu committed
98 99 100 101
extern const TOKENVALUE *av1_dct_value_tokens_ptr;
extern const TOKENVALUE *av1_dct_cat_lt_10_value_tokens;
extern const int *av1_dct_cat_lt_10_value_cost;
extern const int16_t av1_cat6_low_cost[256];
102
#if CONFIG_HIGHBITDEPTH
103
#define CAT6_HIGH_COST_ENTRIES 1024
Jingning Han's avatar
Jingning Han committed
104
#else
105 106 107 108
#define CAT6_HIGH_COST_ENTRIES 64
#endif
extern const int av1_cat6_high_cost[CAT6_HIGH_COST_ENTRIES];
extern const uint8_t av1_cat6_skipped_bits_discount[8];
Jingning Han's avatar
Jingning Han committed
109

Yaowu Xu's avatar
Yaowu Xu committed
110
static INLINE void av1_get_token_extra(int v, int16_t *token, EXTRABIT *extra) {
Jingning Han's avatar
Jingning Han committed
111 112 113 114 115 116 117 118
  if (v >= CAT6_MIN_VAL || v <= -CAT6_MIN_VAL) {
    *token = CATEGORY6_TOKEN;
    if (v >= CAT6_MIN_VAL)
      *extra = 2 * v - 2 * CAT6_MIN_VAL;
    else
      *extra = -2 * v - 2 * CAT6_MIN_VAL + 1;
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
119 120
  *token = av1_dct_cat_lt_10_value_tokens[v].token;
  *extra = av1_dct_cat_lt_10_value_tokens[v].extra;
Jingning Han's avatar
Jingning Han committed
121
}
Yaowu Xu's avatar
Yaowu Xu committed
122
static INLINE int16_t av1_get_token(int v) {
123
  if (v >= CAT6_MIN_VAL || v <= -CAT6_MIN_VAL) return 10;
Yaowu Xu's avatar
Yaowu Xu committed
124
  return av1_dct_cat_lt_10_value_tokens[v].token;
Jingning Han's avatar
Jingning Han committed
125 126
}

127
static INLINE int av1_get_token_cost(int v, int16_t *token, int cat6_bits) {
Julia Robson's avatar
Julia Robson committed
128 129 130 131
  if (v >= CAT6_MIN_VAL || v <= -CAT6_MIN_VAL) {
    EXTRABIT extrabits;
    *token = CATEGORY6_TOKEN;
    extrabits = abs(v) - CAT6_MIN_VAL;
Yaowu Xu's avatar
Yaowu Xu committed
132
    return av1_cat6_low_cost[extrabits & 0xff] +
133 134
           av1_cat6_high_cost[extrabits >> 8] -
           av1_cat6_skipped_bits_discount[18 - cat6_bits];
Julia Robson's avatar
Julia Robson committed
135
  }
Yaowu Xu's avatar
Yaowu Xu committed
136 137
  *token = av1_dct_cat_lt_10_value_tokens[v].token;
  return av1_dct_cat_lt_10_value_cost[v];
Julia Robson's avatar
Julia Robson committed
138
}
Jingning Han's avatar
Jingning Han committed
139

hui su's avatar
hui su committed
140 141
static INLINE int av1_get_tx_eob(const struct segmentation *seg, int segment_id,
                                 TX_SIZE tx_size) {
Angie Chiang's avatar
Angie Chiang committed
142 143 144 145
  const int eob_max = tx_size_2d[tx_size];
  return segfeature_active(seg, segment_id, SEG_LVL_SKIP) ? 0 : eob_max;
}

Jingning Han's avatar
Jingning Han committed
146 147 148 149
#ifdef __cplusplus
}  // extern "C"
#endif

Yaowu Xu's avatar
Yaowu Xu committed
150
#endif  // AV1_ENCODER_TOKENIZE_H_