mlp: missing initializer (fixes #20494)
[vlc.git] / modules / packetizer / hevc.c
blob80ec727b81d21c9201b9bafbe20a3192d21cb7b0
1 /*****************************************************************************
2 * hevc.c: h.265/hevc video packetizer
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35 #include <vlc_block.h>
36 #include <vlc_bits.h>
38 #include <vlc_block_helper.h>
39 #include "packetizer_helper.h"
40 #include "startcode_helper.h"
41 #include "hevc_nal.h"
42 #include "hxxx_nal.h"
43 #include "hxxx_sei.h"
44 #include "hxxx_common.h"
46 #include <limits.h>
48 /*****************************************************************************
49 * Module descriptor
50 *****************************************************************************/
51 static int Open (vlc_object_t *);
52 static void Close(vlc_object_t *);
54 vlc_module_begin ()
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)
60 vlc_module_end ()
63 /****************************************************************************
64 * Local prototypes
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 * );
76 typedef struct
78 /* */
79 packetizer_t packetizer;
81 struct
83 block_t *p_chain;
84 block_t **pp_chain_last;
85 } frame, pre, post;
87 uint8_t i_nal_length_size;
89 struct
91 block_t *p_nal;
92 void *p_decoded;
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;
103 date_t dts;
104 mtime_t pts;
105 bool b_need_ts;
107 /* */
108 cc_storage_t *p_ccs;
109 } decoder_sys_t;
111 #define BLOCK_FLAG_DROP (1 << BLOCK_FLAG_PRIVATE_SHIFT)
113 static const uint8_t p_hevc_startcode[3] = {0x00, 0x00, 0x01};
114 /****************************************************************************
115 * Helpers
116 ****************************************************************************/
117 static inline void InitQueue( block_t **pp_head, block_t ***ppp_tail )
119 *pp_head = NULL;
120 *ppp_tail = pp_head;
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);
134 INITQ(pre);
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;
143 INITQ(frame);
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);
150 INITQ(post);
153 if(p_output)
155 p_output->i_flags |= i_flags;
156 if(!b_valid)
157 p_output->i_flags |= BLOCK_FLAG_DROP;
160 return p_output;
164 /*****************************************************************************
165 * Open
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)
173 return VLC_EGENERIC;
175 p_dec->p_sys = p_sys = calloc(1, sizeof(decoder_sys_t));
176 if (!p_dec->p_sys)
177 return VLC_ENOMEM;
179 p_sys->p_ccs = cc_storage_new();
180 if(unlikely(!p_sys->p_ccs))
182 free(p_dec->p_sys);
183 return VLC_ENOMEM;
186 INITQ(pre);
187 INITQ(frame);
188 INITQ(post);
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;
199 /* Init timings */
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 );
205 else
206 date_Init( &p_sys->dts, 2 * 30000, 1001 );
207 date_Set( &p_sys->dts, VLC_TS_INVALID );
208 p_sys->pts = VLC_TS_INVALID;
209 p_sys->b_need_ts = true;
211 /* Set callbacks */
212 const uint8_t *p_extra = p_dec->fmt_in.p_extra;
213 const size_t i_extra = p_dec->fmt_in.i_extra;
214 /* Check if we have hvcC as extradata */
215 if(hevc_ishvcC(p_extra, i_extra))
217 p_dec->pf_packetize = PacketizeHVC1;
219 /* Clear hvcC/HVC1 extra, to be replaced with AnnexB */
220 free(p_dec->fmt_out.p_extra);
221 p_dec->fmt_out.i_extra = 0;
223 size_t i_new_extra = 0;
224 p_dec->fmt_out.p_extra =
225 hevc_hvcC_to_AnnexB_NAL(p_extra, i_extra,
226 &i_new_extra, &p_sys->i_nal_length_size);
227 if(p_dec->fmt_out.p_extra)
228 p_dec->fmt_out.i_extra = i_new_extra;
230 else
232 p_dec->pf_packetize = PacketizeAnnexB;
234 p_dec->pf_flush = PacketizeFlush;
235 p_dec->pf_get_cc = GetCc;
237 if(p_dec->fmt_out.i_extra)
239 /* Feed with AnnexB VPS/SPS/PPS/SEI extradata */
240 packetizer_Header(&p_sys->packetizer,
241 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra);
244 return VLC_SUCCESS;
247 /*****************************************************************************
248 * Close
249 *****************************************************************************/
250 static void Close(vlc_object_t *p_this)
252 decoder_t *p_dec = (decoder_t*)p_this;
253 decoder_sys_t *p_sys = p_dec->p_sys;
254 packetizer_Clean(&p_sys->packetizer);
256 block_ChainRelease(p_sys->frame.p_chain);
257 block_ChainRelease(p_sys->pre.p_chain);
258 block_ChainRelease(p_sys->post.p_chain);
260 for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
262 if(p_sys->rg_pps[i].p_decoded)
263 hevc_rbsp_release_pps(p_sys->rg_pps[i].p_decoded);
264 if(p_sys->rg_pps[i].p_nal)
265 block_Release(p_sys->rg_pps[i].p_nal);
268 for(unsigned i=0;i<=HEVC_SPS_ID_MAX; i++)
270 if(p_sys->rg_sps[i].p_decoded)
271 hevc_rbsp_release_sps(p_sys->rg_sps[i].p_decoded);
272 if(p_sys->rg_sps[i].p_nal)
273 block_Release(p_sys->rg_sps[i].p_nal);
276 for(unsigned i=0;i<=HEVC_VPS_ID_MAX; i++)
278 if(p_sys->rg_vps[i].p_decoded)
279 hevc_rbsp_release_vps(p_sys->rg_vps[i].p_decoded);
280 if(p_sys->rg_vps[i].p_nal)
281 block_Release(p_sys->rg_vps[i].p_nal);
284 hevc_release_sei_pic_timing( p_sys->p_timing );
286 cc_storage_delete( p_sys->p_ccs );
288 free(p_sys);
291 /****************************************************************************
292 * Packetize
293 ****************************************************************************/
294 static block_t *PacketizeHVC1(decoder_t *p_dec, block_t **pp_block)
296 decoder_sys_t *p_sys = p_dec->p_sys;
298 return PacketizeXXC1( p_dec, p_sys->i_nal_length_size,
299 pp_block, ParseNALBlock );
302 static block_t *PacketizeAnnexB(decoder_t *p_dec, block_t **pp_block)
304 decoder_sys_t *p_sys = p_dec->p_sys;
306 return packetizer_Packetize(&p_sys->packetizer, pp_block);
309 static void PacketizeFlush( decoder_t *p_dec )
311 decoder_sys_t *p_sys = p_dec->p_sys;
313 packetizer_Flush( &p_sys->packetizer );
316 /*****************************************************************************
317 * GetCc:
318 *****************************************************************************/
319 static block_t *GetCc( decoder_t *p_dec, decoder_cc_desc_t *p_desc )
321 decoder_sys_t *p_sys = p_dec->p_sys;
322 return cc_storage_get_current( p_sys->p_ccs, p_desc );
325 /****************************************************************************
326 * Packetizer Helpers
327 ****************************************************************************/
328 static void PacketizeReset(void *p_private, bool b_broken)
330 VLC_UNUSED(b_broken);
332 decoder_t *p_dec = p_private;
333 decoder_sys_t *p_sys = p_dec->p_sys;
335 block_t *p_out = OutputQueues(p_sys, false);
336 if(p_out)
337 block_ChainRelease(p_out);
339 p_sys->b_init_sequence_complete = false;
340 p_sys->b_need_ts = true;
341 date_Set(&p_sys->dts, VLC_TS_INVALID);
344 static bool InsertXPS(decoder_t *p_dec, uint8_t i_nal_type, uint8_t i_id,
345 const block_t *p_nalb)
347 decoder_sys_t *p_sys = p_dec->p_sys;
348 void **pp_decoded;
349 void **pp_active;
350 block_t **pp_nal;
352 switch(i_nal_type)
354 case HEVC_NAL_VPS:
355 if(i_id > HEVC_VPS_ID_MAX)
356 return false;
357 pp_decoded = &p_sys->rg_vps[i_id].p_decoded;
358 pp_nal = &p_sys->rg_vps[i_id].p_nal;
359 pp_active = (void**)&p_sys->p_active_vps;
360 break;
361 case HEVC_NAL_SPS:
362 if(i_id > HEVC_SPS_ID_MAX)
363 return false;
364 pp_decoded = &p_sys->rg_sps[i_id].p_decoded;
365 pp_nal = &p_sys->rg_sps[i_id].p_nal;
366 pp_active = (void**)&p_sys->p_active_sps;
367 break;
368 case HEVC_NAL_PPS:
369 if(i_id > HEVC_PPS_ID_MAX)
370 return false;
371 pp_decoded = &p_sys->rg_pps[i_id].p_decoded;
372 pp_nal = &p_sys->rg_pps[i_id].p_nal;
373 pp_active = (void**)&p_sys->p_active_pps;
374 break;
375 default:
376 return false;
379 /* Check if we really need to re-decode/replace */
380 if(*pp_nal)
382 const uint8_t *p_stored = (*pp_nal)->p_buffer;
383 size_t i_stored = (*pp_nal)->i_buffer;
384 hxxx_strip_AnnexB_startcode(&p_stored, &i_stored);
385 const uint8_t *p_new = p_nalb->p_buffer;
386 size_t i_new = p_nalb->i_buffer;
387 hxxx_strip_AnnexB_startcode(&p_new, &i_new);
388 if(i_stored == i_new && !memcmp(p_stored, p_new, i_new))
389 return true;
392 /* Free associated decoded version */
393 if(*pp_decoded)
395 switch(i_nal_type)
397 case HEVC_NAL_VPS:
398 hevc_rbsp_release_vps(*pp_decoded);
399 break;
400 case HEVC_NAL_SPS:
401 hevc_rbsp_release_sps(*pp_decoded);
402 break;
403 case HEVC_NAL_PPS:
404 hevc_rbsp_release_pps(*pp_decoded);
405 break;
407 if(*pp_active == *pp_decoded)
408 *pp_active = NULL;
409 else
410 pp_active = NULL; /* don't change pointer */
411 *pp_decoded = NULL;
413 else pp_active = NULL;
415 /* Free raw stored version */
416 if(*pp_nal)
418 block_Release(*pp_nal);
419 *pp_nal = NULL;
422 const uint8_t *p_buffer = p_nalb->p_buffer;
423 size_t i_buffer = p_nalb->i_buffer;
424 if( hxxx_strip_AnnexB_startcode( &p_buffer, &i_buffer ) )
426 /* Create decoded entries */
427 switch(i_nal_type)
429 case HEVC_NAL_SPS:
430 *pp_decoded = hevc_decode_sps(p_buffer, i_buffer, true);
431 if(!*pp_decoded)
433 msg_Err(p_dec, "Failed decoding SPS id %d", i_id);
434 return false;
436 break;
437 case HEVC_NAL_PPS:
438 *pp_decoded = hevc_decode_pps(p_buffer, i_buffer, true);
439 if(!*pp_decoded)
441 msg_Err(p_dec, "Failed decoding PPS id %d", i_id);
442 return false;
444 break;
445 case HEVC_NAL_VPS:
446 *pp_decoded = hevc_decode_vps(p_buffer, i_buffer, true);
447 if(!*pp_decoded)
449 msg_Err(p_dec, "Failed decoding VPS id %d", i_id);
450 return false;
452 break;
455 if(*pp_decoded && pp_active) /* restore active by id */
456 *pp_active = *pp_decoded;
458 *pp_nal = block_Duplicate((block_t *)p_nalb);
460 return true;
463 return false;
466 static bool XPSReady(decoder_sys_t *p_sys)
468 for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
470 const hevc_picture_parameter_set_t *p_pps = p_sys->rg_pps[i].p_decoded;
471 if (p_pps)
473 uint8_t id_sps = hevc_get_pps_sps_id(p_pps);
474 const hevc_sequence_parameter_set_t *p_sps = p_sys->rg_sps[id_sps].p_decoded;
475 if(p_sps)
477 uint8_t id_vps = hevc_get_sps_vps_id(p_sps);
478 if(p_sys->rg_vps[id_vps].p_decoded)
479 return true;
483 return false;
486 static void AppendAsAnnexB(const block_t *p_block,
487 uint8_t **pp_dst, size_t *pi_dst)
489 if(SIZE_MAX - p_block->i_buffer < *pi_dst )
490 return;
492 size_t i_realloc = p_block->i_buffer + *pi_dst;
493 uint8_t *p_realloc = realloc(*pp_dst, i_realloc);
494 if(p_realloc)
496 memcpy(&p_realloc[*pi_dst], p_block->p_buffer, p_block->i_buffer);
497 *pi_dst = i_realloc;
498 *pp_dst = p_realloc;
502 #define APPENDIF(idmax, set, rg, b) \
503 for(size_t i=0; i<=idmax; i++)\
505 if(((set != rg[i].p_decoded) == !b) && rg[i].p_nal)\
507 AppendAsAnnexB(rg[i].p_nal, &p_data, &i_data);\
508 break;\
512 static void SetsToAnnexB(decoder_sys_t *p_sys,
513 const hevc_picture_parameter_set_t *p_pps,
514 const hevc_sequence_parameter_set_t *p_sps,
515 const hevc_video_parameter_set_t *p_vps,
516 uint8_t **pp_out, int *pi_out)
518 uint8_t *p_data = NULL;
519 size_t i_data = 0;
521 APPENDIF(HEVC_VPS_ID_MAX, p_vps, p_sys->rg_vps, true);
522 APPENDIF(HEVC_VPS_ID_MAX, p_vps, p_sys->rg_vps, false);
523 APPENDIF(HEVC_SPS_ID_MAX, p_sps, p_sys->rg_sps, true);
524 APPENDIF(HEVC_SPS_ID_MAX, p_sps, p_sys->rg_sps, false);
525 APPENDIF(HEVC_PPS_ID_MAX, p_pps, p_sys->rg_pps, true);
526 APPENDIF(HEVC_PPS_ID_MAX, p_pps, p_sys->rg_pps, false);
528 /* because we copy to i_extra :/ */
529 if(i_data <= INT_MAX)
531 *pp_out = p_data;
532 *pi_out = i_data;
534 else free(p_data);
537 static void ActivateSets(decoder_t *p_dec,
538 const hevc_picture_parameter_set_t *p_pps,
539 const hevc_sequence_parameter_set_t *p_sps,
540 const hevc_video_parameter_set_t *p_vps)
542 decoder_sys_t *p_sys = p_dec->p_sys;
543 p_sys->p_active_pps = p_pps;
544 p_sys->p_active_sps = p_sps;
545 p_sys->p_active_vps = p_vps;
546 if(p_sps)
548 if(!p_dec->fmt_in.video.i_frame_rate || !p_dec->fmt_in.video.i_frame_rate_base)
550 unsigned num, den;
551 if(hevc_get_frame_rate( p_sps, p_vps, &num, &den ))
553 p_dec->fmt_out.video.i_frame_rate = num;
554 p_dec->fmt_out.video.i_frame_rate_base = den;
555 if(p_sys->dts.i_divider_den != den &&
556 p_sys->dts.i_divider_num != 2 * num &&
557 num <= UINT_MAX / 2)
558 date_Change(&p_sys->dts, 2 * num, den);
562 if(p_dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF)
564 (void) hevc_get_colorimetry( p_sps,
565 &p_dec->fmt_out.video.primaries,
566 &p_dec->fmt_out.video.transfer,
567 &p_dec->fmt_out.video.space,
568 &p_dec->fmt_out.video.b_color_range_full);
571 unsigned sizes[4];
572 if( hevc_get_picture_size( p_sps, &sizes[0], &sizes[1],
573 &sizes[2], &sizes[3] ) )
575 p_dec->fmt_out.video.i_width = sizes[0];
576 p_dec->fmt_out.video.i_height = sizes[1];
577 if(p_dec->fmt_in.video.i_visible_width == 0)
579 p_dec->fmt_out.video.i_visible_width = sizes[2];
580 p_dec->fmt_out.video.i_visible_height = sizes[3];
584 if(p_dec->fmt_in.i_profile == -1)
586 uint8_t i_profile, i_level;
587 if( hevc_get_sps_profile_tier_level( p_sps, &i_profile, &i_level ) )
589 p_dec->fmt_out.i_profile = i_profile;
590 p_dec->fmt_out.i_level = i_level;
594 if(p_dec->fmt_out.i_extra == 0 && p_vps && p_pps)
595 SetsToAnnexB(p_sys, p_pps, p_sps, p_vps,
596 (uint8_t **)&p_dec->fmt_out.p_extra, &p_dec->fmt_out.i_extra);
600 static void GetXPSSet(uint8_t i_pps_id, void *priv,
601 hevc_picture_parameter_set_t **pp_pps,
602 hevc_sequence_parameter_set_t **pp_sps,
603 hevc_video_parameter_set_t **pp_vps)
605 decoder_sys_t *p_sys = priv;
606 *pp_sps = NULL;
607 *pp_vps = NULL;
608 if((*pp_pps = p_sys->rg_pps[i_pps_id].p_decoded))
609 if((*pp_sps = p_sys->rg_sps[hevc_get_pps_sps_id(*pp_pps)].p_decoded))
610 *pp_vps = p_sys->rg_vps[hevc_get_sps_vps_id(*pp_sps)].p_decoded;
613 static void ParseStoredSEI( decoder_t *p_dec )
615 decoder_sys_t *p_sys = p_dec->p_sys;
617 for( block_t *p_nal = p_sys->pre.p_chain;
618 p_nal; p_nal = p_nal->p_next )
620 if( p_nal->i_buffer < 5 )
621 continue;
623 if( hevc_getNALType(&p_nal->p_buffer[4]) == HEVC_NAL_PREF_SEI )
625 HxxxParse_AnnexB_SEI( p_nal->p_buffer, p_nal->i_buffer,
626 2 /* nal header */, ParseSEICallback, p_dec );
631 static block_t *ParseVCL(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_frag)
633 decoder_sys_t *p_sys = p_dec->p_sys;
634 block_t *p_outputchain = NULL;
636 const uint8_t *p_buffer = p_frag->p_buffer;
637 size_t i_buffer = p_frag->i_buffer;
639 if(unlikely(!hxxx_strip_AnnexB_startcode(&p_buffer, &i_buffer) || i_buffer < 3))
641 block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag); /* might be corrupted */
642 return NULL;
645 const uint8_t i_layer = hevc_getNALLayer( p_buffer );
646 bool b_first_slice_in_pic = p_buffer[2] & 0x80;
647 if (b_first_slice_in_pic)
649 if(p_sys->frame.p_chain)
651 /* Starting new frame: return previous frame data for output */
652 p_outputchain = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
655 hevc_slice_segment_header_t *p_sli = hevc_decode_slice_header(p_buffer, i_buffer, true,
656 GetXPSSet, p_sys);
657 if(p_sli && i_layer == 0)
659 hevc_sequence_parameter_set_t *p_sps;
660 hevc_picture_parameter_set_t *p_pps;
661 hevc_video_parameter_set_t *p_vps;
662 GetXPSSet(hevc_get_slice_pps_id(p_sli), p_sys, &p_pps, &p_sps, &p_vps);
663 ActivateSets(p_dec, p_pps, p_sps, p_vps);
666 ParseStoredSEI( p_dec );
668 switch(i_nal_type)
670 case HEVC_NAL_BLA_W_LP:
671 case HEVC_NAL_BLA_W_RADL:
672 case HEVC_NAL_BLA_N_LP:
673 case HEVC_NAL_IDR_W_RADL:
674 case HEVC_NAL_IDR_N_LP:
675 case HEVC_NAL_CRA:
676 p_frag->i_flags |= BLOCK_FLAG_TYPE_I;
677 break;
679 default:
681 if(p_sli)
683 enum hevc_slice_type_e type;
684 if(hevc_get_slice_type( p_sli, &type ))
686 if( type == HEVC_SLICE_TYPE_P )
687 p_frag->i_flags |= BLOCK_FLAG_TYPE_P;
688 else
689 p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
692 else p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
694 break;
697 if(p_sli)
698 hevc_rbsp_release_slice_header(p_sli);
701 if(!p_sys->b_init_sequence_complete && i_layer == 0 &&
702 (p_frag->i_flags & BLOCK_FLAG_TYPE_I) && XPSReady(p_sys))
704 p_sys->b_init_sequence_complete = true;
707 if( !p_sys->b_init_sequence_complete )
708 cc_storage_reset( p_sys->p_ccs );
710 block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag);
712 return p_outputchain;
715 static block_t * ParseAUHead(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
717 decoder_sys_t *p_sys = p_dec->p_sys;
718 block_t *p_ret = NULL;
720 if(p_sys->post.p_chain || p_sys->frame.p_chain)
721 p_ret = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
723 switch(i_nal_type)
725 case HEVC_NAL_AUD:
726 if(!p_ret && p_sys->pre.p_chain)
727 p_ret = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
728 break;
730 case HEVC_NAL_VPS:
731 case HEVC_NAL_SPS:
732 case HEVC_NAL_PPS:
734 uint8_t i_id;
735 const uint8_t *p_xps = p_nalb->p_buffer;
736 size_t i_xps = p_nalb->i_buffer;
737 if(hxxx_strip_AnnexB_startcode(&p_xps, &i_xps) &&
738 hevc_get_xps_id(p_nalb->p_buffer, p_nalb->i_buffer, &i_id))
739 InsertXPS(p_dec, i_nal_type, i_id, p_nalb);
740 break;
743 case HEVC_NAL_PREF_SEI:
744 /* stored an parsed later when we get sps & frame */
745 default:
746 break;
749 block_ChainLastAppend(&p_sys->pre.pp_chain_last, p_nalb);
751 return p_ret;
754 static block_t * ParseAUTail(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
756 decoder_sys_t *p_sys = p_dec->p_sys;
757 block_t *p_ret = NULL;
759 block_ChainLastAppend(&p_sys->post.pp_chain_last, p_nalb);
761 switch(i_nal_type)
763 case HEVC_NAL_EOS:
764 case HEVC_NAL_EOB:
765 p_ret = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
766 if( p_ret )
767 p_ret->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
768 break;
770 case HEVC_NAL_SUFF_SEI:
771 HxxxParse_AnnexB_SEI( p_nalb->p_buffer, p_nalb->i_buffer,
772 2 /* nal header */, ParseSEICallback, p_dec );
773 break;
776 if(!p_ret && p_sys->frame.p_chain == NULL)
777 p_ret = OutputQueues(p_sys, false);
779 return p_ret;
782 static block_t * ParseNonVCL(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
784 block_t *p_ret = NULL;
786 if ( (i_nal_type >= HEVC_NAL_VPS && i_nal_type <= HEVC_NAL_AUD) ||
787 i_nal_type == HEVC_NAL_PREF_SEI ||
788 (i_nal_type >= HEVC_NAL_RSV_NVCL41 && i_nal_type <= HEVC_NAL_RSV_NVCL44) ||
789 (i_nal_type >= HEVC_NAL_UNSPEC48 && i_nal_type <= HEVC_NAL_UNSPEC55) )
791 p_ret = ParseAUHead(p_dec, i_nal_type, p_nalb);
793 else
795 p_ret = ParseAUTail(p_dec, i_nal_type, p_nalb);
798 return p_ret;
801 static block_t *GatherAndValidateChain(block_t *p_outputchain)
803 block_t *p_output = NULL;
805 if(p_outputchain)
807 if(p_outputchain->i_flags & BLOCK_FLAG_DROP)
808 p_output = p_outputchain; /* Avoid useless gather */
809 else
810 p_output = block_ChainGather(p_outputchain);
813 if(p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
815 block_ChainRelease(p_output); /* Chain! see above */
816 p_output = NULL;
819 return p_output;
822 static void SetOutputBlockProperties(decoder_t *p_dec, block_t *p_output)
824 decoder_sys_t *p_sys = p_dec->p_sys;
825 /* Set frame duration */
826 if(p_sys->p_active_sps)
828 uint8_t i_num_clock_ts = hevc_get_num_clock_ts(p_sys->p_active_sps,
829 p_sys->p_timing);
830 const mtime_t i_start = date_Get(&p_sys->dts);
831 if( i_start != VLC_TS_INVALID )
833 date_Increment(&p_sys->dts, i_num_clock_ts);
834 p_output->i_length = date_Get(&p_sys->dts) - i_start;
836 p_sys->pts = VLC_TS_INVALID;
838 hevc_release_sei_pic_timing(p_sys->p_timing);
839 p_sys->p_timing = NULL;
842 /*****************************************************************************
843 * ParseNALBlock: parses annexB type NALs
844 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
845 *****************************************************************************/
846 static block_t *ParseNALBlock(decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag)
848 decoder_sys_t *p_sys = p_dec->p_sys;
849 *pb_ts_used = false;
851 if(p_sys->b_need_ts)
853 if(p_frag->i_dts != VLC_TS_INVALID)
854 date_Set(&p_sys->dts, p_frag->i_dts);
855 p_sys->pts = p_frag->i_pts;
856 if(date_Get( &p_sys->dts ) != VLC_TS_INVALID)
857 p_sys->b_need_ts = false;
858 *pb_ts_used = true;
861 if(unlikely(p_frag->i_buffer < 5))
863 msg_Warn(p_dec,"NAL too small");
864 block_Release(p_frag);
865 return NULL;
868 if(p_frag->p_buffer[4] & 0x80)
870 msg_Warn(p_dec,"Forbidden zero bit not null, corrupted NAL");
871 block_Release(p_frag);
872 return GatherAndValidateChain(OutputQueues(p_sys, false)); /* will drop */
875 /* Get NALU type */
876 const mtime_t dts = p_frag->i_dts, pts = p_frag->i_pts;
877 block_t * p_output = NULL;
878 uint8_t i_nal_type = hevc_getNALType(&p_frag->p_buffer[4]);
880 if (i_nal_type < HEVC_NAL_VPS)
882 /* NAL is a VCL NAL */
883 p_output = ParseVCL(p_dec, i_nal_type, p_frag);
884 if (p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
885 msg_Info(p_dec, "Waiting for VPS/SPS/PPS");
887 else
889 p_output = ParseNonVCL(p_dec, i_nal_type, p_frag);
892 p_output = GatherAndValidateChain(p_output);
893 if(p_output)
895 SetOutputBlockProperties( p_dec, p_output );
896 if (dts != VLC_TS_INVALID)
897 date_Set(&p_sys->dts, dts);
898 p_sys->pts = pts;
899 *pb_ts_used = true;
902 return p_output;
905 static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *p_block)
907 decoder_t *p_dec = p_private;
908 decoder_sys_t *p_sys = p_dec->p_sys;
910 /* Remove trailing 0 bytes */
911 while (p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
912 p_block->i_buffer--;
914 p_block = ParseNALBlock( p_dec, pb_ts_used, p_block );
915 if( p_block )
916 cc_storage_commit( p_sys->p_ccs, p_block );
918 return p_block;
921 static int PacketizeValidate( void *p_private, block_t *p_au )
923 VLC_UNUSED(p_private);
924 VLC_UNUSED(p_au);
925 return VLC_SUCCESS;
928 static bool ParseSEICallback( const hxxx_sei_data_t *p_sei_data, void *cbdata )
930 decoder_t *p_dec = (decoder_t *) cbdata;
931 decoder_sys_t *p_sys = p_dec->p_sys;
933 switch( p_sei_data->i_type )
935 case HXXX_SEI_PIC_TIMING:
937 if( p_sys->p_active_sps )
939 hevc_release_sei_pic_timing( p_sys->p_timing );
940 p_sys->p_timing = hevc_decode_sei_pic_timing( p_sei_data->p_bs,
941 p_sys->p_active_sps );
943 } break;
944 case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35:
946 if( p_sei_data->itu_t35.type == HXXX_ITU_T35_TYPE_CC )
948 cc_storage_append( p_sys->p_ccs, true, p_sei_data->itu_t35.u.cc.p_data,
949 p_sei_data->itu_t35.u.cc.i_data );
951 } break;
952 case HXXX_SEI_FRAME_PACKING_ARRANGEMENT:
954 if( p_dec->fmt_in.video.multiview_mode == MULTIVIEW_2D )
956 video_multiview_mode_t mode;
957 switch( p_sei_data->frame_packing.type )
959 case FRAME_PACKING_INTERLEAVED_CHECKERBOARD:
960 mode = MULTIVIEW_STEREO_CHECKERBOARD; break;
961 case FRAME_PACKING_INTERLEAVED_COLUMN:
962 mode = MULTIVIEW_STEREO_COL; break;
963 case FRAME_PACKING_INTERLEAVED_ROW:
964 mode = MULTIVIEW_STEREO_ROW; break;
965 case FRAME_PACKING_SIDE_BY_SIDE:
966 mode = MULTIVIEW_STEREO_SBS; break;
967 case FRAME_PACKING_TOP_BOTTOM:
968 mode = MULTIVIEW_STEREO_TB; break;
969 case FRAME_PACKING_TEMPORAL:
970 mode = MULTIVIEW_STEREO_FRAME; break;
971 case FRAME_PACKING_TILED:
972 default:
973 mode = MULTIVIEW_2D; break;
975 p_dec->fmt_out.video.multiview_mode = mode;
977 } break;
978 case HXXX_SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
980 video_format_t *p_fmt = &p_dec->fmt_out.video;
981 for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.primaries); ++i)
982 p_fmt->mastering.primaries[i] = p_sei_data->colour_volume.primaries[i];
983 for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.white_point); ++i)
984 p_fmt->mastering.white_point[i] = p_sei_data->colour_volume.white_point[i];
985 p_fmt->mastering.max_luminance = p_sei_data->colour_volume.max_luminance;
986 p_fmt->mastering.min_luminance = p_sei_data->colour_volume.min_luminance;
987 } break;
988 case HXXX_SEI_CONTENT_LIGHT_LEVEL:
990 video_format_t *p_fmt = &p_dec->fmt_out.video;
991 p_fmt->lighting.MaxCLL = p_sei_data->content_light_lvl.MaxCLL;
992 p_fmt->lighting.MaxFALL = p_sei_data->content_light_lvl.MaxFALL;
993 } break;
996 return true;