1 #include "../libzmpeg3.h"
3 /* calculate motion vector component */
6 calc_mv(int *pred, int r_size, int motion_code, int motion_r, int full_pel_vector)
8 int lim = 16 << r_size;
9 int vec = full_pel_vector ? (*pred >> 1) : (*pred);
11 if( motion_code > 0 ) {
12 vec += ((motion_code - 1) << r_size) + motion_r + 1;
13 if( vec >= lim ) vec -= lim + lim;
15 else if( motion_code < 0 ) {
16 vec -= ((-motion_code - 1) << r_size) + motion_r + 1;
17 if( vec < -lim ) vec += lim + lim;
19 *pred = full_pel_vector ? (vec << 1) : vec;
24 int *dmvector, * differential motion vector *
25 int mvx, int mvy * decoded mv components (always in field format) *
28 calc_dmv(int DMV[][2], int *dmvector, int mvx, int mvy)
30 if( pict_struct == pics_FRAME_PICTURE ) {
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;
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;
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;
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 )
61 int zslice_decoder_t::
65 if( slice_buffer->get_bit() ) return 0;
67 if( (zcode=slice_buffer->show_bits(9)) >= 64 ) {
69 slice_buffer->flush_bits(MVtab0[zcode].len);
70 return slice_buffer->get_bit() ?
71 -MVtab0[zcode].val : MVtab0[zcode].val;
76 slice_buffer->flush_bits(MVtab1[zcode].len);
77 return slice_buffer->get_bit() ?
78 -MVtab1[zcode].val : MVtab1[zcode].val;
81 if( (zcode-=12) < 0 ) {
82 //zerrs("invalid motion_vector code %d\n",zcode+12);
86 slice_buffer->flush_bits(MVtab2[zcode].len);
87 return slice_buffer->get_bit() ?
88 -MVtab2[zcode].val : MVtab2[zcode].val;
91 /* get differential motion vector (for dual prime prediction) */
92 int zslice_decoder_t::
95 if( slice_buffer->get_bit() )
96 return slice_buffer->get_bit() ? -1 : 1;
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)
107 int motion_code = get_mv();
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();
115 motion_r = (v_r_size != 0 && motion_code != 0) ?
116 slice_buffer->get_bits(v_r_size) : 0;
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();
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)
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;
136 /* update other motion vector predictors */
137 PMV[1][s][0] = PMV[0][s][0];
138 PMV[1][s][1] = PMV[0][s][1];
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;