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