Coverage Report

Created: 2025-08-29 07:30

/src/vlc/modules/packetizer/hevc_nal.c
Line
Count
Source (jump to first uncovered line)
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
68.6k
#define HEVC_MAX_SHORT_TERM_REF_PIC_SET 65
79
17.0k
#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 size_t get_hvcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf )
307
87
{
308
87
    size_t i_total = 0;
309
310
87
    if( i_buf < HEVC_MIN_HVCC_SIZE )
311
0
        return 0;
312
313
87
    const uint8_t i_nal_length_size = (p_buf[21] & 0x03) + 1;
314
87
    if(i_nal_length_size == 3)
315
8
        return 0;
316
317
79
    const uint8_t i_num_array = p_buf[22];
318
79
    p_buf += 23; i_buf -= 23;
319
320
1.40k
    for( uint8_t i = 0; i < i_num_array; i++ )
321
1.37k
    {
322
1.37k
        if(i_buf < 3)
323
0
            return 0;
324
325
1.37k
        const uint16_t i_num_nalu = p_buf[1] << 8 | p_buf[2];
326
1.37k
        p_buf += 3; i_buf -= 3;
327
328
3.44k
        for( uint16_t j = 0; j < i_num_nalu; j++ )
329
2.11k
        {
330
2.11k
            if(i_buf < 2)
331
1
                return 0;
332
333
2.11k
            const uint16_t i_nalu_length = p_buf[0] << 8 | p_buf[1];
334
2.11k
            if(i_buf < (size_t)i_nalu_length + 2)
335
49
                return 0;
336
337
2.06k
            i_total += i_nalu_length + i_nal_length_size;
338
2.06k
            p_buf += i_nalu_length + 2;
339
2.06k
            i_buf -= i_nalu_length + 2;
340
2.06k
        }
341
1.37k
    }
342
343
29
    return i_total;
344
79
}
345
346
uint8_t * hevc_hvcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
347
                                   size_t *pi_result, uint8_t *pi_nal_length_size )
348
87
{
349
87
    *pi_result = get_hvcC_to_AnnexB_NAL_size( p_buf, i_buf ); /* Does all checks */
350
87
    if( *pi_result == 0 )
351
71
        return NULL;
352
353
16
    if( pi_nal_length_size )
354
16
        *pi_nal_length_size = hevc_getNALLengthSize( p_buf );
355
356
16
    uint8_t *p_ret;
357
16
    uint8_t *p_out_buf = p_ret = malloc( *pi_result );
358
16
    if( !p_out_buf )
359
0
    {
360
0
        *pi_result = 0;
361
0
        return NULL;
362
0
    }
363
364
16
    const uint8_t i_num_array = p_buf[22];
365
16
    p_buf += 23;
366
367
1.08k
    for( uint8_t i = 0; i < i_num_array; i++ )
368
1.06k
    {
369
1.06k
        const uint16_t i_num_nalu = p_buf[1] << 8 | p_buf[2];
370
1.06k
        p_buf += 3;
371
372
1.83k
        for( uint16_t j = 0; j < i_num_nalu; j++ )
373
775
        {
374
775
            const uint16_t i_nalu_length = p_buf[0] << 8 | p_buf[1];
375
376
775
            memcpy( p_out_buf, annexb_startcode4, 4 );
377
775
            memcpy( &p_out_buf[4], &p_buf[2], i_nalu_length );
378
379
775
            p_out_buf += 4 + i_nalu_length;
380
775
            p_buf += 2 + i_nalu_length;
381
775
        }
382
1.06k
    }
383
384
16
    return p_ret;
385
16
}
386
387
static bool hevc_parse_scaling_list_rbsp( bs_t *p_bs )
388
12.0k
{
389
60.1k
    for( int i=0; i<4; i++ )
390
48.1k
    {
391
288k
        for( int j=0; j<6; j += (i == 3) ? 3 : 1 )
392
240k
        {
393
240k
            if( bs_read1( p_bs ) == 0 )
394
212k
                bs_read_ue( p_bs );
395
28.3k
            else
396
28.3k
            {
397
28.3k
                unsigned nextCoef = 8;
398
28.3k
                unsigned coefNum = __MIN( 64, (1 << (4 + (i << 1))));
399
28.3k
                if( i > 1 )
400
3.08k
                {
401
3.08k
                    nextCoef = bs_read_se( p_bs ) + 8;
402
3.08k
                }
403
840k
                for( unsigned k=0; k<coefNum; k++ )
404
811k
                {
405
811k
                    nextCoef = ( nextCoef + bs_read_se( p_bs ) + 256 ) % 256;
406
811k
                }
407
28.3k
            }
408
240k
        }
409
48.1k
    }
410
411
12.0k
    return true;
412
12.0k
}
413
414
static bool hevc_parse_vui_parameters_rbsp( bs_t *p_bs, hevc_vui_parameters_t *p_vui,
415
                                            bool b_broken )
416
19.9k
{
417
19.9k
    p_vui->aspect_ratio_info_present_flag = bs_read1( p_bs );
418
19.9k
    if( p_vui->aspect_ratio_info_present_flag )
419
13.4k
    {
420
13.4k
        p_vui->ar.aspect_ratio_idc = bs_read( p_bs, 8 );
421
13.4k
        if( p_vui->ar.aspect_ratio_idc == 0xFF ) //HEVC_SAR__IDC_EXTENDED_SAR )
422
3.29k
        {
423
3.29k
            p_vui->ar.sar_width = bs_read( p_bs, 16 );
424
3.29k
            p_vui->ar.sar_height = bs_read( p_bs, 16 );
425
3.29k
        }
426
13.4k
    }
427
428
19.9k
    p_vui->overscan_info_present_flag = bs_read1( p_bs );
429
19.9k
    if( p_vui->overscan_info_present_flag )
430
9.97k
        p_vui->overscan_appropriate_flag = bs_read1( p_bs );
431
432
19.9k
    p_vui->video_signal_type_present_flag = bs_read1( p_bs );
433
19.9k
    if( p_vui->video_signal_type_present_flag )
434
9.59k
    {
435
9.59k
        p_vui->vs.video_format = bs_read( p_bs, 3 );
436
9.59k
        p_vui->vs.colour.full_range_flag = bs_read1( p_bs );
437
9.59k
        p_vui->vs.colour_description_present_flag = bs_read1( p_bs );
438
9.59k
        if( p_vui->vs.colour_description_present_flag )
439
5.70k
        {
440
5.70k
            p_vui->vs.colour.colour_primaries = bs_read( p_bs, 8 );
441
5.70k
            p_vui->vs.colour.transfer_characteristics = bs_read( p_bs, 8 );
442
5.70k
            p_vui->vs.colour.matrix_coeffs = bs_read( p_bs, 8 );
443
5.70k
        }
444
3.89k
        else
445
3.89k
        {
446
3.89k
            p_vui->vs.colour.colour_primaries = ISO_23001_8_CP_UNSPECIFIED;
447
3.89k
            p_vui->vs.colour.transfer_characteristics = ISO_23001_8_TC_UNSPECIFIED;
448
3.89k
            p_vui->vs.colour.matrix_coeffs = ISO_23001_8_MC_UNSPECIFIED;
449
3.89k
        }
450
9.59k
    }
451
452
19.9k
    p_vui->chroma_loc_info_present_flag = bs_read1( p_bs );
453
19.9k
    if( p_vui->chroma_loc_info_present_flag )
454
6.90k
    {
455
6.90k
        p_vui->chroma.sample_loc_type_top_field = bs_read_ue( p_bs );
456
6.90k
        p_vui->chroma.sample_loc_type_bottom_field = bs_read_ue( p_bs );
457
6.90k
    }
458
459
19.9k
    p_vui->neutral_chroma_indication_flag = bs_read1( p_bs );
460
19.9k
    p_vui->field_seq_flag = bs_read1( p_bs );
461
19.9k
    p_vui->frame_field_info_present_flag = bs_read1( p_bs );
462
463
19.9k
    p_vui->default_display_window_flag = !b_broken && bs_read1( p_bs );
464
19.9k
    if( p_vui->default_display_window_flag )
465
7.96k
    {
466
7.96k
        p_vui->def_disp.win_left_offset = bs_read_ue( p_bs );
467
7.96k
        p_vui->def_disp.win_right_offset = bs_read_ue( p_bs );
468
7.96k
        p_vui->def_disp.win_top_offset = bs_read_ue( p_bs );
469
7.96k
        p_vui->def_disp.win_bottom_offset = bs_read_ue( p_bs );
470
7.96k
    }
471
472
19.9k
    p_vui->vui_timing_info_present_flag = bs_read1( p_bs );
473
19.9k
    if( p_vui->vui_timing_info_present_flag )
474
5.38k
    {
475
5.38k
        p_vui->timing.vui_num_units_in_tick =  bs_read( p_bs, 32 );
476
5.38k
        p_vui->timing.vui_time_scale =  bs_read( p_bs, 32 );
477
5.38k
    }
478
    /* incomplete */
479
480
19.9k
    return !bs_error( p_bs );
481
19.9k
}
482
483
/* Shortcut for retrieving vps/sps/pps id */
484
bool hevc_get_xps_id(const uint8_t *p_buf, size_t i_buf, uint8_t *pi_id)
485
278k
{
486
278k
    if(i_buf < 3)
487
6.50k
        return false;
488
    /* No need to lookup convert from emulation for that data */
489
272k
    uint8_t i_nal_type = hevc_getNALType(p_buf);
490
272k
    bs_t bs;
491
272k
    bs_init(&bs, &p_buf[2], i_buf - 2);
492
272k
    if(i_nal_type == HEVC_NAL_PPS)
493
103k
    {
494
103k
        *pi_id = bs_read_ue( &bs );
495
103k
        if(*pi_id > HEVC_PPS_ID_MAX)
496
5.58k
            return false;
497
103k
    }
498
168k
    else
499
168k
    {
500
168k
        *pi_id = bs_read( &bs, 4 );
501
168k
        if(i_nal_type == HEVC_NAL_SPS)
502
124k
        {
503
124k
            if(*pi_id > HEVC_SPS_ID_MAX)
504
0
                return false;
505
124k
        }
506
43.3k
        else if(*pi_id > HEVC_VPS_ID_MAX)
507
0
            return false;
508
168k
    }
509
266k
    return true;
510
272k
}
511
512
static bool hevc_parse_inner_profile_tier_level_rbsp( bs_t *p_bs,
513
                                                      hevc_inner_profile_tier_level_t *p_in )
