hevc.c 31.6 KB
Newer Older
Denis Charmet's avatar
Denis Charmet committed
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>
Denis Charmet's avatar
Denis Charmet committed
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"
Denis Charmet's avatar
Denis Charmet committed
45

46 47
#include <limits.h>

Denis Charmet's avatar
Denis Charmet committed
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 * );
Denis Charmet's avatar
Denis Charmet committed
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 *);
Denis Charmet's avatar
Denis Charmet committed
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 * );
Denis Charmet's avatar
Denis Charmet committed
75

76
typedef struct
Denis Charmet's avatar
Denis Charmet committed
77 78 79 80
{
    /* */
    packetizer_t packetizer;

81 82 83 84 85
    struct
    {
        block_t *p_chain;
        block_t **pp_chain_last;
    } frame, pre, post;
Denis Charmet's avatar
Denis Charmet committed
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;
François Cartegnie's avatar
François Cartegnie committed
100
    hevc_sei_pic_timing_t *p_timing;
101
    bool b_init_sequence_complete;
102

103
    date_t dts;
Steve Lhomme's avatar
Steve Lhomme committed
104
    vlc_tick_t pts;
105 106
    bool b_need_ts;

107 108
    /* */
    cc_storage_t *p_ccs;
109
} decoder_sys_t;
Denis Charmet's avatar
Denis Charmet committed
110

111 112
#define BLOCK_FLAG_DROP (1 << BLOCK_FLAG_PRIVATE_SHIFT)

Denis Charmet's avatar
Denis Charmet committed
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;
}

Denis Charmet's avatar
Denis Charmet committed
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;
Denis Charmet's avatar
Denis Charmet committed
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));
Denis Charmet's avatar
Denis Charmet committed
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_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,
Denis Charmet's avatar
Denis Charmet committed
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;
Denis Charmet's avatar
Denis Charmet committed
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
François Cartegnie's avatar
François Cartegnie committed
206
        date_Init( &p_sys->dts, 2 * 30000, 1001 );
207
    p_sys->pts = VLC_TICK_INVALID;
208 209
    p_sys->b_need_ts = true;

210 211 212 213 214 215 216 217 218 219 220 221
    /* 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;

222 223 224 225 226 227
        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;
228 229 230 231 232
    }
    else
    {
        p_dec->pf_packetize = PacketizeAnnexB;
    }
233
    p_dec->pf_flush = PacketizeFlush;
234
    p_dec->pf_get_cc = GetCc;
Denis Charmet's avatar
Denis Charmet committed
235

236 237 238 239 240 241 242
    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);
    }

Denis Charmet's avatar
Denis Charmet committed
243 244 245 246 247 248 249 250 251 252 253 254
    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);

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

259
    for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
260
    {
261 262 263 264
        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);
265 266
    }

267
    for(unsigned i=0;i<=HEVC_SPS_ID_MAX; i++)
268
    {
269 270 271 272
        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);
273 274
    }

275
    for(unsigned i=0;i<=HEVC_VPS_ID_MAX; i++)
276
    {
277 278 279 280
        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);
281
    }
282

François Cartegnie's avatar
François Cartegnie committed
283 284
    hevc_release_sei_pic_timing( p_sys->p_timing );

285 286
    cc_storage_delete( p_sys->p_ccs );

Denis Charmet's avatar
Denis Charmet committed
287 288 289 290 291 292
    free(p_sys);
}

/****************************************************************************
 * Packetize
 ****************************************************************************/
293 294 295 296 297 298 299 300 301
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)
Denis Charmet's avatar
Denis Charmet committed
302 303 304 305 306 307
{
    decoder_sys_t *p_sys = p_dec->p_sys;

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

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

    packetizer_Flush( &p_sys->packetizer );
}

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

Denis Charmet's avatar
Denis Charmet committed
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
    p_sys->b_need_ts = true;
