]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_synchro.c
Tentative d'amelioration de la synchro.
[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 #if 0
47 /*****************************************************************************
48  * vpar_SynchroUpdateTab : Update a mean table in the synchro structure
49  *****************************************************************************/
50 float vpar_SynchroUpdateTab( video_synchro_tab_t * tab, int count )
51 {
52         
53     tab->mean = ( tab->mean + MAX_COUNT * count ) / ( MAX_COUNT + 1 );
54     tab->deviation = ( tab->deviation + MAX_COUNT * abs (tab->mean - count) )
55                         / ( MAX_COUNT + 1 );
56
57     return tab->deviation;
58 }
59
60 /*****************************************************************************
61  * vpar_SynchroUpdateStructures : Update the synchro structures
62  *****************************************************************************/
63 void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
64                                    int i_coding_type, int dropped )
65 {
66     float candidate_deviation;
67     float optimal_deviation;
68     float predict;
69     mtime_t i_current_pts;
70     mtime_t i_delay;
71     mtime_t i_displaydate;
72     decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
73
74     /* interpolate the current _decode_ PTS */
75     i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
76                     decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
77                     0;
78     if( !i_current_pts )
79     {
80         i_current_pts = p_vpar->synchro.i_last_decode_pts
81                        + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
82     }
83     p_vpar->synchro.i_last_decode_pts = i_current_pts;
84  
85     /* see if the current image has a pts - if not, set to 0 */
86     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
87             = i_current_pts;
88
89     /* update display time */
90     i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
91                     decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
92                     0;
93     if( !i_displaydate || i_coding_type != I_CODING_TYPE )
94     {
95         if (!p_vpar->synchro.i_images_since_pts )
96             p_vpar->synchro.i_images_since_pts = 10;
97
98         i_displaydate = p_vpar->synchro.i_last_display_pts
99                        + 1000000.0 / (p_vpar->synchro.theorical_fps);
100         //fprintf (stderr, "  ");
101     }
102     
103     decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts = 0;
104
105     /* else fprintf (stderr, "R ");
106     if (dropped) fprintf (stderr, "  "); else fprintf (stderr, "* ");
107     fprintf (stderr, "%i ", i_coding_type);
108     fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts); */
109
110     p_vpar->synchro.i_images_since_pts--;
111     p_vpar->synchro.i_last_display_pts = i_displaydate;
112
113
114
115     /* update structures */
116     switch(i_coding_type)
117     {
118         case P_CODING_TYPE:
119
120             p_vpar->synchro.current_p_count++;
121             if( !dropped ) p_vpar->synchro.nondropped_p_count++;
122             break;
123
124         case B_CODING_TYPE:
125             p_vpar->synchro.current_b_count++;
126             if( !dropped ) p_vpar->synchro.nondropped_b_count++;
127             break;
128
129         case I_CODING_TYPE:
130
131             /* update information about images we can decode */
132             if (i_current_pts != p_vpar->synchro.i_last_i_pts)
133             {
134                 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
135                 {
136                     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;
137                 }
138                 p_vpar->synchro.i_last_i_pts = i_current_pts;
139             }
140
141             if( !dropped )
142             {
143                 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
144                 {
145                     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;
146                 }
147     
148             }
149
150
151             /* update all the structures for P images */
152
153             /* period == 1 */
154             optimal_deviation = vpar_SynchroUpdateTab(
155                             &p_vpar->synchro.tab_p[0],
156                             p_vpar->synchro.current_p_count);
157             predict = p_vpar->synchro.tab_p[0].mean;
158
159             /* period == 2 */
160             candidate_deviation = vpar_SynchroUpdateTab(
161                             &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
162                             p_vpar->synchro.current_p_count);
163             if (candidate_deviation < optimal_deviation)
164             {
165                 optimal_deviation = candidate_deviation;
166                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
167             }
168
169             /* period == 3 */
170             candidate_deviation = vpar_SynchroUpdateTab(
171                             &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
172                             p_vpar->synchro.current_p_count);
173             if (candidate_deviation < optimal_deviation)
174             {
175                 optimal_deviation = candidate_deviation;
176                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
177             }
178
179             p_vpar->synchro.p_count_predict = predict;
180             p_vpar->synchro.current_p_count = 0;
181
182
183             /* update all the structures for B images */
184
185             /* period == 1 */
186             optimal_deviation = vpar_SynchroUpdateTab(
187                             &p_vpar->synchro.tab_b[0],
188                             p_vpar->synchro.current_b_count);
189             predict = p_vpar->synchro.tab_b[0].mean;
190
191             /* period == 2 */
192             candidate_deviation = vpar_SynchroUpdateTab(
193                             &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
194                             p_vpar->synchro.current_b_count);
195             if (candidate_deviation < optimal_deviation)
196             {
197                 optimal_deviation = candidate_deviation;
198                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
199             }
200
201             /* period == 3 */
202             candidate_deviation = vpar_SynchroUpdateTab(
203                             &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
204                             p_vpar->synchro.current_b_count);
205             if (candidate_deviation < optimal_deviation)
206             {
207                 optimal_deviation = candidate_deviation;
208                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
209             }
210
211             p_vpar->synchro.b_count_predict = predict;
212             p_vpar->synchro.current_b_count = 0;
213             
214             /* now we calculated all statistics, it's time to
215              * decide what we have the time to display
216              */
217             i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
218
219             p_vpar->synchro.can_display_i
220                 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
221
222             p_vpar->synchro.can_display_p
223                     = ( p_vpar->synchro.i_mean_decode_time
224                     * (1 + p_vpar->synchro.p_count_predict) < i_delay );
225
226             if( !p_vpar->synchro.can_display_p )
227             {
228                 p_vpar->synchro.displayable_p
229                     = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
230                 if( p_vpar->synchro.displayable_p < 0 )
231                     p_vpar->synchro.displayable_p = 0;
232             }
233             else
234                 p_vpar->synchro.displayable_p = 0;
235
236             if( p_vpar->synchro.can_display_p
237                 && !(p_vpar->synchro.can_display_b 
238                     = ( p_vpar->synchro.i_mean_decode_time
239                     * (1 + p_vpar->synchro.b_count_predict
240                         + p_vpar->synchro.p_count_predict)) < i_delay) )
241             {
242                 p_vpar->synchro.displayable_b
243                     = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
244                         - p_vpar->synchro.can_display_p;
245             }
246             else
247                 p_vpar->synchro.displayable_b = 0;
248
249 #if 0
250             fprintf( stderr,
251                 "I %i  P %i (%f)  B %i (%f)\n",
252                 p_vpar->synchro.can_display_i,
253                 p_vpar->synchro.can_display_p,
254                 p_vpar->synchro.displayable_p,
255                 p_vpar->synchro.can_display_b,
256                 p_vpar->synchro.displayable_b );
257 #endif
258
259             /* update some values */
260             if( !dropped )
261             {
262                 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
263                 p_vpar->synchro.nondropped_p_count = 0;
264                 p_vpar->synchro.nondropped_b_count = 0;
265             }
266
267             break;
268
269     }
270
271     p_vpar->synchro.modulo++;
272
273 }
274
275 /*****************************************************************************
276  * vpar_SynchroChoose : Decide whether we will decode a picture or not
277  *****************************************************************************/
278 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
279                               int i_structure )
280 {
281     mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
282
283     switch( i_coding_type )
284     {
285         case I_CODING_TYPE:
286
287             return( p_vpar->synchro.can_display_i );
288
289         case P_CODING_TYPE:
290
291             if( p_vpar->synchro.can_display_p )
292                 return( 1 );
293
294             if( p_vpar->synchro.displayable_p * i_delay
295                 < p_vpar->synchro.i_mean_decode_time )
296             {
297                 //fprintf( stderr, "trashed a P\n");
298                 return( 0 );
299             }
300
301             p_vpar->synchro.displayable_p--;
302             return( 1 );
303    
304         case B_CODING_TYPE:
305
306             if( p_vpar->synchro.can_display_b )
307                 return( 1 );
308
309             /* modulo & 0x3 is here to add some randomness */
310             if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
311                 * p_vpar->synchro.i_mean_decode_time )
312             {
313                 //fprintf( stderr, "trashed a B\n");
314                 return( 0 );
315             }
316  
317             if( p_vpar->synchro.displayable_b <= 0 )
318                 return( 0 );
319
320             p_vpar->synchro.displayable_b--;
321             return( 1 );
322     }
323
324     return( 0 );
325
326 }
327
328 /*****************************************************************************
329  * vpar_SynchroTrash : Update timers when we trash a picture
330  *****************************************************************************/
331 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
332                         int i_structure )
333 {
334     vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
335
336 }
337
338 /*****************************************************************************
339  * vpar_SynchroDecode : Update timers when we decide to decode a picture
340  *****************************************************************************/
341 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
342                             int i_structure )
343 {
344     vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
345
346     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
347     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
348         = i_coding_type;
349
350     p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
351
352 }
353
354 /*****************************************************************************
355  * vpar_SynchroEnd : Called when the image is totally decoded
356  *****************************************************************************/
357 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
358 {
359     mtime_t i_decode_time;
360
361     i_decode_time = (mdate() -
362             p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
363         / (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start & 0x0f);
364
365     p_vpar->synchro.i_mean_decode_time =
366         ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
367
368     /* fprintf (stderr,
369         "decoding time was %lli\n",
370         p_vpar->synchro.i_mean_decode_time); */
371
372     p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
373
374 }
375
376 /*****************************************************************************
377  * vpar_SynchroDate : When an image has been decoded, ask for its date
378  *****************************************************************************/
379 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
380 {
381     mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
382     
383 #if 0
384     static mtime_t i_delta = 0;
385
386     fprintf( stderr,
387         "displaying type %i with delay %lli and delta %lli\n",
388         p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
389         i_displaydate - mdate(),
390         i_displaydate - i_delta );
391
392     fprintf (stderr,
393         "theorical fps: %f - actual fps: %f \n",
394         p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
395
396     i_delta = i_displaydate;
397 #endif
398
399     return i_displaydate;
400 }
401
402 #else
403
404 /* synchro a deux balles backportee du decodeur de reference. NE MARCHE PAS
405 AVEC LES IMAGES MONOTRAMES */
406
407 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
408                               int i_structure )
409 {
410     switch (i_coding_type) 
411     {
412     case B_CODING_TYPE:
413         if ((p_vpar->synchro.kludge_level <= p_vpar->synchro.kludge_nbp))
414         {
415             p_vpar->synchro.kludge_b++;
416             return( 0 );
417         }
418         if (p_vpar->synchro.kludge_b %
419              (p_vpar->synchro.kludge_nbb /
420                 (p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp)))
421         {
422             p_vpar->synchro.kludge_b++;
423             return( 0 );
424         }
425         p_vpar->synchro.kludge_b++;
426         return( 1 );
427
428     case P_CODING_TYPE:
429         if (p_vpar->synchro.kludge_p++ >= p_vpar->synchro.kludge_level) 
430         {
431             return( 0 );
432         }
433         return( 1 );
434
435     default:
436         return( 1 );
437     }
438 }
439
440 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
441                         int i_structure )
442 {
443     if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
444     {
445         p_vpar->synchro.kludge_nbframes = 0;
446         p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
447         DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
448     }
449     else
450         p_vpar->synchro.kludge_nbframes++;
451 }
452
453 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
454                             int i_structure )
455 {
456     if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
457     {
458         p_vpar->synchro.kludge_nbframes = 0;
459         p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
460         DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
461     }
462     else
463         p_vpar->synchro.kludge_nbframes++;
464 }
465
466 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
467 {
468     return( p_vpar->synchro.kludge_date
469             + p_vpar->synchro.kludge_nbframes*1000000/(p_vpar->sequence.r_frame_rate ) );
470 }
471
472 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
473 {
474 }
475
476 void vpar_SynchroKludge( vpar_thread_t * p_vpar, mtime_t date )
477 {
478     mtime_t     show_date;
479     int         temp = p_vpar->synchro.kludge_level;
480
481     p_vpar->synchro.kludge_nbp = p_vpar->synchro.kludge_p ? p_vpar->synchro.kludge_p : 5;
482     p_vpar->synchro.kludge_nbb = p_vpar->synchro.kludge_b ? p_vpar->synchro.kludge_b : 6;
483     show_date = date - mdate();
484     p_vpar->synchro.kludge_p = 0;
485     p_vpar->synchro.kludge_b = 0;
486
487     if (show_date < (SYNC_DELAY - SYNC_TOLERATE) && show_date <= p_vpar->synchro.kludge_prevdate)
488     {
489         p_vpar->synchro.kludge_level--;
490         if (p_vpar->synchro.kludge_level < 0)
491             p_vpar->synchro.kludge_level = 0;
492         else if (p_vpar->synchro.kludge_level >
493                      p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
494             p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
495 #ifdef DEBUG
496         if (temp != p_vpar->synchro.kludge_level)
497             intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
498                         temp, p_vpar->synchro.kludge_level, show_date );
499 #endif
500     }
501     else if (show_date > (SYNC_DELAY + SYNC_TOLERATE) && show_date >= p_vpar->synchro.kludge_prevdate)
502     {
503         p_vpar->synchro.kludge_level++;
504         if (p_vpar->synchro.kludge_level > p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
505             p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
506 #ifdef DEBUG
507         if (temp != p_vpar->synchro.kludge_level)
508             intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
509                         temp, p_vpar->synchro.kludge_level, show_date );
510 #endif
511     }
512
513     p_vpar->synchro.kludge_prevdate = show_date;
514     if ((p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp) > p_vpar->synchro.kludge_nbb)
515         p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbb + p_vpar->synchro.kludge_nbp;
516 }
517
518 #endif