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