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