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;
404 /* synchro a deux balles backportee du decodeur de reference. NE MARCHE PAS
405 AVEC LES IMAGES MONOTRAMES */
407 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
410 switch (i_coding_type)
413 if ((p_vpar->synchro.kludge_level <= p_vpar->synchro.kludge_nbp))
415 p_vpar->synchro.kludge_b++;
418 if (p_vpar->synchro.kludge_b %
419 (p_vpar->synchro.kludge_nbb /
420 (p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp)))
422 p_vpar->synchro.kludge_b++;
425 p_vpar->synchro.kludge_b++;
429 if (p_vpar->synchro.kludge_p++ >= p_vpar->synchro.kludge_level)
440 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
443 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
445 p_vpar->synchro.kludge_nbframes = 0;
446 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
447 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
450 p_vpar->synchro.kludge_nbframes++;
453 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
456 if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
458 p_vpar->synchro.kludge_nbframes = 0;
459 p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
460 DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
463 p_vpar->synchro.kludge_nbframes++;
466 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
468 return( p_vpar->synchro.kludge_date
469 + p_vpar->synchro.kludge_nbframes*1000000/(p_vpar->sequence.r_frame_rate ) );
472 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
476 void vpar_SynchroKludge( vpar_thread_t * p_vpar, mtime_t date )
479 int temp = p_vpar->synchro.kludge_level;
481 p_vpar->synchro.kludge_nbp = p_vpar->synchro.kludge_p ? p_vpar->synchro.kludge_p : 5;
482 p_vpar->synchro.kludge_nbb = p_vpar->synchro.kludge_b ? p_vpar->synchro.kludge_b : 6;
483 show_date = date - mdate();
484 p_vpar->synchro.kludge_p = 0;
485 p_vpar->synchro.kludge_b = 0;
487 if (show_date < (SYNC_DELAY - SYNC_TOLERATE) && show_date <= p_vpar->synchro.kludge_prevdate)
489 p_vpar->synchro.kludge_level--;
490 if (p_vpar->synchro.kludge_level < 0)
491 p_vpar->synchro.kludge_level = 0;
492 else if (p_vpar->synchro.kludge_level >
493 p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
494 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
496 if (temp != p_vpar->synchro.kludge_level)
497 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
498 temp, p_vpar->synchro.kludge_level, show_date );
501 else 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 > p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
505 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
507 if (temp != p_vpar->synchro.kludge_level)
508 intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
509 temp, p_vpar->synchro.kludge_level, show_date );
513 p_vpar->synchro.kludge_prevdate = show_date;
514 if ((p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp) > p_vpar->synchro.kludge_nbb)
515 p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbb + p_vpar->synchro.kludge_nbp;