514
198k
{
515
198k
    p_in->profile_space = bs_read( p_bs, 2 );
516
198k
    p_in->tier_flag = bs_read1( p_bs );
517
198k
    p_in->profile_idc = bs_read( p_bs, 5 );
518
198k
    p_in->profile_compatibility_flag = bs_read( p_bs, 32 );
519
198k
    p_in->progressive_source_flag = bs_read1( p_bs );
520
198k
    p_in->interlaced_source_flag = bs_read1( p_bs );
521
198k
    p_in->non_packed_constraint_flag = bs_read1( p_bs );
522
198k
    p_in->frame_only_constraint_flag = bs_read1( p_bs );
523
524
198k
    if( ( p_in->profile_idc >= 4 && p_in->profile_idc <= 10 ) ||
525
198k
        ( p_in->profile_compatibility_flag & 0x0F700000 ) )
526
129k
    {
527
129k
        p_in->idc4to7.max_12bit_constraint_flag = bs_read1( p_bs );
528
129k
        p_in->idc4to7.max_10bit_constraint_flag = bs_read1( p_bs );
529
129k
        p_in->idc4to7.max_8bit_constraint_flag = bs_read1( p_bs );
530
129k
        p_in->idc4to7.max_422chroma_constraint_flag = bs_read1( p_bs );
531
129k
        p_in->idc4to7.max_420chroma_constraint_flag = bs_read1( p_bs );
532
129k
        p_in->idc4to7.max_monochrome_constraint_flag = bs_read1( p_bs );
533
129k
        p_in->idc4to7.intra_constraint_flag = bs_read1( p_bs );
534
129k
        p_in->idc4to7.one_picture_only_constraint_flag = bs_read1( p_bs );
535
129k
        p_in->idc4to7.lower_bit_rate_constraint_flag = bs_read1( p_bs );
536
129k
        if( p_in->profile_idc == 5 ||
537
129k
            p_in->profile_idc == 9 ||
538
129k
            p_in->profile_idc == 10 ||
539
129k
           (p_in->profile_compatibility_flag & 0x08600000) )
540
107k
        {
541
107k
            p_in->idc4to7.max_14bit_constraint_flag = bs_read1( p_bs );
542
107k
            bs_skip( p_bs, 33 );
543
107k
        }
544
22.1k
        else bs_skip( p_bs, 34 );
545
129k
    }
546
69.0k
    else if( p_in->profile_idc == 2 ||
547
69.0k
            (p_in->profile_compatibility_flag & 0x20000000) )
548
13.1k
    {
549
13.1k
        bs_skip( p_bs, 7 );
550
13.1k
        p_in->idc4to7.one_picture_only_constraint_flag = bs_read1( p_bs );
551
13.1k
        bs_skip( p_bs, 35 );
552
13.1k
    }
553
55.8k
    else
554
55.8k
    {
555
55.8k
        bs_read( p_bs, 43 );
556
55.8k
    }
557
558
198k
    if( ( p_in->profile_idc >= 1 && p_in->profile_idc <= 5 ) ||
559
198k
         p_in->profile_idc == 9 ||
560
198k
        ( p_in->profile_compatibility_flag & 0x7C400000 ) )
561
117k
        p_in->idc1to5.inbld_flag = bs_read1( p_bs );
562
80.7k
    else
563
80.7k
        bs_skip( p_bs, 1 );
564
565
198k
    return !bs_error( p_bs );
566
198k
}
567
568
static bool hevc_parse_profile_tier_level_rbsp( bs_t *p_bs, bool profile_present,
569
                                                uint8_t max_num_sub_layers_minus1,
570
                                                hevc_profile_tier_level_t *p_ptl )
571
166k
{
572
166k
    if( profile_present && !hevc_parse_inner_profile_tier_level_rbsp( p_bs, &p_ptl->general ) )
573
62.5k
        return false;
574
575
103k
    if( bs_error( p_bs ) )
576
0
        return false;
577
578
103k
    p_ptl->general_level_idc = bs_read( p_bs, 8 );
579
580
103k
    if( max_num_sub_layers_minus1 > 0 )
581
46.3k
    {
582
46.3k
        if( bs_eof( p_bs ) )
583
2.77k
            return false;
584
585
391k
        for( uint8_t i=0; i< 8; i++ )
586
348k
        {
587
348k
            if( i < max_num_sub_layers_minus1 )
588
213k
            {
589
213k
                if( bs_read1( p_bs ) )
590
64.4k
                    p_ptl->sublayer_profile_present_flag |= (0x80 >> i);
591
213k
                if( bs_read1( p_bs ) )
592
81.5k
                    p_ptl->sublayer_level_present_flag |= (0x80 >> i);
593
213k
            }
594
134k
            else
595
134k
                bs_read( p_bs, 2 );
596
348k
        }
597
598
176k
        for( uint8_t i=0; i < max_num_sub_layers_minus1; i++ )
599
150k
        {
600
150k
            if( ( p_ptl->sublayer_profile_present_flag & (0x80 >> i) ) &&
601
150k
                ! hevc_parse_inner_profile_tier_level_rbsp( p_bs, &p_ptl->sub_layer[i] ) )
602
11.0k
                return false;
603
604
139k
            if( p_ptl->sublayer_profile_present_flag & (0x80 >> i) )
605
20.7k
            {
606
20.7k
                if( bs_eof( p_bs ) )
607
6.89k
                    return false;
608
13.8k
                p_ptl->sub_layer_level_idc[i] = bs_read( p_bs, 8 );
609
13.8k
            }
610
139k
        }
611
43.5k
    }
612
613
83.2k
    return !bs_error( p_bs );
614
103k
}
615
616
static bool hevc_parse_video_parameter_set_rbsp( bs_t *p_bs,
617
                                                 hevc_video_parameter_set_t *p_vps )
618
42.4k
{
619
42.4k
    if( bs_eof( p_bs ) )
620
0
        return false;
621
622
42.4k
    p_vps->vps_video_parameter_set_id = bs_read( p_bs, 4 );
623
42.4k
    p_vps->vps_base_layer_internal_flag = bs_read1( p_bs );
624
42.4k
    p_vps->vps_base_layer_available_flag = bs_read1( p_bs );
625
42.4k
    p_vps->vps_max_layers_minus1 = bs_read( p_bs, 6 );
626
42.4k
    p_vps->vps_max_sub_layers_minus1 = bs_read( p_bs, 3 );
627
42.4k
    p_vps->vps_temporal_id_nesting_flag = bs_read1( p_bs );
628
42.4k
    bs_skip( p_bs, 16 );
629
630
42.4k
    if( !hevc_parse_profile_tier_level_rbsp( p_bs, true, p_vps->vps_max_sub_layers_minus1,
631
42.4k
                                            &p_vps->profile_tier_level ) )
632
24.8k
        return false;
633
634
17.6k
    p_vps->vps_sub_layer_ordering_info_present_flag = bs_read1( p_bs );
635
17.6k
    for( unsigned i= (p_vps->vps_sub_layer_ordering_info_present_flag ?
636
11.2k
                      0 : p_vps->vps_max_sub_layers_minus1);
637
47.5k
         i<= p_vps->vps_max_sub_layers_minus1; i++ )
638
29.9k
    {
639
29.9k
        p_vps->vps_max[i].dec_pic_buffering_minus1 = bs_read_ue( p_bs );
640
29.9k
        p_vps->vps_max[i].num_reorder_pics = bs_read_ue( p_bs );
641
29.9k
        p_vps->vps_max[i].max_latency_increase_plus1 = bs_read_ue( p_bs );
642
29.9k
    }
643
644
17.6k
    if( bs_error( p_bs ) )
645
2.77k
        return false;
646
647
14.8k
    p_vps->vps_max_layer_id = bs_read( p_bs, 6 );
648
14.8k
    p_vps->vps_num_layer_set_minus1 = bs_read_ue( p_bs );
649
    // layer_id_included_flag; read but discarded
650
14.8k
    bs_skip( p_bs, p_vps->vps_num_layer_set_minus1 * (p_vps->vps_max_layer_id + 1) );
651
652
14.8k
    p_vps->vps_timing_info_present_flag = bs_read1( p_bs );
653
14.8k
    if( p_vps->vps_timing_info_present_flag )
654
7.27k
    {
655
7.27k
        p_vps->vps_num_units_in_tick = bs_read( p_bs, 32 );
656
7.27k
        p_vps->vps_time_scale = bs_read( p_bs, 32 );
657
7.27k
    }
658
    /* parsing incomplete */
659
660
14.8k
    return !bs_error( p_bs );
661
17.6k
}
662
663
void hevc_rbsp_release_vps( hevc_video_parameter_set_t *p_vps )
664
42.7k
{
665
42.7k
    free( p_vps );
666
42.7k
}
667
668
#define IMPL_hevc_generic_decode( name, hevctype, decode, release ) \
669
    hevctype * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
