Coverage Report

Created: 2026-09-03 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/packetizer/hevc_nal.c
Line
Count
Source
1
/*****************************************************************************
2
 * Copyright © 2010-2014 VideoLAN
3
 *
4
 * Authors: Thomas Guillem <thomas.guillem@gmail.com>
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 2.1 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19
 *****************************************************************************/
20
#ifdef HAVE_CONFIG_H
21
# include "config.h"
22
#endif
23
24
#include <stdbit.h>
25
26
#include "hevc_nal.h"
27
#include "h26x_nal_common.h"
28
#include "hxxx_nal.h"
29
#include "hxxx_ep3b.h"
30
31
#include <vlc_common.h>
32
#include <vlc_bits.h>
33
34
#include <limits.h>
35
36
//#define HEVC_POC_DEBUG
37
38
typedef struct
39
{
40
    nal_u2_t profile_space;
41
    nal_u1_t tier_flag;
42
    nal_u5_t profile_idc;
43
    uint32_t profile_compatibility_flag; /* nal_u1_t * 32 */
44
    nal_u1_t progressive_source_flag;
45
    nal_u1_t interlaced_source_flag;
46
    nal_u1_t non_packed_constraint_flag;
47
    nal_u1_t frame_only_constraint_flag;
48
    struct
49
    {
50
        nal_u1_t max_12bit_constraint_flag;
51
        nal_u1_t max_10bit_constraint_flag;
52
        nal_u1_t max_8bit_constraint_flag;
53
        nal_u1_t max_422chroma_constraint_flag;
54
        nal_u1_t max_420chroma_constraint_flag;
55
        nal_u1_t max_monochrome_constraint_flag;
56
        nal_u1_t intra_constraint_flag;
57
        nal_u1_t one_picture_only_constraint_flag;
58
        nal_u1_t lower_bit_rate_constraint_flag;
59
        nal_u1_t max_14bit_constraint_flag;
60
    } idc4to7;
61
    struct
62
    {
63
        nal_u1_t inbld_flag;
64
    } idc1to5;
65
} hevc_inner_profile_tier_level_t;
66
67
#define HEVC_MAX_SUBLAYERS 8
68
typedef struct
69
{
70
    hevc_inner_profile_tier_level_t general;
71
    nal_u8_t general_level_idc;
72
    uint8_t  sublayer_profile_present_flag;  /* nal_u1_t * 8 */
73
    uint8_t  sublayer_level_present_flag;    /* nal_u1_t * 8 */
74
    hevc_inner_profile_tier_level_t sub_layer[HEVC_MAX_SUBLAYERS];
75
    nal_u8_t sub_layer_level_idc[HEVC_MAX_SUBLAYERS];
76
} hevc_profile_tier_level_t;
77
78
293k
#define HEVC_MAX_SHORT_TERM_REF_PIC_SET 65
79
87.3k
#define HEVC_MAX_LONG_TERM_REF_PIC_SET 33
80
81
typedef struct
82
{
83
    unsigned num_delta_pocs;
84
} hevc_short_term_ref_pic_set_t;
85
86
typedef struct
87
{
88
    nal_u1_t aspect_ratio_info_present_flag;
89
    h26x_aspect_ratio_t ar;
90
    nal_u1_t overscan_info_present_flag;
91
    nal_u1_t overscan_appropriate_flag;
92
93
    nal_u1_t video_signal_type_present_flag;
94
    struct
95
    {
96
        nal_u3_t video_format;
97
        nal_u1_t colour_description_present_flag;
98
        h26x_colour_description_t colour;
99
    } vs;
100
101
    nal_u1_t chroma_loc_info_present_flag;
102
    struct
103
    {
104
        nal_ue_t sample_loc_type_top_field;
105
        nal_ue_t sample_loc_type_bottom_field;
106
    } chroma;
107
108
    nal_u1_t neutral_chroma_indication_flag;
109
    nal_u1_t field_seq_flag;
110
    nal_u1_t frame_field_info_present_flag;
111
112
    nal_u1_t default_display_window_flag;
113
    struct
114
    {
115
        nal_ue_t win_left_offset;
116
        nal_ue_t win_right_offset;
117
        nal_ue_t win_top_offset;
118
        nal_ue_t win_bottom_offset;
119
    } def_disp;
120
121
    nal_u1_t vui_timing_info_present_flag;
122
    struct
123
    {
124
        uint32_t vui_num_units_in_tick;
125
        uint32_t vui_time_scale;
126
        /* incomplete */
127
    } timing;
128
129
    /* incomplete */
130
} hevc_vui_parameters_t;
131
132
struct hevc_video_parameter_set_t
133
{
134
    nal_u4_t vps_video_parameter_set_id;
135
    nal_u1_t vps_base_layer_internal_flag;
136
    nal_u1_t vps_base_layer_available_flag;
137
    nal_u6_t vps_max_layers_minus1;
138
    nal_u3_t vps_max_sub_layers_minus1;
139
    nal_u1_t vps_temporal_id_nesting_flag;
140
141
    hevc_profile_tier_level_t profile_tier_level;
142
143
    nal_u1_t vps_sub_layer_ordering_info_present_flag;
144
    struct
145
    {
146
        nal_ue_t dec_pic_buffering_minus1;
147
        nal_ue_t num_reorder_pics;
148
        nal_ue_t max_latency_increase_plus1;
149
    } vps_max[1 + HEVC_MAX_SUBLAYERS];
150
151
    nal_u6_t vps_max_layer_id;
152
    nal_ue_t vps_num_layer_set_minus1;
153
    // layer_id_included_flag; read but discarded
154
155
    nal_u1_t vps_timing_info_present_flag;
156
    uint32_t vps_num_units_in_tick;
157
    uint32_t vps_time_scale;
158
159
    /* incomplete */
160
};
161
162
struct hevc_sequence_parameter_set_t
163
{
164
    nal_u4_t sps_video_parameter_set_id;
165
    nal_u3_t sps_max_sub_layers_minus1;
166
    nal_u1_t sps_temporal_id_nesting_flag;
167
168
    hevc_profile_tier_level_t profile_tier_level;
169
170
    nal_ue_t sps_seq_parameter_set_id;
171
    nal_ue_t chroma_format_idc;
172
    nal_u1_t separate_colour_plane_flag;
173
174
    nal_ue_t pic_width_in_luma_samples;
175
    nal_ue_t pic_height_in_luma_samples;
176
177
    nal_u1_t conformance_window_flag;
178
    h26x_conf_window_t conf_win;
179
180
    nal_ue_t bit_depth_luma_minus8;
181
    nal_ue_t bit_depth_chroma_minus8;
182
    nal_ue_t log2_max_pic_order_cnt_lsb_minus4;
183
184
    nal_u1_t sps_sub_layer_ordering_info_present_flag;
185
    struct
186
    {
187
    nal_ue_t dec_pic_buffering_minus1;
188
    nal_ue_t num_reorder_pics;
189
    nal_ue_t latency_increase_plus1;
190
    } sps_max[1 + HEVC_MAX_SUBLAYERS];
191
192
    nal_ue_t log2_min_luma_coding_block_size_minus3;
193
    nal_ue_t log2_diff_max_min_luma_coding_block_size;
194
    nal_ue_t log2_min_luma_transform_block_size_minus2;
195
    nal_ue_t log2_diff_max_min_luma_transform_block_size;
196
197
    /* incomplete */
198
    nal_ue_t max_transform_hierarchy_depth_inter;
199
    nal_ue_t max_transform_hierarchy_depth_intra;
200
    nal_u1_t scaling_list_enabled;
201
    nal_u1_t sps_scaling_list_data_present_flag;
202
    // scaling_list_data; read but discarded
203
204
    nal_u1_t amp_enabled_flag;
205
    nal_u1_t sample_adaptive_offset_enabled_flag;
206
207
    nal_u1_t pcm_enabled_flag;
208
    nal_u4_t pcm_sample_bit_depth_luma_minus1;
209
    nal_u4_t pcm_sample_bit_depth_chroma_minus1;
210
    nal_ue_t log2_min_pcm_luma_coding_block_size_minus3;
211
    nal_ue_t log2_diff_max_min_pcm_luma_coding_block_size;
212
    nal_u1_t pcm_loop_filter_disabled_flag;
213
214
    nal_ue_t num_short_term_ref_pic_sets;
215
    // st_ref_pic_set
216
217
    nal_u1_t long_term_ref_pics_present_flag;
218
    nal_ue_t num_long_term_ref_pics_sps;
219
    //
220
221
    nal_u1_t sps_temporal_mvp_enabled_flag;
222
    nal_u1_t strong_intra_smoothing_enabled_flag;
223
224
    nal_u1_t vui_parameters_present_flag;
225
    hevc_vui_parameters_t vui;
226
    /* incomplete */
227
};
228
229
struct hevc_picture_parameter_set_t
230
{
231
    nal_ue_t pps_pic_parameter_set_id;
232
    nal_ue_t pps_seq_parameter_set_id;
233
    nal_u1_t dependent_slice_segments_enabled_flag;
234
    nal_u1_t output_flag_present_flag;
235
    nal_u3_t num_extra_slice_header_bits;
236
    nal_u1_t sign_data_hiding_enabled_flag;
237
    nal_u1_t cabac_init_present_flag;
238
    nal_ue_t num_ref_idx_l0_default_active_minus1;
239
    nal_ue_t num_ref_idx_l1_default_active_minus1;
240
    nal_se_t init_qp_minus26;
241
    nal_u1_t constrained_intra_pred_flag;
242
    nal_u1_t transform_skip_enabled_flag;
243
244
    nal_u1_t cu_qp_delta_enabled_flag;
245
    nal_ue_t diff_cu_qp_delta_depth;
246
247
    nal_se_t pps_cb_qp_offset;
248
    nal_se_t pps_cr_qp_offset;
249
    nal_u1_t pic_slice_level_chroma_qp_offsets_present_flag;
250
    nal_u1_t weighted_pred_flag;
251
    nal_u1_t weighted_bipred_flag;
252
    nal_u1_t transquant_bypass_enable_flag;
253
254
    nal_u1_t tiles_enabled_flag;
255
    nal_u1_t entropy_coding_sync_enabled_flag;
256
    nal_ue_t num_tile_columns_minus1;
257
    nal_ue_t num_tile_rows_minus1;
258
    nal_u1_t uniform_spacing_flag;
259
    // nal_ue_t *p_column_width_minus1; read but discarded
260
    // nal_ue_t *p_row_height_minus1; read but discarded
261
    nal_u1_t loop_filter_across_tiles_enabled_flag;
262
263
    nal_u1_t pps_loop_filter_across_slices_enabled_flag;
264
265
    nal_u1_t deblocking_filter_control_present_flag;
266
    nal_u1_t deblocking_filter_override_enabled_flag;
267
    nal_u1_t pps_deblocking_filter_disabled_flag;
268
    nal_se_t pps_beta_offset_div2;
269
    nal_se_t pps_tc_offset_div2;
270
271
    nal_u1_t scaling_list_data_present_flag;
272
    // scaling_list_data; read but discarded
273
274
    nal_u1_t lists_modification_present_flag;
275
    nal_ue_t log2_parallel_merge_level_minus2;
276
    nal_u1_t slice_header_extension_present_flag;
277
278
    nal_u1_t pps_extension_present_flag;
279
    nal_u1_t pps_range_extension_flag;
280
    nal_u1_t pps_multilayer_extension_flag;
281
    nal_u1_t pps_3d_extension_flag;
282
    nal_u5_t pps_extension_5bits;
283
    /* incomplete */
284
285
};
286
287
struct hevc_slice_segment_header_t
288
{
289
    nal_u6_t nal_type;
290
    nal_u6_t nuh_layer_id;
291
    nal_u3_t temporal_id_plus1;
292
    nal_u1_t first_slice_segment_in_pic_flag;
293
    nal_u1_t no_output_of_prior_pics_flag;
294
    nal_ue_t slice_pic_parameter_set_id;
295
    nal_u1_t dependent_slice_segment_flag;
296
    // slice_segment_address; read but discarded
297
    nal_ue_t slice_type;
298
    nal_u1_t pic_output_flag;
299
300
    uint32_t pic_order_cnt_lsb;
301
    /* incomplete */
302
303
};
304
305
/* Computes size and does check the whole struct integrity */
306
static bool get_hvcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf, size_t *pi_res )
307
7
{
308
7
    size_t i_total = 0;
309
310
7
    if( i_buf < HEVC_MIN_HVCC_SIZE )
311
0
        return false;
312
313
7
    const uint8_t i_nal_length_size = (p_buf[21] & 0x03) + 1;
314
7
    if(i_nal_length_size == 3)
315
0
        return false;
316
317
7
    const uint8_t i_num_array = p_buf[22];
318
7
    p_buf += 23; i_buf -= 23;
319
320
16
    for( uint8_t i = 0; i < i_num_array; i++ )
321
10
    {
322
10
        if(i_buf < 3)
323
0
            return false;
324
325
10
        const uint16_t i_num_nalu = p_buf[1] << 8 | p_buf[2];
326
10
        p_buf += 3; i_buf -= 3;
327
328
20
        for( uint16_t j = 0; j < i_num_nalu; j++ )
329
11
        {
330
11
            if(i_buf < 2)
331
0
                return false;
332
333
11
            const uint16_t i_nalu_length = p_buf[0] << 8 | p_buf[1];
334
11
            if(i_buf < (size_t)i_nalu_length + 2)
335
1
                return false;
336
337
10
            i_total += i_nalu_length + 4; // annexb_startcode4;
338
10
            p_buf += i_nalu_length + 2;
339
10
            i_buf -= i_nalu_length + 2;
340
10
        }
341
10
    }
342
343
6
    *pi_res = i_total;
344
6
    return true;
345
7
}
346
347
bool hevc_hvcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
348
                              uint8_t **pp_result, size_t *pi_result,
