/src/libvpx/vp9/encoder/vp9_bitstream.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
3 | | * |
4 | | * Use of this source code is governed by a BSD-style license |
5 | | * that can be found in the LICENSE file in the root of the source |
6 | | * tree. An additional intellectual property rights grant can be found |
7 | | * in the file PATENTS. All contributing project authors may |
8 | | * be found in the AUTHORS file in the root of the source tree. |
9 | | */ |
10 | | |
11 | | #include <assert.h> |
12 | | #include <stdint.h> |
13 | | #include <stdio.h> |
14 | | #include <limits.h> |
15 | | |
16 | | #include "vpx/vpx_encoder.h" |
17 | | #include "vpx_dsp/bitwriter_buffer.h" |
18 | | #include "vpx_dsp/vpx_dsp_common.h" |
19 | | #include "vpx_mem/vpx_mem.h" |
20 | | #include "vpx_ports/mem_ops.h" |
21 | | #include "vpx_ports/system_state.h" |
22 | | #if CONFIG_BITSTREAM_DEBUG |
23 | | #include "vpx_util/vpx_debug_util.h" |
24 | | #endif // CONFIG_BITSTREAM_DEBUG |
25 | | |
26 | | #include "vp9/common/vp9_entropy.h" |
27 | | #include "vp9/common/vp9_entropymode.h" |
28 | | #include "vp9/common/vp9_entropymv.h" |
29 | | #include "vp9/common/vp9_mvref_common.h" |
30 | | #include "vp9/common/vp9_pred_common.h" |
31 | | #include "vp9/common/vp9_seg_common.h" |
32 | | #include "vp9/common/vp9_tile_common.h" |
33 | | |
34 | | #include "vp9/encoder/vp9_cost.h" |
35 | | #include "vp9/encoder/vp9_bitstream.h" |
36 | | #include "vp9/encoder/vp9_encodemv.h" |
37 | | #include "vp9/encoder/vp9_mcomp.h" |
38 | | #include "vp9/encoder/vp9_segmentation.h" |
39 | | #include "vp9/encoder/vp9_subexp.h" |
40 | | #include "vp9/encoder/vp9_tokenize.h" |
41 | | |
42 | | static const struct vp9_token intra_mode_encodings[INTRA_MODES] = { |
43 | | { 0, 1 }, { 6, 3 }, { 28, 5 }, { 30, 5 }, { 58, 6 }, |
44 | | { 59, 6 }, { 126, 7 }, { 127, 7 }, { 62, 6 }, { 2, 2 } |
45 | | }; |
46 | | static const struct vp9_token |
47 | | switchable_interp_encodings[SWITCHABLE_FILTERS] = { { 0, 1 }, |
48 | | { 2, 2 }, |
49 | | { 3, 2 } }; |
50 | | static const struct vp9_token partition_encodings[PARTITION_TYPES] = { |
51 | | { 0, 1 }, { 2, 2 }, { 6, 3 }, { 7, 3 } |
52 | | }; |
53 | | static const struct vp9_token inter_mode_encodings[INTER_MODES] = { |
54 | | { 2, 2 }, { 6, 3 }, { 0, 1 }, { 7, 3 } |
55 | | }; |
56 | | |
57 | | static void write_intra_mode(vpx_writer *w, PREDICTION_MODE mode, |
58 | 4.85M | const vpx_prob *probs) { |
59 | 4.85M | vp9_write_token(w, vp9_intra_mode_tree, probs, &intra_mode_encodings[mode]); |
60 | 4.85M | } |
61 | | |
62 | | static void write_inter_mode(vpx_writer *w, PREDICTION_MODE mode, |
63 | 1.31M | const vpx_prob *probs) { |
64 | 1.31M | assert(is_inter_mode(mode)); |
65 | 1.31M | vp9_write_token(w, vp9_inter_mode_tree, probs, |
66 | 1.31M | &inter_mode_encodings[INTER_OFFSET(mode)]); |
67 | 1.31M | } |
68 | | |
69 | | static void encode_unsigned_max(struct vpx_write_bit_buffer *wb, int data, |
70 | 0 | int max) { |
71 | 0 | vpx_wb_write_literal(wb, data, get_unsigned_bits(max)); |
72 | 0 | } |
73 | | |
74 | | static void prob_diff_update(const vpx_tree_index *tree, |
75 | | vpx_prob probs[/*n - 1*/], |
76 | | const unsigned int counts[/*n - 1*/], int n, |
77 | 976k | vpx_writer *w) { |
78 | 976k | int i; |
79 | 976k | unsigned int branch_ct[32][2]; |
80 | | |
81 | | // Assuming max number of probabilities <= 32 |
82 | 976k | assert(n <= 32); |
83 | | |
84 | 976k | vp9_tree_probs_from_distribution(tree, branch_ct, counts); |
85 | 4.69M | for (i = 0; i < n - 1; ++i) |
86 | 3.71M | vp9_cond_prob_diff_update(w, &probs[i], branch_ct[i]); |
87 | 976k | } |
88 | | |
89 | | static void write_selected_tx_size(const VP9_COMMON *cm, |
90 | 378k | const MACROBLOCKD *const xd, vpx_writer *w) { |
91 | 378k | TX_SIZE tx_size = xd->mi[0]->tx_size; |
92 | 378k | BLOCK_SIZE bsize = xd->mi[0]->sb_type; |
93 | 378k | const TX_SIZE max_tx_size = max_txsize_lookup[bsize]; |
94 | 378k | const vpx_prob *const tx_probs = |
95 | 378k | get_tx_probs(max_tx_size, get_tx_size_context(xd), &cm->fc->tx_probs); |
96 | 378k | vpx_write(w, tx_size != TX_4X4, tx_probs[0]); |
97 | 378k | if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) { |
98 | 48.5k | vpx_write(w, tx_size != TX_8X8, tx_probs[1]); |
99 | 48.5k | if (tx_size != TX_8X8 && max_tx_size >= TX_32X32) |
100 | 18.3k | vpx_write(w, tx_size != TX_16X16, tx_probs[2]); |
101 | 48.5k | } |
102 | 378k | } |
103 | | |
104 | | static int write_skip(const VP9_COMMON *cm, const MACROBLOCKD *const xd, |
105 | 1.89M | int segment_id, const MODE_INFO *mi, vpx_writer *w) { |
106 | 1.89M | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) { |
107 | 0 | return 1; |
108 | 1.89M | } else { |
109 | 1.89M | const int skip = mi->skip; |
110 | 1.89M | vpx_write(w, skip, vp9_get_skip_prob(cm, xd)); |
111 | 1.89M | return skip; |
112 | 1.89M | } |
113 | 1.89M | } |
114 | | |
115 | | static void update_skip_probs(VP9_COMMON *cm, vpx_writer *w, |
116 | 42.2k | FRAME_COUNTS *counts) { |
117 | 42.2k | int k; |
118 | | |
119 | 169k | for (k = 0; k < SKIP_CONTEXTS; ++k) |
120 | 126k | vp9_cond_prob_diff_update(w, &cm->fc->skip_probs[k], counts->skip[k]); |
121 | 42.2k | } |
122 | | |
123 | | static void update_switchable_interp_probs(VP9_COMMON *cm, vpx_writer *w, |
124 | 11.0k | FRAME_COUNTS *counts) { |
125 | 11.0k | int j; |
126 | 55.2k | for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) |
127 | 44.2k | prob_diff_update(vp9_switchable_interp_tree, |
128 | 44.2k | cm->fc->switchable_interp_prob[j], |
129 | 44.2k | counts->switchable_interp[j], SWITCHABLE_FILTERS, w); |
130 | 11.0k | } |
131 | | |
132 | | static void pack_mb_tokens(vpx_writer *w, TOKENEXTRA **tp, |
133 | | const TOKENEXTRA *const stop, |
134 | 1.89M | vpx_bit_depth_t bit_depth) { |
135 | 1.89M | const TOKENEXTRA *p; |
136 | 1.89M | const vp9_extra_bit *const extra_bits = |
137 | 1.89M | #if CONFIG_VP9_HIGHBITDEPTH |
138 | 1.89M | (bit_depth == VPX_BITS_12) ? vp9_extra_bits_high12 |
139 | 1.89M | : (bit_depth == VPX_BITS_10) ? vp9_extra_bits_high10 |
140 | 1.89M | : vp9_extra_bits; |
141 | | #else |
142 | | vp9_extra_bits; |
143 | | (void)bit_depth; |
144 | | #endif // CONFIG_VP9_HIGHBITDEPTH |
145 | | |
146 | 105M | for (p = *tp; p < stop && p->token != EOSB_TOKEN; ++p) { |
147 | 103M | if (p->token == EOB_TOKEN) { |
148 | 4.68M | vpx_write(w, 0, p->context_tree[0]); |
149 | 4.68M | continue; |
150 | 4.68M | } |
151 | 99.2M | vpx_write(w, 1, p->context_tree[0]); |
152 | 125M | while (p->token == ZERO_TOKEN) { |
153 | 25.8M | vpx_write(w, 0, p->context_tree[1]); |
154 | 25.8M | ++p; |
155 | 25.8M | if (p == stop || p->token == EOSB_TOKEN) { |
156 | 0 | *tp = (TOKENEXTRA *)(uintptr_t)p + (p->token == EOSB_TOKEN); |
157 | 0 | return; |
158 | 0 | } |
159 | 25.8M | } |
160 | | |
161 | 99.2M | { |
162 | 99.2M | const int t = p->token; |
163 | 99.2M | const vpx_prob *const context_tree = p->context_tree; |
164 | 99.2M | assert(t != ZERO_TOKEN); |
165 | 99.2M | assert(t != EOB_TOKEN); |
166 | 99.2M | assert(t != EOSB_TOKEN); |
167 | 99.2M | vpx_write(w, 1, context_tree[1]); |
168 | 99.2M | if (t == ONE_TOKEN) { |
169 | 17.5M | vpx_write(w, 0, context_tree[2]); |
170 | 17.5M | vpx_write_bit(w, p->extra & 1); |
171 | 81.6M | } else { // t >= TWO_TOKEN && t < EOB_TOKEN |
172 | 81.6M | const struct vp9_token *const a = &vp9_coef_encodings[t]; |
173 | 81.6M | int v = a->value; |
174 | 81.6M | int n = a->len; |
175 | 81.6M | const int e = p->extra; |
176 | 81.6M | vpx_write(w, 1, context_tree[2]); |
177 | 81.6M | vp9_write_tree(w, vp9_coef_con_tree, |
178 | 81.6M | vp9_pareto8_full[context_tree[PIVOT_NODE] - 1], v, |
179 | 81.6M | n - UNCONSTRAINED_NODES, 0); |
180 | 81.6M | if (t >= CATEGORY1_TOKEN) { |
181 | 59.3M | const vp9_extra_bit *const b = &extra_bits[t]; |
182 | 59.3M | const unsigned char *pb = b->prob; |
183 | 59.3M | v = e >> 1; |
184 | 59.3M | n = b->len; // number of bits in v, assumed nonzero |
185 | 259M | do { |
186 | 259M | const int bb = (v >> --n) & 1; |
187 | 259M | vpx_write(w, bb, *pb++); |
188 | 259M | } while (n); |
189 | 59.3M | } |
190 | 81.6M | vpx_write_bit(w, e & 1); |
191 | 81.6M | } |
192 | 99.2M | } |
193 | 99.2M | } |
194 | 1.89M | *tp = (TOKENEXTRA *)(uintptr_t)p + (p->token == EOSB_TOKEN); |
195 | 1.89M | } |
196 | | |
197 | | static void write_segment_id(vpx_writer *w, const struct segmentation *seg, |
198 | 0 | int segment_id) { |
199 | 0 | if (seg->enabled && seg->update_map) |
200 | 0 | vp9_write_tree(w, vp9_segment_tree, seg->tree_probs, segment_id, 3, 0); |
201 | 0 | } |
202 | | |
203 | | // This function encodes the reference frame |
204 | | static void write_ref_frames(const VP9_COMMON *cm, const MACROBLOCKD *const xd, |
205 | 582k | vpx_writer *w) { |
206 | 582k | const MODE_INFO *const mi = xd->mi[0]; |
207 | 582k | const int is_compound = has_second_ref(mi); |
208 | 582k | const int segment_id = mi->segment_id; |
209 | | |
210 | | // If segment level coding of this signal is disabled... |
211 | | // or the segment allows multiple reference frame options |
212 | 582k | if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) { |
213 | 0 | assert(!is_compound); |
214 | 0 | assert(mi->ref_frame[0] == |
215 | 0 | get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME)); |
216 | 582k | } else { |
217 | | // does the feature use compound prediction or not |
218 | | // (if not specified at the frame/segment level) |
219 | 582k | if (cm->reference_mode == REFERENCE_MODE_SELECT) { |
220 | 0 | vpx_write(w, is_compound, vp9_get_reference_mode_prob(cm, xd)); |
221 | 582k | } else { |
222 | 582k | assert((!is_compound) == (cm->reference_mode == SINGLE_REFERENCE)); |
223 | 582k | } |
224 | | |
225 | 582k | if (is_compound) { |
226 | 0 | const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref]; |
227 | 0 | vpx_write(w, mi->ref_frame[!idx] == cm->comp_var_ref[1], |
228 | 0 | vp9_get_pred_prob_comp_ref_p(cm, xd)); |
229 | 582k | } else { |
230 | 582k | const int bit0 = mi->ref_frame[0] != LAST_FRAME; |
231 | 582k | vpx_write(w, bit0, vp9_get_pred_prob_single_ref_p1(cm, xd)); |
232 | 582k | if (bit0) { |
233 | 212k | const int bit1 = mi->ref_frame[0] != GOLDEN_FRAME; |
234 | 212k | vpx_write(w, bit1, vp9_get_pred_prob_single_ref_p2(cm, xd)); |
235 | 212k | } |
236 | 582k | } |
237 | 582k | } |
238 | 582k | } |
239 | | |
240 | | static void pack_inter_mode_mvs(VP9_COMP *cpi, const MACROBLOCKD *const xd, |
241 | | const MB_MODE_INFO_EXT *const mbmi_ext, |
242 | | vpx_writer *w, |
243 | | unsigned int *const max_mv_magnitude, |
244 | 1.39M | int interp_filter_selected[][SWITCHABLE]) { |
245 | 1.39M | VP9_COMMON *const cm = &cpi->common; |
246 | 1.39M | const nmv_context *nmvc = &cm->fc->nmvc; |
247 | 1.39M | const struct segmentation *const seg = &cm->seg; |
248 | 1.39M | const MODE_INFO *const mi = xd->mi[0]; |
249 | 1.39M | const PREDICTION_MODE mode = mi->mode; |
250 | 1.39M | const int segment_id = mi->segment_id; |
251 | 1.39M | const BLOCK_SIZE bsize = mi->sb_type; |
252 | 1.39M | const int allow_hp = cm->allow_high_precision_mv; |
253 | 1.39M | const int is_inter = is_inter_block(mi); |
254 | 1.39M | const int is_compound = has_second_ref(mi); |
255 | 1.39M | int skip, ref; |
256 | | |
257 | 1.39M | if (seg->update_map) { |
258 | 0 | if (seg->temporal_update) { |
259 | 0 | const int pred_flag = mi->seg_id_predicted; |
260 | 0 | vpx_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd); |
261 | 0 | vpx_write(w, pred_flag, pred_prob); |
262 | 0 | if (!pred_flag) write_segment_id(w, seg, segment_id); |
263 | 0 | } else { |
264 | 0 | write_segment_id(w, seg, segment_id); |
265 | 0 | } |
266 | 0 | } |
267 | | |
268 | 1.39M | skip = write_skip(cm, xd, segment_id, mi, w); |
269 | | |
270 | 1.39M | if (!segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME)) |
271 | 1.39M | vpx_write(w, is_inter, vp9_get_intra_inter_prob(cm, xd)); |
272 | | |
273 | 1.39M | if (bsize >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT && |
274 | 1.39M | !(is_inter && skip)) { |
275 | 243k | write_selected_tx_size(cm, xd, w); |
276 | 243k | } |
277 | | |
278 | 1.39M | if (!is_inter) { |
279 | 813k | if (bsize >= BLOCK_8X8) { |
280 | 276k | write_intra_mode(w, mode, cm->fc->y_mode_prob[size_group_lookup[bsize]]); |
281 | 536k | } else { |
282 | 536k | int idx, idy; |
283 | 536k | const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize]; |
284 | 536k | const int num_4x4_h = num_4x4_blocks_high_lookup[bsize]; |
285 | 1.55M | for (idy = 0; idy < 2; idy += num_4x4_h) { |
286 | 2.86M | for (idx = 0; idx < 2; idx += num_4x4_w) { |
287 | 1.84M | const PREDICTION_MODE b_mode = mi->bmi[idy * 2 + idx].as_mode; |
288 | 1.84M | write_intra_mode(w, b_mode, cm->fc->y_mode_prob[0]); |
289 | 1.84M | } |
290 | 1.01M | } |
291 | 536k | } |
292 | 813k | write_intra_mode(w, mi->uv_mode, cm->fc->uv_mode_prob[mode]); |
293 | 813k | } else { |
294 | 582k | const int mode_ctx = mbmi_ext->mode_context[mi->ref_frame[0]]; |
295 | 582k | const vpx_prob *const inter_probs = cm->fc->inter_mode_probs[mode_ctx]; |
296 | 582k | write_ref_frames(cm, xd, w); |
297 | | |
298 | | // If segment skip is not enabled code the mode. |
299 | 582k | if (!segfeature_active(seg, segment_id, SEG_LVL_SKIP)) { |
300 | 582k | if (bsize >= BLOCK_8X8) { |
301 | 302k | write_inter_mode(w, mode, inter_probs); |
302 | 302k | } |
303 | 582k | } |
304 | | |
305 | 582k | if (cm->interp_filter == SWITCHABLE) { |
306 | 267k | const int ctx = get_pred_context_switchable_interp(xd); |
307 | 267k | vp9_write_token(w, vp9_switchable_interp_tree, |
308 | 267k | cm->fc->switchable_interp_prob[ctx], |
309 | 267k | &switchable_interp_encodings[mi->interp_filter]); |
310 | 267k | ++interp_filter_selected[0][mi->interp_filter]; |
311 | 315k | } else { |
312 | 315k | assert(mi->interp_filter == cm->interp_filter); |
313 | 315k | } |
314 | | |
315 | 582k | if (bsize < BLOCK_8X8) { |
316 | 280k | const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize]; |
317 | 280k | const int num_4x4_h = num_4x4_blocks_high_lookup[bsize]; |
318 | 280k | int idx, idy; |
319 | 828k | for (idy = 0; idy < 2; idy += num_4x4_h) { |
320 | 1.56M | for (idx = 0; idx < 2; idx += num_4x4_w) { |
321 | 1.01M | const int j = idy * 2 + idx; |
322 | 1.01M | const PREDICTION_MODE b_mode = mi->bmi[j].as_mode; |
323 | 1.01M | write_inter_mode(w, b_mode, inter_probs); |
324 | 1.01M | if (b_mode == NEWMV) { |
325 | 350k | for (ref = 0; ref < 1 + is_compound; ++ref) |
326 | 175k | vp9_encode_mv(cpi, w, &mi->bmi[j].as_mv[ref].as_mv, |
327 | 175k | &mbmi_ext->ref_mvs[mi->ref_frame[ref]][0].as_mv, |
328 | 175k | nmvc, allow_hp, max_mv_magnitude); |
329 | 175k | } |
330 | 1.01M | } |
331 | 548k | } |
332 | 302k | } else { |
333 | 302k | if (mode == NEWMV) { |
334 | 151k | for (ref = 0; ref < 1 + is_compound; ++ref) |
335 | 75.5k | vp9_encode_mv(cpi, w, &mi->mv[ref].as_mv, |
336 | 75.5k | &mbmi_ext->ref_mvs[mi->ref_frame[ref]][0].as_mv, nmvc, |
337 | 75.5k | allow_hp, max_mv_magnitude); |
338 | 75.5k | } |
339 | 302k | } |
340 | 582k | } |
341 | 1.39M | } |
342 | | |
343 | | static void write_mb_modes_kf(const VP9_COMMON *cm, const MACROBLOCKD *xd, |
344 | 503k | vpx_writer *w) { |
345 | 503k | const struct segmentation *const seg = &cm->seg; |
346 | 503k | const MODE_INFO *const mi = xd->mi[0]; |
347 | 503k | const MODE_INFO *const above_mi = xd->above_mi; |
348 | 503k | const MODE_INFO *const left_mi = xd->left_mi; |
349 | 503k | const BLOCK_SIZE bsize = mi->sb_type; |
350 | | |
351 | 503k | if (seg->update_map) write_segment_id(w, seg, mi->segment_id); |
352 | | |
353 | 503k | write_skip(cm, xd, mi->segment_id, mi, w); |
354 | | |
355 | 503k | if (bsize >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT) |
356 | 135k | write_selected_tx_size(cm, xd, w); |
357 | | |
358 | 503k | if (bsize >= BLOCK_8X8) { |
359 | 160k | write_intra_mode(w, mi->mode, get_y_mode_probs(mi, above_mi, left_mi, 0)); |
360 | 343k | } else { |
361 | 343k | const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize]; |
362 | 343k | const int num_4x4_h = num_4x4_blocks_high_lookup[bsize]; |
363 | 343k | int idx, idy; |
364 | | |
365 | 1.00M | for (idy = 0; idy < 2; idy += num_4x4_h) { |
366 | 1.91M | for (idx = 0; idx < 2; idx += num_4x4_w) { |
367 | 1.25M | const int block = idy * 2 + idx; |
368 | 1.25M | write_intra_mode(w, mi->bmi[block].as_mode, |
369 | 1.25M | get_y_mode_probs(mi, above_mi, left_mi, block)); |
370 | 1.25M | } |
371 | 661k | } |
372 | 343k | } |
373 | | |
374 | 503k | write_intra_mode(w, mi->uv_mode, vp9_kf_uv_mode_prob[mi->mode]); |
375 | 503k | } |
376 | | |
377 | | static void write_modes_b(VP9_COMP *cpi, MACROBLOCKD *const xd, |
378 | | const TileInfo *const tile, vpx_writer *w, |
379 | | TOKENEXTRA **tok, const TOKENEXTRA *const tok_end, |
380 | | int mi_row, int mi_col, |
381 | | unsigned int *const max_mv_magnitude, |
382 | 1.89M | int interp_filter_selected[][SWITCHABLE]) { |
383 | 1.89M | const VP9_COMMON *const cm = &cpi->common; |
384 | 1.89M | const MB_MODE_INFO_EXT *const mbmi_ext = |
385 | 1.89M | cpi->td.mb.mbmi_ext_base + (mi_row * cm->mi_cols + mi_col); |
386 | 1.89M | MODE_INFO *m; |
387 | | |
388 | 1.89M | xd->mi = cm->mi_grid_visible + (mi_row * cm->mi_stride + mi_col); |
389 | 1.89M | m = xd->mi[0]; |
390 | | |
391 | 1.89M | set_mi_row_col(xd, tile, mi_row, num_8x8_blocks_high_lookup[m->sb_type], |
392 | 1.89M | mi_col, num_8x8_blocks_wide_lookup[m->sb_type], cm->mi_rows, |
393 | 1.89M | cm->mi_cols); |
394 | 1.89M | if (frame_is_intra_only(cm)) { |
395 | 503k | write_mb_modes_kf(cm, xd, w); |
396 | 1.39M | } else { |
397 | 1.39M | pack_inter_mode_mvs(cpi, xd, mbmi_ext, w, max_mv_magnitude, |
398 | 1.39M | interp_filter_selected); |
399 | 1.39M | } |
400 | | |
401 | 1.89M | assert(*tok < tok_end); |
402 | 1.89M | pack_mb_tokens(w, tok, tok_end, cm->bit_depth); |
403 | 1.89M | } |
404 | | |
405 | | static void write_partition(const VP9_COMMON *const cm, |
406 | | const MACROBLOCKD *const xd, int hbs, int mi_row, |
407 | | int mi_col, PARTITION_TYPE p, BLOCK_SIZE bsize, |
408 | 2.52M | vpx_writer *w) { |
409 | 2.52M | const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize); |
410 | 2.52M | const vpx_prob *const probs = xd->partition_probs[ctx]; |
411 | 2.52M | const int has_rows = (mi_row + hbs) < cm->mi_rows; |
412 | 2.52M | const int has_cols = (mi_col + hbs) < cm->mi_cols; |
413 | | |
414 | 2.52M | if (has_rows && has_cols) { |
415 | 2.40M | vp9_write_token(w, vp9_partition_tree, probs, &partition_encodings[p]); |
416 | 2.40M | } else if (!has_rows && has_cols) { |
417 | 68.4k | assert(p == PARTITION_SPLIT || p == PARTITION_HORZ); |
418 | 68.4k | vpx_write(w, p == PARTITION_SPLIT, probs[1]); |
419 | 68.4k | } else if (has_rows && !has_cols) { |
420 | 28.6k | assert(p == PARTITION_SPLIT || p == PARTITION_VERT); |
421 | 28.6k | vpx_write(w, p == PARTITION_SPLIT, probs[2]); |
422 | 28.6k | } else { |
423 | 20.6k | assert(p == PARTITION_SPLIT); |
424 | 20.6k | } |
425 | 2.52M | } |
426 | | |
427 | | static void write_modes_sb(VP9_COMP *cpi, MACROBLOCKD *const xd, |
428 | | const TileInfo *const tile, vpx_writer *w, |
429 | | TOKENEXTRA **tok, const TOKENEXTRA *const tok_end, |
430 | | int mi_row, int mi_col, BLOCK_SIZE bsize, |
431 | | unsigned int *const max_mv_magnitude, |
432 | 2.75M | int interp_filter_selected[][SWITCHABLE]) { |
433 | 2.75M | const VP9_COMMON *const cm = &cpi->common; |
434 | 2.75M | const int bsl = b_width_log2_lookup[bsize]; |
435 | 2.75M | const int bs = (1 << bsl) / 4; |
436 | 2.75M | PARTITION_TYPE partition; |
437 | 2.75M | BLOCK_SIZE subsize; |
438 | 2.75M | const MODE_INFO *m = NULL; |
439 | | |
440 | 2.75M | if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return; |
441 | | |
442 | 2.52M | m = cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col]; |
443 | | |
444 | 2.52M | partition = partition_lookup[bsl][m->sb_type]; |
445 | 2.52M | write_partition(cm, xd, bs, mi_row, mi_col, partition, bsize, w); |
446 | 2.52M | subsize = get_subsize(bsize, partition); |
447 | 2.52M | if (subsize < BLOCK_8X8) { |
448 | 1.16M | write_modes_b(cpi, xd, tile, w, tok, tok_end, mi_row, mi_col, |
449 | 1.16M | max_mv_magnitude, interp_filter_selected); |
450 | 1.36M | } else { |
451 | 1.36M | switch (partition) { |
452 | 641k | case PARTITION_NONE: |
453 | 641k | write_modes_b(cpi, xd, tile, w, tok, tok_end, mi_row, mi_col, |
454 | 641k | max_mv_magnitude, interp_filter_selected); |
455 | 641k | break; |
456 | 35.6k | case PARTITION_HORZ: |
457 | 35.6k | write_modes_b(cpi, xd, tile, w, tok, tok_end, mi_row, mi_col, |
458 | 35.6k | max_mv_magnitude, interp_filter_selected); |
459 | 35.6k | if (mi_row + bs < cm->mi_rows) |
460 | 25.3k | write_modes_b(cpi, xd, tile, w, tok, tok_end, mi_row + bs, mi_col, |
461 | 25.3k | max_mv_magnitude, interp_filter_selected); |
462 | 35.6k | break; |
463 | 20.7k | case PARTITION_VERT: |
464 | 20.7k | write_modes_b(cpi, xd, tile, w, tok, tok_end, mi_row, mi_col, |
465 | 20.7k | max_mv_magnitude, interp_filter_selected); |
466 | 20.7k | if (mi_col + bs < cm->mi_cols) |
467 | 15.1k | write_modes_b(cpi, xd, tile, w, tok, tok_end, mi_row, mi_col + bs, |
468 | 15.1k | max_mv_magnitude, interp_filter_selected); |
469 | 20.7k | break; |
470 | 668k | default: |
471 | 668k | assert(partition == PARTITION_SPLIT); |
472 | 668k | write_modes_sb(cpi, xd, tile, w, tok, tok_end, mi_row, mi_col, subsize, |
473 | 668k | max_mv_magnitude, interp_filter_selected); |
474 | 668k | write_modes_sb(cpi, xd, tile, w, tok, tok_end, mi_row, mi_col + bs, |
475 | 668k | subsize, max_mv_magnitude, interp_filter_selected); |
476 | 668k | write_modes_sb(cpi, xd, tile, w, tok, tok_end, mi_row + bs, mi_col, |
477 | 668k | subsize, max_mv_magnitude, interp_filter_selected); |
478 | 668k | write_modes_sb(cpi, xd, tile, w, tok, tok_end, mi_row + bs, mi_col + bs, |
479 | 668k | subsize, max_mv_magnitude, interp_filter_selected); |
480 | 668k | break; |
481 | 1.36M | } |
482 | 1.36M | } |
483 | | |
484 | | // update partition context |
485 | 2.52M | if (bsize >= BLOCK_8X8 && |
486 | 2.52M | (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) |
487 | 1.85M | update_partition_context(xd, mi_row, mi_col, subsize, bsize); |
488 | 2.52M | } |
489 | | |
490 | | static void write_modes(VP9_COMP *cpi, MACROBLOCKD *const xd, |
491 | | const TileInfo *const tile, vpx_writer *w, int tile_row, |
492 | | int tile_col, unsigned int *const max_mv_magnitude, |
493 | 44.7k | int interp_filter_selected[][SWITCHABLE]) { |
494 | 44.7k | const VP9_COMMON *const cm = &cpi->common; |
495 | 44.7k | int mi_row, mi_col, tile_sb_row; |
496 | 44.7k | TOKENEXTRA *tok = NULL; |
497 | 44.7k | TOKENEXTRA *tok_end = NULL; |
498 | | |
499 | 44.7k | set_partition_probs(cm, xd); |
500 | | |
501 | 100k | for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end; |
502 | 55.4k | mi_row += MI_BLOCK_SIZE) { |
503 | 55.4k | tile_sb_row = mi_cols_aligned_to_sb(mi_row - tile->mi_row_start) >> |
504 | 55.4k | MI_BLOCK_SIZE_LOG2; |
505 | 55.4k | tok = cpi->tplist[tile_row][tile_col][tile_sb_row].start; |
506 | 55.4k | tok_end = tok + cpi->tplist[tile_row][tile_col][tile_sb_row].count; |
507 | | |
508 | 55.4k | vp9_zero(xd->left_seg_context); |
509 | 133k | for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end; |
510 | 78.3k | mi_col += MI_BLOCK_SIZE) |
511 | 78.3k | write_modes_sb(cpi, xd, tile, w, &tok, tok_end, mi_row, mi_col, |
512 | 78.3k | BLOCK_64X64, max_mv_magnitude, interp_filter_selected); |
513 | | |
514 | 55.4k | assert(tok == cpi->tplist[tile_row][tile_col][tile_sb_row].stop); |
515 | 55.4k | } |
516 | 44.7k | } |
517 | | |
518 | | static void build_tree_distribution(VP9_COMP *cpi, TX_SIZE tx_size, |
519 | | vp9_coeff_stats *coef_branch_ct, |
520 | 36.0k | vp9_coeff_probs_model *coef_probs) { |
521 | 36.0k | vp9_coeff_count *coef_counts = cpi->td.rd_counts.coef_counts[tx_size]; |
522 | 36.0k | unsigned int(*eob_branch_ct)[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS] = |
523 | 36.0k | cpi->common.counts.eob_branch[tx_size]; |
524 | 36.0k | int i, j, k, l, m; |
525 | | |
526 | 108k | for (i = 0; i < PLANE_TYPES; ++i) { |
527 | 216k | for (j = 0; j < REF_TYPES; ++j) { |
528 | 1.00M | for (k = 0; k < COEF_BANDS; ++k) { |
529 | 5.62M | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
530 | 4.75M | vp9_tree_probs_from_distribution(vp9_coef_tree, |
531 | 4.75M | coef_branch_ct[i][j][k][l], |
532 | 4.75M | coef_counts[i][j][k][l]); |
533 | 4.75M | coef_branch_ct[i][j][k][l][0][1] = |
534 | 4.75M | eob_branch_ct[i][j][k][l] - coef_branch_ct[i][j][k][l][0][0]; |
535 | 19.0M | for (m = 0; m < UNCONSTRAINED_NODES; ++m) |
536 | 14.2M | coef_probs[i][j][k][l][m] = |
537 | 14.2M | get_binary_prob(coef_branch_ct[i][j][k][l][m][0], |
538 | 14.2M | coef_branch_ct[i][j][k][l][m][1]); |
539 | 4.75M | } |
540 | 865k | } |
541 | 144k | } |
542 | 72.0k | } |
543 | 36.0k | } |
544 | | |
545 | | static void update_coef_probs_common(vpx_writer *const bc, VP9_COMP *cpi, |
546 | | TX_SIZE tx_size, |
547 | | vp9_coeff_stats *frame_branch_ct, |
548 | 36.0k | vp9_coeff_probs_model *new_coef_probs) { |
549 | 36.0k | vp9_coeff_probs_model *old_coef_probs = cpi->common.fc->coef_probs[tx_size]; |
550 | 36.0k | const vpx_prob upd = DIFF_UPDATE_PROB; |
551 | 36.0k | const int entropy_nodes_update = UNCONSTRAINED_NODES; |
552 | 36.0k | int i, j, k, l, t; |
553 | 36.0k | int stepsize = cpi->sf.coeff_prob_appx_step; |
554 | | |
555 | 36.0k | switch (cpi->sf.use_fast_coef_updates) { |
556 | 36.0k | case TWO_LOOP: { |
557 | | /* dry run to see if there is any update at all needed */ |
558 | 36.0k | int64_t savings = 0; |
559 | 36.0k | int update[2] = { 0, 0 }; |
560 | 108k | for (i = 0; i < PLANE_TYPES; ++i) { |
561 | 216k | for (j = 0; j < REF_TYPES; ++j) { |
562 | 1.00M | for (k = 0; k < COEF_BANDS; ++k) { |
563 | 5.62M | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
564 | 19.0M | for (t = 0; t < entropy_nodes_update; ++t) { |
565 | 14.2M | vpx_prob newp = new_coef_probs[i][j][k][l][t]; |
566 | 14.2M | const vpx_prob oldp = old_coef_probs[i][j][k][l][t]; |
567 | 14.2M | int64_t s; |
568 | 14.2M | int u = 0; |
569 | 14.2M | if (t == PIVOT_NODE) |
570 | 4.75M | s = vp9_prob_diff_update_savings_search_model( |
571 | 4.75M | frame_branch_ct[i][j][k][l][0], oldp, &newp, upd, |
572 | 4.75M | stepsize); |
573 | 9.51M | else |
574 | 9.51M | s = vp9_prob_diff_update_savings_search( |
575 | 9.51M | frame_branch_ct[i][j][k][l][t], oldp, &newp, upd); |
576 | 14.2M | if (s > 0 && newp != oldp) u = 1; |
577 | 14.2M | if (u) |
578 | 535k | savings += s - (int)(vp9_cost_zero(upd)); |
579 | 13.7M | else |
580 | 13.7M | savings -= (int)(vp9_cost_zero(upd)); |
581 | 14.2M | update[u]++; |
582 | 14.2M | } |
583 | 4.75M | } |
584 | 865k | } |
585 | 144k | } |
586 | 72.0k | } |
587 | | |
588 | | // printf("Update %d %d, savings %d\n", update[0], update[1], savings); |
589 | | /* Is coef updated at all */ |
590 | 36.0k | if (update[1] == 0 || savings < 0) { |
591 | 12.1k | vpx_write_bit(bc, 0); |
592 | 12.1k | return; |
593 | 12.1k | } |
594 | 23.8k | vpx_write_bit(bc, 1); |
595 | 71.6k | for (i = 0; i < PLANE_TYPES; ++i) { |
596 | 143k | for (j = 0; j < REF_TYPES; ++j) { |
597 | 668k | for (k = 0; k < COEF_BANDS; ++k) { |
598 | 3.72M | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
599 | | // calc probs and branch cts for this frame only |
600 | 12.6M | for (t = 0; t < entropy_nodes_update; ++t) { |
601 | 9.46M | vpx_prob newp = new_coef_probs[i][j][k][l][t]; |
602 | 9.46M | vpx_prob *oldp = old_coef_probs[i][j][k][l] + t; |
603 | 9.46M | int64_t s; |
604 | 9.46M | int u = 0; |
605 | 9.46M | if (t == PIVOT_NODE) |
606 | 3.15M | s = vp9_prob_diff_update_savings_search_model( |
607 | 3.15M | frame_branch_ct[i][j][k][l][0], *oldp, &newp, upd, |
608 | 3.15M | stepsize); |
609 | 6.30M | else |
610 | 6.30M | s = vp9_prob_diff_update_savings_search( |
611 | 6.30M | frame_branch_ct[i][j][k][l][t], *oldp, &newp, upd); |
612 | 9.46M | if (s > 0 && newp != *oldp) u = 1; |
613 | 9.46M | vpx_write(bc, u, upd); |
614 | 9.46M | if (u) { |
615 | | /* send/use new probability */ |
616 | 527k | vp9_write_prob_diff_update(bc, newp, *oldp); |
617 | 527k | *oldp = newp; |
618 | 527k | } |
619 | 9.46M | } |
620 | 3.15M | } |
621 | 573k | } |
622 | 95.5k | } |
623 | 47.7k | } |
624 | 23.8k | return; |
625 | 36.0k | } |
626 | | |
627 | 0 | default: { |
628 | 0 | int updates = 0; |
629 | 0 | int noupdates_before_first = 0; |
630 | 0 | assert(cpi->sf.use_fast_coef_updates == ONE_LOOP_REDUCED); |
631 | 0 | for (i = 0; i < PLANE_TYPES; ++i) { |
632 | 0 | for (j = 0; j < REF_TYPES; ++j) { |
633 | 0 | for (k = 0; k < COEF_BANDS; ++k) { |
634 | 0 | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) { |
635 | | // calc probs and branch cts for this frame only |
636 | 0 | for (t = 0; t < entropy_nodes_update; ++t) { |
637 | 0 | vpx_prob newp = new_coef_probs[i][j][k][l][t]; |
638 | 0 | vpx_prob *oldp = old_coef_probs[i][j][k][l] + t; |
639 | 0 | int64_t s; |
640 | 0 | int u = 0; |
641 | |
|
642 | 0 | if (t == PIVOT_NODE) { |
643 | 0 | s = vp9_prob_diff_update_savings_search_model( |
644 | 0 | frame_branch_ct[i][j][k][l][0], *oldp, &newp, upd, |
645 | 0 | stepsize); |
646 | 0 | } else { |
647 | 0 | s = vp9_prob_diff_update_savings_search( |
648 | 0 | frame_branch_ct[i][j][k][l][t], *oldp, &newp, upd); |
649 | 0 | } |
650 | |
|
651 | 0 | if (s > 0 && newp != *oldp) u = 1; |
652 | 0 | updates += u; |
653 | 0 | if (u == 0 && updates == 0) { |
654 | 0 | noupdates_before_first++; |
655 | 0 | continue; |
656 | 0 | } |
657 | 0 | if (u == 1 && updates == 1) { |
658 | 0 | int v; |
659 | | // first update |
660 | 0 | vpx_write_bit(bc, 1); |
661 | 0 | for (v = 0; v < noupdates_before_first; ++v) |
662 | 0 | vpx_write(bc, 0, upd); |
663 | 0 | } |
664 | 0 | vpx_write(bc, u, upd); |
665 | 0 | if (u) { |
666 | | /* send/use new probability */ |
667 | 0 | vp9_write_prob_diff_update(bc, newp, *oldp); |
668 | 0 | *oldp = newp; |
669 | 0 | } |
670 | 0 | } |
671 | 0 | } |
672 | 0 | } |
673 | 0 | } |
674 | 0 | } |
675 | 0 | if (updates == 0) { |
676 | 0 | vpx_write_bit(bc, 0); // no updates |
677 | 0 | } |
678 | 0 | return; |
679 | 36.0k | } |
680 | 36.0k | } |
681 | 36.0k | } |
682 | | |
683 | 42.2k | static void update_coef_probs(VP9_COMP *cpi, vpx_writer *w) { |
684 | 42.2k | const TX_MODE tx_mode = cpi->common.tx_mode; |
685 | 42.2k | const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode]; |
686 | 42.2k | TX_SIZE tx_size; |
687 | 177k | for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size) { |
688 | 135k | vp9_coeff_stats frame_branch_ct[PLANE_TYPES]; |
689 | 135k | vp9_coeff_probs_model frame_coef_probs[PLANE_TYPES]; |
690 | 135k | if (cpi->td.counts->tx.tx_totals[tx_size] <= 20 || |
691 | 135k | (tx_size >= TX_16X16 && cpi->sf.tx_size_search_method == USE_TX_8X8)) { |
692 | 99.0k | vpx_write_bit(w, 0); |
693 | 99.0k | } else { |
694 | 36.0k | build_tree_distribution(cpi, tx_size, frame_branch_ct, frame_coef_probs); |
695 | 36.0k | update_coef_probs_common(w, cpi, tx_size, frame_branch_ct, |
696 | 36.0k | frame_coef_probs); |
697 | 36.0k | } |
698 | 135k | } |
699 | 42.2k | } |
700 | | |
701 | | static void encode_loopfilter(struct loopfilter *lf, |
702 | 42.2k | struct vpx_write_bit_buffer *wb) { |
703 | 42.2k | int i; |
704 | | |
705 | | // Encode the loop filter level and type |
706 | 42.2k | vpx_wb_write_literal(wb, lf->filter_level, 6); |
707 | 42.2k | vpx_wb_write_literal(wb, lf->sharpness_level, 3); |
708 | | |
709 | | // Write out loop filter deltas applied at the MB level based on mode or |
710 | | // ref frame (if they are enabled). |
711 | 42.2k | vpx_wb_write_bit(wb, lf->mode_ref_delta_enabled); |
712 | | |
713 | 42.2k | if (lf->mode_ref_delta_enabled) { |
714 | 42.2k | vpx_wb_write_bit(wb, lf->mode_ref_delta_update); |
715 | 42.2k | if (lf->mode_ref_delta_update) { |
716 | 38.7k | for (i = 0; i < MAX_REF_LF_DELTAS; i++) { |
717 | 31.0k | const int delta = lf->ref_deltas[i]; |
718 | 31.0k | const int changed = delta != lf->last_ref_deltas[i]; |
719 | 31.0k | vpx_wb_write_bit(wb, changed); |
720 | 31.0k | if (changed) { |
721 | 23.2k | lf->last_ref_deltas[i] = delta; |
722 | 23.2k | vpx_wb_write_literal(wb, abs(delta) & 0x3F, 6); |
723 | 23.2k | vpx_wb_write_bit(wb, delta < 0); |
724 | 23.2k | } |
725 | 31.0k | } |
726 | | |
727 | 23.2k | for (i = 0; i < MAX_MODE_LF_DELTAS; i++) { |
728 | 15.5k | const int delta = lf->mode_deltas[i]; |
729 | 15.5k | const int changed = delta != lf->last_mode_deltas[i]; |
730 | 15.5k | vpx_wb_write_bit(wb, changed); |
731 | 15.5k | if (changed) { |
732 | 0 | lf->last_mode_deltas[i] = delta; |
733 | 0 | vpx_wb_write_literal(wb, abs(delta) & 0x3F, 6); |
734 | 0 | vpx_wb_write_bit(wb, delta < 0); |
735 | 0 | } |
736 | 15.5k | } |
737 | 7.75k | } |
738 | 42.2k | } |
739 | 42.2k | } |
740 | | |
741 | 126k | static void write_delta_q(struct vpx_write_bit_buffer *wb, int delta_q) { |
742 | 126k | if (delta_q != 0) { |
743 | 0 | vpx_wb_write_bit(wb, 1); |
744 | 0 | vpx_wb_write_literal(wb, abs(delta_q), 4); |
745 | 0 | vpx_wb_write_bit(wb, delta_q < 0); |
746 | 126k | } else { |
747 | 126k | vpx_wb_write_bit(wb, 0); |
748 | 126k | } |
749 | 126k | } |
750 | | |
751 | | static void encode_quantization(const VP9_COMMON *const cm, |
752 | 42.2k | struct vpx_write_bit_buffer *wb) { |
753 | 42.2k | vpx_wb_write_literal(wb, cm->base_qindex, QINDEX_BITS); |
754 | 42.2k | write_delta_q(wb, cm->y_dc_delta_q); |
755 | 42.2k | write_delta_q(wb, cm->uv_dc_delta_q); |
756 | 42.2k | write_delta_q(wb, cm->uv_ac_delta_q); |
757 | 42.2k | } |
758 | | |
759 | | static void encode_segmentation(VP9_COMMON *cm, MACROBLOCKD *xd, |
760 | 42.2k | struct vpx_write_bit_buffer *wb) { |
761 | 42.2k | int i, j; |
762 | | |
763 | 42.2k | const struct segmentation *seg = &cm->seg; |
764 | | |
765 | 42.2k | vpx_wb_write_bit(wb, seg->enabled); |
766 | 42.2k | if (!seg->enabled) return; |
767 | | |
768 | | // Segmentation map |
769 | 0 | vpx_wb_write_bit(wb, seg->update_map); |
770 | 0 | if (seg->update_map) { |
771 | | // Select the coding strategy (temporal or spatial) |
772 | 0 | vp9_choose_segmap_coding_method(cm, xd); |
773 | | // Write out probabilities used to decode unpredicted macro-block segments |
774 | 0 | for (i = 0; i < SEG_TREE_PROBS; i++) { |
775 | 0 | const int prob = seg->tree_probs[i]; |
776 | 0 | const int update = prob != MAX_PROB; |
777 | 0 | vpx_wb_write_bit(wb, update); |
778 | 0 | if (update) vpx_wb_write_literal(wb, prob, 8); |
779 | 0 | } |
780 | | |
781 | | // Write out the chosen coding method. |
782 | 0 | vpx_wb_write_bit(wb, seg->temporal_update); |
783 | 0 | if (seg->temporal_update) { |
784 | 0 | for (i = 0; i < PREDICTION_PROBS; i++) { |
785 | 0 | const int prob = seg->pred_probs[i]; |
786 | 0 | const int update = prob != MAX_PROB; |
787 | 0 | vpx_wb_write_bit(wb, update); |
788 | 0 | if (update) vpx_wb_write_literal(wb, prob, 8); |
789 | 0 | } |
790 | 0 | } |
791 | 0 | } |
792 | | |
793 | | // Segmentation data |
794 | 0 | vpx_wb_write_bit(wb, seg->update_data); |
795 | 0 | if (seg->update_data) { |
796 | 0 | vpx_wb_write_bit(wb, seg->abs_delta); |
797 | |
|
798 | 0 | for (i = 0; i < MAX_SEGMENTS; i++) { |
799 | 0 | for (j = 0; j < SEG_LVL_MAX; j++) { |
800 | 0 | const int active = segfeature_active(seg, i, j); |
801 | 0 | vpx_wb_write_bit(wb, active); |
802 | 0 | if (active) { |
803 | 0 | const int data = get_segdata(seg, i, j); |
804 | 0 | const int data_max = vp9_seg_feature_data_max(j); |
805 | |
|
806 | 0 | if (vp9_is_segfeature_signed(j)) { |
807 | 0 | encode_unsigned_max(wb, abs(data), data_max); |
808 | 0 | vpx_wb_write_bit(wb, data < 0); |
809 | 0 | } else { |
810 | 0 | encode_unsigned_max(wb, data, data_max); |
811 | 0 | } |
812 | 0 | } |
813 | 0 | } |
814 | 0 | } |
815 | 0 | } |
816 | 0 | } |
817 | | |
818 | | static void encode_txfm_probs(VP9_COMMON *cm, vpx_writer *w, |
819 | 35.4k | FRAME_COUNTS *counts) { |
820 | | // Mode |
821 | 35.4k | vpx_write_literal(w, VPXMIN(cm->tx_mode, ALLOW_32X32), 2); |
822 | 35.4k | if (cm->tx_mode >= ALLOW_32X32) |
823 | 29.7k | vpx_write_bit(w, cm->tx_mode == TX_MODE_SELECT); |
824 | | |
825 | | // Probabilities |
826 | 35.4k | if (cm->tx_mode == TX_MODE_SELECT) { |
827 | 12.8k | int i, j; |
828 | 12.8k | unsigned int ct_8x8p[TX_SIZES - 3][2]; |
829 | 12.8k | unsigned int ct_16x16p[TX_SIZES - 2][2]; |
830 | 12.8k | unsigned int ct_32x32p[TX_SIZES - 1][2]; |
831 | | |
832 | 38.4k | for (i = 0; i < TX_SIZE_CONTEXTS; i++) { |
833 | 25.6k | tx_counts_to_branch_counts_8x8(counts->tx.p8x8[i], ct_8x8p); |
834 | 51.2k | for (j = 0; j < TX_SIZES - 3; j++) |
835 | 25.6k | vp9_cond_prob_diff_update(w, &cm->fc->tx_probs.p8x8[i][j], ct_8x8p[j]); |
836 | 25.6k | } |
837 | | |
838 | 38.4k | for (i = 0; i < TX_SIZE_CONTEXTS; i++) { |
839 | 25.6k | tx_counts_to_branch_counts_16x16(counts->tx.p16x16[i], ct_16x16p); |
840 | 76.8k | for (j = 0; j < TX_SIZES - 2; j++) |
841 | 51.2k | vp9_cond_prob_diff_update(w, &cm->fc->tx_probs.p16x16[i][j], |
842 | 51.2k | ct_16x16p[j]); |
843 | 25.6k | } |
844 | | |
845 | 38.4k | for (i = 0; i < TX_SIZE_CONTEXTS; i++) { |
846 | 25.6k | tx_counts_to_branch_counts_32x32(counts->tx.p32x32[i], ct_32x32p); |
847 | 102k | for (j = 0; j < TX_SIZES - 1; j++) |
848 | 76.8k | vp9_cond_prob_diff_update(w, &cm->fc->tx_probs.p32x32[i][j], |
849 | 76.8k | ct_32x32p[j]); |
850 | 25.6k | } |
851 | 12.8k | } |
852 | 35.4k | } |
853 | | |
854 | | static void write_interp_filter(INTERP_FILTER filter, |
855 | 34.5k | struct vpx_write_bit_buffer *wb) { |
856 | 34.5k | const int filter_to_literal[] = { 1, 0, 2, 3 }; |
857 | | |
858 | 34.5k | vpx_wb_write_bit(wb, filter == SWITCHABLE); |
859 | 34.5k | if (filter != SWITCHABLE) |
860 | 23.4k | vpx_wb_write_literal(wb, filter_to_literal[filter], 2); |
861 | 34.5k | } |
862 | | |
863 | 34.5k | static void fix_interp_filter(VP9_COMMON *cm, FRAME_COUNTS *counts) { |
864 | 34.5k | if (cm->interp_filter == SWITCHABLE) { |
865 | | // Check to see if only one of the filters is actually used |
866 | 15.9k | int count[SWITCHABLE_FILTERS]; |
867 | 15.9k | int i, j, c = 0; |
868 | 63.6k | for (i = 0; i < SWITCHABLE_FILTERS; ++i) { |
869 | 47.7k | count[i] = 0; |
870 | 238k | for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) |
871 | 190k | count[i] += counts->switchable_interp[j][i]; |
872 | 47.7k | c += (count[i] > 0); |
873 | 47.7k | } |
874 | 15.9k | if (c == 1) { |
875 | | // Only one filter is used. So set the filter at frame level |
876 | 5.52k | for (i = 0; i < SWITCHABLE_FILTERS; ++i) { |
877 | 5.52k | if (count[i]) { |
878 | 4.84k | cm->interp_filter = i; |
879 | 4.84k | break; |
880 | 4.84k | } |
881 | 5.52k | } |
882 | 4.84k | } |
883 | 15.9k | } |
884 | 34.5k | } |
885 | | |
886 | | static void write_tile_info(const VP9_COMMON *const cm, |
887 | 42.2k | struct vpx_write_bit_buffer *wb) { |
888 | 42.2k | int min_log2_tile_cols, max_log2_tile_cols, ones; |
889 | 42.2k | vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); |
890 | | |
891 | | // columns |
892 | 42.2k | ones = cm->log2_tile_cols - min_log2_tile_cols; |
893 | 43.0k | while (ones--) vpx_wb_write_bit(wb, 1); |
894 | | |
895 | 42.2k | if (cm->log2_tile_cols < max_log2_tile_cols) vpx_wb_write_bit(wb, 0); |
896 | | |
897 | | // rows |
898 | 42.2k | vpx_wb_write_bit(wb, cm->log2_tile_rows != 0); |
899 | 42.2k | if (cm->log2_tile_rows != 0) vpx_wb_write_bit(wb, cm->log2_tile_rows != 1); |
900 | 42.2k | } |
901 | | |
902 | 34.5k | int vp9_get_refresh_mask(VP9_COMP *cpi) { |
903 | 34.5k | if (cpi->ext_ratectrl.ready && |
904 | 34.5k | (cpi->ext_ratectrl.funcs.rc_type & VPX_RC_GOP) != 0 && |
905 | 34.5k | cpi->ext_ratectrl.funcs.get_gop_decision != NULL) { |
906 | 0 | GF_GROUP *const gf_group = &cpi->twopass.gf_group; |
907 | 0 | const int this_gf_index = gf_group->index; |
908 | 0 | const int update_ref_idx = gf_group->update_ref_idx[this_gf_index]; |
909 | |
|
910 | 0 | if (update_ref_idx != INVALID_IDX) { |
911 | 0 | return (1 << update_ref_idx); |
912 | 0 | } else { |
913 | 0 | return 0; |
914 | 0 | } |
915 | 0 | } |
916 | 34.5k | if (vp9_preserve_existing_gf(cpi)) { |
917 | | // We have decided to preserve the previously existing golden frame as our |
918 | | // new ARF frame. However, in the short term we leave it in the GF slot and, |
919 | | // if we're updating the GF with the current decoded frame, we save it |
920 | | // instead to the ARF slot. |
921 | | // Later, in the function vp9_encoder.c:vp9_update_reference_frames() we |
922 | | // will swap gld_fb_idx and alt_fb_idx to achieve our objective. We do it |
923 | | // there so that it can be done outside of the recode loop. |
924 | | // Note: This is highly specific to the use of ARF as a forward reference, |
925 | | // and this needs to be generalized as other uses are implemented |
926 | | // (like RTC/temporal scalability). |
927 | 0 | return (cpi->refresh_last_frame << cpi->lst_fb_idx) | |
928 | 0 | (cpi->refresh_golden_frame << cpi->alt_fb_idx); |
929 | 34.5k | } else { |
930 | 34.5k | int arf_idx = cpi->alt_fb_idx; |
931 | 34.5k | GF_GROUP *const gf_group = &cpi->twopass.gf_group; |
932 | | |
933 | 34.5k | if (cpi->multi_layer_arf) { |
934 | 0 | for (arf_idx = 0; arf_idx < REF_FRAMES; ++arf_idx) { |
935 | 0 | if (arf_idx != cpi->alt_fb_idx && arf_idx != cpi->lst_fb_idx && |
936 | 0 | arf_idx != cpi->gld_fb_idx) { |
937 | 0 | int idx; |
938 | 0 | for (idx = 0; idx < gf_group->stack_size; ++idx) |
939 | 0 | if (arf_idx == gf_group->arf_index_stack[idx]) break; |
940 | 0 | if (idx == gf_group->stack_size) break; |
941 | 0 | } |
942 | 0 | } |
943 | 0 | } |
944 | 34.5k | cpi->twopass.gf_group.top_arf_idx = arf_idx; |
945 | | |
946 | 34.5k | if (cpi->use_svc && cpi->svc.use_set_ref_frame_config && |
947 | 34.5k | cpi->svc.temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) |
948 | 0 | return cpi->svc.update_buffer_slot[cpi->svc.spatial_layer_id]; |
949 | 34.5k | return (cpi->refresh_last_frame << cpi->lst_fb_idx) | |
950 | 34.5k | (cpi->refresh_golden_frame << cpi->gld_fb_idx) | |
951 | 34.5k | (cpi->refresh_alt_ref_frame << arf_idx); |
952 | 34.5k | } |
953 | 34.5k | } |
954 | | |
955 | 0 | static int encode_tile_worker(void *arg1, void *arg2) { |
956 | 0 | VP9_COMP *cpi = (VP9_COMP *)arg1; |
957 | 0 | VP9BitstreamWorkerData *data = (VP9BitstreamWorkerData *)arg2; |
958 | 0 | MACROBLOCKD *const xd = &data->xd; |
959 | 0 | const int tile_row = 0; |
960 | 0 | vpx_start_encode(&data->bit_writer, data->dest, data->dest_size); |
961 | 0 | write_modes(cpi, xd, &cpi->tile_data[data->tile_idx].tile_info, |
962 | 0 | &data->bit_writer, tile_row, data->tile_idx, |
963 | 0 | &data->max_mv_magnitude, data->interp_filter_selected); |
964 | 0 | return vpx_stop_encode(&data->bit_writer) == 0; |
965 | 0 | } |
966 | | |
967 | 2.98k | void vp9_bitstream_encode_tiles_buffer_dealloc(VP9_COMP *const cpi) { |
968 | 2.98k | if (cpi->vp9_bitstream_worker_data) { |
969 | 0 | int i; |
970 | 0 | for (i = 1; i < cpi->num_workers; ++i) { |
971 | 0 | vpx_free(cpi->vp9_bitstream_worker_data[i].dest); |
972 | 0 | } |
973 | 0 | vpx_free(cpi->vp9_bitstream_worker_data); |
974 | 0 | cpi->vp9_bitstream_worker_data = NULL; |
975 | 0 | } |
976 | 2.98k | } |
977 | | |
978 | 0 | static size_t encode_tiles_buffer_alloc_size(const VP9_COMP *cpi) { |
979 | 0 | const VP9_COMMON *cm = &cpi->common; |
980 | 0 | const int image_bps = |
981 | 0 | (8 + 2 * (8 >> (cm->subsampling_x + cm->subsampling_y))) * |
982 | 0 | (1 + (cm->bit_depth > 8)); |
983 | 0 | const int64_t size = |
984 | 0 | (int64_t)cpi->oxcf.width * cpi->oxcf.height * image_bps / 8; |
985 | 0 | return (size_t)size; |
986 | 0 | } |
987 | | |
988 | | static void encode_tiles_buffer_alloc(VP9_COMP *const cpi, |
989 | 0 | size_t buffer_alloc_size) { |
990 | 0 | VP9_COMMON *const cm = &cpi->common; |
991 | 0 | int i; |
992 | 0 | const size_t worker_data_size = |
993 | 0 | cpi->num_workers * sizeof(*cpi->vp9_bitstream_worker_data); |
994 | 0 | CHECK_MEM_ERROR(&cm->error, cpi->vp9_bitstream_worker_data, |
995 | 0 | vpx_memalign(16, worker_data_size)); |
996 | 0 | memset(cpi->vp9_bitstream_worker_data, 0, worker_data_size); |
997 | 0 | for (i = 1; i < cpi->num_workers; ++i) { |
998 | 0 | CHECK_MEM_ERROR(&cm->error, cpi->vp9_bitstream_worker_data[i].dest, |
999 | 0 | vpx_malloc(buffer_alloc_size)); |
1000 | 0 | cpi->vp9_bitstream_worker_data[i].dest_size = buffer_alloc_size; |
1001 | 0 | } |
1002 | 0 | } |
1003 | | |
1004 | | static size_t encode_tiles_mt(VP9_COMP *cpi, uint8_t *data_ptr, |
1005 | 0 | size_t data_size) { |
1006 | 0 | const VPxWorkerInterface *const winterface = vpx_get_worker_interface(); |
1007 | 0 | VP9_COMMON *const cm = &cpi->common; |
1008 | 0 | const int tile_cols = 1 << cm->log2_tile_cols; |
1009 | 0 | const int num_workers = cpi->num_workers; |
1010 | 0 | size_t total_size = 0; |
1011 | 0 | int tile_col = 0; |
1012 | 0 | int error = 0; |
1013 | |
|
1014 | 0 | const size_t buffer_alloc_size = encode_tiles_buffer_alloc_size(cpi); |
1015 | 0 | if (!cpi->vp9_bitstream_worker_data || |
1016 | 0 | cpi->vp9_bitstream_worker_data[1].dest_size != buffer_alloc_size) { |
1017 | 0 | vp9_bitstream_encode_tiles_buffer_dealloc(cpi); |
1018 | 0 | encode_tiles_buffer_alloc(cpi, buffer_alloc_size); |
1019 | 0 | } |
1020 | |
|
1021 | 0 | while (tile_col < tile_cols) { |
1022 | 0 | int i, j; |
1023 | 0 | for (i = 0; i < num_workers && tile_col < tile_cols; ++i) { |
1024 | 0 | VPxWorker *const worker = &cpi->workers[i]; |
1025 | 0 | VP9BitstreamWorkerData *const data = &cpi->vp9_bitstream_worker_data[i]; |
1026 | | |
1027 | | // Populate the worker data. |
1028 | 0 | data->xd = cpi->td.mb.e_mbd; |
1029 | 0 | data->tile_idx = tile_col; |
1030 | 0 | data->max_mv_magnitude = cpi->max_mv_magnitude; |
1031 | 0 | memset(data->interp_filter_selected, 0, |
1032 | 0 | sizeof(data->interp_filter_selected[0][0]) * SWITCHABLE); |
1033 | | |
1034 | | // First thread can directly write into the output buffer. |
1035 | 0 | if (i == 0) { |
1036 | | // If this worker happens to be for the last tile, then do not offset it |
1037 | | // by 4 for the tile size. |
1038 | 0 | const size_t offset = total_size + (tile_col == tile_cols - 1 ? 0 : 4); |
1039 | 0 | if (data_size < offset) { |
1040 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_ERROR, |
1041 | 0 | "encode_tiles_mt: output buffer full"); |
1042 | 0 | } |
1043 | 0 | data->dest = data_ptr + offset; |
1044 | 0 | data->dest_size = data_size - offset; |
1045 | 0 | } |
1046 | 0 | worker->data1 = cpi; |
1047 | 0 | worker->data2 = data; |
1048 | 0 | worker->hook = encode_tile_worker; |
1049 | 0 | worker->had_error = 0; |
1050 | |
|
1051 | 0 | if (i < num_workers - 1) { |
1052 | 0 | winterface->launch(worker); |
1053 | 0 | } else { |
1054 | 0 | winterface->execute(worker); |
1055 | 0 | } |
1056 | 0 | ++tile_col; |
1057 | 0 | } |
1058 | 0 | for (j = 0; j < i; ++j) { |
1059 | 0 | VPxWorker *const worker = &cpi->workers[j]; |
1060 | 0 | VP9BitstreamWorkerData *const data = |
1061 | 0 | (VP9BitstreamWorkerData *)worker->data2; |
1062 | 0 | uint32_t tile_size; |
1063 | 0 | int k; |
1064 | |
|
1065 | 0 | if (!winterface->sync(worker)) { |
1066 | 0 | error = 1; |
1067 | 0 | continue; |
1068 | 0 | } |
1069 | | |
1070 | 0 | tile_size = data->bit_writer.pos; |
1071 | | |
1072 | | // Aggregate per-thread bitstream stats. |
1073 | 0 | cpi->max_mv_magnitude = |
1074 | 0 | VPXMAX(cpi->max_mv_magnitude, data->max_mv_magnitude); |
1075 | 0 | for (k = 0; k < SWITCHABLE; ++k) { |
1076 | 0 | cpi->interp_filter_selected[0][k] += data->interp_filter_selected[0][k]; |
1077 | 0 | } |
1078 | | |
1079 | | // Prefix the size of the tile on all but the last. |
1080 | 0 | if (tile_col != tile_cols || j < i - 1) { |
1081 | 0 | if (data_size - total_size < 4) { |
1082 | 0 | error = 1; |
1083 | 0 | continue; |
1084 | 0 | } |
1085 | 0 | mem_put_be32(data_ptr + total_size, tile_size); |
1086 | 0 | total_size += 4; |
1087 | 0 | } |
1088 | 0 | if (j > 0) { |
1089 | 0 | if (data_size - total_size < tile_size) { |
1090 | 0 | error = 1; |
1091 | 0 | continue; |
1092 | 0 | } |
1093 | 0 | memcpy(data_ptr + total_size, data->dest, tile_size); |
1094 | 0 | } |
1095 | 0 | total_size += tile_size; |
1096 | 0 | } |
1097 | 0 | if (error) { |
1098 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_ERROR, |
1099 | 0 | "encode_tiles_mt: output buffer full"); |
1100 | 0 | } |
1101 | 0 | } |
1102 | 0 | return total_size; |
1103 | 0 | } |
1104 | | |
1105 | 42.2k | static size_t encode_tiles(VP9_COMP *cpi, uint8_t *data_ptr, size_t data_size) { |
1106 | 42.2k | VP9_COMMON *const cm = &cpi->common; |
1107 | 42.2k | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
1108 | 42.2k | vpx_writer residual_bc; |
1109 | 42.2k | int tile_row, tile_col; |
1110 | 42.2k | size_t total_size = 0; |
1111 | 42.2k | const int tile_cols = 1 << cm->log2_tile_cols; |
1112 | 42.2k | const int tile_rows = 1 << cm->log2_tile_rows; |
1113 | | |
1114 | 42.2k | memset(cm->above_seg_context, 0, |
1115 | 42.2k | sizeof(*cm->above_seg_context) * mi_cols_aligned_to_sb(cm->mi_cols)); |
1116 | | |
1117 | | // Encoding tiles in parallel is done only for realtime mode now. In other |
1118 | | // modes the speed up is insignificant and requires further testing to ensure |
1119 | | // that it does not make the overall process worse in any case. |
1120 | 42.2k | if (cpi->oxcf.mode == REALTIME && cpi->num_workers > 1 && tile_rows == 1 && |
1121 | 42.2k | tile_cols > 1) { |
1122 | 0 | return encode_tiles_mt(cpi, data_ptr, data_size); |
1123 | 0 | } |
1124 | | |
1125 | 84.5k | for (tile_row = 0; tile_row < tile_rows; tile_row++) { |
1126 | 86.9k | for (tile_col = 0; tile_col < tile_cols; tile_col++) { |
1127 | 44.7k | int tile_idx = tile_row * tile_cols + tile_col; |
1128 | | |
1129 | 44.7k | size_t offset; |
1130 | 44.7k | if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1) |
1131 | 2.44k | offset = total_size + 4; |
1132 | 42.2k | else |
1133 | 42.2k | offset = total_size; |
1134 | 44.7k | if (data_size < offset) { |
1135 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_ERROR, |
1136 | 0 | "encode_tiles: output buffer full"); |
1137 | 0 | } |
1138 | 44.7k | vpx_start_encode(&residual_bc, data_ptr + offset, data_size - offset); |
1139 | | |
1140 | 44.7k | write_modes(cpi, xd, &cpi->tile_data[tile_idx].tile_info, &residual_bc, |
1141 | 44.7k | tile_row, tile_col, &cpi->max_mv_magnitude, |
1142 | 44.7k | cpi->interp_filter_selected); |
1143 | | |
1144 | 44.7k | if (vpx_stop_encode(&residual_bc)) { |
1145 | 71 | vpx_internal_error(&cm->error, VPX_CODEC_ERROR, |
1146 | 71 | "encode_tiles: output buffer full"); |
1147 | 71 | } |
1148 | 44.7k | if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1) { |
1149 | | // size of this tile |
1150 | 2.44k | mem_put_be32(data_ptr + total_size, residual_bc.pos); |
1151 | 2.44k | total_size += 4; |
1152 | 2.44k | } |
1153 | | |
1154 | 44.7k | total_size += residual_bc.pos; |
1155 | 44.7k | } |
1156 | 42.2k | } |
1157 | 42.2k | return total_size; |
1158 | 42.2k | } |
1159 | | |
1160 | | static void write_render_size(const VP9_COMMON *cm, |
1161 | 42.2k | struct vpx_write_bit_buffer *wb) { |
1162 | 42.2k | const int scaling_active = |
1163 | 42.2k | cm->width != cm->render_width || cm->height != cm->render_height; |
1164 | 42.2k | vpx_wb_write_bit(wb, scaling_active); |
1165 | 42.2k | if (scaling_active) { |
1166 | 0 | vpx_wb_write_literal(wb, cm->render_width - 1, 16); |
1167 | 0 | vpx_wb_write_literal(wb, cm->render_height - 1, 16); |
1168 | 0 | } |
1169 | 42.2k | } |
1170 | | |
1171 | | static void write_frame_size(const VP9_COMMON *cm, |
1172 | 7.75k | struct vpx_write_bit_buffer *wb) { |
1173 | 7.75k | vpx_wb_write_literal(wb, cm->width - 1, 16); |
1174 | 7.75k | vpx_wb_write_literal(wb, cm->height - 1, 16); |
1175 | | |
1176 | 7.75k | write_render_size(cm, wb); |
1177 | 7.75k | } |
1178 | | |
1179 | | static void write_frame_size_with_refs(VP9_COMP *cpi, |
1180 | 34.5k | struct vpx_write_bit_buffer *wb) { |
1181 | 34.5k | VP9_COMMON *const cm = &cpi->common; |
1182 | 34.5k | int found = 0; |
1183 | | |
1184 | 34.5k | MV_REFERENCE_FRAME ref_frame; |
1185 | 34.5k | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
1186 | 34.5k | YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, ref_frame); |
1187 | | |
1188 | | // Set "found" to 0 for temporal svc and for spatial svc key frame |
1189 | 34.5k | if (cpi->use_svc && |
1190 | 34.5k | ((cpi->svc.number_temporal_layers > 1 && |
1191 | 0 | cpi->oxcf.rc_mode == VPX_CBR) || |
1192 | 0 | (cpi->svc.number_spatial_layers > 1 && |
1193 | 0 | cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame))) { |
1194 | 0 | found = 0; |
1195 | 34.5k | } else if (cfg != NULL) { |
1196 | 34.5k | found = |
1197 | 34.5k | cm->width == cfg->y_crop_width && cm->height == cfg->y_crop_height; |
1198 | 34.5k | } |
1199 | 34.5k | vpx_wb_write_bit(wb, found); |
1200 | 34.5k | if (found) { |
1201 | 34.5k | break; |
1202 | 34.5k | } |
1203 | 34.5k | } |
1204 | | |
1205 | 34.5k | if (!found) { |
1206 | 0 | vpx_wb_write_literal(wb, cm->width - 1, 16); |
1207 | 0 | vpx_wb_write_literal(wb, cm->height - 1, 16); |
1208 | 0 | } |
1209 | | |
1210 | 34.5k | write_render_size(cm, wb); |
1211 | 34.5k | } |
1212 | | |
1213 | 7.75k | static void write_sync_code(struct vpx_write_bit_buffer *wb) { |
1214 | 7.75k | vpx_wb_write_literal(wb, VP9_SYNC_CODE_0, 8); |
1215 | 7.75k | vpx_wb_write_literal(wb, VP9_SYNC_CODE_1, 8); |
1216 | 7.75k | vpx_wb_write_literal(wb, VP9_SYNC_CODE_2, 8); |
1217 | 7.75k | } |
1218 | | |
1219 | | static void write_profile(BITSTREAM_PROFILE profile, |
1220 | 42.2k | struct vpx_write_bit_buffer *wb) { |
1221 | 42.2k | switch (profile) { |
1222 | 42.2k | case PROFILE_0: vpx_wb_write_literal(wb, 0, 2); break; |
1223 | 0 | case PROFILE_1: vpx_wb_write_literal(wb, 2, 2); break; |
1224 | 0 | case PROFILE_2: vpx_wb_write_literal(wb, 1, 2); break; |
1225 | 0 | default: |
1226 | 0 | assert(profile == PROFILE_3); |
1227 | 0 | vpx_wb_write_literal(wb, 6, 3); |
1228 | 0 | break; |
1229 | 42.2k | } |
1230 | 42.2k | } |
1231 | | |
1232 | | static void write_bitdepth_colorspace_sampling( |
1233 | 7.75k | VP9_COMMON *const cm, struct vpx_write_bit_buffer *wb) { |
1234 | 7.75k | if (cm->profile >= PROFILE_2) { |
1235 | 0 | assert(cm->bit_depth > VPX_BITS_8); |
1236 | 0 | vpx_wb_write_bit(wb, cm->bit_depth == VPX_BITS_10 ? 0 : 1); |
1237 | 0 | } |
1238 | 7.75k | vpx_wb_write_literal(wb, cm->color_space, 3); |
1239 | 7.75k | if (cm->color_space != VPX_CS_SRGB) { |
1240 | | // 0: [16, 235] (i.e. xvYCC), 1: [0, 255] |
1241 | 7.75k | vpx_wb_write_bit(wb, cm->color_range); |
1242 | 7.75k | if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) { |
1243 | 0 | assert(cm->subsampling_x != 1 || cm->subsampling_y != 1); |
1244 | 0 | vpx_wb_write_bit(wb, cm->subsampling_x); |
1245 | 0 | vpx_wb_write_bit(wb, cm->subsampling_y); |
1246 | 0 | vpx_wb_write_bit(wb, 0); // unused |
1247 | 7.75k | } else { |
1248 | 7.75k | assert(cm->subsampling_x == 1 && cm->subsampling_y == 1); |
1249 | 7.75k | } |
1250 | 7.75k | } else { |
1251 | 0 | assert(cm->profile == PROFILE_1 || cm->profile == PROFILE_3); |
1252 | 0 | vpx_wb_write_bit(wb, 0); // unused |
1253 | 0 | } |
1254 | 7.75k | } |
1255 | | |
1256 | | static void write_uncompressed_header(VP9_COMP *cpi, |
1257 | 42.2k | struct vpx_write_bit_buffer *wb) { |
1258 | 42.2k | VP9_COMMON *const cm = &cpi->common; |
1259 | 42.2k | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
1260 | | |
1261 | 42.2k | vpx_wb_write_literal(wb, VP9_FRAME_MARKER, 2); |
1262 | | |
1263 | 42.2k | write_profile(cm->profile, wb); |
1264 | | |
1265 | | // If to use show existing frame. |
1266 | 42.2k | vpx_wb_write_bit(wb, cm->show_existing_frame); |
1267 | 42.2k | if (cm->show_existing_frame) { |
1268 | 0 | vpx_wb_write_literal(wb, cpi->alt_fb_idx, 3); |
1269 | 0 | return; |
1270 | 0 | } |
1271 | | |
1272 | 42.2k | vpx_wb_write_bit(wb, cm->frame_type); |
1273 | 42.2k | vpx_wb_write_bit(wb, cm->show_frame); |
1274 | 42.2k | vpx_wb_write_bit(wb, cm->error_resilient_mode); |
1275 | | |
1276 | 42.2k | if (cm->frame_type == KEY_FRAME) { |
1277 | 7.75k | write_sync_code(wb); |
1278 | 7.75k | write_bitdepth_colorspace_sampling(cm, wb); |
1279 | 7.75k | write_frame_size(cm, wb); |
1280 | 34.5k | } else { |
1281 | 34.5k | if (!cm->show_frame) vpx_wb_write_bit(wb, cm->intra_only); |
1282 | | |
1283 | 34.5k | if (!cm->error_resilient_mode) |
1284 | 34.5k | vpx_wb_write_literal(wb, cm->reset_frame_context, 2); |
1285 | | |
1286 | 34.5k | if (cm->intra_only) { |
1287 | 0 | write_sync_code(wb); |
1288 | | |
1289 | | // Note for profile 0, 420 8bpp is assumed. |
1290 | 0 | if (cm->profile > PROFILE_0) { |
1291 | 0 | write_bitdepth_colorspace_sampling(cm, wb); |
1292 | 0 | } |
1293 | |
|
1294 | 0 | vpx_wb_write_literal(wb, vp9_get_refresh_mask(cpi), REF_FRAMES); |
1295 | 0 | write_frame_size(cm, wb); |
1296 | 34.5k | } else { |
1297 | 34.5k | MV_REFERENCE_FRAME ref_frame; |
1298 | 34.5k | vpx_wb_write_literal(wb, vp9_get_refresh_mask(cpi), REF_FRAMES); |
1299 | 138k | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
1300 | 103k | assert(get_ref_frame_map_idx(cpi, ref_frame) != INVALID_IDX); |
1301 | 103k | vpx_wb_write_literal(wb, get_ref_frame_map_idx(cpi, ref_frame), |
1302 | 103k | REF_FRAMES_LOG2); |
1303 | 103k | vpx_wb_write_bit(wb, cm->ref_frame_sign_bias[ref_frame]); |
1304 | 103k | } |
1305 | | |
1306 | 34.5k | write_frame_size_with_refs(cpi, wb); |
1307 | | |
1308 | 34.5k | vpx_wb_write_bit(wb, cm->allow_high_precision_mv); |
1309 | | |
1310 | 34.5k | fix_interp_filter(cm, cpi->td.counts); |
1311 | 34.5k | write_interp_filter(cm->interp_filter, wb); |
1312 | 34.5k | } |
1313 | 34.5k | } |
1314 | | |
1315 | 42.2k | if (!cm->error_resilient_mode) { |
1316 | 42.2k | vpx_wb_write_bit(wb, cm->refresh_frame_context); |
1317 | 42.2k | vpx_wb_write_bit(wb, cm->frame_parallel_decoding_mode); |
1318 | 42.2k | } |
1319 | | |
1320 | 42.2k | vpx_wb_write_literal(wb, cm->frame_context_idx, FRAME_CONTEXTS_LOG2); |
1321 | | |
1322 | 42.2k | encode_loopfilter(&cm->lf, wb); |
1323 | 42.2k | encode_quantization(cm, wb); |
1324 | 42.2k | encode_segmentation(cm, xd, wb); |
1325 | | |
1326 | 42.2k | write_tile_info(cm, wb); |
1327 | 42.2k | } |
1328 | | |
1329 | | static size_t write_compressed_header(VP9_COMP *cpi, uint8_t *data, |
1330 | 42.2k | size_t data_size) { |
1331 | 42.2k | VP9_COMMON *const cm = &cpi->common; |
1332 | 42.2k | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
1333 | 42.2k | FRAME_CONTEXT *const fc = cm->fc; |
1334 | 42.2k | FRAME_COUNTS *counts = cpi->td.counts; |
1335 | 42.2k | vpx_writer header_bc; |
1336 | | |
1337 | 42.2k | vpx_start_encode(&header_bc, data, data_size); |
1338 | | |
1339 | 42.2k | if (xd->lossless) |
1340 | 6.83k | cm->tx_mode = ONLY_4X4; |
1341 | 35.4k | else |
1342 | 35.4k | encode_txfm_probs(cm, &header_bc, counts); |
1343 | | |
1344 | 42.2k | update_coef_probs(cpi, &header_bc); |
1345 | 42.2k | update_skip_probs(cm, &header_bc, counts); |
1346 | | |
1347 | 42.2k | if (!frame_is_intra_only(cm)) { |
1348 | 34.5k | int i; |
1349 | | |
1350 | 276k | for (i = 0; i < INTER_MODE_CONTEXTS; ++i) |
1351 | 241k | prob_diff_update(vp9_inter_mode_tree, cm->fc->inter_mode_probs[i], |
1352 | 241k | counts->inter_mode[i], INTER_MODES, &header_bc); |
1353 | | |
1354 | 34.5k | if (cm->interp_filter == SWITCHABLE) |
1355 | 11.0k | update_switchable_interp_probs(cm, &header_bc, counts); |
1356 | | |
1357 | 172k | for (i = 0; i < INTRA_INTER_CONTEXTS; i++) |
1358 | 138k | vp9_cond_prob_diff_update(&header_bc, &fc->intra_inter_prob[i], |
1359 | 138k | counts->intra_inter[i]); |
1360 | | |
1361 | 34.5k | if (cpi->allow_comp_inter_inter) { |
1362 | 0 | const int use_compound_pred = cm->reference_mode != SINGLE_REFERENCE; |
1363 | 0 | const int use_hybrid_pred = cm->reference_mode == REFERENCE_MODE_SELECT; |
1364 | |
|
1365 | 0 | vpx_write_bit(&header_bc, use_compound_pred); |
1366 | 0 | if (use_compound_pred) { |
1367 | 0 | vpx_write_bit(&header_bc, use_hybrid_pred); |
1368 | 0 | if (use_hybrid_pred) |
1369 | 0 | for (i = 0; i < COMP_INTER_CONTEXTS; i++) |
1370 | 0 | vp9_cond_prob_diff_update(&header_bc, &fc->comp_inter_prob[i], |
1371 | 0 | counts->comp_inter[i]); |
1372 | 0 | } |
1373 | 0 | } |
1374 | | |
1375 | 34.5k | if (cm->reference_mode != COMPOUND_REFERENCE) { |
1376 | 207k | for (i = 0; i < REF_CONTEXTS; i++) { |
1377 | 172k | vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][0], |
1378 | 172k | counts->single_ref[i][0]); |
1379 | 172k | vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][1], |
1380 | 172k | counts->single_ref[i][1]); |
1381 | 172k | } |
1382 | 34.5k | } |
1383 | | |
1384 | 34.5k | if (cm->reference_mode != SINGLE_REFERENCE) |
1385 | 0 | for (i = 0; i < REF_CONTEXTS; i++) |
1386 | 0 | vp9_cond_prob_diff_update(&header_bc, &fc->comp_ref_prob[i], |
1387 | 0 | counts->comp_ref[i]); |
1388 | | |
1389 | 172k | for (i = 0; i < BLOCK_SIZE_GROUPS; ++i) |
1390 | 138k | prob_diff_update(vp9_intra_mode_tree, cm->fc->y_mode_prob[i], |
1391 | 138k | counts->y_mode[i], INTRA_MODES, &header_bc); |
1392 | | |
1393 | 586k | for (i = 0; i < PARTITION_CONTEXTS; ++i) |
1394 | 552k | prob_diff_update(vp9_partition_tree, fc->partition_prob[i], |
1395 | 552k | counts->partition[i], PARTITION_TYPES, &header_bc); |
1396 | | |
1397 | 34.5k | vp9_write_nmv_probs(cm, cm->allow_high_precision_mv, &header_bc, |
1398 | 34.5k | &counts->mv); |
1399 | 34.5k | } |
1400 | | |
1401 | 42.2k | if (vpx_stop_encode(&header_bc)) { |
1402 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_ERROR, |
1403 | 0 | "write_compressed_header: output buffer full"); |
1404 | 0 | } |
1405 | | |
1406 | 42.2k | return header_bc.pos; |
1407 | 42.2k | } |
1408 | | |
1409 | | void vp9_pack_bitstream(VP9_COMP *cpi, uint8_t *dest, size_t dest_size, |
1410 | 42.2k | size_t *size) { |
1411 | 42.2k | VP9_COMMON *const cm = &cpi->common; |
1412 | 42.2k | uint8_t *data = dest; |
1413 | 42.2k | size_t data_size = dest_size; |
1414 | 42.2k | size_t uncompressed_hdr_size, compressed_hdr_size; |
1415 | 42.2k | struct vpx_write_bit_buffer wb; |
1416 | 42.2k | struct vpx_write_bit_buffer saved_wb; |
1417 | | |
1418 | | #if CONFIG_BITSTREAM_DEBUG |
1419 | | bitstream_queue_reset_write(); |
1420 | | #endif |
1421 | | |
1422 | 42.2k | vpx_wb_init(&wb, data, data_size); |
1423 | 42.2k | write_uncompressed_header(cpi, &wb); |
1424 | 42.2k | if (vpx_wb_has_error(&wb)) { |
1425 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_ERROR, |
1426 | 0 | "vp9_pack_bitstream: output buffer full"); |
1427 | 0 | } |
1428 | | |
1429 | | // Skip the rest coding process if use show existing frame. |
1430 | 42.2k | if (cm->show_existing_frame) { |
1431 | 0 | uncompressed_hdr_size = vpx_wb_bytes_written(&wb); |
1432 | 0 | data += uncompressed_hdr_size; |
1433 | 0 | *size = data - dest; |
1434 | 0 | return; |
1435 | 0 | } |
1436 | | |
1437 | 42.2k | saved_wb = wb; |
1438 | | // don't know in advance compressed header size |
1439 | 42.2k | vpx_wb_write_literal(&wb, 0, 16); |
1440 | 42.2k | if (vpx_wb_has_error(&wb)) { |
1441 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_ERROR, |
1442 | 0 | "vp9_pack_bitstream: output buffer full"); |
1443 | 0 | } |
1444 | | |
1445 | 42.2k | uncompressed_hdr_size = vpx_wb_bytes_written(&wb); |
1446 | 42.2k | data += uncompressed_hdr_size; |
1447 | 42.2k | data_size -= uncompressed_hdr_size; |
1448 | | |
1449 | 42.2k | vpx_clear_system_state(); |
1450 | | |
1451 | 42.2k | compressed_hdr_size = write_compressed_header(cpi, data, data_size); |
1452 | 42.2k | data += compressed_hdr_size; |
1453 | 42.2k | data_size -= compressed_hdr_size; |
1454 | 42.2k | if (compressed_hdr_size > UINT16_MAX) { |
1455 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_ERROR, |
1456 | 0 | "compressed_hdr_size > 16 bits"); |
1457 | 0 | } |
1458 | 42.2k | vpx_wb_write_literal(&saved_wb, (int)compressed_hdr_size, 16); |
1459 | 42.2k | assert(!vpx_wb_has_error(&saved_wb)); |
1460 | | |
1461 | 42.2k | data += encode_tiles(cpi, data, data_size); |
1462 | | |
1463 | 42.2k | *size = data - dest; |
1464 | 42.2k | } |