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,
65 float candidate_deviation;
66 float optimal_deviation;
68 mtime_t i_current_pts;
69 decoder_fifo_t * decoder_fifo;
71 /* interpolate the current PTS */
72 decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
73 /* see if the current image has a pts - if not, interpolate */
74 if( i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->i_pts )
76 p_vpar->synchro.i_images_since_pts = 1;
80 i_current_pts = p_vpar->synchro.i_last_pts
81 + (1000000 / p_vpar->synchro.theorical_fps);
82 p_vpar->synchro.i_images_since_pts++;
84 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts = i_current_pts;
85 p_vpar->synchro.i_last_pts = i_current_pts;
88 /* update structures */
93 p_vpar->synchro.current_p_count++;
97 p_vpar->synchro.current_b_count++;
102 /* update information about images we can decode */
103 if ( p_vpar->synchro.i_last_i_pts )
105 p_vpar->synchro.theorical_fps = 1000000 * (1 + p_vpar->synchro.current_b_count + p_vpar->synchro.current_p_count) / (i_current_pts - p_vpar->synchro.i_last_i_pts);
107 p_vpar->synchro.i_last_i_pts = i_current_pts;
110 /* update all the structures for P images */
113 optimal_deviation = vpar_SynchroUpdateTab(
114 &p_vpar->synchro.tab_p[0],
115 p_vpar->synchro.current_p_count);
116 predict = p_vpar->synchro.tab_p[0].mean;
119 candidate_deviation = vpar_SynchroUpdateTab(
120 &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
121 p_vpar->synchro.current_p_count);
122 if (candidate_deviation < optimal_deviation)
124 optimal_deviation = candidate_deviation;
125 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
129 candidate_deviation = vpar_SynchroUpdateTab(
130 &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
131 p_vpar->synchro.current_p_count);
132 if (candidate_deviation < optimal_deviation)
134 optimal_deviation = candidate_deviation;
135 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
138 p_vpar->synchro.p_count_predict = predict;
139 p_vpar->synchro.current_p_count = 0;
142 /* update all the structures for B images */
145 optimal_deviation = vpar_SynchroUpdateTab(
146 &p_vpar->synchro.tab_b[0],
147 p_vpar->synchro.current_b_count);
148 predict = p_vpar->synchro.tab_b[0].mean;
151 candidate_deviation = vpar_SynchroUpdateTab(
152 &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
153 p_vpar->synchro.current_b_count);
154 if (candidate_deviation < optimal_deviation)
156 optimal_deviation = candidate_deviation;
157 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
161 candidate_deviation = vpar_SynchroUpdateTab(
162 &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
163 p_vpar->synchro.current_b_count);
164 if (candidate_deviation < optimal_deviation)
166 optimal_deviation = candidate_deviation;
167 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
170 p_vpar->synchro.b_count_predict = predict;
171 p_vpar->synchro.current_b_count = 0;
176 p_vpar->synchro.modulo++;
180 /*****************************************************************************
181 * vpar_SynchroChoose : Decide whether we will decode a picture or not
182 *****************************************************************************/
183 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
187 if( p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date + ((p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start) & 0xf) * p_vpar->synchro.i_mean_decode_time > mdate() )
189 //fprintf( stderr, "chooser : we are à la bourre !\n");
190 return( i_coding_type == I_CODING_TYPE );
193 return( i_coding_type == I_CODING_TYPE || (i_coding_type == P_CODING_TYPE));
197 /*****************************************************************************
198 * vpar_SynchroTrash : Update timers when we trash a picture
199 *****************************************************************************/
200 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
203 vpar_SynchroUpdateStructures (p_vpar, i_coding_type);
207 /*****************************************************************************
208 * vpar_SynchroDecode : Update timers when we decide to decode a picture
209 *****************************************************************************/
210 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
213 vpar_SynchroUpdateStructures (p_vpar, i_coding_type);
215 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
216 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
218 p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
222 /*****************************************************************************
223 * vpar_SynchroEnd : Called when the image is totally decoded
224 *****************************************************************************/
225 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
227 mtime_t i_decode_time;
229 i_decode_time = (mdate() -
230 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
231 / (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start & 0x0f);
233 p_vpar->synchro.i_mean_decode_time =
234 ( 3 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 4;
236 p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
239 /*****************************************************************************
240 * vpar_SynchroDate : When an image has been decoded, ask for its date
241 *****************************************************************************/
242 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
244 decoder_fifo_t * fifo;
245 mtime_t i_displaydate;
249 p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date;
250 /* this value should be removed */
251 i_displaydate += 500000;
252 i_delay = i_displaydate - mdate();
254 //fprintf(stderr, "displaying type %i with delay %lli)\n", p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type, i_delay);
256 return i_displaydate;