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