hevc.c 31.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*****************************************************************************
 * hevc.c: h.265/hevc video packetizer
 *****************************************************************************
 * Copyright (C) 2014 VLC authors and VideoLAN
 * $Id$
 *
 * Authors: Denis Charmet <typx@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_codec.h>
#include <vlc_block.h>
36
#include <vlc_bits.h>
37 38 39

#include <vlc_block_helper.h>
#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"
45

46 47
#include <limits.h>

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open (vlc_object_t *);
static void Close(vlc_object_t *);

vlc_module_begin ()
    set_category(CAT_SOUT)
    set_subcategory(SUBCAT_SOUT_PACKETIZER)
    set_description(N_("HEVC/H.265 video packetizer"))
    set_capability("packetizer", 50)
    set_callbacks(Open, Close)
vlc_module_end ()


/****************************************************************************
 * Local prototypes
 ****************************************************************************/
66 67
static block_t *PacketizeAnnexB(decoder_t *, block_t **);
static block_t *PacketizeHVC1(decoder_t *, block_t **);
68
static void PacketizeFlush( decoder_t * );
69 70
static void PacketizeReset(void *p_private, bool b_broken);
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 * );
75 76 77 78 79 80

struct decoder_sys_t
{
    /* */
    packetizer_t packetizer;

81 82 83 84 85
    struct
    {
        block_t *p_chain;
        block_t **pp_chain_last;
    } frame, pre, post;
86

87
    uint8_t  i_nal_length_size;
88 89 90 91 92 93 94 95 96

    struct
    {
        block_t *p_nal;
        void *p_decoded;
    } rg_vps[HEVC_VPS_ID_MAX + 1],
      rg_sps[HEVC_SPS_ID_MAX + 1],
      rg_pps[HEVC_PPS_ID_MAX + 1];

97 98 99
    const hevc_video_parameter_set_t    *p_active_vps;
    const hevc_sequence_parameter_set_t *p_active_sps;
    const hevc_picture_parameter_set_t  *p_active_pps;
100
    hevc_sei_pic_timing_t *p_timing;
101
    bool b_init_sequence_complete;
102

103 104 105 106
    date_t dts;
    mtime_t pts;
    bool b_need_ts;

107 108
    /* */
    cc_storage_t *p_ccs;
109 110
};

111 112
#define BLOCK_FLAG_DROP (1 << BLOCK_FLAG_PRIVATE_SHIFT)

113
static const uint8_t p_hevc_startcode[3] = {0x00, 0x00, 0x01};
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
/****************************************************************************
 * Helpers
 ****************************************************************************/
static inline void InitQueue( block_t **pp_head, block_t ***ppp_tail )
{
    *pp_head = NULL;
    *ppp_tail = pp_head;
}
#define INITQ(name) InitQueue(&p_sys->name.p_chain, &p_sys->name.pp_chain_last)

static block_t * OutputQueues(decoder_sys_t *p_sys, bool b_valid)
{
    block_t *p_output = NULL;
    block_t **pp_output_last = &p_output;
    uint32_t i_flags = 0; /* Because block_ChainGather does not merge flags or times */

    if(p_sys->pre.p_chain)
    {
        i_flags |= p_sys->pre.p_chain->i_flags;
        block_ChainLastAppend(&pp_output_last, p_sys->pre.p_chain);
        INITQ(pre);
    }

    if(p_sys->frame.p_chain)
    {
        i_flags |= p_sys->frame.p_chain->i_flags;
140
        block_ChainLastAppend(&pp_output_last, p_sys->frame.p_chain);
141 142
        p_output->i_dts = date_Get(&p_sys->dts);
        p_output->i_pts = p_sys->pts;
143 144 145 146 147 148 149 150 151 152 153 154 155 156
        INITQ(frame);
    }

    if(p_sys->post.p_chain)
    {
        i_flags |= p_sys->post.p_chain->i_flags;
        block_ChainLastAppend(&pp_output_last, p_sys->post.p_chain);
        INITQ(post);
    }

    if(p_output)
    {
        p_output->i_flags |= i_flags;
        if(!b_valid)
157
            p_output->i_flags |= BLOCK_FLAG_DROP;
158 159 160 161 162
    }

    return p_output;
}

163 164 165 166 167 168 169

/*****************************************************************************
 * Open
 *****************************************************************************/
