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