1 /*****************************************************************************
2 * hevc.c: h.265/hevc video packetizer
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
7 * Authors: Denis Charmet <typx@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35 #include <vlc_block.h>
38 #include <vlc_block_helper.h>
39 #include "packetizer_helper.h"
40 #include "startcode_helper.h"
44 #include "hxxx_common.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_("HEVC/H.265 video packetizer"))
58 set_capability("packetizer", 50)
59 set_callbacks(Open
, Close
)
63 /****************************************************************************
65 ****************************************************************************/
66 static block_t
*PacketizeAnnexB(decoder_t
*, block_t
**);
67 static block_t
*PacketizeHVC1(decoder_t
*, block_t
**);
68 static void PacketizeFlush( decoder_t
* );
69 static void PacketizeReset(void *p_private
, bool b_broken
);
70 static block_t
*PacketizeParse(void *p_private
, bool *pb_ts_used
, block_t
*);
71 static block_t
*ParseNALBlock(decoder_t
*, bool *pb_ts_used
, block_t
*);
72 static int PacketizeValidate(void *p_private
, block_t
*);
73 static bool ParseSEICallback( const hxxx_sei_data_t
*, void * );
74 static block_t
*GetCc( decoder_t
*, decoder_cc_desc_t
* );
79 packetizer_t packetizer
;
84 block_t
**pp_chain_last
;
87 uint8_t i_nal_length_size
;
93 } rg_vps
[HEVC_VPS_ID_MAX
+ 1],
94 rg_sps
[HEVC_SPS_ID_MAX
+ 1],
95 rg_pps
[HEVC_PPS_ID_MAX
+ 1];
97 const hevc_video_parameter_set_t
*p_active_vps
;
98 const hevc_sequence_parameter_set_t
*p_active_sps
;
99 const hevc_picture_parameter_set_t
*p_active_pps
;
100 hevc_sei_pic_timing_t
*p_timing
;
101 bool b_init_sequence_complete
;
111 #define BLOCK_FLAG_DROP (1 << BLOCK_FLAG_PRIVATE_SHIFT)
113 static const uint8_t p_hevc_startcode
[3] = {0x00, 0x00, 0x01};
114 /****************************************************************************
116 ****************************************************************************/
117 static inline void InitQueue( block_t
**pp_head
, block_t
***ppp_tail
)
122 #define INITQ(name) InitQueue(&p_sys->name.p_chain, &p_sys->name.pp_chain_last)
124 static block_t
* OutputQueues(decoder_sys_t
*p_sys
, bool b_valid
)
126 block_t
*p_output
= NULL
;
127 block_t
**pp_output_last
= &p_output
;
128 uint32_t i_flags
= 0; /* Because block_ChainGather does not merge flags or times */
130 if(p_sys
->pre
.p_chain
)
132 i_flags
|= p_sys
->pre
.p_chain
->i_flags
;
133 block_ChainLastAppend(&pp_output_last
, p_sys
->pre
.p_chain
);
137 if(p_sys
->frame
.p_chain
)
139 i_flags
|= p_sys
->frame
.p_chain
->i_flags
;
140 block_ChainLastAppend(&pp_output_last
, p_sys
->frame
.p_chain
);
141 p_output
->i_dts
= date_Get(&p_sys
->dts
);
142 p_output
->i_pts
= p_sys
->pts
;
146 if(p_sys
->post
.p_chain
)
148 i_flags
|= p_sys
->post
.p_chain
->i_flags
;
149 block_ChainLastAppend(&pp_output_last
, p_sys
->post
.p_chain
);
155 p_output
->i_flags
|= i_flags
;
157 p_output
->i_flags
|= BLOCK_FLAG_DROP
;
164 /*****************************************************************************
166 *****************************************************************************/
167 static int Open(vlc_object_t
*p_this
)
169 decoder_t
*p_dec
= (decoder_t
*)p_this
;
170 decoder_sys_t
*p_sys
;
172 if (p_dec
->fmt_in
.i_codec
!= VLC_CODEC_HEVC
)
175 p_dec
->p_sys
= p_sys
= calloc(1, sizeof(decoder_sys_t
));
179 p_sys
->p_ccs
= cc_storage_new();
180 if(unlikely(!p_sys
->p_ccs
))
190 packetizer_Init(&p_sys
->packetizer
,
191 p_hevc_startcode
, sizeof(p_hevc_startcode
), startcode_FindAnnexB
,
192 p_hevc_startcode
, 1, 5,
193 PacketizeReset
, PacketizeParse
, PacketizeValidate
, p_dec
);
195 /* Copy properties */
196 es_format_Copy(&p_dec
->fmt_out
, &p_dec
->fmt_in
);
197 p_dec
->fmt_out
.b_packetized
= true;
200 if( p_dec
->fmt_in
.video
.i_frame_rate_base
&&
201 p_dec
->fmt_in
.video
.i_frame_rate
&&
202 p_dec
->fmt_in
.video
.i_frame_rate
<= UINT_MAX
/ 2 )
203 date_Init( &p_sys
->dts
, p_dec
->fmt_in
.video
.i_frame_rate
* 2,
204 p_dec
->fmt_in
.video
.i_frame_rate_base
);
206 date_Init( &p_sys
->dts
, 2 * 30000, 1001 );
207 p_sys
->pts
= VLC_TICK_INVALID
;
208 p_sys
->b_need_ts
= true;
211 const uint8_t *p_extra
= p_dec
->fmt_in
.p_extra
;
212 const size_t i_extra
= p_dec
->fmt_in
.i_extra
;
213 /* Check if we have hvcC as extradata */
214 if(hevc_ishvcC(p_extra
, i_extra
))
216 p_dec
->pf_packetize
= PacketizeHVC1
;
218 /* Clear hvcC/HVC1 extra, to be replaced with AnnexB */
219 free(p_dec
->fmt_out
.p_extra
);
220 p_dec
->fmt_out
.i_extra
= 0;
222 size_t i_new_extra
= 0;
223 p_dec
->fmt_out
.p_extra
=
224 hevc_hvcC_to_AnnexB_NAL(p_extra
, i_extra
,
225 &i_new_extra
, &p_sys
->i_nal_length_size
);
226 if(p_dec
->fmt_out
.p_extra
)
227 p_dec
->fmt_out
.i_extra
= i_new_extra
;
231 p_dec
->pf_packetize
= PacketizeAnnexB
;
233 p_dec
->pf_flush
= PacketizeFlush
;
234 p_dec
->pf_get_cc
= GetCc
;
236 if(p_dec
->fmt_out
.i_extra
)
238 /* Feed with AnnexB VPS/SPS/PPS/SEI extradata */
239 packetizer_Header(&p_sys
->packetizer
,
240 p_dec
->fmt_out
.p_extra
, p_dec
->fmt_out
.i_extra
);
246 /*****************************************************************************
248 *****************************************************************************/
249 static void Close(vlc_object_t
*p_this
)
251 decoder_t
*p_dec
= (decoder_t
*)p_this
;
252 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
253 packetizer_Clean(&p_sys
->packetizer
);
255 block_ChainRelease(p_sys
->frame
.p_chain
);
256 block_ChainRelease(p_sys
->pre
.p_chain
);
257 block_ChainRelease(p_sys
->post
.p_chain
);
259 for(unsigned i
=0;i
<=HEVC_PPS_ID_MAX
; i
++)
261 if(p_sys
->rg_pps
[i
].p_decoded
)
262 hevc_rbsp_release_pps(p_sys
->rg_pps
[i
].p_decoded
);
263 if(p_sys
->rg_pps
[i
].p_nal
)
264 block_Release(p_sys
->rg_pps
[i
].p_nal
);
267 for(unsigned i
=0;i
<=HEVC_SPS_ID_MAX
; i
++)
269 if(p_sys
->rg_sps
[i
].p_decoded
)
270 hevc_rbsp_release_sps(p_sys
->rg_sps
[i
].p_decoded
);
271 if(p_sys
->rg_sps
[i
].p_nal
)
272 block_Release(p_sys
->rg_sps
[i
].p_nal
);
275 for(unsigned i
=0;i
<=HEVC_VPS_ID_MAX
; i
++)
277 if(p_sys
->rg_vps
[i
].p_decoded
)
278 hevc_rbsp_release_vps(p_sys
->rg_vps
[i
].p_decoded
);
279 if(p_sys
->rg_vps
[i
].p_nal
)
280 block_Release(p_sys
->rg_vps
[i
].p_nal
);
283 hevc_release_sei_pic_timing( p_sys
->p_timing
);
285 cc_storage_delete( p_sys
->p_ccs
);
290 /****************************************************************************
292 ****************************************************************************/
293 static block_t
*PacketizeHVC1(decoder_t
*p_dec
, block_t
**pp_block
)
295 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
297 return PacketizeXXC1( p_dec
, p_sys
->i_nal_length_size
,
298 pp_block
, ParseNALBlock
);
301 static block_t
*PacketizeAnnexB(decoder_t
*p_dec
, block_t
**pp_block
)
303 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
305 return packetizer_Packetize(&p_sys
->packetizer
, pp_block
);
308 static void PacketizeFlush( decoder_t
*p_dec
)
310 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
312 packetizer_Flush( &p_sys
->packetizer
);
315 /*****************************************************************************
317 *****************************************************************************/
318 static block_t
*GetCc( decoder_t
*p_dec
, decoder_cc_desc_t
*p_desc
)
320 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
321 return cc_storage_get_current( p_sys
->p_ccs
, p_desc
);
324 /****************************************************************************
326 ****************************************************************************/
327 static void PacketizeReset(void *p_private
, bool b_broken
)
329 VLC_UNUSED(b_broken
);
331 decoder_t
*p_dec
= p_private
;
332 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
334 block_t
*p_out
= OutputQueues(p_sys
, false);
336 block_ChainRelease(p_out
);
338 p_sys
->b_init_sequence_complete
= false;
339 p_sys
->b_need_ts
= true;
340 date_Set(&p_sys
->dts
, VLC_TICK_INVALID
);
343 static bool InsertXPS(decoder_t
*p_dec
, uint8_t i_nal_type
, uint8_t i_id
,
344 const block_t
*p_nalb
)
346 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
354 if(i_id
> HEVC_VPS_ID_MAX
)
356 pp_decoded
= &p_sys
->rg_vps
[i_id
].p_decoded
;
357 pp_nal
= &p_sys
->rg_vps
[i_id
].p_nal
;
358 pp_active
= (void**)&p_sys
->p_active_vps
;
361 if(i_id
> HEVC_SPS_ID_MAX
)
363 pp_decoded
= &p_sys
->rg_sps
[i_id
].p_decoded
;
364 pp_nal
= &p_sys
->rg_sps
[i_id
].p_nal
;
365 pp_active
= (void**)&p_sys
->p_active_sps
;
368 if(i_id
> HEVC_PPS_ID_MAX
)
370 pp_decoded
= &p_sys
->rg_pps
[i_id
].p_decoded
;
371 pp_nal
= &p_sys
->rg_pps
[i_id
].p_nal
;
372 pp_active
= (void**)&p_sys
->p_active_pps
;
378 /* Check if we really need to re-decode/replace */
381 const uint8_t *p_stored
= (*pp_nal
)->p_buffer
;
382 size_t i_stored
= (*pp_nal
)->i_buffer
;
383 hxxx_strip_AnnexB_startcode(&p_stored
, &i_stored
);
384 const uint8_t *p_new
= p_nalb
->p_buffer
;
385 size_t i_new
= p_nalb
->i_buffer
;
386 hxxx_strip_AnnexB_startcode(&p_new
, &i_new
);
387 if(i_stored
== i_new
&& !memcmp(p_stored
, p_new
, i_new
))
391 /* Free associated decoded version */
397 hevc_rbsp_release_vps(*pp_decoded
);
400 hevc_rbsp_release_sps(*pp_decoded
);
403 hevc_rbsp_release_pps(*pp_decoded
);
406 if(*pp_active
== *pp_decoded
)
409 pp_active
= NULL
; /* don't change pointer */
412 else pp_active
= NULL
;
414 /* Free raw stored version */
417 block_Release(*pp_nal
);
421 const uint8_t *p_buffer
= p_nalb
->p_buffer
;
422 size_t i_buffer
= p_nalb
->i_buffer
;
423 if( hxxx_strip_AnnexB_startcode( &p_buffer
, &i_buffer
) )
425 /* Create decoded entries */
429 *pp_decoded
= hevc_decode_sps(p_buffer
, i_buffer
, true);
432 msg_Err(p_dec
, "Failed decoding SPS id %d", i_id
);
437 *pp_decoded
= hevc_decode_pps(p_buffer
, i_buffer
, true);
440 msg_Err(p_dec
, "Failed decoding PPS id %d", i_id
);
445 *pp_decoded
= hevc_decode_vps(p_buffer
, i_buffer
, true);
448 msg_Err(p_dec
, "Failed decoding VPS id %d", i_id
);
454 if(*pp_decoded
&& pp_active
) /* restore active by id */
455 *pp_active
= *pp_decoded
;
457 *pp_nal
= block_Duplicate((block_t
*)p_nalb
);
465 static bool XPSReady(decoder_sys_t
*p_sys
)
467 for(unsigned i
=0;i
<=HEVC_PPS_ID_MAX
; i
++)
469 const hevc_picture_parameter_set_t
*p_pps
= p_sys
->rg_pps
[i
].p_decoded
;
472 uint8_t id_sps
= hevc_get_pps_sps_id(p_pps
);
473 const hevc_sequence_parameter_set_t
*p_sps
= p_sys
->rg_sps
[id_sps
].p_decoded
;
476 uint8_t id_vps
= hevc_get_sps_vps_id(p_sps
);
477 if(p_sys
->rg_vps
[id_vps
].p_decoded
)
485 static void AppendAsAnnexB(const block_t
*p_block
,
486 uint8_t **pp_dst
, size_t *pi_dst
)
488 if(SIZE_MAX
- p_block
->i_buffer
< *pi_dst
)
491 size_t i_realloc
= p_block
->i_buffer
+ *pi_dst
;
492 uint8_t *p_realloc
= realloc(*pp_dst
, i_realloc
);
495 memcpy(&p_realloc
[*pi_dst
], p_block
->p_buffer
, p_block
->i_buffer
);
501 #define APPENDIF(idmax, set, rg, b) \
502 for(size_t i=0; i<=idmax; i++)\
504 if(((set != rg[i].p_decoded) == !b) && rg[i].p_nal)\
506 AppendAsAnnexB(rg[i].p_nal, &p_data, &i_data);\
511 static void SetsToAnnexB(decoder_sys_t
*p_sys
,
512 const hevc_picture_parameter_set_t
*p_pps
,
513 const hevc_sequence_parameter_set_t
*p_sps
,
514 const hevc_video_parameter_set_t
*p_vps
,
515 uint8_t **pp_out
, int *pi_out
)
517 uint8_t *p_data
= NULL
;
520 APPENDIF(HEVC_VPS_ID_MAX
, p_vps
, p_sys
->rg_vps
, true);
521 APPENDIF(HEVC_VPS_ID_MAX
, p_vps
, p_sys
->rg_vps
, false);
522 APPENDIF(HEVC_SPS_ID_MAX
, p_sps
, p_sys
->rg_sps
, true);
523 APPENDIF(HEVC_SPS_ID_MAX
, p_sps
, p_sys
->rg_sps
, false);
524 APPENDIF(HEVC_PPS_ID_MAX
, p_pps
, p_sys
->rg_pps
, true);
525 APPENDIF(HEVC_PPS_ID_MAX
, p_pps
, p_sys
->rg_pps
, false);
527 /* because we copy to i_extra :/ */
528 if(i_data
<= INT_MAX
)
536 static void ActivateSets(decoder_t
*p_dec
,
537 const hevc_picture_parameter_set_t
*p_pps
,
538 const hevc_sequence_parameter_set_t
*p_sps
,
539 const hevc_video_parameter_set_t
*p_vps
)
541 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
542 p_sys
->p_active_pps
= p_pps
;
543 p_sys
->p_active_sps
= p_sps
;
544 p_sys
->p_active_vps
= p_vps
;
547 if(!p_dec
->fmt_in
.video
.i_frame_rate
|| !p_dec
->fmt_in
.video
.i_frame_rate_base
)
550 if(hevc_get_frame_rate( p_sps
, p_vps
, &num
, &den
))
552 p_dec
->fmt_out
.video
.i_frame_rate
= num
;
553 p_dec
->fmt_out
.video
.i_frame_rate_base
= den
;
554 if(p_sys
->dts
.i_divider_den
!= den
&&
555 p_sys
->dts
.i_divider_num
!= 2 * num
&&
557 date_Change(&p_sys
->dts
, 2 * num
, den
);
561 if(p_dec
->fmt_in
.video
.primaries
== COLOR_PRIMARIES_UNDEF
)
563 (void) hevc_get_colorimetry( p_sps
,
564 &p_dec
->fmt_out
.video
.primaries
,
565 &p_dec
->fmt_out
.video
.transfer
,
566 &p_dec
->fmt_out
.video
.space
,
567 &p_dec
->fmt_out
.video
.b_color_range_full
);
571 if( hevc_get_picture_size( p_sps
, &sizes
[0], &sizes
[1],
572 &sizes
[2], &sizes
[3] ) )
574 p_dec
->fmt_out
.video
.i_width
= sizes
[0];
575 p_dec
->fmt_out
.video
.i_height
= sizes
[1];
576 if(p_dec
->fmt_in
.video
.i_visible_width
== 0)
578 p_dec
->fmt_out
.video
.i_visible_width
= sizes
[2];
579 p_dec
->fmt_out
.video
.i_visible_height
= sizes
[3];
583 if(p_dec
->fmt_in
.i_profile
== -1)
585 uint8_t i_profile
, i_level
;
586 if( hevc_get_sps_profile_tier_level( p_sps
, &i_profile
, &i_level
) )
588 p_dec
->fmt_out
.i_profile
= i_profile
;
589 p_dec
->fmt_out
.i_level
= i_level
;
593 if(p_dec
->fmt_out
.i_extra
== 0 && p_vps
&& p_pps
)
594 SetsToAnnexB(p_sys
, p_pps
, p_sps
, p_vps
,
595 (uint8_t **)&p_dec
->fmt_out
.p_extra
, &p_dec
->fmt_out
.i_extra
);
599 static void GetXPSSet(uint8_t i_pps_id
, void *priv
,
600 hevc_picture_parameter_set_t
**pp_pps
,
601 hevc_sequence_parameter_set_t
**pp_sps
,
602 hevc_video_parameter_set_t
**pp_vps
)
604 decoder_sys_t
*p_sys
= priv
;
607 if((*pp_pps
= p_sys
->rg_pps
[i_pps_id
].p_decoded
))
608 if((*pp_sps
= p_sys
->rg_sps
[hevc_get_pps_sps_id(*pp_pps
)].p_decoded
))
609 *pp_vps
= p_sys
->rg_vps
[hevc_get_sps_vps_id(*pp_sps
)].p_decoded
;
612 static void ParseStoredSEI( decoder_t
*p_dec
)
614 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
616 for( block_t
*p_nal
= p_sys
->pre
.p_chain
;
617 p_nal
; p_nal
= p_nal
->p_next
)
619 if( p_nal
->i_buffer
< 5 )
622 if( hevc_getNALType(&p_nal
->p_buffer
[4]) == HEVC_NAL_PREF_SEI
)
624 HxxxParse_AnnexB_SEI( p_nal
->p_buffer
, p_nal
->i_buffer
,
625 2 /* nal header */, ParseSEICallback
, p_dec
);
630 static block_t
*ParseVCL(decoder_t
*p_dec
, uint8_t i_nal_type
, block_t
*p_frag
)
632 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
633 block_t
*p_outputchain
= NULL
;
635 const uint8_t *p_buffer
= p_frag
->p_buffer
;
636 size_t i_buffer
= p_frag
->i_buffer
;
638 if(unlikely(!hxxx_strip_AnnexB_startcode(&p_buffer
, &i_buffer
) || i_buffer
< 3))
640 block_ChainLastAppend(&p_sys
->frame
.pp_chain_last
, p_frag
); /* might be corrupted */
644 const uint8_t i_layer
= hevc_getNALLayer( p_buffer
);
645 bool b_first_slice_in_pic
= p_buffer
[2] & 0x80;
646 if (b_first_slice_in_pic
)
648 if(p_sys
->frame
.p_chain
)
650 /* Starting new frame: return previous frame data for output */
651 p_outputchain
= OutputQueues(p_sys
, p_sys
->b_init_sequence_complete
);
654 hevc_slice_segment_header_t
*p_sli
= hevc_decode_slice_header(p_buffer
, i_buffer
, true,
656 if(p_sli
&& i_layer
== 0)
658 hevc_sequence_parameter_set_t
*p_sps
;
659 hevc_picture_parameter_set_t
*p_pps
;
660 hevc_video_parameter_set_t
*p_vps
;
661 GetXPSSet(hevc_get_slice_pps_id(p_sli
), p_sys
, &p_pps
, &p_sps
, &p_vps
);
662 ActivateSets(p_dec
, p_pps
, p_sps
, p_vps
);
665 ParseStoredSEI( p_dec
);
669 case HEVC_NAL_BLA_W_LP
:
670 case HEVC_NAL_BLA_W_RADL
:
671 case HEVC_NAL_BLA_N_LP
:
672 case HEVC_NAL_IDR_W_RADL
:
673 case HEVC_NAL_IDR_N_LP
:
675 p_frag
->i_flags
|= BLOCK_FLAG_TYPE_I
;
682 enum hevc_slice_type_e type
;
683 if(hevc_get_slice_type( p_sli
, &type
))
685 if( type
== HEVC_SLICE_TYPE_P
)
686 p_frag
->i_flags
|= BLOCK_FLAG_TYPE_P
;
688 p_frag
->i_flags
|= BLOCK_FLAG_TYPE_B
;
691 else p_frag
->i_flags
|= BLOCK_FLAG_TYPE_B
;
697 hevc_rbsp_release_slice_header(p_sli
);
700 if(!p_sys
->b_init_sequence_complete
&& i_layer
== 0 &&
701 (p_frag
->i_flags
& BLOCK_FLAG_TYPE_I
) && XPSReady(p_sys
))
703 p_sys
->b_init_sequence_complete
= true;
706 if( !p_sys
->b_init_sequence_complete
)
707 cc_storage_reset( p_sys
->p_ccs
);
709 block_ChainLastAppend(&p_sys
->frame
.pp_chain_last
, p_frag
);
711 return p_outputchain
;
714 static block_t
* ParseAUHead(decoder_t
*p_dec
, uint8_t i_nal_type
, block_t
*p_nalb
)
716 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
717 block_t
*p_ret
= NULL
;
719 if(p_sys
->post
.p_chain
|| p_sys
->frame
.p_chain
)
720 p_ret
= OutputQueues(p_sys
, p_sys
->b_init_sequence_complete
);
725 if(!p_ret
&& p_sys
->pre
.p_chain
)
726 p_ret
= OutputQueues(p_sys
, p_sys
->b_init_sequence_complete
);
734 const uint8_t *p_xps
= p_nalb
->p_buffer
;
735 size_t i_xps
= p_nalb
->i_buffer
;
736 if(hxxx_strip_AnnexB_startcode(&p_xps
, &i_xps
) &&
737 hevc_get_xps_id(p_nalb
->p_buffer
, p_nalb
->i_buffer
, &i_id
))
738 InsertXPS(p_dec
, i_nal_type
, i_id
, p_nalb
);
742 case HEVC_NAL_PREF_SEI
:
743 /* stored an parsed later when we get sps & frame */
748 block_ChainLastAppend(&p_sys
->pre
.pp_chain_last
, p_nalb
);
753 static block_t
* ParseAUTail(decoder_t
*p_dec
, uint8_t i_nal_type
, block_t
*p_nalb
)
755 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
756 block_t
*p_ret
= NULL
;
758 block_ChainLastAppend(&p_sys
->post
.pp_chain_last
, p_nalb
);
764 p_ret
= OutputQueues(p_sys
, p_sys
->b_init_sequence_complete
);
766 p_ret
->i_flags
|= BLOCK_FLAG_END_OF_SEQUENCE
;
769 case HEVC_NAL_SUFF_SEI
:
770 HxxxParse_AnnexB_SEI( p_nalb
->p_buffer
, p_nalb
->i_buffer
,
771 2 /* nal header */, ParseSEICallback
, p_dec
);
775 if(!p_ret
&& p_sys
->frame
.p_chain
== NULL
)
776 p_ret
= OutputQueues(p_sys
, false);
781 static block_t
* ParseNonVCL(decoder_t
*p_dec
, uint8_t i_nal_type
, block_t
*p_nalb
)
783 block_t
*p_ret
= NULL
;
785 if ( (i_nal_type
>= HEVC_NAL_VPS
&& i_nal_type
<= HEVC_NAL_AUD
) ||
786 i_nal_type
== HEVC_NAL_PREF_SEI
||
787 (i_nal_type
>= HEVC_NAL_RSV_NVCL41
&& i_nal_type
<= HEVC_NAL_RSV_NVCL44
) ||
788 (i_nal_type
>= HEVC_NAL_UNSPEC48
&& i_nal_type
<= HEVC_NAL_UNSPEC55
) )
790 p_ret
= ParseAUHead(p_dec
, i_nal_type
, p_nalb
);
794 p_ret
= ParseAUTail(p_dec
, i_nal_type
, p_nalb
);
800 static block_t
*GatherAndValidateChain(block_t
*p_outputchain
)
802 block_t
*p_output
= NULL
;
806 if(p_outputchain
->i_flags
& BLOCK_FLAG_DROP
)
807 p_output
= p_outputchain
; /* Avoid useless gather */
809 p_output
= block_ChainGather(p_outputchain
);
812 if(p_output
&& (p_output
->i_flags
& BLOCK_FLAG_DROP
))
814 block_ChainRelease(p_output
); /* Chain! see above */
821 static void SetOutputBlockProperties(decoder_t
*p_dec
, block_t
*p_output
)
823 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
824 /* Set frame duration */
825 if(p_sys
->p_active_sps
)
827 uint8_t i_num_clock_ts
= hevc_get_num_clock_ts(p_sys
->p_active_sps
,
829 const vlc_tick_t i_start
= date_Get(&p_sys
->dts
);
830 if( i_start
!= VLC_TICK_INVALID
)
832 date_Increment(&p_sys
->dts
, i_num_clock_ts
);
833 p_output
->i_length
= date_Get(&p_sys
->dts
) - i_start
;
835 p_sys
->pts
= VLC_TICK_INVALID
;
837 hevc_release_sei_pic_timing(p_sys
->p_timing
);
838 p_sys
->p_timing
= NULL
;
841 /*****************************************************************************
842 * ParseNALBlock: parses annexB type NALs
843 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
844 *****************************************************************************/
845 static block_t
*ParseNALBlock(decoder_t
*p_dec
, bool *pb_ts_used
, block_t
*p_frag
)
847 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
852 if(p_frag
->i_dts
!= VLC_TICK_INVALID
)
853 date_Set(&p_sys
->dts
, p_frag
->i_dts
);
854 p_sys
->pts
= p_frag
->i_pts
;
855 if(date_Get( &p_sys
->dts
) != VLC_TICK_INVALID
)
856 p_sys
->b_need_ts
= false;
860 if(unlikely(p_frag
->i_buffer
< 5))
862 msg_Warn(p_dec
,"NAL too small");
863 block_Release(p_frag
);
867 if(p_frag
->p_buffer
[4] & 0x80)
869 msg_Warn(p_dec
,"Forbidden zero bit not null, corrupted NAL");
870 block_Release(p_frag
);
871 return GatherAndValidateChain(OutputQueues(p_sys
, false)); /* will drop */
875 const vlc_tick_t dts
= p_frag
->i_dts
, pts
= p_frag
->i_pts
;
876 block_t
* p_output
= NULL
;
877 uint8_t i_nal_type
= hevc_getNALType(&p_frag
->p_buffer
[4]);
879 if (i_nal_type
< HEVC_NAL_VPS
)
881 /* NAL is a VCL NAL */
882 p_output
= ParseVCL(p_dec
, i_nal_type
, p_frag
);
883 if (p_output
&& (p_output
->i_flags
& BLOCK_FLAG_DROP
))
884 msg_Info(p_dec
, "Waiting for VPS/SPS/PPS");
888 p_output
= ParseNonVCL(p_dec
, i_nal_type
, p_frag
);
891 p_output
= GatherAndValidateChain(p_output
);
894 SetOutputBlockProperties( p_dec
, p_output
);
895 if (dts
!= VLC_TICK_INVALID
)
896 date_Set(&p_sys
->dts
, dts
);
904 static block_t
*PacketizeParse(void *p_private
, bool *pb_ts_used
, block_t
*p_block
)
906 decoder_t
*p_dec
= p_private
;
907 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
909 /* Remove trailing 0 bytes */
910 while (p_block
->i_buffer
> 5 && p_block
->p_buffer
[p_block
->i_buffer
-1] == 0x00 )
913 p_block
= ParseNALBlock( p_dec
, pb_ts_used
, p_block
);
915 cc_storage_commit( p_sys
->p_ccs
, p_block
);
920 static int PacketizeValidate( void *p_private
, block_t
*p_au
)
922 VLC_UNUSED(p_private
);
927 static bool ParseSEICallback( const hxxx_sei_data_t
*p_sei_data
, void *cbdata
)
929 decoder_t
*p_dec
= (decoder_t
*) cbdata
;
930 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
932 switch( p_sei_data
->i_type
)
934 case HXXX_SEI_PIC_TIMING
:
936 if( p_sys
->p_active_sps
)
938 hevc_release_sei_pic_timing( p_sys
->p_timing
);
939 p_sys
->p_timing
= hevc_decode_sei_pic_timing( p_sei_data
->p_bs
,
940 p_sys
->p_active_sps
);
943 case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35
:
945 if( p_sei_data
->itu_t35
.type
== HXXX_ITU_T35_TYPE_CC
)
947 cc_storage_append( p_sys
->p_ccs
, true, p_sei_data
->itu_t35
.u
.cc
.p_data
,
948 p_sei_data
->itu_t35
.u
.cc
.i_data
);
951 case HXXX_SEI_FRAME_PACKING_ARRANGEMENT
:
953 if( p_dec
->fmt_in
.video
.multiview_mode
== MULTIVIEW_2D
)
955 video_multiview_mode_t mode
;
956 switch( p_sei_data
->frame_packing
.type
)
958 case FRAME_PACKING_INTERLEAVED_CHECKERBOARD
:
959 mode
= MULTIVIEW_STEREO_CHECKERBOARD
; break;
960 case FRAME_PACKING_INTERLEAVED_COLUMN
:
961 mode
= MULTIVIEW_STEREO_COL
; break;
962 case FRAME_PACKING_INTERLEAVED_ROW
:
963 mode
= MULTIVIEW_STEREO_ROW
; break;
964 case FRAME_PACKING_SIDE_BY_SIDE
:
965 mode
= MULTIVIEW_STEREO_SBS
; break;
966 case FRAME_PACKING_TOP_BOTTOM
:
967 mode
= MULTIVIEW_STEREO_TB
; break;
968 case FRAME_PACKING_TEMPORAL
:
969 mode
= MULTIVIEW_STEREO_FRAME
; break;
970 case FRAME_PACKING_TILED
:
972 mode
= MULTIVIEW_2D
; break;
974 p_dec
->fmt_out
.video
.multiview_mode
= mode
;
977 case HXXX_SEI_MASTERING_DISPLAY_COLOUR_VOLUME
:
979 video_format_t
*p_fmt
= &p_dec
->fmt_out
.video
;
980 for (size_t i
=0; i
<ARRAY_SIZE(p_sei_data
->colour_volume
.primaries
); ++i
)
981 p_fmt
->mastering
.primaries
[i
] = p_sei_data
->colour_volume
.primaries
[i
];
982 for (size_t i
=0; i
<ARRAY_SIZE(p_sei_data
->colour_volume
.white_point
); ++i
)
983 p_fmt
->mastering
.white_point
[i
] = p_sei_data
->colour_volume
.white_point
[i
];
984 p_fmt
->mastering
.max_luminance
= p_sei_data
->colour_volume
.max_luminance
;
985 p_fmt
->mastering
.min_luminance
= p_sei_data
->colour_volume
.min_luminance
;
987 case HXXX_SEI_CONTENT_LIGHT_LEVEL
:
989 video_format_t
*p_fmt
= &p_dec
->fmt_out
.video
;
990 p_fmt
->lighting
.MaxCLL
= p_sei_data
->content_light_lvl
.MaxCLL
;
991 p_fmt
->lighting
.MaxFALL
= p_sei_data
->content_light_lvl
.MaxFALL
;