mask mousewheel segv bug, mask opengl sw fallback read to ram, fix tiff config withou...
[goodguy/cinelerra.git] / cinelerra-5.1 / libzmpeg3 / video / motion.C
1 #include "../libzmpeg3.h"
2
3 /* calculate motion vector component */
4
5 static void
6 calc_mv(int *pred, int r_size, int motion_code, int motion_r, int full_pel_vector)
7 {
8   int lim = 16 << r_size;
9   int vec = full_pel_vector ? (*pred >> 1) : (*pred);
10
11   if( motion_code > 0 ) {
12     vec += ((motion_code - 1) << r_size) + motion_r + 1;
13     if( vec >= lim ) vec -= lim + lim;
14   }
15   else if( motion_code < 0 ) {
16     vec -= ((-motion_code - 1) << r_size) + motion_r + 1;
17     if( vec < -lim ) vec += lim + lim;
18   }
19   *pred = full_pel_vector ? (vec << 1) : vec;
20 }
21
22
23 /*
24 int *dmvector, * differential motion vector *
25 int mvx, int mvy  * decoded mv components (always in field format) *
26 */
27 void zvideo_t::
28 calc_dmv(int DMV[][2], int *dmvector, int mvx, int mvy)
29 {
30   if( pict_struct == pics_FRAME_PICTURE ) {
31     if( topfirst ) {
32       /* vector for prediction of top field from bottom field */
33       DMV[0][0] = ((mvx  + (mvx>0)) >> 1) + dmvector[0];
34       DMV[0][1] = ((mvy  + (mvy>0)) >> 1) + dmvector[1] - 1;
35
36       /* vector for prediction of bottom field from top field */
37       DMV[1][0] = ((3 * mvx + (mvx > 0)) >> 1) + dmvector[0];
38       DMV[1][1] = ((3 * mvy + (mvy > 0)) >> 1) + dmvector[1] + 1;
39     }
40     else {
41       /* vector for prediction of top field from bottom field */
42       DMV[0][0] = ((3 * mvx + (mvx>0)) >> 1) + dmvector[0];
43       DMV[0][1] = ((3 * mvy + (mvy>0)) >> 1) + dmvector[1] - 1;
44       /* vector for prediction of bottom field from top field */
45       DMV[1][0] = ((mvx + (mvx>0)) >> 1) + dmvector[0];
46       DMV[1][1] = ((mvy + (mvy>0)) >> 1) + dmvector[1] + 1;
47     }
48   }
49   else {
50     /* vector for prediction from field of opposite 'parity' */
51     DMV[0][0] = ((mvx + (mvx > 0)) >> 1) + dmvector[0];
52     DMV[0][1] = ((mvy + (mvy > 0)) >> 1) + dmvector[1];
53     /* correct for vertical field shift */
54     if(  pict_struct == pics_TOP_FIELD )
55       --DMV[0][1];
56     else 
57       ++DMV[0][1];
58   }
59 }
60
61 int zslice_decoder_t::
62 get_mv()
63 {
64   int zcode;
65   if( slice_buffer->get_bit() ) return 0;
66
67   if( (zcode=slice_buffer->show_bits(9)) >= 64 ) {
68     zcode >>= 6;
69     slice_buffer->flush_bits(MVtab0[zcode].len);
70     return slice_buffer->get_bit() ?
71       -MVtab0[zcode].val : MVtab0[zcode].val;
72   }
73
74   if( zcode >= 24 ) {
75     zcode >>= 3;
76     slice_buffer->flush_bits(MVtab1[zcode].len);
77     return slice_buffer->get_bit() ?
78       -MVtab1[zcode].val : MVtab1[zcode].val;
79   }
80
81   if( (zcode-=12) < 0 ) {
82 //zerrs("invalid motion_vector code %d\n",zcode+12);
83     fault = 1;
84     return 1;
85   }
86   slice_buffer->flush_bits(MVtab2[zcode].len);
87   return slice_buffer->get_bit() ?
88     -MVtab2[zcode].val : MVtab2[zcode].val;
89 }
90
91 /* get differential motion vector (for dual prime prediction) */
92 int zslice_decoder_t::
93 get_dmv()
94 {
95   if( slice_buffer->get_bit() )
96     return slice_buffer->get_bit() ? -1 : 1;
97   return 0;
98 }
99
100
101 /* get and decode motion vector and differential motion vector */
102 void zslice_decoder_t::
103 motion_vector(int *PMV, int *dmvector, int h_r_size, int v_r_size,
104     int dmv, int mvscale, int full_pel_vector)
105 {
106   int motion_r;
107   int motion_code = get_mv();
108   if( fault ) return;
109   motion_r = (h_r_size != 0 && motion_code != 0) ?
110     slice_buffer->get_bits(h_r_size) : 0;
111   calc_mv(&PMV[0], h_r_size, motion_code, motion_r, full_pel_vector);
112   if( dmv ) dmvector[0] = get_dmv();
113   motion_code = get_mv();
114   if( fault )  return;
115   motion_r = (v_r_size != 0 && motion_code != 0) ?
116     slice_buffer->get_bits(v_r_size) : 0;
117
118   /* DIV 2 */
119   if( mvscale ) PMV[1] >>= 1; 
120   calc_mv(&PMV[1], v_r_size, motion_code, motion_r, full_pel_vector);
121   if( mvscale ) PMV[1] <<= 1;
122   if( dmv ) dmvector[1] = get_dmv();
123 }
124
125 int zslice_decoder_t::
126 motion_vectors( int PMV[2][2][2], int dmvector[2], int mv_field_sel[2][2],
127     int s, int mv_count, int mv_format, int h_r_size, int v_r_size, 
128     int dmv, int mvscale)
129 {
130   if( mv_count == 1 ) {
131     if( mv_format == mv_FIELD && !dmv )
132       mv_field_sel[1][s] = mv_field_sel[0][s] = slice_buffer->get_bit();
133     motion_vector(PMV[0][s], dmvector, h_r_size, v_r_size, dmv, mvscale, 0);
134     if( fault ) return 1;
135
136     /* update other motion vector predictors */
137     PMV[1][s][0] = PMV[0][s][0];
138     PMV[1][s][1] = PMV[0][s][1];
139   }
140   else {
141     mv_field_sel[0][s] = slice_buffer->get_bit();
142     motion_vector(PMV[0][s], dmvector, h_r_size, v_r_size, dmv, mvscale, 0);
143     if( fault ) return 1;
144     mv_field_sel[1][s] = slice_buffer->get_bit();
145     motion_vector(PMV[1][s], dmvector, h_r_size, v_r_size, dmv, mvscale, 0);
146     if( fault ) return 1;
147   }
148   return 0;
149 }
150