PageRenderTime 105ms CodeModel.GetById 16ms app.highlight 78ms RepoModel.GetById 1ms app.codeStats 0ms

/media/libvpx/vp8/encoder/bitstream.c

http://github.com/zpao/v8monkey
C | 1913 lines | 1465 code | 363 blank | 85 comment | 217 complexity | 14b3a542b5051c3d79e37447ee865635 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file