349
                              uint8_t *pi_nal_length_size )
350
7
{
351
7
    size_t i_result;
352
7
    if(!get_hvcC_to_AnnexB_NAL_size( p_buf, i_buf, &i_result )) /* Does all checks */
353
1
        return false;
354
355
6
    const uint8_t i_nal_length_size = hevc_getNALLengthSize( p_buf );
356
6
    uint8_t *p_out_buf = NULL;
357
6
    uint8_t *p_ret = NULL;
358
6
    if( i_result > 0 )
359
2
    {
360
2
        p_ret = p_out_buf = malloc( i_result );
361
2
        if( !p_out_buf )
362
0
            return false;
363
364
2
        const uint8_t i_num_array = p_buf[22];
365
2
        p_buf += 23;
366
367
10
        for( uint8_t i = 0; i < i_num_array; i++ )
368
8
        {
369
8
            const uint16_t i_num_nalu = p_buf[1] << 8 | p_buf[2];
370
8
            p_buf += 3;
371
372
16
            for( uint16_t j = 0; j < i_num_nalu; j++ )
373
8
            {
374
8
                const uint16_t i_nalu_length = p_buf[0] << 8 | p_buf[1];
375
376
8
                memcpy( p_out_buf, annexb_startcode4, 4 );
377
8
                memcpy( &p_out_buf[4], &p_buf[2], i_nalu_length );
378
379
8
                p_out_buf += 4 + i_nalu_length;
380
8
                p_buf += 2 + i_nalu_length;
381
8
            }
382
8
        }
383
2
    }
384
385
6
    *pp_result = p_ret;
386
6
    *pi_result = i_result;
387
6
    if( pi_nal_length_size )
388
6
        *pi_nal_length_size = i_nal_length_size;
389
390
6
    return true;
391
6
}
392
393
static bool hevc_parse_scaling_list_rbsp( bs_t *p_bs )
394
35.2k
{
395
176k
    for( int i=0; i<4; i++ )
396
141k
    {
397
846k
        for( int j=0; j<6; j += (i == 3) ? 3 : 1 )
398
705k
        {
399
705k
            if( bs_read1( p_bs ) == 0 )
400
614k
                bs_read_ue( p_bs );
401
90.5k
            else
402
90.5k
            {
403
90.5k
                unsigned nextCoef = 8;
404
90.5k
                unsigned coefNum = __MIN( 64, (1 << (4 + (i << 1))));
405
90.5k
                if( i > 1 )
406
10.9k
                {
407
10.9k
                    nextCoef = bs_read_se( p_bs ) + 8;
408
10.9k
                }
409
2.91M
                for( unsigned k=0; k<coefNum; k++ )
410
2.82M
                {
411
2.82M
                    nextCoef = ( nextCoef + bs_read_se( p_bs ) + 256 ) % 256;
412
2.82M
                }
413
90.5k
            }
414
705k
        }
415
141k
    }
416
417
35.2k
    return true;
418
35.2k
}
419
420
static bool hevc_parse_vui_parameters_rbsp( bs_t *p_bs, hevc_vui_parameters_t *p_vui,
421
                                            bool b_broken )
422
61.1k
{
423
61.1k
    p_vui->aspect_ratio_info_present_flag = bs_read1( p_bs );
424
61.1k
    if( p_vui->aspect_ratio_info_present_flag )
425
35.4k
    {
426
35.4k
        p_vui->ar.aspect_ratio_idc = bs_read( p_bs, 8 );
427
35.4k
        if( p_vui->ar.aspect_ratio_idc == 0xFF ) //HEVC_SAR__IDC_EXTENDED_SAR )
428
18.1k
        {
429
18.1k
            p_vui->ar.sar_width = bs_read( p_bs, 16 );
430
18.1k
            p_vui->ar.sar_height = bs_read( p_bs, 16 );
431
18.1k
        }
432
35.4k
    }
433
434
61.1k
    p_vui->overscan_info_present_flag = bs_read1( p_bs );
435
61.1k
    if( p_vui->overscan_info_present_flag )
436
24.0k
        p_vui->overscan_appropriate_flag = bs_read1( p_bs );
437
438
61.1k
    p_vui->video_signal_type_present_flag = bs_read1( p_bs );
439
61.1k
    if( p_vui->video_signal_type_present_flag )
440
29.6k
    {
441
29.6k
        p_vui->vs.video_format = bs_read( p_bs, 3 );
442
29.6k
        p_vui->vs.colour.full_range_flag = bs_read1( p_bs );
443
29.6k
        p_vui->vs.colour_description_present_flag = bs_read1( p_bs );
444
29.6k
        if( p_vui->vs.colour_description_present_flag )
445
21.3k
        {
446
21.3k
            p_vui->vs.colour.colour_primaries = bs_read( p_bs, 8 );
447
21.3k
            p_vui->vs.colour.transfer_characteristics = bs_read( p_bs, 8 );
448
21.3k
            p_vui->vs.colour.matrix_coeffs = bs_read( p_bs, 8 );
449
21.3k
        }
450
8.23k
        else
451
8.23k
        {
452
8.23k
            p_vui->vs.colour.colour_primaries = ISO_23001_8_CP_UNSPECIFIED;
453
8.23k
            p_vui->vs.colour.transfer_characteristics = ISO_23001_8_TC_UNSPECIFIED;
454
8.23k
            p_vui->vs.colour.matrix_coeffs = ISO_23001_8_MC_UNSPECIFIED;
455
8.23k
        }
456
29.6k
    }
457
458
61.1k
    p_vui->chroma_loc_info_present_flag = bs_read1( p_bs );
459
61.1k
    if( p_vui->chroma_loc_info_present_flag )
460
21.1k
    {
461
21.1k
        p_vui->chroma.sample_loc_type_top_field = bs_read_ue( p_bs );
462
21.1k
        p_vui->chroma.sample_loc_type_bottom_field = bs_read_ue( p_bs );
463
21.1k
        if( p_vui->chroma.sample_loc_type_top_field > 5 ||
464
20.0k
            p_vui->chroma.sample_loc_type_bottom_field > 5 )
465
4.09k
            return false;
466
21.1k
    }
467
468
57.0k
    p_vui->neutral_chroma_indication_flag = bs_read1( p_bs );
469
57.0k
    p_vui->field_seq_flag = bs_read1( p_bs );
470
57.0k
    p_vui->frame_field_info_present_flag = bs_read1( p_bs );
471
472
57.0k
    p_vui->default_display_window_flag = !b_broken && bs_read1( p_bs );
473
57.0k
    if( p_vui->default_display_window_flag )
474
22.9k
    {
475
22.9k
        p_vui->def_disp.win_left_offset = bs_read_ue( p_bs );
476
22.9k
        p_vui->def_disp.win_right_offset = bs_read_ue( p_bs );
477
22.9k
        p_vui->def_disp.win_top_offset = bs_read_ue( p_bs );
478
22.9k
        p_vui->def_disp.win_bottom_offset = bs_read_ue( p_bs );
479
22.9k
    }
480
481
57.0k
    p_vui->vui_timing_info_present_flag = bs_read1( p_bs );
482
57.0k
    if( p_vui->vui_timing_info_present_flag )
483
14.7k
    {
484
14.7k
        p_vui->timing.vui_num_units_in_tick =  bs_read( p_bs, 32 );
485
14.7k
        p_vui->timing.vui_time_scale =  bs_read( p_bs, 32 );
486
14.7k
    }
487
    /* incomplete */
488
489
57.0k
    return !bs_error( p_bs );
490
61.1k
}
491
492
/* Shortcut for retrieving vps/sps/pps id */
493
bool hevc_get_xps_id(const uint8_t *p_buf, size_t i_buf, uint8_t *pi_id)
494
735k
{
495
735k
    if(i_buf < 3)
496
36.3k
        return false;
497
    /* No need to lookup convert from emulation for that data */
498
699k
    uint8_t i_nal_type = hevc_getNALType(p_buf);
499
699k
    bs_t bs;
500
699k
    bs_init(&bs, &p_buf[2], i_buf - 2);
501
699k
    if(i_nal_type == HEVC_NAL_PPS)
502
232k
    {
503
232k
        *pi_id = bs_read_ue( &bs );
504
232k
        if(*pi_id > HEVC_PPS_ID_MAX)
505
7.12k
            return false;
506
232k
    }
507
466k
    else
508
466k
    {
509
466k
        *pi_id = bs_read( &bs, 4 );
510
466k
        if(i_nal_type == HEVC_NAL_SPS)
511
335k
        {
512
335k
            if(*pi_id > HEVC_SPS_ID_MAX)
513
0
                return false;
514
335k
        }
515
131k
        else if(*pi_id > HEVC_VPS_ID_MAX)
516
0
            return false;
517
466k
    }
518
691k
    return true;
519
699k
}
520
521
static bool hevc_parse_inner_profile_tier_level_rbsp( bs_t *p_bs,
522
                                                      hevc_inner_profile_tier_level_t *p_in )
