Coverage Report

Created: 2025-11-09 06:39

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