access: srt: add support stream encryption
[vlc.git] / modules / packetizer / hevc.c
blob83c782a856dc975e41ee688e7d39e6cc816d61ed
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 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, decoder_cc_desc_t *p_desc )
309 return cc_storage_get_current( p_dec->p_sys->p_ccs, p_desc );
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_vps, &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 const uint8_t *p_xps = p_nalb->p_buffer;
647 size_t i_xps = p_nalb->i_buffer;
648 if(hxxx_strip_AnnexB_startcode(&p_xps, &i_xps) &&
649 hevc_get_xps_id(p_nalb->p_buffer, p_nalb->i_buffer, &i_id))
650 InsertXPS(p_dec, i_nal_type, i_id, p_nalb);
651 break;
654 case HEVC_NAL_PREF_SEI:
655 /* stored an parsed later when we get sps & frame */
656 default:
657 break;
660 block_ChainLastAppend(&p_sys->pre.pp_chain_last, p_nalb);
662 return p_ret;
665 static block_t * ParseAUTail(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
667 decoder_sys_t *p_sys = p_dec->p_sys;
668 block_t *p_ret = NULL;
670 block_ChainLastAppend(&p_sys->post.pp_chain_last, p_nalb);
672 switch(i_nal_type)
674 case HEVC_NAL_EOS:
675 case HEVC_NAL_EOB:
676 p_ret = OutputQueues(p_sys, true);
677 if( p_ret )
678 p_ret->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
679 break;
681 case HEVC_NAL_SUFF_SEI:
682 HxxxParse_AnnexB_SEI( p_nalb->p_buffer, p_nalb->i_buffer,
683 2 /* nal header */, ParseSEICallback, p_dec );
684 break;
687 if(!p_ret && p_sys->frame.p_chain == NULL)
688 p_ret = OutputQueues(p_sys, false);
690 return p_ret;
693 static block_t * ParseNonVCL(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
695 block_t *p_ret = NULL;
697 if ( (i_nal_type >= HEVC_NAL_VPS && i_nal_type <= HEVC_NAL_AUD) ||
698 i_nal_type == HEVC_NAL_PREF_SEI ||
699 (i_nal_type >= HEVC_NAL_RSV_NVCL41 && i_nal_type <= HEVC_NAL_RSV_NVCL44) ||
700 (i_nal_type >= HEVC_NAL_UNSPEC48 && i_nal_type <= HEVC_NAL_UNSPEC55) )
702 p_ret = ParseAUHead(p_dec, i_nal_type, p_nalb);
704 else
706 p_ret = ParseAUTail(p_dec, i_nal_type, p_nalb);
709 return p_ret;
712 static block_t *GatherAndValidateChain(block_t *p_outputchain)
714 block_t *p_output = NULL;
716 if(p_outputchain)
718 if(p_outputchain->i_flags & BLOCK_FLAG_DROP)
719 p_output = p_outputchain; /* Avoid useless gather */
720 else
721 p_output = block_ChainGather(p_outputchain);
724 if(p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
726 block_ChainRelease(p_output); /* Chain! see above */
727 p_output = NULL;
730 return p_output;
733 static void SetOutputBlockProperties(decoder_t *p_dec, block_t *p_output)
735 decoder_sys_t *p_sys = p_dec->p_sys;
736 /* Set frame duration */
737 if(p_sys->p_active_sps)
739 uint8_t i_num_clock_ts = hevc_get_num_clock_ts(p_sys->p_active_sps,
740 p_sys->p_timing);
741 const mtime_t i_start = date_Get(&p_sys->dts);
742 if( i_start != VLC_TS_INVALID )
744 date_Increment(&p_sys->dts, i_num_clock_ts);
745 p_output->i_length = date_Get(&p_sys->dts) - i_start;
747 p_sys->pts = VLC_TS_INVALID;
749 hevc_release_sei_pic_timing(p_sys->p_timing);
750 p_sys->p_timing = NULL;
753 /*****************************************************************************
754 * ParseNALBlock: parses annexB type NALs
755 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
756 *****************************************************************************/
757 static block_t *ParseNALBlock(decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag)
759 decoder_sys_t *p_sys = p_dec->p_sys;
760 *pb_ts_used = false;
762 if(p_sys->b_need_ts)
764 if(p_frag->i_dts > VLC_TS_INVALID)
765 date_Set(&p_sys->dts, p_frag->i_dts);
766 p_sys->pts = p_frag->i_pts;
767 if(date_Get( &p_sys->dts ) != VLC_TS_INVALID)
768 p_sys->b_need_ts = false;
769 *pb_ts_used = true;
772 if(unlikely(p_frag->i_buffer < 5))
774 msg_Warn(p_dec,"NAL too small");
775 block_Release(p_frag);
776 return NULL;
779 if(p_frag->p_buffer[4] & 0x80)
781 msg_Warn(p_dec,"Forbidden zero bit not null, corrupted NAL");
782 block_Release(p_frag);
783 return GatherAndValidateChain(OutputQueues(p_sys, false)); /* will drop */
786 /* Get NALU type */
787 const mtime_t dts = p_frag->i_dts, pts = p_frag->i_pts;
788 block_t * p_output = NULL;
789 uint8_t i_nal_type = hevc_getNALType(&p_frag->p_buffer[4]);
791 if (i_nal_type < HEVC_NAL_VPS)
793 /* NAL is a VCL NAL */
794 p_output = ParseVCL(p_dec, i_nal_type, p_frag);
795 if (p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
796 msg_Info(p_dec, "Waiting for VPS/SPS/PPS");
798 else
800 p_output = ParseNonVCL(p_dec, i_nal_type, p_frag);
803 p_output = GatherAndValidateChain(p_output);
804 if(p_output)
806 SetOutputBlockProperties( p_dec, p_output );
807 if (dts > VLC_TS_INVALID)
808 date_Set(&p_sys->dts, dts);
809 p_sys->pts = pts;
810 *pb_ts_used = true;
813 return p_output;
816 static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *p_block)
818 decoder_t *p_dec = p_private;
819 decoder_sys_t *p_sys = p_dec->p_sys;
821 /* Remove trailing 0 bytes */
822 while (p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
823 p_block->i_buffer--;
825 p_block = ParseNALBlock( p_dec, pb_ts_used, p_block );
826 if( p_block )
827 cc_storage_commit( p_sys->p_ccs, p_block );
829 return p_block;
832 static int PacketizeValidate( void *p_private, block_t *p_au )
834 VLC_UNUSED(p_private);
835 VLC_UNUSED(p_au);
836 return VLC_SUCCESS;
839 static bool ParseSEICallback( const hxxx_sei_data_t *p_sei_data, void *cbdata )
841 decoder_t *p_dec = (decoder_t *) cbdata;
842 decoder_sys_t *p_sys = p_dec->p_sys;
844 switch( p_sei_data->i_type )
846 case HXXX_SEI_PIC_TIMING:
848 if( p_sys->p_active_sps )
850 hevc_release_sei_pic_timing( p_sys->p_timing );
851 p_sys->p_timing = hevc_decode_sei_pic_timing( p_sei_data->p_bs,
852 p_sys->p_active_sps );
854 } break;
855 case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35:
857 if( p_sei_data->itu_t35.type == HXXX_ITU_T35_TYPE_CC )
859 cc_storage_append( p_sys->p_ccs, true, p_sei_data->itu_t35.u.cc.p_data,
860 p_sei_data->itu_t35.u.cc.i_data );
862 } break;
863 case HXXX_SEI_FRAME_PACKING_ARRANGEMENT:
865 if( p_dec->fmt_in.video.multiview_mode == MULTIVIEW_2D )
867 video_multiview_mode_t mode;
868 switch( p_sei_data->frame_packing.type )
870 case FRAME_PACKING_INTERLEAVED_CHECKERBOARD:
871 mode = MULTIVIEW_STEREO_CHECKERBOARD; break;
872 case FRAME_PACKING_INTERLEAVED_COLUMN:
873 mode = MULTIVIEW_STEREO_COL; break;
874 case FRAME_PACKING_INTERLEAVED_ROW:
875 mode = MULTIVIEW_STEREO_ROW; break;
876 case FRAME_PACKING_SIDE_BY_SIDE:
877 mode = MULTIVIEW_STEREO_SBS; break;
878 case FRAME_PACKING_TOP_BOTTOM:
879 mode = MULTIVIEW_STEREO_TB; break;
880 case FRAME_PACKING_TEMPORAL:
881 mode = MULTIVIEW_STEREO_FRAME; break;
882 case FRAME_PACKING_TILED:
883 default:
884 mode = MULTIVIEW_2D; break;
886 p_dec->fmt_out.video.multiview_mode = mode;
888 } break;
889 case HXXX_SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
891 video_format_t *p_fmt = &p_dec->fmt_out.video;
892 for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.primaries); ++i)
893 p_fmt->mastering.primaries[i] = p_sei_data->colour_volume.primaries[i];
894 for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.white_point); ++i)
895 p_fmt->mastering.white_point[i] = p_sei_data->colour_volume.white_point[i];
896 p_fmt->mastering.max_luminance = p_sei_data->colour_volume.max_luminance;
897 p_fmt->mastering.min_luminance = p_sei_data->colour_volume.min_luminance;
898 } break;
899 case HXXX_SEI_CONTENT_LIGHT_LEVEL:
901 video_format_t *p_fmt = &p_dec->fmt_out.video;
902 p_fmt->lighting.MaxCLL = p_sei_data->content_light_lvl.MaxCLL;
903 p_fmt->lighting.MaxFALL = p_sei_data->content_light_lvl.MaxFALL;
904 } break;
907 return true;