340
    date_Set(&p_sys->dts, VLC_TICK_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
        {
François Cartegnie's avatar
François Cartegnie committed
549
            unsigned num, den;
550
            if(hevc_get_frame_rate( p_sps, p_vps, &num, &den ))
François Cartegnie's avatar
François Cartegnie committed
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)
François Cartegnie's avatar
François Cartegnie committed
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
        {
            (void) hevc_get_colorimetry( p_sps,
                                         &p_dec->fmt_out.video.primaries,
                                         &p_dec->fmt_out.video.transfer,
                                         &p_dec->fmt_out.video.space,
567
                                         &p_dec->fmt_out.video.color_range);
568 569 570 571 572 573
        }

        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 691 692 693 694 695 696
                        switch(type)
                        {
                            case HEVC_SLICE_TYPE_B:
                                p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
                                break;
                            case HEVC_SLICE_TYPE_P:
                                p_frag->i_flags |= BLOCK_FLAG_TYPE_P;
                                break;
                            case HEVC_SLICE_TYPE_I:
                                p_frag->i_flags |= BLOCK_FLAG_TYPE_I;
                                break;
                        }
697 698
                    }
                }
699
                else p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
700 701 702
            }
            break;
        }
703 704 705

        if(p_sli)
            hevc_rbsp_release_slice_header(p_sli);
706 707
    }

708 709 710 711 712
    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;
    }
713

714 715 716
    if( !p_sys->b_init_sequence_complete )
        cc_storage_reset( p_sys->p_ccs );

717 718 719
    block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag);

    return p_outputchain;
720 721
}

722
static block_t * ParseAUHead(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
723
{
724 725 726 727
    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)
728
        p_ret = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
729 730 731

    switch(i_nal_type)
    {
732 733
        case HEVC_NAL_AUD:
            if(!p_ret && p_sys->pre.p_chain)
734
                p_ret = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
735 736
            break;

737 738 739 740 741
        case HEVC_NAL_VPS:
        case HEVC_NAL_SPS:
        case HEVC_NAL_PPS:
        {
            uint8_t i_id;
742 743 744 745
            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))
746
                InsertXPS(p_dec, i_nal_type, i_id, p_nalb);
747
            break;
748
        }
749 750

        case HEVC_NAL_PREF_SEI:
751
            /* stored an parsed later when we get sps & frame */
752
        default:
753
            break;
754
    }
755

756 757 758 759 760 761 762 763 764 765 766 767 768 769
    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)
    {
770 771
        case HEVC_NAL_EOS:
        case HEVC_NAL_EOB:
772
            p_ret = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
773 774
            if( p_ret )
                p_ret->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
775
            break;
776 777 778 779 780

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

783 784 785
    if(!p_ret && p_sys->frame.p_chain == NULL)
        p_ret = OutputQueues(p_sys, false);

786
    return p_ret;
Denis Charmet's avatar
Denis Charmet committed
787 788
}

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
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)
    {
814
        if(p_outputchain->i_flags & BLOCK_FLAG_DROP)
815 816 817 818 819
            p_output = p_outputchain; /* Avoid useless gather */
        else
            p_output = block_ChainGather(p_outputchain);
    }

820
    if(p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
821 822 823 824 825 826 827 828
    {
        block_ChainRelease(p_output); /* Chain! see above */
        p_output = NULL;
    }

    return p_output;
}

François Cartegnie's avatar
François Cartegnie committed
829 830 831 832 833 834 835 836
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);
Steve Lhomme's avatar
Steve Lhomme committed
837
        const vlc_tick_t i_start = date_Get(&p_sys->dts);
838
        if( i_start != VLC_TICK_INVALID )
839 840 841 842
        {
            date_Increment(&p_sys->dts, i_num_clock_ts);
            p_output->i_length = date_Get(&p_sys->dts) - i_start;
        }
843
        p_sys->pts = VLC_TICK_INVALID;
François Cartegnie's avatar
François Cartegnie committed
844 845 846 847 848
    }
    hevc_release_sei_pic_timing(p_sys->p_timing);
    p_sys->p_timing = NULL;
}

