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 #ifndef VP9_COMMON_VP9_ENTROPYMV_H_
13 #define VP9_COMMON_VP9_ENTROPYMV_H_
14 
15 #include "./vpx_config.h"
16 #include "vp9/common/vp9_blockd.h"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 struct VP9Common;
23 
24 void vp9_init_mv_probs(struct VP9Common *cm);
25 
26 void vp9_adapt_mv_probs(struct VP9Common *cm, int usehp);
27 int vp9_use_mv_hp(const MV *ref);
28 
29 #define MV_UPDATE_PROB 252
30 
31 /* Symbols for coding which components are zero jointly */
32 #define MV_JOINTS     4
33 typedef enum {
34   MV_JOINT_ZERO = 0,             /* Zero vector */
35   MV_JOINT_HNZVZ = 1,            /* Vert zero, hor nonzero */
36   MV_JOINT_HZVNZ = 2,            /* Hor zero, vert nonzero */
37   MV_JOINT_HNZVNZ = 3,           /* Both components nonzero */
38 } MV_JOINT_TYPE;
39 
mv_joint_vertical(MV_JOINT_TYPE type)40 static INLINE int mv_joint_vertical(MV_JOINT_TYPE type) {
41   return type == MV_JOINT_HZVNZ || type == MV_JOINT_HNZVNZ;
42 }
43 
mv_joint_horizontal(MV_JOINT_TYPE type)44 static INLINE int mv_joint_horizontal(MV_JOINT_TYPE type) {
45   return type == MV_JOINT_HNZVZ || type == MV_JOINT_HNZVNZ;
46 }
47 
48 /* Symbols for coding magnitude class of nonzero components */
49 #define MV_CLASSES     11
50 typedef enum {
51   MV_CLASS_0 = 0,      /* (0, 2]     integer pel */
52   MV_CLASS_1 = 1,      /* (2, 4]     integer pel */
53   MV_CLASS_2 = 2,      /* (4, 8]     integer pel */
54   MV_CLASS_3 = 3,      /* (8, 16]    integer pel */
55   MV_CLASS_4 = 4,      /* (16, 32]   integer pel */
56   MV_CLASS_5 = 5,      /* (32, 64]   integer pel */
57   MV_CLASS_6 = 6,      /* (64, 128]  integer pel */
58   MV_CLASS_7 = 7,      /* (128, 256] integer pel */
59   MV_CLASS_8 = 8,      /* (256, 512] integer pel */
60   MV_CLASS_9 = 9,      /* (512, 1024] integer pel */
61   MV_CLASS_10 = 10,    /* (1024,2048] integer pel */
62 } MV_CLASS_TYPE;
63 
64 #define CLASS0_BITS    1  /* bits at integer precision for class 0 */
65 #define CLASS0_SIZE    (1 << CLASS0_BITS)
66 #define MV_OFFSET_BITS (MV_CLASSES + CLASS0_BITS - 2)
67 #define MV_FP_SIZE 4
68 
69 #define MV_MAX_BITS    (MV_CLASSES + CLASS0_BITS + 2)
70 #define MV_MAX         ((1 << MV_MAX_BITS) - 1)
71 #define MV_VALS        ((MV_MAX << 1) + 1)
72 
73 #define MV_IN_USE_BITS 14
74 #define MV_UPP   ((1 << MV_IN_USE_BITS) - 1)
75 #define MV_LOW   (-(1 << MV_IN_USE_BITS))
76 
77 extern const vp9_tree_index vp9_mv_joint_tree[];
78 extern const vp9_tree_index vp9_mv_class_tree[];
79 extern const vp9_tree_index vp9_mv_class0_tree[];
80 extern const vp9_tree_index vp9_mv_fp_tree[];
81 
82 typedef struct {
83   vp9_prob sign;
84   vp9_prob classes[MV_CLASSES - 1];
85   vp9_prob class0[CLASS0_SIZE - 1];
86   vp9_prob bits[MV_OFFSET_BITS];
87   vp9_prob class0_fp[CLASS0_SIZE][MV_FP_SIZE - 1];
88   vp9_prob fp[MV_FP_SIZE - 1];
89   vp9_prob class0_hp;
90   vp9_prob hp;
91 } nmv_component;
92 
93 typedef struct {
94   vp9_prob joints[MV_JOINTS - 1];
95   nmv_component comps[2];
96 } nmv_context;
97 
vp9_get_mv_joint(const MV * mv)98 static INLINE MV_JOINT_TYPE vp9_get_mv_joint(const MV *mv) {
99   if (mv->row == 0) {
100     return mv->col == 0 ? MV_JOINT_ZERO : MV_JOINT_HNZVZ;
101   } else {
102     return mv->col == 0 ? MV_JOINT_HZVNZ : MV_JOINT_HNZVNZ;
103   }
104 }
105 
106 MV_CLASS_TYPE vp9_get_mv_class(int z, int *offset);
107 int vp9_get_mv_mag(MV_CLASS_TYPE c, int offset);
108 
109 
110 typedef struct {
111   unsigned int sign[2];
112   unsigned int classes[MV_CLASSES];
113   unsigned int class0[CLASS0_SIZE];
114   unsigned int bits[MV_OFFSET_BITS][2];
115   unsigned int class0_fp[CLASS0_SIZE][MV_FP_SIZE];
116   unsigned int fp[MV_FP_SIZE];
117   unsigned int class0_hp[2];
118   unsigned int hp[2];
119 } nmv_component_counts;
120 
121 typedef struct {
122   unsigned int joints[MV_JOINTS];
123   nmv_component_counts comps[2];
124 } nmv_context_counts;
125 
126 void vp9_inc_mv(const MV *mv, nmv_context_counts *mvctx);
127 
128 #ifdef __cplusplus
129 }  // extern "C"
130 #endif
131 
132 #endif  // VP9_COMMON_VP9_ENTROPYMV_H_
133