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