1 /*****************************************************************************
2 * vpar_motion.c : motion vectors parsing
4 *****************************************************************************/
6 /*****************************************************************************
8 *****************************************************************************/
21 #include "vlc_thread.h"
24 #include "debug.h" /* ?? temporaire, requis par netlist.h */
27 #include "input_netlist.h"
28 #include "decoder_fifo.h"
30 #include "video_output.h"
32 #include "vdec_idct.h"
33 #include "video_decoder.h"
34 #include "vdec_motion.h"
36 #include "vpar_blocks.h"
37 #include "vpar_headers.h"
38 #include "video_fifo.h"
39 #include "vpar_synchro.h"
40 #include "video_parser.h"
41 #include "vpar_motion.h"
48 /****************************************************************************
49 * vpar_MotionCode : Parse the next motion code
50 ****************************************************************************/
51 static __inline__ int vpar_MotionCode( vpar_thread_t * p_vpar )
54 static lookup_t pl_mv_tab0[8] =
55 { {-1,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1} };
56 /* Table B-10, motion_code, codes 0000011 ... 000011x */
57 static lookup_t pl_mv_tab1[8] =
58 { {-1,0}, {-1,0}, {-1,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5} };
59 /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
60 static lookup_t pl_mv_tab2[12] = {
61 {16,9}, {15,9}, {14,9}, {13,9},
62 {12,9}, {11,9}, {10,8}, {10,8},
63 {9,8}, {9,8}, {8,8}, {8,8} };
65 if( GetBits(&p_vpar->bit_stream, 1) )
69 if( (i_code = ShowBits(&p_vpar->bit_stream, 9)) >= 64 )
72 RemoveBits( &p_vpar->bit_stream, pl_mv_tab0[i_code].i_length );
73 return( GetBits(&p_vpar->bit_stream, 1) ? -pl_mv_tab0[i_code].i_value : pl_mv_tab0[i_code].i_value );
79 RemoveBits( &p_vpar->bit_stream, pl_mv_tab1[i_code].i_length );
80 return( GetBits(&p_vpar->bit_stream, 1) ? -pl_mv_tab1[i_code].i_value : pl_mv_tab1[i_code].i_value );
83 if( (i_code -= 12) < 0 )
85 p_vpar->picture.b_error = 1;
86 intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
90 RemoveBits( &p_vpar->bit_stream, pl_mv_tab2[i_code].i_length );
91 return( GetBits(&p_vpar->bit_stream, 1) ? -pl_mv_tab2[i_code].i_value : pl_mv_tab2[i_code].i_value );
94 /****************************************************************************
95 * vpar_DecodeMotionVector : decode a motion_vector
96 ****************************************************************************/
97 static __inline__ void vpar_DecodeMotionVector( int * pi_prediction, int i_r_size,
98 int i_motion_code, int i_motion_residual, int i_full_pel )
100 int i_limit, i_vector;
102 /* ISO/IEC 13818-1 section 7.6.3.1 */
103 i_limit = 16 << i_r_size;
104 i_vector = *pi_prediction >> i_full_pel;
106 if( i_motion_code > 0 )
108 i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
109 if( i_vector >= i_limit )
110 i_vector -= i_limit + i_limit;
112 else if( i_motion_code < 0 )
114 i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
115 if( i_vector < -i_limit )
116 i_vector += i_limit + i_limit;
118 *pi_prediction = i_vector << i_full_pel;
121 /****************************************************************************
122 * vpar_MotionVector : Parse the next motion_vector field
123 ****************************************************************************/
124 void vpar_MotionVector( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_r,
125 int i_s, int i_full_pel )
127 int i_motion_code, i_motion_residual;
130 i_r_size = p_vpar->picture.ppi_f_code[i_s][0]-1;
131 i_motion_code = vpar_MotionCode( p_vpar );
132 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
133 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
134 vpar_DecodeMotionVector( &p_vpar->slice.pppi_pmv[i_r][i_s][0], i_r_size,
135 i_motion_code, i_motion_residual, i_full_pel );
136 p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->slice.pppi_pmv[i_r][i_s][0];
139 if( p_vpar->mb.b_dmv )
141 if( GetBits(&p_vpar->bit_stream, 1) )
143 p_mb->pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
147 p_mb->pi_dm_vector[0] = 0;
151 i_r_size = p_vpar->picture.ppi_f_code[i_s][1]-1;
152 i_motion_code = vpar_MotionCode( p_vpar );
153 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
154 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
157 if( (p_vpar->mb.i_mv_format == MOTION_FIELD) && (p_vpar->picture.i_structure == FRAME_STRUCTURE) )
159 p_vpar->slice.pppi_pmv[i_r][i_s][1] >>= 1;
162 vpar_DecodeMotionVector( &p_vpar->slice.pppi_pmv[i_r][i_s][1], i_r_size,
163 i_motion_code, i_motion_residual, i_full_pel );
165 if( (p_vpar->mb.i_mv_format == MOTION_FIELD) && (p_vpar->picture.i_structure == FRAME_STRUCTURE) )
166 p_vpar->slice.pppi_pmv[i_r][i_s][1] <<= 1;
168 p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->slice.pppi_pmv[i_r][i_s][1];
171 if( p_vpar->mb.b_dmv )
173 if( GetBits(&p_vpar->bit_stream, 1) )
175 p_mb->pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
179 p_mb->pi_dm_vector[1] = 0;
184 /*****************************************************************************
185 * vpar_MPEG1MotionVector : Parse the next MPEG-1 motion vectors
186 *****************************************************************************/
187 void vpar_MPEG1MotionVector( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_s )
189 vpar_MotionVector( p_vpar, p_mb, 0, i_s, p_vpar->picture.pb_full_pel_vector[i_s] );
192 /*****************************************************************************
193 * vpar_MPEG2MotionVector : Parse the next MPEG-2 motion_vectors field
194 *****************************************************************************/
195 void vpar_MPEG2MotionVector( vpar_thread_t * p_vpar, macroblock_t * p_mb, int i_s )
197 if( p_vpar->mb.i_mv_count == 1 )
199 if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
201 p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
202 = GetBits( &p_vpar->bit_stream, 1 );
204 vpar_MotionVector( p_vpar, p_mb, 0, i_s, 0 );
205 p_vpar->slice.pppi_pmv[1][i_s][0] = p_vpar->slice.pppi_pmv[0][i_s][0];
206 p_vpar->slice.pppi_pmv[1][i_s][1] = p_vpar->slice.pppi_pmv[0][i_s][1];
207 p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->slice.pppi_pmv[0][i_s][0];
208 p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->slice.pppi_pmv[0][i_s][1];
212 p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
213 vpar_MotionVector( p_vpar, p_mb, 0, i_s, 0 );
214 p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
215 vpar_MotionVector( p_vpar, p_mb, 1, i_s, 0 );