GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_h265_syntax_template.c Lines: 881 1304 67.6 %
Date: 2020-09-21 17:35:45 Branches: 885 2122 41.7 %

Line Branch Exec Source
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18
19
7872
static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
20
{
21
    int err;
22
23
7872
    fixed(1, rbsp_stop_one_bit, 1);
24
57160
    while (byte_alignment(rw) != 0)
25
49288
        fixed(1, rbsp_alignment_zero_bit, 0);
26
27
7872
    return 0;
28
}
29
30
31328
static int FUNC(nal_unit_header)(CodedBitstreamContext *ctx, RWContext *rw,
31
                                 H265RawNALUnitHeader *current,
32
                                 int expected_nal_unit_type)
33
{
34
    int err;
35
36
31328
    fixed(1, forbidden_zero_bit, 0);
37
38
31328
    if (expected_nal_unit_type >= 0)
39
7872
        u(6, nal_unit_type, expected_nal_unit_type,
40
                            expected_nal_unit_type);
41
    else
42
23456
        ub(6, nal_unit_type);
43
44
31328
    u(6, nuh_layer_id,          0, 62);
45
31328
    u(3, nuh_temporal_id_plus1, 1,  7);
46
47
31328
    return 0;
48
}
49
50
23456
static int FUNC(byte_alignment)(CodedBitstreamContext *ctx, RWContext *rw)
51
{
52
    int err;
53
54
23456
    fixed(1, alignment_bit_equal_to_one, 1);
55
118336
    while (byte_alignment(rw) != 0)
56
94880
        fixed(1, alignment_bit_equal_to_zero, 0);
57
58
23456
    return 0;
59
}
60
61
static int FUNC(extension_data)(CodedBitstreamContext *ctx, RWContext *rw,
62
                                H265RawExtensionData *current)
63
{
64
    int err;
65
    size_t k;
66
#ifdef READ
67
    GetBitContext start;
68
    uint8_t bit;
69
    start = *rw;
70
    for (k = 0; cbs_h2645_read_more_rbsp_data(rw); k++)
71
        skip_bits(rw, 1);
72
    current->bit_length = k;
73
    if (k > 0) {
74
        *rw = start;
75
        allocate(current->data, (current->bit_length + 7) / 8);
76
        for (k = 0; k < current->bit_length; k++) {
77
            xu(1, extension_data, bit, 0, 1, 0);
78
            current->data[k / 8] |= bit << (7 - k % 8);
79
        }
80
    }
81
#else
82
    for (k = 0; k < current->bit_length; k++)
83
        xu(1, extension_data, current->data[k / 8] >> (7 - k % 8) & 1, 0, 1, 0);
84
#endif
85
    return 0;
86
}
87
88
392
static int FUNC(profile_tier_level)(CodedBitstreamContext *ctx, RWContext *rw,
89
                                    H265RawProfileTierLevel *current,
90
                                    int profile_present_flag,
91
                                    int max_num_sub_layers_minus1)
92
{
93
    int err, i, j;
94
95
392
    if (profile_present_flag) {
96
392
        u(2, general_profile_space, 0, 0);
97
392
        flag(general_tier_flag);
98
392
        ub(5, general_profile_idc);
99
100
12936
        for (j = 0; j < 32; j++)
101
12544
            flags(general_profile_compatibility_flag[j], 1, j);
102
103
392
        flag(general_progressive_source_flag);
104
392
        flag(general_interlaced_source_flag);
105
392
        flag(general_non_packed_constraint_flag);
106
392
        flag(general_frame_only_constraint_flag);
107
108
#define profile_compatible(x) (current->general_profile_idc == (x) || \
109
                               current->general_profile_compatibility_flag[x])
110


392
        if (profile_compatible(4) || profile_compatible(5) ||
111


392
            profile_compatible(6) || profile_compatible(7) ||
112


392
            profile_compatible(8) || profile_compatible(9) ||
113

392
            profile_compatible(10)) {
114
            flag(general_max_12bit_constraint_flag);
115
            flag(general_max_10bit_constraint_flag);
116
            flag(general_max_8bit_constraint_flag);
117
            flag(general_max_422chroma_constraint_flag);
118
            flag(general_max_420chroma_constraint_flag);
119
            flag(general_max_monochrome_constraint_flag);
120
            flag(general_intra_constraint_flag);
121
            flag(general_one_picture_only_constraint_flag);
122
            flag(general_lower_bit_rate_constraint_flag);
123
124
            if (profile_compatible(5) || profile_compatible(9) ||
125
                profile_compatible(10)) {
126
                flag(general_max_14bit_constraint_flag);
127
                fixed(24, general_reserved_zero_33bits, 0);
128
                fixed( 9, general_reserved_zero_33bits, 0);
129
            } else {
130
                fixed(24, general_reserved_zero_34bits, 0);
131
                fixed(10, general_reserved_zero_34bits, 0);
132
            }
133

392
        } else if (profile_compatible(2)) {
134
312
            fixed(7, general_reserved_zero_7bits, 0);
135
312
            flag(general_one_picture_only_constraint_flag);
136
312
            fixed(24, general_reserved_zero_35bits, 0);
137
312
            fixed(11, general_reserved_zero_35bits, 0);
138
        } else {
139
80
            fixed(24, general_reserved_zero_43bits, 0);
140
80
            fixed(19, general_reserved_zero_43bits, 0);
141
        }
142
143


392
        if (profile_compatible(1) || profile_compatible(2) ||
144
            profile_compatible(3) || profile_compatible(4) ||
145
            profile_compatible(5) || profile_compatible(9)) {
146
392
            flag(general_inbld_flag);
147
        } else {
148
            fixed(1, general_reserved_zero_bit, 0);
149
        }
150
#undef profile_compatible
151
    }
152
153
392
    ub(8, general_level_idc);
154
155
584
    for (i = 0; i < max_num_sub_layers_minus1; i++) {
156
192
        flags(sub_layer_profile_present_flag[i], 1, i);
157
192
        flags(sub_layer_level_present_flag[i],   1, i);
158
    }
159
160
392
    if (max_num_sub_layers_minus1 > 0) {
161
672
        for (i = max_num_sub_layers_minus1; i < 8; i++)
162
576
            fixed(2, reserved_zero_2bits, 0);
163
    }
164
165
584
    for (i = 0; i < max_num_sub_layers_minus1; i++) {
166
192
        if (current->sub_layer_profile_present_flag[i]) {
167
32
            us(2, sub_layer_profile_space[i], 0, 0, 1, i);
168
32
            flags(sub_layer_tier_flag[i],           1, i);
169
32
            ubs(5, sub_layer_profile_idc[i], 1, i);
170
171
1056
            for (j = 0; j < 32; j++)
172
1024
                flags(sub_layer_profile_compatibility_flag[i][j], 2, i, j);
173
174
32
            flags(sub_layer_progressive_source_flag[i],    1, i);
175
32
            flags(sub_layer_interlaced_source_flag[i],     1, i);
176
32
            flags(sub_layer_non_packed_constraint_flag[i], 1, i);
177
32
            flags(sub_layer_frame_only_constraint_flag[i], 1, i);
178
179
#define profile_compatible(x) (current->sub_layer_profile_idc[i] == (x) ||   \
180
                               current->sub_layer_profile_compatibility_flag[i][x])
181


32
            if (profile_compatible(4) || profile_compatible(5) ||
182


32
                profile_compatible(6) || profile_compatible(7) ||
183


32
                profile_compatible(8) || profile_compatible(9) ||
184

32
                profile_compatible(10)) {
185
                flags(sub_layer_max_12bit_constraint_flag[i],        1, i);
186
                flags(sub_layer_max_10bit_constraint_flag[i],        1, i);
187
                flags(sub_layer_max_8bit_constraint_flag[i],         1, i);
188
                flags(sub_layer_max_422chroma_constraint_flag[i],    1, i);
189
                flags(sub_layer_max_420chroma_constraint_flag[i],    1, i);
190
                flags(sub_layer_max_monochrome_constraint_flag[i],   1, i);
191
                flags(sub_layer_intra_constraint_flag[i],            1, i);
192
                flags(sub_layer_one_picture_only_constraint_flag[i], 1, i);
193
                flags(sub_layer_lower_bit_rate_constraint_flag[i],   1, i);
194
195
                if (profile_compatible(5)) {
196
                    flags(sub_layer_max_14bit_constraint_flag[i], 1, i);
197
                    fixed(24, sub_layer_reserved_zero_33bits, 0);
198
                    fixed( 9, sub_layer_reserved_zero_33bits, 0);
199
                } else {
200
                    fixed(24, sub_layer_reserved_zero_34bits, 0);
201
                    fixed(10, sub_layer_reserved_zero_34bits, 0);
202
                }
203

32
            } else if (profile_compatible(2)) {
204
                fixed(7, sub_layer_reserved_zero_7bits, 0);
205
                flags(sub_layer_one_picture_only_constraint_flag[i], 1, i);
206
                fixed(24, sub_layer_reserved_zero_43bits, 0);
207
                fixed(11, sub_layer_reserved_zero_43bits, 0);
208
            } else {
209
32
                fixed(24, sub_layer_reserved_zero_43bits, 0);
210
32
                fixed(19, sub_layer_reserved_zero_43bits, 0);
211
            }
212
213


32
            if (profile_compatible(1) || profile_compatible(2) ||
214
                profile_compatible(3) || profile_compatible(4) ||
215
                profile_compatible(5) || profile_compatible(9)) {
216
32
                flags(sub_layer_inbld_flag[i], 1, i);
217
            } else {
218
                fixed(1, sub_layer_reserved_zero_bit, 0);
219
            }
220
#undef profile_compatible
221
        }
222
192
        if (current->sub_layer_level_present_flag[i])
223
32
            ubs(8, sub_layer_level_idc[i], 1, i);
224
    }
225
226
392
    return 0;
227
}
228
229
32
static int FUNC(sub_layer_hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
230
                                          H265RawHRDParameters *hrd,
231
                                          int nal, int sub_layer_id)
