1 /*****************************************************************************
2 * xxmc.c : HW MPEG decoder thread
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <<vlc_vout.h>>
30 #include <vlc_codec.h>
35 #include "attributes.h"
36 #include "mpeg2_internal.h"
39 #include "vout_synchro.h"
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) */
48 /*****************************************************************************
49 * decoder_sys_t : libmpeg2 decoder descriptor
50 *****************************************************************************/
56 mpeg2dec_t *p_mpeg2dec;
57 const mpeg2_info_t *p_info;
63 pes_packet_t *p_pes; /* current PES we are decoding */
65 mtime_t i_previous_pts;
66 mtime_t i_current_pts;
67 mtime_t i_previous_dts;
68 mtime_t i_current_dts;
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 */
79 vout_synchro_t *p_synchro;
81 mtime_t i_last_frame_pts;
85 /*****************************************************************************
87 *****************************************************************************/
89 static int OpenDecoder( vlc_object_t * );
90 static void CloseDecoder( vlc_object_t * );
92 static picture_t *DecodeBlock( decoder_t *, block_t ** );
94 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
96 /*****************************************************************************
98 *****************************************************************************/
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" );
106 /*****************************************************************************
107 * OpenDecoder: probe the decoder and return score
108 *****************************************************************************/
109 static int OpenDecoder( vlc_object_t *p_this )
112 decoder_t *p_dec = (decoder_t*)p_this;
113 decoder_sys_t *p_sys;
114 uint32_t i_accel = 0;
117 msg_Dbg(p_dec, "OpenDecoder Entering");
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') &&
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') )
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 )
136 msg_Err( p_dec, "out of memory" );
140 /* Initialize the thread properties */
141 memset( p_sys, 0, sizeof(decoder_sys_t) );
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;
156 #if defined( __i386__ )
157 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
159 i_accel |= MPEG2_ACCEL_X86_MMX;
162 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW )
164 i_accel |= MPEG2_ACCEL_X86_3DNOW;
167 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
169 i_accel |= MPEG2_ACCEL_X86_MMXEXT;
172 #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
173 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
175 i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
179 /* If we do not know this CPU, trust libmpeg2's feature detection */
180 i_accel = MPEG2_ACCEL_DETECT;
184 /* Set CPU acceleration features */
185 mpeg2_accel( i_accel );
187 /* Initialize decoder */
188 p_sys->p_mpeg2dec = mpeg2_init();
189 if( p_sys->p_mpeg2dec == NULL)
191 msg_Err( p_dec, "mpeg2_init() failed" );
196 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
198 p_dec->pf_decode_video = DecodeBlock;
200 f_wd_dec = fopen("/vlc/dec_pid", "w");
202 if (f_wd_dec != NULL)
204 fprintf(f_wd_dec, "%d\n", getpid());
209 msg_Dbg(p_dec, "OpenDecoder Leaving");
213 static void WriteDecodeFile(int value)
217 f_wd_ok = fopen("/vlc/dec_ok", "w");
220 fprintf(f_wd_ok, "%d", value);
226 /*****************************************************************************
227 * RunDecoder: the libmpeg2 decoder
228 *****************************************************************************/
229 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
231 decoder_sys_t *p_sys = p_dec->p_sys;
237 if( !pp_block || !*pp_block )
243 state = mpeg2_parse( p_sys->p_mpeg2dec );
247 if( !p_block->i_buffer )
249 block_Release( p_block );
252 if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
254 p_sys->p_info->sequence &&
255 p_sys->p_info->sequence->width != (unsigned int)-1 )
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 )
261 p_sys->b_garbage_pic = 1;
262 p_pic = p_sys->p_info->current_fbuf->id;
267 buf[0] = buf[1] = buf[2] = NULL;
268 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
270 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
272 p_sys->p_picture_to_destroy = p_pic;
274 if ( p_sys->b_slice_i )
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 );
286 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
288 #else /* New interface */
289 if( p_block->i_pts || p_block->i_dts )
291 mpeg2_tag_picture( p_sys->p_mpeg2dec,
292 (uint32_t)p_block->i_pts,
293 (uint32_t)p_block->i_dts );
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;
300 p_sys->i_current_rate = p_block->i_rate;
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;
309 /* Initialize video output */
311 buf[0] = buf[1] = buf[2] = NULL;
313 /* Check whether the input gave a particular aspect ratio */
314 if( p_dec->fmt_in.video.i_aspect )
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 )
321 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
323 case AR_16_9_PICTURE:
324 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
326 case AR_221_1_PICTURE:
327 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
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;
338 /* Use the value provided in the MPEG sequence header */
339 if( p_sys->p_info->sequence->pixel_height > 0 )
342 ((uint64_t)p_sys->p_info->sequence->display_width) *
343 p_sys->p_info->sequence->pixel_width *
345 p_sys->p_info->sequence->display_height /
346 p_sys->p_info->sequence->pixel_height;
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;
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) );
365 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
367 /* Set the first 2 reference frames */
368 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
370 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
372 block_Release( p_block );
375 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
378 p_dec->pf_picture_link( p_dec, p_pic );
380 if( p_sys->p_synchro )
382 vout_SynchroRelease( p_sys->p_synchro );
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;
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 );
399 vout_SynchroTrash( p_sys->p_synchro );
403 vout_SynchroDecode( p_sys->p_synchro );
411 buf[0] = buf[1] = buf[2] = NULL;
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) )
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;
425 p_sys->b_after_sequence_header = 0;
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;
438 /* Hack to handle demuxers which only have DTS timestamps */
439 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
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 )
445 i_pts = p_block->i_dts;
448 p_block->i_pts = p_block->i_dts = 0;
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 );
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*/ ) )
464 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
466 vout_SynchroTrash( p_sys->p_synchro );
467 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
471 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
473 vout_SynchroDecode( p_sys->p_synchro );
475 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
477 block_Release( p_block );
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;
484 if ((p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE) != B_CODING_TYPE)
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);
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;
494 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
502 if( p_sys->p_info->display_fbuf
503 && p_sys->p_info->display_fbuf->id )
505 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
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;
513 if ( p_sys->p_picture_to_destroy != p_pic )
515 p_pic->date = vout_SynchroDate( p_sys->p_synchro );
519 p_sys->p_picture_to_destroy = NULL;
524 if( p_sys->p_info->discard_fbuf &&
525 p_sys->p_info->discard_fbuf->id )
527 p_dec->pf_picture_unlink( p_dec, p_sys->p_info->discard_fbuf->id );
529 /* For still frames */
530 //if( state == STATE_END && p_pic ) p_pic->b_force = VLC_TRUE;
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;
547 buf[0] = buf[1] = buf[2] = NULL;
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 ) )
554 if( p_sys->p_synchro ) vout_SynchroReset( p_sys->p_synchro );
556 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
559 if( p_sys->p_info->current_fbuf &&
560 p_sys->p_info->current_fbuf->id )
562 p_sys->b_garbage_pic = 1;
563 p_pic = p_sys->p_info->current_fbuf->id;
565 else if( !p_sys->p_info->sequence )
571 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
573 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
575 p_sys->p_picture_to_destroy = p_pic;
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 );
587 if( p_sys->b_slice_i )
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 );
604 /*****************************************************************************
605 * CloseDecoder: libmpeg2 decoder destruction
606 *****************************************************************************/
607 static void CloseDecoder( vlc_object_t *p_this )
609 decoder_t *p_dec = (decoder_t *)p_this;
610 decoder_sys_t *p_sys = p_dec->p_sys;
613 if( p_sys->p_synchro ) vout_SynchroRelease( p_sys->p_synchro );
614 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
616 f_wd_dec = fopen("/vlc/dec_pid", "w");
617 if (f_wd_dec != NULL)
619 fprintf(f_wd_dec, "0\n");
626 static double get_aspect_ratio( decoder_t *p_dec )
628 decoder_sys_t *p_sys = p_dec->p_sys;
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*/ };
634 if( !p_sys->p_mpeg2dec->decoder.mpeg1 )
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 )
651 ratio = (double)p_sys->p_mpeg2dec->decoder.width/(double)p_sys->p_mpeg2dec->decoder.height;
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];
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 )
668 //msg_Dbg(p_dec, "GetNewPicture Entering");
669 decoder_sys_t *p_sys = p_dec->p_sys;
671 static int nbpic = 0;
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;
677 if( p_sys->p_info->sequence->frame_period > 0 )
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;
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');
691 p_sys->f_wd_nb = fopen("/vlc/dec_nb", "w");
692 if (p_sys->f_wd_nb != NULL)
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);
699 p_pic = p_dec->pf_vout_buffer_new( p_dec );
701 if( p_pic == NULL ) return NULL;
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;
712 p_dec->pf_picture_link( p_dec, p_pic );
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;
718 /* let driver ensure this image has the right format */
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,
726 mpeg2_xxmc_choose_coding( p_dec, &p_sys->p_mpeg2dec->decoder, p_pic,
727 get_aspect_ratio(p_dec), 0 );