packetizer: hevc: set multiview
[vlc.git] / modules / packetizer / hevc.c
blob73e8bc68866c1d81f806c57039e0b3671286271a
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 *, bool pb_present[4], int * );
76 struct decoder_sys_t
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;
88 hevc_video_parameter_set_t *rgi_p_decvps[HEVC_VPS_ID_MAX + 1];
89 hevc_sequence_parameter_set_t *rgi_p_decsps[HEVC_SPS_ID_MAX + 1];
90 hevc_picture_parameter_set_t *rgi_p_decpps[HEVC_PPS_ID_MAX + 1];
91 const hevc_video_parameter_set_t *p_active_vps;
92 const hevc_sequence_parameter_set_t *p_active_sps;
93 const hevc_picture_parameter_set_t *p_active_pps;
94 hevc_sei_pic_timing_t *p_timing;
95 bool b_init_sequence_complete;
97 date_t dts;
98 mtime_t pts;
99 bool b_need_ts;
101 /* */
102 cc_storage_t *p_ccs;
105 #define BLOCK_FLAG_DROP (1 << BLOCK_FLAG_PRIVATE_SHIFT)
107 static const uint8_t p_hevc_startcode[3] = {0x00, 0x00, 0x01};
108 /****************************************************************************
109 * Helpers
110 ****************************************************************************/
111 static inline void InitQueue( block_t **pp_head, block_t ***ppp_tail )
113 *pp_head = NULL;
114 *ppp_tail = pp_head;
116 #define INITQ(name) InitQueue(&p_sys->name.p_chain, &p_sys->name.pp_chain_last)
118 static block_t * OutputQueues(decoder_sys_t *p_sys, bool b_valid)
120 block_t *p_output = NULL;
121 block_t **pp_output_last = &p_output;
122 uint32_t i_flags = 0; /* Because block_ChainGather does not merge flags or times */
124 if(p_sys->pre.p_chain)
126 i_flags |= p_sys->pre.p_chain->i_flags;
127 block_ChainLastAppend(&pp_output_last, p_sys->pre.p_chain);
128 INITQ(pre);
131 if(p_sys->frame.p_chain)
133 i_flags |= p_sys->frame.p_chain->i_flags;
134 block_ChainLastAppend(&pp_output_last, p_sys->frame.p_chain);
135 p_output->i_dts = date_Get(&p_sys->dts);
136 p_output->i_pts = p_sys->pts;
137 INITQ(frame);
140 if(p_sys->post.p_chain)
142 i_flags |= p_sys->post.p_chain->i_flags;
143 block_ChainLastAppend(&pp_output_last, p_sys->post.p_chain);
144 INITQ(post);
147 if(p_output)
149 p_output->i_flags |= i_flags;
150 if(!b_valid)
151 p_output->i_flags |= BLOCK_FLAG_DROP;
154 return p_output;
158 /*****************************************************************************
159 * Open
160 *****************************************************************************/
161 static int Open(vlc_object_t *p_this)
163 decoder_t *p_dec = (decoder_t*)p_this;
164 decoder_sys_t *p_sys;
166 if (p_dec->fmt_in.i_codec != VLC_CODEC_HEVC)
167 return VLC_EGENERIC;
169 p_dec->p_sys = p_sys = calloc(1, sizeof(decoder_sys_t));
170 if (!p_dec->p_sys)
171 return VLC_ENOMEM;
173 p_sys->p_ccs = cc_storage_new();
174 if(unlikely(!p_sys->p_ccs))
176 free(p_dec->p_sys);
177 return VLC_ENOMEM;
180 INITQ(pre);
181 INITQ(frame);
182 INITQ(post);
184 packetizer_Init(&p_dec->p_sys->packetizer,
185 p_hevc_startcode, sizeof(p_hevc_startcode), startcode_FindAnnexB,
186 p_hevc_startcode, 1, 5,
187 PacketizeReset, PacketizeParse, PacketizeValidate, p_dec);
189 /* Copy properties */
190 es_format_Copy(&p_dec->fmt_out, &p_dec->fmt_in);
191 p_dec->fmt_out.b_packetized = true;
193 /* Init timings */
194 if( p_dec->fmt_in.video.i_frame_rate_base &&
195 p_dec->fmt_in.video.i_frame_rate &&
196 p_dec->fmt_in.video.i_frame_rate <= UINT_MAX / 2 )
197 date_Init( &p_sys->dts, p_dec->fmt_in.video.i_frame_rate * 2,
198 p_dec->fmt_in.video.i_frame_rate_base );
199 else
200 date_Init( &p_sys->dts, 2 * 30000, 1001 );
201 date_Set( &p_sys->dts, VLC_TS_INVALID );
202 p_sys->pts = VLC_TS_INVALID;
203 p_sys->b_need_ts = true;
205 /* Set callbacks */
206 const uint8_t *p_extra = p_dec->fmt_in.p_extra;
207 const size_t i_extra = p_dec->fmt_in.i_extra;
208 /* Check if we have hvcC as extradata */
209 if(hevc_ishvcC(p_extra, i_extra))
211 p_dec->pf_packetize = PacketizeHVC1;
213 /* Clear hvcC/HVC1 extra, to be replaced with AnnexB */
214 free(p_dec->fmt_out.p_extra);
215 p_dec->fmt_out.i_extra = 0;
217 size_t i_new_extra = 0;
218 p_dec->fmt_out.p_extra =
219 hevc_hvcC_to_AnnexB_NAL(p_extra, i_extra,
220 &i_new_extra, &p_sys->i_nal_length_size);
221 if(p_dec->fmt_out.p_extra)
222 p_dec->fmt_out.i_extra = i_new_extra;
224 else
226 p_dec->pf_packetize = PacketizeAnnexB;
228 p_dec->pf_flush = PacketizeFlush;
229 p_dec->pf_get_cc = GetCc;
231 if(p_dec->fmt_out.i_extra)
233 /* Feed with AnnexB VPS/SPS/PPS/SEI extradata */
234 packetizer_Header(&p_sys->packetizer,
235 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra);
238 return VLC_SUCCESS;
241 /*****************************************************************************
242 * Close
243 *****************************************************************************/
244 static void Close(vlc_object_t *p_this)
246 decoder_t *p_dec = (decoder_t*)p_this;
247 decoder_sys_t *p_sys = p_dec->p_sys;
248 packetizer_Clean(&p_sys->packetizer);
250 block_ChainRelease(p_sys->frame.p_chain);
251 block_ChainRelease(p_sys->pre.p_chain);
252 block_ChainRelease(p_sys->post.p_chain);
254 for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
256 if(p_sys->rgi_p_decpps[i])
257 hevc_rbsp_release_pps(p_sys->rgi_p_decpps[i]);
260 for(unsigned i=0;i<=HEVC_SPS_ID_MAX; i++)
262 if(p_sys->rgi_p_decsps[i])
263 hevc_rbsp_release_sps(p_sys->rgi_p_decsps[i]);
266 for(unsigned i=0;i<=HEVC_VPS_ID_MAX; i++)
268 if(p_sys->rgi_p_decvps[i])
269 hevc_rbsp_release_vps(p_sys->rgi_p_decvps[i]);
272 hevc_release_sei_pic_timing( p_sys->p_timing );
274 cc_storage_delete( p_sys->p_ccs );
276 free(p_sys);
279 /****************************************************************************
280 * Packetize
281 ****************************************************************************/
282 static block_t *PacketizeHVC1(decoder_t *p_dec, block_t **pp_block)
284 decoder_sys_t *p_sys = p_dec->p_sys;
286 return PacketizeXXC1( p_dec, p_sys->i_nal_length_size,
287 pp_block, ParseNALBlock );
290 static block_t *PacketizeAnnexB(decoder_t *p_dec, block_t **pp_block)
292 decoder_sys_t *p_sys = p_dec->p_sys;
294 return packetizer_Packetize(&p_sys->packetizer, pp_block);
297 static void PacketizeFlush( decoder_t *p_dec )
299 decoder_sys_t *p_sys = p_dec->p_sys;
301 packetizer_Flush( &p_sys->packetizer );
304 /*****************************************************************************
305 * GetCc:
306 *****************************************************************************/
307 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4], int *pi_reorder_depth )
309 *pi_reorder_depth = 0;
310 return cc_storage_get_current( p_dec->p_sys->p_ccs, pb_present );
313 /****************************************************************************
314 * Packetizer Helpers
315 ****************************************************************************/
316 static void PacketizeReset(void *p_private, bool b_broken)
318 VLC_UNUSED(b_broken);
320 decoder_t *p_dec = p_private;
321 decoder_sys_t *p_sys = p_dec->p_sys;
323 block_t *p_out = OutputQueues(p_sys, false);
324 if(p_out)
325 block_ChainRelease(p_out);
327 p_sys->b_init_sequence_complete = false;
328 p_sys->b_need_ts = true;
329 date_Set(&p_sys->dts, VLC_TS_INVALID);
332 static bool InsertXPS(decoder_t *p_dec, uint8_t i_nal_type, uint8_t i_id,
333 const block_t *p_nalb)
335 decoder_sys_t *p_sys = p_dec->p_sys;
336 bool b_active = false;
338 switch(i_nal_type)
340 case HEVC_NAL_VPS:
341 if(i_id > HEVC_VPS_ID_MAX)
342 return false;
343 break;
344 case HEVC_NAL_SPS:
345 if(i_id > HEVC_SPS_ID_MAX)
346 return false;
347 break;
348 case HEVC_NAL_PPS:
349 if(i_id > HEVC_PPS_ID_MAX)
350 return false;
351 break;
352 default:
353 return false;
356 /* Free associated decoded version */
357 if(i_nal_type == HEVC_NAL_SPS && p_sys->rgi_p_decsps[i_id])
359 hevc_rbsp_release_sps(p_sys->rgi_p_decsps[i_id]);
360 if(p_sys->p_active_sps == p_sys->rgi_p_decsps[i_id])
362 p_sys->p_active_sps = NULL;
363 b_active = true;
365 p_sys->rgi_p_decsps[i_id] = NULL;
367 else if(i_nal_type == HEVC_NAL_PPS && p_sys->rgi_p_decpps[i_id])
369 hevc_rbsp_release_pps(p_sys->rgi_p_decpps[i_id]);
370 if(p_sys->p_active_pps == p_sys->rgi_p_decpps[i_id])
372 p_sys->p_active_pps = NULL;
373 b_active = true;
375 p_sys->rgi_p_decpps[i_id] = NULL;
377 else if(i_nal_type == HEVC_NAL_VPS && p_sys->rgi_p_decvps[i_id])
379 hevc_rbsp_release_vps(p_sys->rgi_p_decvps[i_id]);
380 if(p_sys->p_active_vps == p_sys->rgi_p_decvps[i_id])
382 p_sys->p_active_vps = NULL;
383 b_active = true;
385 p_sys->rgi_p_decvps[i_id] = NULL;
388 const uint8_t *p_buffer = p_nalb->p_buffer;
389 size_t i_buffer = p_nalb->i_buffer;
390 if( hxxx_strip_AnnexB_startcode( &p_buffer, &i_buffer ) )
392 /* Create decoded entries */
393 if(i_nal_type == HEVC_NAL_SPS)
395 p_sys->rgi_p_decsps[i_id] = hevc_decode_sps(p_buffer, i_buffer, true);
396 if(!p_sys->rgi_p_decsps[i_id])
398 msg_Err(p_dec, "Failed decoding SPS id %d", i_id);
399 return false;
401 if(b_active)
402 p_sys->p_active_sps = p_sys->rgi_p_decsps[i_id];
404 else if(i_nal_type == HEVC_NAL_PPS)
406 p_sys->rgi_p_decpps[i_id] = hevc_decode_pps(p_buffer, i_buffer, true);
407 if(!p_sys->rgi_p_decpps[i_id])
409 msg_Err(p_dec, "Failed decoding PPS id %d", i_id);
410 return false;
412 if(b_active)
413 p_sys->p_active_pps = p_sys->rgi_p_decpps[i_id];
415 else if(i_nal_type == HEVC_NAL_VPS)
417 p_sys->rgi_p_decvps[i_id] = hevc_decode_vps(p_buffer, i_buffer, true);
418 if(!p_sys->rgi_p_decvps[i_id])
420 msg_Err(p_dec, "Failed decoding VPS id %d", i_id);
421 return false;
423 if(b_active)
424 p_sys->p_active_vps = p_sys->rgi_p_decvps[i_id];
426 return true;
430 return false;
433 static bool XPSReady(decoder_sys_t *p_sys)
435 for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
437 const hevc_picture_parameter_set_t *p_pps = p_sys->rgi_p_decpps[i];
438 if (p_pps)
440 uint8_t id_sps = hevc_get_pps_sps_id(p_pps);
441 const hevc_sequence_parameter_set_t *p_sps = p_sys->rgi_p_decsps[id_sps];
442 if(p_sps)
444 uint8_t id_vps = hevc_get_sps_vps_id(p_sps);
445 if(p_sys->rgi_p_decvps[id_vps])
446 return true;
450 return false;
453 static void ActivateSets(decoder_t *p_dec,
454 const hevc_picture_parameter_set_t *p_pps,
455 const hevc_sequence_parameter_set_t *p_sps,
456 const hevc_video_parameter_set_t *p_vps)
458 decoder_sys_t *p_sys = p_dec->p_sys;
459 p_sys->p_active_pps = p_pps;
460 p_sys->p_active_sps = p_sps;
461 p_sys->p_active_vps = p_vps;
462 if(p_sps)
464 if(!p_dec->fmt_in.video.i_frame_rate || !p_dec->fmt_in.video.i_frame_rate_base)
466 unsigned num, den;
467 if(hevc_get_frame_rate( p_sps, p_dec->p_sys->rgi_p_decvps, &num, &den ))
469 p_dec->fmt_out.video.i_frame_rate = num;
470 p_dec->fmt_out.video.i_frame_rate_base = den;
471 if(p_sys->dts.i_divider_den != den &&
472 p_sys->dts.i_divider_num != 2 * num &&
473 num <= UINT_MAX / 2)
474 date_Change(&p_sys->dts, 2 * num, den);
478 if(p_dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF)
480 (void) hevc_get_colorimetry( p_sps,
481 &p_dec->fmt_out.video.primaries,
482 &p_dec->fmt_out.video.transfer,
483 &p_dec->fmt_out.video.space,
484 &p_dec->fmt_out.video.b_color_range_full);
487 unsigned sizes[4];
488 if( hevc_get_picture_size( p_sps, &sizes[0], &sizes[1],
489 &sizes[2], &sizes[3] ) )
491 p_dec->fmt_out.video.i_width = sizes[0];
492 p_dec->fmt_out.video.i_height = sizes[1];
493 if(p_dec->fmt_in.video.i_visible_width == 0)
495 p_dec->fmt_out.video.i_visible_width = sizes[2];
496 p_dec->fmt_out.video.i_visible_height = sizes[3];
500 if(p_dec->fmt_in.i_profile == -1)
502 uint8_t i_profile, i_level;
503 if( hevc_get_sps_profile_tier_level( p_sps, &i_profile, &i_level ) )
505 p_dec->fmt_out.i_profile = i_profile;
506 p_dec->fmt_out.i_level = i_level;
512 static void GetXPSSet(uint8_t i_pps_id, void *priv,
513 hevc_picture_parameter_set_t **pp_pps,
514 hevc_sequence_parameter_set_t **pp_sps,
515 hevc_video_parameter_set_t **pp_vps)
517 decoder_sys_t *p_sys = priv;
518 *pp_sps = NULL;
519 *pp_vps = NULL;
520 if((*pp_pps = p_sys->rgi_p_decpps[i_pps_id]))
521 if((*pp_sps = p_sys->rgi_p_decsps[hevc_get_pps_sps_id(*pp_pps)]))
522 *pp_vps = p_sys->rgi_p_decvps[hevc_get_sps_vps_id(*pp_sps)];
525 static void ParseStoredSEI( decoder_t *p_dec )
527 decoder_sys_t *p_sys = p_dec->p_sys;
529 for( block_t *p_nal = p_sys->pre.p_chain;
530 p_nal; p_nal = p_nal->p_next )
532 if( p_nal->i_buffer < 5 )
533 continue;
535 if( hevc_getNALType(&p_nal->p_buffer[4]) == HEVC_NAL_PREF_SEI )
537 HxxxParse_AnnexB_SEI( p_nal->p_buffer, p_nal->i_buffer,
538 2 /* nal header */, ParseSEICallback, p_dec );
543 static block_t *ParseVCL(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_frag)
545 decoder_sys_t *p_sys = p_dec->p_sys;
546 block_t *p_outputchain = NULL;
548 const uint8_t *p_buffer = p_frag->p_buffer;
549 size_t i_buffer = p_frag->i_buffer;
551 if(unlikely(!hxxx_strip_AnnexB_startcode(&p_buffer, &i_buffer) || i_buffer < 3))
553 block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag); /* might be corrupted */
554 return NULL;
557 const uint8_t i_layer = hevc_getNALLayer( p_buffer );
558 bool b_first_slice_in_pic = p_buffer[2] & 0x80;
559 if (b_first_slice_in_pic)
561 if(p_sys->frame.p_chain)
563 /* Starting new frame: return previous frame data for output */
564 p_outputchain = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
567 hevc_slice_segment_header_t *p_sli = hevc_decode_slice_header(p_buffer, i_buffer, true,
568 GetXPSSet, p_sys);
569 if(p_sli && i_layer == 0)
571 hevc_sequence_parameter_set_t *p_sps;
572 hevc_picture_parameter_set_t *p_pps;
573 hevc_video_parameter_set_t *p_vps;
574 GetXPSSet(hevc_get_slice_pps_id(p_sli), p_sys, &p_pps, &p_sps, &p_vps);
575 ActivateSets(p_dec, p_pps, p_sps, p_vps);
578 ParseStoredSEI( p_dec );
580 switch(i_nal_type)
582 case HEVC_NAL_BLA_W_LP:
583 case HEVC_NAL_BLA_W_RADL:
584 case HEVC_NAL_BLA_N_LP:
585 case HEVC_NAL_IDR_W_RADL:
586 case HEVC_NAL_IDR_N_LP:
587 case HEVC_NAL_CRA:
588 p_frag->i_flags |= BLOCK_FLAG_TYPE_I;
589 break;
591 default:
593 if(p_sli)
595 enum hevc_slice_type_e type;
596 if(hevc_get_slice_type( p_sli, &type ))
598 if( type == HEVC_SLICE_TYPE_P )
599 p_frag->i_flags |= BLOCK_FLAG_TYPE_P;
600 else
601 p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
604 else p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
606 break;
609 if(p_sli)
610 hevc_rbsp_release_slice_header(p_sli);
613 if(!p_sys->b_init_sequence_complete && i_layer == 0 &&
614 (p_frag->i_flags & BLOCK_FLAG_TYPE_I) && XPSReady(p_sys))
616 p_sys->b_init_sequence_complete = true;
619 if( !p_sys->b_init_sequence_complete )
620 cc_storage_reset( p_sys->p_ccs );
622 block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag);
624 return p_outputchain;
627 static block_t * ParseAUHead(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
629 decoder_sys_t *p_sys = p_dec->p_sys;
630 block_t *p_ret = NULL;
632 if(p_sys->post.p_chain || p_sys->frame.p_chain)
633 p_ret = OutputQueues(p_sys, true);
635 switch(i_nal_type)
637 case HEVC_NAL_AUD:
638 if(!p_ret && p_sys->pre.p_chain)
639 p_ret = OutputQueues(p_sys, true);
640 break;
642 case HEVC_NAL_VPS:
643 case HEVC_NAL_SPS:
644 case HEVC_NAL_PPS:
646 uint8_t i_id;
647 if(hevc_get_xps_id(p_nalb->p_buffer, p_nalb->i_buffer, &i_id))
648 InsertXPS(p_dec, i_nal_type, i_id, p_nalb);
649 break;
652 case HEVC_NAL_PREF_SEI:
653 /* stored an parsed later when we get sps & frame */
654 default:
655 break;
658 block_ChainLastAppend(&p_sys->pre.pp_chain_last, p_nalb);
660 return p_ret;
663 static block_t * ParseAUTail(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
665 decoder_sys_t *p_sys = p_dec->p_sys;
666 block_t *p_ret = NULL;
668 block_ChainLastAppend(&p_sys->post.pp_chain_last, p_nalb);
670 switch(i_nal_type)
672 case HEVC_NAL_EOS:
673 case HEVC_NAL_EOB:
674 p_ret = OutputQueues(p_sys, true);
675 if( p_ret )
676 p_ret->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
677 break;
679 case HEVC_NAL_SUFF_SEI:
680 HxxxParse_AnnexB_SEI( p_nalb->p_buffer, p_nalb->i_buffer,
681 2 /* nal header */, ParseSEICallback, p_dec );
682 break;
685 if(!p_ret && p_sys->frame.p_chain == NULL)
686 p_ret = OutputQueues(p_sys, false);
688 return p_ret;
691 static block_t * ParseNonVCL(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
693 block_t *p_ret = NULL;
695 if ( (i_nal_type >= HEVC_NAL_VPS && i_nal_type <= HEVC_NAL_AUD) ||
696 i_nal_type == HEVC_NAL_PREF_SEI ||
697 (i_nal_type >= HEVC_NAL_RSV_NVCL41 && i_nal_type <= HEVC_NAL_RSV_NVCL44) ||
698 (i_nal_type >= HEVC_NAL_UNSPEC48 && i_nal_type <= HEVC_NAL_UNSPEC55) )
700 p_ret = ParseAUHead(p_dec, i_nal_type, p_nalb);
702 else
704 p_ret = ParseAUTail(p_dec, i_nal_type, p_nalb);
707 return p_ret;
710 static block_t *GatherAndValidateChain(block_t *p_outputchain)
712 block_t *p_output = NULL;
714 if(p_outputchain)
716 if(p_outputchain->i_flags & BLOCK_FLAG_DROP)
717 p_output = p_outputchain; /* Avoid useless gather */
718 else
719 p_output = block_ChainGather(p_outputchain);
722 if(p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
724 block_ChainRelease(p_output); /* Chain! see above */
725 p_output = NULL;
728 return p_output;
731 static void SetOutputBlockProperties(decoder_t *p_dec, block_t *p_output)
733 decoder_sys_t *p_sys = p_dec->p_sys;
734 /* Set frame duration */
735 if(p_sys->p_active_sps)
737 uint8_t i_num_clock_ts = hevc_get_num_clock_ts(p_sys->p_active_sps,
738 p_sys->p_timing);
739 const mtime_t i_start = date_Get(&p_sys->dts);
740 if( i_start != VLC_TS_INVALID )
742 date_Increment(&p_sys->dts, i_num_clock_ts);
743 p_output->i_length = date_Get(&p_sys->dts) - i_start;
745 p_sys->pts = VLC_TS_INVALID;
747 hevc_release_sei_pic_timing(p_sys->p_timing);
748 p_sys->p_timing = NULL;
751 /*****************************************************************************
752 * ParseNALBlock: parses annexB type NALs
753 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
754 *****************************************************************************/
755 static block_t *ParseNALBlock(decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag)
757 decoder_sys_t *p_sys = p_dec->p_sys;
758 *pb_ts_used = false;
760 if(p_sys->b_need_ts)
762 if(p_frag->i_dts > VLC_TS_INVALID)
763 date_Set(&p_sys->dts, p_frag->i_dts);
764 p_sys->pts = p_frag->i_pts;
765 if(date_Get( &p_sys->dts ) != VLC_TS_INVALID)
766 p_sys->b_need_ts = false;
767 *pb_ts_used = true;
770 if(unlikely(p_frag->i_buffer < 5))
772 msg_Warn(p_dec,"NAL too small");
773 block_Release(p_frag);
774 return NULL;
777 if(p_frag->p_buffer[4] & 0x80)
779 msg_Warn(p_dec,"Forbidden zero bit not null, corrupted NAL");
780 block_Release(p_frag);
781 return GatherAndValidateChain(OutputQueues(p_sys, false)); /* will drop */
784 /* Get NALU type */
785 block_t * p_output = NULL;
786 uint8_t i_nal_type = hevc_getNALType(&p_frag->p_buffer[4]);
787 if (i_nal_type < HEVC_NAL_VPS)
789 /* NAL is a VCL NAL */
790 p_output = ParseVCL(p_dec, i_nal_type, p_frag);
791 if (p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
792 msg_Info(p_dec, "Waiting for VPS/SPS/PPS");
794 else
796 p_output = ParseNonVCL(p_dec, i_nal_type, p_frag);
799 p_output = GatherAndValidateChain(p_output);
800 if(p_output)
802 SetOutputBlockProperties( p_dec, p_output );
803 if(p_frag->i_dts > VLC_TS_INVALID)
804 date_Set(&p_sys->dts, p_frag->i_dts);
805 p_sys->pts = p_frag->i_pts;
806 *pb_ts_used = true;
809 return p_output;
812 static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *p_block)
814 decoder_t *p_dec = p_private;
815 decoder_sys_t *p_sys = p_dec->p_sys;
817 /* Remove trailing 0 bytes */
818 while (p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
819 p_block->i_buffer--;
821 p_block = ParseNALBlock( p_dec, pb_ts_used, p_block );
822 if( p_block )
823 cc_storage_commit( p_sys->p_ccs, p_block );
825 return p_block;
828 static int PacketizeValidate( void *p_private, block_t *p_au )
830 VLC_UNUSED(p_private);
831 VLC_UNUSED(p_au);
832 return VLC_SUCCESS;
835 static bool ParseSEICallback( const hxxx_sei_data_t *p_sei_data, void *cbdata )
837 decoder_t *p_dec = (decoder_t *) cbdata;
838 decoder_sys_t *p_sys = p_dec->p_sys;
840 switch( p_sei_data->i_type )
842 case HXXX_SEI_PIC_TIMING:
844 if( p_sys->p_active_sps )
846 hevc_release_sei_pic_timing( p_sys->p_timing );
847 p_sys->p_timing = hevc_decode_sei_pic_timing( p_sei_data->p_bs,
848 p_sys->p_active_sps );
850 } break;
851 case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35:
853 if( p_sei_data->itu_t35.type == HXXX_ITU_T35_TYPE_CC )
855 cc_storage_append( p_sys->p_ccs, true, p_sei_data->itu_t35.u.cc.p_data,
856 p_sei_data->itu_t35.u.cc.i_data );
858 } break;
859 case HXXX_SEI_FRAME_PACKING_ARRANGEMENT:
861 if( p_dec->fmt_in.video.multiview_mode == MULTIVIEW_2D )
863 video_multiview_mode_t mode;
864 switch( p_sei_data->frame_packing.type )
866 case FRAME_PACKING_INTERLEAVED_CHECKERBOARD:
867 mode = MULTIVIEW_STEREO_CHECKERBOARD; break;
868 case FRAME_PACKING_INTERLEAVED_COLUMN:
869 mode = MULTIVIEW_STEREO_COL; break;
870 case FRAME_PACKING_INTERLEAVED_ROW:
871 mode = MULTIVIEW_STEREO_ROW; break;
872 case FRAME_PACKING_SIDE_BY_SIDE:
873 mode = MULTIVIEW_STEREO_SBS; break;
874 case FRAME_PACKING_TOP_BOTTOM:
875 mode = MULTIVIEW_STEREO_TB; break;
876 case FRAME_PACKING_TEMPORAL:
877 mode = MULTIVIEW_STEREO_FRAME; break;
878 case FRAME_PACKING_TILED:
879 default:
880 mode = MULTIVIEW_2D; break;
882 p_dec->fmt_out.video.multiview_mode = mode;
884 } break;
885 case HXXX_SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
887 video_format_t *p_fmt = &p_dec->fmt_out.video;
888 for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.primaries); ++i)
889 p_fmt->mastering.primaries[i] = p_sei_data->colour_volume.primaries[i];
890 for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.white_point); ++i)
891 p_fmt->mastering.white_point[i] = p_sei_data->colour_volume.white_point[i];
892 p_fmt->mastering.max_luminance = p_sei_data->colour_volume.max_luminance;
893 p_fmt->mastering.min_luminance = p_sei_data->colour_volume.min_luminance;
894 } break;
895 case HXXX_SEI_CONTENT_LIGHT_LEVEL:
897 video_format_t *p_fmt = &p_dec->fmt_out.video;
898 p_fmt->lighting.MaxCLL = p_sei_data->content_light_lvl.MaxCLL;
899 p_fmt->lighting.MaxFALL = p_sei_data->content_light_lvl.MaxFALL;
900 } break;
903 return true;