232
{
233
    H265RawSubLayerHRDParameters *current;
234
    int err, i;
235
236
32
    if (nal)
237
16
        current = &hrd->nal_sub_layer_hrd_parameters[sub_layer_id];
238
    else
239
16
        current = &hrd->vcl_sub_layer_hrd_parameters[sub_layer_id];
240
241
64
    for (i = 0; i <= hrd->cpb_cnt_minus1[sub_layer_id]; i++) {
242
32
        ues(bit_rate_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
243
32
        ues(cpb_size_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
244
32
        if (hrd->sub_pic_hrd_params_present_flag) {
245
16
            ues(cpb_size_du_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
246
16
            ues(bit_rate_du_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
247
        }
248
32
        flags(cbr_flag[i], 1, i);
249
    }
250
251
32
    return 0;
252
}
253
254
16
static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
255
                                H265RawHRDParameters *current, int common_inf_present_flag,
256
                                int max_num_sub_layers_minus1)
257
{
258
    int err, i;
259
260
16
    if (common_inf_present_flag) {
261
16
        flag(nal_hrd_parameters_present_flag);
262
16
        flag(vcl_hrd_parameters_present_flag);
263
264
16
        if (current->nal_hrd_parameters_present_flag ||
265
            current->vcl_hrd_parameters_present_flag) {
266
16
            flag(sub_pic_hrd_params_present_flag);
267
16
            if (current->sub_pic_hrd_params_present_flag) {
268
8
                ub(8, tick_divisor_minus2);
269
8
                ub(5, du_cpb_removal_delay_increment_length_minus1);
270
8
                flag(sub_pic_cpb_params_in_pic_timing_sei_flag);
271
8
                ub(5, dpb_output_delay_du_length_minus1);
272
            }
273
274
16
            ub(4, bit_rate_scale);
275
16
            ub(4, cpb_size_scale);
276
16
            if (current->sub_pic_hrd_params_present_flag)
277
8
                ub(4, cpb_size_du_scale);
278
279
16
            ub(5, initial_cpb_removal_delay_length_minus1);
280
16
            ub(5, au_cpb_removal_delay_length_minus1);
281
16
            ub(5, dpb_output_delay_length_minus1);
282
        } else {
283
            infer(sub_pic_hrd_params_present_flag, 0);
284
285
            infer(initial_cpb_removal_delay_length_minus1, 23);
286
            infer(au_cpb_removal_delay_length_minus1,      23);
287
            infer(dpb_output_delay_length_minus1,          23);
288
        }
289
    }
290
291
32
    for (i = 0; i <= max_num_sub_layers_minus1; i++) {
292
16
        flags(fixed_pic_rate_general_flag[i], 1, i);
293
294
16
        if (!current->fixed_pic_rate_general_flag[i])
295
8
            flags(fixed_pic_rate_within_cvs_flag[i], 1, i);
296
        else
297
8
            infer(fixed_pic_rate_within_cvs_flag[i], 1);
298
299
16
        if (current->fixed_pic_rate_within_cvs_flag[i]) {
300
8
            ues(elemental_duration_in_tc_minus1[i], 0, 2047, 1, i);
301
8
            infer(low_delay_hrd_flag[i], 0);
302
        } else
303
8
            flags(low_delay_hrd_flag[i], 1, i);
304
305
16
        if (!current->low_delay_hrd_flag[i])
306
16
            ues(cpb_cnt_minus1[i], 0, 31, 1, i);
307
        else
308
            infer(cpb_cnt_minus1[i], 0);
309
310
16
        if (current->nal_hrd_parameters_present_flag)
311
16
            CHECK(FUNC(sub_layer_hrd_parameters)(ctx, rw, current, 0, i));
312
16
        if (current->vcl_hrd_parameters_present_flag)
313
16
            CHECK(FUNC(sub_layer_hrd_parameters)(ctx, rw, current, 1, i));
314
    }
315
316
16
    return 0;
317
}
318
319
72
static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
320
                                H265RawVUI *current, const H265RawSPS *sps)
321
{
322
    int err;
323
324
72
    flag(aspect_ratio_info_present_flag);
325
72
    if (current->aspect_ratio_info_present_flag) {
326
24
        ub(8, aspect_ratio_idc);
327
24
        if (current->aspect_ratio_idc == 255) {
328
            ub(16, sar_width);
329
            ub(16, sar_height);
330
        }
331
    } else {
332
48
        infer(aspect_ratio_idc, 0);
333
    }
334
335
72
    flag(overscan_info_present_flag);
336
72
    if (current->overscan_info_present_flag)
337
        flag(overscan_appropriate_flag);
338
339
72
    flag(video_signal_type_present_flag);
340
72
    if (current->video_signal_type_present_flag) {
341
        ub(3, video_format);
342
        flag(video_full_range_flag);
343
        flag(colour_description_present_flag);
344
        if (current->colour_description_present_flag) {
345
            ub(8, colour_primaries);
346
            ub(8, transfer_characteristics);
347
            ub(8, matrix_coefficients);
348
        } else {
349
            infer(colour_primaries,         2);
350
            infer(transfer_characteristics, 2);
351
            infer(matrix_coefficients,      2);
352
        }
353
    } else {
354
72
        infer(video_format,             5);
355
72
        infer(video_full_range_flag,    0);
356
72
        infer(colour_primaries,         2);
357
72
        infer(transfer_characteristics, 2);
358
72
        infer(matrix_coefficients,      2);
359
    }
360
361
72
    flag(chroma_loc_info_present_flag);
362
72
    if (current->chroma_loc_info_present_flag) {
363
        ue(chroma_sample_loc_type_top_field,    0, 5);
364
        ue(chroma_sample_loc_type_bottom_field, 0, 5);
365
    } else {
366
72
        infer(chroma_sample_loc_type_top_field,    0);
367
72
        infer(chroma_sample_loc_type_bottom_field, 0);
368
    }
369
370
72
    flag(neutral_chroma_indication_flag);
371
72
    flag(field_seq_flag);
372
72
    flag(frame_field_info_present_flag);
373
374
72
    flag(default_display_window_flag);
375
72
    if (current->default_display_window_flag) {
376
24
        ue(def_disp_win_left_offset,   0, 16384);
377
24
        ue(def_disp_win_right_offset,  0, 16384);
378
24
        ue(def_disp_win_top_offset,    0, 16384);
379
24
        ue(def_disp_win_bottom_offset, 0, 16384);
380
    }
381
382
72
    flag(vui_timing_info_present_flag);
383
72
    if (current->vui_timing_info_present_flag) {
384
16
        u(32, vui_num_units_in_tick, 1, UINT32_MAX);
385
16
        u(32, vui_time_scale,        1, UINT32_MAX);
386
16
        flag(vui_poc_proportional_to_timing_flag);
387
16
        if (current->vui_poc_proportional_to_timing_flag)
388
8
            ue(vui_num_ticks_poc_diff_one_minus1, 0, UINT32_MAX - 1);
389
390
16
        flag(vui_hrd_parameters_present_flag);
391
16
        if (current->vui_hrd_parameters_present_flag) {
392
16
            CHECK(FUNC(hrd_parameters)(ctx, rw, &current->hrd_parameters,
393
                                       1, sps->sps_max_sub_layers_minus1));
394
        }
395
    }
396
397
72
    flag(bitstream_restriction_flag);
398
72
    if (current->bitstream_restriction_flag) {
399
48
        flag(tiles_fixed_structure_flag);
400
48
        flag(motion_vectors_over_pic_boundaries_flag);
401
48
        flag(restricted_ref_pic_lists_flag);
402
48
        ue(min_spatial_segmentation_idc,  0, 4095);
403
48
        ue(max_bytes_per_pic_denom,       0, 16);
404
48
        ue(max_bits_per_min_cu_denom,     0, 16);
405
48
        ue(log2_max_mv_length_horizontal, 0, 16);
406
48
        ue(log2_max_mv_length_vertical,   0, 16);
407
    } else {
408
24
        infer(tiles_fixed_structure_flag,    0);
409
24
        infer(motion_vectors_over_pic_boundaries_flag, 1);
410
24
        infer(min_spatial_segmentation_idc,  0);
411
24
        infer(max_bytes_per_pic_denom,       2);
412
24
        infer(max_bits_per_min_cu_denom,     1);
413
24
        infer(log2_max_mv_length_horizontal, 15);
414
24
        infer(log2_max_mv_length_vertical,   15);
415
    }
416
417
72
    return 0;
418
}
419
420
192
static int FUNC(vps)(CodedBitstreamContext *ctx, RWContext *rw,
421
                     H265RawVPS *current)
422
{
423
    int err, i, j;
424
425
192
    HEADER("Video Parameter Set");
426
427
192
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_VPS));
428
429
192
    ub(4, vps_video_parameter_set_id);
430
431
192
    flag(vps_base_layer_internal_flag);
432
192
    flag(vps_base_layer_available_flag);
433
192
    u(6, vps_max_layers_minus1,     0, HEVC_MAX_LAYERS - 1);
434
192
    u(3, vps_max_sub_layers_minus1, 0, HEVC_MAX_SUB_LAYERS - 1);
435
192
    flag(vps_temporal_id_nesting_flag);
436
437
192
    if (current->vps_max_sub_layers_minus1 == 0 &&
438
144
        current->vps_temporal_id_nesting_flag != 1) {
439
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
440
               "vps_temporal_id_nesting_flag must be 1 if "
441
               "vps_max_sub_layers_minus1 is 0.\n");
442
        return AVERROR_INVALIDDATA;
443
    }
444
445
192
    fixed(16, vps_reserved_0xffff_16bits, 0xffff);
446
447
192
    CHECK(FUNC(profile_tier_level)(ctx, rw, &current->profile_tier_level,
448
                                   1, current->vps_max_sub_layers_minus1));
449
450
192
    flag(vps_sub_layer_ordering_info_present_flag);
451
192
    for (i = (current->vps_sub_layer_ordering_info_present_flag ?
452
192
              0 : current->vps_max_sub_layers_minus1);
453
400
         i <= current->vps_max_sub_layers_minus1; i++) {
454
208
        ues(vps_max_dec_pic_buffering_minus1[i],
455
            0, HEVC_MAX_DPB_SIZE - 1,                        1, i);
456
208
        ues(vps_max_num_reorder_pics[i],
457
            0, current->vps_max_dec_pic_buffering_minus1[i], 1, i);
458
208
        ues(vps_max_latency_increase_plus1[i],
459
            0, UINT32_MAX - 1,                               1, i);
460
    }
461
192
    if (!current->vps_sub_layer_ordering_info_present_flag) {
462
128
        for (i = 0; i < current->vps_max_sub_layers_minus1; i++) {
463
80
            infer(vps_max_dec_pic_buffering_minus1[i],
464
                  current->vps_max_dec_pic_buffering_minus1[current->vps_max_sub_layers_minus1]);
465
80
            infer(vps_max_num_reorder_pics[i],
466
                  current->vps_max_num_reorder_pics[current->vps_max_sub_layers_minus1]);
467
80
            infer(vps_max_latency_increase_plus1[i],
468
                  current->vps_max_latency_increase_plus1[current->vps_max_sub_layers_minus1]);
469
        }
470
    }
471
472
192
    u(6, vps_max_layer_id,        0, HEVC_MAX_LAYERS - 1);
473
192
    ue(vps_num_layer_sets_minus1, 0, HEVC_MAX_LAYER_SETS - 1);
474
192
    for (i = 1; i <= current->vps_num_layer_sets_minus1; i++) {
475
        for (j = 0; j <= current->vps_max_layer_id; j++)
476
            flags(layer_id_included_flag[i][j], 2, i, j);
477
    }
478
384
    for (j = 0; j <= current->vps_max_layer_id; j++)
479
192
        infer(layer_id_included_flag[0][j], j == 0);
480
481
192
    flag(vps_timing_info_present_flag);
482
192
    if (current->vps_timing_info_present_flag) {
483
8
        u(32, vps_num_units_in_tick, 1, UINT32_MAX);
484
8
        u(32, vps_time_scale,        1, UINT32_MAX);
485
8
        flag(vps_poc_proportional_to_timing_flag);
486
8
        if (current->vps_poc_proportional_to_timing_flag)
487
8
            ue(vps_num_ticks_poc_diff_one_minus1, 0, UINT32_MAX - 1);
488
8
        ue(vps_num_hrd_parameters, 0, current->vps_num_layer_sets_minus1 + 1);
489
8
        for (i = 0; i < current->vps_num_hrd_parameters; i++) {
490
            ues(hrd_layer_set_idx[i],
491
                current->vps_base_layer_internal_flag ? 0 : 1,
492
                current->vps_num_layer_sets_minus1, 1, i);
493
            if (i > 0)
494
                flags(cprms_present_flag[i], 1, i);
495
            else
496
                infer(cprms_present_flag[0], 1);
497
498
            CHECK(FUNC(hrd_parameters)(ctx, rw, &current->hrd_parameters[i],
499
                                       current->cprms_present_flag[i],
500
                                       current->vps_max_sub_layers_minus1));
501
        }
502
    }
503
504
192
    flag(vps_extension_flag);
505
192
    if (current->vps_extension_flag)
506
        CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
507
508
192
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
509
510
192
    return 0;
511
}
512
513
9116
static int FUNC(st_ref_pic_set)(CodedBitstreamContext *ctx, RWContext *rw,
514
                                H265RawSTRefPicSet *current, int st_rps_idx,
515
                                const H265RawSPS *sps)
516
{
517
    int err, i, j;
518
519
9116
    if (st_rps_idx != 0)
520
8820
        flag(inter_ref_pic_set_prediction_flag);
521
    else
522
296
        infer(inter_ref_pic_set_prediction_flag, 0);
523
524
9116
    if (current->inter_ref_pic_set_prediction_flag) {
525
        unsigned int ref_rps_idx, num_delta_pocs, num_ref_pics;
526
        const H265RawSTRefPicSet *ref;
527
        int delta_rps, d_poc;
528
        int ref_delta_poc_s0[HEVC_MAX_REFS], ref_delta_poc_s1[HEVC_MAX_REFS];
529
        int delta_poc_s0[HEVC_MAX_REFS], delta_poc_s1[HEVC_MAX_REFS];
530
        uint8_t used_by_curr_pic_s0[HEVC_MAX_REFS],
531
                used_by_curr_pic_s1[HEVC_MAX_REFS];
532
533
2048
        if (st_rps_idx == sps->num_short_term_ref_pic_sets)
534
744
            ue(delta_idx_minus1, 0, st_rps_idx - 1);
535
        else
536
1304
            infer(delta_idx_minus1, 0);
537
538
2048
        ref_rps_idx = st_rps_idx - (current->delta_idx_minus1 + 1);
539
2048
        ref = &sps->st_ref_pic_set[ref_rps_idx];
540
2048
        num_delta_pocs = ref->num_negative_pics + ref->num_positive_pics;
541
2048
        av_assert0(num_delta_pocs < HEVC_MAX_DPB_SIZE);
542
543
2048
        flag(delta_rps_sign);
544
2048
        ue(abs_delta_rps_minus1, 0, INT16_MAX);
545
2048
        delta_rps = (1 - 2 * current->delta_rps_sign) *
546
2048
            (current->abs_delta_rps_minus1 + 1);
547
548
2048
        num_ref_pics = 0;
549
11016
        for (j = 0; j <= num_delta_pocs; j++) {
550
8968
            flags(used_by_curr_pic_flag[j], 1, j);
551
8968
            if (!current->used_by_curr_pic_flag[j])
552
2044
                flags(use_delta_flag[j], 1, j);
553
            else
554
6924
                infer(use_delta_flag[j], 1);
555
8968
            if (current->use_delta_flag[j])
556
6924
                ++num_ref_pics;
557
        }
558
2048
        if (num_ref_pics >= HEVC_MAX_DPB_SIZE) {
559
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
560
                   "short-term ref pic set %d "
561
                   "contains too many pictures.\n", st_rps_idx);
562
            return AVERROR_INVALIDDATA;
563
        }
564
565
        // Since the stored form of an RPS here is actually the delta-step
566
        // form used when inter_ref_pic_set_prediction_flag is not set, we
567
        // need to reconstruct that here in order to be able to refer to
568
        // the RPS later (which is required for parsing, because we don't
569
        // even know what syntax elements appear without it).  Therefore,
570
        // this code takes the delta-step form of the reference set, turns
571
        // it into the delta-array form, applies the prediction process of
572
        // 7.4.8, converts the result back to the delta-step form, and
573
        // stores that as the current set for future use.  Note that the
574
        // inferences here mean that writers using prediction will need
575
        // to fill in the delta-step values correctly as well - since the
576
        // whole RPS prediction process is somewhat overly sophisticated,
577
        // this hopefully forms a useful check for them to ensure their
578
        // predicted form actually matches what was intended rather than
579
        // an onerous additional requirement.
580
581
2048
        d_poc = 0;
582
6724
        for (i = 0; i < ref->num_negative_pics; i++) {
583
4676
            d_poc -= ref->delta_poc_s0_minus1[i] + 1;
584
4676
            ref_delta_poc_s0[i] = d_poc;
585
        }
586
2048
        d_poc = 0;
587
4292
        for (i = 0; i < ref->num_positive_pics; i++) {
588
2244
            d_poc += ref->delta_poc_s1_minus1[i] + 1;
589
2244
            ref_delta_poc_s1[i] = d_poc;
590
        }
591
592
2048
        i = 0;
593
4292
        for (j = ref->num_positive_pics - 1; j >= 0; j--) {
594
2244
            d_poc = ref_delta_poc_s1[j] + delta_rps;
595

2244
            if (d_poc < 0 && current->use_delta_flag[ref->num_negative_pics + j]) {
596
576
                delta_poc_s0[i] = d_poc;
597
576
                used_by_curr_pic_s0[i++] =
598
576
                    current->used_by_curr_pic_flag[ref->num_negative_pics + j];
599
            }
600
        }
601

2048
        if (delta_rps < 0 && current->use_delta_flag[num_delta_pocs]) {
602
472
            delta_poc_s0[i] = delta_rps;
603
472
            used_by_curr_pic_s0[i++] =
604
472
                current->used_by_curr_pic_flag[num_delta_pocs];
605
        }
606
6724
        for (j = 0; j < ref->num_negative_pics; j++) {
607
4676
            d_poc = ref_delta_poc_s0[j] + delta_rps;
608

4676
            if (d_poc < 0 && current->use_delta_flag[j]) {
609
3400
                delta_poc_s0[i] = d_poc;
610
3400
                used_by_curr_pic_s0[i++] = current->used_by_curr_pic_flag[j];
611
            }
612
        }
613
614
2048
        infer(num_negative_pics, i);
615
6496
        for (i = 0; i < current->num_negative_pics; i++) {
616

4448
            infer(delta_poc_s0_minus1[i],
617
                  -(delta_poc_s0[i] - (i == 0 ? 0 : delta_poc_s0[i - 1])) - 1);
618
4448
            infer(used_by_curr_pic_s0_flag[i], used_by_curr_pic_s0[i]);
619
        }
620
621
2048
        i = 0;
622
6724
        for (j = ref->num_negative_pics - 1; j >= 0; j--) {
623
4676
            d_poc = ref_delta_poc_s0[j] + delta_rps;
624

4676
            if (d_poc > 0 && current->use_delta_flag[j]) {
625
                delta_poc_s1[i] = d_poc;
626
                used_by_curr_pic_s1[i++] = current->used_by_curr_pic_flag[j];
627
            }
628
        }
629

2048
        if (delta_rps > 0 && current->use_delta_flag[num_delta_pocs]) {
630
932
            delta_poc_s1[i] = delta_rps;
631
932
            used_by_curr_pic_s1[i++] =
632
932
                current->used_by_curr_pic_flag[num_delta_pocs];
633
        }
634
4292
        for (j = 0; j < ref->num_positive_pics; j++) {
635
2244
            d_poc = ref_delta_poc_s1[j] + delta_rps;
636

2244
            if (d_poc > 0 && current->use_delta_flag[ref->num_negative_pics + j]) {
637
1544
                delta_poc_s1[i] = d_poc;
638
1544
                used_by_curr_pic_s1[i++] =
639
1544
                    current->used_by_curr_pic_flag[ref->num_negative_pics + j];
640
            }
641
        }
642
643
2048
        infer(num_positive_pics, i);
644
4524
        for (i = 0; i < current->num_positive_pics; i++) {
645

2476
            infer(delta_poc_s1_minus1[i],
646
                  delta_poc_s1[i] - (i == 0 ? 0 : delta_poc_s1[i - 1]) - 1);
647
2476
            infer(used_by_curr_pic_s1_flag[i], used_by_curr_pic_s1[i]);
648
        }
649
650
    } else {
651
7068
        ue(num_negative_pics, 0, 15);
652
7068
        ue(num_positive_pics, 0, 15 - current->num_negative_pics);
653
654
26492
        for (i = 0; i < current->num_negative_pics; i++) {
655
19424
            ues(delta_poc_s0_minus1[i], 0, INT16_MAX, 1, i);
656
19424
            flags(used_by_curr_pic_s0_flag[i],        1, i);
657
        }
658
659
8688
        for (i = 0; i < current->num_positive_pics; i++) {
660
1620
            ues(delta_poc_s1_minus1[i], 0, INT16_MAX, 1, i);
661
1620
            flags(used_by_curr_pic_s1_flag[i],        1, i);
662
        }
663
    }
