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);
116 decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts = 0;
118 p_vpar->synchro.i_images_since_pts--;
119 p_vpar->synchro.i_last_display_pts = i_displaydate;
123 /* update structures */
124 switch(i_coding_type)
128 p_vpar->synchro.current_p_count++;
129 if( !dropped ) p_vpar->synchro.nondropped_p_count++;
133 p_vpar->synchro.current_b_count++;
134 if( !dropped ) p_vpar->synchro.nondropped_b_count++;
139 /* update information about images we can decode */
140 if (i_current_pts != p_vpar->synchro.i_last_i_pts)
142 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
144 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;
146 p_vpar->synchro.i_last_i_pts = i_current_pts;
151 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
153 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;
159 /* update all the structures for P images */
162 optimal_deviation = vpar_SynchroUpdateTab(
163 &p_vpar->synchro.tab_p[0],
164 p_vpar->synchro.current_p_count);
165 predict = p_vpar->synchro.tab_p[0].mean;
168 candidate_deviation = vpar_SynchroUpdateTab(
169 &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
170 p_vpar->synchro.current_p_count);
171 if (candidate_deviation < optimal_deviation)
173 optimal_deviation = candidate_deviation;
174 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
178 candidate_deviation = vpar_SynchroUpdateTab(
179 &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
180 p_vpar->synchro.current_p_count);
181 if (candidate_deviation < optimal_deviation)
183 optimal_deviation = candidate_deviation;
184 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
187 p_vpar->synchro.p_count_predict = predict;
188 p_vpar->synchro.current_p_count = 0;
191 /* update all the structures for B images */
194 optimal_deviation = vpar_SynchroUpdateTab(
195 &p_vpar->synchro.tab_b[0],
196 p_vpar->synchro.current_b_count);
197 predict = p_vpar->synchro.tab_b[0].mean;
200 candidate_deviation = vpar_SynchroUpdateTab(
201 &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
202 p_vpar->synchro.current_b_count);
203 if (candidate_deviation < optimal_deviation)
205 optimal_deviation = candidate_deviation;
206 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
210 candidate_deviation = vpar_SynchroUpdateTab(
211 &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
212 p_vpar->synchro.current_b_count);
213 if (candidate_deviation < optimal_deviation)
215 optimal_deviation = candidate_deviation;
216 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
219 p_vpar->synchro.b_count_predict = predict;
220 p_vpar->synchro.current_b_count = 0;
222 /* now we calculated all statistics, it's time to
223 * decide what we have the time to display
225 i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
227 p_vpar->synchro.can_display_i
228 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
230 p_vpar->synchro.can_display_p
231 = ( p_vpar->synchro.i_mean_decode_time
232 * (1 + p_vpar->synchro.p_count_predict) < i_delay );
234 if( !p_vpar->synchro.can_display_p )
236 p_vpar->synchro.displayable_p
237 = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
238 if( p_vpar->synchro.displayable_p < 0 )
239 p_vpar->synchro.displayable_p = 0;
242 p_vpar->synchro.displayable_p = 0;
244 if( p_vpar->synchro.can_display_p
245 && !(p_vpar->synchro.can_display_b
246 = ( p_vpar->synchro.i_mean_decode_time
247 * (1 + p_vpar->synchro.b_count_predict
248 + p_vpar->synchro.p_count_predict)) < i_delay) )
250 p_vpar->synchro.displayable_b
251 = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
252 - p_vpar->synchro.can_display_p;
255 p_vpar->synchro.displayable_b = 0;
258 intf_DbgMsg( "I %i P %i (%f) B %i (%f)\n",
259 p_vpar->synchro.can_display_i,
260 p_vpar->synchro.can_display_p,
261 p_vpar->synchro.displayable_p,
262 p_vpar->synchro.can_display_b,
263 p_vpar->synchro.displayable_b );
266 /* update some values */
269 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
270 p_vpar->synchro.nondropped_p_count = 0;
271 p_vpar->synchro.nondropped_b_count = 0;
278 p_vpar->synchro.modulo++;
282 /*****************************************************************************
283 * vpar_SynchroChoose : Decide whether we will decode a picture or not
284 *****************************************************************************/
285 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
288 mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
290 switch( i_coding_type )
294 return( p_vpar->synchro.can_display_i );
298 if( p_vpar->synchro.can_display_p )
301 if( p_vpar->synchro.displayable_p * i_delay
302 < p_vpar->synchro.i_mean_decode_time )
304 //intf_ErrMsg( "trashed a P\n" );
308 p_vpar->synchro.displayable_p--;
313 if( p_vpar->synchro.can_display_b )
316 /* modulo & 0x3 is here to add some randomness */
317 if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
318 * p_vpar->synchro.i_mean_decode_time )
320 //intf_ErrMsg( "trashed a B\n" );
324 if( p_vpar->synchro.displayable_b <= 0 )
327 p_vpar->synchro.displayable_b--;
335 /*****************************************************************************
336 * vpar_SynchroTrash : Update timers when we trash a picture
337 *****************************************************************************/
338 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
341 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
345 /*****************************************************************************
346 * vpar_SynchroDecode : Update timers when we decide to decode a picture
347 *****************************************************************************/
348 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
351 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
353 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
354 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
357 p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
361 /*****************************************************************************
362 * vpar_SynchroEnd : Called when the image is totally decoded
363 *****************************************************************************/
364 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
366 mtime_t i_decode_time;
368 i_decode_time = (mdate() -
369 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
370 / ( (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start) & 0x0f);
372 p_vpar->synchro.i_mean_decode_time =
373 ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
375 /* intf_ErrMsg( "decoding time was %lli\n",
376 p_vpar->synchro.i_mean_decode_time ); */
378 p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
382 /*****************************************************************************
383 * vpar_SynchroDate : When an image has been decoded, ask for its date
384 *****************************************************************************/
385 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
387 mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
390 static mtime_t i_delta = 0;
392 intf_ErrMsg( "displaying type %i with delay %lli and delta %lli\n",
393 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
394 i_displaydate - mdate(),
395 i_displaydate - i_delta );
397 intf_ErrMsg ( "theorical fps: %f - actual fps: %f \n",
398 p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
400 i_delta = i_displaydate;
403 return i_displaydate;
410 /* synchro a deux balles backportee du decodeur de reference. NE MARCHE PAS
411 AVEC LES IMAGES MONOTRAMES */
413 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
416 switch (i_coding_type)
419 if ((p_vpar->synchro.kludge_level <= p_vpar->synchro.kludge_nbp))
421 p_vpar->synchro.kludge_b++;
424 if (p_vpar->synchro.kludge_b %
425 (p_vpar->synchro.kludge_nbb /
426 (p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp)))
428 p_vpar->synchro.kludge_b++;
431 p_vpar->synchro.kludge_b++;
435 if (p_vpar->synchro.kludge_p++ >= p_vpar->synchro.kludge_level)
446 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
449 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
451 p_vpar->synchro.kludge_nbframes = 0;
452 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
455 p_vpar->synchro.kludge_nbframes++;
456 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
459 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
462 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
464 p_vpar->synchro.kludge_nbframes = 0;
465 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
466 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
469 p_vpar->synchro.kludge_nbframes++;
472 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
474 return( p_vpar->synchro.kludge_date
475 + p_vpar->synchro.kludge_nbframes*1000000/(p_vpar->sequence.r_frame_rate ) );
478 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
482 void vpar_SynchroKludge( vpar_thread_t * p_vpar, mtime_t date )
485 int temp = p_vpar->synchro.kludge_level;
487 p_vpar->synchro.kludge_nbp = p_vpar->synchro.kludge_p ? p_vpar->synchro.kludge_p : 5;
488 p_vpar->synchro.kludge_nbb = p_vpar->synchro.kludge_b ? p_vpar->synchro.kludge_b : 6;
489 show_date = date - mdate();
490 p_vpar->synchro.kludge_p = 0;
491 p_vpar->synchro.kludge_b = 0;
493 if (show_date < (SYNC_DELAY - SYNC_TOLERATE) && show_date <= p_vpar->synchro.kludge_prevdate)
495 p_vpar->synchro.kludge_level--;
496 if (p_vpar->synchro.kludge_level < 0)
497 p_vpar->synchro.kludge_level = 0;
498 else if (p_vpar->synchro.kludge_level >
499 p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
500 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
502 if (temp != p_vpar->synchro.kludge_level)
503 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
504 temp, p_vpar->synchro.kludge_level, show_date );
507 else if (show_date > (SYNC_DELAY + SYNC_TOLERATE) && show_date >= p_vpar->synchro.kludge_prevdate)
509 p_vpar->synchro.kludge_level++;
510 if (p_vpar->synchro.kludge_level > 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 );
519 p_vpar->synchro.kludge_prevdate = show_date;
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_nbb + p_vpar->synchro.kludge_nbp;
529 void vpar_SynchroSetCurrentDate( vpar_thread_t * p_vpar, int i_coding_type )
531 pes_packet_t * p_pes =
532 p_vpar->bit_stream.p_decoder_fifo->buffer[p_vpar->bit_stream.p_decoder_fifo->i_start];
535 switch( i_coding_type )
538 if( p_pes->b_has_pts )
540 if( p_pes->i_pts < p_vpar->synchro.i_current_frame_date )
542 intf_ErrMsg( "vpar warning: pts_date < current_date\n" );
544 p_vpar->synchro.i_current_frame_date = p_pes->i_pts;
545 p_pes->b_has_pts = 0;
549 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
555 if( p_vpar->synchro.i_backward_frame_date == 0 )
557 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
561 if( p_vpar->synchro.i_backward_frame_date < p_vpar->synchro.i_current_frame_date )
563 intf_ErrMsg( "vpar warning: backward_date < current_date (%Ld)\n",
564 p_vpar->synchro.i_backward_frame_date - p_vpar->synchro.i_current_frame_date );
566 p_vpar->synchro.i_current_frame_date = p_vpar->synchro.i_backward_frame_date;
567 p_vpar->synchro.i_backward_frame_date = 0;
570 if( p_pes->b_has_pts )
572 p_vpar->synchro.i_backward_frame_date = p_pes->i_pts;
573 p_pes->b_has_pts = 0;
579 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
582 boolean_t b_result = 1;
583 int i_synchro_level = p_vpar->p_vout->i_synchro_level;
585 vpar_SynchroSetCurrentDate( p_vpar, i_coding_type );
588 * The synchro level is updated by the video input (see SynchroLevelUpdate)
589 * so we just use the synchro_level to decide which frame to trash
592 switch( i_coding_type )
596 p_vpar->synchro.r_p_average =
597 (p_vpar->synchro.r_p_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_p_count)/SYNC_AVERAGE_COUNT;
598 p_vpar->synchro.r_b_average =
599 (p_vpar->synchro.r_b_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_b_count)/SYNC_AVERAGE_COUNT;
601 p_vpar->synchro.i_p_nb = (int)(p_vpar->synchro.r_p_average+0.5);
602 p_vpar->synchro.i_b_nb = (int)(p_vpar->synchro.r_b_average+0.5);
604 p_vpar->synchro.i_p_count = p_vpar->synchro.i_b_count = 0;
605 p_vpar->synchro.i_b_trasher = p_vpar->synchro.i_b_nb / 2;
606 p_vpar->synchro.i_i_count++;
610 p_vpar->synchro.i_p_count++;
611 if( p_vpar->synchro.i_p_count > i_synchro_level )
618 p_vpar->synchro.i_b_count++;
619 if( p_vpar->synchro.i_p_nb >= i_synchro_level )
621 /* We must trash all the B */
626 /* We use the brensenham algorithm to decide which B to trash */
627 p_vpar->synchro.i_b_trasher +=
628 p_vpar->synchro.i_b_nb - (i_synchro_level-p_vpar->synchro.i_p_nb);
629 if( p_vpar->synchro.i_b_trasher >= p_vpar->synchro.i_b_nb )
632 p_vpar->synchro.i_b_trasher -= p_vpar->synchro.i_b_nb;
641 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
644 vpar_SynchroChoose( p_vpar, i_coding_type, i_structure );
647 void vpar_SynchroUpdateLevel()
649 //vlc_mutex_lock( &level_lock );
650 //vlc_mutex_unlock( &level_lock );
653 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
655 return( p_vpar->synchro.i_current_frame_date );
658 /* functions with no use */
660 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
664 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,