hevc.c 24.2 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

/*****************************************************************************
 * 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
 ****************************************************************************/
64
65
static block_t *PacketizeAnnexB(decoder_t *, block_t **);
static block_t *PacketizeHVC1(decoder_t *, block_t **);
66
static void PacketizeFlush( decoder_t * );
Denis Charmet's avatar
Denis Charmet committed
67
68
static void PacketizeReset(void *p_private, bool b_broken);
static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *);
69
static block_t *ParseNALBlock(decoder_t *, bool *pb_ts_used, block_t *);
Denis Charmet's avatar
Denis Charmet committed
70
static int PacketizeValidate(void *p_private, block_t *);
71
72
static bool ParseSEICallback( const hxxx_sei_data_t *, void * );
static block_t *GetCc( decoder_t *, bool pb_present[4] );
Denis Charmet's avatar
Denis Charmet committed
73
74
75
76
77
78

struct decoder_sys_t
{
    /* */
    packetizer_t packetizer;

79
80
81
82
83
    struct
    {
        block_t *p_chain;
        block_t **pp_chain_last;
    } frame, pre, post;
Denis Charmet's avatar
Denis Charmet committed
84

85
    uint8_t  i_nal_length_size;
86
87
88
    hevc_video_parameter_set_t    *rgi_p_decvps[HEVC_VPS_ID_MAX + 1];
    hevc_sequence_parameter_set_t *rgi_p_decsps[HEVC_SPS_ID_MAX + 1];
    hevc_picture_parameter_set_t  *rgi_p_decpps[HEVC_PPS_ID_MAX + 1];
89
90
91
    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;
92
    bool b_init_sequence_complete;
93
94
95

    /* */
    cc_storage_t *p_ccs;
Denis Charmet's avatar
Denis Charmet committed
96
97
98
};

static const uint8_t p_hevc_startcode[3] = {0x00, 0x00, 0x01};
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
140
141
142
143
144
145
146
147
148
149
150
/****************************************************************************
 * 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;
        if(p_output && p_output->i_dts == 0)
        {
            p_output->i_dts = p_sys->frame.p_chain->i_dts;
            p_output->i_pts = p_sys->frame.p_chain->i_pts;
        }
        block_ChainLastAppend(&pp_output_last, p_sys->frame.p_chain);
        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)
            p_output->i_flags |= BLOCK_FLAG_CORRUPTED;
    }

    return p_output;
}

Denis Charmet's avatar
Denis Charmet committed
151
152
153
154
155
156
157

/*****************************************************************************
 * Open
 *****************************************************************************/
