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