664
665
9116
    return 0;
666
}
667
668
352
static int FUNC(scaling_list_data)(CodedBitstreamContext *ctx, RWContext *rw,
669
                                   H265RawScalingList *current)
670
{
671
    int sizeId, matrixId;
672
    int err, n, i;
673
674
1760
    for (sizeId = 0; sizeId < 4; sizeId++) {
675

8448
        for (matrixId = 0; matrixId < 6; matrixId += (sizeId == 3 ? 3 : 1)) {
676
7040
            flags(scaling_list_pred_mode_flag[sizeId][matrixId],
677
                  2, sizeId, matrixId);
678
7040
            if (!current->scaling_list_pred_mode_flag[sizeId][matrixId]) {
679

3128
                ues(scaling_list_pred_matrix_id_delta[sizeId][matrixId],
680
                    0, sizeId == 3 ? matrixId / 3 : matrixId,
681
                    2, sizeId, matrixId);
682
            } else {
683
3912
                n = FFMIN(64, 1 << (4 + (sizeId << 1)));
684
3912
                if (sizeId > 1) {
685
2376
                    ses(scaling_list_dc_coef_minus8[sizeId - 2][matrixId], -7, +247,
686
                        2, sizeId - 2, matrixId);
687
                }
688
215112
                for (i = 0; i < n; i++) {
689
211200
                    ses(scaling_list_delta_coeff[sizeId][matrixId][i],
690
                        -128, +127, 3, sizeId, matrixId, i);
691
                }
692
            }
693
        }
694
    }
695
696
352
    return 0;
697
}
698
699
static int FUNC(sps_range_extension)(CodedBitstreamContext *ctx, RWContext *rw,
700
                                     H265RawSPS *current)
701
{
702
    int err;
703
704
    flag(transform_skip_rotation_enabled_flag);
705
    flag(transform_skip_context_enabled_flag);
706
    flag(implicit_rdpcm_enabled_flag);
707
    flag(explicit_rdpcm_enabled_flag);
708
    flag(extended_precision_processing_flag);
709
    flag(intra_smoothing_disabled_flag);
710
    flag(high_precision_offsets_enabled_flag);
711
    flag(persistent_rice_adaptation_enabled_flag);
712
    flag(cabac_bypass_alignment_enabled_flag);
713
714
    return 0;
715
}
716
717
static int FUNC(sps_scc_extension)(CodedBitstreamContext *ctx, RWContext *rw,
718
                                   H265RawSPS *current)
719
{
720
    int err, comp, i;
721
722
    flag(sps_curr_pic_ref_enabled_flag);
723
724
    flag(palette_mode_enabled_flag);
725
    if (current->palette_mode_enabled_flag) {
726
        ue(palette_max_size, 0, 64);
727
        ue(delta_palette_max_predictor_size, 0, 128);
728
729
        flag(sps_palette_predictor_initializer_present_flag);
730
        if (current->sps_palette_predictor_initializer_present_flag) {
731
            ue(sps_num_palette_predictor_initializer_minus1, 0, 128);
732
            for (comp = 0; comp < (current->chroma_format_idc ? 3 : 1); comp++) {
733
                int bit_depth = comp == 0 ? current->bit_depth_luma_minus8 + 8
734
                                          : current->bit_depth_chroma_minus8 + 8;
735
                for (i = 0; i <= current->sps_num_palette_predictor_initializer_minus1; i++)
736
                    ubs(bit_depth, sps_palette_predictor_initializers[comp][i], 2, comp, i);
737
            }
738
        }
739
    }
740
741
    u(2, motion_vector_resolution_control_idc, 0, 2);
742
    flag(intra_boundary_filtering_disable_flag);
743
744
    return 0;
745
}
746
747
128
static int FUNC(vui_parameters_default)(CodedBitstreamContext *ctx,
748
                                        RWContext *rw, H265RawVUI *current,
749
                                        H265RawSPS *sps)
750
{
751
128
    infer(aspect_ratio_idc, 0);
752
753
128
    infer(video_format,             5);
754
128
    infer(video_full_range_flag,    0);
755
128
    infer(colour_primaries,         2);
756
128
    infer(transfer_characteristics, 2);
757
128
    infer(matrix_coefficients,      2);
758
759
128
    infer(chroma_sample_loc_type_top_field,    0);
760
128
    infer(chroma_sample_loc_type_bottom_field, 0);
761
762
128
    infer(tiles_fixed_structure_flag,    0);
763
128
    infer(motion_vectors_over_pic_boundaries_flag, 1);
764
128
    infer(min_spatial_segmentation_idc,  0);
765
128
    infer(max_bytes_per_pic_denom,       2);
766
128
    infer(max_bits_per_min_cu_denom,     1);
767
128
    infer(log2_max_mv_length_horizontal, 15);
768
128
    infer(log2_max_mv_length_vertical,   15);
769
770
128
    return 0;
771
}
772
773
200
static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
774
                     H265RawSPS *current)
775
{
776
200
    CodedBitstreamH265Context *h265 = ctx->priv_data;
777
    const H265RawVPS *vps;
778
    int err, i;
779
    unsigned int min_cb_log2_size_y, ctb_log2_size_y,
780
                 min_cb_size_y,   min_tb_log2_size_y;
781
782
200
    HEADER("Sequence Parameter Set");
783
784
200
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_SPS));
785
786
200
    ub(4, sps_video_parameter_set_id);
787
200
    h265->active_vps = vps = h265->vps[current->sps_video_parameter_set_id];
788
789
200
    u(3, sps_max_sub_layers_minus1, 0, HEVC_MAX_SUB_LAYERS - 1);
790
200
    flag(sps_temporal_id_nesting_flag);
791
200
    if (vps) {
792
200
        if (vps->vps_max_sub_layers_minus1 > current->sps_max_sub_layers_minus1) {
793
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
794
                   "sps_max_sub_layers_minus1 (%d) must be less than or equal to "
795
                   "vps_max_sub_layers_minus1 (%d).\n",
796
                   vps->vps_max_sub_layers_minus1,
797
                   current->sps_max_sub_layers_minus1);
798
            return AVERROR_INVALIDDATA;
799
        }
800
200
        if (vps->vps_temporal_id_nesting_flag &&
801
152
            !current->sps_temporal_id_nesting_flag) {
802
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid stream: "
803
                   "sps_temporal_id_nesting_flag must be 1 if "
804
                   "vps_temporal_id_nesting_flag is 1.\n");
805
            return AVERROR_INVALIDDATA;
806
        }
807
    }
808
809
200
    CHECK(FUNC(profile_tier_level)(ctx, rw, &current->profile_tier_level,
810
                                   1, current->sps_max_sub_layers_minus1));
811
812
200
    ue(sps_seq_parameter_set_id, 0, 15);
813
814
200
    ue(chroma_format_idc, 0, 3);
815
200
    if (current->chroma_format_idc == 3)
816
        flag(separate_colour_plane_flag);
817
    else
818
200
        infer(separate_colour_plane_flag, 0);
819
820
200
    ue(pic_width_in_luma_samples,  1, HEVC_MAX_WIDTH);
821
200
    ue(pic_height_in_luma_samples, 1, HEVC_MAX_HEIGHT);
822
823
200
    flag(conformance_window_flag);
824
200
    if (current->conformance_window_flag) {
825
152
        ue(conf_win_left_offset,   0, current->pic_width_in_luma_samples);
826
152
        ue(conf_win_right_offset,  0, current->pic_width_in_luma_samples);
827
152
        ue(conf_win_top_offset,    0, current->pic_height_in_luma_samples);
828
152
        ue(conf_win_bottom_offset, 0, current->pic_height_in_luma_samples);
829
    } else {
830
48
        infer(conf_win_left_offset,   0);
831
48
        infer(conf_win_right_offset,  0);
832
48
        infer(conf_win_top_offset,    0);
833
48
        infer(conf_win_bottom_offset, 0);
834
    }
835
836
200
    ue(bit_depth_luma_minus8,   0, 8);
837
200
    ue(bit_depth_chroma_minus8, 0, 8);
838
839
200
    ue(log2_max_pic_order_cnt_lsb_minus4, 0, 12);
840
841
200
    flag(sps_sub_layer_ordering_info_present_flag);
842
200
    for (i = (current->sps_sub_layer_ordering_info_present_flag ?
843
200
              0 : current->sps_max_sub_layers_minus1);
844
416
         i <= current->sps_max_sub_layers_minus1; i++) {
845
216
        ues(sps_max_dec_pic_buffering_minus1[i],
846
            0, HEVC_MAX_DPB_SIZE - 1,                        1, i);
847
216
        ues(sps_max_num_reorder_pics[i],
848
            0, current->sps_max_dec_pic_buffering_minus1[i], 1, i);
849
216
        ues(sps_max_latency_increase_plus1[i],
850
            0, UINT32_MAX - 1,                               1, i);
851
    }
852
200
    if (!current->sps_sub_layer_ordering_info_present_flag) {
853
128
        for (i = 0; i < current->sps_max_sub_layers_minus1; i++) {
854
80
            infer(sps_max_dec_pic_buffering_minus1[i],
855
                  current->sps_max_dec_pic_buffering_minus1[current->sps_max_sub_layers_minus1]);
856
80
            infer(sps_max_num_reorder_pics[i],
857
                  current->sps_max_num_reorder_pics[current->sps_max_sub_layers_minus1]);
858
80
            infer(sps_max_latency_increase_plus1[i],
859
                  current->sps_max_latency_increase_plus1[current->sps_max_sub_layers_minus1]);
860
        }
861
    }
862
863
200
    ue(log2_min_luma_coding_block_size_minus3,   0, 3);
864
200
    min_cb_log2_size_y = current->log2_min_luma_coding_block_size_minus3 + 3;
