]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_synchro.c
. nouvelle synchro. �a devrait d�j� fonctionner correctement en monothread
[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_delay;
70     mtime_t i_displaydate;
71     decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
72
73     /* interpolate the current _decode_ PTS */
74     i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->i_pts;
75     if( !i_current_pts )
76     {
77         i_current_pts = p_vpar->synchro.i_last_decode_pts
78                        + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
79     }
80     p_vpar->synchro.i_last_decode_pts = i_current_pts;
81  
82     /* see if the current image has a pts - if not, set to 0 */
83     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
84             = i_current_pts;
85
86     /* update display time */
87     i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->i_pts;
88     if( !i_displaydate || i_coding_type != I_CODING_TYPE )
89     {
90         if (!p_vpar->synchro.i_images_since_pts )
91             p_vpar->synchro.i_images_since_pts = 10;
92
93         i_displaydate = p_vpar->synchro.i_last_display_pts
94                        + 1000000.0 / (p_vpar->synchro.theorical_fps);
95         //fprintf (stderr, "  ");
96     }
97     
98     /* else fprintf (stderr, "R ");
99     if (dropped) fprintf (stderr, "  "); else fprintf (stderr, "* ");
100     fprintf (stderr, "%i ", i_coding_type);
101     fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts); */
102
103     p_vpar->synchro.i_images_since_pts--;
104     p_vpar->synchro.i_last_display_pts = i_displaydate;
105
106
107
108     /* update structures */
109     switch(i_coding_type)
110     {
111         case P_CODING_TYPE:
112
113             p_vpar->synchro.current_p_count++;
114             if( !dropped ) p_vpar->synchro.nondropped_p_count++;
115             break;
116
117         case B_CODING_TYPE:
118             p_vpar->synchro.current_b_count++;
119             if( !dropped ) p_vpar->synchro.nondropped_b_count++;
120             break;
121
122         case I_CODING_TYPE:
123
124             /* update information about images we can decode */
125             if (i_current_pts != p_vpar->synchro.i_last_i_pts)
126             {
127                 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
128                 {
129                     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;
130                 }
131                 p_vpar->synchro.i_last_i_pts = i_current_pts;
132             }
133
134             if( !dropped )
135             {
136                 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
137                 {
138                     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;
139                 }
140     
141             }
142
143
144             /* update all the structures for P images */
145
146             /* period == 1 */
147             optimal_deviation = vpar_SynchroUpdateTab(
148                             &p_vpar->synchro.tab_p[0],
149                             p_vpar->synchro.current_p_count);
150             predict = p_vpar->synchro.tab_p[0].mean;
151
152             /* period == 2 */
153             candidate_deviation = vpar_SynchroUpdateTab(
154                             &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
155                             p_vpar->synchro.current_p_count);
156             if (candidate_deviation < optimal_deviation)
157             {
158                 optimal_deviation = candidate_deviation;
159                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
160             }
161
162             /* period == 3 */
163             candidate_deviation = vpar_SynchroUpdateTab(
164                             &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
165                             p_vpar->synchro.current_p_count);
166             if (candidate_deviation < optimal_deviation)
167             {
168                 optimal_deviation = candidate_deviation;
169                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
170             }
171
172             p_vpar->synchro.p_count_predict = predict;
173             p_vpar->synchro.current_p_count = 0;
174
175
176             /* update all the structures for B images */
177
178             /* period == 1 */
179             optimal_deviation = vpar_SynchroUpdateTab(
180                             &p_vpar->synchro.tab_b[0],
181                             p_vpar->synchro.current_b_count);
182             predict = p_vpar->synchro.tab_b[0].mean;
183
184             /* period == 2 */
185             candidate_deviation = vpar_SynchroUpdateTab(
186                             &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
187                             p_vpar->synchro.current_b_count);
188             if (candidate_deviation < optimal_deviation)
189             {
190                 optimal_deviation = candidate_deviation;
191                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
192             }
193
194             /* period == 3 */
195             candidate_deviation = vpar_SynchroUpdateTab(
196                             &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
197                             p_vpar->synchro.current_b_count);
198             if (candidate_deviation < optimal_deviation)
199             {
200                 optimal_deviation = candidate_deviation;
201                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
202             }
203
204             p_vpar->synchro.b_count_predict = predict;
205             p_vpar->synchro.current_b_count = 0;
206             
207             /* now we calculated all statistics, it's time to
208              * decide what we have the time to display
209              */
210             i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
211
212             p_vpar->synchro.can_display_i
213                 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
214
215             p_vpar->synchro.can_display_p
216                     = ( p_vpar->synchro.i_mean_decode_time
217                     * (1 + p_vpar->synchro.p_count_predict) < i_delay );
218
219             if( !p_vpar->synchro.can_display_p )
220             {
221                 p_vpar->synchro.displayable_p
222                     = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
223                 if( p_vpar->synchro.displayable_p < 0 )
224                     p_vpar->synchro.displayable_p = 0;
225             }
226             else
227                 p_vpar->synchro.displayable_p = 0;
228
229             if( p_vpar->synchro.can_display_p
230                 && !(p_vpar->synchro.can_display_b 
231                     = ( p_vpar->synchro.i_mean_decode_time
232                     * (1 + p_vpar->synchro.b_count_predict
233                         + p_vpar->synchro.p_count_predict)) < i_delay) )
234             {
235                 p_vpar->synchro.displayable_b
236                     = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
237                         - p_vpar->synchro.can_display_p;
238             }
239             else
240                 p_vpar->synchro.displayable_b = 0;
241
242 #if 0
243             fprintf( stderr,
244                 "I %i  P %i (%f)  B %i (%f)\n",
245                 p_vpar->synchro.can_display_i,
246                 p_vpar->synchro.can_display_p,
247                 p_vpar->synchro.displayable_p,
248                 p_vpar->synchro.can_display_b,
249                 p_vpar->synchro.displayable_b );
250 #endif
251
252             /* update some values */
253             if( !dropped )
254             {
255                 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
256                 p_vpar->synchro.nondropped_p_count = 0;
257                 p_vpar->synchro.nondropped_b_count = 0;
258             }
259
260             break;
261
262     }
263
264     p_vpar->synchro.modulo++;
265
266 }
267
268 /*****************************************************************************
269  * vpar_SynchroChoose : Decide whether we will decode a picture or not
270  *****************************************************************************/
271 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
272                               int i_structure )
273 {
274     mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
275
276     switch( i_coding_type )
277     {
278         case I_CODING_TYPE:
279
280             return( p_vpar->synchro.can_display_i );
281
282         case P_CODING_TYPE:
283
284             if( p_vpar->synchro.can_display_p )
285                 return( 1 );
286
287             if( p_vpar->synchro.displayable_p * i_delay
288                 < p_vpar->synchro.i_mean_decode_time )
289             {
290                 //fprintf( stderr, "trashed a P\n");
291                 return( 0 );
292             }
293
294             p_vpar->synchro.displayable_p--;
295             return( 1 );
296    
297         case B_CODING_TYPE:
298
299             if( p_vpar->synchro.can_display_b )
300                 return( 1 );
301
302             /* modulo & 0x3 is here to add some randomness */
303             if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
304                 * p_vpar->synchro.i_mean_decode_time )
305             {
306                 //fprintf( stderr, "trashed a B\n");
307                 return( 0 );
308             }
309  
310             if( p_vpar->synchro.displayable_b <= 0 )
311                 return( 0 );
312
313             p_vpar->synchro.displayable_b--;
314             return( 1 );
315     }
316
317     return( 0 );
318
319 }
320
321 /*****************************************************************************
322  * vpar_SynchroTrash : Update timers when we trash a picture
323  *****************************************************************************/
324 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
325                         int i_structure )
326 {
327     vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
328
329 }
330
331 /*****************************************************************************
332  * vpar_SynchroDecode : Update timers when we decide to decode a picture
333  *****************************************************************************/
334 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
335                             int i_structure )
336 {
337     vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
338
339     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
340     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
341         = i_coding_type;
342
343     p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
344
345 }
346
347 /*****************************************************************************
348  * vpar_SynchroEnd : Called when the image is totally decoded
349  *****************************************************************************/
350 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
351 {
352     mtime_t i_decode_time;
353
354     i_decode_time = (mdate() -
355             p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
356         / (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start & 0x0f);
357
358     p_vpar->synchro.i_mean_decode_time =
359         ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
360
361     /* fprintf (stderr,
362         "decoding time was %lli\n",
363         p_vpar->synchro.i_mean_decode_time); */
364
365     p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
366
367 }
368
369 /*****************************************************************************
370  * vpar_SynchroDate : When an image has been decoded, ask for its date
371  *****************************************************************************/
372 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
373 {
374     mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
375     
376 #if 0
377     static mtime_t i_delta = 0;
378
379     fprintf( stderr,
380         "displaying type %i with delay %lli and delta %lli\n",
381         p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
382         i_displaydate - mdate(),
383         i_displaydate - i_delta );
384
385     fprintf (stderr,
386         "theorical fps: %f - actual fps: %f \n",
387         p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
388
389     i_delta = i_displaydate;
390 #endif
391
392     return i_displaydate;
393 }
394