clarify *_offsets.asm differences
[libvpx.git] / vp8 / encoder / bitstream.c
blob412542d1038333d35aa3371d0c9dc20447f578b7
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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 */
12 #include "header.h"
13 #include "encodemv.h"
14 #include "entropymode.h"
15 #include "findnearmv.h"
16 #include "mcomp.h"
17 #include "systemdependent.h"
18 #include <assert.h>
19 #include <stdio.h>
20 #include "pragmas.h"
21 #include "vpx_mem/vpx_mem.h"
22 #include "bitstream.h"
24 const int vp8cx_base_skip_false_prob[128] =
26 255, 255, 255, 255, 255, 255, 255, 255,
27 255, 255, 255, 255, 255, 255, 255, 255,
28 255, 255, 255, 255, 255, 255, 255, 255,
29 255, 255, 255, 255, 255, 255, 255, 255,
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 251, 248, 244, 240, 236, 232, 229, 225,
34 221, 217, 213, 208, 204, 199, 194, 190,
35 187, 183, 179, 175, 172, 168, 164, 160,
36 157, 153, 149, 145, 142, 138, 134, 130,
37 127, 124, 120, 117, 114, 110, 107, 104,
38 101, 98, 95, 92, 89, 86, 83, 80,
39 77, 74, 71, 68, 65, 62, 59, 56,
40 53, 50, 47, 44, 41, 38, 35, 32,
41 30, 28, 26, 24, 22, 20, 18, 16,
43 #ifdef VP8REF
44 #define __int64 long long
45 #endif
47 #if defined(SECTIONBITS_OUTPUT)
48 unsigned __int64 Sectionbits[500];
49 #endif
51 #ifdef ENTROPY_STATS
52 int intra_mode_stats[10][10][10];
53 static unsigned int tree_update_hist [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1] [2];
54 extern unsigned int active_section;
55 #endif
57 #ifdef MODE_STATS
58 int count_mb_seg[4] = { 0, 0, 0, 0 };
59 #endif
61 #if CONFIG_BIG_ENDIAN
62 # define make_endian_16(a) \
63 (((unsigned int)(a & 0xff)) << 8) | (((unsigned int)(a & 0xff00)) >> 8)
64 # define make_endian_32(a) \
65 (((unsigned int)(a & 0xff)) << 24) | (((unsigned int)(a & 0xff00)) << 8) | \
66 (((unsigned int)(a & 0xff0000)) >> 8) | (((unsigned int)(a & 0xff000000)) >> 24)
67 #else
68 # define make_endian_16(a) a
69 # define make_endian_32(a) a
70 #endif
72 static void update_mode(
73 vp8_writer *const w,
74 int n,
75 vp8_token tok [/* n */],
76 vp8_tree tree,
77 vp8_prob Pnew [/* n-1 */],
78 vp8_prob Pcur [/* n-1 */],
79 unsigned int bct [/* n-1 */] [2],
80 const unsigned int num_events[/* n */]
83 unsigned int new_b = 0, old_b = 0;
84 int i = 0;
86 vp8_tree_probs_from_distribution(
87 n--, tok, tree,
88 Pnew, bct, num_events,
89 256, 1
94 new_b += vp8_cost_branch(bct[i], Pnew[i]);
95 old_b += vp8_cost_branch(bct[i], Pcur[i]);
97 while (++i < n);
99 if (new_b + (n << 8) < old_b)
101 int i = 0;
103 vp8_write_bit(w, 1);
107 const vp8_prob p = Pnew[i];
109 vp8_write_literal(w, Pcur[i] = p ? p : 1, 8);
111 while (++i < n);
113 else
114 vp8_write_bit(w, 0);
117 static void update_mbintra_mode_probs(VP8_COMP *cpi)
119 VP8_COMMON *const x = & cpi->common;
121 vp8_writer *const w = & cpi->bc;
124 vp8_prob Pnew [VP8_YMODES-1];
125 unsigned int bct [VP8_YMODES-1] [2];
127 update_mode(
128 w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree,
129 Pnew, x->fc.ymode_prob, bct, (unsigned int *)cpi->ymode_count
133 vp8_prob Pnew [VP8_UV_MODES-1];
134 unsigned int bct [VP8_UV_MODES-1] [2];
136 update_mode(
137 w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree,
138 Pnew, x->fc.uv_mode_prob, bct, (unsigned int *)cpi->uv_mode_count
143 static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p)
145 vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m);
148 static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p)
150 vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m);
153 static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p)
155 vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m);
159 static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p)
161 vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m);
164 static void write_split(vp8_writer *bc, int x)
166 vp8_write_token(
167 bc, vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + x
171 static const unsigned int norm[256] =
173 0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
174 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
175 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
176 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
180 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
183 static void pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount)
185 const TOKENEXTRA *const stop = p + xcount;
186 unsigned int split;
187 unsigned int shift;
188 int count = w->count;
189 unsigned int range = w->range;
190 unsigned int lowvalue = w->lowvalue;
192 while (p < stop)
194 const int t = p->Token;
195 vp8_token *const a = vp8_coef_encodings + t;
196 const vp8_extra_bit_struct *const b = vp8_extra_bits + t;
197 int i = 0;
198 const unsigned char *pp = p->context_tree;
199 int v = a->value;
200 int n = a->Len;
202 if (p->skip_eob_node)
204 n--;
205 i = 2;
210 const int bb = (v >> --n) & 1;
211 split = 1 + (((range - 1) * pp[i>>1]) >> 8);
212 i = vp8_coef_tree[i+bb];
214 if (bb)
216 lowvalue += split;
217 range = range - split;
219 else
221 range = split;
224 shift = norm[range];
225 range <<= shift;
226 count += shift;
228 if (count >= 0)
230 int offset = shift - count;
232 if ((lowvalue << (offset - 1)) & 0x80000000)
234 int x = w->pos - 1;
236 while (x >= 0 && w->buffer[x] == 0xff)
238 w->buffer[x] = (unsigned char)0;
239 x--;
242 w->buffer[x] += 1;
245 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
246 lowvalue <<= offset;
247 shift = count;
248 lowvalue &= 0xffffff;
249 count -= 8 ;
252 lowvalue <<= shift;
254 while (n);
257 if (b->base_val)
259 const int e = p->Extra, L = b->Len;
261 if (L)
263 const unsigned char *pp = b->prob;
264 int v = e >> 1;
265 int n = L; /* number of bits in v, assumed nonzero */
266 int i = 0;
270 const int bb = (v >> --n) & 1;
271 split = 1 + (((range - 1) * pp[i>>1]) >> 8);
272 i = b->tree[i+bb];
274 if (bb)
276 lowvalue += split;
277 range = range - split;
279 else
281 range = split;
284 shift = norm[range];
285 range <<= shift;
286 count += shift;
288 if (count >= 0)
290 int offset = shift - count;
292 if ((lowvalue << (offset - 1)) & 0x80000000)
294 int x = w->pos - 1;
296 while (x >= 0 && w->buffer[x] == 0xff)
298 w->buffer[x] = (unsigned char)0;
299 x--;
302 w->buffer[x] += 1;
305 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
306 lowvalue <<= offset;
307 shift = count;
308 lowvalue &= 0xffffff;
309 count -= 8 ;
312 lowvalue <<= shift;
314 while (n);
320 split = (range + 1) >> 1;
322 if (e & 1)
324 lowvalue += split;
325 range = range - split;
327 else
329 range = split;
332 range <<= 1;
334 if ((lowvalue & 0x80000000))
336 int x = w->pos - 1;
338 while (x >= 0 && w->buffer[x] == 0xff)
340 w->buffer[x] = (unsigned char)0;
341 x--;
344 w->buffer[x] += 1;
348 lowvalue <<= 1;
350 if (!++count)
352 count = -8;
353 w->buffer[w->pos++] = (lowvalue >> 24);
354 lowvalue &= 0xffffff;
360 ++p;
363 w->count = count;
364 w->lowvalue = lowvalue;
365 w->range = range;
369 static void write_partition_size(unsigned char *cx_data, int size)
371 signed char csize;
373 csize = size & 0xff;
374 *cx_data = csize;
375 csize = (size >> 8) & 0xff;
376 *(cx_data + 1) = csize;
377 csize = (size >> 16) & 0xff;
378 *(cx_data + 2) = csize;
382 static void pack_tokens_into_partitions_c(VP8_COMP *cpi, unsigned char *cx_data, int num_part, int *size)
385 int i;
386 unsigned char *ptr = cx_data;
387 unsigned int shift;
388 vp8_writer *w = &cpi->bc2;
389 *size = 3 * (num_part - 1);
390 ptr = cx_data + (*size);
392 for (i = 0; i < num_part; i++)
394 vp8_start_encode(w, ptr);
396 unsigned int split;
397 int count = w->count;
398 unsigned int range = w->range;
399 unsigned int lowvalue = w->lowvalue;
400 int mb_row;
402 for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part)
404 TOKENEXTRA *p = cpi->tplist[mb_row].start;
405 TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
407 while (p < stop)
409 const int t = p->Token;
410 vp8_token *const a = vp8_coef_encodings + t;
411 const vp8_extra_bit_struct *const b = vp8_extra_bits + t;
412 int i = 0;
413 const unsigned char *pp = p->context_tree;
414 int v = a->value;
415 int n = a->Len;
417 if (p->skip_eob_node)
419 n--;
420 i = 2;
425 const int bb = (v >> --n) & 1;
426 split = 1 + (((range - 1) * pp[i>>1]) >> 8);
427 i = vp8_coef_tree[i+bb];
429 if (bb)
431 lowvalue += split;
432 range = range - split;
434 else
436 range = split;
439 shift = norm[range];
440 range <<= shift;
441 count += shift;
443 if (count >= 0)
445 int offset = shift - count;
447 if ((lowvalue << (offset - 1)) & 0x80000000)
449 int x = w->pos - 1;
451 while (x >= 0 && w->buffer[x] == 0xff)
453 w->buffer[x] = (unsigned char)0;
454 x--;
457 w->buffer[x] += 1;
460 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
461 lowvalue <<= offset;
462 shift = count;
463 lowvalue &= 0xffffff;
464 count -= 8 ;
467 lowvalue <<= shift;
469 while (n);
472 if (b->base_val)
474 const int e = p->Extra, L = b->Len;
476 if (L)
478 const unsigned char *pp = b->prob;
479 int v = e >> 1;
480 int n = L; /* number of bits in v, assumed nonzero */
481 int i = 0;
485 const int bb = (v >> --n) & 1;
486 split = 1 + (((range - 1) * pp[i>>1]) >> 8);
487 i = b->tree[i+bb];
489 if (bb)
491 lowvalue += split;
492 range = range - split;
494 else
496 range = split;
499 shift = norm[range];
500 range <<= shift;
501 count += shift;
503 if (count >= 0)
505 int offset = shift - count;
507 if ((lowvalue << (offset - 1)) & 0x80000000)
509 int x = w->pos - 1;
511 while (x >= 0 && w->buffer[x] == 0xff)
513 w->buffer[x] = (unsigned char)0;
514 x--;
517 w->buffer[x] += 1;
520 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
521 lowvalue <<= offset;
522 shift = count;
523 lowvalue &= 0xffffff;
524 count -= 8 ;
527 lowvalue <<= shift;
529 while (n);
533 split = (range + 1) >> 1;
535 if (e & 1)
537 lowvalue += split;
538 range = range - split;
540 else
542 range = split;
545 range <<= 1;
547 if ((lowvalue & 0x80000000))
549 int x = w->pos - 1;
551 while (x >= 0 && w->buffer[x] == 0xff)
553 w->buffer[x] = (unsigned char)0;
554 x--;
557 w->buffer[x] += 1;
561 lowvalue <<= 1;
563 if (!++count)
565 count = -8;
566 w->buffer[w->pos++] = (lowvalue >> 24);
567 lowvalue &= 0xffffff;
573 ++p;
577 w->count = count;
578 w->lowvalue = lowvalue;
579 w->range = range;
583 vp8_stop_encode(w);
584 *size += w->pos;
586 if (i < (num_part - 1))
588 write_partition_size(cx_data, w->pos);
589 cx_data += 3;
590 ptr += w->pos;
596 static void pack_mb_row_tokens_c(VP8_COMP *cpi, vp8_writer *w)
599 unsigned int split;
600 int count = w->count;
601 unsigned int range = w->range;
602 unsigned int lowvalue = w->lowvalue;
603 unsigned int shift;
604 int mb_row;
606 for (mb_row = 0; mb_row < cpi->common.mb_rows; mb_row++)
608 TOKENEXTRA *p = cpi->tplist[mb_row].start;
609 TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
611 while (p < stop)
613 const int t = p->Token;
614 vp8_token *const a = vp8_coef_encodings + t;
615 const vp8_extra_bit_struct *const b = vp8_extra_bits + t;
616 int i = 0;
617 const unsigned char *pp = p->context_tree;
618 int v = a->value;
619 int n = a->Len;
621 if (p->skip_eob_node)
623 n--;
624 i = 2;
629 const int bb = (v >> --n) & 1;
630 split = 1 + (((range - 1) * pp[i>>1]) >> 8);
631 i = vp8_coef_tree[i+bb];
633 if (bb)
635 lowvalue += split;
636 range = range - split;
638 else
640 range = split;
643 shift = norm[range];
644 range <<= shift;
645 count += shift;
647 if (count >= 0)
649 int offset = shift - count;
651 if ((lowvalue << (offset - 1)) & 0x80000000)
653 int x = w->pos - 1;
655 while (x >= 0 && w->buffer[x] == 0xff)
657 w->buffer[x] = (unsigned char)0;
658 x--;
661 w->buffer[x] += 1;
664 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
665 lowvalue <<= offset;
666 shift = count;
667 lowvalue &= 0xffffff;
668 count -= 8 ;
671 lowvalue <<= shift;
673 while (n);
676 if (b->base_val)
678 const int e = p->Extra, L = b->Len;
680 if (L)
682 const unsigned char *pp = b->prob;
683 int v = e >> 1;
684 int n = L; /* number of bits in v, assumed nonzero */
685 int i = 0;
689 const int bb = (v >> --n) & 1;
690 split = 1 + (((range - 1) * pp[i>>1]) >> 8);
691 i = b->tree[i+bb];
693 if (bb)
695 lowvalue += split;
696 range = range - split;
698 else
700 range = split;
703 shift = norm[range];
704 range <<= shift;
705 count += shift;
707 if (count >= 0)
709 int offset = shift - count;
711 if ((lowvalue << (offset - 1)) & 0x80000000)
713 int x = w->pos - 1;
715 while (x >= 0 && w->buffer[x] == 0xff)
717 w->buffer[x] = (unsigned char)0;
718 x--;
721 w->buffer[x] += 1;
724 w->buffer[w->pos++] = (lowvalue >> (24 - offset));
725 lowvalue <<= offset;
726 shift = count;
727 lowvalue &= 0xffffff;
728 count -= 8 ;
731 lowvalue <<= shift;
733 while (n);
737 split = (range + 1) >> 1;
739 if (e & 1)
741 lowvalue += split;
742 range = range - split;
744 else
746 range = split;
749 range <<= 1;
751 if ((lowvalue & 0x80000000))
753 int x = w->pos - 1;
755 while (x >= 0 && w->buffer[x] == 0xff)
757 w->buffer[x] = (unsigned char)0;
758 x--;
761 w->buffer[x] += 1;
765 lowvalue <<= 1;
767 if (!++count)
769 count = -8;
770 w->buffer[w->pos++] = (lowvalue >> 24);
771 lowvalue &= 0xffffff;
777 ++p;
781 w->count = count;
782 w->lowvalue = lowvalue;
783 w->range = range;
787 static void write_mv_ref
789 vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p
793 assert(NEARESTMV <= m && m <= SPLITMV);
795 vp8_write_token(w, vp8_mv_ref_tree, p,
796 vp8_mv_ref_encoding_array - NEARESTMV + m);
799 static void write_sub_mv_ref
801 vp8_writer *w, B_PREDICTION_MODE m, const vp8_prob *p
804 assert(LEFT4X4 <= m && m <= NEW4X4);
806 vp8_write_token(w, vp8_sub_mv_ref_tree, p,
807 vp8_sub_mv_ref_encoding_array - LEFT4X4 + m);
810 static void write_mv
812 vp8_writer *w, const MV *mv, const MV *ref, const MV_CONTEXT *mvc
815 MV e;
816 e.row = mv->row - ref->row;
817 e.col = mv->col - ref->col;
819 vp8_encode_motion_vector(w, &e, mvc);
822 static void write_mb_features(vp8_writer *w, const MB_MODE_INFO *mi, const MACROBLOCKD *x)
824 // Encode the MB segment id.
825 if (x->segmentation_enabled && x->update_mb_segmentation_map)
827 switch (mi->segment_id)
829 case 0:
830 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
831 vp8_write(w, 0, x->mb_segment_tree_probs[1]);
832 break;
833 case 1:
834 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
835 vp8_write(w, 1, x->mb_segment_tree_probs[1]);
836 break;
837 case 2:
838 vp8_write(w, 1, x->mb_segment_tree_probs[0]);
839 vp8_write(w, 0, x->mb_segment_tree_probs[2]);
840 break;
841 case 3:
842 vp8_write(w, 1, x->mb_segment_tree_probs[0]);
843 vp8_write(w, 1, x->mb_segment_tree_probs[2]);
844 break;
846 // TRAP.. This should not happen
847 default:
848 vp8_write(w, 0, x->mb_segment_tree_probs[0]);
849 vp8_write(w, 0, x->mb_segment_tree_probs[1]);
850 break;
856 static void pack_inter_mode_mvs(VP8_COMP *const cpi)
858 VP8_COMMON *const pc = & cpi->common;
859 vp8_writer *const w = & cpi->bc;
860 const MV_CONTEXT *mvc = pc->fc.mvc;
862 const int *const rfct = cpi->count_mb_ref_frame_usage;
863 const int rf_intra = rfct[INTRA_FRAME];
864 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
866 MODE_INFO *m = pc->mi, *ms;
867 const int mis = pc->mode_info_stride;
868 int mb_row = -1;
870 int prob_last_coded;
871 int prob_gf_coded;
872 int prob_skip_false = 0;
873 ms = pc->mi - 1;
875 cpi->mb.partition_info = cpi->mb.pi;
877 // Calculate the probabilities to be used to code the reference frame based on actual useage this frame
878 if (!(cpi->prob_intra_coded = rf_intra * 255 / (rf_intra + rf_inter)))
879 cpi->prob_intra_coded = 1;
881 prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
883 if (!prob_last_coded)
884 prob_last_coded = 1;
886 prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
887 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
889 if (!prob_gf_coded)
890 prob_gf_coded = 1;
893 #ifdef ENTROPY_STATS
894 active_section = 1;
895 #endif
897 if (pc->mb_no_coeff_skip)
899 prob_skip_false = cpi->skip_false_count * 256 / (cpi->skip_false_count + cpi->skip_true_count);
901 if (prob_skip_false <= 1)
902 prob_skip_false = 1;
904 if (prob_skip_false > 255)
905 prob_skip_false = 255;
907 cpi->prob_skip_false = prob_skip_false;
908 vp8_write_literal(w, prob_skip_false, 8);
911 vp8_write_literal(w, cpi->prob_intra_coded, 8);
912 vp8_write_literal(w, prob_last_coded, 8);
913 vp8_write_literal(w, prob_gf_coded, 8);
915 update_mbintra_mode_probs(cpi);
917 vp8_write_mvprobs(cpi);
919 while (++mb_row < pc->mb_rows)
921 int mb_col = -1;
923 while (++mb_col < pc->mb_cols)
925 const MB_MODE_INFO *const mi = & m->mbmi;
926 const MV_REFERENCE_FRAME rf = mi->ref_frame;
927 const MB_PREDICTION_MODE mode = mi->mode;
929 MACROBLOCKD *xd = &cpi->mb.e_mbd;
931 // Distance of Mb to the various image edges.
932 // These specified to 8th pel as they are always compared to MV values that are in 1/8th pel units
933 xd->mb_to_left_edge = -((mb_col * 16) << 3);
934 xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
935 xd->mb_to_top_edge = -((mb_row * 16)) << 3;
936 xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
938 #ifdef ENTROPY_STATS
939 active_section = 9;
940 #endif
942 if (cpi->mb.e_mbd.update_mb_segmentation_map)
943 write_mb_features(w, mi, &cpi->mb.e_mbd);
945 if (pc->mb_no_coeff_skip)
946 vp8_encode_bool(w, m->mbmi.mb_skip_coeff, prob_skip_false);
948 if (rf == INTRA_FRAME)
950 vp8_write(w, 0, cpi->prob_intra_coded);
951 #ifdef ENTROPY_STATS
952 active_section = 6;
953 #endif
954 write_ymode(w, mode, pc->fc.ymode_prob);
956 if (mode == B_PRED)
958 int j = 0;
961 write_bmode(w, m->bmi[j].mode, pc->fc.bmode_prob);
963 while (++j < 16);
966 write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob);
968 else /* inter coded */
970 MV best_mv;
971 vp8_prob mv_ref_p [VP8_MVREFS-1];
973 vp8_write(w, 1, cpi->prob_intra_coded);
975 if (rf == LAST_FRAME)
976 vp8_write(w, 0, prob_last_coded);
977 else
979 vp8_write(w, 1, prob_last_coded);
980 vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, prob_gf_coded);
984 MV n1, n2;
985 int ct[4];
987 vp8_find_near_mvs(xd, m, &n1, &n2, &best_mv, ct, rf, cpi->common.ref_frame_sign_bias);
988 vp8_mv_ref_probs(mv_ref_p, ct);
990 #ifdef ENTROPY_STATS
991 accum_mv_refs(mode, ct);
992 #endif
996 #ifdef ENTROPY_STATS
997 active_section = 3;
998 #endif
1000 write_mv_ref(w, mode, mv_ref_p);
1002 switch (mode) /* new, split require MVs */
1004 case NEWMV:
1006 #ifdef ENTROPY_STATS
1007 active_section = 5;
1008 #endif
1010 write_mv(w, &mi->mv.as_mv, &best_mv, mvc);
1011 break;
1013 case SPLITMV:
1015 int j = 0;
1017 #ifdef MODE_STATS
1018 ++count_mb_seg [mi->partitioning];
1019 #endif
1021 write_split(w, mi->partitioning);
1025 const B_MODE_INFO *const b = cpi->mb.partition_info->bmi + j;
1026 const int *const L = vp8_mbsplits [mi->partitioning];
1027 int k = -1; /* first block in subset j */
1028 int mv_contz;
1030 while (j != L[++k])
1031 if (k >= 16)
1032 assert(0);
1034 mv_contz = vp8_mv_cont
1035 (&(vp8_left_bmi(m, k)->mv.as_mv),
1036 &(vp8_above_bmi(m, k, mis)->mv.as_mv));
1037 write_sub_mv_ref(w, b->mode, vp8_sub_mv_ref_prob2 [mv_contz]); //pc->fc.sub_mv_ref_prob);
1039 if (b->mode == NEW4X4)
1041 #ifdef ENTROPY_STATS
1042 active_section = 11;
1043 #endif
1044 write_mv(w, &b->mv.as_mv, &best_mv, (const MV_CONTEXT *) mvc);
1047 while (++j < cpi->mb.partition_info->count);
1049 break;
1050 default:
1051 break;
1055 ++m;
1056 cpi->mb.partition_info++;
1059 ++m; /* skip L prediction border */
1060 cpi->mb.partition_info++;
1065 static void write_kfmodes(VP8_COMP *cpi)
1067 vp8_writer *const bc = & cpi->bc;
1068 const VP8_COMMON *const c = & cpi->common;
1069 /* const */
1070 MODE_INFO *m = c->mi;
1072 int mb_row = -1;
1073 int prob_skip_false = 0;
1075 if (c->mb_no_coeff_skip)
1077 prob_skip_false = cpi->skip_false_count * 256 / (cpi->skip_false_count + cpi->skip_true_count);
1079 if (prob_skip_false <= 1)
1080 prob_skip_false = 1;
1082 if (prob_skip_false >= 255)
1083 prob_skip_false = 255;
1085 cpi->prob_skip_false = prob_skip_false;
1086 vp8_write_literal(bc, prob_skip_false, 8);
1089 while (++mb_row < c->mb_rows)
1091 int mb_col = -1;
1093 while (++mb_col < c->mb_cols)
1095 const int ym = m->mbmi.mode;
1097 if (cpi->mb.e_mbd.update_mb_segmentation_map)
1098 write_mb_features(bc, &m->mbmi, &cpi->mb.e_mbd);
1100 if (c->mb_no_coeff_skip)
1101 vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false);
1103 kfwrite_ymode(bc, ym, c->kf_ymode_prob);
1105 if (ym == B_PRED)
1107 const int mis = c->mode_info_stride;
1108 int i = 0;
1112 const B_PREDICTION_MODE A = vp8_above_bmi(m, i, mis)->mode;
1113 const B_PREDICTION_MODE L = vp8_left_bmi(m, i)->mode;
1114 const int bm = m->bmi[i].mode;
1116 #ifdef ENTROPY_STATS
1117 ++intra_mode_stats [A] [L] [bm];
1118 #endif
1120 write_bmode(bc, bm, c->kf_bmode_prob [A] [L]);
1122 while (++i < 16);
1125 write_uv_mode(bc, (m++)->mbmi.uv_mode, c->kf_uv_mode_prob);
1128 m++; // skip L prediction border
1131 int vp8_estimate_entropy_savings(VP8_COMP *cpi)
1133 int i = 0;
1134 int savings = 0;
1136 const int *const rfct = cpi->count_mb_ref_frame_usage;
1137 const int rf_intra = rfct[INTRA_FRAME];
1138 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
1139 int new_intra, new_last, gf_last, oldtotal, newtotal;
1140 int ref_frame_cost[MAX_REF_FRAMES];
1142 vp8_clear_system_state(); //__asm emms;
1144 if (cpi->common.frame_type != KEY_FRAME)
1146 if (!(new_intra = rf_intra * 255 / (rf_intra + rf_inter)))
1147 new_intra = 1;
1149 new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
1151 gf_last = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
1152 ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
1154 // new costs
1155 ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(new_intra);
1156 ref_frame_cost[LAST_FRAME] = vp8_cost_one(new_intra)
1157 + vp8_cost_zero(new_last);
1158 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(new_intra)
1159 + vp8_cost_one(new_last)
1160 + vp8_cost_zero(gf_last);
1161 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(new_intra)
1162 + vp8_cost_one(new_last)
1163 + vp8_cost_one(gf_last);
1165 newtotal =
1166 rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
1167 rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
1168 rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
1169 rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
1172 // old costs
1173 ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(cpi->prob_intra_coded);
1174 ref_frame_cost[LAST_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1175 + vp8_cost_zero(cpi->prob_last_coded);
1176 ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1177 + vp8_cost_one(cpi->prob_last_coded)
1178 + vp8_cost_zero(cpi->prob_gf_coded);
1179 ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(cpi->prob_intra_coded)
1180 + vp8_cost_one(cpi->prob_last_coded)
1181 + vp8_cost_one(cpi->prob_gf_coded);
1183 oldtotal =
1184 rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
1185 rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
1186 rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
1187 rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
1189 savings += (oldtotal - newtotal) / 256;
1195 int j = 0;
1199 int k = 0;
1203 /* at every context */
1205 /* calc probs and branch cts for this frame only */
1206 //vp8_prob new_p [vp8_coef_tokens-1];
1207 //unsigned int branch_ct [vp8_coef_tokens-1] [2];
1209 int t = 0; /* token/prob index */
1211 vp8_tree_probs_from_distribution(
1212 vp8_coef_tokens, vp8_coef_encodings, vp8_coef_tree,
1213 cpi->frame_coef_probs [i][j][k], cpi->frame_branch_ct [i][j][k], cpi->coef_counts [i][j][k],
1214 256, 1
1219 const unsigned int *ct = cpi->frame_branch_ct [i][j][k][t];
1220 const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
1222 const vp8_prob old = cpi->common.fc.coef_probs [i][j][k][t];
1223 const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
1225 const int old_b = vp8_cost_branch(ct, old);
1226 const int new_b = vp8_cost_branch(ct, newp);
1228 const int update_b = 8 +
1229 ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);
1231 const int s = old_b - new_b - update_b;
1233 if (s > 0)
1234 savings += s;
1238 while (++t < vp8_coef_tokens - 1);
1242 while (++k < PREV_COEF_CONTEXTS);
1244 while (++j < COEF_BANDS);
1246 while (++i < BLOCK_TYPES);
1248 return savings;
1251 static void update_coef_probs(VP8_COMP *cpi)
1253 int i = 0;
1254 vp8_writer *const w = & cpi->bc;
1255 int savings = 0;
1257 vp8_clear_system_state(); //__asm emms;
1262 int j = 0;
1266 int k = 0;
1270 //note: use result from vp8_estimate_entropy_savings, so no need to call vp8_tree_probs_from_distribution here.
1271 /* at every context */
1273 /* calc probs and branch cts for this frame only */
1274 //vp8_prob new_p [vp8_coef_tokens-1];
1275 //unsigned int branch_ct [vp8_coef_tokens-1] [2];
1277 int t = 0; /* token/prob index */
1279 //vp8_tree_probs_from_distribution(
1280 // vp8_coef_tokens, vp8_coef_encodings, vp8_coef_tree,
1281 // new_p, branch_ct, (unsigned int *)cpi->coef_counts [i][j][k],
1282 // 256, 1
1283 // );
1287 const unsigned int *ct = cpi->frame_branch_ct [i][j][k][t];
1288 const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
1290 vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t;
1291 const vp8_prob old = *Pold;
1292 const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
1294 const int old_b = vp8_cost_branch(ct, old);
1295 const int new_b = vp8_cost_branch(ct, newp);
1297 const int update_b = 8 +
1298 ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);
1300 const int s = old_b - new_b - update_b;
1301 const int u = s > 0 ? 1 : 0;
1303 vp8_write(w, u, upd);
1306 #ifdef ENTROPY_STATS
1307 ++ tree_update_hist [i][j][k][t] [u];
1308 #endif
1310 if (u)
1312 /* send/use new probability */
1314 *Pold = newp;
1315 vp8_write_literal(w, newp, 8);
1317 savings += s;
1322 while (++t < vp8_coef_tokens - 1);
1324 /* Accum token counts for generation of default statistics */
1325 #ifdef ENTROPY_STATS
1326 t = 0;
1330 context_counters [i][j][k][t] += cpi->coef_counts [i][j][k][t];
1332 while (++t < vp8_coef_tokens);
1334 #endif
1337 while (++k < PREV_COEF_CONTEXTS);
1339 while (++j < COEF_BANDS);
1341 while (++i < BLOCK_TYPES);
1344 #ifdef PACKET_TESTING
1345 FILE *vpxlogc = 0;
1346 #endif
1348 static void put_delta_q(vp8_writer *bc, int delta_q)
1350 if (delta_q != 0)
1352 vp8_write_bit(bc, 1);
1353 vp8_write_literal(bc, abs(delta_q), 4);
1355 if (delta_q < 0)
1356 vp8_write_bit(bc, 1);
1357 else
1358 vp8_write_bit(bc, 0);
1360 else
1361 vp8_write_bit(bc, 0);
1364 void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned long *size)
1366 int i, j;
1367 VP8_HEADER oh;
1368 VP8_COMMON *const pc = & cpi->common;
1369 vp8_writer *const bc = & cpi->bc;
1370 MACROBLOCKD *const xd = & cpi->mb.e_mbd;
1371 int extra_bytes_packed = 0;
1373 unsigned char *cx_data = dest;
1374 const int *mb_feature_data_bits;
1376 oh.show_frame = (int) pc->show_frame;
1377 oh.type = (int)pc->frame_type;
1378 oh.version = pc->version;
1380 mb_feature_data_bits = vp8_mb_feature_data_bits;
1381 cx_data += 3;
1383 #if defined(SECTIONBITS_OUTPUT)
1384 Sectionbits[active_section = 1] += sizeof(VP8_HEADER) * 8 * 256;
1385 #endif
1387 //vp8_kf_default_bmode_probs() is called in vp8_setup_key_frame() once for each
1388 //K frame before encode frame. pc->kf_bmode_prob doesn't get changed anywhere
1389 //else. No need to call it again here. --yw
1390 //vp8_kf_default_bmode_probs( pc->kf_bmode_prob);
1392 // every keyframe send startcode, width, height, scale factor, clamp and color type
1393 if (oh.type == KEY_FRAME)
1395 // Start / synch code
1396 cx_data[0] = 0x9D;
1397 cx_data[1] = 0x01;
1398 cx_data[2] = 0x2a;
1400 *((unsigned short *)(cx_data + 3)) = make_endian_16((pc->horiz_scale << 14) | pc->Width);
1401 *((unsigned short *)(cx_data + 5)) = make_endian_16((pc->vert_scale << 14) | pc->Height);
1403 extra_bytes_packed = 7;
1404 cx_data += extra_bytes_packed ;
1406 vp8_start_encode(bc, cx_data);
1408 // signal clr type
1409 vp8_write_bit(bc, pc->clr_type);
1410 vp8_write_bit(bc, pc->clamp_type);
1413 else
1414 vp8_start_encode(bc, cx_data);
1417 // Signal whether or not Segmentation is enabled
1418 vp8_write_bit(bc, (xd->segmentation_enabled) ? 1 : 0);
1420 // Indicate which features are enabled
1421 if (xd->segmentation_enabled)
1423 // Signal whether or not the segmentation map is being updated.
1424 vp8_write_bit(bc, (xd->update_mb_segmentation_map) ? 1 : 0);
1425 vp8_write_bit(bc, (xd->update_mb_segmentation_data) ? 1 : 0);
1427 if (xd->update_mb_segmentation_data)
1429 signed char Data;
1431 vp8_write_bit(bc, (xd->mb_segement_abs_delta) ? 1 : 0);
1433 // For each segmentation feature (Quant and loop filter level)
1434 for (i = 0; i < MB_LVL_MAX; i++)
1436 // For each of the segments
1437 for (j = 0; j < MAX_MB_SEGMENTS; j++)
1439 Data = xd->segment_feature_data[i][j];
1441 // Frame level data
1442 if (Data)
1444 vp8_write_bit(bc, 1);
1446 if (Data < 0)
1448 Data = - Data;
1449 vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
1450 vp8_write_bit(bc, 1);
1452 else
1454 vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
1455 vp8_write_bit(bc, 0);
1458 else
1459 vp8_write_bit(bc, 0);
1464 if (xd->update_mb_segmentation_map)
1466 // Write the probs used to decode the segment id for each macro block.
1467 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++)
1469 int Data = xd->mb_segment_tree_probs[i];
1471 if (Data != 255)
1473 vp8_write_bit(bc, 1);
1474 vp8_write_literal(bc, Data, 8);
1476 else
1477 vp8_write_bit(bc, 0);
1482 // Code to determine whether or not to update the scan order.
1483 vp8_write_bit(bc, pc->filter_type);
1484 vp8_write_literal(bc, pc->filter_level, 6);
1485 vp8_write_literal(bc, pc->sharpness_level, 3);
1487 // Write out loop filter deltas applied at the MB level based on mode or ref frame (if they are enabled).
1488 vp8_write_bit(bc, (xd->mode_ref_lf_delta_enabled) ? 1 : 0);
1490 if (xd->mode_ref_lf_delta_enabled)
1492 // Do the deltas need to be updated
1493 int send_update = xd->mode_ref_lf_delta_update
1494 || cpi->oxcf.error_resilient_mode;
1496 vp8_write_bit(bc, send_update);
1497 if (send_update)
1499 int Data;
1501 // Send update
1502 for (i = 0; i < MAX_REF_LF_DELTAS; i++)
1504 Data = xd->ref_lf_deltas[i];
1506 // Frame level data
1507 if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i]
1508 || cpi->oxcf.error_resilient_mode)
1510 xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i];
1511 vp8_write_bit(bc, 1);
1513 if (Data > 0)
1515 vp8_write_literal(bc, (Data & 0x3F), 6);
1516 vp8_write_bit(bc, 0); // sign
1518 else
1520 Data = -Data;
1521 vp8_write_literal(bc, (Data & 0x3F), 6);
1522 vp8_write_bit(bc, 1); // sign
1525 else
1526 vp8_write_bit(bc, 0);
1529 // Send update
1530 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
1532 Data = xd->mode_lf_deltas[i];
1534 if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i]
1535 || cpi->oxcf.error_resilient_mode)
1537 xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i];
1538 vp8_write_bit(bc, 1);
1540 if (Data > 0)
1542 vp8_write_literal(bc, (Data & 0x3F), 6);
1543 vp8_write_bit(bc, 0); // sign
1545 else
1547 Data = -Data;
1548 vp8_write_literal(bc, (Data & 0x3F), 6);
1549 vp8_write_bit(bc, 1); // sign
1552 else
1553 vp8_write_bit(bc, 0);
1558 //signal here is multi token partition is enabled
1559 vp8_write_literal(bc, pc->multi_token_partition, 2);
1561 // Frame Qbaseline quantizer index
1562 vp8_write_literal(bc, pc->base_qindex, 7);
1564 // Transmit Dc, Second order and Uv quantizer delta information
1565 put_delta_q(bc, pc->y1dc_delta_q);
1566 put_delta_q(bc, pc->y2dc_delta_q);
1567 put_delta_q(bc, pc->y2ac_delta_q);
1568 put_delta_q(bc, pc->uvdc_delta_q);
1569 put_delta_q(bc, pc->uvac_delta_q);
1571 // When there is a key frame all reference buffers are updated using the new key frame
1572 if (pc->frame_type != KEY_FRAME)
1574 // Should the GF or ARF be updated using the transmitted frame or buffer
1575 vp8_write_bit(bc, pc->refresh_golden_frame);
1576 vp8_write_bit(bc, pc->refresh_alt_ref_frame);
1578 // If not being updated from current frame should either GF or ARF be updated from another buffer
1579 if (!pc->refresh_golden_frame)
1580 vp8_write_literal(bc, pc->copy_buffer_to_gf, 2);
1582 if (!pc->refresh_alt_ref_frame)
1583 vp8_write_literal(bc, pc->copy_buffer_to_arf, 2);
1585 // Indicate reference frame sign bias for Golden and ARF frames (always 0 for last frame buffer)
1586 vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]);
1587 vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]);
1590 vp8_write_bit(bc, pc->refresh_entropy_probs);
1592 if (pc->frame_type != KEY_FRAME)
1593 vp8_write_bit(bc, pc->refresh_last_frame);
1595 #ifdef ENTROPY_STATS
1597 if (pc->frame_type == INTER_FRAME)
1598 active_section = 0;
1599 else
1600 active_section = 7;
1602 #endif
1604 vp8_clear_system_state(); //__asm emms;
1606 //************************************************
1607 // save a copy for later refresh
1609 vpx_memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc));
1612 update_coef_probs(cpi);
1614 #ifdef ENTROPY_STATS
1615 active_section = 2;
1616 #endif
1618 // Write out the mb_no_coeff_skip flag
1619 vp8_write_bit(bc, pc->mb_no_coeff_skip);
1621 if (pc->frame_type == KEY_FRAME)
1623 write_kfmodes(cpi);
1625 #ifdef ENTROPY_STATS
1626 active_section = 8;
1627 #endif
1629 else
1631 pack_inter_mode_mvs(cpi);
1633 #ifdef ENTROPY_STATS
1634 active_section = 1;
1635 #endif
1638 vp8_stop_encode(bc);
1641 if (pc->multi_token_partition != ONE_PARTITION)
1643 int num_part;
1644 int asize;
1645 num_part = 1 << pc->multi_token_partition;
1647 pack_tokens_into_partitions(cpi, cx_data + bc->pos, num_part, &asize);
1649 oh.first_partition_length_in_bytes = cpi->bc.pos;
1651 *size = cpi->bc.pos + VP8_HEADER_SIZE + asize + extra_bytes_packed;
1653 else
1655 vp8_start_encode(&cpi->bc2, cx_data + bc->pos);
1657 if (!cpi->b_multi_threaded)
1658 pack_tokens(&cpi->bc2, cpi->tok, cpi->tok_count);
1659 else
1660 pack_mb_row_tokens(cpi, &cpi->bc2);
1662 vp8_stop_encode(&cpi->bc2);
1663 oh.first_partition_length_in_bytes = cpi->bc.pos ;
1664 *size = cpi->bc2.pos + cpi->bc.pos + VP8_HEADER_SIZE + extra_bytes_packed;
1667 #if CONFIG_BIG_ENDIAN
1669 int v = (oh.first_partition_length_in_bytes << 5) |
1670 (oh.show_frame << 4) |
1671 (oh.version << 1) |
1672 oh.type;
1674 v = make_endian_32(v);
1675 vpx_memcpy(dest, &v, 3);
1677 #else
1678 vpx_memcpy(dest, &oh, 3);
1679 #endif
1682 #ifdef ENTROPY_STATS
1683 void print_tree_update_probs()
1685 int i, j, k, l;
1686 FILE *f = fopen("context.c", "a");
1687 int Sum;
1688 fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
1689 fprintf(f, "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [vp8_coef_tokens-1] = {\n");
1691 for (i = 0; i < BLOCK_TYPES; i++)
1693 fprintf(f, " { \n");
1695 for (j = 0; j < COEF_BANDS; j++)
1697 fprintf(f, " {\n");
1699 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
1701 fprintf(f, " {");
1703 for (l = 0; l < MAX_ENTROPY_TOKENS - 1; l++)
1705 Sum = tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1];
1707 if (Sum > 0)
1709 if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0)
1710 fprintf(f, "%3ld, ", (tree_update_hist[i][j][k][l][0] * 255) / Sum);
1711 else
1712 fprintf(f, "%3ld, ", 1);
1714 else
1715 fprintf(f, "%3ld, ", 128);
1718 fprintf(f, "},\n");
1721 fprintf(f, " },\n");
1724 fprintf(f, " },\n");
1727 fprintf(f, "};\n");
1728 fclose(f);
1730 #endif