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