849 850 851 852 853
/*****************************************************************************
 * 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)
Denis Charmet's avatar
Denis Charmet committed
854 855
{
    decoder_sys_t *p_sys = p_dec->p_sys;
856 857 858 859
    *pb_ts_used = false;

    if(p_sys->b_need_ts)
    {
860
        if(p_frag->i_dts != VLC_TICK_INVALID)
861 862
            date_Set(&p_sys->dts, p_frag->i_dts);
        p_sys->pts = p_frag->i_pts;
863
        if(date_Get( &p_sys->dts ) != VLC_TICK_INVALID)
864
            p_sys->b_need_ts = false;
865
        *pb_ts_used = true;
866
    }
Denis Charmet's avatar
Denis Charmet committed
867

868 869 870 871 872 873
    if(unlikely(p_frag->i_buffer < 5))
    {
        msg_Warn(p_dec,"NAL too small");
        block_Release(p_frag);
        return NULL;
    }
Denis Charmet's avatar
Denis Charmet committed
874

875
    if(p_frag->p_buffer[4] & 0x80)
Denis Charmet's avatar
Denis Charmet committed
876
    {
877
        msg_Warn(p_dec,"Forbidden zero bit not null, corrupted NAL");
878
        block_Release(p_frag);
879
        return GatherAndValidateChain(OutputQueues(p_sys, false)); /* will drop */
Denis Charmet's avatar
Denis Charmet committed
880 881
    }

882
    /* Get NALU type */
Steve Lhomme's avatar
Steve Lhomme committed
883
    const vlc_tick_t dts = p_frag->i_dts, pts = p_frag->i_pts;
884
    block_t * p_output = NULL;
885
    uint8_t i_nal_type = hevc_getNALType(&p_frag->p_buffer[4]);
886

887
    if (i_nal_type < HEVC_NAL_VPS)
Denis Charmet's avatar
Denis Charmet committed
888 889
    {
        /* NAL is a VCL NAL */
890
        p_output = ParseVCL(p_dec, i_nal_type, p_frag);
891
        if (p_output && (p_output->i_flags & BLOCK_FLAG_DROP))
892
            msg_Info(p_dec, "Waiting for VPS/SPS/PPS");
Denis Charmet's avatar
Denis Charmet committed
893 894 895
    }
    else
    {
896
        p_output = ParseNonVCL(p_dec, i_nal_type, p_frag);
Denis Charmet's avatar
Denis Charmet committed
897 898
    }

899
    p_output = GatherAndValidateChain(p_output);
900
    if(p_output)
François Cartegnie's avatar
François Cartegnie committed
901 902
    {
        SetOutputBlockProperties( p_dec, p_output );
903
        if (dts != VLC_TICK_INVALID)
904 905
            date_Set(&p_sys->dts, dts);
        p_sys->pts = pts;
906
        *pb_ts_used = true;
François Cartegnie's avatar
François Cartegnie committed
907
    }
908

909
    return p_output;
Denis Charmet's avatar
Denis Charmet committed
910 911
}

912 913 914
static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *p_block)
{
    decoder_t *p_dec = p_private;
915
    decoder_sys_t *p_sys = p_dec->p_sys;
916 917 918 919 920

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

921 922 923 924 925
    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;
926 927
}

Denis Charmet's avatar
Denis Charmet committed
928 929 930 931 932 933
static int PacketizeValidate( void *p_private, block_t *p_au )
{
    VLC_UNUSED(p_private);
    VLC_UNUSED(p_au);
    return VLC_SUCCESS;
}
934 935 936 937 938 939

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;

940
    switch( p_sei_data->i_type )
941
    {
François Cartegnie's avatar
François Cartegnie committed
942 943 944 945 946 947 948 949 950
        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;
951
        case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35:
952
        {
953 954 955 956 957 958
            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;
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
        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;
985 986 987 988 989 990 991 992 993 994 995 996 997
        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;
998 999
            p_fmt->lighting.MaxCLL = p_sei_data->content_light_lvl.MaxCLL;
            p_fmt->lighting.MaxFALL = p_sei_data->content_light_lvl.MaxFALL;
1000
        } break;
1001
    }
1002 1003 1004

    return true;
}