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