media/libvpx/vp8/encoder/bitstream.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp8/encoder/bitstream.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1738 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +
    1.15 +#include "vp8/common/header.h"
    1.16 +#include "encodemv.h"
    1.17 +#include "vp8/common/entropymode.h"
    1.18 +#include "vp8/common/findnearmv.h"
    1.19 +#include "mcomp.h"
    1.20 +#include "vp8/common/systemdependent.h"
    1.21 +#include <assert.h>
    1.22 +#include <stdio.h>
    1.23 +#include <limits.h>
    1.24 +#include "vp8/common/pragmas.h"
    1.25 +#include "vpx/vpx_encoder.h"
    1.26 +#include "vpx_mem/vpx_mem.h"
    1.27 +#include "bitstream.h"
    1.28 +
    1.29 +#include "defaultcoefcounts.h"
    1.30 +#include "vp8/common/common.h"
    1.31 +
    1.32 +const int vp8cx_base_skip_false_prob[128] =
    1.33 +{
    1.34 +    255, 255, 255, 255, 255, 255, 255, 255,
    1.35 +    255, 255, 255, 255, 255, 255, 255, 255,
    1.36 +    255, 255, 255, 255, 255, 255, 255, 255,
    1.37 +    255, 255, 255, 255, 255, 255, 255, 255,
    1.38 +    255, 255, 255, 255, 255, 255, 255, 255,
    1.39 +    255, 255, 255, 255, 255, 255, 255, 255,
    1.40 +    255, 255, 255, 255, 255, 255, 255, 255,
    1.41 +    251, 248, 244, 240, 236, 232, 229, 225,
    1.42 +    221, 217, 213, 208, 204, 199, 194, 190,
    1.43 +    187, 183, 179, 175, 172, 168, 164, 160,
    1.44 +    157, 153, 149, 145, 142, 138, 134, 130,
    1.45 +    127, 124, 120, 117, 114, 110, 107, 104,
    1.46 +    101, 98,  95,  92,  89,  86,  83, 80,
    1.47 +    77,  74,  71,  68,  65,  62,  59, 56,
    1.48 +    53,  50,  47,  44,  41,  38,  35, 32,
    1.49 +    30,  28,  26,  24,  22,  20,  18, 16,
    1.50 +};
    1.51 +
    1.52 +#if defined(SECTIONBITS_OUTPUT)
    1.53 +unsigned __int64 Sectionbits[500];
    1.54 +#endif
    1.55 +
    1.56 +#ifdef VP8_ENTROPY_STATS
    1.57 +int intra_mode_stats[10][10][10];
    1.58 +static unsigned int tree_update_hist [BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] [2];
    1.59 +extern unsigned int active_section;
    1.60 +#endif
    1.61 +
    1.62 +#ifdef MODE_STATS
    1.63 +int count_mb_seg[4] = { 0, 0, 0, 0 };
    1.64 +#endif
    1.65 +
    1.66 +
    1.67 +static void update_mode(
    1.68 +    vp8_writer *const w,
    1.69 +    int n,
    1.70 +    vp8_token tok               [/* n */],
    1.71 +    vp8_tree tree,
    1.72 +    vp8_prob Pnew               [/* n-1 */],
    1.73 +    vp8_prob Pcur               [/* n-1 */],
    1.74 +    unsigned int bct            [/* n-1 */] [2],
    1.75 +    const unsigned int num_events[/* n */]
    1.76 +)
    1.77 +{
    1.78 +    unsigned int new_b = 0, old_b = 0;
    1.79 +    int i = 0;
    1.80 +
    1.81 +    vp8_tree_probs_from_distribution(
    1.82 +        n--, tok, tree,
    1.83 +        Pnew, bct, num_events,
    1.84 +        256, 1
    1.85 +    );
    1.86 +
    1.87 +    do
    1.88 +    {
    1.89 +        new_b += vp8_cost_branch(bct[i], Pnew[i]);
    1.90 +        old_b += vp8_cost_branch(bct[i], Pcur[i]);
    1.91 +    }
    1.92 +    while (++i < n);
    1.93 +
    1.94 +    if (new_b + (n << 8) < old_b)
    1.95 +    {
    1.96 +        int j = 0;
    1.97 +
    1.98 +        vp8_write_bit(w, 1);
    1.99 +
   1.100 +        do
   1.101 +        {
   1.102 +            const vp8_prob p = Pnew[j];
   1.103 +
   1.104 +            vp8_write_literal(w, Pcur[j] = p ? p : 1, 8);
   1.105 +        }
   1.106 +        while (++j < n);
   1.107 +    }
   1.108 +    else
   1.109 +        vp8_write_bit(w, 0);
   1.110 +}
   1.111 +
   1.112 +static void update_mbintra_mode_probs(VP8_COMP *cpi)
   1.113 +{
   1.114 +    VP8_COMMON *const x = & cpi->common;
   1.115 +
   1.116 +    vp8_writer *const w = cpi->bc;
   1.117 +
   1.118 +    {
   1.119 +        vp8_prob Pnew   [VP8_YMODES-1];
   1.120 +        unsigned int bct [VP8_YMODES-1] [2];
   1.121 +
   1.122 +        update_mode(
   1.123 +            w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree,
   1.124 +            Pnew, x->fc.ymode_prob, bct, (unsigned int *)cpi->mb.ymode_count
   1.125 +        );
   1.126 +    }
   1.127 +    {
   1.128 +        vp8_prob Pnew   [VP8_UV_MODES-1];
   1.129 +        unsigned int bct [VP8_UV_MODES-1] [2];
   1.130 +
   1.131 +        update_mode(
   1.132 +            w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree,
   1.133 +            Pnew, x->fc.uv_mode_prob, bct, (unsigned int *)cpi->mb.uv_mode_count
   1.134 +        );
   1.135 +    }
   1.136 +}
   1.137 +
   1.138 +static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p)
   1.139 +{
   1.140 +    vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m);
   1.141 +}
   1.142 +
   1.143 +static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p)
   1.144 +{
   1.145 +    vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m);
   1.146 +}
   1.147 +
   1.148 +static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p)
   1.149 +{
   1.150 +    vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m);
   1.151 +}
   1.152 +
   1.153 +
   1.154 +static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p)
   1.155 +{
   1.156 +    vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m);
   1.157 +}
   1.158 +
   1.159 +static void write_split(vp8_writer *bc, int x)
   1.160 +{
   1.161 +    vp8_write_token(
   1.162 +        bc, vp8_mbsplit_tree, vp8_mbsplit_probs, vp8_mbsplit_encodings + x
   1.163 +    );
   1.164 +}
   1.165 +
   1.166 +void vp8_pack_tokens_c(vp8_writer *w, const TOKENEXTRA *p, int xcount)
   1.167 +{
   1.168 +    const TOKENEXTRA *stop = p + xcount;
   1.169 +    unsigned int split;
   1.170 +    unsigned int shift;
   1.171 +    int count = w->count;
   1.172 +    unsigned int range = w->range;
   1.173 +    unsigned int lowvalue = w->lowvalue;
   1.174 +
   1.175 +    while (p < stop)
   1.176 +    {
   1.177 +        const int t = p->Token;
   1.178 +        vp8_token *a = vp8_coef_encodings + t;
   1.179 +        const vp8_extra_bit_struct *b = vp8_extra_bits + t;
   1.180 +        int i = 0;
   1.181 +        const unsigned char *pp = p->context_tree;
   1.182 +        int v = a->value;
   1.183 +        int n = a->Len;
   1.184 +
   1.185 +        if (p->skip_eob_node)
   1.186 +        {
   1.187 +            n--;
   1.188 +            i = 2;
   1.189 +        }
   1.190 +
   1.191 +        do
   1.192 +        {
   1.193 +            const int bb = (v >> --n) & 1;
   1.194 +            split = 1 + (((range - 1) * pp[i>>1]) >> 8);
   1.195 +            i = vp8_coef_tree[i+bb];
   1.196 +
   1.197 +            if (bb)
   1.198 +            {
   1.199 +                lowvalue += split;
   1.200 +                range = range - split;
   1.201 +            }
   1.202 +            else
   1.203 +            {
   1.204 +                range = split;
   1.205 +            }
   1.206 +
   1.207 +            shift = vp8_norm[range];
   1.208 +            range <<= shift;
   1.209 +            count += shift;
   1.210 +
   1.211 +            if (count >= 0)
   1.212 +            {
   1.213 +                int offset = shift - count;
   1.214 +
   1.215 +                if ((lowvalue << (offset - 1)) & 0x80000000)
   1.216 +                {
   1.217 +                    int x = w->pos - 1;
   1.218 +
   1.219 +                    while (x >= 0 && w->buffer[x] == 0xff)
   1.220 +                    {
   1.221 +                        w->buffer[x] = (unsigned char)0;
   1.222 +                        x--;
   1.223 +                    }
   1.224 +
   1.225 +                    w->buffer[x] += 1;
   1.226 +                }
   1.227 +
   1.228 +                validate_buffer(w->buffer + w->pos,
   1.229 +                                1,
   1.230 +                                w->buffer_end,
   1.231 +                                w->error);
   1.232 +
   1.233 +                w->buffer[w->pos++] = (lowvalue >> (24 - offset));
   1.234 +                lowvalue <<= offset;
   1.235 +                shift = count;
   1.236 +                lowvalue &= 0xffffff;
   1.237 +                count -= 8 ;
   1.238 +            }
   1.239 +
   1.240 +            lowvalue <<= shift;
   1.241 +        }
   1.242 +        while (n);
   1.243 +
   1.244 +
   1.245 +        if (b->base_val)
   1.246 +        {
   1.247 +            const int e = p->Extra, L = b->Len;
   1.248 +
   1.249 +            if (L)
   1.250 +            {
   1.251 +                const unsigned char *proba = b->prob;
   1.252 +                const int v2 = e >> 1;
   1.253 +                int n2 = L;              /* number of bits in v2, assumed nonzero */
   1.254 +                i = 0;
   1.255 +
   1.256 +                do
   1.257 +                {
   1.258 +                    const int bb = (v2 >> --n2) & 1;
   1.259 +                    split = 1 + (((range - 1) * proba[i>>1]) >> 8);
   1.260 +                    i = b->tree[i+bb];
   1.261 +
   1.262 +                    if (bb)
   1.263 +                    {
   1.264 +                        lowvalue += split;
   1.265 +                        range = range - split;
   1.266 +                    }
   1.267 +                    else
   1.268 +                    {
   1.269 +                        range = split;
   1.270 +                    }
   1.271 +
   1.272 +                    shift = vp8_norm[range];
   1.273 +                    range <<= shift;
   1.274 +                    count += shift;
   1.275 +
   1.276 +                    if (count >= 0)
   1.277 +                    {
   1.278 +                        int offset = shift - count;
   1.279 +
   1.280 +                        if ((lowvalue << (offset - 1)) & 0x80000000)
   1.281 +                        {
   1.282 +                            int x = w->pos - 1;
   1.283 +
   1.284 +                            while (x >= 0 && w->buffer[x] == 0xff)
   1.285 +                            {
   1.286 +                                w->buffer[x] = (unsigned char)0;
   1.287 +                                x--;
   1.288 +                            }
   1.289 +
   1.290 +                            w->buffer[x] += 1;
   1.291 +                        }
   1.292 +
   1.293 +                        validate_buffer(w->buffer + w->pos,
   1.294 +                                        1,
   1.295 +                                        w->buffer_end,
   1.296 +                                        w->error);
   1.297 +
   1.298 +                        w->buffer[w->pos++] = (lowvalue >> (24 - offset));
   1.299 +                        lowvalue <<= offset;
   1.300 +                        shift = count;
   1.301 +                        lowvalue &= 0xffffff;
   1.302 +                        count -= 8 ;
   1.303 +                    }
   1.304 +
   1.305 +                    lowvalue <<= shift;
   1.306 +                }
   1.307 +                while (n2);
   1.308 +            }
   1.309 +
   1.310 +
   1.311 +            {
   1.312 +
   1.313 +                split = (range + 1) >> 1;
   1.314 +
   1.315 +                if (e & 1)
   1.316 +                {
   1.317 +                    lowvalue += split;
   1.318 +                    range = range - split;
   1.319 +                }
   1.320 +                else
   1.321 +                {
   1.322 +                    range = split;
   1.323 +                }
   1.324 +
   1.325 +                range <<= 1;
   1.326 +
   1.327 +                if ((lowvalue & 0x80000000))
   1.328 +                {
   1.329 +                    int x = w->pos - 1;
   1.330 +
   1.331 +                    while (x >= 0 && w->buffer[x] == 0xff)
   1.332 +                    {
   1.333 +                        w->buffer[x] = (unsigned char)0;
   1.334 +                        x--;
   1.335 +                    }
   1.336 +
   1.337 +                    w->buffer[x] += 1;
   1.338 +
   1.339 +                }
   1.340 +
   1.341 +                lowvalue  <<= 1;
   1.342 +
   1.343 +                if (!++count)
   1.344 +                {
   1.345 +                    count = -8;
   1.346 +
   1.347 +                    validate_buffer(w->buffer + w->pos,
   1.348 +                                    1,
   1.349 +                                    w->buffer_end,
   1.350 +                                    w->error);
   1.351 +
   1.352 +                    w->buffer[w->pos++] = (lowvalue >> 24);
   1.353 +                    lowvalue &= 0xffffff;
   1.354 +                }
   1.355 +            }
   1.356 +
   1.357 +        }
   1.358 +
   1.359 +        ++p;
   1.360 +    }
   1.361 +
   1.362 +    w->count = count;
   1.363 +    w->lowvalue = lowvalue;
   1.364 +    w->range = range;
   1.365 +
   1.366 +}
   1.367 +
   1.368 +static void write_partition_size(unsigned char *cx_data, int size)
   1.369 +{
   1.370 +    signed char csize;
   1.371 +
   1.372 +    csize = size & 0xff;
   1.373 +    *cx_data = csize;
   1.374 +    csize = (size >> 8) & 0xff;
   1.375 +    *(cx_data + 1) = csize;
   1.376 +    csize = (size >> 16) & 0xff;
   1.377 +    *(cx_data + 2) = csize;
   1.378 +
   1.379 +}
   1.380 +
   1.381 +static void pack_tokens_into_partitions_c(VP8_COMP *cpi, unsigned char *cx_data,
   1.382 +                                          unsigned char * cx_data_end,
   1.383 +                                          int num_part)
   1.384 +{
   1.385 +
   1.386 +    int i;
   1.387 +    unsigned char *ptr = cx_data;
   1.388 +    unsigned char *ptr_end = cx_data_end;
   1.389 +    vp8_writer * w;
   1.390 +
   1.391 +    for (i = 0; i < num_part; i++)
   1.392 +    {
   1.393 +        int mb_row;
   1.394 +
   1.395 +        w = cpi->bc + i + 1;
   1.396 +
   1.397 +        vp8_start_encode(w, ptr, ptr_end);
   1.398 +
   1.399 +        for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part)
   1.400 +        {
   1.401 +            const TOKENEXTRA *p    = cpi->tplist[mb_row].start;
   1.402 +            const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
   1.403 +            int tokens = (int)(stop - p);
   1.404 +
   1.405 +            vp8_pack_tokens_c(w, p, tokens);
   1.406 +        }
   1.407 +
   1.408 +        vp8_stop_encode(w);
   1.409 +        ptr += w->pos;
   1.410 +    }
   1.411 +}
   1.412 +
   1.413 +
   1.414 +static void pack_mb_row_tokens_c(VP8_COMP *cpi, vp8_writer *w)
   1.415 +{
   1.416 +    int mb_row;
   1.417 +
   1.418 +    for (mb_row = 0; mb_row < cpi->common.mb_rows; mb_row++)
   1.419 +    {
   1.420 +        const TOKENEXTRA *p    = cpi->tplist[mb_row].start;
   1.421 +        const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
   1.422 +        int tokens = (int)(stop - p);
   1.423 +
   1.424 +        vp8_pack_tokens_c(w, p, tokens);
   1.425 +    }
   1.426 +
   1.427 +}
   1.428 +
   1.429 +static void write_mv_ref
   1.430 +(
   1.431 +    vp8_writer *w, MB_PREDICTION_MODE m, const vp8_prob *p
   1.432 +)
   1.433 +{
   1.434 +#if CONFIG_DEBUG
   1.435 +    assert(NEARESTMV <= m  &&  m <= SPLITMV);
   1.436 +#endif
   1.437 +    vp8_write_token(w, vp8_mv_ref_tree, p,
   1.438 +                    vp8_mv_ref_encoding_array + (m - NEARESTMV));
   1.439 +}
   1.440 +
   1.441 +static void write_sub_mv_ref
   1.442 +(
   1.443 +    vp8_writer *w, B_PREDICTION_MODE m, const vp8_prob *p
   1.444 +)
   1.445 +{
   1.446 +#if CONFIG_DEBUG
   1.447 +    assert(LEFT4X4 <= m  &&  m <= NEW4X4);
   1.448 +#endif
   1.449 +    vp8_write_token(w, vp8_sub_mv_ref_tree, p,
   1.450 +                    vp8_sub_mv_ref_encoding_array + (m - LEFT4X4));
   1.451 +}
   1.452 +
   1.453 +static void write_mv
   1.454 +(
   1.455 +    vp8_writer *w, const MV *mv, const int_mv *ref, const MV_CONTEXT *mvc
   1.456 +)
   1.457 +{
   1.458 +    MV e;
   1.459 +    e.row = mv->row - ref->as_mv.row;
   1.460 +    e.col = mv->col - ref->as_mv.col;
   1.461 +
   1.462 +    vp8_encode_motion_vector(w, &e, mvc);
   1.463 +}
   1.464 +
   1.465 +static void write_mb_features(vp8_writer *w, const MB_MODE_INFO *mi, const MACROBLOCKD *x)
   1.466 +{
   1.467 +    /* Encode the MB segment id. */
   1.468 +    if (x->segmentation_enabled && x->update_mb_segmentation_map)
   1.469 +    {
   1.470 +        switch (mi->segment_id)
   1.471 +        {
   1.472 +        case 0:
   1.473 +            vp8_write(w, 0, x->mb_segment_tree_probs[0]);
   1.474 +            vp8_write(w, 0, x->mb_segment_tree_probs[1]);
   1.475 +            break;
   1.476 +        case 1:
   1.477 +            vp8_write(w, 0, x->mb_segment_tree_probs[0]);
   1.478 +            vp8_write(w, 1, x->mb_segment_tree_probs[1]);
   1.479 +            break;
   1.480 +        case 2:
   1.481 +            vp8_write(w, 1, x->mb_segment_tree_probs[0]);
   1.482 +            vp8_write(w, 0, x->mb_segment_tree_probs[2]);
   1.483 +            break;
   1.484 +        case 3:
   1.485 +            vp8_write(w, 1, x->mb_segment_tree_probs[0]);
   1.486 +            vp8_write(w, 1, x->mb_segment_tree_probs[2]);
   1.487 +            break;
   1.488 +
   1.489 +            /* TRAP.. This should not happen */
   1.490 +        default:
   1.491 +            vp8_write(w, 0, x->mb_segment_tree_probs[0]);
   1.492 +            vp8_write(w, 0, x->mb_segment_tree_probs[1]);
   1.493 +            break;
   1.494 +        }
   1.495 +    }
   1.496 +}
   1.497 +void vp8_convert_rfct_to_prob(VP8_COMP *const cpi)
   1.498 +{
   1.499 +    const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
   1.500 +    const int rf_intra = rfct[INTRA_FRAME];
   1.501 +    const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
   1.502 +
   1.503 +    /* Calculate the probabilities used to code the ref frame based on usage */
   1.504 +    if (!(cpi->prob_intra_coded = rf_intra * 255 / (rf_intra + rf_inter)))
   1.505 +        cpi->prob_intra_coded = 1;
   1.506 +
   1.507 +    cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
   1.508 +
   1.509 +    if (!cpi->prob_last_coded)
   1.510 +        cpi->prob_last_coded = 1;
   1.511 +
   1.512 +    cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
   1.513 +                  ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
   1.514 +
   1.515 +    if (!cpi->prob_gf_coded)
   1.516 +        cpi->prob_gf_coded = 1;
   1.517 +
   1.518 +}
   1.519 +
   1.520 +static void pack_inter_mode_mvs(VP8_COMP *const cpi)
   1.521 +{
   1.522 +    VP8_COMMON *const pc = & cpi->common;
   1.523 +    vp8_writer *const w = cpi->bc;
   1.524 +    const MV_CONTEXT *mvc = pc->fc.mvc;
   1.525 +
   1.526 +
   1.527 +    MODE_INFO *m = pc->mi;
   1.528 +    const int mis = pc->mode_info_stride;
   1.529 +    int mb_row = -1;
   1.530 +
   1.531 +    int prob_skip_false = 0;
   1.532 +
   1.533 +    cpi->mb.partition_info = cpi->mb.pi;
   1.534 +
   1.535 +    vp8_convert_rfct_to_prob(cpi);
   1.536 +
   1.537 +#ifdef VP8_ENTROPY_STATS
   1.538 +    active_section = 1;
   1.539 +#endif
   1.540 +
   1.541 +    if (pc->mb_no_coeff_skip)
   1.542 +    {
   1.543 +        int total_mbs = pc->mb_rows * pc->mb_cols;
   1.544 +
   1.545 +        prob_skip_false = (total_mbs - cpi->mb.skip_true_count ) * 256 / total_mbs;
   1.546 +
   1.547 +        if (prob_skip_false <= 1)
   1.548 +            prob_skip_false = 1;
   1.549 +
   1.550 +        if (prob_skip_false > 255)
   1.551 +            prob_skip_false = 255;
   1.552 +
   1.553 +        cpi->prob_skip_false = prob_skip_false;
   1.554 +        vp8_write_literal(w, prob_skip_false, 8);
   1.555 +    }
   1.556 +
   1.557 +    vp8_write_literal(w, cpi->prob_intra_coded, 8);
   1.558 +    vp8_write_literal(w, cpi->prob_last_coded, 8);
   1.559 +    vp8_write_literal(w, cpi->prob_gf_coded, 8);
   1.560 +
   1.561 +    update_mbintra_mode_probs(cpi);
   1.562 +
   1.563 +    vp8_write_mvprobs(cpi);
   1.564 +
   1.565 +    while (++mb_row < pc->mb_rows)
   1.566 +    {
   1.567 +        int mb_col = -1;
   1.568 +
   1.569 +        while (++mb_col < pc->mb_cols)
   1.570 +        {
   1.571 +            const MB_MODE_INFO *const mi = & m->mbmi;
   1.572 +            const MV_REFERENCE_FRAME rf = mi->ref_frame;
   1.573 +            const MB_PREDICTION_MODE mode = mi->mode;
   1.574 +
   1.575 +            MACROBLOCKD *xd = &cpi->mb.e_mbd;
   1.576 +
   1.577 +            /* Distance of Mb to the various image edges.
   1.578 +             * These specified to 8th pel as they are always compared to MV
   1.579 +             * values that are in 1/8th pel units
   1.580 +             */
   1.581 +            xd->mb_to_left_edge = -((mb_col * 16) << 3);
   1.582 +            xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
   1.583 +            xd->mb_to_top_edge = -((mb_row * 16) << 3);
   1.584 +            xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
   1.585 +
   1.586 +#ifdef VP8_ENTROPY_STATS
   1.587 +            active_section = 9;
   1.588 +#endif
   1.589 +
   1.590 +            if (cpi->mb.e_mbd.update_mb_segmentation_map)
   1.591 +                write_mb_features(w, mi, &cpi->mb.e_mbd);
   1.592 +
   1.593 +            if (pc->mb_no_coeff_skip)
   1.594 +                vp8_encode_bool(w, m->mbmi.mb_skip_coeff, prob_skip_false);
   1.595 +
   1.596 +            if (rf == INTRA_FRAME)
   1.597 +            {
   1.598 +                vp8_write(w, 0, cpi->prob_intra_coded);
   1.599 +#ifdef VP8_ENTROPY_STATS
   1.600 +                active_section = 6;
   1.601 +#endif
   1.602 +                write_ymode(w, mode, pc->fc.ymode_prob);
   1.603 +
   1.604 +                if (mode == B_PRED)
   1.605 +                {
   1.606 +                    int j = 0;
   1.607 +
   1.608 +                    do
   1.609 +                        write_bmode(w, m->bmi[j].as_mode, pc->fc.bmode_prob);
   1.610 +                    while (++j < 16);
   1.611 +                }
   1.612 +
   1.613 +                write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob);
   1.614 +            }
   1.615 +            else    /* inter coded */
   1.616 +            {
   1.617 +                int_mv best_mv;
   1.618 +                vp8_prob mv_ref_p [VP8_MVREFS-1];
   1.619 +
   1.620 +                vp8_write(w, 1, cpi->prob_intra_coded);
   1.621 +
   1.622 +                if (rf == LAST_FRAME)
   1.623 +                    vp8_write(w, 0, cpi->prob_last_coded);
   1.624 +                else
   1.625 +                {
   1.626 +                    vp8_write(w, 1, cpi->prob_last_coded);
   1.627 +                    vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, cpi->prob_gf_coded);
   1.628 +                }
   1.629 +
   1.630 +                {
   1.631 +                    int_mv n1, n2;
   1.632 +                    int ct[4];
   1.633 +
   1.634 +                    vp8_find_near_mvs(xd, m, &n1, &n2, &best_mv, ct, rf, cpi->common.ref_frame_sign_bias);
   1.635 +                    vp8_clamp_mv2(&best_mv, xd);
   1.636 +
   1.637 +                    vp8_mv_ref_probs(mv_ref_p, ct);
   1.638 +
   1.639 +#ifdef VP8_ENTROPY_STATS
   1.640 +                    accum_mv_refs(mode, ct);
   1.641 +#endif
   1.642 +
   1.643 +                }
   1.644 +
   1.645 +#ifdef VP8_ENTROPY_STATS
   1.646 +                active_section = 3;
   1.647 +#endif
   1.648 +
   1.649 +                write_mv_ref(w, mode, mv_ref_p);
   1.650 +
   1.651 +                switch (mode)   /* new, split require MVs */
   1.652 +                {
   1.653 +                case NEWMV:
   1.654 +
   1.655 +#ifdef VP8_ENTROPY_STATS
   1.656 +                    active_section = 5;
   1.657 +#endif
   1.658 +
   1.659 +                    write_mv(w, &mi->mv.as_mv, &best_mv, mvc);
   1.660 +                    break;
   1.661 +
   1.662 +                case SPLITMV:
   1.663 +                {
   1.664 +                    int j = 0;
   1.665 +
   1.666 +#ifdef MODE_STATS
   1.667 +                    ++count_mb_seg [mi->partitioning];
   1.668 +#endif
   1.669 +
   1.670 +                    write_split(w, mi->partitioning);
   1.671 +
   1.672 +                    do
   1.673 +                    {
   1.674 +                        B_PREDICTION_MODE blockmode;
   1.675 +                        int_mv blockmv;
   1.676 +                        const int *const  L = vp8_mbsplits [mi->partitioning];
   1.677 +                        int k = -1;  /* first block in subset j */
   1.678 +                        int mv_contz;
   1.679 +                        int_mv leftmv, abovemv;
   1.680 +
   1.681 +                        blockmode =  cpi->mb.partition_info->bmi[j].mode;
   1.682 +                        blockmv =  cpi->mb.partition_info->bmi[j].mv;
   1.683 +#if CONFIG_DEBUG
   1.684 +                        while (j != L[++k])
   1.685 +                            if (k >= 16)
   1.686 +                                assert(0);
   1.687 +#else
   1.688 +                        while (j != L[++k]);
   1.689 +#endif
   1.690 +                        leftmv.as_int = left_block_mv(m, k);
   1.691 +                        abovemv.as_int = above_block_mv(m, k, mis);
   1.692 +                        mv_contz = vp8_mv_cont(&leftmv, &abovemv);
   1.693 +
   1.694 +                        write_sub_mv_ref(w, blockmode, vp8_sub_mv_ref_prob2 [mv_contz]);
   1.695 +
   1.696 +                        if (blockmode == NEW4X4)
   1.697 +                        {
   1.698 +#ifdef VP8_ENTROPY_STATS
   1.699 +                            active_section = 11;
   1.700 +#endif
   1.701 +                            write_mv(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT *) mvc);
   1.702 +                        }
   1.703 +                    }
   1.704 +                    while (++j < cpi->mb.partition_info->count);
   1.705 +                }
   1.706 +                break;
   1.707 +                default:
   1.708 +                    break;
   1.709 +                }
   1.710 +            }
   1.711 +
   1.712 +            ++m;
   1.713 +            cpi->mb.partition_info++;
   1.714 +        }
   1.715 +
   1.716 +        ++m;  /* skip L prediction border */
   1.717 +        cpi->mb.partition_info++;
   1.718 +    }
   1.719 +}
   1.720 +
   1.721 +
   1.722 +static void write_kfmodes(VP8_COMP *cpi)
   1.723 +{
   1.724 +    vp8_writer *const bc = cpi->bc;
   1.725 +    const VP8_COMMON *const c = & cpi->common;
   1.726 +    /* const */
   1.727 +    MODE_INFO *m = c->mi;
   1.728 +
   1.729 +    int mb_row = -1;
   1.730 +    int prob_skip_false = 0;
   1.731 +
   1.732 +    if (c->mb_no_coeff_skip)
   1.733 +    {
   1.734 +        int total_mbs = c->mb_rows * c->mb_cols;
   1.735 +
   1.736 +        prob_skip_false = (total_mbs - cpi->mb.skip_true_count ) * 256 / total_mbs;
   1.737 +
   1.738 +        if (prob_skip_false <= 1)
   1.739 +            prob_skip_false = 1;
   1.740 +
   1.741 +        if (prob_skip_false >= 255)
   1.742 +            prob_skip_false = 255;
   1.743 +
   1.744 +        cpi->prob_skip_false = prob_skip_false;
   1.745 +        vp8_write_literal(bc, prob_skip_false, 8);
   1.746 +    }
   1.747 +
   1.748 +    while (++mb_row < c->mb_rows)
   1.749 +    {
   1.750 +        int mb_col = -1;
   1.751 +
   1.752 +        while (++mb_col < c->mb_cols)
   1.753 +        {
   1.754 +            const int ym = m->mbmi.mode;
   1.755 +
   1.756 +            if (cpi->mb.e_mbd.update_mb_segmentation_map)
   1.757 +                write_mb_features(bc, &m->mbmi, &cpi->mb.e_mbd);
   1.758 +
   1.759 +            if (c->mb_no_coeff_skip)
   1.760 +                vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false);
   1.761 +
   1.762 +            kfwrite_ymode(bc, ym, vp8_kf_ymode_prob);
   1.763 +
   1.764 +            if (ym == B_PRED)
   1.765 +            {
   1.766 +                const int mis = c->mode_info_stride;
   1.767 +                int i = 0;
   1.768 +
   1.769 +                do
   1.770 +                {
   1.771 +                    const B_PREDICTION_MODE A = above_block_mode(m, i, mis);
   1.772 +                    const B_PREDICTION_MODE L = left_block_mode(m, i);
   1.773 +                    const int bm = m->bmi[i].as_mode;
   1.774 +
   1.775 +#ifdef VP8_ENTROPY_STATS
   1.776 +                    ++intra_mode_stats [A] [L] [bm];
   1.777 +#endif
   1.778 +
   1.779 +                    write_bmode(bc, bm, vp8_kf_bmode_prob [A] [L]);
   1.780 +                }
   1.781 +                while (++i < 16);
   1.782 +            }
   1.783 +
   1.784 +            write_uv_mode(bc, (m++)->mbmi.uv_mode, vp8_kf_uv_mode_prob);
   1.785 +        }
   1.786 +
   1.787 +        m++;    /* skip L prediction border */
   1.788 +    }
   1.789 +}
   1.790 +
   1.791 +#if 0
   1.792 +/* This function is used for debugging probability trees. */
   1.793 +static void print_prob_tree(vp8_prob
   1.794 +     coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES])
   1.795 +{
   1.796 +    /* print coef probability tree */
   1.797 +    int i,j,k,l;
   1.798 +    FILE* f = fopen("enc_tree_probs.txt", "a");
   1.799 +    fprintf(f, "{\n");
   1.800 +    for (i = 0; i < BLOCK_TYPES; i++)
   1.801 +    {
   1.802 +        fprintf(f, "  {\n");
   1.803 +        for (j = 0; j < COEF_BANDS; j++)
   1.804 +        {
   1.805 +            fprintf(f, "    {\n");
   1.806 +            for (k = 0; k < PREV_COEF_CONTEXTS; k++)
   1.807 +            {
   1.808 +                fprintf(f, "      {");
   1.809 +                for (l = 0; l < ENTROPY_NODES; l++)
   1.810 +                {
   1.811 +                    fprintf(f, "%3u, ",
   1.812 +                            (unsigned int)(coef_probs [i][j][k][l]));
   1.813 +                }
   1.814 +                fprintf(f, " }\n");
   1.815 +            }
   1.816 +            fprintf(f, "    }\n");
   1.817 +        }
   1.818 +        fprintf(f, "  }\n");
   1.819 +    }
   1.820 +    fprintf(f, "}\n");
   1.821 +    fclose(f);
   1.822 +}
   1.823 +#endif
   1.824 +
   1.825 +static void sum_probs_over_prev_coef_context(
   1.826 +        const unsigned int probs[PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
   1.827 +        unsigned int* out)
   1.828 +{
   1.829 +    int i, j;
   1.830 +    for (i=0; i < MAX_ENTROPY_TOKENS; ++i)
   1.831 +    {
   1.832 +        for (j=0; j < PREV_COEF_CONTEXTS; ++j)
   1.833 +        {
   1.834 +            const unsigned int tmp = out[i];
   1.835 +            out[i] += probs[j][i];
   1.836 +            /* check for wrap */
   1.837 +            if (out[i] < tmp)
   1.838 +                out[i] = UINT_MAX;
   1.839 +        }
   1.840 +    }
   1.841 +}
   1.842 +
   1.843 +static int prob_update_savings(const unsigned int *ct,
   1.844 +                                   const vp8_prob oldp, const vp8_prob newp,
   1.845 +                                   const vp8_prob upd)
   1.846 +{
   1.847 +    const int old_b = vp8_cost_branch(ct, oldp);
   1.848 +    const int new_b = vp8_cost_branch(ct, newp);
   1.849 +    const int update_b = 8 +
   1.850 +                         ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);
   1.851 +
   1.852 +    return old_b - new_b - update_b;
   1.853 +}
   1.854 +
   1.855 +static int independent_coef_context_savings(VP8_COMP *cpi)
   1.856 +{
   1.857 +    MACROBLOCK *const x = & cpi->mb;
   1.858 +    int savings = 0;
   1.859 +    int i = 0;
   1.860 +    do
   1.861 +    {
   1.862 +        int j = 0;
   1.863 +        do
   1.864 +        {
   1.865 +            int k = 0;
   1.866 +            unsigned int prev_coef_count_sum[MAX_ENTROPY_TOKENS] = {0};
   1.867 +            int prev_coef_savings[MAX_ENTROPY_TOKENS] = {0};
   1.868 +            const unsigned int (*probs)[MAX_ENTROPY_TOKENS];
   1.869 +            /* Calculate new probabilities given the constraint that
   1.870 +             * they must be equal over the prev coef contexts
   1.871 +             */
   1.872 +
   1.873 +            probs = (const unsigned int (*)[MAX_ENTROPY_TOKENS])
   1.874 +                x->coef_counts[i][j];
   1.875 +
   1.876 +            /* Reset to default probabilities at key frames */
   1.877 +            if (cpi->common.frame_type == KEY_FRAME)
   1.878 +                probs = default_coef_counts[i][j];
   1.879 +
   1.880 +            sum_probs_over_prev_coef_context(probs, prev_coef_count_sum);
   1.881 +
   1.882 +            do
   1.883 +            {
   1.884 +                /* at every context */
   1.885 +
   1.886 +                /* calc probs and branch cts for this frame only */
   1.887 +                int t = 0;      /* token/prob index */
   1.888 +
   1.889 +                vp8_tree_probs_from_distribution(
   1.890 +                    MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
   1.891 +                    cpi->frame_coef_probs[i][j][k],
   1.892 +                    cpi->frame_branch_ct [i][j][k],
   1.893 +                    prev_coef_count_sum,
   1.894 +                    256, 1);
   1.895 +
   1.896 +                do
   1.897 +                {
   1.898 +                    const unsigned int *ct  = cpi->frame_branch_ct [i][j][k][t];
   1.899 +                    const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
   1.900 +                    const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t];
   1.901 +                    const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
   1.902 +                    const int s = prob_update_savings(ct, oldp, newp, upd);
   1.903 +
   1.904 +                    if (cpi->common.frame_type != KEY_FRAME ||
   1.905 +                        (cpi->common.frame_type == KEY_FRAME && newp != oldp))
   1.906 +                        prev_coef_savings[t] += s;
   1.907 +                }
   1.908 +                while (++t < ENTROPY_NODES);
   1.909 +            }
   1.910 +            while (++k < PREV_COEF_CONTEXTS);
   1.911 +            k = 0;
   1.912 +            do
   1.913 +            {
   1.914 +                /* We only update probabilities if we can save bits, except
   1.915 +                 * for key frames where we have to update all probabilities
   1.916 +                 * to get the equal probabilities across the prev coef
   1.917 +                 * contexts.
   1.918 +                 */
   1.919 +                if (prev_coef_savings[k] > 0 ||
   1.920 +                    cpi->common.frame_type == KEY_FRAME)
   1.921 +                    savings += prev_coef_savings[k];
   1.922 +            }
   1.923 +            while (++k < ENTROPY_NODES);
   1.924 +        }
   1.925 +        while (++j < COEF_BANDS);
   1.926 +    }
   1.927 +    while (++i < BLOCK_TYPES);
   1.928 +    return savings;
   1.929 +}
   1.930 +
   1.931 +static int default_coef_context_savings(VP8_COMP *cpi)
   1.932 +{
   1.933 +    MACROBLOCK *const x = & cpi->mb;
   1.934 +    int savings = 0;
   1.935 +    int i = 0;
   1.936 +    do
   1.937 +    {
   1.938 +        int j = 0;
   1.939 +        do
   1.940 +        {
   1.941 +            int k = 0;
   1.942 +            do
   1.943 +            {
   1.944 +                /* at every context */
   1.945 +
   1.946 +                /* calc probs and branch cts for this frame only */
   1.947 +                int t = 0;      /* token/prob index */
   1.948 +
   1.949 +                vp8_tree_probs_from_distribution(
   1.950 +                    MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
   1.951 +                    cpi->frame_coef_probs [i][j][k],
   1.952 +                    cpi->frame_branch_ct [i][j][k],
   1.953 +                    x->coef_counts [i][j][k],
   1.954 +                    256, 1
   1.955 +                );
   1.956 +
   1.957 +                do
   1.958 +                {
   1.959 +                    const unsigned int *ct  = cpi->frame_branch_ct [i][j][k][t];
   1.960 +                    const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
   1.961 +                    const vp8_prob oldp = cpi->common.fc.coef_probs [i][j][k][t];
   1.962 +                    const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
   1.963 +                    const int s = prob_update_savings(ct, oldp, newp, upd);
   1.964 +
   1.965 +                    if (s > 0)
   1.966 +                    {
   1.967 +                        savings += s;
   1.968 +                    }
   1.969 +                }
   1.970 +                while (++t < ENTROPY_NODES);
   1.971 +            }
   1.972 +            while (++k < PREV_COEF_CONTEXTS);
   1.973 +        }
   1.974 +        while (++j < COEF_BANDS);
   1.975 +    }
   1.976 +    while (++i < BLOCK_TYPES);
   1.977 +    return savings;
   1.978 +}
   1.979 +
   1.980 +void vp8_calc_ref_frame_costs(int *ref_frame_cost,
   1.981 +                              int prob_intra,
   1.982 +                              int prob_last,
   1.983 +                              int prob_garf
   1.984 +                             )
   1.985 +{
   1.986 +    assert(prob_intra >= 0);
   1.987 +    assert(prob_intra <= 255);
   1.988 +    assert(prob_last >= 0);
   1.989 +    assert(prob_last <= 255);
   1.990 +    assert(prob_garf >= 0);
   1.991 +    assert(prob_garf <= 255);
   1.992 +    ref_frame_cost[INTRA_FRAME]   = vp8_cost_zero(prob_intra);
   1.993 +    ref_frame_cost[LAST_FRAME]    = vp8_cost_one(prob_intra)
   1.994 +                                    + vp8_cost_zero(prob_last);
   1.995 +    ref_frame_cost[GOLDEN_FRAME]  = vp8_cost_one(prob_intra)
   1.996 +                                    + vp8_cost_one(prob_last)
   1.997 +                                    + vp8_cost_zero(prob_garf);
   1.998 +    ref_frame_cost[ALTREF_FRAME]  = vp8_cost_one(prob_intra)
   1.999 +                                    + vp8_cost_one(prob_last)
  1.1000 +                                    + vp8_cost_one(prob_garf);
  1.1001 +
  1.1002 +}
  1.1003 +
  1.1004 +int vp8_estimate_entropy_savings(VP8_COMP *cpi)
  1.1005 +{
  1.1006 +    int savings = 0;
  1.1007 +
  1.1008 +    const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
  1.1009 +    const int rf_intra = rfct[INTRA_FRAME];
  1.1010 +    const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
  1.1011 +    int new_intra, new_last, new_garf, oldtotal, newtotal;
  1.1012 +    int ref_frame_cost[MAX_REF_FRAMES];
  1.1013 +
  1.1014 +    vp8_clear_system_state();
  1.1015 +
  1.1016 +    if (cpi->common.frame_type != KEY_FRAME)
  1.1017 +    {
  1.1018 +        if (!(new_intra = rf_intra * 255 / (rf_intra + rf_inter)))
  1.1019 +            new_intra = 1;
  1.1020 +
  1.1021 +        new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
  1.1022 +
  1.1023 +        new_garf = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
  1.1024 +                  ? (rfct[GOLDEN_FRAME] * 255) / (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]) : 128;
  1.1025 +
  1.1026 +
  1.1027 +        vp8_calc_ref_frame_costs(ref_frame_cost,new_intra,new_last,new_garf);
  1.1028 +
  1.1029 +        newtotal =
  1.1030 +            rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
  1.1031 +            rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
  1.1032 +            rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
  1.1033 +            rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
  1.1034 +
  1.1035 +
  1.1036 +        /* old costs */
  1.1037 +        vp8_calc_ref_frame_costs(ref_frame_cost,cpi->prob_intra_coded,
  1.1038 +                                 cpi->prob_last_coded,cpi->prob_gf_coded);
  1.1039 +
  1.1040 +        oldtotal =
  1.1041 +            rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
  1.1042 +            rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
  1.1043 +            rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
  1.1044 +            rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
  1.1045 +
  1.1046 +        savings += (oldtotal - newtotal) / 256;
  1.1047 +    }
  1.1048 +
  1.1049 +
  1.1050 +    if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
  1.1051 +        savings += independent_coef_context_savings(cpi);
  1.1052 +    else
  1.1053 +        savings += default_coef_context_savings(cpi);
  1.1054 +
  1.1055 +
  1.1056 +    return savings;
  1.1057 +}
  1.1058 +
  1.1059 +#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  1.1060 +int vp8_update_coef_context(VP8_COMP *cpi)
  1.1061 +{
  1.1062 +    int savings = 0;
  1.1063 +
  1.1064 +
  1.1065 +    if (cpi->common.frame_type == KEY_FRAME)
  1.1066 +    {
  1.1067 +        /* Reset to default counts/probabilities at key frames */
  1.1068 +        vp8_copy(cpi->mb.coef_counts, default_coef_counts);
  1.1069 +    }
  1.1070 +
  1.1071 +    if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
  1.1072 +        savings += independent_coef_context_savings(cpi);
  1.1073 +    else
  1.1074 +        savings += default_coef_context_savings(cpi);
  1.1075 +
  1.1076 +    return savings;
  1.1077 +}
  1.1078 +#endif
  1.1079 +
  1.1080 +void vp8_update_coef_probs(VP8_COMP *cpi)
  1.1081 +{
  1.1082 +    int i = 0;
  1.1083 +#if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  1.1084 +    vp8_writer *const w = cpi->bc;
  1.1085 +#endif
  1.1086 +    int savings = 0;
  1.1087 +
  1.1088 +    vp8_clear_system_state();
  1.1089 +
  1.1090 +    do
  1.1091 +    {
  1.1092 +        int j = 0;
  1.1093 +
  1.1094 +        do
  1.1095 +        {
  1.1096 +            int k = 0;
  1.1097 +            int prev_coef_savings[ENTROPY_NODES] = {0};
  1.1098 +            if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
  1.1099 +            {
  1.1100 +                for (k = 0; k < PREV_COEF_CONTEXTS; ++k)
  1.1101 +                {
  1.1102 +                    int t;      /* token/prob index */
  1.1103 +                    for (t = 0; t < ENTROPY_NODES; ++t)
  1.1104 +                    {
  1.1105 +                        const unsigned int *ct = cpi->frame_branch_ct [i][j]
  1.1106 +                                                                      [k][t];
  1.1107 +                        const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
  1.1108 +                        const vp8_prob oldp = cpi->common.fc.coef_probs[i][j]
  1.1109 +                                                                       [k][t];
  1.1110 +                        const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
  1.1111 +
  1.1112 +                        prev_coef_savings[t] +=
  1.1113 +                                prob_update_savings(ct, oldp, newp, upd);
  1.1114 +                    }
  1.1115 +                }
  1.1116 +                k = 0;
  1.1117 +            }
  1.1118 +            do
  1.1119 +            {
  1.1120 +                /* note: use result from vp8_estimate_entropy_savings, so no
  1.1121 +                 * need to call vp8_tree_probs_from_distribution here.
  1.1122 +                 */
  1.1123 +
  1.1124 +                /* at every context */
  1.1125 +
  1.1126 +                /* calc probs and branch cts for this frame only */
  1.1127 +                int t = 0;      /* token/prob index */
  1.1128 +
  1.1129 +                do
  1.1130 +                {
  1.1131 +                    const vp8_prob newp = cpi->frame_coef_probs [i][j][k][t];
  1.1132 +
  1.1133 +                    vp8_prob *Pold = cpi->common.fc.coef_probs [i][j][k] + t;
  1.1134 +                    const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
  1.1135 +
  1.1136 +                    int s = prev_coef_savings[t];
  1.1137 +                    int u = 0;
  1.1138 +
  1.1139 +                    if (!(cpi->oxcf.error_resilient_mode &
  1.1140 +                            VPX_ERROR_RESILIENT_PARTITIONS))
  1.1141 +                    {
  1.1142 +                        s = prob_update_savings(
  1.1143 +                                cpi->frame_branch_ct [i][j][k][t],
  1.1144 +                                *Pold, newp, upd);
  1.1145 +                    }
  1.1146 +
  1.1147 +                    if (s > 0)
  1.1148 +                        u = 1;
  1.1149 +
  1.1150 +                    /* Force updates on key frames if the new is different,
  1.1151 +                     * so that we can be sure we end up with equal probabilities
  1.1152 +                     * over the prev coef contexts.
  1.1153 +                     */
  1.1154 +                    if ((cpi->oxcf.error_resilient_mode &
  1.1155 +                            VPX_ERROR_RESILIENT_PARTITIONS) &&
  1.1156 +                        cpi->common.frame_type == KEY_FRAME && newp != *Pold)
  1.1157 +                        u = 1;
  1.1158 +
  1.1159 +#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  1.1160 +                    cpi->update_probs[i][j][k][t] = u;
  1.1161 +#else
  1.1162 +                    vp8_write(w, u, upd);
  1.1163 +#endif
  1.1164 +
  1.1165 +
  1.1166 +#ifdef VP8_ENTROPY_STATS
  1.1167 +                    ++ tree_update_hist [i][j][k][t] [u];
  1.1168 +#endif
  1.1169 +
  1.1170 +                    if (u)
  1.1171 +                    {
  1.1172 +                        /* send/use new probability */
  1.1173 +
  1.1174 +                        *Pold = newp;
  1.1175 +#if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  1.1176 +                        vp8_write_literal(w, newp, 8);
  1.1177 +#endif
  1.1178 +
  1.1179 +                        savings += s;
  1.1180 +
  1.1181 +                    }
  1.1182 +
  1.1183 +                }
  1.1184 +                while (++t < ENTROPY_NODES);
  1.1185 +
  1.1186 +                /* Accum token counts for generation of default statistics */
  1.1187 +#ifdef VP8_ENTROPY_STATS
  1.1188 +                t = 0;
  1.1189 +
  1.1190 +                do
  1.1191 +                {
  1.1192 +                    context_counters [i][j][k][t] += cpi->coef_counts [i][j][k][t];
  1.1193 +                }
  1.1194 +                while (++t < MAX_ENTROPY_TOKENS);
  1.1195 +
  1.1196 +#endif
  1.1197 +
  1.1198 +            }
  1.1199 +            while (++k < PREV_COEF_CONTEXTS);
  1.1200 +        }
  1.1201 +        while (++j < COEF_BANDS);
  1.1202 +    }
  1.1203 +    while (++i < BLOCK_TYPES);
  1.1204 +
  1.1205 +}
  1.1206 +
  1.1207 +#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  1.1208 +static void pack_coef_probs(VP8_COMP *cpi)
  1.1209 +{
  1.1210 +    int i = 0;
  1.1211 +    vp8_writer *const w = cpi->bc;
  1.1212 +
  1.1213 +    do
  1.1214 +    {
  1.1215 +        int j = 0;
  1.1216 +
  1.1217 +        do
  1.1218 +        {
  1.1219 +            int k = 0;
  1.1220 +
  1.1221 +            do
  1.1222 +            {
  1.1223 +                int t = 0;      /* token/prob index */
  1.1224 +
  1.1225 +                do
  1.1226 +                {
  1.1227 +                    const vp8_prob newp = cpi->common.fc.coef_probs [i][j][k][t];
  1.1228 +                    const vp8_prob upd = vp8_coef_update_probs [i][j][k][t];
  1.1229 +
  1.1230 +                    const char u = cpi->update_probs[i][j][k][t] ;
  1.1231 +
  1.1232 +                    vp8_write(w, u, upd);
  1.1233 +
  1.1234 +                    if (u)
  1.1235 +                    {
  1.1236 +                        /* send/use new probability */
  1.1237 +                        vp8_write_literal(w, newp, 8);
  1.1238 +                    }
  1.1239 +                }
  1.1240 +                while (++t < ENTROPY_NODES);
  1.1241 +            }
  1.1242 +            while (++k < PREV_COEF_CONTEXTS);
  1.1243 +        }
  1.1244 +        while (++j < COEF_BANDS);
  1.1245 +    }
  1.1246 +    while (++i < BLOCK_TYPES);
  1.1247 +}
  1.1248 +#endif
  1.1249 +
  1.1250 +#ifdef PACKET_TESTING
  1.1251 +FILE *vpxlogc = 0;
  1.1252 +#endif
  1.1253 +
  1.1254 +static void put_delta_q(vp8_writer *bc, int delta_q)
  1.1255 +{
  1.1256 +    if (delta_q != 0)
  1.1257 +    {
  1.1258 +        vp8_write_bit(bc, 1);
  1.1259 +        vp8_write_literal(bc, abs(delta_q), 4);
  1.1260 +
  1.1261 +        if (delta_q < 0)
  1.1262 +            vp8_write_bit(bc, 1);
  1.1263 +        else
  1.1264 +            vp8_write_bit(bc, 0);
  1.1265 +    }
  1.1266 +    else
  1.1267 +        vp8_write_bit(bc, 0);
  1.1268 +}
  1.1269 +
  1.1270 +void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest, unsigned char * dest_end, unsigned long *size)
  1.1271 +{
  1.1272 +    int i, j;
  1.1273 +    VP8_HEADER oh;
  1.1274 +    VP8_COMMON *const pc = & cpi->common;
  1.1275 +    vp8_writer *const bc = cpi->bc;
  1.1276 +    MACROBLOCKD *const xd = & cpi->mb.e_mbd;
  1.1277 +    int extra_bytes_packed = 0;
  1.1278 +
  1.1279 +    unsigned char *cx_data = dest;
  1.1280 +    unsigned char *cx_data_end = dest_end;
  1.1281 +    const int *mb_feature_data_bits;
  1.1282 +
  1.1283 +    oh.show_frame = (int) pc->show_frame;
  1.1284 +    oh.type = (int)pc->frame_type;
  1.1285 +    oh.version = pc->version;
  1.1286 +    oh.first_partition_length_in_bytes = 0;
  1.1287 +
  1.1288 +    mb_feature_data_bits = vp8_mb_feature_data_bits;
  1.1289 +
  1.1290 +    bc[0].error = &pc->error;
  1.1291 +
  1.1292 +    validate_buffer(cx_data, 3, cx_data_end, &cpi->common.error);
  1.1293 +    cx_data += 3;
  1.1294 +
  1.1295 +#if defined(SECTIONBITS_OUTPUT)
  1.1296 +    Sectionbits[active_section = 1] += sizeof(VP8_HEADER) * 8 * 256;
  1.1297 +#endif
  1.1298 +
  1.1299 +    /* every keyframe send startcode, width, height, scale factor, clamp
  1.1300 +     * and color type
  1.1301 +     */
  1.1302 +    if (oh.type == KEY_FRAME)
  1.1303 +    {
  1.1304 +        int v;
  1.1305 +
  1.1306 +        validate_buffer(cx_data, 7, cx_data_end, &cpi->common.error);
  1.1307 +
  1.1308 +        /* Start / synch code */
  1.1309 +        cx_data[0] = 0x9D;
  1.1310 +        cx_data[1] = 0x01;
  1.1311 +        cx_data[2] = 0x2a;
  1.1312 +
  1.1313 +        v = (pc->horiz_scale << 14) | pc->Width;
  1.1314 +        cx_data[3] = v;
  1.1315 +        cx_data[4] = v >> 8;
  1.1316 +
  1.1317 +        v = (pc->vert_scale << 14) | pc->Height;
  1.1318 +        cx_data[5] = v;
  1.1319 +        cx_data[6] = v >> 8;
  1.1320 +
  1.1321 +
  1.1322 +        extra_bytes_packed = 7;
  1.1323 +        cx_data += extra_bytes_packed ;
  1.1324 +
  1.1325 +        vp8_start_encode(bc, cx_data, cx_data_end);
  1.1326 +
  1.1327 +        /* signal clr type */
  1.1328 +        vp8_write_bit(bc, 0);
  1.1329 +        vp8_write_bit(bc, pc->clamp_type);
  1.1330 +
  1.1331 +    }
  1.1332 +    else
  1.1333 +        vp8_start_encode(bc, cx_data, cx_data_end);
  1.1334 +
  1.1335 +
  1.1336 +    /* Signal whether or not Segmentation is enabled */
  1.1337 +    vp8_write_bit(bc, xd->segmentation_enabled);
  1.1338 +
  1.1339 +    /*  Indicate which features are enabled */
  1.1340 +    if (xd->segmentation_enabled)
  1.1341 +    {
  1.1342 +        /* Signal whether or not the segmentation map is being updated. */
  1.1343 +        vp8_write_bit(bc, xd->update_mb_segmentation_map);
  1.1344 +        vp8_write_bit(bc, xd->update_mb_segmentation_data);
  1.1345 +
  1.1346 +        if (xd->update_mb_segmentation_data)
  1.1347 +        {
  1.1348 +            signed char Data;
  1.1349 +
  1.1350 +            vp8_write_bit(bc, xd->mb_segement_abs_delta);
  1.1351 +
  1.1352 +            /* For each segmentation feature (Quant and loop filter level) */
  1.1353 +            for (i = 0; i < MB_LVL_MAX; i++)
  1.1354 +            {
  1.1355 +                /* For each of the segments */
  1.1356 +                for (j = 0; j < MAX_MB_SEGMENTS; j++)
  1.1357 +                {
  1.1358 +                    Data = xd->segment_feature_data[i][j];
  1.1359 +
  1.1360 +                    /* Frame level data */
  1.1361 +                    if (Data)
  1.1362 +                    {
  1.1363 +                        vp8_write_bit(bc, 1);
  1.1364 +
  1.1365 +                        if (Data < 0)
  1.1366 +                        {
  1.1367 +                            Data = - Data;
  1.1368 +                            vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
  1.1369 +                            vp8_write_bit(bc, 1);
  1.1370 +                        }
  1.1371 +                        else
  1.1372 +                        {
  1.1373 +                            vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
  1.1374 +                            vp8_write_bit(bc, 0);
  1.1375 +                        }
  1.1376 +                    }
  1.1377 +                    else
  1.1378 +                        vp8_write_bit(bc, 0);
  1.1379 +                }
  1.1380 +            }
  1.1381 +        }
  1.1382 +
  1.1383 +        if (xd->update_mb_segmentation_map)
  1.1384 +        {
  1.1385 +            /* Write the probs used to decode the segment id for each mb */
  1.1386 +            for (i = 0; i < MB_FEATURE_TREE_PROBS; i++)
  1.1387 +            {
  1.1388 +                int Data = xd->mb_segment_tree_probs[i];
  1.1389 +
  1.1390 +                if (Data != 255)
  1.1391 +                {
  1.1392 +                    vp8_write_bit(bc, 1);
  1.1393 +                    vp8_write_literal(bc, Data, 8);
  1.1394 +                }
  1.1395 +                else
  1.1396 +                    vp8_write_bit(bc, 0);
  1.1397 +            }
  1.1398 +        }
  1.1399 +    }
  1.1400 +
  1.1401 +    vp8_write_bit(bc, pc->filter_type);
  1.1402 +    vp8_write_literal(bc, pc->filter_level, 6);
  1.1403 +    vp8_write_literal(bc, pc->sharpness_level, 3);
  1.1404 +
  1.1405 +    /* Write out loop filter deltas applied at the MB level based on mode
  1.1406 +     * or ref frame (if they are enabled).
  1.1407 +     */
  1.1408 +    vp8_write_bit(bc, xd->mode_ref_lf_delta_enabled);
  1.1409 +
  1.1410 +    if (xd->mode_ref_lf_delta_enabled)
  1.1411 +    {
  1.1412 +        /* Do the deltas need to be updated */
  1.1413 +        int send_update = xd->mode_ref_lf_delta_update
  1.1414 +                          || cpi->oxcf.error_resilient_mode;
  1.1415 +
  1.1416 +        vp8_write_bit(bc, send_update);
  1.1417 +        if (send_update)
  1.1418 +        {
  1.1419 +            int Data;
  1.1420 +
  1.1421 +            /* Send update */
  1.1422 +            for (i = 0; i < MAX_REF_LF_DELTAS; i++)
  1.1423 +            {
  1.1424 +                Data = xd->ref_lf_deltas[i];
  1.1425 +
  1.1426 +                /* Frame level data */
  1.1427 +                if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i]
  1.1428 +                    || cpi->oxcf.error_resilient_mode)
  1.1429 +                {
  1.1430 +                    xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i];
  1.1431 +                    vp8_write_bit(bc, 1);
  1.1432 +
  1.1433 +                    if (Data > 0)
  1.1434 +                    {
  1.1435 +                        vp8_write_literal(bc, (Data & 0x3F), 6);
  1.1436 +                        vp8_write_bit(bc, 0);    /* sign */
  1.1437 +                    }
  1.1438 +                    else
  1.1439 +                    {
  1.1440 +                        Data = -Data;
  1.1441 +                        vp8_write_literal(bc, (Data & 0x3F), 6);
  1.1442 +                        vp8_write_bit(bc, 1);    /* sign */
  1.1443 +                    }
  1.1444 +                }
  1.1445 +                else
  1.1446 +                    vp8_write_bit(bc, 0);
  1.1447 +            }
  1.1448 +
  1.1449 +            /* Send update */
  1.1450 +            for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
  1.1451 +            {
  1.1452 +                Data = xd->mode_lf_deltas[i];
  1.1453 +
  1.1454 +                if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i]
  1.1455 +                    || cpi->oxcf.error_resilient_mode)
  1.1456 +                {
  1.1457 +                    xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i];
  1.1458 +                    vp8_write_bit(bc, 1);
  1.1459 +
  1.1460 +                    if (Data > 0)
  1.1461 +                    {
  1.1462 +                        vp8_write_literal(bc, (Data & 0x3F), 6);
  1.1463 +                        vp8_write_bit(bc, 0);    /* sign */
  1.1464 +                    }
  1.1465 +                    else
  1.1466 +                    {
  1.1467 +                        Data = -Data;
  1.1468 +                        vp8_write_literal(bc, (Data & 0x3F), 6);
  1.1469 +                        vp8_write_bit(bc, 1);    /* sign */
  1.1470 +                    }
  1.1471 +                }
  1.1472 +                else
  1.1473 +                    vp8_write_bit(bc, 0);
  1.1474 +            }
  1.1475 +        }
  1.1476 +    }
  1.1477 +
  1.1478 +    /* signal here is multi token partition is enabled */
  1.1479 +    vp8_write_literal(bc, pc->multi_token_partition, 2);
  1.1480 +
  1.1481 +    /* Frame Qbaseline quantizer index */
  1.1482 +    vp8_write_literal(bc, pc->base_qindex, 7);
  1.1483 +
  1.1484 +    /* Transmit Dc, Second order and Uv quantizer delta information */
  1.1485 +    put_delta_q(bc, pc->y1dc_delta_q);
  1.1486 +    put_delta_q(bc, pc->y2dc_delta_q);
  1.1487 +    put_delta_q(bc, pc->y2ac_delta_q);
  1.1488 +    put_delta_q(bc, pc->uvdc_delta_q);
  1.1489 +    put_delta_q(bc, pc->uvac_delta_q);
  1.1490 +
  1.1491 +    /* When there is a key frame all reference buffers are updated using
  1.1492 +     * the new key frame
  1.1493 +     */
  1.1494 +    if (pc->frame_type != KEY_FRAME)
  1.1495 +    {
  1.1496 +        /* Should the GF or ARF be updated using the transmitted frame
  1.1497 +         * or buffer
  1.1498 +         */
  1.1499 +        vp8_write_bit(bc, pc->refresh_golden_frame);
  1.1500 +        vp8_write_bit(bc, pc->refresh_alt_ref_frame);
  1.1501 +
  1.1502 +        /* If not being updated from current frame should either GF or ARF
  1.1503 +         * be updated from another buffer
  1.1504 +         */
  1.1505 +        if (!pc->refresh_golden_frame)
  1.1506 +            vp8_write_literal(bc, pc->copy_buffer_to_gf, 2);
  1.1507 +
  1.1508 +        if (!pc->refresh_alt_ref_frame)
  1.1509 +            vp8_write_literal(bc, pc->copy_buffer_to_arf, 2);
  1.1510 +
  1.1511 +        /* Indicate reference frame sign bias for Golden and ARF frames
  1.1512 +         * (always 0 for last frame buffer)
  1.1513 +         */
  1.1514 +        vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]);
  1.1515 +        vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]);
  1.1516 +    }
  1.1517 +
  1.1518 +#if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  1.1519 +    if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
  1.1520 +    {
  1.1521 +        if (pc->frame_type == KEY_FRAME)
  1.1522 +            pc->refresh_entropy_probs = 1;
  1.1523 +        else
  1.1524 +            pc->refresh_entropy_probs = 0;
  1.1525 +    }
  1.1526 +#endif
  1.1527 +
  1.1528 +    vp8_write_bit(bc, pc->refresh_entropy_probs);
  1.1529 +
  1.1530 +    if (pc->frame_type != KEY_FRAME)
  1.1531 +        vp8_write_bit(bc, pc->refresh_last_frame);
  1.1532 +
  1.1533 +#ifdef VP8_ENTROPY_STATS
  1.1534 +
  1.1535 +    if (pc->frame_type == INTER_FRAME)
  1.1536 +        active_section = 0;
  1.1537 +    else
  1.1538 +        active_section = 7;
  1.1539 +
  1.1540 +#endif
  1.1541 +
  1.1542 +    vp8_clear_system_state();
  1.1543 +
  1.1544 +#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  1.1545 +    pack_coef_probs(cpi);
  1.1546 +#else
  1.1547 +    if (pc->refresh_entropy_probs == 0)
  1.1548 +    {
  1.1549 +        /* save a copy for later refresh */
  1.1550 +        vpx_memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc));
  1.1551 +    }
  1.1552 +
  1.1553 +    vp8_update_coef_probs(cpi);
  1.1554 +#endif
  1.1555 +
  1.1556 +#ifdef VP8_ENTROPY_STATS
  1.1557 +    active_section = 2;
  1.1558 +#endif
  1.1559 +
  1.1560 +    /* Write out the mb_no_coeff_skip flag */
  1.1561 +    vp8_write_bit(bc, pc->mb_no_coeff_skip);
  1.1562 +
  1.1563 +    if (pc->frame_type == KEY_FRAME)
  1.1564 +    {
  1.1565 +        write_kfmodes(cpi);
  1.1566 +
  1.1567 +#ifdef VP8_ENTROPY_STATS
  1.1568 +        active_section = 8;
  1.1569 +#endif
  1.1570 +    }
  1.1571 +    else
  1.1572 +    {
  1.1573 +        pack_inter_mode_mvs(cpi);
  1.1574 +
  1.1575 +#ifdef VP8_ENTROPY_STATS
  1.1576 +        active_section = 1;
  1.1577 +#endif
  1.1578 +    }
  1.1579 +
  1.1580 +    vp8_stop_encode(bc);
  1.1581 +
  1.1582 +    cx_data += bc->pos;
  1.1583 +
  1.1584 +    oh.first_partition_length_in_bytes = cpi->bc->pos;
  1.1585 +
  1.1586 +    /* update frame tag */
  1.1587 +    {
  1.1588 +        int v = (oh.first_partition_length_in_bytes << 5) |
  1.1589 +                (oh.show_frame << 4) |
  1.1590 +                (oh.version << 1) |
  1.1591 +                oh.type;
  1.1592 +
  1.1593 +        dest[0] = v;
  1.1594 +        dest[1] = v >> 8;
  1.1595 +        dest[2] = v >> 16;
  1.1596 +    }
  1.1597 +
  1.1598 +    *size = VP8_HEADER_SIZE + extra_bytes_packed + cpi->bc->pos;
  1.1599 +
  1.1600 +    cpi->partition_sz[0] = *size;
  1.1601 +
  1.1602 +#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  1.1603 +    {
  1.1604 +        const int num_part = (1 << pc->multi_token_partition);
  1.1605 +        unsigned char * dp = cpi->partition_d[0] + cpi->partition_sz[0];
  1.1606 +
  1.1607 +        if (num_part > 1)
  1.1608 +        {
  1.1609 +            /* write token part sizes (all but last) if more than 1 */
  1.1610 +            validate_buffer(dp, 3 * (num_part - 1), cpi->partition_d_end[0],
  1.1611 +                            &pc->error);
  1.1612 +
  1.1613 +            cpi->partition_sz[0] += 3*(num_part-1);
  1.1614 +
  1.1615 +            for(i = 1; i < num_part; i++)
  1.1616 +            {
  1.1617 +                write_partition_size(dp, cpi->partition_sz[i]);
  1.1618 +                dp += 3;
  1.1619 +            }
  1.1620 +        }
  1.1621 +
  1.1622 +        if (!cpi->output_partition)
  1.1623 +        {
  1.1624 +            /* concatenate partition buffers */
  1.1625 +            for(i = 0; i < num_part; i++)
  1.1626 +            {
  1.1627 +                vpx_memmove(dp, cpi->partition_d[i+1], cpi->partition_sz[i+1]);
  1.1628 +                cpi->partition_d[i+1] = dp;
  1.1629 +                dp += cpi->partition_sz[i+1];
  1.1630 +            }
  1.1631 +        }
  1.1632 +
  1.1633 +        /* update total size */
  1.1634 +        *size = 0;
  1.1635 +        for(i = 0; i < num_part+1; i++)
  1.1636 +        {
  1.1637 +            *size += cpi->partition_sz[i];
  1.1638 +        }
  1.1639 +    }
  1.1640 +#else
  1.1641 +    if (pc->multi_token_partition != ONE_PARTITION)
  1.1642 +    {
  1.1643 +        int num_part = 1 << pc->multi_token_partition;
  1.1644 +
  1.1645 +        /* partition size table at the end of first partition */
  1.1646 +        cpi->partition_sz[0] += 3 * (num_part - 1);
  1.1647 +        *size += 3 * (num_part - 1);
  1.1648 +
  1.1649 +        validate_buffer(cx_data, 3 * (num_part - 1), cx_data_end,
  1.1650 +                        &pc->error);
  1.1651 +
  1.1652 +        for(i = 1; i < num_part + 1; i++)
  1.1653 +        {
  1.1654 +            cpi->bc[i].error = &pc->error;
  1.1655 +        }
  1.1656 +
  1.1657 +        pack_tokens_into_partitions(cpi, cx_data + 3 * (num_part - 1),
  1.1658 +                                    cx_data_end, num_part);
  1.1659 +
  1.1660 +        for(i = 1; i < num_part; i++)
  1.1661 +        {
  1.1662 +            cpi->partition_sz[i] = cpi->bc[i].pos;
  1.1663 +            write_partition_size(cx_data, cpi->partition_sz[i]);
  1.1664 +            cx_data += 3;
  1.1665 +            *size += cpi->partition_sz[i]; /* add to total */
  1.1666 +        }
  1.1667 +
  1.1668 +        /* add last partition to total size */
  1.1669 +        cpi->partition_sz[i] = cpi->bc[i].pos;
  1.1670 +        *size += cpi->partition_sz[i];
  1.1671 +    }
  1.1672 +    else
  1.1673 +    {
  1.1674 +        bc[1].error = &pc->error;
  1.1675 +
  1.1676 +        vp8_start_encode(&cpi->bc[1], cx_data, cx_data_end);
  1.1677 +
  1.1678 +#if CONFIG_MULTITHREAD
  1.1679 +        if (cpi->b_multi_threaded)
  1.1680 +            pack_mb_row_tokens(cpi, &cpi->bc[1]);
  1.1681 +        else
  1.1682 +#endif
  1.1683 +            pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count);
  1.1684 +
  1.1685 +        vp8_stop_encode(&cpi->bc[1]);
  1.1686 +
  1.1687 +        *size += cpi->bc[1].pos;
  1.1688 +        cpi->partition_sz[1] = cpi->bc[1].pos;
  1.1689 +    }
  1.1690 +#endif
  1.1691 +}
  1.1692 +
  1.1693 +#ifdef VP8_ENTROPY_STATS
  1.1694 +void print_tree_update_probs()
  1.1695 +{
  1.1696 +    int i, j, k, l;
  1.1697 +    FILE *f = fopen("context.c", "a");
  1.1698 +    int Sum;
  1.1699 +    fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
  1.1700 +    fprintf(f, "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] [PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n");
  1.1701 +
  1.1702 +    for (i = 0; i < BLOCK_TYPES; i++)
  1.1703 +    {
  1.1704 +        fprintf(f, "  { \n");
  1.1705 +
  1.1706 +        for (j = 0; j < COEF_BANDS; j++)
  1.1707 +        {
  1.1708 +            fprintf(f, "    {\n");
  1.1709 +
  1.1710 +            for (k = 0; k < PREV_COEF_CONTEXTS; k++)
  1.1711 +            {
  1.1712 +                fprintf(f, "      {");
  1.1713 +
  1.1714 +                for (l = 0; l < ENTROPY_NODES; l++)
  1.1715 +                {
  1.1716 +                    Sum = tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1];
  1.1717 +
  1.1718 +                    if (Sum > 0)
  1.1719 +                    {
  1.1720 +                        if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0)
  1.1721 +                            fprintf(f, "%3ld, ", (tree_update_hist[i][j][k][l][0] * 255) / Sum);
  1.1722 +                        else
  1.1723 +                            fprintf(f, "%3ld, ", 1);
  1.1724 +                    }
  1.1725 +                    else
  1.1726 +                        fprintf(f, "%3ld, ", 128);
  1.1727 +                }
  1.1728 +
  1.1729 +                fprintf(f, "},\n");
  1.1730 +            }
  1.1731 +
  1.1732 +            fprintf(f, "    },\n");
  1.1733 +        }
  1.1734 +
  1.1735 +        fprintf(f, "  },\n");
  1.1736 +    }
  1.1737 +
  1.1738 +    fprintf(f, "};\n");
  1.1739 +    fclose(f);
  1.1740 +}
  1.1741 +#endif

mercurial