]> git.sesse.net Git - vlc/blob - src/video_output/vout_synchro.c
395574f08350e953c73bb815158df3d6e3f88eb0
[vlc] / src / video_output / vout_synchro.c
1 /*****************************************************************************
2  * vout_synchro.c : frame dropping routines
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: vout_synchro.c,v 1.2 2003/05/04 22:33:35 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
103 #include "vout_synchro.h"
104 #include "stream_control.h"
105
106 /*
107  * Local prototypes
108  */
109
110 /* Error margins */
111 #define DELTA                   (int)(0.075*CLOCK_FREQ)
112
113 #define DEFAULT_NB_P            5
114 #define DEFAULT_NB_B            1
115
116 /*****************************************************************************
117  * vout_SynchroInit : You know what ?
118  *****************************************************************************/
119 vout_synchro_t * __vout_SynchroInit( vlc_object_t * p_object,
120                                    vout_thread_t * p_vout, int i_frame_rate )
121 {
122     vout_synchro_t * p_synchro = vlc_object_create( p_object,
123                                                   sizeof(vout_synchro_t) );
124     if ( p_synchro == NULL )
125     {
126         msg_Err( p_object, "out of memory" );
127         return NULL;
128     }
129     vlc_object_attach( p_synchro, p_object );
130
131     /* We use a fake stream pattern, which is often right. */
132     p_synchro->i_n_p = p_synchro->i_eta_p = DEFAULT_NB_P;
133     p_synchro->i_n_b = p_synchro->i_eta_b = DEFAULT_NB_B;
134     memset( p_synchro->p_tau, 0, 4 * sizeof(mtime_t) );
135     memset( p_synchro->pi_meaningful, 0, 4 * sizeof(unsigned int) );
136     p_synchro->i_nb_ref = 0;
137     p_synchro->current_pts = mdate() + DEFAULT_PTS_DELAY;
138     p_synchro->backward_pts = 0;
139     p_synchro->i_current_period = p_synchro->i_backward_period = 0;
140     p_synchro->i_trashed_pic = p_synchro->i_not_chosen_pic = 
141         p_synchro->i_pic = 0;
142
143     p_synchro->i_frame_rate = i_frame_rate;
144     p_synchro->p_vout = p_vout;
145
146     return p_synchro;
147 }
148
149 /*****************************************************************************
150  * vout_SynchroRelease : You know what ?
151  *****************************************************************************/
152 void vout_SynchroRelease( vout_synchro_t * p_synchro )
153 {
154     vlc_object_detach( p_synchro );
155     vlc_object_destroy( p_synchro );
156 }
157
158 /*****************************************************************************
159  * vout_SynchroReset : Reset the reference picture counter
160  *****************************************************************************/
161 void vout_SynchroReset( vout_synchro_t * p_synchro )
162 {
163     p_synchro->i_nb_ref = 0;
164 }
165
166 /*****************************************************************************
167  * vout_SynchroChoose : Decide whether we will decode a picture or not
168  *****************************************************************************/
169 vlc_bool_t vout_SynchroChoose( vout_synchro_t * p_synchro, int i_coding_type )
170 {
171 #define TAU_PRIME( coding_type )    (p_synchro->p_tau[(coding_type)] \
172                                     + (p_synchro->p_tau[(coding_type)] >> 1) \
173                                     + tau_yuv)
174 #define S (*p_synchro)
175     /* VPAR_SYNCHRO_DEFAULT */
176     mtime_t         now, period, tau_yuv;
177     mtime_t         pts = 0;
178     vlc_bool_t      b_decode = 0;
179
180     now = mdate();
181     period = 1000000 * 1001 / p_synchro->i_frame_rate
182                      * p_synchro->i_current_rate / DEFAULT_RATE;
183
184     vlc_mutex_lock( &p_synchro->p_vout->change_lock );
185     tau_yuv = p_synchro->p_vout->render_time;
186     vlc_mutex_unlock( &p_synchro->p_vout->change_lock );
187
188     switch( i_coding_type )
189     {
190     case I_CODING_TYPE:
191         if( S.backward_pts )
192         {
193             pts = S.backward_pts;
194         }
195         else
196         {
197             /* displaying order : B B P B B I
198              *                      ^       ^
199              *                      |       +- current picture
200              *                      +- current PTS
201              */
202             pts = S.current_pts + period * (S.i_n_b + 2);
203         }
204
205         if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
206                 S.p_tau[I_CODING_TYPE] )
207         {
208             b_decode = 1;
209         }
210         else
211         {
212             b_decode = (pts - now) > (TAU_PRIME(I_CODING_TYPE) + DELTA);
213         }
214         if( !b_decode )
215         {
216             msg_Warn( p_synchro,
217                       "synchro trashing I ("I64Fd")", pts - now );
218             p_synchro->i_nb_ref = 0;
219         }
220         else if( p_synchro->i_nb_ref < 2 )
221             p_synchro->i_nb_ref++;
222         break;
223
224     case P_CODING_TYPE:
225         if( S.backward_pts )
226         {
227             pts = S.backward_pts;
228         }
229         else
230         {
231             pts = S.current_pts + period * (S.i_n_b + 1);
232         }
233
234         if( p_synchro->i_nb_ref < 1 )
235         {
236             b_decode = 0;
237         }
238         else if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
239                 S.p_tau[I_CODING_TYPE] )
240         {
241             if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
242             {
243                 /* Security in case we're _really_ late */
244                 b_decode = (pts - now > 0);
245             }
246             else
247             {
248                 b_decode = (pts - now) > (TAU_PRIME(P_CODING_TYPE) + DELTA);
249                 /* next I */
250                 b_decode &= (pts - now
251                               + period
252                           * ( (S.i_n_p - S.i_eta_p) * (1 + S.i_n_b) - 1 ))
253                             > (TAU_PRIME(P_CODING_TYPE)
254                                 + TAU_PRIME(I_CODING_TYPE) + DELTA);
255             }
256         }
257         else
258         {
259             b_decode = 0;
260         }
261         if( b_decode )
262             p_synchro->i_nb_ref = 2;
263         else
264             p_synchro->i_nb_ref = 0;
265         break;
266
267     case B_CODING_TYPE:
268         pts = S.current_pts;
269
270         if( p_synchro->i_nb_ref < 2 )
271         {
272             b_decode = 0;
273         }
274         else if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
275         {
276             b_decode = (pts - now) > (TAU_PRIME(B_CODING_TYPE) + DELTA);
277         }
278         else
279         {
280             b_decode = 0;
281         }
282     }
283
284     if( !b_decode )
285     {
286         S.i_not_chosen_pic++;
287     }
288     return( b_decode );
289 #undef S
290 #undef TAU_PRIME
291 }
292
293 /*****************************************************************************
294  * vout_SynchroTrash : Update counters when we trash a picture
295  *****************************************************************************/
296 void vout_SynchroTrash( vout_synchro_t * p_synchro )
297 {
298     p_synchro->i_trashed_pic++;
299 }
300
301 /*****************************************************************************
302  * vout_SynchroDecode : Update timers when we decide to decode a picture
303  *****************************************************************************/
304 void vout_SynchroDecode( vout_synchro_t * p_synchro )
305 {
306     p_synchro->decoding_start = mdate();
307 }
308
309 /*****************************************************************************
310  * vout_SynchroEnd : Called when the image is totally decoded
311  *****************************************************************************/
312 void vout_SynchroEnd( vout_synchro_t * p_synchro, int i_coding_type,
313                       vlc_bool_t b_garbage )
314 {
315     mtime_t     tau;
316
317     if( !b_garbage )
318     {
319         tau = mdate() - p_synchro->decoding_start;
320
321         /* If duration too high, something happened (pause ?), so don't
322          * take it into account. */
323         if( tau < 3 * p_synchro->p_tau[i_coding_type]
324              || !p_synchro->pi_meaningful[i_coding_type] )
325         {
326             /* Mean with average tau, to ensure stability. */
327             p_synchro->p_tau[i_coding_type] =
328                 (p_synchro->pi_meaningful[i_coding_type]
329                  * p_synchro->p_tau[i_coding_type] + tau)
330                 / (p_synchro->pi_meaningful[i_coding_type] + 1);
331             if( p_synchro->pi_meaningful[i_coding_type] < MAX_PIC_AVERAGE )
332             {
333                 p_synchro->pi_meaningful[i_coding_type]++;
334             }
335         }
336     }
337 }
338
339 /*****************************************************************************
340  * vout_SynchroDate : When an image has been decoded, ask for its date
341  *****************************************************************************/
342 mtime_t vout_SynchroDate( vout_synchro_t * p_synchro )
343 {
344     /* No need to lock, since PTS are only used by the video parser. */
345     return p_synchro->current_pts;
346 }
347
348 /*****************************************************************************
349  * vout_SynchroNewPicture: Update stream structure and PTS
350  *****************************************************************************/
351 void vout_SynchroNewPicture( vout_synchro_t * p_synchro, int i_coding_type,
352                              int i_repeat_field, mtime_t next_pts,
353                              mtime_t next_dts, int i_current_rate )
354 {
355     mtime_t         period = 1000000 * 1001 / p_synchro->i_frame_rate
356                               * i_current_rate / DEFAULT_RATE;
357 #if 0
358     mtime_t         now = mdate(); 
359 #endif
360     p_synchro->i_current_rate = i_current_rate;
361
362     switch( i_coding_type )
363     {
364     case I_CODING_TYPE:
365         if( p_synchro->i_eta_p
366              && p_synchro->i_eta_p != p_synchro->i_n_p )
367         {
368 #if 0
369             msg_Dbg( p_synchro,
370                      "stream periodicity changed from P[%d] to P[%d]",
371                      p_synchro->i_n_p, p_synchro->i_eta_p );
372 #endif
373             p_synchro->i_n_p = p_synchro->i_eta_p;
374         }
375         p_synchro->i_eta_p = p_synchro->i_eta_b = 0;
376
377 #if 0
378         msg_Dbg( p_synchro, "I("I64Fd") P("I64Fd")[%d] B("I64Fd")"
379               "[%d] YUV("I64Fd") : trashed %d:%d/%d",
380               p_synchro->p_tau[I_CODING_TYPE],
381               p_synchro->p_tau[P_CODING_TYPE],
382               p_synchro->i_n_p,
383               p_synchro->p_tau[B_CODING_TYPE],
384               p_synchro->i_n_b,
385               p_synchro->p_vout->render_time,
386               p_synchro->i_not_chosen_pic,
387               p_synchro->i_trashed_pic -
388               p_synchro->i_not_chosen_pic,
389               p_synchro->i_pic );
390         p_synchro->i_trashed_pic = p_synchro->i_not_chosen_pic
391             = p_synchro->i_pic = 0;
392 #else
393         if ( p_synchro->i_pic >= 100 )
394         {
395             msg_Dbg( p_synchro, "decoded %d/%d pictures",
396                      p_synchro->i_pic
397                        - p_synchro->i_trashed_pic,
398                      p_synchro->i_pic );
399             p_synchro->i_trashed_pic = p_synchro->i_not_chosen_pic
400                 = p_synchro->i_pic = 0;
401         }
402 #endif
403         break;
404
405     case P_CODING_TYPE:
406         p_synchro->i_eta_p++;
407         if( p_synchro->i_eta_b
408              && p_synchro->i_eta_b != p_synchro->i_n_b )
409         {
410             msg_Warn( p_synchro,
411                       "stream periodicity changed from B[%d] to B[%d]",
412                       p_synchro->i_n_b, p_synchro->i_eta_b );
413             p_synchro->i_n_b = p_synchro->i_eta_b;
414         }
415         p_synchro->i_eta_b = 0;
416         break;
417     case B_CODING_TYPE:
418         p_synchro->i_eta_b++;
419         break;
420     }
421
422     p_synchro->current_pts += p_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_synchro->i_current_period = i_repeat_field;
432
433         if( next_pts )
434         {
435             if( next_pts - p_synchro->current_pts
436                     > PTS_THRESHOLD
437                  || p_synchro->current_pts - next_pts
438                     > PTS_THRESHOLD )
439             {
440                 msg_Warn( p_synchro, "vout synchro warning: pts != "
441                           "current_date ("I64Fd")",
442                           p_synchro->current_pts
443                               - next_pts );
444             }
445             p_synchro->current_pts = next_pts;
446         }
447     }
448     else
449     {
450         p_synchro->i_current_period = p_synchro->i_backward_period;
451         p_synchro->i_backward_period = i_repeat_field;
452
453         if( p_synchro->backward_pts )
454         {
455             if( next_dts && 
456                 (next_dts - p_synchro->backward_pts
457                     > PTS_THRESHOLD
458               || p_synchro->backward_pts - next_dts
459                     > PTS_THRESHOLD) )
460             {
461                 msg_Warn( p_synchro, "backward_pts != dts ("I64Fd")",
462                            next_dts
463                                - p_synchro->backward_pts );
464             }
465             if( p_synchro->backward_pts - p_synchro->current_pts
466                     > PTS_THRESHOLD
467                  || p_synchro->current_pts - p_synchro->backward_pts
468                     > PTS_THRESHOLD )
469             {
470                 msg_Warn( p_synchro,
471                           "backward_pts != current_pts ("I64Fd")",
472                           p_synchro->current_pts
473                               - p_synchro->backward_pts );
474             }
475             p_synchro->current_pts = p_synchro->backward_pts;
476             p_synchro->backward_pts = 0;
477         }
478         else if( next_dts )
479         {
480             if( next_dts - p_synchro->current_pts
481                     > PTS_THRESHOLD
482                  || p_synchro->current_pts - next_dts
483                     > PTS_THRESHOLD )
484             {
485                 msg_Warn( p_synchro, "dts != current_pts ("I64Fd")",
486                           p_synchro->current_pts
487                               - next_dts );
488             }
489             /* By definition of a DTS. */
490             p_synchro->current_pts = next_dts;
491             next_dts = 0;
492         }
493
494         if( next_pts )
495         {
496             /* Store the PTS for the next time we have to date an I picture. */
497             p_synchro->backward_pts = next_pts;
498             next_pts = 0;
499         }
500     }
501 #undef PTS_THRESHOLD
502
503 #if 0
504     /* Removed for incompatibility with slow motion */
505     if( p_synchro->current_pts + DEFAULT_PTS_DELAY < now )
506     {
507         /* We cannot be _that_ late, something must have happened, reinit
508          * the dates. */
509         msg_Warn( p_synchro, "PTS << now ("I64Fd"), resetting",
510                    now - p_synchro->current_pts - DEFAULT_PTS_DELAY );
511         p_synchro->current_pts = now + DEFAULT_PTS_DELAY;
512     }
513     if( p_synchro->backward_pts
514          && p_synchro->backward_pts + DEFAULT_PTS_DELAY < now )
515     {
516         /* The same. */
517         p_synchro->backward_pts = 0;
518     }
519 #endif
520
521     p_synchro->i_pic++;
522 }