static int Open(vlc_object_t *p_this)
{
    decoder_t     *p_dec = (decoder_t*)p_this;
170
    decoder_sys_t *p_sys;
171 172 173 174

    if (p_dec->fmt_in.i_codec != VLC_CODEC_HEVC)
        return VLC_EGENERIC;

175
    p_dec->p_sys = p_sys = calloc(1, sizeof(decoder_sys_t));
176 177 178
    if (!p_dec->p_sys)
        return VLC_ENOMEM;

179 180 181 182 183 184 185
    p_sys->p_ccs = cc_storage_new();
    if(unlikely(!p_sys->p_ccs))
    {
        free(p_dec->p_sys);
        return VLC_ENOMEM;
    }

186 187 188
    INITQ(pre);
    INITQ(frame);
    INITQ(post);
189

190
    packetizer_Init(&p_dec->p_sys->packetizer,
191
                    p_hevc_startcode, sizeof(p_hevc_startcode), startcode_FindAnnexB,
Denis Charmet's avatar
Denis Charmet committed
192
                    p_hevc_startcode, 1, 5,
193 194 195 196
                    PacketizeReset, PacketizeParse, PacketizeValidate, p_dec);

    /* Copy properties */
    es_format_Copy(&p_dec->fmt_out, &p_dec->fmt_in);
197
    p_dec->fmt_out.b_packetized = true;
198

199
    /* Init timings */
200
    if( p_dec->fmt_in.video.i_frame_rate_base &&
201 202
        p_dec->fmt_in.video.i_frame_rate &&
        p_dec->fmt_in.video.i_frame_rate <= UINT_MAX / 2 )
203 204 205
        date_Init( &p_sys->dts, p_dec->fmt_in.video.i_frame_rate * 2,
                                p_dec->fmt_in.video.i_frame_rate_base );
    else
206
        date_Init( &p_sys->dts, 2 * 30000, 1001 );
207 208 209 210
    date_Set( &p_sys->dts, VLC_TS_INVALID );
    p_sys->pts = VLC_TS_INVALID;
    p_sys->b_need_ts = true;

211 212 213 214 215 216 217 218 219 220 221 222
    /* Set callbacks */
    const uint8_t *p_extra = p_dec->fmt_in.p_extra;
    const size_t i_extra = p_dec->fmt_in.i_extra;
    /* Check if we have hvcC as extradata */
    if(hevc_ishvcC(p_extra, i_extra))
    {
        p_dec->pf_packetize = PacketizeHVC1;

        /* Clear hvcC/HVC1 extra, to be replaced with AnnexB */
        free(p_dec->fmt_out.p_extra);
        p_dec->fmt_out.i_extra = 0;

223 224 225 226 227 228
        size_t i_new_extra = 0;
        p_dec->fmt_out.p_extra =
                hevc_hvcC_to_AnnexB_NAL(p_extra, i_extra,
                                        &i_new_extra, &p_sys->i_nal_length_size);
        if(p_dec->fmt_out.p_extra)
            p_dec->fmt_out.i_extra = i_new_extra;
229 230 231 232 233
    }
    else
    {
        p_dec->pf_packetize = PacketizeAnnexB;
    }
234
    p_dec->pf_flush = PacketizeFlush;
235
    p_dec->pf_get_cc = GetCc;
236

237 238 239 240 241 242 243
    if(p_dec->fmt_out.i_extra)
    {
        /* Feed with AnnexB VPS/SPS/PPS/SEI extradata */
        packetizer_Header(&p_sys->packetizer,
                          p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra);
    }

244 245 246 247 248 249 250 251 252 253 254 255
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close
 *****************************************************************************/
static void Close(vlc_object_t *p_this)
{
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys = p_dec->p_sys;
    packetizer_Clean(&p_sys->packetizer);

256 257 258 259
    block_ChainRelease(p_sys->frame.p_chain);
    block_ChainRelease(p_sys->pre.p_chain);
    block_ChainRelease(p_sys->post.p_chain);

260
    for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
261
    {
262 263 264 265
        if(p_sys->rg_pps[i].p_decoded)
            hevc_rbsp_release_pps(p_sys->rg_pps[i].p_decoded);
        if(p_sys->rg_pps[i].p_nal)
            block_Release(p_sys->rg_pps[i].p_nal);
266 267
    }

268
    for(unsigned i=0;i<=HEVC_SPS_ID_MAX; i++)
269
    {
270 271 272 273
        if(p_sys->rg_sps[i].p_decoded)
            hevc_rbsp_release_sps(p_sys->rg_sps[i].p_decoded);
        if(p_sys->rg_sps[i].p_nal)
            block_Release(p_sys->rg_sps[i].p_nal);
274 275
    }

276
    for(unsigned i=0;i<=HEVC_VPS_ID_MAX; i++)
277
    {
278 279 280 281
        if(p_sys->rg_vps[i].p_decoded)
            hevc_rbsp_release_vps(p_sys->rg_vps[i].p_decoded);
        if(p_sys->rg_vps[i].p_nal)
            block_Release(p_sys->rg_vps[i].p_nal);
282
    }
283

284 285
    hevc_release_sei_pic_timing( p_sys->p_timing );

286 287
    cc_storage_delete( p_sys->p_ccs );

288 289 290 291 292 293
    free(p_sys);
}

/****************************************************************************
 * Packetize
 ****************************************************************************/
294 295 296 297 298 299 300 301 302
static block_t *PacketizeHVC1(decoder_t *p_dec, block_t **pp_block)
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    return PacketizeXXC1( p_dec, p_sys->i_nal_length_size,
                          pp_block, ParseNALBlock );
}

