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