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>
34 #include <vlc_codec.h>
35 #include <vlc_codec_synchro.h>
43 //#include "attributes.h"
44 #include "mpeg2_internal.h"
45 //#include "xvmc_vld.h"
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) */
53 /*****************************************************************************
54 * decoder_sys_t : libmpeg2 decoder descriptor
55 *****************************************************************************/
61 mpeg2dec_t *p_mpeg2dec;
62 const mpeg2_info_t *p_info;
69 mtime_t i_previous_pts;
70 mtime_t i_current_pts;
71 mtime_t i_previous_dts;
72 mtime_t i_current_dts;
74 picture_t * p_picture_to_destroy;
76 bool b_after_sequence_header; /* is it the next frame after
77 * the sequence header ? */
78 bool b_slice_i; /* intra-slice refresh stream */
83 decoder_synchro_t *p_synchro;
85 mtime_t i_last_frame_pts;
88 /*****************************************************************************
90 *****************************************************************************/
92 static int OpenDecoder( vlc_object_t * );
93 static void CloseDecoder( vlc_object_t * );
95 static picture_t *DecodeBlock( decoder_t *, block_t ** );
97 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
99 /*****************************************************************************
101 *****************************************************************************/
103 set_description( N_("MPEG I/II hw video decoder (using libmpeg2)") )
104 set_capability( "decoder", 140 )
105 set_callbacks( OpenDecoder, CloseDecoder )
106 add_shortcut( "xxmc" )
109 /*****************************************************************************
110 * OpenDecoder: probe the decoder and return score
111 *****************************************************************************/
112 static int OpenDecoder( vlc_object_t *p_this )
115 decoder_t *p_dec = (decoder_t*)p_this;
116 decoder_sys_t *p_sys = NULL;
117 uint32_t i_accel = 0;
123 if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGV )
125 /* Select onl recognized original format (standard mpeg video) */
126 switch( p_dec->fmt_in.i_original_fourcc )
128 case VLC_FOURCC('m','p','g','1'):
129 case VLC_FOURCC('m','p','g','2'):
130 case VLC_FOURCC('m','p','g','v'):
131 /* Pinnacle hardware-mpeg1 */
132 case VLC_FOURCC('P','I','M','1'):
133 /* VIA hardware-mpeg2 */
134 case VLC_FOURCC('X','x','M','C'):
136 case VLC_FOURCC('V','C','R','2'):
139 if( p_dec->fmt_in.i_original_fourcc )
144 msg_Dbg(p_dec, "OpenDecoder Entering");
146 /* Allocate the memory needed to store the decoder's structure */
147 p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys) );
151 /* Initialize the thread properties */
152 p_sys->p_mpeg2dec = NULL;
153 p_sys->p_synchro = NULL;
154 p_sys->p_info = NULL;
155 p_sys->i_pts = mdate() + DEFAULT_PTS_DELAY;
156 p_sys->i_current_pts = 0;
157 p_sys->i_previous_pts = 0;
158 p_sys->i_current_dts = 0;
159 p_sys->i_previous_dts = 0;
160 p_sys->p_picture_to_destroy = NULL;
161 p_sys->b_garbage_pic = 0;
162 p_sys->b_slice_i = 0;
165 #if defined( __i386__ )
166 if( vlc_CPU() & CPU_CAPABILITY_MMX )
168 i_accel |= MPEG2_ACCEL_X86_MMX;
171 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
173 i_accel |= MPEG2_ACCEL_X86_3DNOW;
176 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
178 i_accel |= MPEG2_ACCEL_X86_MMXEXT;
181 #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
182 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
184 i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
188 /* If we do not know this CPU, trust libmpeg2's feature detection */
189 i_accel = MPEG2_ACCEL_DETECT;
193 /* Set CPU acceleration features */
194 mpeg2_accel( i_accel );
196 /* Initialize decoder */
197 p_sys->p_mpeg2dec = mpeg2_init();
198 if( p_sys->p_mpeg2dec == NULL)
200 msg_Err( p_dec, "mpeg2_init() failed" );
205 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
207 p_dec->pf_decode_video = DecodeBlock;
208 p_dec->fmt_out.i_cat = VIDEO_ES;
209 p_dec->fmt_out.i_codec = 0;
211 f_wd_dec = fopen("/vlc/dec_pid", "w");
212 if (f_wd_dec != NULL)
214 fprintf(f_wd_dec, "%d\n", getpid());
219 msg_Dbg(p_dec, "OpenDecoder Leaving");
223 static void WriteDecodeFile(int value)
227 f_wd_ok = fopen("/vlc/dec_ok", "w");
230 fprintf(f_wd_ok, "%d", value);
236 /*****************************************************************************
237 * RunDecoder: the libmpeg2 decoder
238 *****************************************************************************/
239 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
241 decoder_sys_t *p_sys = p_dec->p_sys;
247 if( !pp_block || !*pp_block )
253 state = mpeg2_parse( p_sys->p_mpeg2dec );
257 if( !p_block->i_buffer )
259 block_Release( p_block );
262 if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
264 p_sys->p_info->sequence &&
265 p_sys->p_info->sequence->width != (unsigned int)-1 )
267 decoder_SynchroReset( p_sys->p_synchro );
268 if( p_sys->p_info->current_fbuf != NULL
269 && p_sys->p_info->current_fbuf->id != NULL )
271 p_sys->b_garbage_pic = 1;
272 p_pic = p_sys->p_info->current_fbuf->id;
277 buf[0] = buf[1] = buf[2] = NULL;
278 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
280 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
282 p_sys->p_picture_to_destroy = p_pic;
284 if ( p_sys->b_slice_i )
286 decoder_SynchroNewPicture( p_sys->p_synchro,
287 I_CODING_TYPE, 2, 0, 0,
288 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
289 decoder_SynchroDecode( p_sys->p_synchro );
290 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
297 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
299 #else /* New interface */
300 if( p_block->i_pts || p_block->i_dts )
302 mpeg2_tag_picture( p_sys->p_mpeg2dec,
303 (uint32_t)p_block->i_pts,
304 (uint32_t)p_block->i_dts );
306 p_sys->i_previous_pts = p_sys->i_current_pts;
307 p_sys->i_current_pts = p_block->i_pts;
308 p_sys->i_previous_dts = p_sys->i_current_dts;
309 p_sys->i_current_dts = p_block->i_dts;
311 p_sys->i_current_rate = p_block->i_rate;
313 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
314 p_block->p_buffer + p_block->i_buffer );
315 p_block->i_buffer = 0;
320 /* Initialize video output */
322 buf[0] = buf[1] = buf[2] = NULL;
324 /* Check whether the input gave a particular aspect ratio */
325 if( p_dec->fmt_in.video.i_aspect )
327 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
328 if( p_sys->i_aspect <= AR_221_1_PICTURE )
329 switch( p_sys->i_aspect )
332 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
334 case AR_16_9_PICTURE:
335 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
337 case AR_221_1_PICTURE:
338 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
340 case AR_SQUARE_PICTURE:
341 p_sys->i_aspect = VOUT_ASPECT_FACTOR *
342 p_sys->p_info->sequence->width /
343 p_sys->p_info->sequence->height;
349 /* Use the value provided in the MPEG sequence header */
350 if( p_sys->p_info->sequence->pixel_height > 0 )
353 ((uint64_t)p_sys->p_info->sequence->display_width) *
354 p_sys->p_info->sequence->pixel_width *
356 p_sys->p_info->sequence->display_height /
357 p_sys->p_info->sequence->pixel_height;
361 /* Invalid aspect, assume 4:3.
362 * This shouldn't happen and if it does it is a bug
363 * in libmpeg2 (likely triggered by an invalid stream) */
364 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
368 msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
369 p_sys->p_info->sequence->width,
370 p_sys->p_info->sequence->height, p_sys->i_aspect,
371 (uint32_t)((uint64_t)1001000000 * 27 /
372 p_sys->p_info->sequence->frame_period / 1001),
373 (uint32_t)((uint64_t)1001000000 * 27 /
374 p_sys->p_info->sequence->frame_period % 1001) );
376 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
378 /* Set the first 2 reference frames */
379 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
381 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
383 block_Release( p_block );
386 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
389 decoder_LinkPicture( p_dec, p_pic );
391 if( p_sys->p_synchro )
393 decoder_SynchroRelease( p_sys->p_synchro );
395 p_sys->p_synchro = decoder_SynchroInit( p_dec,
396 (uint32_t)((uint64_t)1001000000 * 27 /
397 p_sys->p_info->sequence->frame_period) );
398 p_sys->b_after_sequence_header = 1;
402 case STATE_PICTURE_2ND:
403 decoder_SynchroNewPicture( p_sys->p_synchro,
404 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
405 p_sys->p_info->current_picture->nb_fields,
407 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
411 decoder_SynchroTrash( p_sys->p_synchro );
415 decoder_SynchroDecode( p_sys->p_synchro );
423 buf[0] = buf[1] = buf[2] = NULL;
425 if ( p_sys->b_after_sequence_header &&
426 ((p_sys->p_info->current_picture->flags &
427 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
429 /* Intra-slice refresh. Simulate a blank I picture. */
430 msg_Dbg( p_dec, "intra-slice refresh stream" );
431 decoder_SynchroNewPicture( p_sys->p_synchro,
432 I_CODING_TYPE, 2, 0, 0,
433 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
434 decoder_SynchroDecode( p_sys->p_synchro );
435 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
436 p_sys->b_slice_i = 1;
438 p_sys->b_after_sequence_header = 0;
441 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
442 ( ( p_sys->p_info->current_picture->pts ==
443 (uint32_t)p_sys->i_current_pts ) ?
444 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
445 #else /* New interface */
446 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
447 ( ( p_sys->p_info->current_picture->tag ==
448 (uint32_t)p_sys->i_current_pts ) ?
449 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
451 /* Hack to handle demuxers which only have DTS timestamps */
452 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
454 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
455 (p_sys->p_info->current_picture->flags &
456 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
458 i_pts = p_block->i_dts;
461 p_block->i_pts = p_block->i_dts = 0;
464 decoder_SynchroNewPicture( p_sys->p_synchro,
465 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
466 p_sys->p_info->current_picture->nb_fields, i_pts,
468 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
470 if ( !(p_sys->b_slice_i
471 && ((p_sys->p_info->current_picture->flags
472 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
473 && !decoder_SynchroChoose( p_sys->p_synchro,
474 p_sys->p_info->current_picture->flags
475 & PIC_MASK_CODING_TYPE,
476 /*FindVout(p_dec)->render_time*/ 0 /*FIXME*/,
477 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
479 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
481 decoder_SynchroTrash( p_sys->p_synchro );
482 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
486 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
488 decoder_SynchroDecode( p_sys->p_synchro );
490 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
492 block_Release( p_block );
496 //p_sys->p_mpeg2dec->ptr_forward_ref_picture = p_sys->p_mpeg2dec->fbuf[2]->id;
497 //p_sys->p_mpeg2dec->ptr_backward_ref_picture = p_sys->p_mpeg2dec->fbuf[1]->id;
499 if ((p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE) != B_CODING_TYPE)
501 //if (p_sys->p_mpeg2dec->ptr_forward_ref_picture &&
502 // p_sys->p_mpeg2dec->ptr_forward_ref_picture != picture->backward_reference_frame)
503 // p_pic->forward_reference_frame->free (p_pic->forward_reference_frame);
505 //p_sys->p_mpeg2dec->ptr_forward_ref_picture =
506 // p_sys->p_mpeg2dec->ptr_backward_ref_picture;
507 //p_sys->p_mpeg2dec->ptr_backward_ref_picture = (void *)p_pic;
509 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
517 if( p_sys->p_info->display_fbuf
518 && p_sys->p_info->display_fbuf->id )
520 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
522 decoder_SynchroEnd( p_sys->p_synchro,
523 p_sys->p_info->display_picture->flags
524 & PIC_MASK_CODING_TYPE,
525 p_sys->b_garbage_pic );
526 p_sys->b_garbage_pic = 0;
528 if ( p_sys->p_picture_to_destroy != p_pic )
530 p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
534 p_sys->p_picture_to_destroy = NULL;
539 if( p_sys->p_info->discard_fbuf &&
540 p_sys->p_info->discard_fbuf->id )
542 decoder_UnlinkPicture( p_dec, p_sys->p_info->discard_fbuf->id );
544 /* For still frames */
545 //if( state == STATE_END && p_pic ) p_pic->b_force = true;
550 /* Avoid frames with identical timestamps.
551 * Especially needed for still frames in DVD menus. */
552 if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
553 p_sys->i_last_frame_pts = p_pic->date;
562 buf[0] = buf[1] = buf[2] = NULL;
564 msg_Warn( p_dec, "invalid picture encountered" );
565 if ( ( p_sys->p_info->current_picture == NULL ) ||
566 ( ( p_sys->p_info->current_picture->flags &
567 PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
569 if( p_sys->p_synchro ) decoder_SynchroReset( p_sys->p_synchro );
571 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
574 if( p_sys->p_info->current_fbuf &&
575 p_sys->p_info->current_fbuf->id )
577 p_sys->b_garbage_pic = 1;
578 p_pic = p_sys->p_info->current_fbuf->id;
580 else if( !p_sys->p_info->sequence )
586 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
588 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
590 p_sys->p_picture_to_destroy = p_pic;
592 memset( p_pic->p[0].p_pixels, 0,
593 p_sys->p_info->sequence->width
594 * p_sys->p_info->sequence->height );
595 memset( p_pic->p[1].p_pixels, 0x80,
596 p_sys->p_info->sequence->width
597 * p_sys->p_info->sequence->height / 4 );
598 memset( p_pic->p[2].p_pixels, 0x80,
599 p_sys->p_info->sequence->width
600 * p_sys->p_info->sequence->height / 4 );
602 if( p_sys->b_slice_i )
604 decoder_SynchroNewPicture( p_sys->p_synchro,
605 I_CODING_TYPE, 2, 0, 0,
606 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
607 decoder_SynchroDecode( p_sys->p_synchro );
608 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
620 /*****************************************************************************
621 * CloseDecoder: libmpeg2 decoder destruction
622 *****************************************************************************/
623 static void CloseDecoder( vlc_object_t *p_this )
625 decoder_t *p_dec = (decoder_t *)p_this;
626 decoder_sys_t *p_sys = p_dec->p_sys;
629 if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
630 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
632 f_wd_dec = fopen("/vlc/dec_pid", "w");
633 if (f_wd_dec != NULL)
635 fprintf(f_wd_dec, "0\n");
642 static double get_aspect_ratio( decoder_t *p_dec )
644 decoder_sys_t *p_sys = p_dec->p_sys;
646 double mpeg1_pel_ratio[16] = {1.0 /* forbidden */,
647 1.0, 0.6735, 0.7031, 0.7615, 0.8055, 0.8437, 0.8935, 0.9157,
648 0.9815, 1.0255, 1.0695, 1.0950, 1.1575, 1.2015, 1.0 /*reserved*/ };
650 if( !p_sys->p_mpeg2dec->decoder.mpeg1 )
652 /* these hardcoded values are defined on mpeg2 standard for
653 * aspect ratio. other values are reserved or forbidden. */
654 /*switch( p_sys->p_mpeg2dec->decoder.aspect_ratio_information )
667 ratio = (double)p_sys->p_mpeg2dec->decoder.width/(double)p_sys->p_mpeg2dec->decoder.height;
673 /* mpeg1 constants refer to pixel aspect ratio */
674 ratio = (double)p_sys->p_mpeg2dec->decoder.width/(double)p_sys->p_mpeg2dec->decoder.height;
675 /* ratio /= mpeg1_pel_ratio[p_sys->p_mpeg2dec->decoder.aspect_ratio_information]; */
679 /*****************************************************************************
680 * GetNewPicture: Get a new picture from the vout and set the buf struct
681 *****************************************************************************/
682 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
684 //msg_Dbg(p_dec, "GetNewPicture Entering");
685 decoder_sys_t *p_sys = p_dec->p_sys;
688 p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
689 p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
690 p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
692 if( p_sys->p_info->sequence->frame_period > 0 )
694 p_dec->fmt_out.video.i_frame_rate =
695 (uint32_t)( (uint64_t)1001000000 * 27 /
696 p_sys->p_info->sequence->frame_period );
697 p_dec->fmt_out.video.i_frame_rate_base = 1001;
700 p_dec->fmt_out.i_codec =
701 ( p_sys->p_info->sequence->chroma_height <
702 p_sys->p_info->sequence->height ) ?
703 VLC_CODEC_I420 : VLC_CODEC_I422;
706 p_sys->f_wd_nb = fopen("/vlc/dec_nb", "w");
707 if (p_sys->f_wd_nb != NULL)
709 // fprintf(p_sys->f_wd_nb, "%d\n", mdate());
710 fprintf(p_sys->f_wd_nb, "%s\n", mdate());
711 fflush(p_sys->f_wd_nb);
714 p_pic = decoder_NewPicture( p_dec );
716 if( p_pic == NULL ) return NULL;
718 p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
719 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
720 p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
721 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
722 p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
723 p_sys->p_info->current_picture->nb_fields : 2;
724 p_pic->format.i_frame_rate = p_dec->fmt_out.video.i_frame_rate;
725 p_pic->format.i_frame_rate_base = p_dec->fmt_out.video.i_frame_rate_base;
727 decoder_LinkPicture( p_dec, p_pic );
729 pp_buf[0] = p_pic->p[0].p_pixels;
730 pp_buf[1] = p_pic->p[1].p_pixels;
731 pp_buf[2] = p_pic->p[2].p_pixels;
733 /* let driver ensure this image has the right format */
735 this->driver->update_frame_format( p_pic->p_sys->p_vout, p_pic,
736 p_dec->fmt_out.video.i_width,
737 p_dec->fmt_out.video.i_height,
738 p_dec->fmt_out.video.i_aspect,
740 mpeg2_xxmc_choose_coding( p_dec, &p_sys->p_mpeg2dec->decoder, p_pic,
741 get_aspect_ratio(p_dec), 0 );