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