670
259k
    { \
671
259k
        hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \
672
259k
        if(likely(p_hevctype)) \
673
259k
        { \
674
259k
            bs_t bs; \
675
259k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
676
259k
            if( b_escaped ) \
677
259k
            { \
678
259k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
679
259k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
680
259k
            } \
681
259k
            else bs_init( &bs, p_buf, i_buf ); \
682
259k
            bs_skip( &bs, 7 ); /* nal_unit_header */ \
683
259k
            uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \
684
259k
            bs_skip( &bs, 3 ); /* !nal_unit_header */ \
685
259k
            if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \
686
259k
            { \
687
224k
                release( p_hevctype ); \
688
224k
                p_hevctype = NULL; \
689
224k
            } \
690
259k
        } \
691
259k
        return p_hevctype; \
692
259k
    }
hevc_decode_vps
Line
Count
Source
670
42.7k
    { \
671
42.7k
        hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \
672
42.7k
        if(likely(p_hevctype)) \
673
42.7k
        { \
674
42.7k
            bs_t bs; \
675
42.7k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
676
42.7k
            if( b_escaped ) \
677
42.7k
            { \
678
42.7k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
679
42.7k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
680
42.7k
            } \
681
42.7k
            else bs_init( &bs, p_buf, i_buf ); \
682
42.7k
            bs_skip( &bs, 7 ); /* nal_unit_header */ \
683
42.7k
            uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \
684
42.7k
            bs_skip( &bs, 3 ); /* !nal_unit_header */ \
685
42.7k
            if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \
686
42.7k
            { \
687
35.4k
                release( p_hevctype ); \
688
35.4k
                p_hevctype = NULL; \
689
35.4k
            } \
690
42.7k
        } \
691
42.7k
        return p_hevctype; \
692
42.7k
    }
hevc_decode_sps
Line
Count
Source
670
124k
    { \
671
124k
        hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \
672
124k
        if(likely(p_hevctype)) \
673
124k
        { \
674
124k
            bs_t bs; \
675
124k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
676
124k
            if( b_escaped ) \
677
124k
            { \
678
124k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
679
124k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
680
124k
            } \
681
124k
            else bs_init( &bs, p_buf, i_buf ); \
682
124k
            bs_skip( &bs, 7 ); /* nal_unit_header */ \
683
124k
            uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \
684
124k
            bs_skip( &bs, 3 ); /* !nal_unit_header */ \
685
124k
            if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \
686
124k
            { \
687
114k
                release( p_hevctype ); \
688
114k
                p_hevctype = NULL; \
689
114k
            } \
690
124k
        } \
691
124k
        return p_hevctype; \
692
124k
    }
hevc_decode_pps
Line
Count
Source
670
92.2k
    { \
671
92.2k
        hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \
672
92.2k
        if(likely(p_hevctype)) \
673
92.2k
        { \
674
92.2k
            bs_t bs; \
675
92.2k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
676
92.2k
            if( b_escaped ) \
677
92.2k
            { \
678
92.2k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
679
92.2k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
680
92.2k
            } \
681
92.2k
            else bs_init( &bs, p_buf, i_buf ); \
682
92.2k
            bs_skip( &bs, 7 ); /* nal_unit_header */ \
683
92.2k
            uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \
684
92.2k
            bs_skip( &bs, 3 ); /* !nal_unit_header */ \
685
92.2k
            if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \
686
92.2k
            { \
687
74.9k
                release( p_hevctype ); \
688
74.9k
                p_hevctype = NULL; \
689
74.9k
            } \
690
92.2k
        } \
691
92.2k
        return p_hevctype; \
692
92.2k
    }
693
694
IMPL_hevc_generic_decode( hevc_decode_vps, hevc_video_parameter_set_t,
695
                          hevc_parse_video_parameter_set_rbsp, hevc_rbsp_release_vps )
696
697
static bool hevc_parse_st_ref_pic_set( bs_t *p_bs, unsigned stRpsIdx,
698
                                       unsigned num_short_term_ref_pic_sets,
699
                                       hevc_short_term_ref_pic_set_t *p_sets )
700
54.4k
{
701
54.4k
    if( stRpsIdx && bs_read1( p_bs ) ) /* Interref pic set prediction flag */
702
28.0k
    {
703
28.0k
        nal_ue_t delta_idx_minus_1 = 0;
704
28.0k
        if( stRpsIdx == num_short_term_ref_pic_sets )
705
0
        {
706
0
            delta_idx_minus_1 = bs_read_ue( p_bs );
707
0
            if( delta_idx_minus_1 >= stRpsIdx )
708
0
                return false;
709
0
        }
710
28.0k
        if(delta_idx_minus_1 == stRpsIdx)
711
0
            return false;
712
713
28.0k
        nal_u1_t delta_rps_sign = bs_read1( p_bs );
714
28.0k
        nal_ue_t abs_delta_rps_minus1 = bs_read_ue( p_bs );
715
28.0k
        unsigned RefRpsIdx = stRpsIdx - delta_idx_minus_1 - 1;
716
28.0k
        int deltaRps = ( 1 - ( delta_rps_sign << 1 ) ) * ( abs_delta_rps_minus1 + 1 );
717
28.0k
        VLC_UNUSED(deltaRps);
718
719
28.0k
        unsigned numDeltaPocs = p_sets[RefRpsIdx].num_delta_pocs;
720
28.0k
        p_sets[stRpsIdx].num_delta_pocs = 0;
721
146k
        for( unsigned j=0; j<= numDeltaPocs; j++ )
722
117k
        {
723
117k
            if( ! bs_read1( p_bs ) ) /* used_by_curr_pic_flag */
724
42.8k
            {
725
42.8k
                if( bs_read1( p_bs ) ) /* use_delta_flag */
726
12.1k
                    p_sets[stRpsIdx].num_delta_pocs++;
727
42.8k
            }
728
75.0k
            else
729
75.0k
                p_sets[stRpsIdx].num_delta_pocs++;
730
117k
        }
731
28.0k
    }
732
26.3k
    else
733
26.3k
    {
734
26.3k
        nal_ue_t num_negative_pics = bs_read_ue( p_bs );
735
26.3k
        nal_ue_t num_positive_pics = bs_read_ue( p_bs );
736
26.3k
        if( bs_error( p_bs ) )
737
1.40k
            return false;
738
611M
        for(unsigned int i=0; i<num_negative_pics; i++)
739
611M
        {
740
611M
            (void) bs_read_ue( p_bs ); /* delta_poc_s0_minus1 */
741
611M
            (void) bs_read1( p_bs ); /* used_by_current_pic_s0_flag */
742
611M
        }
743
1.37G
        for(unsigned int i=0; i<num_positive_pics; i++)
744
1.37G
        {
745
1.37G
            (void) bs_read_ue( p_bs ); /* delta_poc_s1_minus1 */
746
1.37G
            (void) bs_read1( p_bs ); /* used_by_current_pic_s1_flag */
747
1.37G
        }
748
24.9k
        p_sets[stRpsIdx].num_delta_pocs = num_positive_pics + num_negative_pics;
749
24.9k
    }
750
751
53.0k
    return !bs_error( p_bs );
752
54.4k
}
753
754
static bool hevc_parse_sequence_parameter_set_rbsp( bs_t *p_bs,
755
                                                    hevc_sequence_parameter_set_t *p_sps )