static block_t *PacketizeAnnexB(decoder_t *p_dec, block_t **pp_block)
303 304 305 306 307 308
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    return packetizer_Packetize(&p_sys->packetizer, pp_block);
}

309 310 311 312 313 314 315
static void PacketizeFlush( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    packetizer_Flush( &p_sys->packetizer );
}

316 317 318
/*****************************************************************************
 * GetCc:
 *****************************************************************************/
319
static block_t *GetCc( decoder_t *p_dec, decoder_cc_desc_t *p_desc )
320
{
321
    return cc_storage_get_current( p_dec->p_sys->p_ccs, p_desc );
322 323
}

324 325 326 327 328 329 330 331 332 333
/****************************************************************************
 * Packetizer Helpers
 ****************************************************************************/
static void PacketizeReset(void *p_private, bool b_broken)
{
    VLC_UNUSED(b_broken);

    decoder_t *p_dec = p_private;
    decoder_sys_t *p_sys = p_dec->p_sys;

334 335 336 337 338
    block_t *p_out = OutputQueues(p_sys, false);
    if(p_out)
        block_ChainRelease(p_out);

    p_sys->b_init_sequence_complete = false;
339 340
    p_sys->b_need_ts = true;
    date_Set(&p_sys->dts, VLC_TS_INVALID);
341 342
}

343
static bool InsertXPS(decoder_t *p_dec, uint8_t i_nal_type, uint8_t i_id,
344
                      const block_t *p_nalb)
345 346
{
    decoder_sys_t *p_sys = p_dec->p_sys;
347
    void **pp_decoded;
348
    void **pp_active;
349
    block_t **pp_nal;
350 351 352 353

    switch(i_nal_type)
    {
        case HEVC_NAL_VPS:
354
            if(i_id > HEVC_VPS_ID_MAX)
355
                return false;
356 357
            pp_decoded = &p_sys->rg_vps[i_id].p_decoded;
            pp_nal = &p_sys->rg_vps[i_id].p_nal;
358
            pp_active = (void**)&p_sys->p_active_vps;
359 360
            break;
        case HEVC_NAL_SPS:
361
            if(i_id > HEVC_SPS_ID_MAX)
362
                return false;
363 364
            pp_decoded = &p_sys->rg_sps[i_id].p_decoded;
            pp_nal = &p_sys->rg_sps[i_id].p_nal;
365
            pp_active = (void**)&p_sys->p_active_sps;
366 367
            break;
        case HEVC_NAL_PPS:
368
            if(i_id > HEVC_PPS_ID_MAX)
369
                return false;
370 371
            pp_decoded = &p_sys->rg_pps[i_id].p_decoded;
            pp_nal = &p_sys->rg_pps[i_id].p_nal;
372
            pp_active = (void**)&p_sys->p_active_pps;
373
            break;
374
        default:
375
            return false;
376 377
    }

378 379 380 381 382 383 384 385 386 387 388 389 390
    /* Check if we really need to re-decode/replace */
    if(*pp_nal)
    {
        const uint8_t *p_stored = (*pp_nal)->p_buffer;
        size_t i_stored = (*pp_nal)->i_buffer;
        hxxx_strip_AnnexB_startcode(&p_stored, &i_stored);
        const uint8_t *p_new = p_nalb->p_buffer;
        size_t i_new = p_nalb->i_buffer;
        hxxx_strip_AnnexB_startcode(&p_new, &i_new);
        if(i_stored == i_new && !memcmp(p_stored, p_new, i_new))
            return true;
    }

391
    /* Free associated decoded version */
392
    if(*pp_decoded)
393
    {
394
        switch(i_nal_type)
395
        {
396 397 398 399 400 401 402 403 404
            case HEVC_NAL_VPS:
                hevc_rbsp_release_vps(*pp_decoded);
                break;
            case HEVC_NAL_SPS:
                hevc_rbsp_release_sps(*pp_decoded);
                break;
            case HEVC_NAL_PPS:
                hevc_rbsp_release_pps(*pp_decoded);
                break;
405
        }
406 407 408 409
        if(*pp_active == *pp_decoded)
            *pp_active = NULL;
        else
            pp_active = NULL; /* don't change pointer */
410
        *pp_decoded = NULL;
411
    }
412
    else pp_active = NULL;
413 414 415

    /* Free raw stored version */
    if(*pp_nal)
416
    {
417 418
        block_Release(*pp_nal);
        *pp_nal = NULL;
419 420
    }

421 422 423
    const uint8_t *p_buffer = p_nalb->p_buffer;
    size_t i_buffer = p_nalb->i_buffer;
    if( hxxx_strip_AnnexB_startcode( &p_buffer, &i_buffer ) )
424
    {
425
        /* Create decoded entries */
426
        switch(i_nal_type)
427
        {
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
            case HEVC_NAL_SPS:
                *pp_decoded = hevc_decode_sps(p_buffer, i_buffer, true);
                if(!*pp_decoded)
                {
                    msg_Err(p_dec, "Failed decoding SPS id %d", i_id);
                    return false;
                }
                break;
            case HEVC_NAL_PPS:
                *pp_decoded = hevc_decode_pps(p_buffer, i_buffer, true);
                if(!*pp_decoded)
                {
                    msg_Err(p_dec, "Failed decoding PPS id %d", i_id);
                    return false;
                }
                break;
            case HEVC_NAL_VPS:
                *pp_decoded = hevc_decode_vps(p_buffer, i_buffer, true);
                if(!*pp_decoded)
                {
                    msg_Err(p_dec, "Failed decoding VPS id %d", i_id);
                    return false;
                }
                break;
452
        }
453

454 455 456
        if(*pp_decoded && pp_active) /* restore active by id */
            *pp_active = *pp_decoded;

457
        *pp_nal = block_Duplicate((block_t *)p_nalb);
458

459
        return true;
460 461
    }

462
    return false;
463 464
}

