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