tokenize.h 4.5 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 {
Yaowu Xu's avatar
Yaowu Xu committed
38
  const aom_prob *context_tree;
39
#if CONFIG_ANS
40
  const rans_lut *token_cdf;
41
#endif  // CONFIG_ANS
Jingning Han's avatar
Jingning Han committed
42
43
44
45
46
  EXTRABIT extra;
  uint8_t token;
  uint8_t skip_eob_node;
} TOKENEXTRA;

Yaowu Xu's avatar
Yaowu Xu committed
47
48
extern const aom_tree_index av1_coef_tree[];
extern const aom_tree_index av1_coef_con_tree[];
Alex Converse's avatar
Alex Converse committed
49
#if !CONFIG_ANS
Yaowu Xu's avatar
Yaowu Xu committed
50
extern const struct av1_token av1_coef_encodings[];
Alex Converse's avatar
Alex Converse committed
51
#endif  // !CONFIG_ANS
Jingning Han's avatar
Jingning Han committed
52

Yaowu Xu's avatar
Yaowu Xu committed
53
54
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
55

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

#if CONFIG_VAR_TX
Yaowu Xu's avatar
Yaowu Xu committed
60
61
62
void av1_tokenize_sb_inter(struct AV1_COMP *cpi, struct ThreadData *td,
                           TOKENEXTRA **t, int dry_run, int mi_row, int mi_col,
                           BLOCK_SIZE bsize);
63
#endif
Jingning Han's avatar
Jingning Han committed
64

Yaowu Xu's avatar
Yaowu Xu committed
65
66
67
68
void av1_tokenize_palette_sb(struct ThreadData *const td, BLOCK_SIZE bsize,
                             int plane, TOKENEXTRA **t);
void av1_tokenize_sb(struct AV1_COMP *cpi, struct ThreadData *td,
                     TOKENEXTRA **t, int dry_run, BLOCK_SIZE bsize);
69
#if CONFIG_SUPERTX
Yaowu Xu's avatar
Yaowu Xu committed
70
71
void av1_tokenize_sb_supertx(struct AV1_COMP *cpi, struct ThreadData *td,
                             TOKENEXTRA **t, int dry_run, BLOCK_SIZE bsize);
72
#endif
Jingning Han's avatar
Jingning Han committed
73

Yaowu Xu's avatar
Yaowu Xu committed
74
extern const int16_t *av1_dct_value_cost_ptr;
Jingning Han's avatar
Jingning Han committed
75
76
77
78
/* 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
79
80
81
82
83
84
85
86
87
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
88
  if (token != CATEGORY6_TOKEN)
Yaowu Xu's avatar
Yaowu Xu committed
89
90
    return av1_extra_bits[token].cost[extrabits >> 1];
  return av1_cat6_low_cost[(extrabits >> 1) & 0xff] +
91
         cat6_high_table[extrabits >> 9];
Jingning Han's avatar
Jingning Han committed
92
93
}

Yaowu Xu's avatar
Yaowu Xu committed
94
95
96
97
98
#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
99
100
}
#else
Yaowu Xu's avatar
Yaowu Xu committed
101
static INLINE const int *av1_get_high_cost_table(int bit_depth) {
102
  (void)bit_depth;
Yaowu Xu's avatar
Yaowu Xu committed
103
  return av1_cat6_high_cost;
Jingning Han's avatar
Jingning Han committed
104
}
Yaowu Xu's avatar
Yaowu Xu committed
105
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
106

Yaowu Xu's avatar
Yaowu Xu committed
107
static INLINE void av1_get_token_extra(int v, int16_t *token, EXTRABIT *extra) {
Jingning Han's avatar
Jingning Han committed
108
109
110
111
112
113
114
115
  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
116
117
  *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
118
}
Yaowu Xu's avatar
Yaowu Xu committed
119
static INLINE int16_t av1_get_token(int v) {
120
  if (v >= CAT6_MIN_VAL || v <= -CAT6_MIN_VAL) return 10;
Yaowu Xu's avatar
Yaowu Xu committed
121
  return av1_dct_cat_lt_10_value_tokens[v].token;
Jingning Han's avatar
Jingning Han committed
122
123
}

Yaowu Xu's avatar
Yaowu Xu committed
124
125
static INLINE int av1_get_token_cost(int v, int16_t *token,
                                     const int *cat6_high_table) {
Julia Robson's avatar
Julia Robson committed
126
127
128
129
  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
130
    return av1_cat6_low_cost[extrabits & 0xff] +
131
           cat6_high_table[extrabits >> 8];
Julia Robson's avatar
Julia Robson committed
132
  }
Yaowu Xu's avatar
Yaowu Xu committed
133
134
  *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
135
}
Jingning Han's avatar
Jingning Han committed
136
137
138
139
140

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

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