Coverage Report

Created: 2026-01-17 06:26

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
70.0k
#define HEVC_MAX_SHORT_TERM_REF_PIC_SET 65
79
14.2k
#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
990
{
308
990
    size_t i_total = 0;
309
310
990
    if( i_buf < HEVC_MIN_HVCC_SIZE )
311
0
        return 0;
312
313
990
    const uint8_t i_nal_length_size = (p_buf[21] & 0x03) + 1;
314
990
    if(i_nal_length_size == 3)
315
14
        return 0;
316
317
976
    const uint8_t i_num_array = p_buf[22];
318
976
    p_buf += 23; i_buf -= 23;
319
320
8.47k
    for( uint8_t i = 0; i < i_num_array; i++ )
321
7.65k
    {
322
7.65k
        if(i_buf < 3)
323
12
            return 0;
324
325
7.63k
        const uint16_t i_num_nalu = p_buf[1] << 8 | p_buf[2];
326
7.63k
        p_buf += 3; i_buf -= 3;
327
328
733k
        for( uint16_t j = 0; j < i_num_nalu; j++ )
329
726k
        {
330
726k
            if(i_buf < 2)
331
6
                return 0;
332
333
726k
            const uint16_t i_nalu_length = p_buf[0] << 8 | p_buf[1];
334
726k
            if(i_buf < (size_t)i_nalu_length + 2)
335
135
                return 0;
336
337
726k
            i_total += i_nalu_length + 4; // annexb_startcode4;
338
726k
            p_buf += i_nalu_length + 2;
339
726k
            i_buf -= i_nalu_length + 2;
340
726k
        }
341
7.63k
    }
342
343
823
    return i_total;
344
976
}
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
990
{
349
990
    *pi_result = get_hvcC_to_AnnexB_NAL_size( p_buf, i_buf ); /* Does all checks */
350
990
    if( *pi_result == 0 )
351
200
        return NULL;
352
353
790
    if( pi_nal_length_size )
354
790
        *pi_nal_length_size = hevc_getNALLengthSize( p_buf );
355
356
790
    uint8_t *p_ret;
357
790
    uint8_t *p_out_buf = p_ret = malloc( *pi_result );
358
790
    if( !p_out_buf )
359
0
    {
360
0
        *pi_result = 0;
361
0
        return NULL;
362
0
    }
363
364
790
    const uint8_t i_num_array = p_buf[22];
365
790
    p_buf += 23;
366
367
7.96k
    for( uint8_t i = 0; i < i_num_array; i++ )
368
7.17k
    {
369
7.17k
        const uint16_t i_num_nalu = p_buf[1] << 8 | p_buf[2];
370
7.17k
        p_buf += 3;
371
372
720k
        for( uint16_t j = 0; j < i_num_nalu; j++ )
373
713k
        {
374
713k
            const uint16_t i_nalu_length = p_buf[0] << 8 | p_buf[1];
375
376
713k
            memcpy( p_out_buf, annexb_startcode4, 4 );
377
713k
            memcpy( &p_out_buf[4], &p_buf[2], i_nalu_length );
378
379
713k
            p_out_buf += 4 + i_nalu_length;
380
713k
            p_buf += 2 + i_nalu_length;
381
713k
        }
382
7.17k
    }
383
384
790
    return p_ret;
385
790
}
386
387
static bool hevc_parse_scaling_list_rbsp( bs_t *p_bs )
388
11.6k
{
389
58.3k
    for( int i=0; i<4; i++ )
390
46.6k
    {
391
279k
        for( int j=0; j<6; j += (i == 3) ? 3 : 1 )
392
233k
        {
393
233k
            if( bs_read1( p_bs ) == 0 )
394
212k
                bs_read_ue( p_bs );
395
21.0k
            else
396
21.0k
            {
397
21.0k
                unsigned nextCoef = 8;
398
21.0k
                unsigned coefNum = __MIN( 64, (1 << (4 + (i << 1))));
399
21.0k
                if( i > 1 )
400
1.76k
                {
401
1.76k
                    nextCoef = bs_read_se( p_bs ) + 8;
402
1.76k
                }
403
579k
                for( unsigned k=0; k<coefNum; k++ )
404
558k
                {
405
558k
                    nextCoef = ( nextCoef + bs_read_se( p_bs ) + 256 ) % 256;
406
558k
                }
407
21.0k
            }
408
233k
        }
409
46.6k
    }
410
411
11.6k
    return true;
412
11.6k
}
413
414
static bool hevc_parse_vui_parameters_rbsp( bs_t *p_bs, hevc_vui_parameters_t *p_vui,
415
                                            bool b_broken )
