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"
46 /*****************************************************************************
47 * vpar_SynchroUpdateTab : Update a mean table in the synchro structure
48 *****************************************************************************/
49 float vpar_SynchroUpdateTab( video_synchro_tab_t * tab, int count )
52 tab->mean = ( tab->mean + MAX_COUNT * count ) / ( MAX_COUNT + 1 );
53 tab->deviation = ( tab->deviation + MAX_COUNT * abs (tab->mean - count) )
56 return tab->deviation;
59 /*****************************************************************************
60 * vpar_SynchroUpdateStructures : Update the synchro structures
61 *****************************************************************************/
62 void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
63 int i_coding_type, int dropped )
65 float candidate_deviation;
66 float optimal_deviation;
68 mtime_t i_current_pts;
70 mtime_t i_displaydate;
71 decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
73 /* interpolate the current _decode_ PTS */
74 i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
75 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
79 i_current_pts = p_vpar->synchro.i_last_decode_pts
80 + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
82 p_vpar->synchro.i_last_decode_pts = i_current_pts;
84 /* see if the current image has a pts - if not, set to 0 */
85 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
88 /* update display time */
89 i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
90 decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
92 if( !i_displaydate || i_coding_type != I_CODING_TYPE )
94 if (!p_vpar->synchro.i_images_since_pts )
95 p_vpar->synchro.i_images_since_pts = 10;
97 i_displaydate = p_vpar->synchro.i_last_display_pts
98 + 1000000.0 / (p_vpar->synchro.theorical_fps);
99 //fprintf (stderr, " ");
102 decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts = 0;
104 /* else fprintf (stderr, "R ");
105 if (dropped) fprintf (stderr, " "); else fprintf (stderr, "* ");
106 fprintf (stderr, "%i ", i_coding_type);
107 fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts); */
109 p_vpar->synchro.i_images_since_pts--;
110 p_vpar->synchro.i_last_display_pts = i_displaydate;
114 /* update structures */
115 switch(i_coding_type)
119 p_vpar->synchro.current_p_count++;
120 if( !dropped ) p_vpar->synchro.nondropped_p_count++;
124 p_vpar->synchro.current_b_count++;
125 if( !dropped ) p_vpar->synchro.nondropped_b_count++;
130 /* update information about images we can decode */
131 if (i_current_pts != p_vpar->synchro.i_last_i_pts)
133 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
135 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;
137 p_vpar->synchro.i_last_i_pts = i_current_pts;
142 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
144 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;
150 /* update all the structures for P images */
153 optimal_deviation = vpar_SynchroUpdateTab(
154 &p_vpar->synchro.tab_p[0],
155 p_vpar->synchro.current_p_count);
156 predict = p_vpar->synchro.tab_p[0].mean;
159 candidate_deviation = vpar_SynchroUpdateTab(
160 &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
161 p_vpar->synchro.current_p_count);
162 if (candidate_deviation < optimal_deviation)
164 optimal_deviation = candidate_deviation;
165 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
169 candidate_deviation = vpar_SynchroUpdateTab(
170 &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
171 p_vpar->synchro.current_p_count);
172 if (candidate_deviation < optimal_deviation)
174 optimal_deviation = candidate_deviation;
175 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
178 p_vpar->synchro.p_count_predict = predict;
179 p_vpar->synchro.current_p_count = 0;
182 /* update all the structures for B images */
185 optimal_deviation = vpar_SynchroUpdateTab(
186 &p_vpar->synchro.tab_b[0],
187 p_vpar->synchro.current_b_count);
188 predict = p_vpar->synchro.tab_b[0].mean;
191 candidate_deviation = vpar_SynchroUpdateTab(
192 &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
193 p_vpar->synchro.current_b_count);
194 if (candidate_deviation < optimal_deviation)
196 optimal_deviation = candidate_deviation;
197 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
201 candidate_deviation = vpar_SynchroUpdateTab(
202 &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
203 p_vpar->synchro.current_b_count);
204 if (candidate_deviation < optimal_deviation)
206 optimal_deviation = candidate_deviation;
207 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
210 p_vpar->synchro.b_count_predict = predict;
211 p_vpar->synchro.current_b_count = 0;
213 /* now we calculated all statistics, it's time to
214 * decide what we have the time to display
216 i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
218 p_vpar->synchro.can_display_i
219 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
221 p_vpar->synchro.can_display_p
222 = ( p_vpar->synchro.i_mean_decode_time
223 * (1 + p_vpar->synchro.p_count_predict) < i_delay );
225 if( !p_vpar->synchro.can_display_p )
227 p_vpar->synchro.displayable_p
228 = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
229 if( p_vpar->synchro.displayable_p < 0 )
230 p_vpar->synchro.displayable_p = 0;
233 p_vpar->synchro.displayable_p = 0;
235 if( p_vpar->synchro.can_display_p
236 && !(p_vpar->synchro.can_display_b
237 = ( p_vpar->synchro.i_mean_decode_time
238 * (1 + p_vpar->synchro.b_count_predict
239 + p_vpar->synchro.p_count_predict)) < i_delay) )
241 p_vpar->synchro.displayable_b
242 = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
243 - p_vpar->synchro.can_display_p;
246 p_vpar->synchro.displayable_b = 0;
250 "I %i P %i (%f) B %i (%f)\n",
251 p_vpar->synchro.can_display_i,
252 p_vpar->synchro.can_display_p,
253 p_vpar->synchro.displayable_p,
254 p_vpar->synchro.can_display_b,
255 p_vpar->synchro.displayable_b );
258 /* update some values */
261 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
262 p_vpar->synchro.nondropped_p_count = 0;
263 p_vpar->synchro.nondropped_b_count = 0;
270 p_vpar->synchro.modulo++;
274 /*****************************************************************************
275 * vpar_SynchroChoose : Decide whether we will decode a picture or not
276 *****************************************************************************/
277 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
280 mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
282 switch( i_coding_type )
286 return( p_vpar->synchro.can_display_i );
290 if( p_vpar->synchro.can_display_p )
293 if( p_vpar->synchro.displayable_p * i_delay
294 < p_vpar->synchro.i_mean_decode_time )
296 //fprintf( stderr, "trashed a P\n");
300 p_vpar->synchro.displayable_p--;
305 if( p_vpar->synchro.can_display_b )
308 /* modulo & 0x3 is here to add some randomness */
309 if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
310 * p_vpar->synchro.i_mean_decode_time )
312 //fprintf( stderr, "trashed a B\n");
316 if( p_vpar->synchro.displayable_b <= 0 )
319 p_vpar->synchro.displayable_b--;
327 /*****************************************************************************
328 * vpar_SynchroTrash : Update timers when we trash a picture
329 *****************************************************************************/
330 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
333 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
337 /*****************************************************************************
338 * vpar_SynchroDecode : Update timers when we decide to decode a picture
339 *****************************************************************************/
340 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
343 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
345 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
346 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
349 p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
353 /*****************************************************************************
354 * vpar_SynchroEnd : Called when the image is totally decoded
355 *****************************************************************************/
356 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
358 mtime_t i_decode_time;
360 i_decode_time = (mdate() -
361 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
362 / (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start & 0x0f);
364 p_vpar->synchro.i_mean_decode_time =
365 ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
368 "decoding time was %lli\n",
369 p_vpar->synchro.i_mean_decode_time); */
371 p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
375 /*****************************************************************************
376 * vpar_SynchroDate : When an image has been decoded, ask for its date
377 *****************************************************************************/
378 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
380 mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
383 static mtime_t i_delta = 0;
386 "displaying type %i with delay %lli and delta %lli\n",
387 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
388 i_displaydate - mdate(),
389 i_displaydate - i_delta );
392 "theorical fps: %f - actual fps: %f \n",
393 p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
395 i_delta = i_displaydate;
398 return i_displaydate;