865
866
200
    ue(log2_diff_max_min_luma_coding_block_size, 0, 3);
867
200
    ctb_log2_size_y = min_cb_log2_size_y +
868
200
        current->log2_diff_max_min_luma_coding_block_size;
869
870
200
    min_cb_size_y = 1 << min_cb_log2_size_y;
871
200
    if (current->pic_width_in_luma_samples  % min_cb_size_y ||
872
200
        current->pic_height_in_luma_samples % min_cb_size_y) {
873
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid dimensions: %ux%u not divisible "
874
               "by MinCbSizeY = %u.\n", current->pic_width_in_luma_samples,
875
               current->pic_height_in_luma_samples, min_cb_size_y);
876
        return AVERROR_INVALIDDATA;
877
    }
878
879
200
    ue(log2_min_luma_transform_block_size_minus2, 0, min_cb_log2_size_y - 3);
880
200
    min_tb_log2_size_y = current->log2_min_luma_transform_block_size_minus2 + 2;
881
882
200
    ue(log2_diff_max_min_luma_transform_block_size,
883
       0, FFMIN(ctb_log2_size_y, 5) - min_tb_log2_size_y);
884
885
200
    ue(max_transform_hierarchy_depth_inter,
886
       0, ctb_log2_size_y - min_tb_log2_size_y);
887
200
    ue(max_transform_hierarchy_depth_intra,
888
       0, ctb_log2_size_y - min_tb_log2_size_y);
889
890
200
    flag(scaling_list_enabled_flag);
891
200
    if (current->scaling_list_enabled_flag) {
892
16
        flag(sps_scaling_list_data_present_flag);
893
16
        if (current->sps_scaling_list_data_present_flag)
894
16
            CHECK(FUNC(scaling_list_data)(ctx, rw, &current->scaling_list));
895
    } else {
896
184
        infer(sps_scaling_list_data_present_flag, 0);
897
    }
898
899
200
    flag(amp_enabled_flag);
900
200
    flag(sample_adaptive_offset_enabled_flag);
901
902
200
    flag(pcm_enabled_flag);
903
200
    if (current->pcm_enabled_flag) {
904
8
        u(4, pcm_sample_bit_depth_luma_minus1,
905
          0, current->bit_depth_luma_minus8 + 8 - 1);
906
8
        u(4, pcm_sample_bit_depth_chroma_minus1,
907
          0, current->bit_depth_chroma_minus8 + 8 - 1);
908
909
8
        ue(log2_min_pcm_luma_coding_block_size_minus3,
910
           FFMIN(min_cb_log2_size_y, 5) - 3, FFMIN(ctb_log2_size_y, 5) - 3);
911
8
        ue(log2_diff_max_min_pcm_luma_coding_block_size,
912
           0, FFMIN(ctb_log2_size_y, 5) - (current->log2_min_pcm_luma_coding_block_size_minus3 + 3));
913
914
8
        flag(pcm_loop_filter_disabled_flag);
915
    }
916
917
200
    ue(num_short_term_ref_pic_sets, 0, HEVC_MAX_SHORT_TERM_REF_PIC_SETS);
918
1856
    for (i = 0; i < current->num_short_term_ref_pic_sets; i++)
919
1656
        CHECK(FUNC(st_ref_pic_set)(ctx, rw, &current->st_ref_pic_set[i], i, current));
920
921
200
    flag(long_term_ref_pics_present_flag);
922
200
    if (current->long_term_ref_pics_present_flag) {
923
56
        ue(num_long_term_ref_pics_sps, 0, HEVC_MAX_LONG_TERM_REF_PICS);
924
120
        for (i = 0; i < current->num_long_term_ref_pics_sps; i++) {
925
64
            ubs(current->log2_max_pic_order_cnt_lsb_minus4 + 4,
926
                lt_ref_pic_poc_lsb_sps[i], 1, i);
927
64
            flags(used_by_curr_pic_lt_sps_flag[i], 1, i);
928
        }
929
    }
930
931
200
    flag(sps_temporal_mvp_enabled_flag);
932
200
    flag(strong_intra_smoothing_enabled_flag);
933
934
200
    flag(vui_parameters_present_flag);
935
200
    if (current->vui_parameters_present_flag)
936
72
        CHECK(FUNC(vui_parameters)(ctx, rw, &current->vui, current));
937
    else
938
128
        CHECK(FUNC(vui_parameters_default)(ctx, rw, &current->vui, current));
939
940
200
    flag(sps_extension_present_flag);
941
200
    if (current->sps_extension_present_flag) {
942
        flag(sps_range_extension_flag);
943
        flag(sps_multilayer_extension_flag);
944
        flag(sps_3d_extension_flag);
945
        flag(sps_scc_extension_flag);
946
        ub(4, sps_extension_4bits);
947
    }
948
949
200
    if (current->sps_range_extension_flag)
950
        CHECK(FUNC(sps_range_extension)(ctx, rw, current));
951
200
    if (current->sps_multilayer_extension_flag)
952
        return AVERROR_PATCHWELCOME;
953
200
    if (current->sps_3d_extension_flag)
954
        return AVERROR_PATCHWELCOME;
955
200
    if (current->sps_scc_extension_flag)
956
        CHECK(FUNC(sps_scc_extension)(ctx, rw, current));
957
200
    if (current->sps_extension_4bits)
958
        CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
959
960
200
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
961
962
200
    return 0;
963
}
964
965
static int FUNC(pps_range_extension)(CodedBitstreamContext *ctx, RWContext *rw,
966
                                     H265RawPPS *current)
967
{
968
    CodedBitstreamH265Context *h265 = ctx->priv_data;
969
    const H265RawSPS *sps = h265->active_sps;
970
    int err, i;
971
972
    if (current->transform_skip_enabled_flag)
973
        ue(log2_max_transform_skip_block_size_minus2, 0, 3);
974
    flag(cross_component_prediction_enabled_flag);
975
976
    flag(chroma_qp_offset_list_enabled_flag);
977
    if (current->chroma_qp_offset_list_enabled_flag) {
978
        ue(diff_cu_chroma_qp_offset_depth,
979
           0, sps->log2_diff_max_min_luma_coding_block_size);
980
        ue(chroma_qp_offset_list_len_minus1, 0, 5);
981
        for (i = 0; i <= current->chroma_qp_offset_list_len_minus1; i++) {
982
            ses(cb_qp_offset_list[i], -12, +12, 1, i);
983
            ses(cr_qp_offset_list[i], -12, +12, 1, i);
984
        }
985
    }
986
987
    ue(log2_sao_offset_scale_luma,   0, FFMAX(0, sps->bit_depth_luma_minus8   - 2));
988
    ue(log2_sao_offset_scale_chroma, 0, FFMAX(0, sps->bit_depth_chroma_minus8 - 2));
989
990
    return 0;
991
}
992
993
static int FUNC(pps_scc_extension)(CodedBitstreamContext *ctx, RWContext *rw,
994
                                   H265RawPPS *current)
995
{
996
    int err, comp, i;
997
998
    flag(pps_curr_pic_ref_enabled_flag);
999
1000
    flag(residual_adaptive_colour_transform_enabled_flag);
1001
    if (current->residual_adaptive_colour_transform_enabled_flag) {
1002
        flag(pps_slice_act_qp_offsets_present_flag);
1003
        se(pps_act_y_qp_offset_plus5,  -7, +17);
1004
        se(pps_act_cb_qp_offset_plus5, -7, +17);
1005
        se(pps_act_cr_qp_offset_plus3, -9, +15);
1006
    } else {
1007
        infer(pps_slice_act_qp_offsets_present_flag, 0);
1008
        infer(pps_act_y_qp_offset_plus5,  0);
1009
        infer(pps_act_cb_qp_offset_plus5, 0);
1010
        infer(pps_act_cr_qp_offset_plus3, 0);
1011
    }
1012
1013
    flag(pps_palette_predictor_initializer_present_flag);
1014
    if (current->pps_palette_predictor_initializer_present_flag) {
1015
        ue(pps_num_palette_predictor_initializer, 0, 128);
1016
        if (current->pps_num_palette_predictor_initializer > 0) {
1017
            flag(monochrome_palette_flag);
1018
            ue(luma_bit_depth_entry_minus8, 0, 8);
1019
            if (!current->monochrome_palette_flag)
1020
                ue(chroma_bit_depth_entry_minus8, 0, 8);
1021
            for (comp = 0; comp < (current->monochrome_palette_flag ? 1 : 3); comp++) {
1022
                int bit_depth = comp == 0 ? current->luma_bit_depth_entry_minus8 + 8
1023
                                          : current->chroma_bit_depth_entry_minus8 + 8;
1024
                for (i = 0; i < current->pps_num_palette_predictor_initializer; i++)
1025
                    ubs(bit_depth, pps_palette_predictor_initializers[comp][i], 2, comp, i);
1026
            }
1027
        }
1028
    }
1029
1030
    return 0;
1031
}
1032
1033
1100
static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
1034
                     H265RawPPS *current)
1035
{
1036
1100
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1037
    const H265RawSPS *sps;
1038
    int err, i;
1039
1040
1100
    HEADER("Picture Parameter Set");
1041
1042
1100
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_PPS));
1043
1044
1100
    ue(pps_pic_parameter_set_id, 0, 63);
1045
1100
    ue(pps_seq_parameter_set_id, 0, 15);
1046
1100
    sps = h265->sps[current->pps_seq_parameter_set_id];
1047
1100
    if (!sps) {
1048
        av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
1049
               current->pps_seq_parameter_set_id);
1050
        return AVERROR_INVALIDDATA;
1051
    }
1052
1100
    h265->active_sps = sps;
1053
1054
1100
    flag(dependent_slice_segments_enabled_flag);
1055
1100
    flag(output_flag_present_flag);
1056
1100
    ub(3, num_extra_slice_header_bits);
1057
1100
    flag(sign_data_hiding_enabled_flag);
1058
1100
    flag(cabac_init_present_flag);
1059
1060
1100
    ue(num_ref_idx_l0_default_active_minus1, 0, 14);
1061
1100
    ue(num_ref_idx_l1_default_active_minus1, 0, 14);
1062
1063
1100
    se(init_qp_minus26, -(26 + 6 * sps->bit_depth_luma_minus8), +25);
1064
1065
1100
    flag(constrained_intra_pred_flag);
1066
1100
    flag(transform_skip_enabled_flag);
1067
1100
    flag(cu_qp_delta_enabled_flag);
1068
1100
    if (current->cu_qp_delta_enabled_flag)
1069
24
        ue(diff_cu_qp_delta_depth,
1070
           0, sps->log2_diff_max_min_luma_coding_block_size);
1071
    else
1072
1076
        infer(diff_cu_qp_delta_depth, 0);
1073
1074
1100
    se(pps_cb_qp_offset, -12, +12);
1075
1100
    se(pps_cr_qp_offset, -12, +12);
1076
1100
    flag(pps_slice_chroma_qp_offsets_present_flag);
1077
1078
1100
    flag(weighted_pred_flag);
1079
1100
    flag(weighted_bipred_flag);
1080
1081
1100
    flag(transquant_bypass_enabled_flag);
1082
1100
    flag(tiles_enabled_flag);
1083
1100
    flag(entropy_coding_sync_enabled_flag);
1084
1085
1100
    if (current->tiles_enabled_flag) {
1086
420
        ue(num_tile_columns_minus1, 0, HEVC_MAX_TILE_COLUMNS);
1087
420
        ue(num_tile_rows_minus1,    0, HEVC_MAX_TILE_ROWS);
1088
420
        flag(uniform_spacing_flag);
1089
420
        if (!current->uniform_spacing_flag) {
1090
2020
            for (i = 0; i < current->num_tile_columns_minus1; i++)
1091
1616
                ues(column_width_minus1[i], 0, sps->pic_width_in_luma_samples,  1, i);
1092
2020
            for (i = 0; i < current->num_tile_rows_minus1; i++)
1093
1616
                ues(row_height_minus1[i],   0, sps->pic_height_in_luma_samples, 1, i);
1094
        }
1095
420
        flag(loop_filter_across_tiles_enabled_flag);
1096
    } else {
1097
680
        infer(num_tile_columns_minus1, 0);
1098
680
        infer(num_tile_rows_minus1,    0);
1099
    }
1100
1101
1100
    flag(pps_loop_filter_across_slices_enabled_flag);
1102
1100
    flag(deblocking_filter_control_present_flag);
1103
1100
    if (current->deblocking_filter_control_present_flag) {
1104
452
        flag(deblocking_filter_override_enabled_flag);
1105
452
        flag(pps_deblocking_filter_disabled_flag);
1106
452
        if (!current->pps_deblocking_filter_disabled_flag) {
1107
444
            se(pps_beta_offset_div2, -6, +6);
1108
444
            se(pps_tc_offset_div2,   -6, +6);
1109
        } else {
1110
8
            infer(pps_beta_offset_div2, 0);
1111
8
            infer(pps_tc_offset_div2,   0);
1112
        }
1113
    } else {
1114
648
        infer(deblocking_filter_override_enabled_flag, 0);
1115
648
        infer(pps_deblocking_filter_disabled_flag,     0);
1116
648
        infer(pps_beta_offset_div2, 0);
1117
648
        infer(pps_tc_offset_div2,   0);
1118
    }
1119
1120
1100
    flag(pps_scaling_list_data_present_flag);
1121
1100
    if (current->pps_scaling_list_data_present_flag)
1122
336
        CHECK(FUNC(scaling_list_data)(ctx, rw, &current->scaling_list));
1123
1124
1100
    flag(lists_modification_present_flag);
1125
1126
1100
    ue(log2_parallel_merge_level_minus2,
1127
       0, (sps->log2_min_luma_coding_block_size_minus3 + 3 +
1128
           sps->log2_diff_max_min_luma_coding_block_size - 2));
