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