Line data Source code
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 "vp8/common/header.h"
12 : #include "encodemv.h"
13 : #include "vp8/common/entropymode.h"
14 : #include "vp8/common/findnearmv.h"
15 : #include "mcomp.h"
16 : #include "vp8/common/systemdependent.h"
17 : #include <assert.h>
18 : #include <stdio.h>
19 : #include <limits.h>
20 : #include "vpx/vpx_encoder.h"
21 : #include "vpx_mem/vpx_mem.h"
22 : #include "vpx_ports/system_state.h"
23 : #include "bitstream.h"
24 :
25 : #include "defaultcoefcounts.h"
26 : #include "vp8/common/common.h"
27 :
28 : const int vp8cx_base_skip_false_prob[128] = {
29 : 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
30 : 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
31 : 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
32 : 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 251, 248, 244, 240,
33 : 236, 232, 229, 225, 221, 217, 213, 208, 204, 199, 194, 190, 187, 183, 179,
34 : 175, 172, 168, 164, 160, 157, 153, 149, 145, 142, 138, 134, 130, 127, 124,
35 : 120, 117, 114, 110, 107, 104, 101, 98, 95, 92, 89, 86, 83, 80, 77,
36 : 74, 71, 68, 65, 62, 59, 56, 53, 50, 47, 44, 41, 38, 35, 32,
37 : 30, 28, 26, 24, 22, 20, 18, 16,
38 : };
39 :
40 : #if defined(SECTIONBITS_OUTPUT)
41 : unsigned __int64 Sectionbits[500];
42 : #endif
43 :
44 : #ifdef VP8_ENTROPY_STATS
45 : int intra_mode_stats[10][10][10];
46 : static unsigned int tree_update_hist[BLOCK_TYPES][COEF_BANDS]
47 : [PREV_COEF_CONTEXTS][ENTROPY_NODES][2];
48 : extern unsigned int active_section;
49 : #endif
50 :
51 : #ifdef MODE_STATS
52 : int count_mb_seg[4] = { 0, 0, 0, 0 };
53 : #endif
54 :
55 0 : static void update_mode(vp8_writer *const w, int n, vp8_token tok[/* n */],
56 : vp8_tree tree, vp8_prob Pnew[/* n-1 */],
57 : vp8_prob Pcur[/* n-1 */],
58 : unsigned int bct[/* n-1 */][2],
59 : const unsigned int num_events[/* n */]) {
60 0 : unsigned int new_b = 0, old_b = 0;
61 0 : int i = 0;
62 :
63 0 : vp8_tree_probs_from_distribution(n--, tok, tree, Pnew, bct, num_events, 256,
64 : 1);
65 :
66 : do {
67 0 : new_b += vp8_cost_branch(bct[i], Pnew[i]);
68 0 : old_b += vp8_cost_branch(bct[i], Pcur[i]);
69 0 : } while (++i < n);
70 :
71 0 : if (new_b + (n << 8) < old_b) {
72 0 : int j = 0;
73 :
74 0 : vp8_write_bit(w, 1);
75 :
76 : do {
77 0 : const vp8_prob p = Pnew[j];
78 :
79 0 : vp8_write_literal(w, Pcur[j] = p ? p : 1, 8);
80 0 : } while (++j < n);
81 : } else
82 0 : vp8_write_bit(w, 0);
83 0 : }
84 :
85 0 : static void update_mbintra_mode_probs(VP8_COMP *cpi) {
86 0 : VP8_COMMON *const x = &cpi->common;
87 :
88 0 : vp8_writer *const w = cpi->bc;
89 :
90 : {
91 : vp8_prob Pnew[VP8_YMODES - 1];
92 : unsigned int bct[VP8_YMODES - 1][2];
93 :
94 0 : update_mode(w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree, Pnew,
95 0 : x->fc.ymode_prob, bct, (unsigned int *)cpi->mb.ymode_count);
96 : }
97 : {
98 : vp8_prob Pnew[VP8_UV_MODES - 1];
99 : unsigned int bct[VP8_UV_MODES - 1][2];
100 :
101 0 : update_mode(w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree, Pnew,
102 0 : x->fc.uv_mode_prob, bct, (unsigned int *)cpi->mb.uv_mode_count);
103 : }
104 0 : }
105 :
106 0 : static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p) {
107 0 : vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m);
108 0 : }
109 :
110 0 : static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p) {
111 0 : vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m);
112 0 : }
113 :
114 0 : static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p) {
115 0 : vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m);
116 0 : }
117 :
118 0 : static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p) {
119 0 : vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m);
120 0 : }
121 :
122 0 : static void write_split(vp8_writer *bc, int x) {
123 0 : vp8_write_token(bc, vp8_mbsplit_tree, vp8_mbsplit_probs,
124 0 : vp8_mbsplit_encodings + x);
125 0 : }
126 :
127 0 : void vp8_pack_tokens(vp8_writer *w, const TOKENEXTRA *p, int xcount) {
128 0 : const TOKENEXTRA *stop = p + xcount;
129 : unsigned int split;
130 : int shift;
131 0 : int count = w->count;
132 0 : unsigned int range = w->range;
133 0 : unsigned int lowvalue = w->lowvalue;
134 :
135 0 : while (p < stop) {
136 0 : const int t = p->Token;
137 0 : vp8_token *a = vp8_coef_encodings + t;
138 0 : const vp8_extra_bit_struct *b = vp8_extra_bits + t;
139 0 : int i = 0;
140 0 : const unsigned char *pp = p->context_tree;
141 0 : int v = a->value;
142 0 : int n = a->Len;
143 :
144 0 : if (p->skip_eob_node) {
145 0 : n--;
146 0 : i = 2;
147 : }
148 :
149 : do {
150 0 : const int bb = (v >> --n) & 1;
151 0 : split = 1 + (((range - 1) * pp[i >> 1]) >> 8);
152 0 : i = vp8_coef_tree[i + bb];
153 :
154 0 : if (bb) {
155 0 : lowvalue += split;
156 0 : range = range - split;
157 : } else {
158 0 : range = split;
159 : }
160 :
161 0 : shift = vp8_norm[range];
162 0 : range <<= shift;
163 0 : count += shift;
164 :
165 0 : if (count >= 0) {
166 0 : int offset = shift - count;
167 :
168 0 : if ((lowvalue << (offset - 1)) & 0x80000000) {
169 0 : int x = w->pos - 1;
170 :
171 0 : while (x >= 0 && w->buffer[x] == 0xff) {
172 0 : w->buffer[x] = (unsigned char)0;
173 0 : x--;
174 : }
175 :
176 0 : w->buffer[x] += 1;
177 : }
178 :
179 0 : validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
180 :
181 0 : w->buffer[w->pos++] = (lowvalue >> (24 - offset));
182 0 : lowvalue <<= offset;
183 0 : shift = count;
184 0 : lowvalue &= 0xffffff;
185 0 : count -= 8;
186 : }
187 :
188 0 : lowvalue <<= shift;
189 0 : } while (n);
190 :
191 0 : if (b->base_val) {
192 0 : const int e = p->Extra, L = b->Len;
193 :
194 0 : if (L) {
195 0 : const unsigned char *proba = b->prob;
196 0 : const int v2 = e >> 1;
197 0 : int n2 = L; /* number of bits in v2, assumed nonzero */
198 0 : i = 0;
199 :
200 : do {
201 0 : const int bb = (v2 >> --n2) & 1;
202 0 : split = 1 + (((range - 1) * proba[i >> 1]) >> 8);
203 0 : i = b->tree[i + bb];
204 :
205 0 : if (bb) {
206 0 : lowvalue += split;
207 0 : range = range - split;
208 : } else {
209 0 : range = split;
210 : }
211 :
212 0 : shift = vp8_norm[range];
213 0 : range <<= shift;
214 0 : count += shift;
215 :
216 0 : if (count >= 0) {
217 0 : int offset = shift - count;
218 :
219 0 : if ((lowvalue << (offset - 1)) & 0x80000000) {
220 0 : int x = w->pos - 1;
221 :
222 0 : while (x >= 0 && w->buffer[x] == 0xff) {
223 0 : w->buffer[x] = (unsigned char)0;
224 0 : x--;
225 : }
226 :
227 0 : w->buffer[x] += 1;
228 : }
229 :
230 0 : validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
231 :
232 0 : w->buffer[w->pos++] = (lowvalue >> (24 - offset));
233 0 : lowvalue <<= offset;
234 0 : shift = count;
235 0 : lowvalue &= 0xffffff;
236 0 : count -= 8;
237 : }
238 :
239 0 : lowvalue <<= shift;
240 0 : } while (n2);
241 : }
242 :
243 : {
244 0 : split = (range + 1) >> 1;
245 :
246 0 : if (e & 1) {
247 0 : lowvalue += split;
248 0 : range = range - split;
249 : } else {
250 0 : range = split;
251 : }
252 :
253 0 : range <<= 1;
254 :
255 0 : if ((lowvalue & 0x80000000)) {
256 0 : int x = w->pos - 1;
257 :
258 0 : while (x >= 0 && w->buffer[x] == 0xff) {
259 0 : w->buffer[x] = (unsigned char)0;
260 0 : x--;
261 : }
262 :
263 0 : w->buffer[x] += 1;
264 : }
265 :
266 0 : lowvalue <<= 1;
267 :
268 0 : if (!++count) {
269 0 : count = -8;
270 :
271 0 : validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
272 :
273 0 : w->buffer[w->pos++] = (lowvalue >> 24);
274 0 : lowvalue &= 0xffffff;
275 : }
276 : }
277 : }
278 :
279 0 : ++p;
280 : }
281 :
282 0 : w->count = count;
283 0 : w->lowvalue = lowvalue;
284 0 : w->range = range;
285 0 : }
286 :
287 0 : static void write_partition_size(unsigned char *cx_data, int size) {
288 : signed char csize;
289 :
290 0 : csize = size & 0xff;
291 0 : *cx_data = csize;
292 0 : csize = (size >> 8) & 0xff;
293 0 : *(cx_data + 1) = csize;
294 0 : csize = (size >> 16) & 0xff;
295 0 : *(cx_data + 2) = csize;
296 0 : }
297 :
298 0 : static void pack_tokens_into_partitions(VP8_COMP *cpi, unsigned char *cx_data,
299 : unsigned char *cx_data_end,
300 : int num_part) {
301 : int i;
302 0 : unsigned char *ptr = cx_data;
303 0 : unsigned char *ptr_end = cx_data_end;
304 : vp8_writer *w;
305 :
306 0 : for (i = 0; i < num_part; ++i) {
307 : int mb_row;
308 :
309 0 : w = cpi->bc + i + 1;
310 :
311 0 : vp8_start_encode(w, ptr, ptr_end);
312 :
313 0 : for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part) {
314 0 : const TOKENEXTRA *p = cpi->tplist[mb_row].start;
315 0 : const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
316 0 : int tokens = (int)(stop - p);
317 :
318 0 : vp8_pack_tokens(w, p, tokens);
319 : }
320 :
321 0 : vp8_stop_encode(w);
322 0 : ptr += w->pos;
323 : }
324 0 : }
325 :
326 : #if CONFIG_MULTITHREAD
327 0 : static void pack_mb_row_tokens(VP8_COMP *cpi, vp8_writer *w) {
328 : int mb_row;
329 :
330 0 : for (mb_row = 0; mb_row < cpi->common.mb_rows; ++mb_row) {
331 0 : const TOKENEXTRA *p = cpi->tplist[mb_row].start;
332 0 : const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
333 0 : int tokens = (int)(stop - p);
334 :
335 0 : vp8_pack_tokens(w, p, tokens);
336 : }
337 0 : }
338 : #endif // CONFIG_MULTITHREAD
339 :
340 0 : static void write_mv_ref(vp8_writer *w, MB_PREDICTION_MODE m,
341 : const vp8_prob *p) {
342 0 : assert(NEARESTMV <= m && m <= SPLITMV);
343 0 : vp8_write_token(w, vp8_mv_ref_tree, p,
344 0 : vp8_mv_ref_encoding_array + (m - NEARESTMV));
345 0 : }
346 :
347 0 : static void write_sub_mv_ref(vp8_writer *w, B_PREDICTION_MODE m,
348 : const vp8_prob *p) {
349 0 : assert(LEFT4X4 <= m && m <= NEW4X4);
350 0 : vp8_write_token(w, vp8_sub_mv_ref_tree, p,
351 0 : vp8_sub_mv_ref_encoding_array + (m - LEFT4X4));
352 0 : }
353 :
354 0 : static void write_mv(vp8_writer *w, const MV *mv, const int_mv *ref,
355 : const MV_CONTEXT *mvc) {
356 : MV e;
357 0 : e.row = mv->row - ref->as_mv.row;
358 0 : e.col = mv->col - ref->as_mv.col;
359 :
360 0 : vp8_encode_motion_vector(w, &e, mvc);
361 0 : }
362 :
363 0 : static void write_mb_features(vp8_writer *w, const MB_MODE_INFO *mi,
364 : const MACROBLOCKD *x) {
365 : /* Encode the MB segment id. */
366 0 : if (x->segmentation_enabled && x->update_mb_segmentation_map) {
367 0 : switch (mi->segment_id) {
368 : case 0:
369 0 : vp8_write(w, 0, x->mb_segment_tree_probs[0]);
370 0 : vp8_write(w, 0, x->mb_segment_tree_probs[1]);
371 0 : break;
372 : case 1:
373 0 : vp8_write(w, 0, x->mb_segment_tree_probs[0]);
374 0 : vp8_write(w, 1, x->mb_segment_tree_probs[1]);
375 0 : break;
376 : case 2:
377 0 : vp8_write(w, 1, x->mb_segment_tree_probs[0]);
378 0 : vp8_write(w, 0, x->mb_segment_tree_probs[2]);
379 0 : break;
380 : case 3:
381 0 : vp8_write(w, 1, x->mb_segment_tree_probs[0]);
382 0 : vp8_write(w, 1, x->mb_segment_tree_probs[2]);
383 0 : break;
384 :
385 : /* TRAP.. This should not happen */
386 : default:
387 0 : vp8_write(w, 0, x->mb_segment_tree_probs[0]);
388 0 : vp8_write(w, 0, x->mb_segment_tree_probs[1]);
389 0 : break;
390 : }
391 : }
392 0 : }
393 0 : void vp8_convert_rfct_to_prob(VP8_COMP *const cpi) {
394 0 : const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
395 0 : const int rf_intra = rfct[INTRA_FRAME];
396 0 : const int rf_inter =
397 0 : rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
398 :
399 : /* Calculate the probabilities used to code the ref frame based on usage */
400 0 : if (!(cpi->prob_intra_coded = rf_intra * 255 / (rf_intra + rf_inter))) {
401 0 : cpi->prob_intra_coded = 1;
402 : }
403 :
404 0 : cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
405 :
406 0 : if (!cpi->prob_last_coded) cpi->prob_last_coded = 1;
407 :
408 0 : cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
409 0 : ? (rfct[GOLDEN_FRAME] * 255) /
410 0 : (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
411 0 : : 128;
412 :
413 0 : if (!cpi->prob_gf_coded) cpi->prob_gf_coded = 1;
414 0 : }
415 :
416 0 : static void pack_inter_mode_mvs(VP8_COMP *const cpi) {
417 0 : VP8_COMMON *const pc = &cpi->common;
418 0 : vp8_writer *const w = cpi->bc;
419 0 : const MV_CONTEXT *mvc = pc->fc.mvc;
420 :
421 0 : MODE_INFO *m = pc->mi;
422 0 : const int mis = pc->mode_info_stride;
423 0 : int mb_row = -1;
424 :
425 0 : int prob_skip_false = 0;
426 :
427 0 : cpi->mb.partition_info = cpi->mb.pi;
428 :
429 0 : vp8_convert_rfct_to_prob(cpi);
430 :
431 : #ifdef VP8_ENTROPY_STATS
432 : active_section = 1;
433 : #endif
434 :
435 0 : if (pc->mb_no_coeff_skip) {
436 0 : int total_mbs = pc->mb_rows * pc->mb_cols;
437 :
438 0 : prob_skip_false = (total_mbs - cpi->mb.skip_true_count) * 256 / total_mbs;
439 :
440 0 : if (prob_skip_false <= 1) prob_skip_false = 1;
441 :
442 0 : if (prob_skip_false > 255) prob_skip_false = 255;
443 :
444 0 : cpi->prob_skip_false = prob_skip_false;
445 0 : vp8_write_literal(w, prob_skip_false, 8);
446 : }
447 :
448 0 : vp8_write_literal(w, cpi->prob_intra_coded, 8);
449 0 : vp8_write_literal(w, cpi->prob_last_coded, 8);
450 0 : vp8_write_literal(w, cpi->prob_gf_coded, 8);
451 :
452 0 : update_mbintra_mode_probs(cpi);
453 :
454 0 : vp8_write_mvprobs(cpi);
455 :
456 0 : while (++mb_row < pc->mb_rows) {
457 0 : int mb_col = -1;
458 :
459 0 : while (++mb_col < pc->mb_cols) {
460 0 : const MB_MODE_INFO *const mi = &m->mbmi;
461 0 : const MV_REFERENCE_FRAME rf = mi->ref_frame;
462 0 : const MB_PREDICTION_MODE mode = mi->mode;
463 :
464 0 : MACROBLOCKD *xd = &cpi->mb.e_mbd;
465 :
466 : /* Distance of Mb to the various image edges.
467 : * These specified to 8th pel as they are always compared to MV
468 : * values that are in 1/8th pel units
469 : */
470 0 : xd->mb_to_left_edge = -((mb_col * 16) << 3);
471 0 : xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
472 0 : xd->mb_to_top_edge = -((mb_row * 16) << 3);
473 0 : xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
474 :
475 : #ifdef VP8_ENTROPY_STATS
476 : active_section = 9;
477 : #endif
478 :
479 0 : if (cpi->mb.e_mbd.update_mb_segmentation_map) {
480 0 : write_mb_features(w, mi, &cpi->mb.e_mbd);
481 : }
482 :
483 0 : if (pc->mb_no_coeff_skip) {
484 0 : vp8_encode_bool(w, m->mbmi.mb_skip_coeff, prob_skip_false);
485 : }
486 :
487 0 : if (rf == INTRA_FRAME) {
488 0 : vp8_write(w, 0, cpi->prob_intra_coded);
489 : #ifdef VP8_ENTROPY_STATS
490 : active_section = 6;
491 : #endif
492 0 : write_ymode(w, mode, pc->fc.ymode_prob);
493 :
494 0 : if (mode == B_PRED) {
495 0 : int j = 0;
496 :
497 : do {
498 0 : write_bmode(w, m->bmi[j].as_mode, pc->fc.bmode_prob);
499 0 : } while (++j < 16);
500 : }
501 :
502 0 : write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob);
503 : } else /* inter coded */
504 : {
505 : int_mv best_mv;
506 : vp8_prob mv_ref_p[VP8_MVREFS - 1];
507 :
508 0 : vp8_write(w, 1, cpi->prob_intra_coded);
509 :
510 0 : if (rf == LAST_FRAME)
511 0 : vp8_write(w, 0, cpi->prob_last_coded);
512 : else {
513 0 : vp8_write(w, 1, cpi->prob_last_coded);
514 0 : vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, cpi->prob_gf_coded);
515 : }
516 :
517 : {
518 : int_mv n1, n2;
519 : int ct[4];
520 :
521 0 : vp8_find_near_mvs(xd, m, &n1, &n2, &best_mv, ct, rf,
522 0 : cpi->common.ref_frame_sign_bias);
523 0 : vp8_clamp_mv2(&best_mv, xd);
524 :
525 0 : vp8_mv_ref_probs(mv_ref_p, ct);
526 :
527 : #ifdef VP8_ENTROPY_STATS
528 : accum_mv_refs(mode, ct);
529 : #endif
530 : }
531 :
532 : #ifdef VP8_ENTROPY_STATS
533 : active_section = 3;
534 : #endif
535 :
536 0 : write_mv_ref(w, mode, mv_ref_p);
537 :
538 0 : switch (mode) /* new, split require MVs */
539 : {
540 : case NEWMV:
541 :
542 : #ifdef VP8_ENTROPY_STATS
543 : active_section = 5;
544 : #endif
545 :
546 0 : write_mv(w, &mi->mv.as_mv, &best_mv, mvc);
547 0 : break;
548 :
549 : case SPLITMV: {
550 0 : int j = 0;
551 :
552 : #ifdef MODE_STATS
553 : ++count_mb_seg[mi->partitioning];
554 : #endif
555 :
556 0 : write_split(w, mi->partitioning);
557 :
558 : do {
559 : B_PREDICTION_MODE blockmode;
560 : int_mv blockmv;
561 0 : const int *const L = vp8_mbsplits[mi->partitioning];
562 0 : int k = -1; /* first block in subset j */
563 : int mv_contz;
564 : int_mv leftmv, abovemv;
565 :
566 0 : blockmode = cpi->mb.partition_info->bmi[j].mode;
567 0 : blockmv = cpi->mb.partition_info->bmi[j].mv;
568 0 : while (j != L[++k]) {
569 0 : assert(k < 16);
570 : }
571 0 : leftmv.as_int = left_block_mv(m, k);
572 0 : abovemv.as_int = above_block_mv(m, k, mis);
573 0 : mv_contz = vp8_mv_cont(&leftmv, &abovemv);
574 :
575 0 : write_sub_mv_ref(w, blockmode, vp8_sub_mv_ref_prob2[mv_contz]);
576 :
577 0 : if (blockmode == NEW4X4) {
578 : #ifdef VP8_ENTROPY_STATS
579 : active_section = 11;
580 : #endif
581 0 : write_mv(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT *)mvc);
582 : }
583 0 : } while (++j < cpi->mb.partition_info->count);
584 0 : break;
585 : }
586 0 : default: break;
587 : }
588 : }
589 :
590 0 : ++m;
591 0 : cpi->mb.partition_info++;
592 : }
593 :
594 0 : ++m; /* skip L prediction border */
595 0 : cpi->mb.partition_info++;
596 : }
597 0 : }
598 :
599 0 : static void write_kfmodes(VP8_COMP *cpi) {
600 0 : vp8_writer *const bc = cpi->bc;
601 0 : const VP8_COMMON *const c = &cpi->common;
602 : /* const */
603 0 : MODE_INFO *m = c->mi;
604 :
605 0 : int mb_row = -1;
606 0 : int prob_skip_false = 0;
607 :
608 0 : if (c->mb_no_coeff_skip) {
609 0 : int total_mbs = c->mb_rows * c->mb_cols;
610 :
611 0 : prob_skip_false = (total_mbs - cpi->mb.skip_true_count) * 256 / total_mbs;
612 :
613 0 : if (prob_skip_false <= 1) prob_skip_false = 1;
614 :
615 0 : if (prob_skip_false >= 255) prob_skip_false = 255;
616 :
617 0 : cpi->prob_skip_false = prob_skip_false;
618 0 : vp8_write_literal(bc, prob_skip_false, 8);
619 : }
620 :
621 0 : while (++mb_row < c->mb_rows) {
622 0 : int mb_col = -1;
623 :
624 0 : while (++mb_col < c->mb_cols) {
625 0 : const int ym = m->mbmi.mode;
626 :
627 0 : if (cpi->mb.e_mbd.update_mb_segmentation_map) {
628 0 : write_mb_features(bc, &m->mbmi, &cpi->mb.e_mbd);
629 : }
630 :
631 0 : if (c->mb_no_coeff_skip) {
632 0 : vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false);
633 : }
634 :
635 0 : kfwrite_ymode(bc, ym, vp8_kf_ymode_prob);
636 :
637 0 : if (ym == B_PRED) {
638 0 : const int mis = c->mode_info_stride;
639 0 : int i = 0;
640 :
641 : do {
642 0 : const B_PREDICTION_MODE A = above_block_mode(m, i, mis);
643 0 : const B_PREDICTION_MODE L = left_block_mode(m, i);
644 0 : const int bm = m->bmi[i].as_mode;
645 :
646 : #ifdef VP8_ENTROPY_STATS
647 : ++intra_mode_stats[A][L][bm];
648 : #endif
649 :
650 0 : write_bmode(bc, bm, vp8_kf_bmode_prob[A][L]);
651 0 : } while (++i < 16);
652 : }
653 :
654 0 : write_uv_mode(bc, (m++)->mbmi.uv_mode, vp8_kf_uv_mode_prob);
655 : }
656 :
657 0 : m++; /* skip L prediction border */
658 : }
659 0 : }
660 :
661 : #if 0
662 : /* This function is used for debugging probability trees. */
663 : static void print_prob_tree(vp8_prob
664 : coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES])
665 : {
666 : /* print coef probability tree */
667 : int i,j,k,l;
668 : FILE* f = fopen("enc_tree_probs.txt", "a");
669 : fprintf(f, "{\n");
670 : for (i = 0; i < BLOCK_TYPES; ++i)
671 : {
672 : fprintf(f, " {\n");
673 : for (j = 0; j < COEF_BANDS; ++j)
674 : {
675 : fprintf(f, " {\n");
676 : for (k = 0; k < PREV_COEF_CONTEXTS; ++k)
677 : {
678 : fprintf(f, " {");
679 : for (l = 0; l < ENTROPY_NODES; ++l)
680 : {
681 : fprintf(f, "%3u, ",
682 : (unsigned int)(coef_probs [i][j][k][l]));
683 : }
684 : fprintf(f, " }\n");
685 : }
686 : fprintf(f, " }\n");
687 : }
688 : fprintf(f, " }\n");
689 : }
690 : fprintf(f, "}\n");
691 : fclose(f);
692 : }
693 : #endif
694 :
695 0 : static void sum_probs_over_prev_coef_context(
696 : const unsigned int probs[PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
697 : unsigned int *out) {
698 : int i, j;
699 0 : for (i = 0; i < MAX_ENTROPY_TOKENS; ++i) {
700 0 : for (j = 0; j < PREV_COEF_CONTEXTS; ++j) {
701 0 : const unsigned int tmp = out[i];
702 0 : out[i] += probs[j][i];
703 : /* check for wrap */
704 0 : if (out[i] < tmp) out[i] = UINT_MAX;
705 : }
706 : }
707 0 : }
708 :
709 0 : static int prob_update_savings(const unsigned int *ct, const vp8_prob oldp,
710 : const vp8_prob newp, const vp8_prob upd) {
711 0 : const int old_b = vp8_cost_branch(ct, oldp);
712 0 : const int new_b = vp8_cost_branch(ct, newp);
713 0 : const int update_b = 8 + ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);
714 :
715 0 : return old_b - new_b - update_b;
716 : }
717 :
718 0 : static int independent_coef_context_savings(VP8_COMP *cpi) {
719 0 : MACROBLOCK *const x = &cpi->mb;
720 0 : int savings = 0;
721 0 : int i = 0;
722 : do {
723 0 : int j = 0;
724 : do {
725 0 : int k = 0;
726 0 : unsigned int prev_coef_count_sum[MAX_ENTROPY_TOKENS] = { 0 };
727 0 : int prev_coef_savings[MAX_ENTROPY_TOKENS] = { 0 };
728 : const unsigned int(*probs)[MAX_ENTROPY_TOKENS];
729 : /* Calculate new probabilities given the constraint that
730 : * they must be equal over the prev coef contexts
731 : */
732 :
733 0 : probs = (const unsigned int(*)[MAX_ENTROPY_TOKENS])x->coef_counts[i][j];
734 :
735 : /* Reset to default probabilities at key frames */
736 0 : if (cpi->common.frame_type == KEY_FRAME) {
737 0 : probs = default_coef_counts[i][j];
738 : }
739 :
740 0 : sum_probs_over_prev_coef_context(probs, prev_coef_count_sum);
741 :
742 : do {
743 : /* at every context */
744 :
745 : /* calc probs and branch cts for this frame only */
746 0 : int t = 0; /* token/prob index */
747 :
748 0 : vp8_tree_probs_from_distribution(
749 : MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
750 0 : cpi->frame_coef_probs[i][j][k], cpi->frame_branch_ct[i][j][k],
751 : prev_coef_count_sum, 256, 1);
752 :
753 : do {
754 0 : const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
755 0 : const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
756 0 : const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
757 0 : const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
758 0 : const int s = prob_update_savings(ct, oldp, newp, upd);
759 :
760 0 : if (cpi->common.frame_type != KEY_FRAME ||
761 0 : (cpi->common.frame_type == KEY_FRAME && newp != oldp)) {
762 0 : prev_coef_savings[t] += s;
763 : }
764 0 : } while (++t < ENTROPY_NODES);
765 0 : } while (++k < PREV_COEF_CONTEXTS);
766 0 : k = 0;
767 : do {
768 : /* We only update probabilities if we can save bits, except
769 : * for key frames where we have to update all probabilities
770 : * to get the equal probabilities across the prev coef
771 : * contexts.
772 : */
773 0 : if (prev_coef_savings[k] > 0 || cpi->common.frame_type == KEY_FRAME) {
774 0 : savings += prev_coef_savings[k];
775 : }
776 0 : } while (++k < ENTROPY_NODES);
777 0 : } while (++j < COEF_BANDS);
778 0 : } while (++i < BLOCK_TYPES);
779 0 : return savings;
780 : }
781 :
782 0 : static int default_coef_context_savings(VP8_COMP *cpi) {
783 0 : MACROBLOCK *const x = &cpi->mb;
784 0 : int savings = 0;
785 0 : int i = 0;
786 : do {
787 0 : int j = 0;
788 : do {
789 0 : int k = 0;
790 : do {
791 : /* at every context */
792 :
793 : /* calc probs and branch cts for this frame only */
794 0 : int t = 0; /* token/prob index */
795 :
796 0 : vp8_tree_probs_from_distribution(
797 : MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
798 0 : cpi->frame_coef_probs[i][j][k], cpi->frame_branch_ct[i][j][k],
799 0 : x->coef_counts[i][j][k], 256, 1);
800 :
801 : do {
802 0 : const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
803 0 : const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
804 0 : const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
805 0 : const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
806 0 : const int s = prob_update_savings(ct, oldp, newp, upd);
807 :
808 0 : if (s > 0) {
809 0 : savings += s;
810 : }
811 0 : } while (++t < ENTROPY_NODES);
812 0 : } while (++k < PREV_COEF_CONTEXTS);
813 0 : } while (++j < COEF_BANDS);
814 0 : } while (++i < BLOCK_TYPES);
815 0 : return savings;
816 : }
817 :
818 0 : void vp8_calc_ref_frame_costs(int *ref_frame_cost, int prob_intra,
819 : int prob_last, int prob_garf) {
820 0 : assert(prob_intra >= 0);
821 0 : assert(prob_intra <= 255);
822 0 : assert(prob_last >= 0);
823 0 : assert(prob_last <= 255);
824 0 : assert(prob_garf >= 0);
825 0 : assert(prob_garf <= 255);
826 0 : ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(prob_intra);
827 0 : ref_frame_cost[LAST_FRAME] =
828 0 : vp8_cost_one(prob_intra) + vp8_cost_zero(prob_last);
829 0 : ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(prob_intra) +
830 0 : vp8_cost_one(prob_last) +
831 0 : vp8_cost_zero(prob_garf);
832 0 : ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(prob_intra) +
833 0 : vp8_cost_one(prob_last) +
834 0 : vp8_cost_one(prob_garf);
835 0 : }
836 :
837 0 : int vp8_estimate_entropy_savings(VP8_COMP *cpi) {
838 0 : int savings = 0;
839 :
840 0 : const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
841 0 : const int rf_intra = rfct[INTRA_FRAME];
842 0 : const int rf_inter =
843 0 : rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
844 : int new_intra, new_last, new_garf, oldtotal, newtotal;
845 : int ref_frame_cost[MAX_REF_FRAMES];
846 :
847 0 : vpx_clear_system_state();
848 :
849 0 : if (cpi->common.frame_type != KEY_FRAME) {
850 0 : if (!(new_intra = rf_intra * 255 / (rf_intra + rf_inter))) new_intra = 1;
851 :
852 0 : new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
853 :
854 0 : new_garf = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
855 0 : ? (rfct[GOLDEN_FRAME] * 255) /
856 0 : (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
857 0 : : 128;
858 :
859 0 : vp8_calc_ref_frame_costs(ref_frame_cost, new_intra, new_last, new_garf);
860 :
861 0 : newtotal = rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
862 0 : rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
863 0 : rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
864 0 : rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
865 :
866 : /* old costs */
867 0 : vp8_calc_ref_frame_costs(ref_frame_cost, cpi->prob_intra_coded,
868 : cpi->prob_last_coded, cpi->prob_gf_coded);
869 :
870 0 : oldtotal = rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
871 0 : rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
872 0 : rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
873 0 : rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
874 :
875 0 : savings += (oldtotal - newtotal) / 256;
876 : }
877 :
878 0 : if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
879 0 : savings += independent_coef_context_savings(cpi);
880 : } else {
881 0 : savings += default_coef_context_savings(cpi);
882 : }
883 :
884 0 : return savings;
885 : }
886 :
887 : #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
888 : int vp8_update_coef_context(VP8_COMP *cpi) {
889 : int savings = 0;
890 :
891 : if (cpi->common.frame_type == KEY_FRAME) {
892 : /* Reset to default counts/probabilities at key frames */
893 : vp8_copy(cpi->mb.coef_counts, default_coef_counts);
894 : }
895 :
896 : if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
897 : savings += independent_coef_context_savings(cpi);
898 : else
899 : savings += default_coef_context_savings(cpi);
900 :
901 : return savings;
902 : }
903 : #endif
904 :
905 0 : void vp8_update_coef_probs(VP8_COMP *cpi) {
906 0 : int i = 0;
907 : #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
908 0 : vp8_writer *const w = cpi->bc;
909 : #endif
910 0 : int savings = 0;
911 :
912 0 : vpx_clear_system_state();
913 :
914 : do {
915 0 : int j = 0;
916 :
917 : do {
918 0 : int k = 0;
919 0 : int prev_coef_savings[ENTROPY_NODES] = { 0 };
920 0 : if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
921 0 : for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
922 : int t; /* token/prob index */
923 0 : for (t = 0; t < ENTROPY_NODES; ++t) {
924 0 : const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
925 0 : const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
926 0 : const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
927 0 : const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
928 :
929 0 : prev_coef_savings[t] += prob_update_savings(ct, oldp, newp, upd);
930 : }
931 : }
932 0 : k = 0;
933 : }
934 : do {
935 : /* note: use result from vp8_estimate_entropy_savings, so no
936 : * need to call vp8_tree_probs_from_distribution here.
937 : */
938 :
939 : /* at every context */
940 :
941 : /* calc probs and branch cts for this frame only */
942 0 : int t = 0; /* token/prob index */
943 :
944 : do {
945 0 : const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
946 :
947 0 : vp8_prob *Pold = cpi->common.fc.coef_probs[i][j][k] + t;
948 0 : const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
949 :
950 0 : int s = prev_coef_savings[t];
951 0 : int u = 0;
952 :
953 0 : if (!(cpi->oxcf.error_resilient_mode &
954 : VPX_ERROR_RESILIENT_PARTITIONS)) {
955 0 : s = prob_update_savings(cpi->frame_branch_ct[i][j][k][t], *Pold,
956 : newp, upd);
957 : }
958 :
959 0 : if (s > 0) u = 1;
960 :
961 : /* Force updates on key frames if the new is different,
962 : * so that we can be sure we end up with equal probabilities
963 : * over the prev coef contexts.
964 : */
965 0 : if ((cpi->oxcf.error_resilient_mode &
966 0 : VPX_ERROR_RESILIENT_PARTITIONS) &&
967 0 : cpi->common.frame_type == KEY_FRAME && newp != *Pold) {
968 0 : u = 1;
969 : }
970 :
971 : #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
972 : cpi->update_probs[i][j][k][t] = u;
973 : #else
974 0 : vp8_write(w, u, upd);
975 : #endif
976 :
977 : #ifdef VP8_ENTROPY_STATS
978 : ++tree_update_hist[i][j][k][t][u];
979 : #endif
980 :
981 0 : if (u) {
982 : /* send/use new probability */
983 :
984 0 : *Pold = newp;
985 : #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
986 0 : vp8_write_literal(w, newp, 8);
987 : #endif
988 :
989 0 : savings += s;
990 : }
991 :
992 0 : } while (++t < ENTROPY_NODES);
993 :
994 : /* Accum token counts for generation of default statistics */
995 : #ifdef VP8_ENTROPY_STATS
996 : t = 0;
997 :
998 : do {
999 : context_counters[i][j][k][t] += cpi->coef_counts[i][j][k][t];
1000 : } while (++t < MAX_ENTROPY_TOKENS);
1001 :
1002 : #endif
1003 :
1004 0 : } while (++k < PREV_COEF_CONTEXTS);
1005 0 : } while (++j < COEF_BANDS);
1006 0 : } while (++i < BLOCK_TYPES);
1007 0 : }
1008 :
1009 : #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1010 : static void pack_coef_probs(VP8_COMP *cpi) {
1011 : int i = 0;
1012 : vp8_writer *const w = cpi->bc;
1013 :
1014 : do {
1015 : int j = 0;
1016 :
1017 : do {
1018 : int k = 0;
1019 :
1020 : do {
1021 : int t = 0; /* token/prob index */
1022 :
1023 : do {
1024 : const vp8_prob newp = cpi->common.fc.coef_probs[i][j][k][t];
1025 : const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
1026 :
1027 : const char u = cpi->update_probs[i][j][k][t];
1028 :
1029 : vp8_write(w, u, upd);
1030 :
1031 : if (u) {
1032 : /* send/use new probability */
1033 : vp8_write_literal(w, newp, 8);
1034 : }
1035 : } while (++t < ENTROPY_NODES);
1036 : } while (++k < PREV_COEF_CONTEXTS);
1037 : } while (++j < COEF_BANDS);
1038 : } while (++i < BLOCK_TYPES);
1039 : }
1040 : #endif
1041 :
1042 : #ifdef PACKET_TESTING
1043 : FILE *vpxlogc = 0;
1044 : #endif
1045 :
1046 0 : static void put_delta_q(vp8_writer *bc, int delta_q) {
1047 0 : if (delta_q != 0) {
1048 0 : vp8_write_bit(bc, 1);
1049 0 : vp8_write_literal(bc, abs(delta_q), 4);
1050 :
1051 0 : if (delta_q < 0)
1052 0 : vp8_write_bit(bc, 1);
1053 : else
1054 0 : vp8_write_bit(bc, 0);
1055 : } else
1056 0 : vp8_write_bit(bc, 0);
1057 0 : }
1058 :
1059 0 : void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest,
1060 : unsigned char *dest_end, size_t *size) {
1061 : int i, j;
1062 : VP8_HEADER oh;
1063 0 : VP8_COMMON *const pc = &cpi->common;
1064 0 : vp8_writer *const bc = cpi->bc;
1065 0 : MACROBLOCKD *const xd = &cpi->mb.e_mbd;
1066 0 : int extra_bytes_packed = 0;
1067 :
1068 0 : unsigned char *cx_data = dest;
1069 0 : unsigned char *cx_data_end = dest_end;
1070 : const int *mb_feature_data_bits;
1071 :
1072 0 : oh.show_frame = (int)pc->show_frame;
1073 0 : oh.type = (int)pc->frame_type;
1074 0 : oh.version = pc->version;
1075 0 : oh.first_partition_length_in_bytes = 0;
1076 :
1077 0 : mb_feature_data_bits = vp8_mb_feature_data_bits;
1078 :
1079 0 : bc[0].error = &pc->error;
1080 :
1081 0 : validate_buffer(cx_data, 3, cx_data_end, &cpi->common.error);
1082 0 : cx_data += 3;
1083 :
1084 : #if defined(SECTIONBITS_OUTPUT)
1085 : Sectionbits[active_section = 1] += sizeof(VP8_HEADER) * 8 * 256;
1086 : #endif
1087 :
1088 : /* every keyframe send startcode, width, height, scale factor, clamp
1089 : * and color type
1090 : */
1091 0 : if (oh.type == KEY_FRAME) {
1092 : int v;
1093 :
1094 0 : validate_buffer(cx_data, 7, cx_data_end, &cpi->common.error);
1095 :
1096 : /* Start / synch code */
1097 0 : cx_data[0] = 0x9D;
1098 0 : cx_data[1] = 0x01;
1099 0 : cx_data[2] = 0x2a;
1100 :
1101 0 : v = (pc->horiz_scale << 14) | pc->Width;
1102 0 : cx_data[3] = v;
1103 0 : cx_data[4] = v >> 8;
1104 :
1105 0 : v = (pc->vert_scale << 14) | pc->Height;
1106 0 : cx_data[5] = v;
1107 0 : cx_data[6] = v >> 8;
1108 :
1109 0 : extra_bytes_packed = 7;
1110 0 : cx_data += extra_bytes_packed;
1111 :
1112 0 : vp8_start_encode(bc, cx_data, cx_data_end);
1113 :
1114 : /* signal clr type */
1115 0 : vp8_write_bit(bc, 0);
1116 0 : vp8_write_bit(bc, pc->clamp_type);
1117 :
1118 : } else {
1119 0 : vp8_start_encode(bc, cx_data, cx_data_end);
1120 : }
1121 :
1122 : /* Signal whether or not Segmentation is enabled */
1123 0 : vp8_write_bit(bc, xd->segmentation_enabled);
1124 :
1125 : /* Indicate which features are enabled */
1126 0 : if (xd->segmentation_enabled) {
1127 : /* Signal whether or not the segmentation map is being updated. */
1128 0 : vp8_write_bit(bc, xd->update_mb_segmentation_map);
1129 0 : vp8_write_bit(bc, xd->update_mb_segmentation_data);
1130 :
1131 0 : if (xd->update_mb_segmentation_data) {
1132 : signed char Data;
1133 :
1134 0 : vp8_write_bit(bc, xd->mb_segement_abs_delta);
1135 :
1136 : /* For each segmentation feature (Quant and loop filter level) */
1137 0 : for (i = 0; i < MB_LVL_MAX; ++i) {
1138 : /* For each of the segments */
1139 0 : for (j = 0; j < MAX_MB_SEGMENTS; ++j) {
1140 0 : Data = xd->segment_feature_data[i][j];
1141 :
1142 : /* Frame level data */
1143 0 : if (Data) {
1144 0 : vp8_write_bit(bc, 1);
1145 :
1146 0 : if (Data < 0) {
1147 0 : Data = -Data;
1148 0 : vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
1149 0 : vp8_write_bit(bc, 1);
1150 : } else {
1151 0 : vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
1152 0 : vp8_write_bit(bc, 0);
1153 : }
1154 : } else
1155 0 : vp8_write_bit(bc, 0);
1156 : }
1157 : }
1158 : }
1159 :
1160 0 : if (xd->update_mb_segmentation_map) {
1161 : /* Write the probs used to decode the segment id for each mb */
1162 0 : for (i = 0; i < MB_FEATURE_TREE_PROBS; ++i) {
1163 0 : int Data = xd->mb_segment_tree_probs[i];
1164 :
1165 0 : if (Data != 255) {
1166 0 : vp8_write_bit(bc, 1);
1167 0 : vp8_write_literal(bc, Data, 8);
1168 : } else
1169 0 : vp8_write_bit(bc, 0);
1170 : }
1171 : }
1172 : }
1173 :
1174 0 : vp8_write_bit(bc, pc->filter_type);
1175 0 : vp8_write_literal(bc, pc->filter_level, 6);
1176 0 : vp8_write_literal(bc, pc->sharpness_level, 3);
1177 :
1178 : /* Write out loop filter deltas applied at the MB level based on mode
1179 : * or ref frame (if they are enabled).
1180 : */
1181 0 : vp8_write_bit(bc, xd->mode_ref_lf_delta_enabled);
1182 :
1183 0 : if (xd->mode_ref_lf_delta_enabled) {
1184 : /* Do the deltas need to be updated */
1185 0 : int send_update =
1186 0 : xd->mode_ref_lf_delta_update || cpi->oxcf.error_resilient_mode;
1187 :
1188 0 : vp8_write_bit(bc, send_update);
1189 0 : if (send_update) {
1190 : int Data;
1191 :
1192 : /* Send update */
1193 0 : for (i = 0; i < MAX_REF_LF_DELTAS; ++i) {
1194 0 : Data = xd->ref_lf_deltas[i];
1195 :
1196 : /* Frame level data */
1197 0 : if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i] ||
1198 0 : cpi->oxcf.error_resilient_mode) {
1199 0 : xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i];
1200 0 : vp8_write_bit(bc, 1);
1201 :
1202 0 : if (Data > 0) {
1203 0 : vp8_write_literal(bc, (Data & 0x3F), 6);
1204 0 : vp8_write_bit(bc, 0); /* sign */
1205 : } else {
1206 0 : Data = -Data;
1207 0 : vp8_write_literal(bc, (Data & 0x3F), 6);
1208 0 : vp8_write_bit(bc, 1); /* sign */
1209 : }
1210 : } else
1211 0 : vp8_write_bit(bc, 0);
1212 : }
1213 :
1214 : /* Send update */
1215 0 : for (i = 0; i < MAX_MODE_LF_DELTAS; ++i) {
1216 0 : Data = xd->mode_lf_deltas[i];
1217 :
1218 0 : if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i] ||
1219 0 : cpi->oxcf.error_resilient_mode) {
1220 0 : xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i];
1221 0 : vp8_write_bit(bc, 1);
1222 :
1223 0 : if (Data > 0) {
1224 0 : vp8_write_literal(bc, (Data & 0x3F), 6);
1225 0 : vp8_write_bit(bc, 0); /* sign */
1226 : } else {
1227 0 : Data = -Data;
1228 0 : vp8_write_literal(bc, (Data & 0x3F), 6);
1229 0 : vp8_write_bit(bc, 1); /* sign */
1230 : }
1231 : } else
1232 0 : vp8_write_bit(bc, 0);
1233 : }
1234 : }
1235 : }
1236 :
1237 : /* signal here is multi token partition is enabled */
1238 0 : vp8_write_literal(bc, pc->multi_token_partition, 2);
1239 :
1240 : /* Frame Qbaseline quantizer index */
1241 0 : vp8_write_literal(bc, pc->base_qindex, 7);
1242 :
1243 : /* Transmit Dc, Second order and Uv quantizer delta information */
1244 0 : put_delta_q(bc, pc->y1dc_delta_q);
1245 0 : put_delta_q(bc, pc->y2dc_delta_q);
1246 0 : put_delta_q(bc, pc->y2ac_delta_q);
1247 0 : put_delta_q(bc, pc->uvdc_delta_q);
1248 0 : put_delta_q(bc, pc->uvac_delta_q);
1249 :
1250 : /* When there is a key frame all reference buffers are updated using
1251 : * the new key frame
1252 : */
1253 0 : if (pc->frame_type != KEY_FRAME) {
1254 : /* Should the GF or ARF be updated using the transmitted frame
1255 : * or buffer
1256 : */
1257 0 : vp8_write_bit(bc, pc->refresh_golden_frame);
1258 0 : vp8_write_bit(bc, pc->refresh_alt_ref_frame);
1259 :
1260 : /* If not being updated from current frame should either GF or ARF
1261 : * be updated from another buffer
1262 : */
1263 0 : if (!pc->refresh_golden_frame)
1264 0 : vp8_write_literal(bc, pc->copy_buffer_to_gf, 2);
1265 :
1266 0 : if (!pc->refresh_alt_ref_frame)
1267 0 : vp8_write_literal(bc, pc->copy_buffer_to_arf, 2);
1268 :
1269 : /* Indicate reference frame sign bias for Golden and ARF frames
1270 : * (always 0 for last frame buffer)
1271 : */
1272 0 : vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]);
1273 0 : vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]);
1274 : }
1275 :
1276 : #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
1277 0 : if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
1278 0 : if (pc->frame_type == KEY_FRAME) {
1279 0 : pc->refresh_entropy_probs = 1;
1280 : } else {
1281 0 : pc->refresh_entropy_probs = 0;
1282 : }
1283 : }
1284 : #endif
1285 :
1286 0 : vp8_write_bit(bc, pc->refresh_entropy_probs);
1287 :
1288 0 : if (pc->frame_type != KEY_FRAME) vp8_write_bit(bc, pc->refresh_last_frame);
1289 :
1290 : #ifdef VP8_ENTROPY_STATS
1291 :
1292 : if (pc->frame_type == INTER_FRAME)
1293 : active_section = 0;
1294 : else
1295 : active_section = 7;
1296 :
1297 : #endif
1298 :
1299 0 : vpx_clear_system_state();
1300 :
1301 : #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1302 : pack_coef_probs(cpi);
1303 : #else
1304 0 : if (pc->refresh_entropy_probs == 0) {
1305 : /* save a copy for later refresh */
1306 0 : memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc));
1307 : }
1308 :
1309 0 : vp8_update_coef_probs(cpi);
1310 : #endif
1311 :
1312 : #ifdef VP8_ENTROPY_STATS
1313 : active_section = 2;
1314 : #endif
1315 :
1316 : /* Write out the mb_no_coeff_skip flag */
1317 0 : vp8_write_bit(bc, pc->mb_no_coeff_skip);
1318 :
1319 0 : if (pc->frame_type == KEY_FRAME) {
1320 0 : write_kfmodes(cpi);
1321 :
1322 : #ifdef VP8_ENTROPY_STATS
1323 : active_section = 8;
1324 : #endif
1325 : } else {
1326 0 : pack_inter_mode_mvs(cpi);
1327 :
1328 : #ifdef VP8_ENTROPY_STATS
1329 : active_section = 1;
1330 : #endif
1331 : }
1332 :
1333 0 : vp8_stop_encode(bc);
1334 :
1335 0 : cx_data += bc->pos;
1336 :
1337 0 : oh.first_partition_length_in_bytes = cpi->bc->pos;
1338 :
1339 : /* update frame tag */
1340 : {
1341 0 : int v = (oh.first_partition_length_in_bytes << 5) | (oh.show_frame << 4) |
1342 0 : (oh.version << 1) | oh.type;
1343 :
1344 0 : dest[0] = v;
1345 0 : dest[1] = v >> 8;
1346 0 : dest[2] = v >> 16;
1347 : }
1348 :
1349 0 : *size = VP8_HEADER_SIZE + extra_bytes_packed + cpi->bc->pos;
1350 :
1351 0 : cpi->partition_sz[0] = (unsigned int)*size;
1352 :
1353 : #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
1354 : {
1355 : const int num_part = (1 << pc->multi_token_partition);
1356 : unsigned char *dp = cpi->partition_d[0] + cpi->partition_sz[0];
1357 :
1358 : if (num_part > 1) {
1359 : /* write token part sizes (all but last) if more than 1 */
1360 : validate_buffer(dp, 3 * (num_part - 1), cpi->partition_d_end[0],
1361 : &pc->error);
1362 :
1363 : cpi->partition_sz[0] += 3 * (num_part - 1);
1364 :
1365 : for (i = 1; i < num_part; ++i) {
1366 : write_partition_size(dp, cpi->partition_sz[i]);
1367 : dp += 3;
1368 : }
1369 : }
1370 :
1371 : if (!cpi->output_partition) {
1372 : /* concatenate partition buffers */
1373 : for (i = 0; i < num_part; ++i) {
1374 : memmove(dp, cpi->partition_d[i + 1], cpi->partition_sz[i + 1]);
1375 : cpi->partition_d[i + 1] = dp;
1376 : dp += cpi->partition_sz[i + 1];
1377 : }
1378 : }
1379 :
1380 : /* update total size */
1381 : *size = 0;
1382 : for (i = 0; i < num_part + 1; ++i) {
1383 : *size += cpi->partition_sz[i];
1384 : }
1385 : }
1386 : #else
1387 0 : if (pc->multi_token_partition != ONE_PARTITION) {
1388 0 : int num_part = 1 << pc->multi_token_partition;
1389 :
1390 : /* partition size table at the end of first partition */
1391 0 : cpi->partition_sz[0] += 3 * (num_part - 1);
1392 0 : *size += 3 * (num_part - 1);
1393 :
1394 0 : validate_buffer(cx_data, 3 * (num_part - 1), cx_data_end, &pc->error);
1395 :
1396 0 : for (i = 1; i < num_part + 1; ++i) {
1397 0 : cpi->bc[i].error = &pc->error;
1398 : }
1399 :
1400 0 : pack_tokens_into_partitions(cpi, cx_data + 3 * (num_part - 1), cx_data_end,
1401 : num_part);
1402 :
1403 0 : for (i = 1; i < num_part; ++i) {
1404 0 : cpi->partition_sz[i] = cpi->bc[i].pos;
1405 0 : write_partition_size(cx_data, cpi->partition_sz[i]);
1406 0 : cx_data += 3;
1407 0 : *size += cpi->partition_sz[i]; /* add to total */
1408 : }
1409 :
1410 : /* add last partition to total size */
1411 0 : cpi->partition_sz[i] = cpi->bc[i].pos;
1412 0 : *size += cpi->partition_sz[i];
1413 : } else {
1414 0 : bc[1].error = &pc->error;
1415 :
1416 0 : vp8_start_encode(&cpi->bc[1], cx_data, cx_data_end);
1417 :
1418 : #if CONFIG_MULTITHREAD
1419 0 : if (cpi->b_multi_threaded) {
1420 0 : pack_mb_row_tokens(cpi, &cpi->bc[1]);
1421 : } else {
1422 0 : vp8_pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count);
1423 : }
1424 : #else
1425 : vp8_pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count);
1426 : #endif // CONFIG_MULTITHREAD
1427 :
1428 0 : vp8_stop_encode(&cpi->bc[1]);
1429 :
1430 0 : *size += cpi->bc[1].pos;
1431 0 : cpi->partition_sz[1] = cpi->bc[1].pos;
1432 : }
1433 : #endif
1434 0 : }
1435 :
1436 : #ifdef VP8_ENTROPY_STATS
1437 : void print_tree_update_probs() {
1438 : int i, j, k, l;
1439 : FILE *f = fopen("context.c", "a");
1440 : int Sum;
1441 : fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
1442 : fprintf(f,
1443 : "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] "
1444 : "[PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n");
1445 :
1446 : for (i = 0; i < BLOCK_TYPES; ++i) {
1447 : fprintf(f, " { \n");
1448 :
1449 : for (j = 0; j < COEF_BANDS; ++j) {
1450 : fprintf(f, " {\n");
1451 :
1452 : for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
1453 : fprintf(f, " {");
1454 :
1455 : for (l = 0; l < ENTROPY_NODES; ++l) {
1456 : Sum =
1457 : tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1];
1458 :
1459 : if (Sum > 0) {
1460 : if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0)
1461 : fprintf(f, "%3ld, ",
1462 : (tree_update_hist[i][j][k][l][0] * 255) / Sum);
1463 : else
1464 : fprintf(f, "%3ld, ", 1);
1465 : } else
1466 : fprintf(f, "%3ld, ", 128);
1467 : }
1468 :
1469 : fprintf(f, "},\n");
1470 : }
1471 :
1472 : fprintf(f, " },\n");
1473 : }
1474 :
1475 : fprintf(f, " },\n");
1476 : }
1477 :
1478 : fprintf(f, "};\n");
1479 : fclose(f);
1480 : }
1481 : #endif
|