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