1 /*****************************************************************************
2 * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Christophe Massiot <massiot@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * NOTA BENE: this module requires the linking against a library which is
27 * known to require licensing under the GNU General Public License version 2
28 * (or later). Therefore, the result of compiling this module will normally
29 * be subject to the terms of that later license.
30 *****************************************************************************/
33 /*****************************************************************************
35 *****************************************************************************/
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_codec.h>
44 #include <vlc_block_helper.h>
51 /*****************************************************************************
52 * decoder_sys_t : libmpeg2 decoder descriptor
53 *****************************************************************************/
54 #define DPB_COUNT (3+1)
67 mpeg2dec_t
*p_mpeg2dec
;
68 const mpeg2_info_t
*p_info
;
74 vlc_tick_t i_previous_pts
;
75 vlc_tick_t i_current_pts
;
76 vlc_tick_t i_previous_dts
;
77 vlc_tick_t i_current_dts
;
79 bool b_after_sequence_header
; /* is it the next frame after
80 * the sequence header ? */
81 bool b_slice_i
; /* intra-slice refresh stream */
87 picture_dpb_t p_dpb
[DPB_COUNT
];
92 decoder_synchro_t
*p_synchro
;
95 vlc_tick_t i_last_frame_pts
;
97 /* Closed captioning support */
101 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
104 uint8_t *p_gop_user_data
;
105 uint32_t i_gop_user_data
;
108 /*****************************************************************************
110 *****************************************************************************/
111 static int OpenDecoder( vlc_object_t
* );
112 static void CloseDecoder( vlc_object_t
* );
114 static int DecodeVideo( decoder_t
*, block_t
*);
115 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
116 static void SendCc( decoder_t
*p_dec
);
119 static picture_t
*GetNewPicture( decoder_t
* );
120 static void PutPicture( decoder_t
*, picture_t
* );
122 static void GetAR( decoder_t
*p_dec
);
124 static void Reset( decoder_t
*p_dec
);
127 static void DpbInit( decoder_t
* );
128 static void DpbClean( decoder_t
* );
129 static picture_t
*DpbNewPicture( decoder_t
* );
130 static void DpbUnlinkPicture( decoder_t
*, picture_t
* );
131 static int DpbDisplayPicture( decoder_t
*, picture_t
* );
133 /*****************************************************************************
135 *****************************************************************************/
137 set_description( N_("MPEG I/II video decoder (using libmpeg2)") )
138 set_capability( "video decoder", 50 )
139 set_category( CAT_INPUT
)
140 set_subcategory( SUBCAT_INPUT_VCODEC
)
141 set_callbacks( OpenDecoder
, CloseDecoder
)
142 add_shortcut( "libmpeg2" )
145 /*****************************************************************************
146 * OpenDecoder: probe the decoder and return score
147 *****************************************************************************/
148 static int OpenDecoder( vlc_object_t
*p_this
)
150 decoder_t
*p_dec
= (decoder_t
*)p_this
;
151 decoder_sys_t
*p_sys
;
152 uint32_t i_accel
= 0;
154 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_MPGV
)
157 /* Select onl recognized original format (standard mpeg video) */
158 switch( p_dec
->fmt_in
.i_original_fourcc
)
160 case VLC_FOURCC('m','p','g','1'):
161 case VLC_FOURCC('m','p','g','2'):
162 case VLC_FOURCC('m','p','g','v'):
163 case VLC_FOURCC('P','I','M','1'):
164 case VLC_FOURCC('h','d','v','2'):
167 if( p_dec
->fmt_in
.i_original_fourcc
)
172 /* Allocate the memory needed to store the decoder's structure */
173 if( ( p_dec
->p_sys
= p_sys
= calloc( 1, sizeof(*p_sys
)) ) == NULL
)
176 /* Initialize the thread properties */
177 p_sys
->p_mpeg2dec
= NULL
;
178 p_sys
->p_synchro
= NULL
;
179 p_sys
->p_info
= NULL
;
180 p_sys
->i_current_pts
= 0;
181 p_sys
->i_previous_pts
= 0;
182 p_sys
->i_current_dts
= 0;
183 p_sys
->i_previous_dts
= 0;
184 p_sys
->i_sar_num
= 0;
185 p_sys
->i_sar_den
= 0;
186 p_sys
->b_garbage_pic
= false;
187 p_sys
->b_slice_i
= false;
188 p_sys
->b_second_field
= false;
189 p_sys
->b_skip
= false;
190 p_sys
->b_preroll
= false;
195 p_sys
->i_cc_flags
= 0;
196 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
197 cc_Init( &p_sys
->cc
);
199 p_sys
->p_gop_user_data
= NULL
;
200 p_sys
->i_gop_user_data
= 0;
202 #if defined( __i386__ ) || defined( __x86_64__ )
204 i_accel
|= MPEG2_ACCEL_X86_MMX
;
205 if( vlc_CPU_3dNOW() )
206 i_accel
|= MPEG2_ACCEL_X86_3DNOW
;
207 if( vlc_CPU_MMXEXT() )
208 i_accel
|= MPEG2_ACCEL_X86_MMXEXT
;
209 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
210 if( vlc_CPU_ALTIVEC() )
211 i_accel
|= MPEG2_ACCEL_PPC_ALTIVEC
;
213 #elif defined(__arm__)
214 # ifdef MPEG2_ACCEL_ARM
215 i_accel
|= MPEG2_ACCEL_ARM
;
217 # ifdef MPEG2_ACCEL_ARM_NEON
218 if( vlc_CPU_ARM_NEON() )
219 i_accel
|= MPEG2_ACCEL_ARM_NEON
;
224 /* If we do not know this CPU, trust libmpeg2's feature detection */
225 i_accel
= MPEG2_ACCEL_DETECT
;
229 /* Set CPU acceleration features */
230 mpeg2_accel( i_accel
);
232 /* Initialize decoder */
233 p_sys
->p_mpeg2dec
= mpeg2_init();
234 if( p_sys
->p_mpeg2dec
== NULL
)
236 msg_Err( p_dec
, "mpeg2_init() failed" );
241 p_sys
->p_info
= mpeg2_info( p_sys
->p_mpeg2dec
);
243 p_dec
->pf_decode
= DecodeVideo
;
244 p_dec
->pf_flush
= Reset
;
245 p_dec
->fmt_out
.i_codec
= 0;
250 /*****************************************************************************
251 * RunDecoder: the libmpeg2 decoder
252 *****************************************************************************/
253 static picture_t
*DecodeBlock( decoder_t
*p_dec
, block_t
**pp_block
)
255 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
260 if( !pp_block
|| !*pp_block
)
264 if( p_block
->i_flags
& (BLOCK_FLAG_CORRUPTED
) )
269 state
= mpeg2_parse( p_sys
->p_mpeg2dec
);
279 mpeg2_custom_fbuf( p_sys
->p_mpeg2dec
, 1 );
281 if( p_sys
->p_synchro
)
282 decoder_SynchroRelease( p_sys
->p_synchro
);
284 if( p_sys
->p_info
->sequence
->frame_period
<= 0 )
285 p_sys
->p_synchro
= NULL
;
288 decoder_SynchroInit( p_dec
, (uint32_t)(UINT64_C(1001000000) *
289 27 / p_sys
->p_info
->sequence
->frame_period
) );
290 p_sys
->b_after_sequence_header
= true;
292 /* Set the first 2 reference frames */
294 for( int i
= 0; i
< 2; i
++ )
296 picture_t
*p_picture
= DpbNewPicture( p_dec
);
300 block_Release( p_block
);
303 PutPicture( p_dec
, p_picture
);
309 /* There can be userdata in a GOP. It needs to be remembered for the next picture. */
310 if( p_sys
->p_info
->user_data_len
> 2 )
312 free( p_sys
->p_gop_user_data
);
313 p_sys
->p_gop_user_data
= calloc( p_sys
->p_info
->user_data_len
, sizeof(uint8_t) );
314 if( p_sys
->p_gop_user_data
)
316 p_sys
->i_gop_user_data
= p_sys
->p_info
->user_data_len
;
317 memcpy( p_sys
->p_gop_user_data
, p_sys
->p_info
->user_data
, p_sys
->p_info
->user_data_len
);
324 const mpeg2_info_t
*p_info
= p_sys
->p_info
;
325 const mpeg2_picture_t
*p_current
= p_info
->current_picture
;
327 vlc_tick_t i_pts
, i_dts
;
329 if( p_sys
->b_after_sequence_header
&&
331 PIC_MASK_CODING_TYPE
) == PIC_FLAG_CODING_TYPE_P
)
333 /* Intra-slice refresh. Simulate a blank I picture. */
334 msg_Dbg( p_dec
, "intra-slice refresh stream" );
335 decoder_SynchroNewPicture( p_sys
->p_synchro
,
336 I_CODING_TYPE
, 2, 0, 0,
337 p_info
->sequence
->flags
& SEQ_FLAG_LOW_DELAY
);
338 decoder_SynchroDecode( p_sys
->p_synchro
);
339 decoder_SynchroEnd( p_sys
->p_synchro
, I_CODING_TYPE
, 0 );
340 p_sys
->b_slice_i
= true;
342 p_sys
->b_after_sequence_header
= false;
345 i_pts
= p_current
->flags
& PIC_FLAG_PTS
?
346 ( ( p_current
->pts
==
347 (uint32_t)p_sys
->i_current_pts
) ?
348 p_sys
->i_current_pts
: p_sys
->i_previous_pts
) : 0;
351 /* Hack to handle demuxers which only have DTS timestamps */
352 if( !i_pts
&& !p_block
->i_pts
&& p_block
->i_dts
> 0 )
354 if( p_info
->sequence
->flags
& SEQ_FLAG_LOW_DELAY
||
356 PIC_MASK_CODING_TYPE
) == PIC_FLAG_CODING_TYPE_B
)
358 i_pts
= p_block
->i_dts
;
361 p_block
->i_pts
= p_block
->i_dts
= 0;
364 #else /* New interface */
366 i_pts
= p_current
->flags
& PIC_FLAG_TAGS
?
367 ( ( p_current
->tag
== (uint32_t)p_sys
->i_current_pts
) ?
368 p_sys
->i_current_pts
: p_sys
->i_previous_pts
) : 0;
369 i_dts
= p_current
->flags
& PIC_FLAG_TAGS
?
370 ( ( p_current
->tag2
== (uint32_t)p_sys
->i_current_dts
) ?
371 p_sys
->i_current_dts
: p_sys
->i_previous_dts
) : 0;
374 /* If nb_fields == 1, it is a field picture, and it will be
375 * followed by another field picture for which we won't call
376 * decoder_SynchroNewPicture() because this would have other
377 * problems, so we take it into account here.
378 * This kind of sucks, but I didn't think better. --Meuuh
380 decoder_SynchroNewPicture( p_sys
->p_synchro
,
381 p_current
->flags
& PIC_MASK_CODING_TYPE
,
382 p_current
->nb_fields
== 1 ? 2 :
383 p_current
->nb_fields
, i_pts
, i_dts
,
384 p_info
->sequence
->flags
& SEQ_FLAG_LOW_DELAY
);
389 if( p_dec
->b_frame_drop_allowed
&& !p_sys
->b_preroll
&&
391 && ((p_current
->flags
392 & PIC_MASK_CODING_TYPE
) == PIC_FLAG_CODING_TYPE_P
))
393 && !decoder_SynchroChoose( p_sys
->p_synchro
,
395 & PIC_MASK_CODING_TYPE
,
396 /*p_sys->p_vout->render_time*/ 0 /*FIXME*/,
397 p_info
->sequence
->flags
& SEQ_FLAG_LOW_DELAY
) )
401 p_pic
= DpbNewPicture( p_dec
);
406 p_pic
= DpbNewPicture( p_dec
);
409 mpeg2_reset( p_sys
->p_mpeg2dec
, 1 );
410 block_Release( p_block
);
416 mpeg2_skip( p_sys
->p_mpeg2dec
, p_pic
== NULL
);
417 p_sys
->b_skip
= p_pic
== NULL
;
419 decoder_SynchroDecode( p_sys
->p_synchro
);
421 decoder_SynchroTrash( p_sys
->p_synchro
);
423 PutPicture( p_dec
, p_pic
);
425 if( p_info
->user_data_len
> 2 || p_sys
->i_gop_user_data
> 2 )
427 p_sys
->i_cc_pts
= i_pts
;
428 p_sys
->i_cc_dts
= i_dts
;
429 if( (p_current
->flags
430 & PIC_MASK_CODING_TYPE
) == PIC_FLAG_CODING_TYPE_P
)
431 p_sys
->i_cc_flags
= BLOCK_FLAG_TYPE_P
;
432 else if( (p_current
->flags
433 & PIC_MASK_CODING_TYPE
) == PIC_FLAG_CODING_TYPE_B
)
434 p_sys
->i_cc_flags
= BLOCK_FLAG_TYPE_B
;
435 else p_sys
->i_cc_flags
= BLOCK_FLAG_TYPE_I
;
436 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
437 bool b_top_field_first
= p_sys
->p_info
->current_picture
->flags
438 & PIC_FLAG_TOP_FIELD_FIRST
;
440 if( p_sys
->i_gop_user_data
> 2 )
442 /* We now have picture info for any cached user_data out of the gop */
443 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
444 cc_ProbeAndExtract( &p_sys
->cc
, b_top_field_first
,
445 &p_sys
->p_gop_user_data
[0], p_sys
->i_gop_user_data
);
447 p_sys
->i_gop_user_data
= 0;
450 /* Extract the CC from the user_data of the picture */
451 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
452 if( p_info
->user_data_len
> 2 )
453 cc_ProbeAndExtract( &p_sys
->cc
, b_top_field_first
,
454 &p_info
->user_data
[0], p_info
->user_data_len
);
456 if( p_sys
->cc
.i_data
)
465 if( !p_block
->i_buffer
)
467 block_Release( p_block
);
471 if( (p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
472 | BLOCK_FLAG_CORRUPTED
)) &&
474 p_sys
->p_info
->sequence
&&
475 p_sys
->p_info
->sequence
->width
!= (unsigned)-1 )
477 decoder_SynchroReset( p_sys
->p_synchro
);
478 if( p_sys
->p_info
->current_fbuf
!= NULL
&&
479 p_sys
->p_info
->current_fbuf
->id
!= NULL
)
481 p_sys
->b_garbage_pic
= true;
483 if( p_sys
->b_slice_i
)
485 decoder_SynchroNewPicture( p_sys
->p_synchro
,
486 I_CODING_TYPE
, 2, 0, 0,
487 p_sys
->p_info
->sequence
->flags
&
488 SEQ_FLAG_LOW_DELAY
);
489 decoder_SynchroDecode( p_sys
->p_synchro
);
490 decoder_SynchroEnd( p_sys
->p_synchro
, I_CODING_TYPE
, 0 );
494 if( p_block
->i_flags
& BLOCK_FLAG_PREROLL
)
496 p_sys
->b_preroll
= true;
498 else if( p_sys
->b_preroll
)
500 p_sys
->b_preroll
= false;
501 if( p_sys
->p_synchro
)
502 decoder_SynchroReset( p_sys
->p_synchro
);
508 mpeg2_pts( p_sys
->p_mpeg2dec
, (uint32_t)p_block
->i_pts
);
510 #else /* New interface */
511 if( p_block
->i_pts
|| p_block
->i_dts
)
513 mpeg2_tag_picture( p_sys
->p_mpeg2dec
,
514 (uint32_t)p_block
->i_pts
,
515 (uint32_t)p_block
->i_dts
);
517 p_sys
->i_previous_pts
= p_sys
->i_current_pts
;
518 p_sys
->i_current_pts
= p_block
->i_pts
;
519 p_sys
->i_previous_dts
= p_sys
->i_current_dts
;
520 p_sys
->i_current_dts
= p_block
->i_dts
;
523 mpeg2_buffer( p_sys
->p_mpeg2dec
, p_block
->p_buffer
,
524 p_block
->p_buffer
+ p_block
->i_buffer
);
526 p_block
->i_buffer
= 0;
529 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
531 case STATE_SEQUENCE_MODIFIED
:
535 case STATE_PICTURE_2ND
:
536 p_sys
->b_second_field
= true;
540 case STATE_INVALID_END
:
544 picture_t
*p_pic
= NULL
;
546 if( p_sys
->p_info
->display_fbuf
&&
547 p_sys
->p_info
->display_fbuf
->id
)
549 p_pic
= p_sys
->p_info
->display_fbuf
->id
;
550 if( DpbDisplayPicture( p_dec
, p_pic
) )
553 decoder_SynchroEnd( p_sys
->p_synchro
,
554 p_sys
->p_info
->display_picture
->flags
& PIC_MASK_CODING_TYPE
,
555 p_sys
->b_garbage_pic
);
559 p_pic
->date
= decoder_SynchroDate( p_sys
->p_synchro
);
560 if( p_sys
->b_garbage_pic
)
561 p_pic
->date
= 0; /* ??? */
562 p_sys
->b_garbage_pic
= false;
566 if( p_sys
->p_info
->discard_fbuf
&&
567 p_sys
->p_info
->discard_fbuf
->id
)
569 DpbUnlinkPicture( p_dec
, p_sys
->p_info
->discard_fbuf
->id
);
574 if( state
== STATE_END
)
575 p_pic
->b_force
= true; /* For still frames */
577 /* Avoid frames with identical timestamps.
578 * Especially needed for still frames in DVD menus. */
579 if( p_sys
->i_last_frame_pts
== p_pic
->date
)
581 p_sys
->i_last_frame_pts
= p_pic
->date
;
589 msg_Err( p_dec
, "invalid picture encountered" );
590 /* I don't think we have anything to do, but well without
604 static int DecodeVideo( decoder_t
*p_dec
, block_t
*p_block
)
606 if( p_block
== NULL
) /* No Drain */
607 return VLCDEC_SUCCESS
;
609 block_t
**pp_block
= &p_block
;
611 while( ( p_pic
= DecodeBlock( p_dec
, pp_block
) ) != NULL
)
612 decoder_QueueVideo( p_dec
, p_pic
);
613 return VLCDEC_SUCCESS
;
616 /*****************************************************************************
617 * CloseDecoder: libmpeg2 decoder destruction
618 *****************************************************************************/
619 static void CloseDecoder( vlc_object_t
*p_this
)
621 decoder_t
*p_dec
= (decoder_t
*)p_this
;
622 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
624 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
625 cc_Flush( &p_sys
->cc
);
630 free( p_sys
->p_gop_user_data
);
632 if( p_sys
->p_synchro
) decoder_SynchroRelease( p_sys
->p_synchro
);
634 if( p_sys
->p_mpeg2dec
) mpeg2_close( p_sys
->p_mpeg2dec
);
639 /*****************************************************************************
640 * Reset: reset the decoder state
641 *****************************************************************************/
642 static void Reset( decoder_t
*p_dec
)
644 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
646 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
647 cc_Flush( &p_sys
->cc
);
649 mpeg2_reset( p_sys
->p_mpeg2dec
, 0 );
653 /*****************************************************************************
654 * GetNewPicture: Get a new picture from the vout and set the buf struct
655 *****************************************************************************/
656 static picture_t
*GetNewPicture( decoder_t
*p_dec
)
658 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
661 p_dec
->fmt_out
.video
.i_width
= p_sys
->p_info
->sequence
->width
;
662 p_dec
->fmt_out
.video
.i_visible_width
=
663 p_sys
->p_info
->sequence
->picture_width
;
664 p_dec
->fmt_out
.video
.i_height
= p_sys
->p_info
->sequence
->height
;
665 p_dec
->fmt_out
.video
.i_visible_height
=
666 p_sys
->p_info
->sequence
->picture_height
;
667 p_dec
->fmt_out
.video
.i_sar_num
= p_sys
->i_sar_num
;
668 p_dec
->fmt_out
.video
.i_sar_den
= p_sys
->i_sar_den
;
670 if( p_sys
->p_info
->sequence
->frame_period
> 0 )
672 p_dec
->fmt_out
.video
.i_frame_rate
=
673 (uint32_t)( (uint64_t)1001000000 * 27 /
674 p_sys
->p_info
->sequence
->frame_period
);
675 p_dec
->fmt_out
.video
.i_frame_rate_base
= 1001;
678 p_dec
->fmt_out
.i_codec
=
679 ( p_sys
->p_info
->sequence
->chroma_height
<
680 p_sys
->p_info
->sequence
->height
) ?
681 VLC_CODEC_I420
: VLC_CODEC_I422
;
683 /* Get a new picture */
684 if( decoder_UpdateVideoFormat( p_dec
) )
686 p_pic
= decoder_NewPicture( p_dec
);
691 p_pic
->b_progressive
= p_sys
->p_info
->current_picture
!= NULL
?
692 p_sys
->p_info
->current_picture
->flags
& PIC_FLAG_PROGRESSIVE_FRAME
: 1;
693 p_pic
->b_top_field_first
= p_sys
->p_info
->current_picture
!= NULL
?
694 p_sys
->p_info
->current_picture
->flags
& PIC_FLAG_TOP_FIELD_FIRST
: 1;
695 p_pic
->i_nb_fields
= p_sys
->p_info
->current_picture
!= NULL
?
696 p_sys
->p_info
->current_picture
->nb_fields
: 2;
701 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
702 /*****************************************************************************
703 * SendCc: Sends the Closed Captions for the CC decoder.
704 *****************************************************************************/
705 static void SendCc( decoder_t
*p_dec
)
707 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
708 block_t
*p_cc
= NULL
;
710 if( !p_sys
->cc
.b_reorder
&& p_sys
->cc
.i_data
<= 0 )
713 p_cc
= block_Alloc( p_sys
->cc
.i_data
);
716 memcpy( p_cc
->p_buffer
, p_sys
->cc
.p_data
, p_sys
->cc
.i_data
);
718 p_cc
->i_pts
= p_sys
->cc
.b_reorder
? p_sys
->i_cc_pts
: p_sys
->i_cc_dts
;
719 p_cc
->i_flags
= p_sys
->i_cc_flags
& BLOCK_FLAG_TYPE_MASK
;
720 decoder_cc_desc_t desc
;
721 desc
.i_608_channels
= p_sys
->cc
.i_608channels
;
722 desc
.i_708_channels
= p_sys
->cc
.i_708channels
;
723 desc
.i_reorder_depth
= p_sys
->cc
.b_reorder
? 0 : -1;
724 decoder_QueueCc( p_dec
, p_cc
, &desc
);
726 cc_Flush( &p_sys
->cc
);
731 /*****************************************************************************
732 * GetAR: Get aspect ratio
733 *****************************************************************************/
734 static void GetAR( decoder_t
*p_dec
)
736 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
737 int i_old_sar_num
= p_sys
->i_sar_num
;
738 int i_old_sar_den
= p_sys
->i_sar_den
;
740 /* Check whether the input gave a particular aspect ratio */
741 if( p_dec
->fmt_in
.video
.i_sar_num
> 0 &&
742 p_dec
->fmt_in
.video
.i_sar_den
> 0 )
744 p_sys
->i_sar_num
= p_dec
->fmt_in
.video
.i_sar_num
;
745 p_sys
->i_sar_den
= p_dec
->fmt_in
.video
.i_sar_den
;
747 /* Use the value provided in the MPEG sequence header */
748 else if( p_sys
->p_info
->sequence
->pixel_height
> 0 )
750 p_sys
->i_sar_num
= p_sys
->p_info
->sequence
->pixel_width
;
751 p_sys
->i_sar_den
= p_sys
->p_info
->sequence
->pixel_height
;
755 /* Invalid aspect, assume 4:3.
756 * This shouldn't happen and if it does it is a bug
757 * in libmpeg2 (likely triggered by an invalid stream) */
758 p_sys
->i_sar_num
= p_sys
->p_info
->sequence
->picture_height
* 4;
759 p_sys
->i_sar_den
= p_sys
->p_info
->sequence
->picture_width
* 3;
762 if( p_sys
->i_sar_num
== i_old_sar_num
&&
763 p_sys
->i_sar_den
== i_old_sar_den
)
766 if( p_sys
->p_info
->sequence
->frame_period
> 0 )
768 "%dx%d (display %d,%d), sar %i:%i, %u.%03u fps",
769 p_sys
->p_info
->sequence
->picture_width
,
770 p_sys
->p_info
->sequence
->picture_height
,
771 p_sys
->p_info
->sequence
->display_width
,
772 p_sys
->p_info
->sequence
->display_height
,
773 p_sys
->i_sar_num
, p_sys
->i_sar_den
,
774 (uint32_t)((uint64_t)1001000000 * 27 /
775 p_sys
->p_info
->sequence
->frame_period
/ 1001),
776 (uint32_t)((uint64_t)1001000000 * 27 /
777 p_sys
->p_info
->sequence
->frame_period
% 1001) );
779 msg_Dbg( p_dec
, "bad frame period" );
782 /*****************************************************************************
783 * PutPicture: Put a picture_t in mpeg2 context
784 *****************************************************************************/
785 static void PutPicture( decoder_t
*p_dec
, picture_t
*p_picture
)
787 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
791 for( int j
= 0; j
< 3; j
++ )
792 pp_buf
[j
] = p_picture
? p_picture
->p
[j
].p_pixels
: NULL
;
793 mpeg2_set_buf( p_sys
->p_mpeg2dec
, pp_buf
, p_picture
);
795 /* Completly broken API, why the hell does it suppose
796 * the stride of the chroma planes ! */
798 mpeg2_stride( p_sys
->p_mpeg2dec
, p_picture
->p
[Y_PLANE
].i_pitch
);
803 * Initialize a virtual Decoded Picture Buffer to workaround
804 * libmpeg2 deficient API
806 static void DpbInit( decoder_t
*p_dec
)
808 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
810 for( int i
= 0; i
< DPB_COUNT
; i
++ )
811 p_sys
->p_dpb
[i
].p_picture
= NULL
;
814 * Empty and reset the current DPB
816 static void DpbClean( decoder_t
*p_dec
)
818 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
820 for( int i
= 0; i
< DPB_COUNT
; i
++ )
822 picture_dpb_t
*p
= &p_sys
->p_dpb
[i
];
826 picture_Release( p
->p_picture
);
827 if( !p
->b_displayed
)
828 picture_Release( p
->p_picture
);
834 * Retreive a picture and reserve a place in the DPB
836 static picture_t
*DpbNewPicture( decoder_t
*p_dec
)
838 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
843 for( i
= 0; i
< DPB_COUNT
; i
++ )
845 p
= &p_sys
->p_dpb
[i
];
851 msg_Err( p_dec
, "Leaking picture" );
855 p
->p_picture
= GetNewPicture( p_dec
);
858 picture_Hold( p
->p_picture
);
860 p
->b_displayed
= false;
862 p
->p_picture
->date
= 0;
866 static picture_dpb_t
*DpbFindPicture( decoder_t
*p_dec
, picture_t
*p_picture
)
868 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
870 for( int i
= 0; i
< DPB_COUNT
; i
++ )
872 picture_dpb_t
*p
= &p_sys
->p_dpb
[i
];
873 if( p
->p_picture
== p_picture
)
879 * Unlink the provided picture and ensure that the decoder
880 * does not own it anymore.
882 static void DpbUnlinkPicture( decoder_t
*p_dec
, picture_t
*p_picture
)
884 picture_dpb_t
*p
= DpbFindPicture( p_dec
, p_picture
);
886 /* XXX it is needed to workaround libmpeg2 bugs */
887 if( !p
|| !p
->b_linked
)
889 msg_Err( p_dec
, "DpbUnlinkPicture called on an invalid picture" );
893 assert( p
&& p
->b_linked
);
895 picture_Release( p
->p_picture
);
898 if( !p
->b_displayed
)
899 picture_Release( p
->p_picture
);
903 * Mark the provided picture as displayed.
905 static int DpbDisplayPicture( decoder_t
*p_dec
, picture_t
*p_picture
)
907 picture_dpb_t
*p
= DpbFindPicture( p_dec
, p_picture
);
909 /* XXX it is needed to workaround libmpeg2 bugs */
910 if( !p
|| p
->b_displayed
|| !p
->b_linked
)
912 msg_Err( p_dec
, "DpbDisplayPicture called on an invalid picture" );
916 assert( p
&& !p
->b_displayed
&& p
->b_linked
);
918 p
->b_displayed
= true;