1 /*****************************************************************************
2 * vpar_motion.c : motion vectors parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <stdlib.h> /* free() */
29 #include <sys/types.h> /* on BSD, uio.h needs types.h */
30 #include <sys/uio.h> /* "input.h" */
41 #include "decoder_fifo.h"
43 #include "video_output.h"
45 #include "vdec_idct.h"
46 #include "video_decoder.h"
47 #include "vdec_motion.h"
49 #include "vpar_blocks.h"
50 #include "vpar_headers.h"
51 #include "vpar_synchro.h"
52 #include "video_parser.h"
61 /*****************************************************************************
62 * vpar_SynchroUpdateTab : Update a mean table in the synchro structure
63 *****************************************************************************/
64 float vpar_SynchroUpdateTab( video_synchro_tab_t * tab, int count )
67 tab->mean = ( tab->mean + MAX_COUNT * count ) / ( MAX_COUNT + 1 );
68 tab->deviation = ( tab->deviation + MAX_COUNT * abs (tab->mean - count) )
71 return tab->deviation;
74 /*****************************************************************************
75 * vpar_SynchroUpdateStructures : Update the synchro structures
76 *****************************************************************************/
77 void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
78 int i_coding_type, int dropped )
80 float candidate_deviation;
81 float optimal_deviation;
83 mtime_t i_current_pts;
85 mtime_t i_displaydate;
86 decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
88 /* interpolate the current _decode_ PTS */
89 i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
90 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
94 i_current_pts = p_vpar->synchro.i_last_decode_pts
95 + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
97 p_vpar->synchro.i_last_decode_pts = i_current_pts;
99 /* see if the current image has a pts - if not, set to 0 */
100 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
103 /* update display time */
104 i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
105 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
107 if( !i_displaydate || i_coding_type != I_CODING_TYPE )
109 if (!p_vpar->synchro.i_images_since_pts )
110 p_vpar->synchro.i_images_since_pts = 10;
112 i_displaydate = p_vpar->synchro.i_last_display_pts
113 + 1000000.0 / (p_vpar->synchro.theorical_fps);
114 //fprintf (stderr, " ");
117 decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts = 0;
119 /* else fprintf (stderr, "R ");
120 if (dropped) fprintf (stderr, " "); else fprintf (stderr, "* ");
121 fprintf (stderr, "%i ", i_coding_type);
122 fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts); */
124 p_vpar->synchro.i_images_since_pts--;
125 p_vpar->synchro.i_last_display_pts = i_displaydate;
129 /* update structures */
130 switch(i_coding_type)
134 p_vpar->synchro.current_p_count++;
135 if( !dropped ) p_vpar->synchro.nondropped_p_count++;
139 p_vpar->synchro.current_b_count++;
140 if( !dropped ) p_vpar->synchro.nondropped_b_count++;
145 /* update information about images we can decode */
146 if (i_current_pts != p_vpar->synchro.i_last_i_pts)
148 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
150 p_vpar->synchro.theorical_fps = (p_vpar->synchro.theorical_fps + 1000000.0 * (1 + p_vpar->synchro.current_b_count + p_vpar->synchro.current_p_count) / (i_current_pts - p_vpar->synchro.i_last_i_pts)) / 2;
152 p_vpar->synchro.i_last_i_pts = i_current_pts;
157 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
159 p_vpar->synchro.actual_fps = (p_vpar->synchro.actual_fps + 1000000.0 * (1 + p_vpar->synchro.nondropped_b_count + p_vpar->synchro.nondropped_p_count) / (i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts)) / 2;
165 /* update all the structures for P images */
168 optimal_deviation = vpar_SynchroUpdateTab(
169 &p_vpar->synchro.tab_p[0],
170 p_vpar->synchro.current_p_count);
171 predict = p_vpar->synchro.tab_p[0].mean;
174 candidate_deviation = vpar_SynchroUpdateTab(
175 &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
176 p_vpar->synchro.current_p_count);
177 if (candidate_deviation < optimal_deviation)
179 optimal_deviation = candidate_deviation;
180 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
184 candidate_deviation = vpar_SynchroUpdateTab(
185 &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
186 p_vpar->synchro.current_p_count);
187 if (candidate_deviation < optimal_deviation)
189 optimal_deviation = candidate_deviation;
190 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
193 p_vpar->synchro.p_count_predict = predict;
194 p_vpar->synchro.current_p_count = 0;
197 /* update all the structures for B images */
200 optimal_deviation = vpar_SynchroUpdateTab(
201 &p_vpar->synchro.tab_b[0],
202 p_vpar->synchro.current_b_count);
203 predict = p_vpar->synchro.tab_b[0].mean;
206 candidate_deviation = vpar_SynchroUpdateTab(
207 &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
208 p_vpar->synchro.current_b_count);
209 if (candidate_deviation < optimal_deviation)
211 optimal_deviation = candidate_deviation;
212 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
216 candidate_deviation = vpar_SynchroUpdateTab(
217 &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
218 p_vpar->synchro.current_b_count);
219 if (candidate_deviation < optimal_deviation)
221 optimal_deviation = candidate_deviation;
222 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
225 p_vpar->synchro.b_count_predict = predict;
226 p_vpar->synchro.current_b_count = 0;
228 /* now we calculated all statistics, it's time to
229 * decide what we have the time to display
231 i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
233 p_vpar->synchro.can_display_i
234 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
236 p_vpar->synchro.can_display_p
237 = ( p_vpar->synchro.i_mean_decode_time
238 * (1 + p_vpar->synchro.p_count_predict) < i_delay );
240 if( !p_vpar->synchro.can_display_p )
242 p_vpar->synchro.displayable_p
243 = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
244 if( p_vpar->synchro.displayable_p < 0 )
245 p_vpar->synchro.displayable_p = 0;
248 p_vpar->synchro.displayable_p = 0;
250 if( p_vpar->synchro.can_display_p
251 && !(p_vpar->synchro.can_display_b
252 = ( p_vpar->synchro.i_mean_decode_time
253 * (1 + p_vpar->synchro.b_count_predict
254 + p_vpar->synchro.p_count_predict)) < i_delay) )
256 p_vpar->synchro.displayable_b
257 = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
258 - p_vpar->synchro.can_display_p;
261 p_vpar->synchro.displayable_b = 0;
265 "I %i P %i (%f) B %i (%f)\n",
266 p_vpar->synchro.can_display_i,
267 p_vpar->synchro.can_display_p,
268 p_vpar->synchro.displayable_p,
269 p_vpar->synchro.can_display_b,
270 p_vpar->synchro.displayable_b );
273 /* update some values */
276 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
277 p_vpar->synchro.nondropped_p_count = 0;
278 p_vpar->synchro.nondropped_b_count = 0;
285 p_vpar->synchro.modulo++;
289 /*****************************************************************************
290 * vpar_SynchroChoose : Decide whether we will decode a picture or not
291 *****************************************************************************/
292 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
295 mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
297 switch( i_coding_type )
301 return( p_vpar->synchro.can_display_i );
305 if( p_vpar->synchro.can_display_p )
308 if( p_vpar->synchro.displayable_p * i_delay
309 < p_vpar->synchro.i_mean_decode_time )
311 //fprintf( stderr, "trashed a P\n");
315 p_vpar->synchro.displayable_p--;
320 if( p_vpar->synchro.can_display_b )
323 /* modulo & 0x3 is here to add some randomness */
324 if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
325 * p_vpar->synchro.i_mean_decode_time )
327 //fprintf( stderr, "trashed a B\n");
331 if( p_vpar->synchro.displayable_b <= 0 )
334 p_vpar->synchro.displayable_b--;
342 /*****************************************************************************
343 * vpar_SynchroTrash : Update timers when we trash a picture
344 *****************************************************************************/
345 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
348 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
352 /*****************************************************************************
353 * vpar_SynchroDecode : Update timers when we decide to decode a picture
354 *****************************************************************************/
355 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
358 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
360 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
361 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
364 p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
368 /*****************************************************************************
369 * vpar_SynchroEnd : Called when the image is totally decoded
370 *****************************************************************************/
371 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
373 mtime_t i_decode_time;
375 i_decode_time = (mdate() -
376 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
377 / ( (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start) & 0x0f);
379 p_vpar->synchro.i_mean_decode_time =
380 ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
383 "decoding time was %lli\n",
384 p_vpar->synchro.i_mean_decode_time); */
386 p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
390 /*****************************************************************************
391 * vpar_SynchroDate : When an image has been decoded, ask for its date
392 *****************************************************************************/
393 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
395 mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
398 static mtime_t i_delta = 0;
401 "displaying type %i with delay %lli and delta %lli\n",
402 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
403 i_displaydate - mdate(),
404 i_displaydate - i_delta );
407 "theorical fps: %f - actual fps: %f \n",
408 p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
410 i_delta = i_displaydate;
413 return i_displaydate;
420 /* synchro a deux balles backportee du decodeur de reference. NE MARCHE PAS
421 AVEC LES IMAGES MONOTRAMES */
423 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
426 switch (i_coding_type)
429 if ((p_vpar->synchro.kludge_level <= p_vpar->synchro.kludge_nbp))
431 p_vpar->synchro.kludge_b++;
434 if (p_vpar->synchro.kludge_b %
435 (p_vpar->synchro.kludge_nbb /
436 (p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp)))
438 p_vpar->synchro.kludge_b++;
441 p_vpar->synchro.kludge_b++;
445 if (p_vpar->synchro.kludge_p++ >= p_vpar->synchro.kludge_level)
456 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
459 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
461 p_vpar->synchro.kludge_nbframes = 0;
462 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
465 p_vpar->synchro.kludge_nbframes++;
466 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
469 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
472 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
474 p_vpar->synchro.kludge_nbframes = 0;
475 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
476 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
479 p_vpar->synchro.kludge_nbframes++;
482 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
484 return( p_vpar->synchro.kludge_date
485 + p_vpar->synchro.kludge_nbframes*1000000/(p_vpar->sequence.r_frame_rate ) );
488 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
492 void vpar_SynchroKludge( vpar_thread_t * p_vpar, mtime_t date )
495 int temp = p_vpar->synchro.kludge_level;
497 p_vpar->synchro.kludge_nbp = p_vpar->synchro.kludge_p ? p_vpar->synchro.kludge_p : 5;
498 p_vpar->synchro.kludge_nbb = p_vpar->synchro.kludge_b ? p_vpar->synchro.kludge_b : 6;
499 show_date = date - mdate();
500 p_vpar->synchro.kludge_p = 0;
501 p_vpar->synchro.kludge_b = 0;
503 if (show_date < (SYNC_DELAY - SYNC_TOLERATE) && show_date <= p_vpar->synchro.kludge_prevdate)
505 p_vpar->synchro.kludge_level--;
506 if (p_vpar->synchro.kludge_level < 0)
507 p_vpar->synchro.kludge_level = 0;
508 else if (p_vpar->synchro.kludge_level >
509 p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
510 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
512 if (temp != p_vpar->synchro.kludge_level)
513 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
514 temp, p_vpar->synchro.kludge_level, show_date );
517 else if (show_date > (SYNC_DELAY + SYNC_TOLERATE) && show_date >= p_vpar->synchro.kludge_prevdate)
519 p_vpar->synchro.kludge_level++;
520 if (p_vpar->synchro.kludge_level > p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
521 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
523 if (temp != p_vpar->synchro.kludge_level)
524 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
525 temp, p_vpar->synchro.kludge_level, show_date );
529 p_vpar->synchro.kludge_prevdate = show_date;
530 if ((p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp) > p_vpar->synchro.kludge_nbb)
531 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbb + p_vpar->synchro.kludge_nbp;
539 void vpar_SynchroSetCurrentDate( vpar_thread_t * p_vpar, int i_coding_type )
541 pes_packet_t * p_pes =
542 p_vpar->bit_stream.p_decoder_fifo->buffer[p_vpar->bit_stream.p_decoder_fifo->i_start];
545 switch( i_coding_type )
548 if( p_pes->b_has_pts )
550 if( p_pes->i_pts < p_vpar->synchro.i_current_frame_date )
552 intf_ErrMsg( "vpar warning: pts_date < current_date\n" );
554 p_vpar->synchro.i_current_frame_date = p_pes->i_pts;
555 p_pes->b_has_pts = 0;
559 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
565 if( p_vpar->synchro.i_backward_frame_date == 0 )
567 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
571 if( p_vpar->synchro.i_backward_frame_date < p_vpar->synchro.i_current_frame_date )
573 intf_ErrMsg( "vpar warning: backward_date < current_date (%Ld)\n",
574 p_vpar->synchro.i_backward_frame_date - p_vpar->synchro.i_current_frame_date );
576 p_vpar->synchro.i_current_frame_date = p_vpar->synchro.i_backward_frame_date;
577 p_vpar->synchro.i_backward_frame_date = 0;
580 if( p_pes->b_has_pts )
582 p_vpar->synchro.i_backward_frame_date = p_pes->i_pts;
583 p_pes->b_has_pts = 0;
589 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
592 boolean_t b_result = 1;
593 int i_synchro_level = p_vpar->p_vout->i_synchro_level;
595 vpar_SynchroSetCurrentDate( p_vpar, i_coding_type );
598 * The synchro level is updated by the video input (see SynchroLevelUpdate)
599 * so we just use the synchro_level to decide which frame to trash
602 switch( i_coding_type )
606 //fprintf( stderr, "p : %d (%d), b : %d (%d)\n", p_vpar->synchro.i_p_count, p_vpar->synchro.i_p_nb,
607 // p_vpar->synchro.i_b_count, p_vpar->synchro.i_b_nb );
609 p_vpar->synchro.r_p_average =
610 (p_vpar->synchro.r_p_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_p_count)/SYNC_AVERAGE_COUNT;
611 p_vpar->synchro.r_b_average =
612 (p_vpar->synchro.r_b_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_b_count)/SYNC_AVERAGE_COUNT;
614 p_vpar->synchro.i_p_nb = (int)(p_vpar->synchro.r_p_average+0.5);
615 p_vpar->synchro.i_b_nb = (int)(p_vpar->synchro.r_b_average+0.5);
617 p_vpar->synchro.i_p_count = p_vpar->synchro.i_b_count = 0;
618 p_vpar->synchro.i_b_trasher = p_vpar->synchro.i_b_nb / 2;
619 p_vpar->synchro.i_i_count++;
623 p_vpar->synchro.i_p_count++;
624 if( p_vpar->synchro.i_p_count > i_synchro_level )
631 p_vpar->synchro.i_b_count++;
632 if( p_vpar->synchro.i_p_nb >= i_synchro_level )
634 /* We must trash all the B */
639 /* We use the brensenham algorithm to decide which B to trash */
640 p_vpar->synchro.i_b_trasher +=
641 p_vpar->synchro.i_b_nb - (i_synchro_level-p_vpar->synchro.i_p_nb);
642 if( p_vpar->synchro.i_b_trasher >= p_vpar->synchro.i_b_nb )
645 p_vpar->synchro.i_b_trasher -= p_vpar->synchro.i_b_nb;
654 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
657 vpar_SynchroChoose( p_vpar, i_coding_type, i_structure );
660 void vpar_SynchroUpdateLevel()
662 //vlc_mutex_lock( &level_lock );
663 //vlc_mutex_unlock( &level_lock );
666 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
668 //fprintf( stderr, "delay : %Ld\n" , mdate() - p_vpar->synchro.i_current_frame_date );
669 return( p_vpar->synchro.i_current_frame_date );
672 /* functions with no use */
674 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
678 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,