es: pass no cc reorder in es fmt
[vlc.git] / modules / packetizer / hevc.c
blob9bdca109ba64a4a2712a4d32801960c0d70e6c3a
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 return cc_storage_get_current( p_dec->p_sys->p_ccs, pb_present, pi_reorder_depth );
312 /****************************************************************************
313 * Packetizer Helpers
314 ****************************************************************************/
315 static void PacketizeReset(void *p_private, bool b_broken)
317 VLC_UNUSED(b_broken);
319 decoder_t *p_dec = p_private;
320 decoder_sys_t *p_sys = p_dec->p_sys;
322 block_t *p_out = OutputQueues(p_sys, false);
323 if(p_out)
324 block_ChainRelease(p_out);
326 p_sys->b_init_sequence_complete = false;
327 p_sys->b_need_ts = true;
328 date_Set(&p_sys->dts, VLC_TS_INVALID);
331 static bool InsertXPS(decoder_t *p_dec, uint8_t i_nal_type, uint8_t i_id,
332 const block_t *p_nalb)
334 decoder_sys_t *p_sys = p_dec->p_sys;
335 bool b_active = false;
337 switch(i_nal_type)
339 case HEVC_NAL_VPS:
340 if(i_id > HEVC_VPS_ID_MAX)
341 return false;
342 break;
343 case HEVC_NAL_SPS:
344 if(i_id > HEVC_SPS_ID_MAX)
345 return false;
346 break;
347 case HEVC_NAL_PPS:
348 if(i_id > HEVC_PPS_ID_MAX)
349 return false;
350 break;
351 default:
352 return false;
355 /* Free associated decoded version */
356 if(i_nal_type == HEVC_NAL_SPS && p_sys->rgi_p_decsps[i_id])
358 hevc_rbsp_release_sps(p_sys->rgi_p_decsps[i_id]);
359 if(p_sys->p_active_sps == p_sys->rgi_p_decsps[i_id])
361 p_sys->p_active_sps = NULL;
362 b_active = true;
364 p_sys->rgi_p_decsps[i_id] = NULL;
366 else if(i_nal_type == HEVC_NAL_PPS && p_sys->rgi_p_decpps[i_id])
368 hevc_rbsp_release_pps(p_sys->rgi_p_decpps[i_id]);
369 if(p_sys->p_active_pps == p_sys->rgi_p_decpps[i_id])
371 p_sys->p_active_pps = NULL;
372 b_active = true;
374 p_sys->rgi_p_decpps[i_id] = NULL;
376 else if(i_nal_type == HEVC_NAL_VPS && p_sys->rgi_p_decvps[i_id])
378 hevc_rbsp_release_vps(p_sys->rgi_p_decvps[i_id]);
379 if(p_sys->p_active_vps == p_sys->rgi_p_decvps[i_id])
381 p_sys->p_active_vps = NULL;
382 b_active = true;
384 p_sys->rgi_p_decvps[i_id] = NULL;
387 const uint8_t *p_buffer = p_nalb->p_buffer;
388 size_t i_buffer = p_nalb->i_buffer;
389 if( hxxx_strip_AnnexB_startcode( &p_buffer, &i_buffer ) )
391 /* Create decoded entries */
392 if(i_nal_type == HEVC_NAL_SPS)
394 p_sys->rgi_p_decsps[i_id] = hevc_decode_sps(p_buffer, i_buffer, true);
395 if(!p_sys->rgi_p_decsps[i_id])
397 msg_Err(p_dec, "Failed decoding SPS id %d", i_id);
398 return false;
400 if(b_active)
401 p_sys->p_active_sps = p_sys->rgi_p_decsps[i_id];
403 else if(i_nal_type == HEVC_NAL_PPS)
405 p_sys->rgi_p_decpps[i_id] = hevc_decode_pps(p_buffer, i_buffer, true);
406 if(!p_sys->rgi_p_decpps[i_id])
408 msg_Err(p_dec, "Failed decoding PPS id %d", i_id);
409 return false;
411 if(b_active)
412 p_sys->p_active_pps = p_sys->rgi_p_decpps[i_id];
414 else if(i_nal_type == HEVC_NAL_VPS)
416 p_sys->rgi_p_decvps[i_id] = hevc_decode_vps(p_buffer, i_buffer, true);
417 if(!p_sys->rgi_p_decvps[i_id])
419 msg_Err(p_dec, "Failed decoding VPS id %d", i_id);
420 return false;
422 if(b_active)
423 p_sys->p_active_vps = p_sys->rgi_p_decvps[i_id];
425 return true;
429 return false;
432 static bool XPSReady(decoder_sys_t *p_sys)
434 for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
436 const hevc_picture_parameter_set_t *p_pps = p_sys->rgi_p_decpps[i];
437 if (p_pps)
439 uint8_t id_sps = hevc_get_pps_sps_id(p_pps);
440 const hevc_sequence_parameter_set_t *p_sps = p_sys->rgi_p_decsps[id_sps];
441 if(p_sps)
443 uint8_t id_vps = hevc_get_sps_vps_id(p_sps);
444 if(p_sys->rgi_p_decvps[id_vps])
445 return true;
449 return false;
452 static void ActivateSets(decoder_t *p_dec,
453 const hevc_picture_parameter_set_t *p_pps,
454 const hevc_sequence_parameter_set_t *p_sps,
455 const hevc_video_parameter_set_t *p_vps)
457 decoder_sys_t *p_sys = p_dec->p_sys;
458 p_sys->p_active_pps = p_pps;
459 p_sys->p_active_sps = p_sps;
460 p_sys->p_active_vps = p_vps;
461 if(p_sps)
463 if(!p_dec->fmt_in.video.i_frame_rate || !p_dec->fmt_in.video.i_frame_rate_base)
465 unsigned num, den;
466 if(hevc_get_frame_rate( p_sps, p_dec->p_sys->rgi_p_decvps, &num, &den ))
468 p_dec->fmt_out.video.i_frame_rate = num;
469 p_dec->fmt_out.video.i_frame_rate_base = den;
470 if(p_sys->dts.i_divider_den != den &&
471 p_sys->dts.i_divider_num != 2 * num &&
472 num <= UINT_MAX / 2)
473 date_Change(&p_sys->dts, 2 * num, den);
477 if(p_dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF)
479 (void) hevc_get_colorimetry( p_sps,
480 &p_dec->fmt_out.video.primaries,
481 &p_dec->fmt_out.video.transfer,
482 &p_dec->fmt_out.video.space,
483 &p_dec->fmt_out.video.b_color_range_full);
486 unsigned sizes[4];
487 if( hevc_get_picture_size( p_sps, &sizes[0], &sizes[1],
488 &sizes[2], &sizes[3] ) )
490 p_dec->fmt_out.video.i_width = sizes[0];
491 p_dec->fmt_out.video.i_height = sizes[1];
492 if(p_dec->fmt_in.video.i_visible_width == 0)
494 p_dec->fmt_out.video.i_visible_width = sizes[2];
495 p_dec->fmt_out.video.i_visible_height = sizes[3];
499 if(p_dec->fmt_in.i_profile == -1)
501 uint8_t i_profile, i_level;
502 if( hevc_get_sps_profile_tier_level( p_sps, &i_profile, &i_level ) )
504 p_dec->fmt_out.i_profile = i_profile;
505 p_dec->fmt_out.i_level = i_level;
511 static void GetXPSSet(uint8_t i_pps_id, void *priv,
512 hevc_picture_parameter_set_t **pp_pps,
513 hevc_sequence_parameter_set_t **pp_sps,
514 hevc_video_parameter_set_t **pp_vps)
516 decoder_sys_t *p_sys = priv;
517 *pp_sps = NULL;
518 *pp_vps = NULL;
519 if((*pp_pps = p_sys->rgi_p_decpps[i_pps_id]))
520 if((*pp_sps = p_sys->rgi_p_decsps[hevc_get_pps_sps_id(*pp_pps)]))
521 *pp_vps = p_sys->rgi_p_decvps[hevc_get_sps_vps_id(*pp_sps)];
524 static void ParseStoredSEI( decoder_t *p_dec )
526 decoder_sys_t *p_sys = p_dec->p_sys;
528 for( block_t *p_nal = p_sys->pre.p_chain;
529 p_nal; p_nal = p_nal->p_next )
531 if( p_nal->i_buffer < 5 )
532 continue;
534 if( hevc_getNALType(&p_nal->p_buffer[4]) == HEVC_NAL_PREF_SEI )
536 HxxxParse_AnnexB_SEI( p_nal->p_buffer, p_nal->i_buffer,
537 2 /* nal header */, ParseSEICallback, p_dec );
542 static block_t *ParseVCL(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_frag)
544 decoder_sys_t *p_sys = p_dec->p_sys;
545 block_t *p_outputchain = NULL;
547 const uint8_t *p_buffer = p_frag->p_buffer;
548 size_t i_buffer = p_frag->i_buffer;
550 if(unlikely(!hxxx_strip_AnnexB_startcode(&p_buffer, &i_buffer) || i_buffer < 3))
552 block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag); /* might be corrupted */
553 return NULL;
556 const uint8_t i_layer = hevc_getNALLayer( p_buffer );
557 bool b_first_slice_in_pic = p_buffer[2] & 0x80;
558 if (b_first_slice_in_pic)
560 if(p_sys->frame.p_chain)
562 /* Starting new frame: return previous frame data for output */
563 p_outputchain = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
566 hevc_slice_segment_header_t *p_sli = hevc_decode_slice_header(p_buffer, i_buffer, true,
567 GetXPSSet, p_sys);
568 if(p_sli && i_layer == 0)
570 hevc_sequence_parameter_set_t *p_sps;
571 hevc_picture_parameter_set_t *p_pps;
572 hevc_video_parameter_set_t *p_vps;
573 GetXPSSet(hevc_get_slice_pps_id(p_sli), p_sys, &p_pps, &p_sps, &p_vps);
574 ActivateSets(p_dec, p_pps, p_sps, p_vps);
577 ParseStoredSEI( p_dec );
579 switch(i_nal_type)
581 case HEVC_NAL_BLA_W_LP:
582 case HEVC_NAL_BLA_W_RADL:
583 case HEVC_NAL_BLA_N_LP:
584 case HEVC_NAL_IDR_W_RADL:
585 case HEVC_NAL_IDR_N_LP:
586 case HEVC_NAL_CRA:
587 p_frag->i_flags |= BLOCK_FLAG_TYPE_I;
588 break;
590 default:
592 if(p_sli)
594 enum hevc_slice_type_e type;
595 if(hevc_get_slice_type( p_sli, &type ))
597 if( type == HEVC_SLICE_TYPE_P )
598 p_frag->i_flags |= BLOCK_FLAG_TYPE_P;
599 else
600 p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
603 else p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
605 break;
608 if(p_sli)
609 hevc_rbsp_release_slice_header(p_sli);
612 if(!p_sys->b_init_sequence_complete && i_layer == 0 &&
613 (p_frag->i_flags & BLOCK_FLAG_TYPE_I) && XPSReady(p_sys))
615 p_sys->b_init_sequence_complete = true;
618 if( !p_sys->b_init_sequence_complete )
619 cc_storage_reset( p_sys->p_ccs );
621 block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag);
623 return p_outputchain;
626 static block_t * ParseAUHead(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
628 decoder_sys_t *p_sys = p_dec->p_sys;
629 block_t *p_ret = NULL;
631 if(p_sys->post.p_chain || p_sys->frame.p_chain)
632 p_ret = OutputQueues(p_sys, true);
634 switch(i_nal_type)
636 case HEVC_NAL_AUD:
637 if(!p_ret && p_sys->pre.p_chain)
638 p_ret = OutputQueues(p_sys, true);
639 break;
641 case HEVC_NAL_VPS:
642 case HEVC_NAL_SPS:
643 case HEVC_NAL_PPS:
645 uint8_t i_id;
646 if(hevc_get_xps_id(p_nalb->p_buffer, p_nalb->i_buffer, &i_id))
647 InsertXPS(p_dec, i_nal_type, i_id, p_nalb);
648 break;
651 case HEVC_NAL_PREF_SEI:
652 /* stored an parsed later when we get sps & frame */
653 default:
654 break;
657 block_ChainLastAppend(&p_sys->pre.pp_chain_last, p_nalb);
659 return p_ret;
662 static block_t * ParseAUTail(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
664 decoder_sys_t *p_sys = p_dec->p_sys;
665 block_t *p_ret = NULL;
667 block_ChainLastAppend(&p_sys->post.pp_chain_last, p_nalb);
669 switch(i_nal_type)
671 case HEVC_NAL_EOS:
672 case HEVC_NAL_EOB:
673 p_ret = OutputQueues(p_sys, true);
674 if( p_ret )
675 p_ret->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
676 break;
678 case HEVC_NAL_SUFF_SEI:
679 HxxxParse_AnnexB_SEI( p_nalb->p_buffer, p_nalb->i_buffer,
680 2 /* nal header */, ParseSEICallback, p_dec );
681 break;
684 if(!p_ret && p_sys->frame.p_chain == NULL)
685 p_ret = OutputQueues(p_sys, false);
687 return p_ret;
690 static block_t * ParseNonVCL(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
692 block_t *p_ret = NULL;
694 if ( (i_nal_type >= HEVC_NAL_VPS && i_nal_type <= HEVC_NAL_AUD) ||
695 i_nal_type == HEVC_NAL_PREF_SEI ||
696 (i_nal_type >= HEVC_NAL_RSV_NVCL41 && i_nal_type <= HEVC_NAL_RSV_NVCL44) ||
697 (i_nal_type >= HEVC_NAL_UNSPEC48 && i_nal_type <= HEVC_NAL_UNSPEC55) )
699 p_ret = ParseAUHead(p_dec, i_nal_type, p_nalb);
701 else
703 p_ret = ParseAUTail(p_dec, i_nal_type, p_nalb);
706 return p_ret;
709 static block_t *GatherAndValidateChain(block_t *p_outputchain)
711 block_t *p_output = NULL;
713 if(p_outputchain)
715 if(p_outputchain->i_flags & BLOCK_FLAG_DROP)
716 p_output = p_outputchain; /* Avoid useless gather */
717 else
718 p_output = block_ChainGather(p_outputchain);
721 if(p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
723 block_ChainRelease(p_output); /* Chain! see above */
724 p_output = NULL;
727 return p_output;
730 static void SetOutputBlockProperties(decoder_t *p_dec, block_t *p_output)
732 decoder_sys_t *p_sys = p_dec->p_sys;
733 /* Set frame duration */
734 if(p_sys->p_active_sps)
736 uint8_t i_num_clock_ts = hevc_get_num_clock_ts(p_sys->p_active_sps,
737 p_sys->p_timing);
738 const mtime_t i_start = date_Get(&p_sys->dts);
739 if( i_start != VLC_TS_INVALID )
741 date_Increment(&p_sys->dts, i_num_clock_ts);
742 p_output->i_length = date_Get(&p_sys->dts) - i_start;
744 p_sys->pts = VLC_TS_INVALID;
746 hevc_release_sei_pic_timing(p_sys->p_timing);
747 p_sys->p_timing = NULL;
750 /*****************************************************************************
751 * ParseNALBlock: parses annexB type NALs
752 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
753 *****************************************************************************/
754 static block_t *ParseNALBlock(decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag)
756 decoder_sys_t *p_sys = p_dec->p_sys;
757 *pb_ts_used = false;
759 if(p_sys->b_need_ts)
761 if(p_frag->i_dts > VLC_TS_INVALID)
762 date_Set(&p_sys->dts, p_frag->i_dts);
763 p_sys->pts = p_frag->i_pts;
764 if(date_Get( &p_sys->dts ) != VLC_TS_INVALID)
765 p_sys->b_need_ts = false;
766 *pb_ts_used = true;
769 if(unlikely(p_frag->i_buffer < 5))
771 msg_Warn(p_dec,"NAL too small");
772 block_Release(p_frag);
773 return NULL;
776 if(p_frag->p_buffer[4] & 0x80)
778 msg_Warn(p_dec,"Forbidden zero bit not null, corrupted NAL");
779 block_Release(p_frag);
780 return GatherAndValidateChain(OutputQueues(p_sys, false)); /* will drop */
783 /* Get NALU type */
784 block_t * p_output = NULL;
785 uint8_t i_nal_type = hevc_getNALType(&p_frag->p_buffer[4]);
786 if (i_nal_type < HEVC_NAL_VPS)
788 /* NAL is a VCL NAL */
789 p_output = ParseVCL(p_dec, i_nal_type, p_frag);
790 if (p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
791 msg_Info(p_dec, "Waiting for VPS/SPS/PPS");
793 else
795 p_output = ParseNonVCL(p_dec, i_nal_type, p_frag);
798 p_output = GatherAndValidateChain(p_output);
799 if(p_output)
801 SetOutputBlockProperties( p_dec, p_output );
802 if(p_frag->i_dts > VLC_TS_INVALID)
803 date_Set(&p_sys->dts, p_frag->i_dts);
804 p_sys->pts = p_frag->i_pts;
805 *pb_ts_used = true;
808 return p_output;
811 static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *p_block)
813 decoder_t *p_dec = p_private;
814 decoder_sys_t *p_sys = p_dec->p_sys;
816 /* Remove trailing 0 bytes */
817 while (p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
818 p_block->i_buffer--;
820 p_block = ParseNALBlock( p_dec, pb_ts_used, p_block );
821 if( p_block )
822 cc_storage_commit( p_sys->p_ccs, p_block );
824 return p_block;
827 static int PacketizeValidate( void *p_private, block_t *p_au )
829 VLC_UNUSED(p_private);
830 VLC_UNUSED(p_au);
831 return VLC_SUCCESS;
834 static bool ParseSEICallback( const hxxx_sei_data_t *p_sei_data, void *cbdata )
836 decoder_t *p_dec = (decoder_t *) cbdata;
837 decoder_sys_t *p_sys = p_dec->p_sys;
839 switch( p_sei_data->i_type )
841 case HXXX_SEI_PIC_TIMING:
843 if( p_sys->p_active_sps )
845 hevc_release_sei_pic_timing( p_sys->p_timing );
846 p_sys->p_timing = hevc_decode_sei_pic_timing( p_sei_data->p_bs,
847 p_sys->p_active_sps );
849 } break;
850 case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35:
852 if( p_sei_data->itu_t35.type == HXXX_ITU_T35_TYPE_CC )
854 cc_storage_append( p_sys->p_ccs, true, p_sei_data->itu_t35.u.cc.p_data,
855 p_sei_data->itu_t35.u.cc.i_data );
857 } break;
858 case HXXX_SEI_FRAME_PACKING_ARRANGEMENT:
860 if( p_dec->fmt_in.video.multiview_mode == MULTIVIEW_2D )
862 video_multiview_mode_t mode;
863 switch( p_sei_data->frame_packing.type )
865 case FRAME_PACKING_INTERLEAVED_CHECKERBOARD:
866 mode = MULTIVIEW_STEREO_CHECKERBOARD; break;
867 case FRAME_PACKING_INTERLEAVED_COLUMN:
868 mode = MULTIVIEW_STEREO_COL; break;
869 case FRAME_PACKING_INTERLEAVED_ROW:
870 mode = MULTIVIEW_STEREO_ROW; break;
871 case FRAME_PACKING_SIDE_BY_SIDE:
872 mode = MULTIVIEW_STEREO_SBS; break;
873 case FRAME_PACKING_TOP_BOTTOM:
874 mode = MULTIVIEW_STEREO_TB; break;
875 case FRAME_PACKING_TEMPORAL:
876 mode = MULTIVIEW_STEREO_FRAME; break;
877 case FRAME_PACKING_TILED:
878 default:
879 mode = MULTIVIEW_2D; break;
881 p_dec->fmt_out.video.multiview_mode = mode;
883 } break;
884 case HXXX_SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
886 video_format_t *p_fmt = &p_dec->fmt_out.video;
887 for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.primaries); ++i)
888 p_fmt->mastering.primaries[i] = p_sei_data->colour_volume.primaries[i];
889 for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.white_point); ++i)
890 p_fmt->mastering.white_point[i] = p_sei_data->colour_volume.white_point[i];
891 p_fmt->mastering.max_luminance = p_sei_data->colour_volume.max_luminance;
892 p_fmt->mastering.min_luminance = p_sei_data->colour_volume.min_luminance;
893 } break;
894 case HXXX_SEI_CONTENT_LIGHT_LEVEL:
896 video_format_t *p_fmt = &p_dec->fmt_out.video;
897 p_fmt->lighting.MaxCLL = p_sei_data->content_light_lvl.MaxCLL;
898 p_fmt->lighting.MaxFALL = p_sei_data->content_light_lvl.MaxFALL;
899 } break;
902 return true;