756
124k
{
757
124k
    p_sps->sps_video_parameter_set_id = bs_read( p_bs, 4 );
758
124k
    p_sps->sps_max_sub_layers_minus1 = bs_read( p_bs, 3 );
759
124k
    p_sps->sps_temporal_id_nesting_flag = bs_read1( p_bs );
760
124k
    if( !hevc_parse_profile_tier_level_rbsp( p_bs, true, p_sps->sps_max_sub_layers_minus1,
761
124k
                                            &p_sps->profile_tier_level ) )
762
59.4k
        return false;
763
764
64.6k
    if( bs_error( p_bs ) )
765
0
        return false;
766
767
64.6k
    p_sps->sps_seq_parameter_set_id = bs_read_ue( p_bs );
768
64.6k
    if( p_sps->sps_seq_parameter_set_id > HEVC_SPS_ID_MAX )
769
3.90k
        return false;
770
771
60.7k
    p_sps->chroma_format_idc = bs_read_ue( p_bs );
772
60.7k
    if( p_sps->chroma_format_idc == 3 )
773
7.81k
        p_sps->separate_colour_plane_flag = bs_read1( p_bs );
774
60.7k
    p_sps->pic_width_in_luma_samples = bs_read_ue( p_bs );
775
60.7k
    p_sps->pic_height_in_luma_samples = bs_read_ue( p_bs );
776
60.7k
    if( !p_sps->pic_width_in_luma_samples || !p_sps->pic_height_in_luma_samples )
777
9.88k
        return false;
778
779
50.8k
    p_sps->conformance_window_flag = bs_read1( p_bs );
780
50.8k
    if( p_sps->conformance_window_flag )
781
26.3k
    {
782
26.3k
        p_sps->conf_win.left_offset = bs_read_ue( p_bs );
783
26.3k
        p_sps->conf_win.right_offset = bs_read_ue( p_bs );
784
26.3k
        p_sps->conf_win.top_offset = bs_read_ue( p_bs );
785
26.3k
        p_sps->conf_win.bottom_offset = bs_read_ue( p_bs );
786
26.3k
    }
787
788
50.8k
    p_sps->bit_depth_luma_minus8 = bs_read_ue( p_bs );
789
50.8k
    p_sps->bit_depth_chroma_minus8 = bs_read_ue( p_bs );
790
50.8k
    p_sps->log2_max_pic_order_cnt_lsb_minus4 = bs_read_ue( p_bs );
791
792
50.8k
    p_sps->sps_sub_layer_ordering_info_present_flag = bs_read1( p_bs );
793
50.8k
    for( uint8_t i=(p_sps->sps_sub_layer_ordering_info_present_flag ? 0 : p_sps->sps_max_sub_layers_minus1);
794
110k
         i <= p_sps->sps_max_sub_layers_minus1; i++ )
795
59.7k
    {
796
59.7k
        p_sps->sps_max[i].dec_pic_buffering_minus1 = bs_read_ue( p_bs );
797
59.7k
        p_sps->sps_max[i].num_reorder_pics = bs_read_ue( p_bs );
798
59.7k
        p_sps->sps_max[i].latency_increase_plus1 = bs_read_ue( p_bs );
799
59.7k
    }
800
801
50.8k
    if( bs_eof( p_bs ) )
802
7.77k
        return false;
803
804
43.1k
    p_sps->log2_min_luma_coding_block_size_minus3 = bs_read_ue( p_bs );
805
43.1k
    p_sps->log2_diff_max_min_luma_coding_block_size = bs_read_ue( p_bs );
806
43.1k
    p_sps->log2_min_luma_transform_block_size_minus2 = bs_read_ue( p_bs );
807
43.1k
    p_sps->log2_diff_max_min_luma_transform_block_size = bs_read_ue( p_bs );
808
809
    /* parsing incomplete */
810
811
43.1k
    p_sps->max_transform_hierarchy_depth_inter = bs_read_ue( p_bs );
812
43.1k
    p_sps->max_transform_hierarchy_depth_intra = bs_read_ue( p_bs );
813
43.1k
    p_sps->scaling_list_enabled = bs_read1( p_bs );
814
43.1k
    if( p_sps->scaling_list_enabled )
815
10.4k
    {
816
10.4k
        p_sps->sps_scaling_list_data_present_flag = bs_read1( p_bs );
817
10.4k
        if( p_sps->sps_scaling_list_data_present_flag &&
818
10.4k
            ! hevc_parse_scaling_list_rbsp( p_bs ) )
819
0
        {
820
0
            return false;
821
0
        }
822
10.4k
    }
823
824
43.1k
    if( bs_error( p_bs ) )
825
8.00k
        return false;
826
827
35.1k
    p_sps->amp_enabled_flag = bs_read1( p_bs );
828
35.1k
    p_sps->sample_adaptive_offset_enabled_flag = bs_read1( p_bs );
829
830
35.1k
    p_sps->pcm_enabled_flag = bs_read1( p_bs );
831
35.1k
    if( p_sps->pcm_enabled_flag )
832
14.1k
    {
833
14.1k
        p_sps->pcm_sample_bit_depth_luma_minus1 = bs_read( p_bs, 4 );
834
14.1k
        p_sps->pcm_sample_bit_depth_chroma_minus1 = bs_read( p_bs, 4 );
835
14.1k
        p_sps->log2_min_pcm_luma_coding_block_size_minus3 = bs_read_ue( p_bs );
836
14.1k
        p_sps->log2_diff_max_min_pcm_luma_coding_block_size = bs_read_ue( p_bs );
837
14.1k
        p_sps->pcm_loop_filter_disabled_flag = bs_read1( p_bs );
838
14.1k
    }
839
840
35.1k
    p_sps->num_short_term_ref_pic_sets = bs_read_ue( p_bs );
841
35.1k
    if( p_sps->num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SET )
842
1.56k
        return false;
843
844
33.5k
    hevc_short_term_ref_pic_set_t sets[HEVC_MAX_SHORT_TERM_REF_PIC_SET];
845
33.5k
    memset(&sets, 0, sizeof(hevc_short_term_ref_pic_set_t) * HEVC_MAX_SHORT_TERM_REF_PIC_SET);
846
80.7k
    for( unsigned int i=0; i<p_sps->num_short_term_ref_pic_sets; i++ )
847
54.4k
    {
848
54.4k
        if( !hevc_parse_st_ref_pic_set( p_bs, i, p_sps->num_short_term_ref_pic_sets, sets ) )
849
7.22k
            return false;
850
54.4k
    }
851
852
26.3k
    p_sps->long_term_ref_pics_present_flag = bs_read1( p_bs );
853
26.3k
    if( p_sps->long_term_ref_pics_present_flag )
854
17.0k
    {
855
17.0k
        p_sps->num_long_term_ref_pics_sps = bs_read_ue( p_bs );
856
17.0k
        if( p_sps->num_long_term_ref_pics_sps > HEVC_MAX_LONG_TERM_REF_PIC_SET )
857
441
            return false;
858
43.3k
        for( unsigned int i=0; i< p_sps->num_long_term_ref_pics_sps; i++ )
859
26.8k
        {
860
             /* lt_ref_pic_poc_lsb_sps */
861
26.8k
            bs_skip( p_bs, p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4 );
862
             /* used_by_curr_pic_lt_sps_flag */
863
26.8k
            bs_skip( p_bs, 1 );
864
26.8k
        }
865
16.5k
    }
866
867
25.8k
    p_sps->sps_temporal_mvp_enabled_flag = bs_read1( p_bs );
868
25.8k
    p_sps->strong_intra_smoothing_enabled_flag = bs_read1( p_bs );
869
870
25.8k
    if( bs_error( p_bs ) )
871
2.14k
        return false;
872
873
23.7k
    p_sps->vui_parameters_present_flag = bs_read1( p_bs );
874
23.7k
    if( p_sps->vui_parameters_present_flag )
875
19.9k
    {
876
19.9k
        bs_t rollbackpoint = *p_bs;
877
19.9k
        if( !hevc_parse_vui_parameters_rbsp( p_bs, &p_sps->vui, false ) &&
878
19.9k
            p_sps->vui.default_display_window_flag &&
879
19.9k
            !bs_error( p_bs ) )
880
0
        {
881
            /* Broken MKV SPS vui bitstreams with missing display_window bits.
882
             * Forced to accept it since some decided to accept it...
883
             * see https://trac.ffmpeg.org/ticket/6644
884
             * Might break decoders since cropping & clock rate have totally
885
             * funky values when decoded properly */
886
0
            *p_bs = rollbackpoint;
887
0
            memset( &p_sps->vui, 0, sizeof(p_sps->vui) );
888
0
            if( !hevc_parse_vui_parameters_rbsp( p_bs, &p_sps->vui, true ) )
889
0
                return false;
890
0
        }
891
19.9k
    }
892
893
    /* incomplete */
894
895
23.7k
    return !bs_error( p_bs );
896
23.7k
}
897
898
void hevc_rbsp_release_sps( hevc_sequence_parameter_set_t *p_sps )
899
124k
{
900
124k
    free( p_sps );
901
124k
}
902
903
IMPL_hevc_generic_decode( hevc_decode_sps, hevc_sequence_parameter_set_t,
904
                          hevc_parse_sequence_parameter_set_rbsp, hevc_rbsp_release_sps )
905
906
static bool hevc_parse_pic_parameter_set_rbsp( bs_t *p_bs,
907
                                               hevc_picture_parameter_set_t *p_pps )
