]> git.sesse.net Git - vlc/blob - modules/codec/xvmc/xxmc.c
Use <vlc_cpu.h>
[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_codec.h>
35 #include <vlc_codec_synchro.h>
36 #include <vlc_cpu.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_CODEC_MPGV )
125         return VLC_EGENERIC;
126     /* Select onl recognized original format (standard mpeg video) */
127     switch( p_dec->fmt_in.i_original_fourcc )
128     {
129     case VLC_FOURCC('m','p','g','1'):
130     case VLC_FOURCC('m','p','g','2'):
131     case VLC_FOURCC('m','p','g','v'):
132     /* Pinnacle hardware-mpeg1 */
133     case VLC_FOURCC('P','I','M','1'):
134     /* VIA hardware-mpeg2 */
135     case VLC_FOURCC('X','x','M','C'):
136     /* ATI Video */
137     case VLC_FOURCC('V','C','R','2'):
138         break;
139     default:
140         if( p_dec->fmt_in.i_original_fourcc )
141             return VLC_EGENERIC;
142         break;
143     }
144
145     msg_Dbg(p_dec, "OpenDecoder Entering");
146
147     /* Allocate the memory needed to store the decoder's structure */
148     p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys) );
149     if( !p_sys )
150         return VLC_ENOMEM;
151
152     /* Initialize the thread properties */
153     p_sys->p_mpeg2dec = NULL;
154     p_sys->p_synchro  = NULL;
155     p_sys->p_info     = NULL;
156     p_sys->i_pts      = mdate() + DEFAULT_PTS_DELAY;
157     p_sys->i_current_pts  = 0;
158     p_sys->i_previous_pts = 0;
159     p_sys->i_current_dts  = 0;
160     p_sys->i_previous_dts = 0;
161     p_sys->p_picture_to_destroy = NULL;
162     p_sys->b_garbage_pic = 0;
163     p_sys->b_slice_i  = 0;
164     p_sys->b_skip     = 0;
165
166 #if defined( __i386__ )
167     if( vlc_CPU() & CPU_CAPABILITY_MMX )
168     {
169         i_accel |= MPEG2_ACCEL_X86_MMX;
170     }
171
172     if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
173     {
174         i_accel |= MPEG2_ACCEL_X86_3DNOW;
175     }
176
177     if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
178     {
179         i_accel |= MPEG2_ACCEL_X86_MMXEXT;
180     }
181
182 #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
183     if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
184     {
185         i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
186     }
187
188 #else
189     /* If we do not know this CPU, trust libmpeg2's feature detection */
190     i_accel = MPEG2_ACCEL_DETECT;
191
192 #endif
193
194     /* Set CPU acceleration features */
195     mpeg2_accel( i_accel );
196
197     /* Initialize decoder */
198     p_sys->p_mpeg2dec = mpeg2_init();
199     if( p_sys->p_mpeg2dec == NULL)
200     {
201         msg_Err( p_dec, "mpeg2_init() failed" );
202         free( p_sys );
203         return VLC_EGENERIC;
204     }
205
206     p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
207
208     p_dec->pf_decode_video = DecodeBlock;
209     p_dec->fmt_out.i_cat = VIDEO_ES;
210     p_dec->fmt_out.i_codec = 0;
211
212     f_wd_dec = fopen("/vlc/dec_pid", "w");
213     if (f_wd_dec != NULL)
214     {
215         fprintf(f_wd_dec, "%d\n", getpid());
216         fflush(f_wd_dec);
217         fclose(f_wd_dec);
218     }
219
220     msg_Dbg(p_dec, "OpenDecoder Leaving");
221     return VLC_SUCCESS;
222 }
223
224 #if 0
225 static void WriteDecodeFile(int value)
226 {
227     FILE *f_wd_ok;
228
229     f_wd_ok = fopen("/vlc/dec_ok", "w");
230     if (f_wd_ok != NULL)
231     {
232         fprintf(f_wd_ok, "%d", value);
233         fflush(f_wd_ok);
234         fclose(f_wd_ok);
235     }
236 }
237 #endif
238
239 /*****************************************************************************
240  * RunDecoder: the libmpeg2 decoder
241  *****************************************************************************/
242 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
243 {
244     decoder_sys_t   *p_sys = p_dec->p_sys;
245     mpeg2_state_t   state;
246     picture_t       *p_pic;
247
248     block_t *p_block;
249
250     if( !pp_block || !*pp_block )
251         return NULL;
252
253     p_block = *pp_block;
254     while( 1 )
255     {
256         state = mpeg2_parse( p_sys->p_mpeg2dec );
257         switch( state )
258         {
259             case STATE_BUFFER:
260                 if( !p_block->i_buffer )
261                 {
262                     block_Release( p_block );
263                     return NULL;
264                 }
265                 if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
266                     p_sys->p_synchro &&
267                     p_sys->p_info->sequence &&
268                     p_sys->p_info->sequence->width != (unsigned int)-1 )
269                 {
270                     decoder_SynchroReset( p_sys->p_synchro );
271                     if( p_sys->p_info->current_fbuf != NULL
272                         && p_sys->p_info->current_fbuf->id != NULL )
273                     {
274                         p_sys->b_garbage_pic = 1;
275                         p_pic = p_sys->p_info->current_fbuf->id;
276                     }
277                     else
278                     {
279                         uint8_t *buf[3];
280                         buf[0] = buf[1] = buf[2] = NULL;
281                         if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
282                             break;
283                         mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
284                     }
285                     p_sys->p_picture_to_destroy = p_pic;
286
287                     if ( p_sys->b_slice_i )
288                     {
289                         decoder_SynchroNewPicture( p_sys->p_synchro,
290                             I_CODING_TYPE, 2, 0, 0,
291                             p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
292                         decoder_SynchroDecode( p_sys->p_synchro );
293                         decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
294                     }
295                 }
296
297 #ifdef PIC_FLAG_PTS
298                 if( p_block->i_pts )
299                 {
300                     mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
301
302 #else /* New interface */
303                 if( p_block->i_pts || p_block->i_dts )
304                 {
305                     mpeg2_tag_picture( p_sys->p_mpeg2dec,
306                                         (uint32_t)p_block->i_pts,
307                                         (uint32_t)p_block->i_dts );
308 #endif
309                     p_sys->i_previous_pts = p_sys->i_current_pts;
310                     p_sys->i_current_pts = p_block->i_pts;
311                     p_sys->i_previous_dts = p_sys->i_current_dts;
312                     p_sys->i_current_dts = p_block->i_dts;
313                 }
314                 p_sys->i_current_rate = p_block->i_rate;
315
316                 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
317                                 p_block->p_buffer + p_block->i_buffer );
318                 p_block->i_buffer = 0;
319                 break;
320
321             case STATE_SEQUENCE:
322             {
323                 /* Initialize video output */
324                 uint8_t *buf[3];
325                 buf[0] = buf[1] = buf[2] = NULL;
326
327                 /* Check whether the input gave a particular aspect ratio */
328                 if( p_dec->fmt_in.video.i_aspect )
329                 {
330                     p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
331                     if( p_sys->i_aspect <= AR_221_1_PICTURE )
332                     switch( p_sys->i_aspect )
333                     {
334                     case AR_3_4_PICTURE:
335                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
336                         break;
337                     case AR_16_9_PICTURE:
338                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
339                         break;
340                     case AR_221_1_PICTURE:
341                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
342                         break;
343                     case AR_SQUARE_PICTURE:
344                         p_sys->i_aspect = VOUT_ASPECT_FACTOR *
345                                     p_sys->p_info->sequence->width /
346                                     p_sys->p_info->sequence->height;
347                         break;
348                     }
349                 }
350                 else
351                 {
352                     /* Use the value provided in the MPEG sequence header */
353                     if( p_sys->p_info->sequence->pixel_height > 0 )
354                     {
355                         p_sys->i_aspect =
356                             ((uint64_t)p_sys->p_info->sequence->display_width) *
357                             p_sys->p_info->sequence->pixel_width *
358                             VOUT_ASPECT_FACTOR /
359                             p_sys->p_info->sequence->display_height /
360                             p_sys->p_info->sequence->pixel_height;
361                     }
362                     else
363                     {
364                         /* Invalid aspect, assume 4:3.
365                         * This shouldn't happen and if it does it is a bug
366                         * in libmpeg2 (likely triggered by an invalid stream) */
367                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
368                     }
369                 }
370
371                 msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
372                         p_sys->p_info->sequence->width,
373                         p_sys->p_info->sequence->height, p_sys->i_aspect,
374                         (uint32_t)((uint64_t)1001000000 * 27 /
375                             p_sys->p_info->sequence->frame_period / 1001),
376                         (uint32_t)((uint64_t)1001000000 * 27 /
377                             p_sys->p_info->sequence->frame_period % 1001) );
378
379                 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
380
381                 /* Set the first 2 reference frames */
382                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
383
384                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
385                 {
386                     block_Release( p_block );
387                     return NULL;
388                 }
389                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
390
391                 p_pic->date = 0;
392                 decoder_LinkPicture( p_dec, p_pic );
393
394                 if( p_sys->p_synchro )
395                 {
396                     decoder_SynchroRelease( p_sys->p_synchro );
397                 }
398                 p_sys->p_synchro = decoder_SynchroInit( p_dec,
399                     (uint32_t)((uint64_t)1001000000 * 27 /
400                     p_sys->p_info->sequence->frame_period) );
401                 p_sys->b_after_sequence_header = 1;
402             }
403             break;
404
405             case STATE_PICTURE_2ND:
406                 decoder_SynchroNewPicture( p_sys->p_synchro,
407                         p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
408                         p_sys->p_info->current_picture->nb_fields,
409                         0, 0,
410                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
411
412                 if( p_sys->b_skip )
413                 {
414                     decoder_SynchroTrash( p_sys->p_synchro );
415                 }
416                 else
417                 {
418                     decoder_SynchroDecode( p_sys->p_synchro );
419                 }
420                 break;
421
422             case STATE_PICTURE:
423             {
424                 uint8_t *buf[3];
425                 mtime_t i_pts;
426                 buf[0] = buf[1] = buf[2] = NULL;
427
428                 if ( p_sys->b_after_sequence_header &&
429                     ((p_sys->p_info->current_picture->flags &
430                         PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
431                 {
432                     /* Intra-slice refresh. Simulate a blank I picture. */
433                     msg_Dbg( p_dec, "intra-slice refresh stream" );
434                     decoder_SynchroNewPicture( p_sys->p_synchro,
435                         I_CODING_TYPE, 2, 0, 0,
436                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
437                     decoder_SynchroDecode( p_sys->p_synchro );
438                     decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
439                     p_sys->b_slice_i = 1;
440                 }
441                 p_sys->b_after_sequence_header = 0;
442
443 #ifdef PIC_FLAG_PTS
444                 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
445                     ( ( p_sys->p_info->current_picture->pts ==
446                         (uint32_t)p_sys->i_current_pts ) ?
447                     p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
448 #else /* New interface */
449                 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
450                     ( ( p_sys->p_info->current_picture->tag ==
451                         (uint32_t)p_sys->i_current_pts ) ?
452                     p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
453 #endif
454                 /* Hack to handle demuxers which only have DTS timestamps */
455                 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
456                 {
457                     if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
458                         (p_sys->p_info->current_picture->flags &
459                         PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
460                     {
461                         i_pts = p_block->i_dts;
462                     }
463                 }
464                 p_block->i_pts = p_block->i_dts = 0;
465                 /* End hack */
466
467                 decoder_SynchroNewPicture( p_sys->p_synchro,
468                     p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
469                     p_sys->p_info->current_picture->nb_fields, i_pts,
470                     0,
471                     p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
472
473                 if ( !(p_sys->b_slice_i
474                     && ((p_sys->p_info->current_picture->flags
475                             & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
476                     && !decoder_SynchroChoose( p_sys->p_synchro,
477                                 p_sys->p_info->current_picture->flags
478                                     & PIC_MASK_CODING_TYPE,
479                                 /*FindVout(p_dec)->render_time*/ 0 /*FIXME*/,
480                                 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
481                 {
482                     mpeg2_skip( p_sys->p_mpeg2dec, 1 );
483                     p_sys->b_skip = 1;
484                     decoder_SynchroTrash( p_sys->p_synchro );
485                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
486                 }
487                 else
488                 {
489                     mpeg2_skip( p_sys->p_mpeg2dec, 0 );
490                     p_sys->b_skip = 0;
491                     decoder_SynchroDecode( p_sys->p_synchro );
492
493                     if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
494                     {
495                         block_Release( p_block );
496                         return NULL;
497                     }
498
499                     //p_sys->p_mpeg2dec->ptr_forward_ref_picture = p_sys->p_mpeg2dec->fbuf[2]->id;
500                     //p_sys->p_mpeg2dec->ptr_backward_ref_picture = p_sys->p_mpeg2dec->fbuf[1]->id;
501
502                     if ((p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE) != B_CODING_TYPE)
503                     {
504                         //if (p_sys->p_mpeg2dec->ptr_forward_ref_picture &&
505                         //    p_sys->p_mpeg2dec->ptr_forward_ref_picture != picture->backward_reference_frame)
506                         //    p_pic->forward_reference_frame->free (p_pic->forward_reference_frame);
507
508                         //p_sys->p_mpeg2dec->ptr_forward_ref_picture =
509                         //            p_sys->p_mpeg2dec->ptr_backward_ref_picture;
510                         //p_sys->p_mpeg2dec->ptr_backward_ref_picture = (void *)p_pic;
511                     }
512                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
513                 }
514             }
515             break;
516
517             case STATE_END:
518             case STATE_SLICE:
519                 p_pic = NULL;
520                 if( p_sys->p_info->display_fbuf
521                     && p_sys->p_info->display_fbuf->id )
522                 {
523                     p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
524
525                     decoder_SynchroEnd( p_sys->p_synchro,
526                                 p_sys->p_info->display_picture->flags
527                                 & PIC_MASK_CODING_TYPE,
528                                 p_sys->b_garbage_pic );
529                     p_sys->b_garbage_pic = 0;
530
531                     if ( p_sys->p_picture_to_destroy != p_pic )
532                     {
533                         p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
534                     }
535                     else
536                     {
537                         p_sys->p_picture_to_destroy = NULL;
538                         p_pic->date = 0;
539                     }
540                 }
541
542                 if( p_sys->p_info->discard_fbuf &&
543                     p_sys->p_info->discard_fbuf->id )
544                 {
545                     decoder_UnlinkPicture( p_dec, p_sys->p_info->discard_fbuf->id );
546                 }
547                 /* For still frames */
548                 //if( state == STATE_END && p_pic ) p_pic->b_force = true;
549
550                 if( p_pic )
551                 {
552 #if 0
553                     /* Avoid frames with identical timestamps.
554                      * Especially needed for still frames in DVD menus. */
555                      if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
556                         p_sys->i_last_frame_pts = p_pic->date;
557 #endif
558                     return p_pic;
559                 }
560                 break;
561
562             case STATE_INVALID:
563             {
564                 uint8_t *buf[3];
565                 buf[0] = buf[1] = buf[2] = NULL;
566
567                 msg_Warn( p_dec, "invalid picture encountered" );
568                 if ( ( p_sys->p_info->current_picture == NULL ) ||
569                 ( ( p_sys->p_info->current_picture->flags &
570                     PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
571                 {
572                     if( p_sys->p_synchro ) decoder_SynchroReset( p_sys->p_synchro );
573                 }
574                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
575                 p_sys->b_skip = 1;
576
577                 if( p_sys->p_info->current_fbuf &&
578                     p_sys->p_info->current_fbuf->id )
579                 {
580                     p_sys->b_garbage_pic = 1;
581                     p_pic = p_sys->p_info->current_fbuf->id;
582                 }
583                 else if( !p_sys->p_info->sequence )
584                 {
585                     break;
586                 }
587                 else
588                 {
589                     if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
590                         break;
591                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
592                 }
593                 p_sys->p_picture_to_destroy = p_pic;
594
595                 memset( p_pic->p[0].p_pixels, 0,
596                         p_sys->p_info->sequence->width
597                         * p_sys->p_info->sequence->height );
598                 memset( p_pic->p[1].p_pixels, 0x80,
599                         p_sys->p_info->sequence->width
600                         * p_sys->p_info->sequence->height / 4 );
601                 memset( p_pic->p[2].p_pixels, 0x80,
602                         p_sys->p_info->sequence->width
603                         * p_sys->p_info->sequence->height / 4 );
604
605                 if( p_sys->b_slice_i )
606                 {
607                     decoder_SynchroNewPicture( p_sys->p_synchro,
608                         I_CODING_TYPE, 2, 0, 0,
609                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
610                     decoder_SynchroDecode( p_sys->p_synchro );
611                     decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
612                 }
613                 break;
614             }
615
616             default:
617                 break;
618         }
619     }
620     return NULL;
621 }
622
623 /*****************************************************************************
624  * CloseDecoder: libmpeg2 decoder destruction
625  *****************************************************************************/
626 static void CloseDecoder( vlc_object_t *p_this )
627 {
628     decoder_t *p_dec = (decoder_t *)p_this;
629     decoder_sys_t *p_sys = p_dec->p_sys;
630     FILE *f_wd_dec;
631
632     if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
633     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
634
635     f_wd_dec = fopen("/vlc/dec_pid", "w");
636     if (f_wd_dec != NULL)
637     {
638         fprintf(f_wd_dec, "0\n");
639         fflush(f_wd_dec);
640         fclose(f_wd_dec);
641     }
642     free( p_sys );
643 }
644
645 #if 0
646 static double get_aspect_ratio( decoder_t *p_dec )
647 {
648     decoder_sys_t *p_sys = p_dec->p_sys;
649     double ratio;
650     double mpeg1_pel_ratio[16] = {1.0 /* forbidden */,
651         1.0, 0.6735, 0.7031, 0.7615, 0.8055, 0.8437, 0.8935, 0.9157,
652         0.9815, 1.0255, 1.0695, 1.0950, 1.1575, 1.2015, 1.0 /*reserved*/ };
653
654     if( !p_sys->p_mpeg2dec->decoder.mpeg1 )
655     {
656         /* these hardcoded values are defined on mpeg2 standard for
657         * aspect ratio. other values are reserved or forbidden.  */
658         /*switch( p_sys->p_mpeg2dec->decoder.aspect_ratio_information )
659         {
660             case 2:
661                 ratio = 4.0/3.0;
662                 break;
663             case 3:
664                 ratio = 16.0/9.0;
665                 break;
666             case 4:
667                 ratio = 2.11/1.0;
668                 break;
669             case 1:
670                 default:*/
671                 ratio = (double)p_sys->p_mpeg2dec->decoder.width/(double)p_sys->p_mpeg2dec->decoder.height;
672         /*    break;
673         }*/
674     }
675     else
676     {
677         /* mpeg1 constants refer to pixel aspect ratio */
678         ratio = (double)p_sys->p_mpeg2dec->decoder.width/(double)p_sys->p_mpeg2dec->decoder.height;
679         /* ratio /= mpeg1_pel_ratio[p_sys->p_mpeg2dec->decoder.aspect_ratio_information]; */
680     }
681     return ratio;
682 }
683 #endif
684
685 /*****************************************************************************
686  * GetNewPicture: Get a new picture from the vout and set the buf struct
687  *****************************************************************************/
688 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
689 {
690     //msg_Dbg(p_dec, "GetNewPicture Entering");
691     decoder_sys_t *p_sys = p_dec->p_sys;
692     picture_t *p_pic;
693
694     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
695     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
696     p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
697
698     if( p_sys->p_info->sequence->frame_period > 0 )
699     {
700         p_dec->fmt_out.video.i_frame_rate =
701             (uint32_t)( (uint64_t)1001000000 * 27 /
702                         p_sys->p_info->sequence->frame_period );
703         p_dec->fmt_out.video.i_frame_rate_base = 1001;
704     }
705
706     p_dec->fmt_out.i_codec =
707         ( p_sys->p_info->sequence->chroma_height <
708           p_sys->p_info->sequence->height ) ?
709         VLC_CODEC_I420 : VLC_CODEC_I422;
710
711 #if 0
712     p_sys->f_wd_nb = fopen("/vlc/dec_nb", "w");
713     if (p_sys->f_wd_nb != NULL)
714     {
715 //      fprintf(p_sys->f_wd_nb, "%d\n", mdate());
716         fprintf(p_sys->f_wd_nb, "%s\n", mdate());
717         fflush(p_sys->f_wd_nb);
718     }
719 #endif
720     p_pic = decoder_NewPicture( p_dec );
721
722     if( p_pic == NULL ) return NULL;
723
724     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
725         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
726     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
727         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
728     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
729         p_sys->p_info->current_picture->nb_fields : 2;
730     p_pic->format.i_frame_rate = p_dec->fmt_out.video.i_frame_rate;
731     p_pic->format.i_frame_rate_base = p_dec->fmt_out.video.i_frame_rate_base;
732
733     decoder_LinkPicture( p_dec, p_pic );
734
735     pp_buf[0] = p_pic->p[0].p_pixels;
736     pp_buf[1] = p_pic->p[1].p_pixels;
737     pp_buf[2] = p_pic->p[2].p_pixels;
738
739     /* let driver ensure this image has the right format */
740 #if 0
741     this->driver->update_frame_format( p_pic->p_sys->p_vout, p_pic,
742                                        p_dec->fmt_out.video.i_width,
743                                        p_dec->fmt_out.video.i_height,
744                                        p_dec->fmt_out.video.i_aspect,
745                                        format, flags);
746     mpeg2_xxmc_choose_coding( p_dec, &p_sys->p_mpeg2dec->decoder, p_pic,
747                               get_aspect_ratio(p_dec), 0 );
748 #endif
749     return p_pic;
750 }