]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_synchro.c
. encore un peu plus de synchro. si �a saccade c'est normal, �a sera
[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, int dropped )
64 {
65     float candidate_deviation;
66     float optimal_deviation;
67     float predict;
68     mtime_t i_current_pts;
69     mtime_t i_displaydate;
70     decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
71
72     /* interpolate the current _decode_ PTS */
73     i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->i_pts;
74     if( !i_current_pts )
75     {
76         i_current_pts = p_vpar->synchro.i_last_decode_pts
77                        + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
78     }
79     p_vpar->synchro.i_last_decode_pts = i_current_pts;
80  
81     /* see if the current image has a pts - if not, set to 0 */
82     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
83             = i_current_pts;
84
85     /* update display time */
86     i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->i_pts;
87     if( !i_displaydate || i_coding_type != I_CODING_TYPE )
88     {
89         if (!p_vpar->synchro.i_images_since_pts )
90             p_vpar->synchro.i_images_since_pts = 10;
91
92         i_displaydate = p_vpar->synchro.i_last_display_pts
93                        + 1000000.0 / (p_vpar->synchro.theorical_fps);
94         //fprintf (stderr, "  ");
95     }
96     //else fprintf (stderr, "R ");
97     //if (dropped) fprintf (stderr, "  "); else fprintf (stderr, "* ");
98     //fprintf (stderr, "%i ", i_coding_type);
99     //fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts);
100
101     p_vpar->synchro.i_images_since_pts--;
102     p_vpar->synchro.i_last_display_pts = i_displaydate;
103
104
105
106     /* update structures */
107     switch(i_coding_type)
108     {
109         case P_CODING_TYPE:
110
111             p_vpar->synchro.current_p_count++;
112             if( !dropped ) p_vpar->synchro.nondropped_p_count++;
113             break;
114
115         case B_CODING_TYPE:
116             p_vpar->synchro.current_b_count++;
117             if( !dropped ) p_vpar->synchro.nondropped_b_count++;
118             break;
119
120         case I_CODING_TYPE:
121
122             /* update information about images we can decode */
123             if (i_current_pts != p_vpar->synchro.i_last_i_pts)
124             {
125                 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
126                 {
127                     p_vpar->synchro.theorical_fps = (p_vpar->synchro.theorical_fps + 1000000.0 * (1 + p_vpar->synchro.current_b_count + p_vpar->synchro.current_p_count) / (i_current_pts - p_vpar->synchro.i_last_i_pts)) / 2;
128                 }
129                 p_vpar->synchro.i_last_i_pts = i_current_pts;
130             }
131
132             if( !dropped )
133             {
134                 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
135                 {
136                     p_vpar->synchro.actual_fps = (p_vpar->synchro.actual_fps + 1000000.0 * (1 + p_vpar->synchro.nondropped_b_count + p_vpar->synchro.nondropped_p_count) / (i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts)) / 2;
137                 }
138     
139                 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
140                 p_vpar->synchro.nondropped_p_count = 0;
141                 p_vpar->synchro.nondropped_b_count = 0;
142             }
143
144
145             /* update all the structures for P images */
146
147             /* period == 1 */
148             optimal_deviation = vpar_SynchroUpdateTab(
149                             &p_vpar->synchro.tab_p[0],
150                             p_vpar->synchro.current_p_count);
151             predict = p_vpar->synchro.tab_p[0].mean;
152
153             /* period == 2 */
154             candidate_deviation = vpar_SynchroUpdateTab(
155                             &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
156                             p_vpar->synchro.current_p_count);
157             if (candidate_deviation < optimal_deviation)
158             {
159                 optimal_deviation = candidate_deviation;
160                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
161             }
162
163             /* period == 3 */
164             candidate_deviation = vpar_SynchroUpdateTab(
165                             &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
166                             p_vpar->synchro.current_p_count);
167             if (candidate_deviation < optimal_deviation)
168             {
169                 optimal_deviation = candidate_deviation;
170                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
171             }
172
173             p_vpar->synchro.p_count_predict = predict;
174             p_vpar->synchro.current_p_count = 0;
175
176
177             /* update all the structures for B images */
178
179             /* period == 1 */
180             optimal_deviation = vpar_SynchroUpdateTab(
181                             &p_vpar->synchro.tab_b[0],
182                             p_vpar->synchro.current_b_count);
183             predict = p_vpar->synchro.tab_b[0].mean;
184
185             /* period == 2 */
186             candidate_deviation = vpar_SynchroUpdateTab(
187                             &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
188                             p_vpar->synchro.current_b_count);
189             if (candidate_deviation < optimal_deviation)
190             {
191                 optimal_deviation = candidate_deviation;
192                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
193             }
194
195             /* period == 3 */
196             candidate_deviation = vpar_SynchroUpdateTab(
197                             &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
198                             p_vpar->synchro.current_b_count);
199             if (candidate_deviation < optimal_deviation)
200             {
201                 optimal_deviation = candidate_deviation;
202                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
203             }
204
205             p_vpar->synchro.b_count_predict = predict;
206             p_vpar->synchro.current_b_count = 0;
207             
208             break;
209     }
210
211     p_vpar->synchro.modulo++;
212
213 }
214
215 /*****************************************************************************
216  * vpar_SynchroChoose : Decide whether we will decode a picture or not
217  *****************************************************************************/
218 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
219                               int i_structure )
220 {
221     mtime_t i_delay;
222     int keep;
223
224     i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
225
226     //fprintf( stderr, "delay is %lli - ", i_delay);
227     
228 #if 1
229     /*if ( i_coding_type == B_CODING_TYPE )
230         return (0);*/
231
232     //return( i_coding_type == I_CODING_TYPE || i_coding_type == P_CODING_TYPE );
233     if( i_delay > 120000 )
234     {       
235         keep = 1;
236     }
237     else if( i_delay > 100000 )
238     {
239         keep = ( i_coding_type == I_CODING_TYPE
240                     || i_coding_type == P_CODING_TYPE );
241     }
242     else if( i_delay > 50000 )
243     {       
244         keep = ( i_coding_type == I_CODING_TYPE );
245     }
246     else
247     {       
248         keep = 0;
249     }
250 #endif
251
252     //if (!keep) fprintf( stderr, "trashing a type %i with delay %lli\n", i_coding_type, i_delay);
253 //    else fprintf( stderr, "chooser :               ok - displaying a %i \n", i_coding_type);
254
255     return (keep);
256
257 }
258
259 /*****************************************************************************
260  * vpar_SynchroTrash : Update timers when we trash a picture
261  *****************************************************************************/
262 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
263                         int i_structure )
264 {
265     vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
266
267 }
268
269 /*****************************************************************************
270  * vpar_SynchroDecode : Update timers when we decide to decode a picture
271  *****************************************************************************/
272 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
273                             int i_structure )
274 {
275     vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
276
277     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
278     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
279         = i_coding_type;
280
281     p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
282
283 }
284
285 /*****************************************************************************
286  * vpar_SynchroEnd : Called when the image is totally decoded
287  *****************************************************************************/
288 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
289 {
290     mtime_t i_decode_time;
291
292     i_decode_time = (mdate() -
293             p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
294         / (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start & 0x0f);
295
296     p_vpar->synchro.i_mean_decode_time =
297         ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
298
299     //fprintf (stderr, "decoding time is %lli\n", p_vpar->synchro.i_mean_decode_time);
300
301     p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
302
303 }
304
305 /*****************************************************************************
306  * vpar_SynchroDate : When an image has been decoded, ask for its date
307  *****************************************************************************/
308 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
309 {
310     mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
311     static mtime_t i_delta = 0;
312     
313     //fprintf(stderr, "displaying type %i with delay %lli and delta %lli\n", p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type, i_displaydate - mdate(), i_displaydate - i_delta);
314     //fprintf (stderr, "theorical fps: %f - actual fps: %f \n", p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps);
315
316     i_delta = i_displaydate;
317     return i_displaydate;
318 }
319