static int Open(vlc_object_t *p_this)
{
    decoder_t     *p_dec = (decoder_t*)p_this;
158
    decoder_sys_t *p_sys;
Denis Charmet's avatar
Denis Charmet committed
159
160
161
162

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

163
    p_dec->p_sys = p_sys = calloc(1, sizeof(decoder_sys_t));
Denis Charmet's avatar
Denis Charmet committed
164
165
166
    if (!p_dec->p_sys)
        return VLC_ENOMEM;

167
168
169
170
171
172
173
    p_sys->p_ccs = cc_storage_new();
    if(unlikely(!p_sys->p_ccs))
    {
        free(p_dec->p_sys);
        return VLC_ENOMEM;
    }

174
175
176
    INITQ(pre);
    INITQ(frame);
    INITQ(post);
177

Denis Charmet's avatar
Denis Charmet committed
178
    packetizer_Init(&p_dec->p_sys->packetizer,
179
                    p_hevc_startcode, sizeof(p_hevc_startcode), startcode_FindAnnexB,
Denis Charmet's avatar
Denis Charmet committed
180
                    p_hevc_startcode, 1, 5,
Denis Charmet's avatar
Denis Charmet committed
181
182
183
184
                    PacketizeReset, PacketizeParse, PacketizeValidate, p_dec);

    /* Copy properties */
    es_format_Copy(&p_dec->fmt_out, &p_dec->fmt_in);
185
    p_dec->fmt_out.b_packetized = true;
Denis Charmet's avatar
Denis Charmet committed
186

187
188
189
190
191
192
193
194
195
196
197
198
    /* 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;

199
200
201
202
203
204
        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;
205
206
207
208
209
    }
    else
    {
        p_dec->pf_packetize = PacketizeAnnexB;
    }
210
    p_dec->pf_flush = PacketizeFlush;
211
    p_dec->pf_get_cc = GetCc;
Denis Charmet's avatar
Denis Charmet committed
212

213
214
215
216
217
218
219
    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
220
221
222
223
224
225
226
227
228
229
230
231
    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);

232
233
234
235
    block_ChainRelease(p_sys->frame.p_chain);
    block_ChainRelease(p_sys->pre.p_chain);
    block_ChainRelease(p_sys->post.p_chain);

236
    for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
237
238
239
240
241
    {
        if(p_sys->rgi_p_decpps[i])
            hevc_rbsp_release_pps(p_sys->rgi_p_decpps[i]);
    }

242
    for(unsigned i=0;i<=HEVC_SPS_ID_MAX; i++)
243
244
245
246
247
    {
        if(p_sys->rgi_p_decsps[i])
            hevc_rbsp_release_sps(p_sys->rgi_p_decsps[i]);
    }

248
    for(unsigned i=0;i<=HEVC_VPS_ID_MAX; i++)
249
250
251
252
    {
        if(p_sys->rgi_p_decvps[i])
            hevc_rbsp_release_vps(p_sys->rgi_p_decvps[i]);
    }
253

254
255
    cc_storage_delete( p_sys->p_ccs );

Denis Charmet's avatar
Denis Charmet committed
256
257
258
259
260
261
    free(p_sys);
}

/****************************************************************************
 * Packetize
 ****************************************************************************/
262
263
264
265
266
267
268
269
270
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
271
272
273
274
275
276
{
    decoder_sys_t *p_sys = p_dec->p_sys;

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

277
278
279
280
281
282
283
static void PacketizeFlush( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    packetizer_Flush( &p_sys->packetizer );
}

284
285
286
287
288
289
290
291
/*****************************************************************************
 * GetCc:
 *****************************************************************************/
static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
{
    return cc_storage_get_current( p_dec->p_sys->p_ccs, pb_present );
}

Denis Charmet's avatar
Denis Charmet committed
292
293
294
295
296
297
298
299
300
301
/****************************************************************************
 * 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;

302
303
304
305
306
    block_t *p_out = OutputQueues(p_sys, false);
    if(p_out)
        block_ChainRelease(p_out);

    p_sys->b_init_sequence_complete = false;
307
308
}

309
static bool InsertXPS(decoder_t *p_dec, uint8_t i_nal_type, uint8_t i_id,
310
                      const block_t *p_nalb)
311
312
{
    decoder_sys_t *p_sys = p_dec->p_sys;
313
    bool b_active = false;
314
315
316
317

    switch(i_nal_type)
    {
        case HEVC_NAL_VPS:
318
            if(i_id > HEVC_VPS_ID_MAX)
319
                return false;
320
321
            break;
        case HEVC_NAL_SPS:
322
            if(i_id > HEVC_SPS_ID_MAX)
323
                return false;
324
325
            break;
        case HEVC_NAL_PPS:
326
            if(i_id > HEVC_PPS_ID_MAX)
327
                return false;
328
            break;
329
        default:
330
            return false;
331
332
    }

333
334
    /* Free associated decoded version */
    if(i_nal_type == HEVC_NAL_SPS && p_sys->rgi_p_decsps[i_id])
335
    {
336
        hevc_rbsp_release_sps(p_sys->rgi_p_decsps[i_id]);
337
338
339
340
341
        if(p_sys->p_active_sps == p_sys->rgi_p_decsps[i_id])
        {
           p_sys->p_active_sps = NULL;
           b_active = true;
        }
342
343
344
345
346
        p_sys->rgi_p_decsps[i_id] = NULL;
    }
    else if(i_nal_type == HEVC_NAL_PPS && p_sys->rgi_p_decpps[i_id])
    {
        hevc_rbsp_release_pps(p_sys->rgi_p_decpps[i_id]);
347
348
349
350
351
        if(p_sys->p_active_pps == p_sys->rgi_p_decpps[i_id])
        {
            p_sys->p_active_pps = NULL;
            b_active = true;
        }
352
353
354
355
356
        p_sys->rgi_p_decpps[i_id] = NULL;
    }
    else if(i_nal_type == HEVC_NAL_VPS && p_sys->rgi_p_decvps[i_id])
    {
        hevc_rbsp_release_vps(p_sys->rgi_p_decvps[i_id]);
357
358
359
360
361
        if(p_sys->p_active_vps == p_sys->rgi_p_decvps[i_id])
        {
           p_sys->p_active_vps = NULL;
           b_active = true;
        }
362
        p_sys->rgi_p_decvps[i_id] = NULL;
363
364
    }

365
366
367
    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 ) )
