packetizer: hevc: fix divbyzero
[vlc.git] / modules / packetizer / hevc.c
blob3e104ec85998d63939daba51d38718f3baa6e612
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 /*****************************************************************************
47 * Module descriptor
48 *****************************************************************************/
49 static int Open (vlc_object_t *);
50 static void Close(vlc_object_t *);
52 vlc_module_begin ()
53 set_category(CAT_SOUT)
54 set_subcategory(SUBCAT_SOUT_PACKETIZER)
55 set_description(N_("HEVC/H.265 video packetizer"))
56 set_capability("packetizer", 50)
57 set_callbacks(Open, Close)
58 vlc_module_end ()
61 /****************************************************************************
62 * Local prototypes
63 ****************************************************************************/
64 static block_t *PacketizeAnnexB(decoder_t *, block_t **);
65 static block_t *PacketizeHVC1(decoder_t *, block_t **);
66 static void PacketizeFlush( decoder_t * );
67 static void PacketizeReset(void *p_private, bool b_broken);
68 static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *);
69 static block_t *ParseNALBlock(decoder_t *, bool *pb_ts_used, block_t *);
70 static int PacketizeValidate(void *p_private, block_t *);
71 static bool ParseSEICallback( const hxxx_sei_data_t *, void * );
72 static block_t *GetCc( decoder_t *, bool pb_present[4], int * );
74 struct decoder_sys_t
76 /* */
77 packetizer_t packetizer;
79 struct
81 block_t *p_chain;
82 block_t **pp_chain_last;
83 } frame, pre, post;
85 uint8_t i_nal_length_size;
86 hevc_video_parameter_set_t *rgi_p_decvps[HEVC_VPS_ID_MAX + 1];
87 hevc_sequence_parameter_set_t *rgi_p_decsps[HEVC_SPS_ID_MAX + 1];
88 hevc_picture_parameter_set_t *rgi_p_decpps[HEVC_PPS_ID_MAX + 1];
89 const hevc_video_parameter_set_t *p_active_vps;
90 const hevc_sequence_parameter_set_t *p_active_sps;
91 const hevc_picture_parameter_set_t *p_active_pps;
92 hevc_sei_pic_timing_t *p_timing;
93 bool b_init_sequence_complete;
95 date_t dts;
96 mtime_t pts;
97 bool b_need_ts;
99 /* */
100 cc_storage_t *p_ccs;
103 #define BLOCK_FLAG_DROP (1 << BLOCK_FLAG_PRIVATE_SHIFT)
105 static const uint8_t p_hevc_startcode[3] = {0x00, 0x00, 0x01};
106 /****************************************************************************
107 * Helpers
108 ****************************************************************************/
109 static inline void InitQueue( block_t **pp_head, block_t ***ppp_tail )
111 *pp_head = NULL;
112 *ppp_tail = pp_head;
114 #define INITQ(name) InitQueue(&p_sys->name.p_chain, &p_sys->name.pp_chain_last)
116 static block_t * OutputQueues(decoder_sys_t *p_sys, bool b_valid)
118 block_t *p_output = NULL;
119 block_t **pp_output_last = &p_output;
120 uint32_t i_flags = 0; /* Because block_ChainGather does not merge flags or times */
122 if(p_sys->pre.p_chain)
124 i_flags |= p_sys->pre.p_chain->i_flags;
125 block_ChainLastAppend(&pp_output_last, p_sys->pre.p_chain);
126 INITQ(pre);
129 if(p_sys->frame.p_chain)
131 i_flags |= p_sys->frame.p_chain->i_flags;
132 block_ChainLastAppend(&pp_output_last, p_sys->frame.p_chain);
133 p_output->i_dts = date_Get(&p_sys->dts);
134 p_output->i_pts = p_sys->pts;
135 INITQ(frame);
138 if(p_sys->post.p_chain)
140 i_flags |= p_sys->post.p_chain->i_flags;
141 block_ChainLastAppend(&pp_output_last, p_sys->post.p_chain);
142 INITQ(post);
145 if(p_output)
147 p_output->i_flags |= i_flags;
148 if(!b_valid)
149 p_output->i_flags |= BLOCK_FLAG_DROP;
152 return p_output;
156 /*****************************************************************************
157 * Open
158 *****************************************************************************/
159 static int Open(vlc_object_t *p_this)
161 decoder_t *p_dec = (decoder_t*)p_this;
162 decoder_sys_t *p_sys;
164 if (p_dec->fmt_in.i_codec != VLC_CODEC_HEVC)
165 return VLC_EGENERIC;
167 p_dec->p_sys = p_sys = calloc(1, sizeof(decoder_sys_t));
168 if (!p_dec->p_sys)
169 return VLC_ENOMEM;
171 p_sys->p_ccs = cc_storage_new();
172 if(unlikely(!p_sys->p_ccs))
174 free(p_dec->p_sys);
175 return VLC_ENOMEM;
178 INITQ(pre);
179 INITQ(frame);
180 INITQ(post);
182 packetizer_Init(&p_dec->p_sys->packetizer,
183 p_hevc_startcode, sizeof(p_hevc_startcode), startcode_FindAnnexB,
184 p_hevc_startcode, 1, 5,
185 PacketizeReset, PacketizeParse, PacketizeValidate, p_dec);
187 /* Copy properties */
188 es_format_Copy(&p_dec->fmt_out, &p_dec->fmt_in);
189 p_dec->fmt_out.b_packetized = true;
191 /* Init timings */
192 if( p_dec->fmt_in.video.i_frame_rate_base &&
193 p_dec->fmt_in.video.i_frame_rate )
194 date_Init( &p_sys->dts, p_dec->fmt_in.video.i_frame_rate * 2,
195 p_dec->fmt_in.video.i_frame_rate_base );
196 else
197 date_Init( &p_sys->dts, 2 * 30000, 1001 );
198 date_Set( &p_sys->dts, VLC_TS_INVALID );
199 p_sys->pts = VLC_TS_INVALID;
200 p_sys->b_need_ts = true;
202 /* Set callbacks */
203 const uint8_t *p_extra = p_dec->fmt_in.p_extra;
204 const size_t i_extra = p_dec->fmt_in.i_extra;
205 /* Check if we have hvcC as extradata */
206 if(hevc_ishvcC(p_extra, i_extra))
208 p_dec->pf_packetize = PacketizeHVC1;
210 /* Clear hvcC/HVC1 extra, to be replaced with AnnexB */
211 free(p_dec->fmt_out.p_extra);
212 p_dec->fmt_out.i_extra = 0;
214 size_t i_new_extra = 0;
215 p_dec->fmt_out.p_extra =
216 hevc_hvcC_to_AnnexB_NAL(p_extra, i_extra,
217 &i_new_extra, &p_sys->i_nal_length_size);
218 if(p_dec->fmt_out.p_extra)
219 p_dec->fmt_out.i_extra = i_new_extra;
221 else
223 p_dec->pf_packetize = PacketizeAnnexB;
225 p_dec->pf_flush = PacketizeFlush;
226 p_dec->pf_get_cc = GetCc;
228 if(p_dec->fmt_out.i_extra)
230 /* Feed with AnnexB VPS/SPS/PPS/SEI extradata */
231 packetizer_Header(&p_sys->packetizer,
232 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra);
235 return VLC_SUCCESS;
238 /*****************************************************************************
239 * Close
240 *****************************************************************************/
241 static void Close(vlc_object_t *p_this)
243 decoder_t *p_dec = (decoder_t*)p_this;
244 decoder_sys_t *p_sys = p_dec->p_sys;
245 packetizer_Clean(&p_sys->packetizer);
247 block_ChainRelease(p_sys->frame.p_chain);
248 block_ChainRelease(p_sys->pre.p_chain);
249 block_ChainRelease(p_sys->post.p_chain);
251 for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
253 if(p_sys->rgi_p_decpps[i])
254 hevc_rbsp_release_pps(p_sys->rgi_p_decpps[i]);
257 for(unsigned i=0;i<=HEVC_SPS_ID_MAX; i++)
259 if(p_sys->rgi_p_decsps[i])
260 hevc_rbsp_release_sps(p_sys->rgi_p_decsps[i]);
263 for(unsigned i=0;i<=HEVC_VPS_ID_MAX; i++)
265 if(p_sys->rgi_p_decvps[i])
266 hevc_rbsp_release_vps(p_sys->rgi_p_decvps[i]);
269 hevc_release_sei_pic_timing( p_sys->p_timing );
271 cc_storage_delete( p_sys->p_ccs );
273 free(p_sys);
276 /****************************************************************************
277 * Packetize
278 ****************************************************************************/
279 static block_t *PacketizeHVC1(decoder_t *p_dec, block_t **pp_block)
281 decoder_sys_t *p_sys = p_dec->p_sys;
283 return PacketizeXXC1( p_dec, p_sys->i_nal_length_size,
284 pp_block, ParseNALBlock );
287 static block_t *PacketizeAnnexB(decoder_t *p_dec, block_t **pp_block)
289 decoder_sys_t *p_sys = p_dec->p_sys;
291 return packetizer_Packetize(&p_sys->packetizer, pp_block);
294 static void PacketizeFlush( decoder_t *p_dec )
296 decoder_sys_t *p_sys = p_dec->p_sys;
298 packetizer_Flush( &p_sys->packetizer );
301 /*****************************************************************************
302 * GetCc:
303 *****************************************************************************/
304 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4], int *pi_reorder_depth )
306 *pi_reorder_depth = 0;
307 return cc_storage_get_current( p_dec->p_sys->p_ccs, pb_present );
310 /****************************************************************************
311 * Packetizer Helpers
312 ****************************************************************************/
313 static void PacketizeReset(void *p_private, bool b_broken)
315 VLC_UNUSED(b_broken);
317 decoder_t *p_dec = p_private;
318 decoder_sys_t *p_sys = p_dec->p_sys;
320 block_t *p_out = OutputQueues(p_sys, false);
321 if(p_out)
322 block_ChainRelease(p_out);
324 p_sys->b_init_sequence_complete = false;
325 p_sys->b_need_ts = true;
326 date_Set(&p_sys->dts, VLC_TS_INVALID);
329 static bool InsertXPS(decoder_t *p_dec, uint8_t i_nal_type, uint8_t i_id,
330 const block_t *p_nalb)
332 decoder_sys_t *p_sys = p_dec->p_sys;
333 bool b_active = false;
335 switch(i_nal_type)
337 case HEVC_NAL_VPS:
338 if(i_id > HEVC_VPS_ID_MAX)
339 return false;
340 break;
341 case HEVC_NAL_SPS:
342 if(i_id > HEVC_SPS_ID_MAX)
343 return false;
344 break;
345 case HEVC_NAL_PPS:
346 if(i_id > HEVC_PPS_ID_MAX)
347 return false;
348 break;
349 default:
350 return false;
353 /* Free associated decoded version */
354 if(i_nal_type == HEVC_NAL_SPS && p_sys->rgi_p_decsps[i_id])
356 hevc_rbsp_release_sps(p_sys->rgi_p_decsps[i_id]);
357 if(p_sys->p_active_sps == p_sys->rgi_p_decsps[i_id])
359 p_sys->p_active_sps = NULL;
360 b_active = true;
362 p_sys->rgi_p_decsps[i_id] = NULL;
364 else if(i_nal_type == HEVC_NAL_PPS && p_sys->rgi_p_decpps[i_id])
366 hevc_rbsp_release_pps(p_sys->rgi_p_decpps[i_id]);
367 if(p_sys->p_active_pps == p_sys->rgi_p_decpps[i_id])
369 p_sys->p_active_pps = NULL;
370 b_active = true;
372 p_sys->rgi_p_decpps[i_id] = NULL;
374 else if(i_nal_type == HEVC_NAL_VPS && p_sys->rgi_p_decvps[i_id])
376 hevc_rbsp_release_vps(p_sys->rgi_p_decvps[i_id]);
377 if(p_sys->p_active_vps == p_sys->rgi_p_decvps[i_id])
379 p_sys->p_active_vps = NULL;
380 b_active = true;
382 p_sys->rgi_p_decvps[i_id] = NULL;
385 const uint8_t *p_buffer = p_nalb->p_buffer;
386 size_t i_buffer = p_nalb->i_buffer;
387 if( hxxx_strip_AnnexB_startcode( &p_buffer, &i_buffer ) )
389 /* Create decoded entries */
390 if(i_nal_type == HEVC_NAL_SPS)
392 p_sys->rgi_p_decsps[i_id] = hevc_decode_sps(p_buffer, i_buffer, true);
393 if(!p_sys->rgi_p_decsps[i_id])
395 msg_Err(p_dec, "Failed decoding SPS id %d", i_id);
396 return false;
398 if(b_active)
399 p_sys->p_active_sps = p_sys->rgi_p_decsps[i_id];
401 else if(i_nal_type == HEVC_NAL_PPS)
403 p_sys->rgi_p_decpps[i_id] = hevc_decode_pps(p_buffer, i_buffer, true);
404 if(!p_sys->rgi_p_decpps[i_id])
406 msg_Err(p_dec, "Failed decoding PPS id %d", i_id);
407 return false;
409 if(b_active)
410 p_sys->p_active_pps = p_sys->rgi_p_decpps[i_id];
412 else if(i_nal_type == HEVC_NAL_VPS)
414 p_sys->rgi_p_decvps[i_id] = hevc_decode_vps(p_buffer, i_buffer, true);
415 if(!p_sys->rgi_p_decvps[i_id])
417 msg_Err(p_dec, "Failed decoding VPS id %d", i_id);
418 return false;
420 if(b_active)
421 p_sys->p_active_vps = p_sys->rgi_p_decvps[i_id];
423 return true;
427 return false;
430 static bool XPSReady(decoder_sys_t *p_sys)
432 for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
434 const hevc_picture_parameter_set_t *p_pps = p_sys->rgi_p_decpps[i];
435 if (p_pps)
437 uint8_t id_sps = hevc_get_pps_sps_id(p_pps);
438 const hevc_sequence_parameter_set_t *p_sps = p_sys->rgi_p_decsps[id_sps];
439 if(p_sps)
441 uint8_t id_vps = hevc_get_sps_vps_id(p_sps);
442 if(p_sys->rgi_p_decvps[id_vps])
443 return true;
447 return false;
450 static void ActivateSets(decoder_t *p_dec,
451 const hevc_picture_parameter_set_t *p_pps,
452 const hevc_sequence_parameter_set_t *p_sps,
453 const hevc_video_parameter_set_t *p_vps)
455 decoder_sys_t *p_sys = p_dec->p_sys;
456 p_sys->p_active_pps = p_pps;
457 p_sys->p_active_sps = p_sps;
458 p_sys->p_active_vps = p_vps;
459 if(p_sps)
461 if(!p_dec->fmt_in.video.i_frame_rate || !p_dec->fmt_in.video.i_frame_rate_base)
463 unsigned num, den;
464 if(hevc_get_frame_rate( p_sps, p_dec->p_sys->rgi_p_decvps, &num, &den ))
466 p_dec->fmt_out.video.i_frame_rate = num;
467 p_dec->fmt_out.video.i_frame_rate_base = den;
468 if(p_sys->dts.i_divider_den != den && p_sys->dts.i_divider_num != 2 * num)
469 date_Change(&p_sys->dts, 2 * num, den);
473 if(p_dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF)
475 (void) hevc_get_colorimetry( p_sps,
476 &p_dec->fmt_out.video.primaries,
477 &p_dec->fmt_out.video.transfer,
478 &p_dec->fmt_out.video.space,
479 &p_dec->fmt_out.video.b_color_range_full);
482 unsigned sizes[4];
483 if( hevc_get_picture_size( p_sps, &sizes[0], &sizes[1],
484 &sizes[2], &sizes[3] ) )
486 p_dec->fmt_out.video.i_width = sizes[0];
487 p_dec->fmt_out.video.i_height = sizes[1];
488 if(p_dec->fmt_in.video.i_visible_width == 0)
490 p_dec->fmt_out.video.i_visible_width = sizes[2];
491 p_dec->fmt_out.video.i_visible_height = sizes[3];
495 if(p_dec->fmt_in.i_profile == -1)
497 uint8_t i_profile, i_level;
498 if( hevc_get_sps_profile_tier_level( p_sps, &i_profile, &i_level ) )
500 p_dec->fmt_out.i_profile = i_profile;
501 p_dec->fmt_out.i_level = i_level;
507 static void GetXPSSet(uint8_t i_pps_id, void *priv,
508 hevc_picture_parameter_set_t **pp_pps,
509 hevc_sequence_parameter_set_t **pp_sps,
510 hevc_video_parameter_set_t **pp_vps)
512 decoder_sys_t *p_sys = priv;
513 *pp_sps = NULL;
514 *pp_vps = NULL;
515 if((*pp_pps = p_sys->rgi_p_decpps[i_pps_id]))
516 if((*pp_sps = p_sys->rgi_p_decsps[hevc_get_pps_sps_id(*pp_pps)]))
517 *pp_vps = p_sys->rgi_p_decvps[hevc_get_sps_vps_id(*pp_sps)];
520 static void ParseStoredSEI( decoder_t *p_dec )
522 decoder_sys_t *p_sys = p_dec->p_sys;
524 for( block_t *p_nal = p_sys->pre.p_chain;
525 p_nal; p_nal = p_nal->p_next )
527 if( p_nal->i_buffer < 5 )
528 continue;
530 if( hevc_getNALType(&p_nal->p_buffer[4]) == HEVC_NAL_PREF_SEI )
532 HxxxParse_AnnexB_SEI( p_nal->p_buffer, p_nal->i_buffer,
533 2 /* nal header */, ParseSEICallback, p_dec );
538 static block_t *ParseVCL(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_frag)
540 decoder_sys_t *p_sys = p_dec->p_sys;
541 block_t *p_outputchain = NULL;
543 const uint8_t *p_buffer = p_frag->p_buffer;
544 size_t i_buffer = p_frag->i_buffer;
546 if(unlikely(!hxxx_strip_AnnexB_startcode(&p_buffer, &i_buffer) || i_buffer < 3))
548 block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag); /* might be corrupted */
549 return NULL;
552 const uint8_t i_layer = hevc_getNALLayer( p_buffer );
553 bool b_first_slice_in_pic = p_buffer[2] & 0x80;
554 if (b_first_slice_in_pic)
556 if(p_sys->frame.p_chain)
558 /* Starting new frame: return previous frame data for output */
559 p_outputchain = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
562 hevc_slice_segment_header_t *p_sli = hevc_decode_slice_header(p_buffer, i_buffer, true,
563 GetXPSSet, p_sys);
564 if(p_sli && i_layer == 0)
566 hevc_sequence_parameter_set_t *p_sps;
567 hevc_picture_parameter_set_t *p_pps;
568 hevc_video_parameter_set_t *p_vps;
569 GetXPSSet(hevc_get_slice_pps_id(p_sli), p_sys, &p_pps, &p_sps, &p_vps);
570 ActivateSets(p_dec, p_pps, p_sps, p_vps);
573 ParseStoredSEI( p_dec );
575 switch(i_nal_type)
577 case HEVC_NAL_BLA_W_LP:
578 case HEVC_NAL_BLA_W_RADL:
579 case HEVC_NAL_BLA_N_LP:
580 case HEVC_NAL_IDR_W_RADL:
581 case HEVC_NAL_IDR_N_LP:
582 case HEVC_NAL_CRA:
583 p_frag->i_flags |= BLOCK_FLAG_TYPE_I;
584 break;
586 default:
588 if(p_sli)
590 enum hevc_slice_type_e type;
591 if(hevc_get_slice_type( p_sli, &type ))
593 if( type == HEVC_SLICE_TYPE_P )
594 p_frag->i_flags |= BLOCK_FLAG_TYPE_P;
595 else
596 p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
599 else p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
601 break;
604 if(p_sli)
605 hevc_rbsp_release_slice_header(p_sli);
608 if(!p_sys->b_init_sequence_complete && i_layer == 0 &&
609 (p_frag->i_flags & BLOCK_FLAG_TYPE_I) && XPSReady(p_sys))
611 p_sys->b_init_sequence_complete = true;
614 if( !p_sys->b_init_sequence_complete )
615 cc_storage_reset( p_sys->p_ccs );
617 block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag);
619 return p_outputchain;
622 static block_t * ParseAUHead(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
624 decoder_sys_t *p_sys = p_dec->p_sys;
625 block_t *p_ret = NULL;
627 if(p_sys->post.p_chain || p_sys->frame.p_chain)
628 p_ret = OutputQueues(p_sys, true);
630 switch(i_nal_type)
632 case HEVC_NAL_AUD:
633 if(!p_ret && p_sys->pre.p_chain)
634 p_ret = OutputQueues(p_sys, true);
635 break;
637 case HEVC_NAL_VPS:
638 case HEVC_NAL_SPS:
639 case HEVC_NAL_PPS:
641 uint8_t i_id;
642 if(hevc_get_xps_id(p_nalb->p_buffer, p_nalb->i_buffer, &i_id))
643 InsertXPS(p_dec, i_nal_type, i_id, p_nalb);
644 break;
647 case HEVC_NAL_PREF_SEI:
648 /* stored an parsed later when we get sps & frame */
649 default:
650 break;
653 block_ChainLastAppend(&p_sys->pre.pp_chain_last, p_nalb);
655 return p_ret;
658 static block_t * ParseAUTail(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
660 decoder_sys_t *p_sys = p_dec->p_sys;
661 block_t *p_ret = NULL;
663 block_ChainLastAppend(&p_sys->post.pp_chain_last, p_nalb);
665 switch(i_nal_type)
667 case HEVC_NAL_EOS:
668 case HEVC_NAL_EOB:
669 p_ret = OutputQueues(p_sys, true);
670 if( p_ret )
671 p_ret->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
672 break;
674 case HEVC_NAL_SUFF_SEI:
675 HxxxParse_AnnexB_SEI( p_nalb->p_buffer, p_nalb->i_buffer,
676 2 /* nal header */, ParseSEICallback, p_dec );
677 break;
680 if(!p_ret && p_sys->frame.p_chain == NULL)
681 p_ret = OutputQueues(p_sys, false);
683 return p_ret;
686 static block_t * ParseNonVCL(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
688 block_t *p_ret = NULL;
690 if ( (i_nal_type >= HEVC_NAL_VPS && i_nal_type <= HEVC_NAL_AUD) ||
691 i_nal_type == HEVC_NAL_PREF_SEI ||
692 (i_nal_type >= HEVC_NAL_RSV_NVCL41 && i_nal_type <= HEVC_NAL_RSV_NVCL44) ||
693 (i_nal_type >= HEVC_NAL_UNSPEC48 && i_nal_type <= HEVC_NAL_UNSPEC55) )
695 p_ret = ParseAUHead(p_dec, i_nal_type, p_nalb);
697 else
699 p_ret = ParseAUTail(p_dec, i_nal_type, p_nalb);
702 return p_ret;
705 static block_t *GatherAndValidateChain(block_t *p_outputchain)
707 block_t *p_output = NULL;
709 if(p_outputchain)
711 if(p_outputchain->i_flags & BLOCK_FLAG_DROP)
712 p_output = p_outputchain; /* Avoid useless gather */
713 else
714 p_output = block_ChainGather(p_outputchain);
717 if(p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
719 block_ChainRelease(p_output); /* Chain! see above */
720 p_output = NULL;
723 return p_output;
726 static void SetOutputBlockProperties(decoder_t *p_dec, block_t *p_output)
728 decoder_sys_t *p_sys = p_dec->p_sys;
729 /* Set frame duration */
730 if(p_sys->p_active_sps)
732 uint8_t i_num_clock_ts = hevc_get_num_clock_ts(p_sys->p_active_sps,
733 p_sys->p_timing);
734 const mtime_t i_start = date_Get(&p_sys->dts);
735 if( i_start != VLC_TS_INVALID )
737 date_Increment(&p_sys->dts, i_num_clock_ts);
738 p_output->i_length = date_Get(&p_sys->dts) - i_start;
740 p_sys->pts = VLC_TS_INVALID;
742 hevc_release_sei_pic_timing(p_sys->p_timing);
743 p_sys->p_timing = NULL;
746 /*****************************************************************************
747 * ParseNALBlock: parses annexB type NALs
748 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
749 *****************************************************************************/
750 static block_t *ParseNALBlock(decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag)
752 decoder_sys_t *p_sys = p_dec->p_sys;
753 *pb_ts_used = false;
755 if(p_sys->b_need_ts)
757 if(p_frag->i_dts > VLC_TS_INVALID)
758 date_Set(&p_sys->dts, p_frag->i_dts);
759 p_sys->pts = p_frag->i_pts;
760 if(date_Get( &p_sys->dts ) != VLC_TS_INVALID)
761 p_sys->b_need_ts = false;
762 *pb_ts_used = true;
765 if(unlikely(p_frag->i_buffer < 5))
767 msg_Warn(p_dec,"NAL too small");
768 block_Release(p_frag);
769 return NULL;
772 if(p_frag->p_buffer[4] & 0x80)
774 msg_Warn(p_dec,"Forbidden zero bit not null, corrupted NAL");
775 block_Release(p_frag);
776 return GatherAndValidateChain(OutputQueues(p_sys, false)); /* will drop */
779 /* Get NALU type */
780 block_t * p_output = NULL;
781 uint8_t i_nal_type = hevc_getNALType(&p_frag->p_buffer[4]);
782 if (i_nal_type < HEVC_NAL_VPS)
784 /* NAL is a VCL NAL */
785 p_output = ParseVCL(p_dec, i_nal_type, p_frag);
786 if (p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
787 msg_Info(p_dec, "Waiting for VPS/SPS/PPS");
789 else
791 p_output = ParseNonVCL(p_dec, i_nal_type, p_frag);
794 p_output = GatherAndValidateChain(p_output);
795 if(p_output)
797 SetOutputBlockProperties( p_dec, p_output );
798 if(p_frag->i_dts > VLC_TS_INVALID)
799 date_Set(&p_sys->dts, p_frag->i_dts);
800 p_sys->pts = p_frag->i_pts;
801 *pb_ts_used = true;
804 return p_output;
807 static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *p_block)
809 decoder_t *p_dec = p_private;
810 decoder_sys_t *p_sys = p_dec->p_sys;
812 /* Remove trailing 0 bytes */
813 while (p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
814 p_block->i_buffer--;
816 p_block = ParseNALBlock( p_dec, pb_ts_used, p_block );
817 if( p_block )
818 cc_storage_commit( p_sys->p_ccs, p_block );
820 return p_block;
823 static int PacketizeValidate( void *p_private, block_t *p_au )
825 VLC_UNUSED(p_private);
826 VLC_UNUSED(p_au);
827 return VLC_SUCCESS;
830 static bool ParseSEICallback( const hxxx_sei_data_t *p_sei_data, void *cbdata )
832 decoder_t *p_dec = (decoder_t *) cbdata;
833 decoder_sys_t *p_sys = p_dec->p_sys;
835 switch( p_sei_data->i_type )
837 case HXXX_SEI_PIC_TIMING:
839 if( p_sys->p_active_sps )
841 hevc_release_sei_pic_timing( p_sys->p_timing );
842 p_sys->p_timing = hevc_decode_sei_pic_timing( p_sei_data->p_bs,
843 p_sys->p_active_sps );
845 } break;
846 case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35:
848 if( p_sei_data->itu_t35.type == HXXX_ITU_T35_TYPE_CC )
850 cc_storage_append( p_sys->p_ccs, true, p_sei_data->itu_t35.u.cc.p_data,
851 p_sei_data->itu_t35.u.cc.i_data );
853 } break;
854 case HXXX_SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
856 video_format_t *p_fmt = &p_dec->fmt_out.video;
857 for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.primaries); ++i)
858 p_fmt->mastering.primaries[i] = p_sei_data->colour_volume.primaries[i];
859 for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.white_point); ++i)
860 p_fmt->mastering.white_point[i] = p_sei_data->colour_volume.white_point[i];
861 p_fmt->mastering.max_luminance = p_sei_data->colour_volume.max_luminance;
862 p_fmt->mastering.min_luminance = p_sei_data->colour_volume.min_luminance;
863 } break;
864 case HXXX_SEI_CONTENT_LIGHT_LEVEL:
866 video_format_t *p_fmt = &p_dec->fmt_out.video;
867 p_fmt->lighting.MaxCLL = p_sei_data->content_light_lvl.MaxCLL;
868 p_fmt->lighting.MaxFALL = p_sei_data->content_light_lvl.MaxFALL;
869 } break;
872 return true;