416
22.7k
{
417
22.7k
    p_vui->aspect_ratio_info_present_flag = bs_read1( p_bs );
418
22.7k
    if( p_vui->aspect_ratio_info_present_flag )
419
14.0k
    {
420
14.0k
        p_vui->ar.aspect_ratio_idc = bs_read( p_bs, 8 );
421
14.0k
        if( p_vui->ar.aspect_ratio_idc == 0xFF ) //HEVC_SAR__IDC_EXTENDED_SAR )
422
3.10k
        {
423
3.10k
            p_vui->ar.sar_width = bs_read( p_bs, 16 );
424
3.10k
            p_vui->ar.sar_height = bs_read( p_bs, 16 );
425
3.10k
        }
426
14.0k
    }
427
428
22.7k
    p_vui->overscan_info_present_flag = bs_read1( p_bs );
429
22.7k
    if( p_vui->overscan_info_present_flag )
430
13.2k
        p_vui->overscan_appropriate_flag = bs_read1( p_bs );
431
432
22.7k
    p_vui->video_signal_type_present_flag = bs_read1( p_bs );
433
22.7k
    if( p_vui->video_signal_type_present_flag )
434
12.0k
    {
435
12.0k
        p_vui->vs.video_format = bs_read( p_bs, 3 );
436
12.0k
        p_vui->vs.colour.full_range_flag = bs_read1( p_bs );
437
12.0k
        p_vui->vs.colour_description_present_flag = bs_read1( p_bs );
438
12.0k
        if( p_vui->vs.colour_description_present_flag )
439
7.23k
        {
440
7.23k
            p_vui->vs.colour.colour_primaries = bs_read( p_bs, 8 );
441
7.23k
            p_vui->vs.colour.transfer_characteristics = bs_read( p_bs, 8 );
442
7.23k
            p_vui->vs.colour.matrix_coeffs = bs_read( p_bs, 8 );
443
7.23k
        }
444
4.78k
        else
445
4.78k
        {
446
4.78k
            p_vui->vs.colour.colour_primaries = ISO_23001_8_CP_UNSPECIFIED;
447
4.78k
            p_vui->vs.colour.transfer_characteristics = ISO_23001_8_TC_UNSPECIFIED;
448
4.78k
            p_vui->vs.colour.matrix_coeffs = ISO_23001_8_MC_UNSPECIFIED;
449
4.78k
        }
450
12.0k
    }
451
452
22.7k
    p_vui->chroma_loc_info_present_flag = bs_read1( p_bs );
453
22.7k
    if( p_vui->chroma_loc_info_present_flag )
454
6.84k
    {
455
6.84k
        p_vui->chroma.sample_loc_type_top_field = bs_read_ue( p_bs );
456
6.84k
        p_vui->chroma.sample_loc_type_bottom_field = bs_read_ue( p_bs );
457
6.84k
    }
458
459
22.7k
    p_vui->neutral_chroma_indication_flag = bs_read1( p_bs );
460
22.7k
    p_vui->field_seq_flag = bs_read1( p_bs );
461
22.7k
    p_vui->frame_field_info_present_flag = bs_read1( p_bs );
462
463
22.7k
    p_vui->default_display_window_flag = !b_broken && bs_read1( p_bs );
464
22.7k
    if( p_vui->default_display_window_flag )
465
6.79k
    {
466
6.79k
        p_vui->def_disp.win_left_offset = bs_read_ue( p_bs );
467
6.79k
        p_vui->def_disp.win_right_offset = bs_read_ue( p_bs );
468
6.79k
        p_vui->def_disp.win_top_offset = bs_read_ue( p_bs );
469
6.79k
        p_vui->def_disp.win_bottom_offset = bs_read_ue( p_bs );
470
6.79k
    }
471
472
22.7k
    p_vui->vui_timing_info_present_flag = bs_read1( p_bs );
473
22.7k
    if( p_vui->vui_timing_info_present_flag )
474
7.29k
    {
475
7.29k
        p_vui->timing.vui_num_units_in_tick =  bs_read( p_bs, 32 );
476
7.29k
        p_vui->timing.vui_time_scale =  bs_read( p_bs, 32 );
477
7.29k
    }
478
    /* incomplete */
479
480
22.7k
    return !bs_error( p_bs );
481
22.7k
}
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
217k
{
486
217k
    if(i_buf < 3)
487
6.69k
        return false;
488
    /* No need to lookup convert from emulation for that data */
489
210k
    uint8_t i_nal_type = hevc_getNALType(p_buf);
490
210k
    bs_t bs;
491
210k
    bs_init(&bs, &p_buf[2], i_buf - 2);
492
210k
    if(i_nal_type == HEVC_NAL_PPS)
493
84.3k
    {
494
84.3k
        *pi_id = bs_read_ue( &bs );
495
84.3k
        if(*pi_id > HEVC_PPS_ID_MAX)
496
1.64k
            return false;
497
84.3k
    }
498
126k
    else
499
126k
    {
500
126k
        *pi_id = bs_read( &bs, 4 );
501
126k
        if(i_nal_type == HEVC_NAL_SPS)
502
96.2k
        {
503
96.2k
            if(*pi_id > HEVC_SPS_ID_MAX)
504
0
                return false;
505
96.2k
        }
506
30.3k
        else if(*pi_id > HEVC_VPS_ID_MAX)
507
0
            return false;
508
126k
    }
509
209k
    return true;
510
210k
}
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
146k
{
515
146k
    p_in->profile_space = bs_read( p_bs, 2 );
516
146k
    p_in->tier_flag = bs_read1( p_bs );
517
146k
    p_in->profile_idc = bs_read( p_bs, 5 );
518
146k
    p_in->profile_compatibility_flag = bs_read( p_bs, 32 );
519
146k
    p_in->progressive_source_flag = bs_read1( p_bs );
520
146k
    p_in->interlaced_source_flag = bs_read1( p_bs );
521
146k
    p_in->non_packed_constraint_flag = bs_read1( p_bs );
522
146k
    p_in->frame_only_constraint_flag = bs_read1( p_bs );
523
524
146k
    if( ( p_in->profile_idc >= 4 && p_in->profile_idc <= 10 ) ||
525
128k
        ( p_in->profile_compatibility_flag & 0x0F700000 ) )
526
109k
    {
527
109k
        p_in->idc4to7.max_12bit_constraint_flag = bs_read1( p_bs );
528
109k
        p_in->idc4to7.max_10bit_constraint_flag = bs_read1( p_bs );
529
109k
        p_in->idc4to7.max_8bit_constraint_flag = bs_read1( p_bs );
530
109k
        p_in->idc4to7.max_422chroma_constraint_flag = bs_read1( p_bs );
531
109k
        p_in->idc4to7.max_420chroma_constraint_flag = bs_read1( p_bs );
532
109k
        p_in->idc4to7.max_monochrome_constraint_flag = bs_read1( p_bs );
533
109k
        p_in->idc4to7.intra_constraint_flag = bs_read1( p_bs );
534
109k
        p_in->idc4to7.one_picture_only_constraint_flag = bs_read1( p_bs );
535
109k
        p_in->idc4to7.lower_bit_rate_constraint_flag = bs_read1( p_bs );
536
109k
        if( p_in->profile_idc == 5 ||
537
107k
            p_in->profile_idc == 9 ||
538
105k
            p_in->profile_idc == 10 ||
539
99.2k
           (p_in->profile_compatibility_flag & 0x08600000) )
540
94.1k
        {
541
94.1k
            p_in->idc4to7.max_14bit_constraint_flag = bs_read1( p_bs );
542
94.1k
            bs_skip( p_bs, 33 );
543
94.1k
        }
544
14.9k
        else bs_skip( p_bs, 34 );
545
109k
    }
546
37.0k
    else if( p_in->profile_idc == 2 ||
547
33.5k
            (p_in->profile_compatibility_flag & 0x20000000) )
548
9.46k
    {
549
9.46k
        bs_skip( p_bs, 7 );
550
9.46k
        p_in->idc4to7.one_picture_only_constraint_flag = bs_read1( p_bs );
551
9.46k
        bs_skip( p_bs, 35 );
552
9.46k
    }
553
27.5k
    else
554
27.5k
    {
555
27.5k
        bs_read( p_bs, 43 );
556
27.5k
    }
557
558
146k
    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
103k
        p_in->idc1to5.inbld_flag = bs_read1( p_bs );
562
42.3k
    else
563
42.3k
        bs_skip( p_bs, 1 );
564
565
146k
    return !bs_error( p_bs );
566
146k
}
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
123k
{
572
123k
    if( profile_present && !hevc_parse_inner_profile_tier_level_rbsp( p_bs, &p_ptl->general ) )
573
27.0k
        return false;
574
575
96.8k
    if( bs_error( p_bs ) )
576
0
        return false;
577
578
96.8k
    p_ptl->general_level_idc = bs_read( p_bs, 8 );
579
580
96.8k
    if( max_num_sub_layers_minus1 > 0 )
581
33.6k
    {
582
33.6k
        if( bs_eof( p_bs ) )
583
2.56k
            return false;
584
585
279k
        for( uint8_t i=0; i< 8; i++ )
586
248k
        {
587
248k
            if( i < max_num_sub_layers_minus1 )
588
153k
            {
589
153k
                if( bs_read1( p_bs ) )
590
44.6k
                    p_ptl->sublayer_profile_present_flag |= (0x80 >> i);
591
153k
                if( bs_read1( p_bs ) )
592
60.9k
                    p_ptl->sublayer_level_present_flag |= (0x80 >> i);
593
153k
            }
594
95.0k
            else
595
95.0k
                bs_read( p_bs, 2 );
596
248k
        }
597
598
122k
        for( uint8_t i=0; i < max_num_sub_layers_minus1; i++ )
599
104k
        {
600
104k
            if( ( p_ptl->sublayer_profile_present_flag & (0x80 >> i) ) &&
601
22.2k
                ! hevc_parse_inner_profile_tier_level_rbsp( p_bs, &p_ptl->sub_layer[i] ) )
602
6.51k
                return false;
603
604
97.6k
            if( p_ptl->sublayer_profile_present_flag & (0x80 >> i) )
605
15.7k
            {
606
15.7k
                if( bs_eof( p_bs ) )
607
5.87k
                    return false;
608
9.85k
                p_ptl->sub_layer_level_idc[i] = bs_read( p_bs, 8 );
609
9.85k
            }
610
97.6k
        }
611
31.0k
    }
612
613
81.8k
    return !bs_error( p_bs );
614
96.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.7k
{
619
29.7k
    if( bs_eof( p_bs ) )
620
0
        return false;
621
622
29.7k
    p_vps->vps_video_parameter_set_id = bs_read( p_bs, 4 );
623
29.7k
    p_vps->vps_base_layer_internal_flag = bs_read1( p_bs );
624
29.7k
    p_vps->vps_base_layer_available_flag = bs_read1( p_bs );
625
29.7k
    p_vps->vps_max_layers_minus1 = bs_read( p_bs, 6 );
626
29.7k
    p_vps->vps_max_sub_layers_minus1 = bs_read( p_bs, 3 );
627
29.7k
    p_vps->vps_temporal_id_nesting_flag = bs_read1( p_bs );
628
29.7k
    bs_skip( p_bs, 16 );
629
630
29.7k
    if( !hevc_parse_profile_tier_level_rbsp( p_bs, true, p_vps->vps_max_sub_layers_minus1,
631
29.7k
                                            &p_vps->profile_tier_level ) )
632
11.8k
        return false;
633
634
17.8k
    p_vps->vps_sub_layer_ordering_info_present_flag = bs_read1( p_bs );
635
17.8k
    for( unsigned i= (p_vps->vps_sub_layer_ordering_info_present_flag ?
636
10.4k
                      0 : p_vps->vps_max_sub_layers_minus1);
637
47.0k
         i<= p_vps->vps_max_sub_layers_minus1; i++ )
638
29.1k
    {
639
29.1k
        p_vps->vps_max[i].dec_pic_buffering_minus1 = bs_read_ue( p_bs );
640
29.1k
        p_vps->vps_max[i].num_reorder_pics = bs_read_ue( p_bs );
641
29.1k
        p_vps->vps_max[i].max_latency_increase_plus1 = bs_read_ue( p_bs );
642
29.1k
    }
643
644
17.8k
    if( bs_error( p_bs ) )
645
2.41k
        return false;
646
647
15.4k
    p_vps->vps_max_layer_id = bs_read( p_bs, 6 );
648
15.4k
    p_vps->vps_num_layer_set_minus1 = bs_read_ue( p_bs );
649
    // layer_id_included_flag; read but discarded
650
15.4k
    bs_skip( p_bs, p_vps->vps_num_layer_set_minus1 * (p_vps->vps_max_layer_id + 1) );
651
652
15.4k
    p_vps->vps_timing_info_present_flag = bs_read1( p_bs );
653
15.4k
    if( p_vps->vps_timing_info_present_flag )
654
7.59k
    {
655
7.59k
        p_vps->vps_num_units_in_tick = bs_read( p_bs, 32 );
656
7.59k
        p_vps->vps_time_scale = bs_read( p_bs, 32 );
657
7.59k
    }
658
    /* parsing incomplete */
659
660
15.4k
    return !bs_error( p_bs );
661
17.8k
}
662
663
void hevc_rbsp_release_vps( hevc_video_parameter_set_t *p_vps )
664
30.0k
{
665
30.0k
    free( p_vps );
666
30.0k
}
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
204k
    { \
671
204k
        hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \
672
204k
        if(likely(p_hevctype)) \
673
204k
        { \
674
204k
            bs_t bs; \
675
204k
            struct hxxx_bsfw_ep3b_ctx_s bsctx; \
676
204k
            if( b_escaped ) \
677
204k
            { \
678
204k
                hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
679
204k
                bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
680
204k
            } \
681
204k
            else bs_init( &bs, p_buf, i_buf ); \
682
204k
            bs_skip( &bs, 7 ); /* nal_unit_header */ \
683
204k
            uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \
684
204k
            bs_skip( &bs, 3 ); /* !nal_unit_header */ \
685
204k
            if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \
686
204k
            { \
687
164k
                release( p_hevctype ); \
688
164k
                p_hevctype = NULL; \
689
164k
            } \
690
204k
        } \
691
204k
        return p_hevctype; \
692
204k
    }
693
694
30.0k
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
43.8k
{
701
43.8k
    if( stRpsIdx && bs_read1( p_bs ) ) /* Interref pic set prediction flag */
702
19.9k
    {
703
19.9k
        nal_ue_t delta_idx_minus_1 = 0;
704
19.9k
        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
19.9k
        if(delta_idx_minus_1 == stRpsIdx)
711
0
            return false;
712
713
19.9k
        nal_u1_t delta_rps_sign = bs_read1( p_bs );
714
19.9k
        nal_ue_t abs_delta_rps_minus1 = bs_read_ue( p_bs );
715
19.9k
        unsigned RefRpsIdx = stRpsIdx - delta_idx_minus_1 - 1;
716
19.9k
        int deltaRps = ( 1 - ( delta_rps_sign << 1 ) ) * ( abs_delta_rps_minus1 + 1 );
717
19.9k
        VLC_UNUSED(deltaRps);
718
719
19.9k
        unsigned numDeltaPocs = p_sets[RefRpsIdx].num_delta_pocs;
720
19.9k
        p_sets[stRpsIdx].num_delta_pocs = 0;
721
102k
        for( unsigned j=0; j<= numDeltaPocs; j++ )
722
82.5k
        {
723
82.5k
            if( ! bs_read1( p_bs ) ) /* used_by_curr_pic_flag */
724
32.8k
            {
725
32.8k
                if( bs_read1( p_bs ) ) /* use_delta_flag */
726
9.12k
                    p_sets[stRpsIdx].num_delta_pocs++;
727
32.8k
            }
728
49.6k
            else
729
49.6k
                p_sets[stRpsIdx].num_delta_pocs++;
730
82.5k
        }
731
19.9k
    }
732
23.8k
    else
733
23.8k
    {
734
23.8k
        nal_ue_t num_negative_pics = bs_read_ue( p_bs );
735
23.8k
        nal_ue_t num_positive_pics = bs_read_ue( p_bs );
736
23.8k
        if( bs_error( p_bs ) )
737
2.51k
            return false;
738
963M
        for(unsigned int i=0; i<num_negative_pics; i++)
739
963M
        {
740
963M
            (void) bs_read_ue( p_bs ); /* delta_poc_s0_minus1 */
741
963M
            (void) bs_read1( p_bs ); /* used_by_current_pic_s0_flag */
742
963M
        }
743
3.50G
        for(unsigned int i=0; i<num_positive_pics; i++)
744
3.50G
        {
745
3.50G
            (void) bs_read_ue( p_bs ); /* delta_poc_s1_minus1 */
746
3.50G
            (void) bs_read1( p_bs ); /* used_by_current_pic_s1_flag */
747
3.50G
        }
748
21.3k
        p_sets[stRpsIdx].num_delta_pocs = num_positive_pics + num_negative_pics;
749
21.3k
    }
750
751
41.2k
    return !bs_error( p_bs );
752
43.8k
}
753
754
static bool hevc_parse_sequence_parameter_set_rbsp( bs_t *p_bs,
755
                                                    hevc_sequence_parameter_set_t *p_sps )
756
94.0k
{
757
94.0k
    p_sps->sps_video_parameter_set_id = bs_read( p_bs, 4 );
758
94.0k
    p_sps->sps_max_sub_layers_minus1 = bs_read( p_bs, 3 );
759
94.0k
    p_sps->sps_temporal_id_nesting_flag = bs_read1( p_bs );
760
94.0k
    if( !hevc_parse_profile_tier_level_rbsp( p_bs, true, p_sps->sps_max_sub_layers_minus1,
761
94.0k
                                            &p_sps->profile_tier_level ) )
762
31.1k
        return false;
763
764
62.9k
    if( bs_error( p_bs ) )
765
0
        return false;
766
767
62.9k
    p_sps->sps_seq_parameter_set_id = bs_read_ue( p_bs );
768
62.9k
    if( p_sps->sps_seq_parameter_set_id > HEVC_SPS_ID_MAX )
769
3.50k
        return false;
770
771
59.4k
    p_sps->chroma_format_idc = bs_read_ue( p_bs );
772
59.4k
    if( p_sps->chroma_format_idc == 3 )
773
8.00k
        p_sps->separate_colour_plane_flag = bs_read1( p_bs );
774
51.4k
    else if ( p_sps->chroma_format_idc > 3 )
775
3.26k
        return false;
776
56.1k
    p_sps->pic_width_in_luma_samples = bs_read_ue( p_bs );
777
56.1k
    p_sps->pic_height_in_luma_samples = bs_read_ue( p_bs );
778
56.1k
    if( !p_sps->pic_width_in_luma_samples || !p_sps->pic_height_in_luma_samples )
779
8.00k
        return false;
780
781
48.1k
    p_sps->conformance_window_flag = bs_read1( p_bs );
782
48.1k
    if( p_sps->conformance_window_flag )
783
23.5k
    {
784
23.5k
        p_sps->conf_win.left_offset = bs_read_ue( p_bs );
785
23.5k
        p_sps->conf_win.right_offset = bs_read_ue( p_bs );
786
23.5k
        p_sps->conf_win.top_offset = bs_read_ue( p_bs );
787
23.5k
        p_sps->conf_win.bottom_offset = bs_read_ue( p_bs );
788
23.5k
    }
789
790
48.1k
    p_sps->bit_depth_luma_minus8 = bs_read_ue( p_bs );
791
48.1k
    p_sps->bit_depth_chroma_minus8 = bs_read_ue( p_bs );
792
48.1k
    p_sps->log2_max_pic_order_cnt_lsb_minus4 = bs_read_ue( p_bs );
793
794
48.1k
    p_sps->sps_sub_layer_ordering_info_present_flag = bs_read1( p_bs );
795
48.1k
    for( uint8_t i=(p_sps->sps_sub_layer_ordering_info_present_flag ? 0 : p_sps->sps_max_sub_layers_minus1);
796
101k
         i <= p_sps->sps_max_sub_layers_minus1; i++ )
797
53.6k
    {
798
53.6k
        p_sps->sps_max[i].dec_pic_buffering_minus1 = bs_read_ue( p_bs );
799
53.6k
        p_sps->sps_max[i].num_reorder_pics = bs_read_ue( p_bs );
800
53.6k
        p_sps->sps_max[i].latency_increase_plus1 = bs_read_ue( p_bs );
801
53.6k
    }
802
803
48.1k
    if( bs_eof( p_bs ) )
804
8.58k
        return false;
805
806
39.5k
    p_sps->log2_min_luma_coding_block_size_minus3 = bs_read_ue( p_bs );
807
39.5k
    p_sps->log2_diff_max_min_luma_coding_block_size = bs_read_ue( p_bs );
808
39.5k
    p_sps->log2_min_luma_transform_block_size_minus2 = bs_read_ue( p_bs );
809
39.5k
    p_sps->log2_diff_max_min_luma_transform_block_size = bs_read_ue( p_bs );
810
811
    /* parsing incomplete */
812
813
39.5k
    p_sps->max_transform_hierarchy_depth_inter = bs_read_ue( p_bs );
814
39.5k
    p_sps->max_transform_hierarchy_depth_intra = bs_read_ue( p_bs );
815
39.5k
    p_sps->scaling_list_enabled = bs_read1( p_bs );
816
39.5k
    if( p_sps->scaling_list_enabled )
817
12.9k
    {
818
12.9k
        p_sps->sps_scaling_list_data_present_flag = bs_read1( p_bs );
819
12.9k
        if( p_sps->sps_scaling_list_data_present_flag &&
820
2.15k
            ! hevc_parse_scaling_list_rbsp( p_bs ) )
821
0
        {
822
0
            return false;
823
0
        }
824
12.9k
    }
825
826
39.5k
    if( bs_error( p_bs ) )
827
4.14k
        return false;
828
829
35.4k
    p_sps->amp_enabled_flag = bs_read1( p_bs );
830
35.4k
    p_sps->sample_adaptive_offset_enabled_flag = bs_read1( p_bs );
831
832
35.4k
    p_sps->pcm_enabled_flag = bs_read1( p_bs );
833
35.4k
    if( p_sps->pcm_enabled_flag )
834
13.5k
    {
835
13.5k
        p_sps->pcm_sample_bit_depth_luma_minus1 = bs_read( p_bs, 4 );
836
13.5k
        p_sps->pcm_sample_bit_depth_chroma_minus1 = bs_read( p_bs, 4 );
837
13.5k
        p_sps->log2_min_pcm_luma_coding_block_size_minus3 = bs_read_ue( p_bs );
838
13.5k
        p_sps->log2_diff_max_min_pcm_luma_coding_block_size = bs_read_ue( p_bs );
839
13.5k
        p_sps->pcm_loop_filter_disabled_flag = bs_read1( p_bs );
840
13.5k
    }
841
842
35.4k
    p_sps->num_short_term_ref_pic_sets = bs_read_ue( p_bs );
843
35.4k
    if( p_sps->num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SET )
844
845
        return false;
845
846
34.5k
    hevc_short_term_ref_pic_set_t sets[HEVC_MAX_SHORT_TERM_REF_PIC_SET];
847
34.5k
    memset(&sets, 0, sizeof(hevc_short_term_ref_pic_set_t) * HEVC_MAX_SHORT_TERM_REF_PIC_SET);
848
72.5k
    for( unsigned int i=0; i<p_sps->num_short_term_ref_pic_sets; i++ )
849
43.8k
    {
850
43.8k
        if( !hevc_parse_st_ref_pic_set( p_bs, i, p_sps->num_short_term_ref_pic_sets, sets ) )
851
5.85k
            return false;
852
43.8k
    }
853
854
28.7k
    p_sps->long_term_ref_pics_present_flag = bs_read1( p_bs );
855
28.7k
    if( p_sps->long_term_ref_pics_present_flag )
856
14.2k
    {
857
14.2k
        p_sps->num_long_term_ref_pics_sps = bs_read_ue( p_bs );
858
14.2k
        if( p_sps->num_long_term_ref_pics_sps > HEVC_MAX_LONG_TERM_REF_PIC_SET )
859
340
            return false;
860
47.3k
        for( unsigned int i=0; i< p_sps->num_long_term_ref_pics_sps; i++ )
861
33.4k
        {
862
             /* lt_ref_pic_poc_lsb_sps */
863
33.4k
            bs_skip( p_bs, p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4 );
864
             /* used_by_curr_pic_lt_sps_flag */
865
33.4k
            bs_skip( p_bs, 1 );
866
33.4k
        }
867
13.8k
    }
868
869
28.3k
    p_sps->sps_temporal_mvp_enabled_flag = bs_read1( p_bs );
870
28.3k
    p_sps->strong_intra_smoothing_enabled_flag = bs_read1( p_bs );
871
872
28.3k
    if( bs_error( p_bs ) )
873
2.21k
        return false;
874
875
26.1k
    p_sps->vui_parameters_present_flag = bs_read1( p_bs );
876
26.1k
    if( p_sps->vui_parameters_present_flag )
877
22.7k
    {
878
22.7k
        bs_t rollbackpoint = *p_bs;
879
22.7k
        if( !hevc_parse_vui_parameters_rbsp( p_bs, &p_sps->vui, false ) &&
880
15.9k
            p_sps->vui.default_display_window_flag &&
881
4.62k
            !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
22.7k
    }
894
895
    /* incomplete */
896
897
26.1k
    return !bs_error( p_bs );
898
26.1k
}
899
900
void hevc_rbsp_release_sps( hevc_sequence_parameter_set_t *p_sps )
901
95.5k
{
902
95.5k
    free( p_sps );
903
95.5k
}
904
905
95.5k
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
78.7k
{
911
78.7k
    if( bs_eof( p_bs ) )
912
0
        return false;
913
78.7k
    p_pps->pps_pic_parameter_set_id = bs_read_ue( p_bs );
914
78.7k
    if( p_pps->pps_pic_parameter_set_id > HEVC_PPS_ID_MAX )
915
847
        return false;
916
77.8k
    p_pps->pps_seq_parameter_set_id = bs_read_ue( p_bs );
917
77.8k
    if( p_pps->pps_seq_parameter_set_id > HEVC_SPS_ID_MAX )
918
6.51k
        return false;
919
71.3k
    p_pps->dependent_slice_segments_enabled_flag = bs_read1( p_bs );
920
71.3k
    p_pps->output_flag_present_flag = bs_read1( p_bs );
921
71.3k
    p_pps->num_extra_slice_header_bits = bs_read( p_bs, 3 );
922
71.3k
    p_pps->sign_data_hiding_enabled_flag = bs_read1( p_bs );
923
71.3k
    p_pps->cabac_init_present_flag = bs_read1( p_bs );
924
925
71.3k
    p_pps->num_ref_idx_l0_default_active_minus1 = bs_read_ue( p_bs );
926
71.3k
    p_pps->num_ref_idx_l1_default_active_minus1 = bs_read_ue( p_bs );
927
928
71.3k
    p_pps->init_qp_minus26 = bs_read_se( p_bs );
929
71.3k
    p_pps->constrained_intra_pred_flag = bs_read1( p_bs );
930
71.3k
    p_pps->transform_skip_enabled_flag = bs_read1( p_bs );
931
71.3k
    p_pps->cu_qp_delta_enabled_flag = bs_read1( p_bs );
932
71.3k
    if( p_pps->cu_qp_delta_enabled_flag )
933
23.1k
        p_pps->diff_cu_qp_delta_depth = bs_read_ue( p_bs );
934
935
71.3k
    if( bs_error( p_bs ) )
936
9.75k
        return false;
937
938
61.5k
    p_pps->pps_cb_qp_offset = bs_read_se( p_bs );
939
61.5k
    p_pps->pps_cr_qp_offset = bs_read_se( p_bs );
940
61.5k
    p_pps->pic_slice_level_chroma_qp_offsets_present_flag = bs_read1( p_bs );
941
61.5k
    p_pps->weighted_pred_flag = bs_read1( p_bs );
942
61.5k
    p_pps->weighted_bipred_flag = bs_read1( p_bs );
943
61.5k
    p_pps->transquant_bypass_enable_flag = bs_read1( p_bs );
944
61.5k
    p_pps->tiles_enabled_flag = bs_read1( p_bs );
945
61.5k
    p_pps->entropy_coding_sync_enabled_flag = bs_read1( p_bs );
946
947
61.5k
    if( p_pps->tiles_enabled_flag )
948
21.8k
    {
949
21.8k
        p_pps->num_tile_columns_minus1 = bs_read_ue( p_bs ); /* TODO: validate max col/row values */
950
21.8k
        p_pps->num_tile_rows_minus1 = bs_read_ue( p_bs );    /*       against sps PicWidthInCtbsY */
951
21.8k
        p_pps->uniform_spacing_flag = bs_read1( p_bs );
952
21.8k
        if( !p_pps->uniform_spacing_flag )
953
12.7k
        {
954
2.68G
            for( unsigned i=0; i< p_pps->num_tile_columns_minus1; i++ )
955
2.68G
                (void) bs_read_ue( p_bs );
956
4.79G
            for( unsigned i=0; i< p_pps->num_tile_rows_minus1; i++ )
957
4.79G
                (void) bs_read_ue( p_bs );
958
12.7k
        }
959
21.8k
        p_pps->loop_filter_across_tiles_enabled_flag = bs_read1( p_bs );
960
21.8k
        if( bs_error( p_bs ) )
961
6.43k
            return false;
962
21.8k
    }
963
964
55.1k
    p_pps->pps_loop_filter_across_slices_enabled_flag = bs_read1( p_bs );
965
55.1k
    p_pps->deblocking_filter_control_present_flag = bs_read1( p_bs );
966
55.1k
    if( p_pps->deblocking_filter_control_present_flag )
967
16.1k
    {
968
16.1k
        p_pps->deblocking_filter_override_enabled_flag = bs_read1( p_bs );
969
16.1k
        p_pps->pps_deblocking_filter_disabled_flag = bs_read1( p_bs );
970
16.1k
        if( !p_pps->pps_deblocking_filter_disabled_flag )
971
9.40k
        {
972
9.40k
            p_pps->pps_beta_offset_div2 = bs_read_se( p_bs );
973
9.40k
            p_pps->pps_tc_offset_div2 = bs_read_se( p_bs );
974
9.40k
        }
975
16.1k
    }
976
977
55.1k
    p_pps->scaling_list_data_present_flag = bs_read1( p_bs );
978
55.1k
    if( p_pps->scaling_list_data_present_flag && !hevc_parse_scaling_list_rbsp( p_bs ) )
979
0
        return false;
980
981
55.1k
    p_pps->lists_modification_present_flag = bs_read1( p_bs );
982
55.1k
    p_pps->log2_parallel_merge_level_minus2 = bs_read_ue( p_bs );
983
55.1k
    p_pps->slice_header_extension_present_flag = bs_read1( p_bs );
984
985
55.1k
    p_pps->pps_extension_present_flag = bs_read1( p_bs );
986
55.1k
    if( p_pps->pps_extension_present_flag )
987
14.9k
    {
988
14.9k
        p_pps->pps_range_extension_flag = bs_read1( p_bs );
989
14.9k
        p_pps->pps_multilayer_extension_flag = bs_read1( p_bs );
990
14.9k
        p_pps->pps_3d_extension_flag = bs_read1( p_bs );
991
14.9k
        p_pps->pps_extension_5bits = bs_read( p_bs, 5 );
992
14.9k
    }
993
994
55.1k
    return !bs_error( p_bs );
995
55.1k
}
996
997
void hevc_rbsp_release_pps( hevc_picture_parameter_set_t *p_pps )
998
79.1k
{
999
79.1k
    free( p_pps );
1000
79.1k
}
1001
1002
79.1k
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
83.3k
{
1007
83.3k
    return p_sps->sps_video_parameter_set_id;
1008
83.3k
}
1009
1010
uint8_t hevc_get_pps_sps_id( const hevc_picture_parameter_set_t *p_pps )
1011
95.8k
{
1012
95.8k
    return p_pps->pps_seq_parameter_set_id;
1013
95.8k
}
1014
1015
uint8_t hevc_get_slice_pps_id( const hevc_slice_segment_header_t *p_slice )
1016
15.1k
{
1017
15.1k
    return p_slice->slice_pic_parameter_set_id;
1018
15.1k
}
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
15.1k
{
1028
15.1k
    if(p_sps->profile_tier_level.general.profile_idc)
1029
13.6k
    {
1030
13.6k
        *pi_profile = p_sps->profile_tier_level.general.profile_idc;
1031
13.6k
        *pi_level = p_sps->profile_tier_level.general_level_idc;
1032
13.6k
        return true;
1033
13.6k
    }
1034
1.44k
    return false;
1035
15.1k
}
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
15.1k
{
1042
15.1k
    return h26x_get_picture_size( p_sps->chroma_format_idc,
1043
15.1k
                                  p_sps->pic_width_in_luma_samples,
1044
15.1k
                                  p_sps->pic_height_in_luma_samples,
1045
15.1k
                                  &p_sps->conf_win,
1046
15.1k
                                  p_ox, p_oy, p_w, p_h, p_vw, p_vh );
1047
15.1k
}
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.33k
{
1084
1.33k
    if( p_sps->vui_parameters_present_flag && p_sps->vui.vui_timing_info_present_flag )
1085
423
    {
1086
423
        *pi_den = p_sps->vui.timing.vui_num_units_in_tick;
1087
423
        *pi_num = p_sps->vui.timing.vui_time_scale;
1088
423
        return (*pi_den && *pi_num);
1089
423
    }
1090
916
    else if( p_vps && p_vps->vps_timing_info_present_flag )
1091
488
    {
1092
488
        *pi_den = p_vps->vps_num_units_in_tick;
1093
488
        *pi_num = p_vps->vps_time_scale;
1094
488
        return (*pi_den && *pi_num);
1095
488
    }
1096
428
    return false;
1097
1.33k
}
1098
1099
bool hevc_get_aspect_ratio( const hevc_sequence_parameter_set_t *p_sps,
1100
                            unsigned *num, unsigned *den )
1101
15.0k
{
1102
15.0k
    if( !p_sps->vui_parameters_present_flag )
1103
3.79k
        return false;
1104
11.2k
    return h26x_get_aspect_ratio( &p_sps->vui.ar, num, den );
1105
15.0k
}
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
15.1k
{
1122
15.1k
    if( !p_sps->vui_parameters_present_flag )
1123
3.80k
        return false;
1124
11.3k
    return h26x_get_colorimetry( &p_sps->vui.vs.colour,
1125
11.3k
                                p_primaries, p_transfer, p_colorspace, p_full_range );
1126
15.1k
}
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
1.05M
{
1133
1.05M
    hevc_sequence_parameter_set_t *p_sps;
1134
1.05M
    hevc_picture_parameter_set_t *p_pps;
1135
1.05M
    hevc_video_parameter_set_t *p_vps;
1136
1137
1.05M
    if( bs_eof( p_bs ) )
1138
0
        return false;
1139
1140
1.05M
    p_sl->first_slice_segment_in_pic_flag = bs_read1( p_bs );
1141
1.05M
    if( p_sl->nal_type >= HEVC_NAL_BLA_W_LP && p_sl->nal_type <= HEVC_NAL_IRAP_VCL23 )
1142
992k
        p_sl->no_output_of_prior_pics_flag = bs_read1( p_bs );
1143
1.05M
    p_sl->slice_pic_parameter_set_id = bs_read_ue( p_bs );
1144
1.05M
    if( p_sl->slice_pic_parameter_set_id > HEVC_PPS_ID_MAX )
1145
50.2k
        return false;
1146
1147
1.00M
    if( bs_error( p_bs ) )
1148
928k
        return false;
1149
1150
75.8k
    get_matchedxps( p_sl->slice_pic_parameter_set_id, priv, &p_pps, &p_sps, &p_vps );
1151
75.8k
    if(!p_sps || !p_pps)
1152
16.2k
        return false;
1153
1154
59.6k
    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
59.6k
    if( !p_sl->dependent_slice_segment_flag )
1167
59.6k
    {
1168
59.6k
        unsigned i=0;
1169
59.6k
        if( p_pps->num_extra_slice_header_bits > i )
1170
51.1k
        {
1171
51.1k
            i++;
1172
51.1k
            bs_skip( p_bs, 1 ); /* discardable_flag */
1173
51.1k
        }
1174
1175
59.6k
        if( p_pps->num_extra_slice_header_bits > i )
1176
50.3k
        {
1177
50.3k
            i++;
1178
50.3k
            bs_skip( p_bs, 1 ); /* cross_layer_bla_flag */
1179
50.3k
        }
1180
1181
59.6k
        if( i < p_pps->num_extra_slice_header_bits )
1182
49.5k
           bs_skip( p_bs, p_pps->num_extra_slice_header_bits - i );
1183
1184
59.6k
        p_sl->slice_type = bs_read_ue( p_bs );
1185
59.6k
        if( p_sl->slice_type > HEVC_SLICE_TYPE_I )
1186
2.64k
            return false;
1187
1188
57.0k
        if( p_pps->output_flag_present_flag )
1189
44.0k
            p_sl->pic_output_flag = bs_read1( p_bs );
1190
12.9k
        else
1191
12.9k
            p_sl->pic_output_flag = 1;
1192
57.0k
    }
1193
1194
57.0k
    if( p_sps->separate_colour_plane_flag )
1195
22.2k
        bs_skip( p_bs, 2 ); /* colour_plane_id */
1196
1197
57.0k
    if( p_sl->nal_type != HEVC_NAL_IDR_W_RADL && p_sl->nal_type != HEVC_NAL_IDR_N_LP )
1198
54.4k
        p_sl->pic_order_cnt_lsb = bs_read( p_bs, p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4 );
1199
2.58k
    else
1200
2.58k
        p_sl->pic_order_cnt_lsb = 0;
1201
1202
57.0k
    return !bs_error( p_bs );
1203
59.6k
}
1204
1205
void hevc_rbsp_release_slice_header( hevc_slice_segment_header_t *p_sh )
1206
1.05M
{
1207
1.05M
    free( p_sh );
1208
1.05M
}
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
1.05M
{
1213
1.05M
    hevc_slice_segment_header_t *p_sh = calloc(1, sizeof(hevc_slice_segment_header_t));
1214
1.05M
    if(likely(p_sh))
1215
1.05M
    {
1216
1.05M
        bs_t bs;
1217
1.05M
        struct hxxx_bsfw_ep3b_ctx_s bsctx;
1218
1.05M
        if( b_escaped )
1219
1.05M
        {
1220
1.05M
            hxxx_bsfw_ep3b_ctx_init( &bsctx );
1221
1.05M
            bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );
1222
1.05M
        }
1223
0
        else bs_init( &bs, p_buf, i_buf );
1224
1.05M
        bs_skip( &bs, 1 );
1225
1.05M
        p_sh->nal_type = bs_read( &bs, 6 );
1226
1.05M
        p_sh->nuh_layer_id = bs_read( &bs, 6 );
1227
1.05M
        p_sh->temporal_id_plus1 = bs_read( &bs, 3 );
1228
1.05M
        if( p_sh->nuh_layer_id > 62 || p_sh->temporal_id_plus1 == 0 ||
1229
1.05M
           !hevc_parse_slice_segment_header_rbsp( &bs, get_matchedxps, priv, p_sh ) )
1230
1.03M
        {
1231
1.03M
            hevc_rbsp_release_slice_header( p_sh );
1232
1.03M
            p_sh = NULL;
1233
1.03M
        }
1234
1.05M
    }
1235
1.05M
    return p_sh;
1236
1.05M
}
1237
1238
bool hevc_get_slice_type( const hevc_slice_segment_header_t *p_sli, enum hevc_slice_type_e *pi_type )
1239
9.39k
{
1240
9.39k
    if( !p_sli->dependent_slice_segment_flag )
1241
9.39k
    {
1242
9.39k
        *pi_type = p_sli->slice_type;
1243
9.39k
        return true;
1244
9.39k
    }
1245
0
    return false;
1246
9.39k
}
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
1.04M
{
1567
1.04M
    free( p_timing );
1568
1.04M
}
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
2.31k
{
1573
2.31k
    hevc_sei_pic_timing_t *p_timing = malloc(sizeof(*p_timing));
1574
2.31k
    if( p_timing )
1575
2.31k
    {
1576
2.31k
        if( p_sps->vui.frame_field_info_present_flag )
1577
1.91k
        {
1578
1.91k
            p_timing->pic_struct = bs_read( p_bs, 4 );
1579
1.91k
            p_timing->source_scan_type = bs_read( p_bs, 2 );
1580
1.91k
        }
1581
402
        else
1582
402
        {
1583
402
            p_timing->pic_struct = 0;
1584
402
            p_timing->source_scan_type = 1;
1585
402
        }
1586
2.31k
    }
1587
2.31k
    return p_timing;
1588
2.31k
}
1589
1590
bool hevc_decode_sei_recovery_point( bs_t *p_bs, hevc_sei_recovery_point_t *p_reco )
1591
473
{
1592
473
    p_reco->i_frames = bs_read_se( p_bs );
1593
473
    return true;
1594
473
}
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
926k
{
1626
926k
    if( p_sps->vui.frame_field_info_present_flag && p_timing && p_timing->pic_struct < 13 )
1627
212
    {
1628
        /* !WARN modified with units_field_based_flag (D.3.25) for values 0, 7 and 8 */
1629
212
        const uint8_t rgi_numclock[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1630
212
        return rgi_numclock[p_timing->pic_struct];
1631
212
    }
1632
1633
926k
    if( p_sps->vui_parameters_present_flag )
1634
56.3k
    {
1635
56.3k
        if( p_sps->vui.field_seq_flag )
1636
11.4k
            return 1; /* D.3.27 */
1637
56.3k
    }
1638
869k
    else if( p_sps->profile_tier_level.general.interlaced_source_flag &&
1639
614k
            !p_sps->profile_tier_level.general.progressive_source_flag )
1640
364
    {
1641
364
        return 1;
1642
364
    }
1643
1644
914k
    return 2;
1645
926k
}