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