Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 2ab7ff0 | 2016-09-02 12:04:54 -0700 | [diff] [blame] | 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <assert.h> |
| 13 | #include <limits.h> |
| 14 | #include <stdio.h> |
| 15 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 16 | #include "aom/aom_encoder.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 17 | #include "aom_dsp/bitwriter_buffer.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 18 | #include "aom_dsp/aom_dsp_common.h" |
Debargha Mukherjee | 47748b5 | 2017-03-24 12:20:49 -0700 | [diff] [blame] | 19 | #include "aom_dsp/binary_codes_writer.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 20 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 21 | #include "aom_ports/mem_ops.h" |
| 22 | #include "aom_ports/system_state.h" |
Angie Chiang | 6062a8b | 2016-09-21 16:01:04 -0700 | [diff] [blame] | 23 | #if CONFIG_BITSTREAM_DEBUG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 24 | #include "aom_util/debug_util.h" |
Angie Chiang | 6062a8b | 2016-09-21 16:01:04 -0700 | [diff] [blame] | 25 | #endif // CONFIG_BITSTREAM_DEBUG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 26 | |
Jean-Marc Valin | 0143513 | 2017-02-18 14:12:53 -0500 | [diff] [blame] | 27 | #if CONFIG_CDEF |
Steinar Midtskogen | a9d41e8 | 2017-03-17 12:48:15 +0100 | [diff] [blame] | 28 | #include "av1/common/cdef.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 29 | #include "av1/common/clpf.h" |
Jean-Marc Valin | 0143513 | 2017-02-18 14:12:53 -0500 | [diff] [blame] | 30 | #endif // CONFIG_CDEF |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 31 | #include "av1/common/entropy.h" |
| 32 | #include "av1/common/entropymode.h" |
| 33 | #include "av1/common/entropymv.h" |
| 34 | #include "av1/common/mvref_common.h" |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 35 | #include "av1/common/odintrin.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 36 | #include "av1/common/pred_common.h" |
| 37 | #include "av1/common/reconinter.h" |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 38 | #if CONFIG_EXT_INTRA |
| 39 | #include "av1/common/reconintra.h" |
| 40 | #endif // CONFIG_EXT_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 41 | #include "av1/common/seg_common.h" |
| 42 | #include "av1/common/tile_common.h" |
| 43 | |
| 44 | #if CONFIG_ANS |
Alex Converse | 1ac1ae7 | 2016-09-17 15:11:16 -0700 | [diff] [blame] | 45 | #include "aom_dsp/buf_ans.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 46 | #endif // CONFIG_ANS |
Angie Chiang | c8af611 | 2017-03-16 16:11:22 -0700 | [diff] [blame] | 47 | #if CONFIG_LV_MAP |
| 48 | #include "av1/encoder/encodetxb.h" |
| 49 | #endif // CONFIG_LV_MAP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 50 | #include "av1/encoder/bitstream.h" |
| 51 | #include "av1/encoder/cost.h" |
| 52 | #include "av1/encoder/encodemv.h" |
| 53 | #include "av1/encoder/mcomp.h" |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 54 | #if CONFIG_PALETTE && CONFIG_PALETTE_DELTA_ENCODING |
| 55 | #include "av1/encoder/palette.h" |
| 56 | #endif // CONFIG_PALETTE && CONFIG_PALETTE_DELTA_ENCODING |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 57 | #include "av1/encoder/segmentation.h" |
| 58 | #include "av1/encoder/subexp.h" |
| 59 | #include "av1/encoder/tokenize.h" |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 60 | #if CONFIG_PVQ |
| 61 | #include "av1/encoder/pvq_encoder.h" |
| 62 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 63 | |
Nathan E. Egge | 3c05679 | 2016-05-20 08:58:44 -0400 | [diff] [blame] | 64 | static struct av1_token intra_mode_encodings[INTRA_MODES]; |
| 65 | static struct av1_token switchable_interp_encodings[SWITCHABLE_FILTERS]; |
Alex Converse | 57795a4 | 2017-03-14 12:18:25 -0700 | [diff] [blame] | 66 | #if CONFIG_EXT_PARTITION_TYPES && !CONFIG_EC_MULTISYMBOL |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 67 | static const struct av1_token ext_partition_encodings[EXT_PARTITION_TYPES] = { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 68 | { 0, 1 }, { 4, 3 }, { 12, 4 }, { 7, 3 }, |
| 69 | { 10, 4 }, { 11, 4 }, { 26, 5 }, { 27, 5 } |
| 70 | }; |
| 71 | #endif |
Nathan E. Egge | 3c05679 | 2016-05-20 08:58:44 -0400 | [diff] [blame] | 72 | static struct av1_token partition_encodings[PARTITION_TYPES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 73 | #if !CONFIG_REF_MV |
Nathan E. Egge | 3c05679 | 2016-05-20 08:58:44 -0400 | [diff] [blame] | 74 | static struct av1_token inter_mode_encodings[INTER_MODES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 75 | #endif |
| 76 | #if CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 77 | static const struct av1_token |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 78 | inter_compound_mode_encodings[INTER_COMPOUND_MODES] = { |
| 79 | { 2, 2 }, { 50, 6 }, { 51, 6 }, { 24, 5 }, { 52, 6 }, |
| 80 | { 53, 6 }, { 54, 6 }, { 55, 6 }, { 0, 1 }, { 7, 3 } |
| 81 | }; |
| 82 | #endif // CONFIG_EXT_INTER |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 83 | #if CONFIG_PALETTE |
Alex Converse | 9210981 | 2017-02-22 10:21:40 -0800 | [diff] [blame] | 84 | static struct av1_token palette_size_encodings[PALETTE_SIZES]; |
| 85 | static struct av1_token palette_color_index_encodings[PALETTE_SIZES] |
| 86 | [PALETTE_COLORS]; |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 87 | #endif // CONFIG_PALETTE |
Nathan E. Egge | 2ea519e | 2017-02-16 18:18:15 -0500 | [diff] [blame] | 88 | #if !CONFIG_EC_MULTISYMBOL |
Jingning Han | aae72a6 | 2016-10-25 15:35:29 -0700 | [diff] [blame] | 89 | static const struct av1_token tx_size_encodings[MAX_TX_DEPTH][TX_SIZES] = { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 90 | { { 0, 1 }, { 1, 1 } }, // Max tx_size is 8X8 |
| 91 | { { 0, 1 }, { 2, 2 }, { 3, 2 } }, // Max tx_size is 16X16 |
| 92 | { { 0, 1 }, { 2, 2 }, { 6, 3 }, { 7, 3 } }, // Max tx_size is 32X32 |
Debargha Mukherjee | 25ed530 | 2016-11-22 12:13:41 -0800 | [diff] [blame] | 93 | #if CONFIG_TX64X64 |
| 94 | { { 0, 1 }, { 2, 2 }, { 6, 3 }, { 14, 4 }, { 15, 4 } }, // Max tx_size 64X64 |
| 95 | #endif // CONFIG_TX64X64 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 96 | }; |
Nathan E. Egge | 2ea519e | 2017-02-16 18:18:15 -0500 | [diff] [blame] | 97 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 98 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 99 | #if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 100 | static INLINE void write_uniform(aom_writer *w, int n, int v) { |
hui su | 3749929 | 2017-04-26 09:49:53 -0700 | [diff] [blame] | 101 | const int l = get_unsigned_bits(n); |
| 102 | const int m = (1 << l) - n; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 103 | if (l == 0) return; |
| 104 | if (v < m) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 105 | aom_write_literal(w, v, l - 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 106 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 107 | aom_write_literal(w, m + ((v - m) >> 1), l - 1); |
| 108 | aom_write_literal(w, (v - m) & 1, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 111 | #endif // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 112 | |
| 113 | #if CONFIG_EXT_TX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 114 | static struct av1_token ext_tx_inter_encodings[EXT_TX_SETS_INTER][TX_TYPES]; |
| 115 | static struct av1_token ext_tx_intra_encodings[EXT_TX_SETS_INTRA][TX_TYPES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 116 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 117 | static struct av1_token ext_tx_encodings[TX_TYPES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 118 | #endif // CONFIG_EXT_TX |
| 119 | #if CONFIG_GLOBAL_MOTION |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 120 | static struct av1_token global_motion_types_encodings[GLOBAL_TRANS_TYPES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 121 | #endif // CONFIG_GLOBAL_MOTION |
| 122 | #if CONFIG_EXT_INTRA |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 123 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 124 | static struct av1_token intra_filter_encodings[INTRA_FILTERS]; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 125 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 126 | #endif // CONFIG_EXT_INTRA |
| 127 | #if CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 128 | static struct av1_token interintra_mode_encodings[INTERINTRA_MODES]; |
Debargha Mukherjee | b9c33d9 | 2017-04-27 12:28:35 -0700 | [diff] [blame] | 129 | #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Sarah Parker | 6fddd18 | 2016-11-10 20:57:20 -0800 | [diff] [blame] | 130 | static struct av1_token compound_type_encodings[COMPOUND_TYPES]; |
Debargha Mukherjee | b9c33d9 | 2017-04-27 12:28:35 -0700 | [diff] [blame] | 131 | #endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 132 | #endif // CONFIG_EXT_INTER |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 133 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 134 | static struct av1_token motion_mode_encodings[MOTION_MODES]; |
| 135 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 136 | #if CONFIG_LOOP_RESTORATION |
| 137 | static struct av1_token switchable_restore_encodings[RESTORE_SWITCHABLE_TYPES]; |
| 138 | #endif // CONFIG_LOOP_RESTORATION |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 139 | static void write_uncompressed_header(AV1_COMP *cpi, |
| 140 | struct aom_write_bit_buffer *wb); |
| 141 | static uint32_t write_compressed_header(AV1_COMP *cpi, uint8_t *data); |
Thomas Davies | dbfc4f9 | 2017-01-18 16:46:09 +0000 | [diff] [blame] | 142 | static int remux_tiles(const AV1_COMMON *const cm, uint8_t *dst, |
| 143 | const uint32_t data_size, const uint32_t max_tile_size, |
| 144 | const uint32_t max_tile_col_size, |
| 145 | int *const tile_size_bytes, |
| 146 | int *const tile_col_size_bytes); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 147 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 148 | void av1_encode_token_init(void) { |
Urvang Joshi | 0b32597 | 2016-10-24 14:06:43 -0700 | [diff] [blame] | 149 | #if CONFIG_EXT_TX || CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 150 | int s; |
Urvang Joshi | 0b32597 | 2016-10-24 14:06:43 -0700 | [diff] [blame] | 151 | #endif // CONFIG_EXT_TX || CONFIG_PALETTE |
| 152 | #if CONFIG_EXT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 153 | for (s = 1; s < EXT_TX_SETS_INTER; ++s) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 154 | av1_tokens_from_tree(ext_tx_inter_encodings[s], av1_ext_tx_inter_tree[s]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 155 | } |
| 156 | for (s = 1; s < EXT_TX_SETS_INTRA; ++s) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 157 | av1_tokens_from_tree(ext_tx_intra_encodings[s], av1_ext_tx_intra_tree[s]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 158 | } |
| 159 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 160 | av1_tokens_from_tree(ext_tx_encodings, av1_ext_tx_tree); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 161 | #endif // CONFIG_EXT_TX |
Nathan E. Egge | 3c05679 | 2016-05-20 08:58:44 -0400 | [diff] [blame] | 162 | av1_tokens_from_tree(intra_mode_encodings, av1_intra_mode_tree); |
| 163 | av1_tokens_from_tree(switchable_interp_encodings, av1_switchable_interp_tree); |
| 164 | av1_tokens_from_tree(partition_encodings, av1_partition_tree); |
| 165 | #if !CONFIG_REF_MV |
| 166 | av1_tokens_from_tree(inter_mode_encodings, av1_inter_mode_tree); |
| 167 | #endif |
| 168 | |
Urvang Joshi | 0b32597 | 2016-10-24 14:06:43 -0700 | [diff] [blame] | 169 | #if CONFIG_PALETTE |
| 170 | av1_tokens_from_tree(palette_size_encodings, av1_palette_size_tree); |
Alex Converse | 9210981 | 2017-02-22 10:21:40 -0800 | [diff] [blame] | 171 | for (s = 0; s < PALETTE_SIZES; ++s) { |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 172 | av1_tokens_from_tree(palette_color_index_encodings[s], |
| 173 | av1_palette_color_index_tree[s]); |
Urvang Joshi | 0b32597 | 2016-10-24 14:06:43 -0700 | [diff] [blame] | 174 | } |
| 175 | #endif // CONFIG_PALETTE |
| 176 | |
hui su | b4e25d2 | 2017-03-09 15:32:30 -0800 | [diff] [blame] | 177 | #if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 178 | av1_tokens_from_tree(intra_filter_encodings, av1_intra_filter_tree); |
hui su | b4e25d2 | 2017-03-09 15:32:30 -0800 | [diff] [blame] | 179 | #endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 180 | #if CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 181 | av1_tokens_from_tree(interintra_mode_encodings, av1_interintra_mode_tree); |
Debargha Mukherjee | b9c33d9 | 2017-04-27 12:28:35 -0700 | [diff] [blame] | 182 | #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Sarah Parker | 6fddd18 | 2016-11-10 20:57:20 -0800 | [diff] [blame] | 183 | av1_tokens_from_tree(compound_type_encodings, av1_compound_type_tree); |
Debargha Mukherjee | b9c33d9 | 2017-04-27 12:28:35 -0700 | [diff] [blame] | 184 | #endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 185 | #endif // CONFIG_EXT_INTER |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 186 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 187 | av1_tokens_from_tree(motion_mode_encodings, av1_motion_mode_tree); |
| 188 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 189 | #if CONFIG_GLOBAL_MOTION |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 190 | av1_tokens_from_tree(global_motion_types_encodings, |
| 191 | av1_global_motion_types_tree); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 192 | #endif // CONFIG_GLOBAL_MOTION |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 193 | #if CONFIG_LOOP_RESTORATION |
| 194 | av1_tokens_from_tree(switchable_restore_encodings, |
| 195 | av1_switchable_restore_tree); |
| 196 | #endif // CONFIG_LOOP_RESTORATION |
Nathan E. Egge | 4947c29 | 2016-04-26 11:37:06 -0400 | [diff] [blame] | 197 | |
Nathan E. Egge | dfa33f2 | 2016-11-16 09:44:26 -0500 | [diff] [blame] | 198 | #if CONFIG_EC_MULTISYMBOL |
Angie Chiang | 1733f6b | 2017-01-05 09:52:20 -0800 | [diff] [blame] | 199 | /* This hack is necessary when CONFIG_DUAL_FILTER is enabled because the five |
Nathan E. Egge | 4947c29 | 2016-04-26 11:37:06 -0400 | [diff] [blame] | 200 | SWITCHABLE_FILTERS are not consecutive, e.g., 0, 1, 2, 3, 4, when doing |
| 201 | an in-order traversal of the av1_switchable_interp_tree structure. */ |
| 202 | av1_indices_from_tree(av1_switchable_interp_ind, av1_switchable_interp_inv, |
Jingning Han | 8e67c05 | 2017-03-23 15:47:33 -0700 | [diff] [blame] | 203 | av1_switchable_interp_tree); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 204 | /* This hack is necessary because the four TX_TYPES are not consecutive, |
| 205 | e.g., 0, 1, 2, 3, when doing an in-order traversal of the av1_ext_tx_tree |
| 206 | structure. */ |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 207 | #if CONFIG_EXT_TX |
| 208 | for (s = 1; s < EXT_TX_SETS_INTRA; ++s) |
| 209 | av1_indices_from_tree(av1_ext_tx_intra_ind[s], av1_ext_tx_intra_inv[s], |
Jingning Han | 8e67c05 | 2017-03-23 15:47:33 -0700 | [diff] [blame] | 210 | av1_ext_tx_intra_tree[s]); |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 211 | for (s = 1; s < EXT_TX_SETS_INTER; ++s) |
| 212 | av1_indices_from_tree(av1_ext_tx_inter_ind[s], av1_ext_tx_inter_inv[s], |
Jingning Han | 8e67c05 | 2017-03-23 15:47:33 -0700 | [diff] [blame] | 213 | av1_ext_tx_inter_tree[s]); |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 214 | #else |
Jingning Han | 8e67c05 | 2017-03-23 15:47:33 -0700 | [diff] [blame] | 215 | av1_indices_from_tree(av1_ext_tx_ind, av1_ext_tx_inv, av1_ext_tx_tree); |
David Barker | f541932 | 2016-11-10 12:04:21 +0000 | [diff] [blame] | 216 | #endif |
Jingning Han | 8e67c05 | 2017-03-23 15:47:33 -0700 | [diff] [blame] | 217 | av1_indices_from_tree(av1_intra_mode_ind, av1_intra_mode_inv, |
Nathan E. Egge | 3ef926e | 2016-09-07 18:20:41 -0400 | [diff] [blame] | 218 | av1_intra_mode_tree); |
Jingning Han | 8e67c05 | 2017-03-23 15:47:33 -0700 | [diff] [blame] | 219 | av1_indices_from_tree(av1_inter_mode_ind, av1_inter_mode_inv, |
Nathan E. Egge | 6ec4d10 | 2016-09-08 10:41:20 -0400 | [diff] [blame] | 220 | av1_inter_mode_tree); |
Nathan E. Egge | 4947c29 | 2016-04-26 11:37:06 -0400 | [diff] [blame] | 221 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 224 | static void write_intra_mode_kf(const AV1_COMMON *cm, FRAME_CONTEXT *frame_ctx, |
| 225 | const MODE_INFO *mi, const MODE_INFO *above_mi, |
| 226 | const MODE_INFO *left_mi, int block, |
| 227 | PREDICTION_MODE mode, aom_writer *w) { |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 228 | #if CONFIG_INTRABC |
| 229 | assert(!is_intrabc_block(&mi->mbmi)); |
| 230 | #endif // CONFIG_INTRABC |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 231 | #if CONFIG_EC_MULTISYMBOL |
| 232 | aom_write_symbol(w, av1_intra_mode_ind[mode], |
| 233 | get_y_mode_cdf(frame_ctx, mi, above_mi, left_mi, block), |
| 234 | INTRA_MODES); |
| 235 | (void)cm; |
| 236 | #else |
| 237 | av1_write_token(w, av1_intra_mode_tree, |
| 238 | get_y_mode_probs(cm, mi, above_mi, left_mi, block), |
| 239 | &intra_mode_encodings[mode]); |
| 240 | (void)frame_ctx; |
Nathan E. Egge | 380cb1a | 2016-09-08 10:13:42 -0400 | [diff] [blame] | 241 | #endif |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 242 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 243 | |
| 244 | #if CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 245 | static void write_interintra_mode(aom_writer *w, INTERINTRA_MODE mode, |
| 246 | const aom_prob *probs) { |
| 247 | av1_write_token(w, av1_interintra_mode_tree, probs, |
| 248 | &interintra_mode_encodings[mode]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 249 | } |
| 250 | #endif // CONFIG_EXT_INTER |
| 251 | |
Thomas Davies | 1de6c88 | 2017-01-11 17:47:49 +0000 | [diff] [blame] | 252 | static void write_inter_mode(aom_writer *w, PREDICTION_MODE mode, |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 253 | FRAME_CONTEXT *ec_ctx, const int16_t mode_ctx) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 254 | #if CONFIG_REF_MV |
| 255 | const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK; |
Thomas Davies | 1de6c88 | 2017-01-11 17:47:49 +0000 | [diff] [blame] | 256 | const aom_prob newmv_prob = ec_ctx->newmv_prob[newmv_ctx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 257 | |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 258 | #define IS_NEWMV_MODE(mode) ((mode) == NEWMV) |
| 259 | aom_write(w, !IS_NEWMV_MODE(mode), newmv_prob); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 260 | |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 261 | if (!IS_NEWMV_MODE(mode)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 262 | const int16_t zeromv_ctx = (mode_ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK; |
Thomas Davies | 1de6c88 | 2017-01-11 17:47:49 +0000 | [diff] [blame] | 263 | const aom_prob zeromv_prob = ec_ctx->zeromv_prob[zeromv_ctx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 264 | |
| 265 | if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) { |
| 266 | assert(mode == ZEROMV); |
| 267 | return; |
| 268 | } |
| 269 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 270 | aom_write(w, mode != ZEROMV, zeromv_prob); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 271 | |
| 272 | if (mode != ZEROMV) { |
| 273 | int16_t refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 274 | aom_prob refmv_prob; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 275 | |
| 276 | if (mode_ctx & (1 << SKIP_NEARESTMV_OFFSET)) refmv_ctx = 6; |
| 277 | if (mode_ctx & (1 << SKIP_NEARMV_OFFSET)) refmv_ctx = 7; |
| 278 | if (mode_ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) refmv_ctx = 8; |
| 279 | |
Thomas Davies | 1de6c88 | 2017-01-11 17:47:49 +0000 | [diff] [blame] | 280 | refmv_prob = ec_ctx->refmv_prob[refmv_ctx]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 281 | aom_write(w, mode != NEARESTMV, refmv_prob); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 284 | |
| 285 | #undef IS_NEWMV_MODE |
| 286 | |
| 287 | #else // !CONFIG_REF_MV |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 288 | assert(is_inter_mode(mode)); |
Nathan E. Egge | a59b23d | 2016-11-16 09:44:26 -0500 | [diff] [blame] | 289 | #if CONFIG_EC_MULTISYMBOL |
Nathan E. Egge | 6ec4d10 | 2016-09-08 10:41:20 -0400 | [diff] [blame] | 290 | aom_write_symbol(w, av1_inter_mode_ind[INTER_OFFSET(mode)], |
Thomas Davies | 1de6c88 | 2017-01-11 17:47:49 +0000 | [diff] [blame] | 291 | ec_ctx->inter_mode_cdf[mode_ctx], INTER_MODES); |
Nathan E. Egge | 6ec4d10 | 2016-09-08 10:41:20 -0400 | [diff] [blame] | 292 | #else |
| 293 | { |
Thomas Davies | 1de6c88 | 2017-01-11 17:47:49 +0000 | [diff] [blame] | 294 | const aom_prob *const inter_probs = ec_ctx->inter_mode_probs[mode_ctx]; |
Nathan E. Egge | 6ec4d10 | 2016-09-08 10:41:20 -0400 | [diff] [blame] | 295 | av1_write_token(w, av1_inter_mode_tree, inter_probs, |
| 296 | &inter_mode_encodings[INTER_OFFSET(mode)]); |
| 297 | } |
| 298 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 299 | #endif |
| 300 | } |
| 301 | |
| 302 | #if CONFIG_REF_MV |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 303 | static void write_drl_idx(const AV1_COMMON *cm, const MB_MODE_INFO *mbmi, |
| 304 | const MB_MODE_INFO_EXT *mbmi_ext, aom_writer *w) { |
| 305 | uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 306 | |
| 307 | assert(mbmi->ref_mv_idx < 3); |
| 308 | |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 309 | #if CONFIG_EXT_INTER |
| 310 | if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) { |
| 311 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 312 | if (mbmi->mode == NEWMV) { |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 313 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 314 | int idx; |
| 315 | for (idx = 0; idx < 2; ++idx) { |
| 316 | if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) { |
| 317 | uint8_t drl_ctx = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 318 | av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx); |
| 319 | aom_prob drl_prob = cm->fc->drl_prob[drl_ctx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 320 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 321 | aom_write(w, mbmi->ref_mv_idx != idx, drl_prob); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 322 | if (mbmi->ref_mv_idx == idx) return; |
| 323 | } |
| 324 | } |
| 325 | return; |
| 326 | } |
| 327 | |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 328 | if (have_nearmv_in_inter_mode(mbmi->mode)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 329 | int idx; |
| 330 | // TODO(jingning): Temporary solution to compensate the NEARESTMV offset. |
| 331 | for (idx = 1; idx < 3; ++idx) { |
| 332 | if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) { |
| 333 | uint8_t drl_ctx = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 334 | av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx); |
| 335 | aom_prob drl_prob = cm->fc->drl_prob[drl_ctx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 336 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 337 | aom_write(w, mbmi->ref_mv_idx != (idx - 1), drl_prob); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 338 | if (mbmi->ref_mv_idx == (idx - 1)) return; |
| 339 | } |
| 340 | } |
| 341 | return; |
| 342 | } |
| 343 | } |
| 344 | #endif |
| 345 | |
| 346 | #if CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 347 | static void write_inter_compound_mode(AV1_COMMON *cm, aom_writer *w, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 348 | PREDICTION_MODE mode, |
| 349 | const int16_t mode_ctx) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 350 | const aom_prob *const inter_compound_probs = |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 351 | cm->fc->inter_compound_mode_probs[mode_ctx]; |
| 352 | |
| 353 | assert(is_inter_compound_mode(mode)); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 354 | av1_write_token(w, av1_inter_compound_mode_tree, inter_compound_probs, |
| 355 | &inter_compound_mode_encodings[INTER_COMPOUND_OFFSET(mode)]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 356 | } |
| 357 | #endif // CONFIG_EXT_INTER |
| 358 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 359 | static void encode_unsigned_max(struct aom_write_bit_buffer *wb, int data, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 360 | int max) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 361 | aom_wb_write_literal(wb, data, get_unsigned_bits(max)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Thomas Davies | 493623e | 2017-03-31 16:12:25 +0100 | [diff] [blame] | 364 | #if !CONFIG_EC_ADAPT || \ |
| 365 | (CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION || CONFIG_EXT_INTER) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 366 | static void prob_diff_update(const aom_tree_index *tree, |
| 367 | aom_prob probs[/*n - 1*/], |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 368 | const unsigned int counts[/*n - 1*/], int n, |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 369 | int probwt, aom_writer *w) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 370 | int i; |
| 371 | unsigned int branch_ct[32][2]; |
| 372 | |
| 373 | // Assuming max number of probabilities <= 32 |
| 374 | assert(n <= 32); |
| 375 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 376 | av1_tree_probs_from_distribution(tree, branch_ct, counts); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 377 | for (i = 0; i < n - 1; ++i) |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 378 | av1_cond_prob_diff_update(w, &probs[i], branch_ct[i], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 379 | } |
Nathan E. Egge | b353a8e | 2017-02-17 10:27:37 -0500 | [diff] [blame] | 380 | #endif |
| 381 | |
Sarah Parker | e2e591c | 2017-03-03 15:21:19 -0800 | [diff] [blame] | 382 | #if CONFIG_EXT_INTER || !CONFIG_EC_ADAPT |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 383 | static int prob_diff_update_savings(const aom_tree_index *tree, |
| 384 | aom_prob probs[/*n - 1*/], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 385 | const unsigned int counts[/*n - 1*/], int n, |
| 386 | int probwt) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 387 | int i; |
| 388 | unsigned int branch_ct[32][2]; |
| 389 | int savings = 0; |
| 390 | |
| 391 | // Assuming max number of probabilities <= 32 |
| 392 | assert(n <= 32); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 393 | av1_tree_probs_from_distribution(tree, branch_ct, counts); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 394 | for (i = 0; i < n - 1; ++i) { |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 395 | savings += |
| 396 | av1_cond_prob_diff_update_savings(&probs[i], branch_ct[i], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 397 | } |
| 398 | return savings; |
| 399 | } |
Sarah Parker | e2e591c | 2017-03-03 15:21:19 -0800 | [diff] [blame] | 400 | #endif // CONFIG_EXT_INTER || !CONFIG_EC_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 401 | |
| 402 | #if CONFIG_VAR_TX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 403 | static void write_tx_size_vartx(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 404 | const MB_MODE_INFO *mbmi, TX_SIZE tx_size, |
Jingning Han | 94d5bfc | 2016-10-21 10:14:36 -0700 | [diff] [blame] | 405 | int depth, int blk_row, int blk_col, |
| 406 | aom_writer *w) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 407 | const int tx_row = blk_row >> 1; |
| 408 | const int tx_col = blk_col >> 1; |
Jingning Han | f65b870 | 2016-10-31 12:13:20 -0700 | [diff] [blame] | 409 | const int max_blocks_high = max_block_high(xd, mbmi->sb_type, 0); |
| 410 | const int max_blocks_wide = max_block_wide(xd, mbmi->sb_type, 0); |
| 411 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 412 | int ctx = txfm_partition_context(xd->above_txfm_context + tx_col, |
Jingning Han | c8b8936 | 2016-11-01 10:28:53 -0700 | [diff] [blame] | 413 | xd->left_txfm_context + tx_row, |
| 414 | mbmi->sb_type, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 415 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 416 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 417 | |
Jingning Han | 571189c | 2016-10-24 10:38:43 -0700 | [diff] [blame] | 418 | if (depth == MAX_VARTX_DEPTH) { |
Jingning Han | 94d5bfc | 2016-10-21 10:14:36 -0700 | [diff] [blame] | 419 | txfm_partition_update(xd->above_txfm_context + tx_col, |
Jingning Han | 581d169 | 2017-01-05 16:03:54 -0800 | [diff] [blame] | 420 | xd->left_txfm_context + tx_row, tx_size, tx_size); |
Jingning Han | 94d5bfc | 2016-10-21 10:14:36 -0700 | [diff] [blame] | 421 | return; |
| 422 | } |
| 423 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 424 | if (tx_size == mbmi->inter_tx_size[tx_row][tx_col]) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 425 | aom_write(w, 0, cm->fc->txfm_partition_prob[ctx]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 426 | txfm_partition_update(xd->above_txfm_context + tx_col, |
Jingning Han | 581d169 | 2017-01-05 16:03:54 -0800 | [diff] [blame] | 427 | xd->left_txfm_context + tx_row, tx_size, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 428 | } else { |
Jingning Han | f64062f | 2016-11-02 16:22:18 -0700 | [diff] [blame] | 429 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
| 430 | const int bsl = tx_size_wide_unit[sub_txs]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 431 | int i; |
Jingning Han | f64062f | 2016-11-02 16:22:18 -0700 | [diff] [blame] | 432 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 433 | aom_write(w, 1, cm->fc->txfm_partition_prob[ctx]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 434 | |
| 435 | if (tx_size == TX_8X8) { |
| 436 | txfm_partition_update(xd->above_txfm_context + tx_col, |
Jingning Han | ab9ecba | 2017-01-13 09:11:58 -0800 | [diff] [blame] | 437 | xd->left_txfm_context + tx_row, sub_txs, tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 438 | return; |
| 439 | } |
| 440 | |
| 441 | assert(bsl > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 442 | for (i = 0; i < 4; ++i) { |
Jingning Han | f64062f | 2016-11-02 16:22:18 -0700 | [diff] [blame] | 443 | int offsetr = blk_row + (i >> 1) * bsl; |
| 444 | int offsetc = blk_col + (i & 0x01) * bsl; |
| 445 | write_tx_size_vartx(cm, xd, mbmi, sub_txs, depth + 1, offsetr, offsetc, |
| 446 | w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 451 | static void update_txfm_partition_probs(AV1_COMMON *cm, aom_writer *w, |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 452 | FRAME_COUNTS *counts, int probwt) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 453 | int k; |
| 454 | for (k = 0; k < TXFM_PARTITION_CONTEXTS; ++k) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 455 | av1_cond_prob_diff_update(w, &cm->fc->txfm_partition_prob[k], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 456 | counts->txfm_partition[k], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 457 | } |
| 458 | #endif |
| 459 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 460 | static void write_selected_tx_size(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
| 461 | aom_writer *w) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 462 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 463 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Thomas Davies | 15580c5 | 2017-03-09 13:53:42 +0000 | [diff] [blame] | 464 | #if CONFIG_EC_ADAPT |
| 465 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 466 | (void)cm; |
| 467 | #else |
| 468 | FRAME_CONTEXT *ec_ctx = cm->fc; |
| 469 | #endif |
Jingning Han | 3daa4fd | 2017-01-20 10:33:50 -0800 | [diff] [blame] | 470 | // For sub8x8 blocks the tx_size symbol does not need to be sent |
Debargha Mukherjee | 428bbb2 | 2017-03-17 07:30:24 -0700 | [diff] [blame] | 471 | #if CONFIG_CB4X4 && (CONFIG_VAR_TX || CONFIG_EXT_TX) && CONFIG_RECT_TX |
Jingning Han | 3daa4fd | 2017-01-20 10:33:50 -0800 | [diff] [blame] | 472 | if (bsize > BLOCK_4X4) { |
| 473 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 474 | if (bsize >= BLOCK_8X8) { |
Jingning Han | 3daa4fd | 2017-01-20 10:33:50 -0800 | [diff] [blame] | 475 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 476 | const TX_SIZE tx_size = mbmi->tx_size; |
| 477 | const int is_inter = is_inter_block(mbmi); |
| 478 | const int tx_size_ctx = get_tx_size_context(xd); |
| 479 | const int tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize] |
| 480 | : intra_tx_size_cat_lookup[bsize]; |
| 481 | const TX_SIZE coded_tx_size = txsize_sqr_up_map[tx_size]; |
Jingning Han | 4e1737a | 2016-10-25 16:05:02 -0700 | [diff] [blame] | 482 | const int depth = tx_size_to_depth(coded_tx_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 483 | #if CONFIG_EXT_TX && CONFIG_RECT_TX |
Yue Chen | 49587a7 | 2016-09-28 17:09:47 -0700 | [diff] [blame] | 484 | assert(IMPLIES(is_rect_tx(tx_size), is_rect_tx_allowed(xd, mbmi))); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 485 | assert( |
| 486 | IMPLIES(is_rect_tx(tx_size), tx_size == max_txsize_rect_lookup[bsize])); |
| 487 | #endif // CONFIG_EXT_TX && CONFIG_RECT_TX |
| 488 | |
Nathan E. Egge | 2ea519e | 2017-02-16 18:18:15 -0500 | [diff] [blame] | 489 | #if CONFIG_EC_MULTISYMBOL |
Thomas Davies | 15580c5 | 2017-03-09 13:53:42 +0000 | [diff] [blame] | 490 | aom_write_symbol(w, depth, ec_ctx->tx_size_cdf[tx_size_cat][tx_size_ctx], |
Nathan E. Egge | 2ea519e | 2017-02-16 18:18:15 -0500 | [diff] [blame] | 491 | tx_size_cat + 2); |
| 492 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 493 | av1_write_token(w, av1_tx_size_tree[tx_size_cat], |
Thomas Davies | 15580c5 | 2017-03-09 13:53:42 +0000 | [diff] [blame] | 494 | ec_ctx->tx_size_probs[tx_size_cat][tx_size_ctx], |
Jingning Han | 4e1737a | 2016-10-25 16:05:02 -0700 | [diff] [blame] | 495 | &tx_size_encodings[tx_size_cat][depth]); |
Nathan E. Egge | 2ea519e | 2017-02-16 18:18:15 -0500 | [diff] [blame] | 496 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | #if CONFIG_REF_MV |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 501 | static void update_inter_mode_probs(AV1_COMMON *cm, aom_writer *w, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 502 | FRAME_COUNTS *counts) { |
| 503 | int i; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 504 | #if CONFIG_TILE_GROUPS |
| 505 | const int probwt = cm->num_tg; |
| 506 | #else |
| 507 | const int probwt = 1; |
| 508 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 509 | for (i = 0; i < NEWMV_MODE_CONTEXTS; ++i) |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 510 | av1_cond_prob_diff_update(w, &cm->fc->newmv_prob[i], counts->newmv_mode[i], |
| 511 | probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 512 | for (i = 0; i < ZEROMV_MODE_CONTEXTS; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 513 | av1_cond_prob_diff_update(w, &cm->fc->zeromv_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 514 | counts->zeromv_mode[i], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 515 | for (i = 0; i < REFMV_MODE_CONTEXTS; ++i) |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 516 | av1_cond_prob_diff_update(w, &cm->fc->refmv_prob[i], counts->refmv_mode[i], |
| 517 | probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 518 | for (i = 0; i < DRL_MODE_CONTEXTS; ++i) |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 519 | av1_cond_prob_diff_update(w, &cm->fc->drl_prob[i], counts->drl_mode[i], |
| 520 | probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 521 | } |
| 522 | #endif |
| 523 | |
| 524 | #if CONFIG_EXT_INTER |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 525 | static void update_inter_compound_mode_probs(AV1_COMMON *cm, int probwt, |
| 526 | aom_writer *w) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 527 | const int savings_thresh = av1_cost_one(GROUP_DIFF_UPDATE_PROB) - |
| 528 | av1_cost_zero(GROUP_DIFF_UPDATE_PROB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 529 | int i; |
| 530 | int savings = 0; |
| 531 | int do_update = 0; |
| 532 | for (i = 0; i < INTER_MODE_CONTEXTS; ++i) { |
| 533 | savings += prob_diff_update_savings( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 534 | av1_inter_compound_mode_tree, cm->fc->inter_compound_mode_probs[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 535 | cm->counts.inter_compound_mode[i], INTER_COMPOUND_MODES, probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 536 | } |
| 537 | do_update = savings > savings_thresh; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 538 | aom_write(w, do_update, GROUP_DIFF_UPDATE_PROB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 539 | if (do_update) { |
| 540 | for (i = 0; i < INTER_MODE_CONTEXTS; ++i) { |
| 541 | prob_diff_update( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 542 | av1_inter_compound_mode_tree, cm->fc->inter_compound_mode_probs[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 543 | cm->counts.inter_compound_mode[i], INTER_COMPOUND_MODES, probwt, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | } |
| 547 | #endif // CONFIG_EXT_INTER |
| 548 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 549 | static int write_skip(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
| 550 | int segment_id, const MODE_INFO *mi, aom_writer *w) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 551 | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) { |
| 552 | return 1; |
| 553 | } else { |
| 554 | const int skip = mi->mbmi.skip; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 555 | aom_write(w, skip, av1_get_skip_prob(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 556 | return skip; |
| 557 | } |
| 558 | } |
| 559 | |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 560 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 561 | static void write_motion_mode(const AV1_COMMON *cm, const MODE_INFO *mi, |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 562 | aom_writer *w) { |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 563 | const MB_MODE_INFO *mbmi = &mi->mbmi; |
| 564 | MOTION_MODE last_motion_mode_allowed = motion_mode_allowed( |
| 565 | #if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION |
| 566 | 0, cm->global_motion, |
| 567 | #endif // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION |
| 568 | mi); |
Yue Chen | 69f18e1 | 2016-09-08 14:48:15 -0700 | [diff] [blame] | 569 | |
| 570 | if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return; |
| 571 | #if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION |
| 572 | if (last_motion_mode_allowed == OBMC_CAUSAL) { |
| 573 | aom_write(w, mbmi->motion_mode == OBMC_CAUSAL, |
| 574 | cm->fc->obmc_prob[mbmi->sb_type]); |
| 575 | } else { |
| 576 | #endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION |
| 577 | av1_write_token(w, av1_motion_mode_tree, |
| 578 | cm->fc->motion_mode_prob[mbmi->sb_type], |
| 579 | &motion_mode_encodings[mbmi->motion_mode]); |
| 580 | #if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION |
| 581 | } |
| 582 | #endif // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION |
| 583 | } |
| 584 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
| 585 | |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 586 | #if CONFIG_DELTA_Q |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 587 | static void write_delta_qindex(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
| 588 | int delta_qindex, aom_writer *w) { |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 589 | int sign = delta_qindex < 0; |
| 590 | int abs = sign ? -delta_qindex : delta_qindex; |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 591 | int rem_bits, thr; |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 592 | int smallval = abs < DELTA_Q_SMALL ? 1 : 0; |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 593 | #if CONFIG_EC_ADAPT |
| 594 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 595 | (void)cm; |
| 596 | #else |
| 597 | FRAME_CONTEXT *ec_ctx = cm->fc; |
| 598 | (void)xd; |
| 599 | #endif |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 600 | |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 601 | #if CONFIG_EC_MULTISYMBOL |
| 602 | aom_write_symbol(w, AOMMIN(abs, DELTA_Q_SMALL), ec_ctx->delta_q_cdf, |
| 603 | DELTA_Q_PROBS + 1); |
| 604 | #else |
| 605 | int i = 0; |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 606 | while (i < DELTA_Q_SMALL && i <= abs) { |
| 607 | int bit = (i < abs); |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 608 | aom_write(w, bit, ec_ctx->delta_q_prob[i]); |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 609 | i++; |
| 610 | } |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 611 | #endif |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 612 | |
| 613 | if (!smallval) { |
| 614 | rem_bits = OD_ILOG_NZ(abs - 1) - 1; |
| 615 | thr = (1 << rem_bits) + 1; |
| 616 | aom_write_literal(w, rem_bits, 3); |
| 617 | aom_write_literal(w, abs - thr, rem_bits); |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 618 | } |
| 619 | if (abs > 0) { |
| 620 | aom_write_bit(w, sign); |
| 621 | } |
| 622 | } |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 623 | |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 624 | #if !CONFIG_EC_ADAPT |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 625 | static void update_delta_q_probs(AV1_COMMON *cm, aom_writer *w, |
| 626 | FRAME_COUNTS *counts) { |
| 627 | int k; |
Jingning Han | be44c5f | 2016-09-30 11:35:22 -0700 | [diff] [blame] | 628 | #if CONFIG_TILE_GROUPS |
| 629 | const int probwt = cm->num_tg; |
| 630 | #else |
| 631 | const int probwt = 1; |
| 632 | #endif |
Fangwen Fu | 6160df2 | 2017-04-24 09:45:51 -0700 | [diff] [blame] | 633 | #if CONFIG_EXT_DELTA_Q |
| 634 | if (!cm->delta_q_present_flag) return; |
| 635 | #endif // CONFIG_EXT_DELTA_Q |
Thomas Davies | 665cd70 | 2017-03-02 10:20:30 +0000 | [diff] [blame] | 636 | for (k = 0; k < DELTA_Q_PROBS; ++k) { |
Jingning Han | be44c5f | 2016-09-30 11:35:22 -0700 | [diff] [blame] | 637 | av1_cond_prob_diff_update(w, &cm->fc->delta_q_prob[k], counts->delta_q[k], |
| 638 | probwt); |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 639 | } |
| 640 | } |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 641 | #endif // CONFIG_EC_ADAPT |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 642 | |
| 643 | #if CONFIG_EXT_DELTA_Q |
| 644 | static void write_delta_lflevel(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
| 645 | int delta_lflevel, aom_writer *w) { |
| 646 | int sign = delta_lflevel < 0; |
| 647 | int abs = sign ? -delta_lflevel : delta_lflevel; |
| 648 | int rem_bits, thr; |
| 649 | int smallval = abs < DELTA_LF_SMALL ? 1 : 0; |
| 650 | #if CONFIG_EC_ADAPT |
| 651 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 652 | (void)cm; |
| 653 | #else |
| 654 | FRAME_CONTEXT *ec_ctx = cm->fc; |
| 655 | (void)xd; |
| 656 | #endif |
| 657 | |
| 658 | #if CONFIG_EC_MULTISYMBOL |
| 659 | aom_write_symbol(w, AOMMIN(abs, DELTA_LF_SMALL), ec_ctx->delta_lf_cdf, |
| 660 | DELTA_LF_PROBS + 1); |
| 661 | #else |
| 662 | int i = 0; |
| 663 | while (i < DELTA_LF_SMALL && i <= abs) { |
| 664 | int bit = (i < abs); |
| 665 | aom_write(w, bit, ec_ctx->delta_lf_prob[i]); |
| 666 | i++; |
| 667 | } |
| 668 | #endif // CONFIG_EC_MULTISYMBOL |
| 669 | |
| 670 | if (!smallval) { |
| 671 | rem_bits = OD_ILOG_NZ(abs - 1) - 1; |
| 672 | thr = (1 << rem_bits) + 1; |
| 673 | aom_write_literal(w, rem_bits, 3); |
| 674 | aom_write_literal(w, abs - thr, rem_bits); |
| 675 | } |
| 676 | if (abs > 0) { |
| 677 | aom_write_bit(w, sign); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | #if !CONFIG_EC_ADAPT |
| 682 | static void update_delta_lf_probs(AV1_COMMON *cm, aom_writer *w, |
| 683 | FRAME_COUNTS *counts) { |
| 684 | int k; |
| 685 | #if CONFIG_TILE_GROUPS |
| 686 | const int probwt = cm->num_tg; |
| 687 | #else |
| 688 | const int probwt = 1; |
| 689 | #endif |
| 690 | if (!cm->delta_lf_present_flag) return; |
| 691 | for (k = 0; k < DELTA_LF_PROBS; ++k) { |
| 692 | av1_cond_prob_diff_update(w, &cm->fc->delta_lf_prob[k], counts->delta_lf[k], |
| 693 | probwt); |
| 694 | } |
| 695 | } |
| 696 | #endif // CONFIG_EC_ADAPT |
| 697 | #endif // CONFIG_EXT_DELTA_Q |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 698 | #endif // CONFIG_DELTA_Q |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 699 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 700 | static void update_skip_probs(AV1_COMMON *cm, aom_writer *w, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 701 | FRAME_COUNTS *counts) { |
| 702 | int k; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 703 | #if CONFIG_TILE_GROUPS |
| 704 | const int probwt = cm->num_tg; |
| 705 | #else |
| 706 | const int probwt = 1; |
| 707 | #endif |
| 708 | for (k = 0; k < SKIP_CONTEXTS; ++k) { |
| 709 | av1_cond_prob_diff_update(w, &cm->fc->skip_probs[k], counts->skip[k], |
| 710 | probwt); |
| 711 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Nathan E. Egge | baaaa16 | 2016-10-24 09:50:52 -0400 | [diff] [blame] | 714 | #if !CONFIG_EC_ADAPT |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 715 | static void update_switchable_interp_probs(AV1_COMMON *cm, aom_writer *w, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 716 | FRAME_COUNTS *counts) { |
| 717 | int j; |
Nathan E. Egge | 4947c29 | 2016-04-26 11:37:06 -0400 | [diff] [blame] | 718 | for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) { |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 719 | #if CONFIG_TILE_GROUPS |
| 720 | const int probwt = cm->num_tg; |
| 721 | #else |
| 722 | const int probwt = 1; |
| 723 | #endif |
| 724 | prob_diff_update( |
| 725 | av1_switchable_interp_tree, cm->fc->switchable_interp_prob[j], |
| 726 | counts->switchable_interp[j], SWITCHABLE_FILTERS, probwt, w); |
Nathan E. Egge | 4947c29 | 2016-04-26 11:37:06 -0400 | [diff] [blame] | 727 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 728 | } |
Thomas Davies | 6519beb | 2016-10-19 14:46:07 +0100 | [diff] [blame] | 729 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 730 | |
Sarah Parker | e2e591c | 2017-03-03 15:21:19 -0800 | [diff] [blame] | 731 | #if !CONFIG_EC_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 732 | #if CONFIG_EXT_TX |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 733 | static void update_ext_tx_probs(AV1_COMMON *cm, aom_writer *w) { |
| 734 | const int savings_thresh = av1_cost_one(GROUP_DIFF_UPDATE_PROB) - |
| 735 | av1_cost_zero(GROUP_DIFF_UPDATE_PROB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 736 | int i, j; |
| 737 | int s; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 738 | #if CONFIG_TILE_GROUPS |
| 739 | const int probwt = cm->num_tg; |
| 740 | #else |
| 741 | const int probwt = 1; |
| 742 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 743 | for (s = 1; s < EXT_TX_SETS_INTER; ++s) { |
| 744 | int savings = 0; |
| 745 | int do_update = 0; |
| 746 | for (i = TX_4X4; i < EXT_TX_SIZES; ++i) { |
| 747 | if (!use_inter_ext_tx_for_txsize[s][i]) continue; |
| 748 | savings += prob_diff_update_savings( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 749 | av1_ext_tx_inter_tree[s], cm->fc->inter_ext_tx_prob[s][i], |
Debargha Mukherjee | 08542b9 | 2017-02-21 01:08:14 -0800 | [diff] [blame] | 750 | cm->counts.inter_ext_tx[s][i], |
| 751 | num_ext_tx_set[ext_tx_set_type_inter[s]], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 752 | } |
| 753 | do_update = savings > savings_thresh; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 754 | aom_write(w, do_update, GROUP_DIFF_UPDATE_PROB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 755 | if (do_update) { |
| 756 | for (i = TX_4X4; i < EXT_TX_SIZES; ++i) { |
| 757 | if (!use_inter_ext_tx_for_txsize[s][i]) continue; |
Debargha Mukherjee | 08542b9 | 2017-02-21 01:08:14 -0800 | [diff] [blame] | 758 | prob_diff_update(av1_ext_tx_inter_tree[s], |
| 759 | cm->fc->inter_ext_tx_prob[s][i], |
| 760 | cm->counts.inter_ext_tx[s][i], |
| 761 | num_ext_tx_set[ext_tx_set_type_inter[s]], probwt, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | for (s = 1; s < EXT_TX_SETS_INTRA; ++s) { |
| 767 | int savings = 0; |
| 768 | int do_update = 0; |
| 769 | for (i = TX_4X4; i < EXT_TX_SIZES; ++i) { |
| 770 | if (!use_intra_ext_tx_for_txsize[s][i]) continue; |
| 771 | for (j = 0; j < INTRA_MODES; ++j) |
| 772 | savings += prob_diff_update_savings( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 773 | av1_ext_tx_intra_tree[s], cm->fc->intra_ext_tx_prob[s][i][j], |
Debargha Mukherjee | 08542b9 | 2017-02-21 01:08:14 -0800 | [diff] [blame] | 774 | cm->counts.intra_ext_tx[s][i][j], |
| 775 | num_ext_tx_set[ext_tx_set_type_intra[s]], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 776 | } |
| 777 | do_update = savings > savings_thresh; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 778 | aom_write(w, do_update, GROUP_DIFF_UPDATE_PROB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 779 | if (do_update) { |
| 780 | for (i = TX_4X4; i < EXT_TX_SIZES; ++i) { |
| 781 | if (!use_intra_ext_tx_for_txsize[s][i]) continue; |
| 782 | for (j = 0; j < INTRA_MODES; ++j) |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 783 | prob_diff_update(av1_ext_tx_intra_tree[s], |
| 784 | cm->fc->intra_ext_tx_prob[s][i][j], |
| 785 | cm->counts.intra_ext_tx[s][i][j], |
Debargha Mukherjee | 08542b9 | 2017-02-21 01:08:14 -0800 | [diff] [blame] | 786 | num_ext_tx_set[ext_tx_set_type_intra[s]], probwt, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 793 | static void update_ext_tx_probs(AV1_COMMON *cm, aom_writer *w) { |
| 794 | const int savings_thresh = av1_cost_one(GROUP_DIFF_UPDATE_PROB) - |
| 795 | av1_cost_zero(GROUP_DIFF_UPDATE_PROB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 796 | int i, j; |
| 797 | |
| 798 | int savings = 0; |
| 799 | int do_update = 0; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 800 | #if CONFIG_TILE_GROUPS |
| 801 | const int probwt = cm->num_tg; |
| 802 | #else |
| 803 | const int probwt = 1; |
| 804 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 805 | for (i = TX_4X4; i < EXT_TX_SIZES; ++i) { |
| 806 | for (j = 0; j < TX_TYPES; ++j) |
| 807 | savings += prob_diff_update_savings( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 808 | av1_ext_tx_tree, cm->fc->intra_ext_tx_prob[i][j], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 809 | cm->counts.intra_ext_tx[i][j], TX_TYPES, probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 810 | } |
| 811 | do_update = savings > savings_thresh; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 812 | aom_write(w, do_update, GROUP_DIFF_UPDATE_PROB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 813 | if (do_update) { |
| 814 | for (i = TX_4X4; i < EXT_TX_SIZES; ++i) { |
Nathan E. Egge | 7c5b4c1 | 2016-04-26 12:31:14 -0400 | [diff] [blame] | 815 | for (j = 0; j < TX_TYPES; ++j) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 816 | prob_diff_update(av1_ext_tx_tree, cm->fc->intra_ext_tx_prob[i][j], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 817 | cm->counts.intra_ext_tx[i][j], TX_TYPES, probwt, w); |
Nathan E. Egge | 7c5b4c1 | 2016-04-26 12:31:14 -0400 | [diff] [blame] | 818 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 819 | } |
| 820 | } |
Thomas Davies | 6519beb | 2016-10-19 14:46:07 +0100 | [diff] [blame] | 821 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 822 | savings = 0; |
| 823 | for (i = TX_4X4; i < EXT_TX_SIZES; ++i) { |
| 824 | savings += |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 825 | prob_diff_update_savings(av1_ext_tx_tree, cm->fc->inter_ext_tx_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 826 | cm->counts.inter_ext_tx[i], TX_TYPES, probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 827 | } |
| 828 | do_update = savings > savings_thresh; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 829 | aom_write(w, do_update, GROUP_DIFF_UPDATE_PROB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 830 | if (do_update) { |
| 831 | for (i = TX_4X4; i < EXT_TX_SIZES; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 832 | prob_diff_update(av1_ext_tx_tree, cm->fc->inter_ext_tx_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 833 | cm->counts.inter_ext_tx[i], TX_TYPES, probwt, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | } |
| 837 | #endif // CONFIG_EXT_TX |
Sarah Parker | e2e591c | 2017-03-03 15:21:19 -0800 | [diff] [blame] | 838 | #endif // !CONFIG_EC_ADAPT |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 839 | #if CONFIG_PALETTE |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 840 | static void pack_palette_tokens(aom_writer *w, const TOKENEXTRA **tp, int n, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 841 | int num) { |
| 842 | int i; |
| 843 | const TOKENEXTRA *p = *tp; |
| 844 | |
| 845 | for (i = 0; i < num; ++i) { |
Alex Converse | 9210981 | 2017-02-22 10:21:40 -0800 | [diff] [blame] | 846 | av1_write_token( |
| 847 | w, av1_palette_color_index_tree[n - PALETTE_MIN_SIZE], p->context_tree, |
| 848 | &palette_color_index_encodings[n - PALETTE_MIN_SIZE][p->token]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 849 | ++p; |
| 850 | } |
| 851 | |
| 852 | *tp = p; |
| 853 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 854 | #endif // CONFIG_PALETTE |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 855 | |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 856 | #if !CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 857 | #if CONFIG_SUPERTX |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 858 | static void update_supertx_probs(AV1_COMMON *cm, int probwt, aom_writer *w) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 859 | const int savings_thresh = av1_cost_one(GROUP_DIFF_UPDATE_PROB) - |
| 860 | av1_cost_zero(GROUP_DIFF_UPDATE_PROB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 861 | int i, j; |
| 862 | int savings = 0; |
| 863 | int do_update = 0; |
| 864 | for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) { |
Jingning Han | 5b7706a | 2016-12-21 09:55:10 -0800 | [diff] [blame] | 865 | for (j = TX_8X8; j < TX_SIZES; ++j) { |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 866 | savings += av1_cond_prob_diff_update_savings( |
| 867 | &cm->fc->supertx_prob[i][j], cm->counts.supertx[i][j], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | do_update = savings > savings_thresh; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 871 | aom_write(w, do_update, GROUP_DIFF_UPDATE_PROB); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 872 | if (do_update) { |
| 873 | for (i = 0; i < PARTITION_SUPERTX_CONTEXTS; ++i) { |
Jingning Han | 5b7706a | 2016-12-21 09:55:10 -0800 | [diff] [blame] | 874 | for (j = TX_8X8; j < TX_SIZES; ++j) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 875 | av1_cond_prob_diff_update(w, &cm->fc->supertx_prob[i][j], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 876 | cm->counts.supertx[i][j], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | #endif // CONFIG_SUPERTX |
| 882 | |
Thomas Davies | 9b8393f | 2017-03-21 11:04:08 +0000 | [diff] [blame] | 883 | #if CONFIG_NEW_MULTISYMBOL |
| 884 | static INLINE void write_coeff_extra(const aom_cdf_prob *const *cdf, int val, |
| 885 | int n, aom_writer *w) { |
| 886 | // Code the extra bits from LSB to MSB in groups of 4 |
| 887 | int i = 0; |
| 888 | int count = 0; |
| 889 | while (count < n) { |
| 890 | const int size = AOMMIN(n - count, 4); |
| 891 | const int mask = (1 << size) - 1; |
| 892 | aom_write_cdf(w, val & mask, cdf[i++], 1 << size); |
| 893 | val >>= size; |
| 894 | count += size; |
| 895 | } |
| 896 | } |
| 897 | #else |
| 898 | static INLINE void write_coeff_extra(const aom_prob *pb, int value, |
| 899 | int num_bits, int skip_bits, aom_writer *w, |
| 900 | TOKEN_STATS *token_stats) { |
| 901 | // Code the extra bits from MSB to LSB 1 bit at a time |
| 902 | int index; |
| 903 | for (index = skip_bits; index < num_bits; ++index) { |
| 904 | const int shift = num_bits - index - 1; |
| 905 | const int bb = (value >> shift) & 1; |
| 906 | aom_write_record(w, bb, pb[index], token_stats); |
| 907 | } |
| 908 | } |
| 909 | #endif |
| 910 | |
Thomas Davies | eb64972 | 2017-03-31 16:12:57 +0100 | [diff] [blame] | 911 | #if CONFIG_NEW_TOKENSET && !CONFIG_LV_MAP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 912 | static void pack_mb_tokens(aom_writer *w, const TOKENEXTRA **tp, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 913 | const TOKENEXTRA *const stop, |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 914 | aom_bit_depth_t bit_depth, const TX_SIZE tx_size, |
| 915 | TOKEN_STATS *token_stats) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 916 | const TOKENEXTRA *p = *tp; |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 917 | #if CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 918 | int count = 0; |
Jingning Han | 7e99297 | 2016-10-31 11:03:06 -0700 | [diff] [blame] | 919 | const int seg_eob = tx_size_2d[tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 920 | #endif |
| 921 | |
| 922 | while (p < stop && p->token != EOSB_TOKEN) { |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 923 | const int token = p->token; |
Thomas Davies | fc1598a | 2017-01-13 17:07:25 +0000 | [diff] [blame] | 924 | if (token == BLOCK_Z_TOKEN) { |
Thomas Davies | 1c05c63 | 2017-03-15 09:58:34 +0000 | [diff] [blame] | 925 | aom_write_symbol(w, 0, *p->head_cdf, HEAD_TOKENS + 1); |
Thomas Davies | fc1598a | 2017-01-13 17:07:25 +0000 | [diff] [blame] | 926 | p++; |
| 927 | continue; |
| 928 | } |
Yaowu Xu | c8ab0bc | 2017-04-11 21:53:22 -0700 | [diff] [blame] | 929 | |
| 930 | const av1_extra_bit *const extra_bits = &av1_extra_bits[token]; |
Thomas Davies | 04bdd52 | 2017-03-13 22:34:14 +0000 | [diff] [blame] | 931 | if (p->eob_val == LAST_EOB) { |
| 932 | // Just code a flag indicating whether the value is >1 or 1. |
| 933 | aom_write_bit(w, token != ONE_TOKEN); |
| 934 | } else { |
Thomas Davies | 1c05c63 | 2017-03-15 09:58:34 +0000 | [diff] [blame] | 935 | int comb_symb = 2 * AOMMIN(token, TWO_TOKEN) - p->eob_val + p->first_val; |
| 936 | aom_write_symbol(w, comb_symb, *p->head_cdf, HEAD_TOKENS + p->first_val); |
Thomas Davies | 04bdd52 | 2017-03-13 22:34:14 +0000 | [diff] [blame] | 937 | } |
Thomas Davies | fc1598a | 2017-01-13 17:07:25 +0000 | [diff] [blame] | 938 | if (token > ONE_TOKEN) { |
Thomas Davies | 1c05c63 | 2017-03-15 09:58:34 +0000 | [diff] [blame] | 939 | aom_write_symbol(w, token - TWO_TOKEN, *p->tail_cdf, TAIL_TOKENS); |
Alex Converse | dc62b09 | 2016-10-11 16:50:56 -0700 | [diff] [blame] | 940 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 941 | |
Alex Converse | d8fdfaa | 2016-07-26 16:27:51 -0700 | [diff] [blame] | 942 | if (extra_bits->base_val) { |
| 943 | const int bit_string = p->extra; |
| 944 | const int bit_string_length = extra_bits->len; // Length of extra bits to |
Thomas Davies | 9b8393f | 2017-03-21 11:04:08 +0000 | [diff] [blame] | 945 | const int is_cat6 = (extra_bits->base_val == CAT6_MIN_VAL); |
Thomas Davies | fc1598a | 2017-01-13 17:07:25 +0000 | [diff] [blame] | 946 | // be written excluding |
| 947 | // the sign bit. |
Thomas Davies | 9b8393f | 2017-03-21 11:04:08 +0000 | [diff] [blame] | 948 | int skip_bits = is_cat6 |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 949 | ? (int)sizeof(av1_cat6_prob) - |
| 950 | av1_get_cat6_extrabits_size(tx_size, bit_depth) |
| 951 | : 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 952 | |
Thomas Davies | 9b8393f | 2017-03-21 11:04:08 +0000 | [diff] [blame] | 953 | assert(!(bit_string >> (bit_string_length - skip_bits + 1))); |
| 954 | if (bit_string_length > 0) |
| 955 | #if CONFIG_NEW_MULTISYMBOL |
| 956 | write_coeff_extra(extra_bits->cdf, bit_string >> 1, |
| 957 | bit_string_length - skip_bits, w); |
| 958 | #else |
| 959 | write_coeff_extra(extra_bits->prob, bit_string >> 1, bit_string_length, |
| 960 | skip_bits, w, token_stats); |
| 961 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 962 | |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 963 | aom_write_bit_record(w, bit_string & 1, token_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 964 | } |
| 965 | ++p; |
| 966 | |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 967 | #if CONFIG_VAR_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 968 | ++count; |
Urvang Joshi | 454280d | 2016-10-14 16:51:44 -0700 | [diff] [blame] | 969 | if (token == EOB_TOKEN || count == seg_eob) break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 970 | #endif |
| 971 | } |
| 972 | |
| 973 | *tp = p; |
| 974 | } |
Angie Chiang | c8af611 | 2017-03-16 16:11:22 -0700 | [diff] [blame] | 975 | #else // CONFIG_NEW_TOKENSET |
| 976 | #if !CONFIG_LV_MAP |
Thomas Davies | f639032 | 2017-01-13 14:31:43 +0000 | [diff] [blame] | 977 | static void pack_mb_tokens(aom_writer *w, const TOKENEXTRA **tp, |
| 978 | const TOKENEXTRA *const stop, |
| 979 | aom_bit_depth_t bit_depth, const TX_SIZE tx_size, |
| 980 | TOKEN_STATS *token_stats) { |
| 981 | const TOKENEXTRA *p = *tp; |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 982 | #if CONFIG_VAR_TX |
Thomas Davies | f639032 | 2017-01-13 14:31:43 +0000 | [diff] [blame] | 983 | int count = 0; |
| 984 | const int seg_eob = tx_size_2d[tx_size]; |
| 985 | #endif |
Thomas Davies | f639032 | 2017-01-13 14:31:43 +0000 | [diff] [blame] | 986 | |
| 987 | while (p < stop && p->token != EOSB_TOKEN) { |
| 988 | const int token = p->token; |
Alex Converse | a9598cd | 2017-02-03 14:18:05 -0800 | [diff] [blame] | 989 | #if !CONFIG_EC_MULTISYMBOL |
Thomas Davies | f639032 | 2017-01-13 14:31:43 +0000 | [diff] [blame] | 990 | const struct av1_token *const coef_encoding = &av1_coef_encodings[token]; |
| 991 | int coef_value = coef_encoding->value; |
| 992 | int coef_length = coef_encoding->len; |
Alex Converse | a9598cd | 2017-02-03 14:18:05 -0800 | [diff] [blame] | 993 | #endif // !CONFIG_EC_MULTISYMBOL |
Alex Converse | e063e2d | 2017-03-15 12:35:24 -0700 | [diff] [blame] | 994 | const av1_extra_bit *const extra_bits = &av1_extra_bits[token]; |
Thomas Davies | f639032 | 2017-01-13 14:31:43 +0000 | [diff] [blame] | 995 | |
Alex Converse | a9598cd | 2017-02-03 14:18:05 -0800 | [diff] [blame] | 996 | #if CONFIG_EC_MULTISYMBOL |
| 997 | /* skip one or two nodes */ |
| 998 | if (!p->skip_eob_node) |
| 999 | aom_write_record(w, token != EOB_TOKEN, p->context_tree[0], token_stats); |
| 1000 | if (token != EOB_TOKEN) { |
| 1001 | aom_write_record(w, token != ZERO_TOKEN, p->context_tree[1], token_stats); |
| 1002 | if (token != ZERO_TOKEN) { |
| 1003 | aom_write_symbol(w, token - ONE_TOKEN, *p->token_cdf, |
| 1004 | CATEGORY6_TOKEN - ONE_TOKEN + 1); |
| 1005 | } |
| 1006 | } |
| 1007 | #else |
Thomas Davies | f639032 | 2017-01-13 14:31:43 +0000 | [diff] [blame] | 1008 | /* skip one or two nodes */ |
| 1009 | if (p->skip_eob_node) |
| 1010 | coef_length -= p->skip_eob_node; |
| 1011 | else |
| 1012 | aom_write_record(w, token != EOB_TOKEN, p->context_tree[0], token_stats); |
| 1013 | |
| 1014 | if (token != EOB_TOKEN) { |
| 1015 | aom_write_record(w, token != ZERO_TOKEN, p->context_tree[1], token_stats); |
| 1016 | |
| 1017 | if (token != ZERO_TOKEN) { |
| 1018 | aom_write_record(w, token != ONE_TOKEN, p->context_tree[2], |
| 1019 | token_stats); |
| 1020 | |
| 1021 | if (token != ONE_TOKEN) { |
| 1022 | const int unconstrained_len = UNCONSTRAINED_NODES - p->skip_eob_node; |
| 1023 | aom_write_tree_record( |
| 1024 | w, av1_coef_con_tree, |
| 1025 | av1_pareto8_full[p->context_tree[PIVOT_NODE] - 1], coef_value, |
| 1026 | coef_length - unconstrained_len, 0, token_stats); |
| 1027 | } |
| 1028 | } |
| 1029 | } |
Alex Converse | a9598cd | 2017-02-03 14:18:05 -0800 | [diff] [blame] | 1030 | #endif // CONFIG_EC_MULTISYMBOL |
Thomas Davies | f639032 | 2017-01-13 14:31:43 +0000 | [diff] [blame] | 1031 | |
| 1032 | if (extra_bits->base_val) { |
| 1033 | const int bit_string = p->extra; |
| 1034 | const int bit_string_length = extra_bits->len; // Length of extra bits to |
Thomas Davies | 9b8393f | 2017-03-21 11:04:08 +0000 | [diff] [blame] | 1035 | // be written excluding |
| 1036 | // the sign bit. |
Alex Converse | da3d94f | 2017-03-15 14:54:29 -0700 | [diff] [blame] | 1037 | int skip_bits = (extra_bits->base_val == CAT6_MIN_VAL) |
| 1038 | ? (int)sizeof(av1_cat6_prob) - |
| 1039 | av1_get_cat6_extrabits_size(tx_size, bit_depth) |
| 1040 | : 0; |
Thomas Davies | 9b8393f | 2017-03-21 11:04:08 +0000 | [diff] [blame] | 1041 | |
| 1042 | assert(!(bit_string >> (bit_string_length - skip_bits + 1))); |
Thomas Davies | f639032 | 2017-01-13 14:31:43 +0000 | [diff] [blame] | 1043 | if (bit_string_length > 0) { |
Thomas Davies | 9b8393f | 2017-03-21 11:04:08 +0000 | [diff] [blame] | 1044 | #if CONFIG_NEW_MULTISYMBOL |
| 1045 | skip_bits &= ~3; |
| 1046 | write_coeff_extra(extra_bits->cdf, bit_string >> 1, |
| 1047 | bit_string_length - skip_bits, w); |
| 1048 | #else |
| 1049 | write_coeff_extra(extra_bits->prob, bit_string >> 1, bit_string_length, |
| 1050 | skip_bits, w, token_stats); |
| 1051 | #endif |
Thomas Davies | f639032 | 2017-01-13 14:31:43 +0000 | [diff] [blame] | 1052 | } |
Thomas Davies | f639032 | 2017-01-13 14:31:43 +0000 | [diff] [blame] | 1053 | aom_write_bit_record(w, bit_string & 1, token_stats); |
| 1054 | } |
| 1055 | ++p; |
| 1056 | |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 1057 | #if CONFIG_VAR_TX |
Thomas Davies | f639032 | 2017-01-13 14:31:43 +0000 | [diff] [blame] | 1058 | ++count; |
| 1059 | if (token == EOB_TOKEN || count == seg_eob) break; |
| 1060 | #endif |
| 1061 | } |
| 1062 | |
| 1063 | *tp = p; |
| 1064 | } |
Angie Chiang | c8af611 | 2017-03-16 16:11:22 -0700 | [diff] [blame] | 1065 | #endif // !CONFIG_LV_MAP |
| 1066 | #endif // CONFIG_NEW_TOKENSET |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1067 | #else // !CONFIG_PVQ |
| 1068 | static PVQ_INFO *get_pvq_block(PVQ_QUEUE *pvq_q) { |
| 1069 | PVQ_INFO *pvq; |
| 1070 | |
| 1071 | assert(pvq_q->curr_pos <= pvq_q->last_pos); |
| 1072 | assert(pvq_q->curr_pos < pvq_q->buf_len); |
| 1073 | |
| 1074 | pvq = pvq_q->buf + pvq_q->curr_pos; |
| 1075 | ++pvq_q->curr_pos; |
| 1076 | |
| 1077 | return pvq; |
| 1078 | } |
| 1079 | |
| 1080 | static void pack_pvq_tokens(aom_writer *w, MACROBLOCK *const x, |
| 1081 | MACROBLOCKD *const xd, int plane, BLOCK_SIZE bsize, |
| 1082 | const TX_SIZE tx_size) { |
| 1083 | PVQ_INFO *pvq; |
| 1084 | int idx, idy; |
| 1085 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1086 | od_adapt_ctx *adapt; |
| 1087 | int max_blocks_wide; |
| 1088 | int max_blocks_high; |
| 1089 | int step = (1 << tx_size); |
| 1090 | const BLOCK_SIZE plane_bsize = |
| 1091 | get_plane_block_size(AOMMAX(bsize, BLOCK_8X8), pd); |
| 1092 | |
Yushin Cho | c49ef3a | 2017-03-13 17:27:25 -0700 | [diff] [blame] | 1093 | adapt = x->daala_enc.state.adapt; |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1094 | |
| 1095 | max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
| 1096 | max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 1097 | |
| 1098 | for (idy = 0; idy < max_blocks_high; idy += step) { |
| 1099 | for (idx = 0; idx < max_blocks_wide; idx += step) { |
| 1100 | const int is_keyframe = 0; |
| 1101 | const int encode_flip = 0; |
| 1102 | const int flip = 0; |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1103 | int i; |
| 1104 | const int has_dc_skip = 1; |
| 1105 | int *exg = &adapt->pvq.pvq_exg[plane][tx_size][0]; |
| 1106 | int *ext = adapt->pvq.pvq_ext + tx_size * PVQ_MAX_PARTITIONS; |
| 1107 | generic_encoder *model = adapt->pvq.pvq_param_model; |
| 1108 | |
| 1109 | pvq = get_pvq_block(x->pvq_q); |
| 1110 | |
| 1111 | // encode block skip info |
Yushin Cho | 0077927 | 2017-02-21 10:38:16 -0800 | [diff] [blame] | 1112 | aom_write_symbol(w, pvq->ac_dc_coded, |
| 1113 | adapt->skip_cdf[2 * tx_size + (plane != 0)], 4); |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1114 | |
| 1115 | // AC coeffs coded? |
| 1116 | if (pvq->ac_dc_coded & AC_CODED) { |
| 1117 | assert(pvq->bs == tx_size); |
| 1118 | for (i = 0; i < pvq->nb_bands; i++) { |
| 1119 | if (i == 0 || |
| 1120 | (!pvq->skip_rest && !(pvq->skip_dir & (1 << ((i - 1) % 3))))) { |
| 1121 | pvq_encode_partition( |
Timothy B. Terriberry | 44bb6d0 | 2017-04-07 15:44:14 -0700 | [diff] [blame] | 1122 | w, pvq->qg[i], pvq->theta[i], pvq->y + pvq->off[i], |
| 1123 | pvq->size[i], pvq->k[i], model, adapt, exg + i, ext + i, |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1124 | (plane != 0) * OD_TXSIZES * PVQ_MAX_PARTITIONS + |
| 1125 | pvq->bs * PVQ_MAX_PARTITIONS + i, |
| 1126 | is_keyframe, i == 0 && (i < pvq->nb_bands - 1), pvq->skip_rest, |
| 1127 | encode_flip, flip); |
| 1128 | } |
| 1129 | if (i == 0 && !pvq->skip_rest && pvq->bs > 0) { |
Yushin Cho | ac4535c | 2017-02-21 14:59:46 -0800 | [diff] [blame] | 1130 | aom_write_symbol( |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1131 | w, pvq->skip_dir, |
| 1132 | &adapt->pvq |
| 1133 | .pvq_skip_dir_cdf[(plane != 0) + 2 * (pvq->bs - 1)][0], |
Yushin Cho | ac4535c | 2017-02-21 14:59:46 -0800 | [diff] [blame] | 1134 | 7); |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | // Encode residue of DC coeff, if exist. |
| 1139 | if (!has_dc_skip || (pvq->ac_dc_coded & DC_CODED)) { |
| 1140 | generic_encode(w, &adapt->model_dc[plane], |
Timothy B. Terriberry | 44bb6d0 | 2017-04-07 15:44:14 -0700 | [diff] [blame] | 1141 | abs(pvq->dq_dc_residue) - has_dc_skip, |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1142 | &adapt->ex_dc[plane][pvq->bs][0], 2); |
| 1143 | } |
| 1144 | if ((pvq->ac_dc_coded & DC_CODED)) { |
| 1145 | aom_write_bit(w, pvq->dq_dc_residue < 0); |
| 1146 | } |
| 1147 | } |
| 1148 | } // for (idy = 0; |
| 1149 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1150 | #endif // !CONFIG_PVG |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1151 | |
| 1152 | #if CONFIG_VAR_TX && !CONFIG_COEF_INTERLEAVE |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1153 | static void pack_txb_tokens(aom_writer *w, const TOKENEXTRA **tp, |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1154 | const TOKENEXTRA *const tok_end, |
| 1155 | #if CONFIG_PVQ |
| 1156 | MACROBLOCK *const x, |
| 1157 | #endif |
| 1158 | MACROBLOCKD *xd, MB_MODE_INFO *mbmi, int plane, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1159 | BLOCK_SIZE plane_bsize, aom_bit_depth_t bit_depth, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1160 | int block, int blk_row, int blk_col, |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 1161 | TX_SIZE tx_size, TOKEN_STATS *token_stats) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1162 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
| 1163 | const BLOCK_SIZE bsize = txsize_to_bsize[tx_size]; |
| 1164 | const int tx_row = blk_row >> (1 - pd->subsampling_y); |
| 1165 | const int tx_col = blk_col >> (1 - pd->subsampling_x); |
| 1166 | TX_SIZE plane_tx_size; |
Jingning Han | f65b870 | 2016-10-31 12:13:20 -0700 | [diff] [blame] | 1167 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 1168 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1169 | |
| 1170 | if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return; |
| 1171 | |
Debargha Mukherjee | 2f12340 | 2016-08-30 17:43:38 -0700 | [diff] [blame] | 1172 | plane_tx_size = |
| 1173 | plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0] |
| 1174 | : mbmi->inter_tx_size[tx_row][tx_col]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1175 | |
| 1176 | if (tx_size == plane_tx_size) { |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 1177 | TOKEN_STATS tmp_token_stats; |
| 1178 | init_token_stats(&tmp_token_stats); |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1179 | #if !CONFIG_PVQ |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 1180 | pack_mb_tokens(w, tp, tok_end, bit_depth, tx_size, &tmp_token_stats); |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1181 | #else |
| 1182 | pack_pvq_tokens(w, x, xd, plane, bsize, tx_size); |
| 1183 | #endif |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 1184 | #if CONFIG_RD_DEBUG |
| 1185 | token_stats->txb_coeff_cost_map[blk_row][blk_col] = tmp_token_stats.cost; |
| 1186 | token_stats->cost += tmp_token_stats.cost; |
| 1187 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1188 | } else { |
Jingning Han | 1807fdc | 2016-11-08 15:17:58 -0800 | [diff] [blame] | 1189 | const TX_SIZE sub_txs = sub_tx_size_map[tx_size]; |
| 1190 | const int bsl = tx_size_wide_unit[sub_txs]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1191 | int i; |
| 1192 | |
| 1193 | assert(bsl > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1194 | |
| 1195 | for (i = 0; i < 4; ++i) { |
Jingning Han | 42a0fb3 | 2016-10-31 10:43:31 -0700 | [diff] [blame] | 1196 | const int offsetr = blk_row + (i >> 1) * bsl; |
| 1197 | const int offsetc = blk_col + (i & 0x01) * bsl; |
Jingning Han | 42a0fb3 | 2016-10-31 10:43:31 -0700 | [diff] [blame] | 1198 | const int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1199 | |
| 1200 | if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue; |
| 1201 | |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 1202 | pack_txb_tokens(w, tp, tok_end, |
| 1203 | #if CONFIG_PVQ |
| 1204 | x, |
| 1205 | #endif |
| 1206 | xd, mbmi, plane, plane_bsize, bit_depth, block, offsetr, |
| 1207 | offsetc, sub_txs, token_stats); |
Jingning Han | 98d6a1f | 2016-11-03 12:47:47 -0700 | [diff] [blame] | 1208 | block += step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | #endif |
| 1213 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1214 | static void write_segment_id(aom_writer *w, const struct segmentation *seg, |
Thomas | 9ac5508 | 2016-09-23 18:04:17 +0100 | [diff] [blame] | 1215 | struct segmentation_probs *segp, int segment_id) { |
Nathan E. Egge | f627e58 | 2016-08-19 20:06:51 -0400 | [diff] [blame] | 1216 | if (seg->enabled && seg->update_map) { |
Nathan E. Egge | 3129606 | 2016-11-16 09:44:26 -0500 | [diff] [blame] | 1217 | #if CONFIG_EC_MULTISYMBOL |
Nathan E. Egge | f627e58 | 2016-08-19 20:06:51 -0400 | [diff] [blame] | 1218 | aom_write_symbol(w, segment_id, segp->tree_cdf, MAX_SEGMENTS); |
| 1219 | #else |
Nathan E. Egge | eeedc63 | 2016-06-19 12:02:33 -0400 | [diff] [blame] | 1220 | aom_write_tree(w, av1_segment_tree, segp->tree_probs, segment_id, 3, 0); |
Nathan E. Egge | f627e58 | 2016-08-19 20:06:51 -0400 | [diff] [blame] | 1221 | #endif |
| 1222 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | // This function encodes the reference frame |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1226 | static void write_ref_frames(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
| 1227 | aom_writer *w) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1228 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 1229 | const int is_compound = has_second_ref(mbmi); |
| 1230 | const int segment_id = mbmi->segment_id; |
| 1231 | |
| 1232 | // If segment level coding of this signal is disabled... |
| 1233 | // or the segment allows multiple reference frame options |
| 1234 | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) { |
| 1235 | assert(!is_compound); |
| 1236 | assert(mbmi->ref_frame[0] == |
| 1237 | get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME)); |
| 1238 | } else { |
| 1239 | // does the feature use compound prediction or not |
| 1240 | // (if not specified at the frame/segment level) |
| 1241 | if (cm->reference_mode == REFERENCE_MODE_SELECT) { |
Jingning Han | c41a549 | 2017-02-24 11:18:52 -0800 | [diff] [blame] | 1242 | #if SUB8X8_COMP_REF |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1243 | aom_write(w, is_compound, av1_get_reference_mode_prob(cm, xd)); |
Jingning Han | c41a549 | 2017-02-24 11:18:52 -0800 | [diff] [blame] | 1244 | #else |
| 1245 | if (mbmi->sb_type >= BLOCK_8X8) |
| 1246 | aom_write(w, is_compound, av1_get_reference_mode_prob(cm, xd)); |
| 1247 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1248 | } else { |
| 1249 | assert((!is_compound) == (cm->reference_mode == SINGLE_REFERENCE)); |
| 1250 | } |
| 1251 | |
| 1252 | if (is_compound) { |
| 1253 | #if CONFIG_EXT_REFS |
| 1254 | const int bit = (mbmi->ref_frame[0] == GOLDEN_FRAME || |
| 1255 | mbmi->ref_frame[0] == LAST3_FRAME); |
| 1256 | const int bit_bwd = mbmi->ref_frame[1] == ALTREF_FRAME; |
| 1257 | #else // CONFIG_EXT_REFS |
| 1258 | const int bit = mbmi->ref_frame[0] == GOLDEN_FRAME; |
| 1259 | #endif // CONFIG_EXT_REFS |
| 1260 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1261 | aom_write(w, bit, av1_get_pred_prob_comp_ref_p(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1262 | |
| 1263 | #if CONFIG_EXT_REFS |
| 1264 | if (!bit) { |
| 1265 | const int bit1 = mbmi->ref_frame[0] == LAST_FRAME; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1266 | aom_write(w, bit1, av1_get_pred_prob_comp_ref_p1(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1267 | } else { |
| 1268 | const int bit2 = mbmi->ref_frame[0] == GOLDEN_FRAME; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1269 | aom_write(w, bit2, av1_get_pred_prob_comp_ref_p2(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1270 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1271 | aom_write(w, bit_bwd, av1_get_pred_prob_comp_bwdref_p(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1272 | #endif // CONFIG_EXT_REFS |
| 1273 | } else { |
| 1274 | #if CONFIG_EXT_REFS |
| 1275 | const int bit0 = (mbmi->ref_frame[0] == ALTREF_FRAME || |
| 1276 | mbmi->ref_frame[0] == BWDREF_FRAME); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1277 | aom_write(w, bit0, av1_get_pred_prob_single_ref_p1(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1278 | |
| 1279 | if (bit0) { |
| 1280 | const int bit1 = mbmi->ref_frame[0] == ALTREF_FRAME; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1281 | aom_write(w, bit1, av1_get_pred_prob_single_ref_p2(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1282 | } else { |
| 1283 | const int bit2 = (mbmi->ref_frame[0] == LAST3_FRAME || |
| 1284 | mbmi->ref_frame[0] == GOLDEN_FRAME); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1285 | aom_write(w, bit2, av1_get_pred_prob_single_ref_p3(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1286 | |
| 1287 | if (!bit2) { |
| 1288 | const int bit3 = mbmi->ref_frame[0] != LAST_FRAME; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1289 | aom_write(w, bit3, av1_get_pred_prob_single_ref_p4(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1290 | } else { |
| 1291 | const int bit4 = mbmi->ref_frame[0] != LAST3_FRAME; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1292 | aom_write(w, bit4, av1_get_pred_prob_single_ref_p5(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1293 | } |
| 1294 | } |
| 1295 | #else // CONFIG_EXT_REFS |
| 1296 | const int bit0 = mbmi->ref_frame[0] != LAST_FRAME; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1297 | aom_write(w, bit0, av1_get_pred_prob_single_ref_p1(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1298 | |
| 1299 | if (bit0) { |
| 1300 | const int bit1 = mbmi->ref_frame[0] != GOLDEN_FRAME; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1301 | aom_write(w, bit1, av1_get_pred_prob_single_ref_p2(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1302 | } |
| 1303 | #endif // CONFIG_EXT_REFS |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 1308 | #if CONFIG_FILTER_INTRA |
| 1309 | static void write_filter_intra_mode_info(const AV1_COMMON *const cm, |
| 1310 | const MB_MODE_INFO *const mbmi, |
| 1311 | aom_writer *w) { |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 1312 | if (mbmi->mode == DC_PRED |
| 1313 | #if CONFIG_PALETTE |
| 1314 | && mbmi->palette_mode_info.palette_size[0] == 0 |
| 1315 | #endif // CONFIG_PALETTE |
| 1316 | ) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 1317 | aom_write(w, mbmi->filter_intra_mode_info.use_filter_intra_mode[0], |
| 1318 | cm->fc->filter_intra_probs[0]); |
| 1319 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[0]) { |
| 1320 | const FILTER_INTRA_MODE mode = |
| 1321 | mbmi->filter_intra_mode_info.filter_intra_mode[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1322 | write_uniform(w, FILTER_INTRA_MODES, mode); |
| 1323 | } |
| 1324 | } |
| 1325 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 1326 | if (mbmi->uv_mode == DC_PRED |
| 1327 | #if CONFIG_PALETTE |
| 1328 | && mbmi->palette_mode_info.palette_size[1] == 0 |
| 1329 | #endif // CONFIG_PALETTE |
| 1330 | ) { |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 1331 | aom_write(w, mbmi->filter_intra_mode_info.use_filter_intra_mode[1], |
| 1332 | cm->fc->filter_intra_probs[1]); |
| 1333 | if (mbmi->filter_intra_mode_info.use_filter_intra_mode[1]) { |
| 1334 | const FILTER_INTRA_MODE mode = |
| 1335 | mbmi->filter_intra_mode_info.filter_intra_mode[1]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1336 | write_uniform(w, FILTER_INTRA_MODES, mode); |
| 1337 | } |
| 1338 | } |
| 1339 | } |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 1340 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1341 | |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 1342 | #if CONFIG_EXT_INTRA |
hui su | b4e25d2 | 2017-03-09 15:32:30 -0800 | [diff] [blame] | 1343 | static void write_intra_angle_info(const MACROBLOCKD *xd, |
| 1344 | FRAME_CONTEXT *const ec_ctx, aom_writer *w) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1345 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
| 1346 | const BLOCK_SIZE bsize = mbmi->sb_type; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 1347 | #if CONFIG_INTRA_INTERP |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1348 | const int intra_filter_ctx = av1_get_pred_context_intra_interp(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1349 | int p_angle; |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 1350 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1351 | |
hui su | b4e25d2 | 2017-03-09 15:32:30 -0800 | [diff] [blame] | 1352 | (void)ec_ctx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1353 | if (bsize < BLOCK_8X8) return; |
| 1354 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 1355 | if (av1_is_directional_mode(mbmi->mode, bsize)) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 1356 | write_uniform(w, 2 * MAX_ANGLE_DELTA + 1, |
| 1357 | MAX_ANGLE_DELTA + mbmi->angle_delta[0]); |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 1358 | #if CONFIG_INTRA_INTERP |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 1359 | p_angle = mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1360 | if (av1_is_intra_filter_switchable(p_angle)) { |
hui su | b4e25d2 | 2017-03-09 15:32:30 -0800 | [diff] [blame] | 1361 | #if CONFIG_EC_MULTISYMBOL |
| 1362 | aom_write_symbol(w, mbmi->intra_filter, |
| 1363 | ec_ctx->intra_filter_cdf[intra_filter_ctx], |
| 1364 | INTRA_FILTERS); |
| 1365 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1366 | av1_write_token(w, av1_intra_filter_tree, |
hui su | b4e25d2 | 2017-03-09 15:32:30 -0800 | [diff] [blame] | 1367 | ec_ctx->intra_filter_probs[intra_filter_ctx], |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1368 | &intra_filter_encodings[mbmi->intra_filter]); |
hui su | b4e25d2 | 2017-03-09 15:32:30 -0800 | [diff] [blame] | 1369 | #endif // CONFIG_EC_MULTISYMBOL |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1370 | } |
hui su | eda3d76 | 2016-12-06 16:58:23 -0800 | [diff] [blame] | 1371 | #endif // CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
hui su | 45dc597 | 2016-12-08 17:42:50 -0800 | [diff] [blame] | 1374 | if (av1_is_directional_mode(mbmi->uv_mode, bsize)) { |
hui su | 0a6731f | 2017-04-26 15:23:47 -0700 | [diff] [blame] | 1375 | write_uniform(w, 2 * MAX_ANGLE_DELTA + 1, |
| 1376 | MAX_ANGLE_DELTA + mbmi->angle_delta[1]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | #endif // CONFIG_EXT_INTRA |
| 1380 | |
Angie Chiang | 5678ad9 | 2016-11-21 09:38:40 -0800 | [diff] [blame] | 1381 | static void write_mb_interp_filter(AV1_COMP *cpi, const MACROBLOCKD *xd, |
| 1382 | aom_writer *w) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1383 | AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1384 | const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi; |
Thomas Davies | 77c7c40 | 2017-01-11 17:58:54 +0000 | [diff] [blame] | 1385 | #if CONFIG_EC_ADAPT |
| 1386 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 1387 | #else |
| 1388 | FRAME_CONTEXT *ec_ctx = cm->fc; |
| 1389 | #endif |
Jingning Han | 203b1d3 | 2017-01-12 16:00:13 -0800 | [diff] [blame] | 1390 | |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 1391 | if (!av1_is_interp_needed(xd)) { |
| 1392 | #if CONFIG_DUAL_FILTER |
| 1393 | for (int i = 0; i < 4; ++i) |
| 1394 | assert(mbmi->interp_filter[i] == (cm->interp_filter == SWITCHABLE |
| 1395 | ? EIGHTTAP_REGULAR |
| 1396 | : cm->interp_filter)); |
| 1397 | #else |
| 1398 | assert(mbmi->interp_filter == (cm->interp_filter == SWITCHABLE |
| 1399 | ? EIGHTTAP_REGULAR |
| 1400 | : cm->interp_filter)); |
| 1401 | #endif // CONFIG_DUAL_FILTER |
| 1402 | return; |
| 1403 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1404 | if (cm->interp_filter == SWITCHABLE) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1405 | #if CONFIG_DUAL_FILTER |
Jingning Han | 203b1d3 | 2017-01-12 16:00:13 -0800 | [diff] [blame] | 1406 | int dir; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1407 | for (dir = 0; dir < 2; ++dir) { |
| 1408 | if (has_subpel_mv_component(xd->mi[0], xd, dir) || |
| 1409 | (mbmi->ref_frame[1] > INTRA_FRAME && |
| 1410 | has_subpel_mv_component(xd->mi[0], xd, dir + 2))) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1411 | const int ctx = av1_get_pred_context_switchable_interp(xd, dir); |
Angie Chiang | b9b42a0 | 2017-01-20 12:47:36 -0800 | [diff] [blame] | 1412 | #if CONFIG_EC_MULTISYMBOL |
| 1413 | aom_write_symbol(w, av1_switchable_interp_ind[mbmi->interp_filter[dir]], |
| 1414 | ec_ctx->switchable_interp_cdf[ctx], |
| 1415 | SWITCHABLE_FILTERS); |
| 1416 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1417 | av1_write_token(w, av1_switchable_interp_tree, |
Thomas Davies | 77c7c40 | 2017-01-11 17:58:54 +0000 | [diff] [blame] | 1418 | ec_ctx->switchable_interp_prob[ctx], |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1419 | &switchable_interp_encodings[mbmi->interp_filter[dir]]); |
Angie Chiang | b9b42a0 | 2017-01-20 12:47:36 -0800 | [diff] [blame] | 1420 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1421 | ++cpi->interp_filter_selected[0][mbmi->interp_filter[dir]]; |
Angie Chiang | 38edf68 | 2017-02-21 15:13:09 -0800 | [diff] [blame] | 1422 | } else { |
| 1423 | assert(mbmi->interp_filter[dir] == EIGHTTAP_REGULAR); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1424 | } |
| 1425 | } |
| 1426 | #else |
| 1427 | { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1428 | const int ctx = av1_get_pred_context_switchable_interp(xd); |
Nathan E. Egge | 00b3331 | 2016-11-16 09:44:26 -0500 | [diff] [blame] | 1429 | #if CONFIG_EC_MULTISYMBOL |
Nathan E. Egge | 56eeaa5 | 2016-07-25 10:23:33 -0400 | [diff] [blame] | 1430 | aom_write_symbol(w, av1_switchable_interp_ind[mbmi->interp_filter], |
Thomas Davies | 77c7c40 | 2017-01-11 17:58:54 +0000 | [diff] [blame] | 1431 | ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS); |
Nathan E. Egge | 4947c29 | 2016-04-26 11:37:06 -0400 | [diff] [blame] | 1432 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1433 | av1_write_token(w, av1_switchable_interp_tree, |
Thomas Davies | 77c7c40 | 2017-01-11 17:58:54 +0000 | [diff] [blame] | 1434 | ec_ctx->switchable_interp_prob[ctx], |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1435 | &switchable_interp_encodings[mbmi->interp_filter]); |
Nathan E. Egge | 4947c29 | 2016-04-26 11:37:06 -0400 | [diff] [blame] | 1436 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1437 | ++cpi->interp_filter_selected[0][mbmi->interp_filter]; |
| 1438 | } |
Jingning Han | 203b1d3 | 2017-01-12 16:00:13 -0800 | [diff] [blame] | 1439 | #endif // CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1440 | } |
| 1441 | } |
| 1442 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 1443 | #if CONFIG_PALETTE |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 1444 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 1445 | // Write luma palette color values with delta encoding. Write the first value as |
| 1446 | // literal, and the deltas between each value and the previous one. The luma |
| 1447 | // palette is sorted so each delta is larger than 0. |
| 1448 | static void write_palette_colors_y(const PALETTE_MODE_INFO *const pmi, |
| 1449 | int bit_depth, aom_writer *w) { |
| 1450 | const int n = pmi->palette_size[0]; |
| 1451 | int min_bits, i; |
| 1452 | int bits = av1_get_palette_delta_bits_y(pmi, bit_depth, &min_bits); |
| 1453 | aom_write_literal(w, bits - min_bits, 2); |
| 1454 | aom_write_literal(w, pmi->palette_colors[0], bit_depth); |
| 1455 | for (i = 1; i < n; ++i) { |
| 1456 | aom_write_literal( |
| 1457 | w, pmi->palette_colors[i] - pmi->palette_colors[i - 1] - 1, bits); |
| 1458 | bits = |
| 1459 | AOMMIN(bits, av1_ceil_log2((1 << bit_depth) - pmi->palette_colors[i])); |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | // Write chroma palette color values. Use delta encoding for u channel as its |
| 1464 | // palette is sorted. For v channel, either use delta encoding or transmit |
| 1465 | // raw values directly, whichever costs less. |
| 1466 | static void write_palette_colors_uv(const PALETTE_MODE_INFO *const pmi, |
| 1467 | int bit_depth, aom_writer *w) { |
| 1468 | int i; |
| 1469 | const int n = pmi->palette_size[1]; |
| 1470 | #if CONFIG_HIGHBITDEPTH |
| 1471 | const uint16_t *colors_u = pmi->palette_colors + PALETTE_MAX_SIZE; |
| 1472 | const uint16_t *colors_v = pmi->palette_colors + 2 * PALETTE_MAX_SIZE; |
| 1473 | #else |
| 1474 | const uint8_t *colors_u = pmi->palette_colors + PALETTE_MAX_SIZE; |
| 1475 | const uint8_t *colors_v = pmi->palette_colors + 2 * PALETTE_MAX_SIZE; |
| 1476 | #endif // CONFIG_HIGHBITDEPTH |
| 1477 | // U channel colors. |
| 1478 | int min_bits_u = 0; |
| 1479 | int bits_u = av1_get_palette_delta_bits_u(pmi, bit_depth, &min_bits_u); |
| 1480 | aom_write_literal(w, bits_u - min_bits_u, 2); |
| 1481 | aom_write_literal(w, colors_u[0], bit_depth); |
| 1482 | for (i = 1; i < n; ++i) { |
| 1483 | aom_write_literal(w, colors_u[i] - colors_u[i - 1], bits_u); |
| 1484 | bits_u = AOMMIN(bits_u, av1_ceil_log2(1 + (1 << bit_depth) - colors_u[i])); |
| 1485 | } |
| 1486 | // V channel colors. |
| 1487 | const int max_val = 1 << bit_depth; |
| 1488 | int zero_count = 0, min_bits_v = 0; |
| 1489 | int bits_v = |
| 1490 | av1_get_palette_delta_bits_v(pmi, bit_depth, &zero_count, &min_bits_v); |
| 1491 | const int rate_using_delta = |
| 1492 | 2 + bit_depth + (bits_v + 1) * (n - 1) - zero_count; |
| 1493 | const int rate_using_raw = bit_depth * n; |
| 1494 | if (rate_using_delta < rate_using_raw) { // delta encoding |
| 1495 | aom_write_bit(w, 1); |
| 1496 | aom_write_literal(w, bits_v - min_bits_v, 2); |
| 1497 | aom_write_literal(w, colors_v[0], bit_depth); |
| 1498 | for (i = 1; i < n; ++i) { |
| 1499 | if (colors_v[i] == colors_v[i - 1]) { // No need to signal sign bit. |
| 1500 | aom_write_literal(w, 0, bits_v); |
| 1501 | continue; |
| 1502 | } |
| 1503 | const int delta = abs((int)colors_v[i] - colors_v[i - 1]); |
| 1504 | const int sign_bit = colors_v[i] < colors_v[i - 1]; |
| 1505 | if (delta <= max_val - delta) { |
| 1506 | aom_write_literal(w, delta, bits_v); |
| 1507 | aom_write_bit(w, sign_bit); |
| 1508 | } else { |
| 1509 | aom_write_literal(w, max_val - delta, bits_v); |
| 1510 | aom_write_bit(w, !sign_bit); |
| 1511 | } |
| 1512 | } |
| 1513 | } else { // Transmit raw values. |
| 1514 | aom_write_bit(w, 0); |
| 1515 | for (i = 0; i < n; ++i) aom_write_literal(w, colors_v[i], bit_depth); |
| 1516 | } |
| 1517 | } |
| 1518 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
| 1519 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1520 | static void write_palette_mode_info(const AV1_COMMON *cm, const MACROBLOCKD *xd, |
| 1521 | const MODE_INFO *const mi, aom_writer *w) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1522 | const MB_MODE_INFO *const mbmi = &mi->mbmi; |
| 1523 | const MODE_INFO *const above_mi = xd->above_mi; |
| 1524 | const MODE_INFO *const left_mi = xd->left_mi; |
| 1525 | const BLOCK_SIZE bsize = mbmi->sb_type; |
| 1526 | const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1527 | |
| 1528 | if (mbmi->mode == DC_PRED) { |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 1529 | const int n = pmi->palette_size[0]; |
| 1530 | int palette_y_mode_ctx = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1531 | if (above_mi) |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 1532 | palette_y_mode_ctx += |
| 1533 | (above_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1534 | if (left_mi) |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 1535 | palette_y_mode_ctx += |
| 1536 | (left_mi->mbmi.palette_mode_info.palette_size[0] > 0); |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 1537 | aom_write( |
| 1538 | w, n > 0, |
| 1539 | av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_y_mode_ctx]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1540 | if (n > 0) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1541 | av1_write_token(w, av1_palette_size_tree, |
| 1542 | av1_default_palette_y_size_prob[bsize - BLOCK_8X8], |
Alex Converse | 9210981 | 2017-02-22 10:21:40 -0800 | [diff] [blame] | 1543 | &palette_size_encodings[n - PALETTE_MIN_SIZE]); |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 1544 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 1545 | write_palette_colors_y(pmi, cm->bit_depth, w); |
| 1546 | #else |
| 1547 | int i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1548 | for (i = 0; i < n; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1549 | aom_write_literal(w, pmi->palette_colors[i], cm->bit_depth); |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 1550 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1551 | write_uniform(w, n, pmi->palette_first_color_idx[0]); |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | if (mbmi->uv_mode == DC_PRED) { |
Urvang Joshi | 23a6111 | 2017-01-30 14:59:27 -0800 | [diff] [blame] | 1556 | const int n = pmi->palette_size[1]; |
| 1557 | const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0); |
| 1558 | aom_write(w, n > 0, av1_default_palette_uv_mode_prob[palette_uv_mode_ctx]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1559 | if (n > 0) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1560 | av1_write_token(w, av1_palette_size_tree, |
| 1561 | av1_default_palette_uv_size_prob[bsize - BLOCK_8X8], |
Alex Converse | 9210981 | 2017-02-22 10:21:40 -0800 | [diff] [blame] | 1562 | &palette_size_encodings[n - PALETTE_MIN_SIZE]); |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 1563 | #if CONFIG_PALETTE_DELTA_ENCODING |
| 1564 | write_palette_colors_uv(pmi, cm->bit_depth, w); |
| 1565 | #else |
| 1566 | int i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1567 | for (i = 0; i < n; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1568 | aom_write_literal(w, pmi->palette_colors[PALETTE_MAX_SIZE + i], |
| 1569 | cm->bit_depth); |
| 1570 | aom_write_literal(w, pmi->palette_colors[2 * PALETTE_MAX_SIZE + i], |
| 1571 | cm->bit_depth); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1572 | } |
hui su | d13c24a | 2017-04-07 16:13:07 -0700 | [diff] [blame] | 1573 | #endif // CONFIG_PALETTE_DELTA_ENCODING |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1574 | write_uniform(w, n, pmi->palette_first_color_idx[1]); |
| 1575 | } |
| 1576 | } |
| 1577 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 1578 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1579 | |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1580 | void av1_write_tx_type(const AV1_COMMON *const cm, const MACROBLOCKD *xd, |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1581 | #if CONFIG_SUPERTX |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1582 | const int supertx_enabled, |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1583 | #endif |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 1584 | #if CONFIG_TXK_SEL |
Angie Chiang | 39b06eb | 2017-04-14 09:52:29 -0700 | [diff] [blame] | 1585 | int block, int plane, |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1586 | #endif |
| 1587 | aom_writer *w) { |
| 1588 | MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1589 | const int is_inter = is_inter_block(mbmi); |
Jingning Han | e67b38a | 2016-11-04 10:30:00 -0700 | [diff] [blame] | 1590 | #if CONFIG_VAR_TX |
| 1591 | const TX_SIZE tx_size = is_inter ? mbmi->min_tx_size : mbmi->tx_size; |
| 1592 | #else |
Jingning Han | 641b1ad | 2016-11-04 09:58:36 -0700 | [diff] [blame] | 1593 | const TX_SIZE tx_size = mbmi->tx_size; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 1594 | #endif // CONFIG_VAR_TX |
Thomas Davies | cef0962 | 2017-01-11 17:27:12 +0000 | [diff] [blame] | 1595 | #if CONFIG_EC_ADAPT |
| 1596 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 1597 | #else |
| 1598 | FRAME_CONTEXT *ec_ctx = cm->fc; |
| 1599 | #endif |
| 1600 | |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 1601 | #if !CONFIG_TXK_SEL |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1602 | TX_TYPE tx_type = mbmi->tx_type; |
| 1603 | #else |
| 1604 | // Only y plane's tx_type is transmitted |
Angie Chiang | 39b06eb | 2017-04-14 09:52:29 -0700 | [diff] [blame] | 1605 | if (plane > 0) return; |
| 1606 | PLANE_TYPE plane_type = get_plane_type(plane); |
| 1607 | TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size); |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1608 | #endif |
| 1609 | |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1610 | if (!FIXED_TX_TYPE) { |
| 1611 | #if CONFIG_EXT_TX |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 1612 | const TX_SIZE square_tx_size = txsize_sqr_map[tx_size]; |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1613 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 1614 | if (get_ext_tx_types(tx_size, bsize, is_inter, cm->reduced_tx_set_used) > |
| 1615 | 1 && |
Yue Chen | eeacc4c | 2017-01-17 17:29:17 -0800 | [diff] [blame] | 1616 | ((!cm->seg.enabled && cm->base_qindex > 0) || |
| 1617 | (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) && |
Jingning Han | 641b1ad | 2016-11-04 09:58:36 -0700 | [diff] [blame] | 1618 | !mbmi->skip && |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1619 | #if CONFIG_SUPERTX |
| 1620 | !supertx_enabled && |
| 1621 | #endif // CONFIG_SUPERTX |
| 1622 | !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) { |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 1623 | const int eset = |
| 1624 | get_ext_tx_set(tx_size, bsize, is_inter, cm->reduced_tx_set_used); |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1625 | if (is_inter) { |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1626 | assert(ext_tx_used_inter[eset][tx_type]); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 1627 | if (eset > 0) { |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 1628 | #if CONFIG_EC_MULTISYMBOL |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1629 | aom_write_symbol(w, av1_ext_tx_inter_ind[eset][tx_type], |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 1630 | ec_ctx->inter_ext_tx_cdf[eset][square_tx_size], |
| 1631 | ext_tx_cnt_inter[eset]); |
| 1632 | #else |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 1633 | av1_write_token(w, av1_ext_tx_inter_tree[eset], |
Thomas Davies | cef0962 | 2017-01-11 17:27:12 +0000 | [diff] [blame] | 1634 | ec_ctx->inter_ext_tx_prob[eset][square_tx_size], |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1635 | &ext_tx_inter_encodings[eset][tx_type]); |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 1636 | #endif |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 1637 | } |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1638 | } else if (ALLOW_INTRA_EXT_TX) { |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1639 | assert(ext_tx_used_intra[eset][tx_type]); |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 1640 | if (eset > 0) { |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 1641 | #if CONFIG_EC_MULTISYMBOL |
| 1642 | aom_write_symbol( |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1643 | w, av1_ext_tx_intra_ind[eset][tx_type], |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 1644 | ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode], |
| 1645 | ext_tx_cnt_intra[eset]); |
| 1646 | #else |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 1647 | av1_write_token( |
| 1648 | w, av1_ext_tx_intra_tree[eset], |
Thomas Davies | cef0962 | 2017-01-11 17:27:12 +0000 | [diff] [blame] | 1649 | ec_ctx->intra_ext_tx_prob[eset][square_tx_size][mbmi->mode], |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1650 | &ext_tx_intra_encodings[eset][tx_type]); |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 1651 | #endif |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 1652 | } |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1653 | } |
| 1654 | } |
| 1655 | #else |
Yue Chen | eeacc4c | 2017-01-17 17:29:17 -0800 | [diff] [blame] | 1656 | if (tx_size < TX_32X32 && |
| 1657 | ((!cm->seg.enabled && cm->base_qindex > 0) || |
| 1658 | (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) && |
| 1659 | !mbmi->skip && |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1660 | #if CONFIG_SUPERTX |
| 1661 | !supertx_enabled && |
| 1662 | #endif // CONFIG_SUPERTX |
| 1663 | !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) { |
| 1664 | if (is_inter) { |
Nathan E. Egge | dfa33f2 | 2016-11-16 09:44:26 -0500 | [diff] [blame] | 1665 | #if CONFIG_EC_MULTISYMBOL |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1666 | aom_write_symbol(w, av1_ext_tx_ind[tx_type], |
Thomas Davies | cef0962 | 2017-01-11 17:27:12 +0000 | [diff] [blame] | 1667 | ec_ctx->inter_ext_tx_cdf[tx_size], TX_TYPES); |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1668 | #else |
Thomas Davies | cef0962 | 2017-01-11 17:27:12 +0000 | [diff] [blame] | 1669 | av1_write_token(w, av1_ext_tx_tree, ec_ctx->inter_ext_tx_prob[tx_size], |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1670 | &ext_tx_encodings[tx_type]); |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1671 | #endif |
| 1672 | } else { |
Nathan E. Egge | 29ccee0 | 2016-11-16 09:44:26 -0500 | [diff] [blame] | 1673 | #if CONFIG_EC_MULTISYMBOL |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1674 | aom_write_symbol( |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1675 | w, av1_ext_tx_ind[tx_type], |
Thomas Davies | cef0962 | 2017-01-11 17:27:12 +0000 | [diff] [blame] | 1676 | ec_ctx->intra_ext_tx_cdf[tx_size] |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1677 | [intra_mode_to_tx_type_context[mbmi->mode]], |
| 1678 | TX_TYPES); |
| 1679 | #else |
| 1680 | av1_write_token( |
| 1681 | w, av1_ext_tx_tree, |
Thomas Davies | cef0962 | 2017-01-11 17:27:12 +0000 | [diff] [blame] | 1682 | ec_ctx |
Jingning Han | 641b1ad | 2016-11-04 09:58:36 -0700 | [diff] [blame] | 1683 | ->intra_ext_tx_prob[tx_size] |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1684 | [intra_mode_to_tx_type_context[mbmi->mode]], |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1685 | &ext_tx_encodings[tx_type]); |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1686 | #endif |
| 1687 | } |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 1688 | } |
| 1689 | #endif // CONFIG_EXT_TX |
| 1690 | } |
| 1691 | } |
| 1692 | |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 1693 | static void write_intra_mode(FRAME_CONTEXT *frame_ctx, BLOCK_SIZE bsize, |
| 1694 | PREDICTION_MODE mode, aom_writer *w) { |
| 1695 | #if CONFIG_EC_MULTISYMBOL |
| 1696 | aom_write_symbol(w, av1_intra_mode_ind[mode], |
| 1697 | frame_ctx->y_mode_cdf[size_group_lookup[bsize]], |
| 1698 | INTRA_MODES); |
| 1699 | #else |
| 1700 | av1_write_token(w, av1_intra_mode_tree, |
| 1701 | frame_ctx->y_mode_prob[size_group_lookup[bsize]], |
| 1702 | &intra_mode_encodings[mode]); |
| 1703 | #endif |
| 1704 | } |
| 1705 | |
| 1706 | static void write_intra_uv_mode(FRAME_CONTEXT *frame_ctx, |
| 1707 | PREDICTION_MODE uv_mode, PREDICTION_MODE y_mode, |
| 1708 | aom_writer *w) { |
| 1709 | #if CONFIG_EC_MULTISYMBOL |
| 1710 | aom_write_symbol(w, av1_intra_mode_ind[uv_mode], |
| 1711 | frame_ctx->uv_mode_cdf[y_mode], INTRA_MODES); |
| 1712 | #else |
| 1713 | av1_write_token(w, av1_intra_mode_tree, frame_ctx->uv_mode_prob[y_mode], |
| 1714 | &intra_mode_encodings[uv_mode]); |
| 1715 | #endif |
| 1716 | } |
| 1717 | |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1718 | static void pack_inter_mode_mvs(AV1_COMP *cpi, const int mi_row, |
| 1719 | const int mi_col, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1720 | #if CONFIG_SUPERTX |
| 1721 | int supertx_enabled, |
| 1722 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1723 | aom_writer *w) { |
| 1724 | AV1_COMMON *const cm = &cpi->common; |
Thomas Davies | 2452329 | 2017-01-11 16:56:47 +0000 | [diff] [blame] | 1725 | #if CONFIG_DELTA_Q || CONFIG_EC_ADAPT |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 1726 | MACROBLOCK *const x = &cpi->td.mb; |
| 1727 | MACROBLOCKD *const xd = &x->e_mbd; |
| 1728 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1729 | const MACROBLOCK *x = &cpi->td.mb; |
| 1730 | const MACROBLOCKD *xd = &x->e_mbd; |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 1731 | #endif |
Thomas Davies | 2452329 | 2017-01-11 16:56:47 +0000 | [diff] [blame] | 1732 | #if CONFIG_EC_ADAPT |
| 1733 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 1734 | #else |
| 1735 | FRAME_CONTEXT *ec_ctx = cm->fc; |
| 1736 | #endif |
| 1737 | #if !CONFIG_REF_MV |
| 1738 | nmv_context *nmvc = &ec_ctx->nmvc; |
| 1739 | #endif |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 1740 | const MODE_INFO *mi = xd->mi[0]; |
Thomas Davies | 2452329 | 2017-01-11 16:56:47 +0000 | [diff] [blame] | 1741 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1742 | const struct segmentation *const seg = &cm->seg; |
Thomas | 9ac5508 | 2016-09-23 18:04:17 +0100 | [diff] [blame] | 1743 | struct segmentation_probs *const segp = &cm->fc->seg; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1744 | const MB_MODE_INFO *const mbmi = &mi->mbmi; |
| 1745 | const MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext; |
| 1746 | const PREDICTION_MODE mode = mbmi->mode; |
| 1747 | const int segment_id = mbmi->segment_id; |
| 1748 | const BLOCK_SIZE bsize = mbmi->sb_type; |
| 1749 | const int allow_hp = cm->allow_high_precision_mv; |
| 1750 | const int is_inter = is_inter_block(mbmi); |
| 1751 | const int is_compound = has_second_ref(mbmi); |
| 1752 | int skip, ref; |
Jingning Han | 5226184 | 2016-12-14 12:17:49 -0800 | [diff] [blame] | 1753 | #if CONFIG_CB4X4 |
| 1754 | const int unify_bsize = 1; |
| 1755 | #else |
| 1756 | const int unify_bsize = 0; |
| 1757 | #endif |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 1758 | (void)mi_row; |
| 1759 | (void)mi_col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1760 | |
| 1761 | if (seg->update_map) { |
| 1762 | if (seg->temporal_update) { |
| 1763 | const int pred_flag = mbmi->seg_id_predicted; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1764 | aom_prob pred_prob = av1_get_pred_prob_seg_id(segp, xd); |
| 1765 | aom_write(w, pred_flag, pred_prob); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1766 | if (!pred_flag) write_segment_id(w, seg, segp, segment_id); |
| 1767 | } else { |
| 1768 | write_segment_id(w, seg, segp, segment_id); |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | #if CONFIG_SUPERTX |
| 1773 | if (supertx_enabled) |
| 1774 | skip = mbmi->skip; |
| 1775 | else |
| 1776 | skip = write_skip(cm, xd, segment_id, mi, w); |
| 1777 | #else |
| 1778 | skip = write_skip(cm, xd, segment_id, mi, w); |
| 1779 | #endif // CONFIG_SUPERTX |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 1780 | #if CONFIG_DELTA_Q |
| 1781 | if (cm->delta_q_present_flag) { |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 1782 | int super_block_upper_left = |
| 1783 | ((mi_row & MAX_MIB_MASK) == 0) && ((mi_col & MAX_MIB_MASK) == 0); |
Alex Converse | 68abef8 | 2017-03-23 14:50:33 -0700 | [diff] [blame] | 1784 | if ((bsize != BLOCK_LARGEST || skip == 0) && super_block_upper_left) { |
Arild Fuldseth (arilfuld) | 54de7d6 | 2017-03-20 13:07:11 +0100 | [diff] [blame] | 1785 | assert(mbmi->current_q_index > 0); |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 1786 | int reduced_delta_qindex = |
| 1787 | (mbmi->current_q_index - xd->prev_qindex) / cm->delta_q_res; |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 1788 | write_delta_qindex(cm, xd, reduced_delta_qindex, w); |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 1789 | xd->prev_qindex = mbmi->current_q_index; |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 1790 | #if CONFIG_EXT_DELTA_Q |
| 1791 | if (cm->delta_lf_present_flag) { |
| 1792 | int reduced_delta_lflevel = |
| 1793 | (mbmi->current_delta_lf_from_base - xd->prev_delta_lf_from_base) / |
| 1794 | cm->delta_lf_res; |
| 1795 | write_delta_lflevel(cm, xd, reduced_delta_lflevel, w); |
| 1796 | xd->prev_delta_lf_from_base = mbmi->current_delta_lf_from_base; |
| 1797 | } |
| 1798 | #endif // CONFIG_EXT_DELTA_Q |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1802 | |
| 1803 | #if CONFIG_SUPERTX |
| 1804 | if (!supertx_enabled) |
| 1805 | #endif // CONFIG_SUPERTX |
| 1806 | if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1807 | aom_write(w, is_inter, av1_get_intra_inter_prob(cm, xd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1808 | |
Jingning Han | 581d169 | 2017-01-05 16:03:54 -0800 | [diff] [blame] | 1809 | if (cm->tx_mode == TX_MODE_SELECT && |
Jingning Han | 3daa4fd | 2017-01-20 10:33:50 -0800 | [diff] [blame] | 1810 | #if CONFIG_CB4X4 && (CONFIG_VAR_TX || CONFIG_RECT_TX) |
Jingning Han | cb51228 | 2017-02-10 14:21:35 -0800 | [diff] [blame] | 1811 | #if CONFIG_RECT_TX |
Jingning Han | 3daa4fd | 2017-01-20 10:33:50 -0800 | [diff] [blame] | 1812 | bsize > BLOCK_4X4 && |
Jingning Han | 581d169 | 2017-01-05 16:03:54 -0800 | [diff] [blame] | 1813 | #else |
Jingning Han | cb51228 | 2017-02-10 14:21:35 -0800 | [diff] [blame] | 1814 | (bsize >= BLOCK_8X8 || (bsize > BLOCK_4X4 && is_inter)) && |
| 1815 | #endif // CONFIG_RECT_TX |
| 1816 | #else |
Jingning Han | 581d169 | 2017-01-05 16:03:54 -0800 | [diff] [blame] | 1817 | bsize >= BLOCK_8X8 && |
| 1818 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1819 | #if CONFIG_SUPERTX |
| 1820 | !supertx_enabled && |
| 1821 | #endif // CONFIG_SUPERTX |
| 1822 | !(is_inter && skip) && !xd->lossless[segment_id]) { |
| 1823 | #if CONFIG_VAR_TX |
| 1824 | if (is_inter) { // This implies skip flag is 0. |
Sarah Parker | 106b3cb | 2017-04-21 12:13:37 -0700 | [diff] [blame] | 1825 | const TX_SIZE max_tx_size = get_vartx_max_txsize(mbmi, bsize); |
Jingning Han | f64062f | 2016-11-02 16:22:18 -0700 | [diff] [blame] | 1826 | const int bh = tx_size_high_unit[max_tx_size]; |
| 1827 | const int bw = tx_size_wide_unit[max_tx_size]; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 1828 | const int width = block_size_wide[bsize] >> tx_size_wide_log2[0]; |
| 1829 | const int height = block_size_high[bsize] >> tx_size_wide_log2[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1830 | int idx, idy; |
Jingning Han | fe45b21 | 2016-11-22 10:30:23 -0800 | [diff] [blame] | 1831 | for (idy = 0; idy < height; idy += bh) |
| 1832 | for (idx = 0; idx < width; idx += bw) |
| 1833 | write_tx_size_vartx(cm, xd, mbmi, max_tx_size, height != width, idy, |
| 1834 | idx, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1835 | } else { |
Jingning Han | 1b1dc93 | 2016-11-09 10:55:30 -0800 | [diff] [blame] | 1836 | set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, skip, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1837 | write_selected_tx_size(cm, xd, w); |
| 1838 | } |
| 1839 | } else { |
Jingning Han | 1b1dc93 | 2016-11-09 10:55:30 -0800 | [diff] [blame] | 1840 | set_txfm_ctxs(mbmi->tx_size, xd->n8_w, xd->n8_h, skip, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1841 | #else |
| 1842 | write_selected_tx_size(cm, xd, w); |
| 1843 | #endif |
| 1844 | } |
| 1845 | |
| 1846 | if (!is_inter) { |
Jingning Han | 5226184 | 2016-12-14 12:17:49 -0800 | [diff] [blame] | 1847 | if (bsize >= BLOCK_8X8 || unify_bsize) { |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 1848 | write_intra_mode(ec_ctx, bsize, mode, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1849 | } else { |
| 1850 | int idx, idy; |
| 1851 | const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize]; |
| 1852 | const int num_4x4_h = num_4x4_blocks_high_lookup[bsize]; |
| 1853 | for (idy = 0; idy < 2; idy += num_4x4_h) { |
| 1854 | for (idx = 0; idx < 2; idx += num_4x4_w) { |
| 1855 | const PREDICTION_MODE b_mode = mi->bmi[idy * 2 + idx].as_mode; |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 1856 | write_intra_mode(ec_ctx, bsize, b_mode, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1857 | } |
| 1858 | } |
| 1859 | } |
Jingning Han | 0b7cbe6 | 2017-03-08 10:22:47 -0800 | [diff] [blame] | 1860 | #if CONFIG_CB4X4 |
Jingning Han | d3a6443 | 2017-04-06 17:04:17 -0700 | [diff] [blame] | 1861 | if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x, |
| 1862 | xd->plane[1].subsampling_y)) |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 1863 | write_intra_uv_mode(ec_ctx, mbmi->uv_mode, mode, w); |
Jingning Han | 0b7cbe6 | 2017-03-08 10:22:47 -0800 | [diff] [blame] | 1864 | #else // !CONFIG_CB4X4 |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 1865 | write_intra_uv_mode(ec_ctx, mbmi->uv_mode, mode, w); |
Jingning Han | 36fe320 | 2017-02-20 22:31:49 -0800 | [diff] [blame] | 1866 | #endif // CONFIG_CB4X4 |
Jingning Han | 0b7cbe6 | 2017-03-08 10:22:47 -0800 | [diff] [blame] | 1867 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1868 | #if CONFIG_EXT_INTRA |
hui su | b4e25d2 | 2017-03-09 15:32:30 -0800 | [diff] [blame] | 1869 | write_intra_angle_info(xd, ec_ctx, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1870 | #endif // CONFIG_EXT_INTRA |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 1871 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1872 | if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools) |
| 1873 | write_palette_mode_info(cm, xd, mi, w); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 1874 | #endif // CONFIG_PALETTE |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 1875 | #if CONFIG_FILTER_INTRA |
Jingning Han | 48b1cb3 | 2017-01-23 10:26:14 -0800 | [diff] [blame] | 1876 | if (bsize >= BLOCK_8X8 || unify_bsize) |
| 1877 | write_filter_intra_mode_info(cm, mbmi, w); |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 1878 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1879 | } else { |
Yaowu Xu | b0d0d00 | 2016-11-22 09:26:43 -0800 | [diff] [blame] | 1880 | int16_t mode_ctx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1881 | write_ref_frames(cm, xd, w); |
| 1882 | |
| 1883 | #if CONFIG_REF_MV |
| 1884 | #if CONFIG_EXT_INTER |
| 1885 | if (is_compound) |
| 1886 | mode_ctx = mbmi_ext->compound_mode_context[mbmi->ref_frame[0]]; |
| 1887 | else |
| 1888 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1889 | mode_ctx = av1_mode_context_analyzer(mbmi_ext->mode_context, |
| 1890 | mbmi->ref_frame, bsize, -1); |
Yaowu Xu | b0d0d00 | 2016-11-22 09:26:43 -0800 | [diff] [blame] | 1891 | #else // CONFIG_REF_MV |
| 1892 | mode_ctx = mbmi_ext->mode_context[mbmi->ref_frame[0]]; |
| 1893 | #endif // CONFIG_REF_MV |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1894 | |
| 1895 | // If segment skip is not enabled code the mode. |
| 1896 | if (!segfeature_active(seg, segment_id, SEG_LVL_SKIP)) { |
Jingning Han | 5226184 | 2016-12-14 12:17:49 -0800 | [diff] [blame] | 1897 | if (bsize >= BLOCK_8X8 || unify_bsize) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1898 | #if CONFIG_EXT_INTER |
| 1899 | if (is_inter_compound_mode(mode)) |
| 1900 | write_inter_compound_mode(cm, w, mode, mode_ctx); |
| 1901 | else if (is_inter_singleref_mode(mode)) |
| 1902 | #endif // CONFIG_EXT_INTER |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 1903 | write_inter_mode(w, mode, ec_ctx, mode_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1904 | |
| 1905 | #if CONFIG_REF_MV |
David Barker | 9620bcd | 2017-03-22 14:46:42 +0000 | [diff] [blame] | 1906 | #if CONFIG_EXT_INTER |
David Barker | 3dfba99 | 2017-04-03 16:10:09 +0100 | [diff] [blame] | 1907 | if (mode == NEWMV || mode == NEW_NEWMV || |
| 1908 | have_nearmv_in_inter_mode(mode)) |
David Barker | 404b2e8 | 2017-03-27 13:07:47 +0100 | [diff] [blame] | 1909 | #else |
| 1910 | if (mode == NEARMV || mode == NEWMV) |
| 1911 | #endif |
| 1912 | write_drl_idx(cm, mbmi, mbmi_ext, w); |
David Barker | 9620bcd | 2017-03-22 14:46:42 +0000 | [diff] [blame] | 1913 | else |
| 1914 | assert(mbmi->ref_mv_idx == 0); |
| 1915 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | |
Yue Chen | 19e7aa8 | 2016-11-30 14:05:39 -0800 | [diff] [blame] | 1919 | #if !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION && !CONFIG_GLOBAL_MOTION |
Angie Chiang | 5678ad9 | 2016-11-21 09:38:40 -0800 | [diff] [blame] | 1920 | write_mb_interp_filter(cpi, xd, w); |
Angie Chiang | 1733f6b | 2017-01-05 09:52:20 -0800 | [diff] [blame] | 1921 | #endif // !CONFIG_DUAL_FILTER && !CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1922 | |
Jingning Han | 5226184 | 2016-12-14 12:17:49 -0800 | [diff] [blame] | 1923 | if (bsize < BLOCK_8X8 && !unify_bsize) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1924 | const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize]; |
| 1925 | const int num_4x4_h = num_4x4_blocks_high_lookup[bsize]; |
| 1926 | int idx, idy; |
| 1927 | for (idy = 0; idy < 2; idy += num_4x4_h) { |
| 1928 | for (idx = 0; idx < 2; idx += num_4x4_w) { |
| 1929 | const int j = idy * 2 + idx; |
| 1930 | const PREDICTION_MODE b_mode = mi->bmi[j].as_mode; |
| 1931 | #if CONFIG_REF_MV |
| 1932 | #if CONFIG_EXT_INTER |
| 1933 | if (!is_compound) |
| 1934 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1935 | mode_ctx = av1_mode_context_analyzer(mbmi_ext->mode_context, |
| 1936 | mbmi->ref_frame, bsize, j); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1937 | #endif |
| 1938 | #if CONFIG_EXT_INTER |
| 1939 | if (is_inter_compound_mode(b_mode)) |
| 1940 | write_inter_compound_mode(cm, w, b_mode, mode_ctx); |
| 1941 | else if (is_inter_singleref_mode(b_mode)) |
| 1942 | #endif // CONFIG_EXT_INTER |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 1943 | write_inter_mode(w, b_mode, ec_ctx, mode_ctx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1944 | |
| 1945 | #if CONFIG_EXT_INTER |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 1946 | if (b_mode == NEWMV || b_mode == NEW_NEWMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1947 | #else |
| 1948 | if (b_mode == NEWMV) { |
| 1949 | #endif // CONFIG_EXT_INTER |
| 1950 | for (ref = 0; ref < 1 + is_compound; ++ref) { |
| 1951 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 1952 | int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame); |
| 1953 | int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type], |
| 1954 | mbmi_ext->ref_mv_stack[rf_type], ref, |
| 1955 | mbmi->ref_mv_idx); |
Thomas Davies | 2452329 | 2017-01-11 16:56:47 +0000 | [diff] [blame] | 1956 | nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1957 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1958 | av1_encode_mv(cpi, w, &mi->bmi[j].as_mv[ref].as_mv, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1959 | #if CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1960 | &mi->bmi[j].ref_mv[ref].as_mv, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1961 | #else |
| 1962 | #if CONFIG_REF_MV |
Yue Chen | 09c0a5b | 2017-01-20 12:48:52 -0800 | [diff] [blame] | 1963 | &mi->bmi[j].pred_mv[ref].as_mv, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1964 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1965 | &mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0].as_mv, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1966 | #endif // CONFIG_REF_MV |
| 1967 | #endif // CONFIG_EXT_INTER |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1968 | nmvc, allow_hp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1969 | } |
| 1970 | } |
| 1971 | #if CONFIG_EXT_INTER |
| 1972 | else if (b_mode == NEAREST_NEWMV || b_mode == NEAR_NEWMV) { |
| 1973 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 1974 | int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame); |
| 1975 | int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type], |
| 1976 | mbmi_ext->ref_mv_stack[rf_type], 1, |
| 1977 | mbmi->ref_mv_idx); |
Thomas Davies | 2452329 | 2017-01-11 16:56:47 +0000 | [diff] [blame] | 1978 | nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1979 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1980 | av1_encode_mv(cpi, w, &mi->bmi[j].as_mv[1].as_mv, |
Yue Chen | 09c0a5b | 2017-01-20 12:48:52 -0800 | [diff] [blame] | 1981 | &mi->bmi[j].ref_mv[1].as_mv, nmvc, allow_hp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1982 | } else if (b_mode == NEW_NEARESTMV || b_mode == NEW_NEARMV) { |
| 1983 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 1984 | int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame); |
| 1985 | int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type], |
| 1986 | mbmi_ext->ref_mv_stack[rf_type], 0, |
| 1987 | mbmi->ref_mv_idx); |
Thomas Davies | 2452329 | 2017-01-11 16:56:47 +0000 | [diff] [blame] | 1988 | nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1989 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 1990 | av1_encode_mv(cpi, w, &mi->bmi[j].as_mv[0].as_mv, |
Yue Chen | 09c0a5b | 2017-01-20 12:48:52 -0800 | [diff] [blame] | 1991 | &mi->bmi[j].ref_mv[0].as_mv, nmvc, allow_hp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1992 | } |
| 1993 | #endif // CONFIG_EXT_INTER |
| 1994 | } |
| 1995 | } |
| 1996 | } else { |
| 1997 | #if CONFIG_EXT_INTER |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 1998 | if (mode == NEWMV || mode == NEW_NEWMV) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1999 | #else |
| 2000 | if (mode == NEWMV) { |
| 2001 | #endif // CONFIG_EXT_INTER |
| 2002 | int_mv ref_mv; |
| 2003 | for (ref = 0; ref < 1 + is_compound; ++ref) { |
| 2004 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 2005 | int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame); |
| 2006 | int nmv_ctx = av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type], |
| 2007 | mbmi_ext->ref_mv_stack[rf_type], ref, |
| 2008 | mbmi->ref_mv_idx); |
Thomas Davies | 2452329 | 2017-01-11 16:56:47 +0000 | [diff] [blame] | 2009 | nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2010 | #endif |
| 2011 | ref_mv = mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0]; |
Zoe Liu | 7f24e1b | 2017-03-17 17:42:05 -0700 | [diff] [blame] | 2012 | av1_encode_mv(cpi, w, &mbmi->mv[ref].as_mv, &ref_mv.as_mv, nmvc, |
| 2013 | allow_hp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2014 | } |
| 2015 | #if CONFIG_EXT_INTER |
| 2016 | } else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) { |
| 2017 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 2018 | int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame); |
| 2019 | int nmv_ctx = |
| 2020 | av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type], |
| 2021 | mbmi_ext->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx); |
Thomas Davies | 2452329 | 2017-01-11 16:56:47 +0000 | [diff] [blame] | 2022 | nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2023 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2024 | av1_encode_mv(cpi, w, &mbmi->mv[1].as_mv, |
Yue Chen | 09c0a5b | 2017-01-20 12:48:52 -0800 | [diff] [blame] | 2025 | &mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0].as_mv, nmvc, |
| 2026 | allow_hp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2027 | } else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) { |
| 2028 | #if CONFIG_REF_MV |
Yaowu Xu | 4306b6e | 2016-09-27 12:55:32 -0700 | [diff] [blame] | 2029 | int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame); |
| 2030 | int nmv_ctx = |
| 2031 | av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type], |
| 2032 | mbmi_ext->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx); |
Thomas Davies | 2452329 | 2017-01-11 16:56:47 +0000 | [diff] [blame] | 2033 | nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2034 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2035 | av1_encode_mv(cpi, w, &mbmi->mv[0].as_mv, |
Yue Chen | 09c0a5b | 2017-01-20 12:48:52 -0800 | [diff] [blame] | 2036 | &mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0].as_mv, nmvc, |
| 2037 | allow_hp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2038 | #endif // CONFIG_EXT_INTER |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | #if CONFIG_EXT_INTER |
| 2043 | if (cpi->common.reference_mode != COMPOUND_REFERENCE && |
| 2044 | #if CONFIG_SUPERTX |
| 2045 | !supertx_enabled && |
| 2046 | #endif // CONFIG_SUPERTX |
| 2047 | is_interintra_allowed(mbmi)) { |
| 2048 | const int interintra = mbmi->ref_frame[1] == INTRA_FRAME; |
| 2049 | const int bsize_group = size_group_lookup[bsize]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2050 | aom_write(w, interintra, cm->fc->interintra_prob[bsize_group]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2051 | if (interintra) { |
| 2052 | write_interintra_mode(w, mbmi->interintra_mode, |
| 2053 | cm->fc->interintra_mode_prob[bsize_group]); |
| 2054 | if (is_interintra_wedge_used(bsize)) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2055 | aom_write(w, mbmi->use_wedge_interintra, |
| 2056 | cm->fc->wedge_interintra_prob[bsize]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2057 | if (mbmi->use_wedge_interintra) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2058 | aom_write_literal(w, mbmi->interintra_wedge_index, |
| 2059 | get_wedge_bits_lookup(bsize)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2060 | assert(mbmi->interintra_wedge_sign == 0); |
| 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | } |
| 2065 | #endif // CONFIG_EXT_INTER |
| 2066 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 2067 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2068 | #if CONFIG_SUPERTX |
| 2069 | if (!supertx_enabled) |
| 2070 | #endif // CONFIG_SUPERTX |
| 2071 | #if CONFIG_EXT_INTER |
| 2072 | if (mbmi->ref_frame[1] != INTRA_FRAME) |
| 2073 | #endif // CONFIG_EXT_INTER |
Sarah Parker | 19234cc | 2017-03-10 16:43:25 -0800 | [diff] [blame] | 2074 | write_motion_mode(cm, mi, w); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 2075 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2076 | |
| 2077 | #if CONFIG_EXT_INTER |
| 2078 | if (cpi->common.reference_mode != SINGLE_REFERENCE && |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 2079 | is_inter_compound_mode(mbmi->mode) |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 2080 | #if CONFIG_MOTION_VAR |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 2081 | && mbmi->motion_mode == SIMPLE_TRANSLATION |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 2082 | #endif // CONFIG_MOTION_VAR |
Sarah Parker | 42d9610 | 2017-01-31 21:05:27 -0800 | [diff] [blame] | 2083 | && is_any_masked_compound_used(bsize)) { |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 2084 | #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Sarah Parker | 2cf73eb | 2017-05-01 13:25:28 -0700 | [diff] [blame] | 2085 | av1_write_token(w, av1_compound_type_tree, |
| 2086 | cm->fc->compound_type_prob[bsize], |
| 2087 | &compound_type_encodings[mbmi->interinter_compound_type]); |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 2088 | #endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
| 2089 | #if CONFIG_WEDGE |
Sarah Parker | 2cf73eb | 2017-05-01 13:25:28 -0700 | [diff] [blame] | 2090 | if (mbmi->interinter_compound_type == COMPOUND_WEDGE) { |
| 2091 | aom_write_literal(w, mbmi->wedge_index, get_wedge_bits_lookup(bsize)); |
| 2092 | aom_write_bit(w, mbmi->wedge_sign); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2093 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 2094 | #endif // CONFIG_WEDGE |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 2095 | #if CONFIG_COMPOUND_SEGMENT |
Sarah Parker | 2cf73eb | 2017-05-01 13:25:28 -0700 | [diff] [blame] | 2096 | if (mbmi->interinter_compound_type == COMPOUND_SEG) { |
| 2097 | aom_write_literal(w, mbmi->mask_type, MAX_SEG_MASK_BITS); |
Sarah Parker | 569edda | 2016-12-14 14:57:38 -0800 | [diff] [blame] | 2098 | } |
| 2099 | #endif // CONFIG_COMPOUND_SEGMENT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2100 | } |
| 2101 | #endif // CONFIG_EXT_INTER |
| 2102 | |
Yue Chen | 19e7aa8 | 2016-11-30 14:05:39 -0800 | [diff] [blame] | 2103 | #if CONFIG_DUAL_FILTER || CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION |
Debargha Mukherjee | 0df711f | 2017-05-02 16:00:20 -0700 | [diff] [blame] | 2104 | write_mb_interp_filter(cpi, xd, w); |
Angie Chiang | 1733f6b | 2017-01-05 09:52:20 -0800 | [diff] [blame] | 2105 | #endif // CONFIG_DUAL_FILTE || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2106 | } |
| 2107 | |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2108 | #if !CONFIG_TXK_SEL |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 2109 | av1_write_tx_type(cm, xd, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2110 | #if CONFIG_SUPERTX |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 2111 | supertx_enabled, |
Nathan E. Egge | 93878c4 | 2016-05-03 10:01:32 -0400 | [diff] [blame] | 2112 | #endif |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 2113 | w); |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2114 | #endif // !CONFIG_TXK_SEL |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2115 | } |
| 2116 | |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 2117 | #if CONFIG_DELTA_Q |
Jingning Han | 36fe320 | 2017-02-20 22:31:49 -0800 | [diff] [blame] | 2118 | static void write_mb_modes_kf(AV1_COMMON *cm, MACROBLOCKD *xd, const int mi_row, |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 2119 | const int mi_col, aom_writer *w) { |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 2120 | int skip; |
| 2121 | #else |
Thomas | 9ac5508 | 2016-09-23 18:04:17 +0100 | [diff] [blame] | 2122 | static void write_mb_modes_kf(AV1_COMMON *cm, const MACROBLOCKD *xd, |
Jingning Han | 36fe320 | 2017-02-20 22:31:49 -0800 | [diff] [blame] | 2123 | const int mi_row, const int mi_col, |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 2124 | aom_writer *w) { |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 2125 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2126 | const struct segmentation *const seg = &cm->seg; |
Thomas | 9ac5508 | 2016-09-23 18:04:17 +0100 | [diff] [blame] | 2127 | struct segmentation_probs *const segp = &cm->fc->seg; |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 2128 | const MODE_INFO *const mi = xd->mi[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2129 | const MODE_INFO *const above_mi = xd->above_mi; |
| 2130 | const MODE_INFO *const left_mi = xd->left_mi; |
| 2131 | const MB_MODE_INFO *const mbmi = &mi->mbmi; |
| 2132 | const BLOCK_SIZE bsize = mbmi->sb_type; |
Jingning Han | 5226184 | 2016-12-14 12:17:49 -0800 | [diff] [blame] | 2133 | #if CONFIG_CB4X4 |
| 2134 | const int unify_bsize = 1; |
| 2135 | #else |
| 2136 | const int unify_bsize = 0; |
| 2137 | #endif |
David Barker | 45390c1 | 2017-02-20 14:44:40 +0000 | [diff] [blame] | 2138 | (void)mi_row; |
| 2139 | (void)mi_col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2140 | |
Thomas Davies | 1bfb5ed | 2017-01-11 15:28:11 +0000 | [diff] [blame] | 2141 | #if CONFIG_EC_ADAPT |
| 2142 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 2143 | #else |
Thomas Davies | 1bfb5ed | 2017-01-11 15:28:11 +0000 | [diff] [blame] | 2144 | FRAME_CONTEXT *ec_ctx = cm->fc; |
| 2145 | #endif |
| 2146 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2147 | if (seg->update_map) write_segment_id(w, seg, segp, mbmi->segment_id); |
| 2148 | |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 2149 | #if CONFIG_DELTA_Q |
| 2150 | skip = write_skip(cm, xd, mbmi->segment_id, mi, w); |
| 2151 | if (cm->delta_q_present_flag) { |
Alex Converse | 68abef8 | 2017-03-23 14:50:33 -0700 | [diff] [blame] | 2152 | int super_block_upper_left = |
| 2153 | ((mi_row & MAX_MIB_MASK) == 0) && ((mi_col & MAX_MIB_MASK) == 0); |
| 2154 | if ((bsize != BLOCK_LARGEST || skip == 0) && super_block_upper_left) { |
Arild Fuldseth (arilfuld) | 54de7d6 | 2017-03-20 13:07:11 +0100 | [diff] [blame] | 2155 | assert(mbmi->current_q_index > 0); |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 2156 | int reduced_delta_qindex = |
| 2157 | (mbmi->current_q_index - xd->prev_qindex) / cm->delta_q_res; |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 2158 | write_delta_qindex(cm, xd, reduced_delta_qindex, w); |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 2159 | xd->prev_qindex = mbmi->current_q_index; |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 2160 | #if CONFIG_EXT_DELTA_Q |
| 2161 | if (cm->delta_lf_present_flag) { |
| 2162 | int reduced_delta_lflevel = |
| 2163 | (mbmi->current_delta_lf_from_base - xd->prev_delta_lf_from_base) / |
| 2164 | cm->delta_lf_res; |
| 2165 | write_delta_lflevel(cm, xd, reduced_delta_lflevel, w); |
| 2166 | xd->prev_delta_lf_from_base = mbmi->current_delta_lf_from_base; |
| 2167 | } |
| 2168 | #endif // CONFIG_EXT_DELTA_Q |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2172 | write_skip(cm, xd, mbmi->segment_id, mi, w); |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 2173 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2174 | |
Jingning Han | 3daa4fd | 2017-01-20 10:33:50 -0800 | [diff] [blame] | 2175 | if (cm->tx_mode == TX_MODE_SELECT && |
| 2176 | #if CONFIG_CB4X4 && (CONFIG_VAR_TX || CONFIG_RECT_TX) |
Jingning Han | cb51228 | 2017-02-10 14:21:35 -0800 | [diff] [blame] | 2177 | #if CONFIG_RECT_TX |
Jingning Han | 3daa4fd | 2017-01-20 10:33:50 -0800 | [diff] [blame] | 2178 | bsize > BLOCK_4X4 && |
| 2179 | #else |
| 2180 | bsize >= BLOCK_8X8 && |
Jingning Han | cb51228 | 2017-02-10 14:21:35 -0800 | [diff] [blame] | 2181 | #endif // CONFIG_RECT_TX |
| 2182 | #else |
| 2183 | bsize >= BLOCK_8X8 && |
Jingning Han | 3daa4fd | 2017-01-20 10:33:50 -0800 | [diff] [blame] | 2184 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2185 | !xd->lossless[mbmi->segment_id]) |
| 2186 | write_selected_tx_size(cm, xd, w); |
| 2187 | |
Alex Converse | 2874430 | 2017-04-13 14:46:22 -0700 | [diff] [blame] | 2188 | #if CONFIG_INTRABC |
| 2189 | if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools) { |
| 2190 | int use_intrabc = is_intrabc_block(mbmi); |
| 2191 | aom_write(w, use_intrabc, INTRABC_PROB); |
| 2192 | if (use_intrabc) { |
| 2193 | assert(mbmi->mode == DC_PRED); |
| 2194 | assert(mbmi->uv_mode == DC_PRED); |
| 2195 | int_mv dv_ref; |
| 2196 | av1_find_ref_dv(&dv_ref, mi_row, mi_col); |
| 2197 | av1_encode_dv(w, &mbmi->mv[0].as_mv, &dv_ref.as_mv, &ec_ctx->ndvc); |
| 2198 | return; |
| 2199 | } |
| 2200 | } |
| 2201 | #endif // CONFIG_INTRABC |
| 2202 | |
Jingning Han | 5226184 | 2016-12-14 12:17:49 -0800 | [diff] [blame] | 2203 | if (bsize >= BLOCK_8X8 || unify_bsize) { |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 2204 | write_intra_mode_kf(cm, ec_ctx, mi, above_mi, left_mi, 0, mbmi->mode, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2205 | } else { |
| 2206 | const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize]; |
| 2207 | const int num_4x4_h = num_4x4_blocks_high_lookup[bsize]; |
| 2208 | int idx, idy; |
| 2209 | |
| 2210 | for (idy = 0; idy < 2; idy += num_4x4_h) { |
| 2211 | for (idx = 0; idx < 2; idx += num_4x4_w) { |
| 2212 | const int block = idy * 2 + idx; |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 2213 | write_intra_mode_kf(cm, ec_ctx, mi, above_mi, left_mi, block, |
| 2214 | mi->bmi[block].as_mode, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2215 | } |
| 2216 | } |
| 2217 | } |
Jingning Han | 0b7cbe6 | 2017-03-08 10:22:47 -0800 | [diff] [blame] | 2218 | |
| 2219 | #if CONFIG_CB4X4 |
Jingning Han | d3a6443 | 2017-04-06 17:04:17 -0700 | [diff] [blame] | 2220 | if (is_chroma_reference(mi_row, mi_col, bsize, xd->plane[1].subsampling_x, |
| 2221 | xd->plane[1].subsampling_y)) |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 2222 | write_intra_uv_mode(ec_ctx, mbmi->uv_mode, mbmi->mode, w); |
Jingning Han | 0b7cbe6 | 2017-03-08 10:22:47 -0800 | [diff] [blame] | 2223 | #else // !CONFIG_CB4X4 |
Jingning Han | f04254f | 2017-03-08 10:51:35 -0800 | [diff] [blame] | 2224 | write_intra_uv_mode(ec_ctx, mbmi->uv_mode, mbmi->mode, w); |
Jingning Han | 36fe320 | 2017-02-20 22:31:49 -0800 | [diff] [blame] | 2225 | #endif // CONFIG_CB4X4 |
Jingning Han | 0b7cbe6 | 2017-03-08 10:22:47 -0800 | [diff] [blame] | 2226 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2227 | #if CONFIG_EXT_INTRA |
hui su | b4e25d2 | 2017-03-09 15:32:30 -0800 | [diff] [blame] | 2228 | write_intra_angle_info(xd, ec_ctx, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2229 | #endif // CONFIG_EXT_INTRA |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2230 | #if CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2231 | if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools) |
| 2232 | write_palette_mode_info(cm, xd, mi, w); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2233 | #endif // CONFIG_PALETTE |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2234 | #if CONFIG_FILTER_INTRA |
Jingning Han | 48b1cb3 | 2017-01-23 10:26:14 -0800 | [diff] [blame] | 2235 | if (bsize >= BLOCK_8X8 || unify_bsize) |
| 2236 | write_filter_intra_mode_info(cm, mbmi, w); |
hui su | 5db9743 | 2016-10-14 16:10:14 -0700 | [diff] [blame] | 2237 | #endif // CONFIG_FILTER_INTRA |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2238 | |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2239 | #if !CONFIG_TXK_SEL |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 2240 | av1_write_tx_type(cm, xd, |
Jingning Han | 2a4da94 | 2016-11-03 18:31:30 -0700 | [diff] [blame] | 2241 | #if CONFIG_SUPERTX |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 2242 | 0, |
Nathan E. Egge | 72762a2 | 2016-09-07 17:12:07 -0400 | [diff] [blame] | 2243 | #endif |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 2244 | w); |
Angie Chiang | cd9b03f | 2017-04-16 13:37:13 -0700 | [diff] [blame] | 2245 | #endif // !CONFIG_TXK_SEL |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | #if CONFIG_SUPERTX |
| 2249 | #define write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, \ |
| 2250 | mi_row, mi_col) \ |
| 2251 | write_modes_b(cpi, tile, w, tok, tok_end, supertx_enabled, mi_row, mi_col) |
| 2252 | #else |
| 2253 | #define write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, \ |
| 2254 | mi_row, mi_col) \ |
| 2255 | write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col) |
Alex Converse | ec6fb64 | 2016-10-19 11:31:48 -0700 | [diff] [blame] | 2256 | #endif // CONFIG_SUPERTX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2257 | |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 2258 | #if CONFIG_RD_DEBUG |
| 2259 | static void dump_mode_info(MODE_INFO *mi) { |
| 2260 | printf("\nmi->mbmi.mi_row == %d\n", mi->mbmi.mi_row); |
| 2261 | printf("&& mi->mbmi.mi_col == %d\n", mi->mbmi.mi_col); |
| 2262 | printf("&& mi->mbmi.sb_type == %d\n", mi->mbmi.sb_type); |
| 2263 | printf("&& mi->mbmi.tx_size == %d\n", mi->mbmi.tx_size); |
| 2264 | if (mi->mbmi.sb_type >= BLOCK_8X8) { |
| 2265 | printf("&& mi->mbmi.mode == %d\n", mi->mbmi.mode); |
| 2266 | } else { |
| 2267 | printf("&& mi->bmi[0].as_mode == %d\n", mi->bmi[0].as_mode); |
| 2268 | } |
| 2269 | } |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 2270 | static int rd_token_stats_mismatch(RD_STATS *rd_stats, TOKEN_STATS *token_stats, |
| 2271 | int plane) { |
| 2272 | if (rd_stats->txb_coeff_cost[plane] != token_stats->cost) { |
Angie Chiang | 3963d63 | 2016-11-10 18:41:40 -0800 | [diff] [blame] | 2273 | #if CONFIG_VAR_TX |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 2274 | int r, c; |
Angie Chiang | 3963d63 | 2016-11-10 18:41:40 -0800 | [diff] [blame] | 2275 | #endif |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 2276 | printf("\nplane %d rd_stats->txb_coeff_cost %d token_stats->cost %d\n", |
| 2277 | plane, rd_stats->txb_coeff_cost[plane], token_stats->cost); |
| 2278 | #if CONFIG_VAR_TX |
| 2279 | printf("rd txb_coeff_cost_map\n"); |
| 2280 | for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) { |
| 2281 | for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) { |
| 2282 | printf("%d ", rd_stats->txb_coeff_cost_map[plane][r][c]); |
| 2283 | } |
| 2284 | printf("\n"); |
| 2285 | } |
| 2286 | |
| 2287 | printf("pack txb_coeff_cost_map\n"); |
| 2288 | for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r) { |
| 2289 | for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c) { |
| 2290 | printf("%d ", token_stats->txb_coeff_cost_map[r][c]); |
| 2291 | } |
| 2292 | printf("\n"); |
| 2293 | } |
| 2294 | #endif |
| 2295 | return 1; |
| 2296 | } |
| 2297 | return 0; |
| 2298 | } |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 2299 | #endif |
| 2300 | |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2301 | static void write_mbmi_b(AV1_COMP *cpi, const TileInfo *const tile, |
| 2302 | aom_writer *w, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2303 | #if CONFIG_SUPERTX |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2304 | int supertx_enabled, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2305 | #endif |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2306 | int mi_row, int mi_col) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2307 | AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2308 | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
| 2309 | MODE_INFO *m; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2310 | int bh, bw; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2311 | xd->mi = cm->mi_grid_visible + (mi_row * cm->mi_stride + mi_col); |
| 2312 | m = xd->mi[0]; |
| 2313 | |
| 2314 | assert(m->mbmi.sb_type <= cm->sb_size); |
| 2315 | |
Jingning Han | c709e1f | 2016-12-06 14:48:09 -0800 | [diff] [blame] | 2316 | bh = mi_size_high[m->mbmi.sb_type]; |
| 2317 | bw = mi_size_wide[m->mbmi.sb_type]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2318 | |
| 2319 | cpi->td.mb.mbmi_ext = cpi->mbmi_ext_base + (mi_row * cm->mi_cols + mi_col); |
| 2320 | |
Urvang Joshi | 359dc2b | 2017-04-27 15:41:47 -0700 | [diff] [blame] | 2321 | set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, |
Fangwen Fu | 7b9f2b3 | 2017-01-17 14:01:52 -0800 | [diff] [blame] | 2322 | #if CONFIG_DEPENDENT_HORZTILES |
Urvang Joshi | 359dc2b | 2017-04-27 15:41:47 -0700 | [diff] [blame] | 2323 | cm->dependent_horz_tiles, |
| 2324 | #endif // CONFIG_DEPENDENT_HORZTILES |
| 2325 | cm->mi_rows, cm->mi_cols); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 2326 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2327 | if (frame_is_intra_only(cm)) { |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 2328 | write_mb_modes_kf(cm, xd, mi_row, mi_col, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2329 | } else { |
| 2330 | #if CONFIG_VAR_TX |
| 2331 | xd->above_txfm_context = cm->above_txfm_context + mi_col; |
| 2332 | xd->left_txfm_context = |
| 2333 | xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK); |
| 2334 | #endif |
Angie Chiang | 1733f6b | 2017-01-05 09:52:20 -0800 | [diff] [blame] | 2335 | #if CONFIG_DUAL_FILTER |
Angie Chiang | 38edf68 | 2017-02-21 15:13:09 -0800 | [diff] [blame] | 2336 | // has_subpel_mv_component needs the ref frame buffers set up to look |
| 2337 | // up if they are scaled. has_subpel_mv_component is in turn needed by |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2338 | // write_switchable_interp_filter, which is called by pack_inter_mode_mvs. |
| 2339 | set_ref_ptrs(cm, xd, m->mbmi.ref_frame[0], m->mbmi.ref_frame[1]); |
Angie Chiang | 1733f6b | 2017-01-05 09:52:20 -0800 | [diff] [blame] | 2340 | #endif // CONFIG_DUAL_FILTER |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2341 | #if 0 |
| 2342 | // NOTE(zoeliu): For debug |
| 2343 | if (cm->current_video_frame == FRAME_TO_CHECK && cm->show_frame == 1) { |
| 2344 | const PREDICTION_MODE mode = m->mbmi.mode; |
| 2345 | const int segment_id = m->mbmi.segment_id; |
| 2346 | const BLOCK_SIZE bsize = m->mbmi.sb_type; |
| 2347 | |
| 2348 | // For sub8x8, simply dump out the first sub8x8 block info |
| 2349 | const PREDICTION_MODE b_mode = |
| 2350 | (bsize < BLOCK_8X8) ? m->bmi[0].as_mode : -1; |
| 2351 | const int mv_x = (bsize < BLOCK_8X8) ? |
| 2352 | m->bmi[0].as_mv[0].as_mv.row : m->mbmi.mv[0].as_mv.row; |
| 2353 | const int mv_y = (bsize < BLOCK_8X8) ? |
| 2354 | m->bmi[0].as_mv[0].as_mv.col : m->mbmi.mv[0].as_mv.col; |
| 2355 | |
| 2356 | printf("Before pack_inter_mode_mvs(): " |
| 2357 | "Frame=%d, (mi_row,mi_col)=(%d,%d), " |
| 2358 | "mode=%d, segment_id=%d, bsize=%d, b_mode=%d, " |
| 2359 | "mv[0]=(%d, %d), ref[0]=%d, ref[1]=%d\n", |
| 2360 | cm->current_video_frame, mi_row, mi_col, |
| 2361 | mode, segment_id, bsize, b_mode, mv_x, mv_y, |
| 2362 | m->mbmi.ref_frame[0], m->mbmi.ref_frame[1]); |
| 2363 | } |
| 2364 | #endif // 0 |
Angie Chiang | c31ea68 | 2017-04-13 16:20:54 -0700 | [diff] [blame] | 2365 | pack_inter_mode_mvs(cpi, mi_row, mi_col, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2366 | #if CONFIG_SUPERTX |
| 2367 | supertx_enabled, |
| 2368 | #endif |
| 2369 | w); |
| 2370 | } |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2371 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2372 | |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2373 | static void write_tokens_b(AV1_COMP *cpi, const TileInfo *const tile, |
| 2374 | aom_writer *w, const TOKENEXTRA **tok, |
| 2375 | const TOKENEXTRA *const tok_end, int mi_row, |
| 2376 | int mi_col) { |
| 2377 | AV1_COMMON *const cm = &cpi->common; |
| 2378 | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2379 | MODE_INFO *const m = xd->mi[0]; |
| 2380 | MB_MODE_INFO *const mbmi = &m->mbmi; |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2381 | int plane; |
| 2382 | int bh, bw; |
Angie Chiang | c8af611 | 2017-03-16 16:11:22 -0700 | [diff] [blame] | 2383 | #if CONFIG_PVQ || CONFIG_LV_MAP |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 2384 | MACROBLOCK *const x = &cpi->td.mb; |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2385 | (void)tok; |
| 2386 | (void)tok_end; |
| 2387 | #endif |
| 2388 | xd->mi = cm->mi_grid_visible + (mi_row * cm->mi_stride + mi_col); |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2389 | |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2390 | assert(mbmi->sb_type <= cm->sb_size); |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2391 | |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2392 | bh = mi_size_high[mbmi->sb_type]; |
| 2393 | bw = mi_size_wide[mbmi->sb_type]; |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2394 | cpi->td.mb.mbmi_ext = cpi->mbmi_ext_base + (mi_row * cm->mi_cols + mi_col); |
| 2395 | |
Urvang Joshi | 359dc2b | 2017-04-27 15:41:47 -0700 | [diff] [blame] | 2396 | set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, |
Fangwen Fu | 7b9f2b3 | 2017-01-17 14:01:52 -0800 | [diff] [blame] | 2397 | #if CONFIG_DEPENDENT_HORZTILES |
Urvang Joshi | 359dc2b | 2017-04-27 15:41:47 -0700 | [diff] [blame] | 2398 | cm->dependent_horz_tiles, |
| 2399 | #endif // CONFIG_DEPENDENT_HORZTILES |
| 2400 | cm->mi_rows, cm->mi_cols); |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2401 | |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2402 | #if CONFIG_PALETTE |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 2403 | for (plane = 0; plane <= 1; ++plane) { |
| 2404 | const uint8_t palette_size_plane = |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2405 | mbmi->palette_mode_info.palette_size[plane]; |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 2406 | if (palette_size_plane > 0) { |
Alex Converse | ed37d01 | 2017-04-24 11:15:24 -0700 | [diff] [blame] | 2407 | #if CONFIG_INTRABC |
| 2408 | assert(mbmi->use_intrabc == 0); |
| 2409 | #endif |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 2410 | int rows, cols; |
hui su | 9bc1d8d | 2017-03-24 12:36:03 -0700 | [diff] [blame] | 2411 | assert(mbmi->sb_type >= BLOCK_8X8); |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2412 | av1_get_block_dimensions(mbmi->sb_type, plane, xd, NULL, NULL, &rows, |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 2413 | &cols); |
| 2414 | assert(*tok < tok_end); |
| 2415 | pack_palette_tokens(w, tok, palette_size_plane, rows * cols - 1); |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2416 | assert(*tok < tok_end + mbmi->skip); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2417 | } |
Fangwen Fu | b3be926 | 2017-03-06 15:34:28 -0800 | [diff] [blame] | 2418 | } |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 2419 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2420 | |
iole moccagatta | f25a4cf | 2016-11-11 23:57:57 -0800 | [diff] [blame] | 2421 | #if CONFIG_COEF_INTERLEAVE |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2422 | if (!mbmi->skip) { |
iole moccagatta | f25a4cf | 2016-11-11 23:57:57 -0800 | [diff] [blame] | 2423 | const struct macroblockd_plane *const pd_y = &xd->plane[0]; |
| 2424 | const struct macroblockd_plane *const pd_c = &xd->plane[1]; |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2425 | const TX_SIZE tx_log2_y = mbmi->tx_size; |
| 2426 | const TX_SIZE tx_log2_c = get_uv_tx_size(mbmi, pd_c); |
iole moccagatta | f25a4cf | 2016-11-11 23:57:57 -0800 | [diff] [blame] | 2427 | const int tx_sz_y = (1 << tx_log2_y); |
| 2428 | const int tx_sz_c = (1 << tx_log2_c); |
| 2429 | |
| 2430 | const BLOCK_SIZE plane_bsize_y = |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2431 | get_plane_block_size(AOMMAX(mbmi->sb_type, 3), pd_y); |
iole moccagatta | f25a4cf | 2016-11-11 23:57:57 -0800 | [diff] [blame] | 2432 | const BLOCK_SIZE plane_bsize_c = |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2433 | get_plane_block_size(AOMMAX(mbmi->sb_type, 3), pd_c); |
iole moccagatta | f25a4cf | 2016-11-11 23:57:57 -0800 | [diff] [blame] | 2434 | |
| 2435 | const int num_4x4_w_y = num_4x4_blocks_wide_lookup[plane_bsize_y]; |
| 2436 | const int num_4x4_w_c = num_4x4_blocks_wide_lookup[plane_bsize_c]; |
| 2437 | const int num_4x4_h_y = num_4x4_blocks_high_lookup[plane_bsize_y]; |
| 2438 | const int num_4x4_h_c = num_4x4_blocks_high_lookup[plane_bsize_c]; |
| 2439 | |
| 2440 | const int max_4x4_w_y = get_max_4x4_size(num_4x4_w_y, xd->mb_to_right_edge, |
| 2441 | pd_y->subsampling_x); |
| 2442 | const int max_4x4_h_y = get_max_4x4_size(num_4x4_h_y, xd->mb_to_bottom_edge, |
| 2443 | pd_y->subsampling_y); |
| 2444 | const int max_4x4_w_c = get_max_4x4_size(num_4x4_w_c, xd->mb_to_right_edge, |
| 2445 | pd_c->subsampling_x); |
| 2446 | const int max_4x4_h_c = get_max_4x4_size(num_4x4_h_c, xd->mb_to_bottom_edge, |
| 2447 | pd_c->subsampling_y); |
| 2448 | |
| 2449 | // The max_4x4_w/h may be smaller than tx_sz under some corner cases, |
| 2450 | // i.e. when the SB is splitted by tile boundaries. |
| 2451 | const int tu_num_w_y = (max_4x4_w_y + tx_sz_y - 1) / tx_sz_y; |
| 2452 | const int tu_num_h_y = (max_4x4_h_y + tx_sz_y - 1) / tx_sz_y; |
| 2453 | const int tu_num_w_c = (max_4x4_w_c + tx_sz_c - 1) / tx_sz_c; |
| 2454 | const int tu_num_h_c = (max_4x4_h_c + tx_sz_c - 1) / tx_sz_c; |
| 2455 | const int tu_num_y = tu_num_w_y * tu_num_h_y; |
| 2456 | const int tu_num_c = tu_num_w_c * tu_num_h_c; |
| 2457 | |
| 2458 | int tu_idx_y = 0, tu_idx_c = 0; |
| 2459 | TOKEN_STATS token_stats; |
| 2460 | init_token_stats(&token_stats); |
| 2461 | |
| 2462 | assert(*tok < tok_end); |
| 2463 | |
| 2464 | while (tu_idx_y < tu_num_y) { |
| 2465 | pack_mb_tokens(w, tok, tok_end, cm->bit_depth, tx_log2_y, &token_stats); |
| 2466 | assert(*tok < tok_end && (*tok)->token == EOSB_TOKEN); |
| 2467 | (*tok)++; |
| 2468 | tu_idx_y++; |
| 2469 | |
| 2470 | if (tu_idx_c < tu_num_c) { |
| 2471 | pack_mb_tokens(w, tok, tok_end, cm->bit_depth, tx_log2_c, &token_stats); |
| 2472 | assert(*tok < tok_end && (*tok)->token == EOSB_TOKEN); |
| 2473 | (*tok)++; |
| 2474 | |
| 2475 | pack_mb_tokens(w, tok, tok_end, cm->bit_depth, tx_log2_c, &token_stats); |
| 2476 | assert(*tok < tok_end && (*tok)->token == EOSB_TOKEN); |
| 2477 | (*tok)++; |
| 2478 | |
| 2479 | tu_idx_c++; |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | // In 422 case, it's possilbe that Chroma has more TUs than Luma |
| 2484 | while (tu_idx_c < tu_num_c) { |
| 2485 | pack_mb_tokens(w, tok, tok_end, cm->bit_depth, tx_log2_c, &token_stats); |
| 2486 | assert(*tok < tok_end && (*tok)->token == EOSB_TOKEN); |
| 2487 | (*tok)++; |
| 2488 | |
| 2489 | pack_mb_tokens(w, tok, tok_end, cm->bit_depth, tx_log2_c, &token_stats); |
| 2490 | assert(*tok < tok_end && (*tok)->token == EOSB_TOKEN); |
| 2491 | (*tok)++; |
| 2492 | |
| 2493 | tu_idx_c++; |
| 2494 | } |
| 2495 | } |
| 2496 | #else // CONFIG_COEF_INTERLEAVE |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2497 | if (!mbmi->skip) { |
Angie Chiang | 1dca126 | 2017-03-17 16:11:52 -0700 | [diff] [blame] | 2498 | #if !CONFIG_PVQ && !CONFIG_LV_MAP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2499 | assert(*tok < tok_end); |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 2500 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2501 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
Jingning Han | c20dc8e | 2017-02-17 15:37:28 -0800 | [diff] [blame] | 2502 | #if CONFIG_CB4X4 |
Jingning Han | d3a6443 | 2017-04-06 17:04:17 -0700 | [diff] [blame] | 2503 | if (!is_chroma_reference(mi_row, mi_col, mbmi->sb_type, |
| 2504 | xd->plane[plane].subsampling_x, |
| 2505 | xd->plane[plane].subsampling_y)) { |
Jingning Han | c20dc8e | 2017-02-17 15:37:28 -0800 | [diff] [blame] | 2506 | (*tok)++; |
| 2507 | continue; |
| 2508 | } |
| 2509 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2510 | #if CONFIG_VAR_TX |
| 2511 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2512 | BLOCK_SIZE bsize = mbmi->sb_type; |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 2513 | #if CONFIG_CB4X4 |
Jingning Han | 29a4120 | 2017-04-05 11:53:32 -0700 | [diff] [blame] | 2514 | #if CONFIG_CHROMA_2X2 |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 2515 | const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd); |
| 2516 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2517 | const BLOCK_SIZE plane_bsize = |
Jingning Han | 29a4120 | 2017-04-05 11:53:32 -0700 | [diff] [blame] | 2518 | AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd)); |
| 2519 | #endif |
| 2520 | #else |
| 2521 | const BLOCK_SIZE plane_bsize = |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2522 | get_plane_block_size(AOMMAX(bsize, BLOCK_8X8), pd); |
Jingning Han | 9ca05b7 | 2017-01-03 14:41:36 -0800 | [diff] [blame] | 2523 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2524 | |
Jingning Han | 42a0fb3 | 2016-10-31 10:43:31 -0700 | [diff] [blame] | 2525 | const int num_4x4_w = |
| 2526 | block_size_wide[plane_bsize] >> tx_size_wide_log2[0]; |
| 2527 | const int num_4x4_h = |
| 2528 | block_size_high[plane_bsize] >> tx_size_wide_log2[0]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2529 | int row, col; |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 2530 | TOKEN_STATS token_stats; |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 2531 | init_token_stats(&token_stats); |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 2532 | |
Jingning Han | fe45b21 | 2016-11-22 10:30:23 -0800 | [diff] [blame] | 2533 | if (is_inter_block(mbmi)) { |
Sarah Parker | 106b3cb | 2017-04-21 12:13:37 -0700 | [diff] [blame] | 2534 | const TX_SIZE max_tx_size = get_vartx_max_txsize(mbmi, plane_bsize); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2535 | int block = 0; |
Jingning Han | 42a0fb3 | 2016-10-31 10:43:31 -0700 | [diff] [blame] | 2536 | const int step = |
| 2537 | tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size]; |
| 2538 | const int bkw = tx_size_wide_unit[max_tx_size]; |
| 2539 | const int bkh = tx_size_high_unit[max_tx_size]; |
| 2540 | for (row = 0; row < num_4x4_h; row += bkh) { |
| 2541 | for (col = 0; col < num_4x4_w; col += bkw) { |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 2542 | pack_txb_tokens(w, tok, tok_end, |
| 2543 | #if CONFIG_PVQ |
| 2544 | x, |
| 2545 | #endif |
| 2546 | xd, mbmi, plane, plane_bsize, cm->bit_depth, block, |
| 2547 | row, col, max_tx_size, &token_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2548 | block += step; |
| 2549 | } |
| 2550 | } |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 2551 | #if CONFIG_RD_DEBUG |
Angie Chiang | 3963d63 | 2016-11-10 18:41:40 -0800 | [diff] [blame] | 2552 | if (mbmi->sb_type >= BLOCK_8X8 && |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2553 | rd_token_stats_mismatch(&mbmi->rd_stats, &token_stats, plane)) { |
Angie Chiang | d02001d | 2016-11-06 15:31:49 -0800 | [diff] [blame] | 2554 | dump_mode_info(m); |
| 2555 | assert(0); |
| 2556 | } |
Jingning Han | fe45b21 | 2016-11-22 10:30:23 -0800 | [diff] [blame] | 2557 | #endif // CONFIG_RD_DEBUG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2558 | } else { |
Angie Chiang | 7fcfee4 | 2017-02-24 15:51:03 -0800 | [diff] [blame] | 2559 | TX_SIZE tx = get_tx_size(plane, xd); |
Jingning Han | 29a4120 | 2017-04-05 11:53:32 -0700 | [diff] [blame] | 2560 | #if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2 |
| 2561 | tx = AOMMAX(TX_4X4, tx); |
| 2562 | #endif |
Jingning Han | 42a0fb3 | 2016-10-31 10:43:31 -0700 | [diff] [blame] | 2563 | const int bkw = tx_size_wide_unit[tx]; |
| 2564 | const int bkh = tx_size_high_unit[tx]; |
Fangwen Fu | 33bcd11 | 2017-02-07 16:42:41 -0800 | [diff] [blame] | 2565 | for (row = 0; row < num_4x4_h; row += bkh) { |
| 2566 | for (col = 0; col < num_4x4_w; col += bkw) { |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 2567 | #if !CONFIG_PVQ |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 2568 | pack_mb_tokens(w, tok, tok_end, cm->bit_depth, tx, &token_stats); |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 2569 | #else |
| 2570 | pack_pvq_tokens(w, x, xd, plane, bsize, tx); |
| 2571 | #endif |
Fangwen Fu | 33bcd11 | 2017-02-07 16:42:41 -0800 | [diff] [blame] | 2572 | } |
| 2573 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2574 | } |
| 2575 | #else |
Angie Chiang | 7fcfee4 | 2017-02-24 15:51:03 -0800 | [diff] [blame] | 2576 | TX_SIZE tx = get_tx_size(plane, xd); |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 2577 | TOKEN_STATS token_stats; |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 2578 | #if !CONFIG_PVQ |
Angie Chiang | 3963d63 | 2016-11-10 18:41:40 -0800 | [diff] [blame] | 2579 | init_token_stats(&token_stats); |
Angie Chiang | c8af611 | 2017-03-16 16:11:22 -0700 | [diff] [blame] | 2580 | #if CONFIG_LV_MAP |
| 2581 | (void)tx; |
| 2582 | av1_write_coeffs_mb(cm, x, w, plane); |
| 2583 | #else // CONFIG_LV_MAP |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 2584 | pack_mb_tokens(w, tok, tok_end, cm->bit_depth, tx, &token_stats); |
Angie Chiang | c8af611 | 2017-03-16 16:11:22 -0700 | [diff] [blame] | 2585 | #endif // CONFIG_LV_MAP |
| 2586 | |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 2587 | #else |
| 2588 | (void)token_stats; |
| 2589 | pack_pvq_tokens(w, x, xd, plane, mbmi->sb_type, tx); |
| 2590 | #endif |
Angie Chiang | 3963d63 | 2016-11-10 18:41:40 -0800 | [diff] [blame] | 2591 | #if CONFIG_RD_DEBUG |
| 2592 | if (is_inter_block(mbmi) && mbmi->sb_type >= BLOCK_8X8 && |
Luc Trudeau | 3e32f1a | 2017-03-08 10:37:49 -0500 | [diff] [blame] | 2593 | rd_token_stats_mismatch(&mbmi->rd_stats, &token_stats, plane)) { |
Angie Chiang | 3963d63 | 2016-11-10 18:41:40 -0800 | [diff] [blame] | 2594 | dump_mode_info(m); |
| 2595 | assert(0); |
| 2596 | } |
Jingning Han | fe45b21 | 2016-11-22 10:30:23 -0800 | [diff] [blame] | 2597 | #endif // CONFIG_RD_DEBUG |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2598 | #endif // CONFIG_VAR_TX |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 2599 | |
Angie Chiang | 1dca126 | 2017-03-17 16:11:52 -0700 | [diff] [blame] | 2600 | #if !CONFIG_PVQ && !CONFIG_LV_MAP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2601 | assert(*tok < tok_end && (*tok)->token == EOSB_TOKEN); |
| 2602 | (*tok)++; |
Yushin Cho | 258a024 | 2017-03-06 13:53:01 -0800 | [diff] [blame] | 2603 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2604 | } |
| 2605 | } |
iole moccagatta | f25a4cf | 2016-11-11 23:57:57 -0800 | [diff] [blame] | 2606 | #endif // CONFIG_COEF_INTERLEAVE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2609 | #if CONFIG_MOTION_VAR && CONFIG_NCOBMC |
| 2610 | static void write_tokens_sb(AV1_COMP *cpi, const TileInfo *const tile, |
| 2611 | aom_writer *w, const TOKENEXTRA **tok, |
| 2612 | const TOKENEXTRA *const tok_end, int mi_row, |
| 2613 | int mi_col, BLOCK_SIZE bsize) { |
| 2614 | const AV1_COMMON *const cm = &cpi->common; |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 2615 | const int hbs = mi_size_wide[bsize] / 2; |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2616 | PARTITION_TYPE partition; |
| 2617 | BLOCK_SIZE subsize; |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 2618 | #if CONFIG_CB4X4 |
| 2619 | const int unify_bsize = 1; |
| 2620 | #else |
| 2621 | const int unify_bsize = 0; |
| 2622 | #endif |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2623 | |
| 2624 | if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return; |
| 2625 | |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 2626 | partition = get_partition(cm, mi_row, mi_col, bsize); |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2627 | subsize = get_subsize(bsize, partition); |
| 2628 | |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 2629 | if (subsize < BLOCK_8X8 && !unify_bsize) { |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2630 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); |
| 2631 | } else { |
| 2632 | switch (partition) { |
| 2633 | case PARTITION_NONE: |
| 2634 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); |
| 2635 | break; |
| 2636 | case PARTITION_HORZ: |
| 2637 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 2638 | if (mi_row + hbs < cm->mi_rows) |
| 2639 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col); |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2640 | break; |
| 2641 | case PARTITION_VERT: |
| 2642 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 2643 | if (mi_col + hbs < cm->mi_cols) |
| 2644 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col + hbs); |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2645 | break; |
| 2646 | case PARTITION_SPLIT: |
| 2647 | write_tokens_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col, subsize); |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 2648 | write_tokens_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col + hbs, |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2649 | subsize); |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 2650 | write_tokens_sb(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col, |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2651 | subsize); |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 2652 | write_tokens_sb(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs, |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2653 | subsize); |
| 2654 | break; |
Yue Chen | f27b160 | 2017-01-13 11:11:43 -0800 | [diff] [blame] | 2655 | #if CONFIG_EXT_PARTITION_TYPES |
| 2656 | case PARTITION_HORZ_A: |
| 2657 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); |
| 2658 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col + hbs); |
| 2659 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col); |
| 2660 | break; |
| 2661 | case PARTITION_HORZ_B: |
| 2662 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); |
| 2663 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col); |
| 2664 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs); |
| 2665 | break; |
| 2666 | case PARTITION_VERT_A: |
| 2667 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); |
| 2668 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col); |
| 2669 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col + hbs); |
| 2670 | break; |
| 2671 | case PARTITION_VERT_B: |
| 2672 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); |
| 2673 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col + hbs); |
| 2674 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row + hbs, mi_col + hbs); |
| 2675 | break; |
| 2676 | #endif // CONFIG_EXT_PARTITION_TYPES |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2677 | default: assert(0); |
| 2678 | } |
| 2679 | } |
| 2680 | } |
| 2681 | #endif |
| 2682 | |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2683 | static void write_modes_b(AV1_COMP *cpi, const TileInfo *const tile, |
| 2684 | aom_writer *w, const TOKENEXTRA **tok, |
| 2685 | const TOKENEXTRA *const tok_end, |
| 2686 | #if CONFIG_SUPERTX |
| 2687 | int supertx_enabled, |
| 2688 | #endif |
| 2689 | int mi_row, int mi_col) { |
| 2690 | write_mbmi_b(cpi, tile, w, |
| 2691 | #if CONFIG_SUPERTX |
| 2692 | supertx_enabled, |
| 2693 | #endif |
| 2694 | mi_row, mi_col); |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2695 | #if CONFIG_MOTION_VAR && CONFIG_NCOBMC |
| 2696 | (void)tok; |
| 2697 | (void)tok_end; |
| 2698 | #else |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2699 | #if !CONFIG_PVQ && CONFIG_SUPERTX |
| 2700 | if (!supertx_enabled) |
| 2701 | #endif |
| 2702 | write_tokens_b(cpi, tile, w, tok, tok_end, mi_row, mi_col); |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 2703 | #endif |
Yue Chen | 64550b6 | 2017-01-12 12:18:22 -0800 | [diff] [blame] | 2704 | } |
| 2705 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2706 | static void write_partition(const AV1_COMMON *const cm, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2707 | const MACROBLOCKD *const xd, int hbs, int mi_row, |
| 2708 | int mi_col, PARTITION_TYPE p, BLOCK_SIZE bsize, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2709 | aom_writer *w) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2710 | const int has_rows = (mi_row + hbs) < cm->mi_rows; |
| 2711 | const int has_cols = (mi_col + hbs) < cm->mi_cols; |
Alex Converse | 55c6bde | 2017-01-12 15:55:31 -0800 | [diff] [blame] | 2712 | const int is_partition_point = bsize >= BLOCK_8X8; |
| 2713 | const int ctx = is_partition_point |
| 2714 | ? partition_plane_context(xd, mi_row, mi_col, |
| 2715 | #if CONFIG_UNPOISON_PARTITION_CTX |
| 2716 | has_rows, has_cols, |
| 2717 | #endif |
| 2718 | bsize) |
| 2719 | : 0; |
| 2720 | #if CONFIG_UNPOISON_PARTITION_CTX |
Alex Converse | 2b9d19d | 2017-04-03 11:11:17 -0700 | [diff] [blame] | 2721 | const aom_prob *const probs = |
| 2722 | ctx < PARTITION_CONTEXTS ? cm->fc->partition_prob[ctx] : NULL; |
Alex Converse | 55c6bde | 2017-01-12 15:55:31 -0800 | [diff] [blame] | 2723 | #else |
| 2724 | const aom_prob *const probs = cm->fc->partition_prob[ctx]; |
| 2725 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2726 | |
Thomas Davies | c2ec0e4 | 2017-01-11 16:27:27 +0000 | [diff] [blame] | 2727 | #if CONFIG_EC_ADAPT |
| 2728 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 2729 | (void)cm; |
| 2730 | #elif CONFIG_EC_MULTISYMBOL |
| 2731 | FRAME_CONTEXT *ec_ctx = cm->fc; |
| 2732 | #endif |
| 2733 | |
Jingning Han | bf9c6b7 | 2016-12-14 14:50:45 -0800 | [diff] [blame] | 2734 | if (!is_partition_point) return; |
| 2735 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2736 | if (has_rows && has_cols) { |
| 2737 | #if CONFIG_EXT_PARTITION_TYPES |
| 2738 | if (bsize <= BLOCK_8X8) |
Thomas Davies | c2ec0e4 | 2017-01-11 16:27:27 +0000 | [diff] [blame] | 2739 | #if CONFIG_EC_MULTISYMBOL |
| 2740 | aom_write_symbol(w, p, ec_ctx->partition_cdf[ctx], PARTITION_TYPES); |
| 2741 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2742 | av1_write_token(w, av1_partition_tree, probs, &partition_encodings[p]); |
Thomas Davies | c2ec0e4 | 2017-01-11 16:27:27 +0000 | [diff] [blame] | 2743 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2744 | else |
Alex Converse | 57795a4 | 2017-03-14 12:18:25 -0700 | [diff] [blame] | 2745 | #if CONFIG_EC_MULTISYMBOL |
| 2746 | aom_write_symbol(w, p, ec_ctx->partition_cdf[ctx], EXT_PARTITION_TYPES); |
| 2747 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2748 | av1_write_token(w, av1_ext_partition_tree, probs, |
| 2749 | &ext_partition_encodings[p]); |
Alex Converse | 57795a4 | 2017-03-14 12:18:25 -0700 | [diff] [blame] | 2750 | #endif // CONFIG_EC_MULTISYMBOL |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2751 | #else |
Nathan E. Egge | 9d9eb6c | 2016-11-16 09:44:26 -0500 | [diff] [blame] | 2752 | #if CONFIG_EC_MULTISYMBOL |
Thomas Davies | c2ec0e4 | 2017-01-11 16:27:27 +0000 | [diff] [blame] | 2753 | aom_write_symbol(w, p, ec_ctx->partition_cdf[ctx], PARTITION_TYPES); |
Nathan E. Egge | fba2be6 | 2016-05-03 09:48:54 -0400 | [diff] [blame] | 2754 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2755 | av1_write_token(w, av1_partition_tree, probs, &partition_encodings[p]); |
Nathan E. Egge | fba2be6 | 2016-05-03 09:48:54 -0400 | [diff] [blame] | 2756 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2757 | #endif // CONFIG_EXT_PARTITION_TYPES |
| 2758 | } else if (!has_rows && has_cols) { |
| 2759 | assert(p == PARTITION_SPLIT || p == PARTITION_HORZ); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2760 | aom_write(w, p == PARTITION_SPLIT, probs[1]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2761 | } else if (has_rows && !has_cols) { |
| 2762 | assert(p == PARTITION_SPLIT || p == PARTITION_VERT); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2763 | aom_write(w, p == PARTITION_SPLIT, probs[2]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2764 | } else { |
| 2765 | assert(p == PARTITION_SPLIT); |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | #if CONFIG_SUPERTX |
| 2770 | #define write_modes_sb_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, \ |
| 2771 | mi_row, mi_col, bsize) \ |
| 2772 | write_modes_sb(cpi, tile, w, tok, tok_end, supertx_enabled, mi_row, mi_col, \ |
| 2773 | bsize) |
| 2774 | #else |
| 2775 | #define write_modes_sb_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, \ |
| 2776 | mi_row, mi_col, bsize) \ |
| 2777 | write_modes_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col, bsize) |
Alex Converse | ec6fb64 | 2016-10-19 11:31:48 -0700 | [diff] [blame] | 2778 | #endif // CONFIG_SUPERTX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2779 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2780 | static void write_modes_sb(AV1_COMP *const cpi, const TileInfo *const tile, |
| 2781 | aom_writer *const w, const TOKENEXTRA **tok, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2782 | const TOKENEXTRA *const tok_end, |
| 2783 | #if CONFIG_SUPERTX |
| 2784 | int supertx_enabled, |
| 2785 | #endif |
| 2786 | int mi_row, int mi_col, BLOCK_SIZE bsize) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2787 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2788 | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
Jingning Han | c709e1f | 2016-12-06 14:48:09 -0800 | [diff] [blame] | 2789 | const int hbs = mi_size_wide[bsize] / 2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2790 | const PARTITION_TYPE partition = get_partition(cm, mi_row, mi_col, bsize); |
| 2791 | const BLOCK_SIZE subsize = get_subsize(bsize, partition); |
Jingning Han | 5226184 | 2016-12-14 12:17:49 -0800 | [diff] [blame] | 2792 | #if CONFIG_CB4X4 |
| 2793 | const int unify_bsize = 1; |
| 2794 | #else |
| 2795 | const int unify_bsize = 0; |
| 2796 | #endif |
| 2797 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2798 | #if CONFIG_SUPERTX |
| 2799 | const int mi_offset = mi_row * cm->mi_stride + mi_col; |
| 2800 | MB_MODE_INFO *mbmi; |
| 2801 | const int pack_token = !supertx_enabled; |
| 2802 | TX_SIZE supertx_size; |
| 2803 | int plane; |
| 2804 | #endif |
| 2805 | |
| 2806 | if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return; |
| 2807 | |
| 2808 | write_partition(cm, xd, hbs, mi_row, mi_col, partition, bsize, w); |
| 2809 | #if CONFIG_SUPERTX |
| 2810 | mbmi = &cm->mi_grid_visible[mi_offset]->mbmi; |
| 2811 | xd->mi = cm->mi_grid_visible + mi_offset; |
Urvang Joshi | 359dc2b | 2017-04-27 15:41:47 -0700 | [diff] [blame] | 2812 | set_mi_row_col(xd, tile, mi_row, mi_size_high[bsize], mi_col, |
| 2813 | mi_size_wide[bsize], |
Fangwen Fu | 7b9f2b3 | 2017-01-17 14:01:52 -0800 | [diff] [blame] | 2814 | #if CONFIG_DEPENDENT_HORZTILES |
Urvang Joshi | 359dc2b | 2017-04-27 15:41:47 -0700 | [diff] [blame] | 2815 | cm->dependent_horz_tiles, |
| 2816 | #endif // CONFIG_DEPENDENT_HORZTILES |
| 2817 | cm->mi_rows, cm->mi_cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2818 | if (!supertx_enabled && !frame_is_intra_only(cm) && |
| 2819 | partition != PARTITION_NONE && bsize <= MAX_SUPERTX_BLOCK_SIZE && |
| 2820 | !xd->lossless[0]) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2821 | aom_prob prob; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2822 | supertx_size = max_txsize_lookup[bsize]; |
| 2823 | prob = cm->fc->supertx_prob[partition_supertx_context_lookup[partition]] |
| 2824 | [supertx_size]; |
| 2825 | supertx_enabled = (xd->mi[0]->mbmi.tx_size == supertx_size); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2826 | aom_write(w, supertx_enabled, prob); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2827 | } |
| 2828 | #endif // CONFIG_SUPERTX |
Jingning Han | 5226184 | 2016-12-14 12:17:49 -0800 | [diff] [blame] | 2829 | if (subsize < BLOCK_8X8 && !unify_bsize) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2830 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, mi_row, |
| 2831 | mi_col); |
| 2832 | } else { |
| 2833 | switch (partition) { |
| 2834 | case PARTITION_NONE: |
| 2835 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2836 | mi_row, mi_col); |
| 2837 | break; |
| 2838 | case PARTITION_HORZ: |
| 2839 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2840 | mi_row, mi_col); |
| 2841 | if (mi_row + hbs < cm->mi_rows) |
| 2842 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2843 | mi_row + hbs, mi_col); |
| 2844 | break; |
| 2845 | case PARTITION_VERT: |
| 2846 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2847 | mi_row, mi_col); |
| 2848 | if (mi_col + hbs < cm->mi_cols) |
| 2849 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2850 | mi_row, mi_col + hbs); |
| 2851 | break; |
| 2852 | case PARTITION_SPLIT: |
| 2853 | write_modes_sb_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2854 | mi_row, mi_col, subsize); |
| 2855 | write_modes_sb_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2856 | mi_row, mi_col + hbs, subsize); |
| 2857 | write_modes_sb_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2858 | mi_row + hbs, mi_col, subsize); |
| 2859 | write_modes_sb_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2860 | mi_row + hbs, mi_col + hbs, subsize); |
| 2861 | break; |
| 2862 | #if CONFIG_EXT_PARTITION_TYPES |
| 2863 | case PARTITION_HORZ_A: |
| 2864 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2865 | mi_row, mi_col); |
| 2866 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2867 | mi_row, mi_col + hbs); |
| 2868 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2869 | mi_row + hbs, mi_col); |
| 2870 | break; |
| 2871 | case PARTITION_HORZ_B: |
| 2872 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2873 | mi_row, mi_col); |
| 2874 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2875 | mi_row + hbs, mi_col); |
| 2876 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2877 | mi_row + hbs, mi_col + hbs); |
| 2878 | break; |
| 2879 | case PARTITION_VERT_A: |
| 2880 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2881 | mi_row, mi_col); |
| 2882 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2883 | mi_row + hbs, mi_col); |
| 2884 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2885 | mi_row, mi_col + hbs); |
| 2886 | break; |
| 2887 | case PARTITION_VERT_B: |
| 2888 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2889 | mi_row, mi_col); |
| 2890 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2891 | mi_row, mi_col + hbs); |
| 2892 | write_modes_b_wrapper(cpi, tile, w, tok, tok_end, supertx_enabled, |
| 2893 | mi_row + hbs, mi_col + hbs); |
| 2894 | break; |
| 2895 | #endif // CONFIG_EXT_PARTITION_TYPES |
| 2896 | default: assert(0); |
| 2897 | } |
| 2898 | } |
| 2899 | #if CONFIG_SUPERTX |
| 2900 | if (partition != PARTITION_NONE && supertx_enabled && pack_token) { |
| 2901 | int skip; |
Jingning Han | 5b7706a | 2016-12-21 09:55:10 -0800 | [diff] [blame] | 2902 | const int bsw = mi_size_wide[bsize]; |
| 2903 | const int bsh = mi_size_high[bsize]; |
Yaowu Xu | 41a36de | 2017-03-23 15:55:03 -0700 | [diff] [blame] | 2904 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2905 | xd->mi = cm->mi_grid_visible + mi_offset; |
| 2906 | supertx_size = mbmi->tx_size; |
Urvang Joshi | 359dc2b | 2017-04-27 15:41:47 -0700 | [diff] [blame] | 2907 | set_mi_row_col(xd, tile, mi_row, bsh, mi_col, bsw, |
Fangwen Fu | 7b9f2b3 | 2017-01-17 14:01:52 -0800 | [diff] [blame] | 2908 | #if CONFIG_DEPENDENT_HORZTILES |
Urvang Joshi | 359dc2b | 2017-04-27 15:41:47 -0700 | [diff] [blame] | 2909 | cm->dependent_horz_tiles, |
| 2910 | #endif // CONFIG_DEPENDENT_HORZTILES |
| 2911 | cm->mi_rows, cm->mi_cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2912 | |
| 2913 | assert(IMPLIES(!cm->seg.enabled, mbmi->segment_id_supertx == 0)); |
| 2914 | assert(mbmi->segment_id_supertx < MAX_SEGMENTS); |
| 2915 | |
| 2916 | skip = write_skip(cm, xd, mbmi->segment_id_supertx, xd->mi[0], w); |
| 2917 | #if CONFIG_EXT_TX |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 2918 | if (get_ext_tx_types(supertx_size, bsize, 1, cm->reduced_tx_set_used) > 1 && |
| 2919 | !skip) { |
| 2920 | const int eset = |
| 2921 | get_ext_tx_set(supertx_size, bsize, 1, cm->reduced_tx_set_used); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2922 | if (eset > 0) { |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 2923 | #if CONFIG_EC_MULTISYMBOL |
Yaowu Xu | 41a36de | 2017-03-23 15:55:03 -0700 | [diff] [blame] | 2924 | #if CONFIG_EC_ADAPT |
| 2925 | FRAME_CONTEXT *ec_ctx = xd->tile_ctx; |
| 2926 | #else |
| 2927 | FRAME_CONTEXT *ec_ctx = cm->fc; |
| 2928 | #endif |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 2929 | aom_write_symbol(w, av1_ext_tx_inter_ind[eset][mbmi->tx_type], |
| 2930 | ec_ctx->inter_ext_tx_cdf[eset][supertx_size], |
| 2931 | ext_tx_cnt_inter[eset]); |
| 2932 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2933 | av1_write_token(w, av1_ext_tx_inter_tree[eset], |
| 2934 | cm->fc->inter_ext_tx_prob[eset][supertx_size], |
| 2935 | &ext_tx_inter_encodings[eset][mbmi->tx_type]); |
Thomas Davies | b1bedf5 | 2017-03-17 14:03:28 +0000 | [diff] [blame] | 2936 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2937 | } |
| 2938 | } |
| 2939 | #else |
| 2940 | if (supertx_size < TX_32X32 && !skip) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 2941 | av1_write_token(w, av1_ext_tx_tree, |
| 2942 | cm->fc->inter_ext_tx_prob[supertx_size], |
| 2943 | &ext_tx_encodings[mbmi->tx_type]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2944 | } |
| 2945 | #endif // CONFIG_EXT_TX |
| 2946 | |
| 2947 | if (!skip) { |
| 2948 | assert(*tok < tok_end); |
| 2949 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
Jingning Han | 5b7706a | 2016-12-21 09:55:10 -0800 | [diff] [blame] | 2950 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2951 | const int mbmi_txb_size = txsize_to_bsize[mbmi->tx_size]; |
Jingning Han | 5b7706a | 2016-12-21 09:55:10 -0800 | [diff] [blame] | 2952 | const BLOCK_SIZE plane_bsize = get_plane_block_size(mbmi_txb_size, pd); |
| 2953 | |
| 2954 | const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane); |
| 2955 | const int max_blocks_high = max_block_high(xd, plane_bsize, plane); |
| 2956 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2957 | int row, col; |
Angie Chiang | 7fcfee4 | 2017-02-24 15:51:03 -0800 | [diff] [blame] | 2958 | TX_SIZE tx = get_tx_size(plane, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2959 | BLOCK_SIZE txb_size = txsize_to_bsize[tx]; |
Jingning Han | 5b7706a | 2016-12-21 09:55:10 -0800 | [diff] [blame] | 2960 | |
| 2961 | const int stepr = tx_size_high_unit[txb_size]; |
| 2962 | const int stepc = tx_size_wide_unit[txb_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2963 | |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 2964 | TOKEN_STATS token_stats; |
| 2965 | token_stats.cost = 0; |
Jingning Han | 5b7706a | 2016-12-21 09:55:10 -0800 | [diff] [blame] | 2966 | for (row = 0; row < max_blocks_high; row += stepr) |
| 2967 | for (col = 0; col < max_blocks_wide; col += stepc) |
Angie Chiang | d402282 | 2016-11-02 18:30:25 -0700 | [diff] [blame] | 2968 | pack_mb_tokens(w, tok, tok_end, cm->bit_depth, tx, &token_stats); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2969 | assert(*tok < tok_end && (*tok)->token == EOSB_TOKEN); |
| 2970 | (*tok)++; |
| 2971 | } |
| 2972 | } |
Jingning Han | e92bf1c | 2016-11-21 10:41:56 -0800 | [diff] [blame] | 2973 | #if CONFIG_VAR_TX |
| 2974 | xd->above_txfm_context = cm->above_txfm_context + mi_col; |
| 2975 | xd->left_txfm_context = |
| 2976 | xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK); |
| 2977 | set_txfm_ctxs(xd->mi[0]->mbmi.tx_size, bsw, bsh, skip, xd); |
| 2978 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2979 | } |
| 2980 | #endif // CONFIG_SUPERTX |
| 2981 | |
| 2982 | // update partition context |
| 2983 | #if CONFIG_EXT_PARTITION_TYPES |
| 2984 | update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition); |
| 2985 | #else |
| 2986 | if (bsize >= BLOCK_8X8 && |
| 2987 | (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) |
| 2988 | update_partition_context(xd, mi_row, mi_col, subsize, bsize); |
David Barker | f8935c9 | 2016-10-26 14:54:06 +0100 | [diff] [blame] | 2989 | #endif // CONFIG_EXT_PARTITION_TYPES |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 2990 | |
Jean-Marc Valin | 0143513 | 2017-02-18 14:12:53 -0500 | [diff] [blame] | 2991 | #if CONFIG_CDEF |
David Barker | 9739f36 | 2016-11-10 09:29:32 +0000 | [diff] [blame] | 2992 | #if CONFIG_EXT_PARTITION |
| 2993 | if (cm->sb_size == BLOCK_128X128 && bsize == BLOCK_128X128 && |
Steinar Midtskogen | a9d41e8 | 2017-03-17 12:48:15 +0100 | [diff] [blame] | 2994 | !sb_all_skip(cm, mi_row, mi_col)) { |
Steinar Midtskogen | a9d41e8 | 2017-03-17 12:48:15 +0100 | [diff] [blame] | 2995 | aom_write_literal(w, cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col] |
Jean-Marc Valin | 1df427e | 2017-03-23 18:32:57 -0400 | [diff] [blame] | 2996 | ->mbmi.cdef_strength, |
| 2997 | cm->cdef_bits); |
David Barker | 9739f36 | 2016-11-10 09:29:32 +0000 | [diff] [blame] | 2998 | } else if (cm->sb_size == BLOCK_64X64 && bsize == BLOCK_64X64 && |
| 2999 | #else |
| 3000 | if (bsize == BLOCK_64X64 && |
| 3001 | #endif // CONFIG_EXT_PARTITION |
Steinar Midtskogen | a9d41e8 | 2017-03-17 12:48:15 +0100 | [diff] [blame] | 3002 | !sb_all_skip(cm, mi_row, mi_col)) { |
Jean-Marc Valin | 5f5c132 | 2017-03-21 16:20:21 -0400 | [diff] [blame] | 3003 | if (cm->cdef_bits != 0) |
Steinar Midtskogen | a9d41e8 | 2017-03-17 12:48:15 +0100 | [diff] [blame] | 3004 | aom_write_literal(w, cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col] |
Jean-Marc Valin | 5f5c132 | 2017-03-21 16:20:21 -0400 | [diff] [blame] | 3005 | ->mbmi.cdef_strength, |
| 3006 | cm->cdef_bits); |
Steinar Midtskogen | 5d56f4d | 2016-09-25 09:23:16 +0200 | [diff] [blame] | 3007 | } |
| 3008 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3011 | static void write_modes(AV1_COMP *const cpi, const TileInfo *const tile, |
| 3012 | aom_writer *const w, const TOKENEXTRA **tok, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3013 | const TOKENEXTRA *const tok_end) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3014 | AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3015 | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
| 3016 | const int mi_row_start = tile->mi_row_start; |
| 3017 | const int mi_row_end = tile->mi_row_end; |
| 3018 | const int mi_col_start = tile->mi_col_start; |
| 3019 | const int mi_col_end = tile->mi_col_end; |
| 3020 | int mi_row, mi_col; |
Fangwen Fu | 7b9f2b3 | 2017-01-17 14:01:52 -0800 | [diff] [blame] | 3021 | |
| 3022 | #if CONFIG_DEPENDENT_HORZTILES |
Fangwen Fu | 73126c0 | 2017-02-08 22:37:47 -0800 | [diff] [blame] | 3023 | #if CONFIG_TILE_GROUPS |
| 3024 | if (!cm->dependent_horz_tiles || mi_row_start == 0 || |
| 3025 | tile->tg_horz_boundary) { |
| 3026 | #else |
Fangwen Fu | 7b9f2b3 | 2017-01-17 14:01:52 -0800 | [diff] [blame] | 3027 | if (!cm->dependent_horz_tiles || mi_row_start == 0) { |
Fangwen Fu | 73126c0 | 2017-02-08 22:37:47 -0800 | [diff] [blame] | 3028 | #endif |
Fangwen Fu | 7b9f2b3 | 2017-01-17 14:01:52 -0800 | [diff] [blame] | 3029 | av1_zero_above_context(cm, mi_col_start, mi_col_end); |
| 3030 | } |
| 3031 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3032 | av1_zero_above_context(cm, mi_col_start, mi_col_end); |
Fangwen Fu | 7b9f2b3 | 2017-01-17 14:01:52 -0800 | [diff] [blame] | 3033 | #endif |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3034 | #if CONFIG_PVQ |
| 3035 | assert(cpi->td.mb.pvq_q->curr_pos == 0); |
| 3036 | #endif |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 3037 | #if CONFIG_DELTA_Q |
| 3038 | if (cpi->common.delta_q_present_flag) { |
| 3039 | xd->prev_qindex = cpi->common.base_qindex; |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 3040 | #if CONFIG_EXT_DELTA_Q |
| 3041 | if (cpi->common.delta_lf_present_flag) { |
| 3042 | xd->prev_delta_lf_from_base = 0; |
| 3043 | } |
| 3044 | #endif // CONFIG_EXT_DELTA_Q |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 3045 | } |
| 3046 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3047 | |
| 3048 | for (mi_row = mi_row_start; mi_row < mi_row_end; mi_row += cm->mib_size) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3049 | av1_zero_left_context(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3050 | |
| 3051 | for (mi_col = mi_col_start; mi_col < mi_col_end; mi_col += cm->mib_size) { |
| 3052 | write_modes_sb_wrapper(cpi, tile, w, tok, tok_end, 0, mi_row, mi_col, |
| 3053 | cm->sb_size); |
Yue Chen | 9ab6d71 | 2017-01-12 15:50:46 -0800 | [diff] [blame] | 3054 | #if CONFIG_MOTION_VAR && CONFIG_NCOBMC |
| 3055 | write_tokens_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col, cm->sb_size); |
| 3056 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3057 | } |
| 3058 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 3059 | #if CONFIG_PVQ |
| 3060 | // Check that the number of PVQ blocks encoded and written to the bitstream |
| 3061 | // are the same |
| 3062 | assert(cpi->td.mb.pvq_q->curr_pos == cpi->td.mb.pvq_q->last_pos); |
| 3063 | // Reset curr_pos in case we repack the bitstream |
| 3064 | cpi->td.mb.pvq_q->curr_pos = 0; |
| 3065 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3066 | } |
| 3067 | |
Angie Chiang | 800df03 | 2017-03-22 11:14:12 -0700 | [diff] [blame] | 3068 | #if !CONFIG_LV_MAP |
Alex Converse | a9598cd | 2017-02-03 14:18:05 -0800 | [diff] [blame] | 3069 | #if !CONFIG_PVQ && !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3070 | static void build_tree_distribution(AV1_COMP *cpi, TX_SIZE tx_size, |
| 3071 | av1_coeff_stats *coef_branch_ct, |
| 3072 | av1_coeff_probs_model *coef_probs) { |
| 3073 | av1_coeff_count *coef_counts = cpi->td.rd_counts.coef_counts[tx_size]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3074 | unsigned int(*eob_branch_ct)[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS] = |
| 3075 | cpi->common.counts.eob_branch[tx_size]; |
| 3076 | int i, j, k, l, m; |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3077 | #if CONFIG_RECT_TX |
| 3078 | assert(!is_rect_tx(tx_size)); |
| 3079 | #endif // CONFIG_RECT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3080 | |
| 3081 | for (i = 0; i < PLANE_TYPES; ++i) { |
| 3082 | for (j = 0; j < REF_TYPES; ++j) { |
| 3083 | for (k = 0; k < COEF_BANDS; ++k) { |
| 3084 | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3085 | av1_tree_probs_from_distribution(av1_coef_tree, |
| 3086 | coef_branch_ct[i][j][k][l], |
| 3087 | coef_counts[i][j][k][l]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3088 | coef_branch_ct[i][j][k][l][0][1] = |
| 3089 | eob_branch_ct[i][j][k][l] - coef_branch_ct[i][j][k][l][0][0]; |
| 3090 | for (m = 0; m < UNCONSTRAINED_NODES; ++m) |
| 3091 | coef_probs[i][j][k][l][m] = |
| 3092 | get_binary_prob(coef_branch_ct[i][j][k][l][m][0], |
| 3093 | coef_branch_ct[i][j][k][l][m][1]); |
| 3094 | } |
| 3095 | } |
| 3096 | } |
| 3097 | } |
| 3098 | } |
| 3099 | |
Alex Converse | a9598cd | 2017-02-03 14:18:05 -0800 | [diff] [blame] | 3100 | #if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3101 | static void update_coef_probs_common(aom_writer *const bc, AV1_COMP *cpi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3102 | TX_SIZE tx_size, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3103 | av1_coeff_stats *frame_branch_ct, |
| 3104 | av1_coeff_probs_model *new_coef_probs) { |
| 3105 | av1_coeff_probs_model *old_coef_probs = cpi->common.fc->coef_probs[tx_size]; |
| 3106 | const aom_prob upd = DIFF_UPDATE_PROB; |
Alex Converse | a9598cd | 2017-02-03 14:18:05 -0800 | [diff] [blame] | 3107 | #if CONFIG_EC_ADAPT |
| 3108 | const int entropy_nodes_update = UNCONSTRAINED_NODES - 1; |
| 3109 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3110 | const int entropy_nodes_update = UNCONSTRAINED_NODES; |
Alex Converse | a9598cd | 2017-02-03 14:18:05 -0800 | [diff] [blame] | 3111 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3112 | int i, j, k, l, t; |
| 3113 | int stepsize = cpi->sf.coeff_prob_appx_step; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 3114 | #if CONFIG_TILE_GROUPS |
| 3115 | const int probwt = cpi->common.num_tg; |
| 3116 | #else |
| 3117 | const int probwt = 1; |
| 3118 | #endif |
Urvang Joshi | feb925f | 2016-12-05 10:37:29 -0800 | [diff] [blame] | 3119 | #if CONFIG_RECT_TX |
| 3120 | assert(!is_rect_tx(tx_size)); |
| 3121 | #endif // CONFIG_RECT_TX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3122 | |
| 3123 | switch (cpi->sf.use_fast_coef_updates) { |
| 3124 | case TWO_LOOP: { |
| 3125 | /* dry run to see if there is any update at all needed */ |
| 3126 | int savings = 0; |
| 3127 | int update[2] = { 0, 0 }; |
| 3128 | for (i = 0; i < PLANE_TYPES; ++i) { |
| 3129 | for (j = 0; j < REF_TYPES; ++j) { |
| 3130 | for (k = 0; k < COEF_BANDS; ++k) { |
| 3131 | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
| 3132 | for (t = 0; t < entropy_nodes_update; ++t) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3133 | aom_prob newp = new_coef_probs[i][j][k][l][t]; |
| 3134 | const aom_prob oldp = old_coef_probs[i][j][k][l][t]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3135 | int s; |
| 3136 | int u = 0; |
| 3137 | if (t == PIVOT_NODE) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3138 | s = av1_prob_diff_update_savings_search_model( |
Alex Converse | a9ce4b7 | 2016-05-25 10:28:03 -0700 | [diff] [blame] | 3139 | frame_branch_ct[i][j][k][l][0], oldp, &newp, upd, |
| 3140 | stepsize, probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3141 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3142 | s = av1_prob_diff_update_savings_search( |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 3143 | frame_branch_ct[i][j][k][l][t], oldp, &newp, upd, probwt); |
| 3144 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3145 | if (s > 0 && newp != oldp) u = 1; |
| 3146 | if (u) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3147 | savings += s - (int)(av1_cost_zero(upd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3148 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3149 | savings -= (int)(av1_cost_zero(upd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3150 | update[u]++; |
| 3151 | } |
| 3152 | } |
| 3153 | } |
| 3154 | } |
| 3155 | } |
| 3156 | |
| 3157 | /* Is coef updated at all */ |
| 3158 | if (update[1] == 0 || savings < 0) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3159 | aom_write_bit(bc, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3160 | return; |
| 3161 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3162 | aom_write_bit(bc, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3163 | for (i = 0; i < PLANE_TYPES; ++i) { |
| 3164 | for (j = 0; j < REF_TYPES; ++j) { |
| 3165 | for (k = 0; k < COEF_BANDS; ++k) { |
| 3166 | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
| 3167 | // calc probs and branch cts for this frame only |
| 3168 | for (t = 0; t < entropy_nodes_update; ++t) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3169 | aom_prob newp = new_coef_probs[i][j][k][l][t]; |
| 3170 | aom_prob *oldp = old_coef_probs[i][j][k][l] + t; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3171 | int s; |
| 3172 | int u = 0; |
| 3173 | if (t == PIVOT_NODE) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3174 | s = av1_prob_diff_update_savings_search_model( |
Alex Converse | a9ce4b7 | 2016-05-25 10:28:03 -0700 | [diff] [blame] | 3175 | frame_branch_ct[i][j][k][l][0], *oldp, &newp, upd, |
| 3176 | stepsize, probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3177 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3178 | s = av1_prob_diff_update_savings_search( |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 3179 | frame_branch_ct[i][j][k][l][t], *oldp, &newp, upd, |
| 3180 | probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3181 | if (s > 0 && newp != *oldp) u = 1; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3182 | aom_write(bc, u, upd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3183 | if (u) { |
| 3184 | /* send/use new probability */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3185 | av1_write_prob_diff_update(bc, newp, *oldp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3186 | *oldp = newp; |
| 3187 | } |
| 3188 | } |
| 3189 | } |
| 3190 | } |
| 3191 | } |
| 3192 | } |
| 3193 | return; |
| 3194 | } |
| 3195 | |
| 3196 | case ONE_LOOP_REDUCED: { |
| 3197 | int updates = 0; |
| 3198 | int noupdates_before_first = 0; |
| 3199 | for (i = 0; i < PLANE_TYPES; ++i) { |
| 3200 | for (j = 0; j < REF_TYPES; ++j) { |
| 3201 | for (k = 0; k < COEF_BANDS; ++k) { |
| 3202 | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
| 3203 | // calc probs and branch cts for this frame only |
| 3204 | for (t = 0; t < entropy_nodes_update; ++t) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3205 | aom_prob newp = new_coef_probs[i][j][k][l][t]; |
| 3206 | aom_prob *oldp = old_coef_probs[i][j][k][l] + t; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3207 | int s; |
| 3208 | int u = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3209 | if (t == PIVOT_NODE) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3210 | s = av1_prob_diff_update_savings_search_model( |
Alex Converse | a9ce4b7 | 2016-05-25 10:28:03 -0700 | [diff] [blame] | 3211 | frame_branch_ct[i][j][k][l][0], *oldp, &newp, upd, |
| 3212 | stepsize, probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3213 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3214 | s = av1_prob_diff_update_savings_search( |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 3215 | frame_branch_ct[i][j][k][l][t], *oldp, &newp, upd, |
| 3216 | probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | if (s > 0 && newp != *oldp) u = 1; |
| 3220 | updates += u; |
| 3221 | if (u == 0 && updates == 0) { |
| 3222 | noupdates_before_first++; |
| 3223 | continue; |
| 3224 | } |
| 3225 | if (u == 1 && updates == 1) { |
| 3226 | int v; |
| 3227 | // first update |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3228 | aom_write_bit(bc, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3229 | for (v = 0; v < noupdates_before_first; ++v) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3230 | aom_write(bc, 0, upd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3231 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3232 | aom_write(bc, u, upd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3233 | if (u) { |
| 3234 | /* send/use new probability */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3235 | av1_write_prob_diff_update(bc, newp, *oldp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3236 | *oldp = newp; |
| 3237 | } |
| 3238 | } |
| 3239 | } |
| 3240 | } |
| 3241 | } |
| 3242 | } |
| 3243 | if (updates == 0) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3244 | aom_write_bit(bc, 0); // no updates |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3245 | } |
| 3246 | return; |
| 3247 | } |
| 3248 | default: assert(0); |
| 3249 | } |
| 3250 | } |
Thomas Davies | fc1598a | 2017-01-13 17:07:25 +0000 | [diff] [blame] | 3251 | #endif |
hui su | 0d10357 | 2017-03-01 17:58:01 -0800 | [diff] [blame] | 3252 | #if CONFIG_SUBFRAME_PROB_UPDATE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3253 | // Calculate the token counts between subsequent subframe updates. |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 3254 | static void get_coef_counts_diff( |
| 3255 | AV1_COMP *cpi, int index, |
| 3256 | av1_coeff_count coef_counts[TX_SIZES][PLANE_TYPES], |
| 3257 | unsigned int eob_counts[TX_SIZES][PLANE_TYPES][REF_TYPES][COEF_BANDS] |
| 3258 | [COEFF_CONTEXTS]) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3259 | int i, j, k, l, m, tx_size, val; |
| 3260 | const int max_idx = cpi->common.coef_probs_update_idx; |
| 3261 | const TX_MODE tx_mode = cpi->common.tx_mode; |
Urvang Joshi | cb586f3 | 2016-09-20 11:36:33 -0700 | [diff] [blame] | 3262 | const int max_tx_size = tx_mode_to_biggest_tx_size[tx_mode]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3263 | const SUBFRAME_STATS *subframe_stats = &cpi->subframe_stats; |
| 3264 | |
| 3265 | assert(max_idx < COEF_PROBS_BUFS); |
| 3266 | |
Yaowu Xu | 6b763c9 | 2017-01-23 12:13:37 -0800 | [diff] [blame] | 3267 | for (tx_size = 0; tx_size <= max_tx_size; ++tx_size) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3268 | for (i = 0; i < PLANE_TYPES; ++i) |
| 3269 | for (j = 0; j < REF_TYPES; ++j) |
| 3270 | for (k = 0; k < COEF_BANDS; ++k) |
| 3271 | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
| 3272 | if (index == max_idx) { |
| 3273 | val = |
| 3274 | cpi->common.counts.eob_branch[tx_size][i][j][k][l] - |
| 3275 | subframe_stats->eob_counts_buf[max_idx][tx_size][i][j][k][l]; |
| 3276 | } else { |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 3277 | val = subframe_stats |
| 3278 | ->eob_counts_buf[index + 1][tx_size][i][j][k][l] - |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3279 | subframe_stats->eob_counts_buf[index][tx_size][i][j][k][l]; |
| 3280 | } |
| 3281 | assert(val >= 0); |
| 3282 | eob_counts[tx_size][i][j][k][l] = val; |
| 3283 | |
| 3284 | for (m = 0; m < ENTROPY_TOKENS; ++m) { |
| 3285 | if (index == max_idx) { |
| 3286 | val = cpi->td.rd_counts.coef_counts[tx_size][i][j][k][l][m] - |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 3287 | subframe_stats |
| 3288 | ->coef_counts_buf[max_idx][tx_size][i][j][k][l][m]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3289 | } else { |
clang-format | 55ce9e0 | 2017-02-15 22:27:12 -0800 | [diff] [blame] | 3290 | val = subframe_stats |
| 3291 | ->coef_counts_buf[index + 1][tx_size][i][j][k][l][m] - |
| 3292 | subframe_stats |
| 3293 | ->coef_counts_buf[index][tx_size][i][j][k][l][m]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3294 | } |
| 3295 | assert(val >= 0); |
| 3296 | coef_counts[tx_size][i][j][k][l][m] = val; |
| 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | static void update_coef_probs_subframe( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3302 | aom_writer *const bc, AV1_COMP *cpi, TX_SIZE tx_size, |
| 3303 | av1_coeff_stats branch_ct[COEF_PROBS_BUFS][TX_SIZES][PLANE_TYPES], |
| 3304 | av1_coeff_probs_model *new_coef_probs) { |
| 3305 | av1_coeff_probs_model *old_coef_probs = cpi->common.fc->coef_probs[tx_size]; |
| 3306 | const aom_prob upd = DIFF_UPDATE_PROB; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3307 | const int entropy_nodes_update = UNCONSTRAINED_NODES; |
| 3308 | int i, j, k, l, t; |
| 3309 | int stepsize = cpi->sf.coeff_prob_appx_step; |
| 3310 | const int max_idx = cpi->common.coef_probs_update_idx; |
| 3311 | int idx; |
| 3312 | unsigned int this_branch_ct[ENTROPY_NODES][COEF_PROBS_BUFS][2]; |
| 3313 | |
| 3314 | switch (cpi->sf.use_fast_coef_updates) { |
| 3315 | case TWO_LOOP: { |
| 3316 | /* dry run to see if there is any update at all needed */ |
| 3317 | int savings = 0; |
| 3318 | int update[2] = { 0, 0 }; |
| 3319 | for (i = 0; i < PLANE_TYPES; ++i) { |
| 3320 | for (j = 0; j < REF_TYPES; ++j) { |
| 3321 | for (k = 0; k < COEF_BANDS; ++k) { |
| 3322 | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
| 3323 | for (t = 0; t < ENTROPY_NODES; ++t) { |
| 3324 | for (idx = 0; idx <= max_idx; ++idx) { |
| 3325 | memcpy(this_branch_ct[t][idx], |
| 3326 | branch_ct[idx][tx_size][i][j][k][l][t], |
| 3327 | 2 * sizeof(this_branch_ct[t][idx][0])); |
| 3328 | } |
| 3329 | } |
| 3330 | for (t = 0; t < entropy_nodes_update; ++t) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3331 | aom_prob newp = new_coef_probs[i][j][k][l][t]; |
| 3332 | const aom_prob oldp = old_coef_probs[i][j][k][l][t]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3333 | int s, u = 0; |
| 3334 | |
| 3335 | if (t == PIVOT_NODE) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3336 | s = av1_prob_update_search_model_subframe( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3337 | this_branch_ct, old_coef_probs[i][j][k][l], &newp, upd, |
| 3338 | stepsize, max_idx); |
| 3339 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3340 | s = av1_prob_update_search_subframe(this_branch_ct[t], oldp, |
| 3341 | &newp, upd, max_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3342 | if (s > 0 && newp != oldp) u = 1; |
| 3343 | if (u) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3344 | savings += s - (int)(av1_cost_zero(upd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3345 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3346 | savings -= (int)(av1_cost_zero(upd)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3347 | update[u]++; |
| 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | } |
| 3352 | } |
| 3353 | |
| 3354 | /* Is coef updated at all */ |
| 3355 | if (update[1] == 0 || savings < 0) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3356 | aom_write_bit(bc, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3357 | return; |
| 3358 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3359 | aom_write_bit(bc, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3360 | for (i = 0; i < PLANE_TYPES; ++i) { |
| 3361 | for (j = 0; j < REF_TYPES; ++j) { |
| 3362 | for (k = 0; k < COEF_BANDS; ++k) { |
| 3363 | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
| 3364 | for (t = 0; t < ENTROPY_NODES; ++t) { |
| 3365 | for (idx = 0; idx <= max_idx; ++idx) { |
| 3366 | memcpy(this_branch_ct[t][idx], |
| 3367 | branch_ct[idx][tx_size][i][j][k][l][t], |
| 3368 | 2 * sizeof(this_branch_ct[t][idx][0])); |
| 3369 | } |
| 3370 | } |
| 3371 | for (t = 0; t < entropy_nodes_update; ++t) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3372 | aom_prob newp = new_coef_probs[i][j][k][l][t]; |
| 3373 | aom_prob *oldp = old_coef_probs[i][j][k][l] + t; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3374 | int s; |
| 3375 | int u = 0; |
| 3376 | |
| 3377 | if (t == PIVOT_NODE) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3378 | s = av1_prob_update_search_model_subframe( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3379 | this_branch_ct, old_coef_probs[i][j][k][l], &newp, upd, |
| 3380 | stepsize, max_idx); |
| 3381 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3382 | s = av1_prob_update_search_subframe(this_branch_ct[t], *oldp, |
| 3383 | &newp, upd, max_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3384 | if (s > 0 && newp != *oldp) u = 1; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3385 | aom_write(bc, u, upd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3386 | if (u) { |
| 3387 | /* send/use new probability */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3388 | av1_write_prob_diff_update(bc, newp, *oldp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3389 | *oldp = newp; |
| 3390 | } |
| 3391 | } |
| 3392 | } |
| 3393 | } |
| 3394 | } |
| 3395 | } |
| 3396 | return; |
| 3397 | } |
| 3398 | |
| 3399 | case ONE_LOOP_REDUCED: { |
| 3400 | int updates = 0; |
| 3401 | int noupdates_before_first = 0; |
| 3402 | for (i = 0; i < PLANE_TYPES; ++i) { |
| 3403 | for (j = 0; j < REF_TYPES; ++j) { |
| 3404 | for (k = 0; k < COEF_BANDS; ++k) { |
| 3405 | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
| 3406 | for (t = 0; t < ENTROPY_NODES; ++t) { |
| 3407 | for (idx = 0; idx <= max_idx; ++idx) { |
| 3408 | memcpy(this_branch_ct[t][idx], |
| 3409 | branch_ct[idx][tx_size][i][j][k][l][t], |
| 3410 | 2 * sizeof(this_branch_ct[t][idx][0])); |
| 3411 | } |
| 3412 | } |
| 3413 | for (t = 0; t < entropy_nodes_update; ++t) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3414 | aom_prob newp = new_coef_probs[i][j][k][l][t]; |
| 3415 | aom_prob *oldp = old_coef_probs[i][j][k][l] + t; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3416 | int s; |
| 3417 | int u = 0; |
| 3418 | |
| 3419 | if (t == PIVOT_NODE) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3420 | s = av1_prob_update_search_model_subframe( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3421 | this_branch_ct, old_coef_probs[i][j][k][l], &newp, upd, |
| 3422 | stepsize, max_idx); |
| 3423 | else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3424 | s = av1_prob_update_search_subframe(this_branch_ct[t], *oldp, |
| 3425 | &newp, upd, max_idx); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3426 | if (s > 0 && newp != *oldp) u = 1; |
| 3427 | updates += u; |
| 3428 | if (u == 0 && updates == 0) { |
| 3429 | noupdates_before_first++; |
| 3430 | continue; |
| 3431 | } |
| 3432 | if (u == 1 && updates == 1) { |
| 3433 | int v; |
| 3434 | // first update |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3435 | aom_write_bit(bc, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3436 | for (v = 0; v < noupdates_before_first; ++v) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3437 | aom_write(bc, 0, upd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3438 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3439 | aom_write(bc, u, upd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3440 | if (u) { |
| 3441 | /* send/use new probability */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3442 | av1_write_prob_diff_update(bc, newp, *oldp); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3443 | *oldp = newp; |
| 3444 | } |
| 3445 | } |
| 3446 | } |
| 3447 | } |
| 3448 | } |
| 3449 | } |
| 3450 | if (updates == 0) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3451 | aom_write_bit(bc, 0); // no updates |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3452 | } |
| 3453 | return; |
| 3454 | } |
| 3455 | default: assert(0); |
| 3456 | } |
| 3457 | } |
hui su | 0d10357 | 2017-03-01 17:58:01 -0800 | [diff] [blame] | 3458 | #endif // CONFIG_SUBFRAME_PROB_UPDATE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3459 | |
Alex Converse | a9598cd | 2017-02-03 14:18:05 -0800 | [diff] [blame] | 3460 | #if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3461 | static void update_coef_probs(AV1_COMP *cpi, aom_writer *w) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3462 | const TX_MODE tx_mode = cpi->common.tx_mode; |
| 3463 | const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode]; |
| 3464 | TX_SIZE tx_size; |
hui su | 0d10357 | 2017-03-01 17:58:01 -0800 | [diff] [blame] | 3465 | #if CONFIG_SUBFRAME_PROB_UPDATE |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3466 | AV1_COMMON *cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3467 | SUBFRAME_STATS *subframe_stats = &cpi->subframe_stats; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3468 | int i; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3469 | av1_coeff_probs_model dummy_frame_coef_probs[PLANE_TYPES]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3470 | |
| 3471 | if (cm->do_subframe_update && |
| 3472 | cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3473 | av1_copy(cpi->common.fc->coef_probs, |
| 3474 | subframe_stats->enc_starting_coef_probs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3475 | for (i = 0; i <= cpi->common.coef_probs_update_idx; ++i) { |
| 3476 | get_coef_counts_diff(cpi, i, cpi->wholeframe_stats.coef_counts_buf[i], |
| 3477 | cpi->wholeframe_stats.eob_counts_buf[i]); |
| 3478 | } |
| 3479 | } |
hui su | 0d10357 | 2017-03-01 17:58:01 -0800 | [diff] [blame] | 3480 | #endif // CONFIG_SUBFRAME_PROB_UPDATE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3481 | |
Jingning Han | 8363063 | 2016-12-16 11:27:25 -0800 | [diff] [blame] | 3482 | for (tx_size = 0; tx_size <= max_tx_size; ++tx_size) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3483 | av1_coeff_stats frame_branch_ct[PLANE_TYPES]; |
| 3484 | av1_coeff_probs_model frame_coef_probs[PLANE_TYPES]; |
Jingning Han | c7ea761 | 2017-01-11 15:01:30 -0800 | [diff] [blame] | 3485 | if (cpi->td.counts->tx_size_totals[tx_size] <= 20 || CONFIG_RD_DEBUG || |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3486 | (tx_size >= TX_16X16 && cpi->sf.tx_size_search_method == USE_TX_8X8)) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3487 | aom_write_bit(w, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3488 | } else { |
hui su | 0d10357 | 2017-03-01 17:58:01 -0800 | [diff] [blame] | 3489 | #if CONFIG_SUBFRAME_PROB_UPDATE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3490 | if (cm->do_subframe_update && |
| 3491 | cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) { |
Urvang Joshi | 43e6281 | 2016-10-20 14:51:01 -0700 | [diff] [blame] | 3492 | unsigned int this_eob_counts_copy[PLANE_TYPES][REF_TYPES][COEF_BANDS] |
| 3493 | [COEFF_CONTEXTS]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3494 | av1_coeff_count coef_counts_copy[PLANE_TYPES]; |
Urvang Joshi | 43e6281 | 2016-10-20 14:51:01 -0700 | [diff] [blame] | 3495 | av1_copy(this_eob_counts_copy, cpi->common.counts.eob_branch[tx_size]); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3496 | av1_copy(coef_counts_copy, cpi->td.rd_counts.coef_counts[tx_size]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3497 | build_tree_distribution(cpi, tx_size, frame_branch_ct, |
| 3498 | frame_coef_probs); |
| 3499 | for (i = 0; i <= cpi->common.coef_probs_update_idx; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3500 | av1_copy(cpi->common.counts.eob_branch[tx_size], |
| 3501 | cpi->wholeframe_stats.eob_counts_buf[i][tx_size]); |
| 3502 | av1_copy(cpi->td.rd_counts.coef_counts[tx_size], |
| 3503 | cpi->wholeframe_stats.coef_counts_buf[i][tx_size]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3504 | build_tree_distribution(cpi, tx_size, cpi->branch_ct_buf[i][tx_size], |
| 3505 | dummy_frame_coef_probs); |
| 3506 | } |
Urvang Joshi | 43e6281 | 2016-10-20 14:51:01 -0700 | [diff] [blame] | 3507 | av1_copy(cpi->common.counts.eob_branch[tx_size], this_eob_counts_copy); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3508 | av1_copy(cpi->td.rd_counts.coef_counts[tx_size], coef_counts_copy); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3509 | |
| 3510 | update_coef_probs_subframe(w, cpi, tx_size, cpi->branch_ct_buf, |
| 3511 | frame_coef_probs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3512 | } else { |
hui su | 0d10357 | 2017-03-01 17:58:01 -0800 | [diff] [blame] | 3513 | #endif // CONFIG_SUBFRAME_PROB_UPDATE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3514 | build_tree_distribution(cpi, tx_size, frame_branch_ct, |
| 3515 | frame_coef_probs); |
| 3516 | update_coef_probs_common(w, cpi, tx_size, frame_branch_ct, |
| 3517 | frame_coef_probs); |
hui su | 0d10357 | 2017-03-01 17:58:01 -0800 | [diff] [blame] | 3518 | #if CONFIG_SUBFRAME_PROB_UPDATE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3519 | } |
hui su | 0d10357 | 2017-03-01 17:58:01 -0800 | [diff] [blame] | 3520 | #endif // CONFIG_SUBFRAME_PROB_UPDATE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3521 | } |
| 3522 | } |
| 3523 | |
hui su | 0d10357 | 2017-03-01 17:58:01 -0800 | [diff] [blame] | 3524 | #if CONFIG_SUBFRAME_PROB_UPDATE |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3525 | av1_copy(cm->starting_coef_probs, cm->fc->coef_probs); |
| 3526 | av1_copy(subframe_stats->coef_probs_buf[0], cm->fc->coef_probs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3527 | if (cm->do_subframe_update && |
| 3528 | cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) { |
Urvang Joshi | 43e6281 | 2016-10-20 14:51:01 -0700 | [diff] [blame] | 3529 | unsigned int eob_counts_copy[TX_SIZES][PLANE_TYPES][REF_TYPES][COEF_BANDS] |
| 3530 | [COEFF_CONTEXTS]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3531 | av1_copy(eob_counts_copy, cm->counts.eob_branch); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3532 | for (i = 1; i <= cpi->common.coef_probs_update_idx; ++i) { |
Yaowu Xu | 6b763c9 | 2017-01-23 12:13:37 -0800 | [diff] [blame] | 3533 | for (tx_size = 0; tx_size <= max_tx_size; ++tx_size) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3534 | av1_full_to_model_counts(cm->counts.coef[tx_size], |
| 3535 | subframe_stats->coef_counts_buf[i][tx_size]); |
| 3536 | av1_copy(cm->counts.eob_branch, subframe_stats->eob_counts_buf[i]); |
| 3537 | av1_partial_adapt_probs(cm, 0, 0); |
| 3538 | av1_copy(subframe_stats->coef_probs_buf[i], cm->fc->coef_probs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3539 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3540 | av1_copy(cm->fc->coef_probs, subframe_stats->coef_probs_buf[0]); |
| 3541 | av1_copy(cm->counts.eob_branch, eob_counts_copy); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3542 | } |
hui su | 0d10357 | 2017-03-01 17:58:01 -0800 | [diff] [blame] | 3543 | #endif // CONFIG_SUBFRAME_PROB_UPDATE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3544 | } |
Angie Chiang | 800df03 | 2017-03-22 11:14:12 -0700 | [diff] [blame] | 3545 | #endif // !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) |
Thomas Davies | fc1598a | 2017-01-13 17:07:25 +0000 | [diff] [blame] | 3546 | #endif // !CONFIG_EC_ADAPT |
Angie Chiang | 800df03 | 2017-03-22 11:14:12 -0700 | [diff] [blame] | 3547 | #endif // !CONFIG_LV_MAP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3548 | |
| 3549 | #if CONFIG_LOOP_RESTORATION |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 3550 | static void encode_restoration_mode(AV1_COMMON *cm, |
| 3551 | struct aom_write_bit_buffer *wb) { |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 3552 | int p; |
| 3553 | RestorationInfo *rsi = &cm->rst_info[0]; |
| 3554 | switch (rsi->frame_restoration_type) { |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 3555 | case RESTORE_NONE: |
| 3556 | aom_wb_write_bit(wb, 0); |
| 3557 | aom_wb_write_bit(wb, 0); |
| 3558 | break; |
Debargha Mukherjee | 0e67b25 | 2016-12-08 09:22:44 -0800 | [diff] [blame] | 3559 | case RESTORE_WIENER: |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 3560 | aom_wb_write_bit(wb, 1); |
Debargha Mukherjee | 0e67b25 | 2016-12-08 09:22:44 -0800 | [diff] [blame] | 3561 | aom_wb_write_bit(wb, 0); |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 3562 | break; |
Debargha Mukherjee | b3c43bc | 2017-02-01 13:09:03 -0800 | [diff] [blame] | 3563 | case RESTORE_SGRPROJ: |
| 3564 | aom_wb_write_bit(wb, 1); |
| 3565 | aom_wb_write_bit(wb, 1); |
| 3566 | break; |
Debargha Mukherjee | 0e67b25 | 2016-12-08 09:22:44 -0800 | [diff] [blame] | 3567 | case RESTORE_SWITCHABLE: |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 3568 | aom_wb_write_bit(wb, 0); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 3569 | aom_wb_write_bit(wb, 1); |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 3570 | break; |
| 3571 | default: assert(0); |
| 3572 | } |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 3573 | for (p = 1; p < MAX_MB_PLANE; ++p) { |
| 3574 | rsi = &cm->rst_info[p]; |
| 3575 | switch (rsi->frame_restoration_type) { |
| 3576 | case RESTORE_NONE: aom_wb_write_bit(wb, 0); break; |
| 3577 | case RESTORE_WIENER: aom_wb_write_bit(wb, 1); break; |
| 3578 | default: assert(0); |
| 3579 | } |
| 3580 | } |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 3581 | if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE || |
| 3582 | cm->rst_info[1].frame_restoration_type != RESTORE_NONE || |
| 3583 | cm->rst_info[2].frame_restoration_type != RESTORE_NONE) { |
| 3584 | rsi = &cm->rst_info[0]; |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 3585 | aom_wb_write_bit(wb, rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX); |
| 3586 | if (rsi->restoration_tilesize != RESTORATION_TILESIZE_MAX) { |
| 3587 | aom_wb_write_bit( |
| 3588 | wb, rsi->restoration_tilesize != (RESTORATION_TILESIZE_MAX >> 1)); |
| 3589 | } |
| 3590 | } |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 3591 | } |
| 3592 | |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame^] | 3593 | static void write_wiener_filter(WienerInfo *wiener_info, |
| 3594 | WienerInfo *ref_wiener_info, aom_writer *wb) { |
| 3595 | aom_write_primitive_refsubexpfin( |
| 3596 | wb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1, |
| 3597 | WIENER_FILT_TAP0_SUBEXP_K, |
| 3598 | ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV, |
| 3599 | wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV); |
| 3600 | aom_write_primitive_refsubexpfin( |
| 3601 | wb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1, |
| 3602 | WIENER_FILT_TAP1_SUBEXP_K, |
| 3603 | ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV, |
| 3604 | wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV); |
| 3605 | aom_write_primitive_refsubexpfin( |
| 3606 | wb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1, |
| 3607 | WIENER_FILT_TAP2_SUBEXP_K, |
| 3608 | ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV, |
| 3609 | wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV); |
| 3610 | aom_write_primitive_refsubexpfin( |
| 3611 | wb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1, |
| 3612 | WIENER_FILT_TAP0_SUBEXP_K, |
| 3613 | ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV, |
| 3614 | wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV); |
| 3615 | aom_write_primitive_refsubexpfin( |
| 3616 | wb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1, |
| 3617 | WIENER_FILT_TAP1_SUBEXP_K, |
| 3618 | ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV, |
| 3619 | wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV); |
| 3620 | aom_write_primitive_refsubexpfin( |
| 3621 | wb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1, |
| 3622 | WIENER_FILT_TAP2_SUBEXP_K, |
| 3623 | ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV, |
| 3624 | wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV); |
| 3625 | memcpy(ref_wiener_info, wiener_info, sizeof(*wiener_info)); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 3626 | } |
| 3627 | |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame^] | 3628 | static void write_sgrproj_filter(SgrprojInfo *sgrproj_info, |
| 3629 | SgrprojInfo *ref_sgrproj_info, |
| 3630 | aom_writer *wb) { |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 3631 | aom_write_literal(wb, sgrproj_info->ep, SGRPROJ_PARAMS_BITS); |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame^] | 3632 | aom_write_primitive_refsubexpfin(wb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, |
| 3633 | SGRPROJ_PRJ_SUBEXP_K, |
| 3634 | ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, |
| 3635 | sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0); |
| 3636 | aom_write_primitive_refsubexpfin(wb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, |
| 3637 | SGRPROJ_PRJ_SUBEXP_K, |
| 3638 | ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, |
| 3639 | sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1); |
| 3640 | memcpy(ref_sgrproj_info, sgrproj_info, sizeof(*sgrproj_info)); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 3641 | } |
| 3642 | |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 3643 | static void encode_restoration(AV1_COMMON *cm, aom_writer *wb) { |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 3644 | int i, p; |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 3645 | const int ntiles = av1_get_rest_ntiles(cm->width, cm->height, |
| 3646 | cm->rst_info[0].restoration_tilesize, |
| 3647 | NULL, NULL, NULL, NULL); |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame^] | 3648 | WienerInfo ref_wiener_info; |
| 3649 | SgrprojInfo ref_sgrproj_info; |
| 3650 | set_default_wiener(&ref_wiener_info); |
| 3651 | set_default_sgrproj(&ref_sgrproj_info); |
Debargha Mukherjee | 1008c1e | 2017-03-06 19:18:43 -0800 | [diff] [blame] | 3652 | const int ntiles_uv = av1_get_rest_ntiles( |
| 3653 | ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x), |
| 3654 | ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y), |
| 3655 | cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL); |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 3656 | RestorationInfo *rsi = &cm->rst_info[0]; |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 3657 | if (rsi->frame_restoration_type != RESTORE_NONE) { |
| 3658 | if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) { |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 3659 | // RESTORE_SWITCHABLE |
Debargha Mukherjee | d748914 | 2017-01-05 13:58:16 -0800 | [diff] [blame] | 3660 | for (i = 0; i < ntiles; ++i) { |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 3661 | av1_write_token( |
clang-format | bda8d61 | 2016-09-19 15:55:46 -0700 | [diff] [blame] | 3662 | wb, av1_switchable_restore_tree, cm->fc->switchable_restore_prob, |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 3663 | &switchable_restore_encodings[rsi->restoration_type[i]]); |
Debargha Mukherjee | 0e67b25 | 2016-12-08 09:22:44 -0800 | [diff] [blame] | 3664 | if (rsi->restoration_type[i] == RESTORE_WIENER) { |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame^] | 3665 | write_wiener_filter(&rsi->wiener_info[i], &ref_wiener_info, wb); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 3666 | } else if (rsi->restoration_type[i] == RESTORE_SGRPROJ) { |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame^] | 3667 | write_sgrproj_filter(&rsi->sgrproj_info[i], &ref_sgrproj_info, wb); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3668 | } |
| 3669 | } |
Debargha Mukherjee | 5d89a63 | 2016-09-17 13:16:58 -0700 | [diff] [blame] | 3670 | } else if (rsi->frame_restoration_type == RESTORE_WIENER) { |
Debargha Mukherjee | d748914 | 2017-01-05 13:58:16 -0800 | [diff] [blame] | 3671 | for (i = 0; i < ntiles; ++i) { |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 3672 | aom_write(wb, rsi->restoration_type[i] != RESTORE_NONE, |
| 3673 | RESTORE_NONE_WIENER_PROB); |
| 3674 | if (rsi->restoration_type[i] != RESTORE_NONE) { |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame^] | 3675 | write_wiener_filter(&rsi->wiener_info[i], &ref_wiener_info, wb); |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 3676 | } |
| 3677 | } |
| 3678 | } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) { |
Debargha Mukherjee | d748914 | 2017-01-05 13:58:16 -0800 | [diff] [blame] | 3679 | for (i = 0; i < ntiles; ++i) { |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 3680 | aom_write(wb, rsi->restoration_type[i] != RESTORE_NONE, |
Debargha Mukherjee | 8f209a8 | 2016-10-12 10:47:01 -0700 | [diff] [blame] | 3681 | RESTORE_NONE_SGRPROJ_PROB); |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 3682 | if (rsi->restoration_type[i] != RESTORE_NONE) { |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame^] | 3683 | write_sgrproj_filter(&rsi->sgrproj_info[i], &ref_sgrproj_info, wb); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3684 | } |
| 3685 | } |
| 3686 | } |
| 3687 | } |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 3688 | for (p = 1; p < MAX_MB_PLANE; ++p) { |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame^] | 3689 | set_default_wiener(&ref_wiener_info); |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 3690 | rsi = &cm->rst_info[p]; |
| 3691 | if (rsi->frame_restoration_type == RESTORE_WIENER) { |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 3692 | for (i = 0; i < ntiles_uv; ++i) { |
| 3693 | if (ntiles_uv > 1) |
| 3694 | aom_write(wb, rsi->restoration_type[i] != RESTORE_NONE, |
| 3695 | RESTORE_NONE_WIENER_PROB); |
| 3696 | if (rsi->restoration_type[i] != RESTORE_NONE) { |
Debargha Mukherjee | cfc12f3 | 2017-04-18 07:03:32 -0700 | [diff] [blame^] | 3697 | write_wiener_filter(&rsi->wiener_info[i], &ref_wiener_info, wb); |
Debargha Mukherjee | 994ccd7 | 2017-01-06 11:18:23 -0800 | [diff] [blame] | 3698 | } |
| 3699 | } |
Debargha Mukherjee | a43a2d9 | 2017-01-03 15:14:57 -0800 | [diff] [blame] | 3700 | } else if (rsi->frame_restoration_type != RESTORE_NONE) { |
| 3701 | assert(0); |
| 3702 | } |
| 3703 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3704 | } |
| 3705 | #endif // CONFIG_LOOP_RESTORATION |
| 3706 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3707 | static void encode_loopfilter(AV1_COMMON *cm, struct aom_write_bit_buffer *wb) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3708 | int i; |
| 3709 | struct loopfilter *lf = &cm->lf; |
| 3710 | |
| 3711 | // Encode the loop filter level and type |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3712 | aom_wb_write_literal(wb, lf->filter_level, 6); |
| 3713 | aom_wb_write_literal(wb, lf->sharpness_level, 3); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3714 | |
| 3715 | // Write out loop filter deltas applied at the MB level based on mode or |
| 3716 | // ref frame (if they are enabled). |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3717 | aom_wb_write_bit(wb, lf->mode_ref_delta_enabled); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3718 | |
| 3719 | if (lf->mode_ref_delta_enabled) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3720 | aom_wb_write_bit(wb, lf->mode_ref_delta_update); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3721 | if (lf->mode_ref_delta_update) { |
| 3722 | for (i = 0; i < TOTAL_REFS_PER_FRAME; i++) { |
| 3723 | const int delta = lf->ref_deltas[i]; |
| 3724 | const int changed = delta != lf->last_ref_deltas[i]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3725 | aom_wb_write_bit(wb, changed); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3726 | if (changed) { |
| 3727 | lf->last_ref_deltas[i] = delta; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3728 | aom_wb_write_inv_signed_literal(wb, delta, 6); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3729 | } |
| 3730 | } |
| 3731 | |
| 3732 | for (i = 0; i < MAX_MODE_LF_DELTAS; i++) { |
| 3733 | const int delta = lf->mode_deltas[i]; |
| 3734 | const int changed = delta != lf->last_mode_deltas[i]; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3735 | aom_wb_write_bit(wb, changed); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3736 | if (changed) { |
| 3737 | lf->last_mode_deltas[i] = delta; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3738 | aom_wb_write_inv_signed_literal(wb, delta, 6); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3739 | } |
| 3740 | } |
| 3741 | } |
| 3742 | } |
| 3743 | } |
| 3744 | |
Jean-Marc Valin | 0143513 | 2017-02-18 14:12:53 -0500 | [diff] [blame] | 3745 | #if CONFIG_CDEF |
Steinar Midtskogen | a9d41e8 | 2017-03-17 12:48:15 +0100 | [diff] [blame] | 3746 | static void encode_cdef(const AV1_COMMON *cm, struct aom_write_bit_buffer *wb) { |
Jean-Marc Valin | 5f5c132 | 2017-03-21 16:20:21 -0400 | [diff] [blame] | 3747 | int i; |
Steinar Midtskogen | 0c966a5 | 2017-04-18 14:38:13 +0200 | [diff] [blame] | 3748 | aom_wb_write_literal(wb, cm->cdef_dering_damping - 5, 1); |
| 3749 | aom_wb_write_literal(wb, cm->cdef_clpf_damping - 3, 2); |
Jean-Marc Valin | 5f5c132 | 2017-03-21 16:20:21 -0400 | [diff] [blame] | 3750 | aom_wb_write_literal(wb, cm->cdef_bits, 2); |
| 3751 | for (i = 0; i < cm->nb_cdef_strengths; i++) { |
| 3752 | aom_wb_write_literal(wb, cm->cdef_strengths[i], CDEF_STRENGTH_BITS); |
Jean-Marc Valin | e9f7742 | 2017-03-22 17:09:51 -0400 | [diff] [blame] | 3753 | aom_wb_write_literal(wb, cm->cdef_uv_strengths[i], CDEF_STRENGTH_BITS); |
Jean-Marc Valin | 5f5c132 | 2017-03-21 16:20:21 -0400 | [diff] [blame] | 3754 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3755 | } |
| 3756 | #endif |
| 3757 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3758 | static void write_delta_q(struct aom_write_bit_buffer *wb, int delta_q) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3759 | if (delta_q != 0) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3760 | aom_wb_write_bit(wb, 1); |
| 3761 | aom_wb_write_inv_signed_literal(wb, delta_q, 6); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3762 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3763 | aom_wb_write_bit(wb, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3764 | } |
| 3765 | } |
| 3766 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3767 | static void encode_quantization(const AV1_COMMON *const cm, |
| 3768 | struct aom_write_bit_buffer *wb) { |
| 3769 | aom_wb_write_literal(wb, cm->base_qindex, QINDEX_BITS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3770 | write_delta_q(wb, cm->y_dc_delta_q); |
| 3771 | write_delta_q(wb, cm->uv_dc_delta_q); |
| 3772 | write_delta_q(wb, cm->uv_ac_delta_q); |
| 3773 | #if CONFIG_AOM_QM |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3774 | aom_wb_write_bit(wb, cm->using_qmatrix); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3775 | if (cm->using_qmatrix) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3776 | aom_wb_write_literal(wb, cm->min_qmlevel, QM_LEVEL_BITS); |
| 3777 | aom_wb_write_literal(wb, cm->max_qmlevel, QM_LEVEL_BITS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3778 | } |
| 3779 | #endif |
| 3780 | } |
| 3781 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3782 | static void encode_segmentation(AV1_COMMON *cm, MACROBLOCKD *xd, |
| 3783 | struct aom_write_bit_buffer *wb) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3784 | int i, j; |
| 3785 | const struct segmentation *seg = &cm->seg; |
| 3786 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3787 | aom_wb_write_bit(wb, seg->enabled); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3788 | if (!seg->enabled) return; |
| 3789 | |
| 3790 | // Segmentation map |
| 3791 | if (!frame_is_intra_only(cm) && !cm->error_resilient_mode) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3792 | aom_wb_write_bit(wb, seg->update_map); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3793 | } else { |
| 3794 | assert(seg->update_map == 1); |
| 3795 | } |
| 3796 | if (seg->update_map) { |
| 3797 | // Select the coding strategy (temporal or spatial) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3798 | av1_choose_segmap_coding_method(cm, xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3799 | |
| 3800 | // Write out the chosen coding method. |
| 3801 | if (!frame_is_intra_only(cm) && !cm->error_resilient_mode) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3802 | aom_wb_write_bit(wb, seg->temporal_update); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3803 | } else { |
| 3804 | assert(seg->temporal_update == 0); |
| 3805 | } |
| 3806 | } |
| 3807 | |
| 3808 | // Segmentation data |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3809 | aom_wb_write_bit(wb, seg->update_data); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3810 | if (seg->update_data) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3811 | aom_wb_write_bit(wb, seg->abs_delta); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3812 | |
| 3813 | for (i = 0; i < MAX_SEGMENTS; i++) { |
| 3814 | for (j = 0; j < SEG_LVL_MAX; j++) { |
| 3815 | const int active = segfeature_active(seg, i, j); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3816 | aom_wb_write_bit(wb, active); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3817 | if (active) { |
| 3818 | const int data = get_segdata(seg, i, j); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3819 | const int data_max = av1_seg_feature_data_max(j); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3820 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3821 | if (av1_is_segfeature_signed(j)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3822 | encode_unsigned_max(wb, abs(data), data_max); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3823 | aom_wb_write_bit(wb, data < 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3824 | } else { |
| 3825 | encode_unsigned_max(wb, data, data_max); |
| 3826 | } |
| 3827 | } |
| 3828 | } |
| 3829 | } |
| 3830 | } |
| 3831 | } |
| 3832 | |
Nathan E. Egge | baaaa16 | 2016-10-24 09:50:52 -0400 | [diff] [blame] | 3833 | #if !CONFIG_EC_ADAPT |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3834 | static void update_seg_probs(AV1_COMP *cpi, aom_writer *w) { |
| 3835 | AV1_COMMON *cm = &cpi->common; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 3836 | #if CONFIG_TILE_GROUPS |
| 3837 | const int probwt = cm->num_tg; |
| 3838 | #else |
| 3839 | const int probwt = 1; |
| 3840 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3841 | |
| 3842 | if (!cm->seg.enabled || !cm->seg.update_map) return; |
| 3843 | |
| 3844 | if (cm->seg.temporal_update) { |
| 3845 | int i; |
| 3846 | |
| 3847 | for (i = 0; i < PREDICTION_PROBS; i++) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3848 | av1_cond_prob_diff_update(w, &cm->fc->seg.pred_probs[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 3849 | cm->counts.seg.pred[i], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3850 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3851 | prob_diff_update(av1_segment_tree, cm->fc->seg.tree_probs, |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 3852 | cm->counts.seg.tree_mispred, MAX_SEGMENTS, probwt, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3853 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3854 | prob_diff_update(av1_segment_tree, cm->fc->seg.tree_probs, |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 3855 | cm->counts.seg.tree_total, MAX_SEGMENTS, probwt, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3856 | } |
| 3857 | } |
Thomas Davies | 6519beb | 2016-10-19 14:46:07 +0100 | [diff] [blame] | 3858 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3859 | |
Yue Chen | eeacc4c | 2017-01-17 17:29:17 -0800 | [diff] [blame] | 3860 | static void write_tx_mode(AV1_COMMON *cm, MACROBLOCKD *xd, TX_MODE *mode, |
| 3861 | struct aom_write_bit_buffer *wb) { |
| 3862 | int i, all_lossless = 1; |
| 3863 | |
| 3864 | if (cm->seg.enabled) { |
| 3865 | for (i = 0; i < MAX_SEGMENTS; ++i) { |
| 3866 | if (!xd->lossless[i]) { |
| 3867 | all_lossless = 0; |
| 3868 | break; |
| 3869 | } |
| 3870 | } |
| 3871 | } else { |
| 3872 | all_lossless = xd->lossless[0]; |
| 3873 | } |
| 3874 | if (all_lossless) { |
| 3875 | *mode = ONLY_4X4; |
| 3876 | return; |
| 3877 | } |
Debargha Mukherjee | 18d38f6 | 2016-11-17 20:30:16 -0800 | [diff] [blame] | 3878 | #if CONFIG_TX64X64 |
Yue Chen | eeacc4c | 2017-01-17 17:29:17 -0800 | [diff] [blame] | 3879 | aom_wb_write_bit(wb, *mode == TX_MODE_SELECT); |
| 3880 | if (*mode != TX_MODE_SELECT) { |
| 3881 | aom_wb_write_literal(wb, AOMMIN(*mode, ALLOW_32X32), 2); |
| 3882 | if (*mode >= ALLOW_32X32) aom_wb_write_bit(wb, *mode == ALLOW_64X64); |
Debargha Mukherjee | 18d38f6 | 2016-11-17 20:30:16 -0800 | [diff] [blame] | 3883 | } |
| 3884 | #else |
Yue Chen | eeacc4c | 2017-01-17 17:29:17 -0800 | [diff] [blame] | 3885 | aom_wb_write_bit(wb, *mode == TX_MODE_SELECT); |
| 3886 | if (*mode != TX_MODE_SELECT) aom_wb_write_literal(wb, *mode, 2); |
Debargha Mukherjee | 18d38f6 | 2016-11-17 20:30:16 -0800 | [diff] [blame] | 3887 | #endif // CONFIG_TX64X64 |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3888 | } |
| 3889 | |
Nathan E. Egge | b353a8e | 2017-02-17 10:27:37 -0500 | [diff] [blame] | 3890 | #if !CONFIG_EC_ADAPT |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3891 | static void update_txfm_probs(AV1_COMMON *cm, aom_writer *w, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3892 | FRAME_COUNTS *counts) { |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 3893 | #if CONFIG_TILE_GROUPS |
| 3894 | const int probwt = cm->num_tg; |
| 3895 | #else |
| 3896 | const int probwt = 1; |
| 3897 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3898 | if (cm->tx_mode == TX_MODE_SELECT) { |
| 3899 | int i, j; |
Jingning Han | aae72a6 | 2016-10-25 15:35:29 -0700 | [diff] [blame] | 3900 | for (i = 0; i < MAX_TX_DEPTH; ++i) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3901 | for (j = 0; j < TX_SIZE_CONTEXTS; ++j) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3902 | prob_diff_update(av1_tx_size_tree[i], cm->fc->tx_size_probs[i][j], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 3903 | counts->tx_size[i][j], i + 2, probwt, w); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3904 | } |
| 3905 | } |
Nathan E. Egge | b353a8e | 2017-02-17 10:27:37 -0500 | [diff] [blame] | 3906 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3907 | |
Angie Chiang | 5678ad9 | 2016-11-21 09:38:40 -0800 | [diff] [blame] | 3908 | static void write_frame_interp_filter(InterpFilter filter, |
| 3909 | struct aom_write_bit_buffer *wb) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3910 | aom_wb_write_bit(wb, filter == SWITCHABLE); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3911 | if (filter != SWITCHABLE) |
Angie Chiang | 6305abe | 2016-10-24 12:24:44 -0700 | [diff] [blame] | 3912 | aom_wb_write_literal(wb, filter, LOG_SWITCHABLE_FILTERS); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3913 | } |
| 3914 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3915 | static void fix_interp_filter(AV1_COMMON *cm, FRAME_COUNTS *counts) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3916 | if (cm->interp_filter == SWITCHABLE) { |
| 3917 | // Check to see if only one of the filters is actually used |
| 3918 | int count[SWITCHABLE_FILTERS]; |
| 3919 | int i, j, c = 0; |
| 3920 | for (i = 0; i < SWITCHABLE_FILTERS; ++i) { |
| 3921 | count[i] = 0; |
| 3922 | for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) |
| 3923 | count[i] += counts->switchable_interp[j][i]; |
| 3924 | c += (count[i] > 0); |
| 3925 | } |
| 3926 | if (c == 1) { |
| 3927 | // Only one filter is used. So set the filter at frame level |
| 3928 | for (i = 0; i < SWITCHABLE_FILTERS; ++i) { |
| 3929 | if (count[i]) { |
Sarah Parker | a036d86 | 2017-04-11 17:53:37 -0700 | [diff] [blame] | 3930 | #if CONFIG_MOTION_VAR && (CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION) |
| 3931 | #if CONFIG_WARPED_MOTION |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 3932 | if (i == EIGHTTAP_REGULAR || WARP_WM_NEIGHBORS_WITH_OBMC) |
Sarah Parker | a036d86 | 2017-04-11 17:53:37 -0700 | [diff] [blame] | 3933 | #else |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 3934 | if (i == EIGHTTAP_REGULAR || WARP_GM_NEIGHBORS_WITH_OBMC) |
Sarah Parker | a036d86 | 2017-04-11 17:53:37 -0700 | [diff] [blame] | 3935 | #endif // CONFIG_WARPED_MOTION |
| 3936 | #endif // CONFIG_MOTION_VAR && (CONFIG_WARPED_MOTION || CONFIG_GLOBAL_MOTION) |
Debargha Mukherjee | 604d846 | 2017-04-06 15:27:00 -0700 | [diff] [blame] | 3937 | cm->interp_filter = i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3938 | break; |
| 3939 | } |
| 3940 | } |
| 3941 | } |
| 3942 | } |
| 3943 | } |
| 3944 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3945 | static void write_tile_info(const AV1_COMMON *const cm, |
| 3946 | struct aom_write_bit_buffer *wb) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3947 | #if CONFIG_EXT_TILE |
| 3948 | const int tile_width = |
| 3949 | ALIGN_POWER_OF_TWO(cm->tile_width, cm->mib_size_log2) >> |
| 3950 | cm->mib_size_log2; |
| 3951 | const int tile_height = |
| 3952 | ALIGN_POWER_OF_TWO(cm->tile_height, cm->mib_size_log2) >> |
| 3953 | cm->mib_size_log2; |
| 3954 | |
| 3955 | assert(tile_width > 0); |
| 3956 | assert(tile_height > 0); |
| 3957 | |
Yunqing Wang | d8cd55f | 2017-02-27 12:16:00 -0800 | [diff] [blame] | 3958 | aom_wb_write_literal(wb, cm->tile_encoding_mode, 1); |
| 3959 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3960 | // Write the tile sizes |
| 3961 | #if CONFIG_EXT_PARTITION |
| 3962 | if (cm->sb_size == BLOCK_128X128) { |
| 3963 | assert(tile_width <= 32); |
| 3964 | assert(tile_height <= 32); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3965 | aom_wb_write_literal(wb, tile_width - 1, 5); |
| 3966 | aom_wb_write_literal(wb, tile_height - 1, 5); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3967 | } else |
| 3968 | #endif // CONFIG_EXT_PARTITION |
| 3969 | { |
| 3970 | assert(tile_width <= 64); |
| 3971 | assert(tile_height <= 64); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3972 | aom_wb_write_literal(wb, tile_width - 1, 6); |
| 3973 | aom_wb_write_literal(wb, tile_height - 1, 6); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3974 | } |
| 3975 | #else |
| 3976 | int min_log2_tile_cols, max_log2_tile_cols, ones; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3977 | av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3978 | |
| 3979 | // columns |
| 3980 | ones = cm->log2_tile_cols - min_log2_tile_cols; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3981 | while (ones--) aom_wb_write_bit(wb, 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3982 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3983 | if (cm->log2_tile_cols < max_log2_tile_cols) aom_wb_write_bit(wb, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3984 | |
| 3985 | // rows |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3986 | aom_wb_write_bit(wb, cm->log2_tile_rows != 0); |
| 3987 | if (cm->log2_tile_rows != 0) aom_wb_write_bit(wb, cm->log2_tile_rows != 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3988 | #endif // CONFIG_EXT_TILE |
Ryan Lei | 7386eda | 2016-12-08 21:08:31 -0800 | [diff] [blame] | 3989 | |
Fangwen Fu | 7b9f2b3 | 2017-01-17 14:01:52 -0800 | [diff] [blame] | 3990 | #if CONFIG_DEPENDENT_HORZTILES |
| 3991 | if (cm->log2_tile_rows != 0) aom_wb_write_bit(wb, cm->dependent_horz_tiles); |
| 3992 | #endif |
| 3993 | |
Ryan Lei | 9b02b0e | 2017-01-30 15:52:20 -0800 | [diff] [blame] | 3994 | #if CONFIG_LOOPFILTERING_ACROSS_TILES |
Ryan Lei | 7386eda | 2016-12-08 21:08:31 -0800 | [diff] [blame] | 3995 | aom_wb_write_bit(wb, cm->loop_filter_across_tiles_enabled); |
Ryan Lei | 9b02b0e | 2017-01-30 15:52:20 -0800 | [diff] [blame] | 3996 | #endif // CONFIG_LOOPFILTERING_ACROSS_TILES |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3997 | } |
| 3998 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 3999 | static int get_refresh_mask(AV1_COMP *cpi) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4000 | int refresh_mask = 0; |
| 4001 | |
| 4002 | #if CONFIG_EXT_REFS |
| 4003 | // NOTE(zoeliu): When LAST_FRAME is to get refreshed, the decoder will be |
| 4004 | // notified to get LAST3_FRAME refreshed and then the virtual indexes for all |
| 4005 | // the 3 LAST reference frames will be updated accordingly, i.e.: |
| 4006 | // (1) The original virtual index for LAST3_FRAME will become the new virtual |
| 4007 | // index for LAST_FRAME; and |
| 4008 | // (2) The original virtual indexes for LAST_FRAME and LAST2_FRAME will be |
| 4009 | // shifted and become the new virtual indexes for LAST2_FRAME and |
| 4010 | // LAST3_FRAME. |
| 4011 | refresh_mask |= |
| 4012 | (cpi->refresh_last_frame << cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]); |
| 4013 | if (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs) { |
| 4014 | // We have swapped the virtual indices |
| 4015 | refresh_mask |= (cpi->refresh_bwd_ref_frame << cpi->arf_map[0]); |
| 4016 | } else { |
| 4017 | refresh_mask |= (cpi->refresh_bwd_ref_frame << cpi->bwd_fb_idx); |
| 4018 | } |
| 4019 | #else |
| 4020 | refresh_mask |= (cpi->refresh_last_frame << cpi->lst_fb_idx); |
| 4021 | #endif // CONFIG_EXT_REFS |
| 4022 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4023 | if (av1_preserve_existing_gf(cpi)) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4024 | // We have decided to preserve the previously existing golden frame as our |
| 4025 | // new ARF frame. However, in the short term we leave it in the GF slot and, |
| 4026 | // if we're updating the GF with the current decoded frame, we save it |
| 4027 | // instead to the ARF slot. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4028 | // Later, in the function av1_encoder.c:av1_update_reference_frames() we |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4029 | // will swap gld_fb_idx and alt_fb_idx to achieve our objective. We do it |
| 4030 | // there so that it can be done outside of the recode loop. |
| 4031 | // Note: This is highly specific to the use of ARF as a forward reference, |
| 4032 | // and this needs to be generalized as other uses are implemented |
| 4033 | // (like RTC/temporal scalability). |
| 4034 | return refresh_mask | (cpi->refresh_golden_frame << cpi->alt_fb_idx); |
| 4035 | } else { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4036 | #if CONFIG_EXT_REFS |
| 4037 | const GF_GROUP *const gf_group = &cpi->twopass.gf_group; |
Zoe Liu | 6eeea0e | 2017-04-19 08:49:49 -0700 | [diff] [blame] | 4038 | int arf_idx = cpi->arf_map[gf_group->arf_update_idx[gf_group->index]]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4039 | #else |
Zoe Liu | 6eeea0e | 2017-04-19 08:49:49 -0700 | [diff] [blame] | 4040 | int arf_idx = cpi->alt_fb_idx; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4041 | if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) { |
| 4042 | const GF_GROUP *const gf_group = &cpi->twopass.gf_group; |
| 4043 | arf_idx = gf_group->arf_update_idx[gf_group->index]; |
| 4044 | } |
| 4045 | #endif // CONFIG_EXT_REFS |
| 4046 | return refresh_mask | (cpi->refresh_golden_frame << cpi->gld_fb_idx) | |
| 4047 | (cpi->refresh_alt_ref_frame << arf_idx); |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | #if CONFIG_EXT_TILE |
| 4052 | static INLINE int find_identical_tile( |
| 4053 | const int tile_row, const int tile_col, |
| 4054 | TileBufferEnc (*const tile_buffers)[1024]) { |
| 4055 | const MV32 candidate_offset[1] = { { 1, 0 } }; |
| 4056 | const uint8_t *const cur_tile_data = |
| 4057 | tile_buffers[tile_row][tile_col].data + 4; |
Jingning Han | 99ffce6 | 2017-04-25 15:48:41 -0700 | [diff] [blame] | 4058 | const size_t cur_tile_size = tile_buffers[tile_row][tile_col].size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4059 | |
| 4060 | int i; |
| 4061 | |
| 4062 | if (tile_row == 0) return 0; |
| 4063 | |
| 4064 | // (TODO: yunqingwang) For now, only above tile is checked and used. |
| 4065 | // More candidates such as left tile can be added later. |
| 4066 | for (i = 0; i < 1; i++) { |
| 4067 | int row_offset = candidate_offset[0].row; |
| 4068 | int col_offset = candidate_offset[0].col; |
| 4069 | int row = tile_row - row_offset; |
| 4070 | int col = tile_col - col_offset; |
| 4071 | uint8_t tile_hdr; |
| 4072 | const uint8_t *tile_data; |
| 4073 | TileBufferEnc *candidate; |
| 4074 | |
| 4075 | if (row < 0 || col < 0) continue; |
| 4076 | |
| 4077 | tile_hdr = *(tile_buffers[row][col].data); |
| 4078 | |
| 4079 | // Read out tcm bit |
| 4080 | if ((tile_hdr >> 7) == 1) { |
| 4081 | // The candidate is a copy tile itself |
| 4082 | row_offset += tile_hdr & 0x7f; |
| 4083 | row = tile_row - row_offset; |
| 4084 | } |
| 4085 | |
| 4086 | candidate = &tile_buffers[row][col]; |
| 4087 | |
| 4088 | if (row_offset >= 128 || candidate->size != cur_tile_size) continue; |
| 4089 | |
| 4090 | tile_data = candidate->data + 4; |
| 4091 | |
| 4092 | if (memcmp(tile_data, cur_tile_data, cur_tile_size) != 0) continue; |
| 4093 | |
| 4094 | // Identical tile found |
| 4095 | assert(row_offset > 0); |
| 4096 | return row_offset; |
| 4097 | } |
| 4098 | |
| 4099 | // No identical tile found |
| 4100 | return 0; |
| 4101 | } |
| 4102 | #endif // CONFIG_EXT_TILE |
| 4103 | |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4104 | #if CONFIG_TILE_GROUPS |
| 4105 | static uint32_t write_tiles(AV1_COMP *const cpi, |
| 4106 | struct aom_write_bit_buffer *wb, |
| 4107 | unsigned int *max_tile_size, |
| 4108 | unsigned int *max_tile_col_size) { |
| 4109 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4110 | static uint32_t write_tiles(AV1_COMP *const cpi, uint8_t *const dst, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4111 | unsigned int *max_tile_size, |
| 4112 | unsigned int *max_tile_col_size) { |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4113 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4114 | const AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4115 | #if CONFIG_ANS |
Alex Converse | 2a1b3af | 2016-10-26 13:11:26 -0700 | [diff] [blame] | 4116 | struct BufAnsCoder *buf_ans = &cpi->buf_ans; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4117 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4118 | aom_writer mode_bc; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4119 | #endif // CONFIG_ANS |
| 4120 | int tile_row, tile_col; |
| 4121 | TOKENEXTRA *(*const tok_buffers)[MAX_TILE_COLS] = cpi->tile_tok; |
clang-format | 67948d3 | 2016-09-07 22:40:40 -0700 | [diff] [blame] | 4122 | TileBufferEnc(*const tile_buffers)[MAX_TILE_COLS] = cpi->tile_buffers; |
James Zern | 71a37de | 2017-04-20 16:03:13 -0700 | [diff] [blame] | 4123 | uint32_t total_size = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4124 | const int tile_cols = cm->tile_cols; |
| 4125 | const int tile_rows = cm->tile_rows; |
Thomas Davies | af6df17 | 2016-11-09 14:04:18 +0000 | [diff] [blame] | 4126 | unsigned int tile_size = 0; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4127 | #if CONFIG_TILE_GROUPS |
| 4128 | const int n_log2_tiles = cm->log2_tile_rows + cm->log2_tile_cols; |
| 4129 | const int have_tiles = n_log2_tiles > 0; |
James Zern | 71a37de | 2017-04-20 16:03:13 -0700 | [diff] [blame] | 4130 | uint32_t comp_hdr_size; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4131 | // Fixed size tile groups for the moment |
| 4132 | const int num_tg_hdrs = cm->num_tg; |
| 4133 | const int tg_size = (tile_rows * tile_cols + num_tg_hdrs - 1) / num_tg_hdrs; |
| 4134 | int tile_count = 0; |
Thomas Davies | dbfc4f9 | 2017-01-18 16:46:09 +0000 | [diff] [blame] | 4135 | int tg_count = 1; |
| 4136 | int tile_size_bytes = 4; |
| 4137 | int tile_col_size_bytes; |
James Zern | 71a37de | 2017-04-20 16:03:13 -0700 | [diff] [blame] | 4138 | uint32_t uncompressed_hdr_size = 0; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4139 | uint8_t *dst = NULL; |
| 4140 | struct aom_write_bit_buffer comp_hdr_len_wb; |
| 4141 | struct aom_write_bit_buffer tg_params_wb; |
Thomas Davies | dbfc4f9 | 2017-01-18 16:46:09 +0000 | [diff] [blame] | 4142 | struct aom_write_bit_buffer tile_size_bytes_wb; |
James Zern | 71a37de | 2017-04-20 16:03:13 -0700 | [diff] [blame] | 4143 | uint32_t saved_offset; |
Thomas Davies | af6df17 | 2016-11-09 14:04:18 +0000 | [diff] [blame] | 4144 | int mtu_size = cpi->oxcf.mtu; |
| 4145 | int curr_tg_data_size = 0; |
| 4146 | int hdr_size; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4147 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4148 | #if CONFIG_EXT_TILE |
| 4149 | const int have_tiles = tile_cols * tile_rows > 1; |
| 4150 | #endif // CONFIG_EXT_TILE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4151 | |
| 4152 | *max_tile_size = 0; |
| 4153 | *max_tile_col_size = 0; |
| 4154 | |
| 4155 | // All tile size fields are output on 4 bytes. A call to remux_tiles will |
| 4156 | // later compact the data if smaller headers are adequate. |
| 4157 | |
| 4158 | #if CONFIG_EXT_TILE |
| 4159 | for (tile_col = 0; tile_col < tile_cols; tile_col++) { |
| 4160 | TileInfo tile_info; |
| 4161 | const int is_last_col = (tile_col == tile_cols - 1); |
Jingning Han | 99ffce6 | 2017-04-25 15:48:41 -0700 | [diff] [blame] | 4162 | const uint32_t col_offset = total_size; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4163 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4164 | av1_tile_set_col(&tile_info, cm, tile_col); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4165 | |
| 4166 | // The last column does not have a column header |
| 4167 | if (!is_last_col) total_size += 4; |
| 4168 | |
| 4169 | for (tile_row = 0; tile_row < tile_rows; tile_row++) { |
| 4170 | TileBufferEnc *const buf = &tile_buffers[tile_row][tile_col]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4171 | const TOKENEXTRA *tok = tok_buffers[tile_row][tile_col]; |
| 4172 | const TOKENEXTRA *tok_end = tok + cpi->tok_count[tile_row][tile_col]; |
| 4173 | const int data_offset = have_tiles ? 4 : 0; |
Thomas Davies | b2f32d8 | 2017-04-05 14:46:20 +0100 | [diff] [blame] | 4174 | #if CONFIG_EC_ADAPT |
| 4175 | const int tile_idx = tile_row * tile_cols + tile_col; |
| 4176 | TileDataEnc *this_tile = &cpi->tile_data[tile_idx]; |
| 4177 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4178 | av1_tile_set_row(&tile_info, cm, tile_row); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4179 | |
| 4180 | buf->data = dst + total_size; |
| 4181 | |
| 4182 | // Is CONFIG_EXT_TILE = 1, every tile in the row has a header, |
| 4183 | // even for the last one, unless no tiling is used at all. |
| 4184 | total_size += data_offset; |
Thomas Davies | b2f32d8 | 2017-04-05 14:46:20 +0100 | [diff] [blame] | 4185 | #if CONFIG_EC_ADAPT |
| 4186 | // Initialise tile context from the frame context |
| 4187 | this_tile->tctx = *cm->fc; |
| 4188 | cpi->td.mb.e_mbd.tile_ctx = &this_tile->tctx; |
| 4189 | #endif |
Yushin Cho | 749c057 | 2017-04-07 10:36:47 -0700 | [diff] [blame] | 4190 | #if CONFIG_PVQ |
| 4191 | cpi->td.mb.pvq_q = &this_tile->pvq_q; |
| 4192 | cpi->td.mb.daala_enc.state.adapt = &this_tile->tctx.pvq_context; |
| 4193 | #endif // CONFIG_PVQ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4194 | #if !CONFIG_ANS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4195 | aom_start_encode(&mode_bc, buf->data + data_offset); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4196 | write_modes(cpi, &tile_info, &mode_bc, &tok, tok_end); |
| 4197 | assert(tok == tok_end); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4198 | aom_stop_encode(&mode_bc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4199 | tile_size = mode_bc.pos; |
| 4200 | #else |
Alex Converse | 2a1b3af | 2016-10-26 13:11:26 -0700 | [diff] [blame] | 4201 | buf_ans_write_init(buf_ans, buf->data + data_offset); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4202 | write_modes(cpi, &tile_info, buf_ans, &tok, tok_end); |
| 4203 | assert(tok == tok_end); |
Alex Converse | 1ecdf2b | 2016-11-30 15:51:12 -0800 | [diff] [blame] | 4204 | aom_buf_ans_flush(buf_ans); |
Alex Converse | 2a1b3af | 2016-10-26 13:11:26 -0700 | [diff] [blame] | 4205 | tile_size = buf_ans_write_end(buf_ans); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4206 | #endif // !CONFIG_ANS |
Yushin Cho | 749c057 | 2017-04-07 10:36:47 -0700 | [diff] [blame] | 4207 | #if CONFIG_PVQ |
| 4208 | cpi->td.mb.pvq_q = NULL; |
| 4209 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4210 | buf->size = tile_size; |
| 4211 | |
| 4212 | // Record the maximum tile size we see, so we can compact headers later. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4213 | *max_tile_size = AOMMAX(*max_tile_size, tile_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4214 | |
| 4215 | if (have_tiles) { |
| 4216 | // tile header: size of this tile, or copy offset |
| 4217 | uint32_t tile_header = tile_size; |
| 4218 | |
Yunqing Wang | d8cd55f | 2017-02-27 12:16:00 -0800 | [diff] [blame] | 4219 | // If the tile_encoding_mode is 1 (i.e. TILE_VR), check if this tile is |
| 4220 | // a copy tile. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4221 | // Very low chances to have copy tiles on the key frames, so don't |
| 4222 | // search on key frames to reduce unnecessary search. |
Yunqing Wang | d8cd55f | 2017-02-27 12:16:00 -0800 | [diff] [blame] | 4223 | if (cm->frame_type != KEY_FRAME && cm->tile_encoding_mode) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4224 | const int idendical_tile_offset = |
| 4225 | find_identical_tile(tile_row, tile_col, tile_buffers); |
| 4226 | |
| 4227 | if (idendical_tile_offset > 0) { |
| 4228 | tile_size = 0; |
| 4229 | tile_header = idendical_tile_offset | 0x80; |
| 4230 | tile_header <<= 24; |
| 4231 | } |
| 4232 | } |
| 4233 | |
| 4234 | mem_put_le32(buf->data, tile_header); |
| 4235 | } |
| 4236 | |
| 4237 | total_size += tile_size; |
| 4238 | } |
| 4239 | |
| 4240 | if (!is_last_col) { |
Jingning Han | 99ffce6 | 2017-04-25 15:48:41 -0700 | [diff] [blame] | 4241 | uint32_t col_size = total_size - col_offset - 4; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4242 | mem_put_le32(dst + col_offset, col_size); |
| 4243 | |
| 4244 | // If it is not final packing, record the maximum tile column size we see, |
| 4245 | // otherwise, check if the tile size is out of the range. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4246 | *max_tile_col_size = AOMMAX(*max_tile_col_size, col_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4247 | } |
| 4248 | } |
| 4249 | #else |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4250 | #if CONFIG_TILE_GROUPS |
| 4251 | write_uncompressed_header(cpi, wb); |
| 4252 | |
Jingning Han | d3f441c | 2017-03-06 09:12:54 -0800 | [diff] [blame] | 4253 | #if CONFIG_EXT_REFS |
| 4254 | if (cm->show_existing_frame) { |
| 4255 | total_size = aom_wb_bytes_written(wb); |
| 4256 | return (uint32_t)total_size; |
| 4257 | } |
| 4258 | #endif // CONFIG_EXT_REFS |
| 4259 | |
Thomas Davies | dbfc4f9 | 2017-01-18 16:46:09 +0000 | [diff] [blame] | 4260 | // Write the tile length code |
| 4261 | tile_size_bytes_wb = *wb; |
Thomas Davies | 4974e52 | 2016-11-07 17:44:05 +0000 | [diff] [blame] | 4262 | aom_wb_write_literal(wb, 3, 2); |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4263 | |
| 4264 | /* Write a placeholder for the number of tiles in each tile group */ |
| 4265 | tg_params_wb = *wb; |
| 4266 | saved_offset = wb->bit_offset; |
Thomas Davies | dbfc4f9 | 2017-01-18 16:46:09 +0000 | [diff] [blame] | 4267 | if (have_tiles) { |
| 4268 | aom_wb_overwrite_literal(wb, 3, n_log2_tiles); |
| 4269 | aom_wb_overwrite_literal(wb, (1 << n_log2_tiles) - 1, n_log2_tiles); |
| 4270 | } |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4271 | |
| 4272 | /* Write a placeholder for the compressed header length */ |
| 4273 | comp_hdr_len_wb = *wb; |
| 4274 | aom_wb_write_literal(wb, 0, 16); |
| 4275 | |
| 4276 | uncompressed_hdr_size = aom_wb_bytes_written(wb); |
| 4277 | dst = wb->bit_buffer; |
| 4278 | comp_hdr_size = write_compressed_header(cpi, dst + uncompressed_hdr_size); |
Thomas Davies | faa7fcf | 2016-11-14 11:59:43 +0000 | [diff] [blame] | 4279 | aom_wb_overwrite_literal(&comp_hdr_len_wb, (int)(comp_hdr_size), 16); |
Thomas Davies | af6df17 | 2016-11-09 14:04:18 +0000 | [diff] [blame] | 4280 | hdr_size = uncompressed_hdr_size + comp_hdr_size; |
| 4281 | total_size += hdr_size; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4282 | #endif |
| 4283 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4284 | for (tile_row = 0; tile_row < tile_rows; tile_row++) { |
| 4285 | TileInfo tile_info; |
| 4286 | const int is_last_row = (tile_row == tile_rows - 1); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4287 | av1_tile_set_row(&tile_info, cm, tile_row); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4288 | |
| 4289 | for (tile_col = 0; tile_col < tile_cols; tile_col++) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4290 | const int tile_idx = tile_row * tile_cols + tile_col; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4291 | TileBufferEnc *const buf = &tile_buffers[tile_row][tile_col]; |
Thomas Davies | f77d4ad | 2017-01-10 18:55:42 +0000 | [diff] [blame] | 4292 | #if CONFIG_PVQ || CONFIG_EC_ADAPT |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4293 | TileDataEnc *this_tile = &cpi->tile_data[tile_idx]; |
| 4294 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4295 | const TOKENEXTRA *tok = tok_buffers[tile_row][tile_col]; |
| 4296 | const TOKENEXTRA *tok_end = tok + cpi->tok_count[tile_row][tile_col]; |
Yaowu Xu | 8acaa55 | 2016-11-21 09:50:22 -0800 | [diff] [blame] | 4297 | const int is_last_col = (tile_col == tile_cols - 1); |
Thomas Davies | 8fe64a3 | 2016-10-04 13:19:31 +0100 | [diff] [blame] | 4298 | const int is_last_tile = is_last_col && is_last_row; |
Thomas Davies | a0de6d5 | 2017-01-20 14:45:25 +0000 | [diff] [blame] | 4299 | #if !CONFIG_TILE_GROUPS |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4300 | (void)tile_idx; |
Thomas Davies | 8fe64a3 | 2016-10-04 13:19:31 +0100 | [diff] [blame] | 4301 | #else |
Thomas Davies | af6df17 | 2016-11-09 14:04:18 +0000 | [diff] [blame] | 4302 | |
| 4303 | if ((!mtu_size && tile_count > tg_size) || |
| 4304 | (mtu_size && tile_count && curr_tg_data_size >= mtu_size)) { |
Thomas Davies | dbfc4f9 | 2017-01-18 16:46:09 +0000 | [diff] [blame] | 4305 | // New tile group |
| 4306 | tg_count++; |
Thomas Davies | af6df17 | 2016-11-09 14:04:18 +0000 | [diff] [blame] | 4307 | // We've exceeded the packet size |
| 4308 | if (tile_count > 1) { |
| 4309 | /* The last tile exceeded the packet size. The tile group size |
| 4310 | should therefore be tile_count-1. |
| 4311 | Move the last tile and insert headers before it |
| 4312 | */ |
James Zern | 71a37de | 2017-04-20 16:03:13 -0700 | [diff] [blame] | 4313 | uint32_t old_total_size = total_size - tile_size - 4; |
Thomas Davies | af6df17 | 2016-11-09 14:04:18 +0000 | [diff] [blame] | 4314 | memmove(dst + old_total_size + hdr_size, dst + old_total_size, |
| 4315 | (tile_size + 4) * sizeof(uint8_t)); |
| 4316 | // Copy uncompressed header |
| 4317 | memmove(dst + old_total_size, dst, |
| 4318 | uncompressed_hdr_size * sizeof(uint8_t)); |
| 4319 | // Write the number of tiles in the group into the last uncompressed |
| 4320 | // header before the one we've just inserted |
Thomas Davies | faa7fcf | 2016-11-14 11:59:43 +0000 | [diff] [blame] | 4321 | aom_wb_overwrite_literal(&tg_params_wb, tile_idx - tile_count, |
| 4322 | n_log2_tiles); |
| 4323 | aom_wb_overwrite_literal(&tg_params_wb, tile_count - 2, n_log2_tiles); |
Thomas Davies | af6df17 | 2016-11-09 14:04:18 +0000 | [diff] [blame] | 4324 | // Update the pointer to the last TG params |
| 4325 | tg_params_wb.bit_offset = saved_offset + 8 * old_total_size; |
| 4326 | // Copy compressed header |
| 4327 | memmove(dst + old_total_size + uncompressed_hdr_size, |
| 4328 | dst + uncompressed_hdr_size, comp_hdr_size * sizeof(uint8_t)); |
| 4329 | total_size += hdr_size; |
| 4330 | tile_count = 1; |
| 4331 | curr_tg_data_size = hdr_size + tile_size + 4; |
| 4332 | |
| 4333 | } else { |
| 4334 | // We exceeded the packet size in just one tile |
| 4335 | // Copy uncompressed header |
| 4336 | memmove(dst + total_size, dst, |
| 4337 | uncompressed_hdr_size * sizeof(uint8_t)); |
| 4338 | // Write the number of tiles in the group into the last uncompressed |
| 4339 | // header |
Thomas Davies | faa7fcf | 2016-11-14 11:59:43 +0000 | [diff] [blame] | 4340 | aom_wb_overwrite_literal(&tg_params_wb, tile_idx - tile_count, |
| 4341 | n_log2_tiles); |
| 4342 | aom_wb_overwrite_literal(&tg_params_wb, tile_count - 1, n_log2_tiles); |
Thomas Davies | af6df17 | 2016-11-09 14:04:18 +0000 | [diff] [blame] | 4343 | tg_params_wb.bit_offset = saved_offset + 8 * total_size; |
| 4344 | // Copy compressed header |
| 4345 | memmove(dst + total_size + uncompressed_hdr_size, |
| 4346 | dst + uncompressed_hdr_size, comp_hdr_size * sizeof(uint8_t)); |
| 4347 | total_size += hdr_size; |
| 4348 | tile_count = 0; |
| 4349 | curr_tg_data_size = hdr_size; |
| 4350 | } |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4351 | } |
| 4352 | tile_count++; |
| 4353 | #endif |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4354 | av1_tile_set_col(&tile_info, cm, tile_col); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4355 | |
Fangwen Fu | 73126c0 | 2017-02-08 22:37:47 -0800 | [diff] [blame] | 4356 | #if CONFIG_DEPENDENT_HORZTILES && CONFIG_TILE_GROUPS |
| 4357 | av1_tile_set_tg_boundary(&tile_info, cm, tile_row, tile_col); |
| 4358 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4359 | buf->data = dst + total_size; |
| 4360 | |
| 4361 | // The last tile does not have a header. |
| 4362 | if (!is_last_tile) total_size += 4; |
| 4363 | |
Thomas Davies | 493623e | 2017-03-31 16:12:25 +0100 | [diff] [blame] | 4364 | #if CONFIG_EC_ADAPT |
| 4365 | // Initialise tile context from the frame context |
| 4366 | this_tile->tctx = *cm->fc; |
| 4367 | cpi->td.mb.e_mbd.tile_ctx = &this_tile->tctx; |
| 4368 | #endif |
Yushin Cho | c49ef3a | 2017-03-13 17:27:25 -0700 | [diff] [blame] | 4369 | #if CONFIG_PVQ |
| 4370 | cpi->td.mb.pvq_q = &this_tile->pvq_q; |
| 4371 | cpi->td.mb.daala_enc.state.adapt = &this_tile->tctx.pvq_context; |
| 4372 | #endif // CONFIG_PVQ |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4373 | #if CONFIG_ANS |
Alex Converse | 2a1b3af | 2016-10-26 13:11:26 -0700 | [diff] [blame] | 4374 | buf_ans_write_init(buf_ans, dst + total_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4375 | write_modes(cpi, &tile_info, buf_ans, &tok, tok_end); |
| 4376 | assert(tok == tok_end); |
Alex Converse | 1ecdf2b | 2016-11-30 15:51:12 -0800 | [diff] [blame] | 4377 | aom_buf_ans_flush(buf_ans); |
Alex Converse | 2a1b3af | 2016-10-26 13:11:26 -0700 | [diff] [blame] | 4378 | tile_size = buf_ans_write_end(buf_ans); |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4379 | #else |
| 4380 | aom_start_encode(&mode_bc, dst + total_size); |
| 4381 | write_modes(cpi, &tile_info, &mode_bc, &tok, tok_end); |
Jingning Han | 223b90e | 2017-04-04 09:48:37 -0700 | [diff] [blame] | 4382 | #if !CONFIG_LV_MAP |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4383 | assert(tok == tok_end); |
Jingning Han | 223b90e | 2017-04-04 09:48:37 -0700 | [diff] [blame] | 4384 | #endif // !CONFIG_LV_MAP |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4385 | aom_stop_encode(&mode_bc); |
| 4386 | tile_size = mode_bc.pos; |
Alex Converse | 2a1b3af | 2016-10-26 13:11:26 -0700 | [diff] [blame] | 4387 | #endif // CONFIG_ANS |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4388 | #if CONFIG_PVQ |
| 4389 | cpi->td.mb.pvq_q = NULL; |
Alex Converse | 2a1b3af | 2016-10-26 13:11:26 -0700 | [diff] [blame] | 4390 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4391 | |
| 4392 | assert(tile_size > 0); |
| 4393 | |
Thomas Davies | af6df17 | 2016-11-09 14:04:18 +0000 | [diff] [blame] | 4394 | #if CONFIG_TILE_GROUPS |
| 4395 | curr_tg_data_size += tile_size + 4; |
| 4396 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4397 | buf->size = tile_size; |
| 4398 | |
| 4399 | if (!is_last_tile) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4400 | *max_tile_size = AOMMAX(*max_tile_size, tile_size); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4401 | // size of this tile |
| 4402 | mem_put_le32(buf->data, tile_size); |
| 4403 | } |
| 4404 | |
| 4405 | total_size += tile_size; |
| 4406 | } |
| 4407 | } |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4408 | #if CONFIG_TILE_GROUPS |
| 4409 | // Write the final tile group size |
| 4410 | if (n_log2_tiles) { |
Thomas Davies | dbfc4f9 | 2017-01-18 16:46:09 +0000 | [diff] [blame] | 4411 | aom_wb_overwrite_literal(&tg_params_wb, (1 << n_log2_tiles) - tile_count, |
| 4412 | n_log2_tiles); |
| 4413 | aom_wb_overwrite_literal(&tg_params_wb, tile_count - 1, n_log2_tiles); |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4414 | } |
Thomas Davies | dbfc4f9 | 2017-01-18 16:46:09 +0000 | [diff] [blame] | 4415 | // Remux if possible. TODO (Thomas Davies): do this for more than one tile |
| 4416 | // group |
| 4417 | if (have_tiles && tg_count == 1) { |
| 4418 | int data_size = total_size - (uncompressed_hdr_size + comp_hdr_size); |
| 4419 | data_size = remux_tiles(cm, dst + uncompressed_hdr_size + comp_hdr_size, |
| 4420 | data_size, *max_tile_size, *max_tile_col_size, |
| 4421 | &tile_size_bytes, &tile_col_size_bytes); |
| 4422 | total_size = data_size + uncompressed_hdr_size + comp_hdr_size; |
| 4423 | aom_wb_overwrite_literal(&tile_size_bytes_wb, tile_size_bytes - 1, 2); |
| 4424 | } |
| 4425 | |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4426 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4427 | #endif // CONFIG_EXT_TILE |
| 4428 | return (uint32_t)total_size; |
| 4429 | } |
| 4430 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4431 | static void write_render_size(const AV1_COMMON *cm, |
| 4432 | struct aom_write_bit_buffer *wb) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4433 | const int scaling_active = |
| 4434 | cm->width != cm->render_width || cm->height != cm->render_height; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4435 | aom_wb_write_bit(wb, scaling_active); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4436 | if (scaling_active) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4437 | aom_wb_write_literal(wb, cm->render_width - 1, 16); |
| 4438 | aom_wb_write_literal(wb, cm->render_height - 1, 16); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4439 | } |
| 4440 | } |
| 4441 | |
Fergus Simpson | d91c8c9 | 2017-04-07 12:12:00 -0700 | [diff] [blame] | 4442 | #if CONFIG_FRAME_SUPERRES |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 4443 | static void write_superres_scale(const AV1_COMMON *const cm, |
| 4444 | struct aom_write_bit_buffer *wb) { |
| 4445 | // This scaling and frame superres are probably incompatible |
| 4446 | assert(cm->width == cm->render_width && cm->height == cm->render_height); |
| 4447 | |
| 4448 | // First bit is whether to to scale or not |
| 4449 | if (cm->superres_scale_numerator == SUPERRES_SCALE_DENOMINATOR) { |
| 4450 | aom_wb_write_bit(wb, 0); // no scaling |
| 4451 | } else { |
| 4452 | aom_wb_write_bit(wb, 1); // scaling, write scale factor |
| 4453 | // TODO(afergs): write factor to the compressed header instead |
| 4454 | aom_wb_write_literal( |
| 4455 | wb, cm->superres_scale_numerator - SUPERRES_SCALE_NUMERATOR_MIN, |
| 4456 | SUPERRES_SCALE_BITS); |
| 4457 | } |
| 4458 | } |
Fergus Simpson | d91c8c9 | 2017-04-07 12:12:00 -0700 | [diff] [blame] | 4459 | #endif // CONFIG_FRAME_SUPERRES |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 4460 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4461 | static void write_frame_size(const AV1_COMMON *cm, |
| 4462 | struct aom_write_bit_buffer *wb) { |
Fergus Simpson | d91c8c9 | 2017-04-07 12:12:00 -0700 | [diff] [blame] | 4463 | #if CONFIG_FRAME_SUPERRES |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 4464 | // If SUPERRES scaling is happening, write the full resolution instead of the |
| 4465 | // downscaled resolution. The decoder will reduce this resolution itself. |
| 4466 | if (cm->superres_scale_numerator != SUPERRES_SCALE_DENOMINATOR) { |
| 4467 | aom_wb_write_literal(wb, cm->superres_width - 1, 16); |
| 4468 | aom_wb_write_literal(wb, cm->superres_height - 1, 16); |
| 4469 | } else { |
Fergus Simpson | d91c8c9 | 2017-04-07 12:12:00 -0700 | [diff] [blame] | 4470 | #endif // CONFIG_FRAME_SUPERRES |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 4471 | aom_wb_write_literal(wb, cm->width - 1, 16); |
| 4472 | aom_wb_write_literal(wb, cm->height - 1, 16); |
Fergus Simpson | d91c8c9 | 2017-04-07 12:12:00 -0700 | [diff] [blame] | 4473 | #if CONFIG_FRAME_SUPERRES |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 4474 | } |
Fergus Simpson | d91c8c9 | 2017-04-07 12:12:00 -0700 | [diff] [blame] | 4475 | #endif // CONFIG_FRAME_SUPERRES |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4476 | |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 4477 | // TODO(afergs): Also write something different to render_size? |
| 4478 | // When superres scales, they'll be almost guaranteed to be |
| 4479 | // different on the other side. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4480 | write_render_size(cm, wb); |
Fergus Simpson | d91c8c9 | 2017-04-07 12:12:00 -0700 | [diff] [blame] | 4481 | #if CONFIG_FRAME_SUPERRES |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 4482 | write_superres_scale(cm, wb); |
Fergus Simpson | d91c8c9 | 2017-04-07 12:12:00 -0700 | [diff] [blame] | 4483 | #endif // CONFIG_FRAME_SUPERRES |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4484 | } |
| 4485 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4486 | static void write_frame_size_with_refs(AV1_COMP *cpi, |
| 4487 | struct aom_write_bit_buffer *wb) { |
| 4488 | AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4489 | int found = 0; |
| 4490 | |
| 4491 | MV_REFERENCE_FRAME ref_frame; |
| 4492 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 4493 | YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, ref_frame); |
| 4494 | |
| 4495 | if (cfg != NULL) { |
| 4496 | found = |
| 4497 | cm->width == cfg->y_crop_width && cm->height == cfg->y_crop_height; |
| 4498 | found &= cm->render_width == cfg->render_width && |
| 4499 | cm->render_height == cfg->render_height; |
| 4500 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4501 | aom_wb_write_bit(wb, found); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4502 | if (found) { |
| 4503 | break; |
| 4504 | } |
| 4505 | } |
| 4506 | |
| 4507 | if (!found) { |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 4508 | write_frame_size(cm, wb); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4509 | } |
| 4510 | } |
| 4511 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4512 | static void write_sync_code(struct aom_write_bit_buffer *wb) { |
| 4513 | aom_wb_write_literal(wb, AV1_SYNC_CODE_0, 8); |
| 4514 | aom_wb_write_literal(wb, AV1_SYNC_CODE_1, 8); |
| 4515 | aom_wb_write_literal(wb, AV1_SYNC_CODE_2, 8); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | static void write_profile(BITSTREAM_PROFILE profile, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4519 | struct aom_write_bit_buffer *wb) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4520 | switch (profile) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4521 | case PROFILE_0: aom_wb_write_literal(wb, 0, 2); break; |
| 4522 | case PROFILE_1: aom_wb_write_literal(wb, 2, 2); break; |
| 4523 | case PROFILE_2: aom_wb_write_literal(wb, 1, 2); break; |
| 4524 | case PROFILE_3: aom_wb_write_literal(wb, 6, 3); break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4525 | default: assert(0); |
| 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | static void write_bitdepth_colorspace_sampling( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4530 | AV1_COMMON *const cm, struct aom_write_bit_buffer *wb) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4531 | if (cm->profile >= PROFILE_2) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4532 | assert(cm->bit_depth > AOM_BITS_8); |
| 4533 | aom_wb_write_bit(wb, cm->bit_depth == AOM_BITS_10 ? 0 : 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4534 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4535 | aom_wb_write_literal(wb, cm->color_space, 3); |
| 4536 | if (cm->color_space != AOM_CS_SRGB) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4537 | // 0: [16, 235] (i.e. xvYCC), 1: [0, 255] |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4538 | aom_wb_write_bit(wb, cm->color_range); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4539 | if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) { |
| 4540 | assert(cm->subsampling_x != 1 || cm->subsampling_y != 1); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4541 | aom_wb_write_bit(wb, cm->subsampling_x); |
| 4542 | aom_wb_write_bit(wb, cm->subsampling_y); |
| 4543 | aom_wb_write_bit(wb, 0); // unused |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4544 | } else { |
| 4545 | assert(cm->subsampling_x == 1 && cm->subsampling_y == 1); |
| 4546 | } |
| 4547 | } else { |
| 4548 | assert(cm->profile == PROFILE_1 || cm->profile == PROFILE_3); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4549 | aom_wb_write_bit(wb, 0); // unused |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4550 | } |
| 4551 | } |
| 4552 | |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 4553 | #if CONFIG_REFERENCE_BUFFER |
| 4554 | void write_sequence_header(SequenceHeader *seq_params) { |
| 4555 | /* Placeholder for actually writing to the bitstream */ |
| 4556 | seq_params->frame_id_numbers_present_flag = FRAME_ID_NUMBERS_PRESENT_FLAG; |
| 4557 | seq_params->frame_id_length_minus7 = FRAME_ID_LENGTH_MINUS7; |
| 4558 | seq_params->delta_frame_id_length_minus2 = DELTA_FRAME_ID_LENGTH_MINUS2; |
| 4559 | } |
| 4560 | #endif |
| 4561 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4562 | static void write_uncompressed_header(AV1_COMP *cpi, |
| 4563 | struct aom_write_bit_buffer *wb) { |
| 4564 | AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4565 | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
| 4566 | |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 4567 | #if CONFIG_REFERENCE_BUFFER |
| 4568 | /* TODO: Move outside frame loop or inside key-frame branch */ |
| 4569 | write_sequence_header(&cpi->seq_params); |
| 4570 | #endif |
| 4571 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4572 | aom_wb_write_literal(wb, AOM_FRAME_MARKER, 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4573 | |
| 4574 | write_profile(cm->profile, wb); |
| 4575 | |
| 4576 | #if CONFIG_EXT_REFS |
| 4577 | // NOTE: By default all coded frames to be used as a reference |
| 4578 | cm->is_reference_frame = 1; |
| 4579 | |
| 4580 | if (cm->show_existing_frame) { |
| 4581 | RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs; |
| 4582 | const int frame_to_show = cm->ref_frame_map[cpi->existing_fb_idx_to_show]; |
| 4583 | |
| 4584 | if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4585 | aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4586 | "Buffer %d does not contain a reconstructed frame", |
| 4587 | frame_to_show); |
| 4588 | } |
| 4589 | ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show); |
| 4590 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4591 | aom_wb_write_bit(wb, 1); // show_existing_frame |
| 4592 | aom_wb_write_literal(wb, cpi->existing_fb_idx_to_show, 3); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4593 | |
Arild Fuldseth (arilfuld) | 788dc23 | 2016-12-20 17:55:52 +0100 | [diff] [blame] | 4594 | #if CONFIG_REFERENCE_BUFFER |
| 4595 | if (cpi->seq_params.frame_id_numbers_present_flag) { |
| 4596 | int frame_id_len = cpi->seq_params.frame_id_length_minus7 + 7; |
| 4597 | int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show]; |
| 4598 | aom_wb_write_literal(wb, display_frame_id, frame_id_len); |
| 4599 | /* Add a zero byte to prevent emulation of superframe marker */ |
| 4600 | /* Same logic as when when terminating the entropy coder */ |
| 4601 | /* Consider to have this logic only one place */ |
| 4602 | aom_wb_write_literal(wb, 0, 8); |
| 4603 | } |
| 4604 | #endif |
| 4605 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4606 | return; |
| 4607 | } else { |
| 4608 | #endif // CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4609 | aom_wb_write_bit(wb, 0); // show_existing_frame |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4610 | #if CONFIG_EXT_REFS |
| 4611 | } |
| 4612 | #endif // CONFIG_EXT_REFS |
| 4613 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4614 | aom_wb_write_bit(wb, cm->frame_type); |
| 4615 | aom_wb_write_bit(wb, cm->show_frame); |
| 4616 | aom_wb_write_bit(wb, cm->error_resilient_mode); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4617 | |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 4618 | #if CONFIG_REFERENCE_BUFFER |
| 4619 | cm->invalid_delta_frame_id_minus1 = 0; |
| 4620 | if (cpi->seq_params.frame_id_numbers_present_flag) { |
Arild Fuldseth (arilfuld) | 788dc23 | 2016-12-20 17:55:52 +0100 | [diff] [blame] | 4621 | int frame_id_len = cpi->seq_params.frame_id_length_minus7 + 7; |
| 4622 | aom_wb_write_literal(wb, cm->current_frame_id, frame_id_len); |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 4623 | } |
| 4624 | #endif |
| 4625 | |
Fergus Simpson | d91c8c9 | 2017-04-07 12:12:00 -0700 | [diff] [blame] | 4626 | #if CONFIG_FRAME_SUPERRES |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 4627 | // TODO(afergs): Remove - this is just to stop superres from breaking |
| 4628 | cm->superres_scale_numerator = SUPERRES_SCALE_DENOMINATOR; |
Fergus Simpson | d91c8c9 | 2017-04-07 12:12:00 -0700 | [diff] [blame] | 4629 | #endif // CONFIG_FRAME_SUPERRES |
Fergus Simpson | e750841 | 2017-03-14 18:14:09 -0700 | [diff] [blame] | 4630 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4631 | if (cm->frame_type == KEY_FRAME) { |
| 4632 | write_sync_code(wb); |
| 4633 | write_bitdepth_colorspace_sampling(cm, wb); |
| 4634 | write_frame_size(cm, wb); |
Alex Converse | eb780e7 | 2016-12-13 12:46:41 -0800 | [diff] [blame] | 4635 | #if CONFIG_ANS && ANS_MAX_SYMBOLS |
| 4636 | assert(cpi->common.ans_window_size_log2 >= 8); |
| 4637 | assert(cpi->common.ans_window_size_log2 < 24); |
| 4638 | aom_wb_write_literal(wb, cpi->common.ans_window_size_log2 - 8, 4); |
| 4639 | #endif // CONFIG_ANS && ANS_MAX_SYMBOLS |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4640 | #if CONFIG_PALETTE |
hui su | 24f7b07 | 2016-10-12 11:36:24 -0700 | [diff] [blame] | 4641 | aom_wb_write_bit(wb, cm->allow_screen_content_tools); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4642 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4643 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4644 | if (!cm->show_frame) aom_wb_write_bit(wb, cm->intra_only); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4645 | #if CONFIG_PALETTE |
hui su | 24f7b07 | 2016-10-12 11:36:24 -0700 | [diff] [blame] | 4646 | if (cm->intra_only) aom_wb_write_bit(wb, cm->allow_screen_content_tools); |
Urvang Joshi | b100db7 | 2016-10-12 16:28:56 -0700 | [diff] [blame] | 4647 | #endif // CONFIG_PALETTE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4648 | if (!cm->error_resilient_mode) { |
| 4649 | if (cm->intra_only) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4650 | aom_wb_write_bit(wb, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4651 | cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL); |
| 4652 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4653 | aom_wb_write_bit(wb, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4654 | cm->reset_frame_context != RESET_FRAME_CONTEXT_NONE); |
| 4655 | if (cm->reset_frame_context != RESET_FRAME_CONTEXT_NONE) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4656 | aom_wb_write_bit(wb, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4657 | cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL); |
| 4658 | } |
| 4659 | } |
| 4660 | |
| 4661 | #if CONFIG_EXT_REFS |
| 4662 | cpi->refresh_frame_mask = get_refresh_mask(cpi); |
| 4663 | #endif // CONFIG_EXT_REFS |
| 4664 | |
| 4665 | if (cm->intra_only) { |
| 4666 | write_sync_code(wb); |
| 4667 | write_bitdepth_colorspace_sampling(cm, wb); |
| 4668 | |
| 4669 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4670 | aom_wb_write_literal(wb, cpi->refresh_frame_mask, REF_FRAMES); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4671 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4672 | aom_wb_write_literal(wb, get_refresh_mask(cpi), REF_FRAMES); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4673 | #endif // CONFIG_EXT_REFS |
| 4674 | write_frame_size(cm, wb); |
Alex Converse | eb780e7 | 2016-12-13 12:46:41 -0800 | [diff] [blame] | 4675 | |
| 4676 | #if CONFIG_ANS && ANS_MAX_SYMBOLS |
| 4677 | assert(cpi->common.ans_window_size_log2 >= 8); |
| 4678 | assert(cpi->common.ans_window_size_log2 < 24); |
| 4679 | aom_wb_write_literal(wb, cpi->common.ans_window_size_log2 - 8, 4); |
| 4680 | #endif // CONFIG_ANS && ANS_MAX_SYMBOLS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4681 | } else { |
| 4682 | MV_REFERENCE_FRAME ref_frame; |
| 4683 | |
| 4684 | #if CONFIG_EXT_REFS |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4685 | aom_wb_write_literal(wb, cpi->refresh_frame_mask, REF_FRAMES); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4686 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4687 | aom_wb_write_literal(wb, get_refresh_mask(cpi), REF_FRAMES); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4688 | #endif // CONFIG_EXT_REFS |
| 4689 | |
| 4690 | #if CONFIG_EXT_REFS |
| 4691 | if (!cpi->refresh_frame_mask) { |
| 4692 | // NOTE: "cpi->refresh_frame_mask == 0" indicates that the coded frame |
| 4693 | // will not be used as a reference |
| 4694 | cm->is_reference_frame = 0; |
| 4695 | } |
| 4696 | #endif // CONFIG_EXT_REFS |
| 4697 | |
| 4698 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 4699 | assert(get_ref_frame_map_idx(cpi, ref_frame) != INVALID_IDX); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4700 | aom_wb_write_literal(wb, get_ref_frame_map_idx(cpi, ref_frame), |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4701 | REF_FRAMES_LOG2); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4702 | aom_wb_write_bit(wb, cm->ref_frame_sign_bias[ref_frame]); |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 4703 | #if CONFIG_REFERENCE_BUFFER |
| 4704 | if (cpi->seq_params.frame_id_numbers_present_flag) { |
| 4705 | int i = get_ref_frame_map_idx(cpi, ref_frame); |
Arild Fuldseth (arilfuld) | 788dc23 | 2016-12-20 17:55:52 +0100 | [diff] [blame] | 4706 | int frame_id_len = cpi->seq_params.frame_id_length_minus7 + 7; |
| 4707 | int diff_len = cpi->seq_params.delta_frame_id_length_minus2 + 2; |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 4708 | int delta_frame_id_minus1 = |
Arild Fuldseth (arilfuld) | 788dc23 | 2016-12-20 17:55:52 +0100 | [diff] [blame] | 4709 | ((cm->current_frame_id - cm->ref_frame_id[i] + |
| 4710 | (1 << frame_id_len)) % |
| 4711 | (1 << frame_id_len)) - |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 4712 | 1; |
| 4713 | if (delta_frame_id_minus1 < 0 || |
Arild Fuldseth (arilfuld) | 788dc23 | 2016-12-20 17:55:52 +0100 | [diff] [blame] | 4714 | delta_frame_id_minus1 >= (1 << diff_len)) |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 4715 | cm->invalid_delta_frame_id_minus1 = 1; |
Arild Fuldseth (arilfuld) | 788dc23 | 2016-12-20 17:55:52 +0100 | [diff] [blame] | 4716 | aom_wb_write_literal(wb, delta_frame_id_minus1, diff_len); |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 4717 | } |
| 4718 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4719 | } |
| 4720 | |
Arild Fuldseth | 842e9b0 | 2016-09-02 13:00:05 +0200 | [diff] [blame] | 4721 | #if CONFIG_FRAME_SIZE |
| 4722 | if (cm->error_resilient_mode == 0) { |
| 4723 | write_frame_size_with_refs(cpi, wb); |
| 4724 | } else { |
| 4725 | write_frame_size(cm, wb); |
| 4726 | } |
| 4727 | #else |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4728 | write_frame_size_with_refs(cpi, wb); |
Arild Fuldseth | 842e9b0 | 2016-09-02 13:00:05 +0200 | [diff] [blame] | 4729 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4730 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4731 | aom_wb_write_bit(wb, cm->allow_high_precision_mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4732 | |
| 4733 | fix_interp_filter(cm, cpi->td.counts); |
Angie Chiang | 5678ad9 | 2016-11-21 09:38:40 -0800 | [diff] [blame] | 4734 | write_frame_interp_filter(cm->interp_filter, wb); |
Fangwen Fu | 8d164de | 2016-12-14 13:40:54 -0800 | [diff] [blame] | 4735 | #if CONFIG_TEMPMV_SIGNALING |
| 4736 | if (!cm->error_resilient_mode) { |
| 4737 | aom_wb_write_bit(wb, cm->use_prev_frame_mvs); |
| 4738 | } |
| 4739 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4740 | } |
| 4741 | } |
| 4742 | |
Arild Fuldseth (arilfuld) | 5114b7b | 2016-11-09 13:32:54 +0100 | [diff] [blame] | 4743 | #if CONFIG_REFERENCE_BUFFER |
| 4744 | cm->refresh_mask = cm->frame_type == KEY_FRAME ? 0xFF : get_refresh_mask(cpi); |
| 4745 | #endif |
| 4746 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4747 | if (!cm->error_resilient_mode) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4748 | aom_wb_write_bit( |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4749 | wb, cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_FORWARD); |
| 4750 | } |
| 4751 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4752 | aom_wb_write_literal(wb, cm->frame_context_idx, FRAME_CONTEXTS_LOG2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4753 | |
Jingning Han | c709e1f | 2016-12-06 14:48:09 -0800 | [diff] [blame] | 4754 | assert(cm->mib_size == mi_size_wide[cm->sb_size]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4755 | assert(cm->mib_size == 1 << cm->mib_size_log2); |
| 4756 | #if CONFIG_EXT_PARTITION |
| 4757 | assert(cm->sb_size == BLOCK_128X128 || cm->sb_size == BLOCK_64X64); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4758 | aom_wb_write_bit(wb, cm->sb_size == BLOCK_128X128 ? 1 : 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4759 | #else |
| 4760 | assert(cm->sb_size == BLOCK_64X64); |
| 4761 | #endif // CONFIG_EXT_PARTITION |
| 4762 | |
| 4763 | encode_loopfilter(cm, wb); |
Jean-Marc Valin | 0143513 | 2017-02-18 14:12:53 -0500 | [diff] [blame] | 4764 | #if CONFIG_CDEF |
Steinar Midtskogen | a9d41e8 | 2017-03-17 12:48:15 +0100 | [diff] [blame] | 4765 | encode_cdef(cm, wb); |
Steinar Midtskogen | 5d56f4d | 2016-09-25 09:23:16 +0200 | [diff] [blame] | 4766 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4767 | #if CONFIG_LOOP_RESTORATION |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 4768 | encode_restoration_mode(cm, wb); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4769 | #endif // CONFIG_LOOP_RESTORATION |
| 4770 | encode_quantization(cm, wb); |
| 4771 | encode_segmentation(cm, xd, wb); |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 4772 | #if CONFIG_DELTA_Q |
| 4773 | { |
| 4774 | int i; |
| 4775 | struct segmentation *const seg = &cm->seg; |
| 4776 | int segment_quantizer_active = 0; |
| 4777 | for (i = 0; i < MAX_SEGMENTS; i++) { |
| 4778 | if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) { |
| 4779 | segment_quantizer_active = 1; |
| 4780 | } |
| 4781 | } |
Arild Fuldseth (arilfuld) | 54de7d6 | 2017-03-20 13:07:11 +0100 | [diff] [blame] | 4782 | |
| 4783 | if (cm->delta_q_present_flag) |
| 4784 | assert(segment_quantizer_active == 0 && cm->base_qindex > 0); |
| 4785 | if (segment_quantizer_active == 0 && cm->base_qindex > 0) { |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 4786 | aom_wb_write_bit(wb, cm->delta_q_present_flag); |
| 4787 | if (cm->delta_q_present_flag) { |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 4788 | aom_wb_write_literal(wb, OD_ILOG_NZ(cm->delta_q_res) - 1, 2); |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 4789 | xd->prev_qindex = cm->base_qindex; |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 4790 | #if CONFIG_EXT_DELTA_Q |
| 4791 | assert(seg->abs_delta == SEGMENT_DELTADATA); |
| 4792 | aom_wb_write_bit(wb, cm->delta_lf_present_flag); |
| 4793 | if (cm->delta_lf_present_flag) { |
| 4794 | aom_wb_write_literal(wb, OD_ILOG_NZ(cm->delta_lf_res) - 1, 2); |
| 4795 | xd->prev_delta_lf_from_base = 0; |
| 4796 | } |
| 4797 | #endif // CONFIG_EXT_DELTA_Q |
Arild Fuldseth | 0744116 | 2016-08-15 15:07:52 +0200 | [diff] [blame] | 4798 | } |
| 4799 | } |
| 4800 | } |
| 4801 | #endif |
| 4802 | |
Yue Chen | eeacc4c | 2017-01-17 17:29:17 -0800 | [diff] [blame] | 4803 | write_tx_mode(cm, xd, &cm->tx_mode, wb); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4804 | |
| 4805 | if (cpi->allow_comp_inter_inter) { |
| 4806 | const int use_hybrid_pred = cm->reference_mode == REFERENCE_MODE_SELECT; |
Zoe Liu | b05e5d1 | 2017-02-07 14:32:53 -0800 | [diff] [blame] | 4807 | #if !CONFIG_REF_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4808 | const int use_compound_pred = cm->reference_mode != SINGLE_REFERENCE; |
Zoe Liu | b05e5d1 | 2017-02-07 14:32:53 -0800 | [diff] [blame] | 4809 | #endif // !CONFIG_REF_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4810 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4811 | aom_wb_write_bit(wb, use_hybrid_pred); |
Zoe Liu | b05e5d1 | 2017-02-07 14:32:53 -0800 | [diff] [blame] | 4812 | #if !CONFIG_REF_ADAPT |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4813 | if (!use_hybrid_pred) aom_wb_write_bit(wb, use_compound_pred); |
Zoe Liu | b05e5d1 | 2017-02-07 14:32:53 -0800 | [diff] [blame] | 4814 | #endif // !CONFIG_REF_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4815 | } |
| 4816 | |
Sarah Parker | e68a3e4 | 2017-02-16 14:03:24 -0800 | [diff] [blame] | 4817 | #if CONFIG_EXT_TX |
| 4818 | aom_wb_write_bit(wb, cm->reduced_tx_set_used); |
| 4819 | #endif // CONFIG_EXT_TX |
| 4820 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4821 | write_tile_info(cm, wb); |
| 4822 | } |
| 4823 | |
| 4824 | #if CONFIG_GLOBAL_MOTION |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 4825 | static void write_global_motion_params(WarpedMotionParams *params, |
Sarah Parker | f178329 | 2017-04-05 11:55:27 -0700 | [diff] [blame] | 4826 | WarpedMotionParams *ref_params, |
Sarah Parker | 13d0662 | 2017-03-10 17:03:28 -0800 | [diff] [blame] | 4827 | aom_prob *probs, aom_writer *w, |
| 4828 | int allow_hp) { |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 4829 | TransformationType type = params->wmtype; |
Sarah Parker | 13d0662 | 2017-03-10 17:03:28 -0800 | [diff] [blame] | 4830 | int trans_bits; |
| 4831 | int trans_prec_diff; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4832 | av1_write_token(w, av1_global_motion_types_tree, probs, |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 4833 | &global_motion_types_encodings[type]); |
| 4834 | switch (type) { |
Debargha Mukherjee | 3fb33f0 | 2016-11-12 10:43:50 -0800 | [diff] [blame] | 4835 | case HOMOGRAPHY: |
Debargha Mukherjee | 5dfa930 | 2017-02-10 05:00:08 -0800 | [diff] [blame] | 4836 | case HORTRAPEZOID: |
| 4837 | case VERTRAPEZOID: |
| 4838 | if (type != HORTRAPEZOID) |
Sarah Parker | f178329 | 2017-04-05 11:55:27 -0700 | [diff] [blame] | 4839 | aom_write_signed_primitive_refsubexpfin( |
| 4840 | w, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K, |
| 4841 | (ref_params->wmmat[6] >> GM_ROW3HOMO_PREC_DIFF), |
| 4842 | (params->wmmat[6] >> GM_ROW3HOMO_PREC_DIFF)); |
Debargha Mukherjee | 5dfa930 | 2017-02-10 05:00:08 -0800 | [diff] [blame] | 4843 | if (type != VERTRAPEZOID) |
Sarah Parker | f178329 | 2017-04-05 11:55:27 -0700 | [diff] [blame] | 4844 | aom_write_signed_primitive_refsubexpfin( |
| 4845 | w, GM_ROW3HOMO_MAX + 1, SUBEXPFIN_K, |
| 4846 | (ref_params->wmmat[7] >> GM_ROW3HOMO_PREC_DIFF), |
| 4847 | (params->wmmat[7] >> GM_ROW3HOMO_PREC_DIFF)); |
Debargha Mukherjee | 3fb33f0 | 2016-11-12 10:43:50 -0800 | [diff] [blame] | 4848 | // fallthrough intended |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 4849 | case AFFINE: |
| 4850 | case ROTZOOM: |
Sarah Parker | f178329 | 2017-04-05 11:55:27 -0700 | [diff] [blame] | 4851 | aom_write_signed_primitive_refsubexpfin( |
| 4852 | w, GM_ALPHA_MAX + 1, SUBEXPFIN_K, |
| 4853 | (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) - |
| 4854 | (1 << GM_ALPHA_PREC_BITS), |
| 4855 | (params->wmmat[2] >> GM_ALPHA_PREC_DIFF) - (1 << GM_ALPHA_PREC_BITS)); |
Debargha Mukherjee | 5dfa930 | 2017-02-10 05:00:08 -0800 | [diff] [blame] | 4856 | if (type != VERTRAPEZOID) |
Sarah Parker | f178329 | 2017-04-05 11:55:27 -0700 | [diff] [blame] | 4857 | aom_write_signed_primitive_refsubexpfin( |
| 4858 | w, GM_ALPHA_MAX + 1, SUBEXPFIN_K, |
| 4859 | (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF), |
| 4860 | (params->wmmat[3] >> GM_ALPHA_PREC_DIFF)); |
Debargha Mukherjee | 5dfa930 | 2017-02-10 05:00:08 -0800 | [diff] [blame] | 4861 | if (type >= AFFINE) { |
| 4862 | if (type != HORTRAPEZOID) |
Sarah Parker | f178329 | 2017-04-05 11:55:27 -0700 | [diff] [blame] | 4863 | aom_write_signed_primitive_refsubexpfin( |
| 4864 | w, GM_ALPHA_MAX + 1, SUBEXPFIN_K, |
| 4865 | (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF), |
| 4866 | (params->wmmat[4] >> GM_ALPHA_PREC_DIFF)); |
| 4867 | aom_write_signed_primitive_refsubexpfin( |
| 4868 | w, GM_ALPHA_MAX + 1, SUBEXPFIN_K, |
| 4869 | (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) - |
| 4870 | (1 << GM_ALPHA_PREC_BITS), |
| 4871 | (params->wmmat[5] >> GM_ALPHA_PREC_DIFF) - |
| 4872 | (1 << GM_ALPHA_PREC_BITS)); |
Debargha Mukherjee | 8db4c77 | 2016-11-07 12:54:21 -0800 | [diff] [blame] | 4873 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4874 | // fallthrough intended |
David Barker | cf3d0b0 | 2016-11-10 10:14:49 +0000 | [diff] [blame] | 4875 | case TRANSLATION: |
Sarah Parker | 13d0662 | 2017-03-10 17:03:28 -0800 | [diff] [blame] | 4876 | trans_bits = (type == TRANSLATION) ? GM_ABS_TRANS_ONLY_BITS - !allow_hp |
| 4877 | : GM_ABS_TRANS_BITS; |
| 4878 | trans_prec_diff = (type == TRANSLATION) |
| 4879 | ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp |
| 4880 | : GM_TRANS_PREC_DIFF; |
Sarah Parker | f178329 | 2017-04-05 11:55:27 -0700 | [diff] [blame] | 4881 | aom_write_signed_primitive_refsubexpfin( |
| 4882 | w, (1 << trans_bits) + 1, SUBEXPFIN_K, |
| 4883 | (ref_params->wmmat[0] >> trans_prec_diff), |
| 4884 | (params->wmmat[0] >> trans_prec_diff)); |
| 4885 | aom_write_signed_primitive_refsubexpfin( |
| 4886 | w, (1 << trans_bits) + 1, SUBEXPFIN_K, |
| 4887 | (ref_params->wmmat[1] >> trans_prec_diff), |
| 4888 | (params->wmmat[1] >> trans_prec_diff)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4889 | break; |
Debargha Mukherjee | 3fb33f0 | 2016-11-12 10:43:50 -0800 | [diff] [blame] | 4890 | case IDENTITY: break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4891 | default: assert(0); |
| 4892 | } |
| 4893 | } |
| 4894 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4895 | static void write_global_motion(AV1_COMP *cpi, aom_writer *w) { |
| 4896 | AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4897 | int frame; |
| 4898 | for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) { |
David Barker | 43479c6 | 2016-11-30 10:34:20 +0000 | [diff] [blame] | 4899 | #if !CONFIG_REF_MV |
| 4900 | // With ref-mv, clearing unused global motion models here is |
| 4901 | // unsafe, and we need to rely on the recode loop to do it |
| 4902 | // instead. See av1_find_mv_refs for details. |
Debargha Mukherjee | 265db6d | 2017-03-28 11:15:27 -0700 | [diff] [blame] | 4903 | if (!cpi->global_motion_used[frame]) { |
Sarah Parker | 4c10a3c | 2017-04-10 19:37:59 -0700 | [diff] [blame] | 4904 | set_default_warp_params(&cm->global_motion[frame]); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4905 | } |
David Barker | 43479c6 | 2016-11-30 10:34:20 +0000 | [diff] [blame] | 4906 | #endif |
Sarah Parker | f178329 | 2017-04-05 11:55:27 -0700 | [diff] [blame] | 4907 | write_global_motion_params( |
| 4908 | &cm->global_motion[frame], &cm->prev_frame->global_motion[frame], |
| 4909 | cm->fc->global_motion_types_prob, w, cm->allow_high_precision_mv); |
Sarah Parker | e529986 | 2016-08-16 14:57:37 -0700 | [diff] [blame] | 4910 | /* |
Debargha Mukherjee | 265db6d | 2017-03-28 11:15:27 -0700 | [diff] [blame] | 4911 | printf("Frame %d/%d: Enc Ref %d (used %d): %d %d %d %d\n", |
Debargha Mukherjee | b98a702 | 2016-11-15 16:07:12 -0800 | [diff] [blame] | 4912 | cm->current_video_frame, cm->show_frame, frame, |
Debargha Mukherjee | 265db6d | 2017-03-28 11:15:27 -0700 | [diff] [blame] | 4913 | cpi->global_motion_used[frame], cm->global_motion[frame].wmmat[0], |
| 4914 | cm->global_motion[frame].wmmat[1], cm->global_motion[frame].wmmat[2], |
| 4915 | cm->global_motion[frame].wmmat[3]); |
Debargha Mukherjee | 8db4c77 | 2016-11-07 12:54:21 -0800 | [diff] [blame] | 4916 | */ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4917 | } |
| 4918 | } |
| 4919 | #endif |
| 4920 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4921 | static uint32_t write_compressed_header(AV1_COMP *cpi, uint8_t *data) { |
| 4922 | AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4923 | #if CONFIG_SUPERTX |
| 4924 | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
| 4925 | #endif // CONFIG_SUPERTX |
| 4926 | FRAME_CONTEXT *const fc = cm->fc; |
| 4927 | FRAME_COUNTS *counts = cpi->td.counts; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4928 | aom_writer *header_bc; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4929 | int i, j; |
Thomas | 9ac5508 | 2016-09-23 18:04:17 +0100 | [diff] [blame] | 4930 | |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4931 | #if CONFIG_TILE_GROUPS |
| 4932 | const int probwt = cm->num_tg; |
| 4933 | #else |
| 4934 | const int probwt = 1; |
| 4935 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4936 | |
| 4937 | #if CONFIG_ANS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4938 | int header_size; |
| 4939 | header_bc = &cpi->buf_ans; |
Alex Converse | 2a1b3af | 2016-10-26 13:11:26 -0700 | [diff] [blame] | 4940 | buf_ans_write_init(header_bc, data); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4941 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4942 | aom_writer real_header_bc; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4943 | header_bc = &real_header_bc; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4944 | aom_start_encode(header_bc, data); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4945 | #endif |
Debargha Mukherjee | 5cd2ab9 | 2016-09-08 15:15:17 -0700 | [diff] [blame] | 4946 | |
| 4947 | #if CONFIG_LOOP_RESTORATION |
| 4948 | encode_restoration(cm, header_bc); |
| 4949 | #endif // CONFIG_LOOP_RESTORATION |
Nathan E. Egge | b353a8e | 2017-02-17 10:27:37 -0500 | [diff] [blame] | 4950 | #if !CONFIG_EC_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4951 | update_txfm_probs(cm, header_bc, counts); |
Nathan E. Egge | b353a8e | 2017-02-17 10:27:37 -0500 | [diff] [blame] | 4952 | #endif |
Angie Chiang | 800df03 | 2017-03-22 11:14:12 -0700 | [diff] [blame] | 4953 | #if CONFIG_LV_MAP |
| 4954 | av1_write_txb_probs(cpi, header_bc); |
| 4955 | #else |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 4956 | #if !CONFIG_PVQ |
Alex Converse | a9598cd | 2017-02-03 14:18:05 -0800 | [diff] [blame] | 4957 | #if !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4958 | update_coef_probs(cpi, header_bc); |
Alex Converse | a9598cd | 2017-02-03 14:18:05 -0800 | [diff] [blame] | 4959 | #endif // !(CONFIG_EC_ADAPT && CONFIG_NEW_TOKENSET) |
Thomas Davies | fc1598a | 2017-01-13 17:07:25 +0000 | [diff] [blame] | 4960 | #endif // CONFIG_PVQ |
Angie Chiang | 800df03 | 2017-03-22 11:14:12 -0700 | [diff] [blame] | 4961 | #endif // CONFIG_LV_MAP |
| 4962 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4963 | #if CONFIG_VAR_TX |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4964 | update_txfm_partition_probs(cm, header_bc, counts, probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4965 | #endif |
| 4966 | |
| 4967 | update_skip_probs(cm, header_bc, counts); |
Thomas Davies | d6ee8a8 | 2017-03-02 14:42:50 +0000 | [diff] [blame] | 4968 | #if !CONFIG_EC_ADAPT && CONFIG_DELTA_Q |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 4969 | update_delta_q_probs(cm, header_bc, counts); |
Fangwen Fu | 231fe42 | 2017-04-24 17:52:29 -0700 | [diff] [blame] | 4970 | #if CONFIG_EXT_DELTA_Q |
| 4971 | update_delta_lf_probs(cm, header_bc, counts); |
| 4972 | #endif |
Thomas Davies | f693610 | 2016-09-05 16:51:31 +0100 | [diff] [blame] | 4973 | #endif |
Nathan E. Egge | baaaa16 | 2016-10-24 09:50:52 -0400 | [diff] [blame] | 4974 | #if !CONFIG_EC_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4975 | update_seg_probs(cpi, header_bc); |
| 4976 | |
Nathan E. Egge | 380cb1a | 2016-09-08 10:13:42 -0400 | [diff] [blame] | 4977 | for (i = 0; i < INTRA_MODES; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4978 | prob_diff_update(av1_intra_mode_tree, fc->uv_mode_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4979 | counts->uv_mode[i], INTRA_MODES, probwt, header_bc); |
Nathan E. Egge | 380cb1a | 2016-09-08 10:13:42 -0400 | [diff] [blame] | 4980 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4981 | |
| 4982 | #if CONFIG_EXT_PARTITION_TYPES |
Alex Converse | 4e18d40 | 2017-03-14 15:36:38 -0700 | [diff] [blame] | 4983 | for (i = 0; i < PARTITION_PLOFFSET; ++i) |
| 4984 | prob_diff_update(av1_partition_tree, fc->partition_prob[i], |
| 4985 | counts->partition[i], PARTITION_TYPES, probwt, header_bc); |
| 4986 | for (; i < PARTITION_CONTEXTS_PRIMARY; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4987 | prob_diff_update(av1_ext_partition_tree, fc->partition_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4988 | counts->partition[i], EXT_PARTITION_TYPES, probwt, |
| 4989 | header_bc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 4990 | #else |
Alex Converse | 4e18d40 | 2017-03-14 15:36:38 -0700 | [diff] [blame] | 4991 | for (i = 0; i < PARTITION_CONTEXTS_PRIMARY; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 4992 | prob_diff_update(av1_partition_tree, fc->partition_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 4993 | counts->partition[i], PARTITION_TYPES, probwt, header_bc); |
Alex Converse | 55c6bde | 2017-01-12 15:55:31 -0800 | [diff] [blame] | 4994 | #endif // CONFIG_EXT_PARTITION_TYPES |
| 4995 | #if CONFIG_UNPOISON_PARTITION_CTX |
| 4996 | for (; i < PARTITION_CONTEXTS_PRIMARY + PARTITION_BLOCK_SIZES; ++i) { |
| 4997 | unsigned int ct[2] = { counts->partition[i][PARTITION_VERT], |
| 4998 | counts->partition[i][PARTITION_SPLIT] }; |
| 4999 | assert(counts->partition[i][PARTITION_NONE] == 0); |
| 5000 | assert(counts->partition[i][PARTITION_HORZ] == 0); |
| 5001 | assert(fc->partition_prob[i][PARTITION_NONE] == 0); |
| 5002 | assert(fc->partition_prob[i][PARTITION_HORZ] == 0); |
| 5003 | av1_cond_prob_diff_update(header_bc, &fc->partition_prob[i][PARTITION_VERT], |
| 5004 | ct, probwt); |
| 5005 | } |
| 5006 | for (; i < PARTITION_CONTEXTS_PRIMARY + 2 * PARTITION_BLOCK_SIZES; ++i) { |
| 5007 | unsigned int ct[2] = { counts->partition[i][PARTITION_HORZ], |
| 5008 | counts->partition[i][PARTITION_SPLIT] }; |
| 5009 | assert(counts->partition[i][PARTITION_NONE] == 0); |
| 5010 | assert(counts->partition[i][PARTITION_VERT] == 0); |
| 5011 | assert(fc->partition_prob[i][PARTITION_NONE] == 0); |
| 5012 | assert(fc->partition_prob[i][PARTITION_VERT] == 0); |
| 5013 | av1_cond_prob_diff_update(header_bc, &fc->partition_prob[i][PARTITION_HORZ], |
| 5014 | ct, probwt); |
| 5015 | } |
| 5016 | #endif |
hui su | 9aa9749 | 2017-01-26 16:46:01 -0800 | [diff] [blame] | 5017 | #if CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5018 | for (i = 0; i < INTRA_FILTERS + 1; ++i) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5019 | prob_diff_update(av1_intra_filter_tree, fc->intra_filter_probs[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5020 | counts->intra_filter[i], INTRA_FILTERS, probwt, header_bc); |
hui su | 9aa9749 | 2017-01-26 16:46:01 -0800 | [diff] [blame] | 5021 | #endif // CONFIG_EXT_INTRA && CONFIG_INTRA_INTERP |
hui su | b4e25d2 | 2017-03-09 15:32:30 -0800 | [diff] [blame] | 5022 | #endif // !CONFIG_EC_ADAPT |
hui su | 9aa9749 | 2017-01-26 16:46:01 -0800 | [diff] [blame] | 5023 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5024 | if (frame_is_intra_only(cm)) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5025 | av1_copy(cm->kf_y_prob, av1_kf_y_mode_prob); |
Nathan E. Egge | 10ba2be | 2016-11-16 09:44:26 -0500 | [diff] [blame] | 5026 | #if CONFIG_EC_MULTISYMBOL |
Thomas Davies | 1bfb5ed | 2017-01-11 15:28:11 +0000 | [diff] [blame] | 5027 | av1_copy(cm->fc->kf_y_cdf, av1_kf_y_mode_cdf); |
Nathan E. Egge | 3ef926e | 2016-09-07 18:20:41 -0400 | [diff] [blame] | 5028 | #endif |
Thomas | 9ac5508 | 2016-09-23 18:04:17 +0100 | [diff] [blame] | 5029 | |
Nathan E. Egge | baaaa16 | 2016-10-24 09:50:52 -0400 | [diff] [blame] | 5030 | #if !CONFIG_EC_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5031 | for (i = 0; i < INTRA_MODES; ++i) |
Thomas Davies | 6519beb | 2016-10-19 14:46:07 +0100 | [diff] [blame] | 5032 | for (j = 0; j < INTRA_MODES; ++j) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5033 | prob_diff_update(av1_intra_mode_tree, cm->kf_y_prob[i][j], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5034 | counts->kf_y_mode[i][j], INTRA_MODES, probwt, |
| 5035 | header_bc); |
Nathan E. Egge | baaaa16 | 2016-10-24 09:50:52 -0400 | [diff] [blame] | 5036 | #endif // CONFIG_EC_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5037 | } else { |
| 5038 | #if CONFIG_REF_MV |
| 5039 | update_inter_mode_probs(cm, header_bc, counts); |
| 5040 | #else |
Nathan E. Egge | baaaa16 | 2016-10-24 09:50:52 -0400 | [diff] [blame] | 5041 | #if !CONFIG_EC_ADAPT |
Nathan E. Egge | 6ec4d10 | 2016-09-08 10:41:20 -0400 | [diff] [blame] | 5042 | for (i = 0; i < INTER_MODE_CONTEXTS; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5043 | prob_diff_update(av1_inter_mode_tree, cm->fc->inter_mode_probs[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5044 | counts->inter_mode[i], INTER_MODES, probwt, header_bc); |
Nathan E. Egge | 6ec4d10 | 2016-09-08 10:41:20 -0400 | [diff] [blame] | 5045 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5046 | #endif |
Thomas Davies | 6519beb | 2016-10-19 14:46:07 +0100 | [diff] [blame] | 5047 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5048 | #if CONFIG_EXT_INTER |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5049 | update_inter_compound_mode_probs(cm, probwt, header_bc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5050 | |
| 5051 | if (cm->reference_mode != COMPOUND_REFERENCE) { |
| 5052 | for (i = 0; i < BLOCK_SIZE_GROUPS; i++) { |
| 5053 | if (is_interintra_allowed_bsize_group(i)) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5054 | av1_cond_prob_diff_update(header_bc, &fc->interintra_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5055 | cm->counts.interintra[i], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5056 | } |
| 5057 | } |
| 5058 | for (i = 0; i < BLOCK_SIZE_GROUPS; i++) { |
| 5059 | prob_diff_update( |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5060 | av1_interintra_mode_tree, cm->fc->interintra_mode_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5061 | counts->interintra_mode[i], INTERINTRA_MODES, probwt, header_bc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5062 | } |
| 5063 | for (i = 0; i < BLOCK_SIZES; i++) { |
| 5064 | if (is_interintra_allowed_bsize(i) && is_interintra_wedge_used(i)) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5065 | av1_cond_prob_diff_update(header_bc, &fc->wedge_interintra_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5066 | cm->counts.wedge_interintra[i], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5067 | } |
| 5068 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 5069 | #if CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5070 | if (cm->reference_mode != SINGLE_REFERENCE) { |
| 5071 | for (i = 0; i < BLOCK_SIZES; i++) |
Sarah Parker | 6fdc853 | 2016-11-16 17:47:13 -0800 | [diff] [blame] | 5072 | prob_diff_update(av1_compound_type_tree, fc->compound_type_prob[i], |
| 5073 | cm->counts.compound_interinter[i], COMPOUND_TYPES, |
| 5074 | probwt, header_bc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5075 | } |
Debargha Mukherjee | c5f735f | 2017-04-26 03:25:28 +0000 | [diff] [blame] | 5076 | #endif // CONFIG_COMPOUND_SEGMENT || CONFIG_WEDGE |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5077 | #endif // CONFIG_EXT_INTER |
| 5078 | |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 5079 | #if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5080 | for (i = BLOCK_8X8; i < BLOCK_SIZES; ++i) |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 5081 | prob_diff_update(av1_motion_mode_tree, fc->motion_mode_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5082 | counts->motion_mode[i], MOTION_MODES, probwt, header_bc); |
Yue Chen | cb60b18 | 2016-10-13 15:18:22 -0700 | [diff] [blame] | 5083 | #endif // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION |
Nathan E. Egge | baaaa16 | 2016-10-24 09:50:52 -0400 | [diff] [blame] | 5084 | #if !CONFIG_EC_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5085 | if (cm->interp_filter == SWITCHABLE) |
| 5086 | update_switchable_interp_probs(cm, header_bc, counts); |
Thomas | 9ac5508 | 2016-09-23 18:04:17 +0100 | [diff] [blame] | 5087 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5088 | |
| 5089 | for (i = 0; i < INTRA_INTER_CONTEXTS; i++) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5090 | av1_cond_prob_diff_update(header_bc, &fc->intra_inter_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5091 | counts->intra_inter[i], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5092 | |
| 5093 | if (cpi->allow_comp_inter_inter) { |
| 5094 | const int use_hybrid_pred = cm->reference_mode == REFERENCE_MODE_SELECT; |
| 5095 | if (use_hybrid_pred) |
| 5096 | for (i = 0; i < COMP_INTER_CONTEXTS; i++) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5097 | av1_cond_prob_diff_update(header_bc, &fc->comp_inter_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5098 | counts->comp_inter[i], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5099 | } |
| 5100 | |
| 5101 | if (cm->reference_mode != COMPOUND_REFERENCE) { |
| 5102 | for (i = 0; i < REF_CONTEXTS; i++) { |
| 5103 | for (j = 0; j < (SINGLE_REFS - 1); j++) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5104 | av1_cond_prob_diff_update(header_bc, &fc->single_ref_prob[i][j], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5105 | counts->single_ref[i][j], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5106 | } |
| 5107 | } |
| 5108 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5109 | if (cm->reference_mode != SINGLE_REFERENCE) { |
| 5110 | for (i = 0; i < REF_CONTEXTS; i++) { |
| 5111 | #if CONFIG_EXT_REFS |
| 5112 | for (j = 0; j < (FWD_REFS - 1); j++) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5113 | av1_cond_prob_diff_update(header_bc, &fc->comp_ref_prob[i][j], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5114 | counts->comp_ref[i][j], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5115 | } |
| 5116 | for (j = 0; j < (BWD_REFS - 1); j++) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5117 | av1_cond_prob_diff_update(header_bc, &fc->comp_bwdref_prob[i][j], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5118 | counts->comp_bwdref[i][j], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5119 | } |
| 5120 | #else |
| 5121 | for (j = 0; j < (COMP_REFS - 1); j++) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5122 | av1_cond_prob_diff_update(header_bc, &fc->comp_ref_prob[i][j], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5123 | counts->comp_ref[i][j], probwt); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5124 | } |
| 5125 | #endif // CONFIG_EXT_REFS |
| 5126 | } |
| 5127 | } |
| 5128 | |
Nathan E. Egge | baaaa16 | 2016-10-24 09:50:52 -0400 | [diff] [blame] | 5129 | #if !CONFIG_EC_ADAPT |
Nathan E. Egge | 5710c72 | 2016-09-08 10:01:16 -0400 | [diff] [blame] | 5130 | for (i = 0; i < BLOCK_SIZE_GROUPS; ++i) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5131 | prob_diff_update(av1_intra_mode_tree, cm->fc->y_mode_prob[i], |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5132 | counts->y_mode[i], INTRA_MODES, probwt, header_bc); |
Nathan E. Egge | 5710c72 | 2016-09-08 10:01:16 -0400 | [diff] [blame] | 5133 | } |
Thomas Davies | 6519beb | 2016-10-19 14:46:07 +0100 | [diff] [blame] | 5134 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5135 | |
Jingning Han | fd0cf16 | 2016-09-30 10:33:50 -0700 | [diff] [blame] | 5136 | av1_write_nmv_probs(cm, cm->allow_high_precision_mv, header_bc, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5137 | #if CONFIG_REF_MV |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5138 | counts->mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5139 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5140 | &counts->mv); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5141 | #endif |
Nathan E. Egge | baaaa16 | 2016-10-24 09:50:52 -0400 | [diff] [blame] | 5142 | #if !CONFIG_EC_ADAPT |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5143 | update_ext_tx_probs(cm, header_bc); |
Thomas | 9ac5508 | 2016-09-23 18:04:17 +0100 | [diff] [blame] | 5144 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5145 | #if CONFIG_SUPERTX |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5146 | if (!xd->lossless[0]) update_supertx_probs(cm, probwt, header_bc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5147 | #endif // CONFIG_SUPERTX |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5148 | #if CONFIG_GLOBAL_MOTION |
Sarah Parker | 689b0ca | 2016-10-11 12:06:33 -0700 | [diff] [blame] | 5149 | write_global_motion(cpi, header_bc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5150 | #endif // CONFIG_GLOBAL_MOTION |
Sarah Parker | 689b0ca | 2016-10-11 12:06:33 -0700 | [diff] [blame] | 5151 | } |
Thomas Davies | 6519beb | 2016-10-19 14:46:07 +0100 | [diff] [blame] | 5152 | #if CONFIG_EC_MULTISYMBOL |
Thomas Davies | 028b57f | 2017-02-22 16:42:11 +0000 | [diff] [blame] | 5153 | #if !CONFIG_EC_ADAPT |
Thomas Davies | 87aeeb8 | 2017-02-17 00:19:40 +0000 | [diff] [blame] | 5154 | #if CONFIG_NEW_TOKENSET |
| 5155 | av1_coef_head_cdfs(fc); |
| 5156 | #endif |
Thomas Davies | 6519beb | 2016-10-19 14:46:07 +0100 | [diff] [blame] | 5157 | av1_coef_pareto_cdfs(fc); |
David Barker | 599dfd0 | 2016-11-10 13:20:12 +0000 | [diff] [blame] | 5158 | #if CONFIG_REF_MV |
| 5159 | for (i = 0; i < NMV_CONTEXTS; ++i) av1_set_mv_cdfs(&fc->nmvc[i]); |
| 5160 | #else |
Thomas Davies | 6519beb | 2016-10-19 14:46:07 +0100 | [diff] [blame] | 5161 | av1_set_mv_cdfs(&fc->nmvc); |
David Barker | 599dfd0 | 2016-11-10 13:20:12 +0000 | [diff] [blame] | 5162 | #endif |
Nathan E. Egge | 3129606 | 2016-11-16 09:44:26 -0500 | [diff] [blame] | 5163 | #if CONFIG_EC_MULTISYMBOL |
Thomas Davies | 6519beb | 2016-10-19 14:46:07 +0100 | [diff] [blame] | 5164 | av1_set_mode_cdfs(cm); |
| 5165 | #endif |
Thomas Davies | 028b57f | 2017-02-22 16:42:11 +0000 | [diff] [blame] | 5166 | #endif // !CONFIG_EC_ADAPT |
Thomas Davies | 6519beb | 2016-10-19 14:46:07 +0100 | [diff] [blame] | 5167 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5168 | #if CONFIG_ANS |
Alex Converse | 1ecdf2b | 2016-11-30 15:51:12 -0800 | [diff] [blame] | 5169 | aom_buf_ans_flush(header_bc); |
Alex Converse | 2a1b3af | 2016-10-26 13:11:26 -0700 | [diff] [blame] | 5170 | header_size = buf_ans_write_end(header_bc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5171 | assert(header_size <= 0xffff); |
| 5172 | return header_size; |
| 5173 | #else |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5174 | aom_stop_encode(header_bc); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5175 | assert(header_bc->pos <= 0xffff); |
| 5176 | return header_bc->pos; |
| 5177 | #endif // CONFIG_ANS |
| 5178 | } |
| 5179 | |
| 5180 | static int choose_size_bytes(uint32_t size, int spare_msbs) { |
| 5181 | // Choose the number of bytes required to represent size, without |
| 5182 | // using the 'spare_msbs' number of most significant bits. |
| 5183 | |
| 5184 | // Make sure we will fit in 4 bytes to start with.. |
| 5185 | if (spare_msbs > 0 && size >> (32 - spare_msbs) != 0) return -1; |
| 5186 | |
| 5187 | // Normalise to 32 bits |
| 5188 | size <<= spare_msbs; |
| 5189 | |
| 5190 | if (size >> 24 != 0) |
| 5191 | return 4; |
| 5192 | else if (size >> 16 != 0) |
| 5193 | return 3; |
| 5194 | else if (size >> 8 != 0) |
| 5195 | return 2; |
| 5196 | else |
| 5197 | return 1; |
| 5198 | } |
| 5199 | |
| 5200 | static void mem_put_varsize(uint8_t *const dst, const int sz, const int val) { |
| 5201 | switch (sz) { |
| 5202 | case 1: dst[0] = (uint8_t)(val & 0xff); break; |
| 5203 | case 2: mem_put_le16(dst, val); break; |
| 5204 | case 3: mem_put_le24(dst, val); break; |
| 5205 | case 4: mem_put_le32(dst, val); break; |
James Zern | 06c372d | 2017-04-20 16:08:29 -0700 | [diff] [blame] | 5206 | default: assert(0 && "Invalid size"); break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5207 | } |
| 5208 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5209 | static int remux_tiles(const AV1_COMMON *const cm, uint8_t *dst, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5210 | const uint32_t data_size, const uint32_t max_tile_size, |
| 5211 | const uint32_t max_tile_col_size, |
| 5212 | int *const tile_size_bytes, |
| 5213 | int *const tile_col_size_bytes) { |
| 5214 | // Choose the tile size bytes (tsb) and tile column size bytes (tcsb) |
| 5215 | #if CONFIG_EXT_TILE |
| 5216 | // The top bit in the tile size field indicates tile copy mode, so we |
| 5217 | // have 1 less bit to code the tile size |
| 5218 | const int tsb = choose_size_bytes(max_tile_size, 1); |
| 5219 | const int tcsb = choose_size_bytes(max_tile_col_size, 0); |
| 5220 | #else |
| 5221 | const int tsb = choose_size_bytes(max_tile_size, 0); |
| 5222 | const int tcsb = 4; // This is ignored |
| 5223 | (void)max_tile_col_size; |
| 5224 | #endif // CONFIG_EXT_TILE |
| 5225 | |
| 5226 | assert(tsb > 0); |
| 5227 | assert(tcsb > 0); |
| 5228 | |
| 5229 | *tile_size_bytes = tsb; |
| 5230 | *tile_col_size_bytes = tcsb; |
| 5231 | |
| 5232 | if (tsb == 4 && tcsb == 4) { |
| 5233 | return data_size; |
| 5234 | } else { |
| 5235 | uint32_t wpos = 0; |
| 5236 | uint32_t rpos = 0; |
| 5237 | |
| 5238 | #if CONFIG_EXT_TILE |
| 5239 | int tile_row; |
| 5240 | int tile_col; |
| 5241 | |
| 5242 | for (tile_col = 0; tile_col < cm->tile_cols; tile_col++) { |
| 5243 | // All but the last column has a column header |
| 5244 | if (tile_col < cm->tile_cols - 1) { |
| 5245 | uint32_t tile_col_size = mem_get_le32(dst + rpos); |
| 5246 | rpos += 4; |
| 5247 | |
| 5248 | // Adjust the tile column size by the number of bytes removed |
| 5249 | // from the tile size fields. |
| 5250 | tile_col_size -= (4 - tsb) * cm->tile_rows; |
| 5251 | |
| 5252 | mem_put_varsize(dst + wpos, tcsb, tile_col_size); |
| 5253 | wpos += tcsb; |
| 5254 | } |
| 5255 | |
| 5256 | for (tile_row = 0; tile_row < cm->tile_rows; tile_row++) { |
| 5257 | // All, including the last row has a header |
| 5258 | uint32_t tile_header = mem_get_le32(dst + rpos); |
| 5259 | rpos += 4; |
| 5260 | |
| 5261 | // If this is a copy tile, we need to shift the MSB to the |
| 5262 | // top bit of the new width, and there is no data to copy. |
| 5263 | if (tile_header >> 31 != 0) { |
| 5264 | if (tsb < 4) tile_header >>= 32 - 8 * tsb; |
| 5265 | mem_put_varsize(dst + wpos, tsb, tile_header); |
| 5266 | wpos += tsb; |
| 5267 | } else { |
| 5268 | mem_put_varsize(dst + wpos, tsb, tile_header); |
| 5269 | wpos += tsb; |
| 5270 | |
| 5271 | memmove(dst + wpos, dst + rpos, tile_header); |
| 5272 | rpos += tile_header; |
| 5273 | wpos += tile_header; |
| 5274 | } |
| 5275 | } |
| 5276 | } |
| 5277 | #else |
| 5278 | const int n_tiles = cm->tile_cols * cm->tile_rows; |
| 5279 | int n; |
| 5280 | |
| 5281 | for (n = 0; n < n_tiles; n++) { |
| 5282 | int tile_size; |
| 5283 | |
Thomas Davies | a0de6d5 | 2017-01-20 14:45:25 +0000 | [diff] [blame] | 5284 | if (n == n_tiles - 1) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5285 | tile_size = data_size - rpos; |
| 5286 | } else { |
| 5287 | tile_size = mem_get_le32(dst + rpos); |
| 5288 | rpos += 4; |
| 5289 | mem_put_varsize(dst + wpos, tsb, tile_size); |
| 5290 | wpos += tsb; |
| 5291 | } |
| 5292 | |
| 5293 | memmove(dst + wpos, dst + rpos, tile_size); |
| 5294 | |
| 5295 | rpos += tile_size; |
| 5296 | wpos += tile_size; |
| 5297 | } |
| 5298 | #endif // CONFIG_EXT_TILE |
| 5299 | |
| 5300 | assert(rpos > wpos); |
| 5301 | assert(rpos == data_size); |
| 5302 | |
| 5303 | return wpos; |
| 5304 | } |
| 5305 | } |
| 5306 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5307 | void av1_pack_bitstream(AV1_COMP *const cpi, uint8_t *dst, size_t *size) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5308 | uint8_t *data = dst; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5309 | #if !CONFIG_TILE_GROUPS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5310 | uint32_t compressed_header_size; |
| 5311 | uint32_t uncompressed_header_size; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5312 | struct aom_write_bit_buffer saved_wb; |
| 5313 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5314 | uint32_t data_size; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5315 | struct aom_write_bit_buffer wb = { data, 0 }; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5316 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5317 | unsigned int max_tile_size; |
| 5318 | unsigned int max_tile_col_size; |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5319 | |
Angie Chiang | b11aedf | 2017-03-10 17:31:46 -0800 | [diff] [blame] | 5320 | #if CONFIG_BITSTREAM_DEBUG |
| 5321 | bitstream_queue_reset_write(); |
| 5322 | #endif |
| 5323 | |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5324 | #if !CONFIG_TILE_GROUPS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5325 | int tile_size_bytes; |
| 5326 | int tile_col_size_bytes; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5327 | AV1_COMMON *const cm = &cpi->common; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5328 | const int have_tiles = cm->tile_cols * cm->tile_rows > 1; |
| 5329 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5330 | // Write the uncompressed header |
| 5331 | write_uncompressed_header(cpi, &wb); |
| 5332 | |
| 5333 | #if CONFIG_EXT_REFS |
| 5334 | if (cm->show_existing_frame) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5335 | *size = aom_wb_bytes_written(&wb); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5336 | return; |
| 5337 | } |
| 5338 | #endif // CONFIG_EXT_REFS |
| 5339 | |
| 5340 | // We do not know these in advance. Output placeholder bit. |
| 5341 | saved_wb = wb; |
| 5342 | // Write tile size magnitudes |
| 5343 | if (have_tiles) { |
| 5344 | // Note that the last item in the uncompressed header is the data |
| 5345 | // describing tile configuration. |
| 5346 | #if CONFIG_EXT_TILE |
| 5347 | // Number of bytes in tile column size - 1 |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5348 | aom_wb_write_literal(&wb, 0, 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5349 | #endif // CONFIG_EXT_TILE |
| 5350 | // Number of bytes in tile size - 1 |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5351 | aom_wb_write_literal(&wb, 0, 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5352 | } |
| 5353 | // Size of compressed header |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5354 | aom_wb_write_literal(&wb, 0, 16); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5355 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5356 | uncompressed_header_size = (uint32_t)aom_wb_bytes_written(&wb); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5357 | data += uncompressed_header_size; |
| 5358 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5359 | aom_clear_system_state(); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5360 | |
| 5361 | // Write the compressed header |
| 5362 | compressed_header_size = write_compressed_header(cpi, data); |
| 5363 | data += compressed_header_size; |
| 5364 | |
| 5365 | // Write the encoded tile data |
| 5366 | data_size = write_tiles(cpi, data, &max_tile_size, &max_tile_col_size); |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5367 | #else |
| 5368 | data_size = write_tiles(cpi, &wb, &max_tile_size, &max_tile_col_size); |
| 5369 | #endif |
| 5370 | #if !CONFIG_TILE_GROUPS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5371 | if (have_tiles) { |
| 5372 | data_size = |
| 5373 | remux_tiles(cm, data, data_size, max_tile_size, max_tile_col_size, |
| 5374 | &tile_size_bytes, &tile_col_size_bytes); |
| 5375 | } |
| 5376 | |
| 5377 | data += data_size; |
| 5378 | |
| 5379 | // Now fill in the gaps in the uncompressed header. |
| 5380 | if (have_tiles) { |
| 5381 | #if CONFIG_EXT_TILE |
| 5382 | assert(tile_col_size_bytes >= 1 && tile_col_size_bytes <= 4); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5383 | aom_wb_write_literal(&saved_wb, tile_col_size_bytes - 1, 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5384 | #endif // CONFIG_EXT_TILE |
| 5385 | assert(tile_size_bytes >= 1 && tile_size_bytes <= 4); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5386 | aom_wb_write_literal(&saved_wb, tile_size_bytes - 1, 2); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5387 | } |
| 5388 | // TODO(jbb): Figure out what to do if compressed_header_size > 16 bits. |
| 5389 | assert(compressed_header_size <= 0xffff); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 5390 | aom_wb_write_literal(&saved_wb, compressed_header_size, 16); |
Thomas Davies | 80188d1 | 2016-10-26 16:08:35 -0700 | [diff] [blame] | 5391 | #else |
| 5392 | data += data_size; |
| 5393 | #endif |
Alex Converse | b0bbd60 | 2016-10-21 14:15:06 -0700 | [diff] [blame] | 5394 | #if CONFIG_ANS && ANS_REVERSE |
| 5395 | // Avoid aliasing the superframe index |
| 5396 | *data++ = 0; |
| 5397 | #endif |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 5398 | *size = data - dst; |
| 5399 | } |