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