1 /*****************************************************************************
2 * vpar_synchro.c : frame dropping routines
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_synchro.c,v 1.68 2000/12/29 12:49:30 massiot 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.
44 * FIXME: write a few words about stream structure changes.
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.
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
65 * 4. Decoding of an I picture
66 * ========================
67 * On fast machines, we decode all I's.
69 * We can decode an I picture if we simply have enough time to decode it
71 * t0 - t > tau´I + DELTA
73 * 5. Decoding of a P picture
74 * =======================
75 * On fast machines, we decode all P's.
77 * First criterion : have time to decode it.
78 * t2 - t > tau´P + DELTA
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
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
91 * I hope you will have a pleasant flight and do not forget your life
93 * --Meuuh (2000-12-29)
96 /*****************************************************************************
98 *****************************************************************************/
107 #include "intf_msg.h"
109 #include "stream_control.h"
110 #include "input_ext-dec.h"
113 #include "video_output.h"
115 #include "../video_decoder/vdec_idct.h"
116 #include "../video_decoder/video_decoder.h"
117 #include "../video_decoder/vdec_motion.h"
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"
129 static int SynchroType( void );
132 #define DELTA (int)(0.040*CLOCK_FREQ)
133 #define PTS_THRESHOLD (int)(0.030*CLOCK_FREQ)
135 #define DEFAULT_NB_P 5
136 #define DEFAULT_NB_B 1
138 /*****************************************************************************
139 * vpar_SynchroInit : You know what ?
140 *****************************************************************************/
141 void vpar_SynchroInit( vpar_thread_t * p_vpar )
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 );
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;
157 /*****************************************************************************
158 * vpar_SynchroChoose : Decide whether we will decode a picture or not
159 *****************************************************************************/
160 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
163 /* For clarity reasons, we separated the special synchros code from the
164 * mathematical synchro */
166 if( p_vpar->synchro.i_type != VPAR_SYNCHRO_DEFAULT )
168 switch( i_coding_type )
171 /* I, IP, IP+, IPB */
172 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus )
174 p_vpar->synchro.b_dropped_last = 1;
179 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_I ) /* I */
184 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus ) /* I+ */
186 if( p_vpar->synchro.b_dropped_last )
188 p_vpar->synchro.b_dropped_last = 0;
197 return( 1 ); /* IP, IP+, IPB */
200 if( p_vpar->synchro.i_type <= VPAR_SYNCHRO_IP ) /* I, IP */
204 else if( p_vpar->synchro.i_type == VPAR_SYNCHRO_IPB ) /* IPB */
209 p_vpar->synchro.b_dropped_last ^= 1; /* IP+ */
210 return( !p_vpar->synchro.b_dropped_last );
212 return( 0 ); /* never reached but gcc yells at me */
216 #define TAU_PRIME( coding_type ) (p_vpar->synchro.p_tau[(coding_type)] \
217 + (p_vpar->synchro.p_tau[(coding_type)] >> 1) \
219 #define S p_vpar->synchro
220 /* VPAR_SYNCHRO_DEFAULT */
221 mtime_t now, pts, period, tau_yuv;
222 boolean_t b_decode = 0;
224 char p_date[MSTRTIME_MAX_SIZE];
228 period = 1000000 / (p_vpar->sequence.i_frame_rate) * 1001;
230 vlc_mutex_lock( &p_vpar->p_vout->change_lock );
231 tau_yuv = p_vpar->p_vout->render_time;
232 vlc_mutex_unlock( &p_vpar->p_vout->change_lock );
234 vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
236 switch( i_coding_type )
241 pts = S.backward_pts;
245 /* displaying order : B B P B B I
247 * | +- current picture
250 pts = S.current_pts + period * (S.i_n_b + 2);
253 if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
254 S.p_tau[I_CODING_TYPE] )
260 b_decode = (pts - now) > (TAU_PRIME(I_CODING_TYPE) + DELTA);
263 intf_WarnMsg( 3, "vpar synchro warning: trashing I" );
269 pts = S.backward_pts;
273 pts = S.current_pts + period * (S.i_n_b + 1);
276 if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
277 S.p_tau[I_CODING_TYPE] )
279 if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
281 /* Security in case we're _really_ late */
282 b_decode = (pts - now > 0);
286 b_decode = (pts - now) > (TAU_PRIME(P_CODING_TYPE) + DELTA);
288 b_decode &= (pts - now
290 * ( (S.i_n_p - S.i_eta_p) * (1 + S.i_n_b) - 1 ))
291 > (TAU_PRIME(P_CODING_TYPE)
292 + TAU_PRIME(I_CODING_TYPE) + DELTA);
304 if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
306 b_decode = (pts - now) > (TAU_PRIME(B_CODING_TYPE) + DELTA);
314 vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
316 intf_DbgMsg("vpar synchro debug: %s picture scheduled for %s, %s (%lld)",
317 i_coding_type == B_CODING_TYPE ? "B" :
318 (i_coding_type == P_CODING_TYPE ? "P" : "I"),
319 mstrtime(p_date, pts), b_decode ? "decoding" : "trashed",
320 S.p_tau[i_coding_type]);
325 S.i_not_chosen_pic++;
334 /*****************************************************************************
335 * vpar_SynchroTrash : Update counters when we trash a picture
336 *****************************************************************************/
337 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
341 p_vpar->synchro.i_trashed_pic++;
345 /*****************************************************************************
346 * vpar_SynchroDecode : Update timers when we decide to decode a picture
347 *****************************************************************************/
348 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
351 vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
353 if( ((p_vpar->synchro.i_end + 1 - p_vpar->synchro.i_start)
354 % MAX_DECODING_PIC) )
356 p_vpar->synchro.p_date_fifo[p_vpar->synchro.i_end] = mdate();
357 p_vpar->synchro.pi_coding_types[p_vpar->synchro.i_end] = i_coding_type;
359 FIFO_INCREMENT( i_end );
363 /* FIFO full, panic() */
364 intf_ErrMsg("vpar error: synchro fifo full, estimations will be biased");
366 vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
369 /*****************************************************************************
370 * vpar_SynchroEnd : Called when the image is totally decoded
371 *****************************************************************************/
372 void vpar_SynchroEnd( vpar_thread_t * p_vpar, int i_garbage )
377 vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
381 tau = mdate() - p_vpar->synchro.p_date_fifo[p_vpar->synchro.i_start];
382 i_coding_type = p_vpar->synchro.pi_coding_types[p_vpar->synchro.i_start];
384 /* Mean with average tau, to ensure stability. */
385 p_vpar->synchro.p_tau[i_coding_type] =
386 (p_vpar->synchro.pi_meaningful[i_coding_type]
387 * p_vpar->synchro.p_tau[i_coding_type] + tau)
388 / (p_vpar->synchro.pi_meaningful[i_coding_type] + 1);
389 if( p_vpar->synchro.pi_meaningful[i_coding_type] < MAX_PIC_AVERAGE )
391 p_vpar->synchro.pi_meaningful[i_coding_type]++;
394 intf_DbgMsg("vpar synchro debug: finished decoding %s (%lld)",
395 i_coding_type == B_CODING_TYPE ? "B" :
396 (i_coding_type == P_CODING_TYPE ? "P" : "I"), tau);
400 FIFO_INCREMENT( i_start );
402 vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
405 /*****************************************************************************
406 * vpar_SynchroDate : When an image has been decoded, ask for its date
407 *****************************************************************************/
408 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
410 /* No need to lock, since PTS are only used by the video parser. */
411 return( p_vpar->synchro.current_pts );
414 /*****************************************************************************
415 * vpar_SynchroNewPicture: Update stream structure and PTS
416 *****************************************************************************/
417 void vpar_SynchroNewPicture( vpar_thread_t * p_vpar, int i_coding_type,
418 boolean_t b_repeat_field )
420 pes_packet_t * p_pes;
421 mtime_t period = 1000000 / (p_vpar->sequence.i_frame_rate) * 1001;
423 switch( i_coding_type )
426 if( p_vpar->synchro.i_eta_p
427 && p_vpar->synchro.i_eta_p != p_vpar->synchro.i_n_p )
429 intf_WarnMsg( 1, "Stream periodicity changed from P[%d] to P[%d]",
430 p_vpar->synchro.i_n_p, p_vpar->synchro.i_eta_p );
431 p_vpar->synchro.i_n_p = p_vpar->synchro.i_eta_p;
433 p_vpar->synchro.i_eta_p = p_vpar->synchro.i_eta_b = 0;
435 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_DEFAULT )
437 intf_Msg( "vpar synchro stats: I(%lld) P(%lld)[%d] B(%lld)[%d] YUV(%lld) : trashed %d:%d/%d",
438 p_vpar->synchro.p_tau[I_CODING_TYPE],
439 p_vpar->synchro.p_tau[P_CODING_TYPE],
440 p_vpar->synchro.i_n_p,
441 p_vpar->synchro.p_tau[B_CODING_TYPE],
442 p_vpar->synchro.i_n_b,
443 p_vpar->p_vout->render_time,
444 p_vpar->synchro.i_not_chosen_pic,
445 p_vpar->synchro.i_trashed_pic -
446 p_vpar->synchro.i_not_chosen_pic,
447 p_vpar->synchro.i_pic );
448 p_vpar->synchro.i_trashed_pic = p_vpar->synchro.i_not_chosen_pic
449 = p_vpar->synchro.i_pic = 0;
454 p_vpar->synchro.i_eta_p++;
455 if( p_vpar->synchro.i_eta_b
456 && p_vpar->synchro.i_eta_b != p_vpar->synchro.i_n_b )
458 intf_WarnMsg( 1, "Stream periodicity changed from B[%d] to B[%d]",
459 p_vpar->synchro.i_n_b, p_vpar->synchro.i_eta_b );
460 p_vpar->synchro.i_n_b = p_vpar->synchro.i_eta_b;
462 p_vpar->synchro.i_eta_b = 0;
465 p_vpar->synchro.i_eta_b++;
469 /* FIXME: use decoder_fifo callback */
470 p_pes = DECODER_FIFO_START( *p_vpar->bit_stream.p_decoder_fifo );
474 /* MPEG-2 repeat_first_field */
475 /* FIXME : this is not exactly what we should do, repeat_first_field
476 * only regards the next picture */
477 p_vpar->synchro.current_pts += period + (period >> 1);
481 p_vpar->synchro.current_pts += period;
484 if( i_coding_type == B_CODING_TYPE )
488 if( p_pes->i_pts - p_vpar->synchro.current_pts > PTS_THRESHOLD
489 || p_vpar->synchro.current_pts - p_pes->i_pts > PTS_THRESHOLD )
492 "vpar synchro warning: pts != current_date (%lld)",
493 p_vpar->synchro.current_pts - p_pes->i_pts );
495 p_vpar->synchro.current_pts = p_pes->i_pts;
501 if( p_vpar->synchro.backward_pts )
504 (p_pes->i_dts - p_vpar->synchro.backward_pts > PTS_THRESHOLD
505 || p_vpar->synchro.backward_pts - p_pes->i_dts > PTS_THRESHOLD) )
508 "vpar synchro warning: backward_pts != dts (%lld)",
509 p_vpar->synchro.backward_pts - p_pes->i_dts );
512 if( p_vpar->synchro.backward_pts - p_vpar->synchro.current_pts
514 || p_vpar->synchro.current_pts - p_vpar->synchro.backward_pts
518 "vpar synchro warning: backward_pts != current_pts (%lld)",
519 p_vpar->synchro.current_pts - p_vpar->synchro.backward_pts );
521 p_vpar->synchro.current_pts = p_vpar->synchro.backward_pts;
522 p_vpar->synchro.backward_pts = 0;
524 else if( p_pes->i_dts )
526 if( p_pes->i_dts - p_vpar->synchro.current_pts > PTS_THRESHOLD
527 || p_vpar->synchro.current_pts - p_pes->i_dts > PTS_THRESHOLD )
530 "vpar synchro warning: dts != current_pts (%lld)",
531 p_vpar->synchro.current_pts - p_pes->i_dts );
533 /* By definition of a DTS. */
534 p_vpar->synchro.current_pts = p_pes->i_dts;
544 /* Store the PTS for the next time we have to date an I picture. */
545 p_vpar->synchro.backward_pts = p_pes->i_pts;
547 /* FIXME : disabled because it conflicts with streams having
548 * b_repeat_first_field */
550 i_n_b = (p_vpar->synchro.backward_pts
551 - p_vpar->synchro.current_pts) / period - 1;
552 if( i_n_b != p_vpar->synchro.i_n_b )
555 "Anticipating a stream periodicity change from"
557 p_vpar->synchro.i_n_b, i_n_b );
558 p_vpar->synchro.i_n_b = i_n_b;
565 p_vpar->synchro.i_pic++;
569 /*****************************************************************************
570 * SynchroType: Get the user's synchro type
571 *****************************************************************************
572 * This function is called at initialization.
573 *****************************************************************************/
574 static int SynchroType( void )
576 char * psz_synchro = main_GetPszVariable( VPAR_SYNCHRO_VAR, NULL );
578 if( psz_synchro == NULL )
580 return VPAR_SYNCHRO_DEFAULT;
583 switch( *psz_synchro++ )
587 switch( *psz_synchro++ )
590 return VPAR_SYNCHRO_I;
593 if( *psz_synchro ) return 0;
594 return VPAR_SYNCHRO_Iplus;
598 switch( *psz_synchro++ )
601 return VPAR_SYNCHRO_IP;
604 if( *psz_synchro ) return 0;
605 return VPAR_SYNCHRO_IPplus;
609 if( *psz_synchro ) return 0;
610 return VPAR_SYNCHRO_IPB;
613 return VPAR_SYNCHRO_DEFAULT;
618 return VPAR_SYNCHRO_DEFAULT;
622 return VPAR_SYNCHRO_DEFAULT;