465 466
static bool XPSReady(decoder_sys_t *p_sys)
{
467
    for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
468
    {
469
        const hevc_picture_parameter_set_t *p_pps = p_sys->rg_pps[i].p_decoded;
470 471 472
        if (p_pps)
        {
            uint8_t id_sps = hevc_get_pps_sps_id(p_pps);
473
            const hevc_sequence_parameter_set_t *p_sps = p_sys->rg_sps[id_sps].p_decoded;
474 475 476
            if(p_sps)
            {
                uint8_t id_vps = hevc_get_sps_vps_id(p_sps);
477
                if(p_sys->rg_vps[id_vps].p_decoded)
478 479 480 481 482 483 484
                    return true;
            }
        }
    }
    return false;
}

485 486 487
static void AppendAsAnnexB(const block_t *p_block,
                           uint8_t **pp_dst, size_t *pi_dst)
{
488
    if(SIZE_MAX - p_block->i_buffer < *pi_dst )
489 490
        return;

491
    size_t i_realloc = p_block->i_buffer + *pi_dst;
492 493 494
    uint8_t *p_realloc = realloc(*pp_dst, i_realloc);
    if(p_realloc)
    {
495
        memcpy(&p_realloc[*pi_dst], p_block->p_buffer, p_block->i_buffer);
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
        *pi_dst = i_realloc;
        *pp_dst = p_realloc;
    }
}

#define APPENDIF(idmax, set, rg, b) \
    for(size_t i=0; i<=idmax; i++)\
    {\
        if(((set != rg[i].p_decoded) == !b) && rg[i].p_nal)\
        {\
            AppendAsAnnexB(rg[i].p_nal, &p_data, &i_data);\
            break;\
        }\
    }

static void SetsToAnnexB(decoder_sys_t *p_sys,
                         const hevc_picture_parameter_set_t *p_pps,
                         const hevc_sequence_parameter_set_t *p_sps,
                         const hevc_video_parameter_set_t *p_vps,
                         uint8_t **pp_out, int *pi_out)
{
    uint8_t *p_data = NULL;
    size_t i_data = 0;

    APPENDIF(HEVC_VPS_ID_MAX, p_vps, p_sys->rg_vps, true);
    APPENDIF(HEVC_VPS_ID_MAX, p_vps, p_sys->rg_vps, false);
    APPENDIF(HEVC_SPS_ID_MAX, p_sps, p_sys->rg_sps, true);
    APPENDIF(HEVC_SPS_ID_MAX, p_sps, p_sys->rg_sps, false);
    APPENDIF(HEVC_PPS_ID_MAX, p_pps, p_sys->rg_pps, true);
    APPENDIF(HEVC_PPS_ID_MAX, p_pps, p_sys->rg_pps, false);

    /* because we copy to i_extra :/ */
    if(i_data <= INT_MAX)
    {
        *pp_out = p_data;
        *pi_out = i_data;
    }
    else free(p_data);
}