908
91.8k
{
909
91.8k
    if( bs_eof( p_bs ) )
910
0
        return false;
911
91.8k
    p_pps->pps_pic_parameter_set_id = bs_read_ue( p_bs );
912
91.8k
    if( p_pps->pps_pic_parameter_set_id > HEVC_PPS_ID_MAX )
913
3.91k
        return false;
914
87.9k
    p_pps->pps_seq_parameter_set_id = bs_read_ue( p_bs );
915
87.9k
    if( p_pps->pps_seq_parameter_set_id > HEVC_SPS_ID_MAX )
916
9.99k
        return false;
917
77.9k
    p_pps->dependent_slice_segments_enabled_flag = bs_read1( p_bs );
918
77.9k
    p_pps->output_flag_present_flag = bs_read1( p_bs );
919
77.9k
    p_pps->num_extra_slice_header_bits = bs_read( p_bs, 3 );
920
77.9k
    p_pps->sign_data_hiding_enabled_flag = bs_read1( p_bs );
921
77.9k
    p_pps->cabac_init_present_flag = bs_read1( p_bs );
922
923
77.9k
    p_pps->num_ref_idx_l0_default_active_minus1 = bs_read_ue( p_bs );
924
77.9k
    p_pps->num_ref_idx_l1_default_active_minus1 = bs_read_ue( p_bs );
925
926
77.9k
    p_pps->init_qp_minus26 = bs_read_se( p_bs );
927
77.9k
    p_pps->constrained_intra_pred_flag = bs_read1( p_bs );
928
77.9k
    p_pps->transform_skip_enabled_flag = bs_read1( p_bs );
929
77.9k
    p_pps->cu_qp_delta_enabled_flag = bs_read1( p_bs );
930
77.9k
    if( p_pps->cu_qp_delta_enabled_flag )
931
37.4k
        p_pps->diff_cu_qp_delta_depth = bs_read_ue( p_bs );
932
933
77.9k
    if( bs_error( p_bs ) )
934
8.65k
        return false;
935
936
69.2k
    p_pps->pps_cb_qp_offset = bs_read_se( p_bs );
937
69.2k
    p_pps->pps_cr_qp_offset = bs_read_se( p_bs );
938
69.2k
    p_pps->pic_slice_level_chroma_qp_offsets_present_flag = bs_read1( p_bs );
939
69.2k
    p_pps->weighted_pred_flag = bs_read1( p_bs );
940
69.2k
    p_pps->weighted_bipred_flag = bs_read1( p_bs );
941
69.2k
    p_pps->transquant_bypass_enable_flag = bs_read1( p_bs );
942
69.2k
    p_pps->tiles_enabled_flag = bs_read1( p_bs );
943
69.2k
    p_pps->entropy_coding_sync_enabled_flag = bs_read1( p_bs );
944
945
69.2k
    if( p_pps->tiles_enabled_flag )
946
21.0k
    {
947
21.0k
        p_pps->num_tile_columns_minus1 = bs_read_ue( p_bs ); /* TODO: validate max col/row values */
948
21.0k
        p_pps->num_tile_rows_minus1 = bs_read_ue( p_bs );    /*       against sps PicWidthInCtbsY */
949
21.0k
        p_pps->uniform_spacing_flag = bs_read1( p_bs );
950
21.0k
        if( !p_pps->uniform_spacing_flag )
951
8.75k
        {
952
1.47G
            for( unsigned i=0; i< p_pps->num_tile_columns_minus1; i++ )
953
1.47G
                (void) bs_read_ue( p_bs );
954
1.20G
            for( unsigned i=0; i< p_pps->num_tile_rows_minus1; i++ )
955
1.20G
                (void) bs_read_ue( p_bs );
956
8.75k
        }
957
21.0k
        p_pps->loop_filter_across_tiles_enabled_flag = bs_read1( p_bs );
958
21.0k
        if( bs_error( p_bs ) )
959
4.49k
            return false;
960
21.0k
    }
961
962
64.8k
    p_pps->pps_loop_filter_across_slices_enabled_flag = bs_read1( p_bs );
963
64.8k
    p_pps->deblocking_filter_control_present_flag = bs_read1( p_bs );
964
64.8k
    if( p_pps->deblocking_filter_control_present_flag )
965
17.6k
    {
966
17.6k
        p_pps->deblocking_filter_override_enabled_flag = bs_read1( p_bs );
967
17.6k
        p_pps->pps_deblocking_filter_disabled_flag = bs_read1( p_bs );
968
17.6k
        if( !p_pps->pps_deblocking_filter_disabled_flag )
969
11.4k
        {
970
11.4k
            p_pps->pps_beta_offset_div2 = bs_read_se( p_bs );
971
11.4k
            p_pps->pps_tc_offset_div2 = bs_read_se( p_bs );
972
11.4k
        }
973
17.6k
    }
974
975
64.8k
    p_pps->scaling_list_data_present_flag = bs_read1( p_bs );
976
64.8k
    if( p_pps->scaling_list_data_present_flag && !hevc_parse_scaling_list_rbsp( p_bs ) )
977
0
        return false;
978
979
64.8k
    p_pps->lists_modification_present_flag = bs_read1( p_bs );
980
64.8k
    p_pps->log2_parallel_merge_level_minus2 = bs_read_ue( p_bs );
981
64.8k
    p_pps->slice_header_extension_present_flag = bs_read1( p_bs );
982
983
64.8k
    p_pps->pps_extension_present_flag = bs_read1( p_bs );
984
64.8k
    if( p_pps->pps_extension_present_flag )
985
10.7k
    {
986
10.7k
        p_pps->pps_range_extension_flag = bs_read1( p_bs );
987
10.7k
        p_pps->pps_multilayer_extension_flag = bs_read1( p_bs );
988
10.7k
        p_pps->pps_3d_extension_flag = bs_read1( p_bs );
989
10.7k
        p_pps->pps_extension_5bits = bs_read( p_bs, 5 );
990
10.7k
    }
991
992
64.8k
    return !bs_error( p_bs );
993
64.8k
}
994
995
void hevc_rbsp_release_pps( hevc_picture_parameter_set_t *p_pps )
996
92.2k
{
997
92.2k
    free( p_pps );
998
92.2k
}
999
1000
IMPL_hevc_generic_decode( hevc_decode_pps, hevc_picture_parameter_set_t,
1001
                          hevc_parse_pic_parameter_set_rbsp, hevc_rbsp_release_pps )
1002
1003
uint8_t hevc_get_sps_vps_id( const hevc_sequence_parameter_set_t *p_sps )
1004
65.4k
{
1005
65.4k
    return p_sps->sps_video_parameter_set_id;
1006
65.4k
}
1007
1008
uint8_t hevc_get_pps_sps_id( const hevc_picture_parameter_set_t *p_pps )
1009
76.9k
{
1010
76.9k
    return p_pps->pps_seq_parameter_set_id;
1011
76.9k
}
1012
1013
uint8_t hevc_get_slice_pps_id( const hevc_slice_segment_header_t *p_slice )
1014
8.55k
{
1015
8.55k
    return p_slice->slice_pic_parameter_set_id;
1016
8.55k
}
1017
1018
bool hevc_get_slice_no_output_of_prior_pics_flag( const hevc_slice_segment_header_t *p_slice )
1019
0
{
1020
0
    return p_slice->no_output_of_prior_pics_flag;
1021
0
}
1022
1023
bool hevc_get_sps_profile_tier_level( const hevc_sequence_parameter_set_t *p_sps,
1024
                                      uint8_t *pi_profile, uint8_t *pi_level)
1025
8.55k
{
1026
8.55k
    if(p_sps->profile_tier_level.general.profile_idc)
1027
7.04k
    {
1028
7.04k
        *pi_profile = p_sps->profile_tier_level.general.profile_idc;
1029
7.04k
        *pi_level = p_sps->profile_tier_level.general_level_idc;
1030
7.04k
        return true;
1031
7.04k
    }
1032
1.51k
    return false;
1033
8.55k
}
1034
1035
bool hevc_get_picture_size( const hevc_sequence_parameter_set_t *p_sps,
1036
                            unsigned *p_ox, unsigned *p_oy,
1037
                            unsigned *p_w, unsigned *p_h,
1038
                            unsigned *p_vw, unsigned *p_vh )
1039
8.55k
{
1040
8.55k
    return h26x_get_picture_size( p_sps->chroma_format_idc,
1041
8.55k
                                  p_sps->pic_width_in_luma_samples,
1042
8.55k
                                  p_sps->pic_height_in_luma_samples,
1043
8.55k
                                  &p_sps->conf_win,
1044
8.55k
                                  p_ox, p_oy, p_w, p_h, p_vw, p_vh );
1045
8.55k
}
1046
1047
void hevc_get_dpb_values( const hevc_sequence_parameter_set_t *p_sps, uint8_t *max_num_reorder_pics,
1048
                          uint8_t *max_latency_pics, uint8_t *max_dec_pic_buffering )
1049
0
{
1050
0
    *max_num_reorder_pics = p_sps->sps_max[p_sps->sps_max_sub_layers_minus1/* HighestTid */].num_reorder_pics;
1051
0
    *max_latency_pics = p_sps->sps_max[p_sps->sps_max_sub_layers_minus1/* HighestTid */].latency_increase_plus1;
1052
0
    if(*max_latency_pics != 0)
1053
0
        *max_latency_pics = *max_num_reorder_pics + *max_latency_pics - 1;
1054
0
    *max_dec_pic_buffering = 1 +
1055
0
        p_sps->sps_max[p_sps->sps_max_sub_layers_minus1/* HighestTid */].dec_pic_buffering_minus1;
1056
    /*
1057
    When sps_max_dec_pic_buffering_minus1[ TemporalId ] is equal to 0, the value of TwoVersionsOfCurrDecPicFlag
1058
                                                                         shall be equal to 0.
1059
1060
    pps_curr_pic_ref_enabled_flag &&
1061
        ( sample_adaptive_offset_enabled_flag | | !pps_deblocking_filter_disabled_flag | |
1062
                                      deblocking_filter_override_enabled_flag );
1063
    */
1064
0
}
1065
1066
static bool hevc_get_picture_CtbsYsize( const hevc_sequence_parameter_set_t *p_sps, unsigned *p_w, unsigned *p_h )
1067
0
{
1068
0
    const unsigned int MinCbLog2SizeY = p_sps->log2_min_luma_coding_block_size_minus3 + 3;
1069
0
    const unsigned int CtbLog2SizeY = MinCbLog2SizeY + p_sps->log2_diff_max_min_luma_coding_block_size;
1070
0
    if( CtbLog2SizeY > 31 )
1071
0
        return false;
1072
0
    const unsigned int CtbSizeY = 1 << CtbLog2SizeY;
1073
0
    *p_w = (p_sps->pic_width_in_luma_samples - 1) / CtbSizeY + 1;
1074
0
    *p_h = (p_sps->pic_height_in_luma_samples - 1) / CtbSizeY + 1;
1075
0
    return true;
1076
0
}
1077
1078
bool hevc_get_frame_rate( const hevc_sequence_parameter_set_t *p_sps,
1079
                          const hevc_video_parameter_set_t *p_vps,
1080
                          unsigned *pi_num, unsigned *pi_den )
1081
1.11k
{
1082
1.11k
    if( p_sps->vui_parameters_present_flag && p_sps->vui.vui_timing_info_present_flag )
1083
365
    {
1084
365
        *pi_den = p_sps->vui.timing.vui_num_units_in_tick;
1085
365
        *pi_num = p_sps->vui.timing.vui_time_scale;
1086
365
        return (*pi_den && *pi_num);
1087
365
    }
1088
750
    else if( p_vps && p_vps->vps_timing_info_present_flag )
1089
413
    {
1090
413
        *pi_den = p_vps->vps_num_units_in_tick;
1091
413
        *pi_num = p_vps->vps_time_scale;
1092
413
        return (*pi_den && *pi_num);
1093
413
    }
1094
337
    return false;
1095
1.11k
}
1096
1097
bool hevc_get_aspect_ratio( const hevc_sequence_parameter_set_t *p_sps,
1098
                            unsigned *num, unsigned *den )
