• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 VPX_VP8_ENCODER_BOOLHUFF_H_
19 #define VPX_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 void vp8_start_encode(BOOL_CODER *bc, unsigned char *source,
39                       unsigned char *source_end);
40 
41 void vp8_encode_value(BOOL_CODER *bc, int data, int bits);
42 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 
validate_buffer(const unsigned char * start,size_t len,const unsigned char * end,struct vpx_internal_error_info * error)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 }
vp8_encode_bool(BOOL_CODER * bc,int bit,int probability)59 static void vp8_encode_bool(BOOL_CODER *bc, int bit, int probability) {
60   unsigned int split;
61   int count = bc->count;
62   unsigned int range = bc->range;
63   unsigned int lowvalue = bc->lowvalue;
64   int shift;
65 
66   split = 1 + (((range - 1) * probability) >> 8);
67 
68   range = split;
69 
70   if (bit) {
71     lowvalue += split;
72     range = bc->range - split;
73   }
74 
75   shift = vp8_norm[range];
76 
77   range <<= shift;
78   count += shift;
79 
80   if (count >= 0) {
81     int offset = shift - count;
82 
83     if ((lowvalue << (offset - 1)) & 0x80000000) {
84       int x = bc->pos - 1;
85 
86       while (x >= 0 && bc->buffer[x] == 0xff) {
87         bc->buffer[x] = (unsigned char)0;
88         x--;
89       }
90 
91       bc->buffer[x] += 1;
92     }
93 
94     validate_buffer(bc->buffer + bc->pos, 1, bc->buffer_end, bc->error);
95     bc->buffer[bc->pos++] = (lowvalue >> (24 - offset) & 0xff);
96 
97     shift = count;
98     lowvalue = (int)(((uint64_t)lowvalue << offset) & 0xffffff);
99     count -= 8;
100   }
101 
102   lowvalue <<= shift;
103   bc->count = count;
104   bc->lowvalue = lowvalue;
105   bc->range = range;
106 }
107 
108 #ifdef __cplusplus
109 }  // extern "C"
110 #endif
111 
112 #endif  // VPX_VP8_ENCODER_BOOLHUFF_H_
113