523
632k
{
524
632k
    p_in->profile_space = bs_read( p_bs, 2 );
525
632k
    p_in->tier_flag = bs_read1( p_bs );
526
632k
    p_in->profile_idc = bs_read( p_bs, 5 );
527
632k
    p_in->profile_compatibility_flag = bs_read( p_bs, 32 );
528
632k
    p_in->progressive_source_flag = bs_read1( p_bs );
529
632k
    p_in->interlaced_source_flag = bs_read1( p_bs );
530
632k
    p_in->non_packed_constraint_flag = bs_read1( p_bs );
531
632k
    p_in->frame_only_constraint_flag = bs_read1( p_bs );
532
533
632k
    if( ( p_in->profile_idc >= 4 && p_in->profile_idc <= 10 ) ||
534
546k
        ( p_in->profile_compatibility_flag & 0x0F700000 ) )
535
512k
    {
536
512k
        p_in->idc4to7.max_12bit_constraint_flag = bs_read1( p_bs );
537
512k
        p_in->idc4to7.max_10bit_constraint_flag = bs_read1( p_bs );
538
512k
        p_in->idc4to7.max_8bit_constraint_flag = bs_read1( p_bs );
539
512k
        p_in->idc4to7.max_422chroma_constraint_flag = bs_read1( p_bs );
540
512k
        p_in->idc4to7.max_420chroma_constraint_flag = bs_read1( p_bs );
541
512k
        p_in->idc4to7.max_monochrome_constraint_flag = bs_read1( p_bs );
542
512k
        p_in->idc4to7.intra_constraint_flag = bs_read1( p_bs );
543
512k
        p_in->idc4to7.one_picture_only_constraint_flag = bs_read1( p_bs );
544
512k
        p_in->idc4to7.lower_bit_rate_constraint_flag = bs_read1( p_bs );
545
512k
        if( p_in->profile_idc == 5 ||
546
479k
            p_in->profile_idc == 9 ||
547
463k
            p_in->profile_idc == 10 ||
548
458k
           (p_in->profile_compatibility_flag & 0x08600000) )
549
454k
        {
550
454k
            p_in->idc4to7.max_14bit_constraint_flag = bs_read1( p_bs );
551
454k
            bs_skip( p_bs, 33 );
552
454k
        }
553
58.6k
        else bs_skip( p_bs, 34 );
554
512k
    }
555
119k
    else if( p_in->profile_idc == 2 ||
556
117k
            (p_in->profile_compatibility_flag & 0x20000000) )
557
19.6k
    {
558
19.6k
        bs_skip( p_bs, 7 );
559
19.6k
        p_in->idc4to7.one_picture_only_constraint_flag = bs_read1( p_bs );
560
19.6k
        bs_skip( p_bs, 35 );
561
19.6k
    }
562
99.9k
    else
563
99.9k
    {
564
99.9k
        bs_skip( p_bs, 43 );
565
99.9k
    }
566
567
632k
    if( ( p_in->profile_idc >= 1 && p_in->profile_idc <= 5 ) ||
568
499k
         p_in->profile_idc == 9 ||
569
483k
        ( p_in->profile_compatibility_flag & 0x7C400000 ) )
570
522k
        p_in->idc1to5.inbld_flag = bs_read1( p_bs );
571
109k
    else
572
109k
        bs_skip( p_bs, 1 );
573
574
632k
    return !bs_error( p_bs );
575
632k
}
576
577
static bool hevc_parse_profile_tier_level_rbsp( bs_t *p_bs, bool profile_present,
578
                                                uint8_t max_num_sub_layers_minus1,
579
                                                hevc_profile_tier_level_t *p_ptl )
580
451k
{
581
451k
    if( profile_present && !hevc_parse_inner_profile_tier_level_rbsp( p_bs, &p_ptl->general ) )
582
57.4k
        return false;
583
584
394k
    if( bs_error( p_bs ) )
585
0
        return false;
586
587
394k
    p_ptl->general_level_idc = bs_read( p_bs, 8 );
588
589
394k
    if( max_num_sub_layers_minus1 > 0 )
590
240k
    {
591
240k
        if( bs_eof( p_bs ) )
592
9.31k
            return false;
593
594
2.07M
        for( uint8_t i=0; i< 8; i++ )
595
1.84M
        {
596
1.84M
            if( i < max_num_sub_layers_minus1 )
597
875k
            {
598
875k
                if( bs_read1( p_bs ) )
599
241k
                    p_ptl->sublayer_profile_present_flag |= (0x80 >> i);
600
875k
                if( bs_read1( p_bs ) )
601
265k
                    p_ptl->sublayer_level_present_flag |= (0x80 >> i);
602
875k
            }
603
972k
            else
604
972k
                bs_skip( p_bs, 2 );
605
1.84M
        }
606
607
907k
        for( uint8_t i=0; i < max_num_sub_layers_minus1; i++ )
608
733k
        {
609
733k
            if( ( p_ptl->sublayer_profile_present_flag & (0x80 >> i) ) &&
610
180k
                ! hevc_parse_inner_profile_tier_level_rbsp( p_bs, &p_ptl->sub_layer[i] ) )
611
43.0k
                return false;
612
613
690k
            if( p_ptl->sublayer_profile_present_flag & (0x80 >> i) )
614
137k
            {
615
137k
                if( bs_eof( p_bs ) )
616
14.1k
                    return false;
617
123k
                p_ptl->sub_layer_level_idc[i] = bs_read( p_bs, 8 );
618
123k
            }
619
690k
        }
620
230k
    }
621
622
327k
    return !bs_error( p_bs );
623
394k
}
624
625
static bool hevc_parse_video_parameter_set_rbsp( bs_t *p_bs,
626
                                                 hevc_video_parameter_set_t *p_vps )
627
124k
{
628
124k
    if( bs_eof( p_bs ) )
629
0
        return false;
630
631
124k
    p_vps->vps_video_parameter_set_id = bs_read( p_bs, 4 );
632
124k
    p_vps->vps_base_layer_internal_flag = bs_read1( p_bs );
633
124k
    p_vps->vps_base_layer_available_flag = bs_read1( p_bs );
634
124k
    p_vps->vps_max_layers_minus1 = bs_read( p_bs, 6 );
635
124k
    p_vps->vps_max_sub_layers_minus1 = bs_read( p_bs, 3 );
636
124k
    p_vps->vps_temporal_id_nesting_flag = bs_read1( p_bs );
637
124k
    bs_skip( p_bs, 16 );
638
639
124k
    if( !hevc_parse_profile_tier_level_rbsp( p_bs, true, p_vps->vps_max_sub_layers_minus1,
640
124k
                                            &p_vps->profile_tier_level ) )
641
53.0k
        return false;
642
643
71.4k
    p_vps->vps_sub_layer_ordering_info_present_flag = bs_read1( p_bs );
644
71.4k
    for( unsigned i= (p_vps->vps_sub_layer_ordering_info_present_flag ?
645
42.9k
                      0 : p_vps->vps_max_sub_layers_minus1);
646
166k
         i<= p_vps->vps_max_sub_layers_minus1; i++ )
647
95.0k
    {
648
95.0k
        p_vps->vps_max[i].dec_pic_buffering_minus1 = bs_read_ue( p_bs );
649
95.0k
        p_vps->vps_max[i].num_reorder_pics = bs_read_ue( p_bs );
650
95.0k
        p_vps->vps_max[i].max_latency_increase_plus1 = bs_read_ue( p_bs );
651
95.0k
    }
652
653
71.4k
    if( bs_error( p_bs ) )
654
7.24k
        return false;
655
656
64.1k
    p_vps->vps_max_layer_id = bs_read( p_bs, 6 );
657
64.1k
    p_vps->vps_num_layer_set_minus1 = bs_read_ue( p_bs );
658
64.1k
    if( p_vps->vps_num_layer_set_minus1 > 1023 )
659
5.56k
        return false;
660
    // layer_id_included_flag; read but discarded
661
58.5k
    bs_skip( p_bs, p_vps->vps_num_layer_set_minus1 * (p_vps->vps_max_layer_id + 1) );
662
663
58.5k
    p_vps->vps_timing_info_present_flag = bs_read1( p_bs );
664
58.5k
    if( p_vps->vps_timing_info_present_flag )
665
22.0k
    {
666
22.0k
        p_vps->vps_num_units_in_tick = bs_read( p_bs, 32 );
667
22.0k
        p_vps->vps_time_scale = bs_read( p_bs, 32 );
668
22.0k
    }
669
    /* parsing incomplete */
670
671
58.5k
    return !bs_error( p_bs );
672
64.1k
}
673
674
void hevc_rbsp_release_vps( hevc_video_parameter_set_t *p_vps )
675
125k
{
676
125k
    free( p_vps );
677
125k
}
678
679
#define IMPL_hevc_generic_decode( name, hevctype, decode, release ) \
680
    hevctype * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
681
635k
    { \
682
635k
        hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \
683
635k
        if(likely(p_hevctype)) \
684
635k
        { \
685
635k
            bs_t bs; \
686
635k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
687
635k
            if( b_escaped ) \
688
635k
            { \
689
635k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
690
635k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
691
635k
            } \
692
635k
            else bs_init( &bs, p_buf, i_buf ); \
693
635k
            bs_skip( &bs, 7 ); /* nal_unit_header */ \
694
635k
            uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \
695
635k
            bs_skip( &bs, 3 ); /* !nal_unit_header */ \
696
635k
            if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \
697
635k
            { \
698
470k
                release( p_hevctype ); \
699
470k
                p_hevctype = NULL; \
700
470k
            } \
701
635k
        } \
702
635k
        return p_hevctype; \
703
635k
    }
704
705
125k
IMPL_hevc_generic_decode( hevc_decode_vps, hevc_video_parameter_set_t,
706
                          hevc_parse_video_parameter_set_rbsp, hevc_rbsp_release_vps )