1099
8.55k
{
1100
8.55k
    if( !p_sps->vui_parameters_present_flag )
1101
3.82k
        return false;
1102
4.73k
    return h26x_get_aspect_ratio( &p_sps->vui.ar, num, den );
1103
8.55k
}
1104
1105
bool hevc_get_chroma_luma( const hevc_sequence_parameter_set_t *p_sps, uint8_t *pi_chroma_format,
1106
                           uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma )
1107
0
{
1108
0
    *pi_chroma_format = p_sps->chroma_format_idc;
1109
0
    *pi_depth_luma = p_sps->bit_depth_chroma_minus8 + 8;
1110
0
    *pi_depth_chroma = p_sps->bit_depth_chroma_minus8 + 8;
1111
0
    return true;
1112
0
}
1113
1114
bool hevc_get_colorimetry( const hevc_sequence_parameter_set_t *p_sps,
1115
                           video_color_primaries_t *p_primaries,
1116
                           video_transfer_func_t *p_transfer,
1117
                           video_color_space_t *p_colorspace,
1118
                           video_color_range_t *p_full_range )
1119
8.55k
{
1120
8.55k
    if( !p_sps->vui_parameters_present_flag )
1121
3.82k
        return false;
1122
4.73k
    return h26x_get_colorimetry( &p_sps->vui.vs.colour,
1123
4.73k
                                p_primaries, p_transfer, p_colorspace, p_full_range );
1124
8.55k
}
1125
1126
static bool hevc_parse_slice_segment_header_rbsp( bs_t *p_bs,
1127
                                                  pf_get_matchedxps get_matchedxps,
1128
                                                  void *priv,
1129
                                                  hevc_slice_segment_header_t *p_sl )
1130
321k
{
1131
321k
    hevc_sequence_parameter_set_t *p_sps;
1132
321k
    hevc_picture_parameter_set_t *p_pps;
1133
321k
    hevc_video_parameter_set_t *p_vps;
1134
1135
321k
    if( bs_eof( p_bs ) )
1136
0
        return false;
1137
1138
321k
    p_sl->first_slice_segment_in_pic_flag = bs_read1( p_bs );
1139
321k
    if( p_sl->nal_type >= HEVC_NAL_BLA_W_LP && p_sl->nal_type <= HEVC_NAL_IRAP_VCL23 )
1140
269k
        p_sl->no_output_of_prior_pics_flag = bs_read1( p_bs );
1141
321k
    p_sl->slice_pic_parameter_set_id = bs_read_ue( p_bs );
1142
321k
    if( p_sl->slice_pic_parameter_set_id > HEVC_PPS_ID_MAX )
1143
13.6k
        return false;
1144
1145
307k
    if( bs_error( p_bs ) )
1146
247k
        return false;
1147
1148
59.8k
    get_matchedxps( p_sl->slice_pic_parameter_set_id, priv, &p_pps, &p_sps, &p_vps );
1149
59.8k
    if(!p_sps || !p_pps)
1150
11.8k
        return false;
1151
1152
47.9k
    if( !p_sl->first_slice_segment_in_pic_flag )
1153
0
    {
1154
0
        if( p_pps->dependent_slice_segments_enabled_flag )
1155
0
            p_sl->dependent_slice_segment_flag = bs_read1( p_bs );
1156
1157
0
        unsigned w, h;
1158
0
        if( !hevc_get_picture_CtbsYsize( p_sps, &w, &h ) )
1159
0
            return false;
1160
1161
0
        (void) bs_read( p_bs, stdc_bit_width( w * h - 1 ) ); /* slice_segment_address */
1162
0
    }
1163
1164
47.9k
    if( !p_sl->dependent_slice_segment_flag )
1165
47.9k
    {
1166
47.9k
        unsigned i=0;
1167
47.9k
        if( p_pps->num_extra_slice_header_bits > i )
1168
39.8k
        {
1169
39.8k
            i++;
1170
39.8k
            bs_skip( p_bs, 1 ); /* discardable_flag */
1171
39.8k
        }
1172
1173
47.9k
        if( p_pps->num_extra_slice_header_bits > i )
1174
39.5k
        {
1175
39.5k
            i++;
1176
39.5k
            bs_skip( p_bs, 1 ); /* cross_layer_bla_flag */
1177
39.5k
        }
1178
1179
47.9k
        if( i < p_pps->num_extra_slice_header_bits )
1180
38.7k
           bs_skip( p_bs, p_pps->num_extra_slice_header_bits - i );
1181
1182
47.9k
        p_sl->slice_type = bs_read_ue( p_bs );
1183
47.9k
        if( p_sl->slice_type > HEVC_SLICE_TYPE_I )
1184
1.38k
            return false;
1185
1186
46.6k
        if( p_pps->output_flag_present_flag )
1187
36.5k
            p_sl->pic_output_flag = bs_read1( p_bs );
1188
10.1k
        else
1189
10.1k
            p_sl->pic_output_flag = 1;
1190
46.6k
    }
1191
1192
46.6k
    if( p_sps->separate_colour_plane_flag )
1193
2.08k
        bs_skip( p_bs, 2 ); /* colour_plane_id */
1194
1195
46.6k
    if( p_sl->nal_type != HEVC_NAL_IDR_W_RADL && p_sl->nal_type != HEVC_NAL_IDR_N_LP )
1196
44.0k
        p_sl->pic_order_cnt_lsb = bs_read( p_bs, p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4 );
1197
2.52k
    else
1198
2.52k
        p_sl->pic_order_cnt_lsb = 0;
1199
1200
46.6k
    return !bs_error( p_bs );
1201
47.9k
}
1202
1203
void hevc_rbsp_release_slice_header( hevc_slice_segment_header_t *p_sh )
1204
325k
{
1205
325k
    free( p_sh );
1206
325k
}
1207
1208
hevc_slice_segment_header_t * hevc_decode_slice_header( const uint8_t *p_buf, size_t i_buf, bool b_escaped,
1209
                                                        pf_get_matchedxps get_matchedxps, void *priv )
1210
325k
{
1211
325k
    hevc_slice_segment_header_t *p_sh = calloc(1, sizeof(hevc_slice_segment_header_t));
1212
325k
    if(likely(p_sh))
1213
325k
    {
1214
325k
        bs_t bs;
1215
325k
        struct hxxx_bsfw_ep3b_ctx_s bsctx;
1216
325k
        if( b_escaped )
1217
325k
        {
1218
325k
            hxxx_bsfw_ep3b_ctx_init( &bsctx );
1219
325k
            bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );
1220
325k
        }
1221
0
        else bs_init( &bs, p_buf, i_buf );
1222
325k
        bs_skip( &bs, 1 );
1223
325k
        p_sh->nal_type = bs_read( &bs, 6 );
1224
325k
        p_sh->nuh_layer_id = bs_read( &bs, 6 );
1225
325k
        p_sh->temporal_id_plus1 = bs_read( &bs, 3 );
1226
325k
        if( p_sh->nuh_layer_id > 62 || p_sh->temporal_id_plus1 == 0 ||
1227
325k
           !hevc_parse_slice_segment_header_rbsp( &bs, get_matchedxps, priv, p_sh ) )
1228
312k
        {
1229
312k
            hevc_rbsp_release_slice_header( p_sh );
1230
312k
            p_sh = NULL;
1231
312k
        }
1232
325k
    }
1233
325k
    return p_sh;
1234
325k
}
1235
1236
bool hevc_get_slice_type( const hevc_slice_segment_header_t *p_sli, enum hevc_slice_type_e *pi_type )
1237
7.18k
{
1238
7.18k
    if( !p_sli->dependent_slice_segment_flag )
1239
7.18k
    {
1240
7.18k
        *pi_type = p_sli->slice_type;
1241
7.18k
        return true;
1242
7.18k
    }
1243
0
    return false;
1244
7.18k
}
1245
1246
bool hevc_get_slice_pic_output( const hevc_slice_segment_header_t *p_sli )
1247
0
{
1248
0
    return p_sli->pic_output_flag;
1249
0
}
1250
1251
bool hevc_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
1252
                            uint8_t *pi_level, uint8_t *pi_nal_length_size)
