]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_synchro.c
. remis le kludge du i_coding_type == I_CODING_TYPE en attendant qu'on
[vlc] / src / video_parser / vpar_synchro.c
1 /*****************************************************************************
2  * vpar_motion.c : motion vectors parsing
3  * (c)1999 VideoLAN
4  *****************************************************************************/
5
6 /*****************************************************************************
7  * Preamble
8  *****************************************************************************/
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <sys/uio.h>
15
16 #include "config.h"
17 #include "common.h"
18 #include "mtime.h"
19 #include "vlc_thread.h"
20
21 #include "intf_msg.h"
22 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
23
24 #include "input.h"
25 #include "input_netlist.h"
26 #include "decoder_fifo.h"
27 #include "video.h"
28 #include "video_output.h"
29
30 #include "vdec_idct.h"
31 #include "video_decoder.h"
32 #include "vdec_motion.h"
33
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"
39
40 #define MAX_COUNT 3
41
42 /*
43  * Local prototypes
44  */
45
46 /*****************************************************************************
47  * vpar_SynchroUpdateTab : Update a mean table in the synchro structure
48  *****************************************************************************/
49 float vpar_SynchroUpdateTab( video_synchro_tab_t * tab, int count )
50 {
51         
52     tab->mean = ( tab->mean + MAX_COUNT * count ) / ( MAX_COUNT + 1 );
53     tab->deviation = ( tab->deviation + MAX_COUNT * abs (tab->mean - count) )
54                         / ( MAX_COUNT + 1 );
55
56     return tab->deviation;
57 }
58
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 )
64 {
65     float candidate_deviation;
66     float optimal_deviation;
67     float predict;
68     mtime_t i_current_pts;
69     mtime_t i_delay;
70     mtime_t i_displaydate;
71     decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
72
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 :
76                     0;
77     if( !i_current_pts )
78     {
79         i_current_pts = p_vpar->synchro.i_last_decode_pts
80                        + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
81     }
82     p_vpar->synchro.i_last_decode_pts = i_current_pts;
83  
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
86             = i_current_pts;
87
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 :
91                     0;
92     if( !i_displaydate || i_coding_type != I_CODING_TYPE )
93     {
94         if (!p_vpar->synchro.i_images_since_pts )
95             p_vpar->synchro.i_images_since_pts = 10;
96
97         i_displaydate = p_vpar->synchro.i_last_display_pts
98                        + 1000000.0 / (p_vpar->synchro.theorical_fps);
99         //fprintf (stderr, "  ");
100     }
101     
102     decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts = 0;
103
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); */
108
109     p_vpar->synchro.i_images_since_pts--;
110     p_vpar->synchro.i_last_display_pts = i_displaydate;
111
112
113
114     /* update structures */
115     switch(i_coding_type)
116     {
117         case P_CODING_TYPE:
118
119             p_vpar->synchro.current_p_count++;
120             if( !dropped ) p_vpar->synchro.nondropped_p_count++;
121             break;
122
123         case B_CODING_TYPE:
124             p_vpar->synchro.current_b_count++;
125             if( !dropped ) p_vpar->synchro.nondropped_b_count++;
126             break;
127
128         case I_CODING_TYPE:
129
130             /* update information about images we can decode */
131             if (i_current_pts != p_vpar->synchro.i_last_i_pts)
132             {
133                 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
134                 {
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;
136                 }
137                 p_vpar->synchro.i_last_i_pts = i_current_pts;
138             }
139
140             if( !dropped )
141             {
142                 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
143                 {
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;
145                 }
146     
147             }
148
149
150             /* update all the structures for P images */
151
152             /* period == 1 */
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;
157
158             /* period == 2 */
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)
163             {
164                 optimal_deviation = candidate_deviation;
165                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
166             }
167
168             /* period == 3 */
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)
173             {
174                 optimal_deviation = candidate_deviation;
175                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
176             }
177
178             p_vpar->synchro.p_count_predict = predict;
179             p_vpar->synchro.current_p_count = 0;
180
181
182             /* update all the structures for B images */
183
184             /* period == 1 */
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;
189
190             /* period == 2 */
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)
195             {
196                 optimal_deviation = candidate_deviation;
197                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
198             }
199
200             /* period == 3 */
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)
205             {
206                 optimal_deviation = candidate_deviation;
207                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
208             }
209
210             p_vpar->synchro.b_count_predict = predict;
211             p_vpar->synchro.current_b_count = 0;
212             
213             /* now we calculated all statistics, it's time to
214              * decide what we have the time to display
215              */
216             i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
217
218             p_vpar->synchro.can_display_i
219                 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
220
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 );
224
225             if( !p_vpar->synchro.can_display_p )
226             {
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;
231             }
232             else
233                 p_vpar->synchro.displayable_p = 0;
234
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) )
240             {
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;
244             }
245             else
246                 p_vpar->synchro.displayable_b = 0;
247
248 #if 0
249             fprintf( stderr,
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 );
256 #endif
257
258             /* update some values */
259             if( !dropped )
260             {
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;
264             }
265
266             break;
267
268     }
269
270     p_vpar->synchro.modulo++;
271
272 }
273
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,
278                               int i_structure )
279 {
280     mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
281
282     switch( i_coding_type )
283     {
284         case I_CODING_TYPE:
285
286             return( p_vpar->synchro.can_display_i );
287
288         case P_CODING_TYPE:
289
290             if( p_vpar->synchro.can_display_p )
291                 return( 1 );
292
293             if( p_vpar->synchro.displayable_p * i_delay
294                 < p_vpar->synchro.i_mean_decode_time )
295             {
296                 //fprintf( stderr, "trashed a P\n");
297                 return( 0 );
298             }
299
300             p_vpar->synchro.displayable_p--;
301             return( 1 );
302    
303         case B_CODING_TYPE:
304
305             if( p_vpar->synchro.can_display_b )
306                 return( 1 );
307
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 )
311             {
312                 //fprintf( stderr, "trashed a B\n");
313                 return( 0 );
314             }
315  
316             if( p_vpar->synchro.displayable_b <= 0 )
317                 return( 0 );
318
319             p_vpar->synchro.displayable_b--;
320             return( 1 );
321     }
322
323     return( 0 );
324
325 }
326
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,
331                         int i_structure )
332 {
333     vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
334
335 }
336
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,
341                             int i_structure )
342 {
343     vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
344
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
347         = i_coding_type;
348
349     p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
350
351 }
352
353 /*****************************************************************************
354  * vpar_SynchroEnd : Called when the image is totally decoded
355  *****************************************************************************/
356 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
357 {
358     mtime_t i_decode_time;
359
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);
363
364     p_vpar->synchro.i_mean_decode_time =
365         ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
366
367     /* fprintf (stderr,
368         "decoding time was %lli\n",
369         p_vpar->synchro.i_mean_decode_time); */
370
371     p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
372
373 }
374
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 )
379 {
380     mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
381     
382 #if 0
383     static mtime_t i_delta = 0;
384
385     fprintf( stderr,
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 );
390
391     fprintf (stderr,
392         "theorical fps: %f - actual fps: %f \n",
393         p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
394
395     i_delta = i_displaydate;
396 #endif
397
398     return i_displaydate;
399 }
400