input: add input_SetProgramId
[vlc.git] / modules / codec / libmpeg2.c
blob9896f40ee65d8d8aa95e0e4c498d0f1a1a063fb8
1 /*****************************************************************************
2 * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VLC authors and VideoLAN
6 * Authors: Gildas Bazin <gbazin@videolan.org>
7 * Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * NOTA BENE: this module requires the linking against a library which is
26 * known to require licensing under the GNU General Public License version 2
27 * (or later). Therefore, the result of compiling this module will normally
28 * be subject to the terms of that later license.
29 *****************************************************************************/
32 /*****************************************************************************
33 * Preamble
34 *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38 #include <assert.h>
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_codec.h>
43 #include <vlc_block_helper.h>
44 #include <vlc_cpu.h>
45 #include "cc.h"
46 #include "synchro.h"
48 #include <mpeg2.h>
50 /*****************************************************************************
51 * decoder_sys_t : libmpeg2 decoder descriptor
52 *****************************************************************************/
53 #define DPB_COUNT (3+1)
54 typedef struct
56 picture_t *p_picture;
57 bool b_linked;
58 bool b_displayed;
59 } picture_dpb_t;
61 typedef struct
64 * libmpeg2 properties
66 mpeg2dec_t *p_mpeg2dec;
67 const mpeg2_info_t *p_info;
68 bool b_skip;
71 * Input properties
73 vlc_tick_t i_previous_pts;
74 vlc_tick_t i_current_pts;
75 vlc_tick_t i_previous_dts;
76 vlc_tick_t i_current_dts;
77 bool b_garbage_pic;
78 bool b_after_sequence_header; /* is it the next frame after
79 * the sequence header ? */
80 bool b_slice_i; /* intra-slice refresh stream */
81 bool b_second_field;
83 bool b_preroll;
85 /* */
86 picture_dpb_t p_dpb[DPB_COUNT];
89 * Output properties
91 decoder_synchro_t *p_synchro;
92 int i_sar_num;
93 int i_sar_den;
94 vlc_tick_t i_last_frame_pts;
96 /* Closed captioning support */
97 uint32_t i_cc_flags;
98 vlc_tick_t i_cc_pts;
99 vlc_tick_t i_cc_dts;
100 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
101 cc_data_t cc;
102 #endif
103 uint8_t *p_gop_user_data;
104 uint32_t i_gop_user_data;
105 } decoder_sys_t;
107 /*****************************************************************************
108 * Local prototypes
109 *****************************************************************************/
110 static int OpenDecoder( vlc_object_t * );
111 static void CloseDecoder( vlc_object_t * );
113 static int DecodeVideo( decoder_t *, block_t *);
114 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
115 static void SendCc( decoder_t *p_dec );
116 #endif
118 static picture_t *GetNewPicture( decoder_t * );
119 static void PutPicture( decoder_t *, picture_t * );
121 static void GetAR( decoder_t *p_dec );
123 static void Reset( decoder_t *p_dec );
125 /* */
126 static void DpbInit( decoder_t * );
127 static void DpbClean( decoder_t * );
128 static picture_t *DpbNewPicture( decoder_t * );
129 static void DpbUnlinkPicture( decoder_t *, picture_t * );
130 static int DpbDisplayPicture( decoder_t *, picture_t * );
132 /*****************************************************************************
133 * Module descriptor
134 *****************************************************************************/
135 vlc_module_begin ()
136 set_description( N_("MPEG I/II video decoder (using libmpeg2)") )
137 set_capability( "video decoder", 50 )
138 set_category( CAT_INPUT )
139 set_subcategory( SUBCAT_INPUT_VCODEC )
140 set_callbacks( OpenDecoder, CloseDecoder )
141 add_shortcut( "libmpeg2" )
142 vlc_module_end ()
144 /*****************************************************************************
145 * OpenDecoder: probe the decoder and return score
146 *****************************************************************************/
147 static int OpenDecoder( vlc_object_t *p_this )
149 decoder_t *p_dec = (decoder_t*)p_this;
150 decoder_sys_t *p_sys;
151 uint32_t i_accel = 0;
153 if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGV )
154 return VLC_EGENERIC;
156 /* Select onl recognized original format (standard mpeg video) */
157 switch( p_dec->fmt_in.i_original_fourcc )
159 case VLC_FOURCC('m','p','g','1'):
160 case VLC_FOURCC('m','p','g','2'):
161 case VLC_FOURCC('m','p','g','v'):
162 case VLC_FOURCC('P','I','M','1'):
163 case VLC_FOURCC('h','d','v','2'):
164 break;
165 default:
166 if( p_dec->fmt_in.i_original_fourcc )
167 return VLC_EGENERIC;
168 break;
171 /* Allocate the memory needed to store the decoder's structure */
172 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
173 return VLC_ENOMEM;
175 /* Initialize the thread properties */
176 p_sys->p_mpeg2dec = NULL;
177 p_sys->p_synchro = NULL;
178 p_sys->p_info = NULL;
179 p_sys->i_current_pts = 0;
180 p_sys->i_previous_pts = 0;
181 p_sys->i_current_dts = 0;
182 p_sys->i_previous_dts = 0;
183 p_sys->i_sar_num = 0;
184 p_sys->i_sar_den = 0;
185 p_sys->b_garbage_pic = false;
186 p_sys->b_slice_i = false;
187 p_sys->b_second_field = false;
188 p_sys->b_skip = false;
189 p_sys->b_preroll = false;
190 DpbInit( p_dec );
192 p_sys->i_cc_pts = 0;
193 p_sys->i_cc_dts = 0;
194 p_sys->i_cc_flags = 0;
195 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
196 cc_Init( &p_sys->cc );
197 #endif
198 p_sys->p_gop_user_data = NULL;
199 p_sys->i_gop_user_data = 0;
201 #if defined( __i386__ ) || defined( __x86_64__ )
202 if( vlc_CPU_MMX() )
203 i_accel |= MPEG2_ACCEL_X86_MMX;
204 if( vlc_CPU_3dNOW() )
205 i_accel |= MPEG2_ACCEL_X86_3DNOW;
206 if( vlc_CPU_MMXEXT() )
207 i_accel |= MPEG2_ACCEL_X86_MMXEXT;
208 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
209 if( vlc_CPU_ALTIVEC() )
210 i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
212 #elif defined(__arm__)
213 # ifdef MPEG2_ACCEL_ARM
214 i_accel |= MPEG2_ACCEL_ARM;
215 # endif
216 # ifdef MPEG2_ACCEL_ARM_NEON
217 if( vlc_CPU_ARM_NEON() )
218 i_accel |= MPEG2_ACCEL_ARM_NEON;
219 # endif
221 /* TODO: sparc */
222 #else
223 /* If we do not know this CPU, trust libmpeg2's feature detection */
224 i_accel = MPEG2_ACCEL_DETECT;
226 #endif
228 /* Set CPU acceleration features */
229 mpeg2_accel( i_accel );
231 /* Initialize decoder */
232 p_sys->p_mpeg2dec = mpeg2_init();
233 if( p_sys->p_mpeg2dec == NULL)
235 msg_Err( p_dec, "mpeg2_init() failed" );
236 free( p_sys );
237 return VLC_EGENERIC;
240 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
242 p_dec->pf_decode = DecodeVideo;
243 p_dec->pf_flush = Reset;
244 p_dec->fmt_out.i_codec = 0;
246 return VLC_SUCCESS;
249 /*****************************************************************************
250 * RunDecoder: the libmpeg2 decoder
251 *****************************************************************************/
252 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
254 decoder_sys_t *p_sys = p_dec->p_sys;
255 mpeg2_state_t state;
257 block_t *p_block;
259 if( !pp_block || !*pp_block )
260 return NULL;
262 p_block = *pp_block;
263 if( p_block->i_flags & (BLOCK_FLAG_CORRUPTED) )
264 Reset( p_dec );
266 while( 1 )
268 state = mpeg2_parse( p_sys->p_mpeg2dec );
270 switch( state )
272 case STATE_SEQUENCE:
274 /* */
275 DpbClean( p_dec );
277 /* */
278 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
280 if( p_sys->p_synchro )
281 decoder_SynchroRelease( p_sys->p_synchro );
283 if( p_sys->p_info->sequence->frame_period <= 0 )
284 p_sys->p_synchro = NULL;
285 else
286 p_sys->p_synchro =
287 decoder_SynchroInit( p_dec, (uint32_t)(UINT64_C(1001000000) *
288 27 / p_sys->p_info->sequence->frame_period) );
289 p_sys->b_after_sequence_header = true;
291 /* Set the first 2 reference frames */
292 GetAR( p_dec );
293 for( int i = 0; i < 2; i++ )
295 picture_t *p_picture = DpbNewPicture( p_dec );
296 if( !p_picture )
298 Reset( p_dec );
299 block_Release( p_block );
300 return NULL;
302 PutPicture( p_dec, p_picture );
304 break;
307 case STATE_GOP:
308 /* There can be userdata in a GOP. It needs to be remembered for the next picture. */
309 if( p_sys->p_info->user_data_len > 2 )
311 free( p_sys->p_gop_user_data );
312 p_sys->p_gop_user_data = calloc( p_sys->p_info->user_data_len, sizeof(uint8_t) );
313 if( p_sys->p_gop_user_data )
315 p_sys->i_gop_user_data = p_sys->p_info->user_data_len;
316 memcpy( p_sys->p_gop_user_data, p_sys->p_info->user_data, p_sys->p_info->user_data_len );
319 break;
321 case STATE_PICTURE:
323 const mpeg2_info_t *p_info = p_sys->p_info;
324 const mpeg2_picture_t *p_current = p_info->current_picture;
326 vlc_tick_t i_pts, i_dts;
328 if( p_sys->b_after_sequence_header &&
329 (p_current->flags &
330 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P )
332 /* Intra-slice refresh. Simulate a blank I picture. */
333 msg_Dbg( p_dec, "intra-slice refresh stream" );
334 decoder_SynchroNewPicture( p_sys->p_synchro,
335 I_CODING_TYPE, 2, VLC_TICK_INVALID, VLC_TICK_INVALID,
336 p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
337 decoder_SynchroDecode( p_sys->p_synchro );
338 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
339 p_sys->b_slice_i = true;
341 p_sys->b_after_sequence_header = false;
343 #ifdef PIC_FLAG_PTS
344 i_pts = p_current->flags & PIC_FLAG_PTS ?
345 ( ( p_current->pts ==
346 (uint32_t)p_sys->i_current_pts ) ?
347 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
348 i_dts = 0;
350 /* Hack to handle demuxers which only have DTS timestamps */
351 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
353 if( p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
354 (p_current->flags &
355 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
357 i_pts = p_block->i_dts;
360 p_block->i_pts = p_block->i_dts = 0;
361 /* End hack */
363 #else /* New interface */
365 i_pts = p_current->flags & PIC_FLAG_TAGS ?
366 ( ( p_current->tag == (uint32_t)p_sys->i_current_pts ) ?
367 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
368 i_dts = p_current->flags & PIC_FLAG_TAGS ?
369 ( ( p_current->tag2 == (uint32_t)p_sys->i_current_dts ) ?
370 p_sys->i_current_dts : p_sys->i_previous_dts ) : 0;
371 #endif
373 /* If nb_fields == 1, it is a field picture, and it will be
374 * followed by another field picture for which we won't call
375 * decoder_SynchroNewPicture() because this would have other
376 * problems, so we take it into account here.
377 * This kind of sucks, but I didn't think better. --Meuuh
379 decoder_SynchroNewPicture( p_sys->p_synchro,
380 p_current->flags & PIC_MASK_CODING_TYPE,
381 p_current->nb_fields == 1 ? 2 :
382 p_current->nb_fields, i_pts, i_dts,
383 p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
386 picture_t *p_pic;
388 if( p_dec->b_frame_drop_allowed && !p_sys->b_preroll &&
389 !(p_sys->b_slice_i
390 && ((p_current->flags
391 & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P))
392 && !decoder_SynchroChoose( p_sys->p_synchro,
393 p_current->flags
394 & PIC_MASK_CODING_TYPE,
395 /*p_sys->p_vout->render_time*/ 0 /*FIXME*/,
396 p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
397 p_pic = NULL;
398 else
400 p_pic = DpbNewPicture( p_dec );
401 if( !p_pic )
403 Reset( p_dec );
405 p_pic = DpbNewPicture( p_dec );
406 if( !p_pic )
408 mpeg2_reset( p_sys->p_mpeg2dec, 1 );
409 block_Release( p_block );
410 return NULL;
415 mpeg2_skip( p_sys->p_mpeg2dec, p_pic == NULL );
416 p_sys->b_skip = p_pic == NULL;
417 if( p_pic != NULL )
418 decoder_SynchroDecode( p_sys->p_synchro );
419 else
420 decoder_SynchroTrash( p_sys->p_synchro );
422 PutPicture( p_dec, p_pic );
424 if( p_info->user_data_len > 2 || p_sys->i_gop_user_data > 2 )
426 p_sys->i_cc_pts = i_pts;
427 p_sys->i_cc_dts = i_dts;
428 if( (p_current->flags
429 & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P )
430 p_sys->i_cc_flags = BLOCK_FLAG_TYPE_P;
431 else if( (p_current->flags
432 & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
433 p_sys->i_cc_flags = BLOCK_FLAG_TYPE_B;
434 else p_sys->i_cc_flags = BLOCK_FLAG_TYPE_I;
435 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
436 bool b_top_field_first = p_sys->p_info->current_picture->flags
437 & PIC_FLAG_TOP_FIELD_FIRST;
438 #endif
439 if( p_sys->i_gop_user_data > 2 )
441 /* We now have picture info for any cached user_data out of the gop */
442 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
443 cc_ProbeAndExtract( &p_sys->cc, b_top_field_first,
444 &p_sys->p_gop_user_data[0], p_sys->i_gop_user_data );
445 #endif
446 p_sys->i_gop_user_data = 0;
449 /* Extract the CC from the user_data of the picture */
450 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
451 if( p_info->user_data_len > 2 )
452 cc_ProbeAndExtract( &p_sys->cc, b_top_field_first,
453 &p_info->user_data[0], p_info->user_data_len );
455 if( p_sys->cc.i_data )
456 SendCc( p_dec );
457 #endif
460 break;
463 case STATE_BUFFER:
464 if( !p_block->i_buffer )
466 block_Release( p_block );
467 return NULL;
470 if( (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY
471 | BLOCK_FLAG_CORRUPTED)) &&
472 p_sys->p_synchro &&
473 p_sys->p_info->sequence &&
474 p_sys->p_info->sequence->width != (unsigned)-1 )
476 decoder_SynchroReset( p_sys->p_synchro );
477 if( p_sys->p_info->current_fbuf != NULL &&
478 p_sys->p_info->current_fbuf->id != NULL )
480 p_sys->b_garbage_pic = true;
482 if( p_sys->b_slice_i )
484 decoder_SynchroNewPicture( p_sys->p_synchro,
485 I_CODING_TYPE, 2, VLC_TICK_INVALID, VLC_TICK_INVALID,
486 p_sys->p_info->sequence->flags &
487 SEQ_FLAG_LOW_DELAY );
488 decoder_SynchroDecode( p_sys->p_synchro );
489 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
493 if( p_block->i_flags & BLOCK_FLAG_PREROLL )
495 p_sys->b_preroll = true;
497 else if( p_sys->b_preroll )
499 p_sys->b_preroll = false;
500 if( p_sys->p_synchro )
501 decoder_SynchroReset( p_sys->p_synchro );
504 #ifdef PIC_FLAG_PTS
505 if( p_block->i_pts )
507 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
509 #else /* New interface */
510 if( p_block->i_pts || p_block->i_dts )
512 mpeg2_tag_picture( p_sys->p_mpeg2dec,
513 (uint32_t)p_block->i_pts,
514 (uint32_t)p_block->i_dts );
515 #endif
516 p_sys->i_previous_pts = p_sys->i_current_pts;
517 p_sys->i_current_pts = p_block->i_pts;
518 p_sys->i_previous_dts = p_sys->i_current_dts;
519 p_sys->i_current_dts = p_block->i_dts;
522 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
523 p_block->p_buffer + p_block->i_buffer );
525 p_block->i_buffer = 0;
526 break;
528 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
530 case STATE_SEQUENCE_MODIFIED:
531 GetAR( p_dec );
532 break;
533 #endif
534 case STATE_PICTURE_2ND:
535 p_sys->b_second_field = true;
536 break;
539 case STATE_INVALID_END:
540 case STATE_END:
541 case STATE_SLICE:
543 picture_t *p_pic = NULL;
545 if( p_sys->p_info->display_fbuf &&
546 p_sys->p_info->display_fbuf->id )
548 p_pic = p_sys->p_info->display_fbuf->id;
549 if( DpbDisplayPicture( p_dec, p_pic ) )
550 p_pic = NULL;
552 decoder_SynchroEnd( p_sys->p_synchro,
553 p_sys->p_info->display_picture->flags & PIC_MASK_CODING_TYPE,
554 p_sys->b_garbage_pic );
556 if( p_pic )
558 p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
559 if( p_sys->b_garbage_pic )
561 p_pic->date = VLC_TICK_INVALID; /* ??? */
562 p_sys->b_garbage_pic = false;
567 if( p_sys->p_info->discard_fbuf &&
568 p_sys->p_info->discard_fbuf->id )
570 DpbUnlinkPicture( p_dec, p_sys->p_info->discard_fbuf->id );
573 if( p_pic )
575 if( state == STATE_END )
576 p_pic->b_force = true; /* For still frames */
578 /* Avoid frames with identical timestamps.
579 * Especially needed for still frames in DVD menus. */
580 if( p_sys->i_last_frame_pts == p_pic->date )
581 p_pic->date++;
582 p_sys->i_last_frame_pts = p_pic->date;
583 return p_pic;
585 break;
588 case STATE_INVALID:
590 msg_Err( p_dec, "invalid picture encountered" );
591 /* I don't think we have anything to do, but well without
592 * docs ... */
593 break;
596 default:
597 break;
601 /* Never reached */
602 return NULL;
605 static int DecodeVideo( decoder_t *p_dec, block_t *p_block)
607 if( p_block == NULL ) /* No Drain */
608 return VLCDEC_SUCCESS;
610 block_t **pp_block = &p_block;
611 picture_t *p_pic;
612 while( ( p_pic = DecodeBlock( p_dec, pp_block ) ) != NULL )
613 decoder_QueueVideo( p_dec, p_pic );
614 return VLCDEC_SUCCESS;
617 /*****************************************************************************
618 * CloseDecoder: libmpeg2 decoder destruction
619 *****************************************************************************/
620 static void CloseDecoder( vlc_object_t *p_this )
622 decoder_t *p_dec = (decoder_t *)p_this;
623 decoder_sys_t *p_sys = p_dec->p_sys;
625 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
626 cc_Flush( &p_sys->cc );
627 #endif
629 DpbClean( p_dec );
631 free( p_sys->p_gop_user_data );
633 if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
635 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
637 free( p_sys );
640 /*****************************************************************************
641 * Reset: reset the decoder state
642 *****************************************************************************/
643 static void Reset( decoder_t *p_dec )
645 decoder_sys_t *p_sys = p_dec->p_sys;
647 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
648 cc_Flush( &p_sys->cc );
649 #endif
650 mpeg2_reset( p_sys->p_mpeg2dec, 0 );
651 DpbClean( p_dec );
654 /*****************************************************************************
655 * GetNewPicture: Get a new picture from the vout and set the buf struct
656 *****************************************************************************/
657 static picture_t *GetNewPicture( decoder_t *p_dec )
659 decoder_sys_t *p_sys = p_dec->p_sys;
660 picture_t *p_pic;
662 p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
663 p_dec->fmt_out.video.i_visible_width =
664 p_sys->p_info->sequence->picture_width;
665 p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
666 p_dec->fmt_out.video.i_visible_height =
667 p_sys->p_info->sequence->picture_height;
668 p_dec->fmt_out.video.i_sar_num = p_sys->i_sar_num;
669 p_dec->fmt_out.video.i_sar_den = p_sys->i_sar_den;
671 if( p_sys->p_info->sequence->frame_period > 0 )
673 p_dec->fmt_out.video.i_frame_rate =
674 (uint32_t)( (uint64_t)1001000000 * 27 /
675 p_sys->p_info->sequence->frame_period );
676 p_dec->fmt_out.video.i_frame_rate_base = 1001;
679 p_dec->fmt_out.i_codec =
680 ( p_sys->p_info->sequence->chroma_height <
681 p_sys->p_info->sequence->height ) ?
682 VLC_CODEC_I420 : VLC_CODEC_I422;
684 /* Get a new picture */
685 if( decoder_UpdateVideoFormat( p_dec ) )
686 return NULL;
687 p_pic = decoder_NewPicture( p_dec );
689 if( p_pic == NULL )
690 return NULL;
692 p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
693 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
694 p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
695 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
696 p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
697 p_sys->p_info->current_picture->nb_fields : 2;
699 return p_pic;
702 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
703 /*****************************************************************************
704 * SendCc: Sends the Closed Captions for the CC decoder.
705 *****************************************************************************/
706 static void SendCc( decoder_t *p_dec )
708 decoder_sys_t *p_sys = p_dec->p_sys;
709 block_t *p_cc = NULL;
711 if( !p_sys->cc.b_reorder && p_sys->cc.i_data <= 0 )
712 return;
714 p_cc = block_Alloc( p_sys->cc.i_data);
715 if( p_cc )
717 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
718 p_cc->i_dts =
719 p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
720 p_cc->i_flags = p_sys->i_cc_flags & BLOCK_FLAG_TYPE_MASK;
721 decoder_cc_desc_t desc;
722 desc.i_608_channels = p_sys->cc.i_608channels;
723 desc.i_708_channels = p_sys->cc.i_708channels;
724 desc.i_reorder_depth = p_sys->cc.b_reorder ? 0 : -1;
725 decoder_QueueCc( p_dec, p_cc, &desc );
727 cc_Flush( &p_sys->cc );
728 return;
730 #endif
732 /*****************************************************************************
733 * GetAR: Get aspect ratio
734 *****************************************************************************/
735 static void GetAR( decoder_t *p_dec )
737 decoder_sys_t *p_sys = p_dec->p_sys;
738 int i_old_sar_num = p_sys->i_sar_num;
739 int i_old_sar_den = p_sys->i_sar_den;
741 /* Check whether the input gave a particular aspect ratio */
742 if( p_dec->fmt_in.video.i_sar_num > 0 &&
743 p_dec->fmt_in.video.i_sar_den > 0 )
745 p_sys->i_sar_num = p_dec->fmt_in.video.i_sar_num;
746 p_sys->i_sar_den = p_dec->fmt_in.video.i_sar_den;
748 /* Use the value provided in the MPEG sequence header */
749 else if( p_sys->p_info->sequence->pixel_height > 0 )
751 p_sys->i_sar_num = p_sys->p_info->sequence->pixel_width;
752 p_sys->i_sar_den = p_sys->p_info->sequence->pixel_height;
754 else
756 /* Invalid aspect, assume 4:3.
757 * This shouldn't happen and if it does it is a bug
758 * in libmpeg2 (likely triggered by an invalid stream) */
759 p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 4;
760 p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 3;
763 if( p_sys->i_sar_num == i_old_sar_num &&
764 p_sys->i_sar_den == i_old_sar_den )
765 return;
767 if( p_sys->p_info->sequence->frame_period > 0 )
768 msg_Dbg( p_dec,
769 "%dx%d (display %d,%d), sar %i:%i, %u.%03u fps",
770 p_sys->p_info->sequence->picture_width,
771 p_sys->p_info->sequence->picture_height,
772 p_sys->p_info->sequence->display_width,
773 p_sys->p_info->sequence->display_height,
774 p_sys->i_sar_num, p_sys->i_sar_den,
775 (uint32_t)((uint64_t)1001000000 * 27 /
776 p_sys->p_info->sequence->frame_period / 1001),
777 (uint32_t)((uint64_t)1001000000 * 27 /
778 p_sys->p_info->sequence->frame_period % 1001) );
779 else
780 msg_Dbg( p_dec, "bad frame period" );
783 /*****************************************************************************
784 * PutPicture: Put a picture_t in mpeg2 context
785 *****************************************************************************/
786 static void PutPicture( decoder_t *p_dec, picture_t *p_picture )
788 decoder_sys_t *p_sys = p_dec->p_sys;
790 /* */
791 uint8_t *pp_buf[3];
792 for( int j = 0; j < 3; j++ )
793 pp_buf[j] = p_picture ? p_picture->p[j].p_pixels : NULL;
794 mpeg2_set_buf( p_sys->p_mpeg2dec, pp_buf, p_picture );
796 /* Completly broken API, why the hell does it suppose
797 * the stride of the chroma planes ! */
798 if( p_picture )
799 mpeg2_stride( p_sys->p_mpeg2dec, p_picture->p[Y_PLANE].i_pitch );
804 * Initialize a virtual Decoded Picture Buffer to workaround
805 * libmpeg2 deficient API
807 static void DpbInit( decoder_t *p_dec )
809 decoder_sys_t *p_sys = p_dec->p_sys;
811 for( int i = 0; i < DPB_COUNT; i++ )
812 p_sys->p_dpb[i].p_picture = NULL;
815 * Empty and reset the current DPB
817 static void DpbClean( decoder_t *p_dec )
819 decoder_sys_t *p_sys = p_dec->p_sys;
821 for( int i = 0; i < DPB_COUNT; i++ )
823 picture_dpb_t *p = &p_sys->p_dpb[i];
824 if( !p->p_picture )
825 continue;
826 if( p->b_linked )
827 picture_Release( p->p_picture );
828 if( !p->b_displayed )
829 picture_Release( p->p_picture );
831 p->p_picture = NULL;
835 * Retreive a picture and reserve a place in the DPB
837 static picture_t *DpbNewPicture( decoder_t *p_dec )
839 decoder_sys_t *p_sys = p_dec->p_sys;
841 picture_dpb_t *p;
842 int i;
844 for( i = 0; i < DPB_COUNT; i++ )
846 p = &p_sys->p_dpb[i];
847 if( !p->p_picture )
848 break;
850 if( i >= DPB_COUNT )
852 msg_Err( p_dec, "Leaking picture" );
853 return NULL;
856 p->p_picture = GetNewPicture( p_dec );
857 if( p->p_picture )
859 picture_Hold( p->p_picture );
860 p->b_linked = true;
861 p->b_displayed = false;
863 p->p_picture->date = VLC_TICK_INVALID;
865 return p->p_picture;
867 static picture_dpb_t *DpbFindPicture( decoder_t *p_dec, picture_t *p_picture )
869 decoder_sys_t *p_sys = p_dec->p_sys;
871 for( int i = 0; i < DPB_COUNT; i++ )
873 picture_dpb_t *p = &p_sys->p_dpb[i];
874 if( p->p_picture == p_picture )
875 return p;
877 return NULL;
880 * Unlink the provided picture and ensure that the decoder
881 * does not own it anymore.
883 static void DpbUnlinkPicture( decoder_t *p_dec, picture_t *p_picture )
885 picture_dpb_t *p = DpbFindPicture( p_dec, p_picture );
887 /* XXX it is needed to workaround libmpeg2 bugs */
888 if( !p || !p->b_linked )
890 msg_Err( p_dec, "DpbUnlinkPicture called on an invalid picture" );
891 return;
894 assert( p && p->b_linked );
896 picture_Release( p->p_picture );
897 p->b_linked = false;
899 if( !p->b_displayed )
900 picture_Release( p->p_picture );
901 p->p_picture = NULL;
904 * Mark the provided picture as displayed.
906 static int DpbDisplayPicture( decoder_t *p_dec, picture_t *p_picture )
908 picture_dpb_t *p = DpbFindPicture( p_dec, p_picture );
910 /* XXX it is needed to workaround libmpeg2 bugs */
911 if( !p || p->b_displayed || !p->b_linked )
913 msg_Err( p_dec, "DpbDisplayPicture called on an invalid picture" );
914 return VLC_EGENERIC;
917 assert( p && !p->b_displayed && p->b_linked );
919 p->b_displayed = true;
920 return VLC_SUCCESS;