1253
0
{
1254
0
    const uint8_t *p = (const uint8_t*)p_fmt->p_extra;
1255
0
    if(p_fmt->i_extra < 23 || p[0] != 1)
1256
0
        return false;
1257
1258
    /* HEVCDecoderConfigurationRecord */
1259
0
    if(pi_profile)
1260
0
        *pi_profile = p[1] & 0x1F;
1261
1262
0
    if(pi_level)
1263
0
        *pi_level = p[12];
1264
1265
0
    if (pi_nal_length_size)
1266
0
        *pi_nal_length_size = 1 + (p[21]&0x03);
1267
1268
0
    return true;
1269
0
}
1270
1271
static unsigned hevc_make_indication( const hevc_inner_profile_tier_level_t *p )
1272
0
{
1273
0
    uint8_t flags[] =
1274
0
    {
1275
0
        p->idc4to7.max_14bit_constraint_flag,
1276
0
        p->idc4to7.max_12bit_constraint_flag,
1277
0
        p->idc4to7.max_10bit_constraint_flag,
1278
0
        p->idc4to7.max_8bit_constraint_flag,
1279
0
        p->idc4to7.max_422chroma_constraint_flag,
1280
0
        p->idc4to7.max_420chroma_constraint_flag,
1281
0
        p->idc4to7.max_monochrome_constraint_flag,
1282
0
        p->idc4to7.intra_constraint_flag,
1283
0
        p->idc4to7.one_picture_only_constraint_flag,
1284
0
        p->idc4to7.lower_bit_rate_constraint_flag,
1285
0
    };
1286
0
    unsigned indication = 0;
1287
0
    for( size_t i=0; i<ARRAY_SIZE(flags); i++ )
1288
0
    {
1289
0
        if( flags[i] )
1290
0
            indication |= (1 << (ARRAY_SIZE(flags) - 1 - i));
1291
0
    }
1292
0
    return indication;
1293
0
}
1294
1295
enum vlc_hevc_profile_e hevc_get_vlc_profile( const hevc_sequence_parameter_set_t *p_sps )
1296
0
{
1297
0
    unsigned indication = 0;
1298
0
    enum hevc_general_profile_idc_e profile = p_sps->profile_tier_level.general.profile_idc;
1299
0
    switch( profile )
1300
0
    {
1301
0
        case HEVC_PROFILE_IDC_REXT:
1302
0
            indication = hevc_make_indication( &p_sps->profile_tier_level.general ) & 0x1FF;
1303
0
            break;
1304
0
        case HEVC_PROFILE_IDC_HIGH_THROUGHPUT:
1305
0
        case HEVC_PROFILE_IDC_SCREEN_EXTENDED:
1306
0
            indication = hevc_make_indication( &p_sps->profile_tier_level.general );
1307
0
            break;
1308
0
        default:
1309
0
            break;
1310
0
    }
1311
1312
    /* all intras have insignifiant lowest bit */
1313
0
    if( p_sps->profile_tier_level.general.idc4to7.intra_constraint_flag )
1314
0
        indication &= ~1;
1315
1316
0
    return (indication << HEVC_INDICATION_SHIFT) | profile;
1317
0
}
1318
1319
/*
1320
 * HEVCDecoderConfigurationRecord operations
1321
 */
1322
1323
static void hevc_dcr_params_from_vps( const uint8_t * p_buffer, size_t i_buffer,
1324
                                      struct hevc_dcr_values *p_values )
1325
0
{
1326
0
    if( i_buffer < 19 )
1327
0
        return;
1328
1329
0
    bs_t bs;
1330
0
    struct hxxx_bsfw_ep3b_ctx_s bsctx;
1331
0
    hxxx_bsfw_ep3b_ctx_init( &bsctx );
1332
0
    bs_init_custom( &bs, p_buffer, i_buffer, &hxxx_bsfw_ep3b_callbacks, &bsctx );
1333
1334
    /* first two bytes are the NAL header, 3rd and 4th are:
1335
        vps_video_parameter_set_id(4)
1336
        vps_reserved_3_2bis(2)
1337
        vps_max_layers_minus1(6)
1338
        vps_max_sub_layers_minus1(3)
1339
        vps_temporal_id_nesting_flags
1340
    */
1341
0
    bs_skip( &bs, 16 + 4 + 2 + 6 );
1342
0
    p_values->i_numTemporalLayer = bs_read( &bs, 3 ) + 1;
1343
0
    p_values->b_temporalIdNested = bs_read1( &bs );
1344
1345
    /* 5th & 6th are reserved 0xffff */
1346
0
    bs_skip( &bs, 16 );
1347
    /* copy the first 12 bytes of profile tier */
1348
0
    for( unsigned i=0; i<12; i++ )
1349
0
        p_values->general_configuration[i] = bs_read( &bs, 8 );
1350
0
}
1351
1352
0
#define HEVC_DCR_ADD_NALS(type, count, buffers, sizes) \
1353
0
for (uint8_t i = 0; i < count; i++) \
1354
0
{ \
1355
0
    if( i ==0 ) \
1356
0
    { \
1357
0
        *p++ = (type | (b_completeness ? 0x80 : 0)); \
1358
0
        SetWBE( p, count ); p += 2; \
1359
0
    } \
1360
0
    SetWBE( p, sizes[i]); p += 2; \
1361
0
    memcpy( p, buffers[i], sizes[i] ); p += sizes[i];\
1362
0
}
1363
1364
0
#define HEVC_DCR_ADD_SIZES(count, sizes) \
1365
0
if(count > 0) \
1366
0
{\
1367
0
    i_total_size += 3;\
1368
0
    for(uint8_t i=0; i<count; i++)\
1369
0
        i_total_size += 2 + sizes[i];\
1370
0
}
1371
1372
/* Generate HEVCDecoderConfiguration iso/iec 14496-15 3rd edition */
1373
uint8_t * hevc_create_dcr( const struct hevc_dcr_params *p_params,
1374
                           uint8_t i_nal_length_size,
1375
                           bool b_completeness, size_t *pi_size )
1376
0
{
1377
0
    *pi_size = 0;
1378
1379
0
    if( i_nal_length_size != 1 && i_nal_length_size != 2 && i_nal_length_size != 4 )
1380
0
        return NULL;
1381
1382
0
    struct hevc_dcr_values values =
1383
0
    {
1384
0
        .general_configuration = {0},
1385
0
        .i_numTemporalLayer = 0,
1386
0
        .i_chroma_idc = 1,
1387
0
        .i_bit_depth_luma_minus8 = 0,
1388
0
        .i_bit_depth_chroma_minus8 = 0,
1389
0
        .b_temporalIdNested = false,
1390
0
    };
1391
1392
0
    if( p_params->p_values != NULL )
1393
0
    {
1394
0
        values = *p_params->p_values;
1395
0
    }
1396
0
    else
1397
0
    {
1398
0
        if( p_params->i_vps_count == 0 || p_params->i_sps_count == 0 )
1399
0
           return NULL; /* required to extract info */
1400
1401
0
        hevc_dcr_params_from_vps( p_params->p_vps[0], p_params->rgi_vps[0], &values );
1402
1403
0
        hevc_sequence_parameter_set_t *p_sps =
1404
0
                hevc_decode_sps( p_params->p_sps[0], p_params->rgi_sps[0], true );
1405
0
        if( p_sps )
1406
0
        {
1407
0
            values.i_chroma_idc = p_sps->chroma_format_idc;
1408
0
            values.i_bit_depth_chroma_minus8 = p_sps->bit_depth_chroma_minus8;
1409
0
            values.i_bit_depth_luma_minus8 = p_sps->bit_depth_luma_minus8;
1410
0
            hevc_rbsp_release_sps( p_sps );
1411
0
        }
1412
0
    }
1413
1414
0
    size_t i_total_size = 1+12+2+4+2+2;
1415
0
    HEVC_DCR_ADD_SIZES(p_params->i_vps_count, p_params->rgi_vps);
1416
0
    HEVC_DCR_ADD_SIZES(p_params->i_sps_count, p_params->rgi_sps);
1417
0
    HEVC_DCR_ADD_SIZES(p_params->i_pps_count, p_params->rgi_pps);
1418
0
    HEVC_DCR_ADD_SIZES(p_params->i_seipref_count, p_params->rgi_seipref);
1419
0
    HEVC_DCR_ADD_SIZES(p_params->i_seisuff_count, p_params->rgi_seisuff);
1420
1421
0
    uint8_t *p_data = malloc( i_total_size );
1422
0
    if( p_data == NULL )
1423
0
        return NULL;
1424
1425
0
    *pi_size = i_total_size;
1426
0
    uint8_t *p = p_data;
1427
1428
    /* version */
1429
0
    *p++ = 0x01;
1430
0
    memcpy( p, values.general_configuration, 12 ); p += 12;
1431
    /* Don't set min spatial segmentation */
1432
0
    SetWBE( p, 0xF000 ); p += 2;
1433
    /* Don't set parallelism type since segmentation isn't set */
1434
0
    *p++ = 0xFC;
1435
0
    *p++ = (0xFC | (values.i_chroma_idc & 0x03));
1436
0
    *p++ = (0xF8 | (values.i_bit_depth_luma_minus8 & 0x07));
1437
0
    *p++ = (0xF8 | (values.i_bit_depth_chroma_minus8 & 0x07));
1438
1439
    /* Don't set framerate */
1440
0
    SetWBE( p, 0x0000); p += 2;
1441
    /* Force NAL size of 4 bytes that replace the startcode */
1442
0
    *p++ = ( ((values.i_numTemporalLayer & 0x07) << 3) |
1443
0
              (values.b_temporalIdNested << 2) |
1444
0
              (i_nal_length_size - 1) );
1445
    /* total number of arrays */
1446
0
    *p++ = !!p_params->i_vps_count + !!p_params->i_sps_count +
1447
0
           !!p_params->i_pps_count + !!p_params->i_seipref_count +
1448
0
           !!p_params->i_seisuff_count;
1449
1450
    /* Write NAL arrays */
1451
0
    HEVC_DCR_ADD_NALS(HEVC_NAL_VPS, p_params->i_vps_count,
1452
0
                      p_params->p_vps, p_params->rgi_vps);
1453
0
    HEVC_DCR_ADD_NALS(HEVC_NAL_SPS, p_params->i_sps_count,
1454
0
                      p_params->p_sps, p_params->rgi_sps);
1455
0
    HEVC_DCR_ADD_NALS(HEVC_NAL_PPS, p_params->i_pps_count,
1456
0
                      p_params->p_pps, p_params->rgi_pps);
1457
0
    HEVC_DCR_ADD_NALS(HEVC_NAL_PREF_SEI, p_params->i_seipref_count,
1458
0
                      p_params->p_seipref, p_params->rgi_seipref);
1459
0
    HEVC_DCR_ADD_NALS(HEVC_NAL_SUFF_SEI, p_params->i_seisuff_count,
1460
0
                      p_params->p_seisuff, p_params->rgi_seisuff);
1461
1462
0
    return p_data;
1463
0
}
1464
1465
#undef HEVC_DCR_ADD_NALS
1466
#undef HEVC_DCR_ADD_SIZES
1467
1468
bool hevc_NAL_IsIRAP( uint8_t i_nal_type )
1469
0
{
1470
0
    return ( i_nal_type >= HEVC_NAL_BLA_W_LP &&
1471
0
             i_nal_type <= HEVC_NAL_IRAP_VCL23 );
1472
0
}
1473
1474
bool hevc_get_IRAPNoRaslOutputFlag( uint8_t i_nal_type,
1475
                                    const hevc_poc_ctx_t *p_ctx )