1129
1130
1100
    flag(slice_segment_header_extension_present_flag);
1131
1132
1100
    flag(pps_extension_present_flag);
1133
1100
    if (current->pps_extension_present_flag) {
1134
        flag(pps_range_extension_flag);
1135
        flag(pps_multilayer_extension_flag);
1136
        flag(pps_3d_extension_flag);
1137
        flag(pps_scc_extension_flag);
1138
        ub(4, pps_extension_4bits);
1139
    }
1140
1100
    if (current->pps_range_extension_flag)
1141
        CHECK(FUNC(pps_range_extension)(ctx, rw, current));
1142
1100
    if (current->pps_multilayer_extension_flag)
1143
        return AVERROR_PATCHWELCOME;
1144
1100
    if (current->pps_3d_extension_flag)
1145
        return AVERROR_PATCHWELCOME;
1146
1100
    if (current->pps_scc_extension_flag)
1147
        CHECK(FUNC(pps_scc_extension)(ctx, rw, current));
1148
1100
    if (current->pps_extension_4bits)
1149
        CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
1150
1151
1100
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
1152
1153
1100
    return 0;
1154
}
1155
1156
240
static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
1157
                     H265RawAUD *current)
1158
{
1159
    int err;
1160
1161
240
    HEADER("Access Unit Delimiter");
1162
1163
240
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_AUD));
1164
1165
240
    u(3, pic_type, 0, 2);
1166
1167
240
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
1168
1169
240
    return 0;
1170
}
1171
1172
2992
static int FUNC(ref_pic_lists_modification)(CodedBitstreamContext *ctx, RWContext *rw,
1173
                                            H265RawSliceHeader *current,
1174
                                            unsigned int num_pic_total_curr)
1175
{
1176
    unsigned int entry_size;
1177
    int err, i;
1178
1179
2992
    entry_size = av_log2(num_pic_total_curr - 1) + 1;
1180
1181
2992
    flag(ref_pic_list_modification_flag_l0);
1182
2992
    if (current->ref_pic_list_modification_flag_l0) {
1183
4796
        for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++)
1184
3344
            us(entry_size, list_entry_l0[i], 0, num_pic_total_curr - 1, 1, i);
1185
    }
1186
1187
2992
    if (current->slice_type == HEVC_SLICE_B) {
1188
2992
        flag(ref_pic_list_modification_flag_l1);
1189
2992
        if (current->ref_pic_list_modification_flag_l1) {
1190
2872
            for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++)
1191
2020
                us(entry_size, list_entry_l1[i], 0, num_pic_total_curr - 1, 1, i);
1192
        }
1193
    }
1194
1195
2992
    return 0;
1196
}
1197
1198
1020
static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
1199
                                   H265RawSliceHeader *current)
1200
{
1201
1020
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1202
1020
    const H265RawSPS *sps = h265->active_sps;
1203
    int err, i, j;
1204
2040
    int chroma = !sps->separate_colour_plane_flag &&
1205
1020
                  sps->chroma_format_idc != 0;
1206
1207
1020
    ue(luma_log2_weight_denom, 0, 7);
1208
1020
    if (chroma)
1209
1020
        se(delta_chroma_log2_weight_denom, -7, 7);
1210
    else
1211
        infer(delta_chroma_log2_weight_denom, 0);
1212
1213
5076
    for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
1214
        if (1 /* is not same POC and same layer_id */)
1215
4056
            flags(luma_weight_l0_flag[i], 1, i);
1216
        else
1217
            infer(luma_weight_l0_flag[i], 0);
1218
    }
1219
1020
    if (chroma) {
1220
5076
        for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
1221
            if (1 /* is not same POC and same layer_id */)
1222
4056
                flags(chroma_weight_l0_flag[i], 1, i);
1223
            else
1224
                infer(chroma_weight_l0_flag[i], 0);
1225
        }
1226
    }
1227
1228
5076
    for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
1229
4056
        if (current->luma_weight_l0_flag[i]) {
1230
3080
            ses(delta_luma_weight_l0[i], -128, +127, 1, i);
1231
3080
            ses(luma_offset_l0[i],
1232
                -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
1233
                ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1), 1, i);
1234
        } else {
1235
976
            infer(delta_luma_weight_l0[i], 0);
1236
976
            infer(luma_offset_l0[i],       0);
1237
        }
1238
4056
        if (current->chroma_weight_l0_flag[i]) {
1239
3036
            for (j = 0; j < 2; j++) {
1240
2024
                ses(delta_chroma_weight_l0[i][j], -128, +127, 2, i, j);
1241
2024
                ses(chroma_offset_l0[i][j],
1242
                    -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
1243
                    ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1), 2, i, j);
1244
            }
1245
        } else {
1246
9132
            for (j = 0; j < 2; j++) {
1247
6088
                infer(delta_chroma_weight_l0[i][j], 0);
1248
6088
                infer(chroma_offset_l0[i][j],       0);
1249
            }
1250
        }
1251
    }
1252
1253
1020
    if (current->slice_type == HEVC_SLICE_B) {
1254
        for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
1255
            if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
1256
                flags(luma_weight_l1_flag[i], 1, i);
1257
            else
1258
                infer(luma_weight_l1_flag[i], 0);
1259
        }
1260
        if (chroma) {
1261
            for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
1262
                if (1 /* RefPicList1[i] is not CurrPic, nor is it in a different layer */)
1263
                    flags(chroma_weight_l1_flag[i], 1, i);
1264
                else
1265
                    infer(chroma_weight_l1_flag[i], 0);
1266
            }
1267
        }
1268
1269
        for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
1270
            if (current->luma_weight_l1_flag[i]) {
1271
                ses(delta_luma_weight_l1[i], -128, +127, 1, i);
1272
                ses(luma_offset_l1[i],
1273
                    -(1 << (sps->bit_depth_luma_minus8 + 8 - 1)),
1274
                    ((1 << (sps->bit_depth_luma_minus8 + 8 - 1)) - 1), 1, i);
1275
            } else {
1276
                infer(delta_luma_weight_l1[i], 0);
1277
                infer(luma_offset_l1[i],       0);
1278
            }
1279
            if (current->chroma_weight_l1_flag[i]) {
1280
                for (j = 0; j < 2; j++) {
1281
                    ses(delta_chroma_weight_l1[i][j], -128, +127, 2, i, j);
1282
                    ses(chroma_offset_l1[i][j],
1283
                        -(4 << (sps->bit_depth_chroma_minus8 + 8 - 1)),
1284
                        ((4 << (sps->bit_depth_chroma_minus8 + 8 - 1)) - 1), 2, i, j);
1285
                }
1286
            } else {
1287
                for (j = 0; j < 2; j++) {
1288
                    infer(delta_chroma_weight_l1[i][j], 0);
1289
                    infer(chroma_offset_l1[i][j],       0);
1290
                }
1291
            }
1292
        }
1293
    }
1294
1295
1020
    return 0;
1296
}
1297
1298
23456
static int FUNC(slice_segment_header)(CodedBitstreamContext *ctx, RWContext *rw,
1299
                                      H265RawSliceHeader *current)
1300
{
1301
23456
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1302
    const H265RawSPS *sps;
1303
    const H265RawPPS *pps;
1304
    unsigned int min_cb_log2_size_y, ctb_log2_size_y, ctb_size_y;
1305
    unsigned int pic_width_in_ctbs_y, pic_height_in_ctbs_y, pic_size_in_ctbs_y;
1306
23456
    unsigned int num_pic_total_curr = 0;
1307
    int err, i;
1308
1309
23456
    HEADER("Slice Segment Header");
1310
1311
23456
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, -1));
1312
1313
23456
    flag(first_slice_segment_in_pic_flag);
1314
1315
23456
    if (current->nal_unit_header.nal_unit_type >= HEVC_NAL_BLA_W_LP &&
1316
516
        current->nal_unit_header.nal_unit_type <= HEVC_NAL_RSV_IRAP_VCL23)
1317
516
        flag(no_output_of_prior_pics_flag);
1318
1319
23456
    ue(slice_pic_parameter_set_id, 0, 63);
1320
1321
23456
    pps = h265->pps[current->slice_pic_parameter_set_id];
1322
23456
    if (!pps) {
1323
        av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
1324
               current->slice_pic_parameter_set_id);
1325
        return AVERROR_INVALIDDATA;
1326
    }
1327
23456
    h265->active_pps = pps;
1328
1329
23456
    sps = h265->sps[pps->pps_seq_parameter_set_id];
1330
23456
    if (!sps) {
1331
        av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
1332
               pps->pps_seq_parameter_set_id);
1333
        return AVERROR_INVALIDDATA;
1334
    }
1335
23456
    h265->active_sps = sps;
1336
1337
23456
    min_cb_log2_size_y = sps->log2_min_luma_coding_block_size_minus3 + 3;
1338
23456
    ctb_log2_size_y = min_cb_log2_size_y + sps->log2_diff_max_min_luma_coding_block_size;
1339
23456
    ctb_size_y = 1 << ctb_log2_size_y;
1340
23456
    pic_width_in_ctbs_y =
1341
23456
        (sps->pic_width_in_luma_samples + ctb_size_y - 1) / ctb_size_y;
1342
23456
    pic_height_in_ctbs_y =
1343
23456
        (sps->pic_height_in_luma_samples + ctb_size_y - 1) / ctb_size_y;
1344
23456
    pic_size_in_ctbs_y = pic_width_in_ctbs_y * pic_height_in_ctbs_y;
1345
1346
23456
    if (!current->first_slice_segment_in_pic_flag) {
1347
14928
        unsigned int address_size = av_log2(pic_size_in_ctbs_y - 1) + 1;
1348
14928
        if (pps->dependent_slice_segments_enabled_flag)
1349
8164
            flag(dependent_slice_segment_flag);
1350
        else
1351
6764
            infer(dependent_slice_segment_flag, 0);
1352
14928
        u(address_size, slice_segment_address, 0, pic_size_in_ctbs_y - 1);
1353
    } else {
1354
8528
        infer(dependent_slice_segment_flag, 0);
1355
    }
