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