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 GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* free() */
28 #include <sys/types.h> /* on BSD, uio.h needs types.h */
29 #include <sys/uio.h> /* "input.h" */
40 #include "decoder_fifo.h"
42 #include "video_output.h"
44 #include "vdec_idct.h"
45 #include "video_decoder.h"
46 #include "vdec_motion.h"
48 #include "vpar_blocks.h"
49 #include "vpar_headers.h"
50 #include "vpar_synchro.h"
51 #include "video_parser.h"
60 /*****************************************************************************
61 * vpar_SynchroUpdateTab : Update a mean table in the synchro structure
62 *****************************************************************************/
63 float vpar_SynchroUpdateTab( video_synchro_tab_t * tab, int count )
66 tab->mean = ( tab->mean + MAX_COUNT * count ) / ( MAX_COUNT + 1 );
67 tab->deviation = ( tab->deviation + MAX_COUNT * abs (tab->mean - count) )
70 return tab->deviation;
73 /*****************************************************************************
74 * vpar_SynchroUpdateStructures : Update the synchro structures
75 *****************************************************************************/
76 void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
77 int i_coding_type, int dropped )
79 float candidate_deviation;
80 float optimal_deviation;
82 mtime_t i_current_pts;
84 mtime_t i_displaydate;
85 decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
87 /* interpolate the current _decode_ PTS */
88 i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
89 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
93 i_current_pts = p_vpar->synchro.i_last_decode_pts
94 + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
96 p_vpar->synchro.i_last_decode_pts = i_current_pts;
98 /* see if the current image has a pts - if not, set to 0 */
99 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
102 /* update display time */
103 i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
104 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
106 if( !i_displaydate || i_coding_type != I_CODING_TYPE )
108 if (!p_vpar->synchro.i_images_since_pts )
109 p_vpar->synchro.i_images_since_pts = 10;
111 i_displaydate = p_vpar->synchro.i_last_display_pts
112 + 1000000.0 / (p_vpar->synchro.theorical_fps);
113 //fprintf (stderr, " ");
116 decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts = 0;
118 /* else fprintf (stderr, "R ");
119 if (dropped) fprintf (stderr, " "); else fprintf (stderr, "* ");
120 fprintf (stderr, "%i ", i_coding_type);
121 fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts); */
123 p_vpar->synchro.i_images_since_pts--;
124 p_vpar->synchro.i_last_display_pts = i_displaydate;
128 /* update structures */
129 switch(i_coding_type)
133 p_vpar->synchro.current_p_count++;
134 if( !dropped ) p_vpar->synchro.nondropped_p_count++;
138 p_vpar->synchro.current_b_count++;
139 if( !dropped ) p_vpar->synchro.nondropped_b_count++;
144 /* update information about images we can decode */
145 if (i_current_pts != p_vpar->synchro.i_last_i_pts)
147 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
149 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;
151 p_vpar->synchro.i_last_i_pts = i_current_pts;
156 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
158 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;
164 /* update all the structures for P images */
167 optimal_deviation = vpar_SynchroUpdateTab(
168 &p_vpar->synchro.tab_p[0],
169 p_vpar->synchro.current_p_count);
170 predict = p_vpar->synchro.tab_p[0].mean;
173 candidate_deviation = vpar_SynchroUpdateTab(
174 &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
175 p_vpar->synchro.current_p_count);
176 if (candidate_deviation < optimal_deviation)
178 optimal_deviation = candidate_deviation;
179 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
183 candidate_deviation = vpar_SynchroUpdateTab(
184 &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
185 p_vpar->synchro.current_p_count);
186 if (candidate_deviation < optimal_deviation)
188 optimal_deviation = candidate_deviation;
189 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
192 p_vpar->synchro.p_count_predict = predict;
193 p_vpar->synchro.current_p_count = 0;
196 /* update all the structures for B images */
199 optimal_deviation = vpar_SynchroUpdateTab(
200 &p_vpar->synchro.tab_b[0],
201 p_vpar->synchro.current_b_count);
202 predict = p_vpar->synchro.tab_b[0].mean;
205 candidate_deviation = vpar_SynchroUpdateTab(
206 &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
207 p_vpar->synchro.current_b_count);
208 if (candidate_deviation < optimal_deviation)
210 optimal_deviation = candidate_deviation;
211 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
215 candidate_deviation = vpar_SynchroUpdateTab(
216 &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
217 p_vpar->synchro.current_b_count);
218 if (candidate_deviation < optimal_deviation)
220 optimal_deviation = candidate_deviation;
221 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
224 p_vpar->synchro.b_count_predict = predict;
225 p_vpar->synchro.current_b_count = 0;
227 /* now we calculated all statistics, it's time to
228 * decide what we have the time to display
230 i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
232 p_vpar->synchro.can_display_i
233 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
235 p_vpar->synchro.can_display_p
236 = ( p_vpar->synchro.i_mean_decode_time
237 * (1 + p_vpar->synchro.p_count_predict) < i_delay );
239 if( !p_vpar->synchro.can_display_p )
241 p_vpar->synchro.displayable_p
242 = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
243 if( p_vpar->synchro.displayable_p < 0 )
244 p_vpar->synchro.displayable_p = 0;
247 p_vpar->synchro.displayable_p = 0;
249 if( p_vpar->synchro.can_display_p
250 && !(p_vpar->synchro.can_display_b
251 = ( p_vpar->synchro.i_mean_decode_time
252 * (1 + p_vpar->synchro.b_count_predict
253 + p_vpar->synchro.p_count_predict)) < i_delay) )
255 p_vpar->synchro.displayable_b
256 = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
257 - p_vpar->synchro.can_display_p;
260 p_vpar->synchro.displayable_b = 0;
264 "I %i P %i (%f) B %i (%f)\n",
265 p_vpar->synchro.can_display_i,
266 p_vpar->synchro.can_display_p,
267 p_vpar->synchro.displayable_p,
268 p_vpar->synchro.can_display_b,
269 p_vpar->synchro.displayable_b );
272 /* update some values */
275 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
276 p_vpar->synchro.nondropped_p_count = 0;
277 p_vpar->synchro.nondropped_b_count = 0;
284 p_vpar->synchro.modulo++;
288 /*****************************************************************************
289 * vpar_SynchroChoose : Decide whether we will decode a picture or not
290 *****************************************************************************/
291 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
294 mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
296 switch( i_coding_type )
300 return( p_vpar->synchro.can_display_i );
304 if( p_vpar->synchro.can_display_p )
307 if( p_vpar->synchro.displayable_p * i_delay
308 < p_vpar->synchro.i_mean_decode_time )
310 //fprintf( stderr, "trashed a P\n");
314 p_vpar->synchro.displayable_p--;
319 if( p_vpar->synchro.can_display_b )
322 /* modulo & 0x3 is here to add some randomness */
323 if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
324 * p_vpar->synchro.i_mean_decode_time )
326 //fprintf( stderr, "trashed a B\n");
330 if( p_vpar->synchro.displayable_b <= 0 )
333 p_vpar->synchro.displayable_b--;
341 /*****************************************************************************
342 * vpar_SynchroTrash : Update timers when we trash a picture
343 *****************************************************************************/
344 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
347 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
351 /*****************************************************************************
352 * vpar_SynchroDecode : Update timers when we decide to decode a picture
353 *****************************************************************************/
354 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
357 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
359 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
360 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
363 p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
367 /*****************************************************************************
368 * vpar_SynchroEnd : Called when the image is totally decoded
369 *****************************************************************************/
370 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
372 mtime_t i_decode_time;
374 i_decode_time = (mdate() -
375 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
376 / ( (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start) & 0x0f);
378 p_vpar->synchro.i_mean_decode_time =
379 ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
382 "decoding time was %lli\n",
383 p_vpar->synchro.i_mean_decode_time); */
385 p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
389 /*****************************************************************************
390 * vpar_SynchroDate : When an image has been decoded, ask for its date
391 *****************************************************************************/
392 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
394 mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
397 static mtime_t i_delta = 0;
400 "displaying type %i with delay %lli and delta %lli\n",
401 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
402 i_displaydate - mdate(),
403 i_displaydate - i_delta );
406 "theorical fps: %f - actual fps: %f \n",
407 p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
409 i_delta = i_displaydate;
412 return i_displaydate;
419 /* synchro a deux balles backportee du decodeur de reference. NE MARCHE PAS
420 AVEC LES IMAGES MONOTRAMES */
422 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
425 switch (i_coding_type)
428 if ((p_vpar->synchro.kludge_level <= p_vpar->synchro.kludge_nbp))
430 p_vpar->synchro.kludge_b++;
433 if (p_vpar->synchro.kludge_b %
434 (p_vpar->synchro.kludge_nbb /
435 (p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp)))
437 p_vpar->synchro.kludge_b++;
440 p_vpar->synchro.kludge_b++;
444 if (p_vpar->synchro.kludge_p++ >= p_vpar->synchro.kludge_level)
455 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
458 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
460 p_vpar->synchro.kludge_nbframes = 0;
461 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
464 p_vpar->synchro.kludge_nbframes++;
465 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
468 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
471 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
473 p_vpar->synchro.kludge_nbframes = 0;
474 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
475 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
478 p_vpar->synchro.kludge_nbframes++;
481 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
483 return( p_vpar->synchro.kludge_date
484 + p_vpar->synchro.kludge_nbframes*1000000/(p_vpar->sequence.r_frame_rate ) );
487 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
491 void vpar_SynchroKludge( vpar_thread_t * p_vpar, mtime_t date )
494 int temp = p_vpar->synchro.kludge_level;
496 p_vpar->synchro.kludge_nbp = p_vpar->synchro.kludge_p ? p_vpar->synchro.kludge_p : 5;
497 p_vpar->synchro.kludge_nbb = p_vpar->synchro.kludge_b ? p_vpar->synchro.kludge_b : 6;
498 show_date = date - mdate();
499 p_vpar->synchro.kludge_p = 0;
500 p_vpar->synchro.kludge_b = 0;
502 if (show_date < (SYNC_DELAY - SYNC_TOLERATE) && show_date <= p_vpar->synchro.kludge_prevdate)
504 p_vpar->synchro.kludge_level--;
505 if (p_vpar->synchro.kludge_level < 0)
506 p_vpar->synchro.kludge_level = 0;
507 else if (p_vpar->synchro.kludge_level >
508 p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
509 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
511 if (temp != p_vpar->synchro.kludge_level)
512 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
513 temp, p_vpar->synchro.kludge_level, show_date );
516 else if (show_date > (SYNC_DELAY + SYNC_TOLERATE) && show_date >= p_vpar->synchro.kludge_prevdate)
518 p_vpar->synchro.kludge_level++;
519 if (p_vpar->synchro.kludge_level > p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
520 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
522 if (temp != p_vpar->synchro.kludge_level)
523 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
524 temp, p_vpar->synchro.kludge_level, show_date );
528 p_vpar->synchro.kludge_prevdate = show_date;
529 if ((p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp) > p_vpar->synchro.kludge_nbb)
530 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbb + p_vpar->synchro.kludge_nbp;
538 void vpar_SynchroSetCurrentDate( vpar_thread_t * p_vpar, int i_coding_type )
540 pes_packet_t * p_pes =
541 p_vpar->bit_stream.p_decoder_fifo->buffer[p_vpar->bit_stream.p_decoder_fifo->i_start];
544 switch( i_coding_type )
547 if( p_pes->b_has_pts )
549 if( p_pes->i_pts < p_vpar->synchro.i_current_frame_date )
551 intf_ErrMsg( "vpar warning: pts_date < current_date\n" );
553 p_vpar->synchro.i_current_frame_date = p_pes->i_pts;
554 p_pes->b_has_pts = 0;
558 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
564 if( p_vpar->synchro.i_backward_frame_date == 0 )
566 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
570 if( p_vpar->synchro.i_backward_frame_date < p_vpar->synchro.i_current_frame_date )
572 intf_ErrMsg( "vpar warning: backward_date < current_date (%Ld)\n",
573 p_vpar->synchro.i_backward_frame_date - p_vpar->synchro.i_current_frame_date );
575 p_vpar->synchro.i_current_frame_date = p_vpar->synchro.i_backward_frame_date;
576 p_vpar->synchro.i_backward_frame_date = 0;
579 if( p_pes->b_has_pts )
581 p_vpar->synchro.i_backward_frame_date = p_pes->i_pts;
582 p_pes->b_has_pts = 0;
588 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
591 boolean_t b_result = 1;
592 int i_synchro_level = p_vpar->p_vout->i_synchro_level;
594 vpar_SynchroSetCurrentDate( p_vpar, i_coding_type );
597 * The synchro level is updated by the video input (see SynchroLevelUpdate)
598 * so we just use the synchro_level to decide which frame to trash
601 switch( i_coding_type )
605 //fprintf( stderr, "p : %d (%d), b : %d (%d)\n", p_vpar->synchro.i_p_count, p_vpar->synchro.i_p_nb,
606 // p_vpar->synchro.i_b_count, p_vpar->synchro.i_b_nb );
608 p_vpar->synchro.r_p_average =
609 (p_vpar->synchro.r_p_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_p_count)/SYNC_AVERAGE_COUNT;
610 p_vpar->synchro.r_b_average =
611 (p_vpar->synchro.r_b_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_b_count)/SYNC_AVERAGE_COUNT;
613 p_vpar->synchro.i_p_nb = (int)(p_vpar->synchro.r_p_average+0.5);
614 p_vpar->synchro.i_b_nb = (int)(p_vpar->synchro.r_b_average+0.5);
616 p_vpar->synchro.i_p_count = p_vpar->synchro.i_b_count = 0;
617 p_vpar->synchro.i_b_trasher = p_vpar->synchro.i_b_nb / 2;
618 p_vpar->synchro.i_i_count++;
622 p_vpar->synchro.i_p_count++;
623 if( p_vpar->synchro.i_p_count > i_synchro_level )
630 p_vpar->synchro.i_b_count++;
631 if( p_vpar->synchro.i_p_nb >= i_synchro_level )
633 /* We must trash all the B */
638 /* We use the brensenham algorithm to decide which B to trash */
639 p_vpar->synchro.i_b_trasher +=
640 p_vpar->synchro.i_b_nb - (i_synchro_level-p_vpar->synchro.i_p_nb);
641 if( p_vpar->synchro.i_b_trasher >= p_vpar->synchro.i_b_nb )
644 p_vpar->synchro.i_b_trasher -= p_vpar->synchro.i_b_nb;
653 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
656 vpar_SynchroChoose( p_vpar, i_coding_type, i_structure );
659 void vpar_SynchroUpdateLevel()
661 //vlc_mutex_lock( &level_lock );
662 //vlc_mutex_unlock( &level_lock );
665 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
667 //fprintf( stderr, "delay : %Ld\n" , mdate() - p_vpar->synchro.i_current_frame_date );
668 return( p_vpar->synchro.i_current_frame_date );
671 /* functions with no use */
673 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
677 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,