1 /*****************************************************************************
2 * vpar_motion.c : motion vectors parsing
4 *****************************************************************************/
6 /*****************************************************************************
8 *****************************************************************************/
19 #include "vlc_thread.h"
22 #include "debug.h" /* ?? temporaire, requis par netlist.h */
25 #include "input_netlist.h"
26 #include "decoder_fifo.h"
28 #include "video_output.h"
30 #include "vdec_idct.h"
31 #include "video_decoder.h"
32 #include "vdec_motion.h"
34 #include "vpar_blocks.h"
35 #include "vpar_headers.h"
36 #include "vpar_synchro.h"
37 #include "video_parser.h"
38 #include "video_fifo.h"
47 /*****************************************************************************
48 * vpar_SynchroUpdateTab : Update a mean table in the synchro structure
49 *****************************************************************************/
50 float vpar_SynchroUpdateTab( video_synchro_tab_t * tab, int count )
53 tab->mean = ( tab->mean + MAX_COUNT * count ) / ( MAX_COUNT + 1 );
54 tab->deviation = ( tab->deviation + MAX_COUNT * abs (tab->mean - count) )
57 return tab->deviation;
60 /*****************************************************************************
61 * vpar_SynchroUpdateStructures : Update the synchro structures
62 *****************************************************************************/
63 void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
64 int i_coding_type, int dropped )
66 float candidate_deviation;
67 float optimal_deviation;
69 mtime_t i_current_pts;
71 mtime_t i_displaydate;
72 decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
74 /* interpolate the current _decode_ PTS */
75 i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
76 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
80 i_current_pts = p_vpar->synchro.i_last_decode_pts
81 + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
83 p_vpar->synchro.i_last_decode_pts = i_current_pts;
85 /* see if the current image has a pts - if not, set to 0 */
86 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
89 /* update display time */
90 i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
91 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
93 if( !i_displaydate || i_coding_type != I_CODING_TYPE )
95 if (!p_vpar->synchro.i_images_since_pts )
96 p_vpar->synchro.i_images_since_pts = 10;
98 i_displaydate = p_vpar->synchro.i_last_display_pts
99 + 1000000.0 / (p_vpar->synchro.theorical_fps);
100 //fprintf (stderr, " ");
103 decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts = 0;
105 /* else fprintf (stderr, "R ");
106 if (dropped) fprintf (stderr, " "); else fprintf (stderr, "* ");
107 fprintf (stderr, "%i ", i_coding_type);
108 fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts); */
110 p_vpar->synchro.i_images_since_pts--;
111 p_vpar->synchro.i_last_display_pts = i_displaydate;
115 /* update structures */
116 switch(i_coding_type)
120 p_vpar->synchro.current_p_count++;
121 if( !dropped ) p_vpar->synchro.nondropped_p_count++;
125 p_vpar->synchro.current_b_count++;
126 if( !dropped ) p_vpar->synchro.nondropped_b_count++;
131 /* update information about images we can decode */
132 if (i_current_pts != p_vpar->synchro.i_last_i_pts)
134 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
136 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;
138 p_vpar->synchro.i_last_i_pts = i_current_pts;
143 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
145 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;
151 /* update all the structures for P images */
154 optimal_deviation = vpar_SynchroUpdateTab(
155 &p_vpar->synchro.tab_p[0],
156 p_vpar->synchro.current_p_count);
157 predict = p_vpar->synchro.tab_p[0].mean;
160 candidate_deviation = vpar_SynchroUpdateTab(
161 &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
162 p_vpar->synchro.current_p_count);
163 if (candidate_deviation < optimal_deviation)
165 optimal_deviation = candidate_deviation;
166 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
170 candidate_deviation = vpar_SynchroUpdateTab(
171 &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
172 p_vpar->synchro.current_p_count);
173 if (candidate_deviation < optimal_deviation)
175 optimal_deviation = candidate_deviation;
176 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
179 p_vpar->synchro.p_count_predict = predict;
180 p_vpar->synchro.current_p_count = 0;
183 /* update all the structures for B images */
186 optimal_deviation = vpar_SynchroUpdateTab(
187 &p_vpar->synchro.tab_b[0],
188 p_vpar->synchro.current_b_count);
189 predict = p_vpar->synchro.tab_b[0].mean;
192 candidate_deviation = vpar_SynchroUpdateTab(
193 &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
194 p_vpar->synchro.current_b_count);
195 if (candidate_deviation < optimal_deviation)
197 optimal_deviation = candidate_deviation;
198 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
202 candidate_deviation = vpar_SynchroUpdateTab(
203 &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
204 p_vpar->synchro.current_b_count);
205 if (candidate_deviation < optimal_deviation)
207 optimal_deviation = candidate_deviation;
208 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
211 p_vpar->synchro.b_count_predict = predict;
212 p_vpar->synchro.current_b_count = 0;
214 /* now we calculated all statistics, it's time to
215 * decide what we have the time to display
217 i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
219 p_vpar->synchro.can_display_i
220 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
222 p_vpar->synchro.can_display_p
223 = ( p_vpar->synchro.i_mean_decode_time
224 * (1 + p_vpar->synchro.p_count_predict) < i_delay );
226 if( !p_vpar->synchro.can_display_p )
228 p_vpar->synchro.displayable_p
229 = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
230 if( p_vpar->synchro.displayable_p < 0 )
231 p_vpar->synchro.displayable_p = 0;
234 p_vpar->synchro.displayable_p = 0;
236 if( p_vpar->synchro.can_display_p
237 && !(p_vpar->synchro.can_display_b
238 = ( p_vpar->synchro.i_mean_decode_time
239 * (1 + p_vpar->synchro.b_count_predict
240 + p_vpar->synchro.p_count_predict)) < i_delay) )
242 p_vpar->synchro.displayable_b
243 = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
244 - p_vpar->synchro.can_display_p;
247 p_vpar->synchro.displayable_b = 0;
251 "I %i P %i (%f) B %i (%f)\n",
252 p_vpar->synchro.can_display_i,
253 p_vpar->synchro.can_display_p,
254 p_vpar->synchro.displayable_p,
255 p_vpar->synchro.can_display_b,
256 p_vpar->synchro.displayable_b );
259 /* update some values */
262 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
263 p_vpar->synchro.nondropped_p_count = 0;
264 p_vpar->synchro.nondropped_b_count = 0;
271 p_vpar->synchro.modulo++;
275 /*****************************************************************************
276 * vpar_SynchroChoose : Decide whether we will decode a picture or not
277 *****************************************************************************/
278 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
281 mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
283 switch( i_coding_type )
287 return( p_vpar->synchro.can_display_i );
291 if( p_vpar->synchro.can_display_p )
294 if( p_vpar->synchro.displayable_p * i_delay
295 < p_vpar->synchro.i_mean_decode_time )
297 //fprintf( stderr, "trashed a P\n");
301 p_vpar->synchro.displayable_p--;
306 if( p_vpar->synchro.can_display_b )
309 /* modulo & 0x3 is here to add some randomness */
310 if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
311 * p_vpar->synchro.i_mean_decode_time )
313 //fprintf( stderr, "trashed a B\n");
317 if( p_vpar->synchro.displayable_b <= 0 )
320 p_vpar->synchro.displayable_b--;
328 /*****************************************************************************
329 * vpar_SynchroTrash : Update timers when we trash a picture
330 *****************************************************************************/
331 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
334 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
338 /*****************************************************************************
339 * vpar_SynchroDecode : Update timers when we decide to decode a picture
340 *****************************************************************************/
341 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
344 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
346 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
347 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
350 p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
354 /*****************************************************************************
355 * vpar_SynchroEnd : Called when the image is totally decoded
356 *****************************************************************************/
357 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
359 mtime_t i_decode_time;
361 i_decode_time = (mdate() -
362 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
363 / ( (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start) & 0x0f);
365 p_vpar->synchro.i_mean_decode_time =
366 ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
369 "decoding time was %lli\n",
370 p_vpar->synchro.i_mean_decode_time); */
372 p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
376 /*****************************************************************************
377 * vpar_SynchroDate : When an image has been decoded, ask for its date
378 *****************************************************************************/
379 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
381 mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
384 static mtime_t i_delta = 0;
387 "displaying type %i with delay %lli and delta %lli\n",
388 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
389 i_displaydate - mdate(),
390 i_displaydate - i_delta );
393 "theorical fps: %f - actual fps: %f \n",
394 p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
396 i_delta = i_displaydate;
399 return i_displaydate;
406 /* synchro a deux balles backportee du decodeur de reference. NE MARCHE PAS
407 AVEC LES IMAGES MONOTRAMES */
409 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
412 switch (i_coding_type)
415 if ((p_vpar->synchro.kludge_level <= p_vpar->synchro.kludge_nbp))
417 p_vpar->synchro.kludge_b++;
420 if (p_vpar->synchro.kludge_b %
421 (p_vpar->synchro.kludge_nbb /
422 (p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp)))
424 p_vpar->synchro.kludge_b++;
427 p_vpar->synchro.kludge_b++;
431 if (p_vpar->synchro.kludge_p++ >= p_vpar->synchro.kludge_level)
442 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
445 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
447 p_vpar->synchro.kludge_nbframes = 0;
448 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
451 p_vpar->synchro.kludge_nbframes++;
452 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
455 void vpar_SynchroDecode( 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;
462 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
465 p_vpar->synchro.kludge_nbframes++;
468 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
470 return( p_vpar->synchro.kludge_date
471 + p_vpar->synchro.kludge_nbframes*1000000/(p_vpar->sequence.r_frame_rate ) );
474 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
478 void vpar_SynchroKludge( vpar_thread_t * p_vpar, mtime_t date )
481 int temp = p_vpar->synchro.kludge_level;
483 p_vpar->synchro.kludge_nbp = p_vpar->synchro.kludge_p ? p_vpar->synchro.kludge_p : 5;
484 p_vpar->synchro.kludge_nbb = p_vpar->synchro.kludge_b ? p_vpar->synchro.kludge_b : 6;
485 show_date = date - mdate();
486 p_vpar->synchro.kludge_p = 0;
487 p_vpar->synchro.kludge_b = 0;
489 if (show_date < (SYNC_DELAY - SYNC_TOLERATE) && show_date <= p_vpar->synchro.kludge_prevdate)
491 p_vpar->synchro.kludge_level--;
492 if (p_vpar->synchro.kludge_level < 0)
493 p_vpar->synchro.kludge_level = 0;
494 else if (p_vpar->synchro.kludge_level >
495 p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
496 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
498 if (temp != p_vpar->synchro.kludge_level)
499 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
500 temp, p_vpar->synchro.kludge_level, show_date );
503 else 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 > p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
507 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
509 if (temp != p_vpar->synchro.kludge_level)
510 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
511 temp, p_vpar->synchro.kludge_level, show_date );
515 p_vpar->synchro.kludge_prevdate = show_date;
516 if ((p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp) > p_vpar->synchro.kludge_nbb)
517 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbb + p_vpar->synchro.kludge_nbp;
525 void vpar_SynchroSetCurrentDate( vpar_thread_t * p_vpar, int i_coding_type )
527 pes_packet_t * p_pes =
528 p_vpar->bit_stream.p_decoder_fifo->buffer[p_vpar->bit_stream.p_decoder_fifo->i_start];
531 switch( i_coding_type )
534 if( p_pes->b_has_pts )
536 if( p_pes->i_pts < p_vpar->synchro.i_current_frame_date )
538 fprintf( stderr, "vpar warning: pts_date < current_date\n" );
540 p_vpar->synchro.i_current_frame_date = p_pes->i_pts;
541 p_pes->b_has_pts = 0;
545 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
551 if( p_vpar->synchro.i_backward_frame_date == 0 )
553 p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
557 if( p_vpar->synchro.i_backward_frame_date < p_vpar->synchro.i_current_frame_date )
559 fprintf( stderr, "vpar warning: backward_date < current_date (%Ld)\n",
560 p_vpar->synchro.i_backward_frame_date - p_vpar->synchro.i_current_frame_date );
562 p_vpar->synchro.i_current_frame_date = p_vpar->synchro.i_backward_frame_date;
563 p_vpar->synchro.i_backward_frame_date = 0;
566 if( p_pes->b_has_pts )
568 p_vpar->synchro.i_backward_frame_date = p_pes->i_pts;
569 p_pes->b_has_pts = 0;
575 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
578 boolean_t b_result = 1;
579 int i_synchro_level = p_vpar->p_vout->i_synchro_level;
581 vpar_SynchroSetCurrentDate( p_vpar, i_coding_type );
584 * The synchro level is updated by the video input (see SynchroLevelUpdate)
585 * so we just use the synchro_level to decide which frame to trash
588 switch( i_coding_type )
592 //fprintf( stderr, "p : %d (%d), b : %d (%d)\n", p_vpar->synchro.i_p_count, p_vpar->synchro.i_p_nb,
593 // p_vpar->synchro.i_b_count, p_vpar->synchro.i_b_nb );
595 p_vpar->synchro.r_p_average =
596 (p_vpar->synchro.r_p_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_p_count)/SYNC_AVERAGE_COUNT;
597 p_vpar->synchro.r_b_average =
598 (p_vpar->synchro.r_b_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_b_count)/SYNC_AVERAGE_COUNT;
600 p_vpar->synchro.i_p_nb = (int)(p_vpar->synchro.r_p_average+0.5);
601 p_vpar->synchro.i_b_nb = (int)(p_vpar->synchro.r_b_average+0.5);
603 p_vpar->synchro.i_p_count = p_vpar->synchro.i_b_count = 0;
604 p_vpar->synchro.i_b_trasher = p_vpar->synchro.i_b_nb / 2;
605 p_vpar->synchro.i_i_count++;
609 p_vpar->synchro.i_p_count++;
610 if( p_vpar->synchro.i_p_count > i_synchro_level )
617 p_vpar->synchro.i_b_count++;
618 if( p_vpar->synchro.i_p_nb >= i_synchro_level )
620 /* We must trash all the B */
625 /* We use the brensenham algorithm to decide which B to trash */
626 p_vpar->synchro.i_b_trasher +=
627 p_vpar->synchro.i_b_nb - (i_synchro_level-p_vpar->synchro.i_p_nb);
628 if( p_vpar->synchro.i_b_trasher >= p_vpar->synchro.i_b_nb )
631 p_vpar->synchro.i_b_trasher -= p_vpar->synchro.i_b_nb;
640 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
643 vpar_SynchroChoose( p_vpar, i_coding_type, i_structure );
646 void vpar_SynchroUpdateLevel()
648 //vlc_mutex_lock( &level_lock );
649 //vlc_mutex_unlock( &level_lock );
652 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
654 //fprintf( stderr, "delay : %Ld\n" , mdate() - p_vpar->synchro.i_current_frame_date );
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,