]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_synchro.c
* Optimisation de vdec_motion.c et video_decoder.c ;
[vlc] / src / video_parser / vpar_synchro.c
1 /*****************************************************************************
2  * vpar_motion.c : motion vectors parsing
3  * (c)1999 VideoLAN
4  *****************************************************************************/
5
6 /*****************************************************************************
7  * Preamble
8  *****************************************************************************/
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <sys/uio.h>
15
16 #include "config.h"
17 #include "common.h"
18 #include "mtime.h"
19 #include "vlc_thread.h"
20
21 #include "intf_msg.h"
22 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
23
24 #include "input.h"
25 #include "input_netlist.h"
26 #include "decoder_fifo.h"
27 #include "video.h"
28 #include "video_output.h"
29
30 #include "vdec_idct.h"
31 #include "video_decoder.h"
32 #include "vdec_motion.h"
33
34 #include "vpar_blocks.h"
35 #include "vpar_headers.h"
36 #include "vpar_synchro.h"
37 #include "video_parser.h"
38 #include "video_fifo.h"
39
40 #define MAX_COUNT 3
41
42 /*
43  * Local prototypes
44  */
45
46 /*****************************************************************************
47  * vpar_SynchroUpdateTab : Update a mean table in the synchro structure
48  *****************************************************************************/
49 float vpar_SynchroUpdateTab( video_synchro_tab_t * tab, int count )
50 {
51         
52     tab->mean = ( tab->mean + MAX_COUNT * count ) / ( MAX_COUNT + 1 );
53     tab->deviation = ( tab->deviation + MAX_COUNT * abs (tab->mean - count) )
54                         / ( MAX_COUNT + 1 );
55
56     return tab->deviation;
57 }
58
59 /*****************************************************************************
60  * vpar_SynchroUpdateStructures : Update the synchro structures
61  *****************************************************************************/
62 void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
63                                    int i_coding_type )
64 {
65     float candidate_deviation;
66     float optimal_deviation;
67     float predict;
68
69     switch(i_coding_type)
70     {
71         case P_CODING_TYPE:
72             p_vpar->synchro.current_p_count++;
73             break;
74         case B_CODING_TYPE:
75             p_vpar->synchro.current_b_count++;
76             break;
77         case I_CODING_TYPE:
78
79             /* update all the structures for P images */
80
81             /* period == 1 */
82             optimal_deviation = vpar_SynchroUpdateTab(
83                             &p_vpar->synchro.tab_p[0],
84                             p_vpar->synchro.current_p_count);
85             predict = p_vpar->synchro.tab_p[0].mean;
86
87             /* period == 2 */
88             candidate_deviation = vpar_SynchroUpdateTab(
89                             &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
90                             p_vpar->synchro.current_p_count);
91             if (candidate_deviation < optimal_deviation)
92             {
93                 optimal_deviation = candidate_deviation;
94                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
95             }
96
97             /* period == 3 */
98             candidate_deviation = vpar_SynchroUpdateTab(
99                             &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
100                             p_vpar->synchro.current_p_count);
101             if (candidate_deviation < optimal_deviation)
102             {
103                 optimal_deviation = candidate_deviation;
104                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
105             }
106
107             p_vpar->synchro.p_count_predict = predict;
108             p_vpar->synchro.current_p_count = 0;
109
110
111             /* update all the structures for B images */
112
113             /* period == 1 */
114             optimal_deviation = vpar_SynchroUpdateTab(
115                             &p_vpar->synchro.tab_b[0],
116                             p_vpar->synchro.current_b_count);
117             predict = p_vpar->synchro.tab_b[0].mean;
118
119             /* period == 2 */
120             candidate_deviation = vpar_SynchroUpdateTab(
121                             &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
122                             p_vpar->synchro.current_b_count);
123             if (candidate_deviation < optimal_deviation)
124             {
125                 optimal_deviation = candidate_deviation;
126                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
127             }
128
129             /* period == 3 */
130             candidate_deviation = vpar_SynchroUpdateTab(
131                             &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
132                             p_vpar->synchro.current_b_count);
133             if (candidate_deviation < optimal_deviation)
134             {
135                 optimal_deviation = candidate_deviation;
136                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
137             }
138
139             p_vpar->synchro.b_count_predict = predict;
140             p_vpar->synchro.current_b_count = 0;
141
142
143             break;
144     }
145
146     p_vpar->synchro.modulo++;
147 }
148
149 /*****************************************************************************
150  * vpar_SynchroChoose : Decide whether we will decode a picture or not
151  *****************************************************************************/
152 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
153                               int i_structure )
154 {
155     static int meuh = 1;
156     static int truc = 0;
157 //    return( 1 );
158     if( i_coding_type == 1 )
159         meuh = 1;
160     if( i_coding_type == 2 )
161         meuh++;
162     truc++;
163     if( truc == 3 )
164     {
165        // while(1);
166     }
167     return( i_coding_type == I_CODING_TYPE || (i_coding_type == P_CODING_TYPE) && (meuh == 2));
168     intf_DbgMsg("vpar debug: synchro image %i - modulo is %i\n", i_coding_type, p_vpar->synchro.modulo);
169     intf_DbgMsg("vpar debug: synchro predict P %e - predict B %e\n", p_vpar->synchro.p_count_predict, p_vpar->synchro.b_count_predict);
170
171     return(0);
172     return( i_coding_type == I_CODING_TYPE );
173 }
174
175 /*****************************************************************************
176  * vpar_SynchroTrash : Update timers when we trash a picture
177  *****************************************************************************/
178 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
179                         int i_structure )
180 {
181     //fprintf ( stderr, "trashing type %i\n", p_vpar->picture.i_coding_type );
182     
183     vpar_SynchroUpdateStructures (p_vpar, i_coding_type);
184
185 }
186
187 /*****************************************************************************
188  * vpar_SynchroDecode : Update timers when we decide to decode a picture
189  *****************************************************************************/
190 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
191                             int i_structure )
192 {
193     //fprintf ( stderr, "decoding type %i\n", p_vpar->picture.i_coding_type );
194
195     vpar_SynchroUpdateStructures (p_vpar, i_coding_type);
196
197     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].decode_date = mdate();
198     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
199         = i_coding_type;
200     p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
201
202     fprintf ( stderr, "%i images in synchro fifo\n", ( p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start ) & 0xf );
203 }
204
205 /*****************************************************************************
206  * vpar_SynchroEnd : Called when the image is totally decoded
207  *****************************************************************************/
208 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
209 {
210     mtime_t * p_decode_time;
211
212     fprintf ( stderr, "type %i decoding time was %lli\n",
213         p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
214         ( mdate()
215            - p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].decode_date )
216            / (( p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start ) & 0xf ));
217
218     p_vpar->synchro.decode_time =
219         ( (p_vpar->synchro.decode_time * 3) + (mdate()
220             - p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].decode_date)
221             / (( p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start)
222           & 0xf) ) >> 2;
223
224     p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
225 }
226
227 /*****************************************************************************
228  * vpar_SynchroDate : When an image has been decoded, ask for its date
229  *****************************************************************************/
230 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
231 {
232     decoder_fifo_t * fifo;
233     mtime_t displaydate;
234
235     fifo = p_vpar->bit_stream.p_decoder_fifo;
236     displaydate = fifo->buffer[fifo->i_start]->i_pts;
237
238     if (displaydate) fprintf(stderr, "displaying type %i at %lli, (time %lli, delta %lli)\n", p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type, displaydate, mdate() + 1000000, displaydate - mdate() - 1000000);
239     return displaydate;
240 }
241