536 537 538 539 540 541 542 543 544
static void ActivateSets(decoder_t *p_dec,
                         const hevc_picture_parameter_set_t *p_pps,
                         const hevc_sequence_parameter_set_t *p_sps,
                         const hevc_video_parameter_set_t *p_vps)
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    p_sys->p_active_pps = p_pps;
    p_sys->p_active_sps = p_sps;
    p_sys->p_active_vps = p_vps;
545 546
    if(p_sps)
    {
547
        if(!p_dec->fmt_in.video.i_frame_rate || !p_dec->fmt_in.video.i_frame_rate_base)
548
        {
549
            unsigned num, den;
550
            if(hevc_get_frame_rate( p_sps, p_vps, &num, &den ))
551 552 553
            {
                p_dec->fmt_out.video.i_frame_rate = num;
                p_dec->fmt_out.video.i_frame_rate_base = den;
554 555 556
                if(p_sys->dts.i_divider_den != den &&
                   p_sys->dts.i_divider_num != 2 * num &&
                   num <= UINT_MAX / 2)
557 558
                    date_Change(&p_sys->dts, 2 * num, den);
            }
559 560
        }

561
        if(p_dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF)
562 563 564 565 566 567 568 569 570 571 572 573
        {
            (void) hevc_get_colorimetry( p_sps,
                                         &p_dec->fmt_out.video.primaries,
                                         &p_dec->fmt_out.video.transfer,
                                         &p_dec->fmt_out.video.space,
                                         &p_dec->fmt_out.video.b_color_range_full);
        }

        unsigned sizes[4];
        if( hevc_get_picture_size( p_sps, &sizes[0], &sizes[1],
                                          &sizes[2], &sizes[3] ) )
        {
574 575 576
            p_dec->fmt_out.video.i_width = sizes[0];
            p_dec->fmt_out.video.i_height = sizes[1];
            if(p_dec->fmt_in.video.i_visible_width == 0)
577
            {
578 579
                p_dec->fmt_out.video.i_visible_width = sizes[2];
                p_dec->fmt_out.video.i_visible_height = sizes[3];
580 581 582
            }
        }

583
        if(p_dec->fmt_in.i_profile == -1)
584 585 586 587 588 589 590 591
        {
            uint8_t i_profile, i_level;
            if( hevc_get_sps_profile_tier_level( p_sps, &i_profile, &i_level ) )
            {
                p_dec->fmt_out.i_profile = i_profile;
                p_dec->fmt_out.i_level = i_level;
            }
        }
592 593 594 595

        if(p_dec->fmt_out.i_extra == 0 && p_vps && p_pps)
            SetsToAnnexB(p_sys, p_pps, p_sps, p_vps,
                         (uint8_t **)&p_dec->fmt_out.p_extra, &p_dec->fmt_out.i_extra);
596
    }
597 598
}

599 600 601 602 603 604 605 606
static void GetXPSSet(uint8_t i_pps_id, void *priv,
                      hevc_picture_parameter_set_t **pp_pps,
                      hevc_sequence_parameter_set_t **pp_sps,
                      hevc_video_parameter_set_t **pp_vps)
{
    decoder_sys_t *p_sys = priv;
    *pp_sps = NULL;
    *pp_vps = NULL;
607 608 609
    if((*pp_pps = p_sys->rg_pps[i_pps_id].p_decoded))
        if((*pp_sps = p_sys->rg_sps[hevc_get_pps_sps_id(*pp_pps)].p_decoded))
            *pp_vps = p_sys->rg_vps[hevc_get_sps_vps_id(*pp_sps)].p_decoded;
610 611
}

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
static void ParseStoredSEI( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    for( block_t *p_nal = p_sys->pre.p_chain;
                  p_nal; p_nal = p_nal->p_next )
    {
        if( p_nal->i_buffer < 5 )
            continue;

        if( hevc_getNALType(&p_nal->p_buffer[4]) == HEVC_NAL_PREF_SEI )
        {
            HxxxParse_AnnexB_SEI( p_nal->p_buffer, p_nal->i_buffer,
                                  2 /* nal header */, ParseSEICallback, p_dec );
        }
    }
}

630 631 632
static block_t *ParseVCL(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_frag)
{
    decoder_sys_t *p_sys = p_dec->p_sys;
633
    block_t *p_outputchain = NULL;
634 635 636 637

    const uint8_t *p_buffer = p_frag->p_buffer;
    size_t i_buffer = p_frag->i_buffer;

638 639
    if(unlikely(!hxxx_strip_AnnexB_startcode(&p_buffer, &i_buffer) || i_buffer < 3))
    {
640
        block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag); /* might be corrupted */
641
        return NULL;
642
    }
