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