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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
36 #include <vlc_codec_synchro.h>
44 //#include "attributes.h"
45 #include "mpeg2_internal.h"
46 //#include "xvmc_vld.h"
48 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
49 #define AR_SQUARE_PICTURE 1 /* square pixels */
50 #define AR_3_4_PICTURE 2 /* 3:4 picture (TV) */
51 #define AR_16_9_PICTURE 3 /* 16:9 picture (wide screen) */
52 #define AR_221_1_PICTURE 4 /* 2.21:1 picture (movie) */
54 /*****************************************************************************
55 * decoder_sys_t : libmpeg2 decoder descriptor
56 *****************************************************************************/
62 mpeg2dec_t *p_mpeg2dec;
63 const mpeg2_info_t *p_info;
70 mtime_t i_previous_pts;
71 mtime_t i_current_pts;
72 mtime_t i_previous_dts;
73 mtime_t i_current_dts;
75 picture_t * p_picture_to_destroy;
77 bool b_after_sequence_header; /* is it the next frame after
78 * the sequence header ? */
79 bool b_slice_i; /* intra-slice refresh stream */
84 decoder_synchro_t *p_synchro;
86 mtime_t i_last_frame_pts;
89 /*****************************************************************************
91 *****************************************************************************/
93 static int OpenDecoder( vlc_object_t * );
94 static void CloseDecoder( vlc_object_t * );
96 static picture_t *DecodeBlock( decoder_t *, block_t ** );
98 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
100 /*****************************************************************************
102 *****************************************************************************/
104 set_description( N_("MPEG I/II hw video decoder (using libmpeg2)") )
105 set_capability( "decoder", 140 )
106 set_callbacks( OpenDecoder, CloseDecoder )
107 add_shortcut( "xxmc" )
110 /*****************************************************************************
111 * OpenDecoder: probe the decoder and return score
112 *****************************************************************************/
113 static int OpenDecoder( vlc_object_t *p_this )
116 decoder_t *p_dec = (decoder_t*)p_this;
117 decoder_sys_t *p_sys = NULL;
118 uint32_t i_accel = 0;
124 if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGV )
126 /* Select onl recognized original format (standard mpeg video) */
127 switch( p_dec->fmt_in.i_original_fourcc )
129 case VLC_FOURCC('m','p','g','1'):
130 case VLC_FOURCC('m','p','g','2'):
131 case VLC_FOURCC('m','p','g','v'):
132 /* Pinnacle hardware-mpeg1 */
133 case VLC_FOURCC('P','I','M','1'):
134 /* VIA hardware-mpeg2 */
135 case VLC_FOURCC('X','x','M','C'):
137 case VLC_FOURCC('V','C','R','2'):
140 if( p_dec->fmt_in.i_original_fourcc )
145 msg_Dbg(p_dec, "OpenDecoder Entering");
147 /* Allocate the memory needed to store the decoder's structure */
148 p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys) );
152 /* Initialize the thread properties */
153 p_sys->p_mpeg2dec = NULL;
154 p_sys->p_synchro = NULL;
155 p_sys->p_info = NULL;
156 p_sys->i_pts = mdate() + DEFAULT_PTS_DELAY;
157 p_sys->i_current_pts = 0;
158 p_sys->i_previous_pts = 0;
159 p_sys->i_current_dts = 0;
160 p_sys->i_previous_dts = 0;
161 p_sys->p_picture_to_destroy = NULL;
162 p_sys->b_garbage_pic = 0;
163 p_sys->b_slice_i = 0;
166 #if defined( __i386__ )
167 if( vlc_CPU() & CPU_CAPABILITY_MMX )
169 i_accel |= MPEG2_ACCEL_X86_MMX;
172 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
174 i_accel |= MPEG2_ACCEL_X86_3DNOW;
177 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
179 i_accel |= MPEG2_ACCEL_X86_MMXEXT;
182 #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
183 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
185 i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
189 /* If we do not know this CPU, trust libmpeg2's feature detection */
190 i_accel = MPEG2_ACCEL_DETECT;
194 /* Set CPU acceleration features */
195 mpeg2_accel( i_accel );
197 /* Initialize decoder */
198 p_sys->p_mpeg2dec = mpeg2_init();
199 if( p_sys->p_mpeg2dec == NULL)
201 msg_Err( p_dec, "mpeg2_init() failed" );
206 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
208 p_dec->pf_decode_video = DecodeBlock;
209 p_dec->fmt_out.i_cat = VIDEO_ES;
210 p_dec->fmt_out.i_codec = 0;
212 f_wd_dec = fopen("/vlc/dec_pid", "w");
213 if (f_wd_dec != NULL)
215 fprintf(f_wd_dec, "%d\n", getpid());
220 msg_Dbg(p_dec, "OpenDecoder Leaving");
224 static void WriteDecodeFile(int value)
228 f_wd_ok = fopen("/vlc/dec_ok", "w");
231 fprintf(f_wd_ok, "%d", value);
237 /*****************************************************************************
238 * RunDecoder: the libmpeg2 decoder
239 *****************************************************************************/
240 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
242 decoder_sys_t *p_sys = p_dec->p_sys;
248 if( !pp_block || !*pp_block )
254 state = mpeg2_parse( p_sys->p_mpeg2dec );
258 if( !p_block->i_buffer )
260 block_Release( p_block );
263 if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
265 p_sys->p_info->sequence &&
266 p_sys->p_info->sequence->width != (unsigned int)-1 )
268 decoder_SynchroReset( p_sys->p_synchro );
269 if( p_sys->p_info->current_fbuf != NULL
270 && p_sys->p_info->current_fbuf->id != NULL )
272 p_sys->b_garbage_pic = 1;
273 p_pic = p_sys->p_info->current_fbuf->id;
278 buf[0] = buf[1] = buf[2] = NULL;
279 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
281 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
283 p_sys->p_picture_to_destroy = p_pic;
285 if ( p_sys->b_slice_i )
287 decoder_SynchroNewPicture( p_sys->p_synchro,
288 I_CODING_TYPE, 2, 0, 0,
289 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
290 decoder_SynchroDecode( p_sys->p_synchro );
291 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
298 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
300 #else /* New interface */
301 if( p_block->i_pts || p_block->i_dts )
303 mpeg2_tag_picture( p_sys->p_mpeg2dec,
304 (uint32_t)p_block->i_pts,
305 (uint32_t)p_block->i_dts );
307 p_sys->i_previous_pts = p_sys->i_current_pts;
308 p_sys->i_current_pts = p_block->i_pts;
309 p_sys->i_previous_dts = p_sys->i_current_dts;
310 p_sys->i_current_dts = p_block->i_dts;
312 p_sys->i_current_rate = p_block->i_rate;
314 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
315 p_block->p_buffer + p_block->i_buffer );
316 p_block->i_buffer = 0;
321 /* Initialize video output */
323 buf[0] = buf[1] = buf[2] = NULL;
325 /* Check whether the input gave a particular aspect ratio */
326 if( p_dec->fmt_in.video.i_aspect )
328 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
329 if( p_sys->i_aspect <= AR_221_1_PICTURE )
330 switch( p_sys->i_aspect )
333 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
335 case AR_16_9_PICTURE:
336 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
338 case AR_221_1_PICTURE:
339 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
341 case AR_SQUARE_PICTURE:
342 p_sys->i_aspect = VOUT_ASPECT_FACTOR *
343 p_sys->p_info->sequence->width /
344 p_sys->p_info->sequence->height;
350 /* Use the value provided in the MPEG sequence header */
351 if( p_sys->p_info->sequence->pixel_height > 0 )
354 ((uint64_t)p_sys->p_info->sequence->display_width) *
355 p_sys->p_info->sequence->pixel_width *
357 p_sys->p_info->sequence->display_height /
358 p_sys->p_info->sequence->pixel_height;
362 /* Invalid aspect, assume 4:3.
363 * This shouldn't happen and if it does it is a bug
364 * in libmpeg2 (likely triggered by an invalid stream) */
365 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
369 msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
370 p_sys->p_info->sequence->width,
371 p_sys->p_info->sequence->height, p_sys->i_aspect,
372 (uint32_t)((uint64_t)1001000000 * 27 /
373 p_sys->p_info->sequence->frame_period / 1001),
374 (uint32_t)((uint64_t)1001000000 * 27 /
375 p_sys->p_info->sequence->frame_period % 1001) );
377 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
379 /* Set the first 2 reference frames */
380 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
382 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
384 block_Release( p_block );
387 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
390 decoder_LinkPicture( p_dec, p_pic );
392 if( p_sys->p_synchro )
394 decoder_SynchroRelease( p_sys->p_synchro );
396 p_sys->p_synchro = decoder_SynchroInit( p_dec,
397 (uint32_t)((uint64_t)1001000000 * 27 /
398 p_sys->p_info->sequence->frame_period) );
399 p_sys->b_after_sequence_header = 1;
403 case STATE_PICTURE_2ND:
404 decoder_SynchroNewPicture( p_sys->p_synchro,
405 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
406 p_sys->p_info->current_picture->nb_fields,
408 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
412 decoder_SynchroTrash( p_sys->p_synchro );
416 decoder_SynchroDecode( p_sys->p_synchro );
424 buf[0] = buf[1] = buf[2] = NULL;
426 if ( p_sys->b_after_sequence_header &&
427 ((p_sys->p_info->current_picture->flags &
428 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
430 /* Intra-slice refresh. Simulate a blank I picture. */
431 msg_Dbg( p_dec, "intra-slice refresh stream" );
432 decoder_SynchroNewPicture( p_sys->p_synchro,
433 I_CODING_TYPE, 2, 0, 0,
434 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
435 decoder_SynchroDecode( p_sys->p_synchro );
436 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
437 p_sys->b_slice_i = 1;
439 p_sys->b_after_sequence_header = 0;
442 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
443 ( ( p_sys->p_info->current_picture->pts ==
444 (uint32_t)p_sys->i_current_pts ) ?
445 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
446 #else /* New interface */
447 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
448 ( ( p_sys->p_info->current_picture->tag ==
449 (uint32_t)p_sys->i_current_pts ) ?
450 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
452 /* Hack to handle demuxers which only have DTS timestamps */
453 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
455 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
456 (p_sys->p_info->current_picture->flags &
457 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
459 i_pts = p_block->i_dts;
462 p_block->i_pts = p_block->i_dts = 0;
465 decoder_SynchroNewPicture( p_sys->p_synchro,
466 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
467 p_sys->p_info->current_picture->nb_fields, i_pts,
469 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
471 if ( !(p_sys->b_slice_i
472 && ((p_sys->p_info->current_picture->flags
473 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
474 && !decoder_SynchroChoose( p_sys->p_synchro,
475 p_sys->p_info->current_picture->flags
476 & PIC_MASK_CODING_TYPE,
477 /*FindVout(p_dec)->render_time*/ 0 /*FIXME*/,
478 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
480 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
482 decoder_SynchroTrash( p_sys->p_synchro );
483 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
487 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
489 decoder_SynchroDecode( p_sys->p_synchro );
491 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
493 block_Release( p_block );
497 //p_sys->p_mpeg2dec->ptr_forward_ref_picture = p_sys->p_mpeg2dec->fbuf[2]->id;
498 //p_sys->p_mpeg2dec->ptr_backward_ref_picture = p_sys->p_mpeg2dec->fbuf[1]->id;
500 if ((p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE) != B_CODING_TYPE)
502 //if (p_sys->p_mpeg2dec->ptr_forward_ref_picture &&
503 // p_sys->p_mpeg2dec->ptr_forward_ref_picture != picture->backward_reference_frame)
504 // p_pic->forward_reference_frame->free (p_pic->forward_reference_frame);
506 //p_sys->p_mpeg2dec->ptr_forward_ref_picture =
507 // p_sys->p_mpeg2dec->ptr_backward_ref_picture;
508 //p_sys->p_mpeg2dec->ptr_backward_ref_picture = (void *)p_pic;
510 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
518 if( p_sys->p_info->display_fbuf
519 && p_sys->p_info->display_fbuf->id )
521 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
523 decoder_SynchroEnd( p_sys->p_synchro,
524 p_sys->p_info->display_picture->flags
525 & PIC_MASK_CODING_TYPE,
526 p_sys->b_garbage_pic );
527 p_sys->b_garbage_pic = 0;
529 if ( p_sys->p_picture_to_destroy != p_pic )
531 p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
535 p_sys->p_picture_to_destroy = NULL;
540 if( p_sys->p_info->discard_fbuf &&
541 p_sys->p_info->discard_fbuf->id )
543 decoder_UnlinkPicture( p_dec, p_sys->p_info->discard_fbuf->id );
545 /* For still frames */
546 //if( state == STATE_END && p_pic ) p_pic->b_force = true;
551 /* Avoid frames with identical timestamps.
552 * Especially needed for still frames in DVD menus. */
553 if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
554 p_sys->i_last_frame_pts = p_pic->date;
563 buf[0] = buf[1] = buf[2] = NULL;
565 msg_Warn( p_dec, "invalid picture encountered" );
566 if ( ( p_sys->p_info->current_picture == NULL ) ||
567 ( ( p_sys->p_info->current_picture->flags &
568 PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
570 if( p_sys->p_synchro ) decoder_SynchroReset( p_sys->p_synchro );
572 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
575 if( p_sys->p_info->current_fbuf &&
576 p_sys->p_info->current_fbuf->id )
578 p_sys->b_garbage_pic = 1;
579 p_pic = p_sys->p_info->current_fbuf->id;
581 else if( !p_sys->p_info->sequence )
587 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
589 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
591 p_sys->p_picture_to_destroy = p_pic;
593 memset( p_pic->p[0].p_pixels, 0,
594 p_sys->p_info->sequence->width
595 * p_sys->p_info->sequence->height );
596 memset( p_pic->p[1].p_pixels, 0x80,
597 p_sys->p_info->sequence->width
598 * p_sys->p_info->sequence->height / 4 );
599 memset( p_pic->p[2].p_pixels, 0x80,
600 p_sys->p_info->sequence->width
601 * p_sys->p_info->sequence->height / 4 );
603 if( p_sys->b_slice_i )
605 decoder_SynchroNewPicture( p_sys->p_synchro,
606 I_CODING_TYPE, 2, 0, 0,
607 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
608 decoder_SynchroDecode( p_sys->p_synchro );
609 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
621 /*****************************************************************************
622 * CloseDecoder: libmpeg2 decoder destruction
623 *****************************************************************************/
624 static void CloseDecoder( vlc_object_t *p_this )
626 decoder_t *p_dec = (decoder_t *)p_this;
627 decoder_sys_t *p_sys = p_dec->p_sys;
630 if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
631 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
633 f_wd_dec = fopen("/vlc/dec_pid", "w");
634 if (f_wd_dec != NULL)
636 fprintf(f_wd_dec, "0\n");
643 static double get_aspect_ratio( decoder_t *p_dec )
645 decoder_sys_t *p_sys = p_dec->p_sys;
647 double mpeg1_pel_ratio[16] = {1.0 /* forbidden */,
648 1.0, 0.6735, 0.7031, 0.7615, 0.8055, 0.8437, 0.8935, 0.9157,
649 0.9815, 1.0255, 1.0695, 1.0950, 1.1575, 1.2015, 1.0 /*reserved*/ };
651 if( !p_sys->p_mpeg2dec->decoder.mpeg1 )
653 /* these hardcoded values are defined on mpeg2 standard for
654 * aspect ratio. other values are reserved or forbidden. */
655 /*switch( p_sys->p_mpeg2dec->decoder.aspect_ratio_information )
668 ratio = (double)p_sys->p_mpeg2dec->decoder.width/(double)p_sys->p_mpeg2dec->decoder.height;
674 /* mpeg1 constants refer to pixel aspect ratio */
675 ratio = (double)p_sys->p_mpeg2dec->decoder.width/(double)p_sys->p_mpeg2dec->decoder.height;
676 /* ratio /= mpeg1_pel_ratio[p_sys->p_mpeg2dec->decoder.aspect_ratio_information]; */
680 /*****************************************************************************
681 * GetNewPicture: Get a new picture from the vout and set the buf struct
682 *****************************************************************************/
683 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
685 //msg_Dbg(p_dec, "GetNewPicture Entering");
686 decoder_sys_t *p_sys = p_dec->p_sys;
689 p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
690 p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
691 p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
693 if( p_sys->p_info->sequence->frame_period > 0 )
695 p_dec->fmt_out.video.i_frame_rate =
696 (uint32_t)( (uint64_t)1001000000 * 27 /
697 p_sys->p_info->sequence->frame_period );
698 p_dec->fmt_out.video.i_frame_rate_base = 1001;
701 p_dec->fmt_out.i_codec =
702 ( p_sys->p_info->sequence->chroma_height <
703 p_sys->p_info->sequence->height ) ?
704 VLC_CODEC_I420 : VLC_CODEC_I422;
707 p_sys->f_wd_nb = fopen("/vlc/dec_nb", "w");
708 if (p_sys->f_wd_nb != NULL)
710 // fprintf(p_sys->f_wd_nb, "%d\n", mdate());
711 fprintf(p_sys->f_wd_nb, "%s\n", mdate());
712 fflush(p_sys->f_wd_nb);
715 p_pic = decoder_NewPicture( p_dec );
717 if( p_pic == NULL ) return NULL;
719 p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
720 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
721 p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
722 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
723 p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
724 p_sys->p_info->current_picture->nb_fields : 2;
725 p_pic->format.i_frame_rate = p_dec->fmt_out.video.i_frame_rate;
726 p_pic->format.i_frame_rate_base = p_dec->fmt_out.video.i_frame_rate_base;
728 decoder_LinkPicture( p_dec, p_pic );
730 pp_buf[0] = p_pic->p[0].p_pixels;
731 pp_buf[1] = p_pic->p[1].p_pixels;
732 pp_buf[2] = p_pic->p[2].p_pixels;
734 /* let driver ensure this image has the right format */
736 this->driver->update_frame_format( p_pic->p_sys->p_vout, p_pic,
737 p_dec->fmt_out.video.i_width,
738 p_dec->fmt_out.video.i_height,
739 p_dec->fmt_out.video.i_aspect,
741 mpeg2_xxmc_choose_coding( p_dec, &p_sys->p_mpeg2dec->decoder, p_pic,
742 get_aspect_ratio(p_dec), 0 );