368
    {
369
370
        /* Create decoded entries */
        if(i_nal_type == HEVC_NAL_SPS)
371
        {
372
373
            p_sys->rgi_p_decsps[i_id] = hevc_decode_sps(p_buffer, i_buffer, true);
            if(!p_sys->rgi_p_decsps[i_id])
374
            {
375
376
                msg_Err(p_dec, "Failed decoding SPS id %d", i_id);
                return false;
377
            }
378
379
            if(b_active)
                p_sys->p_active_sps = p_sys->rgi_p_decsps[i_id];
380
381
382
383
384
        }
        else if(i_nal_type == HEVC_NAL_PPS)
        {
            p_sys->rgi_p_decpps[i_id] = hevc_decode_pps(p_buffer, i_buffer, true);
            if(!p_sys->rgi_p_decpps[i_id])
385
            {
386
387
                msg_Err(p_dec, "Failed decoding PPS id %d", i_id);
                return false;
388
            }
389
390
            if(b_active)
                p_sys->p_active_pps = p_sys->rgi_p_decpps[i_id];
391
392
393
394
395
        }
        else if(i_nal_type == HEVC_NAL_VPS)
        {
            p_sys->rgi_p_decvps[i_id] = hevc_decode_vps(p_buffer, i_buffer, true);
            if(!p_sys->rgi_p_decvps[i_id])
396
            {
397
398
                msg_Err(p_dec, "Failed decoding VPS id %d", i_id);
                return false;
399
            }
400
401
            if(b_active)
                p_sys->p_active_vps = p_sys->rgi_p_decvps[i_id];
402
        }
403
        return true;
404
405
406

    }

407
    return false;
408
409
}

410
411
static bool XPSReady(decoder_sys_t *p_sys)
{
412
    for(unsigned i=0;i<=HEVC_PPS_ID_MAX; i++)
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
    {
        const hevc_picture_parameter_set_t *p_pps = p_sys->rgi_p_decpps[i];
        if (p_pps)
        {
            uint8_t id_sps = hevc_get_pps_sps_id(p_pps);
            const hevc_sequence_parameter_set_t *p_sps = p_sys->rgi_p_decsps[id_sps];
            if(p_sps)
            {
                uint8_t id_vps = hevc_get_sps_vps_id(p_sps);
                if(p_sys->rgi_p_decvps[id_vps])
                    return true;
            }
        }
    }
    return false;
}

