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" */
39 #include "decoder_fifo.h"
41 #include "video_output.h"
43 #include "vdec_idct.h"
44 #include "video_decoder.h"
45 #include "vdec_motion.h"
47 #include "vpar_blocks.h"
48 #include "vpar_headers.h"
49 #include "vpar_synchro.h"
50 #include "video_parser.h"
59 /*****************************************************************************
60 * vpar_SynchroUpdateTab : Update a mean table in the synchro structure
61 *****************************************************************************/
62 float vpar_SynchroUpdateTab( video_synchro_tab_t * tab, int count )
65 tab->mean = ( tab->mean + MAX_COUNT * count ) / ( MAX_COUNT + 1 );
66 tab->deviation = ( tab->deviation + MAX_COUNT * abs (tab->mean - count) )
69 return tab->deviation;
72 /*****************************************************************************
73 * vpar_SynchroUpdateStructures : Update the synchro structures
74 *****************************************************************************/
75 void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
76 int i_coding_type, int dropped )
78 float candidate_deviation;
79 float optimal_deviation;
81 mtime_t i_current_pts;
83 mtime_t i_displaydate;
84 decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
86 /* interpolate the current _decode_ PTS */
87 i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
88 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
92 i_current_pts = p_vpar->synchro.i_last_decode_pts
93 + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
95 p_vpar->synchro.i_last_decode_pts = i_current_pts;
97 /* see if the current image has a pts - if not, set to 0 */
98 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
101 /* update display time */
102 i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
103 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
105 if( !i_displaydate || i_coding_type != I_CODING_TYPE )
107 if (!p_vpar->synchro.i_images_since_pts )
108 p_vpar->synchro.i_images_since_pts = 10;
110 i_displaydate = p_vpar->synchro.i_last_display_pts
111 + 1000000.0 / (p_vpar->synchro.theorical_fps);
112 //fprintf (stderr, " ");
115 decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts = 0;
117 /* else fprintf (stderr, "R ");
118 if (dropped) fprintf (stderr, " "); else fprintf (stderr, "* ");
119 fprintf (stderr, "%i ", i_coding_type);
120 fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts); */
122 p_vpar->synchro.i_images_since_pts--;
123 p_vpar->synchro.i_last_display_pts = i_displaydate;
127 /* update structures */
128 switch(i_coding_type)
132 p_vpar->synchro.current_p_count++;
133 if( !dropped ) p_vpar->synchro.nondropped_p_count++;
137 p_vpar->synchro.current_b_count++;
138 if( !dropped ) p_vpar->synchro.nondropped_b_count++;
143 /* update information about images we can decode */
144 if (i_current_pts != p_vpar->synchro.i_last_i_pts)
146 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
148 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;
150 p_vpar->synchro.i_last_i_pts = i_current_pts;
155 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
157 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;
163 /* update all the structures for P images */
166 optimal_deviation = vpar_SynchroUpdateTab(
167 &p_vpar->synchro.tab_p[0],
168 p_vpar->synchro.current_p_count);
169 predict = p_vpar->synchro.tab_p[0].mean;
172 candidate_deviation = vpar_SynchroUpdateTab(
173 &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
174 p_vpar->synchro.current_p_count);
175 if (candidate_deviation < optimal_deviation)
177 optimal_deviation = candidate_deviation;
178 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
182 candidate_deviation = vpar_SynchroUpdateTab(
183 &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
184 p_vpar->synchro.current_p_count);
185 if (candidate_deviation < optimal_deviation)
187 optimal_deviation = candidate_deviation;
188 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
191 p_vpar->synchro.p_count_predict = predict;
192 p_vpar->synchro.current_p_count = 0;
195 /* update all the structures for B images */
198 optimal_deviation = vpar_SynchroUpdateTab(
199 &p_vpar->synchro.tab_b[0],
200 p_vpar->synchro.current_b_count);
201 predict = p_vpar->synchro.tab_b[0].mean;
204 candidate_deviation = vpar_SynchroUpdateTab(
205 &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
206 p_vpar->synchro.current_b_count);
207 if (candidate_deviation < optimal_deviation)
209 optimal_deviation = candidate_deviation;
210 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
214 candidate_deviation = vpar_SynchroUpdateTab(
215 &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
216 p_vpar->synchro.current_b_count);
217 if (candidate_deviation < optimal_deviation)
219 optimal_deviation = candidate_deviation;
220 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
223 p_vpar->synchro.b_count_predict = predict;
224 p_vpar->synchro.current_b_count = 0;
226 /* now we calculated all statistics, it's time to
227 * decide what we have the time to display
229 i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
231 p_vpar->synchro.can_display_i
232 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
234 p_vpar->synchro.can_display_p
235 = ( p_vpar->synchro.i_mean_decode_time
236 * (1 + p_vpar->synchro.p_count_predict) < i_delay );
238 if( !p_vpar->synchro.can_display_p )
240 p_vpar->synchro.displayable_p
241 = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
242 if( p_vpar->synchro.displayable_p < 0 )
243 p_vpar->synchro.displayable_p = 0;
246 p_vpar->synchro.displayable_p = 0;
248 if( p_vpar->synchro.can_display_p
249 && !(p_vpar->synchro.can_display_b
250 = ( p_vpar->synchro.i_mean_decode_time
251 * (1 + p_vpar->synchro.b_count_predict
252 + p_vpar->synchro.p_count_predict)) < i_delay) )
254 p_vpar->synchro.displayable_b
255 = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
256 - p_vpar->synchro.can_display_p;
259 p_vpar->synchro.displayable_b = 0;
263 "I %i P %i (%f) B %i (%f)\n",
264 p_vpar->synchro.can_display_i,
265 p_vpar->synchro.can_display_p,
266 p_vpar->synchro.displayable_p,
267 p_vpar->synchro.can_display_b,
268 p_vpar->synchro.displayable_b );
271 /* update some values */
274 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
275 p_vpar->synchro.nondropped_p_count = 0;
276 p_vpar->synchro.nondropped_b_count = 0;
283 p_vpar->synchro.modulo++;
287 /*****************************************************************************
288 * vpar_SynchroChoose : Decide whether we will decode a picture or not
289 *****************************************************************************/
290 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
293 mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
295 switch( i_coding_type )
299 return( p_vpar->synchro.can_display_i );
303 if( p_vpar->synchro.can_display_p )
306 if( p_vpar->synchro.displayable_p * i_delay
307 < p_vpar->synchro.i_mean_decode_time )
309 //fprintf( stderr, "trashed a P\n");
313 p_vpar->synchro.displayable_p--;
318 if( p_vpar->synchro.can_display_b )
321 /* modulo & 0x3 is here to add some randomness */
322 if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
323 * p_vpar->synchro.i_mean_decode_time )
325 //fprintf( stderr, "trashed a B\n");
329 if( p_vpar->synchro.displayable_b <= 0 )
332 p_vpar->synchro.displayable_b--;
340 /*****************************************************************************
341 * vpar_SynchroTrash : Update timers when we trash a picture
342 *****************************************************************************/
343 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
346 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
350 /*****************************************************************************
351 * vpar_SynchroDecode : Update timers when we decide to decode a picture
352 *****************************************************************************/
353 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
356 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
358 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
359 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
362 p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
366 /*****************************************************************************
367 * vpar_SynchroEnd : Called when the image is totally decoded
368 *****************************************************************************/
369 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
371 mtime_t i_decode_time;
373 i_decode_time = (mdate() -
374 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
375 / ( (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start) & 0x0f);
377 p_vpar->synchro.i_mean_decode_time =
378 ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
381 "decoding time was %lli\n",
382 p_vpar->synchro.i_mean_decode_time); */
384 p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
388 /*****************************************************************************
389 * vpar_SynchroDate : When an image has been decoded, ask for its date
390 *****************************************************************************/
391 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
393 mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
396 static mtime_t i_delta = 0;
399 "displaying type %i with delay %lli and delta %lli\n",
400 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
401 i_displaydate - mdate(),
402 i_displaydate - i_delta );
405 "theorical fps: %f - actual fps: %f \n",
406 p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
408 i_delta = i_displaydate;
411 return i_displaydate;
418 /* synchro a deux balles backportee du decodeur de reference. NE MARCHE PAS
419 AVEC LES IMAGES MONOTRAMES */
421 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
424 switch (i_coding_type)
427 if ((p_vpar->synchro.kludge_level <= p_vpar->synchro.kludge_nbp))
429 p_vpar->synchro.kludge_b++;
432 if (p_vpar->synchro.kludge_b %
433 (p_vpar->synchro.kludge_nbb /
434 (p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp)))
436 p_vpar->synchro.kludge_b++;
439 p_vpar->synchro.kludge_b++;
443 if (p_vpar->synchro.kludge_p++ >= p_vpar->synchro.kludge_level)
454 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
457 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
459 p_vpar->synchro.kludge_nbframes = 0;
460 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
463 p_vpar->synchro.kludge_nbframes++;
464 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
467 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
470 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
472 p_vpar->synchro.kludge_nbframes = 0;
473 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
474 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
477 p_vpar->synchro.kludge_nbframes++;
480 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
482 return( p_vpar->synchro.kludge_date
483 + p_vpar->synchro.kludge_nbframes*1000000/(p_vpar->sequence.r_frame_rate ) );
486 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
490 void vpar_SynchroKludge( vpar_thread_t * p_vpar, mtime_t date )
493 int temp = p_vpar->synchro.kludge_level;
495 p_vpar->synchro.kludge_nbp = p_vpar->synchro.kludge_p ? p_vpar->synchro.kludge_p : 5;
496 p_vpar->synchro.kludge_nbb = p_vpar->synchro.kludge_b ? p_vpar->synchro.kludge_b : 6;
497 show_date = date - mdate();
498 p_vpar->synchro.kludge_p = 0;
499 p_vpar->synchro.kludge_b = 0;
501 if (show_date < (SYNC_DELAY - SYNC_TOLERATE) && show_date <= p_vpar->synchro.kludge_prevdate)
503 p_vpar->synchro.kludge_level--;
504 if (p_vpar->synchro.kludge_level < 0)
505 p_vpar->synchro.kludge_level = 0;
506 else if (p_vpar->synchro.kludge_level >
507 p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
508 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
510 if (temp != p_vpar->synchro.kludge_level)
511 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
512 temp, p_vpar->synchro.kludge_level, show_date );
515 else if (show_date > (SYNC_DELAY + SYNC_TOLERATE) && show_date >= p_vpar->synchro.kludge_prevdate)
517 p_vpar->synchro.kludge_level++;
518 if (p_vpar->synchro.kludge_level > p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
519 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
521 if (temp != p_vpar->synchro.kludge_level)
522 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
523 temp, p_vpar->synchro.kludge_level, show_date );
527 p_vpar->synchro.kludge_prevdate = show_date;
528 if ((p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp) > p_vpar->synchro.kludge_nbb)
529 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbb + p_vpar->synchro.kludge_nbp;
537 void vpar_SynchroSetCurrentDate( vpar_thread_t * p_vpar, int i_coding_type )
539 pes_packet_t * p_pes =
540 p_vpar->bit_stream.p_decoder_fifo->buffer[p_vpar->bit_stream.p_decoder_fifo->i_start];
543 switch( i_coding_type )
546 if( p_pes->b_has_pts )
548 if( p_pes->i_pts < p_vpar->synchro.i_current_frame_date )
550 intf_ErrMsg( "vpar warning: pts_date < current_date\n" );
552 p_vpar->synchro.i_current_frame_date = p_pes->i_pts;
553 p_pes->b_has_pts = 0;
557 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
563 if( p_vpar->synchro.i_backward_frame_date == 0 )
565 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
569 if( p_vpar->synchro.i_backward_frame_date < p_vpar->synchro.i_current_frame_date )
571 intf_ErrMsg( "vpar warning: backward_date < current_date (%Ld)\n",
572 p_vpar->synchro.i_backward_frame_date - p_vpar->synchro.i_current_frame_date );
574 p_vpar->synchro.i_current_frame_date = p_vpar->synchro.i_backward_frame_date;
575 p_vpar->synchro.i_backward_frame_date = 0;
578 if( p_pes->b_has_pts )
580 p_vpar->synchro.i_backward_frame_date = p_pes->i_pts;
581 p_pes->b_has_pts = 0;
587 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
590 boolean_t b_result = 1;
591 int i_synchro_level = p_vpar->p_vout->i_synchro_level;
593 vpar_SynchroSetCurrentDate( p_vpar, i_coding_type );
596 * The synchro level is updated by the video input (see SynchroLevelUpdate)
597 * so we just use the synchro_level to decide which frame to trash
600 switch( i_coding_type )
604 //fprintf( stderr, "p : %d (%d), b : %d (%d)\n", p_vpar->synchro.i_p_count, p_vpar->synchro.i_p_nb,
605 // p_vpar->synchro.i_b_count, p_vpar->synchro.i_b_nb );
607 p_vpar->synchro.r_p_average =
608 (p_vpar->synchro.r_p_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_p_count)/SYNC_AVERAGE_COUNT;
609 p_vpar->synchro.r_b_average =
610 (p_vpar->synchro.r_b_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_b_count)/SYNC_AVERAGE_COUNT;
612 p_vpar->synchro.i_p_nb = (int)(p_vpar->synchro.r_p_average+0.5);
613 p_vpar->synchro.i_b_nb = (int)(p_vpar->synchro.r_b_average+0.5);
615 p_vpar->synchro.i_p_count = p_vpar->synchro.i_b_count = 0;
616 p_vpar->synchro.i_b_trasher = p_vpar->synchro.i_b_nb / 2;
617 p_vpar->synchro.i_i_count++;
621 p_vpar->synchro.i_p_count++;
622 if( p_vpar->synchro.i_p_count > i_synchro_level )
629 p_vpar->synchro.i_b_count++;
630 if( p_vpar->synchro.i_p_nb >= i_synchro_level )
632 /* We must trash all the B */
637 /* We use the brensenham algorithm to decide which B to trash */
638 p_vpar->synchro.i_b_trasher +=
639 p_vpar->synchro.i_b_nb - (i_synchro_level-p_vpar->synchro.i_p_nb);
640 if( p_vpar->synchro.i_b_trasher >= p_vpar->synchro.i_b_nb )
643 p_vpar->synchro.i_b_trasher -= p_vpar->synchro.i_b_nb;
652 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
655 vpar_SynchroChoose( p_vpar, i_coding_type, i_structure );
658 void vpar_SynchroUpdateLevel()
660 //vlc_mutex_lock( &level_lock );
661 //vlc_mutex_unlock( &level_lock );
664 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
666 //fprintf( stderr, "delay : %Ld\n" , mdate() - p_vpar->synchro.i_current_frame_date );
667 return( p_vpar->synchro.i_current_frame_date );
670 /* functions with no use */
672 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
676 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,