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"
44 #include "packetizer_helper.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
53 set_category( CAT_SOUT )
54 set_subcategory( SUBCAT_SOUT_PACKETIZER )
55 set_description( N_("H.264 video packetizer") )
56 set_capability( "packetizer", 50 )
57 set_callbacks( Open, Close )
61 /****************************************************************************
63 ****************************************************************************/
70 int i_pic_parameter_set_id;
74 int i_bottom_field_flag;
78 int i_pic_order_cnt_lsb;
79 int i_delta_pic_order_cnt_bottom;
81 int i_delta_pic_order_cnt0;
82 int i_delta_pic_order_cnt1;
90 packetizer_t packetizer;
99 block_t *pp_sps[SPS_MAX];
100 block_t *pp_pps[PPS_MAX];
103 int i_avcC_length_size;
105 /* Useful values of the Sequence Parameter Set */
106 int i_log2_max_frame_num;
107 int b_frame_mbs_only;
108 int i_pic_order_cnt_type;
109 int i_delta_pic_order_always_zero_flag;
110 int i_log2_max_pic_order_cnt_lsb;
112 /* Value from Picture Parameter Set */
113 int i_pic_order_present_flag;
115 /* Useful values of the Slice Header */
138 NAL_SLICE_IDR = 5, /* ref_idc != 0 */
139 NAL_SEI = 6, /* ref_idc == 0 */
143 /* ref_idc == 0 for 6,9,10,11,12 */
148 NAL_PRIORITY_DISPOSABLE = 0,
149 NAL_PRIORITY_LOW = 1,
150 NAL_PRIORITY_HIGH = 2,
151 NAL_PRIORITY_HIGHEST = 3,
154 static block_t *Packetize( decoder_t *, block_t ** );
155 static block_t *PacketizeAVC1( decoder_t *, block_t ** );
156 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] );
158 static void PacketizeReset( void *p_private, bool b_broken );
159 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * );
160 static int PacketizeValidate( void *p_private, block_t * );
162 static block_t *ParseNALBlock( decoder_t *, bool *pb_used_ts, block_t * );
163 static block_t *CreateAnnexbNAL( decoder_t *, const uint8_t *p, int );
165 static block_t *OutputPicture( decoder_t *p_dec );
166 static void PutSPS( decoder_t *p_dec, block_t *p_frag );
167 static void PutPPS( decoder_t *p_dec, block_t *p_frag );
168 static void ParseSlice( decoder_t *p_dec, bool *pb_new_picture, slice_t *p_slice,
169 int i_nal_ref_idc, int i_nal_type, const block_t *p_frag );
170 static void ParseSei( decoder_t *, block_t * );
173 static const uint8_t p_h264_startcode[3] = { 0x00, 0x00, 0x01 };
175 /*****************************************************************************
176 * Open: probe the packetizer and return score
177 * When opening after demux, the packetizer is only loaded AFTER the decoder
178 * That means that what you set in fmt_out is ignored by the decoder in this special case
179 *****************************************************************************/
180 static int Open( vlc_object_t *p_this )
182 decoder_t *p_dec = (decoder_t*)p_this;
183 decoder_sys_t *p_sys;
186 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'h', '2', '6', '4') &&
187 p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') &&
188 p_dec->fmt_in.i_codec != VLC_FOURCC( 'V', 'S', 'S', 'H') &&
189 p_dec->fmt_in.i_codec != VLC_FOURCC( 'v', 's', 's', 'h') &&
190 p_dec->fmt_in.i_codec != VLC_FOURCC( 'D', 'A', 'V', 'C') &&
191 p_dec->fmt_in.i_codec != VLC_FOURCC( 'x', '2', '6', '4') &&
192 p_dec->fmt_in.i_codec != VLC_FOURCC( 'X', '2', '6', '4') &&
193 ( p_dec->fmt_in.i_codec != VLC_FOURCC( 'a', 'v', 'c', '1') ||
194 p_dec->fmt_in.i_extra < 7 ) )
199 /* Allocate the memory needed to store the decoder's structure */
200 if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
205 packetizer_Init( &p_sys->packetizer,
206 p_h264_startcode, sizeof(p_h264_startcode),
208 PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
210 p_sys->b_slice = false;
211 p_sys->p_frame = NULL;
212 p_sys->b_header= false;
213 p_sys->b_sps = false;
214 p_sys->b_pps = false;
215 for( i = 0; i < SPS_MAX; i++ )
216 p_sys->pp_sps[i] = NULL;
217 for( i = 0; i < PPS_MAX; i++ )
218 p_sys->pp_pps[i] = NULL;
220 p_sys->slice.i_nal_type = -1;
221 p_sys->slice.i_nal_ref_idc = -1;
222 p_sys->slice.i_idr_pic_id = -1;
223 p_sys->slice.i_frame_num = -1;
224 p_sys->slice.i_frame_type = 0;
225 p_sys->slice.i_pic_parameter_set_id = -1;
226 p_sys->slice.i_field_pic_flag = 0;
227 p_sys->slice.i_bottom_field_flag = -1;
228 p_sys->slice.i_pic_order_cnt_lsb = -1;
229 p_sys->slice.i_delta_pic_order_cnt_bottom = -1;
231 p_sys->i_frame_dts = -1;
232 p_sys->i_frame_pts = -1;
234 /* Setup properties */
235 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
236 p_dec->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
238 if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'a', 'v', 'c', '1' ) )
240 /* This type of stream is produced by mp4 and matroska
241 * when we want to store it in another streamformat, you need to convert
242 * The fmt_in.p_extra should ALWAYS contain the avcC
243 * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */
244 uint8_t *p = &((uint8_t*)p_dec->fmt_in.p_extra)[4];
250 p_sys->i_avcC_length_size = 1 + ((*p++)&0x03);
254 for( i = 0; i < i_sps; i++ )
256 uint16_t i_length = GetWBE( p ); p += 2;
258 (uint8_t*)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra - p )
262 block_t *p_sps = CreateAnnexbNAL( p_dec, p, i_length );
265 ParseNALBlock( p_dec, &b_dummy, p_sps );
270 for( i = 0; i < i_pps; i++ )
272 uint16_t i_length = GetWBE( p ); p += 2;
274 (uint8_t*)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra - p )
278 block_t *p_pps = CreateAnnexbNAL( p_dec, p, i_length );
281 ParseNALBlock( p_dec, &b_dummy, p_pps );
284 msg_Dbg( p_dec, "avcC length size=%d, sps=%d, pps=%d",
285 p_sys->i_avcC_length_size, i_sps, i_pps );
287 if( !p_sys->b_sps || !p_sys->b_pps )
290 /* FIXME: FFMPEG isn't happy at all if you leave this */
291 if( p_dec->fmt_out.i_extra > 0 )
292 free( p_dec->fmt_out.p_extra );
293 p_dec->fmt_out.i_extra = 0;
294 p_dec->fmt_out.p_extra = NULL;
296 /* Set the new extradata */
297 for( i = 0; i < SPS_MAX; i++ )
299 if( p_sys->pp_sps[i] )
300 p_dec->fmt_out.i_extra += p_sys->pp_sps[i]->i_buffer;
302 for( i = 0; i < PPS_MAX; i++ )
304 if( p_sys->pp_pps[i] )
305 p_dec->fmt_out.i_extra += p_sys->pp_pps[i]->i_buffer;
307 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_out.i_extra );
308 if( p_dec->fmt_out.p_extra )
310 uint8_t *p_dst = p_dec->fmt_out.p_extra;
312 for( i = 0; i < SPS_MAX; i++ )
314 if( p_sys->pp_sps[i] )
316 memcpy( p_dst, p_sys->pp_sps[i]->p_buffer, p_sys->pp_sps[i]->i_buffer );
317 p_dst += p_sys->pp_sps[i]->i_buffer;
320 for( i = 0; i < PPS_MAX; i++ )
322 if( p_sys->pp_pps[i] )
324 memcpy( p_dst, p_sys->pp_pps[i]->p_buffer, p_sys->pp_pps[i]->i_buffer );
325 p_dst += p_sys->pp_pps[i]->i_buffer;
328 p_sys->b_header = true;
332 p_dec->fmt_out.i_extra = 0;
336 p_dec->pf_packetize = PacketizeAVC1;
341 /* This type of stream contains data with 3 of 4 byte startcodes
342 * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes
343 * The fmt_out.p_extra should be the same */
346 p_dec->pf_packetize = Packetize;
347 p_dec->pf_get_cc = GetCc;
352 p_sys->i_cc_flags = 0;
353 cc_Init( &p_sys->cc );
354 cc_Init( &p_sys->cc_next );
357 if( p_dec->fmt_in.i_extra > 0 )
359 block_t *p_init = block_New( p_dec, p_dec->fmt_in.i_extra );
362 memcpy( p_init->p_buffer, p_dec->fmt_in.p_extra,
363 p_dec->fmt_in.i_extra );
365 while( ( p_pic = Packetize( p_dec, &p_init ) ) )
367 /* Should not occur because we should only receive SPS/PPS */
368 block_Release( p_pic );
376 /*****************************************************************************
377 * Close: clean up the packetizer
378 *****************************************************************************/
379 static void Close( vlc_object_t *p_this )
381 decoder_t *p_dec = (decoder_t*)p_this;
382 decoder_sys_t *p_sys = p_dec->p_sys;
386 block_ChainRelease( p_sys->p_frame );
387 for( i = 0; i < SPS_MAX; i++ )
389 if( p_sys->pp_sps[i] )
390 block_Release( p_sys->pp_sps[i] );
392 for( i = 0; i < PPS_MAX; i++ )
394 if( p_sys->pp_pps[i] )
395 block_Release( p_sys->pp_pps[i] );
397 packetizer_Clean( &p_sys->packetizer );
399 if( p_dec->pf_get_cc )
401 cc_Exit( &p_sys->cc_next );
402 cc_Exit( &p_sys->cc );
408 /****************************************************************************
409 * Packetize: the whole thing
410 * Search for the startcodes 3 or more bytes
411 * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs
412 ****************************************************************************/
413 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
415 decoder_sys_t *p_sys = p_dec->p_sys;
417 return packetizer_Packetize( &p_sys->packetizer, pp_block );
420 /****************************************************************************
421 * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream
422 * Will always use 4 byte 0 0 0 1 startcodes
423 * Will prepend a SPS and PPS before each keyframe
424 ****************************************************************************/
425 static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
427 decoder_sys_t *p_sys = p_dec->p_sys;
429 block_t *p_ret = NULL;
432 if( !pp_block || !*pp_block )
434 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
436 block_Release( *pp_block );
443 for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; )
450 for( i = 0; i < p_sys->i_avcC_length_size; i++ )
452 i_size = (i_size << 8) | (*p++);
456 i_size > ( p_block->p_buffer + p_block->i_buffer - p ) )
458 msg_Err( p_dec, "Broken frame : size %d is too big", i_size );
462 block_t *p_part = CreateAnnexbNAL( p_dec, p, i_size );
466 p_part->i_dts = p_block->i_dts;
467 p_part->i_pts = p_block->i_pts;
470 if( ( p_pic = ParseNALBlock( p_dec, &b_dummy, p_part ) ) )
472 block_ChainAppend( &p_ret, p_pic );
476 block_Release( p_block );
481 /*****************************************************************************
483 *****************************************************************************/
484 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
486 decoder_sys_t *p_sys = p_dec->p_sys;
489 for( int i = 0; i < 4; i++ )
490 pb_present[i] = p_sys->cc.pb_present[i];
492 if( p_sys->cc.i_data <= 0 )
495 p_cc = block_New( p_dec, p_sys->cc.i_data);
498 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
500 p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
501 p_cc->i_flags = ( p_sys->cc.b_reorder ? p_sys->i_cc_flags : BLOCK_FLAG_TYPE_P ) & BLOCK_FLAG_TYPE_MASK;
503 cc_Flush( &p_sys->cc );
507 /****************************************************************************
509 ****************************************************************************/
510 static void PacketizeReset( void *p_private, bool b_broken )
512 decoder_t *p_dec = p_private;
513 decoder_sys_t *p_sys = p_dec->p_sys;
518 block_ChainRelease( p_sys->p_frame );
519 p_sys->p_frame = NULL;
520 p_sys->slice.i_frame_type = 0;
521 p_sys->b_slice = false;
523 p_sys->i_frame_pts = -1;
524 p_sys->i_frame_dts = -1;
526 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
528 decoder_t *p_dec = p_private;
530 /* Remove trailing 0 bytes */
531 while( p_block->i_buffer && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
534 return ParseNALBlock( p_dec, pb_ts_used, p_block );
536 static int PacketizeValidate( void *p_private, block_t *p_au )
538 VLC_UNUSED(p_private);
543 static block_t *CreateAnnexbNAL( decoder_t *p_dec, const uint8_t *p, int i_size )
547 p_nal = block_New( p_dec, 4 + i_size );
548 if( !p_nal ) return NULL;
551 p_nal->p_buffer[0] = 0x00;
552 p_nal->p_buffer[1] = 0x00;
553 p_nal->p_buffer[2] = 0x00;
554 p_nal->p_buffer[3] = 0x01;
557 memcpy( &p_nal->p_buffer[4], p, i_size );
563 static void CreateDecodedNAL( uint8_t **pp_ret, int *pi_ret,
564 const uint8_t *src, int i_src )
566 const uint8_t *end = &src[i_src];
567 uint8_t *dst = malloc( i_src );
575 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 &&
587 *pi_ret = dst - *pp_ret;
590 static inline int bs_read_ue( bs_t *s )
594 while( bs_read1( s ) == 0 && s->p < s->p_end && i < 32 )
598 return( ( 1 << i) - 1 + bs_read( s, i ) );
601 static inline int bs_read_se( bs_t *s )
603 int val = bs_read_ue( s );
605 return val&0x01 ? (val+1)/2 : -(val/2);
608 /*****************************************************************************
609 * ParseNALBlock: parses annexB type NALs
610 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
611 *****************************************************************************/
612 static block_t *ParseNALBlock( decoder_t *p_dec, bool *pb_used_ts, block_t *p_frag )
614 decoder_sys_t *p_sys = p_dec->p_sys;
615 block_t *p_pic = NULL;
617 const int i_nal_ref_idc = (p_frag->p_buffer[4] >> 5)&0x03;
618 const int i_nal_type = p_frag->p_buffer[4]&0x1f;
619 const mtime_t i_frag_dts = p_frag->i_dts;
620 const mtime_t i_frag_pts = p_frag->i_pts;
622 if( p_sys->b_slice && ( !p_sys->b_sps || !p_sys->b_pps ) )
624 block_ChainRelease( p_sys->p_frame );
625 msg_Warn( p_dec, "waiting for SPS/PPS" );
628 p_sys->slice.i_frame_type = 0;
629 p_sys->p_frame = NULL;
630 p_sys->b_slice = false;
631 cc_Flush( &p_sys->cc_next );
634 if( ( !p_sys->b_sps || !p_sys->b_pps ) &&
635 i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
637 p_sys->b_slice = true;
638 /* Fragment will be discarded later on */
640 else if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
645 ParseSlice( p_dec, &b_new_picture, &slice, i_nal_ref_idc, i_nal_type, p_frag );
648 if( b_new_picture && p_sys->b_slice )
649 p_pic = OutputPicture( p_dec );
652 p_sys->slice = slice;
653 p_sys->b_slice = true;
655 else if( i_nal_type == NAL_SPS )
658 p_pic = OutputPicture( p_dec );
660 PutSPS( p_dec, p_frag );
662 /* Do not append the SPS because we will insert it on keyframes */
665 else if( i_nal_type == NAL_PPS )
668 p_pic = OutputPicture( p_dec );
670 PutPPS( p_dec, p_frag );
672 /* Do not append the PPS because we will insert it on keyframes */
675 else if( i_nal_type == NAL_AU_DELIMITER ||
676 i_nal_type == NAL_SEI ||
677 ( i_nal_type >= 13 && i_nal_type <= 18 ) )
680 p_pic = OutputPicture( p_dec );
682 /* Parse SEI for CC support */
683 if( i_nal_type == NAL_SEI )
684 ParseSei( p_dec, p_frag );
687 /* Append the block */
689 block_ChainAppend( &p_sys->p_frame, p_frag );
692 if( p_sys->i_frame_dts < 0 && p_sys->i_frame_pts < 0 )
694 p_sys->i_frame_dts = i_frag_dts;
695 p_sys->i_frame_pts = i_frag_pts;
701 static block_t *OutputPicture( decoder_t *p_dec )
703 decoder_sys_t *p_sys = p_dec->p_sys;
706 if( !p_sys->b_header && p_sys->slice.i_frame_type != BLOCK_FLAG_TYPE_I)
709 if( p_sys->slice.i_frame_type == BLOCK_FLAG_TYPE_I && p_sys->b_sps && p_sys->b_pps )
711 block_t *p_list = NULL;
714 for( i = 0; i < SPS_MAX; i++ )
716 if( p_sys->pp_sps[i] )
717 block_ChainAppend( &p_list, block_Duplicate( p_sys->pp_sps[i] ) );
719 for( i = 0; i < PPS_MAX; i++ )
721 if( p_sys->pp_pps[i] )
722 block_ChainAppend( &p_list, block_Duplicate( p_sys->pp_pps[i] ) );
725 p_sys->b_header = true;
727 block_ChainAppend( &p_list, p_sys->p_frame );
728 p_pic = block_ChainGather( p_list );
732 p_pic = block_ChainGather( p_sys->p_frame );
734 p_pic->i_dts = p_sys->i_frame_dts;
735 p_pic->i_pts = p_sys->i_frame_pts;
736 p_pic->i_length = 0; /* FIXME */
737 p_pic->i_flags |= p_sys->slice.i_frame_type;
739 p_sys->slice.i_frame_type = 0;
740 p_sys->p_frame = NULL;
741 p_sys->i_frame_dts = -1;
742 p_sys->i_frame_pts = -1;
743 p_sys->b_slice = false;
746 p_sys->i_cc_pts = p_pic->i_pts;
747 p_sys->i_cc_dts = p_pic->i_dts;
748 p_sys->i_cc_flags = p_pic->i_flags;
751 cc_data_t cc_tmp = p_sys->cc;
752 p_sys->cc = p_sys->cc_next;
753 p_sys->cc_next = cc_tmp;
755 cc_Flush( &p_sys->cc_next );
760 static void PutSPS( decoder_t *p_dec, block_t *p_frag )
762 decoder_sys_t *p_sys = p_dec->p_sys;
764 uint8_t *pb_dec = NULL;
770 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5],
771 p_frag->i_buffer - 5 );
773 bs_init( &s, pb_dec, i_dec );
774 int i_profile_idc = bs_read( &s, 8 );
775 /* Skip constraint_set0123, reserved(4), level(8) */
776 bs_skip( &s, 1+1+1+1 + 4 + 8 );
778 i_sps_id = bs_read_ue( &s );
779 if( i_sps_id >= SPS_MAX )
781 msg_Warn( p_dec, "invalid SPS (sps_id=%d)", i_sps_id );
783 block_Release( p_frag );
787 if( i_profile_idc == 100 || i_profile_idc == 110 ||
788 i_profile_idc == 122 || i_profile_idc == 244 ||
789 i_profile_idc == 44 || i_profile_idc == 83 ||
790 i_profile_idc == 86 )
792 /* chroma_format_idc */
793 const int i_chroma_format_idc = bs_read_ue( &s );
794 if( i_chroma_format_idc == 3 )
795 bs_skip( &s, 1 ); /* seperate_colour_plane_flag */
796 /* bit_depth_luma_minus8 */
798 /* bit_depth_chroma_minus8 */
800 /* qpprime_y_zero_transform_bypass_flag */
802 /* seq_scaling_matrix_present_flag */
803 i_tmp = bs_read( &s, 1 );
806 for( int i = 0; i < ((3 != i_chroma_format_idc) ? 8 : 12); i++ )
808 /* seq_scaling_list_present_flag[i] */
809 i_tmp = bs_read( &s, 1 );
812 const int i_size_of_scaling_list = (i < 6 ) ? 16 : 64;
813 /* scaling_list (...) */
816 for( int j = 0; j < i_size_of_scaling_list; j++ )
818 if( i_nextscale != 0 )
821 i_tmp = bs_read( &s, 1 );
822 i_nextscale = ( i_lastscale + i_tmp + 256 ) % 256;
823 /* useDefaultScalingMatrixFlag = ... */
826 i_lastscale = ( i_nextscale == 0 ) ? i_lastscale : i_nextscale;
832 /* Skip i_log2_max_frame_num */
833 p_sys->i_log2_max_frame_num = bs_read_ue( &s );
834 if( p_sys->i_log2_max_frame_num > 12)
835 p_sys->i_log2_max_frame_num = 12;
837 p_sys->i_pic_order_cnt_type = bs_read_ue( &s );
838 if( p_sys->i_pic_order_cnt_type == 0 )
840 /* skip i_log2_max_poc_lsb */
841 p_sys->i_log2_max_pic_order_cnt_lsb = bs_read_ue( &s );
842 if( p_sys->i_log2_max_pic_order_cnt_lsb > 12 )
843 p_sys->i_log2_max_pic_order_cnt_lsb = 12;
845 else if( p_sys->i_pic_order_cnt_type == 1 )
848 /* skip b_delta_pic_order_always_zero */
849 p_sys->i_delta_pic_order_always_zero_flag = bs_read( &s, 1 );
850 /* skip i_offset_for_non_ref_pic */
852 /* skip i_offset_for_top_to_bottom_field */
854 /* read i_num_ref_frames_in_poc_cycle */
855 i_cycle = bs_read_ue( &s );
856 if( i_cycle > 256 ) i_cycle = 256;
859 /* skip i_offset_for_ref_frame */
864 /* i_num_ref_frames */
866 /* b_gaps_in_frame_num_value_allowed */
870 p_dec->fmt_out.video.i_width = 16 * ( bs_read_ue( &s ) + 1 );
871 p_dec->fmt_out.video.i_height = 16 * ( bs_read_ue( &s ) + 1 );
873 /* b_frame_mbs_only */
874 p_sys->b_frame_mbs_only = bs_read( &s, 1 );
875 if( p_sys->b_frame_mbs_only == 0 )
879 /* b_direct8x8_inference */
883 i_tmp = bs_read( &s, 1 );
897 i_tmp = bs_read( &s, 1 );
900 /* read the aspect ratio part if any */
901 i_tmp = bs_read( &s, 1 );
904 static const struct { int w, h; } sar[17] =
906 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 },
907 { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
908 { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
909 { 64, 33 }, { 160,99 }, { 4, 3 }, { 3, 2 },
912 int i_sar = bs_read( &s, 8 );
920 else if( i_sar == 255 )
922 w = bs_read( &s, 16 );
923 h = bs_read( &s, 16 );
932 p_dec->fmt_out.video.i_aspect = (int64_t)VOUT_ASPECT_FACTOR *
933 ( w * p_dec->fmt_out.video.i_width ) /
934 ( h * p_dec->fmt_out.video.i_height);
936 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR;
942 /* We have a new SPS */
944 msg_Dbg( p_dec, "found NAL_SPS (sps_id=%d)", i_sps_id );
947 if( p_sys->pp_sps[i_sps_id] )
948 block_Release( p_sys->pp_sps[i_sps_id] );
949 p_sys->pp_sps[i_sps_id] = p_frag;
952 static void PutPPS( decoder_t *p_dec, block_t *p_frag )
954 decoder_sys_t *p_sys = p_dec->p_sys;
959 bs_init( &s, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
960 i_pps_id = bs_read_ue( &s ); // pps id
961 i_sps_id = bs_read_ue( &s ); // sps id
962 if( i_pps_id >= PPS_MAX || i_sps_id >= SPS_MAX )
964 msg_Warn( p_dec, "invalid PPS (pps_id=%d sps_id=%d)", i_pps_id, i_sps_id );
965 block_Release( p_frag );
968 bs_skip( &s, 1 ); // entropy coding mode flag
969 p_sys->i_pic_order_present_flag = bs_read( &s, 1 );
972 /* We have a new PPS */
974 msg_Dbg( p_dec, "found NAL_PPS (pps_id=%d sps_id=%d)", i_pps_id, i_sps_id );
977 if( p_sys->pp_pps[i_pps_id] )
978 block_Release( p_sys->pp_pps[i_pps_id] );
979 p_sys->pp_pps[i_pps_id] = p_frag;
982 static void ParseSlice( decoder_t *p_dec, bool *pb_new_picture, slice_t *p_slice,
983 int i_nal_ref_idc, int i_nal_type, const block_t *p_frag )
985 decoder_sys_t *p_sys = p_dec->p_sys;
988 int i_first_mb, i_slice_type;
992 /* do not convert the whole frame */
993 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5],
994 __MIN( p_frag->i_buffer - 5, 60 ) );
995 bs_init( &s, pb_dec, i_dec );
997 /* first_mb_in_slice */
998 i_first_mb = bs_read_ue( &s );
1001 switch( (i_slice_type = bs_read_ue( &s )) )
1004 slice.i_frame_type = BLOCK_FLAG_TYPE_P;
1007 slice.i_frame_type = BLOCK_FLAG_TYPE_B;
1010 slice.i_frame_type = BLOCK_FLAG_TYPE_I;
1012 case 3: case 8: /* SP */
1013 slice.i_frame_type = BLOCK_FLAG_TYPE_P;
1016 slice.i_frame_type = BLOCK_FLAG_TYPE_I;
1019 slice.i_frame_type = 0;
1024 slice.i_nal_type = i_nal_type;
1025 slice.i_nal_ref_idc = i_nal_ref_idc;
1027 slice.i_pic_parameter_set_id = bs_read_ue( &s );
1028 slice.i_frame_num = bs_read( &s, p_sys->i_log2_max_frame_num + 4 );
1030 slice.i_field_pic_flag = 0;
1031 slice.i_bottom_field_flag = -1;
1032 if( !p_sys->b_frame_mbs_only )
1034 /* field_pic_flag */
1035 slice.i_field_pic_flag = bs_read( &s, 1 );
1036 if( slice.i_field_pic_flag )
1037 slice.i_bottom_field_flag = bs_read( &s, 1 );
1040 slice.i_idr_pic_id = p_sys->slice.i_idr_pic_id;
1041 if( slice.i_nal_type == NAL_SLICE_IDR )
1042 slice.i_idr_pic_id = bs_read_ue( &s );
1044 slice.i_pic_order_cnt_lsb = -1;
1045 slice.i_delta_pic_order_cnt_bottom = -1;
1046 slice.i_delta_pic_order_cnt0 = 0;
1047 slice.i_delta_pic_order_cnt1 = 0;
1048 if( p_sys->i_pic_order_cnt_type == 0 )
1050 slice.i_pic_order_cnt_lsb = bs_read( &s, p_sys->i_log2_max_pic_order_cnt_lsb + 4 );
1051 if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
1052 slice.i_delta_pic_order_cnt_bottom = bs_read_se( &s );
1054 else if( (p_sys->i_pic_order_cnt_type == 1) &&
1055 (!p_sys->i_delta_pic_order_always_zero_flag) )
1057 slice.i_delta_pic_order_cnt0 = bs_read_se( &s );
1058 if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
1059 slice.i_delta_pic_order_cnt1 = bs_read_se( &s );
1063 /* Detection of the first VCL NAL unit of a primary coded picture
1064 * (cf. 7.4.1.2.4) */
1066 if( slice.i_frame_num != p_sys->slice.i_frame_num ||
1067 slice.i_pic_parameter_set_id != p_sys->slice.i_pic_parameter_set_id ||
1068 slice.i_field_pic_flag != p_sys->slice.i_field_pic_flag ||
1069 slice.i_nal_ref_idc != p_sys->slice.i_nal_ref_idc )
1071 if( (slice.i_bottom_field_flag != -1) &&
1072 (p_sys->slice.i_bottom_field_flag != -1) &&
1073 (slice.i_bottom_field_flag != p_sys->slice.i_bottom_field_flag) )
1075 if( p_sys->i_pic_order_cnt_type == 0 &&
1076 ( slice.i_pic_order_cnt_lsb != p_sys->slice.i_pic_order_cnt_lsb ||
1077 slice.i_delta_pic_order_cnt_bottom != p_sys->slice.i_delta_pic_order_cnt_bottom ) )
1079 else if( p_sys->i_pic_order_cnt_type == 1 &&
1080 ( slice.i_delta_pic_order_cnt0 != p_sys->slice.i_delta_pic_order_cnt0 ||
1081 slice.i_delta_pic_order_cnt1 != p_sys->slice.i_delta_pic_order_cnt1 ) )
1083 if( ( slice.i_nal_type == NAL_SLICE_IDR || p_sys->slice.i_nal_type == NAL_SLICE_IDR ) &&
1084 ( slice.i_nal_type != p_sys->slice.i_nal_type || slice.i_idr_pic_id != p_sys->slice.i_idr_pic_id ) )
1088 *pb_new_picture = b_pic;
1092 static void ParseSei( decoder_t *p_dec, block_t *p_frag )
1094 decoder_sys_t *p_sys = p_dec->p_sys;
1099 CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
1103 /* The +1 is for rbsp trailing bits */
1104 for( int i_used = 0; i_used+1 < i_dec; )
1108 while( i_used+1 < i_dec )
1110 const int i_byte = pb_dec[i_used++];
1112 if( i_byte != 0xff )
1117 while( i_used+1 < i_dec )
1119 const int i_byte = pb_dec[i_used++];
1121 if( i_byte != 0xff )
1125 if( i_used + i_size + 1 > i_dec )
1128 /* Look for user_data_registered_itu_t_t35 */
1131 static const uint8_t p_dvb1_data_start_code[] = {
1134 0x47, 0x41, 0x39, 0x34
1136 const int i_t35 = i_size;
1137 const uint8_t *p_t35 = &pb_dec[i_used];
1139 /* Check for we have DVB1_data() */
1141 !memcmp( p_t35, p_dvb1_data_start_code, sizeof(p_dvb1_data_start_code) ) )
1143 cc_Extract( &p_sys->cc_next, &p_t35[3], i_t35 - 3 );