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 "../codec/h264_nal.h"
45 #include "packetizer_helper.h"
46 #include "../demux/mpeg/mpeg_parser_helpers.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
55 set_category( CAT_SOUT )
56 set_subcategory( SUBCAT_SOUT_PACKETIZER )
57 set_description( N_("H.264 video packetizer") )
58 set_capability( "packetizer", 50 )
59 set_callbacks( Open, Close )
63 /****************************************************************************
65 ****************************************************************************/
72 int i_pic_parameter_set_id;
76 int i_bottom_field_flag;
80 int i_pic_order_cnt_lsb;
81 int i_delta_pic_order_cnt_bottom;
83 int i_delta_pic_order_cnt0;
84 int i_delta_pic_order_cnt1;
92 packetizer_t packetizer;
103 block_t *pp_sps[SPS_MAX];
104 block_t *pp_pps[PPS_MAX];
105 int i_recovery_frames; /* -1 = no recovery */
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 */
143 NAL_SLICE_IDR = 5, /* ref_idc != 0 */
144 NAL_SEI = 6, /* ref_idc == 0 */
148 /* ref_idc == 0 for 6,9,10,11,12 */
151 /* Defined in H.264 annex D */
154 SEI_USER_DATA_REGISTERED = 4,
155 SEI_RECOVERY_POINT = 6
158 #define BLOCK_FLAG_PRIVATE_AUD (1 << BLOCK_FLAG_PRIVATE_SHIFT)
160 static block_t *Packetize( decoder_t *, block_t ** );
161 static block_t *PacketizeAVC1( decoder_t *, block_t ** );
162 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] );
164 static void PacketizeReset( void *p_private, bool b_broken );
165 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * );
166 static int PacketizeValidate( void *p_private, block_t * );
168 static block_t *ParseNALBlock( decoder_t *, bool *pb_ts_used, block_t * );
169 static block_t *CreateAnnexbNAL( decoder_t *, const uint8_t *p, int );
171 static block_t *OutputPicture( decoder_t *p_dec );
172 static void PutSPS( decoder_t *p_dec, block_t *p_frag );
173 static void PutPPS( decoder_t *p_dec, block_t *p_frag );
174 static void ParseSlice( decoder_t *p_dec, bool *pb_new_picture, slice_t *p_slice,
175 int i_nal_ref_idc, int i_nal_type, const block_t *p_frag );
176 static void ParseSei( decoder_t *, block_t * );
179 static const uint8_t p_h264_startcode[3] = { 0x00, 0x00, 0x01 };
181 /*****************************************************************************
182 * Open: probe the packetizer and return score
183 * When opening after demux, the packetizer is only loaded AFTER the decoder
184 * That means that what you set in fmt_out is ignored by the decoder in this special case
185 *****************************************************************************/
186 static int Open( vlc_object_t *p_this )
188 decoder_t *p_dec = (decoder_t*)p_this;
189 decoder_sys_t *p_sys;
192 if( p_dec->fmt_in.i_codec != VLC_CODEC_H264 )
194 if( p_dec->fmt_in.i_original_fourcc == VLC_FOURCC( 'a', 'v', 'c', '1') &&
195 p_dec->fmt_in.i_extra < 7 )
198 /* Allocate the memory needed to store the decoder's structure */
199 if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
204 packetizer_Init( &p_sys->packetizer,
205 p_h264_startcode, sizeof(p_h264_startcode),
206 p_h264_startcode, 1, 5,
207 PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
209 p_sys->b_slice = false;
210 p_sys->p_frame = NULL;
211 p_sys->b_frame_sps = false;
212 p_sys->b_frame_pps = false;
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;
221 p_sys->i_recovery_frames = -1;
223 p_sys->slice.i_nal_type = -1;
224 p_sys->slice.i_nal_ref_idc = -1;
225 p_sys->slice.i_idr_pic_id = -1;
226 p_sys->slice.i_frame_num = -1;
227 p_sys->slice.i_frame_type = 0;
228 p_sys->slice.i_pic_parameter_set_id = -1;
229 p_sys->slice.i_field_pic_flag = 0;
230 p_sys->slice.i_bottom_field_flag = -1;
231 p_sys->slice.i_pic_order_cnt_lsb = -1;
232 p_sys->slice.i_delta_pic_order_cnt_bottom = -1;
234 p_sys->i_frame_dts = VLC_TS_INVALID;
235 p_sys->i_frame_pts = VLC_TS_INVALID;
237 /* Setup properties */
238 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
239 p_dec->fmt_out.i_codec = VLC_CODEC_H264;
241 if( p_dec->fmt_in.i_original_fourcc == VLC_FOURCC( 'a', 'v', 'c', '1' ) )
243 /* This type of stream is produced by mp4 and matroska
244 * when we want to store it in another streamformat, you need to convert
245 * The fmt_in.p_extra should ALWAYS contain the avcC
246 * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */
247 uint8_t *p = &((uint8_t*)p_dec->fmt_in.p_extra)[4];
253 p_sys->i_avcC_length_size = 1 + ((*p++)&0x03);
257 for( i = 0; i < i_sps; i++ )
259 uint16_t i_length = GetWBE( p ); p += 2;
261 (uint8_t*)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra - p )
265 block_t *p_sps = CreateAnnexbNAL( p_dec, p, i_length );
268 ParseNALBlock( p_dec, &b_dummy, p_sps );
273 for( i = 0; i < i_pps; i++ )
275 uint16_t i_length = GetWBE( p ); p += 2;
277 (uint8_t*)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra - p )
281 block_t *p_pps = CreateAnnexbNAL( p_dec, p, i_length );
284 ParseNALBlock( p_dec, &b_dummy, p_pps );
287 msg_Dbg( p_dec, "avcC length size=%d, sps=%d, pps=%d",
288 p_sys->i_avcC_length_size, i_sps, i_pps );
290 if( !p_sys->b_sps || !p_sys->b_pps )
293 /* FIXME: FFMPEG isn't happy at all if you leave this */
294 if( p_dec->fmt_out.i_extra > 0 )
295 free( p_dec->fmt_out.p_extra );
296 p_dec->fmt_out.i_extra = 0;
297 p_dec->fmt_out.p_extra = NULL;
299 /* Set the new extradata */
300 for( i = 0; i < SPS_MAX; i++ )
302 if( p_sys->pp_sps[i] )
303 p_dec->fmt_out.i_extra += p_sys->pp_sps[i]->i_buffer;
305 for( i = 0; i < PPS_MAX; i++ )
307 if( p_sys->pp_pps[i] )
308 p_dec->fmt_out.i_extra += p_sys->pp_pps[i]->i_buffer;
310 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_out.i_extra );
311 if( p_dec->fmt_out.p_extra )
313 uint8_t *p_dst = p_dec->fmt_out.p_extra;
315 for( i = 0; i < SPS_MAX; i++ )
317 if( p_sys->pp_sps[i] )
319 memcpy( p_dst, p_sys->pp_sps[i]->p_buffer, p_sys->pp_sps[i]->i_buffer );
320 p_dst += p_sys->pp_sps[i]->i_buffer;
323 for( i = 0; i < PPS_MAX; i++ )
325 if( p_sys->pp_pps[i] )
327 memcpy( p_dst, p_sys->pp_pps[i]->p_buffer, p_sys->pp_pps[i]->i_buffer );
328 p_dst += p_sys->pp_pps[i]->i_buffer;
331 p_sys->b_header = true;
335 p_dec->fmt_out.i_extra = 0;
339 p_dec->pf_packetize = PacketizeAVC1;
344 /* This type of stream contains data with 3 of 4 byte startcodes
345 * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes
346 * The fmt_out.p_extra should be the same */
349 p_dec->pf_packetize = Packetize;
350 p_dec->pf_get_cc = GetCc;
353 p_sys->i_cc_pts = VLC_TS_INVALID;
354 p_sys->i_cc_dts = VLC_TS_INVALID;
355 p_sys->i_cc_flags = 0;
356 cc_Init( &p_sys->cc );
357 cc_Init( &p_sys->cc_next );
360 if( p_dec->fmt_in.i_extra > 0 )
361 packetizer_Header( &p_sys->packetizer,
362 p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
368 /*****************************************************************************
369 * Close: clean up the packetizer
370 *****************************************************************************/
371 static void Close( vlc_object_t *p_this )
373 decoder_t *p_dec = (decoder_t*)p_this;
374 decoder_sys_t *p_sys = p_dec->p_sys;
378 block_ChainRelease( p_sys->p_frame );
379 for( i = 0; i < SPS_MAX; i++ )
381 if( p_sys->pp_sps[i] )
382 block_Release( p_sys->pp_sps[i] );
384 for( i = 0; i < PPS_MAX; i++ )
386 if( p_sys->pp_pps[i] )
387 block_Release( p_sys->pp_pps[i] );
389 packetizer_Clean( &p_sys->packetizer );
391 if( p_dec->pf_get_cc )
393 cc_Exit( &p_sys->cc_next );
394 cc_Exit( &p_sys->cc );
400 /****************************************************************************
401 * Packetize: the whole thing
402 * Search for the startcodes 3 or more bytes
403 * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs
404 ****************************************************************************/
405 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
407 decoder_sys_t *p_sys = p_dec->p_sys;
409 return packetizer_Packetize( &p_sys->packetizer, pp_block );
412 /****************************************************************************
413 * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream
414 * Will always use 4 byte 0 0 0 1 startcodes
415 * Will prepend a SPS and PPS before each keyframe
416 ****************************************************************************/
417 static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
419 decoder_sys_t *p_sys = p_dec->p_sys;
421 block_t *p_ret = NULL;
424 if( !pp_block || !*pp_block )
426 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
428 block_Release( *pp_block );
435 for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; )
442 for( i = 0; i < p_sys->i_avcC_length_size; i++ )
444 i_size = (i_size << 8) | (*p++);
448 i_size > ( p_block->p_buffer + p_block->i_buffer - p ) )
450 msg_Err( p_dec, "Broken frame : size %d is too big", i_size );
454 block_t *p_part = CreateAnnexbNAL( p_dec, p, i_size );
458 p_part->i_dts = p_block->i_dts;
459 p_part->i_pts = p_block->i_pts;
462 if( ( p_pic = ParseNALBlock( p_dec, &b_dummy, p_part ) ) )
464 block_ChainAppend( &p_ret, p_pic );
468 block_Release( p_block );
473 /*****************************************************************************
475 *****************************************************************************/
476 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
478 decoder_sys_t *p_sys = p_dec->p_sys;
481 for( int i = 0; i < 4; i++ )
482 pb_present[i] = p_sys->cc.pb_present[i];
484 if( p_sys->cc.i_data <= 0 )
487 p_cc = block_Alloc( p_sys->cc.i_data);
490 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
492 p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
493 p_cc->i_flags = ( p_sys->cc.b_reorder ? p_sys->i_cc_flags : BLOCK_FLAG_TYPE_P ) & BLOCK_FLAG_TYPE_MASK;
495 cc_Flush( &p_sys->cc );
499 /****************************************************************************
501 ****************************************************************************/
502 static void PacketizeReset( void *p_private, bool b_broken )
504 decoder_t *p_dec = p_private;
505 decoder_sys_t *p_sys = p_dec->p_sys;
510 block_ChainRelease( p_sys->p_frame );
511 p_sys->p_frame = NULL;
512 p_sys->b_frame_sps = false;
513 p_sys->b_frame_pps = false;
514 p_sys->slice.i_frame_type = 0;
515 p_sys->b_slice = false;
517 p_sys->i_frame_pts = VLC_TS_INVALID;
518 p_sys->i_frame_dts = VLC_TS_INVALID;
520 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
522 decoder_t *p_dec = p_private;
524 /* Remove trailing 0 bytes */
525 while( p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
528 return ParseNALBlock( p_dec, pb_ts_used, p_block );
530 static int PacketizeValidate( void *p_private, block_t *p_au )
532 VLC_UNUSED(p_private);
537 static block_t *CreateAnnexbNAL( decoder_t *p_dec, const uint8_t *p, int i_size )
541 p_nal = block_Alloc( 4 + i_size );
542 if( !p_nal ) return NULL;
545 p_nal->p_buffer[0] = 0x00;
546 p_nal->p_buffer[1] = 0x00;
547 p_nal->p_buffer[2] = 0x00;
548 p_nal->p_buffer[3] = 0x01;
551 memcpy( &p_nal->p_buffer[4], p, i_size );
557 static void CreateDecodedNAL( uint8_t **pp_ret, int *pi_ret,
558 const uint8_t *src, int i_src )
560 uint8_t *dst = malloc( i_src );
565 *pi_ret = nal_decode(src, dst, i_src);
568 /*****************************************************************************
569 * ParseNALBlock: parses annexB type NALs
570 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
571 *****************************************************************************/
572 static block_t *ParseNALBlock( decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag )
574 decoder_sys_t *p_sys = p_dec->p_sys;
575 block_t *p_pic = NULL;
577 const int i_nal_ref_idc = (p_frag->p_buffer[4] >> 5)&0x03;
578 const int i_nal_type = p_frag->p_buffer[4]&0x1f;
579 const mtime_t i_frag_dts = p_frag->i_dts;
580 const mtime_t i_frag_pts = p_frag->i_pts;
582 if( p_sys->b_slice && ( !p_sys->b_sps || !p_sys->b_pps ) )
584 block_ChainRelease( p_sys->p_frame );
585 msg_Warn( p_dec, "waiting for SPS/PPS" );
588 p_sys->slice.i_frame_type = 0;
589 p_sys->p_frame = NULL;
590 p_sys->b_frame_sps = false;
591 p_sys->b_frame_pps = false;
592 p_sys->b_slice = false;
593 cc_Flush( &p_sys->cc_next );
596 if( ( !p_sys->b_sps || !p_sys->b_pps ) &&
597 i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
599 p_sys->b_slice = true;
600 /* Fragment will be discarded later on */
602 else if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
607 ParseSlice( p_dec, &b_new_picture, &slice, i_nal_ref_idc, i_nal_type, p_frag );
610 if( b_new_picture && p_sys->b_slice )
611 p_pic = OutputPicture( p_dec );
614 p_sys->slice = slice;
615 p_sys->b_slice = true;
617 else if( i_nal_type == NAL_SPS )
620 p_pic = OutputPicture( p_dec );
621 p_sys->b_frame_sps = true;
623 PutSPS( p_dec, p_frag );
625 /* Do not append the SPS because we will insert it on keyframes */
628 else if( i_nal_type == NAL_PPS )
631 p_pic = OutputPicture( p_dec );
632 p_sys->b_frame_pps = true;
634 PutPPS( p_dec, p_frag );
636 /* Do not append the PPS because we will insert it on keyframes */
639 else if( i_nal_type == NAL_AU_DELIMITER ||
640 i_nal_type == NAL_SEI ||
641 ( i_nal_type >= 13 && i_nal_type <= 18 ) )
644 p_pic = OutputPicture( p_dec );
646 /* Parse SEI for CC support */
647 if( i_nal_type == NAL_SEI )
649 ParseSei( p_dec, p_frag );
651 else if( i_nal_type == NAL_AU_DELIMITER )
653 if( p_sys->p_frame && (p_sys->p_frame->i_flags & BLOCK_FLAG_PRIVATE_AUD) )
655 block_Release( p_frag );
660 p_frag->i_flags |= BLOCK_FLAG_PRIVATE_AUD;
665 /* Append the block */
667 block_ChainAppend( &p_sys->p_frame, p_frag );
670 if( p_sys->i_frame_dts <= VLC_TS_INVALID &&
671 p_sys->i_frame_pts <= VLC_TS_INVALID )
673 p_sys->i_frame_dts = i_frag_dts;
674 p_sys->i_frame_pts = i_frag_pts;
680 static block_t *OutputPicture( decoder_t *p_dec )
682 decoder_sys_t *p_sys = p_dec->p_sys;
685 if ( !p_sys->b_header && p_sys->i_recovery_frames != -1 )
687 if( p_sys->i_recovery_frames == 0 )
689 msg_Dbg( p_dec, "Recovery from SEI recovery point complete" );
690 p_sys->b_header = true;
692 --p_sys->i_recovery_frames;
695 if( !p_sys->b_header && p_sys->i_recovery_frames == -1 &&
696 p_sys->slice.i_frame_type != BLOCK_FLAG_TYPE_I)
699 const bool b_sps_pps_i = p_sys->slice.i_frame_type == BLOCK_FLAG_TYPE_I &&
702 if( b_sps_pps_i || p_sys->b_frame_sps || p_sys->b_frame_pps )
704 block_t *p_head = NULL;
705 if( p_sys->p_frame->i_flags & BLOCK_FLAG_PRIVATE_AUD )
707 p_head = p_sys->p_frame;
708 p_sys->p_frame = p_sys->p_frame->p_next;
711 block_t *p_list = NULL;
712 for( int i = 0; i < SPS_MAX && (b_sps_pps_i || p_sys->b_frame_sps); i++ )
714 if( p_sys->pp_sps[i] )
715 block_ChainAppend( &p_list, block_Duplicate( p_sys->pp_sps[i] ) );
717 for( int i = 0; i < PPS_MAX && (b_sps_pps_i || p_sys->b_frame_pps); i++ )
719 if( p_sys->pp_pps[i] )
720 block_ChainAppend( &p_list, block_Duplicate( p_sys->pp_pps[i] ) );
722 if( b_sps_pps_i && p_list )
723 p_sys->b_header = true;
726 p_head->p_next = p_list;
729 block_ChainAppend( &p_head, p_sys->p_frame );
731 p_pic = block_ChainGather( p_head );
735 p_pic = block_ChainGather( p_sys->p_frame );
737 p_pic->i_dts = p_sys->i_frame_dts;
738 p_pic->i_pts = p_sys->i_frame_pts;
739 p_pic->i_length = 0; /* FIXME */
740 p_pic->i_flags |= p_sys->slice.i_frame_type;
741 p_pic->i_flags &= ~BLOCK_FLAG_PRIVATE_AUD;
742 if( !p_sys->b_header )
743 p_pic->i_flags |= BLOCK_FLAG_PREROLL;
745 p_sys->slice.i_frame_type = 0;
746 p_sys->p_frame = NULL;
747 p_sys->i_frame_dts = VLC_TS_INVALID;
748 p_sys->i_frame_pts = VLC_TS_INVALID;
749 p_sys->b_frame_sps = false;
750 p_sys->b_frame_pps = false;
751 p_sys->b_slice = false;
754 p_sys->i_cc_pts = p_pic->i_pts;
755 p_sys->i_cc_dts = p_pic->i_dts;
756 p_sys->i_cc_flags = p_pic->i_flags;
758 p_sys->cc = p_sys->cc_next;
759 cc_Flush( &p_sys->cc_next );
764 static void PutSPS( decoder_t *p_dec, block_t *p_frag )
766 decoder_sys_t *p_sys = p_dec->p_sys;
768 uint8_t *pb_dec = NULL;
774 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5],
775 p_frag->i_buffer - 5 );
777 bs_init( &s, pb_dec, i_dec );
778 int i_profile_idc = bs_read( &s, 8 );
779 p_dec->fmt_out.i_profile = i_profile_idc;
780 /* Skip constraint_set0123, reserved(4) */
781 bs_skip( &s, 1+1+1+1 + 4 );
782 p_dec->fmt_out.i_level = bs_read( &s, 8 );
784 i_sps_id = bs_read_ue( &s );
785 if( i_sps_id >= SPS_MAX || i_sps_id < 0 )
787 msg_Warn( p_dec, "invalid SPS (sps_id=%d)", i_sps_id );
789 block_Release( p_frag );
793 if( i_profile_idc == PROFILE_H264_HIGH || i_profile_idc == PROFILE_H264_HIGH_10 ||
794 i_profile_idc == PROFILE_H264_HIGH_422 || i_profile_idc == PROFILE_H264_HIGH_444_PREDICTIVE ||
795 i_profile_idc == PROFILE_H264_CAVLC_INTRA || i_profile_idc == PROFILE_H264_SVC_BASELINE ||
796 i_profile_idc == PROFILE_H264_SVC_HIGH )
798 /* chroma_format_idc */
799 const int i_chroma_format_idc = bs_read_ue( &s );
800 if( i_chroma_format_idc == 3 )
801 bs_skip( &s, 1 ); /* separate_colour_plane_flag */
802 /* bit_depth_luma_minus8 */
804 /* bit_depth_chroma_minus8 */
806 /* qpprime_y_zero_transform_bypass_flag */
808 /* seq_scaling_matrix_present_flag */
809 i_tmp = bs_read( &s, 1 );
812 for( int i = 0; i < ((3 != i_chroma_format_idc) ? 8 : 12); i++ )
814 /* seq_scaling_list_present_flag[i] */
815 i_tmp = bs_read( &s, 1 );
818 const int i_size_of_scaling_list = (i < 6 ) ? 16 : 64;
819 /* scaling_list (...) */
822 for( int j = 0; j < i_size_of_scaling_list; j++ )
824 if( i_nextscale != 0 )
827 i_tmp = bs_read_se( &s );
828 i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
829 /* useDefaultScalingMatrixFlag = ... */
832 i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
838 /* Skip i_log2_max_frame_num */
839 p_sys->i_log2_max_frame_num = bs_read_ue( &s );
840 if( p_sys->i_log2_max_frame_num > 12)
841 p_sys->i_log2_max_frame_num = 12;
843 p_sys->i_pic_order_cnt_type = bs_read_ue( &s );
844 if( p_sys->i_pic_order_cnt_type == 0 )
846 /* skip i_log2_max_poc_lsb */
847 p_sys->i_log2_max_pic_order_cnt_lsb = bs_read_ue( &s );
848 if( p_sys->i_log2_max_pic_order_cnt_lsb > 12 )
849 p_sys->i_log2_max_pic_order_cnt_lsb = 12;
851 else if( p_sys->i_pic_order_cnt_type == 1 )
854 /* skip b_delta_pic_order_always_zero */
855 p_sys->i_delta_pic_order_always_zero_flag = bs_read( &s, 1 );
856 /* skip i_offset_for_non_ref_pic */
858 /* skip i_offset_for_top_to_bottom_field */
860 /* read i_num_ref_frames_in_poc_cycle */
861 i_cycle = bs_read_ue( &s );
862 if( i_cycle > 256 ) i_cycle = 256;
865 /* skip i_offset_for_ref_frame */
870 /* i_num_ref_frames */
872 /* b_gaps_in_frame_num_value_allowed */
876 p_dec->fmt_out.video.i_width = 16 * ( bs_read_ue( &s ) + 1 );
877 p_dec->fmt_out.video.i_height = 16 * ( bs_read_ue( &s ) + 1 );
879 /* b_frame_mbs_only */
880 p_sys->b_frame_mbs_only = bs_read( &s, 1 );
881 p_dec->fmt_out.video.i_height *= ( 2 - p_sys->b_frame_mbs_only );
882 if( p_sys->b_frame_mbs_only == 0 )
886 /* b_direct8x8_inference */
890 i_tmp = bs_read( &s, 1 );
904 i_tmp = bs_read( &s, 1 );
907 /* read the aspect ratio part if any */
908 i_tmp = bs_read( &s, 1 );
911 static const struct { int w, h; } sar[17] =
913 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 },
914 { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
915 { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
916 { 64, 33 }, { 160,99 }, { 4, 3 }, { 3, 2 },
919 int i_sar = bs_read( &s, 8 );
927 else if( i_sar == 255 )
929 w = bs_read( &s, 16 );
930 h = bs_read( &s, 16 );
938 if( w != 0 && h != 0 )
940 p_dec->fmt_out.video.i_sar_num = w;
941 p_dec->fmt_out.video.i_sar_den = h;
945 p_dec->fmt_out.video.i_sar_num = 1;
946 p_dec->fmt_out.video.i_sar_den = 1;
953 /* We have a new SPS */
955 msg_Dbg( p_dec, "found NAL_SPS (sps_id=%d)", i_sps_id );
958 if( p_sys->pp_sps[i_sps_id] )
959 block_Release( p_sys->pp_sps[i_sps_id] );
960 p_sys->pp_sps[i_sps_id] = p_frag;
963 static void PutPPS( decoder_t *p_dec, block_t *p_frag )
965 decoder_sys_t *p_sys = p_dec->p_sys;
970 bs_init( &s, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
971 i_pps_id = bs_read_ue( &s ); // pps id
972 i_sps_id = bs_read_ue( &s ); // sps id
973 if( i_pps_id >= PPS_MAX || i_sps_id >= SPS_MAX )
975 msg_Warn( p_dec, "invalid PPS (pps_id=%d sps_id=%d)", i_pps_id, i_sps_id );
976 block_Release( p_frag );
979 bs_skip( &s, 1 ); // entropy coding mode flag
980 p_sys->i_pic_order_present_flag = bs_read( &s, 1 );
983 /* We have a new PPS */
985 msg_Dbg( p_dec, "found NAL_PPS (pps_id=%d sps_id=%d)", i_pps_id, i_sps_id );
988 if( p_sys->pp_pps[i_pps_id] )
989 block_Release( p_sys->pp_pps[i_pps_id] );
990 p_sys->pp_pps[i_pps_id] = p_frag;
993 static void ParseSlice( decoder_t *p_dec, bool *pb_new_picture, slice_t *p_slice,
994 int i_nal_ref_idc, int i_nal_type, const block_t *p_frag )
996 decoder_sys_t *p_sys = p_dec->p_sys;
1003 /* do not convert the whole frame */
1004 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5],
1005 __MIN( p_frag->i_buffer - 5, 60 ) );
1006 bs_init( &s, pb_dec, i_dec );
1008 /* first_mb_in_slice */
1009 /* int i_first_mb = */ bs_read_ue( &s );
1012 switch( (i_slice_type = bs_read_ue( &s )) )
1015 slice.i_frame_type = BLOCK_FLAG_TYPE_P;
1018 slice.i_frame_type = BLOCK_FLAG_TYPE_B;
1021 slice.i_frame_type = BLOCK_FLAG_TYPE_I;
1023 case 3: case 8: /* SP */
1024 slice.i_frame_type = BLOCK_FLAG_TYPE_P;
1027 slice.i_frame_type = BLOCK_FLAG_TYPE_I;
1030 slice.i_frame_type = 0;
1035 slice.i_nal_type = i_nal_type;
1036 slice.i_nal_ref_idc = i_nal_ref_idc;
1038 slice.i_pic_parameter_set_id = bs_read_ue( &s );
1039 slice.i_frame_num = bs_read( &s, p_sys->i_log2_max_frame_num + 4 );
1041 slice.i_field_pic_flag = 0;
1042 slice.i_bottom_field_flag = -1;
1043 if( !p_sys->b_frame_mbs_only )
1045 /* field_pic_flag */
1046 slice.i_field_pic_flag = bs_read( &s, 1 );
1047 if( slice.i_field_pic_flag )
1048 slice.i_bottom_field_flag = bs_read( &s, 1 );
1051 slice.i_idr_pic_id = p_sys->slice.i_idr_pic_id;
1052 if( slice.i_nal_type == NAL_SLICE_IDR )
1053 slice.i_idr_pic_id = bs_read_ue( &s );
1055 slice.i_pic_order_cnt_lsb = -1;
1056 slice.i_delta_pic_order_cnt_bottom = -1;
1057 slice.i_delta_pic_order_cnt0 = 0;
1058 slice.i_delta_pic_order_cnt1 = 0;
1059 if( p_sys->i_pic_order_cnt_type == 0 )
1061 slice.i_pic_order_cnt_lsb = bs_read( &s, p_sys->i_log2_max_pic_order_cnt_lsb + 4 );
1062 if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
1063 slice.i_delta_pic_order_cnt_bottom = bs_read_se( &s );
1065 else if( (p_sys->i_pic_order_cnt_type == 1) &&
1066 (!p_sys->i_delta_pic_order_always_zero_flag) )
1068 slice.i_delta_pic_order_cnt0 = bs_read_se( &s );
1069 if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
1070 slice.i_delta_pic_order_cnt1 = bs_read_se( &s );
1074 /* Detection of the first VCL NAL unit of a primary coded picture
1075 * (cf. 7.4.1.2.4) */
1077 if( slice.i_frame_num != p_sys->slice.i_frame_num ||
1078 slice.i_pic_parameter_set_id != p_sys->slice.i_pic_parameter_set_id ||
1079 slice.i_field_pic_flag != p_sys->slice.i_field_pic_flag ||
1080 slice.i_nal_ref_idc != p_sys->slice.i_nal_ref_idc )
1082 if( (slice.i_bottom_field_flag != -1) &&
1083 (p_sys->slice.i_bottom_field_flag != -1) &&
1084 (slice.i_bottom_field_flag != p_sys->slice.i_bottom_field_flag) )
1086 if( p_sys->i_pic_order_cnt_type == 0 &&
1087 ( slice.i_pic_order_cnt_lsb != p_sys->slice.i_pic_order_cnt_lsb ||
1088 slice.i_delta_pic_order_cnt_bottom != p_sys->slice.i_delta_pic_order_cnt_bottom ) )
1090 else if( p_sys->i_pic_order_cnt_type == 1 &&
1091 ( slice.i_delta_pic_order_cnt0 != p_sys->slice.i_delta_pic_order_cnt0 ||
1092 slice.i_delta_pic_order_cnt1 != p_sys->slice.i_delta_pic_order_cnt1 ) )
1094 if( ( slice.i_nal_type == NAL_SLICE_IDR || p_sys->slice.i_nal_type == NAL_SLICE_IDR ) &&
1095 ( slice.i_nal_type != p_sys->slice.i_nal_type || slice.i_idr_pic_id != p_sys->slice.i_idr_pic_id ) )
1099 *pb_new_picture = b_pic;
1103 static void ParseSei( decoder_t *p_dec, block_t *p_frag )
1105 decoder_sys_t *p_sys = p_dec->p_sys;
1110 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
1114 /* The +1 is for rbsp trailing bits */
1115 for( int i_used = 0; i_used+1 < i_dec; )
1119 while( i_used+1 < i_dec )
1121 const int i_byte = pb_dec[i_used++];
1123 if( i_byte != 0xff )
1128 while( i_used+1 < i_dec )
1130 const int i_byte = pb_dec[i_used++];
1132 if( i_byte != 0xff )
1136 if( i_used + i_size + 1 > i_dec )
1139 /* Look for user_data_registered_itu_t_t35 */
1140 if( i_type == SEI_USER_DATA_REGISTERED )
1142 static const uint8_t p_dvb1_data_start_code[] = {
1145 0x47, 0x41, 0x39, 0x34
1147 const int i_t35 = i_size;
1148 const uint8_t *p_t35 = &pb_dec[i_used];
1150 /* Check for we have DVB1_data() */
1152 !memcmp( p_t35, p_dvb1_data_start_code, sizeof(p_dvb1_data_start_code) ) )
1154 cc_Extract( &p_sys->cc_next, true, &p_t35[3], i_t35 - 3 );
1158 /* Look for SEI recovery point */
1159 if( i_type == SEI_RECOVERY_POINT )
1162 const int i_rec = i_size;
1163 const uint8_t *p_rec = &pb_dec[i_used];
1165 bs_init( &s, p_rec, i_rec );
1166 int i_recovery_frames = bs_read_ue( &s );
1167 //bool b_exact_match = bs_read( &s, 1 );
1168 //bool b_broken_link = bs_read( &s, 1 );
1169 //int i_changing_slice_group = bs_read( &s, 2 );
1170 if( !p_sys->b_header )
1172 msg_Dbg( p_dec, "Seen SEI recovery point, %d recovery frames", i_recovery_frames );
1173 if ( p_sys->i_recovery_frames == -1 || i_recovery_frames < p_sys->i_recovery_frames )
1174 p_sys->i_recovery_frames = i_recovery_frames;