430
431
432
433
434
435
436
437
438
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;
439
440
    if(p_sps)
    {
441
        if(!p_dec->fmt_in.video.i_frame_rate)
442
443
444
445
446
447
        {
            (void) hevc_get_frame_rate( p_sps, p_dec->p_sys->rgi_p_decvps,
                                        &p_dec->fmt_out.video.i_frame_rate,
                                        &p_dec->fmt_out.video.i_frame_rate_base );
        }

448
        if(p_dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF)
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
        {
            (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] ) )
        {
            if( p_dec->fmt_out.video.i_width != sizes[0] ||
                p_dec->fmt_out.video.i_height != sizes[1] )
            {
                p_dec->fmt_out.video.i_width = sizes[0];
                p_dec->fmt_out.video.i_height = sizes[1];
            }
        }

469
        if(p_dec->fmt_in.i_profile == -1)
470
471
472
473
474
475
476
477
478
        {
            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;
            }
        }
    }
479
480
}

481
482
483
484
485
486
487
488
489
490
491
492
493
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;
    if((*pp_pps = p_sys->rgi_p_decpps[i_pps_id]))
        if((*pp_sps = p_sys->rgi_p_decsps[hevc_get_pps_sps_id(*pp_pps)]))
            *pp_vps = p_sys->rgi_p_decvps[hevc_get_sps_vps_id(*pp_sps)];
}

494
495
496
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;
497
    block_t *p_outputchain = NULL;
498
499
500
501

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

502
503
    if(unlikely(!hxxx_strip_AnnexB_startcode(&p_buffer, &i_buffer) || i_buffer < 3))
    {
504
        block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag); /* might be corrupted */
505
        return NULL;
506
    }
507

508
    const uint8_t i_layer = hevc_getNALLayer( p_buffer );
509
510
511
    bool b_first_slice_in_pic = p_buffer[2] & 0x80;
    if (b_first_slice_in_pic)
    {
512
        if(p_sys->frame.p_chain)
513
        {
514
515
            /* Starting new frame: return previous frame data for output */
            p_outputchain = OutputQueues(p_sys, p_sys->b_init_sequence_complete);
516
517
        }

518
519
520
521
522
523
524
525
526
527
528
        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);
        }

529
530
531
532
533
534
535
536
537
538
539
540
541
        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:
            {
542
                if(p_sli)
543
544
                {
                    enum hevc_slice_type_e type;
545
                    if(hevc_get_slice_type( p_sli, &type ))
546
547
548
549
550
551
552
                    {
                        if( type == HEVC_SLICE_TYPE_P )
                            p_frag->i_flags |= BLOCK_FLAG_TYPE_P;
                        else
                            p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
                    }
                }
553
                else p_frag->i_flags |= BLOCK_FLAG_TYPE_B;
554
555
556
            }
            break;
        }
557
558
559

        if(p_sli)
            hevc_rbsp_release_slice_header(p_sli);
560
561
    }

562
563
564
565
566
    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;
    }
567

568
569
570
    if( !p_sys->b_init_sequence_complete )
        cc_storage_reset( p_sys->p_ccs );

571
572
573
    block_ChainLastAppend(&p_sys->frame.pp_chain_last, p_frag);

    return p_outputchain;
574
575
}

576
static block_t * ParseAUHead(decoder_t *p_dec, uint8_t i_nal_type, block_t *p_nalb)
577
{
578
579
580
581
582
    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)
        p_ret = OutputQueues(p_sys, true);
583
584
585

    switch(i_nal_type)
    {
586
587
588
589
590
        case HEVC_NAL_AUD:
            if(!p_ret && p_sys->pre.p_chain)
                p_ret = OutputQueues(p_sys, true);
            break;

591
592
593
594
595
        case HEVC_NAL_VPS:
        case HEVC_NAL_SPS:
        case HEVC_NAL_PPS:
        {
            uint8_t i_id;
596
597
            if(hevc_get_xps_id(p_nalb->p_buffer, p_nalb->i_buffer, &i_id))
                InsertXPS(p_dec, i_nal_type, i_id, p_nalb);
598
            break;
599
        }
600
601
602
603
604
605

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

606
        default:
607
            break;
608
    }
