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