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