tokenize.h 5.29 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

Yaowu Xu's avatar
Yaowu Xu committed
26
#if CONFIG_AOM_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
#if CONFIG_NEW_TOKENSET
39
40
  aom_cdf_prob (*tail_cdf)[ENTROPY_TOKENS + CONFIG_EC_ADAPT];
  aom_cdf_prob (*head_cdf)[ENTROPY_TOKENS + CONFIG_EC_ADAPT];
41
  int is_eob;
42
#elif CONFIG_EC_MULTISYMBOL
43
  aom_cdf_prob (*token_cdf)[ENTROPY_TOKENS + CONFIG_EC_ADAPT];
44
#endif
45
  const aom_prob *context_tree;
Jingning Han's avatar
Jingning Han committed
46
47
48
49
50
  EXTRABIT extra;
  uint8_t token;
  uint8_t skip_eob_node;
} TOKENEXTRA;

Yaowu Xu's avatar
Yaowu Xu committed
51
52
extern const aom_tree_index av1_coef_tree[];
extern const aom_tree_index av1_coef_con_tree[];
53
#if !CONFIG_EC_MULTISYMBOL
Yaowu Xu's avatar
Yaowu Xu committed
54
extern const struct av1_token av1_coef_encodings[];
55
#endif  // !CONFIG_EC_MULTISYMBOL
Jingning Han's avatar
Jingning Han committed
56

Yaowu Xu's avatar
Yaowu Xu committed
57
58
int av1_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane);
int av1_has_high_freq_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane);
Jingning Han's avatar
Jingning Han committed
59

Yaowu Xu's avatar
Yaowu Xu committed
60
struct AV1_COMP;
Jingning Han's avatar
Jingning Han committed
61
struct ThreadData;
62

63
64
65
66
67
68
69
70
71
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.
72
#if CONFIG_VAR_TX
73
void av1_tokenize_sb_vartx(const struct AV1_COMP *cpi, struct ThreadData *td,
74
75
                           TOKENEXTRA **t, RUN_TYPE dry_run, int mi_row,
                           int mi_col, BLOCK_SIZE bsize, int *rate);
76
#endif
77
#if CONFIG_PALETTE
78
void av1_tokenize_palette_sb(const struct AV1_COMP *cpi,
79
                             const struct ThreadData *const td, int plane,
80
81
                             TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
                             int *rate);
82
#endif  // CONFIG_PALETTE
83
void av1_tokenize_sb(const struct AV1_COMP *cpi, struct ThreadData *td,
84
                     TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
85
                     int *rate, const int mi_row, const int mi_col);
86
#if CONFIG_SUPERTX
87
void av1_tokenize_sb_supertx(const struct AV1_COMP *cpi, struct ThreadData *td,
88
89
                             TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
                             int *rate);
90
#endif
Jingning Han's avatar
Jingning Han committed
91

Yaowu Xu's avatar
Yaowu Xu committed
92
extern const int16_t *av1_dct_value_cost_ptr;
Jingning Han's avatar
Jingning Han committed
93
94
95
96
/* 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
97
98
99
100
101
102
103
104
105
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];
extern const int av1_cat6_high_cost[64];
extern const int av1_cat6_high10_high_cost[256];
extern const int av1_cat6_high12_high_cost[1024];
static INLINE int av1_get_cost(int16_t token, EXTRABIT extrabits,
                               const int *cat6_high_table) {
Jingning Han's avatar
Jingning Han committed
106
  if (token != CATEGORY6_TOKEN)
Yaowu Xu's avatar
Yaowu Xu committed
107
108
    return av1_extra_bits[token].cost[extrabits >> 1];
  return av1_cat6_low_cost[(extrabits >> 1) & 0xff] +
109
         cat6_high_table[extrabits >> 9];
Jingning Han's avatar
Jingning Han committed
110
111
}

Yaowu Xu's avatar
Yaowu Xu committed
112
113
114
115
116
#if CONFIG_AOM_HIGHBITDEPTH
static INLINE const int *av1_get_high_cost_table(int bit_depth) {
  return bit_depth == 8 ? av1_cat6_high_cost
                        : (bit_depth == 10 ? av1_cat6_high10_high_cost
                                           : av1_cat6_high12_high_cost);
Jingning Han's avatar
Jingning Han committed
117
118
}
#else
Yaowu Xu's avatar
Yaowu Xu committed
119
static INLINE const int *av1_get_high_cost_table(int bit_depth) {
120
  (void)bit_depth;
Yaowu Xu's avatar
Yaowu Xu committed
121
  return av1_cat6_high_cost;
Jingning Han's avatar
Jingning Han committed
122
}
Yaowu Xu's avatar
Yaowu Xu committed
123
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
124

Yaowu Xu's avatar
Yaowu Xu committed
125
static INLINE void av1_get_token_extra(int v, int16_t *token, EXTRABIT *extra) {
Jingning Han's avatar
Jingning Han committed
126
127
128
129
130
131
132
133
  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
134
135
  *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
136
}
Yaowu Xu's avatar
Yaowu Xu committed
137
static INLINE int16_t av1_get_token(int v) {
138
  if (v >= CAT6_MIN_VAL || v <= -CAT6_MIN_VAL) return 10;
Yaowu Xu's avatar
Yaowu Xu committed
139
  return av1_dct_cat_lt_10_value_tokens[v].token;
Jingning Han's avatar
Jingning Han committed
140
141
}

Yaowu Xu's avatar
Yaowu Xu committed
142
143
static INLINE int av1_get_token_cost(int v, int16_t *token,
                                     const int *cat6_high_table) {
Julia Robson's avatar
Julia Robson committed
144
145
146
147
  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
148
    return av1_cat6_low_cost[extrabits & 0xff] +
149
           cat6_high_table[extrabits >> 8];
Julia Robson's avatar
Julia Robson committed
150
  }
Yaowu Xu's avatar
Yaowu Xu committed
151
152
  *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
153
}
Jingning Han's avatar
Jingning Han committed
154
155
156
157
158

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

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