packetizer: mpegvideo: parse/set multiview
[vlc.git] / modules / packetizer / mpegvideo.c
bloba116839193678dbc0a6450dd2dc1d47b7ca49995
1 /*****************************************************************************
2 * mpegvideo.c: parse and packetize an MPEG1/2 video stream
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and VideoLAN
5 * $Id$
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 /*****************************************************************************
42 * Preamble
43 *****************************************************************************/
45 #ifdef HAVE_CONFIG_H
46 # include "config.h"
47 #endif
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"
58 #include <limits.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 /*****************************************************************************
66 * Module descriptor
67 *****************************************************************************/
68 static int Open ( vlc_object_t * );
69 static void Close( vlc_object_t * );
71 vlc_module_begin ()
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 )
81 vlc_module_end ()
83 /*****************************************************************************
84 * Local prototypes
85 *****************************************************************************/
86 struct decoder_sys_t
89 * Input properties
91 packetizer_t packetizer;
93 /* Sequence header and extension */
94 block_t *p_seq;
95 block_t *p_ext;
97 /* Current frame being built */
98 block_t *p_frame;
99 block_t **pp_last;
101 bool b_frame_slice;
102 mtime_t i_pts;
103 mtime_t i_dts;
105 date_t dts;
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;
112 bool b_low_delay;
113 int i_aspect_ratio_info;
114 bool b_inited;
116 /* Picture properties */
117 int i_temporal_ref;
118 int i_prev_temporal_ref;
119 int i_picture_type;
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;
130 bool b_second_field;
132 /* Number of pictures since last sequence header */
133 unsigned i_seq_old;
135 /* Sync behaviour */
136 bool b_sync_on_intra_frame;
137 bool b_waiting_iframe;
138 int i_next_block_flags;
140 /* */
141 bool b_cc_reset;
142 uint32_t i_cc_flags;
143 mtime_t i_cc_pts;
144 mtime_t i_cc_dts;
145 cc_data_t cc;
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 /*****************************************************************************
161 * Open:
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 )
169 return VLC_EGENERIC;
171 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
172 if( !p_dec->p_sys )
173 return VLC_ENOMEM;
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;
179 /* Misc init */
180 packetizer_Init( &p_sys->packetizer,
181 p_mp2v_startcode, sizeof(p_mp2v_startcode), startcode_FindAnnexB,
182 NULL, 0, 4,
183 PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
185 p_sys->p_seq = NULL;
186 p_sys->p_ext = NULL;
187 p_sys->p_frame = NULL;
188 p_sys->pp_last = &p_sys->p_frame;
189 p_sys->b_frame_slice = false;
191 p_sys->i_dts =
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;
211 p_sys->b_inited = 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;
226 p_sys->i_cc_pts = 0;
227 p_sys->i_cc_dts = 0;
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;
235 return VLC_SUCCESS;
238 /*****************************************************************************
239 * Close:
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;
246 if( p_sys->p_seq )
248 block_Release( p_sys->p_seq );
250 if( p_sys->p_ext )
252 block_Release( p_sys->p_ext );
254 if( p_sys->p_frame )
256 block_ChainRelease( p_sys->p_frame );
258 packetizer_Clean( &p_sys->packetizer );
260 var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" );
262 free( p_sys );
265 /*****************************************************************************
266 * Packetize:
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 /*****************************************************************************
283 * GetCc:
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;
288 block_t *p_cc;
289 int i;
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 )
296 return NULL;
298 p_cc = block_Alloc( p_sys->cc.i_data );
299 if( p_cc )
301 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
302 p_cc->i_dts =
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 );
307 return p_cc;
310 /*****************************************************************************
311 * Helpers:
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;
320 if( p_sys->p_frame )
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 );
329 p_sys->i_dts =
330 p_sys->i_pts =
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 );
345 if( p_block )
347 p_block->i_flags |= p_sys->i_next_block_flags;
348 p_sys->i_next_block_flags = 0;
350 return p_block;
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" );
364 return VLC_EGENERIC;
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" );
376 return VLC_EGENERIC;
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;
384 return VLC_SUCCESS;
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
402 * do anything */
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;
415 if( b_eos )
417 block_ChainLastAppend( &p_sys->pp_last, p_frag );
418 p_frag = NULL;
421 p_pic = block_ChainGather( p_sys->p_frame );
422 if( p_pic == NULL )
423 return p_pic;
425 if( b_eos )
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 */ )
431 i_num_fields = 1;
432 else
433 i_num_fields = 2;
435 if( p_sys->b_seq_progressive )
437 if( p_sys->i_top_field_first == 0 &&
438 p_sys->i_repeat_first_field == 1 )
440 i_num_fields *= 2;
442 else if( p_sys->i_top_field_first == 1 &&
443 p_sys->i_repeat_first_field == 1 )
445 i_num_fields *= 3;
448 else
450 if( p_sys->i_picture_structure == 0x03 /* Frame Picture */ )
452 if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
454 i_num_fields += 1;
459 switch ( p_sys->i_picture_type )
461 case 0x01:
462 p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
463 break;
464 case 0x02:
465 p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
466 break;
467 case 0x03:
468 p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
469 break;
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 );
503 else
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 &&
525 b_first_xmited )
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 );
552 else
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;
575 else
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;
588 #if 0
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 );
593 #endif
596 /* Reset context */
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;
605 else
607 p_sys->b_second_field = 0;
610 /* CC */
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 );
623 if( !p_frag )
624 return p_pic;
626 * Check info of current fragment
628 if( p_frag->p_buffer[3] == 0xb8 )
630 /* Group start code */
631 if( p_sys->p_seq &&
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 ) );
636 if( p_sys->p_ext )
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*/
653 { 15, 1001 },
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;
664 p_sys->p_ext = NULL;
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;
673 else
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) );
703 p_sys->b_inited = 1;
706 else if( p_frag->p_buffer[3] == 0xb5 )
708 int i_type = p_frag->p_buffer[4] >> 4;
710 /* Extension start code */
711 if( i_type == 0x01 )
713 #if 0
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},
718 {0,1}, {0,1}
720 #endif
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;
730 p_sys->b_low_delay =
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
740 #if 0
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;
747 #endif
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 )
771 case 1:
772 p_dec->fmt_out.video.primaries = COLOR_PRIMARIES_BT709;
773 break;
774 case 4: /* BT.470M */
775 case 5: /* BT.470BG */
776 p_dec->fmt_out.video.primaries = COLOR_PRIMARIES_BT601_625;
777 break;
778 case 6: /* SMPTE 170M */
779 case 7: /* SMPTE 240M */
780 p_dec->fmt_out.video.primaries = COLOR_PRIMARIES_BT601_525;
781 break;
782 default:
783 break;
785 switch( color_transfer )
787 case 1:
788 p_dec->fmt_out.video.transfer = TRANSFER_FUNC_BT709;
789 break;
790 case 4: /* BT.470M assumed gamma 2.2 */
791 p_dec->fmt_out.video.transfer = TRANSFER_FUNC_SRGB;
792 break;
793 case 5: /* BT.470BG */
794 case 6: /* SMPTE 170M */
795 p_dec->fmt_out.video.transfer = TRANSFER_FUNC_BT2020;
796 break;
797 case 8: /* Linear */
798 p_dec->fmt_out.video.transfer = TRANSFER_FUNC_LINEAR;
799 break;
800 default:
801 break;
803 switch( color_matrix )
805 case 1:
806 p_dec->fmt_out.video.space = COLOR_SPACE_BT709;
807 break;
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;
812 break;
813 default:
814 break;
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 )
830 case 0x03:
831 mode = MULTIVIEW_STEREO_SBS; break;
832 case 0x04:
833 mode = MULTIVIEW_STEREO_TB; break;
834 case 0x08:
835 default:
836 mode = MULTIVIEW_2D; break;
838 p_dec->fmt_out.video.multiview_mode = mode;
840 else
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 */
847 p_sys->i_seq_old++;
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 );
868 return p_pic;