]> git.sesse.net Git - vlc/blob - modules/codec/xvmc/xxmc.c
Remove most stray semi-colons in module descriptions
[vlc] / modules / codec / xvmc / xxmc.c
1 /*****************************************************************************
2  * xxmc.c : HW MPEG decoder thread
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_vout.h>
35 #include <vlc_codec.h>
36 #include <vlc_codec_synchro.h>
37
38 #include <unistd.h>
39 #ifdef __GLIBC__
40     #include <mcheck.h>
41 #endif
42
43 #include "mpeg2.h"
44 #include "attributes.h"
45 #include "mpeg2_internal.h"
46 #include "xvmc_vld.h"
47
48 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
49 #define AR_SQUARE_PICTURE       1                           /* square pixels */
50 #define AR_3_4_PICTURE          2                        /* 3:4 picture (TV) */
51 #define AR_16_9_PICTURE         3              /* 16:9 picture (wide screen) */
52 #define AR_221_1_PICTURE        4                  /* 2.21:1 picture (movie) */
53
54 /*****************************************************************************
55  * decoder_sys_t : libmpeg2 decoder descriptor
56  *****************************************************************************/
57 struct decoder_sys_t
58 {
59     /*
60      * libmpeg2 properties
61      */
62     mpeg2dec_t          *p_mpeg2dec;
63     const mpeg2_info_t  *p_info;
64     bool                b_skip;
65
66     /*
67      * Input properties
68      */
69     mtime_t             i_pts;
70     mtime_t             i_previous_pts;
71     mtime_t             i_current_pts;
72     mtime_t             i_previous_dts;
73     mtime_t             i_current_dts;
74     int                 i_current_rate;
75     picture_t *         p_picture_to_destroy;
76     bool                b_garbage_pic;
77     bool                b_after_sequence_header; /* is it the next frame after
78                                                   * the sequence header ?    */
79     bool                b_slice_i;             /* intra-slice refresh stream */
80
81     /*
82      * Output properties
83      */
84     decoder_synchro_t   *p_synchro;
85     int                 i_aspect;
86     mtime_t             i_last_frame_pts;
87 };
88
89 /*****************************************************************************
90  * Local prototypes
91  *****************************************************************************/
92
93 static int  OpenDecoder( vlc_object_t * );
94 static void CloseDecoder( vlc_object_t * );
95
96 static picture_t *DecodeBlock( decoder_t *, block_t ** );
97
98 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
99
100 /*****************************************************************************
101  * Module descriptor
102  *****************************************************************************/
103 vlc_module_begin ()
104     set_description( N_("MPEG I/II hw video decoder (using libmpeg2)") )
105     set_capability( "decoder", 140 )
106     set_callbacks( OpenDecoder, CloseDecoder )
107     add_shortcut( "xxmc" )
108 vlc_module_end ()
109
110 /*****************************************************************************
111  * OpenDecoder: probe the decoder and return score
112  *****************************************************************************/
113 static int OpenDecoder( vlc_object_t *p_this )
114 {
115
116     decoder_t *p_dec = (decoder_t*)p_this;
117     decoder_sys_t *p_sys = NULL;
118     uint32_t i_accel = 0;
119     FILE *f_wd_dec; 
120
121 #ifdef __GLIBC__
122     mtrace();
123 #endif
124     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','v') &&
125         p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','1') &&
126         /* Pinnacle hardware-mpeg1 */
127         p_dec->fmt_in.i_codec != VLC_FOURCC('P','I','M','1') &&
128         /* VIA hardware-mpeg2 */
129         p_dec->fmt_in.i_codec != VLC_FOURCC('X','x','M','C') &&
130         /* ATI Video */
131         p_dec->fmt_in.i_codec != VLC_FOURCC('V','C','R','2') &&
132         p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','2') )
133     {
134         return VLC_EGENERIC;
135     }
136
137     msg_Dbg(p_dec, "OpenDecoder Entering");
138
139     /* Allocate the memory needed to store the decoder's structure */
140     p_dec->p_sys = p_sys = (decoder_sys_t *)malloc(sizeof(decoder_sys_t));
141     if( !p_sys )
142         return VLC_ENOMEM;
143
144     /* Initialize the thread properties */
145     memset( p_sys, 0, sizeof(decoder_sys_t) );
146     p_sys->p_mpeg2dec = NULL;
147     p_sys->p_synchro  = NULL;
148     p_sys->p_info     = NULL;
149     p_sys->i_pts      = mdate() + DEFAULT_PTS_DELAY;
150     p_sys->i_current_pts  = 0;
151     p_sys->i_previous_pts = 0;
152     p_sys->i_current_dts  = 0;
153     p_sys->i_previous_dts = 0;
154     p_sys->p_picture_to_destroy = NULL;
155     p_sys->b_garbage_pic = 0;
156     p_sys->b_slice_i  = 0;
157     p_sys->b_skip     = 0;
158
159 #if defined( __i386__ )
160     if( vlc_CPU() & CPU_CAPABILITY_MMX )
161     {
162         i_accel |= MPEG2_ACCEL_X86_MMX;
163     }
164
165     if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
166     {
167         i_accel |= MPEG2_ACCEL_X86_3DNOW;
168     }
169
170     if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
171     {
172         i_accel |= MPEG2_ACCEL_X86_MMXEXT;
173     }
174
175 #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
176     if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
177     {
178         i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
179     }
180
181 #else
182     /* If we do not know this CPU, trust libmpeg2's feature detection */
183     i_accel = MPEG2_ACCEL_DETECT;
184
185 #endif
186
187     /* Set CPU acceleration features */
188     mpeg2_accel( i_accel );
189
190     /* Initialize decoder */
191     p_sys->p_mpeg2dec = mpeg2_init();
192     if( p_sys->p_mpeg2dec == NULL)
193     {
194         msg_Err( p_dec, "mpeg2_init() failed" );
195         free( p_sys );
196         return VLC_EGENERIC;
197     }
198
199     p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
200
201     p_dec->pf_decode_video = DecodeBlock;
202
203     f_wd_dec = fopen("/vlc/dec_pid", "w");
204     if (f_wd_dec != NULL)
205     {
206         fprintf(f_wd_dec, "%d\n", getpid());
207         fflush(f_wd_dec);
208         fclose(f_wd_dec);
209     }
210
211     msg_Dbg(p_dec, "OpenDecoder Leaving");
212     return VLC_SUCCESS;
213 }
214
215 static void WriteDecodeFile(int value)
216 {
217     FILE *f_wd_ok;
218
219     f_wd_ok = fopen("/vlc/dec_ok", "w");
220     if (f_wd_ok != NULL)
221     {
222         fprintf(f_wd_ok, "%d", value);
223         fflush(f_wd_ok);
224         fclose(f_wd_ok);
225     }
226 }
227
228 /*****************************************************************************
229  * RunDecoder: the libmpeg2 decoder
230  *****************************************************************************/
231 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
232 {
233     decoder_sys_t   *p_sys = p_dec->p_sys;
234     mpeg2_state_t   state;
235     picture_t       *p_pic;
236
237     block_t *p_block;
238
239     if( !pp_block || !*pp_block )
240         return NULL;
241
242     p_block = *pp_block;
243     while( 1 )
244     {
245         state = mpeg2_parse( p_sys->p_mpeg2dec );
246         switch( state )
247         {
248             case STATE_BUFFER:
249                 if( !p_block->i_buffer )
250                 {
251                     block_Release( p_block );
252                     return NULL;
253                 }
254                 if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
255                     p_sys->p_synchro &&
256                     p_sys->p_info->sequence &&
257                     p_sys->p_info->sequence->width != (unsigned int)-1 )
258                 {
259                     decoder_SynchroReset( p_sys->p_synchro );
260                     if( p_sys->p_info->current_fbuf != NULL
261                         && p_sys->p_info->current_fbuf->id != NULL )
262                     {
263                         p_sys->b_garbage_pic = 1;
264                         p_pic = p_sys->p_info->current_fbuf->id;
265                     }
266                     else
267                     {
268                         uint8_t *buf[3];
269                         buf[0] = buf[1] = buf[2] = NULL;
270                         if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
271                             break;
272                         mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
273                     }
274                     p_sys->p_picture_to_destroy = p_pic;
275
276                     if ( p_sys->b_slice_i )
277                     {
278                         decoder_SynchroNewPicture( p_sys->p_synchro,
279                             I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate,
280                             p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
281                         decoder_SynchroDecode( p_sys->p_synchro );
282                         decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
283                     }
284                 }
285
286 #ifdef PIC_FLAG_PTS
287                 if( p_block->i_pts )
288                 {
289                     mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
290
291 #else /* New interface */
292                 if( p_block->i_pts || p_block->i_dts )
293                 {
294                     mpeg2_tag_picture( p_sys->p_mpeg2dec,
295                                         (uint32_t)p_block->i_pts,
296                                         (uint32_t)p_block->i_dts );
297 #endif
298                     p_sys->i_previous_pts = p_sys->i_current_pts;
299                     p_sys->i_current_pts = p_block->i_pts;
300                     p_sys->i_previous_dts = p_sys->i_current_dts;
301                     p_sys->i_current_dts = p_block->i_dts;
302                 }
303                 p_sys->i_current_rate = p_block->i_rate;
304
305                 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
306                                 p_block->p_buffer + p_block->i_buffer );
307                 p_block->i_buffer = 0;
308                 break;
309
310             case STATE_SEQUENCE:
311             {
312                 /* Initialize video output */
313                 uint8_t *buf[3];
314                 buf[0] = buf[1] = buf[2] = NULL;
315
316                 /* Check whether the input gave a particular aspect ratio */
317                 if( p_dec->fmt_in.video.i_aspect )
318                 {
319                     p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
320                     if( p_sys->i_aspect <= AR_221_1_PICTURE )
321                     switch( p_sys->i_aspect )
322                     {
323                     case AR_3_4_PICTURE:
324                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
325                         break;
326                     case AR_16_9_PICTURE:
327                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
328                         break;
329                     case AR_221_1_PICTURE:
330                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
331                         break;
332                     case AR_SQUARE_PICTURE:
333                         p_sys->i_aspect = VOUT_ASPECT_FACTOR *
334                                     p_sys->p_info->sequence->width /
335                                     p_sys->p_info->sequence->height;
336                         break;
337                     }
338                 }
339                 else
340                 {
341                     /* Use the value provided in the MPEG sequence header */
342                     if( p_sys->p_info->sequence->pixel_height > 0 )
343                     {
344                         p_sys->i_aspect =
345                             ((uint64_t)p_sys->p_info->sequence->display_width) *
346                             p_sys->p_info->sequence->pixel_width *
347                             VOUT_ASPECT_FACTOR /
348                             p_sys->p_info->sequence->display_height /
349                             p_sys->p_info->sequence->pixel_height;
350                     }
351                     else
352                     {
353                         /* Invalid aspect, assume 4:3.
354                         * This shouldn't happen and if it does it is a bug
355                         * in libmpeg2 (likely triggered by an invalid stream) */
356                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
357                     }
358                 }
359
360                 msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
361                         p_sys->p_info->sequence->width,
362                         p_sys->p_info->sequence->height, p_sys->i_aspect,
363                         (uint32_t)((uint64_t)1001000000 * 27 /
364                             p_sys->p_info->sequence->frame_period / 1001),
365                         (uint32_t)((uint64_t)1001000000 * 27 /
366                             p_sys->p_info->sequence->frame_period % 1001) );
367
368                 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
369
370                 /* Set the first 2 reference frames */
371                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
372
373                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
374                 {
375                     block_Release( p_block );
376                     return NULL;
377                 }
378                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
379
380                 p_pic->date = 0;
381                 decoder_LinkPicture( p_dec, p_pic );
382
383                 if( p_sys->p_synchro )
384                 {
385                     decoder_SynchroRelease( p_sys->p_synchro );
386                 }
387                 p_sys->p_synchro = decoder_SynchroInit( p_dec,
388                     (uint32_t)((uint64_t)1001000000 * 27 /
389                     p_sys->p_info->sequence->frame_period) );
390                 p_sys->b_after_sequence_header = 1;
391             }
392             break;
393
394             case STATE_PICTURE_2ND:
395                 decoder_SynchroNewPicture( p_sys->p_synchro,
396                         p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
397                         p_sys->p_info->current_picture->nb_fields,
398                         0, 0, p_sys->i_current_rate,
399                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
400
401                 if( p_sys->b_skip )
402                 {
403                     decoder_SynchroTrash( p_sys->p_synchro );
404                 }
405                 else
406                 {
407                     decoder_SynchroDecode( p_sys->p_synchro );
408                 }
409                 break;
410
411             case STATE_PICTURE:
412             {
413                 uint8_t *buf[3];
414                 mtime_t i_pts;
415                 buf[0] = buf[1] = buf[2] = NULL;
416
417                 if ( p_sys->b_after_sequence_header &&
418                     ((p_sys->p_info->current_picture->flags &
419                         PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
420                 {
421                     /* Intra-slice refresh. Simulate a blank I picture. */
422                     msg_Dbg( p_dec, "intra-slice refresh stream" );
423                     decoder_SynchroNewPicture( p_sys->p_synchro,
424                         I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate,
425                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
426                     decoder_SynchroDecode( p_sys->p_synchro );
427                     decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
428                     p_sys->b_slice_i = 1;
429                 }
430                 p_sys->b_after_sequence_header = 0;
431
432 #ifdef PIC_FLAG_PTS
433                 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
434                     ( ( p_sys->p_info->current_picture->pts ==
435                         (uint32_t)p_sys->i_current_pts ) ?
436                     p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
437 #else /* New interface */
438                 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
439                     ( ( p_sys->p_info->current_picture->tag ==
440                         (uint32_t)p_sys->i_current_pts ) ?
441                     p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
442 #endif
443                 /* Hack to handle demuxers which only have DTS timestamps */
444                 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
445                 {
446                     if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
447                         (p_sys->p_info->current_picture->flags &
448                         PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
449                     {
450                         i_pts = p_block->i_dts;
451                     }
452                 }
453                 p_block->i_pts = p_block->i_dts = 0;
454                 /* End hack */
455
456                 decoder_SynchroNewPicture( p_sys->p_synchro,
457                     p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
458                     p_sys->p_info->current_picture->nb_fields, i_pts,
459                     0, p_sys->i_current_rate,
460                     p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
461
462                 if ( !(p_sys->b_slice_i
463                     && ((p_sys->p_info->current_picture->flags
464                             & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
465                     && !decoder_SynchroChoose( p_sys->p_synchro,
466                                 p_sys->p_info->current_picture->flags
467                                     & PIC_MASK_CODING_TYPE,
468                                 /*FindVout(p_dec)->render_time*/ 0 /*FIXME*/,
469                                 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
470                 {
471                     mpeg2_skip( p_sys->p_mpeg2dec, 1 );
472                     p_sys->b_skip = 1;
473                     decoder_SynchroTrash( p_sys->p_synchro );
474                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
475                 }
476                 else
477                 {
478                     mpeg2_skip( p_sys->p_mpeg2dec, 0 );
479                     p_sys->b_skip = 0;
480                     decoder_SynchroDecode( p_sys->p_synchro );
481
482                     if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
483                     {
484                         block_Release( p_block );
485                         return NULL;
486                     }
487
488                     p_sys->p_mpeg2dec->ptr_forward_ref_picture = p_sys->p_mpeg2dec->fbuf[2]->id;
489                     p_sys->p_mpeg2dec->ptr_backward_ref_picture = p_sys->p_mpeg2dec->fbuf[1]->id;
490
491                     if ((p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE) != B_CODING_TYPE)
492                     {
493                         //if (p_sys->p_mpeg2dec->ptr_forward_ref_picture &&
494                         //    p_sys->p_mpeg2dec->ptr_forward_ref_picture != picture->backward_reference_frame)
495                         //    p_pic->forward_reference_frame->free (p_pic->forward_reference_frame);
496
497                         p_sys->p_mpeg2dec->ptr_forward_ref_picture =
498                                     p_sys->p_mpeg2dec->ptr_backward_ref_picture;
499                         p_sys->p_mpeg2dec->ptr_backward_ref_picture = (void *)p_pic;
500                     }
501                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
502                 }
503             }
504             break;
505
506             case STATE_END:
507             case STATE_SLICE:
508                 p_pic = NULL;
509                 if( p_sys->p_info->display_fbuf
510                     && p_sys->p_info->display_fbuf->id )
511                 {
512                     p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
513
514                     decoder_SynchroEnd( p_sys->p_synchro,
515                                 p_sys->p_info->display_picture->flags
516                                 & PIC_MASK_CODING_TYPE,
517                                 p_sys->b_garbage_pic );
518                     p_sys->b_garbage_pic = 0;
519
520                     if ( p_sys->p_picture_to_destroy != p_pic )
521                     {
522                         p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
523                     }
524                     else
525                     {
526                         p_sys->p_picture_to_destroy = NULL;
527                         p_pic->date = 0;
528                     }
529                 }
530
531                 if( p_sys->p_info->discard_fbuf &&
532                     p_sys->p_info->discard_fbuf->id )
533                 {
534                     decoder_UnlinkPicture( p_dec, p_sys->p_info->discard_fbuf->id );
535                 }
536                 /* For still frames */
537                 //if( state == STATE_END && p_pic ) p_pic->b_force = true;
538
539                 if( p_pic )
540                 {
541 #if 0
542                     /* Avoid frames with identical timestamps.
543                      * Especially needed for still frames in DVD menus. */
544                      if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
545                         p_sys->i_last_frame_pts = p_pic->date;
546 #endif
547                     return p_pic;
548                 }
549                 break;
550
551             case STATE_INVALID:
552             {
553                 uint8_t *buf[3];
554                 buf[0] = buf[1] = buf[2] = NULL;
555
556                 msg_Warn( p_dec, "invalid picture encountered" );
557                 if ( ( p_sys->p_info->current_picture == NULL ) ||
558                 ( ( p_sys->p_info->current_picture->flags &
559                     PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
560                 {
561                     if( p_sys->p_synchro ) decoder_SynchroReset( p_sys->p_synchro );
562                 }
563                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
564                 p_sys->b_skip = 1;
565
566                 if( p_sys->p_info->current_fbuf &&
567                     p_sys->p_info->current_fbuf->id )
568                 {
569                     p_sys->b_garbage_pic = 1;
570                     p_pic = p_sys->p_info->current_fbuf->id;
571                 }
572                 else if( !p_sys->p_info->sequence )
573                 {
574                     break;
575                 }
576                 else
577                 {
578                     if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
579                         break;
580                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
581                 }
582                 p_sys->p_picture_to_destroy = p_pic;
583
584                 memset( p_pic->p[0].p_pixels, 0,
585                         p_sys->p_info->sequence->width
586                         * p_sys->p_info->sequence->height );
587                 memset( p_pic->p[1].p_pixels, 0x80,
588                         p_sys->p_info->sequence->width
589                         * p_sys->p_info->sequence->height / 4 );
590                 memset( p_pic->p[2].p_pixels, 0x80,
591                         p_sys->p_info->sequence->width
592                         * p_sys->p_info->sequence->height / 4 );
593
594                 if( p_sys->b_slice_i )
595                 {
596                     decoder_SynchroNewPicture( p_sys->p_synchro,
597                         I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate,
598                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
599                     decoder_SynchroDecode( p_sys->p_synchro );
600                     decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
601                 }
602                 break;
603             }
604
605             default:
606                 break;
607         }
608     }
609     return NULL;
610 }
611
612 /*****************************************************************************
613  * CloseDecoder: libmpeg2 decoder destruction
614  *****************************************************************************/
615 static void CloseDecoder( vlc_object_t *p_this )
616 {
617     decoder_t *p_dec = (decoder_t *)p_this;
618     decoder_sys_t *p_sys = p_dec->p_sys;
619     FILE *f_wd_dec;
620
621     if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
622     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
623
624     f_wd_dec = fopen("/vlc/dec_pid", "w");
625     if (f_wd_dec != NULL)
626     {
627         fprintf(f_wd_dec, "0\n");
628         fflush(f_wd_dec);
629         fclose(f_wd_dec);
630     }
631     free( p_sys );
632 }
633
634 static double get_aspect_ratio( decoder_t *p_dec )
635 {
636     decoder_sys_t *p_sys = p_dec->p_sys;
637     double ratio;
638     double mpeg1_pel_ratio[16] = {1.0 /* forbidden */,
639         1.0, 0.6735, 0.7031, 0.7615, 0.8055, 0.8437, 0.8935, 0.9157,
640         0.9815, 1.0255, 1.0695, 1.0950, 1.1575, 1.2015, 1.0 /*reserved*/ };
641
642     if( !p_sys->p_mpeg2dec->decoder.mpeg1 )
643     {
644         /* these hardcoded values are defined on mpeg2 standard for
645         * aspect ratio. other values are reserved or forbidden.  */
646         switch( p_sys->p_mpeg2dec->decoder.aspect_ratio_information )
647         {
648             case 2:
649                 ratio = 4.0/3.0;
650                 break;
651             case 3:
652                 ratio = 16.0/9.0;
653                 break;
654             case 4:
655                 ratio = 2.11/1.0;
656                 break;
657             case 1:
658                 default:
659                 ratio = (double)p_sys->p_mpeg2dec->decoder.width/(double)p_sys->p_mpeg2dec->decoder.height;
660             break;
661         }
662     }
663     else
664     {
665         /* mpeg1 constants refer to pixel aspect ratio */
666         ratio = (double)p_sys->p_mpeg2dec->decoder.width/(double)p_sys->p_mpeg2dec->decoder.height;
667         ratio /= mpeg1_pel_ratio[p_sys->p_mpeg2dec->decoder.aspect_ratio_information];
668     }
669     return ratio;
670 }
671 /*****************************************************************************
672  * GetNewPicture: Get a new picture from the vout and set the buf struct
673  *****************************************************************************/
674 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
675 {
676     //msg_Dbg(p_dec, "GetNewPicture Entering");
677     decoder_sys_t *p_sys = p_dec->p_sys;
678     picture_t *p_pic;
679
680     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
681     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
682     p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
683
684     if( p_sys->p_info->sequence->frame_period > 0 )
685     {
686         p_dec->fmt_out.video.i_frame_rate =
687             (uint32_t)( (uint64_t)1001000000 * 27 /
688                         p_sys->p_info->sequence->frame_period );
689         p_dec->fmt_out.video.i_frame_rate_base = 1001;
690     }
691
692     p_dec->fmt_out.i_codec =
693         ( p_sys->p_info->sequence->chroma_height <
694           p_sys->p_info->sequence->height ) ?
695         VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
696
697 #if 0
698     p_sys->f_wd_nb = fopen("/vlc/dec_nb", "w");
699     if (p_sys->f_wd_nb != NULL)
700     {
701 //      fprintf(p_sys->f_wd_nb, "%d\n", mdate());
702         fprintf(p_sys->f_wd_nb, "%s\n", mdate());
703         fflush(p_sys->f_wd_nb);
704     }
705 #endif
706     p_pic = decoder_NewPicture( p_dec );
707
708     if( p_pic == NULL ) return NULL;
709
710     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
711         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
712     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
713         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
714     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
715         p_sys->p_info->current_picture->nb_fields : 2;
716     p_pic->format.i_frame_rate = p_dec->fmt_out.video.i_frame_rate;
717     p_pic->format.i_frame_rate_base = p_dec->fmt_out.video.i_frame_rate_base;
718
719     decoder_LinkPicture( p_dec, p_pic );
720
721     pp_buf[0] = p_pic->p[0].p_pixels;
722     pp_buf[1] = p_pic->p[1].p_pixels;
723     pp_buf[2] = p_pic->p[2].p_pixels;
724
725     /* let driver ensure this image has the right format */
726 #if 0
727     this->driver->update_frame_format( p_pic->p_sys->p_vout, p_pic,
728                                        p_dec->fmt_out.video.i_width,
729                                        p_dec->fmt_out.video.i_height,
730                                        p_dec->fmt_out.video.i_aspect,
731                                        format, flags);
732 #endif
733     mpeg2_xxmc_choose_coding( p_dec, &p_sys->p_mpeg2dec->decoder, p_pic,
734                               get_aspect_ratio(p_dec), 0 );
735     return p_pic;
736 }