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
;
85 /* a new sps/pps can be transmitted outside of iframes */
92 h264_sequence_parameter_set_t
*p_sps
;
93 } sps
[H264_SPS_ID_MAX
+ 1];
97 h264_picture_parameter_set_t
*p_pps
;
98 } pps
[H264_PPS_ID_MAX
+ 1];
99 const h264_sequence_parameter_set_t
*p_active_sps
;
100 const h264_picture_parameter_set_t
*p_active_pps
;
103 uint8_t i_avcC_length_size
;
105 /* From SEI for current frame */
106 uint8_t i_pic_struct
;
107 uint8_t i_dpb_output_delay
;
108 unsigned i_recovery_frame_cnt
;
110 /* Useful values of the Slice Header */
114 int i_next_block_flags
;
116 unsigned i_recoveryfnum
;
119 h264_poc_context_t pocctx
;
135 #define BLOCK_FLAG_PRIVATE_AUD (1 << BLOCK_FLAG_PRIVATE_SHIFT)
136 #define BLOCK_FLAG_PRIVATE_SEI (2 << BLOCK_FLAG_PRIVATE_SHIFT)
137 #define BLOCK_FLAG_DROP (4 << BLOCK_FLAG_PRIVATE_SHIFT)
139 static block_t
*Packetize( decoder_t
*, block_t
** );
140 static block_t
*PacketizeAVC1( decoder_t
*, block_t
** );
141 static block_t
*GetCc( decoder_t
*p_dec
, decoder_cc_desc_t
* );
142 static void PacketizeFlush( decoder_t
* );
144 static void PacketizeReset( void *p_private
, bool b_broken
);
145 static block_t
*PacketizeParse( void *p_private
, bool *pb_ts_used
, block_t
* );
146 static int PacketizeValidate( void *p_private
, block_t
* );
148 static block_t
*ParseNALBlock( decoder_t
*, bool *pb_ts_used
, block_t
* );
150 static block_t
*OutputPicture( decoder_t
*p_dec
);
151 static void PutSPS( decoder_t
*p_dec
, block_t
*p_frag
);
152 static void PutPPS( decoder_t
*p_dec
, block_t
*p_frag
);
153 static bool ParseSliceHeader( decoder_t
*p_dec
, const block_t
*p_frag
, h264_slice_t
*p_slice
);
154 static bool ParseSeiCallback( const hxxx_sei_data_t
*, void * );
157 static const uint8_t p_h264_startcode
[3] = { 0x00, 0x00, 0x01 };
159 /*****************************************************************************
161 *****************************************************************************/
163 static void StoreSPS( decoder_sys_t
*p_sys
, uint8_t i_id
,
164 block_t
*p_block
, h264_sequence_parameter_set_t
*p_sps
)
166 if( p_sys
->sps
[i_id
].p_block
)
167 block_Release( p_sys
->sps
[i_id
].p_block
);
168 if( p_sys
->sps
[i_id
].p_sps
)
169 h264_release_sps( p_sys
->sps
[i_id
].p_sps
);
170 if( p_sys
->sps
[i_id
].p_sps
== p_sys
->p_active_sps
)
171 p_sys
->p_active_sps
= NULL
;
172 p_sys
->sps
[i_id
].p_block
= p_block
;
173 p_sys
->sps
[i_id
].p_sps
= p_sps
;
176 static void StorePPS( decoder_sys_t
*p_sys
, uint8_t i_id
,
177 block_t
*p_block
, h264_picture_parameter_set_t
*p_pps
)
179 if( p_sys
->pps
[i_id
].p_block
)
180 block_Release( p_sys
->pps
[i_id
].p_block
);
181 if( p_sys
->pps
[i_id
].p_pps
)
182 h264_release_pps( p_sys
->pps
[i_id
].p_pps
);
183 if( p_sys
->pps
[i_id
].p_pps
== p_sys
->p_active_pps
)
184 p_sys
->p_active_pps
= NULL
;
185 p_sys
->pps
[i_id
].p_block
= p_block
;
186 p_sys
->pps
[i_id
].p_pps
= p_pps
;
189 static void ActivateSets( decoder_t
*p_dec
, const h264_sequence_parameter_set_t
*p_sps
,
190 const h264_picture_parameter_set_t
*p_pps
)
192 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
194 p_sys
->p_active_pps
= p_pps
;
195 p_sys
->p_active_sps
= p_sps
;
199 p_dec
->fmt_out
.i_profile
= p_sps
->i_profile
;
200 p_dec
->fmt_out
.i_level
= p_sps
->i_level
;
202 (void) h264_get_picture_size( p_sps
, &p_dec
->fmt_out
.video
.i_width
,
203 &p_dec
->fmt_out
.video
.i_height
,
204 &p_dec
->fmt_out
.video
.i_visible_width
,
205 &p_dec
->fmt_out
.video
.i_visible_height
);
207 if( p_sps
->vui
.i_sar_num
!= 0 && p_sps
->vui
.i_sar_den
!= 0 )
209 p_dec
->fmt_out
.video
.i_sar_num
= p_sps
->vui
.i_sar_num
;
210 p_dec
->fmt_out
.video
.i_sar_den
= p_sps
->vui
.i_sar_den
;
213 if( p_sps
->vui
.b_valid
)
215 if( !p_dec
->fmt_in
.video
.i_frame_rate_base
&&
216 p_sps
->vui
.i_num_units_in_tick
> 0 && p_sps
->vui
.i_time_scale
> 1 )
218 const unsigned i_rate_base
= p_sps
->vui
.i_num_units_in_tick
;
219 const unsigned i_rate
= p_sps
->vui
.i_time_scale
>> 1; /* num_clock_ts == 2 */
220 if( i_rate_base
!= p_dec
->fmt_out
.video
.i_frame_rate_base
||
221 i_rate
!= p_dec
->fmt_out
.video
.i_frame_rate
)
223 p_dec
->fmt_out
.video
.i_frame_rate_base
= i_rate_base
;
224 p_dec
->fmt_out
.video
.i_frame_rate
= i_rate
;
225 date_Change( &p_sys
->dts
, p_sps
->vui
.i_time_scale
, p_sps
->vui
.i_num_units_in_tick
);
228 if( p_dec
->fmt_in
.video
.primaries
== COLOR_PRIMARIES_UNDEF
)
229 h264_get_colorimetry( p_sps
, &p_dec
->fmt_out
.video
.primaries
,
230 &p_dec
->fmt_out
.video
.transfer
,
231 &p_dec
->fmt_out
.video
.space
,
232 &p_dec
->fmt_out
.video
.b_color_range_full
);
237 static bool IsFirstVCLNALUnit( const h264_slice_t
*p_prev
, const h264_slice_t
*p_cur
)
239 /* Detection of the first VCL NAL unit of a primary coded picture
241 if( p_cur
->i_frame_num
!= p_prev
->i_frame_num
||
242 p_cur
->i_pic_parameter_set_id
!= p_prev
->i_pic_parameter_set_id
||
243 p_cur
->i_field_pic_flag
!= p_prev
->i_field_pic_flag
||
244 !p_cur
->i_nal_ref_idc
!= !p_prev
->i_nal_ref_idc
)
246 if( (p_cur
->i_bottom_field_flag
!= -1) &&
247 (p_prev
->i_bottom_field_flag
!= -1) &&
248 (p_cur
->i_bottom_field_flag
!= p_prev
->i_bottom_field_flag
) )
250 if( p_cur
->i_pic_order_cnt_type
== 0 &&
251 ( p_cur
->i_pic_order_cnt_lsb
!= p_prev
->i_pic_order_cnt_lsb
||
252 p_cur
->i_delta_pic_order_cnt_bottom
!= p_prev
->i_delta_pic_order_cnt_bottom
) )
254 else if( p_cur
->i_pic_order_cnt_type
== 1 &&
255 ( p_cur
->i_delta_pic_order_cnt0
!= p_prev
->i_delta_pic_order_cnt0
||
256 p_cur
->i_delta_pic_order_cnt1
!= p_prev
->i_delta_pic_order_cnt1
) )
258 if( ( p_cur
->i_nal_type
== H264_NAL_SLICE_IDR
|| p_prev
->i_nal_type
== H264_NAL_SLICE_IDR
) &&
259 ( p_cur
->i_nal_type
!= p_prev
->i_nal_type
|| p_cur
->i_idr_pic_id
!= p_prev
->i_idr_pic_id
) )
265 static void DropStoredNAL( decoder_sys_t
*p_sys
)
267 block_ChainRelease( p_sys
->frame
.p_head
);
268 block_ChainRelease( p_sys
->leading
.p_head
);
269 p_sys
->frame
.p_head
= NULL
;
270 p_sys
->frame
.pp_append
= &p_sys
->frame
.p_head
;
271 p_sys
->leading
.p_head
= NULL
;
272 p_sys
->leading
.pp_append
= &p_sys
->leading
.p_head
;
275 /*****************************************************************************
276 * Open: probe the packetizer and return score
277 * When opening after demux, the packetizer is only loaded AFTER the decoder
278 * That means that what you set in fmt_out is ignored by the decoder in this special case
279 *****************************************************************************/
280 static int Open( vlc_object_t
*p_this
)
282 decoder_t
*p_dec
= (decoder_t
*)p_this
;
283 decoder_sys_t
*p_sys
;
286 const bool b_avc
= (p_dec
->fmt_in
.i_original_fourcc
== VLC_FOURCC( 'a', 'v', 'c', '1' ));
288 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_H264
)
290 if( b_avc
&& p_dec
->fmt_in
.i_extra
< 7 )
293 /* Allocate the memory needed to store the decoder's structure */
294 if( ( p_dec
->p_sys
= p_sys
= malloc( sizeof(decoder_sys_t
) ) ) == NULL
)
299 p_sys
->p_ccs
= cc_storage_new();
300 if( unlikely(!p_sys
->p_ccs
) )
302 free( p_dec
->p_sys
);
306 packetizer_Init( &p_sys
->packetizer
,
307 p_h264_startcode
, sizeof(p_h264_startcode
), startcode_FindAnnexB
,
308 p_h264_startcode
, 1, 5,
309 PacketizeReset
, PacketizeParse
, PacketizeValidate
, p_dec
);
311 p_sys
->b_slice
= false;
312 p_sys
->frame
.p_head
= NULL
;
313 p_sys
->frame
.pp_append
= &p_sys
->frame
.p_head
;
314 p_sys
->leading
.p_head
= NULL
;
315 p_sys
->leading
.pp_append
= &p_sys
->leading
.p_head
;
316 p_sys
->b_new_sps
= false;
317 p_sys
->b_new_pps
= false;
319 for( i
= 0; i
<= H264_SPS_ID_MAX
; i
++ )
321 p_sys
->sps
[i
].p_sps
= NULL
;
322 p_sys
->sps
[i
].p_block
= NULL
;
324 p_sys
->p_active_sps
= NULL
;
325 for( i
= 0; i
<= H264_PPS_ID_MAX
; i
++ )
327 p_sys
->pps
[i
].p_pps
= NULL
;
328 p_sys
->pps
[i
].p_block
= NULL
;
330 p_sys
->p_active_pps
= NULL
;
331 p_sys
->i_recovery_frame_cnt
= UINT_MAX
;
333 h264_slice_init( &p_sys
->slice
);
335 p_sys
->i_next_block_flags
= 0;
336 p_sys
->b_recovered
= false;
337 p_sys
->i_recoveryfnum
= UINT_MAX
;
338 p_sys
->i_frame_dts
= VLC_TS_INVALID
;
339 p_sys
->i_frame_pts
= VLC_TS_INVALID
;
340 p_sys
->i_dpb_output_delay
= 0;
343 h264_poc_context_init( &p_sys
->pocctx
);
344 p_sys
->prevdatedpoc
.pts
= VLC_TS_INVALID
;
346 date_Init( &p_sys
->dts
, 30000 * 2, 1001 );
347 date_Set( &p_sys
->dts
, VLC_TS_INVALID
);
349 /* Setup properties */
350 es_format_Copy( &p_dec
->fmt_out
, &p_dec
->fmt_in
);
351 p_dec
->fmt_out
.i_codec
= VLC_CODEC_H264
;
352 p_dec
->fmt_out
.b_packetized
= true;
354 if( p_dec
->fmt_in
.video
.i_frame_rate_base
&&
355 p_dec
->fmt_in
.video
.i_frame_rate
&&
356 p_dec
->fmt_in
.video
.i_frame_rate
<= UINT_MAX
/ 2 )
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
, decoder_cc_desc_t
*p_desc
)
494 return cc_storage_get_current( p_dec
->p_sys
->p_ccs
, p_desc
);
497 /****************************************************************************
499 ****************************************************************************/
500 static void ResetOutputVariables( decoder_sys_t
*p_sys
)
502 p_sys
->i_frame_dts
= VLC_TS_INVALID
;
503 p_sys
->i_frame_pts
= VLC_TS_INVALID
;
504 p_sys
->slice
.type
= H264_SLICE_TYPE_UNKNOWN
;
505 p_sys
->b_new_sps
= false;
506 p_sys
->b_new_pps
= false;
507 p_sys
->b_slice
= false;
509 p_sys
->i_dpb_output_delay
= 0;
510 p_sys
->i_pic_struct
= UINT8_MAX
;
511 p_sys
->i_recovery_frame_cnt
= UINT_MAX
;
514 static void PacketizeReset( void *p_private
, bool b_broken
)
516 decoder_t
*p_dec
= p_private
;
517 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
519 if( b_broken
|| !p_sys
->b_slice
)
521 DropStoredNAL( p_sys
);
522 ResetOutputVariables( p_sys
);
523 p_sys
->p_active_pps
= NULL
;
524 p_sys
->p_active_sps
= NULL
;
526 h264_poc_context_init( &p_sys
->pocctx
);
527 p_sys
->prevdatedpoc
.pts
= VLC_TS_INVALID
;
529 p_sys
->i_next_block_flags
= BLOCK_FLAG_DISCONTINUITY
;
530 p_sys
->b_recovered
= false;
531 p_sys
->i_recoveryfnum
= UINT_MAX
;
532 date_Set( &p_sys
->dts
, VLC_TS_INVALID
);
534 static block_t
*PacketizeParse( void *p_private
, bool *pb_ts_used
, block_t
*p_block
)
536 decoder_t
*p_dec
= p_private
;
538 /* Remove trailing 0 bytes */
539 while( p_block
->i_buffer
> 5 && p_block
->p_buffer
[p_block
->i_buffer
-1] == 0x00 )
542 return ParseNALBlock( p_dec
, pb_ts_used
, p_block
);
544 static int PacketizeValidate( void *p_private
, block_t
*p_au
)
546 VLC_UNUSED(p_private
);
551 /*****************************************************************************
552 * ParseNALBlock: parses annexB type NALs
553 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
554 *****************************************************************************/
555 static block_t
*ParseNALBlock( decoder_t
*p_dec
, bool *pb_ts_used
, block_t
*p_frag
)
557 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
558 block_t
*p_pic
= NULL
;
560 const int i_nal_type
= p_frag
->p_buffer
[4]&0x1f;
561 const mtime_t i_frag_dts
= p_frag
->i_dts
;
562 const mtime_t i_frag_pts
= p_frag
->i_pts
;
564 if( p_sys
->b_slice
&& (!p_sys
->p_active_pps
|| !p_sys
->p_active_sps
) )
566 msg_Warn( p_dec
, "waiting for SPS/PPS" );
569 DropStoredNAL( p_sys
);
570 ResetOutputVariables( p_sys
);
571 cc_storage_reset( p_sys
->p_ccs
);
578 case H264_NAL_SLICE_DPA
:
579 case H264_NAL_SLICE_DPB
:
580 case H264_NAL_SLICE_DPC
:
581 case H264_NAL_SLICE_IDR
:
583 h264_slice_t newslice
;
585 if( i_nal_type
== H264_NAL_SLICE_IDR
)
587 p_sys
->b_recovered
= true;
588 p_sys
->i_recovery_frame_cnt
= UINT_MAX
;
589 p_sys
->i_recoveryfnum
= UINT_MAX
;
592 if( ParseSliceHeader( p_dec
, p_frag
, &newslice
) )
594 /* Only IDR carries the id, to be propagated */
595 if( newslice
.i_idr_pic_id
== -1 )
596 newslice
.i_idr_pic_id
= p_sys
->slice
.i_idr_pic_id
;
598 bool b_new_picture
= IsFirstVCLNALUnit( &p_sys
->slice
, &newslice
);
601 /* Parse SEI for that frame now we should have matched SPS/PPS */
602 for( block_t
*p_sei
= p_sys
->leading
.p_head
; p_sei
; p_sei
= p_sei
->p_next
)
604 if( (p_sei
->i_flags
& BLOCK_FLAG_PRIVATE_SEI
) == 0 )
606 HxxxParse_AnnexB_SEI( p_sei
->p_buffer
, p_sei
->i_buffer
,
607 1 /* nal header */, ParseSeiCallback
, p_dec
);
611 p_pic
= OutputPicture( p_dec
);
615 p_sys
->slice
= newslice
;
619 p_sys
->p_active_pps
= NULL
;
620 /* Fragment will be discarded later on */
622 p_sys
->b_slice
= true;
624 block_ChainLastAppend( &p_sys
->frame
.pp_append
, p_frag
);
627 /*** Prefix NALs ***/
629 case H264_NAL_AU_DELIMITER
:
631 p_pic
= OutputPicture( p_dec
);
633 /* clear junk if no pic, we're always the first nal */
634 DropStoredNAL( p_sys
);
636 p_frag
->i_flags
|= BLOCK_FLAG_PRIVATE_AUD
;
638 block_ChainLastAppend( &p_sys
->leading
.pp_append
, p_frag
);
644 p_pic
= OutputPicture( p_dec
);
646 /* Stored for insert on keyframes */
647 if( i_nal_type
== H264_NAL_SPS
)
649 PutSPS( p_dec
, p_frag
);
650 p_sys
->b_new_sps
= true;
654 PutPPS( p_dec
, p_frag
);
655 p_sys
->b_new_pps
= true;
661 p_pic
= OutputPicture( p_dec
);
663 p_frag
->i_flags
|= BLOCK_FLAG_PRIVATE_SEI
;
664 block_ChainLastAppend( &p_sys
->leading
.pp_append
, p_frag
);
667 case H264_NAL_SPS_EXT
:
668 case H264_NAL_PREFIX
: /* first slice/VCL associated data */
669 case H264_NAL_SUBSET_SPS
:
670 case H264_NAL_DEPTH_PS
:
671 case H264_NAL_RESERVED_17
:
672 case H264_NAL_RESERVED_18
:
674 p_pic
= OutputPicture( p_dec
);
676 block_ChainLastAppend( &p_sys
->leading
.pp_append
, p_frag
);
679 /*** Suffix NALs ***/
681 case H264_NAL_END_OF_SEQ
:
682 case H264_NAL_END_OF_STREAM
:
683 /* Early end of packetization */
684 block_ChainLastAppend( &p_sys
->frame
.pp_append
, p_frag
);
686 /* important for still pictures/menus */
687 p_sys
->i_next_block_flags
|= BLOCK_FLAG_END_OF_SEQUENCE
;
689 p_pic
= OutputPicture( p_dec
);
692 case H264_NAL_SLICE_WP
: // post
693 case H264_NAL_UNKNOWN
:
694 case H264_NAL_FILLER_DATA
:
695 case H264_NAL_SLICE_EXT
:
696 case H264_NAL_SLICE_3D_EXT
:
697 case H264_NAL_RESERVED_22
:
698 case H264_NAL_RESERVED_23
:
699 default: /* others 24..31, including unknown */
700 block_ChainLastAppend( &p_sys
->frame
.pp_append
, p_frag
);
705 if( p_sys
->i_frame_dts
<= VLC_TS_INVALID
&&
706 p_sys
->i_frame_pts
<= VLC_TS_INVALID
)
708 p_sys
->i_frame_dts
= i_frag_dts
;
709 p_sys
->i_frame_pts
= i_frag_pts
;
711 if( i_frag_dts
> VLC_TS_INVALID
)
712 date_Set( &p_sys
->dts
, i_frag_dts
);
715 if( p_pic
&& (p_pic
->i_flags
& BLOCK_FLAG_DROP
) )
717 block_Release( p_pic
);
724 static block_t
*OutputPicture( decoder_t
*p_dec
)
726 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
727 block_t
*p_pic
= NULL
;
728 block_t
**pp_pic_last
= &p_pic
;
730 if( unlikely(!p_sys
->frame
.p_head
) )
732 assert( p_sys
->frame
.p_head
);
733 DropStoredNAL( p_sys
);
734 ResetOutputVariables( p_sys
);
735 cc_storage_reset( p_sys
->p_ccs
);
739 /* Bind matched/referred PPS and SPS */
740 const h264_picture_parameter_set_t
*p_pps
= p_sys
->p_active_pps
;
741 const h264_sequence_parameter_set_t
*p_sps
= p_sys
->p_active_sps
;
742 if( !p_pps
|| !p_sps
)
744 DropStoredNAL( p_sys
);
745 ResetOutputVariables( p_sys
);
746 cc_storage_reset( p_sys
->p_ccs
);
750 if( !p_sys
->b_recovered
&& p_sys
->i_recoveryfnum
== UINT_MAX
&&
751 p_sys
->i_recovery_frame_cnt
== UINT_MAX
&& p_sys
->slice
.type
== H264_SLICE_TYPE_I
)
753 /* No way to recover using SEI, just sync on I Slice */
754 p_sys
->b_recovered
= true;
757 bool b_need_sps_pps
= p_sys
->slice
.type
== H264_SLICE_TYPE_I
&&
758 p_sys
->p_active_pps
&& p_sys
->p_active_sps
;
760 /* Handle SEI recovery */
761 if ( !p_sys
->b_recovered
&& p_sys
->i_recovery_frame_cnt
!= UINT_MAX
&&
762 p_sys
->i_recoveryfnum
== UINT_MAX
)
764 p_sys
->i_recoveryfnum
= p_sys
->slice
.i_frame_num
+ p_sys
->i_recovery_frame_cnt
;
765 b_need_sps_pps
= true; /* SPS/PPS must be inserted for SEI recovery */
766 msg_Dbg( p_dec
, "Recovering using SEI, prerolling %u reference pics", p_sys
->i_recovery_frame_cnt
);
769 if( p_sys
->i_recoveryfnum
!= UINT_MAX
)
771 assert(p_sys
->b_recovered
== false);
772 const unsigned maxFrameNum
= 1 << (p_sps
->i_log2_max_frame_num
+ 4);
773 if( (p_sys
->i_recoveryfnum
> maxFrameNum
&&
774 (unsigned)p_sys
->slice
.i_frame_num
<= maxFrameNum
/ 2 &&
775 (unsigned)p_sys
->slice
.i_frame_num
>= p_sys
->i_recoveryfnum
% maxFrameNum
) ||
776 (unsigned)p_sys
->slice
.i_frame_num
>= p_sys
->i_recoveryfnum
)
778 p_sys
->i_recoveryfnum
= UINT_MAX
;
779 p_sys
->b_recovered
= true;
780 msg_Dbg( p_dec
, "Recovery from SEI recovery point complete" );
784 /* Gather PPS/SPS if required */
785 block_t
*p_xpsnal
= NULL
;
786 block_t
**pp_xpsnal_tail
= &p_xpsnal
;
787 if( b_need_sps_pps
|| p_sys
->b_new_sps
|| p_sys
->b_new_pps
)
789 for( int i
= 0; i
<= H264_SPS_ID_MAX
&& (b_need_sps_pps
|| p_sys
->b_new_sps
); i
++ )
791 if( p_sys
->sps
[i
].p_block
)
792 block_ChainLastAppend( &pp_xpsnal_tail
, block_Duplicate( p_sys
->sps
[i
].p_block
) );
794 for( int i
= 0; i
< H264_PPS_ID_MAX
&& (b_need_sps_pps
|| p_sys
->b_new_pps
); i
++ )
796 if( p_sys
->pps
[i
].p_block
)
797 block_ChainLastAppend( &pp_xpsnal_tail
, block_Duplicate( p_sys
->pps
[i
].p_block
) );
801 /* Now rebuild NAL Sequence, inserting PPS/SPS if any */
802 if( p_sys
->frame
.p_head
->i_flags
& BLOCK_FLAG_PRIVATE_AUD
)
804 block_t
*p_au
= p_sys
->frame
.p_head
;
805 p_sys
->frame
.p_head
= p_au
->p_next
;
807 block_ChainLastAppend( &pp_pic_last
, p_au
);
811 block_ChainLastAppend( &pp_pic_last
, p_xpsnal
);
813 if( p_sys
->leading
.p_head
)
814 block_ChainLastAppend( &pp_pic_last
, p_sys
->leading
.p_head
);
816 assert( p_sys
->frame
.p_head
);
817 if( p_sys
->frame
.p_head
)
818 block_ChainLastAppend( &pp_pic_last
, p_sys
->frame
.p_head
);
820 /* Reset chains, now empty */
821 p_sys
->frame
.p_head
= NULL
;
822 p_sys
->frame
.pp_append
= &p_sys
->frame
.p_head
;
823 p_sys
->leading
.p_head
= NULL
;
824 p_sys
->leading
.pp_append
= &p_sys
->leading
.p_head
;
826 p_pic
= block_ChainGather( p_pic
);
830 ResetOutputVariables( p_sys
);
831 cc_storage_reset( p_sys
->p_ccs
);
835 /* clear up flags gathered */
836 p_pic
->i_flags
&= ~BLOCK_FLAG_PRIVATE_MASK
;
838 /* for PTS Fixup, interlaced fields (multiple AU/block) */
839 int tFOC
= 0, bFOC
= 0, PictureOrderCount
= 0;
840 h264_compute_poc( p_sps
, &p_sys
->slice
, &p_sys
->pocctx
, &PictureOrderCount
, &tFOC
, &bFOC
);
842 unsigned i_num_clock_ts
= h264_get_num_ts( p_sps
, &p_sys
->slice
, p_sys
->i_pic_struct
, tFOC
, bFOC
);
844 if( p_sps
->frame_mbs_only_flag
== 0 && p_sps
->vui
.b_pic_struct_present_flag
)
846 switch( p_sys
->i_pic_struct
)
848 /* Top and Bottom field slices */
851 p_pic
->i_flags
|= BLOCK_FLAG_SINGLE_FIELD
;
852 p_pic
->i_flags
|= (!p_sys
->slice
.i_bottom_field_flag
) ? BLOCK_FLAG_TOP_FIELD_FIRST
853 : BLOCK_FLAG_BOTTOM_FIELD_FIRST
;
855 /* Each of the following slices contains multiple fields */
857 p_pic
->i_flags
|= BLOCK_FLAG_TOP_FIELD_FIRST
;
860 p_pic
->i_flags
|= BLOCK_FLAG_BOTTOM_FIELD_FIRST
;
863 p_pic
->i_flags
|= BLOCK_FLAG_TOP_FIELD_FIRST
;
866 p_pic
->i_flags
|= BLOCK_FLAG_BOTTOM_FIELD_FIRST
;
873 /* set dts/pts to current block timestamps */
874 p_pic
->i_dts
= p_sys
->i_frame_dts
;
875 p_pic
->i_pts
= p_sys
->i_frame_pts
;
877 /* Fixup missing timestamps after split (multiple AU/block)*/
878 if( p_pic
->i_dts
<= VLC_TS_INVALID
)
879 p_pic
->i_dts
= date_Get( &p_sys
->dts
);
881 if( p_sys
->slice
.type
== H264_SLICE_TYPE_I
)
882 p_sys
->prevdatedpoc
.pts
= VLC_TS_INVALID
;
884 if( p_pic
->i_pts
== VLC_TS_INVALID
)
886 if( p_sys
->prevdatedpoc
.pts
> VLC_TS_INVALID
&&
887 date_Get( &p_sys
->dts
) != VLC_TS_INVALID
)
889 date_t pts
= p_sys
->dts
;
890 date_Set( &pts
, p_sys
->prevdatedpoc
.pts
);
892 int diff
= tFOC
- p_sys
->prevdatedpoc
.num
;
894 date_Increment( &pts
, diff
);
896 date_Decrement( &pts
, -diff
);
898 p_pic
->i_pts
= date_Get( &pts
);
900 /* In case there's no PTS at all */
901 else if( p_sys
->slice
.i_nal_ref_idc
== 0 &&
902 p_sys
->slice
.type
== H264_SLICE_TYPE_B
)
904 p_pic
->i_pts
= p_pic
->i_dts
;
906 else if( p_sys
->slice
.type
== H264_SLICE_TYPE_I
&&
907 date_Get( &p_sys
->dts
) != VLC_TS_INVALID
)
909 /* Hell no PTS on IDR. We're totally blind */
910 date_t pts
= p_sys
->dts
;
911 date_Increment( &pts
, 2 );
912 p_pic
->i_pts
= date_Get( &pts
);
916 if( p_pic
->i_pts
> VLC_TS_INVALID
)
918 p_sys
->prevdatedpoc
.pts
= p_pic
->i_pts
;
919 p_sys
->prevdatedpoc
.num
= PictureOrderCount
;
922 if( p_pic
->i_length
== 0 )
924 if( p_sps
->vui
.i_time_scale
)
926 p_pic
->i_length
= CLOCK_FREQ
* i_num_clock_ts
*
927 p_sps
->vui
.i_num_units_in_tick
/ p_sps
->vui
.i_time_scale
;
931 date_t next
= p_sys
->dts
;
932 date_Increment( &next
, i_num_clock_ts
);
933 p_pic
->i_length
= date_Get( &next
) - date_Get( &p_sys
->dts
);
938 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",
939 tFOC
, bFOC
, PictureOrderCount
,
940 p_sys
->slice
.type
, p_sys
->b_recovered
, p_pic
->i_flags
,
941 p_sys
->slice
.i_nal_ref_idc
, p_sys
->slice
.i_frame_num
, p_sys
->slice
.i_field_pic_flag
,
942 p_pic
->i_pts
- p_pic
->i_dts
, p_pic
->i_pts
% (100*CLOCK_FREQ
), p_pic
->i_length
);
945 /* save for next pic fixups */
946 if( date_Get( &p_sys
->dts
) != VLC_TS_INVALID
)
948 if( p_sys
->i_next_block_flags
& BLOCK_FLAG_DISCONTINUITY
)
949 date_Set( &p_sys
->dts
, VLC_TS_INVALID
);
951 date_Increment( &p_sys
->dts
, i_num_clock_ts
);
956 p_pic
->i_flags
|= p_sys
->i_next_block_flags
;
957 p_sys
->i_next_block_flags
= 0;
960 switch( p_sys
->slice
.type
)
962 case H264_SLICE_TYPE_P
:
963 p_pic
->i_flags
|= BLOCK_FLAG_TYPE_P
;
965 case H264_SLICE_TYPE_B
:
966 p_pic
->i_flags
|= BLOCK_FLAG_TYPE_B
;
968 case H264_SLICE_TYPE_I
:
969 p_pic
->i_flags
|= BLOCK_FLAG_TYPE_I
;
974 if( !p_sys
->b_recovered
)
976 if( p_sys
->i_recoveryfnum
!= UINT_MAX
) /* recovering from SEI */
977 p_pic
->i_flags
|= BLOCK_FLAG_PREROLL
;
979 p_pic
->i_flags
|= BLOCK_FLAG_DROP
;
982 p_pic
->i_flags
&= ~BLOCK_FLAG_PRIVATE_AUD
;
984 /* reset after output */
985 ResetOutputVariables( p_sys
);
988 cc_storage_commit( p_sys
->p_ccs
, p_pic
);
993 static void PutSPS( decoder_t
*p_dec
, block_t
*p_frag
)
995 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
997 const uint8_t *p_buffer
= p_frag
->p_buffer
;
998 size_t i_buffer
= p_frag
->i_buffer
;
1000 if( !hxxx_strip_AnnexB_startcode( &p_buffer
, &i_buffer
) )
1002 block_Release( p_frag
);
1006 h264_sequence_parameter_set_t
*p_sps
= h264_decode_sps( p_buffer
, i_buffer
, true );
1009 msg_Warn( p_dec
, "invalid SPS" );
1010 block_Release( p_frag
);
1014 /* We have a new SPS */
1015 if( !p_sys
->sps
[p_sps
->i_id
].p_sps
)
1016 msg_Dbg( p_dec
, "found NAL_SPS (sps_id=%d)", p_sps
->i_id
);
1018 StoreSPS( p_sys
, p_sps
->i_id
, p_frag
, p_sps
);
1021 static void PutPPS( decoder_t
*p_dec
, block_t
*p_frag
)
1023 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1024 const uint8_t *p_buffer
= p_frag
->p_buffer
;
1025 size_t i_buffer
= p_frag
->i_buffer
;
1027 if( !hxxx_strip_AnnexB_startcode( &p_buffer
, &i_buffer
) )
1029 block_Release( p_frag
);
1033 h264_picture_parameter_set_t
*p_pps
= h264_decode_pps( p_buffer
, i_buffer
, true );
1036 msg_Warn( p_dec
, "invalid PPS" );
1037 block_Release( p_frag
);
1041 /* We have a new PPS */
1042 if( !p_sys
->pps
[p_pps
->i_id
].p_pps
)
1043 msg_Dbg( p_dec
, "found NAL_PPS (pps_id=%d sps_id=%d)", p_pps
->i_id
, p_pps
->i_sps_id
);
1045 StorePPS( p_sys
, p_pps
->i_id
, p_frag
, p_pps
);
1048 static void GetSPSPPS( uint8_t i_pps_id
, void *priv
,
1049 const h264_sequence_parameter_set_t
**pp_sps
,
1050 const h264_picture_parameter_set_t
**pp_pps
)
1052 decoder_sys_t
*p_sys
= priv
;
1054 *pp_pps
= p_sys
->pps
[i_pps_id
].p_pps
;
1055 if( *pp_pps
== NULL
)
1058 *pp_sps
= p_sys
->sps
[(*pp_pps
)->i_sps_id
].p_sps
;
1061 static bool ParseSliceHeader( decoder_t
*p_dec
, const block_t
*p_frag
, h264_slice_t
*p_slice
)
1063 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1065 const uint8_t *p_stripped
= p_frag
->p_buffer
;
1066 size_t i_stripped
= p_frag
->i_buffer
;
1068 if( !hxxx_strip_AnnexB_startcode( &p_stripped
, &i_stripped
) || i_stripped
< 2 )
1071 if( !h264_decode_slice( p_stripped
, i_stripped
, GetSPSPPS
, p_sys
, p_slice
) )
1074 const h264_sequence_parameter_set_t
*p_sps
;
1075 const h264_picture_parameter_set_t
*p_pps
;
1076 GetSPSPPS( p_slice
->i_pic_parameter_set_id
, p_sys
, &p_sps
, &p_pps
);
1077 if( unlikely( !p_sps
|| !p_pps
) )
1080 ActivateSets( p_dec
, p_sps
, p_pps
);
1085 static bool ParseSeiCallback( const hxxx_sei_data_t
*p_sei_data
, void *cbdata
)
1087 decoder_t
*p_dec
= (decoder_t
*) cbdata
;
1088 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
1090 switch( p_sei_data
->i_type
)
1092 /* Look for pic timing */
1093 case HXXX_SEI_PIC_TIMING
:
1095 const h264_sequence_parameter_set_t
*p_sps
= p_sys
->p_active_sps
;
1096 if( unlikely( p_sps
== NULL
) )
1102 if( p_sps
->vui
.b_valid
)
1104 if( p_sps
->vui
.b_hrd_parameters_present_flag
)
1106 bs_read( p_sei_data
->p_bs
, p_sps
->vui
.i_cpb_removal_delay_length_minus1
+ 1 );
1107 p_sys
->i_dpb_output_delay
=
1108 bs_read( p_sei_data
->p_bs
, p_sps
->vui
.i_dpb_output_delay_length_minus1
+ 1 );
1111 if( p_sps
->vui
.b_pic_struct_present_flag
)
1112 p_sys
->i_pic_struct
= bs_read( p_sei_data
->p_bs
, 4 );
1113 /* + unparsed remains */
1117 /* Look for user_data_registered_itu_t_t35 */
1118 case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35
:
1120 if( p_sei_data
->itu_t35
.type
== HXXX_ITU_T35_TYPE_CC
)
1122 cc_storage_append( p_sys
->p_ccs
, true, p_sei_data
->itu_t35
.u
.cc
.p_data
,
1123 p_sei_data
->itu_t35
.u
.cc
.i_data
);
1127 case HXXX_SEI_FRAME_PACKING_ARRANGEMENT
:
1129 if( p_dec
->fmt_in
.video
.multiview_mode
== MULTIVIEW_2D
)
1131 video_multiview_mode_t mode
;
1132 switch( p_sei_data
->frame_packing
.type
)
1134 case FRAME_PACKING_INTERLEAVED_CHECKERBOARD
:
1135 mode
= MULTIVIEW_STEREO_CHECKERBOARD
; break;
1136 case FRAME_PACKING_INTERLEAVED_COLUMN
:
1137 mode
= MULTIVIEW_STEREO_COL
; break;
1138 case FRAME_PACKING_INTERLEAVED_ROW
:
1139 mode
= MULTIVIEW_STEREO_ROW
; break;
1140 case FRAME_PACKING_SIDE_BY_SIDE
:
1141 mode
= MULTIVIEW_STEREO_SBS
; break;
1142 case FRAME_PACKING_TOP_BOTTOM
:
1143 mode
= MULTIVIEW_STEREO_TB
; break;
1144 case FRAME_PACKING_TEMPORAL
:
1145 mode
= MULTIVIEW_STEREO_FRAME
; break;
1146 case FRAME_PACKING_TILED
:
1148 mode
= MULTIVIEW_2D
; break;
1150 p_dec
->fmt_out
.video
.multiview_mode
= mode
;
1154 /* Look for SEI recovery point */
1155 case HXXX_SEI_RECOVERY_POINT
:
1157 if( !p_sys
->b_recovered
)
1158 msg_Dbg( p_dec
, "Seen SEI recovery point, %d recovery frames", p_sei_data
->recovery
.i_frames
);
1159 p_sys
->i_recovery_frame_cnt
= p_sei_data
->recovery
.i_frames
;