1 /*****************************************************************************
2 * vpar_synchro.c : frame dropping routines
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_synchro.c,v 1.1 2001/07/16 16:39:00 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.
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 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
63 * 4. Decoding of an I picture
64 * ========================
65 * On fast machines, we decode all I's.
67 * We can decode an I picture if we simply have enough time to decode it
69 * t0 - t > tau´I + DELTA
71 * 5. Decoding of a P picture
72 * =======================
73 * On fast machines, we decode all P's.
75 * First criterion : have time to decode it.
76 * t2 - t > tau´P + DELTA
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
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
89 * I hope you will have a pleasant flight and do not forget your life
91 * --Meuuh (2000-12-29)
94 /*****************************************************************************
96 *****************************************************************************/
99 #include <string.h> /* memcpy(), memset() */
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.h"
115 #include "vdec_motion.h"
117 #include "vpar_blocks.h"
118 #include "vpar_headers.h"
119 #include "vpar_synchro.h"
120 #include "video_parser.h"
127 static int SynchroType( void );
130 #define DELTA (int)(0.040*CLOCK_FREQ)
132 #define DEFAULT_NB_P 5
133 #define DEFAULT_NB_B 1
135 /*****************************************************************************
136 * vpar_SynchroInit : You know what ?
137 *****************************************************************************/
138 void vpar_SynchroInit( vpar_thread_t * p_vpar )
140 p_vpar->synchro.i_type = SynchroType();
141 p_vpar->synchro.i_start = p_vpar->synchro.i_end = 0;
142 vlc_mutex_init( &p_vpar->synchro.fifo_lock );
144 /* We use a fake stream pattern, which is often right. */
145 p_vpar->synchro.i_n_p = p_vpar->synchro.i_eta_p = DEFAULT_NB_P;
146 p_vpar->synchro.i_n_b = p_vpar->synchro.i_eta_b = DEFAULT_NB_B;
147 memset( p_vpar->synchro.p_tau, 0, 4 * sizeof(mtime_t) );
148 memset( p_vpar->synchro.pi_meaningful, 0, 4 * sizeof(unsigned int) );
149 p_vpar->synchro.b_dropped_last = 0;
150 p_vpar->synchro.current_pts = mdate() + DEFAULT_PTS_DELAY;
151 p_vpar->synchro.backward_pts = 0;
152 p_vpar->synchro.i_current_period = p_vpar->synchro.i_backward_period = 0;
154 p_vpar->synchro.i_trashed_pic = p_vpar->synchro.i_not_chosen_pic =
155 p_vpar->synchro.i_pic = 0;
159 /*****************************************************************************
160 * vpar_SynchroChoose : Decide whether we will decode a picture or not
161 *****************************************************************************/
162 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
165 /* For clarity reasons, we separated the special synchros code from the
166 * mathematical synchro */
168 if( p_vpar->synchro.i_type != VPAR_SYNCHRO_DEFAULT )
170 switch( i_coding_type )
173 /* I, IP, IP+, IPB */
174 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus )
176 p_vpar->synchro.b_dropped_last = 1;
181 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_I ) /* I */
186 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus ) /* I+ */
188 if( p_vpar->synchro.b_dropped_last )
190 p_vpar->synchro.b_dropped_last = 0;
199 return( 1 ); /* IP, IP+, IPB */
202 if( p_vpar->synchro.i_type <= VPAR_SYNCHRO_IP ) /* I, IP */
206 else if( p_vpar->synchro.i_type == VPAR_SYNCHRO_IPB ) /* IPB */
211 p_vpar->synchro.b_dropped_last ^= 1; /* IP+ */
212 return( !p_vpar->synchro.b_dropped_last );
214 return( 0 ); /* never reached but gcc yells at me */
218 #define TAU_PRIME( coding_type ) (p_vpar->synchro.p_tau[(coding_type)] \
219 + (p_vpar->synchro.p_tau[(coding_type)] >> 1) \
221 #define S p_vpar->synchro
222 /* VPAR_SYNCHRO_DEFAULT */
223 mtime_t now, period, tau_yuv;
225 boolean_t b_decode = 0;
227 char p_date[MSTRTIME_MAX_SIZE];
231 period = 1000000 * 1001 / p_vpar->sequence.i_frame_rate
232 * p_vpar->sequence.i_current_rate / DEFAULT_RATE;
234 vlc_mutex_lock( &p_vpar->p_vout->change_lock );
235 tau_yuv = p_vpar->p_vout->render_time;
236 vlc_mutex_unlock( &p_vpar->p_vout->change_lock );
238 vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
241 switch( i_coding_type )
246 pts = S.backward_pts;
250 /* displaying order : B B P B B I
252 * | +- current picture
255 pts = S.current_pts + period * (S.i_n_b + 2);
258 if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
259 S.p_tau[I_CODING_TYPE] )
265 b_decode = (pts - now) > (TAU_PRIME(I_CODING_TYPE) + DELTA);
268 intf_WarnMsg( 1, "vpar synchro warning: trashing I (%lld)",
275 pts = S.backward_pts;
279 pts = S.current_pts + period * (S.i_n_b + 1);
282 if( (1 + S.i_n_p * (S.i_n_b + 1)) * period >
283 S.p_tau[I_CODING_TYPE] )
285 if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
287 /* Security in case we're _really_ late */
288 b_decode = (pts - now > 0);
292 b_decode = (pts - now) > (TAU_PRIME(P_CODING_TYPE) + DELTA);
294 b_decode &= (pts - now
296 * ( (S.i_n_p - S.i_eta_p) * (1 + S.i_n_b) - 1 ))
297 > (TAU_PRIME(P_CODING_TYPE)
298 + TAU_PRIME(I_CODING_TYPE) + DELTA);
310 if( (S.i_n_b + 1) * period > S.p_tau[P_CODING_TYPE] )
312 b_decode = (pts - now) > (TAU_PRIME(B_CODING_TYPE) + DELTA);
321 vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
324 intf_DbgMsg("vpar synchro debug: %s picture scheduled for %s, %s (%lld)",
325 i_coding_type == B_CODING_TYPE ? "B" :
326 (i_coding_type == P_CODING_TYPE ? "P" : "I"),
327 mstrtime(p_date, pts), b_decode ? "decoding" : "trashed",
328 S.p_tau[i_coding_type]);
333 S.i_not_chosen_pic++;
342 /*****************************************************************************
343 * vpar_SynchroTrash : Update counters when we trash a picture
344 *****************************************************************************/
345 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
349 p_vpar->synchro.i_trashed_pic++;
353 /*****************************************************************************
354 * vpar_SynchroDecode : Update timers when we decide to decode a picture
355 *****************************************************************************/
356 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
360 vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
363 if( ((p_vpar->synchro.i_end + 1 - p_vpar->synchro.i_start)
364 % MAX_DECODING_PIC) )
366 p_vpar->synchro.p_date_fifo[p_vpar->synchro.i_end] = mdate();
367 p_vpar->synchro.pi_coding_types[p_vpar->synchro.i_end] = i_coding_type;
369 FIFO_INCREMENT( i_end );
373 /* FIFO full, panic() */
374 intf_ErrMsg("vpar error: synchro fifo full, estimations will be biased (%d:%d)",
375 p_vpar->synchro.i_start, p_vpar->synchro.i_end);
378 vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
382 /*****************************************************************************
383 * vpar_SynchroEnd : Called when the image is totally decoded
384 *****************************************************************************/
385 void vpar_SynchroEnd( vpar_thread_t * p_vpar, int i_garbage )
391 vlc_mutex_lock( &p_vpar->synchro.fifo_lock );
394 i_coding_type = p_vpar->synchro.pi_coding_types[p_vpar->synchro.i_start];
398 tau = mdate() - p_vpar->synchro.p_date_fifo[p_vpar->synchro.i_start];
400 /* If duration too high, something happened (pause ?), so don't
401 * take it into account. */
402 if( tau < 3 * p_vpar->synchro.p_tau[i_coding_type]
403 || !p_vpar->synchro.pi_meaningful[i_coding_type] )
405 /* Mean with average tau, to ensure stability. */
406 p_vpar->synchro.p_tau[i_coding_type] =
407 (p_vpar->synchro.pi_meaningful[i_coding_type]
408 * p_vpar->synchro.p_tau[i_coding_type] + tau)
409 / (p_vpar->synchro.pi_meaningful[i_coding_type] + 1);
410 if( p_vpar->synchro.pi_meaningful[i_coding_type] < MAX_PIC_AVERAGE )
412 p_vpar->synchro.pi_meaningful[i_coding_type]++;
417 intf_DbgMsg("vpar synchro debug: finished decoding %s (%lld)",
418 i_coding_type == B_CODING_TYPE ? "B" :
419 (i_coding_type == P_CODING_TYPE ? "P" : "I"), tau);
424 intf_DbgMsg("vpar synchro debug: aborting %s",
425 i_coding_type == B_CODING_TYPE ? "B" :
426 (i_coding_type == P_CODING_TYPE ? "P" : "I"));
429 FIFO_INCREMENT( i_start );
432 vlc_mutex_unlock( &p_vpar->synchro.fifo_lock );
436 /*****************************************************************************
437 * vpar_SynchroDate : When an image has been decoded, ask for its date
438 *****************************************************************************/
439 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
441 /* No need to lock, since PTS are only used by the video parser. */
442 return( p_vpar->synchro.current_pts );
445 /*****************************************************************************
446 * vpar_SynchroNewPicture: Update stream structure and PTS
447 *****************************************************************************/
448 void vpar_SynchroNewPicture( vpar_thread_t * p_vpar, int i_coding_type,
451 mtime_t period = 1000000 * 1001 / p_vpar->sequence.i_frame_rate
452 * p_vpar->sequence.i_current_rate / DEFAULT_RATE;
454 mtime_t now = mdate();
457 switch( i_coding_type )
460 if( p_vpar->synchro.i_eta_p
461 && p_vpar->synchro.i_eta_p != p_vpar->synchro.i_n_p )
463 intf_WarnMsg( 3, "vpar info: stream periodicity changed "
464 "from P[%d] to P[%d]",
465 p_vpar->synchro.i_n_p, p_vpar->synchro.i_eta_p );
466 p_vpar->synchro.i_n_p = p_vpar->synchro.i_eta_p;
468 p_vpar->synchro.i_eta_p = p_vpar->synchro.i_eta_b = 0;
470 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_DEFAULT )
472 intf_Msg( "vpar synchro stats: I(%lld) P(%lld)[%d] B(%lld)[%d] YUV(%lld) : trashed %d:%d/%d",
473 p_vpar->synchro.p_tau[I_CODING_TYPE],
474 p_vpar->synchro.p_tau[P_CODING_TYPE],
475 p_vpar->synchro.i_n_p,
476 p_vpar->synchro.p_tau[B_CODING_TYPE],
477 p_vpar->synchro.i_n_b,
478 p_vpar->p_vout->render_time,
479 p_vpar->synchro.i_not_chosen_pic,
480 p_vpar->synchro.i_trashed_pic -
481 p_vpar->synchro.i_not_chosen_pic,
482 p_vpar->synchro.i_pic );
483 p_vpar->synchro.i_trashed_pic = p_vpar->synchro.i_not_chosen_pic
484 = p_vpar->synchro.i_pic = 0;
489 p_vpar->synchro.i_eta_p++;
490 if( p_vpar->synchro.i_eta_b
491 && p_vpar->synchro.i_eta_b != p_vpar->synchro.i_n_b )
493 intf_WarnMsg( 3, "vpar info: stream periodicity changed "
494 "from B[%d] to B[%d]",
495 p_vpar->synchro.i_n_b, p_vpar->synchro.i_eta_b );
496 p_vpar->synchro.i_n_b = p_vpar->synchro.i_eta_b;
498 p_vpar->synchro.i_eta_b = 0;
501 p_vpar->synchro.i_eta_b++;
505 p_vpar->synchro.current_pts += p_vpar->synchro.i_current_period
508 #define PTS_THRESHOLD (period >> 2)
509 if( i_coding_type == B_CODING_TYPE )
511 /* A video frame can be displayed 1, 2 or 3 times, according to
512 * repeat_first_field, top_field_first, progressive_sequence and
513 * progressive_frame. */
514 p_vpar->synchro.i_current_period = i_repeat_field;
516 if( p_vpar->sequence.next_pts )
518 if( p_vpar->sequence.next_pts - p_vpar->synchro.current_pts
520 || p_vpar->synchro.current_pts - p_vpar->sequence.next_pts
524 "vpar synchro warning: pts != current_date (%lld)",
525 p_vpar->synchro.current_pts
526 - p_vpar->sequence.next_pts );
528 p_vpar->synchro.current_pts = p_vpar->sequence.next_pts;
529 p_vpar->sequence.next_pts = 0;
534 p_vpar->synchro.i_current_period = p_vpar->synchro.i_backward_period;
535 p_vpar->synchro.i_backward_period = i_repeat_field;
537 if( p_vpar->synchro.backward_pts )
539 if( p_vpar->sequence.next_dts &&
540 (p_vpar->sequence.next_dts - p_vpar->synchro.backward_pts
542 || p_vpar->synchro.backward_pts - p_vpar->sequence.next_dts
546 "vpar synchro warning: backward_pts != dts (%lld)",
547 p_vpar->sequence.next_dts
548 - p_vpar->synchro.backward_pts );
550 if( p_vpar->synchro.backward_pts - p_vpar->synchro.current_pts
552 || p_vpar->synchro.current_pts - p_vpar->synchro.backward_pts
556 "vpar synchro warning: backward_pts != current_pts (%lld)",
557 p_vpar->synchro.current_pts - p_vpar->synchro.backward_pts );
559 p_vpar->synchro.current_pts = p_vpar->synchro.backward_pts;
560 p_vpar->synchro.backward_pts = 0;
562 else if( p_vpar->sequence.next_dts )
564 if( p_vpar->sequence.next_dts - p_vpar->synchro.current_pts
566 || p_vpar->synchro.current_pts - p_vpar->sequence.next_dts
570 "vpar synchro warning: dts != current_pts (%lld)",
571 p_vpar->synchro.current_pts
572 - p_vpar->sequence.next_dts );
574 /* By definition of a DTS. */
575 p_vpar->synchro.current_pts = p_vpar->sequence.next_dts;
576 p_vpar->sequence.next_dts = 0;
579 if( p_vpar->sequence.next_pts )
581 /* Store the PTS for the next time we have to date an I picture. */
582 p_vpar->synchro.backward_pts = p_vpar->sequence.next_pts;
583 p_vpar->sequence.next_pts = 0;
589 /* Removed for incompatibility with slow motion */
590 if( p_vpar->synchro.current_pts + DEFAULT_PTS_DELAY < now )
592 /* We cannot be _that_ late, something must have happened, reinit
594 intf_WarnMsg( 2, "PTS << now (%lld), resetting",
595 now - p_vpar->synchro.current_pts - DEFAULT_PTS_DELAY );
596 p_vpar->synchro.current_pts = now + DEFAULT_PTS_DELAY;
598 if( p_vpar->synchro.backward_pts
599 && p_vpar->synchro.backward_pts + DEFAULT_PTS_DELAY < now )
602 p_vpar->synchro.backward_pts = 0;
607 p_vpar->synchro.i_pic++;
611 /*****************************************************************************
612 * SynchroType: Get the user's synchro type
613 *****************************************************************************
614 * This function is called at initialization.
615 *****************************************************************************/
616 static int SynchroType( void )
618 char * psz_synchro = main_GetPszVariable( VPAR_SYNCHRO_VAR, NULL );
620 if( psz_synchro == NULL )
622 return VPAR_SYNCHRO_DEFAULT;
625 switch( *psz_synchro++ )
629 switch( *psz_synchro++ )
632 return VPAR_SYNCHRO_I;
635 if( *psz_synchro ) return 0;
636 return VPAR_SYNCHRO_Iplus;
640 switch( *psz_synchro++ )
643 return VPAR_SYNCHRO_IP;
646 if( *psz_synchro ) return 0;
647 return VPAR_SYNCHRO_IPplus;
651 if( *psz_synchro ) return 0;
652 return VPAR_SYNCHRO_IPB;
655 return VPAR_SYNCHRO_DEFAULT;
660 return VPAR_SYNCHRO_DEFAULT;
664 return VPAR_SYNCHRO_DEFAULT;