1 /*****************************************************************************
2 * vpar_synchro.c : frame dropping routines
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_synchro.c,v 1.63 2000/12/22 13:04:45 sam Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Samuel Hocevar <sam@via.ecp.fr>
9 * Jean-Marc Dressler <polux@via.ecp.fr>
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.
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.
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 *****************************************************************************/
27 * DISCUSSION : How to Write an efficient Frame-Dropping Algorithm
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.
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.
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.
56 * 3. General considerations
57 * ======================
58 * We define to types of machines :
59 * 2T > tauP : machines capable of decoding all P pictures
60 * 14T > tauI : machines capable of decoding all I pictures
62 * 4. Decoding of an I picture
63 * ========================
64 * On fast machines, we decode all I's.
66 * We can decode an I picture if we simply have enough time to decode it
68 * t0 - t > tau´I + DELTA
70 * 4. Decoding of a P picture
71 * =======================
72 * On fast machines, we decode all P's.
74 * First criterion : have time to decode it.
75 * t2 - t > tau´P + DELTA
77 * Second criterion : it shouldn't prevent us from displaying the forthcoming
78 * I picture, which is more important.
79 * t12 - t > tau´P + tau´I + DELTA
81 * 5. Decoding of a B picture
82 * =======================
83 * First criterion : have time to decode it.
84 * t1 - t > tau´B + DELTA
86 * Second criterion : it shouldn't prevent us from displaying the forthcoming
87 * P picture, which is more important.
88 * t4 - t > tau´B + tau´P + DELTA
90 * I hope you will have a pleasant flight and do not forget your life
92 * --Meuuh (2000-11-09)
95 /*****************************************************************************
97 *****************************************************************************/
106 #include "intf_msg.h"
108 #include "stream_control.h"
109 #include "input_ext-dec.h"
112 #include "video_output.h"
114 #include "../video_decoder/vdec_idct.h"
115 #include "../video_decoder/video_decoder.h"
116 #include "../video_decoder/vdec_motion.h"
118 #include "../video_decoder/vpar_blocks.h"
119 #include "../video_decoder/vpar_headers.h"
120 #include "../video_decoder/vpar_synchro.h"
121 #include "../video_decoder/video_parser.h"
128 static int SynchroType( void );
129 static void SynchroNewPicture( vpar_thread_t * p_vpar, int i_coding_type );
132 #define DELTA (int)(0.040*CLOCK_FREQ)
134 #define DEFAULT_NB_P 5
135 #define DEFAULT_NB_B 1
137 /*****************************************************************************
138 * vpar_SynchroInit : You know what ?
139 *****************************************************************************/
140 void vpar_SynchroInit( vpar_thread_t * p_vpar )
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 );
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;
156 /*****************************************************************************
157 * vpar_SynchroChoose : Decide whether we will decode a picture or not
158 *****************************************************************************/
159 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
162 /* For clarity reasons, we separated the special synchros code from the
163 * mathematical synchro */
165 if( p_vpar->synchro.i_type != VPAR_SYNCHRO_DEFAULT )
167 switch( i_coding_type )
170 /* I, IP, IP+, IPB */
171 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus )
173 p_vpar->synchro.b_dropped_last = 1;
178 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_I ) /* I */
183 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus ) /* I+ */
185 if( p_vpar->synchro.b_dropped_last )
187 p_vpar->synchro.b_dropped_last = 0;
196 return( 1 ); /* IP, IP+, IPB */
199 if( p_vpar->synchro.i_type <= VPAR_SYNCHRO_IP ) /* I, IP */
203 else if( p_vpar->synchro.i_type == VPAR_SYNCHRO_IPB ) /* IPB */
208 p_vpar->synchro.b_dropped_last ^= 1; /* IP+ */
209 return( !p_vpar->synchro.b_dropped_last );
211 return( 0 ); /* never reached but gcc yells at me */
215 #define TAU_PRIME( coding_type ) (p_vpar->synchro.p_tau[(coding_type)] \
216 + (p_vpar->synchro.p_tau[(coding_type)] >> 1) \
218 #define S p_vpar->synchro
219 /* VPAR_SYNCHRO_DEFAULT */
220 mtime_t now, pts, period, tau_yuv;
221 boolean_t b_decode = 0;
223 char p_date[MSTRTIME_MAX_SIZE];
227 period = 1000000 / (p_vpar->sequence.i_frame_rate) * 1001;
229 vlc_mutex_lock( &p_vpar->p_vout->change_lock );
230 tau_yuv = p_vpar->p_vout->render_time;
231 vlc_mutex_unlock( &p_vpar->p_vout->change_lock );
233 vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
235 switch( i_coding_type )
238 /* Stream structure changes */
244 pts = S.backward_pts;
248 /* displaying order : B B P B B I
250 * | +- current picture
253 pts = S.current_pts + period * (S.i_n_b + 2);
256 if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
257 S.p_tau[I_CODING_TYPE] )
263 b_decode = (pts - now) > (TAU_PRIME(I_CODING_TYPE) + DELTA);
266 intf_WarnMsg( 3, "vpar synchro warning: trashing I" );
270 /* Stream structure changes */
273 if( S.i_eta_p + 1 > S.i_n_p )
278 pts = S.backward_pts;
282 pts = S.current_pts + period * (S.i_n_b + 2);
285 if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
286 S.p_tau[I_CODING_TYPE] )
288 if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
290 /* Security in case we're _really_ late */
291 b_decode = (pts - now > 0);
295 b_decode = (pts - now) > (TAU_PRIME(P_CODING_TYPE) + DELTA);
297 b_decode &= (pts - now
299 * ( (S.i_n_p - S.i_eta_p - 1) * (1 + S.i_n_b) - 1 ))
300 > (TAU_PRIME(P_CODING_TYPE)
301 + TAU_PRIME(I_CODING_TYPE) + DELTA);
311 /* Stream structure changes */
312 if( S.i_eta_b + 1 > S.i_n_b )
315 pts = S.current_pts + period;
317 if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
319 b_decode = (pts - now) > (TAU_PRIME(B_CODING_TYPE) + DELTA);
321 /* Remember that S.i_eta_b is for the moment only eta_b - 1. */
322 b_decode &= (pts - now
324 * ( 2 * S.i_n_b - S.i_eta_b + 2))
325 > (TAU_PRIME(B_CODING_TYPE)
326 + TAU_PRIME(P_CODING_TYPE) + DELTA);
334 vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
336 intf_DbgMsg("vpar synchro debug: %s picture scheduled for %s, %s (%lld)",
337 i_coding_type == B_CODING_TYPE ? "B" :
338 (i_coding_type == P_CODING_TYPE ? "P" : "I"),
339 mstrtime(p_date, pts), b_decode ? "decoding" : "trashed",
340 S.p_tau[i_coding_type]);
348 /*****************************************************************************
349 * vpar_SynchroTrash : Update timers when we trash a picture
350 *****************************************************************************/
351 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
354 SynchroNewPicture( p_vpar, i_coding_type );
356 p_vpar->synchro.i_trashed_pic++;
360 /*****************************************************************************
361 * vpar_SynchroDecode : Update timers when we decide to decode a picture
362 *****************************************************************************/
363 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
366 vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
368 if( ((p_vpar->synchro.i_end + 1 - p_vpar->synchro.i_start)
369 % MAX_DECODING_PIC) )
371 p_vpar->synchro.p_date_fifo[p_vpar->synchro.i_end] = mdate();
372 p_vpar->synchro.pi_coding_types[p_vpar->synchro.i_end] = i_coding_type;
374 FIFO_INCREMENT( i_end );
378 /* FIFO full, panic() */
379 intf_ErrMsg("vpar error: synchro fifo full, estimations will be biased");
381 vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
383 SynchroNewPicture( p_vpar, i_coding_type );
386 /*****************************************************************************
387 * vpar_SynchroEnd : Called when the image is totally decoded
388 *****************************************************************************/
389 void vpar_SynchroEnd( vpar_thread_t * p_vpar, int i_garbage )
394 vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
398 tau = mdate() - p_vpar->synchro.p_date_fifo[p_vpar->synchro.i_start];
399 i_coding_type = p_vpar->synchro.pi_coding_types[p_vpar->synchro.i_start];
401 /* Mean with average tau, to ensure stability. */
402 p_vpar->synchro.p_tau[i_coding_type] =
403 (p_vpar->synchro.pi_meaningful[i_coding_type]
404 * p_vpar->synchro.p_tau[i_coding_type] + tau)
405 / (p_vpar->synchro.pi_meaningful[i_coding_type] + 1);
406 if( p_vpar->synchro.pi_meaningful[i_coding_type] < MAX_PIC_AVERAGE )
408 p_vpar->synchro.pi_meaningful[i_coding_type]++;
411 intf_DbgMsg("vpar synchro debug: finished decoding %s (%lld)",
412 i_coding_type == B_CODING_TYPE ? "B" :
413 (i_coding_type == P_CODING_TYPE ? "P" : "I"), tau);
417 FIFO_INCREMENT( i_start );
419 vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
422 /*****************************************************************************
423 * vpar_SynchroDate : When an image has been decoded, ask for its date
424 *****************************************************************************/
425 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
427 /* No need to lock, since PTS are only used by the video parser. */
428 return( p_vpar->synchro.current_pts );
431 /*****************************************************************************
432 * SynchroType: Get the user's synchro type
433 *****************************************************************************
434 * This function is called at initialization.
435 *****************************************************************************/
436 static int SynchroType( void )
438 char * psz_synchro = main_GetPszVariable( VPAR_SYNCHRO_VAR, NULL );
440 if( psz_synchro == NULL )
442 return VPAR_SYNCHRO_DEFAULT;
445 switch( *psz_synchro++ )
449 switch( *psz_synchro++ )
452 return VPAR_SYNCHRO_I;
455 if( *psz_synchro ) return 0;
456 return VPAR_SYNCHRO_Iplus;
460 switch( *psz_synchro++ )
463 return VPAR_SYNCHRO_IP;
466 if( *psz_synchro ) return 0;
467 return VPAR_SYNCHRO_IPplus;
471 if( *psz_synchro ) return 0;
472 return VPAR_SYNCHRO_IPB;
475 return VPAR_SYNCHRO_DEFAULT;
480 return VPAR_SYNCHRO_DEFAULT;
484 return VPAR_SYNCHRO_DEFAULT;
487 /*****************************************************************************
488 * SynchroNewPicture: Update stream structure and PTS
489 *****************************************************************************/
490 static void SynchroNewPicture( vpar_thread_t * p_vpar, int i_coding_type )
492 pes_packet_t * p_pes;
494 switch( i_coding_type )
497 p_vpar->synchro.i_eta_p = p_vpar->synchro.i_eta_b = 0;
499 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_DEFAULT )
501 intf_Msg( "vpar synchro stats: I(%lld) P(%lld)[%d] B(%lld)[%d] YUV(%lld) : %d/%d",
502 p_vpar->synchro.p_tau[I_CODING_TYPE],
503 p_vpar->synchro.p_tau[P_CODING_TYPE],
504 p_vpar->synchro.i_n_p,
505 p_vpar->synchro.p_tau[B_CODING_TYPE],
506 p_vpar->synchro.i_n_b,
507 p_vpar->p_vout->render_time,
508 1 + p_vpar->synchro.i_n_p * (1 + p_vpar->synchro.i_n_b) -
509 p_vpar->synchro.i_trashed_pic,
510 1 + p_vpar->synchro.i_n_p * (1 + p_vpar->synchro.i_n_b) );
511 p_vpar->synchro.i_trashed_pic = 0;
516 p_vpar->synchro.i_eta_b = 0;
517 p_vpar->synchro.i_eta_p++;
520 p_vpar->synchro.i_eta_b++;
524 p_pes = DECODER_FIFO_START( *p_vpar->bit_stream.p_decoder_fifo );
526 if( i_coding_type == B_CODING_TYPE )
528 if( p_pes->b_has_pts )
530 if( p_pes->i_pts < p_vpar->synchro.current_pts )
533 "vpar synchro warning: pts_date < current_date" );
535 p_vpar->synchro.current_pts = p_pes->i_pts;
536 p_pes->b_has_pts = 0;
540 p_vpar->synchro.current_pts += 1000000 / (p_vpar->sequence.i_frame_rate) * 1001;
545 if( p_vpar->synchro.backward_pts == 0 )
547 p_vpar->synchro.current_pts += 1000000 / (p_vpar->sequence.i_frame_rate) * 1001;
551 if( p_vpar->synchro.backward_pts < p_vpar->synchro.current_pts )
554 "vpar warning: backward_date < current_date" );
556 p_vpar->synchro.current_pts = p_vpar->synchro.backward_pts;
557 p_vpar->synchro.backward_pts = 0;
560 if( p_pes->b_has_pts )
562 /* Store the PTS for the next time we have to date an I picture. */
563 p_vpar->synchro.backward_pts = p_pes->i_pts;
564 p_pes->b_has_pts = 0;