1476
0
{
1477
    /* if( IRAP ) NoRaslOutputFlag = first || IDR || BLA || after(EOSNAL) */
1478
0
    return ( p_ctx->first_picture ||
1479
0
             i_nal_type == HEVC_NAL_IDR_N_LP ||
1480
0
             i_nal_type == HEVC_NAL_IDR_W_RADL ||
1481
0
             i_nal_type == HEVC_NAL_BLA_W_LP ||
1482
0
             i_nal_type == HEVC_NAL_BLA_W_RADL ||
1483
0
             i_nal_type == HEVC_NAL_BLA_N_LP ||
1484
0
             p_ctx->HandleCraAsBlaFlag );
1485
0
}
1486
1487
/*
1488
 * 8.3.1 Decoding process for POC
1489
 */
1490
int hevc_compute_picture_order_count( const hevc_sequence_parameter_set_t *p_sps,
1491
                                       const hevc_slice_segment_header_t *p_slice,
1492
                                       hevc_poc_ctx_t *p_ctx )
1493
0
{
1494
0
    struct
1495
0
    {
1496
0
        int lsb;
1497
0
        int msb;
1498
0
    } prevPicOrderCnt;
1499
0
    int pocMSB;
1500
0
    const bool IsIRAP = hevc_NAL_IsIRAP( p_slice->nal_type );
1501
0
    const bool NoRaslOutputFlag =
1502
0
        IsIRAP ? hevc_get_IRAPNoRaslOutputFlag( p_slice->nal_type, p_ctx ) : false;
1503
1504
#ifdef HEVC_POC_DEBUG
1505
    printf("slice lsb=%"PRIu32" irap=%d norasl=%d tid=%d msb=%d lsb=%d",
1506
           p_slice->pic_order_cnt_lsb,
1507
           IsIRAP, NoRaslOutputFlag, p_slice->temporal_id_plus1,
1508
           p_ctx->prevTid0PicOrderCnt.msb, p_ctx->prevTid0PicOrderCnt.lsb);
1509
#endif
1510
1511
0
    if( p_slice->nal_type == HEVC_NAL_IDR_N_LP ||
1512
0
        p_slice->nal_type == HEVC_NAL_IDR_W_RADL )
1513
0
    {
1514
0
        prevPicOrderCnt.msb = 0;
1515
0
        prevPicOrderCnt.lsb = 0;
1516
0
    }
1517
    /* Not an IRAP with NoRaslOutputFlag == 1 */
1518
0
    else if( !IsIRAP || !NoRaslOutputFlag )
1519
0
    {
1520
0
        prevPicOrderCnt.msb = p_ctx->prevTid0PicOrderCnt.msb;
1521
0
        prevPicOrderCnt.lsb = p_ctx->prevTid0PicOrderCnt.lsb;
1522
0
    }
1523
1524
0
    if( IsIRAP && NoRaslOutputFlag )
1525
0
    {
1526
0
        pocMSB = 0;
1527
0
    }
1528
0
    else
1529
0
    {
1530
0
        const unsigned maxPocLSB = 1U << (p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1531
0
        pocMSB = prevPicOrderCnt.msb;
1532
0
        int64_t orderDiff = (int64_t)p_slice->pic_order_cnt_lsb - prevPicOrderCnt.lsb;
1533
0
        if( orderDiff < 0 && -orderDiff >= maxPocLSB / 2 )
1534
0
            pocMSB += maxPocLSB;
1535
0
        else if( orderDiff > maxPocLSB / 2 )
1536
0
            pocMSB -= maxPocLSB;
1537
0
    }
1538
1539
    /* Set prevTid0Pic for next pic */
1540
0
    if( p_slice->temporal_id_plus1 == 1 &&
1541
0
       !( ( p_slice->nal_type <= HEVC_NAL_RSV_VCL_N14 && p_slice->nal_type % 2 == 0 /* SLNR */ ) ||
1542
0
          ( p_slice->nal_type >= HEVC_NAL_RADL_N && p_slice->nal_type <= HEVC_NAL_RASL_R ) /* RADL or RASL */ ) )
1543
0
    {
1544
0
        p_ctx->prevTid0PicOrderCnt.msb = pocMSB;
1545
0
        p_ctx->prevTid0PicOrderCnt.lsb = p_slice->pic_order_cnt_lsb;
1546
0
    }
1547
1548
0
    p_ctx->first_picture = false;
1549
1550
#ifdef HEVC_POC_DEBUG
1551
    printf(" POC=%"PRIu32"\n", pocMSB + p_slice->pic_order_cnt_lsb);
1552
#endif
1553
1554
0
    return pocMSB + p_slice->pic_order_cnt_lsb;
1555
0
}
1556
1557
struct hevc_sei_pic_timing_t
1558
{
1559
    nal_u4_t pic_struct;
1560
    nal_u2_t source_scan_type;
1561
};
1562
1563
void hevc_release_sei_pic_timing( hevc_sei_pic_timing_t *p_timing )
1564
327k
{
1565
327k
    free( p_timing );
1566
327k
}
1567
1568
hevc_sei_pic_timing_t * hevc_decode_sei_pic_timing( bs_t *p_bs,
1569
                                                    const hevc_sequence_parameter_set_t *p_sps )
1570
1.14k
{
1571
1.14k
    hevc_sei_pic_timing_t *p_timing = malloc(sizeof(*p_timing));
1572
1.14k
    if( p_timing )
1573
1.14k
    {
1574
1.14k
        if( p_sps->vui.frame_field_info_present_flag )
1575
796
        {
1576
796
            p_timing->pic_struct = bs_read( p_bs, 4 );
1577
796
            p_timing->source_scan_type = bs_read( p_bs, 2 );
1578
796
        }
1579
346
        else
1580
346
        {
1581
346
            p_timing->pic_struct = 0;
1582
346
            p_timing->source_scan_type = 1;
1583
346
        }
1584
1.14k
    }
1585
1.14k
    return p_timing;
1586
1.14k
}
1587
1588
bool hevc_decode_sei_recovery_point( bs_t *p_bs, hevc_sei_recovery_point_t *p_reco )
1589
429
{
1590
429
    p_reco->i_frames = bs_read_se( p_bs );
1591
429
    return true;
1592
429
}
1593
1594
bool hevc_frame_is_progressive( const hevc_sequence_parameter_set_t *p_sps,
1595
                                const hevc_sei_pic_timing_t *p_timing )
1596
0
{
1597
0
    if( p_sps->vui_parameters_present_flag &&
1598
0
        p_sps->vui.field_seq_flag )
1599
0
        return false;
1600
1601
0
    const hevc_inner_profile_tier_level_t *p_profile = &p_sps->profile_tier_level.general;
1602
    /* 1 & 0, 0 & 1, global */
1603
0
    if( p_profile->progressive_source_flag != p_profile->interlaced_source_flag )
1604
0
    {
1605
0
        return p_profile->progressive_source_flag > p_profile->interlaced_source_flag;
1606
0
    }
1607
    /* 1 & 1, defined in SEI */
1608
0
    else if( p_profile->progressive_source_flag )
1609
0
    {
1610
0
        if( p_timing && p_sps->vui.frame_field_info_present_flag )
1611
0
        {
1612
0
            if( p_timing->source_scan_type < 2 )
1613
0
                return p_timing->source_scan_type != 0;
1614
0
        }
1615
0
    }
1616
    /* else 0 & 0, unspec */
1617
1618
0
    return true;
1619
0
}
1620
1621
uint8_t hevc_get_num_clock_ts( const hevc_sequence_parameter_set_t *p_sps,
1622
                               const hevc_sei_pic_timing_t *p_timing )
1623
286k
{
1624
286k
    if( p_sps->vui.frame_field_info_present_flag && p_timing && p_timing->pic_struct < 13 )
1625
334
    {
1626
        /* !WARN modified with units_field_based_flag (D.3.25) for values 0, 7 and 8 */
1627
334
        const uint8_t rgi_numclock[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1628
334
        return rgi_numclock[p_timing->pic_struct];
1629
334
    }
1630
1631
285k
    if( p_sps->vui_parameters_present_flag )
1632
37.4k
    {
1633
37.4k
        if( p_sps->vui.field_seq_flag )
1634
5.47k
            return 1; /* D.3.27 */
1635
37.4k
    }
1636
248k
    else if( p_sps->profile_tier_level.general.interlaced_source_flag &&
1637
248k
            !p_sps->profile_tier_level.general.progressive_source_flag )
1638
1.59k
    {
1639
1.59k
        return 1;
1640
1.59k
    }
1641
1642
278k
    return 2;
1643
285k
}