]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_synchro.c
* Added debug messages in the video synchro
[vlc] / src / video_parser / vpar_synchro.c
1 /*****************************************************************************
2  * vpar_synchro.c : frame dropping routines
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors: Christophe Massiot <massiot@via.ecp.fr>
7  *          Samuel Hocevar <sam@via.ecp.fr>
8  *          Jean-Marc Dressler <polux@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*
26  * DISCUSSION : How to Write an efficient Frame-Dropping Algorithm
27  * ==========
28  *
29  * This implementation is based on mathematical and statistical
30  * developments. Older implementations used an enslavement, considering
31  * that if we're late when reading an I picture, we will decode one frame
32  * less. It had a tendancy to derive, and wasn't responsive enough, which
33  * would have caused trouble with the stream control stuff.
34  *
35  * 1. Structure of a picture stream
36  *    =============================
37  * Between 2 I's, we have for instance :
38  *    I   B   P   B   P   B   P   B   P   B   P   B   I
39  *    t0  t1  t2  t3  t4  t5  t6  t7  t8  t9  t10 t11 t12
40  * Please bear in mind that B's and IP's will be inverted when displaying
41  * (decoding order != presentation order). Thus, t1 < t0.
42  *
43  * 2. Definitions
44  *    ===========
45  * t[0..12]     : Presentation timestamps of pictures 0..12.
46  * t            : Current timestamp, at the moment of the decoding.
47  * T            : Picture period, T = 1/frame_rate.
48  * tau[I,P,B]   : Mean time to decode an [I,P,B] picture.
49  * tauYUV       : Mean time to render a picture (given by the video_output).
50  * tau´[I,P,B] = 2 * tau[I,P,B] + tauYUV
51  *              : Mean time + typical difference (estimated to tau/2, that
52  *                needs to be confirmed) + render time.
53  * DELTA        : A given error margin.
54  *
55  * 3. General considerations
56  *    ======================
57  * We define to types of machines :
58  *      2T > tauP  : machines capable of decoding all P pictures
59  *      14T > tauI : machines capable of decoding all I pictures
60  *
61  * 4. Decoding of an I picture
62  *    ========================
63  * On fast machines, we decode all I's.
64  * Otherwise :
65  * We can decode an I picture if we simply have enough time to decode it 
66  * before displaying :
67  *      t0 - t > tau´I + DELTA
68  *
69  * 4. Decoding of a P picture
70  *    =======================
71  * On fast machines, we decode all P's.
72  * Otherwise :
73  * First criterion : have time to decode it.
74  *      t2 - t > tau´P + DELTA
75  *
76  * Second criterion : it shouldn't prevent us from displaying the forthcoming
77  * I picture, which is more important.
78  *      t12 - t > tau´P + tau´I + DELTA
79  *
80  * 5. Decoding of a B picture
81  *    =======================
82  * First criterion : have time to decode it.
83  *      t1 - t > tau´B + DELTA
84  *
85  * Second criterion : it shouldn't prevent us from displaying the forthcoming
86  * P picture, which is more important.
87  *      t4 - t > tau´B + tau´P + DELTA
88  *
89  * I hope you will have a pleasant flight and do not forget your life
90  * jacket.
91  *                                                  --Meuuh (2000-11-09)
92  */
93
94 /*****************************************************************************
95  * Preamble
96  *****************************************************************************/
97 #include "defs.h"
98
99 #include <stdlib.h>                                                /* free() */
100 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
101 #include <sys/uio.h>                                            /* "input.h" */
102
103 #include "config.h"
104 #include "common.h"
105 #include "threads.h"
106 #include "mtime.h"
107 #include "plugins.h"
108
109 #include "intf_msg.h"
110
111 #include "input.h"
112 #include "decoder_fifo.h"
113 #include "video.h"
114 #include "video_output.h"
115
116 #include "vdec_idct.h"
117 #include "video_decoder.h"
118 #include "vdec_motion.h"
119
120 #include "vpar_blocks.h"
121 #include "vpar_headers.h"
122 #include "vpar_synchro.h"
123 #include "video_parser.h"
124
125 #include "main.h"
126
127 /*
128  * Local prototypes
129  */
130 static int  SynchroType( void );
131 static void SynchroNewPicture( vpar_thread_t * p_vpar, int i_coding_type );
132
133 /* Error margins */
134 #define DELTA                   (int)(0.040*CLOCK_FREQ)
135
136 #define DEFAULT_NB_P            5
137 #define DEFAULT_NB_B            1
138
139 /*****************************************************************************
140  * vpar_SynchroInit : You know what ?
141  *****************************************************************************/
142 void vpar_SynchroInit( vpar_thread_t * p_vpar )
143 {
144     p_vpar->synchro.i_type = SynchroType();
145     p_vpar->synchro.i_start = p_vpar->synchro.i_end = 0;
146     vlc_mutex_init( &p_vpar->synchro.fifo_lock );
147
148     /* We use a fake stream pattern, which is often right. */
149     p_vpar->synchro.i_n_p = p_vpar->synchro.i_eta_p = DEFAULT_NB_P;
150     p_vpar->synchro.i_n_b = p_vpar->synchro.i_eta_b = DEFAULT_NB_B;
151     memset( p_vpar->synchro.p_tau, 0, 4 * sizeof(mtime_t) );
152     memset( p_vpar->synchro.pi_meaningful, 0, 4 * sizeof(unsigned int) );
153     p_vpar->synchro.b_dropped_last = 0;
154     p_vpar->synchro.current_pts = mdate() + INPUT_PTS_DELAY;
155     p_vpar->synchro.backward_pts = 0;
156 }
157
158 /*****************************************************************************
159  * vpar_SynchroChoose : Decide whether we will decode a picture or not
160  *****************************************************************************/
161 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
162                               int i_structure )
163 {
164     /* For clarity reasons, we separated the special synchros code from the
165      * mathematical synchro */
166
167     if( p_vpar->synchro.i_type != VPAR_SYNCHRO_DEFAULT )
168     {
169         switch( i_coding_type )
170         {
171         case I_CODING_TYPE:
172             /* I, IP, IP+, IPB */
173             if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus )
174             {
175                 p_vpar->synchro.b_dropped_last = 1;
176             }
177             return( 1 );
178
179         case P_CODING_TYPE:
180             if( p_vpar->synchro.i_type == VPAR_SYNCHRO_I ) /* I */
181             {
182                 return( 0 );
183             }
184
185             if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus ) /* I+ */
186             {
187                 if( p_vpar->synchro.b_dropped_last )
188                 {
189                     p_vpar->synchro.b_dropped_last = 0;
190                     return( 1 );
191                 }
192                 else
193                 {
194                     return( 0 );
195                 }
196             }
197
198             return( 1 ); /* IP, IP+, IPB */
199
200         case B_CODING_TYPE:
201             if( p_vpar->synchro.i_type <= VPAR_SYNCHRO_IP ) /* I, IP */
202             {
203                 return( 0 );
204             }
205             else if( p_vpar->synchro.i_type == VPAR_SYNCHRO_IPB ) /* IPB */
206             {
207                 return( 1 );
208             }
209
210             p_vpar->synchro.b_dropped_last ^= 1; /* IP+ */
211             return( !p_vpar->synchro.b_dropped_last );
212         }
213         return( 0 ); /* never reached but gcc yells at me */
214     }
215     else
216     {
217 #define TAU_PRIME( coding_type )    (p_vpar->synchro.p_tau[(coding_type)] \
218                                  + (p_vpar->synchro.p_tau[(coding_type)] >> 1) \
219                                             + tau_yuv)
220 #define S                           p_vpar->synchro
221         /* VPAR_SYNCHRO_DEFAULT */
222         mtime_t         now, pts, period, tau_yuv;
223         boolean_t       b_decode = 0;
224 #ifdef DEBUG_VPAR
225         char            p_date[MSTRTIME_MAX_SIZE];
226 #endif
227
228         now = mdate();
229         period = 1000000 / (p_vpar->sequence.i_frame_rate) * 1001;
230
231         vlc_mutex_lock( &p_vpar->p_vout->change_lock );
232         tau_yuv = p_vpar->p_vout->render_time;
233         vlc_mutex_unlock( &p_vpar->p_vout->change_lock );
234
235         vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
236
237         switch( i_coding_type )
238         {
239         case I_CODING_TYPE:
240             /* Stream structure changes */
241             if( S.i_n_p )
242                 S.i_n_p = S.i_eta_p;
243
244             if( S.backward_pts )
245             {
246                 pts = S.backward_pts;
247             }
248             else
249             {
250                 /* displaying order : B B P B B I
251                  *                      ^       ^
252                  *                      |       +- current picture
253                  *                      +- current PTS
254                  */
255                 pts = S.current_pts + period * (S.i_n_b + 2);
256             }
257
258             if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
259                     S.p_tau[I_CODING_TYPE] )
260             {
261                 b_decode = 1;
262             }
263             else
264             {
265                 b_decode = (pts - now) > (TAU_PRIME(I_CODING_TYPE) + DELTA);
266             }
267             if( !b_decode )
268                 intf_WarnMsg( 3, "vpar synchro warning: trashing I\n" );
269             break;
270
271         case P_CODING_TYPE:
272             /* Stream structure changes */
273             if( S.i_n_b )
274                 S.i_n_b = S.i_eta_b;
275             if( S.i_eta_p + 1 > S.i_n_p )
276                 S.i_n_p++;
277
278             if( S.backward_pts )
279             {
280                 pts = S.backward_pts;
281             }
282             else
283             {
284                 pts = S.current_pts + period * (S.i_n_b + 2);
285             }
286
287             if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
288                     S.p_tau[I_CODING_TYPE] )
289             {
290                 if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
291                 {
292                     /* Security in case we're _really_ late */
293                     b_decode = (pts - now > 0);
294                 }
295                 else
296                 {
297                     b_decode = (pts - now) > (TAU_PRIME(P_CODING_TYPE) + DELTA);
298                     /* next I */
299                     b_decode &= (pts - now
300                                   + period
301                               * ( (S.i_n_p - S.i_eta_p - 1) * (1 + S.i_n_b) - 1 ))
302                                 > (TAU_PRIME(P_CODING_TYPE)
303                                     + TAU_PRIME(I_CODING_TYPE) + DELTA);
304                 }
305             }
306             else
307             {
308                 b_decode = 0;
309             }
310             break;
311
312         case B_CODING_TYPE:
313             /* Stream structure changes */
314             if( S.i_eta_b + 1 > S.i_n_b )
315                 S.i_n_b++;
316
317             pts = S.current_pts + period;
318
319             if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
320             {
321                 b_decode = (pts - now) > (TAU_PRIME(B_CODING_TYPE) + DELTA);
322
323                 /* Remember that S.i_eta_b is for the moment only eta_b - 1. */
324                 b_decode &= (pts - now
325                              + period
326                              * ( 2 * S.i_n_b - S.i_eta_b + 2))
327                                > (TAU_PRIME(B_CODING_TYPE)
328                                    + TAU_PRIME(P_CODING_TYPE) + DELTA);
329             }
330             else
331             {
332                 b_decode = 0;
333             }
334         }
335
336         vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
337 #ifdef DEBUG_VPAR
338         intf_DbgMsg("vpar synchro debug: %s picture scheduled for %s, %s (%lld)\n",
339                     i_coding_type == B_CODING_TYPE ? "B" :
340                     (i_coding_type == P_CODING_TYPE ? "P" : "I"),
341                     mstrtime(p_date, pts), b_decode ? "decoding" : "trashed",
342                     S.p_tau[i_coding_type]);
343 #endif
344         return( b_decode );
345 #undef S
346 #undef TAU_PRIME
347     }
348 }
349
350 /*****************************************************************************
351  * vpar_SynchroTrash : Update timers when we trash a picture
352  *****************************************************************************/
353 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
354                         int i_structure )
355 {
356     SynchroNewPicture( p_vpar, i_coding_type );
357 #ifdef STATS
358     p_vpar->synchro.i_trashed_pic++;
359 #endif
360 }
361
362 /*****************************************************************************
363  * vpar_SynchroDecode : Update timers when we decide to decode a picture
364  *****************************************************************************/
365 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
366                          int i_structure )
367 {
368     vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
369
370     if( ((p_vpar->synchro.i_end + 1 - p_vpar->synchro.i_start)
371             % MAX_DECODING_PIC) )
372     {
373         p_vpar->synchro.p_date_fifo[p_vpar->synchro.i_end] = mdate();
374         p_vpar->synchro.pi_coding_types[p_vpar->synchro.i_end] = i_coding_type;
375
376         FIFO_INCREMENT( i_end );
377     }
378     else
379     {
380         /* FIFO full, panic() */
381         intf_ErrMsg("vpar error: synchro fifo full, estimations will be biased\n");
382     }
383     vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
384
385     SynchroNewPicture( p_vpar, i_coding_type );
386 }
387
388 /*****************************************************************************
389  * vpar_SynchroEnd : Called when the image is totally decoded
390  *****************************************************************************/
391 void vpar_SynchroEnd( vpar_thread_t * p_vpar, int i_garbage )
392 {
393     mtime_t     tau;
394     int         i_coding_type;
395
396     vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
397
398     if (!i_garbage)
399     {
400         tau = mdate() - p_vpar->synchro.p_date_fifo[p_vpar->synchro.i_start];
401         i_coding_type = p_vpar->synchro.pi_coding_types[p_vpar->synchro.i_start];
402
403         /* Mean with average tau, to ensure stability. */
404         p_vpar->synchro.p_tau[i_coding_type] =
405             (p_vpar->synchro.pi_meaningful[i_coding_type]
406              * p_vpar->synchro.p_tau[i_coding_type] + tau)
407             / (p_vpar->synchro.pi_meaningful[i_coding_type] + 1);
408         if( p_vpar->synchro.pi_meaningful[i_coding_type] < MAX_PIC_AVERAGE )
409         {
410             p_vpar->synchro.pi_meaningful[i_coding_type]++;
411         }
412 #ifdef DEBUG_VPAR
413         intf_DbgMsg("vpar synchro debug: finished decoding %s (%lld)\n",
414                     i_coding_type == B_CODING_TYPE ? "B" :
415                     (i_coding_type == P_CODING_TYPE ? "P" : "I"), tau);
416 #endif
417     }
418
419     FIFO_INCREMENT( i_start );
420
421     vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
422 }
423
424 /*****************************************************************************
425  * vpar_SynchroDate : When an image has been decoded, ask for its date
426  *****************************************************************************/
427 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
428 {
429     /* No need to lock, since PTS are only used by the video parser. */
430     return( p_vpar->synchro.current_pts );
431 }
432
433 /*****************************************************************************
434  * SynchroType: Get the user's synchro type
435  *****************************************************************************
436  * This function is called at initialization.
437  *****************************************************************************/
438 static int SynchroType( void )
439 {
440     char * psz_synchro = main_GetPszVariable( VPAR_SYNCHRO_VAR, NULL );
441
442     if( psz_synchro == NULL )
443     {
444         return VPAR_SYNCHRO_DEFAULT;
445     }
446
447     switch( *psz_synchro++ )
448     {
449       case 'i':
450       case 'I':
451         switch( *psz_synchro++ )
452         {
453           case '\0':
454             return VPAR_SYNCHRO_I;
455
456           case '+':
457             if( *psz_synchro ) return 0;
458             return VPAR_SYNCHRO_Iplus;
459
460           case 'p':
461           case 'P':
462             switch( *psz_synchro++ )
463             {
464               case '\0':
465                 return VPAR_SYNCHRO_IP;
466
467               case '+':
468                 if( *psz_synchro ) return 0;
469                 return VPAR_SYNCHRO_IPplus;
470
471               case 'b':
472               case 'B':
473                 if( *psz_synchro ) return 0;
474                 return VPAR_SYNCHRO_IPB;
475
476               default:
477                 return VPAR_SYNCHRO_DEFAULT;
478                 
479             }
480
481           default:
482             return VPAR_SYNCHRO_DEFAULT;
483         }
484     }
485
486     return VPAR_SYNCHRO_DEFAULT;
487 }
488
489 /*****************************************************************************
490  * SynchroNewPicture: Update stream structure and PTS
491  *****************************************************************************/
492 static void SynchroNewPicture( vpar_thread_t * p_vpar, int i_coding_type )
493 {
494     pes_packet_t * p_pes;
495
496     switch( i_coding_type )
497     {
498     case I_CODING_TYPE:
499         p_vpar->synchro.i_eta_p = p_vpar->synchro.i_eta_b = 0;
500 #ifdef STATS
501         if( p_vpar->synchro.i_type == VPAR_SYNCHRO_DEFAULT )
502         {
503             intf_Msg( "vpar synchro stats: I(%lld) P(%lld)[%d] B(%lld)[%d] YUV(%lld) : %d/%d\n",
504                   p_vpar->synchro.p_tau[I_CODING_TYPE],
505                   p_vpar->synchro.p_tau[P_CODING_TYPE],
506                   p_vpar->synchro.i_n_p,
507                   p_vpar->synchro.p_tau[B_CODING_TYPE],
508                   p_vpar->synchro.i_n_b,
509                   p_vpar->p_vout->render_time,
510                   1 + p_vpar->synchro.i_n_p * (1 + p_vpar->synchro.i_n_b) -
511                   p_vpar->synchro.i_trashed_pic,
512                   1 + p_vpar->synchro.i_n_p * (1 + p_vpar->synchro.i_n_b) );
513             p_vpar->synchro.i_trashed_pic = 0;
514         }
515 #endif
516         break;
517     case P_CODING_TYPE:
518         p_vpar->synchro.i_eta_b = 0;
519         p_vpar->synchro.i_eta_p++;
520         break;
521     case B_CODING_TYPE:
522         p_vpar->synchro.i_eta_b++;
523         break;
524     }
525
526     p_pes = DECODER_FIFO_START( *p_vpar->bit_stream.p_decoder_fifo );
527
528     if( i_coding_type == B_CODING_TYPE )
529     {
530         if( p_pes->b_has_pts )
531         {
532             if( p_pes->i_pts < p_vpar->synchro.current_pts )
533             {
534                 intf_WarnMsg( 2,
535                         "vpar synchro warning: pts_date < current_date\n" );
536             }
537             p_vpar->synchro.current_pts = p_pes->i_pts;
538             p_pes->b_has_pts = 0;
539         }
540         else
541         {
542             p_vpar->synchro.current_pts += 1000000 / (p_vpar->sequence.i_frame_rate) * 1001;
543         }
544     }
545     else
546     {
547         if( p_vpar->synchro.backward_pts == 0 )
548         {
549             p_vpar->synchro.current_pts += 1000000 / (p_vpar->sequence.i_frame_rate) * 1001;
550         }
551         else
552         {
553             if( p_vpar->synchro.backward_pts < p_vpar->synchro.current_pts )
554             {
555                 intf_WarnMsg( 2,
556                         "vpar warning: backward_date < current_date\n" );
557             }
558             p_vpar->synchro.current_pts = p_vpar->synchro.backward_pts;
559             p_vpar->synchro.backward_pts = 0;
560         }
561
562         if( p_pes->b_has_pts )
563         {
564             /* Store the PTS for the next time we have to date an I picture. */
565             p_vpar->synchro.backward_pts = p_pes->i_pts;
566             p_pes->b_has_pts = 0;
567         }
568     }
569 }