609

610
611
612
613
614
615
616
617
618
619
620
621
622
623
    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)
    {
624
625
        case HEVC_NAL_EOS:
        case HEVC_NAL_EOB:
626
            p_ret = OutputQueues(p_sys, true);
627
            break;
628
629
630
631
632

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

635
636
637
    if(!p_ret && p_sys->frame.p_chain == NULL)
        p_ret = OutputQueues(p_sys, false);

638
    return p_ret;
Denis Charmet's avatar
Denis Charmet committed
639
640
}

641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
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)
    {
        if(p_outputchain->i_flags & BLOCK_FLAG_CORRUPTED)
            p_output = p_outputchain; /* Avoid useless gather */
        else
            p_output = block_ChainGather(p_outputchain);
    }

    if(p_output && (p_output->i_flags & BLOCK_FLAG_CORRUPTED))
    {
        block_ChainRelease(p_output); /* Chain! see above */
        p_output = NULL;
    }

    return p_output;
}

681
682
683
684
685
/*****************************************************************************
 * 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
686
687
688
{
    decoder_sys_t *p_sys = p_dec->p_sys;

689
690
691
692
    if(unlikely(p_frag->i_buffer < 5))
    {
        msg_Warn(p_dec,"NAL too small");
        block_Release(p_frag);
693
        *pb_ts_used = false;
694
695
        return NULL;
    }
Denis Charmet's avatar
Denis Charmet committed
696

697
    if(p_frag->p_buffer[4] & 0x80)
Denis Charmet's avatar
Denis Charmet committed
698
    {
699
        msg_Warn(p_dec,"Forbidden zero bit not null, corrupted NAL");
700
        block_Release(p_frag);
701
702
        *pb_ts_used = false;
        return GatherAndValidateChain(OutputQueues(p_sys, false)); /* will drop */
Denis Charmet's avatar
Denis Charmet committed
703
704
    }

705
    /* Get NALU type */
706
    block_t * p_output = NULL;
707
    uint8_t i_nal_type = hevc_getNALType(&p_frag->p_buffer[4]);
708
    if (i_nal_type < HEVC_NAL_VPS)
Denis Charmet's avatar
Denis Charmet committed
709
710
    {
        /* NAL is a VCL NAL */
711
712
713
        p_output = ParseVCL(p_dec, i_nal_type, p_frag);
        if (p_output && (p_output->i_flags & BLOCK_FLAG_CORRUPTED))
            msg_Info(p_dec, "Waiting for VPS/SPS/PPS");
Denis Charmet's avatar
Denis Charmet committed
714
715
716
    }
    else
    {
717
        p_output = ParseNonVCL(p_dec, i_nal_type, p_frag);
Denis Charmet's avatar
Denis Charmet committed
718
719
    }

720
721
722
    p_output = GatherAndValidateChain(p_output);
    *pb_ts_used = (p_output != NULL);
    return p_output;
Denis Charmet's avatar
Denis Charmet committed
723
724
}

725
726
727
static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *p_block)
{
    decoder_t *p_dec = p_private;
728
    decoder_sys_t *p_sys = p_dec->p_sys;
729
730
731
732
733

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

734
735
736
737
738
    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;
739
740
}

Denis Charmet's avatar
Denis Charmet committed
741
742
743
744
745
746
static int PacketizeValidate( void *p_private, block_t *p_au )
{
    VLC_UNUSED(p_private);
    VLC_UNUSED(p_au);
    return VLC_SUCCESS;
}
747
748
749
750
751
752

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;

753
    switch( p_sei_data->i_type )
754
    {
755
        case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35:
756
        {
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
            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;
        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;
            p_fmt->ligthing.MaxCLL = p_sei_data->content_light_lvl.MaxCLL;
            p_fmt->ligthing.MaxFALL = p_sei_data->content_light_lvl.MaxFALL;
        } break;
779
    }
780
781
782

    return true;
}