]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_synchro.c
D�but du portage BeOS. Beaucoup de fuchiers ont �t� modifi� car il a fallu
[vlc] / src / video_parser / vpar_synchro.c
1 /*****************************************************************************
2  * vpar_motion.c : motion vectors parsing
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* free() */
28 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
29 #include <sys/uio.h>                                            /* "input.h" */
30
31 #include "threads.h"
32 #include "config.h"
33 #include "common.h"
34 #include "mtime.h"
35 #include "plugins.h"
36
37 #include "intf_msg.h"
38
39 #include "input.h"
40 #include "decoder_fifo.h"
41 #include "video.h"
42 #include "video_output.h"
43
44 #include "vdec_idct.h"
45 #include "video_decoder.h"
46 #include "vdec_motion.h"
47
48 #include "vpar_blocks.h"
49 #include "vpar_headers.h"
50 #include "vpar_synchro.h"
51 #include "video_parser.h"
52
53 #define MAX_COUNT 3
54
55 /*
56  * Local prototypes
57  */
58
59 #ifdef SAM_SYNCHRO
60 /*****************************************************************************
61  * vpar_SynchroUpdateTab : Update a mean table in the synchro structure
62  *****************************************************************************/
63 float vpar_SynchroUpdateTab( video_synchro_tab_t * tab, int count )
64 {
65
66     tab->mean = ( tab->mean + MAX_COUNT * count ) / ( MAX_COUNT + 1 );
67     tab->deviation = ( tab->deviation + MAX_COUNT * abs (tab->mean - count) )
68                         / ( MAX_COUNT + 1 );
69
70     return tab->deviation;
71 }
72
73 /*****************************************************************************
74  * vpar_SynchroUpdateStructures : Update the synchro structures
75  *****************************************************************************/
76 void vpar_SynchroUpdateStructures( vpar_thread_t * p_vpar,
77                                    int i_coding_type, int dropped )
78 {
79     float candidate_deviation;
80     float optimal_deviation;
81     float predict;
82     mtime_t i_current_pts;
83     mtime_t i_delay;
84     mtime_t i_displaydate;
85     decoder_fifo_t * decoder_fifo = p_vpar->bit_stream.p_decoder_fifo;
86
87     /* interpolate the current _decode_ PTS */
88     i_current_pts = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
89                     decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
90                     0;
91     if( !i_current_pts )
92     {
93         i_current_pts = p_vpar->synchro.i_last_decode_pts
94                        + 1000000.0 / (1 + p_vpar->synchro.actual_fps);
95     }
96     p_vpar->synchro.i_last_decode_pts = i_current_pts;
97
98     /* see if the current image has a pts - if not, set to 0 */
99     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_pts
100             = i_current_pts;
101
102     /* update display time */
103     i_displaydate = decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts ?
104                     decoder_fifo->buffer[decoder_fifo->i_start]->i_pts :
105                     0;
106     if( !i_displaydate || i_coding_type != I_CODING_TYPE )
107     {
108         if (!p_vpar->synchro.i_images_since_pts )
109             p_vpar->synchro.i_images_since_pts = 10;
110
111         i_displaydate = p_vpar->synchro.i_last_display_pts
112                        + 1000000.0 / (p_vpar->synchro.theorical_fps);
113         //fprintf (stderr, "  ");
114     }
115
116     decoder_fifo->buffer[decoder_fifo->i_start]->b_has_pts = 0;
117
118     /* else fprintf (stderr, "R ");
119     if (dropped) fprintf (stderr, "  "); else fprintf (stderr, "* ");
120     fprintf (stderr, "%i ", i_coding_type);
121     fprintf (stderr, "pts %lli delta %lli\n", i_displaydate, i_displaydate - p_vpar->synchro.i_last_display_pts); */
122
123     p_vpar->synchro.i_images_since_pts--;
124     p_vpar->synchro.i_last_display_pts = i_displaydate;
125
126
127
128     /* update structures */
129     switch(i_coding_type)
130     {
131         case P_CODING_TYPE:
132
133             p_vpar->synchro.current_p_count++;
134             if( !dropped ) p_vpar->synchro.nondropped_p_count++;
135             break;
136
137         case B_CODING_TYPE:
138             p_vpar->synchro.current_b_count++;
139             if( !dropped ) p_vpar->synchro.nondropped_b_count++;
140             break;
141
142         case I_CODING_TYPE:
143
144             /* update information about images we can decode */
145             if (i_current_pts != p_vpar->synchro.i_last_i_pts)
146             {
147                 if ( p_vpar->synchro.i_last_i_pts && i_current_pts != p_vpar->synchro.i_last_i_pts)
148                 {
149                     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;
150                 }
151                 p_vpar->synchro.i_last_i_pts = i_current_pts;
152             }
153
154             if( !dropped )
155             {
156                 if ( p_vpar->synchro.i_last_nondropped_i_pts && i_current_pts != p_vpar->synchro.i_last_nondropped_i_pts)
157                 {
158                     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;
159                 }
160
161             }
162
163
164             /* update all the structures for P images */
165
166             /* period == 1 */
167             optimal_deviation = vpar_SynchroUpdateTab(
168                             &p_vpar->synchro.tab_p[0],
169                             p_vpar->synchro.current_p_count);
170             predict = p_vpar->synchro.tab_p[0].mean;
171
172             /* period == 2 */
173             candidate_deviation = vpar_SynchroUpdateTab(
174                             &p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)],
175                             p_vpar->synchro.current_p_count);
176             if (candidate_deviation < optimal_deviation)
177             {
178                 optimal_deviation = candidate_deviation;
179                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo & 0x1)].mean;
180             }
181
182             /* period == 3 */
183             candidate_deviation = vpar_SynchroUpdateTab(
184                             &p_vpar->synchro.tab_p[3 + (p_vpar->synchro.modulo % 3)],
185                             p_vpar->synchro.current_p_count);
186             if (candidate_deviation < optimal_deviation)
187             {
188                 optimal_deviation = candidate_deviation;
189                 predict = p_vpar->synchro.tab_p[1 + (p_vpar->synchro.modulo % 3)].mean;
190             }
191
192             p_vpar->synchro.p_count_predict = predict;
193             p_vpar->synchro.current_p_count = 0;
194
195
196             /* update all the structures for B images */
197
198             /* period == 1 */
199             optimal_deviation = vpar_SynchroUpdateTab(
200                             &p_vpar->synchro.tab_b[0],
201                             p_vpar->synchro.current_b_count);
202             predict = p_vpar->synchro.tab_b[0].mean;
203
204             /* period == 2 */
205             candidate_deviation = vpar_SynchroUpdateTab(
206                             &p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)],
207                             p_vpar->synchro.current_b_count);
208             if (candidate_deviation < optimal_deviation)
209             {
210                 optimal_deviation = candidate_deviation;
211                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo & 0x1)].mean;
212             }
213
214             /* period == 3 */
215             candidate_deviation = vpar_SynchroUpdateTab(
216                             &p_vpar->synchro.tab_b[3 + (p_vpar->synchro.modulo % 3)],
217                             p_vpar->synchro.current_b_count);
218             if (candidate_deviation < optimal_deviation)
219             {
220                 optimal_deviation = candidate_deviation;
221                 predict = p_vpar->synchro.tab_b[1 + (p_vpar->synchro.modulo % 3)].mean;
222             }
223
224             p_vpar->synchro.b_count_predict = predict;
225             p_vpar->synchro.current_b_count = 0;
226
227             /* now we calculated all statistics, it's time to
228              * decide what we have the time to display
229              */
230             i_delay = i_current_pts - p_vpar->synchro.i_last_nondropped_i_pts;
231
232             p_vpar->synchro.can_display_i
233                 = ( p_vpar->synchro.i_mean_decode_time < i_delay );
234
235             p_vpar->synchro.can_display_p
236                     = ( p_vpar->synchro.i_mean_decode_time
237                     * (1 + p_vpar->synchro.p_count_predict) < i_delay );
238
239             if( !p_vpar->synchro.can_display_p )
240             {
241                 p_vpar->synchro.displayable_p
242                     = -1 + i_delay / p_vpar->synchro.i_mean_decode_time;
243                 if( p_vpar->synchro.displayable_p < 0 )
244                     p_vpar->synchro.displayable_p = 0;
245             }
246             else
247                 p_vpar->synchro.displayable_p = 0;
248
249             if( p_vpar->synchro.can_display_p
250                 && !(p_vpar->synchro.can_display_b
251                     = ( p_vpar->synchro.i_mean_decode_time
252                     * (1 + p_vpar->synchro.b_count_predict
253                         + p_vpar->synchro.p_count_predict)) < i_delay) )
254             {
255                 p_vpar->synchro.displayable_b
256                     = -2.0 + i_delay / p_vpar->synchro.i_mean_decode_time
257                         - p_vpar->synchro.can_display_p;
258             }
259             else
260                 p_vpar->synchro.displayable_b = 0;
261
262 #if 0
263             fprintf( stderr,
264                 "I %i  P %i (%f)  B %i (%f)\n",
265                 p_vpar->synchro.can_display_i,
266                 p_vpar->synchro.can_display_p,
267                 p_vpar->synchro.displayable_p,
268                 p_vpar->synchro.can_display_b,
269                 p_vpar->synchro.displayable_b );
270 #endif
271
272             /* update some values */
273             if( !dropped )
274             {
275                 p_vpar->synchro.i_last_nondropped_i_pts = i_current_pts;
276                 p_vpar->synchro.nondropped_p_count = 0;
277                 p_vpar->synchro.nondropped_b_count = 0;
278             }
279
280             break;
281
282     }
283
284     p_vpar->synchro.modulo++;
285
286 }
287
288 /*****************************************************************************
289  * vpar_SynchroChoose : Decide whether we will decode a picture or not
290  *****************************************************************************/
291 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
292                               int i_structure )
293 {
294     mtime_t i_delay = p_vpar->synchro.i_last_decode_pts - mdate();
295
296     switch( i_coding_type )
297     {
298         case I_CODING_TYPE:
299
300             return( p_vpar->synchro.can_display_i );
301
302         case P_CODING_TYPE:
303
304             if( p_vpar->synchro.can_display_p )
305                 return( 1 );
306
307             if( p_vpar->synchro.displayable_p * i_delay
308                 < p_vpar->synchro.i_mean_decode_time )
309             {
310                 //fprintf( stderr, "trashed a P\n");
311                 return( 0 );
312             }
313
314             p_vpar->synchro.displayable_p--;
315             return( 1 );
316
317         case B_CODING_TYPE:
318
319             if( p_vpar->synchro.can_display_b )
320                 return( 1 );
321
322             /* modulo & 0x3 is here to add some randomness */
323             if( i_delay < (1 + (p_vpar->synchro.modulo & 0x3))
324                 * p_vpar->synchro.i_mean_decode_time )
325             {
326                 //fprintf( stderr, "trashed a B\n");
327                 return( 0 );
328             }
329
330             if( p_vpar->synchro.displayable_b <= 0 )
331                 return( 0 );
332
333             p_vpar->synchro.displayable_b--;
334             return( 1 );
335     }
336
337     return( 0 );
338
339 }
340
341 /*****************************************************************************
342  * vpar_SynchroTrash : Update timers when we trash a picture
343  *****************************************************************************/
344 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
345                         int i_structure )
346 {
347     vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 1);
348
349 }
350
351 /*****************************************************************************
352  * vpar_SynchroDecode : Update timers when we decide to decode a picture
353  *****************************************************************************/
354 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
355                             int i_structure )
356 {
357     vpar_SynchroUpdateStructures (p_vpar, i_coding_type, 0);
358
359     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_decode_date = mdate();
360     p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_stop].i_image_type
361         = i_coding_type;
362
363     p_vpar->synchro.i_fifo_stop = (p_vpar->synchro.i_fifo_stop + 1) & 0xf;
364
365 }
366
367 /*****************************************************************************
368  * vpar_SynchroEnd : Called when the image is totally decoded
369  *****************************************************************************/
370 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
371 {
372     mtime_t i_decode_time;
373
374     i_decode_time = (mdate() -
375             p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_decode_date)
376         / ( (p_vpar->synchro.i_fifo_stop - p_vpar->synchro.i_fifo_start) & 0x0f);
377
378     p_vpar->synchro.i_mean_decode_time =
379         ( 7 * p_vpar->synchro.i_mean_decode_time + i_decode_time ) / 8;
380
381     /* fprintf (stderr,
382         "decoding time was %lli\n",
383         p_vpar->synchro.i_mean_decode_time); */
384
385     p_vpar->synchro.i_fifo_start = (p_vpar->synchro.i_fifo_start + 1) & 0xf;
386
387 }
388
389 /*****************************************************************************
390  * vpar_SynchroDate : When an image has been decoded, ask for its date
391  *****************************************************************************/
392 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
393 {
394     mtime_t i_displaydate = p_vpar->synchro.i_last_display_pts;
395
396 #if 0
397     static mtime_t i_delta = 0;
398
399     fprintf( stderr,
400         "displaying type %i with delay %lli and delta %lli\n",
401         p_vpar->synchro.fifo[p_vpar->synchro.i_fifo_start].i_image_type,
402         i_displaydate - mdate(),
403         i_displaydate - i_delta );
404
405     fprintf (stderr,
406         "theorical fps: %f - actual fps: %f \n",
407         p_vpar->synchro.theorical_fps, p_vpar->synchro.actual_fps );
408
409     i_delta = i_displaydate;
410 #endif
411
412     return i_displaydate;
413 }
414
415 #endif
416
417 #ifdef MEUUH_SYNCHRO
418
419 /* synchro a deux balles backportee du decodeur de reference. NE MARCHE PAS
420 AVEC LES IMAGES MONOTRAMES */
421
422 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
423                               int i_structure )
424 {
425     switch (i_coding_type)
426     {
427     case B_CODING_TYPE:
428         if ((p_vpar->synchro.kludge_level <= p_vpar->synchro.kludge_nbp))
429         {
430             p_vpar->synchro.kludge_b++;
431             return( 0 );
432         }
433         if (p_vpar->synchro.kludge_b %
434              (p_vpar->synchro.kludge_nbb /
435                 (p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp)))
436         {
437             p_vpar->synchro.kludge_b++;
438             return( 0 );
439         }
440         p_vpar->synchro.kludge_b++;
441         return( 1 );
442
443     case P_CODING_TYPE:
444         if (p_vpar->synchro.kludge_p++ >= p_vpar->synchro.kludge_level)
445         {
446             return( 0 );
447         }
448         return( 1 );
449
450     default:
451         return( 1 );
452     }
453 }
454
455 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
456                         int i_structure )
457 {
458     if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
459     {
460         p_vpar->synchro.kludge_nbframes = 0;
461         p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
462     }
463     else
464         p_vpar->synchro.kludge_nbframes++;
465     DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
466 }
467
468 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
469                             int i_structure )
470 {
471     if (DECODER_FIFO_START(p_vpar->fifo)->b_has_pts && i_coding_type == I_CODING_TYPE)
472     {
473         p_vpar->synchro.kludge_nbframes = 0;
474         p_vpar->synchro.kludge_date = DECODER_FIFO_START(p_vpar->fifo)->i_pts;
475         DECODER_FIFO_START(p_vpar->fifo)->b_has_pts = 0;
476     }
477     else
478         p_vpar->synchro.kludge_nbframes++;
479 }
480
481 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
482 {
483     return( p_vpar->synchro.kludge_date
484             + p_vpar->synchro.kludge_nbframes*1000000/(p_vpar->sequence.r_frame_rate ) );
485 }
486
487 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
488 {
489 }
490
491 void vpar_SynchroKludge( vpar_thread_t * p_vpar, mtime_t date )
492 {
493     mtime_t     show_date;
494     int         temp = p_vpar->synchro.kludge_level;
495
496     p_vpar->synchro.kludge_nbp = p_vpar->synchro.kludge_p ? p_vpar->synchro.kludge_p : 5;
497     p_vpar->synchro.kludge_nbb = p_vpar->synchro.kludge_b ? p_vpar->synchro.kludge_b : 6;
498     show_date = date - mdate();
499     p_vpar->synchro.kludge_p = 0;
500     p_vpar->synchro.kludge_b = 0;
501
502     if (show_date < (SYNC_DELAY - SYNC_TOLERATE) && show_date <= p_vpar->synchro.kludge_prevdate)
503     {
504         p_vpar->synchro.kludge_level--;
505         if (p_vpar->synchro.kludge_level < 0)
506             p_vpar->synchro.kludge_level = 0;
507         else if (p_vpar->synchro.kludge_level >
508                      p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
509             p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
510 #ifdef DEBUG
511         if (temp != p_vpar->synchro.kludge_level)
512             intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
513                         temp, p_vpar->synchro.kludge_level, show_date );
514 #endif
515     }
516     else if (show_date > (SYNC_DELAY + SYNC_TOLERATE) && show_date >= p_vpar->synchro.kludge_prevdate)
517     {
518         p_vpar->synchro.kludge_level++;
519         if (p_vpar->synchro.kludge_level > p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb)
520             p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbp + p_vpar->synchro.kludge_nbb;
521 #ifdef DEBUG
522         if (temp != p_vpar->synchro.kludge_level)
523             intf_DbgMsg("vdec debug: Level changed from %d to %d (%Ld)\n",
524                         temp, p_vpar->synchro.kludge_level, show_date );
525 #endif
526     }
527
528     p_vpar->synchro.kludge_prevdate = show_date;
529     if ((p_vpar->synchro.kludge_level - p_vpar->synchro.kludge_nbp) > p_vpar->synchro.kludge_nbb)
530         p_vpar->synchro.kludge_level = p_vpar->synchro.kludge_nbb + p_vpar->synchro.kludge_nbp;
531 }
532
533 #endif
534
535
536 #ifdef POLUX_SYNCHRO
537
538 void vpar_SynchroSetCurrentDate( vpar_thread_t * p_vpar, int i_coding_type )
539 {
540     pes_packet_t * p_pes =
541         p_vpar->bit_stream.p_decoder_fifo->buffer[p_vpar->bit_stream.p_decoder_fifo->i_start];
542
543
544     switch( i_coding_type )
545     {
546     case B_CODING_TYPE:
547         if( p_pes->b_has_pts )
548         {
549             if( p_pes->i_pts < p_vpar->synchro.i_current_frame_date )
550             {
551                 intf_ErrMsg( "vpar warning: pts_date < current_date\n" );
552             }
553             p_vpar->synchro.i_current_frame_date = p_pes->i_pts;
554             p_pes->b_has_pts = 0;
555         }
556         else
557         {
558             p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
559         }
560         break;
561
562     default:
563
564         if( p_vpar->synchro.i_backward_frame_date == 0 )
565         {
566             p_vpar->synchro.i_current_frame_date += 1000000/(p_vpar->sequence.r_frame_rate);
567         }
568         else
569         {
570             if( p_vpar->synchro.i_backward_frame_date < p_vpar->synchro.i_current_frame_date )
571             {
572                 intf_ErrMsg( "vpar warning: backward_date < current_date (%Ld)\n",
573                          p_vpar->synchro.i_backward_frame_date - p_vpar->synchro.i_current_frame_date );
574             }
575             p_vpar->synchro.i_current_frame_date = p_vpar->synchro.i_backward_frame_date;
576             p_vpar->synchro.i_backward_frame_date = 0;
577         }
578
579         if( p_pes->b_has_pts )
580         {
581             p_vpar->synchro.i_backward_frame_date = p_pes->i_pts;
582             p_pes->b_has_pts = 0;
583         }
584        break;
585     }
586 }
587
588 boolean_t vpar_SynchroChoose( vpar_thread_t * p_vpar, int i_coding_type,
589                               int i_structure )
590 {
591     boolean_t b_result = 1;
592     int i_synchro_level = p_vpar->p_vout->i_synchro_level;
593
594     vpar_SynchroSetCurrentDate( p_vpar, i_coding_type );
595
596     /*
597      * The synchro level is updated by the video input (see SynchroLevelUpdate)
598      * so we just use the synchro_level to decide which frame to trash
599      */
600
601     switch( i_coding_type )
602     {
603     case I_CODING_TYPE:
604
605 //fprintf( stderr, "p : %d (%d), b : %d (%d)\n", p_vpar->synchro.i_p_count, p_vpar->synchro.i_p_nb,
606 //         p_vpar->synchro.i_b_count, p_vpar->synchro.i_b_nb );
607
608         p_vpar->synchro.r_p_average =
609             (p_vpar->synchro.r_p_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_p_count)/SYNC_AVERAGE_COUNT;
610         p_vpar->synchro.r_b_average =
611             (p_vpar->synchro.r_b_average*(SYNC_AVERAGE_COUNT-1)+p_vpar->synchro.i_b_count)/SYNC_AVERAGE_COUNT;
612
613         p_vpar->synchro.i_p_nb = (int)(p_vpar->synchro.r_p_average+0.5);
614         p_vpar->synchro.i_b_nb = (int)(p_vpar->synchro.r_b_average+0.5);
615
616         p_vpar->synchro.i_p_count = p_vpar->synchro.i_b_count = 0;
617         p_vpar->synchro.i_b_trasher = p_vpar->synchro.i_b_nb / 2;
618         p_vpar->synchro.i_i_count++;
619        break;
620
621     case P_CODING_TYPE:
622         p_vpar->synchro.i_p_count++;
623         if( p_vpar->synchro.i_p_count > i_synchro_level )
624         {
625             b_result = 0;
626         }
627         break;
628
629     case B_CODING_TYPE:
630         p_vpar->synchro.i_b_count++;
631         if( p_vpar->synchro.i_p_nb >= i_synchro_level )
632         {
633             /* We must trash all the B */
634             b_result = 0;
635         }
636         else
637         {
638             /* We use the brensenham algorithm to decide which B to trash */
639             p_vpar->synchro.i_b_trasher +=
640                 p_vpar->synchro.i_b_nb - (i_synchro_level-p_vpar->synchro.i_p_nb);
641             if( p_vpar->synchro.i_b_trasher >= p_vpar->synchro.i_b_nb )
642             {
643                 b_result = 0;
644                 p_vpar->synchro.i_b_trasher -= p_vpar->synchro.i_b_nb;
645             }
646         }
647         break;
648     }
649
650     return( b_result );
651 }
652
653 void vpar_SynchroTrash( vpar_thread_t * p_vpar, int i_coding_type,
654                         int i_structure )
655 {
656     vpar_SynchroChoose( p_vpar, i_coding_type, i_structure );
657 }
658
659 void vpar_SynchroUpdateLevel()
660 {
661     //vlc_mutex_lock( &level_lock );
662     //vlc_mutex_unlock( &level_lock );
663 }
664
665 mtime_t vpar_SynchroDate( vpar_thread_t * p_vpar )
666 {
667 //fprintf( stderr, "delay : %Ld\n" , mdate() - p_vpar->synchro.i_current_frame_date );
668     return( p_vpar->synchro.i_current_frame_date );
669 }
670
671 /* functions with no use */
672
673 void vpar_SynchroEnd( vpar_thread_t * p_vpar )
674 {
675 }
676
677 void vpar_SynchroDecode( vpar_thread_t * p_vpar, int i_coding_type,
678                             int i_structure )
679 {
680 }
681
682 #endif