1356
1357
23456
    if (!current->dependent_slice_segment_flag) {
1358
15920
        for (i = 0; i < pps->num_extra_slice_header_bits; i++)
1359
            flags(slice_reserved_flag[i], 1, i);
1360
1361
15920
        ue(slice_type, 0, 2);
1362
1363
15920
        if (pps->output_flag_present_flag)
1364
            flag(pic_output_flag);
1365
1366
15920
        if (sps->separate_colour_plane_flag)
1367
            u(2, colour_plane_id, 0, 2);
1368
1369
15920
        if (current->nal_unit_header.nal_unit_type != HEVC_NAL_IDR_W_RADL &&
1370
15708
            current->nal_unit_header.nal_unit_type != HEVC_NAL_IDR_N_LP) {
1371
            const H265RawSTRefPicSet *rps;
1372
1373
15704
            ub(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, slice_pic_order_cnt_lsb);
1374
1375
15704
            flag(short_term_ref_pic_set_sps_flag);
1376
15704
            if (!current->short_term_ref_pic_set_sps_flag) {
1377
7460
                CHECK(FUNC(st_ref_pic_set)(ctx, rw, &current->short_term_ref_pic_set,
1378
                                           sps->num_short_term_ref_pic_sets, sps));
1379
7460
                rps = &current->short_term_ref_pic_set;
1380
8244
            } else if (sps->num_short_term_ref_pic_sets > 1) {
1381
8244
                unsigned int idx_size = av_log2(sps->num_short_term_ref_pic_sets - 1) + 1;
1382
8244
                u(idx_size, short_term_ref_pic_set_idx,
1383
                  0, sps->num_short_term_ref_pic_sets - 1);
1384
8244
                rps = &sps->st_ref_pic_set[current->short_term_ref_pic_set_idx];
1385
            } else {
1386
                infer(short_term_ref_pic_set_idx, 0);
1387
                rps = &sps->st_ref_pic_set[0];
1388
            }
1389
1390
15704
            num_pic_total_curr = 0;
1391
58848
            for (i = 0; i < rps->num_negative_pics; i++)
1392
43144
                if (rps->used_by_curr_pic_s0_flag[i])
1393
41048
                    ++num_pic_total_curr;
1394
26068
            for (i = 0; i < rps->num_positive_pics; i++)
1395
10364
                if (rps->used_by_curr_pic_s1_flag[i])
1396
9784
                    ++num_pic_total_curr;
1397
1398
15704
            if (sps->long_term_ref_pics_present_flag) {
1399
                unsigned int idx_size;
1400
1401
3328
                if (sps->num_long_term_ref_pics_sps > 0) {
1402
1996
                    ue(num_long_term_sps, 0, sps->num_long_term_ref_pics_sps);
1403
1996
                    idx_size = av_log2(sps->num_long_term_ref_pics_sps - 1) + 1;
1404
                } else {
1405
1332
                    infer(num_long_term_sps, 0);
1406
1332
                    idx_size = 0;
1407
                }
1408
3328
                ue(num_long_term_pics, 0, HEVC_MAX_REFS - current->num_long_term_sps);
1409
1410
3328
                for (i = 0; i < current->num_long_term_sps +
1411
8968
                                current->num_long_term_pics; i++) {
1412
5640
                    if (i < current->num_long_term_sps) {
1413
736
                        if (sps->num_long_term_ref_pics_sps > 1)
1414
736
                            us(idx_size, lt_idx_sps[i],
1415
                               0, sps->num_long_term_ref_pics_sps - 1, 1, i);
1416
736
                        if (sps->used_by_curr_pic_lt_sps_flag[current->lt_idx_sps[i]])
1417
352
                            ++num_pic_total_curr;
1418
                    } else {
1419
4904
                        ubs(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, poc_lsb_lt[i], 1, i);
1420
4904
                        flags(used_by_curr_pic_lt_flag[i], 1, i);
1421
4904
                        if (current->used_by_curr_pic_lt_flag[i])
1422
1512
                            ++num_pic_total_curr;
1423
                    }
1424
5640
                    flags(delta_poc_msb_present_flag[i], 1, i);
1425
5640
                    if (current->delta_poc_msb_present_flag[i])
1426
                        ues(delta_poc_msb_cycle_lt[i], 0, UINT32_MAX - 1, 1, i);
1427
                    else
1428
5640
                        infer(delta_poc_msb_cycle_lt[i], 0);
1429
                }
1430
            }
1431
1432
15704
            if (sps->sps_temporal_mvp_enabled_flag)
1433
10412
                flag(slice_temporal_mvp_enabled_flag);
1434
            else
1435
5292
                infer(slice_temporal_mvp_enabled_flag, 0);
1436
1437
15704
            if (pps->pps_curr_pic_ref_enabled_flag)
1438
                ++num_pic_total_curr;
1439
        }
1440
1441
15920
        if (sps->sample_adaptive_offset_enabled_flag) {
1442
9384
            flag(slice_sao_luma_flag);
1443

9384
            if (!sps->separate_colour_plane_flag && sps->chroma_format_idc != 0)
1444
9384
                flag(slice_sao_chroma_flag);
1445
            else
1446
                infer(slice_sao_chroma_flag, 0);
1447
        } else {
1448
6536
            infer(slice_sao_luma_flag,   0);
1449
6536
            infer(slice_sao_chroma_flag, 0);
1450
        }
1451
1452
15920
        if (current->slice_type == HEVC_SLICE_P ||
1453
9184
            current->slice_type == HEVC_SLICE_B) {
1454
15508
            flag(num_ref_idx_active_override_flag);
1455
15508
            if (current->num_ref_idx_active_override_flag) {
1456
7304
                ue(num_ref_idx_l0_active_minus1, 0, 14);
1457
7304
                if (current->slice_type == HEVC_SLICE_B)
1458
1824
                    ue(num_ref_idx_l1_active_minus1, 0, 14);
1459
                else
1460
5480
                    infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
1461
            } else {
1462
8204
                infer(num_ref_idx_l0_active_minus1, pps->num_ref_idx_l0_default_active_minus1);
1463
8204
                infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
1464
            }
1465
1466

15508
            if (pps->lists_modification_present_flag && num_pic_total_curr > 1)
1467
2992
                CHECK(FUNC(ref_pic_lists_modification)(ctx, rw, current,
1468
                                                       num_pic_total_curr));
1469
1470
15508
            if (current->slice_type == HEVC_SLICE_B)
1471
8772
                flag(mvd_l1_zero_flag);
1472
15508
            if (pps->cabac_init_present_flag)
1473
9876
                flag(cabac_init_flag);
1474
            else
1475
5632
                infer(cabac_init_flag, 0);
1476
15508
            if (current->slice_temporal_mvp_enabled_flag) {
1477
10236
                if (current->slice_type == HEVC_SLICE_B)
1478
8772
                    flag(collocated_from_l0_flag);
1479
                else
1480
1464
                    infer(collocated_from_l0_flag, 1);
1481
10236
                if (current->collocated_from_l0_flag) {
1482
4724
                    if (current->num_ref_idx_l0_active_minus1 > 0)
1483
4252
                        ue(collocated_ref_idx, 0, current->num_ref_idx_l0_active_minus1);
1484
                    else
1485
472
                        infer(collocated_ref_idx, 0);
1486
                } else {
1487
5512
                    if (current->num_ref_idx_l1_active_minus1 > 0)
1488
5148
                        ue(collocated_ref_idx, 0, current->num_ref_idx_l1_active_minus1);
1489
                    else
1490
364
                        infer(collocated_ref_idx, 0);
1491
                }
1492
            }
1493
1494

15508
            if ((pps->weighted_pred_flag   && current->slice_type == HEVC_SLICE_P) ||
1495

14488
                (pps->weighted_bipred_flag && current->slice_type == HEVC_SLICE_B))
1496
1020
                CHECK(FUNC(pred_weight_table)(ctx, rw, current));
1497
1498
15508
            ue(five_minus_max_num_merge_cand, 0, 4);
1499
15508
            if (sps->motion_vector_resolution_control_idc == 2)
1500
                flag(use_integer_mv_flag);
1501
            else
1502
15508
                infer(use_integer_mv_flag, sps->motion_vector_resolution_control_idc);
1503
        }
1504
1505
15920
        se(slice_qp_delta,
1506
           - 6 * sps->bit_depth_luma_minus8 - (pps->init_qp_minus26 + 26),
1507
           + 51 - (pps->init_qp_minus26 + 26));
1508
15920
        if (pps->pps_slice_chroma_qp_offsets_present_flag) {
1509
            se(slice_cb_qp_offset, -12, +12);
1510
            se(slice_cr_qp_offset, -12, +12);
1511
        } else {
1512
15920
            infer(slice_cb_qp_offset, 0);
1513
15920
            infer(slice_cr_qp_offset, 0);
1514
        }
1515
15920
        if (pps->pps_slice_act_qp_offsets_present_flag) {
1516
            se(slice_act_y_qp_offset,
1517
               -12 - (pps->pps_act_y_qp_offset_plus5 - 5),
1518
               +12 - (pps->pps_act_y_qp_offset_plus5 - 5));
1519
            se(slice_act_cb_qp_offset,
1520
               -12 - (pps->pps_act_cb_qp_offset_plus5 - 5),
1521
               +12 - (pps->pps_act_cb_qp_offset_plus5 - 5));
1522
            se(slice_act_cr_qp_offset,
1523
               -12 - (pps->pps_act_cr_qp_offset_plus3 - 3),
1524
               +12 - (pps->pps_act_cr_qp_offset_plus3 - 3));
1525
        } else {
1526
15920
            infer(slice_act_y_qp_offset,  0);
1527
15920
            infer(slice_act_cb_qp_offset, 0);
1528
15920
            infer(slice_act_cr_qp_offset, 0);
1529
        }
1530
15920
        if (pps->chroma_qp_offset_list_enabled_flag)
1531
            flag(cu_chroma_qp_offset_enabled_flag);
1532
        else
1533
15920
            infer(cu_chroma_qp_offset_enabled_flag, 0);
1534
1535
15920
        if (pps->deblocking_filter_override_enabled_flag)
1536
148
            flag(deblocking_filter_override_flag);
1537
        else
1538
15772
            infer(deblocking_filter_override_flag, 0);
1539
15920
        if (current->deblocking_filter_override_flag) {
1540
4
            flag(slice_deblocking_filter_disabled_flag);
1541
4
            if (!current->slice_deblocking_filter_disabled_flag) {
1542
                se(slice_beta_offset_div2, -6, +6);
1543
                se(slice_tc_offset_div2,   -6, +6);
1544
            } else {
1545
4
                infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
1546
4
                infer(slice_tc_offset_div2,   pps->pps_tc_offset_div2);
1547
            }
1548
        } else {
1549
15916
            infer(slice_deblocking_filter_disabled_flag,
1550
                  pps->pps_deblocking_filter_disabled_flag);
1551
15916
            infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
1552
15916
            infer(slice_tc_offset_div2,   pps->pps_tc_offset_div2);
1553
        }
1554
15920
        if (pps->pps_loop_filter_across_slices_enabled_flag &&
1555

12940
            (current->slice_sao_luma_flag || current->slice_sao_chroma_flag ||
1556
9020
             !current->slice_deblocking_filter_disabled_flag))
1557
12936
            flag(slice_loop_filter_across_slices_enabled_flag);
1558
        else
1559
2984
            infer(slice_loop_filter_across_slices_enabled_flag,
1560
                  pps->pps_loop_filter_across_slices_enabled_flag);
1561
    }
1562
1563

23456
    if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
1564
        unsigned int num_entry_point_offsets_limit;
1565

7220
        if (!pps->tiles_enabled_flag && pps->entropy_coding_sync_enabled_flag)
1566
1748
            num_entry_point_offsets_limit = pic_height_in_ctbs_y - 1;
1567

5472
        else if (pps->tiles_enabled_flag && !pps->entropy_coding_sync_enabled_flag)
1568
5472
            num_entry_point_offsets_limit =
1569
5472
                (pps->num_tile_columns_minus1 + 1) * (pps->num_tile_rows_minus1 + 1);
1570
        else
1571
            num_entry_point_offsets_limit =
1572
                (pps->num_tile_columns_minus1 + 1) * pic_height_in_ctbs_y - 1;
1573
7220
        ue(num_entry_point_offsets, 0, num_entry_point_offsets_limit);
1574
1575
7220
        if (current->num_entry_point_offsets > HEVC_MAX_ENTRY_POINT_OFFSETS) {
1576
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many entry points: "
1577
                   "%"PRIu16".\n", current->num_entry_point_offsets);
1578
            return AVERROR_PATCHWELCOME;
1579
        }
1580
1581
7220
        if (current->num_entry_point_offsets > 0) {
1582
3076
            ue(offset_len_minus1, 0, 31);
1583
9300
            for (i = 0; i < current->num_entry_point_offsets; i++)
1584
6224
                ubs(current->offset_len_minus1 + 1, entry_point_offset_minus1[i], 1, i);
1585
        }
1586
    }
1587
1588
23456
    if (pps->slice_segment_header_extension_present_flag) {
1589
1748
        ue(slice_segment_header_extension_length, 0, 256);
1590
14156
        for (i = 0; i < current->slice_segment_header_extension_length; i++)
1591
12408
            us(8, slice_segment_header_extension_data_byte[i], 0x00, 0xff, 1, i);
1592
    }
1593
1594
23456
    CHECK(FUNC(byte_alignment)(ctx, rw));
1595
1596
23456
    return 0;
1597
}
1598
1599
20
static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
1600
                                      H265RawSEIBufferingPeriod *current,
1601
                                      uint32_t *payload_size,
1602
                                      int *more_data)
1603
{
1604
20
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1605
    const H265RawSPS *sps;
1606
    const H265RawHRDParameters *hrd;
1607
    int err, i, length;
1608
1609
#ifdef READ
1610
    int start_pos, end_pos;
1611
10
    start_pos = get_bits_count(rw);
1612
#endif
1613
1614
20
    HEADER("Buffering Period");
1615
1616
20
    ue(bp_seq_parameter_set_id, 0, HEVC_MAX_SPS_COUNT - 1);
1617
1618
20
    sps = h265->sps[current->bp_seq_parameter_set_id];
1619
20
    if (!sps) {
1620
        av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
1621
               current->bp_seq_parameter_set_id);
1622
        return AVERROR_INVALIDDATA;
1623
    }
1624
20
    h265->active_sps = sps;
1625
1626
20
    if (!sps->vui_parameters_present_flag ||
1627
20
        !sps->vui.vui_hrd_parameters_present_flag) {
1628
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Buffering period SEI requires "
1629
               "HRD parameters to be present in SPS.\n");
1630
        return AVERROR_INVALIDDATA;
1631
    }
1632
20
    hrd = &sps->vui.hrd_parameters;
1633
20
    if (!hrd->nal_hrd_parameters_present_flag &&
1634
        !hrd->vcl_hrd_parameters_present_flag) {
1635
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Buffering period SEI requires "
1636
               "NAL or VCL HRD parameters to be present.\n");
1637
        return AVERROR_INVALIDDATA;
1638
    }
1639
1640
20
    if (!hrd->sub_pic_hrd_params_present_flag)
1641
8
        flag(irap_cpb_params_present_flag);
1642
    else
1643
12
        infer(irap_cpb_params_present_flag, 0);
1644
20
    if (current->irap_cpb_params_present_flag) {
1645
        length = hrd->au_cpb_removal_delay_length_minus1 + 1;
1646
        ub(length, cpb_delay_offset);
1647
        length = hrd->dpb_output_delay_length_minus1 + 1;
1648
        ub(length, dpb_delay_offset);
1649
    } else {
1650
20
        infer(cpb_delay_offset, 0);
1651
20
        infer(dpb_delay_offset, 0);
1652
    }
1653
1654
20
    flag(concatenation_flag);
1655
1656
20
    length = hrd->au_cpb_removal_delay_length_minus1 + 1;
1657
20
    ub(length, au_cpb_removal_delay_delta_minus1);
1658
1659
20
    if (hrd->nal_hrd_parameters_present_flag) {
1660
40
        for (i = 0; i <= hrd->cpb_cnt_minus1[0]; i++) {
1661
20
            length = hrd->initial_cpb_removal_delay_length_minus1 + 1;
1662
1663
20
            ubs(length, nal_initial_cpb_removal_delay[i], 1, i);
1664
20
            ubs(length, nal_initial_cpb_removal_offset[i], 1, i);
1665
1666
20
            if (hrd->sub_pic_hrd_params_present_flag ||
1667
8
                current->irap_cpb_params_present_flag) {
1668
12
                ubs(length, nal_initial_alt_cpb_removal_delay[i], 1, i);
1669
12
                ubs(length, nal_initial_alt_cpb_removal_offset[i], 1, i);
1670
            }
1671
        }
1672
    }
1673
20
    if (hrd->vcl_hrd_parameters_present_flag) {
1674
40
        for (i = 0; i <= hrd->cpb_cnt_minus1[0]; i++) {
1675
20
            length = hrd->initial_cpb_removal_delay_length_minus1 + 1;
1676
1677
20
            ubs(length, vcl_initial_cpb_removal_delay[i], 1, i);
1678
20
            ubs(length, vcl_initial_cpb_removal_offset[i], 1, i);
1679
1680
20
            if (hrd->sub_pic_hrd_params_present_flag ||
1681
8
                current->irap_cpb_params_present_flag) {
1682
12
                ubs(length, vcl_initial_alt_cpb_removal_delay[i], 1, i);
1683
12
                ubs(length, vcl_initial_alt_cpb_removal_offset[i], 1, i);
1684
            }
1685
        }
1686
    }
