Home | History | Annotate | Download | only in encoder
      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 *
     13 *   Module Title :     boolhuff.h
     14 *
     15 *   Description  :     Bool Coder header file.
     16 *
     17 ****************************************************************************/
     18 #ifndef VP8_ENCODER_BOOLHUFF_H_
     19 #define VP8_ENCODER_BOOLHUFF_H_
     20 
     21 #include "vpx_ports/mem.h"
     22 #include "vpx/internal/vpx_codec_internal.h"
     23 
     24 #ifdef __cplusplus
     25 extern "C" {
     26 #endif
     27 
     28 typedef struct {
     29   unsigned int lowvalue;
     30   unsigned int range;
     31   int count;
     32   unsigned int pos;
     33   unsigned char *buffer;
     34   unsigned char *buffer_end;
     35   struct vpx_internal_error_info *error;
     36 } BOOL_CODER;
     37 
     38 extern void vp8_start_encode(BOOL_CODER *bc, unsigned char *buffer,
     39                              unsigned char *buffer_end);
     40 
     41 extern void vp8_encode_value(BOOL_CODER *br, int data, int bits);
     42 extern void vp8_stop_encode(BOOL_CODER *bc);
     43 extern const unsigned int vp8_prob_cost[256];
     44 
     45 DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
     46 
     47 static int validate_buffer(const unsigned char *start, size_t len,
     48                            const unsigned char *end,
     49                            struct vpx_internal_error_info *error) {
     50   if (start + len > start && start + len < end) {
     51     return 1;
     52   } else {
     53     vpx_internal_error(error, VPX_CODEC_CORRUPT_FRAME,
     54                        "Truncated packet or corrupt partition ");
     55   }
     56 
     57   return 0;
     58 }
     59 static void vp8_encode_bool(BOOL_CODER *br, int bit, int probability) {
     60   unsigned int split;
     61   int count = br->count;
     62   unsigned int range = br->range;
     63   unsigned int lowvalue = br->lowvalue;
     64   register int shift;
     65 
     66 #ifdef VP8_ENTROPY_STATS
     67 #if defined(SECTIONBITS_OUTPUT)
     68 
     69   if (bit)
     70     Sectionbits[active_section] += vp8_prob_cost[255 - probability];
     71   else
     72     Sectionbits[active_section] += vp8_prob_cost[probability];
     73 
     74 #endif
     75 #endif
     76 
     77   split = 1 + (((range - 1) * probability) >> 8);
     78 
     79   range = split;
     80 
     81   if (bit) {
     82     lowvalue += split;
     83     range = br->range - split;
     84   }
     85 
     86   shift = vp8_norm[range];
     87 
     88   range <<= shift;
     89   count += shift;
     90 
     91   if (count >= 0) {
     92     int offset = shift - count;
     93 
     94     if ((lowvalue << (offset - 1)) & 0x80000000) {
     95       int x = br->pos - 1;
     96 
     97       while (x >= 0 && br->buffer[x] == 0xff) {
     98         br->buffer[x] = (unsigned char)0;
     99         x--;
    100       }
    101 
    102       br->buffer[x] += 1;
    103     }
    104 
    105     validate_buffer(br->buffer + br->pos, 1, br->buffer_end, br->error);
    106     br->buffer[br->pos++] = (lowvalue >> (24 - offset));
    107 
    108     lowvalue <<= offset;
    109     shift = count;
    110     lowvalue &= 0xffffff;
    111     count -= 8;
    112   }
    113 
    114   lowvalue <<= shift;
    115   br->count = count;
    116   br->lowvalue = lowvalue;
    117   br->range = range;
    118 }
    119 
    120 #ifdef __cplusplus
    121 }  // extern "C"
    122 #endif
    123 
    124 #endif  // VP8_ENCODER_BOOLHUFF_H_
    125