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