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