1 /*****************************************************************************
2 * vpar_synchro.c : frame dropping routines
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
6 * Authors: Samuel Hocevar <sam@via.ecp.fr>
7 * Jean-Marc Dressler <polu@via.ecp.fr>
8 * Christophe Massiot <massiot@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <stdlib.h> /* free() */
31 #include <sys/types.h> /* on BSD, uio.h needs types.h */
32 #include <sys/uio.h> /* "input.h" */
43 #include "decoder_fifo.h"
45 #include "video_output.h"
47 #include "vdec_idct.h"
48 #include "video_decoder.h"
49 #include "vdec_motion.h"
51 #include "vpar_blocks.h"
52 #include "vpar_headers.h"
53 #include "vpar_synchro.h"
54 #include "video_parser.h"
64 /*****************************************************************************
65 * vpar_SynchroUpdateStructures : Update the synchro structures
66 *****************************************************************************/
67 void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
68 int i_coding_type, boolean_t b_kept )
72 pes_packet_t * p_pes = p_vpar->bit_stream.p_decoder_fifo->buffer[
73 p_vpar->bit_stream.p_decoder_fifo->i_start ];
75 /* try to guess the current DTS and PTS */
76 if( p_pes->b_has_pts )
80 /* if the image is I type, then the presentation timestamp is
81 * the PTS of the PES. Otherwise, we calculate it with the
82 * theorical framerate value */
83 if( i_coding_type == I_CODING_TYPE )
85 p_vpar->synchro.i_last_pts = p_pes->i_pts;
89 p_vpar->synchro.i_last_pts += p_vpar->synchro.i_theorical_delay;
96 p_vpar->synchro.i_last_pts += p_vpar->synchro.i_theorical_delay;
97 i_pts = p_vpar->synchro.i_last_pts;
100 /* update structures */
101 switch(i_coding_type)
105 p_vpar->synchro.i_P_seen += 1024;
106 if( b_kept ) p_vpar->synchro.i_P_kept += 1024;
110 p_vpar->synchro.i_B_seen += 1024;
111 if( b_kept ) p_vpar->synchro.i_B_kept += 1024;
116 /* update the last I PTS we have, we need it to
117 * calculate the theorical framerate */
118 if (i_pts != p_vpar->synchro.i_last_seen_I_pts)
120 if ( p_vpar->synchro.i_last_seen_I_pts )
122 p_vpar->synchro.i_theorical_delay =
123 1024 * ( i_pts - p_vpar->synchro.i_last_seen_I_pts )
124 / ( 1024 + p_vpar->synchro.i_B_seen
125 + p_vpar->synchro.i_P_seen);
127 p_vpar->synchro.i_last_seen_I_pts = i_pts;
130 /* now we calculated all statistics, it's time to
131 * decide what we have the time to display */
133 ( (i_pts - p_vpar->synchro.i_last_kept_I_pts) << 10 )
134 / p_vpar->synchro.i_delay;
136 p_vpar->synchro.b_all_I = 0;
137 p_vpar->synchro.b_all_B = 0;
138 p_vpar->synchro.b_all_P = 0;
139 p_vpar->synchro.displayable_p = 0;
140 p_vpar->synchro.displayable_b = 0;
142 if( ( p_vpar->synchro.b_all_I = ( i_can_display >= 1024 ) ) )
144 i_can_display -= 1024;
146 if( !( p_vpar->synchro.b_all_P
147 = ( i_can_display > p_vpar->synchro.i_P_seen ) ) )
149 p_vpar->synchro.displayable_p = i_can_display;
153 i_can_display -= p_vpar->synchro.i_P_seen;
155 if( !( p_vpar->synchro.b_all_B
156 = ( i_can_display > p_vpar->synchro.i_B_seen ) ) )
158 p_vpar->synchro.displayable_b = i_can_display;
164 if( p_vpar->synchro.b_all_I )
165 intf_ErrMsg( " I: 1024/1024 " );
167 if( p_vpar->synchro.b_all_P )
168 intf_ErrMsg( "P: %i/%i ", p_vpar->synchro.i_P_seen,
169 p_vpar->synchro.i_P_seen );
170 else if( p_vpar->synchro.displayable_p > 0 )
171 intf_ErrMsg( "P: %i/%i ", p_vpar->synchro.displayable_p,
172 p_vpar->synchro.i_P_seen );
176 if( p_vpar->synchro.b_all_B )
177 intf_ErrMsg( "B: %i/%i", p_vpar->synchro.i_B_seen,
178 p_vpar->synchro.i_B_seen );
179 else if( p_vpar->synchro.displayable_b > 0 )
180 intf_ErrMsg( "B: %i/%i", p_vpar->synchro.displayable_b,
181 p_vpar->synchro.i_B_seen );
185 intf_ErrMsg( "
\rDecoding: " );
186 /*intf_ErrMsg( "\n" );*/
188 p_vpar->synchro.i_P_seen = 0;
189 p_vpar->synchro.i_B_seen = 0;
191 /* update some values */
194 p_vpar->synchro.i_last_kept_I_pts = i_pts;
195 p_vpar->synchro.i_P_kept = 0;
196 p_vpar->synchro.i_B_kept = 0;
203 /*****************************************************************************
204 * vpar_SynchroChoose : Decide whether we will decode a picture or not
205 *****************************************************************************/
206 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
209 mtime_t i_delay = p_vpar->synchro.i_last_pts - mdate();
211 switch( i_coding_type )
215 if( p_vpar->synchro.i_type != VPAR_SYNCHRO_DEFAULT )
217 /* I, IP, IP+, IPB */
218 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus )
220 p_vpar->synchro.b_dropped_last = 1;
225 return( p_vpar->synchro.b_all_I );
229 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_I ) /* I */
234 if( p_vpar->synchro.i_type == VPAR_SYNCHRO_Iplus ) /* I+ */
236 if( p_vpar->synchro.b_dropped_last )
238 p_vpar->synchro.b_dropped_last = 0;
247 if( p_vpar->synchro.i_type >= VPAR_SYNCHRO_IP ) /* IP, IP+, IPB */
252 if( p_vpar->synchro.b_all_P )
257 if( p_vpar->synchro.displayable_p * i_delay
258 < p_vpar->synchro.i_delay )
263 p_vpar->synchro.displayable_p -= 1024;
269 if( p_vpar->synchro.i_type != VPAR_SYNCHRO_DEFAULT )
271 if( p_vpar->synchro.i_type <= VPAR_SYNCHRO_IP ) /* I, IP */
275 else if( p_vpar->synchro.i_type == VPAR_SYNCHRO_IPB ) /* IPB */
280 if( p_vpar->synchro.b_dropped_last ) /* IP+ */
282 p_vpar->synchro.b_dropped_last = 0;
286 p_vpar->synchro.b_dropped_last = 1;
290 if( p_vpar->synchro.b_all_B )
295 if( p_vpar->synchro.displayable_b <= 0 )
302 p_vpar->synchro.displayable_b -= 512;
306 p_vpar->synchro.displayable_b -= 1024;
314 /*****************************************************************************
315 * vpar_SynchroTrash : Update timers when we trash a picture
316 *****************************************************************************/
317 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
320 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
324 /*****************************************************************************
325 * vpar_SynchroDecode : Update timers when we decide to decode a picture
326 *****************************************************************************/
327 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
330 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
332 p_vpar->synchro.i_date_fifo[p_vpar->synchro.i_stop] = mdate();
334 FIFO_INCREMENT( i_stop );
338 /*****************************************************************************
339 * vpar_SynchroEnd : Called when the image is totally decoded
340 *****************************************************************************/
341 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
343 if( p_vpar->synchro.i_stop != p_vpar->synchro.i_start )
347 i_delay = ( mdate() -
348 p_vpar->synchro.i_date_fifo[p_vpar->synchro.i_start] )
349 / ( (p_vpar->synchro.i_stop - p_vpar->synchro.i_start) & 0x0f );
351 p_vpar->synchro.i_delay =
352 ( 7 * p_vpar->synchro.i_delay + i_delay ) >> 3;
355 intf_ErrMsg( "decode %lli (mean %lli, theorical %lli)\n",
356 i_delay, p_vpar->synchro.i_delay,
357 p_vpar->synchro.i_theorical_delay );
362 intf_ErrMsg( "vpar error: critical ! fifo full\n" );
365 FIFO_INCREMENT( i_start );
368 /*****************************************************************************
369 * vpar_SynchroDate : When an image has been decoded, ask for its date
370 *****************************************************************************/
371 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
375 mtime_t i_displaydate = p_vpar->synchro.i_last_pts;
377 static mtime_t i_delta = 0;
379 intf_ErrMsg( "displaying image with delay %lli and delta %lli\n",
380 i_displaydate - mdate(),
381 i_displaydate - i_delta );
383 intf_ErrMsg ( "theorical fps: %f - actual fps: %f \n",
384 1000000.0 / p_vpar->synchro.i_theorical_delay, 1000000.0 / p_vpar->synchro.i_delay );
386 i_delta = i_displaydate;
388 return i_displaydate;
391 return p_vpar->synchro.i_last_pts;
400 /* synchro a deux balles backportee du decodeur de reference. NE MARCHE PAS
401 AVEC LES IMAGES MONOTRAMES */
403 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
406 switch (i_coding_type)
409 if ((p_vpar->synchro.kludge_level <= p_vpar->synchro.kludge_nbp))
411 p_vpar->synchro.kludge_b++;
414 if (p_vpar->synchro.kludge_b %
415 (p_vpar->synchro.kludge_nbb /
416 (p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp)))
418 p_vpar->synchro.kludge_b++;
421 p_vpar->synchro.kludge_b++;
425 if (p_vpar->synchro.kludge_p++ >= p_vpar->synchro.kludge_level)
436 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
439 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
441 p_vpar->synchro.kludge_nbframes = 0;
442 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
445 p_vpar->synchro.kludge_nbframes++;
446 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
449 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
452 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
454 p_vpar->synchro.kludge_nbframes = 0;
455 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
456 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
459 p_vpar->synchro.kludge_nbframes++;
462 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
464 return( p_vpar->synchro.kludge_date
465 + p_vpar->synchro.kludge_nbframes * 1000000
466 / (p_vpar->sequence.i_frame_rate ) * 1001 );
469 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
473 void vpar_SynchroKludge( vpar_thread_t * p_vpar, mtime_t date )
476 int temp = p_vpar->synchro.kludge_level;
478 p_vpar->synchro.kludge_nbp = p_vpar->synchro.kludge_p ? p_vpar->synchro.kludge_p : 5;
479 p_vpar->synchro.kludge_nbb = p_vpar->synchro.kludge_b ? p_vpar->synchro.kludge_b : 6;
480 show_date = date - mdate();
481 p_vpar->synchro.kludge_p = 0;
482 p_vpar->synchro.kludge_b = 0;
484 if (show_date < (SYNC_DELAY - SYNC_TOLERATE) && show_date <= p_vpar->synchro.kludge_prevdate)
486 p_vpar->synchro.kludge_level--;
487 if (p_vpar->synchro.kludge_level < 0)
488 p_vpar->synchro.kludge_level = 0;
489 else if (p_vpar->synchro.kludge_level >
490 p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
491 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
493 if (temp != p_vpar->synchro.kludge_level)
494 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
495 temp, p_vpar->synchro.kludge_level, show_date );
498 else if (show_date > (SYNC_DELAY + SYNC_TOLERATE) && show_date >= p_vpar->synchro.kludge_prevdate)
500 p_vpar->synchro.kludge_level++;
501 if (p_vpar->synchro.kludge_level > p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
502 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
504 if (temp != p_vpar->synchro.kludge_level)
505 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
506 temp, p_vpar->synchro.kludge_level, show_date );
510 p_vpar->synchro.kludge_prevdate = show_date;
511 if ((p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp) > p_vpar->synchro.kludge_nbb)
512 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbb + p_vpar->synchro.kludge_nbp;
520 void vpar_SynchroSetCurrentDate( vpar_thread_t * p_vpar, int i_coding_type )
522 pes_packet_t * p_pes =
523 p_vpar->bit_stream.p_decoder_fifo->buffer[p_vpar->bit_stream.p_decoder_fifo->i_start];
526 switch( i_coding_type )
529 if( p_pes->b_has_pts )
531 if( p_pes->i_pts < p_vpar->synchro.i_current_frame_date )
533 intf_ErrMsg( "vpar warning: pts_date < current_date\n" );
535 p_vpar->synchro.i_current_frame_date = p_pes->i_pts;
536 p_pes->b_has_pts = 0;
540 p_vpar->synchro.i_current_frame_date += 1000000 / (p_vpar->sequence.i_frame_rate) * 1001;
546 if( p_vpar->synchro.i_backward_frame_date == 0 )
548 p_vpar->synchro.i_current_frame_date += 1000000 / (p_vpar->sequence.i_frame_rate) * 1001;
552 if( p_vpar->synchro.i_backward_frame_date < p_vpar->synchro.i_current_frame_date )
554 intf_ErrMsg( "vpar warning: backward_date < current_date (%Ld)\n",
555 p_vpar->synchro.i_backward_frame_date - p_vpar->synchro.i_current_frame_date );
557 p_vpar->synchro.i_current_frame_date = p_vpar->synchro.i_backward_frame_date;
558 p_vpar->synchro.i_backward_frame_date = 0;
561 if( p_pes->b_has_pts )
563 p_vpar->synchro.i_backward_frame_date = p_pes->i_pts;
564 p_pes->b_has_pts = 0;
570 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
573 boolean_t b_result = 1;
574 int i_synchro_level = p_vpar->p_vout->i_synchro_level;
576 vpar_SynchroSetCurrentDate( p_vpar, i_coding_type );
579 * The synchro level is updated by the video input (see SynchroLevelUpdate)
580 * so we just use the synchro_level to decide which frame to trash
583 switch( i_coding_type )
587 p_vpar->synchro.r_p_average =
588 (p_vpar->synchro.r_p_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_p_count)/SYNC_AVERAGE_COUNT;
589 p_vpar->synchro.r_b_average =
590 (p_vpar->synchro.r_b_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_b_count)/SYNC_AVERAGE_COUNT;
592 p_vpar->synchro.i_p_nb = (int)(p_vpar->synchro.r_p_average+0.5);
593 p_vpar->synchro.i_b_nb = (int)(p_vpar->synchro.r_b_average+0.5);
595 p_vpar->synchro.i_p_count = p_vpar->synchro.i_b_count = 0;
596 p_vpar->synchro.i_b_trasher = p_vpar->synchro.i_b_nb / 2;
597 p_vpar->synchro.i_i_count++;
601 p_vpar->synchro.i_p_count++;
602 if( p_vpar->synchro.i_p_count > i_synchro_level )
609 p_vpar->synchro.i_b_count++;
610 if( p_vpar->synchro.i_p_nb >= i_synchro_level )
612 /* We must trash all the B */
617 /* We use the brensenham algorithm to decide which B to trash */
618 p_vpar->synchro.i_b_trasher +=
619 p_vpar->synchro.i_b_nb - (i_synchro_level-p_vpar->synchro.i_p_nb);
620 if( p_vpar->synchro.i_b_trasher >= p_vpar->synchro.i_b_nb )
623 p_vpar->synchro.i_b_trasher -= p_vpar->synchro.i_b_nb;
632 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
635 vpar_SynchroChoose( p_vpar, i_coding_type, i_structure );
638 void vpar_SynchroUpdateLevel()
640 //vlc_mutex_lock( &level_lock );
641 //vlc_mutex_unlock( &level_lock );
644 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
646 return( p_vpar->synchro.i_current_frame_date );
649 /* functions with no use */
651 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
655 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,