707
708
static bool hevc_parse_st_ref_pic_set( bs_t *p_bs, unsigned stRpsIdx,
709
                                       unsigned num_short_term_ref_pic_sets,
710
                                       hevc_short_term_ref_pic_set_t *p_sets )
711
234k
{
712
234k
    if( stRpsIdx && bs_read1( p_bs ) ) /* Interref pic set prediction flag */
713
74.9k
    {
714
74.9k
        nal_ue_t delta_idx_minus_1 = 0;
715
74.9k
        if( stRpsIdx == num_short_term_ref_pic_sets )
716
0
        {
717
0
            delta_idx_minus_1 = bs_read_ue( p_bs );
718
0
            if( delta_idx_minus_1 >= stRpsIdx )
719
0
                return false;
720
0
        }
721
74.9k
        if(delta_idx_minus_1 == stRpsIdx)
722
0
            return false;
723
724
74.9k
        nal_u1_t delta_rps_sign = bs_read1( p_bs );
725
74.9k
        nal_ue_t abs_delta_rps_minus1 = bs_read_ue( p_bs );
726
74.9k
        unsigned RefRpsIdx = stRpsIdx - delta_idx_minus_1 - 1;
727
74.9k
        int deltaRps = ( 1 - ( delta_rps_sign << 1 ) ) * ( abs_delta_rps_minus1 + 1 );
728
74.9k
        VLC_UNUSED(deltaRps);
729
730
74.9k
        unsigned numDeltaPocs = p_sets[RefRpsIdx].num_delta_pocs;
731
74.9k
        p_sets[stRpsIdx].num_delta_pocs = 0;
732
495k
        for( unsigned j=0; j<= numDeltaPocs; j++ )
733
420k
        {
734
420k
            if( ! bs_read1( p_bs ) ) /* used_by_curr_pic_flag */
735
131k
            {
736
131k
                if( bs_read1( p_bs ) ) /* use_delta_flag */
737
45.4k
                    p_sets[stRpsIdx].num_delta_pocs++;
738
131k
            }
739
289k
            else
740
289k
                p_sets[stRpsIdx].num_delta_pocs++;
741
420k
        }
742
74.9k
    }
743
159k
    else
744
159k
    {
745
159k
        nal_ue_t num_negative_pics = bs_read_ue( p_bs );
746
159k
        nal_ue_t num_positive_pics = bs_read_ue( p_bs );
747
        // A.4.2 total to (MaxDpbSize -1) with MaxDpbSize capped either at 16 or 7
748
159k
        if( bs_error( p_bs ) ||
749
157k
            num_negative_pics >= 16 || num_positive_pics >= 16 ||
750
149k
            num_negative_pics + num_positive_pics >= 16 )
751
10.4k
            return false;
752
536k
        for(unsigned int i=0; i<num_negative_pics; i++)
753
387k
        {
754
387k
            (void) bs_read_ue( p_bs ); /* delta_poc_s0_minus1 */
755
387k
            (void) bs_read1( p_bs ); /* used_by_current_pic_s0_flag */
756
387k
        }
757
445k
        for(unsigned int i=0; i<num_positive_pics; i++)
758
296k
        {
759
296k
            (void) bs_read_ue( p_bs ); /* delta_poc_s1_minus1 */
760
296k
            (void) bs_read1( p_bs ); /* used_by_current_pic_s1_flag */
761
296k
        }
762
149k
        p_sets[stRpsIdx].num_delta_pocs = num_positive_pics + num_negative_pics;
763
149k
    }
764
765
224k
    return !bs_error( p_bs );
766
234k
}
767
768
static bool hevc_parse_sequence_parameter_set_rbsp( bs_t *p_bs,
769
                                                    hevc_sequence_parameter_set_t *p_sps )
770
327k
{
771
327k
    p_sps->sps_video_parameter_set_id = bs_read( p_bs, 4 );
772
327k
    p_sps->sps_max_sub_layers_minus1 = bs_read( p_bs, 3 );
773
327k
    p_sps->sps_temporal_id_nesting_flag = bs_read1( p_bs );
774
327k
    if( !hevc_parse_profile_tier_level_rbsp( p_bs, true, p_sps->sps_max_sub_layers_minus1,
775
327k
                                            &p_sps->profile_tier_level ) )
776
73.6k
        return false;
777
778
253k
    if( bs_error( p_bs ) )
779
0
        return false;
780
781
253k
    p_sps->sps_seq_parameter_set_id = bs_read_ue( p_bs );
782
253k
    if( p_sps->sps_seq_parameter_set_id > HEVC_SPS_ID_MAX )
783
19.8k
        return false;
784
785
234k
    p_sps->chroma_format_idc = bs_read_ue( p_bs );
786
234k
    if( p_sps->chroma_format_idc == 3 )
787
31.6k
        p_sps->separate_colour_plane_flag = bs_read1( p_bs );
788
202k
    else if ( p_sps->chroma_format_idc > 3 )
789
8.40k
        return false;
790
225k
    p_sps->pic_width_in_luma_samples = bs_read_ue( p_bs );
791
225k
    p_sps->pic_height_in_luma_samples = bs_read_ue( p_bs );
792
225k
    if( !p_sps->pic_width_in_luma_samples || !p_sps->pic_height_in_luma_samples )
793
37.3k
        return false;
794
795
188k
    p_sps->conformance_window_flag = bs_read1( p_bs );
796
188k
    if( p_sps->conformance_window_flag )
797
84.1k
    {
798
84.1k
        p_sps->conf_win.left_offset = bs_read_ue( p_bs );
799
84.1k
        p_sps->conf_win.right_offset = bs_read_ue( p_bs );
800
84.1k
        p_sps->conf_win.top_offset = bs_read_ue( p_bs );
801
84.1k
        p_sps->conf_win.bottom_offset = bs_read_ue( p_bs );
802
84.1k
    }
803
804
188k
    p_sps->bit_depth_luma_minus8 = bs_read_ue( p_bs );
805
188k
    p_sps->bit_depth_chroma_minus8 = bs_read_ue( p_bs );
806
188k
    p_sps->log2_max_pic_order_cnt_lsb_minus4 = bs_read_ue( p_bs );
807
808
188k
    p_sps->sps_sub_layer_ordering_info_present_flag = bs_read1( p_bs );
809
188k
    for( uint8_t i=(p_sps->sps_sub_layer_ordering_info_present_flag ? 0 : p_sps->sps_max_sub_layers_minus1);
810
539k
         i <= p_sps->sps_max_sub_layers_minus1; i++ )
811
351k
    {
812
351k
        p_sps->sps_max[i].dec_pic_buffering_minus1 = bs_read_ue( p_bs );
813
351k
        p_sps->sps_max[i].num_reorder_pics = bs_read_ue( p_bs );
814
351k
        p_sps->sps_max[i].latency_increase_plus1 = bs_read_ue( p_bs );
815
351k
    }
816
817
188k
    if( bs_eof( p_bs ) )
818
25.2k
        return false;
819
820
162k
    p_sps->log2_min_luma_coding_block_size_minus3 = bs_read_ue( p_bs );
821
162k
    p_sps->log2_diff_max_min_luma_coding_block_size = bs_read_ue( p_bs );
822
162k
    p_sps->log2_min_luma_transform_block_size_minus2 = bs_read_ue( p_bs );
823
162k
    p_sps->log2_diff_max_min_luma_transform_block_size = bs_read_ue( p_bs );
824
825
    /* parsing incomplete */
826
827
162k
    p_sps->max_transform_hierarchy_depth_inter = bs_read_ue( p_bs );
828
162k
    p_sps->max_transform_hierarchy_depth_intra = bs_read_ue( p_bs );
829
162k
    p_sps->scaling_list_enabled = bs_read1( p_bs );
830
162k
    if( p_sps->scaling_list_enabled )
831
34.0k
    {
832
34.0k
        p_sps->sps_scaling_list_data_present_flag = bs_read1( p_bs );
833
34.0k
        if( p_sps->sps_scaling_list_data_present_flag &&
834
9.64k
            ! hevc_parse_scaling_list_rbsp( p_bs ) )
835
0
        {
836
0
            return false;
837
0
        }
838
34.0k
    }
839
840
162k
    if( bs_error( p_bs ) )
841
15.1k
        return false;
842
843
147k
    p_sps->amp_enabled_flag = bs_read1( p_bs );
844
147k
    p_sps->sample_adaptive_offset_enabled_flag = bs_read1( p_bs );
845
846
147k
    p_sps->pcm_enabled_flag = bs_read1( p_bs );
847
147k
    if( p_sps->pcm_enabled_flag )
848
64.6k
    {
849
64.6k
        p_sps->pcm_sample_bit_depth_luma_minus1 = bs_read( p_bs, 4 );
850
64.6k
        p_sps->pcm_sample_bit_depth_chroma_minus1 = bs_read( p_bs, 4 );
851
64.6k
        p_sps->log2_min_pcm_luma_coding_block_size_minus3 = bs_read_ue( p_bs );
852
64.6k
        p_sps->log2_diff_max_min_pcm_luma_coding_block_size = bs_read_ue( p_bs );
853
64.6k
        p_sps->pcm_loop_filter_disabled_flag = bs_read1( p_bs );
854
64.6k
    }
855
856
147k
    p_sps->num_short_term_ref_pic_sets = bs_read_ue( p_bs );
857
147k
    if( p_sps->num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SET )
858
2.42k
        return false;
859
860
145k
    hevc_short_term_ref_pic_set_t sets[HEVC_MAX_SHORT_TERM_REF_PIC_SET];
861
145k
    memset(&sets, 0, sizeof(hevc_short_term_ref_pic_set_t) * HEVC_MAX_SHORT_TERM_REF_PIC_SET);
862
353k
    for( unsigned int i=0; i<p_sps->num_short_term_ref_pic_sets; i++ )
863
234k
    {
864
234k
        if( !hevc_parse_st_ref_pic_set( p_bs, i, p_sps->num_short_term_ref_pic_sets, sets ) )
865
25.9k
            return false;
866
234k
    }
867
868
119k
    p_sps->long_term_ref_pics_present_flag = bs_read1( p_bs );
869
119k
    if( p_sps->long_term_ref_pics_present_flag )
870
87.3k
    {
871
87.3k
        p_sps->num_long_term_ref_pics_sps = bs_read_ue( p_bs );
872
87.3k
        if( p_sps->num_long_term_ref_pics_sps > HEVC_MAX_LONG_TERM_REF_PIC_SET )
873
3.57k
            return false;
874
263k
        for( unsigned int i=0; i< p_sps->num_long_term_ref_pics_sps; i++ )
875
179k
        {
876
             /* lt_ref_pic_poc_lsb_sps */
877
179k
            bs_skip( p_bs, p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4 );
878
             /* used_by_curr_pic_lt_sps_flag */
879
179k
            bs_skip( p_bs, 1 );
880
179k
        }
881
83.7k
    }
882
883
115k
    p_sps->sps_temporal_mvp_enabled_flag = bs_read1( p_bs );
884
115k
    p_sps->strong_intra_smoothing_enabled_flag = bs_read1( p_bs );
885
886
115k
    if( bs_error( p_bs ) )
887
5.98k
        return false;
888
889
109k
    p_sps->vui_parameters_present_flag = bs_read1( p_bs );
890
109k
    if( p_sps->vui_parameters_present_flag )
891
61.1k
    {
892
61.1k
        bs_t rollbackpoint = *p_bs;
893
61.1k
        if( !hevc_parse_vui_parameters_rbsp( p_bs, &p_sps->vui, false ) &&
894
39.6k
            p_sps->vui.default_display_window_flag &&
895
11.3k
            !bs_error( p_bs ) )
896
0
        {
897
            /* Broken MKV SPS vui bitstreams with missing display_window bits.
898
             * Forced to accept it since some decided to accept it...
899
             * see https://trac.ffmpeg.org/ticket/6644
900
             * Might break decoders since cropping & clock rate have totally
901
             * funky values when decoded properly */
902
0
            *p_bs = rollbackpoint;
903
0
            memset( &p_sps->vui, 0, sizeof(p_sps->vui) );
904
0
            if( !hevc_parse_vui_parameters_rbsp( p_bs, &p_sps->vui, true ) )
905
0
                return false;
906
0
        }
907
61.1k
    }
908
909
    /* incomplete */
910
911
109k
    return !bs_error( p_bs );
912
109k
}
913
914
void hevc_rbsp_release_sps( hevc_sequence_parameter_set_t *p_sps )
915
327k
{
916
327k
    free( p_sps );
917
327k
}
918
919
327k
IMPL_hevc_generic_decode( hevc_decode_sps, hevc_sequence_parameter_set_t,
920
                          hevc_parse_sequence_parameter_set_rbsp, hevc_rbsp_release_sps )
