1 /*****************************************************************************
2 * h264.c: h264/avc video packetizer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 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 * Derk-Jan Hartman <hartman at videolan dot org>
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 /*****************************************************************************
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"
44 #include "packetizer_helper.h"
45 #include "../demux/mpeg/mpeg_parser_helpers.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
54 set_category( CAT_SOUT )
55 set_subcategory( SUBCAT_SOUT_PACKETIZER )
56 set_description( N_("H.264 video packetizer") )
57 set_capability( "packetizer", 50 )
58 set_callbacks( Open, Close )
62 /****************************************************************************
64 ****************************************************************************/
71 int i_pic_parameter_set_id;
75 int i_bottom_field_flag;
79 int i_pic_order_cnt_lsb;
80 int i_delta_pic_order_cnt_bottom;
82 int i_delta_pic_order_cnt0;
83 int i_delta_pic_order_cnt1;
91 packetizer_t packetizer;
102 block_t *pp_sps[SPS_MAX];
103 block_t *pp_pps[PPS_MAX];
104 int i_recovery_frames; /* -1 = no recovery */
107 int i_avcC_length_size;
109 /* Useful values of the Sequence Parameter Set */
110 int i_log2_max_frame_num;
111 int b_frame_mbs_only;
112 int i_pic_order_cnt_type;
113 int i_delta_pic_order_always_zero_flag;
114 int i_log2_max_pic_order_cnt_lsb;
116 /* Value from Picture Parameter Set */
117 int i_pic_order_present_flag;
119 /* Useful values of the Slice Header */
142 NAL_SLICE_IDR = 5, /* ref_idc != 0 */
143 NAL_SEI = 6, /* ref_idc == 0 */
147 /* ref_idc == 0 for 6,9,10,11,12 */
150 /* Defined in H.264 annex D */
153 SEI_USER_DATA_REGISTERED = 4,
154 SEI_RECOVERY_POINT = 6
157 #define BLOCK_FLAG_PRIVATE_AUD (1 << BLOCK_FLAG_PRIVATE_SHIFT)
159 static block_t *Packetize( decoder_t *, block_t ** );
160 static block_t *PacketizeAVC1( decoder_t *, block_t ** );
161 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] );
163 static void PacketizeReset( void *p_private, bool b_broken );
164 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * );
165 static int PacketizeValidate( void *p_private, block_t * );
167 static block_t *ParseNALBlock( decoder_t *, bool *pb_ts_used, block_t * );
168 static block_t *CreateAnnexbNAL( decoder_t *, const uint8_t *p, int );
170 static block_t *OutputPicture( decoder_t *p_dec );
171 static void PutSPS( decoder_t *p_dec, block_t *p_frag );
172 static void PutPPS( decoder_t *p_dec, block_t *p_frag );
173 static void ParseSlice( decoder_t *p_dec, bool *pb_new_picture, slice_t *p_slice,
174 int i_nal_ref_idc, int i_nal_type, const block_t *p_frag );
175 static void ParseSei( decoder_t *, block_t * );
178 static const uint8_t p_h264_startcode[3] = { 0x00, 0x00, 0x01 };
180 /*****************************************************************************
181 * Open: probe the packetizer and return score
182 * When opening after demux, the packetizer is only loaded AFTER the decoder
183 * That means that what you set in fmt_out is ignored by the decoder in this special case
184 *****************************************************************************/
185 static int Open( vlc_object_t *p_this )
187 decoder_t *p_dec = (decoder_t*)p_this;
188 decoder_sys_t *p_sys;
191 if( p_dec->fmt_in.i_codec != VLC_CODEC_H264 )
193 if( p_dec->fmt_in.i_original_fourcc == VLC_FOURCC( 'a', 'v', 'c', '1') &&
194 p_dec->fmt_in.i_extra < 7 )
197 /* Allocate the memory needed to store the decoder's structure */
198 if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
203 packetizer_Init( &p_sys->packetizer,
204 p_h264_startcode, sizeof(p_h264_startcode),
205 p_h264_startcode, 1, 5,
206 PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
208 p_sys->b_slice = false;
209 p_sys->p_frame = NULL;
210 p_sys->b_frame_sps = false;
211 p_sys->b_frame_pps = false;
213 p_sys->b_header= false;
214 p_sys->b_sps = false;
215 p_sys->b_pps = false;
216 for( i = 0; i < SPS_MAX; i++ )
217 p_sys->pp_sps[i] = NULL;
218 for( i = 0; i < PPS_MAX; i++ )
219 p_sys->pp_pps[i] = NULL;
220 p_sys->i_recovery_frames = -1;
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 = VLC_TS_INVALID;
234 p_sys->i_frame_pts = VLC_TS_INVALID;
236 /* Setup properties */
237 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
238 p_dec->fmt_out.i_codec = VLC_CODEC_H264;
240 if( p_dec->fmt_in.i_original_fourcc == 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->i_cc_pts = VLC_TS_INVALID;
353 p_sys->i_cc_dts = VLC_TS_INVALID;
354 p_sys->i_cc_flags = 0;
355 cc_Init( &p_sys->cc );
356 cc_Init( &p_sys->cc_next );
359 if( p_dec->fmt_in.i_extra > 0 )
360 packetizer_Header( &p_sys->packetizer,
361 p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
367 /*****************************************************************************
368 * Close: clean up the packetizer
369 *****************************************************************************/
370 static void Close( vlc_object_t *p_this )
372 decoder_t *p_dec = (decoder_t*)p_this;
373 decoder_sys_t *p_sys = p_dec->p_sys;
377 block_ChainRelease( p_sys->p_frame );
378 for( i = 0; i < SPS_MAX; i++ )
380 if( p_sys->pp_sps[i] )
381 block_Release( p_sys->pp_sps[i] );
383 for( i = 0; i < PPS_MAX; i++ )
385 if( p_sys->pp_pps[i] )
386 block_Release( p_sys->pp_pps[i] );
388 packetizer_Clean( &p_sys->packetizer );
390 if( p_dec->pf_get_cc )
392 cc_Exit( &p_sys->cc_next );
393 cc_Exit( &p_sys->cc );
399 /****************************************************************************
400 * Packetize: the whole thing
401 * Search for the startcodes 3 or more bytes
402 * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs
403 ****************************************************************************/
404 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
406 decoder_sys_t *p_sys = p_dec->p_sys;
408 return packetizer_Packetize( &p_sys->packetizer, pp_block );
411 /****************************************************************************
412 * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream
413 * Will always use 4 byte 0 0 0 1 startcodes
414 * Will prepend a SPS and PPS before each keyframe
415 ****************************************************************************/
416 static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
418 decoder_sys_t *p_sys = p_dec->p_sys;
420 block_t *p_ret = NULL;
423 if( !pp_block || !*pp_block )
425 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
427 block_Release( *pp_block );
434 for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; )
441 for( i = 0; i < p_sys->i_avcC_length_size; i++ )
443 i_size = (i_size << 8) | (*p++);
447 i_size > ( p_block->p_buffer + p_block->i_buffer - p ) )
449 msg_Err( p_dec, "Broken frame : size %d is too big", i_size );
453 block_t *p_part = CreateAnnexbNAL( p_dec, p, i_size );
457 p_part->i_dts = p_block->i_dts;
458 p_part->i_pts = p_block->i_pts;
461 if( ( p_pic = ParseNALBlock( p_dec, &b_dummy, p_part ) ) )
463 block_ChainAppend( &p_ret, p_pic );
467 block_Release( p_block );
472 /*****************************************************************************
474 *****************************************************************************/
475 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
477 decoder_sys_t *p_sys = p_dec->p_sys;
480 for( int i = 0; i < 4; i++ )
481 pb_present[i] = p_sys->cc.pb_present[i];
483 if( p_sys->cc.i_data <= 0 )
486 p_cc = block_Alloc( p_sys->cc.i_data);
489 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
491 p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
492 p_cc->i_flags = ( p_sys->cc.b_reorder ? p_sys->i_cc_flags : BLOCK_FLAG_TYPE_P ) & BLOCK_FLAG_TYPE_MASK;
494 cc_Flush( &p_sys->cc );
498 /****************************************************************************
500 ****************************************************************************/
501 static void PacketizeReset( void *p_private, bool b_broken )
503 decoder_t *p_dec = p_private;
504 decoder_sys_t *p_sys = p_dec->p_sys;
509 block_ChainRelease( p_sys->p_frame );
510 p_sys->p_frame = NULL;
511 p_sys->b_frame_sps = false;
512 p_sys->b_frame_pps = false;
513 p_sys->slice.i_frame_type = 0;
514 p_sys->b_slice = false;
516 p_sys->i_frame_pts = VLC_TS_INVALID;
517 p_sys->i_frame_dts = VLC_TS_INVALID;
519 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
521 decoder_t *p_dec = p_private;
523 /* Remove trailing 0 bytes */
524 while( p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
527 return ParseNALBlock( p_dec, pb_ts_used, p_block );
529 static int PacketizeValidate( void *p_private, block_t *p_au )
531 VLC_UNUSED(p_private);
536 static block_t *CreateAnnexbNAL( decoder_t *p_dec, const uint8_t *p, int i_size )
540 p_nal = block_Alloc( 4 + i_size );
541 if( !p_nal ) return NULL;
544 p_nal->p_buffer[0] = 0x00;
545 p_nal->p_buffer[1] = 0x00;
546 p_nal->p_buffer[2] = 0x00;
547 p_nal->p_buffer[3] = 0x01;
550 memcpy( &p_nal->p_buffer[4], p, i_size );
556 static void CreateDecodedNAL( uint8_t **pp_ret, int *pi_ret,
557 const uint8_t *src, int i_src )
559 uint8_t *dst = malloc( i_src );
564 *pi_ret = nal_decode(src, dst, i_src);
567 /*****************************************************************************
568 * ParseNALBlock: parses annexB type NALs
569 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
570 *****************************************************************************/
571 static block_t *ParseNALBlock( decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag )
573 decoder_sys_t *p_sys = p_dec->p_sys;
574 block_t *p_pic = NULL;
576 const int i_nal_ref_idc = (p_frag->p_buffer[4] >> 5)&0x03;
577 const int i_nal_type = p_frag->p_buffer[4]&0x1f;
578 const mtime_t i_frag_dts = p_frag->i_dts;
579 const mtime_t i_frag_pts = p_frag->i_pts;
581 if( p_sys->b_slice && ( !p_sys->b_sps || !p_sys->b_pps ) )
583 block_ChainRelease( p_sys->p_frame );
584 msg_Warn( p_dec, "waiting for SPS/PPS" );
587 p_sys->slice.i_frame_type = 0;
588 p_sys->p_frame = NULL;
589 p_sys->b_frame_sps = false;
590 p_sys->b_frame_pps = false;
591 p_sys->b_slice = false;
592 cc_Flush( &p_sys->cc_next );
595 if( ( !p_sys->b_sps || !p_sys->b_pps ) &&
596 i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
598 p_sys->b_slice = true;
599 /* Fragment will be discarded later on */
601 else if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
606 ParseSlice( p_dec, &b_new_picture, &slice, i_nal_ref_idc, i_nal_type, p_frag );
609 if( b_new_picture && p_sys->b_slice )
610 p_pic = OutputPicture( p_dec );
613 p_sys->slice = slice;
614 p_sys->b_slice = true;
616 else if( i_nal_type == NAL_SPS )
619 p_pic = OutputPicture( p_dec );
620 p_sys->b_frame_sps = true;
622 PutSPS( p_dec, p_frag );
624 /* Do not append the SPS because we will insert it on keyframes */
627 else if( i_nal_type == NAL_PPS )
630 p_pic = OutputPicture( p_dec );
631 p_sys->b_frame_pps = true;
633 PutPPS( p_dec, p_frag );
635 /* Do not append the PPS because we will insert it on keyframes */
638 else if( i_nal_type == NAL_AU_DELIMITER ||
639 i_nal_type == NAL_SEI ||
640 ( i_nal_type >= 13 && i_nal_type <= 18 ) )
643 p_pic = OutputPicture( p_dec );
645 /* Parse SEI for CC support */
646 if( i_nal_type == NAL_SEI )
648 ParseSei( p_dec, p_frag );
650 else if( i_nal_type == NAL_AU_DELIMITER )
652 if( p_sys->p_frame && (p_sys->p_frame->i_flags & BLOCK_FLAG_PRIVATE_AUD) )
654 block_Release( p_frag );
659 p_frag->i_flags |= BLOCK_FLAG_PRIVATE_AUD;
664 /* Append the block */
666 block_ChainAppend( &p_sys->p_frame, p_frag );
669 if( p_sys->i_frame_dts <= VLC_TS_INVALID &&
670 p_sys->i_frame_pts <= VLC_TS_INVALID )
672 p_sys->i_frame_dts = i_frag_dts;
673 p_sys->i_frame_pts = i_frag_pts;
679 static block_t *OutputPicture( decoder_t *p_dec )
681 decoder_sys_t *p_sys = p_dec->p_sys;
684 if ( !p_sys->b_header && p_sys->i_recovery_frames != -1 )
686 if( p_sys->i_recovery_frames == 0 )
688 msg_Dbg( p_dec, "Recovery from SEI recovery point complete" );
689 p_sys->b_header = true;
691 --p_sys->i_recovery_frames;
694 if( !p_sys->b_header && p_sys->i_recovery_frames == -1 &&
695 p_sys->slice.i_frame_type != BLOCK_FLAG_TYPE_I)
698 const bool b_sps_pps_i = p_sys->slice.i_frame_type == BLOCK_FLAG_TYPE_I &&
701 if( b_sps_pps_i || p_sys->b_frame_sps || p_sys->b_frame_pps )
703 block_t *p_head = NULL;
704 if( p_sys->p_frame->i_flags & BLOCK_FLAG_PRIVATE_AUD )
706 p_head = p_sys->p_frame;
707 p_sys->p_frame = p_sys->p_frame->p_next;
710 block_t *p_list = NULL;
711 for( int i = 0; i < SPS_MAX && (b_sps_pps_i || p_sys->b_frame_sps); i++ )
713 if( p_sys->pp_sps[i] )
714 block_ChainAppend( &p_list, block_Duplicate( p_sys->pp_sps[i] ) );
716 for( int i = 0; i < PPS_MAX && (b_sps_pps_i || p_sys->b_frame_pps); i++ )
718 if( p_sys->pp_pps[i] )
719 block_ChainAppend( &p_list, block_Duplicate( p_sys->pp_pps[i] ) );
721 if( b_sps_pps_i && p_list )
722 p_sys->b_header = true;
725 p_head->p_next = p_list;
728 block_ChainAppend( &p_head, p_sys->p_frame );
730 p_pic = block_ChainGather( p_head );
734 p_pic = block_ChainGather( p_sys->p_frame );
736 p_pic->i_dts = p_sys->i_frame_dts;
737 p_pic->i_pts = p_sys->i_frame_pts;
738 p_pic->i_length = 0; /* FIXME */
739 p_pic->i_flags |= p_sys->slice.i_frame_type;
740 p_pic->i_flags &= ~BLOCK_FLAG_PRIVATE_AUD;
741 if( !p_sys->b_header )
742 p_pic->i_flags |= BLOCK_FLAG_PREROLL;
744 p_sys->slice.i_frame_type = 0;
745 p_sys->p_frame = NULL;
746 p_sys->i_frame_dts = VLC_TS_INVALID;
747 p_sys->i_frame_pts = VLC_TS_INVALID;
748 p_sys->b_frame_sps = false;
749 p_sys->b_frame_pps = false;
750 p_sys->b_slice = false;
753 p_sys->i_cc_pts = p_pic->i_pts;
754 p_sys->i_cc_dts = p_pic->i_dts;
755 p_sys->i_cc_flags = p_pic->i_flags;
757 p_sys->cc = p_sys->cc_next;
758 cc_Flush( &p_sys->cc_next );
763 static void PutSPS( decoder_t *p_dec, block_t *p_frag )
765 decoder_sys_t *p_sys = p_dec->p_sys;
767 uint8_t *pb_dec = NULL;
773 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5],
774 p_frag->i_buffer - 5 );
776 bs_init( &s, pb_dec, i_dec );
777 int i_profile_idc = bs_read( &s, 8 );
778 p_dec->fmt_out.i_profile = i_profile_idc;
779 /* Skip constraint_set0123, reserved(4) */
780 bs_skip( &s, 1+1+1+1 + 4 );
781 p_dec->fmt_out.i_level = bs_read( &s, 8 );
783 i_sps_id = bs_read_ue( &s );
784 if( i_sps_id >= SPS_MAX || i_sps_id < 0 )
786 msg_Warn( p_dec, "invalid SPS (sps_id=%d)", i_sps_id );
788 block_Release( p_frag );
792 if( i_profile_idc == 100 || i_profile_idc == 110 ||
793 i_profile_idc == 122 || i_profile_idc == 244 ||
794 i_profile_idc == 44 || i_profile_idc == 83 ||
795 i_profile_idc == 86 )
797 /* chroma_format_idc */
798 const int i_chroma_format_idc = bs_read_ue( &s );
799 if( i_chroma_format_idc == 3 )
800 bs_skip( &s, 1 ); /* separate_colour_plane_flag */
801 /* bit_depth_luma_minus8 */
803 /* bit_depth_chroma_minus8 */
805 /* qpprime_y_zero_transform_bypass_flag */
807 /* seq_scaling_matrix_present_flag */
808 i_tmp = bs_read( &s, 1 );
811 for( int i = 0; i < ((3 != i_chroma_format_idc) ? 8 : 12); i++ )
813 /* seq_scaling_list_present_flag[i] */
814 i_tmp = bs_read( &s, 1 );
817 const int i_size_of_scaling_list = (i < 6 ) ? 16 : 64;
818 /* scaling_list (...) */
821 for( int j = 0; j < i_size_of_scaling_list; j++ )
823 if( i_nextscale != 0 )
826 i_tmp = bs_read_se( &s );
827 i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
828 /* useDefaultScalingMatrixFlag = ... */
831 i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
837 /* Skip i_log2_max_frame_num */
838 p_sys->i_log2_max_frame_num = bs_read_ue( &s );
839 if( p_sys->i_log2_max_frame_num > 12)
840 p_sys->i_log2_max_frame_num = 12;
842 p_sys->i_pic_order_cnt_type = bs_read_ue( &s );
843 if( p_sys->i_pic_order_cnt_type == 0 )
845 /* skip i_log2_max_poc_lsb */
846 p_sys->i_log2_max_pic_order_cnt_lsb = bs_read_ue( &s );
847 if( p_sys->i_log2_max_pic_order_cnt_lsb > 12 )
848 p_sys->i_log2_max_pic_order_cnt_lsb = 12;
850 else if( p_sys->i_pic_order_cnt_type == 1 )
853 /* skip b_delta_pic_order_always_zero */
854 p_sys->i_delta_pic_order_always_zero_flag = bs_read( &s, 1 );
855 /* skip i_offset_for_non_ref_pic */
857 /* skip i_offset_for_top_to_bottom_field */
859 /* read i_num_ref_frames_in_poc_cycle */
860 i_cycle = bs_read_ue( &s );
861 if( i_cycle > 256 ) i_cycle = 256;
864 /* skip i_offset_for_ref_frame */
869 /* i_num_ref_frames */
871 /* b_gaps_in_frame_num_value_allowed */
875 p_dec->fmt_out.video.i_width = 16 * ( bs_read_ue( &s ) + 1 );
876 p_dec->fmt_out.video.i_height = 16 * ( bs_read_ue( &s ) + 1 );
878 /* b_frame_mbs_only */
879 p_sys->b_frame_mbs_only = bs_read( &s, 1 );
880 p_dec->fmt_out.video.i_height *= ( 2 - p_sys->b_frame_mbs_only );
881 if( p_sys->b_frame_mbs_only == 0 )
885 /* b_direct8x8_inference */
889 i_tmp = bs_read( &s, 1 );
903 i_tmp = bs_read( &s, 1 );
906 /* read the aspect ratio part if any */
907 i_tmp = bs_read( &s, 1 );
910 static const struct { int w, h; } sar[17] =
912 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 },
913 { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
914 { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
915 { 64, 33 }, { 160,99 }, { 4, 3 }, { 3, 2 },
918 int i_sar = bs_read( &s, 8 );
926 else if( i_sar == 255 )
928 w = bs_read( &s, 16 );
929 h = bs_read( &s, 16 );
937 if( w != 0 && h != 0 )
939 p_dec->fmt_out.video.i_sar_num = w;
940 p_dec->fmt_out.video.i_sar_den = h;
944 p_dec->fmt_out.video.i_sar_num = 1;
945 p_dec->fmt_out.video.i_sar_den = 1;
952 /* We have a new SPS */
954 msg_Dbg( p_dec, "found NAL_SPS (sps_id=%d)", i_sps_id );
957 if( p_sys->pp_sps[i_sps_id] )
958 block_Release( p_sys->pp_sps[i_sps_id] );
959 p_sys->pp_sps[i_sps_id] = p_frag;
962 static void PutPPS( decoder_t *p_dec, block_t *p_frag )
964 decoder_sys_t *p_sys = p_dec->p_sys;
969 bs_init( &s, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
970 i_pps_id = bs_read_ue( &s ); // pps id
971 i_sps_id = bs_read_ue( &s ); // sps id
972 if( i_pps_id >= PPS_MAX || i_sps_id >= SPS_MAX )
974 msg_Warn( p_dec, "invalid PPS (pps_id=%d sps_id=%d)", i_pps_id, i_sps_id );
975 block_Release( p_frag );
978 bs_skip( &s, 1 ); // entropy coding mode flag
979 p_sys->i_pic_order_present_flag = bs_read( &s, 1 );
982 /* We have a new PPS */
984 msg_Dbg( p_dec, "found NAL_PPS (pps_id=%d sps_id=%d)", i_pps_id, i_sps_id );
987 if( p_sys->pp_pps[i_pps_id] )
988 block_Release( p_sys->pp_pps[i_pps_id] );
989 p_sys->pp_pps[i_pps_id] = p_frag;
992 static void ParseSlice( decoder_t *p_dec, bool *pb_new_picture, slice_t *p_slice,
993 int i_nal_ref_idc, int i_nal_type, const block_t *p_frag )
995 decoder_sys_t *p_sys = p_dec->p_sys;
1002 /* do not convert the whole frame */
1003 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5],
1004 __MIN( p_frag->i_buffer - 5, 60 ) );
1005 bs_init( &s, pb_dec, i_dec );
1007 /* first_mb_in_slice */
1008 /* int i_first_mb = */ bs_read_ue( &s );
1011 switch( (i_slice_type = bs_read_ue( &s )) )
1014 slice.i_frame_type = BLOCK_FLAG_TYPE_P;
1017 slice.i_frame_type = BLOCK_FLAG_TYPE_B;
1020 slice.i_frame_type = BLOCK_FLAG_TYPE_I;
1022 case 3: case 8: /* SP */
1023 slice.i_frame_type = BLOCK_FLAG_TYPE_P;
1026 slice.i_frame_type = BLOCK_FLAG_TYPE_I;
1029 slice.i_frame_type = 0;
1034 slice.i_nal_type = i_nal_type;
1035 slice.i_nal_ref_idc = i_nal_ref_idc;
1037 slice.i_pic_parameter_set_id = bs_read_ue( &s );
1038 slice.i_frame_num = bs_read( &s, p_sys->i_log2_max_frame_num + 4 );
1040 slice.i_field_pic_flag = 0;
1041 slice.i_bottom_field_flag = -1;
1042 if( !p_sys->b_frame_mbs_only )
1044 /* field_pic_flag */
1045 slice.i_field_pic_flag = bs_read( &s, 1 );
1046 if( slice.i_field_pic_flag )
1047 slice.i_bottom_field_flag = bs_read( &s, 1 );
1050 slice.i_idr_pic_id = p_sys->slice.i_idr_pic_id;
1051 if( slice.i_nal_type == NAL_SLICE_IDR )
1052 slice.i_idr_pic_id = bs_read_ue( &s );
1054 slice.i_pic_order_cnt_lsb = -1;
1055 slice.i_delta_pic_order_cnt_bottom = -1;
1056 slice.i_delta_pic_order_cnt0 = 0;
1057 slice.i_delta_pic_order_cnt1 = 0;
1058 if( p_sys->i_pic_order_cnt_type == 0 )
1060 slice.i_pic_order_cnt_lsb = bs_read( &s, p_sys->i_log2_max_pic_order_cnt_lsb + 4 );
1061 if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
1062 slice.i_delta_pic_order_cnt_bottom = bs_read_se( &s );
1064 else if( (p_sys->i_pic_order_cnt_type == 1) &&
1065 (!p_sys->i_delta_pic_order_always_zero_flag) )
1067 slice.i_delta_pic_order_cnt0 = bs_read_se( &s );
1068 if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
1069 slice.i_delta_pic_order_cnt1 = bs_read_se( &s );
1073 /* Detection of the first VCL NAL unit of a primary coded picture
1074 * (cf. 7.4.1.2.4) */
1076 if( slice.i_frame_num != p_sys->slice.i_frame_num ||
1077 slice.i_pic_parameter_set_id != p_sys->slice.i_pic_parameter_set_id ||
1078 slice.i_field_pic_flag != p_sys->slice.i_field_pic_flag ||
1079 slice.i_nal_ref_idc != p_sys->slice.i_nal_ref_idc )
1081 if( (slice.i_bottom_field_flag != -1) &&
1082 (p_sys->slice.i_bottom_field_flag != -1) &&
1083 (slice.i_bottom_field_flag != p_sys->slice.i_bottom_field_flag) )
1085 if( p_sys->i_pic_order_cnt_type == 0 &&
1086 ( slice.i_pic_order_cnt_lsb != p_sys->slice.i_pic_order_cnt_lsb ||
1087 slice.i_delta_pic_order_cnt_bottom != p_sys->slice.i_delta_pic_order_cnt_bottom ) )
1089 else if( p_sys->i_pic_order_cnt_type == 1 &&
1090 ( slice.i_delta_pic_order_cnt0 != p_sys->slice.i_delta_pic_order_cnt0 ||
1091 slice.i_delta_pic_order_cnt1 != p_sys->slice.i_delta_pic_order_cnt1 ) )
1093 if( ( slice.i_nal_type == NAL_SLICE_IDR || p_sys->slice.i_nal_type == NAL_SLICE_IDR ) &&
1094 ( slice.i_nal_type != p_sys->slice.i_nal_type || slice.i_idr_pic_id != p_sys->slice.i_idr_pic_id ) )
1098 *pb_new_picture = b_pic;
1102 static void ParseSei( decoder_t *p_dec, block_t *p_frag )
1104 decoder_sys_t *p_sys = p_dec->p_sys;
1109 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
1113 /* The +1 is for rbsp trailing bits */
1114 for( int i_used = 0; i_used+1 < i_dec; )
1118 while( i_used+1 < i_dec )
1120 const int i_byte = pb_dec[i_used++];
1122 if( i_byte != 0xff )
1127 while( i_used+1 < i_dec )
1129 const int i_byte = pb_dec[i_used++];
1131 if( i_byte != 0xff )
1135 if( i_used + i_size + 1 > i_dec )
1138 /* Look for user_data_registered_itu_t_t35 */
1139 if( i_type == SEI_USER_DATA_REGISTERED )
1141 static const uint8_t p_dvb1_data_start_code[] = {
1144 0x47, 0x41, 0x39, 0x34
1146 const int i_t35 = i_size;
1147 const uint8_t *p_t35 = &pb_dec[i_used];
1149 /* Check for we have DVB1_data() */
1151 !memcmp( p_t35, p_dvb1_data_start_code, sizeof(p_dvb1_data_start_code) ) )
1153 cc_Extract( &p_sys->cc_next, true, &p_t35[3], i_t35 - 3 );
1157 /* Look for SEI recovery point */
1158 if( i_type == SEI_RECOVERY_POINT )
1161 const int i_rec = i_size;
1162 const uint8_t *p_rec = &pb_dec[i_used];
1164 bs_init( &s, p_rec, i_rec );
1165 int i_recovery_frames = bs_read_ue( &s );
1166 //bool b_exact_match = bs_read( &s, 1 );
1167 //bool b_broken_link = bs_read( &s, 1 );
1168 //int i_changing_slice_group = bs_read( &s, 2 );
1169 if( !p_sys->b_header )
1171 msg_Dbg( p_dec, "Seen SEI recovery point, %d recovery frames", i_recovery_frames );
1172 if ( p_sys->i_recovery_frames == -1 || i_recovery_frames < p_sys->i_recovery_frames )
1173 p_sys->i_recovery_frames = i_recovery_frames;