]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_motion.c
. un chti peu plus de synchro video, mais c'est pas encore �a
[vlc] / src / video_parser / vpar_motion.c
1 /*****************************************************************************
2  * vpar_motion.c : motion vectors parsing
3  * (c)1999 VideoLAN
4  *****************************************************************************/
5
6 /*****************************************************************************
7  * Preamble
8  *****************************************************************************/
9
10
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <sys/uio.h>
17
18 #include "config.h"
19 #include "common.h"
20 #include "mtime.h"
21 #include "vlc_thread.h"
22
23 #include "intf_msg.h"
24 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
25
26 #include "input.h"
27 #include "input_netlist.h"
28 #include "decoder_fifo.h"
29 #include "video.h"
30 #include "video_output.h"
31
32 #include "vdec_idct.h"
33 #include "video_decoder.h"
34 #include "vdec_motion.h"
35
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"
42
43
44 /*
45  * Local prototypes
46  */
47
48 /****************************************************************************
49  * vpar_MotionCode : Parse the next motion code
50  ****************************************************************************/
51 static __inline__ int vpar_MotionCode( vpar_thread_t * p_vpar )
52 {
53     int i_code;
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} };
64     
65     if( GetBits(&p_vpar->bit_stream, 1) )
66     {
67         return 0;
68     }
69     if( (i_code = ShowBits(&p_vpar->bit_stream, 9)) >= 64 )
70     {
71         i_code >>= 6;
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 );
74     }
75
76     if( i_code >= 24 )
77     {
78         i_code >>= 3;
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 );
81     }
82
83     if( (i_code -= 12) < 0 )
84     {
85         p_vpar->picture.b_error = 1;
86         intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
87         return 0;
88     }
89
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 );            
92 }
93
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 )
99 {
100     int i_limit, i_vector;
101
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;
105
106     if( i_motion_code > 0 )
107     {
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;
111     }
112     else if( i_motion_code < 0 )
113     {
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;
117     }
118     *pi_prediction = i_vector << i_full_pel;
119 }
120
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 )
126 {
127     int i_motion_code, i_motion_residual;
128     int i_r_size;
129     
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];
137
138
139     if( p_vpar->mb.b_dmv )
140     {
141         if( GetBits(&p_vpar->bit_stream, 1) )
142         {
143             p_mb->pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
144         }
145         else
146         {
147             p_mb->pi_dm_vector[0] = 0;
148         }
149     }
150     
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;
155
156    
157     if( (p_vpar->mb.i_mv_format == MOTION_FIELD) && (p_vpar->picture.i_structure == FRAME_STRUCTURE) )
158     {
159          p_vpar->slice.pppi_pmv[i_r][i_s][1] >>= 1;   
160     }
161     
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 );
164
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;   
167      
168     p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->slice.pppi_pmv[i_r][i_s][1];
169
170  
171     if( p_vpar->mb.b_dmv )
172     {
173         if( GetBits(&p_vpar->bit_stream, 1) )
174         {
175             p_mb->pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
176         }
177         else
178         {
179             p_mb->pi_dm_vector[1] = 0;
180         }
181     }
182 }
183
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 )
188 {
189     vpar_MotionVector( p_vpar, p_mb, 0, i_s, p_vpar->picture.pb_full_pel_vector[i_s] );
190 }
191
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 )
196 {
197     if( p_vpar->mb.i_mv_count == 1 )
198     {
199         if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
200         {
201             p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
202                                             = GetBits( &p_vpar->bit_stream, 1 );
203         }
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];
209     }
210     else
211     {
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 );
216     }
217 }
218
219