921
922
static bool hevc_parse_pic_parameter_set_rbsp( bs_t *p_bs,
923
                                               hevc_picture_parameter_set_t *p_pps )
924
182k
{
925
182k
    if( bs_eof( p_bs ) )
926
0
        return false;
927
182k
    p_pps->pps_pic_parameter_set_id = bs_read_ue( p_bs );
928
182k
    if( p_pps->pps_pic_parameter_set_id > HEVC_PPS_ID_MAX )
929
1.01k
        return false;
930
181k
    p_pps->pps_seq_parameter_set_id = bs_read_ue( p_bs );
931
181k
    if( p_pps->pps_seq_parameter_set_id > HEVC_SPS_ID_MAX )
932
6.49k
        return false;
933
174k
    p_pps->dependent_slice_segments_enabled_flag = bs_read1( p_bs );
934
174k
    p_pps->output_flag_present_flag = bs_read1( p_bs );
935
174k
    p_pps->num_extra_slice_header_bits = bs_read( p_bs, 3 );
936
174k
    p_pps->sign_data_hiding_enabled_flag = bs_read1( p_bs );
937
174k
    p_pps->cabac_init_present_flag = bs_read1( p_bs );
938
939
174k
    p_pps->num_ref_idx_l0_default_active_minus1 = bs_read_ue( p_bs );
940
174k
    p_pps->num_ref_idx_l1_default_active_minus1 = bs_read_ue( p_bs );
941
174k
    if( p_pps->num_ref_idx_l0_default_active_minus1 > 14 ||
942
166k
        p_pps->num_ref_idx_l1_default_active_minus1 > 14 )
943
17.1k
        return false;
944
945
157k
    p_pps->init_qp_minus26 = bs_read_se( p_bs );
946
157k
    p_pps->constrained_intra_pred_flag = bs_read1( p_bs );
947
157k
    p_pps->transform_skip_enabled_flag = bs_read1( p_bs );
948
157k
    p_pps->cu_qp_delta_enabled_flag = bs_read1( p_bs );
949
157k
    if( p_pps->cu_qp_delta_enabled_flag )
950
51.1k
        p_pps->diff_cu_qp_delta_depth = bs_read_ue( p_bs );
951
952
157k
    if( bs_error( p_bs ) )
953
21.2k
        return false;
954
955
136k
    p_pps->pps_cb_qp_offset = bs_read_se( p_bs );
956
136k
    p_pps->pps_cr_qp_offset = bs_read_se( p_bs );
957
136k
    if( p_pps->pps_cb_qp_offset > 12 || p_pps->pps_cb_qp_offset < -12 ||
958
122k
        p_pps->pps_cr_qp_offset > 12 || p_pps->pps_cr_qp_offset < -12 )
959
19.2k
        return false;
960
117k
    p_pps->pic_slice_level_chroma_qp_offsets_present_flag = bs_read1( p_bs );
961
117k
    p_pps->weighted_pred_flag = bs_read1( p_bs );
962
117k
    p_pps->weighted_bipred_flag = bs_read1( p_bs );
963
117k
    p_pps->transquant_bypass_enable_flag = bs_read1( p_bs );
964
117k
    p_pps->tiles_enabled_flag = bs_read1( p_bs );
965
117k
    p_pps->entropy_coding_sync_enabled_flag = bs_read1( p_bs );
966
967
117k
    if( p_pps->tiles_enabled_flag )
968
47.5k
    {
969
47.5k
        p_pps->num_tile_columns_minus1 = bs_read_ue( p_bs ); /* should validate max col/row values */
970
47.5k
        p_pps->num_tile_rows_minus1 = bs_read_ue( p_bs );    /* against PicWidthInCtbsY but we don't want to depend on SPS */
971
47.5k
        p_pps->uniform_spacing_flag = bs_read1( p_bs );
972
47.5k
        if( !p_pps->uniform_spacing_flag )
973
14.2k
        {
974
52.3k
            for( unsigned i=0; i< p_pps->num_tile_columns_minus1; i++ )
975
39.7k
            {
976
39.7k
                (void) bs_read_ue( p_bs );
977
39.7k
                if( bs_error( p_bs ) )
978
1.60k
                    return false;
979
39.7k
            }
980
1.88M
            for( unsigned i=0; i< p_pps->num_tile_rows_minus1; i++ )
981
1.86M
            {
982
1.86M
                (void) bs_read_ue( p_bs );
983
1.86M
                if( bs_error( p_bs ) )
984
810
                    return false;
985
1.86M
            }
986
12.6k
        }
987
45.1k
        p_pps->loop_filter_across_tiles_enabled_flag = bs_read1( p_bs );
988
45.1k
        if( bs_error( p_bs ) )
989
878
            return false;
990
45.1k
    }
991
992
114k
    p_pps->pps_loop_filter_across_slices_enabled_flag = bs_read1( p_bs );
993
114k
    p_pps->deblocking_filter_control_present_flag = bs_read1( p_bs );
994
114k
    if( p_pps->deblocking_filter_control_present_flag )
995
44.3k
    {
996
44.3k
        p_pps->deblocking_filter_override_enabled_flag = bs_read1( p_bs );
997
44.3k
        p_pps->pps_deblocking_filter_disabled_flag = bs_read1( p_bs );
998
44.3k
        if( !p_pps->pps_deblocking_filter_disabled_flag )
999
22.4k
        {
1000
22.4k
            p_pps->pps_beta_offset_div2 = bs_read_se( p_bs );
1001
22.4k
            p_pps->pps_tc_offset_div2 = bs_read_se( p_bs );
1002
22.4k
            if( p_pps->pps_beta_offset_div2 > 6 || p_pps->pps_beta_offset_div2 < -6 ||
1003
20.5k
                p_pps->pps_tc_offset_div2 > 6 || p_pps->pps_tc_offset_div2 < -6 )
1004
4.23k
                return false;
1005
22.4k
        }
1006
44.3k
    }
1007
1008
109k
    p_pps->scaling_list_data_present_flag = bs_read1( p_bs );
1009
109k
    if( p_pps->scaling_list_data_present_flag && !hevc_parse_scaling_list_rbsp( p_bs ) )
1010
0
        return false;
1011
1012
109k
    p_pps->lists_modification_present_flag = bs_read1( p_bs );
1013
109k
    p_pps->log2_parallel_merge_level_minus2 = bs_read_ue( p_bs );
1014
109k
    p_pps->slice_header_extension_present_flag = bs_read1( p_bs );
1015
1016
109k
    p_pps->pps_extension_present_flag = bs_read1( p_bs );
1017
109k
    if( p_pps->pps_extension_present_flag )
1018
35.7k
    {
1019
35.7k
        p_pps->pps_range_extension_flag = bs_read1( p_bs );
1020
35.7k
        p_pps->pps_multilayer_extension_flag = bs_read1( p_bs );
1021
35.7k
        p_pps->pps_3d_extension_flag = bs_read1( p_bs );
1022
35.7k
        p_pps->pps_extension_5bits = bs_read( p_bs, 5 );
1023
35.7k
    }
1024
1025
109k
    return !bs_error( p_bs );
1026
109k
}
1027
1028
void hevc_rbsp_release_pps( hevc_picture_parameter_set_t *p_pps )
1029
182k
{
1030
182k
    free( p_pps );
1031
182k
}
1032
1033
182k
IMPL_hevc_generic_decode( hevc_decode_pps, hevc_picture_parameter_set_t,
1034
                          hevc_parse_pic_parameter_set_rbsp, hevc_rbsp_release_pps )
1035
1036
uint8_t hevc_get_sps_vps_id( const hevc_sequence_parameter_set_t *p_sps )
1037
395k
{
1038
395k
    return p_sps->sps_video_parameter_set_id;
1039
395k
}
1040
1041
uint8_t hevc_get_pps_sps_id( const hevc_picture_parameter_set_t *p_pps )
1042
499k
{
1043
499k
    return p_pps->pps_seq_parameter_set_id;
1044
499k
}
1045
1046
uint8_t hevc_get_slice_pps_id( const hevc_slice_segment_header_t *p_slice )
1047
116k
{
1048
116k
    return p_slice->slice_pic_parameter_set_id;
1049
116k
}
1050
1051
bool hevc_get_slice_no_output_of_prior_pics_flag( const hevc_slice_segment_header_t *p_slice )
1052
0
{
1053
0
    return p_slice->no_output_of_prior_pics_flag;
1054
0
}
1055
1056
bool hevc_get_sps_profile_tier_level( const hevc_sequence_parameter_set_t *p_sps,
1057
                                      uint8_t *pi_profile, uint8_t *pi_level)
1058
83.1k
{
1059
83.1k
    if(p_sps->profile_tier_level.general.profile_idc)
1060
80.2k
    {
1061
80.2k
        *pi_profile = p_sps->profile_tier_level.general.profile_idc;
1062
80.2k
        *pi_level = p_sps->profile_tier_level.general_level_idc;
1063
80.2k
        return true;
1064
80.2k
    }
1065
2.82k
    return false;
1066
83.1k
}
1067
1068
bool hevc_get_picture_size( const hevc_sequence_parameter_set_t *p_sps,
1069
                            unsigned *p_ox, unsigned *p_oy,
1070
                            unsigned *p_w, unsigned *p_h,
1071
                            unsigned *p_vw, unsigned *p_vh )