643

644
    const uint8_t i_layer = hevc_getNALLayer( p_buffer );
645 646 647
    bool b_first_slice_in_pic = p_buffer[2] & 0x80;
    if (b_first_slice_in_pic)
    {
648
        if(p_sys->frame.p_chain)
649
        {
650 651
            /* Starting new frame: return previous frame data for output */
            p_outputchain = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
652 653
        }

654 655 656 657 658 659 660 661 662 663 664
        hevc_slice_segment_header_t *p_sli = hevc_decode_slice_header(p_buffer, i_buffer, true,
                                                                      GetXPSSet, p_sys);
        if(p_sli && i_layer == 0)
        {
            hevc_sequence_parameter_set_t *p_sps;
            hevc_picture_parameter_set_t *p_pps;
            hevc_video_parameter_set_t *p_vps;
            GetXPSSet(hevc_get_slice_pps_id(p_sli), p_sys, &p_pps, &p_sps, &p_vps);
            ActivateSets(p_dec, p_pps, p_sps, p_vps);
        }

665 666
        ParseStoredSEI( p_dec );

667 668 669 670 671 672 673 674 675 676 677 678 679
        switch(i_nal_type)
        {
            case HEVC_NAL_BLA_W_LP:
            case HEVC_NAL_BLA_W_RADL:
            case HEVC_NAL_BLA_N_LP:
            case HEVC_NAL_IDR_W_RADL:
            case HEVC_NAL_IDR_N_LP:
            case HEVC_NAL_CRA:
                p_frag->i_flags |= BLOCK_FLAG_TYPE_I;
                break;

            default:
            {
680
                if(p_sli)
681 682
                {
                    enum hevc_slice_type_e type;
683
                    if(hevc_get_slice_type( p_sli, &type ))
684 685 686 687 688 689 690
                    {
                        if( type == HEVC_SLICE_TYPE_P )
                            p_frag->i_flags |= BLOCK_FLAG_TYPE_P;
                        else
                            p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
                    }
                }
691
                else p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
692 693 694
            }
            break;
        }
695 696 697

        if(p_sli)
            hevc_rbsp_release_slice_header(p_sli);
698 699
    }

700 701 702 703 704
    if(!p_sys->b_init_sequence_complete && i_layer == 0 &&
       (p_frag->i_flags & BLOCK_FLAG_TYPE_I) && XPSReady(p_sys))
    {
        p_sys->b_init_sequence_complete = true;
    }
705

706 707 708
    if( !p_sys->b_init_sequence_complete )
        cc_storage_reset( p_sys->p_ccs );

709 710 711
    block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag);

    return p_outputchain;
712 713
}

