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 *****************************************************************************/
29 #include <stdlib.h> /* free() */
30 #include <sys/types.h> /* on BSD, uio.h needs types.h */
31 #include <sys/uio.h> /* "input.h" */
42 #include "decoder_fifo.h"
44 #include "video_output.h"
46 #include "vdec_idct.h"
47 #include "video_decoder.h"
48 #include "vdec_motion.h"
50 #include "vpar_blocks.h"
51 #include "vpar_headers.h"
52 #include "vpar_synchro.h"
53 #include "video_parser.h"
62 /*****************************************************************************
63 * vpar_SynchroUpdateTab : Update a mean table in the synchro structure
64 *****************************************************************************/
65 float vpar_SynchroUpdateTab( video_synchro_tab_t * tab, int count )
68 tab->mean = ( tab->mean + MAX_COUNT * count ) / ( MAX_COUNT + 1 );
69 tab->deviation = ( tab->deviation + MAX_COUNT * abs (tab->mean - count) )
72 return tab->deviation;
75 /*****************************************************************************
76 * vpar_SynchroUpdateStructures : Update the synchro structures
77 *****************************************************************************/
78 void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
79 int i_coding_type, int dropped )
81 float candidate_deviation;
82 float optimal_deviation;
84 mtime_t i_current_pts;
86 mtime_t i_displaydate;
87 decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
89 /* interpolate the current _decode_ PTS */
90 i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
91 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
95 i_current_pts = p_vpar->synchro.i_last_decode_pts
96 + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
98 p_vpar->synchro.i_last_decode_pts = i_current_pts;
100 /* see if the current image has a pts - if not, set to 0 */
101 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
104 /* update display time */
105 i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
106 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
108 if( !i_displaydate || i_coding_type != I_CODING_TYPE )
110 if (!p_vpar->synchro.i_images_since_pts )
111 p_vpar->synchro.i_images_since_pts = 10;
113 i_displaydate = p_vpar->synchro.i_last_display_pts
114 + 1000000.0 / (p_vpar->synchro.theorical_fps);
115 //fprintf (stderr, " ");
118 decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts = 0;
120 /* else fprintf (stderr, "R ");
121 if (dropped) fprintf (stderr, " "); else fprintf (stderr, "* ");
122 fprintf (stderr, "%i ", i_coding_type);
123 fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts); */
125 p_vpar->synchro.i_images_since_pts--;
126 p_vpar->synchro.i_last_display_pts = i_displaydate;
130 /* update structures */
131 switch(i_coding_type)
135 p_vpar->synchro.current_p_count++;
136 if( !dropped ) p_vpar->synchro.nondropped_p_count++;
140 p_vpar->synchro.current_b_count++;
141 if( !dropped ) p_vpar->synchro.nondropped_b_count++;
146 /* update information about images we can decode */
147 if (i_current_pts != p_vpar->synchro.i_last_i_pts)
149 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
151 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;
153 p_vpar->synchro.i_last_i_pts = i_current_pts;
158 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
160 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;
166 /* update all the structures for P images */
169 optimal_deviation = vpar_SynchroUpdateTab(
170 &p_vpar->synchro.tab_p[0],
171 p_vpar->synchro.current_p_count);
172 predict = p_vpar->synchro.tab_p[0].mean;
175 candidate_deviation = vpar_SynchroUpdateTab(
176 &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
177 p_vpar->synchro.current_p_count);
178 if (candidate_deviation < optimal_deviation)
180 optimal_deviation = candidate_deviation;
181 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
185 candidate_deviation = vpar_SynchroUpdateTab(
186 &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
187 p_vpar->synchro.current_p_count);
188 if (candidate_deviation < optimal_deviation)
190 optimal_deviation = candidate_deviation;
191 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
194 p_vpar->synchro.p_count_predict = predict;
195 p_vpar->synchro.current_p_count = 0;
198 /* update all the structures for B images */
201 optimal_deviation = vpar_SynchroUpdateTab(
202 &p_vpar->synchro.tab_b[0],
203 p_vpar->synchro.current_b_count);
204 predict = p_vpar->synchro.tab_b[0].mean;
207 candidate_deviation = vpar_SynchroUpdateTab(
208 &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
209 p_vpar->synchro.current_b_count);
210 if (candidate_deviation < optimal_deviation)
212 optimal_deviation = candidate_deviation;
213 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
217 candidate_deviation = vpar_SynchroUpdateTab(
218 &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
219 p_vpar->synchro.current_b_count);
220 if (candidate_deviation < optimal_deviation)
222 optimal_deviation = candidate_deviation;
223 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
226 p_vpar->synchro.b_count_predict = predict;
227 p_vpar->synchro.current_b_count = 0;
229 /* now we calculated all statistics, it's time to
230 * decide what we have the time to display
232 i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
234 p_vpar->synchro.can_display_i
235 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
237 p_vpar->synchro.can_display_p
238 = ( p_vpar->synchro.i_mean_decode_time
239 * (1 + p_vpar->synchro.p_count_predict) < i_delay );
241 if( !p_vpar->synchro.can_display_p )
243 p_vpar->synchro.displayable_p
244 = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
245 if( p_vpar->synchro.displayable_p < 0 )
246 p_vpar->synchro.displayable_p = 0;
249 p_vpar->synchro.displayable_p = 0;
251 if( p_vpar->synchro.can_display_p
252 && !(p_vpar->synchro.can_display_b
253 = ( p_vpar->synchro.i_mean_decode_time
254 * (1 + p_vpar->synchro.b_count_predict
255 + p_vpar->synchro.p_count_predict)) < i_delay) )
257 p_vpar->synchro.displayable_b
258 = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
259 - p_vpar->synchro.can_display_p;
262 p_vpar->synchro.displayable_b = 0;
266 "I %i P %i (%f) B %i (%f)\n",
267 p_vpar->synchro.can_display_i,
268 p_vpar->synchro.can_display_p,
269 p_vpar->synchro.displayable_p,
270 p_vpar->synchro.can_display_b,
271 p_vpar->synchro.displayable_b );
274 /* update some values */
277 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
278 p_vpar->synchro.nondropped_p_count = 0;
279 p_vpar->synchro.nondropped_b_count = 0;
286 p_vpar->synchro.modulo++;
290 /*****************************************************************************
291 * vpar_SynchroChoose : Decide whether we will decode a picture or not
292 *****************************************************************************/
293 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
296 mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
298 switch( i_coding_type )
302 return( p_vpar->synchro.can_display_i );
306 if( p_vpar->synchro.can_display_p )
309 if( p_vpar->synchro.displayable_p * i_delay
310 < p_vpar->synchro.i_mean_decode_time )
312 //fprintf( stderr, "trashed a P\n");
316 p_vpar->synchro.displayable_p--;
321 if( p_vpar->synchro.can_display_b )
324 /* modulo & 0x3 is here to add some randomness */
325 if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
326 * p_vpar->synchro.i_mean_decode_time )
328 //fprintf( stderr, "trashed a B\n");
332 if( p_vpar->synchro.displayable_b <= 0 )
335 p_vpar->synchro.displayable_b--;
343 /*****************************************************************************
344 * vpar_SynchroTrash : Update timers when we trash a picture
345 *****************************************************************************/
346 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
349 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
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,
359 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
361 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
362 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
365 p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
369 /*****************************************************************************
370 * vpar_SynchroEnd : Called when the image is totally decoded
371 *****************************************************************************/
372 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
374 mtime_t i_decode_time;
376 i_decode_time = (mdate() -
377 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
378 / ( (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start) & 0x0f);
380 p_vpar->synchro.i_mean_decode_time =
381 ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
384 "decoding time was %lli\n",
385 p_vpar->synchro.i_mean_decode_time); */
387 p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
391 /*****************************************************************************
392 * vpar_SynchroDate : When an image has been decoded, ask for its date
393 *****************************************************************************/
394 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
396 mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
399 static mtime_t i_delta = 0;
402 "displaying type %i with delay %lli and delta %lli\n",
403 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
404 i_displaydate - mdate(),
405 i_displaydate - i_delta );
408 "theorical fps: %f - actual fps: %f \n",
409 p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
411 i_delta = i_displaydate;
414 return i_displaydate;
421 /* synchro a deux balles backportee du decodeur de reference. NE MARCHE PAS
422 AVEC LES IMAGES MONOTRAMES */
424 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
427 switch (i_coding_type)
430 if ((p_vpar->synchro.kludge_level <= p_vpar->synchro.kludge_nbp))
432 p_vpar->synchro.kludge_b++;
435 if (p_vpar->synchro.kludge_b %
436 (p_vpar->synchro.kludge_nbb /
437 (p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp)))
439 p_vpar->synchro.kludge_b++;
442 p_vpar->synchro.kludge_b++;
446 if (p_vpar->synchro.kludge_p++ >= p_vpar->synchro.kludge_level)
457 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
460 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
462 p_vpar->synchro.kludge_nbframes = 0;
463 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
466 p_vpar->synchro.kludge_nbframes++;
467 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
470 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
473 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
475 p_vpar->synchro.kludge_nbframes = 0;
476 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
477 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
480 p_vpar->synchro.kludge_nbframes++;
483 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
485 return( p_vpar->synchro.kludge_date
486 + p_vpar->synchro.kludge_nbframes*1000000/(p_vpar->sequence.r_frame_rate ) );
489 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
493 void vpar_SynchroKludge( vpar_thread_t * p_vpar, mtime_t date )
496 int temp = p_vpar->synchro.kludge_level;
498 p_vpar->synchro.kludge_nbp = p_vpar->synchro.kludge_p ? p_vpar->synchro.kludge_p : 5;
499 p_vpar->synchro.kludge_nbb = p_vpar->synchro.kludge_b ? p_vpar->synchro.kludge_b : 6;
500 show_date = date - mdate();
501 p_vpar->synchro.kludge_p = 0;
502 p_vpar->synchro.kludge_b = 0;
504 if (show_date < (SYNC_DELAY - SYNC_TOLERATE) && show_date <= p_vpar->synchro.kludge_prevdate)
506 p_vpar->synchro.kludge_level--;
507 if (p_vpar->synchro.kludge_level < 0)
508 p_vpar->synchro.kludge_level = 0;
509 else if (p_vpar->synchro.kludge_level >
510 p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
511 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
513 if (temp != p_vpar->synchro.kludge_level)
514 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
515 temp, p_vpar->synchro.kludge_level, show_date );
518 else if (show_date > (SYNC_DELAY + SYNC_TOLERATE) && show_date >= p_vpar->synchro.kludge_prevdate)
520 p_vpar->synchro.kludge_level++;
521 if (p_vpar->synchro.kludge_level > p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
522 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
524 if (temp != p_vpar->synchro.kludge_level)
525 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
526 temp, p_vpar->synchro.kludge_level, show_date );
530 p_vpar->synchro.kludge_prevdate = show_date;
531 if ((p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp) > p_vpar->synchro.kludge_nbb)
532 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbb + p_vpar->synchro.kludge_nbp;
540 void vpar_SynchroSetCurrentDate( vpar_thread_t * p_vpar, int i_coding_type )
542 pes_packet_t * p_pes =
543 p_vpar->bit_stream.p_decoder_fifo->buffer[p_vpar->bit_stream.p_decoder_fifo->i_start];
546 switch( i_coding_type )
549 if( p_pes->b_has_pts )
551 if( p_pes->i_pts < p_vpar->synchro.i_current_frame_date )
553 intf_ErrMsg( "vpar warning: pts_date < current_date\n" );
555 p_vpar->synchro.i_current_frame_date = p_pes->i_pts;
556 p_pes->b_has_pts = 0;
560 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
566 if( p_vpar->synchro.i_backward_frame_date == 0 )
568 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
572 if( p_vpar->synchro.i_backward_frame_date < p_vpar->synchro.i_current_frame_date )
574 intf_ErrMsg( "vpar warning: backward_date < current_date (%Ld)\n",
575 p_vpar->synchro.i_backward_frame_date - p_vpar->synchro.i_current_frame_date );
577 p_vpar->synchro.i_current_frame_date = p_vpar->synchro.i_backward_frame_date;
578 p_vpar->synchro.i_backward_frame_date = 0;
581 if( p_pes->b_has_pts )
583 p_vpar->synchro.i_backward_frame_date = p_pes->i_pts;
584 p_pes->b_has_pts = 0;
590 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
593 boolean_t b_result = 1;
594 int i_synchro_level = p_vpar->p_vout->i_synchro_level;
596 vpar_SynchroSetCurrentDate( p_vpar, i_coding_type );
599 * The synchro level is updated by the video input (see SynchroLevelUpdate)
600 * so we just use the synchro_level to decide which frame to trash
603 switch( i_coding_type )
607 //fprintf( stderr, "p : %d (%d), b : %d (%d)\n", p_vpar->synchro.i_p_count, p_vpar->synchro.i_p_nb,
608 // p_vpar->synchro.i_b_count, p_vpar->synchro.i_b_nb );
610 p_vpar->synchro.r_p_average =
611 (p_vpar->synchro.r_p_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_p_count)/SYNC_AVERAGE_COUNT;
612 p_vpar->synchro.r_b_average =
613 (p_vpar->synchro.r_b_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_b_count)/SYNC_AVERAGE_COUNT;
615 p_vpar->synchro.i_p_nb = (int)(p_vpar->synchro.r_p_average+0.5);
616 p_vpar->synchro.i_b_nb = (int)(p_vpar->synchro.r_b_average+0.5);
618 p_vpar->synchro.i_p_count = p_vpar->synchro.i_b_count = 0;
619 p_vpar->synchro.i_b_trasher = p_vpar->synchro.i_b_nb / 2;
620 p_vpar->synchro.i_i_count++;
624 p_vpar->synchro.i_p_count++;
625 if( p_vpar->synchro.i_p_count > i_synchro_level )
632 p_vpar->synchro.i_b_count++;
633 if( p_vpar->synchro.i_p_nb >= i_synchro_level )
635 /* We must trash all the B */
640 /* We use the brensenham algorithm to decide which B to trash */
641 p_vpar->synchro.i_b_trasher +=
642 p_vpar->synchro.i_b_nb - (i_synchro_level-p_vpar->synchro.i_p_nb);
643 if( p_vpar->synchro.i_b_trasher >= p_vpar->synchro.i_b_nb )
646 p_vpar->synchro.i_b_trasher -= p_vpar->synchro.i_b_nb;
655 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
658 vpar_SynchroChoose( p_vpar, i_coding_type, i_structure );
661 void vpar_SynchroUpdateLevel()
663 //vlc_mutex_lock( &level_lock );
664 //vlc_mutex_unlock( &level_lock );
667 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
669 //fprintf( stderr, "delay : %Ld\n" , mdate() - p_vpar->synchro.i_current_frame_date );
670 return( p_vpar->synchro.i_current_frame_date );
673 /* functions with no use */
675 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
679 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,