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]->i_pts;
77 i_current_pts = p_vpar->synchro.i_last_decode_pts
78 + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
80 p_vpar->synchro.i_last_decode_pts = i_current_pts;
82 /* see if the current image has a pts - if not, set to 0 */
83 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
86 /* update display time */
87 i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->i_pts;
88 if( !i_displaydate || i_coding_type != I_CODING_TYPE )
90 if (!p_vpar->synchro.i_images_since_pts )
91 p_vpar->synchro.i_images_since_pts = 10;
93 i_displaydate = p_vpar->synchro.i_last_display_pts
94 + 1000000.0 / (p_vpar->synchro.theorical_fps);
95 //fprintf (stderr, " ");
98 /* else fprintf (stderr, "R ");
99 if (dropped) fprintf (stderr, " "); else fprintf (stderr, "* ");
100 fprintf (stderr, "%i ", i_coding_type);
101 fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts); */
103 p_vpar->synchro.i_images_since_pts--;
104 p_vpar->synchro.i_last_display_pts = i_displaydate;
108 /* update structures */
109 switch(i_coding_type)
113 p_vpar->synchro.current_p_count++;
114 if( !dropped ) p_vpar->synchro.nondropped_p_count++;
118 p_vpar->synchro.current_b_count++;
119 if( !dropped ) p_vpar->synchro.nondropped_b_count++;
124 /* update information about images we can decode */
125 if (i_current_pts != p_vpar->synchro.i_last_i_pts)
127 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
129 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;
131 p_vpar->synchro.i_last_i_pts = i_current_pts;
136 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
138 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;
144 /* update all the structures for P images */
147 optimal_deviation = vpar_SynchroUpdateTab(
148 &p_vpar->synchro.tab_p[0],
149 p_vpar->synchro.current_p_count);
150 predict = p_vpar->synchro.tab_p[0].mean;
153 candidate_deviation = vpar_SynchroUpdateTab(
154 &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
155 p_vpar->synchro.current_p_count);
156 if (candidate_deviation < optimal_deviation)
158 optimal_deviation = candidate_deviation;
159 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
163 candidate_deviation = vpar_SynchroUpdateTab(
164 &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
165 p_vpar->synchro.current_p_count);
166 if (candidate_deviation < optimal_deviation)
168 optimal_deviation = candidate_deviation;
169 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
172 p_vpar->synchro.p_count_predict = predict;
173 p_vpar->synchro.current_p_count = 0;
176 /* update all the structures for B images */
179 optimal_deviation = vpar_SynchroUpdateTab(
180 &p_vpar->synchro.tab_b[0],
181 p_vpar->synchro.current_b_count);
182 predict = p_vpar->synchro.tab_b[0].mean;
185 candidate_deviation = vpar_SynchroUpdateTab(
186 &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
187 p_vpar->synchro.current_b_count);
188 if (candidate_deviation < optimal_deviation)
190 optimal_deviation = candidate_deviation;
191 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
195 candidate_deviation = vpar_SynchroUpdateTab(
196 &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
197 p_vpar->synchro.current_b_count);
198 if (candidate_deviation < optimal_deviation)
200 optimal_deviation = candidate_deviation;
201 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
204 p_vpar->synchro.b_count_predict = predict;
205 p_vpar->synchro.current_b_count = 0;
207 /* now we calculated all statistics, it's time to
208 * decide what we have the time to display
210 i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
212 p_vpar->synchro.can_display_i
213 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
215 p_vpar->synchro.can_display_p
216 = ( p_vpar->synchro.i_mean_decode_time
217 * (1 + p_vpar->synchro.p_count_predict) < i_delay );
219 if( !p_vpar->synchro.can_display_p )
221 p_vpar->synchro.displayable_p
222 = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
223 if( p_vpar->synchro.displayable_p < 0 )
224 p_vpar->synchro.displayable_p = 0;
227 p_vpar->synchro.displayable_p = 0;
229 if( p_vpar->synchro.can_display_p
230 && !(p_vpar->synchro.can_display_b
231 = ( p_vpar->synchro.i_mean_decode_time
232 * (1 + p_vpar->synchro.b_count_predict
233 + p_vpar->synchro.p_count_predict)) < i_delay) )
235 p_vpar->synchro.displayable_b
236 = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
237 - p_vpar->synchro.can_display_p;
240 p_vpar->synchro.displayable_b = 0;
244 "I %i P %i (%f) B %i (%f)\n",
245 p_vpar->synchro.can_display_i,
246 p_vpar->synchro.can_display_p,
247 p_vpar->synchro.displayable_p,
248 p_vpar->synchro.can_display_b,
249 p_vpar->synchro.displayable_b );
252 /* update some values */
255 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
256 p_vpar->synchro.nondropped_p_count = 0;
257 p_vpar->synchro.nondropped_b_count = 0;
264 p_vpar->synchro.modulo++;
268 /*****************************************************************************
269 * vpar_SynchroChoose : Decide whether we will decode a picture or not
270 *****************************************************************************/
271 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
274 mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
276 switch( i_coding_type )
280 return( p_vpar->synchro.can_display_i );
284 if( p_vpar->synchro.can_display_p )
287 if( p_vpar->synchro.displayable_p * i_delay
288 < p_vpar->synchro.i_mean_decode_time )
290 //fprintf( stderr, "trashed a P\n");
294 p_vpar->synchro.displayable_p--;
299 if( p_vpar->synchro.can_display_b )
302 /* modulo & 0x3 is here to add some randomness */
303 if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
304 * p_vpar->synchro.i_mean_decode_time )
306 //fprintf( stderr, "trashed a B\n");
310 if( p_vpar->synchro.displayable_b <= 0 )
313 p_vpar->synchro.displayable_b--;
321 /*****************************************************************************
322 * vpar_SynchroTrash : Update timers when we trash a picture
323 *****************************************************************************/
324 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
327 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
331 /*****************************************************************************
332 * vpar_SynchroDecode : Update timers when we decide to decode a picture
333 *****************************************************************************/
334 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
337 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
339 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
340 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
343 p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
347 /*****************************************************************************
348 * vpar_SynchroEnd : Called when the image is totally decoded
349 *****************************************************************************/
350 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
352 mtime_t i_decode_time;
354 i_decode_time = (mdate() -
355 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
356 / (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start & 0x0f);
358 p_vpar->synchro.i_mean_decode_time =
359 ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
362 "decoding time was %lli\n",
363 p_vpar->synchro.i_mean_decode_time); */
365 p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
369 /*****************************************************************************
370 * vpar_SynchroDate : When an image has been decoded, ask for its date
371 *****************************************************************************/
372 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
374 mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
377 static mtime_t i_delta = 0;
380 "displaying type %i with delay %lli and delta %lli\n",
381 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
382 i_displaydate - mdate(),
383 i_displaydate - i_delta );
386 "theorical fps: %f - actual fps: %f \n",
387 p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
389 i_delta = i_displaydate;
392 return i_displaydate;