1 /*****************************************************************************
2 * h264.c: h264/avc video packetizer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Derk-Jan Hartman <hartman at videolan dot org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 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 General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_codec.h>
39 #include <vlc_block.h>
41 #include "vlc_block_helper.h"
43 #include "../codec/cc.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
52 set_category( CAT_SOUT )
53 set_subcategory( SUBCAT_SOUT_PACKETIZER )
54 set_description( N_("H.264 video packetizer") )
55 set_capability( "packetizer", 50 )
56 set_callbacks( Open, Close )
60 /****************************************************************************
62 ****************************************************************************/
63 static block_t *Packetize( decoder_t *, block_t ** );
64 static block_t *PacketizeAVC1( decoder_t *, block_t ** );
65 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] );
73 int i_pic_parameter_set_id;
77 int i_bottom_field_flag;
81 int i_pic_order_cnt_lsb;
82 int i_delta_pic_order_cnt_bottom;
84 int i_delta_pic_order_cnt0;
85 int i_delta_pic_order_cnt1;
92 block_bytestream_t bytestream;
104 block_t *pp_sps[SPS_MAX];
105 block_t *pp_pps[PPS_MAX];
108 int i_avcC_length_size;
110 /* Useful values of the Sequence Parameter Set */
111 int i_log2_max_frame_num;
112 int b_frame_mbs_only;
113 int i_pic_order_cnt_type;
114 int i_delta_pic_order_always_zero_flag;
115 int i_log2_max_pic_order_cnt_lsb;
117 /* Value from Picture Parameter Set */
118 int i_pic_order_present_flag;
120 /* Useful values of the Slice Header */
148 NAL_SLICE_IDR = 5, /* ref_idc != 0 */
149 NAL_SEI = 6, /* ref_idc == 0 */
153 /* ref_idc == 0 for 6,9,10,11,12 */
158 NAL_PRIORITY_DISPOSABLE = 0,
159 NAL_PRIORITY_LOW = 1,
160 NAL_PRIORITY_HIGH = 2,
161 NAL_PRIORITY_HIGHEST = 3,
164 static block_t *ParseNALBlock( decoder_t *, bool *pb_used_ts, block_t * );
166 static block_t *CreateAnnexbNAL( decoder_t *, const uint8_t *p, int );
168 static block_t *OutputPicture( decoder_t *p_dec );
169 static void PutSPS( decoder_t *p_dec, block_t *p_frag );
170 static void PutPPS( decoder_t *p_dec, block_t *p_frag );
171 static void ParseSlice( decoder_t *p_dec, bool *pb_new_picture, slice_t *p_slice,
172 int i_nal_ref_idc, int i_nal_type, const block_t *p_frag );
173 static void ParseSei( decoder_t *, block_t * );
176 /*****************************************************************************
177 * Open: probe the packetizer and return score
178 * When opening after demux, the packetizer is only loaded AFTER the decoder
179 * That means that what you set in fmt_out is ignored by the decoder in this special case
180 *****************************************************************************/
181 static int Open( vlc_object_t *p_this )
183 decoder_t *p_dec = (decoder_t*)p_this;
184 decoder_sys_t *p_sys;
187 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'h', '2', '6', '4') &&
188 p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') &&
189 p_dec->fmt_in.i_codec != VLC_FOURCC( 'V', 'S', 'S', 'H') &&
190 p_dec->fmt_in.i_codec != VLC_FOURCC( 'v', 's', 's', 'h') &&
191 p_dec->fmt_in.i_codec != VLC_FOURCC( 'D', 'A', 'V', 'C') &&
192 p_dec->fmt_in.i_codec != VLC_FOURCC( 'x', '2', '6', '4') &&
193 p_dec->fmt_in.i_codec != VLC_FOURCC( 'X', '2', '6', '4') &&
194 ( p_dec->fmt_in.i_codec != VLC_FOURCC( 'a', 'v', 'c', '1') ||
195 p_dec->fmt_in.i_extra < 7 ) )
200 /* Allocate the memory needed to store the decoder's structure */
201 if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
205 p_sys->i_state = STATE_NOSYNC;
207 p_sys->startcode[0] = 0;
208 p_sys->startcode[1] = 0;
209 p_sys->startcode[2] = 0;
210 p_sys->startcode[3] = 1;
211 p_sys->bytestream = block_BytestreamInit();
212 p_sys->b_slice = false;
213 p_sys->p_frame = NULL;
214 p_sys->b_header= false;
215 p_sys->b_sps = false;
216 p_sys->b_pps = false;
217 for( i = 0; i < SPS_MAX; i++ )
218 p_sys->pp_sps[i] = NULL;
219 for( i = 0; i < PPS_MAX; i++ )
220 p_sys->pp_pps[i] = NULL;
222 p_sys->slice.i_nal_type = -1;
223 p_sys->slice.i_nal_ref_idc = -1;
224 p_sys->slice.i_idr_pic_id = -1;
225 p_sys->slice.i_frame_num = -1;
226 p_sys->slice.i_frame_type = 0;
227 p_sys->slice.i_pic_parameter_set_id = -1;
228 p_sys->slice.i_field_pic_flag = 0;
229 p_sys->slice.i_bottom_field_flag = -1;
230 p_sys->slice.i_pic_order_cnt_lsb = -1;
231 p_sys->slice.i_delta_pic_order_cnt_bottom = -1;
233 p_sys->i_frame_dts = -1;
234 p_sys->i_frame_pts = -1;
236 /* Setup properties */
237 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
238 p_dec->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
240 if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'a', 'v', 'c', '1' ) )
242 /* This type of stream is produced by mp4 and matroska
243 * when we want to store it in another streamformat, you need to convert
244 * The fmt_in.p_extra should ALWAYS contain the avcC
245 * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */
246 uint8_t *p = &((uint8_t*)p_dec->fmt_in.p_extra)[4];
252 p_sys->i_avcC_length_size = 1 + ((*p++)&0x03);
256 for( i = 0; i < i_sps; i++ )
258 uint16_t i_length = GetWBE( p ); p += 2;
260 (uint8_t*)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra - p )
264 block_t *p_sps = CreateAnnexbNAL( p_dec, p, i_length );
267 ParseNALBlock( p_dec, &b_dummy, p_sps );
272 for( i = 0; i < i_pps; i++ )
274 uint16_t i_length = GetWBE( p ); p += 2;
276 (uint8_t*)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra - p )
280 block_t *p_pps = CreateAnnexbNAL( p_dec, p, i_length );
283 ParseNALBlock( p_dec, &b_dummy, p_pps );
286 msg_Dbg( p_dec, "avcC length size=%d, sps=%d, pps=%d",
287 p_sys->i_avcC_length_size, i_sps, i_pps );
289 if( !p_sys->b_sps || !p_sys->b_pps )
292 /* FIXME: FFMPEG isn't happy at all if you leave this */
293 if( p_dec->fmt_out.i_extra > 0 )
294 free( p_dec->fmt_out.p_extra );
295 p_dec->fmt_out.i_extra = 0;
296 p_dec->fmt_out.p_extra = NULL;
298 /* Set the new extradata */
299 for( i = 0; i < SPS_MAX; i++ )
301 if( p_sys->pp_sps[i] )
302 p_dec->fmt_out.i_extra += p_sys->pp_sps[i]->i_buffer;
304 for( i = 0; i < PPS_MAX; i++ )
306 if( p_sys->pp_pps[i] )
307 p_dec->fmt_out.i_extra += p_sys->pp_pps[i]->i_buffer;
309 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_out.i_extra );
310 if( p_dec->fmt_out.p_extra )
312 uint8_t *p_dst = p_dec->fmt_out.p_extra;
314 for( i = 0; i < SPS_MAX; i++ )
316 if( p_sys->pp_sps[i] )
318 memcpy( p_dst, p_sys->pp_sps[i]->p_buffer, p_sys->pp_sps[i]->i_buffer );
319 p_dst += p_sys->pp_sps[i]->i_buffer;
322 for( i = 0; i < PPS_MAX; i++ )
324 if( p_sys->pp_pps[i] )
326 memcpy( p_dst, p_sys->pp_pps[i]->p_buffer, p_sys->pp_pps[i]->i_buffer );
327 p_dst += p_sys->pp_pps[i]->i_buffer;
330 p_sys->b_header = true;
334 p_dec->fmt_out.i_extra = 0;
338 p_dec->pf_packetize = PacketizeAVC1;
343 /* This type of stream contains data with 3 of 4 byte startcodes
344 * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes
345 * The fmt_out.p_extra should be the same */
348 p_dec->pf_packetize = Packetize;
349 p_dec->pf_get_cc = GetCc;
352 p_sys->b_cc_reset = false;
355 p_sys->i_cc_flags = 0;
356 cc_Init( &p_sys->cc );
359 if( p_dec->fmt_in.i_extra > 0 )
361 block_t *p_init = block_New( p_dec, p_dec->fmt_in.i_extra );
364 memcpy( p_init->p_buffer, p_dec->fmt_in.p_extra,
365 p_dec->fmt_in.i_extra );
367 while( ( p_pic = Packetize( p_dec, &p_init ) ) )
369 /* Should not occur because we should only receive SPS/PPS */
370 block_Release( p_pic );
378 /*****************************************************************************
379 * Close: clean up the packetizer
380 *****************************************************************************/
381 static void Close( vlc_object_t *p_this )
383 decoder_t *p_dec = (decoder_t*)p_this;
384 decoder_sys_t *p_sys = p_dec->p_sys;
388 block_ChainRelease( p_sys->p_frame );
389 for( i = 0; i < SPS_MAX; i++ )
391 if( p_sys->pp_sps[i] )
392 block_Release( p_sys->pp_sps[i] );
394 for( i = 0; i < PPS_MAX; i++ )
396 if( p_sys->pp_pps[i] )
397 block_Release( p_sys->pp_pps[i] );
399 block_BytestreamRelease( &p_sys->bytestream );
400 if( p_dec->pf_get_cc )
401 cc_Exit( &p_sys->cc );
406 /****************************************************************************
407 * Packetize: the whole thing
408 * Search for the startcodes 3 or more bytes
409 * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs
410 ****************************************************************************/
411 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
413 decoder_sys_t *p_sys = p_dec->p_sys;
416 if( !pp_block || !*pp_block )
419 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
421 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
423 p_sys->i_state = STATE_NOSYNC;
424 block_BytestreamFlush( &p_sys->bytestream );
427 block_ChainRelease( p_sys->p_frame );
428 p_sys->p_frame = NULL;
429 p_sys->slice.i_frame_type = 0;
430 p_sys->b_slice = false;
432 block_Release( *pp_block );
436 block_BytestreamPush( &p_sys->bytestream, *pp_block );
442 switch( p_sys->i_state )
445 /* Skip until 3 byte startcode 0 0 1 */
446 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
447 &p_sys->i_offset, p_sys->startcode+1, 3 ) == VLC_SUCCESS)
449 p_sys->i_state = STATE_NEXT_SYNC;
452 if( p_sys->i_offset )
455 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
457 block_BytestreamFlush( &p_sys->bytestream );
460 if( p_sys->i_state != STATE_NEXT_SYNC )
466 p_sys->i_offset = 1; /* To find next startcode */
468 case STATE_NEXT_SYNC:
469 /* Find the next 3 byte startcode 0 0 1*/
470 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
471 &p_sys->i_offset, p_sys->startcode+1, 3 ) != VLC_SUCCESS)
476 block_BytestreamFlush( &p_sys->bytestream );
478 /* Get the new fragment and set the pts/dts */
479 block_t *p_block_bytestream = p_sys->bytestream.p_block;
481 p_pic = block_New( p_dec, p_sys->i_offset +1 );
482 p_pic->i_pts = p_block_bytestream->i_pts;
483 p_pic->i_dts = p_block_bytestream->i_dts;
485 /* Force 4 byte startcode 0 0 0 1 */
486 p_pic->p_buffer[0] = 0;
488 block_GetBytes( &p_sys->bytestream, &p_pic->p_buffer[1],
491 /* Remove trailing 0 bytes */
492 while( p_pic->i_buffer && (!p_pic->p_buffer[p_pic->i_buffer-1] ) )
497 p_pic = ParseNALBlock( p_dec, &b_used_ts, p_pic );
500 p_block_bytestream->i_dts = -1;
501 p_block_bytestream->i_pts = -1;
506 p_sys->i_state = STATE_NOSYNC;
510 msg_Dbg( p_dec, "pts=%"PRId64" dts=%"PRId64,
511 p_pic->i_pts, p_pic->i_dts );
514 /* So p_block doesn't get re-added several times */
515 *pp_block = block_BytestreamPop( &p_sys->bytestream );
517 p_sys->i_state = STATE_NOSYNC;
524 /****************************************************************************
525 * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream
526 * Will always use 4 byte 0 0 0 1 startcodes
527 * Will prepend a SPS and PPS before each keyframe
528 ****************************************************************************/
529 static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
531 decoder_sys_t *p_sys = p_dec->p_sys;
533 block_t *p_ret = NULL;
536 if( !pp_block || !*pp_block )
538 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
540 block_Release( *pp_block );
547 for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; )
554 for( i = 0; i < p_sys->i_avcC_length_size; i++ )
556 i_size = (i_size << 8) | (*p++);
560 i_size > ( p_block->p_buffer + p_block->i_buffer - p ) )
562 msg_Err( p_dec, "Broken frame : size %d is too big", i_size );
566 block_t *p_part = CreateAnnexbNAL( p_dec, p, i_size );
570 p_part->i_dts = p_block->i_dts;
571 p_part->i_pts = p_block->i_pts;
574 if( ( p_pic = ParseNALBlock( p_dec, &b_dummy, p_part ) ) )
576 block_ChainAppend( &p_ret, p_pic );
580 block_Release( p_block );
585 /*****************************************************************************
587 *****************************************************************************/
588 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
590 decoder_sys_t *p_sys = p_dec->p_sys;
593 for( int i = 0; i < 4; i++ )
594 pb_present[i] = p_sys->cc.pb_present[i];
596 if( p_sys->cc.i_data <= 0 )
599 p_cc = block_New( p_dec, p_sys->cc.i_data);
602 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
604 p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
605 p_cc->i_flags = ( p_sys->cc.b_reorder ? p_sys->i_cc_flags : BLOCK_FLAG_TYPE_P ) & BLOCK_FLAG_TYPE_MASK;
607 cc_Flush( &p_sys->cc );
611 /****************************************************************************
613 ****************************************************************************/
614 static block_t *CreateAnnexbNAL( decoder_t *p_dec, const uint8_t *p, int i_size )
618 p_nal = block_New( p_dec, 4 + i_size );
619 if( !p_nal ) return NULL;
622 p_nal->p_buffer[0] = 0x00;
623 p_nal->p_buffer[1] = 0x00;
624 p_nal->p_buffer[2] = 0x00;
625 p_nal->p_buffer[3] = 0x01;
628 memcpy( &p_nal->p_buffer[4], p, i_size );
634 static void CreateDecodedNAL( uint8_t **pp_ret, int *pi_ret,
635 const uint8_t *src, int i_src )
637 const uint8_t *end = &src[i_src];
638 uint8_t *dst = malloc( i_src );
646 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 &&
658 *pi_ret = dst - *pp_ret;
661 static inline int bs_read_ue( bs_t *s )
665 while( bs_read1( s ) == 0 && s->p < s->p_end && i < 32 )
669 return( ( 1 << i) - 1 + bs_read( s, i ) );
672 static inline int bs_read_se( bs_t *s )
674 int val = bs_read_ue( s );
676 return val&0x01 ? (val+1)/2 : -(val/2);
679 /*****************************************************************************
680 * ParseNALBlock: parses annexB type NALs
681 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
682 *****************************************************************************/
683 static block_t *ParseNALBlock( decoder_t *p_dec, bool *pb_used_ts, block_t *p_frag )
685 decoder_sys_t *p_sys = p_dec->p_sys;
686 block_t *p_pic = NULL;
688 const int i_nal_ref_idc = (p_frag->p_buffer[4] >> 5)&0x03;
689 const int i_nal_type = p_frag->p_buffer[4]&0x1f;
690 const mtime_t i_frag_dts = p_frag->i_dts;
691 const mtime_t i_frag_pts = p_frag->i_pts;
693 if( p_sys->b_slice && ( !p_sys->b_sps || !p_sys->b_pps ) )
695 block_ChainRelease( p_sys->p_frame );
696 msg_Warn( p_dec, "waiting for SPS/PPS" );
699 p_sys->slice.i_frame_type = 0;
700 p_sys->p_frame = NULL;
701 p_sys->b_slice = false;
702 p_sys->b_cc_reset = true;
705 if( ( !p_sys->b_sps || !p_sys->b_pps ) &&
706 i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
708 p_sys->b_slice = true;
709 /* Fragment will be discarded later on */
711 else if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
716 ParseSlice( p_dec, &b_new_picture, &slice, i_nal_ref_idc, i_nal_type, p_frag );
719 if( b_new_picture && p_sys->b_slice )
720 p_pic = OutputPicture( p_dec );
723 p_sys->slice = slice;
724 p_sys->b_slice = true;
726 else if( i_nal_type == NAL_SPS )
729 p_pic = OutputPicture( p_dec );
731 PutSPS( p_dec, p_frag );
733 /* Do not append the SPS because we will insert it on keyframes */
736 else if( i_nal_type == NAL_PPS )
739 p_pic = OutputPicture( p_dec );
741 PutPPS( p_dec, p_frag );
743 /* Do not append the PPS because we will insert it on keyframes */
746 else if( i_nal_type == NAL_AU_DELIMITER ||
747 i_nal_type == NAL_SEI ||
748 ( i_nal_type >= 13 && i_nal_type <= 18 ) )
751 p_pic = OutputPicture( p_dec );
753 /* Parse SEI for CC support */
754 ParseSei( p_dec, p_frag );
757 if( !p_pic && p_sys->b_cc_reset )
759 p_sys->b_cc_reset = false;
760 cc_Flush( &p_sys->cc );
763 /* Append the block */
765 block_ChainAppend( &p_sys->p_frame, p_frag );
768 if( p_sys->i_frame_dts < 0 && p_sys->i_frame_pts < 0 )
770 p_sys->i_frame_dts = i_frag_dts;
771 p_sys->i_frame_pts = i_frag_pts;
777 static block_t *OutputPicture( decoder_t *p_dec )
779 decoder_sys_t *p_sys = p_dec->p_sys;
782 if( !p_sys->b_header && p_sys->slice.i_frame_type != BLOCK_FLAG_TYPE_I)
785 if( p_sys->slice.i_frame_type == BLOCK_FLAG_TYPE_I && p_sys->b_sps && p_sys->b_pps )
787 block_t *p_list = NULL;
790 for( i = 0; i < SPS_MAX; i++ )
792 if( p_sys->pp_sps[i] )
793 block_ChainAppend( &p_list, block_Duplicate( p_sys->pp_sps[i] ) );
795 for( i = 0; i < PPS_MAX; i++ )
797 if( p_sys->pp_pps[i] )
798 block_ChainAppend( &p_list, block_Duplicate( p_sys->pp_pps[i] ) );
801 p_sys->b_header = true;
803 block_ChainAppend( &p_list, p_sys->p_frame );
804 p_pic = block_ChainGather( p_list );
808 p_pic = block_ChainGather( p_sys->p_frame );
810 p_pic->i_dts = p_sys->i_frame_dts;
811 p_pic->i_pts = p_sys->i_frame_pts;
812 p_pic->i_length = 0; /* FIXME */
813 p_pic->i_flags |= p_sys->slice.i_frame_type;
815 p_sys->slice.i_frame_type = 0;
816 p_sys->p_frame = NULL;
817 p_sys->i_frame_dts = -1;
818 p_sys->i_frame_pts = -1;
819 p_sys->b_slice = false;
822 p_sys->b_cc_reset = true;
823 p_sys->i_cc_pts = p_pic->i_pts;
824 p_sys->i_cc_dts = p_pic->i_dts;
825 p_sys->i_cc_flags = p_pic->i_flags;
830 static void PutSPS( decoder_t *p_dec, block_t *p_frag )
832 decoder_sys_t *p_sys = p_dec->p_sys;
834 uint8_t *pb_dec = NULL;
840 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5],
841 p_frag->i_buffer - 5 );
843 bs_init( &s, pb_dec, i_dec );
844 int i_profile_idc = bs_read( &s, 8 );
845 /* Skip constraint_set0123, reserved(4), level(8) */
846 bs_skip( &s, 1+1+1+1 + 4 + 8 );
848 i_sps_id = bs_read_ue( &s );
849 if( i_sps_id >= SPS_MAX )
851 msg_Warn( p_dec, "invalid SPS (sps_id=%d)", i_sps_id );
853 block_Release( p_frag );
857 if( i_profile_idc == 100 || i_profile_idc == 110 ||
858 i_profile_idc == 122 || i_profile_idc == 244 ||
859 i_profile_idc == 44 || i_profile_idc == 83 ||
860 i_profile_idc == 86 )
862 /* chroma_format_idc */
863 const int i_chroma_format_idc = bs_read_ue( &s );
864 if( i_chroma_format_idc == 3 )
865 bs_skip( &s, 1 ); /* seperate_colour_plane_flag */
866 /* bit_depth_luma_minus8 */
868 /* bit_depth_chroma_minus8 */
870 /* qpprime_y_zero_transform_bypass_flag */
872 /* seq_scaling_matrix_present_flag */
873 i_tmp = bs_read( &s, 1 );
876 for( int i = 0; i < ((3 != i_chroma_format_idc) ? 8 : 12); i++ )
878 /* seq_scaling_list_present_flag[i] */
879 i_tmp = bs_read( &s, 1 );
882 const int i_size_of_scaling_list = (i < 6 ) ? 16 : 64;
883 /* scaling_list (...) */
886 for( int j = 0; j < i_size_of_scaling_list; j++ )
888 if( i_nextscale != 0 )
891 i_tmp = bs_read( &s, 1 );
892 i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
893 /* useDefaultScalingMatrixFlag = ... */
896 i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
902 /* Skip i_log2_max_frame_num */
903 p_sys->i_log2_max_frame_num = bs_read_ue( &s );
904 if( p_sys->i_log2_max_frame_num > 12)
905 p_sys->i_log2_max_frame_num = 12;
907 p_sys->i_pic_order_cnt_type = bs_read_ue( &s );
908 if( p_sys->i_pic_order_cnt_type == 0 )
910 /* skip i_log2_max_poc_lsb */
911 p_sys->i_log2_max_pic_order_cnt_lsb = bs_read_ue( &s );
912 if( p_sys->i_log2_max_pic_order_cnt_lsb > 12 )
913 p_sys->i_log2_max_pic_order_cnt_lsb = 12;
915 else if( p_sys->i_pic_order_cnt_type == 1 )
918 /* skip b_delta_pic_order_always_zero */
919 p_sys->i_delta_pic_order_always_zero_flag = bs_read( &s, 1 );
920 /* skip i_offset_for_non_ref_pic */
922 /* skip i_offset_for_top_to_bottom_field */
924 /* read i_num_ref_frames_in_poc_cycle */
925 i_cycle = bs_read_ue( &s );
926 if( i_cycle > 256 ) i_cycle = 256;
929 /* skip i_offset_for_ref_frame */
934 /* i_num_ref_frames */
936 /* b_gaps_in_frame_num_value_allowed */
940 p_dec->fmt_out.video.i_width = 16 * ( bs_read_ue( &s ) + 1 );
941 p_dec->fmt_out.video.i_height = 16 * ( bs_read_ue( &s ) + 1 );
943 /* b_frame_mbs_only */
944 p_sys->b_frame_mbs_only = bs_read( &s, 1 );
945 if( p_sys->b_frame_mbs_only == 0 )
949 /* b_direct8x8_inference */
953 i_tmp = bs_read( &s, 1 );
967 i_tmp = bs_read( &s, 1 );
970 /* read the aspect ratio part if any */
971 i_tmp = bs_read( &s, 1 );
974 static const struct { int w, h; } sar[17] =
976 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 },
977 { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
978 { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
979 { 64, 33 }, { 160,99 }, { 4, 3 }, { 3, 2 },
982 int i_sar = bs_read( &s, 8 );
990 else if( i_sar == 255 )
992 w = bs_read( &s, 16 );
993 h = bs_read( &s, 16 );
1002 p_dec->fmt_out.video.i_aspect = (int64_t)VOUT_ASPECT_FACTOR *
1003 ( w * p_dec->fmt_out.video.i_width ) /
1004 ( h * p_dec->fmt_out.video.i_height);
1006 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR;
1012 /* We have a new SPS */
1014 msg_Dbg( p_dec, "found NAL_SPS (sps_id=%d)", i_sps_id );
1015 p_sys->b_sps = true;
1017 if( p_sys->pp_sps[i_sps_id] )
1018 block_Release( p_sys->pp_sps[i_sps_id] );
1019 p_sys->pp_sps[i_sps_id] = p_frag;
1022 static void PutPPS( decoder_t *p_dec, block_t *p_frag )
1024 decoder_sys_t *p_sys = p_dec->p_sys;
1029 bs_init( &s, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
1030 i_pps_id = bs_read_ue( &s ); // pps id
1031 i_sps_id = bs_read_ue( &s ); // sps id
1032 if( i_pps_id >= PPS_MAX || i_sps_id >= SPS_MAX )
1034 msg_Warn( p_dec, "invalid PPS (pps_id=%d sps_id=%d)", i_pps_id, i_sps_id );
1035 block_Release( p_frag );
1038 bs_skip( &s, 1 ); // entropy coding mode flag
1039 p_sys->i_pic_order_present_flag = bs_read( &s, 1 );
1042 /* We have a new PPS */
1044 msg_Dbg( p_dec, "found NAL_PPS (pps_id=%d sps_id=%d)", i_pps_id, i_sps_id );
1045 p_sys->b_pps = true;
1047 if( p_sys->pp_pps[i_pps_id] )
1048 block_Release( p_sys->pp_pps[i_pps_id] );
1049 p_sys->pp_pps[i_pps_id] = p_frag;
1052 static void ParseSlice( decoder_t *p_dec, bool *pb_new_picture, slice_t *p_slice,
1053 int i_nal_ref_idc, int i_nal_type, const block_t *p_frag )
1055 decoder_sys_t *p_sys = p_dec->p_sys;
1058 int i_first_mb, i_slice_type;
1062 /* do not convert the whole frame */
1063 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5],
1064 __MIN( p_frag->i_buffer - 5, 60 ) );
1065 bs_init( &s, pb_dec, i_dec );
1067 /* first_mb_in_slice */
1068 i_first_mb = bs_read_ue( &s );
1071 switch( (i_slice_type = bs_read_ue( &s )) )
1074 slice.i_frame_type = BLOCK_FLAG_TYPE_P;
1077 slice.i_frame_type = BLOCK_FLAG_TYPE_B;
1080 slice.i_frame_type = BLOCK_FLAG_TYPE_I;
1082 case 3: case 8: /* SP */
1083 slice.i_frame_type = BLOCK_FLAG_TYPE_P;
1086 slice.i_frame_type = BLOCK_FLAG_TYPE_I;
1089 slice.i_frame_type = 0;
1094 slice.i_nal_type = i_nal_type;
1095 slice.i_nal_ref_idc = i_nal_ref_idc;
1097 slice.i_pic_parameter_set_id = bs_read_ue( &s );
1098 slice.i_frame_num = bs_read( &s, p_sys->i_log2_max_frame_num + 4 );
1100 slice.i_field_pic_flag = 0;
1101 slice.i_bottom_field_flag = -1;
1102 if( !p_sys->b_frame_mbs_only )
1104 /* field_pic_flag */
1105 slice.i_field_pic_flag = bs_read( &s, 1 );
1106 if( slice.i_field_pic_flag )
1107 slice.i_bottom_field_flag = bs_read( &s, 1 );
1110 slice.i_idr_pic_id = p_sys->slice.i_idr_pic_id;
1111 if( slice.i_nal_type == NAL_SLICE_IDR )
1112 slice.i_idr_pic_id = bs_read_ue( &s );
1114 slice.i_pic_order_cnt_lsb = -1;
1115 slice.i_delta_pic_order_cnt_bottom = -1;
1116 slice.i_delta_pic_order_cnt0 = 0;
1117 slice.i_delta_pic_order_cnt1 = 0;
1118 if( p_sys->i_pic_order_cnt_type == 0 )
1120 slice.i_pic_order_cnt_lsb = bs_read( &s, p_sys->i_log2_max_pic_order_cnt_lsb + 4 );
1121 if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
1122 slice.i_delta_pic_order_cnt_bottom = bs_read_se( &s );
1124 else if( (p_sys->i_pic_order_cnt_type == 1) &&
1125 (!p_sys->i_delta_pic_order_always_zero_flag) )
1127 slice.i_delta_pic_order_cnt0 = bs_read_se( &s );
1128 if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
1129 slice.i_delta_pic_order_cnt1 = bs_read_se( &s );
1133 /* Detection of the first VCL NAL unit of a primary coded picture
1134 * (cf. 7.4.1.2.4) */
1136 if( slice.i_frame_num != p_sys->slice.i_frame_num ||
1137 slice.i_pic_parameter_set_id != p_sys->slice.i_pic_parameter_set_id ||
1138 slice.i_field_pic_flag != p_sys->slice.i_field_pic_flag ||
1139 slice.i_nal_ref_idc != p_sys->slice.i_nal_ref_idc )
1141 if( (slice.i_bottom_field_flag != -1) &&
1142 (p_sys->slice.i_bottom_field_flag != -1) &&
1143 (slice.i_bottom_field_flag != p_sys->slice.i_bottom_field_flag) )
1145 if( p_sys->i_pic_order_cnt_type == 0 &&
1146 ( slice.i_pic_order_cnt_lsb != p_sys->slice.i_pic_order_cnt_lsb ||
1147 slice.i_delta_pic_order_cnt_bottom != p_sys->slice.i_delta_pic_order_cnt_bottom ) )
1149 else if( p_sys->i_pic_order_cnt_type == 1 &&
1150 ( slice.i_delta_pic_order_cnt0 != p_sys->slice.i_delta_pic_order_cnt0 ||
1151 slice.i_delta_pic_order_cnt1 != p_sys->slice.i_delta_pic_order_cnt1 ) )
1153 if( ( slice.i_nal_type == NAL_SLICE_IDR || p_sys->slice.i_nal_type == NAL_SLICE_IDR ) &&
1154 ( slice.i_nal_type != p_sys->slice.i_nal_type || slice.i_idr_pic_id != p_sys->slice.i_idr_pic_id ) )
1158 *pb_new_picture = b_pic;
1162 static void ParseSei( decoder_t *p_dec, block_t *p_frag )
1164 decoder_sys_t *p_sys = p_dec->p_sys;
1169 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
1173 /* The +1 is for rbsp trailing bits */
1174 for( int i_used = 0; i_used+1 < i_dec; )
1178 while( i_used+1 < i_dec )
1180 const int i_byte = pb_dec[i_used++];
1182 if( i_byte != 0xff )
1187 while( i_used+1 < i_dec )
1189 const int i_byte = pb_dec[i_used++];
1191 if( i_byte != 0xff )
1195 if( i_used + i_size + 1 > i_dec )
1198 /* Look for user_data_registered_itu_t_t35 */
1201 static const uint8_t p_dvb1_data_start_code[] = {
1204 0x47, 0x41, 0x39, 0x34
1206 const int i_t35 = i_size;
1207 const uint8_t *p_t35 = &pb_dec[i_used];
1209 /* Check for we have DVB1_data() */
1211 !memcmp( p_t35, p_dvb1_data_start_code, sizeof(p_dvb1_data_start_code) ) )
1213 cc_Extract( &p_sys->cc, &p_t35[3], i_t35 - 3 );