1 /*****************************************************************************
2 * mpegvideo.c: parse and packetize an MPEG1/2 video stream
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Problem with this implementation:
30 * Although we should time-stamp each picture with a PTS, this isn't possible
31 * with the current implementation.
32 * The problem comes from the fact that for non-low-delay streams we can't
33 * calculate the PTS of pictures used as backward reference. Even the temporal
34 * reference number doesn't help here because all the pictures don't
35 * necessarily have the same duration (eg. 3:2 pulldown).
37 * However this doesn't really matter as far as the MPEG muxers are concerned
38 * because they allow having empty PTS fields. --gibalou
39 *****************************************************************************/
41 /*****************************************************************************
43 *****************************************************************************/
49 #include <vlc_common.h>
50 #include <vlc_plugin.h>
51 #include <vlc_block.h>
52 #include <vlc_codec.h>
53 #include <vlc_block_helper.h>
54 #include "../codec/cc.h"
55 #include "packetizer_helper.h"
56 #include "startcode_helper.h"
60 #define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame")
61 #define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \
62 "sync on the next full frame. This flags instructs the packetizer " \
63 "to sync on the first Intra Frame found.")
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Open ( vlc_object_t
* );
69 static void Close( vlc_object_t
* );
72 set_category( CAT_SOUT
)
73 set_subcategory( SUBCAT_SOUT_PACKETIZER
)
74 set_description( N_("MPEG-I/II video packetizer") )
75 set_shortname( N_("MPEG Video") )
76 set_capability( "packetizer", 50 )
77 set_callbacks( Open
, Close
)
79 add_bool( "packetizer-mpegvideo-sync-iframe", false, SYNC_INTRAFRAME_TEXT
,
80 SYNC_INTRAFRAME_LONGTEXT
, true )
83 /*****************************************************************************
85 *****************************************************************************/
91 packetizer_t packetizer
;
93 /* Sequence header and extension */
97 /* Current frame being built */
106 date_t prev_iframe_dts
;
108 /* Sequence properties */
109 unsigned i_frame_rate
;
110 unsigned i_frame_rate_base
;
111 bool b_seq_progressive
;
113 int i_aspect_ratio_info
;
116 /* Picture properties */
118 int i_prev_temporal_ref
;
120 int i_picture_structure
;
121 int i_top_field_first
;
122 int i_repeat_first_field
;
123 int i_progressive_frame
;
125 mtime_t i_last_ref_pts
;
127 mtime_t i_last_frame_pts
;
128 uint16_t i_last_frame_refid
;
132 /* Number of pictures since last sequence header */
136 bool b_sync_on_intra_frame
;
137 bool b_waiting_iframe
;
138 int i_next_block_flags
;
148 static block_t
*Packetize( decoder_t
*, block_t
** );
149 static void PacketizeFlush( decoder_t
* );
150 static block_t
*GetCc( decoder_t
*p_dec
, bool pb_present
[4], int * );
152 static void PacketizeReset( void *p_private
, bool b_broken
);
153 static block_t
*PacketizeParse( void *p_private
, bool *pb_ts_used
, block_t
* );
154 static int PacketizeValidate( void *p_private
, block_t
* );
156 static block_t
*ParseMPEGBlock( decoder_t
*, block_t
* );
158 static const uint8_t p_mp2v_startcode
[3] = { 0x00, 0x00, 0x01 };
160 /*****************************************************************************
162 *****************************************************************************/
163 static int Open( vlc_object_t
*p_this
)
165 decoder_t
*p_dec
= (decoder_t
*)p_this
;
166 decoder_sys_t
*p_sys
;
168 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_MPGV
)
171 p_dec
->p_sys
= p_sys
= malloc( sizeof( decoder_sys_t
) );
174 memset( p_dec
->p_sys
, 0, sizeof( decoder_sys_t
) );
176 p_dec
->fmt_out
.i_codec
= VLC_CODEC_MPGV
;
177 p_dec
->fmt_out
.i_original_fourcc
= p_dec
->fmt_in
.i_original_fourcc
;
180 packetizer_Init( &p_sys
->packetizer
,
181 p_mp2v_startcode
, sizeof(p_mp2v_startcode
), startcode_FindAnnexB
,
183 PacketizeReset
, PacketizeParse
, PacketizeValidate
, p_dec
);
187 p_sys
->p_frame
= NULL
;
188 p_sys
->pp_last
= &p_sys
->p_frame
;
189 p_sys
->b_frame_slice
= false;
192 p_sys
->i_pts
= VLC_TS_INVALID
;
193 date_Init( &p_sys
->dts
, 30000, 1001 );
194 date_Set( &p_sys
->dts
, VLC_TS_INVALID
);
195 date_Init( &p_sys
->prev_iframe_dts
, 30000, 1001 );
196 date_Set( &p_sys
->prev_iframe_dts
, VLC_TS_INVALID
);
198 p_sys
->i_frame_rate
= 2 * 30000;
199 p_sys
->i_frame_rate_base
= 1001;
200 p_sys
->b_seq_progressive
= true;
201 p_sys
->b_low_delay
= true;
202 p_sys
->i_seq_old
= 0;
204 p_sys
->i_temporal_ref
= 0;
205 p_sys
->i_prev_temporal_ref
= 2048;
206 p_sys
->i_picture_type
= 0;
207 p_sys
->i_picture_structure
= 0x03; /* frame */
208 p_sys
->i_top_field_first
= 0;
209 p_sys
->i_repeat_first_field
= 0;
210 p_sys
->i_progressive_frame
= 0;
213 p_sys
->i_last_ref_pts
= VLC_TS_INVALID
;
214 p_sys
->b_second_field
= 0;
216 p_sys
->i_next_block_flags
= 0;
218 p_sys
->i_last_frame_refid
= 0;
220 p_sys
->b_waiting_iframe
=
221 p_sys
->b_sync_on_intra_frame
= var_CreateGetBool( p_dec
, "packetizer-mpegvideo-sync-iframe" );
222 if( p_sys
->b_sync_on_intra_frame
)
223 msg_Dbg( p_dec
, "syncing on intra frame now" );
225 p_sys
->b_cc_reset
= false;
228 p_sys
->i_cc_flags
= 0;
229 cc_Init( &p_sys
->cc
);
231 p_dec
->pf_packetize
= Packetize
;
232 p_dec
->pf_flush
= PacketizeFlush
;
233 p_dec
->pf_get_cc
= GetCc
;
238 /*****************************************************************************
240 *****************************************************************************/
241 static void Close( vlc_object_t
*p_this
)
243 decoder_t
*p_dec
= (decoder_t
*)p_this
;
244 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
248 block_Release( p_sys
->p_seq
);
252 block_Release( p_sys
->p_ext
);
256 block_ChainRelease( p_sys
->p_frame
);
258 packetizer_Clean( &p_sys
->packetizer
);
260 var_Destroy( p_dec
, "packetizer-mpegvideo-sync-iframe" );
265 /*****************************************************************************
267 *****************************************************************************/
268 static block_t
*Packetize( decoder_t
*p_dec
, block_t
**pp_block
)
270 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
272 return packetizer_Packetize( &p_sys
->packetizer
, pp_block
);
275 static void PacketizeFlush( decoder_t
*p_dec
)
277 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
279 packetizer_Flush( &p_sys
->packetizer
);
282 /*****************************************************************************
284 *****************************************************************************/
285 static block_t
*GetCc( decoder_t
*p_dec
, bool pb_present
[4], int *pi_reorder_depth
)
287 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
290 *pi_reorder_depth
= 0;
292 for( i
= 0; i
< 4; i
++ )
293 pb_present
[i
] = p_sys
->cc
.pb_present
[i
];
295 if( p_sys
->cc
.i_data
<= 0 )
298 p_cc
= block_Alloc( p_sys
->cc
.i_data
);
301 memcpy( p_cc
->p_buffer
, p_sys
->cc
.p_data
, p_sys
->cc
.i_data
);
303 p_cc
->i_pts
= p_sys
->cc
.b_reorder
? p_sys
->i_cc_pts
: p_sys
->i_cc_dts
;
304 p_cc
->i_flags
= ( p_sys
->cc
.b_reorder
? p_sys
->i_cc_flags
: BLOCK_FLAG_TYPE_P
) & ( BLOCK_FLAG_TYPE_I
|BLOCK_FLAG_TYPE_P
|BLOCK_FLAG_TYPE_B
);
306 cc_Flush( &p_sys
->cc
);
310 /*****************************************************************************
312 *****************************************************************************/
313 static void PacketizeReset( void *p_private
, bool b_broken
)
315 VLC_UNUSED(b_broken
);
316 decoder_t
*p_dec
= p_private
;
317 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
319 p_sys
->i_next_block_flags
= BLOCK_FLAG_DISCONTINUITY
;
322 block_ChainRelease( p_sys
->p_frame
);
323 p_sys
->p_frame
= NULL
;
324 p_sys
->pp_last
= &p_sys
->p_frame
;
325 p_sys
->b_frame_slice
= false;
327 date_Set( &p_sys
->dts
, VLC_TS_INVALID
);
328 date_Set( &p_sys
->prev_iframe_dts
, VLC_TS_INVALID
);
331 p_sys
->i_last_ref_pts
= VLC_TS_INVALID
;
332 p_sys
->b_waiting_iframe
= p_sys
->b_sync_on_intra_frame
;
333 p_sys
->i_prev_temporal_ref
= 2048;
336 static block_t
*PacketizeParse( void *p_private
, bool *pb_ts_used
, block_t
*p_block
)
338 decoder_t
*p_dec
= p_private
;
339 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
341 /* Check if we have a picture start code */
342 *pb_ts_used
= p_block
->p_buffer
[3] == 0x00;
344 p_block
= ParseMPEGBlock( p_dec
, p_block
);
347 p_block
->i_flags
|= p_sys
->i_next_block_flags
;
348 p_sys
->i_next_block_flags
= 0;
354 static int PacketizeValidate( void *p_private
, block_t
*p_au
)
356 decoder_t
*p_dec
= p_private
;
357 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
359 if( unlikely( p_sys
->b_waiting_iframe
) )
361 if( (p_au
->i_flags
& BLOCK_FLAG_TYPE_I
) == 0 )
363 msg_Dbg( p_dec
, "waiting on intra frame" );
366 msg_Dbg( p_dec
, "synced on intra frame" );
367 p_sys
->b_waiting_iframe
= false;
370 /* We've just started the stream, wait for the first PTS.
371 * We discard here so we can still get the sequence header. */
372 if( unlikely( p_sys
->i_dts
<= VLC_TS_INVALID
&& p_sys
->i_pts
<= VLC_TS_INVALID
&&
373 date_Get( &p_sys
->dts
) <= VLC_TS_INVALID
))
375 msg_Dbg( p_dec
, "need a starting pts/dts" );
379 /* When starting the stream we can have the first frame with
380 * an invalid DTS (i_interpolated_pts is initialized to VLC_TS_INVALID) */
381 if( unlikely( p_au
->i_dts
<= VLC_TS_INVALID
) )
382 p_au
->i_dts
= p_au
->i_pts
;
386 /*****************************************************************************
387 * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
388 *****************************************************************************/
389 static block_t
*ParseMPEGBlock( decoder_t
*p_dec
, block_t
*p_frag
)
391 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
392 block_t
*p_pic
= NULL
;
395 * Check if previous picture is finished
397 if( ( p_sys
->b_frame_slice
&&
398 (p_frag
->p_buffer
[3] == 0x00 || p_frag
->p_buffer
[3] > 0xaf) ) &&
399 p_sys
->p_seq
== NULL
)
401 /* We have a picture but without a sequence header we can't
403 msg_Dbg( p_dec
, "waiting for sequence start" );
404 if( p_sys
->p_frame
) block_ChainRelease( p_sys
->p_frame
);
405 p_sys
->p_frame
= NULL
;
406 p_sys
->pp_last
= &p_sys
->p_frame
;
407 p_sys
->b_frame_slice
= false;
410 else if( p_sys
->b_frame_slice
&&
411 (p_frag
->p_buffer
[3] == 0x00 || p_frag
->p_buffer
[3] > 0xaf) )
413 const bool b_eos
= p_frag
->p_buffer
[3] == 0xb7;
417 block_ChainLastAppend( &p_sys
->pp_last
, p_frag
);
421 p_pic
= block_ChainGather( p_sys
->p_frame
);
426 p_pic
->i_flags
|= BLOCK_FLAG_END_OF_SEQUENCE
;
428 unsigned i_num_fields
;
430 if( !p_sys
->b_seq_progressive
&& p_sys
->i_picture_structure
!= 0x03 /* Field Picture */ )
435 if( p_sys
->b_seq_progressive
)
437 if( p_sys
->i_top_field_first
== 0 &&
438 p_sys
->i_repeat_first_field
== 1 )
442 else if( p_sys
->i_top_field_first
== 1 &&
443 p_sys
->i_repeat_first_field
== 1 )
450 if( p_sys
->i_picture_structure
== 0x03 /* Frame Picture */ )
452 if( p_sys
->i_progressive_frame
&& p_sys
->i_repeat_first_field
)
459 switch ( p_sys
->i_picture_type
)
462 p_pic
->i_flags
|= BLOCK_FLAG_TYPE_I
;
465 p_pic
->i_flags
|= BLOCK_FLAG_TYPE_P
;
468 p_pic
->i_flags
|= BLOCK_FLAG_TYPE_B
;
472 if( !p_sys
->b_seq_progressive
)
474 if( p_sys
->i_picture_structure
< 0x03 )
476 p_pic
->i_flags
|= BLOCK_FLAG_SINGLE_FIELD
;
477 p_pic
->i_flags
|= (p_sys
->i_picture_structure
== 0x01) ? BLOCK_FLAG_TOP_FIELD_FIRST
478 : BLOCK_FLAG_BOTTOM_FIELD_FIRST
;
480 else /* if( p_sys->i_picture_structure == 0x03 ) */
482 p_pic
->i_flags
|= (p_sys
->i_top_field_first
) ? BLOCK_FLAG_TOP_FIELD_FIRST
483 : BLOCK_FLAG_BOTTOM_FIELD_FIRST
;
487 /* Special case for DVR-MS where we need to fully build pts from scratch
488 * and only use first dts as it does not monotonically increase
489 * This will NOT work with frame repeats and such, as we would need to fully
490 * fill the DPB to get accurate pts timings. */
491 if( unlikely( p_dec
->fmt_in
.i_original_fourcc
== VLC_FOURCC( 'D','V','R',' ') ) )
493 const bool b_first_xmited
= (p_sys
->i_prev_temporal_ref
!= p_sys
->i_temporal_ref
);
495 if( ( p_pic
->i_flags
& BLOCK_FLAG_TYPE_I
) && b_first_xmited
)
497 if( date_Get( &p_sys
->prev_iframe_dts
) == VLC_TS_INVALID
)
499 if( p_sys
->i_dts
!= VLC_TS_INVALID
)
501 date_Set( &p_sys
->dts
, p_sys
->i_dts
);
505 if( date_Get( &p_sys
->dts
) == VLC_TS_INVALID
)
507 date_Set( &p_sys
->dts
, VLC_TS_0
);
511 p_sys
->prev_iframe_dts
= p_sys
->dts
;
514 p_pic
->i_dts
= date_Get( &p_sys
->dts
);
516 /* Compute pts from poc */
517 date_t datepts
= p_sys
->prev_iframe_dts
;
518 date_Increment( &datepts
, (1 + p_sys
->i_temporal_ref
) * 2 );
520 /* Field picture second field case */
521 if( p_sys
->i_picture_structure
!= 0x03 )
523 /* first sent is not the first in display order */
524 if( (p_sys
->i_picture_structure
>> 1) != !p_sys
->i_top_field_first
&&
527 date_Increment( &datepts
, 2 );
531 p_pic
->i_pts
= date_Get( &datepts
);
533 if( date_Get( &p_sys
->dts
) != VLC_TS_INVALID
)
535 date_Increment( &p_sys
->dts
, i_num_fields
);
537 p_pic
->i_length
= date_Get( &p_sys
->dts
) - p_pic
->i_dts
;
539 p_sys
->i_prev_temporal_ref
= p_sys
->i_temporal_ref
;
541 else /* General case, use demuxer's dts/pts when set or interpolate */
543 if( p_sys
->b_low_delay
|| p_sys
->i_picture_type
== 0x03 )
545 /* Trivial case (DTS == PTS) */
546 /* Correct interpolated dts when we receive a new pts/dts */
547 if( p_sys
->i_pts
> VLC_TS_INVALID
)
548 date_Set( &p_sys
->dts
, p_sys
->i_pts
);
549 if( p_sys
->i_dts
> VLC_TS_INVALID
)
550 date_Set( &p_sys
->dts
, p_sys
->i_dts
);
554 /* Correct interpolated dts when we receive a new pts/dts */
555 if(p_sys
->i_last_ref_pts
> VLC_TS_INVALID
&& !p_sys
->b_second_field
)
556 date_Set( &p_sys
->dts
, p_sys
->i_last_ref_pts
);
557 if( p_sys
->i_dts
> VLC_TS_INVALID
)
558 date_Set( &p_sys
->dts
, p_sys
->i_dts
);
560 if( !p_sys
->b_second_field
)
561 p_sys
->i_last_ref_pts
= p_sys
->i_pts
;
564 p_pic
->i_dts
= date_Get( &p_sys
->dts
);
566 /* Set PTS only if we have a B frame or if it comes from the stream */
567 if( p_sys
->i_pts
> VLC_TS_INVALID
)
569 p_pic
->i_pts
= p_sys
->i_pts
;
571 else if( p_sys
->i_picture_type
== 0x03 )
573 p_pic
->i_pts
= p_pic
->i_dts
;
577 p_pic
->i_pts
= VLC_TS_INVALID
;
580 if( date_Get( &p_sys
->dts
) != VLC_TS_INVALID
)
582 date_Increment( &p_sys
->dts
, i_num_fields
);
584 p_pic
->i_length
= date_Get( &p_sys
->dts
) - p_pic
->i_dts
;
589 msg_Dbg( p_dec
, "pic: type=%d ref=%d nf=%d tff=%d dts=%"PRId64
" ptsdiff=%"PRId64
" len=%"PRId64
,
590 p_sys
->i_picture_structure
, p_sys
->i_temporal_ref
, i_num_fields
,
591 p_sys
->i_top_field_first
,
592 p_pic
->i_dts
, (p_pic
->i_pts
> VLC_TS_INVALID
) ? p_pic
->i_pts
- p_pic
->i_dts
: 0, p_pic
->i_length
);
597 p_sys
->p_frame
= NULL
;
598 p_sys
->pp_last
= &p_sys
->p_frame
;
599 p_sys
->b_frame_slice
= false;
601 if( p_sys
->i_picture_structure
!= 0x03 )
603 p_sys
->b_second_field
= !p_sys
->b_second_field
;
607 p_sys
->b_second_field
= 0;
611 p_sys
->b_cc_reset
= true;
612 p_sys
->i_cc_pts
= p_pic
->i_pts
;
613 p_sys
->i_cc_dts
= p_pic
->i_dts
;
614 p_sys
->i_cc_flags
= p_pic
->i_flags
;
617 if( !p_pic
&& p_sys
->b_cc_reset
)
619 p_sys
->b_cc_reset
= false;
620 cc_Flush( &p_sys
->cc
);
626 * Check info of current fragment
628 if( p_frag
->p_buffer
[3] == 0xb8 )
630 /* Group start code */
632 p_sys
->i_seq_old
> p_sys
->i_frame_rate
/p_sys
->i_frame_rate_base
)
634 /* Useful for mpeg1: repeat sequence header every second */
635 block_ChainLastAppend( &p_sys
->pp_last
, block_Duplicate( p_sys
->p_seq
) );
638 block_ChainLastAppend( &p_sys
->pp_last
, block_Duplicate( p_sys
->p_ext
) );
641 p_sys
->i_seq_old
= 0;
644 else if( p_frag
->p_buffer
[3] == 0xb3 && p_frag
->i_buffer
>= 8 )
646 /* Sequence header code */
647 static const int code_to_frame_rate
[16][2] =
649 { 1, 1 }, /* invalid */
650 { 24000, 1001 }, { 24, 1 }, { 25, 1 }, { 30000, 1001 },
651 { 30, 1 }, { 50, 1 }, { 60000, 1001 }, { 60, 1 },
652 /* Unofficial 15fps from Xing*/
654 /* Unofficial economy rates from libmpeg3 */
655 { 5000, 1001 }, { 1000, 1001 }, { 12000, 1001 }, { 15000, 1001 },
656 { 1, 1 }, { 1, 1 } /* invalid */
659 if( p_sys
->p_seq
) block_Release( p_sys
->p_seq
);
660 if( p_sys
->p_ext
) block_Release( p_sys
->p_ext
);
662 p_sys
->p_seq
= block_Duplicate( p_frag
);
663 p_sys
->i_seq_old
= 0;
666 p_dec
->fmt_out
.video
.i_visible_width
=
667 ( p_frag
->p_buffer
[4] << 4)|(p_frag
->p_buffer
[5] >> 4 );
668 p_dec
->fmt_out
.video
.i_width
= (p_dec
->fmt_out
.video
.i_visible_width
+ 0x0F) & ~0x0F;
669 p_dec
->fmt_out
.video
.i_visible_height
=
670 ( (p_frag
->p_buffer
[5]&0x0f) << 8 )|p_frag
->p_buffer
[6];
671 if( p_sys
->b_seq_progressive
)
672 p_dec
->fmt_out
.video
.i_height
= (p_dec
->fmt_out
.video
.i_visible_height
+ 0x0F) & ~0x0F;
674 p_dec
->fmt_out
.video
.i_height
= (p_dec
->fmt_out
.video
.i_visible_height
+ 0x1F) & ~0x1F;
675 p_sys
->i_aspect_ratio_info
= p_frag
->p_buffer
[7] >> 4;
677 /* TODO: MPEG1 aspect ratio */
679 p_sys
->i_frame_rate
= code_to_frame_rate
[p_frag
->p_buffer
[7]&0x0f][0];
680 p_sys
->i_frame_rate_base
=
681 code_to_frame_rate
[p_frag
->p_buffer
[7]&0x0f][1];
683 if( ( p_sys
->i_frame_rate
!= p_dec
->fmt_out
.video
.i_frame_rate
||
684 p_dec
->fmt_out
.video
.i_frame_rate_base
!= p_sys
->i_frame_rate_base
) &&
685 p_sys
->i_frame_rate
&& p_sys
->i_frame_rate_base
&& p_sys
->i_frame_rate
<= UINT_MAX
/2 )
687 date_Change( &p_sys
->dts
, 2 * p_sys
->i_frame_rate
, p_sys
->i_frame_rate_base
);
688 date_Change( &p_sys
->prev_iframe_dts
, 2 * p_sys
->i_frame_rate
, p_sys
->i_frame_rate_base
);
690 p_dec
->fmt_out
.video
.i_frame_rate
= p_sys
->i_frame_rate
;
691 p_dec
->fmt_out
.video
.i_frame_rate_base
= p_sys
->i_frame_rate_base
;
693 p_sys
->b_seq_progressive
= true;
694 p_sys
->b_low_delay
= true;
697 if ( !p_sys
->b_inited
)
699 msg_Dbg( p_dec
, "size %dx%d/%dx%d fps=%.3f",
700 p_dec
->fmt_out
.video
.i_visible_width
, p_dec
->fmt_out
.video
.i_visible_height
,
701 p_dec
->fmt_out
.video
.i_width
, p_dec
->fmt_out
.video
.i_height
,
702 p_sys
->i_frame_rate
/ (float)(p_sys
->i_frame_rate_base
? p_sys
->i_frame_rate_base
: 1) );
706 else if( p_frag
->p_buffer
[3] == 0xb5 )
708 int i_type
= p_frag
->p_buffer
[4] >> 4;
710 /* Extension start code */
714 static const int mpeg2_aspect
[16][2] =
716 {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
717 {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
722 /* sequence extension */
723 if( p_sys
->p_ext
) block_Release( p_sys
->p_ext
);
724 p_sys
->p_ext
= block_Duplicate( p_frag
);
726 if( p_frag
->i_buffer
>= 10 )
728 p_sys
->b_seq_progressive
=
729 p_frag
->p_buffer
[5]&0x08 ? true : false;
731 p_frag
->p_buffer
[9]&0x80 ? true : false;
734 /* Do not set aspect ratio : in case we're transcoding,
735 * transcode will take our fmt_out as a fmt_in to libmpeg2.
736 * libmpeg2.c will then believe that the user has requested
737 * a specific aspect ratio, which she hasn't. Thus in case
738 * of aspect ratio change, we're screwed. --Meuuh
741 p_dec
->fmt_out
.video
.i_sar_num
=
742 mpeg2_aspect
[p_sys
->i_aspect_ratio_info
][0] *
743 p_dec
->fmt_out
.video
.i_height
;
744 p_dec
->fmt_out
.video
.i_sar_den
=
745 mpeg2_aspect
[p_sys
->i_aspect_ratio_info
][1] *
746 p_dec
->fmt_out
.video
.i_width
;
750 else if( i_type
== 0x08 && p_frag
->i_buffer
> 8 )
752 /* picture extension */
753 p_sys
->i_picture_structure
= p_frag
->p_buffer
[6]&0x03;
754 p_sys
->i_top_field_first
= p_frag
->p_buffer
[7] >> 7;
755 p_sys
->i_repeat_first_field
= (p_frag
->p_buffer
[7]>>1)&0x01;
756 p_sys
->i_progressive_frame
= p_frag
->p_buffer
[8] >> 7;
758 else if( i_type
== 0x02 && p_frag
->i_buffer
> 8 )
760 /* Sequence display extension */
761 bool contains_color_description
= (p_frag
->p_buffer
[4] & 0x01);
762 //uint8_t video_format = (p_frag->p_buffer[4] & 0x0f) >> 1;
764 if( contains_color_description
&& p_frag
->i_buffer
> 11 )
766 uint8_t color_primaries
= p_frag
->p_buffer
[5];
767 uint8_t color_transfer
= p_frag
->p_buffer
[6];
768 uint8_t color_matrix
= p_frag
->p_buffer
[7];
769 switch( color_primaries
)
772 p_dec
->fmt_out
.video
.primaries
= COLOR_PRIMARIES_BT709
;
774 case 4: /* BT.470M */
775 case 5: /* BT.470BG */
776 p_dec
->fmt_out
.video
.primaries
= COLOR_PRIMARIES_BT601_625
;
778 case 6: /* SMPTE 170M */
779 case 7: /* SMPTE 240M */
780 p_dec
->fmt_out
.video
.primaries
= COLOR_PRIMARIES_BT601_525
;
785 switch( color_transfer
)
788 p_dec
->fmt_out
.video
.transfer
= TRANSFER_FUNC_BT709
;
790 case 4: /* BT.470M assumed gamma 2.2 */
791 p_dec
->fmt_out
.video
.transfer
= TRANSFER_FUNC_SRGB
;
793 case 5: /* BT.470BG */
794 case 6: /* SMPTE 170M */
795 p_dec
->fmt_out
.video
.transfer
= TRANSFER_FUNC_BT2020
;
798 p_dec
->fmt_out
.video
.transfer
= TRANSFER_FUNC_LINEAR
;
803 switch( color_matrix
)
806 p_dec
->fmt_out
.video
.space
= COLOR_SPACE_BT709
;
808 case 5: /* BT.470BG */
809 case 6: /* SMPTE 170 M */
810 case 7: /* SMPTE 240 M */
811 p_dec
->fmt_out
.video
.space
= COLOR_SPACE_BT601
;
820 else if( p_frag
->p_buffer
[3] == 0xb2 && p_frag
->i_buffer
> 8 )
822 /* Frame Packing extension identifier as H262 2012 Amd4 Annex L */
823 if( !memcmp( &p_frag
->p_buffer
[4], "JP3D", 4 ) &&
824 p_frag
->i_buffer
> 11 && p_frag
->p_buffer
[8] == 0x03 &&
825 p_dec
->fmt_in
.video
.multiview_mode
== MULTIVIEW_2D
)
827 video_multiview_mode_t mode
;
828 switch( p_frag
->p_buffer
[9] & 0x7F )
831 mode
= MULTIVIEW_STEREO_SBS
; break;
833 mode
= MULTIVIEW_STEREO_TB
; break;
836 mode
= MULTIVIEW_2D
; break;
838 p_dec
->fmt_out
.video
.multiview_mode
= mode
;
841 cc_ProbeAndExtract( &p_sys
->cc
, p_sys
->i_top_field_first
,
842 &p_frag
->p_buffer
[4], p_frag
->i_buffer
- 4 );
844 else if( p_frag
->p_buffer
[3] == 0x00 )
846 /* Picture start code */
849 if( p_frag
->i_buffer
>= 6 )
851 p_sys
->i_temporal_ref
=
852 ( p_frag
->p_buffer
[4] << 2 )|(p_frag
->p_buffer
[5] >> 6);
853 p_sys
->i_picture_type
= ( p_frag
->p_buffer
[5] >> 3 ) & 0x03;
856 p_sys
->i_dts
= p_frag
->i_dts
;
857 p_sys
->i_pts
= p_frag
->i_pts
;
859 else if( p_frag
->p_buffer
[3] >= 0x01 && p_frag
->p_buffer
[3] <= 0xaf )
861 /* Slice start code */
862 p_sys
->b_frame_slice
= true;
865 /* Append the block */
866 block_ChainLastAppend( &p_sys
->pp_last
, p_frag
);