1687
1688
#ifdef READ
1689
10
    end_pos = get_bits_count(rw);
1690
10
    if (cbs_h265_payload_extension_present(rw, *payload_size,
1691
                                           end_pos - start_pos))
1692
        flag(use_alt_cpb_params_flag);
1693
    else
1694
10
        infer(use_alt_cpb_params_flag, 0);
1695
#else
1696
    // If unknown extension data exists, then use_alt_cpb_params_flag is
1697
    // coded in the bitstream and must be written even if it's 0.
1698

10
    if (current->use_alt_cpb_params_flag || *more_data) {
1699
        flag(use_alt_cpb_params_flag);
1700
        // Ensure this bit is not the last in the payload by making the
1701
        // more_data_in_payload() check evaluate to true, so it may not
1702
        // be mistaken as something else by decoders.
1703
        *more_data = 1;
1704
    }
1705
#endif
1706
1707
20
    return 0;
1708
}
1709
1710
624
static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
1711
                                H265RawSEIPicTiming *current)
1712
{
1713
624
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1714
    const H265RawSPS *sps;
1715
    const H265RawHRDParameters *hrd;
1716
    int err, expected_source_scan_type, i, length;
1717
1718
624
    HEADER("Picture Timing");
1719
1720
624
    sps = h265->active_sps;
1721
624
    if (!sps) {
1722
        av_log(ctx->log_ctx, AV_LOG_ERROR,
1723
               "No active SPS for pic_timing.\n");
1724
        return AVERROR_INVALIDDATA;
1725
    }
1726
1727
624
    expected_source_scan_type = 2 -
1728
624
        2 * sps->profile_tier_level.general_interlaced_source_flag -
1729
624
        sps->profile_tier_level.general_progressive_source_flag;
1730
1731
624
    if (sps->vui.frame_field_info_present_flag) {
1732
240
        u(4, pic_struct, 0, 12);
1733

240
        u(2, source_scan_type,
1734
          expected_source_scan_type >= 0 ? expected_source_scan_type : 0,
1735
          expected_source_scan_type >= 0 ? expected_source_scan_type : 2);
1736
240
        flag(duplicate_flag);
1737
    } else {
1738
384
        infer(pic_struct, 0);
1739

384
        infer(source_scan_type,
1740
              expected_source_scan_type >= 0 ? expected_source_scan_type : 2);
1741
384
        infer(duplicate_flag, 0);
1742
    }
1743
1744
624
    if (sps->vui_parameters_present_flag &&
1745
624
        sps->vui.vui_hrd_parameters_present_flag)
1746
624
        hrd = &sps->vui.hrd_parameters;
1747
    else
1748
        hrd = NULL;
1749

624
    if (hrd && (hrd->nal_hrd_parameters_present_flag ||
1750
                hrd->vcl_hrd_parameters_present_flag)) {
1751
624
        length = hrd->au_cpb_removal_delay_length_minus1 + 1;
1752
624
        ub(length, au_cpb_removal_delay_minus1);
1753
1754
624
        length = hrd->dpb_output_delay_length_minus1 + 1;
1755
624
        ub(length, pic_dpb_output_delay);
1756
1757
624
        if (hrd->sub_pic_hrd_params_present_flag) {
1758
384
            length = hrd->dpb_output_delay_du_length_minus1 + 1;
1759
384
            ub(length, pic_dpb_output_du_delay);
1760
        }
1761
1762
624
        if (hrd->sub_pic_hrd_params_present_flag &&
1763
384
            hrd->sub_pic_cpb_params_in_pic_timing_sei_flag) {
1764
            // Each decoding unit must contain at least one slice segment.
1765
384
            ue(num_decoding_units_minus1, 0, HEVC_MAX_SLICE_SEGMENTS);
1766
384
            flag(du_common_cpb_removal_delay_flag);
1767
1768
384
            length = hrd->du_cpb_removal_delay_increment_length_minus1 + 1;
1769
384
            if (current->du_common_cpb_removal_delay_flag)
1770
                ub(length, du_common_cpb_removal_delay_increment_minus1);
1771
1772
1920
            for (i = 0; i <= current->num_decoding_units_minus1; i++) {
1773
1536
                ues(num_nalus_in_du_minus1[i],
1774
                    0, HEVC_MAX_SLICE_SEGMENTS, 1, i);
1775
1536
                if (!current->du_common_cpb_removal_delay_flag &&
1776
1536
                    i < current->num_decoding_units_minus1)
1777
1152
                    ubs(length, du_cpb_removal_delay_increment_minus1[i], 1, i);
1778
            }
1779
        }
1780
    }
1781
1782
624
    return 0;
1783
}
1784
1785
static int FUNC(sei_pan_scan_rect)(CodedBitstreamContext *ctx, RWContext *rw,
1786
                                   H265RawSEIPanScanRect *current)
1787
{
1788
    int err, i;
1789
1790
    HEADER("Pan-Scan Rectangle");
1791
1792
    ue(pan_scan_rect_id, 0, UINT32_MAX - 1);
1793
    flag(pan_scan_rect_cancel_flag);
1794
1795
    if (!current->pan_scan_rect_cancel_flag) {
1796
        ue(pan_scan_cnt_minus1, 0, 2);
1797
1798
        for (i = 0; i <= current->pan_scan_cnt_minus1; i++) {
1799
            ses(pan_scan_rect_left_offset[i],   INT32_MIN + 1, INT32_MAX, 1, i);
1800
            ses(pan_scan_rect_right_offset[i],  INT32_MIN + 1, INT32_MAX, 1, i);
1801
            ses(pan_scan_rect_top_offset[i],    INT32_MIN + 1, INT32_MAX, 1, i);
1802
            ses(pan_scan_rect_bottom_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
1803
        }
1804
1805
        flag(pan_scan_rect_persistence_flag);
1806
    }
1807
1808
    return 0;
1809
}
1810
1811
static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext *rw,
1812
                                          H265RawSEIUserDataRegistered *current,
1813
                                          uint32_t *payload_size)
1814
{
1815
    int err, i, j;
1816
1817
    HEADER("User Data Registered ITU-T T.35");
1818
1819
    u(8, itu_t_t35_country_code, 0x00, 0xff);
1820
    if (current->itu_t_t35_country_code != 0xff)
1821
        i = 1;
1822
    else {
1823
        u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
1824
        i = 2;
1825
    }
1826
1827
#ifdef READ
1828
    if (*payload_size < i) {
1829
        av_log(ctx->log_ctx, AV_LOG_ERROR,
1830
               "Invalid SEI user data registered payload.\n");
1831
        return AVERROR_INVALIDDATA;
1832
    }
1833
    current->data_length = *payload_size - i;
1834
#else
1835
    *payload_size = i + current->data_length;
1836
#endif
1837
1838
    allocate(current->data, current->data_length);
1839
    for (j = 0; j < current->data_length; j++)
1840
        xu(8, itu_t_t35_payload_byte[i], current->data[j], 0x00, 0xff, 1, i + j);
1841
1842
    return 0;
1843
}
1844
1845
static int FUNC(sei_user_data_unregistered)(CodedBitstreamContext *ctx, RWContext *rw,
1846
                                            H265RawSEIUserDataUnregistered *current,
1847
                                            uint32_t *payload_size)
1848
{
1849
    int err, i;
1850
1851
    HEADER("User Data Unregistered");
1852
1853
#ifdef READ
1854
    if (*payload_size < 16) {
1855
        av_log(ctx->log_ctx, AV_LOG_ERROR,
1856
               "Invalid SEI user data unregistered payload.\n");
1857
        return AVERROR_INVALIDDATA;
1858
    }
1859
    current->data_length = *payload_size - 16;
1860
#else
1861
    *payload_size = 16 + current->data_length;
1862
#endif
1863
1864
    for (i = 0; i < 16; i++)
1865
        us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
1866
1867
    allocate(current->data, current->data_length);
1868
1869
    for (i = 0; i < current->data_length; i++)
1870
        xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
1871
1872
    return 0;
1873
}
1874
1875
20
static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
1876
                                    H265RawSEIRecoveryPoint *current)
1877
{
1878
    int err;
1879
1880
20
    HEADER("Recovery Point");
1881
1882
20
    se(recovery_poc_cnt, -32768, 32767);
1883
1884
20
    flag(exact_match_flag);
1885
20
    flag(broken_link_flag);
1886
1887
20
    return 0;
1888
}
1889
1890
static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
1891
                                         H265RawSEIDisplayOrientation *current)
1892
{
1893
    int err;
1894
1895
    HEADER("Display Orientation");
1896
1897
    flag(display_orientation_cancel_flag);
1898
    if (!current->display_orientation_cancel_flag) {
1899
        flag(hor_flip);
1900
        flag(ver_flip);
1901
        ub(16, anticlockwise_rotation);
1902
        flag(display_orientation_persistence_flag);
1903
    }
1904
1905
    return 0;
1906
}
1907
1908
12
static int FUNC(sei_active_parameter_sets)(CodedBitstreamContext *ctx, RWContext *rw,
1909
                                           H265RawSEIActiveParameterSets *current)
1910
{
1911
12
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1912
    const H265RawVPS *vps;
1913
    int err, i;
1914
1915
12
    HEADER("Active Parameter Sets");
1916
1917
12
    u(4, active_video_parameter_set_id, 0, HEVC_MAX_VPS_COUNT);
1918
12
    vps = h265->vps[current->active_video_parameter_set_id];
1919
12
    if (!vps) {
1920
        av_log(ctx->log_ctx, AV_LOG_ERROR, "VPS id %d not available for active "
1921
               "parameter sets.\n", current->active_video_parameter_set_id);
1922
        return AVERROR_INVALIDDATA;
1923
    }
1924
12
    h265->active_vps = vps;
1925
1926
12
    flag(self_contained_cvs_flag);
1927
12
    flag(no_parameter_set_update_flag);
1928
1929
12
    ue(num_sps_ids_minus1, 0, HEVC_MAX_SPS_COUNT - 1);
1930
24
    for (i = 0; i <= current->num_sps_ids_minus1; i++)
1931
12
        ues(active_seq_parameter_set_id[i], 0, HEVC_MAX_SPS_COUNT - 1, 1, i);
1932
1933
12
    for (i = vps->vps_base_layer_internal_flag;
1934
12
         i <= FFMIN(62, vps->vps_max_layers_minus1); i++) {
1935
        ues(layer_sps_idx[i], 0, current->num_sps_ids_minus1, 1, i);
1936
1937
        if (i == 0)
1938
            h265->active_sps = h265->sps[current->active_seq_parameter_set_id[current->layer_sps_idx[0]]];
1939
    }
1940
1941
12
    return 0;
1942
}
1943
1944
5464
static int FUNC(sei_decoded_picture_hash)(CodedBitstreamContext *ctx, RWContext *rw,
1945
                                          H265RawSEIDecodedPictureHash *current)
1946
{
1947
5464
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1948
5464
    const H265RawSPS *sps = h265->active_sps;
1949
    int err, c, i;
1950
1951
5464
    HEADER("Decoded Picture Hash");
1952
1953
5464
    if (!sps) {
1954
        av_log(ctx->log_ctx, AV_LOG_ERROR,
1955
               "No active SPS for decoded picture hash.\n");
1956
        return AVERROR_INVALIDDATA;
1957
    }
1958
1959
5464
    u(8, hash_type, 0, 2);
1960
1961

21856
    for (c = 0; c < (sps->chroma_format_idc == 0 ? 1 : 3); c++) {
1962
16392
        if (current->hash_type == 0) {
1963
255000
            for (i = 0; i < 16; i++)
1964
240000
                us(8, picture_md5[c][i], 0x00, 0xff, 2, c, i);
1965
1392
        } else if (current->hash_type == 1) {
1966
            us(16, picture_crc[c], 0x0000, 0xffff, 1, c);
1967
1392
        } else if (current->hash_type == 2) {
1968
1392
            us(32, picture_checksum[c], 0x00000000, 0xffffffff, 1, c);
1969
        }
1970
    }
1971
1972
5464
    return 0;
1973
}
1974
1975
static int FUNC(sei_time_code)(CodedBitstreamContext *ctx, RWContext *rw,
1976
                               H265RawSEITimeCode *current)
1977
{
1978
    int err, i;
1979
1980
    HEADER("Time Code");
1981
1982
    u(2, num_clock_ts, 1, 3);
1983
1984
    for (i = 0; i < current->num_clock_ts; i++) {
1985
        flags(clock_timestamp_flag[i],   1, i);
1986
1987
        if (current->clock_timestamp_flag[i]) {
1988
            flags(units_field_based_flag[i], 1, i);
1989
            us(5, counting_type[i], 0, 6,    1, i);
1990
            flags(full_timestamp_flag[i],    1, i);
1991
            flags(discontinuity_flag[i],     1, i);
1992
            flags(cnt_dropped_flag[i],       1, i);
1993
1994
            ubs(9, n_frames[i], 1, i);
1995
1996
            if (current->full_timestamp_flag[i]) {
1997
                us(6, seconds_value[i], 0, 59, 1, i);
1998
                us(6, minutes_value[i], 0, 59, 1, i);
1999
                us(5, hours_value[i],   0, 23, 1, i);
2000
            } else {
2001
                flags(seconds_flag[i], 1, i);
2002
                if (current->seconds_flag[i]) {
2003
                    us(6, seconds_value[i], 0, 59, 1, i);
2004
                    flags(minutes_flag[i], 1, i);
2005
                    if (current->minutes_flag[i]) {
2006
                        us(6, minutes_value[i], 0, 59, 1, i);
2007
                        flags(hours_flag[i], 1, i);
2008
                        if (current->hours_flag[i])
2009
                            us(5, hours_value[i], 0, 23, 1, i);
2010
                    }
2011
                }
2012
            }
2013
2014
            ubs(5, time_offset_length[i], 1, i);
2015
            if (current->time_offset_length[i] > 0)
2016
                ibs(current->time_offset_length[i], time_offset_value[i], 1, i);
2017
            else
2018
                infer(time_offset_value[i], 0);
2019
        }
2020
    }
2021
2022
    return 0;
2023
}
2024
2025
static int FUNC(sei_mastering_display)(CodedBitstreamContext *ctx, RWContext *rw,
2026
                                       H265RawSEIMasteringDisplayColourVolume *current)
