]> git.sesse.net Git - vlc/blob - src/video_decoder/vpar_synchro.c
Tuned constants, since it seems to make people happy.
[vlc] / src / video_decoder / 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.4 2001/07/24 12:03:00 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 three types of machines :
59  *      14T > tauI : machines capable of decoding all I pictures
60  *      2T > tauP  : machines capable of decoding all P pictures
61  *      T > tauB   : machines capable of decoding all B pictures
62  *
63  * 4. Decoding of an I picture
64  *    ========================
65  * On fast machines, we decode all I's.
66  * Otherwise :
67  * We can decode an I picture if we simply have enough time to decode it 
68  * before displaying :
69  *      t0 - t > tau´I + DELTA
70  *
71  * 5. Decoding of a P picture
72  *    =======================
73  * On fast machines, we decode all P's.
74  * Otherwise :
75  * First criterion : have time to decode it.
76  *      t2 - t > tau´P + DELTA
77  *
78  * Second criterion : it shouldn't prevent us from displaying the forthcoming
79  * I picture, which is more important.
80  *      t12 - t > tau´P + tau´I + DELTA
81  *
82  * 6. Decoding of a B picture
83  *    =======================
84  * On fast machines, we decode all B's. Otherwise :
85  *      t1 - t > tau´B + DELTA
86  * Since the next displayed I or P is already decoded, we don't have to
87  * worry about it.
88  *
89  * I hope you will have a pleasant flight and do not forget your life
90  * jacket.
91  *                                                  --Meuuh (2000-12-29)
92  */
93
94 /*****************************************************************************
95  * Preamble
96  *****************************************************************************/
97 #include "defs.h"
98
99 #include <string.h>                                    /* memcpy(), memset() */
100
101 #include "config.h"
102 #include "common.h"
103 #include "threads.h"
104 #include "mtime.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 "vdec_ext-plugins.h"
115 #include "vpar_pool.h"
116 #include "video_parser.h"
117
118 #include "main.h"
119
120 /*
121  * Local prototypes
122  */
123 static int  SynchroType( void );
124
125 /* Error margins */
126 #define DELTA                   (int)(0.075*CLOCK_FREQ)
127
128 #define DEFAULT_NB_P            5
129 #define DEFAULT_NB_B            1
130
131 /*****************************************************************************
132  * vpar_SynchroInit : You know what ?
133  *****************************************************************************/
134 void vpar_SynchroInit( vpar_thread_t * p_vpar )
135 {
136     p_vpar->synchro.i_type = SynchroType();
137
138     /* We use a fake stream pattern, which is often right. */
139     p_vpar->synchro.i_n_p = p_vpar->synchro.i_eta_p = DEFAULT_NB_P;
140     p_vpar->synchro.i_n_b = p_vpar->synchro.i_eta_b = DEFAULT_NB_B;
141     memset( p_vpar->synchro.p_tau, 0, 4 * sizeof(mtime_t) );
142     memset( p_vpar->synchro.pi_meaningful, 0, 4 * sizeof(unsigned int) );
143     p_vpar->synchro.b_dropped_last = 0;
144     p_vpar->synchro.current_pts = mdate() + DEFAULT_PTS_DELAY;
145     p_vpar->synchro.backward_pts = 0;
146     p_vpar->synchro.i_current_period = p_vpar->synchro.i_backward_period = 0;
147 #ifdef STATS
148     p_vpar->synchro.i_trashed_pic = p_vpar->synchro.i_not_chosen_pic = 
149         p_vpar->synchro.i_pic = 0;
150 #endif
151 }
152
153 /*****************************************************************************
154  * vpar_SynchroChoose : Decide whether we will decode a picture or not
155  *****************************************************************************/
156 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
157                               int i_structure )
158 {
159     /* For clarity reasons, we separated the special synchros code from the
160      * mathematical synchro */
161
162     if( p_vpar->synchro.i_type != VPAR_SYNCHRO_DEFAULT )
163     {
164         switch( i_coding_type )
165         {
166         case I_CODING_TYPE:
167             /* I, IP, IP+, IPB */
168             if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus )
169             {
170                 p_vpar->synchro.b_dropped_last = 1;
171             }
172             return( 1 );
173
174         case P_CODING_TYPE:
175             if( p_vpar->synchro.i_type == VPAR_SYNCHRO_I ) /* I */
176             {
177                 return( 0 );
178             }
179
180             if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus ) /* I+ */
181             {
182                 if( p_vpar->synchro.b_dropped_last )
183                 {
184                     p_vpar->synchro.b_dropped_last = 0;
185                     return( 1 );
186                 }
187                 else
188                 {
189                     return( 0 );
190                 }
191             }
192
193             return( 1 ); /* IP, IP+, IPB */
194
195         case B_CODING_TYPE:
196             if( p_vpar->synchro.i_type <= VPAR_SYNCHRO_IP ) /* I, IP */
197             {
198                 return( 0 );
199             }
200             else if( p_vpar->synchro.i_type == VPAR_SYNCHRO_IPB ) /* IPB */
201             {
202                 return( 1 );
203             }
204
205             p_vpar->synchro.b_dropped_last ^= 1; /* IP+ */
206             return( !p_vpar->synchro.b_dropped_last );
207         }
208         return( 0 ); /* never reached but gcc yells at me */
209     }
210     else
211     {
212 #define TAU_PRIME( coding_type )    (p_vpar->synchro.p_tau[(coding_type)] \
213                                  + (p_vpar->synchro.p_tau[(coding_type)] >> 1) \
214                                             + tau_yuv)
215 #define S                           p_vpar->synchro
216         /* VPAR_SYNCHRO_DEFAULT */
217         mtime_t         now, period, tau_yuv;
218         mtime_t         pts = 0;
219         boolean_t       b_decode = 0;
220 #ifdef TRACE_VPAR
221         char            p_date[MSTRTIME_MAX_SIZE];
222 #endif
223
224         now = mdate();
225         period = 1000000 * 1001 / p_vpar->sequence.i_frame_rate
226                     * p_vpar->sequence.i_current_rate / DEFAULT_RATE;
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         switch( i_coding_type )
233         {
234         case I_CODING_TYPE:
235             if( S.backward_pts )
236             {
237                 pts = S.backward_pts;
238             }
239             else
240             {
241                 /* displaying order : B B P B B I
242                  *                      ^       ^
243                  *                      |       +- current picture
244                  *                      +- current PTS
245                  */
246                 pts = S.current_pts + period * (S.i_n_b + 2);
247             }
248
249             if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
250                     S.p_tau[I_CODING_TYPE] )
251             {
252                 b_decode = 1;
253             }
254             else
255             {
256                 b_decode = (pts - now) > (TAU_PRIME(I_CODING_TYPE) + DELTA);
257             }
258             if( !b_decode )
259                 intf_WarnMsg( 1, "vpar synchro warning: trashing I (%lld)",
260                              pts - now);
261             break;
262
263         case P_CODING_TYPE:
264             if( S.backward_pts )
265             {
266                 pts = S.backward_pts;
267             }
268             else
269             {
270                 pts = S.current_pts + period * (S.i_n_b + 1);
271             }
272
273             if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
274                     S.p_tau[I_CODING_TYPE] )
275             {
276                 if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
277                 {
278                     /* Security in case we're _really_ late */
279                     b_decode = (pts - now > 0);
280                 }
281                 else
282                 {
283                     b_decode = (pts - now) > (TAU_PRIME(P_CODING_TYPE) + DELTA);
284                     /* next I */
285                     b_decode &= (pts - now
286                                   + period
287                               * ( (S.i_n_p - S.i_eta_p) * (1 + S.i_n_b) - 1 ))
288                                 > (TAU_PRIME(P_CODING_TYPE)
289                                     + TAU_PRIME(I_CODING_TYPE) + DELTA);
290                 }
291             }
292             else
293             {
294                 b_decode = 0;
295             }
296             break;
297
298         case B_CODING_TYPE:
299             pts = S.current_pts;
300
301             if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
302             {
303                 b_decode = (pts - now) > (TAU_PRIME(B_CODING_TYPE) + DELTA);
304             }
305             else
306             {
307                 b_decode = 0;
308             }
309         }
310
311 #ifdef TRACE_VPAR
312         intf_DbgMsg("vpar synchro debug: %s picture scheduled for %s, %s (%lld)",
313                     i_coding_type == B_CODING_TYPE ? "B" :
314                     (i_coding_type == P_CODING_TYPE ? "P" : "I"),
315                     mstrtime(p_date, pts), b_decode ? "decoding" : "trashed",
316                     S.p_tau[i_coding_type]);
317 #endif
318 #ifdef STATS
319         if( !b_decode )
320         {
321             S.i_not_chosen_pic++;
322         }
323 #endif
324         return( b_decode );
325 #undef S
326 #undef TAU_PRIME
327     }
328 }
329
330 /*****************************************************************************
331  * vpar_SynchroTrash : Update counters when we trash a picture
332  *****************************************************************************/
333 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
334                         int i_structure )
335 {
336 #ifdef STATS
337     p_vpar->synchro.i_trashed_pic++;
338 #endif
339 }
340
341 /*****************************************************************************
342  * vpar_SynchroDecode : Update timers when we decide to decode a picture
343  *****************************************************************************/
344 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
345                          int i_structure )
346 {
347     p_vpar->synchro.decoding_start = mdate();
348 }
349
350 /*****************************************************************************
351  * vpar_SynchroEnd : Called when the image is totally decoded
352  *****************************************************************************/
353 void vpar_SynchroEnd( vpar_thread_t * p_vpar, int i_coding_type,
354                       int i_structure, int i_garbage )
355 {
356     mtime_t     tau;
357
358     if( !i_garbage )
359     {
360         tau = mdate() - p_vpar->synchro.decoding_start;
361
362         /* If duration too high, something happened (pause ?), so don't
363          * take it into account. */
364         if( tau < 3 * p_vpar->synchro.p_tau[i_coding_type]
365              || !p_vpar->synchro.pi_meaningful[i_coding_type] )
366         {
367             /* Mean with average tau, to ensure stability. */
368             p_vpar->synchro.p_tau[i_coding_type] =
369                 (p_vpar->synchro.pi_meaningful[i_coding_type]
370                  * p_vpar->synchro.p_tau[i_coding_type] + tau)
371                 / (p_vpar->synchro.pi_meaningful[i_coding_type] + 1);
372             if( p_vpar->synchro.pi_meaningful[i_coding_type] < MAX_PIC_AVERAGE )
373             {
374                 p_vpar->synchro.pi_meaningful[i_coding_type]++;
375             }
376         }
377
378 #ifdef TRACE_VPAR
379         intf_DbgMsg("vpar synchro debug: finished decoding %s (%lld)",
380                     i_coding_type == B_CODING_TYPE ? "B" :
381                     (i_coding_type == P_CODING_TYPE ? "P" : "I"), tau);
382 #endif
383     }
384     else
385     {
386         intf_DbgMsg("vpar synchro debug: aborting %s",
387                     i_coding_type == B_CODING_TYPE ? "B" :
388                     (i_coding_type == P_CODING_TYPE ? "P" : "I"));
389     }
390 }
391
392 /*****************************************************************************
393  * vpar_SynchroDate : When an image has been decoded, ask for its date
394  *****************************************************************************/
395 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
396 {
397     /* No need to lock, since PTS are only used by the video parser. */
398     return( p_vpar->synchro.current_pts );
399 }
400
401 /*****************************************************************************
402  * vpar_SynchroNewPicture: Update stream structure and PTS
403  *****************************************************************************/
404 void vpar_SynchroNewPicture( vpar_thread_t * p_vpar, int i_coding_type,
405                              int i_repeat_field )
406 {
407     mtime_t         period = 1000000 * 1001 / p_vpar->sequence.i_frame_rate
408                               * p_vpar->sequence.i_current_rate / DEFAULT_RATE;
409 #if 0
410     mtime_t         now = mdate(); 
411 #endif
412
413     switch( i_coding_type )
414     {
415     case I_CODING_TYPE:
416         if( p_vpar->synchro.i_eta_p
417              && p_vpar->synchro.i_eta_p != p_vpar->synchro.i_n_p )
418         {
419             intf_WarnMsg( 3, "vpar info: stream periodicity changed "
420                           "from P[%d] to P[%d]",
421                           p_vpar->synchro.i_n_p, p_vpar->synchro.i_eta_p );
422             p_vpar->synchro.i_n_p = p_vpar->synchro.i_eta_p;
423         }
424         p_vpar->synchro.i_eta_p = p_vpar->synchro.i_eta_b = 0;
425 #ifdef STATS
426         if( p_vpar->synchro.i_type == VPAR_SYNCHRO_DEFAULT )
427         {
428             intf_Msg( "vpar synchro stats: I(%lld) P(%lld)[%d] B(%lld)[%d] YUV(%lld) : trashed %d:%d/%d",
429                   p_vpar->synchro.p_tau[I_CODING_TYPE],
430                   p_vpar->synchro.p_tau[P_CODING_TYPE],
431                   p_vpar->synchro.i_n_p,
432                   p_vpar->synchro.p_tau[B_CODING_TYPE],
433                   p_vpar->synchro.i_n_b,
434                   p_vpar->p_vout->render_time,
435                   p_vpar->synchro.i_not_chosen_pic,
436                   p_vpar->synchro.i_trashed_pic -
437                   p_vpar->synchro.i_not_chosen_pic,
438                   p_vpar->synchro.i_pic );
439             p_vpar->synchro.i_trashed_pic = p_vpar->synchro.i_not_chosen_pic
440                 = p_vpar->synchro.i_pic = 0;
441         }
442 #endif
443         break;
444     case P_CODING_TYPE:
445         p_vpar->synchro.i_eta_p++;
446         if( p_vpar->synchro.i_eta_b
447              && p_vpar->synchro.i_eta_b != p_vpar->synchro.i_n_b )
448         {
449             intf_WarnMsg( 3, "vpar info: stream periodicity changed "
450                           "from B[%d] to B[%d]",
451                           p_vpar->synchro.i_n_b, p_vpar->synchro.i_eta_b );
452             p_vpar->synchro.i_n_b = p_vpar->synchro.i_eta_b;
453         }
454         p_vpar->synchro.i_eta_b = 0;
455         break;
456     case B_CODING_TYPE:
457         p_vpar->synchro.i_eta_b++;
458         break;
459     }
460
461     p_vpar->synchro.current_pts += p_vpar->synchro.i_current_period
462                                         * (period >> 1);
463  
464 #define PTS_THRESHOLD   (period >> 2)
465     if( i_coding_type == B_CODING_TYPE )
466     {
467         /* A video frame can be displayed 1, 2 or 3 times, according to
468          * repeat_first_field, top_field_first, progressive_sequence and
469          * progressive_frame. */
470         p_vpar->synchro.i_current_period = i_repeat_field;
471
472         if( p_vpar->sequence.next_pts )
473         {
474             if( p_vpar->sequence.next_pts - p_vpar->synchro.current_pts
475                     > PTS_THRESHOLD
476                  || p_vpar->synchro.current_pts - p_vpar->sequence.next_pts
477                     > PTS_THRESHOLD )
478             {
479                 intf_WarnMsg( 2,
480                         "vpar synchro warning: pts != current_date (%lld)",
481                         p_vpar->synchro.current_pts
482                             - p_vpar->sequence.next_pts );
483             }
484             p_vpar->synchro.current_pts = p_vpar->sequence.next_pts;
485             p_vpar->sequence.next_pts = 0;
486         }
487     }
488     else
489     {
490         p_vpar->synchro.i_current_period = p_vpar->synchro.i_backward_period;
491         p_vpar->synchro.i_backward_period = i_repeat_field;
492
493         if( p_vpar->synchro.backward_pts )
494         {
495             if( p_vpar->sequence.next_dts && 
496                 (p_vpar->sequence.next_dts - p_vpar->synchro.backward_pts
497                     > PTS_THRESHOLD
498               || p_vpar->synchro.backward_pts - p_vpar->sequence.next_dts
499                     > PTS_THRESHOLD) )
500             {
501                 intf_WarnMsg( 2,
502                         "vpar synchro warning: backward_pts != dts (%lld)",
503                         p_vpar->sequence.next_dts
504                             - p_vpar->synchro.backward_pts );
505             }
506             if( p_vpar->synchro.backward_pts - p_vpar->synchro.current_pts
507                     > PTS_THRESHOLD
508                  || p_vpar->synchro.current_pts - p_vpar->synchro.backward_pts
509                     > PTS_THRESHOLD )
510             {
511                 intf_WarnMsg( 2,
512                    "vpar synchro warning: backward_pts != current_pts (%lld)",
513                    p_vpar->synchro.current_pts - p_vpar->synchro.backward_pts );
514             }
515             p_vpar->synchro.current_pts = p_vpar->synchro.backward_pts;
516             p_vpar->synchro.backward_pts = 0;
517         }
518         else if( p_vpar->sequence.next_dts )
519         {
520             if( p_vpar->sequence.next_dts - p_vpar->synchro.current_pts
521                     > PTS_THRESHOLD
522                  || p_vpar->synchro.current_pts - p_vpar->sequence.next_dts
523                     > PTS_THRESHOLD )
524             {
525                 intf_WarnMsg( 2,
526                         "vpar synchro warning: dts != current_pts (%lld)",
527                         p_vpar->synchro.current_pts
528                             - p_vpar->sequence.next_dts );
529             }
530             /* By definition of a DTS. */
531             p_vpar->synchro.current_pts = p_vpar->sequence.next_dts;
532             p_vpar->sequence.next_dts = 0;
533         }
534
535         if( p_vpar->sequence.next_pts )
536         {
537             /* Store the PTS for the next time we have to date an I picture. */
538             p_vpar->synchro.backward_pts = p_vpar->sequence.next_pts;
539             p_vpar->sequence.next_pts = 0;
540         }
541     }
542 #undef PTS_THRESHOLD
543
544 #if 0
545     /* Removed for incompatibility with slow motion */
546     if( p_vpar->synchro.current_pts + DEFAULT_PTS_DELAY < now )
547     {
548         /* We cannot be _that_ late, something must have happened, reinit
549          * the dates. */
550         intf_WarnMsg( 2, "PTS << now (%lld), resetting",
551                       now - p_vpar->synchro.current_pts - DEFAULT_PTS_DELAY );
552         p_vpar->synchro.current_pts = now + DEFAULT_PTS_DELAY;
553     }
554     if( p_vpar->synchro.backward_pts
555          && p_vpar->synchro.backward_pts + DEFAULT_PTS_DELAY < now )
556     {
557         /* The same. */
558         p_vpar->synchro.backward_pts = 0;
559     }
560 #endif
561
562 #ifdef STATS
563     p_vpar->synchro.i_pic++;
564 #endif
565 }
566
567 /*****************************************************************************
568  * SynchroType: Get the user's synchro type
569  *****************************************************************************
570  * This function is called at initialization.
571  *****************************************************************************/
572 static int SynchroType( void )
573 {
574     char * psz_synchro = main_GetPszVariable( VPAR_SYNCHRO_VAR, NULL );
575
576     if( psz_synchro == NULL )
577     {
578         return VPAR_SYNCHRO_DEFAULT;
579     }
580
581     switch( *psz_synchro++ )
582     {
583       case 'i':
584       case 'I':
585         switch( *psz_synchro++ )
586         {
587           case '\0':
588             return VPAR_SYNCHRO_I;
589
590           case '+':
591             if( *psz_synchro ) return 0;
592             return VPAR_SYNCHRO_Iplus;
593
594           case 'p':
595           case 'P':
596             switch( *psz_synchro++ )
597             {
598               case '\0':
599                 return VPAR_SYNCHRO_IP;
600
601               case '+':
602                 if( *psz_synchro ) return 0;
603                 return VPAR_SYNCHRO_IPplus;
604
605               case 'b':
606               case 'B':
607                 if( *psz_synchro ) return 0;
608                 return VPAR_SYNCHRO_IPB;
609
610               default:
611                 return VPAR_SYNCHRO_DEFAULT;
612                 
613             }
614
615           default:
616             return VPAR_SYNCHRO_DEFAULT;
617         }
618     }
619
620     return VPAR_SYNCHRO_DEFAULT;
621 }
622