1072
116k
{
1073
116k
    return h26x_get_picture_size( p_sps->chroma_format_idc,
1074
116k
                                  p_sps->pic_width_in_luma_samples,
1075
116k
                                  p_sps->pic_height_in_luma_samples,
1076
116k
                                  &p_sps->conf_win,
1077
116k
                                  p_ox, p_oy, p_w, p_h, p_vw, p_vh );
1078
116k
}
1079
1080
void hevc_get_dpb_values( const hevc_sequence_parameter_set_t *p_sps, uint8_t *max_num_reorder_pics,
1081
                          uint8_t *max_latency_pics, uint8_t *max_dec_pic_buffering )
1082
0
{
1083
0
    *max_num_reorder_pics = p_sps->sps_max[p_sps->sps_max_sub_layers_minus1/* HighestTid */].num_reorder_pics;
1084
0
    *max_latency_pics = p_sps->sps_max[p_sps->sps_max_sub_layers_minus1/* HighestTid */].latency_increase_plus1;
1085
0
    if(*max_latency_pics != 0)
1086
0
        *max_latency_pics = *max_num_reorder_pics + *max_latency_pics - 1;
1087
0
    *max_dec_pic_buffering = 1 +
1088
0
        p_sps->sps_max[p_sps->sps_max_sub_layers_minus1/* HighestTid */].dec_pic_buffering_minus1;
1089
    /*
1090
    When sps_max_dec_pic_buffering_minus1[ TemporalId ] is equal to 0, the value of TwoVersionsOfCurrDecPicFlag
1091
                                                                         shall be equal to 0.
1092
1093
    pps_curr_pic_ref_enabled_flag &&
1094
        ( sample_adaptive_offset_enabled_flag | | !pps_deblocking_filter_disabled_flag | |
1095
                                      deblocking_filter_override_enabled_flag );
1096
    */
1097
0
}
1098
1099
static bool hevc_get_picture_CtbsYsize( const hevc_sequence_parameter_set_t *p_sps, unsigned *p_w, unsigned *p_h )
1100
0
{
1101
0
    const unsigned int MinCbLog2SizeY = p_sps->log2_min_luma_coding_block_size_minus3 + 3;
1102
0
    const unsigned int CtbLog2SizeY = MinCbLog2SizeY + p_sps->log2_diff_max_min_luma_coding_block_size;
1103
0
    if( CtbLog2SizeY > 31 )
1104
0
        return false;
1105
0
    const unsigned int CtbSizeY = 1 << CtbLog2SizeY;
1106
0
    *p_w = (p_sps->pic_width_in_luma_samples - 1) / CtbSizeY + 1;
1107
0
    *p_h = (p_sps->pic_height_in_luma_samples - 1) / CtbSizeY + 1;
1108
0
    return true;
1109
0
}
1110
1111
bool hevc_get_frame_rate( const hevc_sequence_parameter_set_t *p_sps,
1112
                          const hevc_video_parameter_set_t *p_vps,
1113
                          unsigned *pi_num, unsigned *pi_den )
1114
1.25k
{
1115
1.25k
    if( p_sps->vui_parameters_present_flag && p_sps->vui.vui_timing_info_present_flag )
1116
370
    {
1117
370
        *pi_den = p_sps->vui.timing.vui_num_units_in_tick;
1118
370
        *pi_num = p_sps->vui.timing.vui_time_scale;
1119
370
        return (*pi_den && *pi_num);
1120
370
    }
1121
880
    else if( p_vps && p_vps->vps_timing_info_present_flag )
1122
335
    {
1123
335
        *pi_den = p_vps->vps_num_units_in_tick;
1124
335
        *pi_num = p_vps->vps_time_scale;
1125
335
        return (*pi_den && *pi_num);
1126
335
    }
1127
545
    return false;
1128
1.25k
}
1129
1130
bool hevc_get_aspect_ratio( const hevc_sequence_parameter_set_t *p_sps,
1131
                            unsigned *num, unsigned *den )
1132
103k
{
1133
103k
    if( !p_sps->vui_parameters_present_flag )
1134
79.1k
        return false;
1135
24.8k
    return h26x_get_aspect_ratio( &p_sps->vui.ar, num, den );
1136
103k
}
1137
1138
bool hevc_get_chroma_luma( const hevc_sequence_parameter_set_t *p_sps, uint8_t *pi_chroma_format,
1139
                           uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma )
1140
0
{
1141
0
    *pi_chroma_format = p_sps->chroma_format_idc;
1142
0
    *pi_depth_luma = p_sps->bit_depth_chroma_minus8 + 8;
1143
0
    *pi_depth_chroma = p_sps->bit_depth_chroma_minus8 + 8;
1144
0
    return true;
1145
0
}
1146
1147
bool hevc_get_colorimetry( const hevc_sequence_parameter_set_t *p_sps,
1148
                           video_color_primaries_t *p_primaries,
1149
                           video_transfer_func_t *p_transfer,
1150
                           video_color_space_t *p_colorspace,
1151
                           video_color_range_t *p_full_range )
1152
115k
{
1153
115k
    if( !p_sps->vui_parameters_present_flag )
1154
84.9k
        return false;
1155
30.6k
    return h26x_get_colorimetry( &p_sps->vui.vs.colour,
1156
30.6k
                                p_primaries, p_transfer, p_colorspace, p_full_range );
1157
115k
}
1158
1159
static bool hevc_parse_slice_segment_header_rbsp( bs_t *p_bs,
1160
                                                  pf_get_matchedxps get_matchedxps,
1161
                                                  void *priv,
1162
                                                  hevc_slice_segment_header_t *p_sl )
1163
3.76M
{
1164
3.76M
    hevc_sequence_parameter_set_t *p_sps;
1165
3.76M
    hevc_picture_parameter_set_t *p_pps;
1166
3.76M
    hevc_video_parameter_set_t *p_vps;
1167
1168
3.76M
    if( bs_eof( p_bs ) )
1169
0
        return false;
1170
1171
3.76M
    p_sl->first_slice_segment_in_pic_flag = bs_read1( p_bs );
1172
3.76M
    if( p_sl->nal_type >= HEVC_NAL_BLA_W_LP && p_sl->nal_type <= HEVC_NAL_IRAP_VCL23 )
1173
3.58M
        p_sl->no_output_of_prior_pics_flag = bs_read1( p_bs );
1174
3.76M
    p_sl->slice_pic_parameter_set_id = bs_read_ue( p_bs );
1175
3.76M
    if( p_sl->slice_pic_parameter_set_id > HEVC_PPS_ID_MAX )
1176
199k
        return false;
1177
1178
3.56M
    if( bs_error( p_bs ) )
1179
3.19M
        return false;
1180
1181
369k
    get_matchedxps( p_sl->slice_pic_parameter_set_id, priv, &p_pps, &p_sps, &p_vps );
1182
369k
    if(!p_sps || !p_pps)
1183
130k
        return false;
1184
1185
238k
    if( !p_sl->first_slice_segment_in_pic_flag )
1186
0
    {
1187
0
        if( p_pps->dependent_slice_segments_enabled_flag )
1188
0
            p_sl->dependent_slice_segment_flag = bs_read1( p_bs );
1189
1190
0
        unsigned w, h;
1191
0
        if( !hevc_get_picture_CtbsYsize( p_sps, &w, &h ) )
1192
0
            return false;
1193
1194
0
        (void) bs_read( p_bs, stdc_bit_width( w * h - 1 ) ); /* slice_segment_address */
1195
0
    }
1196
1197
238k
    if( !p_sl->dependent_slice_segment_flag )
1198
238k
    {
1199
238k
        unsigned i=0;
1200
238k
        if( p_pps->num_extra_slice_header_bits > i )
1201
211k
        {
1202
211k
            i++;
1203
211k
            bs_skip( p_bs, 1 ); /* discardable_flag */
1204
211k
        }
1205
1206
238k
        if( p_pps->num_extra_slice_header_bits > i )
1207
202k
        {
1208
202k
            i++;
1209
202k
            bs_skip( p_bs, 1 ); /* cross_layer_bla_flag */
1210
202k
        }
1211
1212
238k
        if( i < p_pps->num_extra_slice_header_bits )
1213
201k
           bs_skip( p_bs, p_pps->num_extra_slice_header_bits - i );
1214
1215
238k
        p_sl->slice_type = bs_read_ue( p_bs );
1216
238k
        if( p_sl->slice_type > HEVC_SLICE_TYPE_I )
1217
5.20k
            return false;
1218
1219
233k
        if( p_pps->output_flag_present_flag )
1220
192k
            p_sl->pic_output_flag = bs_read1( p_bs );
1221
41.1k
        else
1222
41.1k
            p_sl->pic_output_flag = 1;
1223
233k
    }
1224
1225
233k
    if( p_sps->separate_colour_plane_flag )
1226
53.6k
        bs_skip( p_bs, 2 ); /* colour_plane_id */
1227
1228
233k
    if( p_sl->nal_type != HEVC_NAL_IDR_W_RADL && p_sl->nal_type != HEVC_NAL_IDR_N_LP )
1229
222k
        p_sl->pic_order_cnt_lsb = bs_read( p_bs, p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4 );
1230
11.2k
    else
1231
11.2k
        p_sl->pic_order_cnt_lsb = 0;
1232
1233
233k
    return !bs_error( p_bs );
1234
238k
}
1235
1236
void hevc_rbsp_release_slice_header( hevc_slice_segment_header_t *p_sh )
1237
3.84M
{
1238
3.84M
    free( p_sh );
1239
3.84M
}
1240
1241
hevc_slice_segment_header_t * hevc_decode_slice_header( const uint8_t *p_buf, size_t i_buf, bool b_escaped,
1242
                                                        pf_get_matchedxps get_matchedxps, void *priv )
1243
3.84M
{
1244
3.84M
    hevc_slice_segment_header_t *p_sh = calloc(1, sizeof(hevc_slice_segment_header_t));
1245
3.84M
    if(likely(p_sh))
1246
3.84M
    {
1247
3.84M
        bs_t bs;
1248
3.84M
        struct hxxx_bsfw_ep3b_ctx_s bsctx;
1249
3.84M
        if( b_escaped )
1250
3.84M
        {
1251
3.84M
            hxxx_bsfw_ep3b_ctx_init( &bsctx );
1252
3.84M
            bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );
1253
3.84M
        }
1254
0
        else bs_init( &bs, p_buf, i_buf );
1255
3.84M
        bs_skip( &bs, 1 );
1256
3.84M
        p_sh->nal_type = bs_read( &bs, 6 );
1257
3.84M
        p_sh->nuh_layer_id = bs_read( &bs, 6 );
1258
3.84M
        p_sh->temporal_id_plus1 = bs_read( &bs, 3 );
1259
3.84M
        if( p_sh->nuh_layer_id > 62 || p_sh->temporal_id_plus1 == 0 ||
1260
3.76M
           !hevc_parse_slice_segment_header_rbsp( &bs, get_matchedxps, priv, p_sh ) )
1261
3.71M
        {
1262
3.71M
            hevc_rbsp_release_slice_header( p_sh );
1263
3.71M
            p_sh = NULL;
1264
3.71M
        }
1265
3.84M
    }