714
static block_t * ParseAUHead(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
715
{
716 717 718 719
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t *p_ret = NULL;

    if(p_sys->post.p_chain || p_sys->frame.p_chain)
720
        p_ret = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
721 722 723

    switch(i_nal_type)
    {
724 725
        case HEVC_NAL_AUD:
            if(!p_ret && p_sys->pre.p_chain)
726
                p_ret = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
727 728
            break;

729 730 731 732 733
        case HEVC_NAL_VPS:
        case HEVC_NAL_SPS:
        case HEVC_NAL_PPS:
        {
            uint8_t i_id;
734 735 736 737
            const uint8_t *p_xps = p_nalb->p_buffer;
            size_t i_xps = p_nalb->i_buffer;
            if(hxxx_strip_AnnexB_startcode(&p_xps, &i_xps) &&
               hevc_get_xps_id(p_nalb->p_buffer, p_nalb->i_buffer, &i_id))
738
                InsertXPS(p_dec, i_nal_type, i_id, p_nalb);
739
            break;
740
        }
741 742

        case HEVC_NAL_PREF_SEI:
743
            /* stored an parsed later when we get sps & frame */
744
        default:
745
            break;
746
    }
747

748 749 750 751 752 753 754 755 756 757 758 759 760 761
    block_ChainLastAppend(&p_sys->pre.pp_chain_last, p_nalb);

    return p_ret;
}

static block_t * ParseAUTail(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t *p_ret = NULL;

    block_ChainLastAppend(&p_sys->post.pp_chain_last, p_nalb);

    switch(i_nal_type)
    {
762 763
        case HEVC_NAL_EOS:
        case HEVC_NAL_EOB:
764
            p_ret = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
765 766
            if( p_ret )
                p_ret->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
767
            break;
768 769 770 771 772

        case HEVC_NAL_SUFF_SEI:
            HxxxParse_AnnexB_SEI( p_nalb->p_buffer, p_nalb->i_buffer,
                                  2 /* nal header */, ParseSEICallback, p_dec );
            break;
773 774
    }

775 776 777
    if(!p_ret && p_sys->frame.p_chain == NULL)
        p_ret = OutputQueues(p_sys, false);

778
    return p_ret;
779 780
}

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
static block_t * ParseNonVCL(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
{
    block_t *p_ret = NULL;

    if ( (i_nal_type >= HEVC_NAL_VPS        && i_nal_type <= HEVC_NAL_AUD) ||
          i_nal_type == HEVC_NAL_PREF_SEI ||
         (i_nal_type >= HEVC_NAL_RSV_NVCL41 && i_nal_type <= HEVC_NAL_RSV_NVCL44) ||
         (i_nal_type >= HEVC_NAL_UNSPEC48   && i_nal_type <= HEVC_NAL_UNSPEC55) )
    {
        p_ret = ParseAUHead(p_dec, i_nal_type, p_nalb);
    }
    else
    {
        p_ret = ParseAUTail(p_dec, i_nal_type, p_nalb);
    }

    return p_ret;
}

static block_t *GatherAndValidateChain(block_t *p_outputchain)
{
    block_t *p_output = NULL;

    if(p_outputchain)
    {
806
        if(p_outputchain->i_flags & BLOCK_FLAG_DROP)
807 808 809 810 811
            p_output = p_outputchain; /* Avoid useless gather */
        else
            p_output = block_ChainGather(p_outputchain);
    }

812
    if(p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
813 814 815 816 817 818 819 820
    {
        block_ChainRelease(p_output); /* Chain! see above */
        p_output = NULL;
    }

    return p_output;
}

821 822 823 824 825 826 827 828 829
static void SetOutputBlockProperties(decoder_t *p_dec, block_t *p_output)
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    /* Set frame duration */
    if(p_sys->p_active_sps)
    {
        uint8_t i_num_clock_ts = hevc_get_num_clock_ts(p_sys->p_active_sps,
                                                       p_sys->p_timing);
        const mtime_t i_start = date_Get(&p_sys->dts);
830 831 832 833 834
        if( i_start != VLC_TS_INVALID )
        {
            date_Increment(&p_sys->dts, i_num_clock_ts);
            p_output->i_length = date_Get(&p_sys->dts) - i_start;
        }
835 836 837 838 839 840
        p_sys->pts = VLC_TS_INVALID;
    }
    hevc_release_sei_pic_timing(p_sys->p_timing);
    p_sys->p_timing = NULL;
}

841 842 843 844 845
/*****************************************************************************
 * ParseNALBlock: parses annexB type NALs
 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
 *****************************************************************************/
static block_t *ParseNALBlock(decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag)
846 847
{
    decoder_sys_t *p_sys = p_dec->p_sys;
848 849 850 851 852 853 854 855 856
    *pb_ts_used = false;

    if(p_sys->b_need_ts)
    {
        if(p_frag->i_dts > VLC_TS_INVALID)
            date_Set(&p_sys->dts, p_frag->i_dts);
        p_sys->pts = p_frag->i_pts;
        if(date_Get( &p_sys->dts ) != VLC_TS_INVALID)
            p_sys->b_need_ts = false;
857
        *pb_ts_used = true;
858
    }
859

860 861 862 863 864 865
    if(unlikely(p_frag->i_buffer < 5))
    {
        msg_Warn(p_dec,"NAL too small");
        block_Release(p_frag);
        return NULL;
    }
866

867
    if(p_frag->p_buffer[4] & 0x80)
868
    {
869
        msg_Warn(p_dec,"Forbidden zero bit not null, corrupted NAL");
870
        block_Release(p_frag);
871
        return GatherAndValidateChain(OutputQueues(p_sys, false)); /* will drop */
872 873
    }

874
    /* Get NALU type */
875
    const mtime_t dts = p_frag->i_dts, pts = p_frag->i_pts;
876
    block_t * p_output = NULL;
877
    uint8_t i_nal_type = hevc_getNALType(&p_frag->p_buffer[4]);
878

879
    if (i_nal_type < HEVC_NAL_VPS)
880 881
    {
        /* NAL is a VCL NAL */
882
        p_output = ParseVCL(p_dec, i_nal_type, p_frag);
883
        if (p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
884
            msg_Info(p_dec, "Waiting for VPS/SPS/PPS");
885 886 887
    }
    else
    {
888
        p_output = ParseNonVCL(p_dec, i_nal_type, p_frag);
889 890
    }

891
    p_output = GatherAndValidateChain(p_output);
892
    if(p_output)
893 894
    {
        SetOutputBlockProperties( p_dec, p_output );
895 896 897
        if (dts > VLC_TS_INVALID)
            date_Set(&p_sys->dts, dts);
        p_sys->pts = pts;
898
        *pb_ts_used = true;
899
    }
900

901
    return p_output;
902 903
}

904 905 906
static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *p_block)
{
    decoder_t *p_dec = p_private;
907
    decoder_sys_t *p_sys = p_dec->p_sys;
908 909 910 911 912

    /* Remove trailing 0 bytes */
    while (p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
        p_block->i_buffer--;

913 914 915 916 917
    p_block = ParseNALBlock( p_dec, pb_ts_used, p_block );
    if( p_block )
        cc_storage_commit( p_sys->p_ccs, p_block );

    return p_block;
918 919
}

920 921 922 923 924 925
static int PacketizeValidate( void *p_private, block_t *p_au )
{
    VLC_UNUSED(p_private);
    VLC_UNUSED(p_au);
    return VLC_SUCCESS;
}
926 927 928 929 930 931

static bool ParseSEICallback( const hxxx_sei_data_t *p_sei_data, void *cbdata )
{
    decoder_t *p_dec = (decoder_t *) cbdata;
    decoder_sys_t *p_sys = p_dec->p_sys;

932
    switch( p_sei_data->i_type )
933
    {
934 935 936 937 938 939 940 941 942
        case HXXX_SEI_PIC_TIMING:
        {
            if( p_sys->p_active_sps )
            {
                hevc_release_sei_pic_timing( p_sys->p_timing );
                p_sys->p_timing = hevc_decode_sei_pic_timing( p_sei_data->p_bs,
                                                              p_sys->p_active_sps );
            }
        } break;
943
        case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35:
944
        {
945 946 947 948 949 950
            if( p_sei_data->itu_t35.type == HXXX_ITU_T35_TYPE_CC )
            {
                cc_storage_append( p_sys->p_ccs, true, p_sei_data->itu_t35.u.cc.p_data,
                                                       p_sei_data->itu_t35.u.cc.i_data );
            }
        } break;
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
        case HXXX_SEI_FRAME_PACKING_ARRANGEMENT:
        {
            if( p_dec->fmt_in.video.multiview_mode == MULTIVIEW_2D )
            {
                video_multiview_mode_t mode;
                switch( p_sei_data->frame_packing.type )
                {
                    case FRAME_PACKING_INTERLEAVED_CHECKERBOARD:
                        mode = MULTIVIEW_STEREO_CHECKERBOARD; break;
                    case FRAME_PACKING_INTERLEAVED_COLUMN:
                        mode = MULTIVIEW_STEREO_COL; break;
                    case FRAME_PACKING_INTERLEAVED_ROW:
                        mode = MULTIVIEW_STEREO_ROW; break;
                    case FRAME_PACKING_SIDE_BY_SIDE:
                        mode = MULTIVIEW_STEREO_SBS; break;
                    case FRAME_PACKING_TOP_BOTTOM:
                        mode = MULTIVIEW_STEREO_TB; break;
                    case FRAME_PACKING_TEMPORAL:
                        mode = MULTIVIEW_STEREO_FRAME; break;
                    case FRAME_PACKING_TILED:
                    default:
                        mode = MULTIVIEW_2D; break;
                }
                p_dec->fmt_out.video.multiview_mode = mode;
            }
        } break;
977 978 979 980 981 982 983 984 985 986 987 988 989
        case HXXX_SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
        {
            video_format_t *p_fmt = &p_dec->fmt_out.video;
            for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.primaries); ++i)
                p_fmt->mastering.primaries[i] = p_sei_data->colour_volume.primaries[i];
            for (size_t i=0; i<ARRAY_SIZE(p_sei_data->colour_volume.white_point); ++i)
                p_fmt->mastering.white_point[i] = p_sei_data->colour_volume.white_point[i];
            p_fmt->mastering.max_luminance = p_sei_data->colour_volume.max_luminance;
            p_fmt->mastering.min_luminance = p_sei_data->colour_volume.min_luminance;
        } break;
        case HXXX_SEI_CONTENT_LIGHT_LEVEL:
        {
            video_format_t *p_fmt = &p_dec->fmt_out.video;
990 991
            p_fmt->lighting.MaxCLL = p_sei_data->content_light_lvl.MaxCLL;
            p_fmt->lighting.MaxFALL = p_sei_data->content_light_lvl.MaxFALL;
992
        } break;
993
    }
994 995 996

    return true;
}