blob: db227231f090efc0a03831c28eac12cf0fa20fee [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Krishna Rapaka7319db52021-09-28 20:35:29 -07002 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Krishna Rapaka7319db52021-09-28 20:35:29 -07004 * This source code is subject to the terms of the BSD 3-Clause Clear License and the
5 * Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear License was
6 * not distributed with this source code in the LICENSE file, you can obtain it
7 * at aomedia.org/license/software-license/bsd-3-c-c/. If the Alliance for Open Media Patent
8 * License 1.0 was not distributed with this source code in the PATENTS file, you
9 * can obtain it at aomedia.org/license/patent-license/.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
James Zerne1cbb132018-08-22 14:10:36 -070012#ifndef AOM_AV1_ENCODER_COST_H_
13#define AOM_AV1_ENCODER_COST_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
15#include "aom_dsp/prob.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
18#ifdef __cplusplus
19extern "C" {
20#endif
21
Hui Suc1cd5192018-01-24 11:05:06 -080022extern const uint16_t av1_prob_cost[128];
Yaowu Xuc27fc142016-08-22 16:08:15 -070023
Yaowu Xuf883b422016-08-30 14:01:10 -070024// The factor to scale from cost in bits to cost in av1_prob_cost units.
25#define AV1_PROB_COST_SHIFT 9
Yaowu Xuc27fc142016-08-22 16:08:15 -070026
Yaowu Xuc27fc142016-08-22 16:08:15 -070027// Cost of coding an n bit literal, using 128 (i.e. 50%) probability
28// for each bit.
Yaowu Xuf883b422016-08-30 14:01:10 -070029#define av1_cost_literal(n) ((n) * (1 << AV1_PROB_COST_SHIFT))
Yaowu Xuc27fc142016-08-22 16:08:15 -070030
Nathan E. Egge50f19112017-06-27 21:07:57 -040031// Calculate the cost of a symbol with probability p15 / 2^15
32static INLINE int av1_cost_symbol(aom_cdf_prob p15) {
Yunqing Wang85923a62019-01-18 18:48:42 -080033 // p15 can be out of range [1, CDF_PROB_TOP - 1]. Clamping it, so that the
34 // following cost calculation works correctly. Otherwise, if p15 =
35 // CDF_PROB_TOP, shift would be -1, and "p15 << shift" would be wrong.
36 p15 = (aom_cdf_prob)clamp(p15, 1, CDF_PROB_TOP - 1);
Nathan E. Egge50f19112017-06-27 21:07:57 -040037 assert(0 < p15 && p15 < CDF_PROB_TOP);
38 const int shift = CDF_PROB_BITS - 1 - get_msb(p15);
Hui Suc1cd5192018-01-24 11:05:06 -080039 const int prob = get_prob(p15 << shift, CDF_PROB_TOP);
40 assert(prob >= 128);
41 return av1_prob_cost[prob - 128] + av1_cost_literal(shift);
Nathan E. Egge50f19112017-06-27 21:07:57 -040042}
43
Nathan E. Egge50f19112017-06-27 21:07:57 -040044void av1_cost_tokens_from_cdf(int *costs, const aom_cdf_prob *cdf,
45 const int *inv_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -070046
Yaowu Xuc27fc142016-08-22 16:08:15 -070047#ifdef __cplusplus
48} // extern "C"
49#endif
50
James Zerne1cbb132018-08-22 14:10:36 -070051#endif // AOM_AV1_ENCODER_COST_H_