1266
3.84M
    return p_sh;
1267
3.84M
}
1268
1269
bool hevc_get_slice_type( const hevc_slice_segment_header_t *p_sli, enum hevc_slice_type_e *pi_type )
1270
26.3k
{
1271
26.3k
    if( !p_sli->dependent_slice_segment_flag )
1272
26.3k
    {
1273
26.3k
        *pi_type = p_sli->slice_type;
1274
26.3k
        return true;
1275
26.3k
    }
1276
0
    return false;
1277
26.3k
}
1278
1279
bool hevc_get_slice_pic_output( const hevc_slice_segment_header_t *p_sli )
1280
0
{
1281
0
    return p_sli->pic_output_flag;
1282
0
}
1283
1284
bool hevc_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
1285
                            uint8_t *pi_level, uint8_t *pi_nal_length_size)
1286
0
{
1287
0
    const uint8_t *p = (const uint8_t*)p_fmt->p_extra;
1288
0
    if(p_fmt->i_extra < 23 || p[0] != 1)
1289
0
        return false;
1290
1291
    /* HEVCDecoderConfigurationRecord */
1292
0
    if(pi_profile)
1293
0
        *pi_profile = p[1] & 0x1F;
1294
1295
0
    if(pi_level)
1296
0
        *pi_level = p[12];
1297
1298
0
    if (pi_nal_length_size)
1299
0
        *pi_nal_length_size = 1 + (p[21]&0x03);
1300
1301
0
    return true;
1302
0
}
1303
1304
static unsigned hevc_make_indication( const hevc_inner_profile_tier_level_t *p )
1305
0
{
1306
0
    uint8_t flags[] =
1307
0
    {
1308
0
        p->idc4to7.max_14bit_constraint_flag,
1309
0
        p->idc4to7.max_12bit_constraint_flag,
1310
0
        p->idc4to7.max_10bit_constraint_flag,
1311
0
        p->idc4to7.max_8bit_constraint_flag,
1312
0
        p->idc4to7.max_422chroma_constraint_flag,
1313
0
        p->idc4to7.max_420chroma_constraint_flag,
1314
0
        p->idc4to7.max_monochrome_constraint_flag,
1315
0
        p->idc4to7.intra_constraint_flag,
1316
0
        p->idc4to7.one_picture_only_constraint_flag,
1317
0
        p->idc4to7.lower_bit_rate_constraint_flag,
1318
0
    };
1319
0
    unsigned indication = 0;
1320
0
    for( size_t i=0; i<ARRAY_SIZE(flags); i++ )
1321
0
    {
1322
0
        if( flags[i] )
1323
0
            indication |= (1 << (ARRAY_SIZE(flags) - 1 - i));
1324
0
    }
1325
0
    return indication;
1326
0
}
1327
1328
enum vlc_hevc_profile_e hevc_get_vlc_profile( const hevc_sequence_parameter_set_t *p_sps )
1329
0
{
1330
0
    unsigned indication = 0;
1331
0
    enum hevc_general_profile_idc_e profile = p_sps->profile_tier_level.general.profile_idc;
1332
0
    switch( profile )
1333
0
    {
1334
0
        case HEVC_PROFILE_IDC_REXT:
1335
0
            indication = hevc_make_indication( &p_sps->profile_tier_level.general ) & 0x1FF;
1336
0
            break;
1337
0
        case HEVC_PROFILE_IDC_HIGH_THROUGHPUT:
1338
0
        case HEVC_PROFILE_IDC_SCREEN_EXTENDED:
1339
0
            indication = hevc_make_indication( &p_sps->profile_tier_level.general );
1340
0
            break;
1341
0
        default:
1342
0
            break;
1343
0
    }
1344
1345
    /* all intras have insignifiant lowest bit */
1346
0
    if( p_sps->profile_tier_level.general.idc4to7.intra_constraint_flag )
1347
0
        indication &= ~1;
1348
1349
0
    return (indication << HEVC_INDICATION_SHIFT) | profile;
1350
0
}
1351
1352
/*
1353
 * HEVCDecoderConfigurationRecord operations
1354
 */
1355
1356
static void hevc_dcr_params_from_vps( const uint8_t * p_buffer, size_t i_buffer,
1357
                                      struct hevc_dcr_values *p_values )
1358
0
{
1359
0
    if( i_buffer < 19 )
1360
0
        return;
1361
1362
0
    bs_t bs;
1363
0
    struct hxxx_bsfw_ep3b_ctx_s bsctx;
1364
0
    hxxx_bsfw_ep3b_ctx_init( &bsctx );
1365
0
    bs_init_custom( &bs, p_buffer, i_buffer, &hxxx_bsfw_ep3b_callbacks, &bsctx );
1366
1367
    /* first two bytes are the NAL header, 3rd and 4th are:
1368
        vps_video_parameter_set_id(4)
1369
        vps_reserved_3_2bis(2)
1370
        vps_max_layers_minus1(6)
1371
        vps_max_sub_layers_minus1(3)
1372
        vps_temporal_id_nesting_flags
1373
    */
1374
0
    bs_skip( &bs, 16 + 4 + 2 + 6 );
1375
0
    p_values->i_numTemporalLayer = bs_read( &bs, 3 ) + 1;
1376
0
    p_values->b_temporalIdNested = bs_read1( &bs );
1377
1378
    /* 5th & 6th are reserved 0xffff */
1379
0
    bs_skip( &bs, 16 );
1380
    /* copy the first 12 bytes of profile tier */
1381
0
    for( unsigned i=0; i<12; i++ )
1382
0
        p_values->general_configuration[i] = bs_read( &bs, 8 );
1383
0
}
1384
1385
0
#define HEVC_DCR_ADD_NALS(type, count, buffers, sizes) \
1386
0
for (uint8_t i = 0; i < count; i++) \
1387
0
{ \
1388
0
    if( i ==0 ) \
1389
0
    { \
1390
0
        *p++ = (type | (b_completeness ? 0x80 : 0)); \
1391
0
        SetWBE( p, count ); p += 2; \
1392
0
    } \
1393
0
    SetWBE( p, sizes[i]); p += 2; \
1394
0
    memcpy( p, buffers[i], sizes[i] ); p += sizes[i];\
1395
0
}
1396
1397
0
#define HEVC_DCR_ADD_SIZES(count, sizes) \
1398
0
if(count > 0) \
1399
0
{\
1400
0
    i_total_size += 3;\
1401
0
    for(uint8_t i=0; i<count; i++)\
1402
0
        i_total_size += 2 + sizes[i];\
1403
0
}
1404
1405
/* Generate HEVCDecoderConfiguration iso/iec 14496-15 3rd edition */
1406
uint8_t * hevc_create_dcr( const struct hevc_dcr_params *p_params,
1407
                           uint8_t i_nal_length_size,
1408
                           bool b_completeness, size_t *pi_size )
1409
0
{
1410
0
    *pi_size = 0;
1411
1412
0
    if( i_nal_length_size != 1 && i_nal_length_size != 2 && i_nal_length_size != 4 )
1413
0
        return NULL;
1414
1415
0
    struct hevc_dcr_values values =
1416
0
    {
1417
0
        .general_configuration = {0},
1418
0
        .i_numTemporalLayer = 0,
1419
0
        .i_chroma_idc = 1,
1420
0
        .i_bit_depth_luma_minus8 = 0,
1421
0
        .i_bit_depth_chroma_minus8 = 0,
1422
0
        .b_temporalIdNested = false,
1423
0
    };
1424
1425
0
    if( p_params->p_values != NULL )
1426
0
    {
1427
0
        values = *p_params->p_values;
1428
0
    }
1429
0
    else
1430
0
    {
1431
0
        if( p_params->i_vps_count == 0 || p_params->i_sps_count == 0 )
1432
0
           return NULL; /* required to extract info */
1433
1434
0
        hevc_dcr_params_from_vps( p_params->p_vps[0], p_params->rgi_vps[0], &values );
1435
1436
0
        hevc_sequence_parameter_set_t *p_sps =
1437
0
                hevc_decode_sps( p_params->p_sps[0], p_params->rgi_sps[0], true );
1438
0
        if( p_sps )
1439
0
        {
1440
0
            values.i_chroma_idc = p_sps->chroma_format_idc;
1441
0
            values.i_bit_depth_chroma_minus8 = p_sps->bit_depth_chroma_minus8;
1442
0
            values.i_bit_depth_luma_minus8 = p_sps->bit_depth_luma_minus8;
1443
0
            hevc_rbsp_release_sps( p_sps );
1444
0
        }
1445
0
    }
1446
1447
0
    size_t i_total_size = 1+12+2+4+2+2;
1448
0
    HEVC_DCR_ADD_SIZES(p_params->i_vps_count, p_params->rgi_vps);
1449
0
    HEVC_DCR_ADD_SIZES(p_params->i_sps_count, p_params->rgi_sps);
1450
0
    HEVC_DCR_ADD_SIZES(p_params->i_pps_count, p_params->rgi_pps);
1451
0
    HEVC_DCR_ADD_SIZES(p_params->i_seipref_count, p_params->rgi_seipref);
1452
0
    HEVC_DCR_ADD_SIZES(p_params->i_seisuff_count, p_params->rgi_seisuff);
1453
1454
0
    uint8_t *p_data = malloc( i_total_size );
1455
0
    if( p_data == NULL )
1456
0
        return NULL;
1457
1458
0
    *pi_size = i_total_size;
1459
0
    uint8_t *p = p_data;
1460
1461
    /* version */
1462
0
    *p++ = 0x01;
1463
0
    memcpy( p, values.general_configuration, 12 ); p += 12;
1464
    /* Don't set min spatial segmentation */
1465
0
    SetWBE( p, 0xF000 ); p += 2;
1466
    /* Don't set parallelism type since segmentation isn't set */
1467
0
    *p++ = 0xFC;
1468
0
    *p++ = (0xFC | (values.i_chroma_idc & 0x03));
1469
0
    *p++ = (0xF8 | (values.i_bit_depth_luma_minus8 & 0x07));
1470
0
    *p++ = (0xF8 | (values.i_bit_depth_chroma_minus8 & 0x07));
1471
1472
    /* Don't set framerate */
1473
0
    SetWBE( p, 0x0000); p += 2;
1474
    /* Force NAL size of 4 bytes that replace the startcode */
1475
0
    *p++ = ( ((values.i_numTemporalLayer & 0x07) << 3) |
1476
0
              (values.b_temporalIdNested << 2) |
1477
0
              (i_nal_length_size - 1) );
1478
    /* total number of arrays */
1479
0
    *p++ = !!p_params->i_vps_count + !!p_params->i_sps_count +
1480
0
           !!p_params->i_pps_count + !!p_params->i_seipref_count +
1481
0
           !!p_params->i_seisuff_count;
1482
1483
    /* Write NAL arrays */
1484
0
    HEVC_DCR_ADD_NALS(HEVC_NAL_VPS, p_params->i_vps_count,
1485
0
                      p_params->p_vps, p_params->rgi_vps);
1486
0
    HEVC_DCR_ADD_NALS(HEVC_NAL_SPS, p_params->i_sps_count,
1487
0
                      p_params->p_sps, p_params->rgi_sps);
1488
0
    HEVC_DCR_ADD_NALS(HEVC_NAL_PPS, p_params->i_pps_count,
1489
0
                      p_params->p_pps, p_params->rgi_pps);
1490
0
    HEVC_DCR_ADD_NALS(HEVC_NAL_PREF_SEI, p_params->i_seipref_count,
1491
0
                      p_params->p_seipref, p_params->rgi_seipref);
1492
0
    HEVC_DCR_ADD_NALS(HEVC_NAL_SUFF_SEI, p_params->i_seisuff_count,
1493
0
                      p_params->p_seisuff, p_params->rgi_seisuff);
1494
1495
0
    return p_data;
1496
0
}
1497
1498
#undef HEVC_DCR_ADD_NALS
1499
#undef HEVC_DCR_ADD_SIZES
1500
1501
bool hevc_NAL_IsIRAP( uint8_t i_nal_type )
1502
0
{
1503
0
    return ( i_nal_type >= HEVC_NAL_BLA_W_LP &&
1504
0
             i_nal_type <= HEVC_NAL_IRAP_VCL23 );
1505
0
}
1506
1507
bool hevc_get_IRAPNoRaslOutputFlag( uint8_t i_nal_type,
1508
                                    const hevc_poc_ctx_t *p_ctx )
