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