2027
{
2028
    int err, c;
2029
2030
    HEADER("Mastering Display Colour Volume");
2031
2032
    for (c = 0; c < 3; c++) {
2033
        us(16, display_primaries_x[c], 0, 50000, 1, c);
2034
        us(16, display_primaries_y[c], 0, 50000, 1, c);
2035
    }
2036
2037
    u(16, white_point_x, 0, 50000);
2038
    u(16, white_point_y, 0, 50000);
2039
2040
    u(32, max_display_mastering_luminance,
2041
      1, MAX_UINT_BITS(32));
2042
    u(32, min_display_mastering_luminance,
2043
      0, current->max_display_mastering_luminance - 1);
2044
2045
    return 0;
2046
}
2047
2048
static int FUNC(sei_content_light_level)(CodedBitstreamContext *ctx, RWContext *rw,
2049
                                         H265RawSEIContentLightLevelInfo *current)
2050
{
2051
    int err;
2052
2053
    HEADER("Content Light Level");
2054
2055
    ub(16, max_content_light_level);
2056
    ub(16, max_pic_average_light_level);
2057
2058
    return 0;
2059
}
2060
2061
static int FUNC(sei_alternative_transfer_characteristics)(CodedBitstreamContext *ctx,
2062
                                                          RWContext *rw,
2063
                                                          H265RawSEIAlternativeTransferCharacteristics *current)
2064
{
2065
    int err;
2066
2067
    HEADER("Alternative Transfer Characteristics");
2068
2069
    ub(8, preferred_transfer_characteristics);
2070
2071
    return 0;
2072
}
2073
2074
static int FUNC(sei_alpha_channel_info)(CodedBitstreamContext *ctx,
2075
                                        RWContext *rw,
2076
                                        H265RawSEIAlphaChannelInfo *current)
2077
{
2078
    int err, length;
2079
2080
    HEADER("Alpha Channel Information");
2081
2082
    flag(alpha_channel_cancel_flag);
2083
    if (!current->alpha_channel_cancel_flag) {
2084
        ub(3, alpha_channel_use_idc);
2085
        ub(3, alpha_channel_bit_depth_minus8);
2086
        length = current->alpha_channel_bit_depth_minus8 + 9;
2087
        ub(length, alpha_transparent_value);
2088
        ub(length, alpha_opaque_value);
2089
        flag(alpha_channel_incr_flag);
2090
        flag(alpha_channel_clip_flag);
2091
        if (current->alpha_channel_clip_flag)
2092
            flag(alpha_channel_clip_type_flag);
2093
    } else {
2094
       infer(alpha_channel_use_idc,   2);
2095
       infer(alpha_channel_incr_flag, 0);
2096
       infer(alpha_channel_clip_flag, 0);
2097
    }
2098
2099
    return 0;
2100
}
2101
2102
644
static int FUNC(payload_extension)(CodedBitstreamContext *ctx, RWContext *rw,
2103
                                   H265RawExtensionData *current, uint32_t payload_size,
2104
                                   int cur_pos)
2105
{
2106
    int err;
2107
    size_t byte_length, k;
2108
2109
#ifdef READ
2110
    GetBitContext tmp;
2111
    int bits_left, payload_zero_bits;
2112
2113
322
    if (!cbs_h265_payload_extension_present(rw, payload_size, cur_pos))
2114
322
        return 0;
2115
2116
    bits_left = 8 * payload_size - cur_pos;
2117
    tmp = *rw;
2118
    if (bits_left > 8)
2119
        skip_bits_long(&tmp, bits_left - 8);
2120
    payload_zero_bits = get_bits(&tmp, FFMIN(bits_left, 8));
2121
    if (!payload_zero_bits)
2122
        return AVERROR_INVALIDDATA;
2123
    payload_zero_bits = ff_ctz(payload_zero_bits);
2124
    current->bit_length = bits_left - payload_zero_bits - 1;
2125
    allocate(current->data, (current->bit_length + 7) / 8);
2126
#endif
2127
2128
322
    byte_length = (current->bit_length + 7) / 8;
2129
322
    for (k = 0; k < byte_length; k++) {
2130
        int length = FFMIN(current->bit_length - k * 8, 8);
2131
        xu(length, reserved_payload_extension_data, current->data[k],
2132
           0, MAX_UINT_BITS(length), 0);
2133
    }
2134
2135
322
    return 0;
2136
}
2137
2138
6140
static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
2139
                             H265RawSEIPayload *current, int prefix)
2140
{
2141
    int err, i;
2142
    int start_position, current_position;
2143
6140
    int more_data = !!current->extension_data.bit_length;
2144
2145
#ifdef READ
2146
3070
    start_position = get_bits_count(rw);
2147
#else
2148
3070
    start_position = put_bits_count(rw);
2149
#endif
2150
2151



6140
    switch (current->payload_type) {
2152
#define SEI_TYPE_CHECK_VALID(name, prefix_valid, suffix_valid) do { \
2153
            if (prefix && !prefix_valid) { \
2154
                av_log(ctx->log_ctx, AV_LOG_ERROR, "SEI type %s invalid " \
2155
                       "as prefix SEI!\n", #name); \
2156
                return AVERROR_INVALIDDATA; \
2157
            } \
2158
            if (!prefix && !suffix_valid) { \
2159
                av_log(ctx->log_ctx, AV_LOG_ERROR, "SEI type %s invalid " \
2160
                       "as suffix SEI!\n", #name); \
2161
                return AVERROR_INVALIDDATA; \
2162
            } \
2163
        } while (0)
2164
#define SEI_TYPE_N(type, prefix_valid, suffix_valid, name) \
2165
    case HEVC_SEI_TYPE_ ## type: \
2166
        SEI_TYPE_CHECK_VALID(name, prefix_valid, suffix_valid); \
2167
        CHECK(FUNC(sei_ ## name)(ctx, rw, &current->payload.name)); \
2168
        break
2169
#define SEI_TYPE_S(type, prefix_valid, suffix_valid, name) \
2170
    case HEVC_SEI_TYPE_ ## type: \
2171
        SEI_TYPE_CHECK_VALID(name, prefix_valid, suffix_valid); \
2172
        CHECK(FUNC(sei_ ## name)(ctx, rw, &current->payload.name, \
2173
                                 &current->payload_size)); \
2174
        break
2175
#define SEI_TYPE_E(type, prefix_valid, suffix_valid, name) \
2176
    case HEVC_SEI_TYPE_ ## type: \
2177
        SEI_TYPE_CHECK_VALID(name, prefix_valid, suffix_valid); \
2178
        CHECK(FUNC(sei_ ## name)(ctx, rw, &current->payload.name, \
2179
                                 &current->payload_size, \
2180
                                 &more_data)); \
2181
        break
2182
2183

20
        SEI_TYPE_E(BUFFERING_PERIOD,         1, 0, buffering_period);
2184

624
        SEI_TYPE_N(PICTURE_TIMING,           1, 0, pic_timing);
2185
        SEI_TYPE_N(PAN_SCAN_RECT,            1, 0, pan_scan_rect);
2186
        SEI_TYPE_S(USER_DATA_REGISTERED_ITU_T_T35,
2187
                                             1, 1, user_data_registered);
2188
        SEI_TYPE_S(USER_DATA_UNREGISTERED,   1, 1, user_data_unregistered);
2189

20
        SEI_TYPE_N(RECOVERY_POINT,           1, 0, recovery_point);
2190
        SEI_TYPE_N(DISPLAY_ORIENTATION,      1, 0, display_orientation);
2191

12
        SEI_TYPE_N(ACTIVE_PARAMETER_SETS,    1, 0, active_parameter_sets);
2192

5464
        SEI_TYPE_N(DECODED_PICTURE_HASH,     0, 1, decoded_picture_hash);
2193
        SEI_TYPE_N(TIME_CODE,                1, 0, time_code);
2194
        SEI_TYPE_N(MASTERING_DISPLAY_INFO,   1, 0, mastering_display);
2195
        SEI_TYPE_N(CONTENT_LIGHT_LEVEL_INFO, 1, 0, content_light_level);
2196
        SEI_TYPE_N(ALTERNATIVE_TRANSFER_CHARACTERISTICS,
2197
                                             1, 0, alternative_transfer_characteristics);
2198
        SEI_TYPE_N(ALPHA_CHANNEL_INFO,       1, 0, alpha_channel_info);
2199
2200
#undef SEI_TYPE
2201
    default:
2202
        {
2203
#ifdef READ
2204
            current->payload.other.data_length = current->payload_size;
2205
#endif
2206
            allocate(current->payload.other.data, current->payload.other.data_length);
2207
2208
            for (i = 0; i < current->payload_size; i++)
2209
                xu(8, payload_byte[i], current->payload.other.data[i], 0, 255,
2210
                   1, i);
2211
        }
2212
    }
2213
2214
    // more_data_in_payload()
2215
#ifdef READ
2216
3070
    current_position = get_bits_count(rw) - start_position;
2217
3070
    if (current_position < 8 * current->payload_size) {
2218
#else
2219
3070
    current_position = put_bits_count(rw) - start_position;
2220

3070
    if (byte_alignment(rw) || more_data) {
2221
#endif
2222
644
        CHECK(FUNC(payload_extension)(ctx, rw, &current->extension_data,
2223
                                      current->payload_size, current_position));
2224
644
        fixed(1, bit_equal_to_one, 1);
2225
3684
        while (byte_alignment(rw))
2226
3040
            fixed(1, bit_equal_to_zero, 0);
2227
    }
2228
2229
#ifdef WRITE
2230
3070
    current->payload_size = (put_bits_count(rw) - start_position) >> 3;
2231
#endif
2232
2233
6140
    return 0;
2234
}
2235
2236
3070
static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
2237
                     H265RawSEI *current, int prefix)
2238
{
2239
    int err, k;
2240
2241
3070
    if (prefix)
2242
338
        HEADER("Prefix Supplemental Enhancement Information");
2243
    else
2244
2732
        HEADER("Suffix Supplemental Enhancement Information");
2245
2246
3070
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
2247
                                prefix ? HEVC_NAL_SEI_PREFIX
2248
                                       : HEVC_NAL_SEI_SUFFIX));
2249
2250
#ifdef READ
2251
1535
    for (k = 0; k < H265_MAX_SEI_PAYLOADS; k++) {
2252
1535
        uint32_t payload_type = 0;
2253
1535
        uint32_t payload_size = 0;
2254
        uint32_t tmp;
2255
2256
1535
        while (show_bits(rw, 8) == 0xff) {
2257
            fixed(8, ff_byte, 0xff);
2258
            payload_type += 255;
2259
        }
2260
1535
        xu(8, last_payload_type_byte, tmp, 0, 254, 0);
2261
1535
        payload_type += tmp;
2262
2263
1535
        while (show_bits(rw, 8) == 0xff) {
2264
            fixed(8, ff_byte, 0xff);
2265
            payload_size += 255;
2266
        }
2267
1535
        xu(8, last_payload_size_byte, tmp, 0, 254, 0);
2268
1535
        payload_size += tmp;
2269
2270
1535
        current->payload[k].payload_type = payload_type;
2271
1535
        current->payload[k].payload_size = payload_size;
2272
2273
1535
        current->payload_count++;
2274
1535
        CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k], prefix));
2275
2276
1535
        if (!cbs_h2645_read_more_rbsp_data(rw))
2277
1535
            break;
2278
    }
2279
1535
    if (k >= H265_MAX_SEI_PAYLOADS) {
2280
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
2281
               "SEI message: found %d.\n", k);
2282
        return AVERROR_INVALIDDATA;
2283
    }
2284
#else
2285
3070
    for (k = 0; k < current->payload_count; k++) {
2286
        PutBitContext start_state;
2287
        uint32_t tmp;
2288
        int need_size, i;
2289
2290
        // Somewhat clumsy: we write the payload twice when
2291
        // we don't know the size in advance.  This will mess
2292
        // with trace output, but is otherwise harmless.
2293
1535
        start_state = *rw;
2294
1535
        need_size = !current->payload[k].payload_size;
2295
3070
        for (i = 0; i < 1 + need_size; i++) {
2296
1535
            *rw = start_state;
2297
2298
1535
            tmp = current->payload[k].payload_type;
2299
1535
            while (tmp >= 255) {
2300
                fixed(8, ff_byte, 0xff);
2301
                tmp -= 255;
2302
            }
2303
1535
            xu(8, last_payload_type_byte, tmp, 0, 254, 0);
2304
2305
1535
            tmp = current->payload[k].payload_size;
2306
1535
            while (tmp >= 255) {
2307
                fixed(8, ff_byte, 0xff);
2308
                tmp -= 255;
2309
            }
2310
1535
            xu(8, last_payload_size_byte, tmp, 0, 254, 0);
2311
2312
1535
            CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k], prefix));
2313
        }
2314
    }
2315
#endif
2316
2317
3070
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
2318
2319
3070
    return 0;
2320
}