1509
0
{
1510
    /* if( IRAP ) NoRaslOutputFlag = first || IDR || BLA || after(EOSNAL) */
1511
0
    return ( p_ctx->first_picture ||
1512
0
             i_nal_type == HEVC_NAL_IDR_N_LP ||
1513
0
             i_nal_type == HEVC_NAL_IDR_W_RADL ||
1514
0
             i_nal_type == HEVC_NAL_BLA_W_LP ||
1515
0
             i_nal_type == HEVC_NAL_BLA_W_RADL ||
1516
0
             i_nal_type == HEVC_NAL_BLA_N_LP ||
1517
0
             p_ctx->HandleCraAsBlaFlag );
1518
0
}
1519
1520
/*
1521
 * 8.3.1 Decoding process for POC
1522
 */
1523
int hevc_compute_picture_order_count( const hevc_sequence_parameter_set_t *p_sps,
1524
                                       const hevc_slice_segment_header_t *p_slice,
1525
                                       hevc_poc_ctx_t *p_ctx )
1526
0
{
1527
0
    struct
1528
0
    {
1529
0
        int lsb;
1530
0
        int msb;
1531
0
    } prevPicOrderCnt;
1532
0
    int pocMSB;
1533
0
    const bool IsIRAP = hevc_NAL_IsIRAP( p_slice->nal_type );
1534
0
    const bool NoRaslOutputFlag =
1535
0
        IsIRAP ? hevc_get_IRAPNoRaslOutputFlag( p_slice->nal_type, p_ctx ) : false;
1536
1537
#ifdef HEVC_POC_DEBUG
1538
    printf("slice lsb=%"PRIu32" irap=%d norasl=%d tid=%d msb=%d lsb=%d",
1539
           p_slice->pic_order_cnt_lsb,
1540
           IsIRAP, NoRaslOutputFlag, p_slice->temporal_id_plus1,
1541
           p_ctx->prevTid0PicOrderCnt.msb, p_ctx->prevTid0PicOrderCnt.lsb);
1542
#endif
1543
1544
0
    if( p_slice->nal_type == HEVC_NAL_IDR_N_LP ||
1545
0
        p_slice->nal_type == HEVC_NAL_IDR_W_RADL )
1546
0
    {
1547
0
        prevPicOrderCnt.msb = 0;
1548
0
        prevPicOrderCnt.lsb = 0;
1549
0
    }
1550
    /* Not an IRAP with NoRaslOutputFlag == 1 */
1551
0
    else if( !IsIRAP || !NoRaslOutputFlag )
1552
0
    {
1553
0
        prevPicOrderCnt.msb = p_ctx->prevTid0PicOrderCnt.msb;
1554
0
        prevPicOrderCnt.lsb = p_ctx->prevTid0PicOrderCnt.lsb;
1555
0
    }
1556
1557
0
    if( IsIRAP && NoRaslOutputFlag )
1558
0
    {
1559
0
        pocMSB = 0;
1560
0
    }
1561
0
    else
1562
0
    {
1563
0
        const unsigned maxPocLSB = 1U << (p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1564
0
        pocMSB = prevPicOrderCnt.msb;
1565
0
        int64_t orderDiff = (int64_t)p_slice->pic_order_cnt_lsb - prevPicOrderCnt.lsb;
1566
0
        if( orderDiff < 0 && -orderDiff >= maxPocLSB / 2 )
1567
0
            pocMSB += maxPocLSB;
1568
0
        else if( orderDiff > maxPocLSB / 2 )
1569
0
            pocMSB -= maxPocLSB;
1570
0
    }
1571
1572
    /* Set prevTid0Pic for next pic */
1573
0
    if( p_slice->temporal_id_plus1 == 1 &&
1574
0
       !( ( p_slice->nal_type <= HEVC_NAL_RSV_VCL_N14 && p_slice->nal_type % 2 == 0 /* SLNR */ ) ||
1575
0
          ( p_slice->nal_type >= HEVC_NAL_RADL_N && p_slice->nal_type <= HEVC_NAL_RASL_R ) /* RADL or RASL */ ) )
1576
0
    {
1577
0
        p_ctx->prevTid0PicOrderCnt.msb = pocMSB;
1578
0
        p_ctx->prevTid0PicOrderCnt.lsb = p_slice->pic_order_cnt_lsb;
1579
0
    }
1580
1581
0
    p_ctx->first_picture = false;
1582
1583
#ifdef HEVC_POC_DEBUG
1584
    printf(" POC=%"PRIu32"\n", pocMSB + p_slice->pic_order_cnt_lsb);
1585
#endif
1586
1587
0
    return pocMSB + p_slice->pic_order_cnt_lsb;
1588
0
}
1589
1590
struct hevc_sei_pic_timing_t
1591
{
1592
    nal_u4_t pic_struct;
1593
    nal_u2_t source_scan_type;
1594
};
1595
1596
void hevc_release_sei_pic_timing( hevc_sei_pic_timing_t *p_timing )
1597
3.91M
{
1598
3.91M
    free( p_timing );
1599
3.91M
}
1600
1601
hevc_sei_pic_timing_t * hevc_decode_sei_pic_timing( bs_t *p_bs,
1602
                                                    const hevc_sequence_parameter_set_t *p_sps )
1603
5.93k
{
1604
5.93k
    hevc_sei_pic_timing_t *p_timing = malloc(sizeof(*p_timing));
1605
5.93k
    if( p_timing )
1606
5.93k
    {
1607
5.93k
        if( p_sps->vui.frame_field_info_present_flag )
1608
3.16k
        {
1609
3.16k
            p_timing->pic_struct = bs_read( p_bs, 4 );
1610
3.16k
            p_timing->source_scan_type = bs_read( p_bs, 2 );
1611
3.16k
        }
1612
2.76k
        else
1613
2.76k
        {
1614
2.76k
            p_timing->pic_struct = 0;
1615
2.76k
            p_timing->source_scan_type = 1;
1616
2.76k
        }
1617
5.93k
    }
1618
5.93k
    return p_timing;
1619
5.93k
}
1620
1621
bool hevc_decode_sei_recovery_point( bs_t *p_bs, hevc_sei_recovery_point_t *p_reco )
1622
340
{
1623
340
    p_reco->i_frames = bs_read_se( p_bs );
1624
340
    return true;
1625
340
}
1626
1627
bool hevc_frame_is_progressive( const hevc_sequence_parameter_set_t *p_sps,
1628
                                const hevc_sei_pic_timing_t *p_timing )
1629
0
{
1630
0
    if( p_sps->vui_parameters_present_flag &&
1631
0
        p_sps->vui.field_seq_flag )
1632
0
        return false;
1633
1634
0
    const hevc_inner_profile_tier_level_t *p_profile = &p_sps->profile_tier_level.general;
1635
    /* 1 & 0, 0 & 1, global */
1636
0
    if( p_profile->progressive_source_flag != p_profile->interlaced_source_flag )
1637
0
    {
1638
0
        return p_profile->progressive_source_flag > p_profile->interlaced_source_flag;
1639
0
    }
1640
    /* 1 & 1, defined in SEI */
1641
0
    else if( p_profile->progressive_source_flag )
1642
0
    {
1643
0
        if( p_timing && p_sps->vui.frame_field_info_present_flag )
1644
0
        {
1645
0
            if( p_timing->source_scan_type < 2 )
1646
0
                return p_timing->source_scan_type != 0;
1647
0
        }
1648
0
    }
1649
    /* else 0 & 0, unspec */
1650
1651
0
    return true;
1652
0
}
1653
1654
uint8_t hevc_get_num_clock_ts( const hevc_sequence_parameter_set_t *p_sps,
1655
                               const hevc_sei_pic_timing_t *p_timing )
1656
1.86M
{
1657
1.86M
    if( p_sps->vui.frame_field_info_present_flag && p_timing && p_timing->pic_struct < 13 )
1658
1.14k
    {
1659
        /* !WARN modified with units_field_based_flag (D.3.25) for values 0, 7 and 8 */
1660
1.14k
        const uint8_t rgi_numclock[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1661
1.14k
        return rgi_numclock[p_timing->pic_struct];
1662
1.14k
    }
1663
1664
1.85M
    if( p_sps->vui_parameters_present_flag )
1665
909k
    {
1666
909k
        if( p_sps->vui.field_seq_flag )
1667
173k
            return 1; /* D.3.27 */
1668
909k
    }
1669
949k
    else if( p_sps->profile_tier_level.general.interlaced_source_flag &&
1670
141k
            !p_sps->profile_tier_level.general.progressive_source_flag )
1671
9.32k
    {
1672
9.32k
        return 1;
1673
9.32k
    }
1674
1675
1.67M
    return 2;
1676
1.85M
}