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