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;
69 mtime_t i_displaydate;
70 decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
72 /* interpolate the current _decode_ PTS */
73 i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->i_pts;
76 i_current_pts = p_vpar->synchro.i_last_decode_pts
77 + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
79 p_vpar->synchro.i_last_decode_pts = i_current_pts;
81 /* see if the current image has a pts - if not, set to 0 */
82 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
85 /* update display time */
86 i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->i_pts;
87 if( !i_displaydate || i_coding_type != I_CODING_TYPE )
89 if (!p_vpar->synchro.i_images_since_pts )
90 p_vpar->synchro.i_images_since_pts = 10;
92 i_displaydate = p_vpar->synchro.i_last_display_pts
93 + 1000000.0 / (p_vpar->synchro.theorical_fps);
94 //fprintf (stderr, " ");
96 //else fprintf (stderr, "R ");
97 //if (dropped) fprintf (stderr, " "); else fprintf (stderr, "* ");
98 //fprintf (stderr, "%i ", i_coding_type);
99 //fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts);
101 p_vpar->synchro.i_images_since_pts--;
102 p_vpar->synchro.i_last_display_pts = i_displaydate;
106 /* update structures */
107 switch(i_coding_type)
111 p_vpar->synchro.current_p_count++;
112 if( !dropped ) p_vpar->synchro.nondropped_p_count++;
116 p_vpar->synchro.current_b_count++;
117 if( !dropped ) p_vpar->synchro.nondropped_b_count++;
122 /* update information about images we can decode */
123 if (i_current_pts != p_vpar->synchro.i_last_i_pts)
125 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
127 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;
129 p_vpar->synchro.i_last_i_pts = i_current_pts;
134 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
136 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;
139 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
140 p_vpar->synchro.nondropped_p_count = 0;
141 p_vpar->synchro.nondropped_b_count = 0;
145 /* update all the structures for P images */
148 optimal_deviation = vpar_SynchroUpdateTab(
149 &p_vpar->synchro.tab_p[0],
150 p_vpar->synchro.current_p_count);
151 predict = p_vpar->synchro.tab_p[0].mean;
154 candidate_deviation = vpar_SynchroUpdateTab(
155 &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
156 p_vpar->synchro.current_p_count);
157 if (candidate_deviation < optimal_deviation)
159 optimal_deviation = candidate_deviation;
160 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
164 candidate_deviation = vpar_SynchroUpdateTab(
165 &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
166 p_vpar->synchro.current_p_count);
167 if (candidate_deviation < optimal_deviation)
169 optimal_deviation = candidate_deviation;
170 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
173 p_vpar->synchro.p_count_predict = predict;
174 p_vpar->synchro.current_p_count = 0;
177 /* update all the structures for B images */
180 optimal_deviation = vpar_SynchroUpdateTab(
181 &p_vpar->synchro.tab_b[0],
182 p_vpar->synchro.current_b_count);
183 predict = p_vpar->synchro.tab_b[0].mean;
186 candidate_deviation = vpar_SynchroUpdateTab(
187 &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
188 p_vpar->synchro.current_b_count);
189 if (candidate_deviation < optimal_deviation)
191 optimal_deviation = candidate_deviation;
192 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
196 candidate_deviation = vpar_SynchroUpdateTab(
197 &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
198 p_vpar->synchro.current_b_count);
199 if (candidate_deviation < optimal_deviation)
201 optimal_deviation = candidate_deviation;
202 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
205 p_vpar->synchro.b_count_predict = predict;
206 p_vpar->synchro.current_b_count = 0;
211 p_vpar->synchro.modulo++;
215 /*****************************************************************************
216 * vpar_SynchroChoose : Decide whether we will decode a picture or not
217 *****************************************************************************/
218 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
224 i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
226 //fprintf( stderr, "delay is %lli - ", i_delay);
229 /*if ( i_coding_type == B_CODING_TYPE )
232 //return( i_coding_type == I_CODING_TYPE || i_coding_type == P_CODING_TYPE );
233 if( i_delay > 120000 )
237 else if( i_delay > 100000 )
239 keep = ( i_coding_type == I_CODING_TYPE
240 || i_coding_type == P_CODING_TYPE );
242 else if( i_delay > 50000 )
244 keep = ( i_coding_type == I_CODING_TYPE );
252 //if (!keep) fprintf( stderr, "trashing a type %i with delay %lli\n", i_coding_type, i_delay);
253 // else fprintf( stderr, "chooser : ok - displaying a %i \n", i_coding_type);
259 /*****************************************************************************
260 * vpar_SynchroTrash : Update timers when we trash a picture
261 *****************************************************************************/
262 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
265 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
269 /*****************************************************************************
270 * vpar_SynchroDecode : Update timers when we decide to decode a picture
271 *****************************************************************************/
272 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
275 vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
277 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
278 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
281 p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
285 /*****************************************************************************
286 * vpar_SynchroEnd : Called when the image is totally decoded
287 *****************************************************************************/
288 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
290 mtime_t i_decode_time;
292 i_decode_time = (mdate() -
293 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
294 / (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start & 0x0f);
296 p_vpar->synchro.i_mean_decode_time =
297 ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
299 //fprintf (stderr, "decoding time is %lli\n", p_vpar->synchro.i_mean_decode_time);
301 p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
305 /*****************************************************************************
306 * vpar_SynchroDate : When an image has been decoded, ask for its date
307 *****************************************************************************/
308 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
310 mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
311 static mtime_t i_delta = 0;
313 //fprintf(stderr, "displaying type %i with delay %lli and delta %lli\n", p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type, i_displaydate - mdate(), i_displaydate - i_delta);
314 //fprintf (stderr, "theorical fps: %f - actual fps: %f \n", p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps);
316 i_delta = i_displaydate;
317 return i_displaydate;