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>
44 #include "h264_slice.h"
47 #include "hxxx_common.h"
48 #include "packetizer_helper.h"
49 #include "startcode_helper.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open ( vlc_object_t
* );
57 static void Close( vlc_object_t
* );
60 set_category( CAT_SOUT
)
61 set_subcategory( SUBCAT_SOUT_PACKETIZER
)
62 set_description( N_("H.264 video packetizer") )
63 set_capability( "packetizer", 50 )
64 set_callbacks( Open
, Close
)
68 /****************************************************************************
70 ****************************************************************************/
75 packetizer_t packetizer
;
80 block_t
**pp_frame_last
;
82 block_t
**pp_sei_last
;
83 /* a new sps/pps can be transmitted outside of iframes */
90 h264_sequence_parameter_set_t
*p_sps
;
91 } sps
[H264_SPS_ID_MAX
+ 1];
95 h264_picture_parameter_set_t
*p_pps
;
96 } pps
[H264_PPS_ID_MAX
+ 1];
97 const h264_sequence_parameter_set_t
*p_active_sps
;
98 const h264_picture_parameter_set_t
*p_active_pps
;
101 uint8_t i_avcC_length_size
;
103 /* From SEI for current frame */
104 uint8_t i_pic_struct
;
105 uint8_t i_dpb_output_delay
;
106 unsigned i_recovery_frame_cnt
;
108 /* Useful values of the Slice Header */
112 int i_next_block_flags
;
114 unsigned i_recoveryfnum
;
117 poc_context_t pocctx
;
133 #define BLOCK_FLAG_PRIVATE_AUD (1 << BLOCK_FLAG_PRIVATE_SHIFT)
134 #define BLOCK_FLAG_DROP (2 << BLOCK_FLAG_PRIVATE_SHIFT)
136 static block_t
*Packetize( decoder_t
*, block_t
** );
137 static block_t
*PacketizeAVC1( decoder_t
*, block_t
** );
138 static block_t
*GetCc( decoder_t
*p_dec
, bool pb_present
[4], int * );
139 static void PacketizeFlush( decoder_t
* );
141 static void PacketizeReset( void *p_private
, bool b_broken
);
142 static block_t
*PacketizeParse( void *p_private
, bool *pb_ts_used
, block_t
* );
143 static int PacketizeValidate( void *p_private
, block_t
* );
145 static block_t
*ParseNALBlock( decoder_t
*, bool *pb_ts_used
, block_t
* );
147 static block_t
*OutputPicture( decoder_t
*p_dec
);
148 static void PutSPS( decoder_t
*p_dec
, block_t
*p_frag
);
149 static void PutPPS( decoder_t
*p_dec
, block_t
*p_frag
);
150 static bool ParseSliceHeader( decoder_t
*p_dec
, const block_t
*p_frag
, h264_slice_t
*p_slice
);
151 static bool ParseSeiCallback( const hxxx_sei_data_t
*, void * );
154 static const uint8_t p_h264_startcode
[3] = { 0x00, 0x00, 0x01 };
156 /*****************************************************************************
158 *****************************************************************************/
160 static void StoreSPS( decoder_sys_t
*p_sys
, uint8_t i_id
,
161 block_t
*p_block
, h264_sequence_parameter_set_t
*p_sps
)
163 if( p_sys
->sps
[i_id
].p_block
)
164 block_Release( p_sys
->sps
[i_id
].p_block
);
165 if( p_sys
->sps
[i_id
].p_sps
)
166 h264_release_sps( p_sys
->sps
[i_id
].p_sps
);
167 if( p_sys
->sps
[i_id
].p_sps
== p_sys
->p_active_sps
)
168 p_sys
->p_active_sps
= NULL
;
169 p_sys
->sps
[i_id
].p_block
= p_block
;
170 p_sys
->sps
[i_id
].p_sps
= p_sps
;
173 static void StorePPS( decoder_sys_t
*p_sys
, uint8_t i_id
,
174 block_t
*p_block
, h264_picture_parameter_set_t
*p_pps
)
176 if( p_sys
->pps
[i_id
].p_block
)
177 block_Release( p_sys
->pps
[i_id
].p_block
);
178 if( p_sys
->pps
[i_id
].p_pps
)
179 h264_release_pps( p_sys
->pps
[i_id
].p_pps
);
180 if( p_sys
->pps
[i_id
].p_pps
== p_sys
->p_active_pps
)
181 p_sys
->p_active_pps
= NULL
;
182 p_sys
->pps
[i_id
].p_block
= p_block
;
183 p_sys
->pps
[i_id
].p_pps
= p_pps
;
186 static void ActivateSets( decoder_t
*p_dec
, const h264_sequence_parameter_set_t
*p_sps
,
187 const h264_picture_parameter_set_t
*p_pps
)
189 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
191 p_sys
->p_active_pps
= p_pps
;
192 p_sys
->p_active_sps
= p_sps
;
196 p_dec
->fmt_out
.i_profile
= p_sps
->i_profile
;
197 p_dec
->fmt_out
.i_level
= p_sps
->i_level
;
199 (void) h264_get_picture_size( p_sps
, &p_dec
->fmt_out
.video
.i_width
,
200 &p_dec
->fmt_out
.video
.i_height
,
201 &p_dec
->fmt_out
.video
.i_visible_width
,
202 &p_dec
->fmt_out
.video
.i_visible_height
);
204 if( p_sps
->vui
.i_sar_num
!= 0 && p_sps
->vui
.i_sar_den
!= 0 )
206 p_dec
->fmt_out
.video
.i_sar_num
= p_sps
->vui
.i_sar_num
;
207 p_dec
->fmt_out
.video
.i_sar_den
= p_sps
->vui
.i_sar_den
;
210 if( p_sps
->vui
.b_valid
)
212 if( !p_dec
->fmt_in
.video
.i_frame_rate_base
&& p_sps
->vui
.i_num_units_in_tick
> 0 )
214 const unsigned i_rate_base
= p_sps
->vui
.i_num_units_in_tick
;
215 const unsigned i_rate
= p_sps
->vui
.i_time_scale
>> 1; /* num_clock_ts == 2 */
216 if( i_rate_base
!= p_dec
->fmt_out
.video
.i_frame_rate_base
||
217 i_rate
!= p_dec
->fmt_out
.video
.i_frame_rate
)
219 p_dec
->fmt_out
.video
.i_frame_rate_base
= i_rate_base
;
220 p_dec
->fmt_out
.video
.i_frame_rate
= i_rate
;
221 date_Change( &p_sys
->dts
, p_sps
->vui
.i_time_scale
, p_sps
->vui
.i_num_units_in_tick
);
224 if( p_dec
->fmt_in
.video
.primaries
== COLOR_PRIMARIES_UNDEF
)
226 p_dec
->fmt_out
.video
.primaries
=
227 hxxx_colour_primaries_to_vlc( p_sps
->vui
.colour
.i_colour_primaries
);
228 p_dec
->fmt_out
.video
.transfer
=
229 hxxx_transfer_characteristics_to_vlc( p_sps
->vui
.colour
.i_transfer_characteristics
);
230 p_dec
->fmt_out
.video
.space
=
231 hxxx_matrix_coeffs_to_vlc( p_sps
->vui
.colour
.i_matrix_coefficients
);
232 p_dec
->fmt_out
.video
.b_color_range_full
= p_sps
->vui
.colour
.b_full_range
;
238 static bool IsFirstVCLNALUnit( const h264_slice_t
*p_prev
, const h264_slice_t
*p_cur
)
240 /* Detection of the first VCL NAL unit of a primary coded picture
242 if( p_cur
->i_frame_num
!= p_prev
->i_frame_num
||
243 p_cur
->i_pic_parameter_set_id
!= p_prev
->i_pic_parameter_set_id
||
244 p_cur
->i_field_pic_flag
!= p_prev
->i_field_pic_flag
||
245 !p_cur
->i_nal_ref_idc
!= !p_prev
->i_nal_ref_idc
)
247 if( (p_cur
->i_bottom_field_flag
!= -1) &&
248 (p_prev
->i_bottom_field_flag
!= -1) &&
249 (p_cur
->i_bottom_field_flag
!= p_prev
->i_bottom_field_flag
) )
251 if( p_cur
->i_pic_order_cnt_type
== 0 &&
252 ( p_cur
->i_pic_order_cnt_lsb
!= p_prev
->i_pic_order_cnt_lsb
||
253 p_cur
->i_delta_pic_order_cnt_bottom
!= p_prev
->i_delta_pic_order_cnt_bottom
) )
255 else if( p_cur
->i_pic_order_cnt_type
== 1 &&
256 ( p_cur
->i_delta_pic_order_cnt0
!= p_prev
->i_delta_pic_order_cnt0
||
257 p_cur
->i_delta_pic_order_cnt1
!= p_prev
->i_delta_pic_order_cnt1
) )
259 if( ( p_cur
->i_nal_type
== H264_NAL_SLICE_IDR
|| p_prev
->i_nal_type
== H264_NAL_SLICE_IDR
) &&
260 ( p_cur
->i_nal_type
!= p_prev
->i_nal_type
|| p_cur
->i_idr_pic_id
!= p_prev
->i_idr_pic_id
) )
266 static void DropStoredNAL( decoder_sys_t
*p_sys
)
268 block_ChainRelease( p_sys
->p_frame
);
269 block_ChainRelease( p_sys
->p_sei
);
270 p_sys
->p_frame
= NULL
;
271 p_sys
->pp_frame_last
= &p_sys
->p_frame
;
273 p_sys
->pp_sei_last
= &p_sys
->p_sei
;
276 /*****************************************************************************
277 * Open: probe the packetizer and return score
278 * When opening after demux, the packetizer is only loaded AFTER the decoder
279 * That means that what you set in fmt_out is ignored by the decoder in this special case
280 *****************************************************************************/
281 static int Open( vlc_object_t
*p_this
)
283 decoder_t
*p_dec
= (decoder_t
*)p_this
;
284 decoder_sys_t
*p_sys
;
287 const bool b_avc
= (p_dec
->fmt_in
.i_original_fourcc
== VLC_FOURCC( 'a', 'v', 'c', '1' ));
289 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_H264
)
291 if( b_avc
&& p_dec
->fmt_in
.i_extra
< 7 )
294 /* Allocate the memory needed to store the decoder's structure */
295 if( ( p_dec
->p_sys
= p_sys
= malloc( sizeof(decoder_sys_t
) ) ) == NULL
)
300 p_sys
->p_ccs
= cc_storage_new();
301 if( unlikely(!p_sys
->p_ccs
) )
303 free( p_dec
->p_sys
);
307 packetizer_Init( &p_sys
->packetizer
,
308 p_h264_startcode
, sizeof(p_h264_startcode
), startcode_FindAnnexB
,
309 p_h264_startcode
, 1, 5,
310 PacketizeReset
, PacketizeParse
, PacketizeValidate
, p_dec
);
312 p_sys
->b_slice
= false;
313 p_sys
->p_frame
= NULL
;
314 p_sys
->pp_frame_last
= &p_sys
->p_frame
;
316 p_sys
->pp_sei_last
= &p_sys
->p_sei
;
317 p_sys
->b_new_sps
= false;
318 p_sys
->b_new_pps
= false;
320 for( i
= 0; i
<= H264_SPS_ID_MAX
; i
++ )
322 p_sys
->sps
[i
].p_sps
= NULL
;
323 p_sys
->sps
[i
].p_block
= NULL
;
325 p_sys
->p_active_sps
= NULL
;
326 for( i
= 0; i
<= H264_PPS_ID_MAX
; i
++ )
328 p_sys
->pps
[i
].p_pps
= NULL
;
329 p_sys
->pps
[i
].p_block
= NULL
;
331 p_sys
->p_active_pps
= NULL
;
332 p_sys
->i_recovery_frame_cnt
= UINT_MAX
;
334 h264_slice_init( &p_sys
->slice
);
336 p_sys
->i_next_block_flags
= 0;
337 p_sys
->b_recovered
= false;
338 p_sys
->i_recoveryfnum
= UINT_MAX
;
339 p_sys
->i_frame_dts
= VLC_TS_INVALID
;
340 p_sys
->i_frame_pts
= VLC_TS_INVALID
;
341 p_sys
->i_dpb_output_delay
= 0;
344 h264_poc_context_init( &p_sys
->pocctx
);
345 p_sys
->prevdatedpoc
.pts
= VLC_TS_INVALID
;
347 date_Init( &p_sys
->dts
, 30000 * 2, 1001 );
348 date_Set( &p_sys
->dts
, VLC_TS_INVALID
);
350 /* Setup properties */
351 es_format_Copy( &p_dec
->fmt_out
, &p_dec
->fmt_in
);
352 p_dec
->fmt_out
.i_codec
= VLC_CODEC_H264
;
353 p_dec
->fmt_out
.b_packetized
= true;
355 if( p_dec
->fmt_in
.video
.i_frame_rate_base
&&
356 p_dec
->fmt_in
.video
.i_frame_rate
)
358 date_Change( &p_sys
->dts
, p_dec
->fmt_in
.video
.i_frame_rate
* 2,
359 p_dec
->fmt_in
.video
.i_frame_rate_base
);
364 /* This type of stream is produced by mp4 and matroska
365 * when we want to store it in another streamformat, you need to convert
366 * The fmt_in.p_extra should ALWAYS contain the avcC
367 * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */
368 if( h264_isavcC( p_dec
->fmt_in
.p_extra
, p_dec
->fmt_in
.i_extra
) )
370 free( p_dec
->fmt_out
.p_extra
);
372 p_dec
->fmt_out
.p_extra
= h264_avcC_to_AnnexB_NAL( p_dec
->fmt_in
.p_extra
,
373 p_dec
->fmt_in
.i_extra
,
375 &p_sys
->i_avcC_length_size
);
376 p_dec
->fmt_out
.i_extra
= i_size
;
377 p_sys
->b_recovered
= !!p_dec
->fmt_out
.i_extra
;
379 if(!p_dec
->fmt_out
.p_extra
)
381 msg_Err( p_dec
, "Invalid AVC extradata");
388 msg_Err( p_dec
, "Invalid or missing AVC extradata");
394 p_dec
->pf_packetize
= PacketizeAVC1
;
398 /* This type of stream contains data with 3 of 4 byte startcodes
399 * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes
400 * The fmt_out.p_extra should be the same */
403 p_dec
->pf_packetize
= Packetize
;
407 if( p_dec
->fmt_out
.i_extra
> 0 )
409 packetizer_Header( &p_sys
->packetizer
,
410 p_dec
->fmt_out
.p_extra
, p_dec
->fmt_out
.i_extra
);
415 /* FIXME: that's not correct for every AVC */
416 if( !p_sys
->b_new_pps
|| !p_sys
->b_new_sps
)
418 msg_Err( p_dec
, "Invalid or missing SPS %d or PPS %d in AVC extradata",
419 p_sys
->b_new_sps
, p_sys
->b_new_pps
);
424 msg_Dbg( p_dec
, "Packetizer fed with AVC, nal length size=%d",
425 p_sys
->i_avcC_length_size
);
428 /* CC are the same for H264/AVC in T35 sections (ETSI TS 101 154) */
429 p_dec
->pf_get_cc
= GetCc
;
430 p_dec
->pf_flush
= PacketizeFlush
;
435 /*****************************************************************************
436 * Close: clean up the packetizer
437 *****************************************************************************/
438 static void Close( vlc_object_t
*p_this
)
440 decoder_t
*p_dec
= (decoder_t
*)p_this
;
441 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
444 DropStoredNAL( p_sys
);
445 for( i
= 0; i
<= H264_SPS_ID_MAX
; i
++ )
446 StoreSPS( p_sys
, i
, NULL
, NULL
);
447 for( i
= 0; i
<= H264_PPS_ID_MAX
; i
++ )
448 StorePPS( p_sys
, i
, NULL
, NULL
);
450 packetizer_Clean( &p_sys
->packetizer
);
452 cc_storage_delete( p_sys
->p_ccs
);
457 static void PacketizeFlush( decoder_t
*p_dec
)
459 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
461 packetizer_Flush( &p_sys
->packetizer
);
464 /****************************************************************************
465 * Packetize: the whole thing
466 * Search for the startcodes 3 or more bytes
467 * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs
468 ****************************************************************************/
469 static block_t
*Packetize( decoder_t
*p_dec
, block_t
**pp_block
)
471 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
473 return packetizer_Packetize( &p_sys
->packetizer
, pp_block
);
476 /****************************************************************************
477 * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream
478 * Will always use 4 byte 0 0 0 1 startcodes
479 * Will prepend a SPS and PPS before each keyframe
480 ****************************************************************************/
481 static block_t
*PacketizeAVC1( decoder_t
*p_dec
, block_t
**pp_block
)
483 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
485 return PacketizeXXC1( p_dec
, p_sys
->i_avcC_length_size
,
486 pp_block
, ParseNALBlock
);
489 /*****************************************************************************
491 *****************************************************************************/
492 static block_t
*GetCc( decoder_t
*p_dec
, bool pb_present
[4], int *pi_reorder_depth
)
494 *pi_reorder_depth
= 0;
495 return cc_storage_get_current( p_dec
->p_sys
->p_ccs
, pb_present
);
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
;
506 if( b_broken
|| !p_sys
->b_slice
)
508 DropStoredNAL( p_sys
);
509 p_sys
->b_new_sps
= false;
510 p_sys
->b_new_pps
= false;
511 p_sys
->p_active_pps
= NULL
;
512 p_sys
->p_active_sps
= NULL
;
513 p_sys
->i_frame_pts
= VLC_TS_INVALID
;
514 p_sys
->i_frame_dts
= VLC_TS_INVALID
;
515 p_sys
->slice
.type
= H264_SLICE_TYPE_UNKNOWN
;
516 p_sys
->b_slice
= false;
518 h264_poc_context_init( &p_sys
->pocctx
);
519 p_sys
->prevdatedpoc
.pts
= VLC_TS_INVALID
;
521 p_sys
->i_dpb_output_delay
= 0;
522 p_sys
->i_pic_struct
= UINT8_MAX
;
523 p_sys
->i_recovery_frame_cnt
= UINT_MAX
;
525 p_sys
->i_next_block_flags
= BLOCK_FLAG_DISCONTINUITY
;
526 p_sys
->b_recovered
= false;
527 p_sys
->i_recoveryfnum
= UINT_MAX
;
528 date_Set( &p_sys
->dts
, VLC_TS_INVALID
);
530 static block_t
*PacketizeParse( void *p_private
, bool *pb_ts_used
, block_t
*p_block
)
532 decoder_t
*p_dec
= p_private
;
534 /* Remove trailing 0 bytes */
535 while( p_block
->i_buffer
> 5 && p_block
->p_buffer
[p_block
->i_buffer
-1] == 0x00 )
538 return ParseNALBlock( p_dec
, pb_ts_used
, p_block
);
540 static int PacketizeValidate( void *p_private
, block_t
*p_au
)
542 VLC_UNUSED(p_private
);
547 /*****************************************************************************
548 * ParseNALBlock: parses annexB type NALs
549 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
550 *****************************************************************************/
551 static block_t
*ParseNALBlock( decoder_t
*p_dec
, bool *pb_ts_used
, block_t
*p_frag
)
553 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
554 block_t
*p_pic
= NULL
;
555 bool b_new_picture
= false;
557 const int i_nal_type
= p_frag
->p_buffer
[4]&0x1f;
558 const mtime_t i_frag_dts
= p_frag
->i_dts
;
559 const mtime_t i_frag_pts
= p_frag
->i_pts
;
561 if( p_sys
->b_slice
&& (!p_sys
->p_active_pps
|| !p_sys
->p_active_sps
) )
563 msg_Warn( p_dec
, "waiting for SPS/PPS" );
566 p_sys
->slice
.type
= H264_SLICE_TYPE_UNKNOWN
;
567 p_sys
->b_slice
= false;
568 DropStoredNAL( p_sys
);
570 p_sys
->i_dpb_output_delay
= 0;
571 p_sys
->i_pic_struct
= UINT8_MAX
;
572 cc_storage_reset( p_sys
->p_ccs
);
575 if( i_nal_type
>= H264_NAL_SLICE
&& i_nal_type
<= H264_NAL_SLICE_IDR
)
577 h264_slice_t newslice
;
579 if( i_nal_type
== H264_NAL_SLICE_IDR
)
581 p_sys
->b_recovered
= true;
582 p_sys
->i_recovery_frame_cnt
= UINT_MAX
;
583 p_sys
->i_recoveryfnum
= UINT_MAX
;
586 if( ParseSliceHeader( p_dec
, p_frag
, &newslice
) )
588 /* Only IDR carries the id, to be propagated */
589 if( newslice
.i_idr_pic_id
== -1 )
590 newslice
.i_idr_pic_id
= p_sys
->slice
.i_idr_pic_id
;
592 b_new_picture
= IsFirstVCLNALUnit( &p_sys
->slice
, &newslice
);
595 /* Parse SEI for that frame now we should have matched SPS/PPS */
596 for( block_t
*p_sei
= p_sys
->p_sei
; p_sei
; p_sei
= p_sei
->p_next
)
598 HxxxParse_AnnexB_SEI( p_sei
->p_buffer
, p_sei
->i_buffer
,
599 1 /* nal header */, ParseSeiCallback
, p_dec
);
603 p_pic
= OutputPicture( p_dec
);
607 p_sys
->slice
= newslice
;
611 p_sys
->p_active_pps
= NULL
;
612 /* Fragment will be discarded later on */
614 p_sys
->b_slice
= true;
616 else if( i_nal_type
== H264_NAL_SPS
)
619 p_pic
= OutputPicture( p_dec
);
621 PutSPS( p_dec
, p_frag
);
622 p_sys
->b_new_sps
= true;
624 /* Do not append the SPS because we will insert it on keyframes */
627 else if( i_nal_type
== H264_NAL_PPS
)
630 p_pic
= OutputPicture( p_dec
);
632 PutPPS( p_dec
, p_frag
);
633 p_sys
->b_new_pps
= true;
635 /* Do not append the PPS because we will insert it on keyframes */
638 else if( i_nal_type
== H264_NAL_SEI
)
641 p_pic
= OutputPicture( p_dec
);
643 block_ChainLastAppend( &p_sys
->pp_sei_last
, p_frag
);
646 else if( i_nal_type
== H264_NAL_END_OF_SEQ
|| i_nal_type
== H264_NAL_END_OF_STREAM
)
648 /* Early end of packetization */
649 block_ChainLastAppend( &p_sys
->pp_sei_last
, p_frag
);
651 /* important for still pictures/menus */
652 p_sys
->i_next_block_flags
|= BLOCK_FLAG_END_OF_SEQUENCE
;
654 p_pic
= OutputPicture( p_dec
);
656 else if( i_nal_type
== H264_NAL_AU_DELIMITER
||
657 ( i_nal_type
>= H264_NAL_PREFIX
&& i_nal_type
<= H264_NAL_RESERVED_18
) )
660 p_pic
= OutputPicture( p_dec
);
662 if( i_nal_type
== H264_NAL_AU_DELIMITER
)
664 if( p_sys
->p_frame
&& (p_sys
->p_frame
->i_flags
& BLOCK_FLAG_PRIVATE_AUD
) )
666 block_Release( p_frag
);
671 p_frag
->i_flags
|= BLOCK_FLAG_PRIVATE_AUD
;
676 /* Append the block */
678 block_ChainLastAppend( &p_sys
->pp_frame_last
, p_frag
);
681 if( p_sys
->i_frame_dts
<= VLC_TS_INVALID
&&
682 p_sys
->i_frame_pts
<= VLC_TS_INVALID
&& b_new_picture
)
684 p_sys
->i_frame_dts
= i_frag_dts
;
685 p_sys
->i_frame_pts
= i_frag_pts
;
687 if( i_frag_dts
> VLC_TS_INVALID
)
688 date_Set( &p_sys
->dts
, i_frag_dts
);
691 if( p_pic
&& (p_pic
->i_flags
& BLOCK_FLAG_DROP
) )
693 block_Release( p_pic
);
700 static block_t
*OutputPicture( decoder_t
*p_dec
)
702 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
703 block_t
*p_pic
= NULL
;
704 block_t
**pp_pic_last
= &p_pic
;
706 if( unlikely(!p_sys
->p_frame
) )
708 assert( p_sys
->p_frame
);
712 /* Bind matched/referred PPS and SPS */
713 const h264_picture_parameter_set_t
*p_pps
= p_sys
->p_active_pps
;
714 const h264_sequence_parameter_set_t
*p_sps
= p_sys
->p_active_sps
;
715 if( !p_pps
|| !p_sps
)
717 DropStoredNAL( p_sys
);
721 if( !p_sys
->b_recovered
&& p_sys
->i_recoveryfnum
== UINT_MAX
&&
722 p_sys
->i_recovery_frame_cnt
== UINT_MAX
&& p_sys
->slice
.type
== H264_SLICE_TYPE_I
)
724 /* No way to recover using SEI, just sync on I Slice */
725 p_sys
->b_recovered
= true;
728 bool b_need_sps_pps
= p_sys
->slice
.type
== H264_SLICE_TYPE_I
&&
729 p_sys
->p_active_pps
&& p_sys
->p_active_sps
;
731 /* Handle SEI recovery */
732 if ( !p_sys
->b_recovered
&& p_sys
->i_recovery_frame_cnt
!= UINT_MAX
&&
733 p_sys
->i_recoveryfnum
== UINT_MAX
)
735 p_sys
->i_recoveryfnum
= p_sys
->slice
.i_frame_num
+ p_sys
->i_recovery_frame_cnt
;
736 b_need_sps_pps
= true; /* SPS/PPS must be inserted for SEI recovery */
737 msg_Dbg( p_dec
, "Recovering using SEI, prerolling %u reference pics", p_sys
->i_recovery_frame_cnt
);
740 if( p_sys
->i_recoveryfnum
!= UINT_MAX
)
742 assert(p_sys
->b_recovered
== false);
743 const unsigned maxFrameNum
= 1 << (p_sps
->i_log2_max_frame_num
+ 4);
744 if( (p_sys
->i_recoveryfnum
> maxFrameNum
&&
745 (unsigned)p_sys
->slice
.i_frame_num
<= maxFrameNum
/ 2 &&
746 (unsigned)p_sys
->slice
.i_frame_num
>= p_sys
->i_recoveryfnum
% maxFrameNum
) ||
747 (unsigned)p_sys
->slice
.i_frame_num
>= p_sys
->i_recoveryfnum
)
749 p_sys
->i_recoveryfnum
= UINT_MAX
;
750 p_sys
->b_recovered
= true;
751 msg_Dbg( p_dec
, "Recovery from SEI recovery point complete" );
755 /* Gather PPS/SPS if required */
756 block_t
*p_xpsnal
= NULL
;
757 block_t
**pp_xpsnal_tail
= &p_xpsnal
;
758 if( b_need_sps_pps
|| p_sys
->b_new_sps
|| p_sys
->b_new_pps
)
760 for( int i
= 0; i
<= H264_SPS_ID_MAX
&& (b_need_sps_pps
|| p_sys
->b_new_sps
); i
++ )
762 if( p_sys
->sps
[i
].p_block
)
763 block_ChainLastAppend( &pp_xpsnal_tail
, block_Duplicate( p_sys
->sps
[i
].p_block
) );
765 for( int i
= 0; i
< H264_PPS_ID_MAX
&& (b_need_sps_pps
|| p_sys
->b_new_pps
); i
++ )
767 if( p_sys
->pps
[i
].p_block
)
768 block_ChainLastAppend( &pp_xpsnal_tail
, block_Duplicate( p_sys
->pps
[i
].p_block
) );
772 /* Now rebuild NAL Sequence, inserting PPS/SPS if any */
773 if( p_sys
->p_frame
->i_flags
& BLOCK_FLAG_PRIVATE_AUD
)
775 block_t
*p_au
= p_sys
->p_frame
;
776 p_sys
->p_frame
= p_au
->p_next
;
778 p_au
->i_flags
&= ~BLOCK_FLAG_PRIVATE_AUD
;
779 block_ChainLastAppend( &pp_pic_last
, p_au
);
783 block_ChainLastAppend( &pp_pic_last
, p_xpsnal
);
786 block_ChainLastAppend( &pp_pic_last
, p_sys
->p_sei
);
788 assert( p_sys
->p_frame
);
790 block_ChainLastAppend( &pp_pic_last
, p_sys
->p_frame
);
792 /* Reset chains, now empty */
793 p_sys
->p_frame
= NULL
;
794 p_sys
->pp_frame_last
= &p_sys
->p_frame
;
796 p_sys
->pp_sei_last
= &p_sys
->p_sei
;
798 p_pic
= block_ChainGather( p_pic
);
803 /* for PTS Fixup, interlaced fields (multiple AU/block) */
804 int tFOC
= 0, bFOC
= 0, PictureOrderCount
= 0;
805 h264_compute_poc( p_sps
, &p_sys
->slice
, &p_sys
->pocctx
, &PictureOrderCount
, &tFOC
, &bFOC
);
807 unsigned i_num_clock_ts
= h264_get_num_ts( p_sps
, &p_sys
->slice
, p_sys
->i_pic_struct
, tFOC
, bFOC
);
809 if( p_sps
->frame_mbs_only_flag
== 0 && p_sps
->vui
.b_pic_struct_present_flag
)
811 switch( p_sys
->i_pic_struct
)
813 /* Top and Bottom field slices */
816 p_pic
->i_flags
|= BLOCK_FLAG_SINGLE_FIELD
;
817 p_pic
->i_flags
|= (!p_sys
->slice
.i_bottom_field_flag
) ? BLOCK_FLAG_TOP_FIELD_FIRST
818 : BLOCK_FLAG_BOTTOM_FIELD_FIRST
;
820 /* Each of the following slices contains multiple fields */
822 p_pic
->i_flags
|= BLOCK_FLAG_TOP_FIELD_FIRST
;
825 p_pic
->i_flags
|= BLOCK_FLAG_BOTTOM_FIELD_FIRST
;
828 p_pic
->i_flags
|= BLOCK_FLAG_TOP_FIELD_FIRST
;
831 p_pic
->i_flags
|= BLOCK_FLAG_BOTTOM_FIELD_FIRST
;
838 /* set dts/pts to current block timestamps */
839 p_pic
->i_dts
= p_sys
->i_frame_dts
;
840 p_pic
->i_pts
= p_sys
->i_frame_pts
;
842 /* Fixup missing timestamps after split (multiple AU/block)*/
843 if( p_pic
->i_dts
<= VLC_TS_INVALID
)
844 p_pic
->i_dts
= date_Get( &p_sys
->dts
);
846 if( p_sys
->slice
.type
== H264_SLICE_TYPE_I
)
847 p_sys
->prevdatedpoc
.pts
= VLC_TS_INVALID
;
849 if( p_pic
->i_pts
== VLC_TS_INVALID
)
851 if( p_sys
->prevdatedpoc
.pts
> VLC_TS_INVALID
&&
852 date_Get( &p_sys
->dts
) != VLC_TS_INVALID
)
854 date_t pts
= p_sys
->dts
;
855 date_Set( &pts
, p_sys
->prevdatedpoc
.pts
);
857 int diff
= tFOC
- p_sys
->prevdatedpoc
.num
;
859 date_Increment( &pts
, diff
);
861 date_Decrement( &pts
, -diff
);
863 p_pic
->i_pts
= date_Get( &pts
);
865 /* In case there's no PTS at all */
866 else if( p_sys
->slice
.i_nal_ref_idc
== 0 &&
867 p_sys
->slice
.type
== H264_SLICE_TYPE_B
)
869 p_pic
->i_pts
= p_pic
->i_dts
;
871 else if( p_sys
->slice
.type
== H264_SLICE_TYPE_I
&&
872 date_Get( &p_sys
->dts
) != VLC_TS_INVALID
)
874 /* Hell no PTS on IDR. We're totally blind */
875 date_t pts
= p_sys
->dts
;
876 date_Increment( &pts
, 2 );
877 p_pic
->i_pts
= date_Get( &pts
);
881 if( p_pic
->i_pts
> VLC_TS_INVALID
)
883 p_sys
->prevdatedpoc
.pts
= p_pic
->i_pts
;
884 p_sys
->prevdatedpoc
.num
= PictureOrderCount
;
887 if( p_pic
->i_length
== 0 )
889 if( p_sps
->vui
.i_time_scale
)
891 p_pic
->i_length
= CLOCK_FREQ
* i_num_clock_ts
*
892 p_sps
->vui
.i_num_units_in_tick
/ p_sps
->vui
.i_time_scale
;
896 date_t next
= p_sys
->dts
;
897 date_Increment( &next
, i_num_clock_ts
);
898 p_pic
->i_length
= date_Get( &next
) - date_Get( &p_sys
->dts
);
903 msg_Err(p_dec
, "F/BOC %d/%d POC %d %d rec %d flags %x ref%d fn %d fp %d %d pts %ld len %ld",
904 tFOC
, bFOC
, PictureOrderCount
,
905 p_sys
->slice
.type
, p_sys
->b_recovered
, p_pic
->i_flags
,
906 p_sys
->slice
.i_nal_ref_idc
, p_sys
->slice
.i_frame_num
, p_sys
->slice
.i_field_pic_flag
,
907 p_pic
->i_pts
- p_pic
->i_dts
, p_pic
->i_pts
% (100*CLOCK_FREQ
), p_pic
->i_length
);
910 /* save for next pic fixups */
911 if( date_Get( &p_sys
->dts
) != VLC_TS_INVALID
)
913 if( p_sys
->i_next_block_flags
& BLOCK_FLAG_DISCONTINUITY
)
914 date_Set( &p_sys
->dts
, VLC_TS_INVALID
);
916 date_Increment( &p_sys
->dts
, i_num_clock_ts
);
921 p_pic
->i_flags
|= p_sys
->i_next_block_flags
;
922 p_sys
->i_next_block_flags
= 0;
925 switch( p_sys
->slice
.type
)
927 case H264_SLICE_TYPE_P
:
928 p_pic
->i_flags
|= BLOCK_FLAG_TYPE_P
;
930 case H264_SLICE_TYPE_B
:
931 p_pic
->i_flags
|= BLOCK_FLAG_TYPE_B
;
933 case H264_SLICE_TYPE_I
:
934 p_pic
->i_flags
|= BLOCK_FLAG_TYPE_I
;
939 if( !p_sys
->b_recovered
)
941 if( p_sys
->i_recoveryfnum
!= UINT_MAX
) /* recovering from SEI */
942 p_pic
->i_flags
|= BLOCK_FLAG_PREROLL
;
944 p_pic
->i_flags
|= BLOCK_FLAG_DROP
;
947 p_pic
->i_flags
&= ~BLOCK_FLAG_PRIVATE_AUD
;
949 /* reset after output */
950 p_sys
->i_frame_dts
= VLC_TS_INVALID
;
951 p_sys
->i_frame_pts
= VLC_TS_INVALID
;
952 p_sys
->i_dpb_output_delay
= 0;
953 p_sys
->i_pic_struct
= UINT8_MAX
;
954 p_sys
->i_recovery_frame_cnt
= UINT_MAX
;
955 p_sys
->slice
.type
= H264_SLICE_TYPE_UNKNOWN
;
957 p_sys
->pp_sei_last
= &p_sys
->p_sei
;
958 p_sys
->b_new_sps
= false;
959 p_sys
->b_new_pps
= false;
960 p_sys
->b_slice
= false;
963 cc_storage_commit( p_sys
->p_ccs
, p_pic
);
968 static void PutSPS( decoder_t
*p_dec
, block_t
*p_frag
)
970 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
972 const uint8_t *p_buffer
= p_frag
->p_buffer
;
973 size_t i_buffer
= p_frag
->i_buffer
;
975 if( !hxxx_strip_AnnexB_startcode( &p_buffer
, &i_buffer
) )
977 block_Release( p_frag
);
981 h264_sequence_parameter_set_t
*p_sps
= h264_decode_sps( p_buffer
, i_buffer
, true );
984 msg_Warn( p_dec
, "invalid SPS" );
985 block_Release( p_frag
);
989 /* We have a new SPS */
990 if( !p_sys
->sps
[p_sps
->i_id
].p_sps
)
991 msg_Dbg( p_dec
, "found NAL_SPS (sps_id=%d)", p_sps
->i_id
);
993 StoreSPS( p_sys
, p_sps
->i_id
, p_frag
, p_sps
);
996 static void PutPPS( decoder_t
*p_dec
, block_t
*p_frag
)
998 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
999 const uint8_t *p_buffer
= p_frag
->p_buffer
;
1000 size_t i_buffer
= p_frag
->i_buffer
;
1002 if( !hxxx_strip_AnnexB_startcode( &p_buffer
, &i_buffer
) )
1004 block_Release( p_frag
);
1008 h264_picture_parameter_set_t
*p_pps
= h264_decode_pps( p_buffer
, i_buffer
, true );
1011 msg_Warn( p_dec
, "invalid PPS" );
1012 block_Release( p_frag
);
1016 /* We have a new PPS */
1017 if( !p_sys
->pps
[p_pps
->i_id
].p_pps
)
1018 msg_Dbg( p_dec
, "found NAL_PPS (pps_id=%d sps_id=%d)", p_pps
->i_id
, p_pps
->i_sps_id
);
1020 StorePPS( p_sys
, p_pps
->i_id
, p_frag
, p_pps
);
1023 static void GetSPSPPS( uint8_t i_pps_id
, void *priv
,
1024 const h264_sequence_parameter_set_t
**pp_sps
,
1025 const h264_picture_parameter_set_t
**pp_pps
)
1027 decoder_sys_t
*p_sys
= priv
;
1029 *pp_pps
= p_sys
->pps
[i_pps_id
].p_pps
;
1030 if( *pp_pps
== NULL
)
1033 *pp_sps
= p_sys
->sps
[(*pp_pps
)->i_sps_id
].p_sps
;
1036 static bool ParseSliceHeader( decoder_t
*p_dec
, const block_t
*p_frag
, h264_slice_t
*p_slice
)
1038 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1040 const uint8_t *p_stripped
= p_frag
->p_buffer
;
1041 size_t i_stripped
= p_frag
->i_buffer
;
1043 if( !hxxx_strip_AnnexB_startcode( &p_stripped
, &i_stripped
) || i_stripped
< 2 )
1046 if( !h264_decode_slice( p_stripped
, i_stripped
, GetSPSPPS
, p_sys
, p_slice
) )
1049 const h264_sequence_parameter_set_t
*p_sps
;
1050 const h264_picture_parameter_set_t
*p_pps
;
1051 GetSPSPPS( p_slice
->i_pic_parameter_set_id
, p_sys
, &p_sps
, &p_pps
);
1052 if( unlikely( !p_sps
|| !p_pps
) )
1055 ActivateSets( p_dec
, p_sps
, p_pps
);
1060 static bool ParseSeiCallback( const hxxx_sei_data_t
*p_sei_data
, void *cbdata
)
1062 decoder_t
*p_dec
= (decoder_t
*) cbdata
;
1063 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1065 switch( p_sei_data
->i_type
)
1067 /* Look for pic timing */
1068 case HXXX_SEI_PIC_TIMING
:
1070 const h264_sequence_parameter_set_t
*p_sps
= p_sys
->p_active_sps
;
1071 if( unlikely( p_sps
== NULL
) )
1077 if( p_sps
->vui
.b_valid
)
1079 if( p_sps
->vui
.b_hrd_parameters_present_flag
)
1081 bs_read( p_sei_data
->p_bs
, p_sps
->vui
.i_cpb_removal_delay_length_minus1
+ 1 );
1082 p_sys
->i_dpb_output_delay
=
1083 bs_read( p_sei_data
->p_bs
, p_sps
->vui
.i_dpb_output_delay_length_minus1
+ 1 );
1086 if( p_sps
->vui
.b_pic_struct_present_flag
)
1087 p_sys
->i_pic_struct
= bs_read( p_sei_data
->p_bs
, 4 );
1088 /* + unparsed remains */
1092 /* Look for user_data_registered_itu_t_t35 */
1093 case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35
:
1095 if( p_sei_data
->itu_t35
.type
== HXXX_ITU_T35_TYPE_CC
)
1097 cc_storage_append( p_sys
->p_ccs
, true, p_sei_data
->itu_t35
.u
.cc
.p_data
,
1098 p_sei_data
->itu_t35
.u
.cc
.i_data
);
1102 /* Look for SEI recovery point */
1103 case HXXX_SEI_RECOVERY_POINT
:
1105 if( !p_sys
->b_recovered
)
1106 msg_Dbg( p_dec
, "Seen SEI recovery point, %d recovery frames", p_sei_data
->recovery
.i_frames
);
1107 p_sys
->i_recovery_frame_cnt
= p_sei_data
->recovery
.i_frames
;