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