GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_h265_syntax_template.c Lines: 851 1263 67.4 %
Date: 2019-11-20 04:07:19 Branches: 859 2070 41.5 %

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
1968
static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
20
{
21
    int err;
22
23
1968
    fixed(1, rbsp_stop_one_bit, 1);
24
14290
    while (byte_alignment(rw) != 0)
25
12322
        fixed(1, rbsp_alignment_zero_bit, 0);
26
27
1968
    return 0;
28
}
29
30
7832
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
7832
    fixed(1, forbidden_zero_bit, 0);
37
38
7832
    if (expected_nal_unit_type >= 0)
39
1968
        u(6, nal_unit_type, expected_nal_unit_type,
40
                            expected_nal_unit_type);
41
    else
42
5864
        ub(6, nal_unit_type);
43
44
7832
    u(6, nuh_layer_id,          0, 62);
45
7832
    u(3, nuh_temporal_id_plus1, 1,  7);
46
47
7832
    return 0;
48
}
49
50
5864
static int FUNC(byte_alignment)(CodedBitstreamContext *ctx, RWContext *rw)
51
{
52
    int err;
53
54
5864
    fixed(1, alignment_bit_equal_to_one, 1);
55
29584
    while (byte_alignment(rw) != 0)
56
23720
        fixed(1, alignment_bit_equal_to_zero, 0);
57
58
5864
    return 0;
59
}
60
61
static int FUNC(extension_data)(CodedBitstreamContext *ctx, RWContext *rw,
62
                                H265RawPSExtensionData *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), 0, 1, 0);
84
#endif
85
    return 0;
86
}
87
88
98
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
98
    if (profile_present_flag) {
96
98
        u(2, general_profile_space, 0, 0);
97
98
        flag(general_tier_flag);
98
98
        ub(5, general_profile_idc);
99
100
3234
        for (j = 0; j < 32; j++)
101
3136
            flags(general_profile_compatibility_flag[j], 1, j);
102
103
98
        flag(general_progressive_source_flag);
104
98
        flag(general_interlaced_source_flag);
105
98
        flag(general_non_packed_constraint_flag);
106
98
        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


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


98
            profile_compatible(6) || profile_compatible(7) ||
112


98
            profile_compatible(8) || profile_compatible(9) ||
113

98
            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

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


98
        if (profile_compatible(1) || profile_compatible(2) ||
144
            profile_compatible(3) || profile_compatible(4) ||
145
            profile_compatible(5) || profile_compatible(9)) {
146
98
            flag(general_inbld_flag);
147
        } else {
148
            fixed(1, general_reserved_zero_bit, 0);
149
        }
150
#undef profile_compatible
151
    }
152
153
98
    ub(8, general_level_idc);
154
155
146
    for (i = 0; i < max_num_sub_layers_minus1; i++) {
156
48
        flags(sub_layer_profile_present_flag[i], 1, i);
157
48
        flags(sub_layer_level_present_flag[i],   1, i);
158
    }
159
160
98
    if (max_num_sub_layers_minus1 > 0) {
161
168
        for (i = max_num_sub_layers_minus1; i < 8; i++)
162
144
            fixed(2, reserved_zero_2bits, 0);
163
    }
164
165
146
    for (i = 0; i < max_num_sub_layers_minus1; i++) {
166
48
        if (current->sub_layer_profile_present_flag[i]) {
167
8
            us(2, sub_layer_profile_space[i], 0, 0, 1, i);
168
8
            flags(sub_layer_tier_flag[i],           1, i);
169
8
            ubs(5, sub_layer_profile_idc[i], 1, i);
170
171
264
            for (j = 0; j < 32; j++)
172
256
                flags(sub_layer_profile_compatibility_flag[i][j], 2, i, j);
173
174
8
            flags(sub_layer_progressive_source_flag[i],    1, i);
175
8
            flags(sub_layer_interlaced_source_flag[i],     1, i);
176
8
            flags(sub_layer_non_packed_constraint_flag[i], 1, i);
177
8
            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


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


8
                profile_compatible(6) || profile_compatible(7) ||
183


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

8
                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

8
            } 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
8
                fixed(24, sub_layer_reserved_zero_43bits, 0);
210
8
                fixed(19, sub_layer_reserved_zero_43bits, 0);
211
            }
212
213


8
            if (profile_compatible(1) || profile_compatible(2) ||
214
                profile_compatible(3) || profile_compatible(4) ||
215
                profile_compatible(5) || profile_compatible(9)) {
216
8
                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
48
        if (current->sub_layer_level_present_flag[i])
223
8
            ubs(8, sub_layer_level_idc[i], 1, i);
224
    }
225
226
98
    return 0;
227
}
228
229
8
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
8
    if (nal)
237
4
        current = &hrd->nal_sub_layer_hrd_parameters[sub_layer_id];
238
    else
239
4
        current = &hrd->vcl_sub_layer_hrd_parameters[sub_layer_id];
240
241
16
    for (i = 0; i <= hrd->cpb_cnt_minus1[sub_layer_id]; i++) {
242
8
        ues(bit_rate_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
243
8
        ues(cpb_size_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
244
8
        if (hrd->sub_pic_hrd_params_present_flag) {
245
4
            ues(cpb_size_du_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
246
4
            ues(bit_rate_du_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
247
        }
248
8
        flags(cbr_flag[i], 1, i);
249
    }
250
251
8
    return 0;
252
}
253
254
4
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
4
    if (common_inf_present_flag) {
261
4
        flag(nal_hrd_parameters_present_flag);
262
4
        flag(vcl_hrd_parameters_present_flag);
263
264
4
        if (current->nal_hrd_parameters_present_flag ||
265
            current->vcl_hrd_parameters_present_flag) {
266
4
            flag(sub_pic_hrd_params_present_flag);
267
4
            if (current->sub_pic_hrd_params_present_flag) {
268
2
                ub(8, tick_divisor_minus2);
269
2
                ub(5, du_cpb_removal_delay_increment_length_minus1);
270
2
                flag(sub_pic_cpb_params_in_pic_timing_sei_flag);
271
2
                ub(5, dpb_output_delay_du_length_minus1);
272
            }
273
274
4
            ub(4, bit_rate_scale);
275
4
            ub(4, cpb_size_scale);
276
4
            if (current->sub_pic_hrd_params_present_flag)
277
2
                ub(4, cpb_size_du_scale);
278
279
4
            ub(5, initial_cpb_removal_delay_length_minus1);
280
4
            ub(5, au_cpb_removal_delay_length_minus1);
281
4
            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
8
    for (i = 0; i <= max_num_sub_layers_minus1; i++) {
292
4
        flags(fixed_pic_rate_general_flag[i], 1, i);
293
294
4
        if (!current->fixed_pic_rate_general_flag[i])
295
2
            flags(fixed_pic_rate_within_cvs_flag[i], 1, i);
296
        else
297
2
            infer(fixed_pic_rate_within_cvs_flag[i], 1);
298
299
4
        if (current->fixed_pic_rate_within_cvs_flag[i]) {
300
2
            ues(elemental_duration_in_tc_minus1[i], 0, 2047, 1, i);
301
2
            infer(low_delay_hrd_flag[i], 0);
302
        } else
303
2
            flags(low_delay_hrd_flag[i], 1, i);
304
305
4
        if (!current->low_delay_hrd_flag[i])
306
4
            ues(cpb_cnt_minus1[i], 0, 31, 1, i);
307
        else
308
            infer(cpb_cnt_minus1[i], 0);
309
310
4
        if (current->nal_hrd_parameters_present_flag)
311
4
            CHECK(FUNC(sub_layer_hrd_parameters)(ctx, rw, current, 0, i));
312
4
        if (current->vcl_hrd_parameters_present_flag)
313
4
            CHECK(FUNC(sub_layer_hrd_parameters)(ctx, rw, current, 1, i));
314
    }
315
316
4
    return 0;
317
}
318
319
18
static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
320
                                H265RawVUI *current, const H265RawSPS *sps)
321
{
322
    int err;
323
324
18
    flag(aspect_ratio_info_present_flag);
325
18
    if (current->aspect_ratio_info_present_flag) {
326
6
        ub(8, aspect_ratio_idc);
327
6
        if (current->aspect_ratio_idc == 255) {
328
            ub(16, sar_width);
329
            ub(16, sar_height);
330
        }
331
    } else {
332
12
        infer(aspect_ratio_idc, 0);
333
    }
334
335
18
    flag(overscan_info_present_flag);
336
18
    if (current->overscan_info_present_flag)
337
        flag(overscan_appropriate_flag);
338
339
18
    flag(video_signal_type_present_flag);
340
18
    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
18
        infer(video_format,             5);
355
18
        infer(video_full_range_flag,    0);
356
18
        infer(colour_primaries,         2);
357
18
        infer(transfer_characteristics, 2);
358
18
        infer(matrix_coefficients,      2);
359
    }
360
361
18
    flag(chroma_loc_info_present_flag);
362
18
    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
18
        infer(chroma_sample_loc_type_top_field,    0);
367
18
        infer(chroma_sample_loc_type_bottom_field, 0);
368
    }
369
370
18
    flag(neutral_chroma_indication_flag);
371
18
    flag(field_seq_flag);
372
18
    flag(frame_field_info_present_flag);
373
374
18
    flag(default_display_window_flag);
375
18
    if (current->default_display_window_flag) {
376
6
        ue(def_disp_win_left_offset,   0, 16384);
377
6
        ue(def_disp_win_right_offset,  0, 16384);
378
6
        ue(def_disp_win_top_offset,    0, 16384);
379
6
        ue(def_disp_win_bottom_offset, 0, 16384);
380
    }
381
382
18
    flag(vui_timing_info_present_flag);
383
18
    if (current->vui_timing_info_present_flag) {
384
4
        u(32, vui_num_units_in_tick, 1, UINT32_MAX);
385
4
        u(32, vui_time_scale,        1, UINT32_MAX);
386
4
        flag(vui_poc_proportional_to_timing_flag);
387
4
        if (current->vui_poc_proportional_to_timing_flag)
388
2
            ue(vui_num_ticks_poc_diff_one_minus1, 0, UINT32_MAX - 1);
389
390
4
        flag(vui_hrd_parameters_present_flag);
391
4
        if (current->vui_hrd_parameters_present_flag) {
392
4
            CHECK(FUNC(hrd_parameters)(ctx, rw, &current->hrd_parameters,
393
                                       1, sps->sps_max_sub_layers_minus1));
394
        }
395
    }
396
397
18
    flag(bitstream_restriction_flag);
398
18
    if (current->bitstream_restriction_flag) {
399
12
        flag(tiles_fixed_structure_flag);
400
12
        flag(motion_vectors_over_pic_boundaries_flag);
401
12
        flag(restricted_ref_pic_lists_flag);
402
12
        ue(min_spatial_segmentation_idc,  0, 4095);
403
12
        ue(max_bytes_per_pic_denom,       0, 16);
404
12
        ue(max_bits_per_min_cu_denom,     0, 16);
405
12
        ue(log2_max_mv_length_horizontal, 0, 16);
406
12
        ue(log2_max_mv_length_vertical,   0, 16);
407
    } else {
408
6
        infer(tiles_fixed_structure_flag,    0);
409
6
        infer(motion_vectors_over_pic_boundaries_flag, 1);
410
6
        infer(min_spatial_segmentation_idc,  0);
411
6
        infer(max_bytes_per_pic_denom,       2);
412
6
        infer(max_bits_per_min_cu_denom,     1);
413
6
        infer(log2_max_mv_length_horizontal, 15);
414
6
        infer(log2_max_mv_length_vertical,   15);
415
    }
416
417
18
    return 0;
418
}
419
420
48
static int FUNC(vps)(CodedBitstreamContext *ctx, RWContext *rw,
421
                     H265RawVPS *current)
422
{
423
    int err, i, j;
424
425
48
    HEADER("Video Parameter Set");
426
427
48
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header, HEVC_NAL_VPS));
428
429
48
    ub(4, vps_video_parameter_set_id);
430
431
48
    flag(vps_base_layer_internal_flag);
432
48
    flag(vps_base_layer_available_flag);
433
48
    u(6, vps_max_layers_minus1,     0, HEVC_MAX_LAYERS - 1);
434
48
    u(3, vps_max_sub_layers_minus1, 0, HEVC_MAX_SUB_LAYERS - 1);
435
48
    flag(vps_temporal_id_nesting_flag);
436
437
48
    if (current->vps_max_sub_layers_minus1 == 0 &&
438
36
        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
48
    fixed(16, vps_reserved_0xffff_16bits, 0xffff);
446
447
48
    CHECK(FUNC(profile_tier_level)(ctx, rw, &current->profile_tier_level,
448
                                   1, current->vps_max_sub_layers_minus1));
449
450
48
    flag(vps_sub_layer_ordering_info_present_flag);
451
48
    for (i = (current->vps_sub_layer_ordering_info_present_flag ?
452
48
              0 : current->vps_max_sub_layers_minus1);
453
100
         i <= current->vps_max_sub_layers_minus1; i++) {
454
52
        ues(vps_max_dec_pic_buffering_minus1[i],
455
            0, HEVC_MAX_DPB_SIZE - 1,                        1, i);
456
52
        ues(vps_max_num_reorder_pics[i],
457
            0, current->vps_max_dec_pic_buffering_minus1[i], 1, i);
458
52
        ues(vps_max_latency_increase_plus1[i],
459
            0, UINT32_MAX - 1,                               1, i);
460
    }
461
48
    if (!current->vps_sub_layer_ordering_info_present_flag) {
462
32
        for (i = 0; i < current->vps_max_sub_layers_minus1; i++) {
463
20
            infer(vps_max_dec_pic_buffering_minus1[i],
464
                  current->vps_max_dec_pic_buffering_minus1[current->vps_max_sub_layers_minus1]);
465
20
            infer(vps_max_num_reorder_pics[i],
466
                  current->vps_max_num_reorder_pics[current->vps_max_sub_layers_minus1]);
467
20
            infer(vps_max_latency_increase_plus1[i],
468
                  current->vps_max_latency_increase_plus1[current->vps_max_sub_layers_minus1]);
469
        }
470
    }
471
472
48
    u(6, vps_max_layer_id,        0, HEVC_MAX_LAYERS - 1);
473
48
    ue(vps_num_layer_sets_minus1, 0, HEVC_MAX_LAYER_SETS - 1);
474
48
    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
96
    for (j = 0; j <= current->vps_max_layer_id; j++)
479
48
        infer(layer_id_included_flag[0][j], j == 0);
480
481
48
    flag(vps_timing_info_present_flag);
482
48
    if (current->vps_timing_info_present_flag) {
483
2
        u(32, vps_num_units_in_tick, 1, UINT32_MAX);
484
2
        u(32, vps_time_scale,        1, UINT32_MAX);
485
2
        flag(vps_poc_proportional_to_timing_flag);
486
2
        if (current->vps_poc_proportional_to_timing_flag)
487
2
            ue(vps_num_ticks_poc_diff_one_minus1, 0, UINT32_MAX - 1);
488
2
        ue(vps_num_hrd_parameters, 0, current->vps_num_layer_sets_minus1 + 1);
489
2
        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
48
    flag(vps_extension_flag);
505
48
    if (current->vps_extension_flag)
506
        CHECK(FUNC(extension_data)(ctx, rw, &current->extension_data));
507
508
48
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
509
510
48
    return 0;
511
}
512
513
2279
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
2279
    if (st_rps_idx != 0)
520
2205
        flag(inter_ref_pic_set_prediction_flag);
521
    else
522
74
        infer(inter_ref_pic_set_prediction_flag, 0);
523
524
2279
    if (current->inter_ref_pic_set_prediction_flag) {
525
        unsigned int ref_rps_idx, num_delta_pocs;
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
512
        if (st_rps_idx == sps->num_short_term_ref_pic_sets)
534
186
            ue(delta_idx_minus1, 0, st_rps_idx - 1);
535
        else
536
326
            infer(delta_idx_minus1, 0);
537
538
512
        ref_rps_idx = st_rps_idx - (current->delta_idx_minus1 + 1);
539
512
        ref = &sps->st_ref_pic_set[ref_rps_idx];
540
512
        num_delta_pocs = ref->num_negative_pics + ref->num_positive_pics;
541
542
512
        flag(delta_rps_sign);
543
512
        ue(abs_delta_rps_minus1, 0, INT16_MAX);
544
512
        delta_rps = (1 - 2 * current->delta_rps_sign) *
545
512
            (current->abs_delta_rps_minus1 + 1);
546
547
2754
        for (j = 0; j <= num_delta_pocs; j++) {
548
2242
            flags(used_by_curr_pic_flag[j], 1, j);
549
2242
            if (!current->used_by_curr_pic_flag[j])
550
511
                flags(use_delta_flag[j], 1, j);
551
            else
552
1731
                infer(use_delta_flag[j], 1);
553
        }
554
555
        // Since the stored form of an RPS here is actually the delta-step
556
        // form used when inter_ref_pic_set_prediction_flag is not set, we
557
        // need to reconstruct that here in order to be able to refer to
558
        // the RPS later (which is required for parsing, because we don't
559
        // even know what syntax elements appear without it).  Therefore,
560
        // this code takes the delta-step form of the reference set, turns
561
        // it into the delta-array form, applies the prediction process of
562
        // 7.4.8, converts the result back to the delta-step form, and
563
        // stores that as the current set for future use.  Note that the
564
        // inferences here mean that writers using prediction will need
565
        // to fill in the delta-step values correctly as well - since the
566
        // whole RPS prediction process is somewhat overly sophisticated,
567
        // this hopefully forms a useful check for them to ensure their
568
        // predicted form actually matches what was intended rather than
569
        // an onerous additional requirement.
570
571
512
        d_poc = 0;
572
1681
        for (i = 0; i < ref->num_negative_pics; i++) {
573
1169
            d_poc -= ref->delta_poc_s0_minus1[i] + 1;
574
1169
            ref_delta_poc_s0[i] = d_poc;
575
        }
576
512
        d_poc = 0;
577
1073
        for (i = 0; i < ref->num_positive_pics; i++) {
578
561
            d_poc += ref->delta_poc_s1_minus1[i] + 1;
579
561
            ref_delta_poc_s1[i] = d_poc;
580
        }
581
582
512
        i = 0;
583
1073
        for (j = ref->num_positive_pics - 1; j >= 0; j--) {
584
561
            d_poc = ref_delta_poc_s1[j] + delta_rps;
585

561
            if (d_poc < 0 && current->use_delta_flag[ref->num_negative_pics + j]) {
586
144
                delta_poc_s0[i] = d_poc;
587
144
                used_by_curr_pic_s0[i++] =
588
144
                    current->used_by_curr_pic_flag[ref->num_negative_pics + j];
589
            }
590
        }
591

512
        if (delta_rps < 0 && current->use_delta_flag[num_delta_pocs]) {
592
118
            delta_poc_s0[i] = delta_rps;
593
118
            used_by_curr_pic_s0[i++] =
594
118
                current->used_by_curr_pic_flag[num_delta_pocs];
595
        }
596
1681
        for (j = 0; j < ref->num_negative_pics; j++) {
597
1169
            d_poc = ref_delta_poc_s0[j] + delta_rps;
598

1169
            if (d_poc < 0 && current->use_delta_flag[j]) {
599
850
                delta_poc_s0[i] = d_poc;
600
850
                used_by_curr_pic_s0[i++] = current->used_by_curr_pic_flag[j];
601
            }
602
        }
603
604
512
        infer(num_negative_pics, i);
605
1624
        for (i = 0; i < current->num_negative_pics; i++) {
606

1112
            infer(delta_poc_s0_minus1[i],
607
                  -(delta_poc_s0[i] - (i == 0 ? 0 : delta_poc_s0[i - 1])) - 1);
608
1112
            infer(used_by_curr_pic_s0_flag[i], used_by_curr_pic_s0[i]);
609
        }
610
611
512
        i = 0;
612
1681
        for (j = ref->num_negative_pics - 1; j >= 0; j--) {
613
1169
            d_poc = ref_delta_poc_s0[j] + delta_rps;
614

1169
            if (d_poc > 0 && current->use_delta_flag[j]) {
615
                delta_poc_s1[i] = d_poc;
616
                used_by_curr_pic_s1[i++] = current->used_by_curr_pic_flag[j];
617
            }
618
        }
619

512
        if (delta_rps > 0 && current->use_delta_flag[num_delta_pocs]) {
620
233
            delta_poc_s1[i] = delta_rps;
621
233
            used_by_curr_pic_s1[i++] =
622
233
                current->used_by_curr_pic_flag[num_delta_pocs];
623
        }
624
1073
        for (j = 0; j < ref->num_positive_pics; j++) {
625
561
            d_poc = ref_delta_poc_s1[j] + delta_rps;
626

561
            if (d_poc > 0 && current->use_delta_flag[ref->num_negative_pics + j]) {
627
386
                delta_poc_s1[i] = d_poc;
628
386
                used_by_curr_pic_s1[i++] =
629
386
                    current->used_by_curr_pic_flag[ref->num_negative_pics + j];
630
            }
631
        }
632
633
512
        infer(num_positive_pics, i);
634
1131
        for (i = 0; i < current->num_positive_pics; i++) {
635

619
            infer(delta_poc_s1_minus1[i],
636
                  delta_poc_s1[i] - (i == 0 ? 0 : delta_poc_s1[i - 1]) - 1);
637
619
            infer(used_by_curr_pic_s1_flag[i], used_by_curr_pic_s1[i]);
638
        }
639
640
    } else {
641
1767
        ue(num_negative_pics, 0, 15);
642
1767
        ue(num_positive_pics, 0, 15 - current->num_negative_pics);
643
644
6623
        for (i = 0; i < current->num_negative_pics; i++) {
645
4856
            ues(delta_poc_s0_minus1[i], 0, INT16_MAX, 1, i);
646
4856
            flags(used_by_curr_pic_s0_flag[i],        1, i);
647
        }
648
649
2172
        for (i = 0; i < current->num_positive_pics; i++) {
650
405
            ues(delta_poc_s1_minus1[i], 0, INT16_MAX, 1, i);
651
405
            flags(used_by_curr_pic_s1_flag[i],        1, i);
652
        }
653
    }
654
655
2279
    return 0;
656
}
657
658
88
static int FUNC(scaling_list_data)(CodedBitstreamContext *ctx, RWContext *rw,
659
                                   H265RawScalingList *current)
660
{
661
    int sizeId, matrixId;
662
    int err, n, i;
663
664
440
    for (sizeId = 0; sizeId < 4; sizeId++) {
665

2112
        for (matrixId = 0; matrixId < 6; matrixId += (sizeId == 3 ? 3 : 1)) {
666
1760
            flags(scaling_list_pred_mode_flag[sizeId][matrixId],
667
                  2, sizeId, matrixId);
668
1760
            if (!current->scaling_list_pred_mode_flag[sizeId][matrixId]) {
669

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

2346
            if (!sps->separate_colour_plane_flag && sps->chroma_format_idc != 0)
1406
2346
                flag(slice_sao_chroma_flag);
1407
            else
1408
                infer(slice_sao_chroma_flag, 0);
1409
        } else {
1410
1634
            infer(slice_sao_luma_flag,   0);
1411
1634
            infer(slice_sao_chroma_flag, 0);
1412
        }
1413
1414
3980
        if (current->slice_type == HEVC_SLICE_P ||
1415
2296
            current->slice_type == HEVC_SLICE_B) {
1416
3877
            flag(num_ref_idx_active_override_flag);
1417
3877
            if (current->num_ref_idx_active_override_flag) {
1418
1826
                ue(num_ref_idx_l0_active_minus1, 0, 14);
1419
1826
                if (current->slice_type == HEVC_SLICE_B)
1420
456
                    ue(num_ref_idx_l1_active_minus1, 0, 14);
1421
                else
1422
1370
                    infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
1423
            } else {
1424
2051
                infer(num_ref_idx_l0_active_minus1, pps->num_ref_idx_l0_default_active_minus1);
1425
2051
                infer(num_ref_idx_l1_active_minus1, pps->num_ref_idx_l1_default_active_minus1);
1426
            }
1427
1428

3877
            if (pps->lists_modification_present_flag && num_pic_total_curr > 1)
1429
748
                CHECK(FUNC(ref_pic_lists_modification)(ctx, rw, current,
1430
                                                       num_pic_total_curr));
1431
1432
3877
            if (current->slice_type == HEVC_SLICE_B)
1433
2193
                flag(mvd_l1_zero_flag);
1434
3877
            if (pps->cabac_init_present_flag)
1435
2469
                flag(cabac_init_flag);
1436
            else
1437
1408
                infer(cabac_init_flag, 0);
1438
3877
            if (current->slice_temporal_mvp_enabled_flag) {
1439
2559
                if (current->slice_type == HEVC_SLICE_B)
1440
2193
                    flag(collocated_from_l0_flag);
1441
                else
1442
366
                    infer(collocated_from_l0_flag, 1);
1443
2559
                if (current->collocated_from_l0_flag) {
1444
1181
                    if (current->num_ref_idx_l0_active_minus1 > 0)
1445
1063
                        ue(collocated_ref_idx, 0, current->num_ref_idx_l0_active_minus1);
1446
                    else
1447
118
                        infer(collocated_ref_idx, 0);
1448
                } else {
1449
1378
                    if (current->num_ref_idx_l1_active_minus1 > 0)
1450
1287
                        ue(collocated_ref_idx, 0, current->num_ref_idx_l1_active_minus1);
1451
                    else
1452
91
                        infer(collocated_ref_idx, 0);
1453
                }
1454
            }
1455
1456

3877
            if ((pps->weighted_pred_flag   && current->slice_type == HEVC_SLICE_P) ||
1457

3622
                (pps->weighted_bipred_flag && current->slice_type == HEVC_SLICE_B))
1458
255
                CHECK(FUNC(pred_weight_table)(ctx, rw, current));
1459
1460
3877
            ue(five_minus_max_num_merge_cand, 0, 4);
1461
3877
            if (sps->motion_vector_resolution_control_idc == 2)
1462
                flag(use_integer_mv_flag);
1463
            else
1464
3877
                infer(use_integer_mv_flag, sps->motion_vector_resolution_control_idc);
1465
        }
1466
1467
3980
        se(slice_qp_delta,
1468
           - 6 * sps->bit_depth_luma_minus8 - (pps->init_qp_minus26 + 26),
1469
           + 51 - (pps->init_qp_minus26 + 26));
1470
3980
        if (pps->pps_slice_chroma_qp_offsets_present_flag) {
1471
            se(slice_cb_qp_offset, -12, +12);
1472
            se(slice_cr_qp_offset, -12, +12);
1473
        } else {
1474
3980
            infer(slice_cb_qp_offset, 0);
1475
3980
            infer(slice_cr_qp_offset, 0);
1476
        }
1477
3980
        if (pps->pps_slice_act_qp_offsets_present_flag) {
1478
            se(slice_act_y_qp_offset,
1479
               -12 - (pps->pps_act_y_qp_offset_plus5 - 5),
1480
               +12 - (pps->pps_act_y_qp_offset_plus5 - 5));
1481
            se(slice_act_cb_qp_offset,
1482
               -12 - (pps->pps_act_cb_qp_offset_plus5 - 5),
1483
               +12 - (pps->pps_act_cb_qp_offset_plus5 - 5));
1484
            se(slice_act_cr_qp_offset,
1485
               -12 - (pps->pps_act_cr_qp_offset_plus3 - 3),
1486
               +12 - (pps->pps_act_cr_qp_offset_plus3 - 3));
1487
        } else {
1488
3980
            infer(slice_act_y_qp_offset,  0);
1489
3980
            infer(slice_act_cb_qp_offset, 0);
1490
3980
            infer(slice_act_cr_qp_offset, 0);
1491
        }
1492
3980
        if (pps->chroma_qp_offset_list_enabled_flag)
1493
            flag(cu_chroma_qp_offset_enabled_flag);
1494
        else
1495
3980
            infer(cu_chroma_qp_offset_enabled_flag, 0);
1496
1497
3980
        if (pps->deblocking_filter_override_enabled_flag)
1498
37
            flag(deblocking_filter_override_flag);
1499
        else
1500
3943
            infer(deblocking_filter_override_flag, 0);
1501
3980
        if (current->deblocking_filter_override_flag) {
1502
1
            flag(slice_deblocking_filter_disabled_flag);
1503
1
            if (!current->slice_deblocking_filter_disabled_flag) {
1504
                se(slice_beta_offset_div2, -6, +6);
1505
                se(slice_tc_offset_div2,   -6, +6);
1506
            } else {
1507
1
                infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
1508
1
                infer(slice_tc_offset_div2,   pps->pps_tc_offset_div2);
1509
            }
1510
        } else {
1511
3979
            infer(slice_deblocking_filter_disabled_flag,
1512
                  pps->pps_deblocking_filter_disabled_flag);
1513
3979
            infer(slice_beta_offset_div2, pps->pps_beta_offset_div2);
1514
3979
            infer(slice_tc_offset_div2,   pps->pps_tc_offset_div2);
1515
        }
1516
3980
        if (pps->pps_loop_filter_across_slices_enabled_flag &&
1517

3235
            (current->slice_sao_luma_flag || current->slice_sao_chroma_flag ||
1518
2255
             !current->slice_deblocking_filter_disabled_flag))
1519
3234
            flag(slice_loop_filter_across_slices_enabled_flag);
1520
        else
1521
746
            infer(slice_loop_filter_across_slices_enabled_flag,
1522
                  pps->pps_loop_filter_across_slices_enabled_flag);
1523
    }
1524
1525

5864
    if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
1526
        unsigned int num_entry_point_offsets_limit;
1527

1805
        if (!pps->tiles_enabled_flag && pps->entropy_coding_sync_enabled_flag)
1528
437
            num_entry_point_offsets_limit = pic_height_in_ctbs_y - 1;
1529

1368
        else if (pps->tiles_enabled_flag && !pps->entropy_coding_sync_enabled_flag)
1530
1368
            num_entry_point_offsets_limit =
1531
1368
                (pps->num_tile_columns_minus1 + 1) * (pps->num_tile_rows_minus1 + 1);
1532
        else
1533
            num_entry_point_offsets_limit =
1534
                (pps->num_tile_columns_minus1 + 1) * pic_height_in_ctbs_y - 1;
1535
1805
        ue(num_entry_point_offsets, 0, num_entry_point_offsets_limit);
1536
1537
1805
        if (current->num_entry_point_offsets > HEVC_MAX_ENTRY_POINT_OFFSETS) {
1538
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many entry points: "
1539
                   "%"PRIu16".\n", current->num_entry_point_offsets);
1540
            return AVERROR_PATCHWELCOME;
1541
        }
1542
1543
1805
        if (current->num_entry_point_offsets > 0) {
1544
769
            ue(offset_len_minus1, 0, 31);
1545
2325
            for (i = 0; i < current->num_entry_point_offsets; i++)
1546
1556
                ubs(current->offset_len_minus1 + 1, entry_point_offset_minus1[i], 1, i);
1547
        }
1548
    }
1549
1550
5864
    if (pps->slice_segment_header_extension_present_flag) {
1551
437
        ue(slice_segment_header_extension_length, 0, 256);
1552
3539
        for (i = 0; i < current->slice_segment_header_extension_length; i++)
1553
3102
            us(8, slice_segment_header_extension_data_byte[i], 0x00, 0xff, 1, i);
1554
    }
1555
1556
5864
    CHECK(FUNC(byte_alignment)(ctx, rw));
1557
1558
5864
    return 0;
1559
}
1560
1561
5
static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
1562
                                      H265RawSEIBufferingPeriod *current,
1563
                                      uint32_t *payload_size)
1564
{
1565
5
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1566
    const H265RawSPS *sps;
1567
    const H265RawHRDParameters *hrd;
1568
    int err, i, length;
1569
1570
#ifdef READ
1571
    int start_pos, end_pos, bits_left;
1572
5
    start_pos = get_bits_count(rw);
1573
#endif
1574
1575
5
    HEADER("Buffering Period");
1576
1577
5
    ue(bp_seq_parameter_set_id, 0, HEVC_MAX_SPS_COUNT - 1);
1578
1579
5
    sps = h265->sps[current->bp_seq_parameter_set_id];
1580
5
    if (!sps) {
1581
        av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
1582
               current->bp_seq_parameter_set_id);
1583
        return AVERROR_INVALIDDATA;
1584
    }
1585
5
    h265->active_sps = sps;
1586
1587
5
    if (!sps->vui_parameters_present_flag ||
1588
5
        !sps->vui.vui_hrd_parameters_present_flag) {
1589
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Buffering period SEI requires "
1590
               "HRD parameters to be present in SPS.\n");
1591
        return AVERROR_INVALIDDATA;
1592
    }
1593
5
    hrd = &sps->vui.hrd_parameters;
1594
5
    if (!hrd->nal_hrd_parameters_present_flag &&
1595
        !hrd->vcl_hrd_parameters_present_flag) {
1596
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Buffering period SEI requires "
1597
               "NAL or VCL HRD parameters to be present.\n");
1598
        return AVERROR_INVALIDDATA;
1599
    }
1600
1601
5
    if (!hrd->sub_pic_hrd_params_present_flag)
1602
2
        flag(irap_cpb_params_present_flag);
1603
    else
1604
3
        infer(irap_cpb_params_present_flag, 0);
1605
5
    if (current->irap_cpb_params_present_flag) {
1606
        length = hrd->au_cpb_removal_delay_length_minus1 + 1;
1607
        ub(length, cpb_delay_offset);
1608
        length = hrd->dpb_output_delay_length_minus1 + 1;
1609
        ub(length, dpb_delay_offset);
1610
    } else {
1611
5
        infer(cpb_delay_offset, 0);
1612
5
        infer(dpb_delay_offset, 0);
1613
    }
1614
1615
5
    flag(concatenation_flag);
1616
1617
5
    length = hrd->au_cpb_removal_delay_length_minus1 + 1;
1618
5
    ub(length, au_cpb_removal_delay_delta_minus1);
1619
1620
5
    if (hrd->nal_hrd_parameters_present_flag) {
1621
10
        for (i = 0; i <= hrd->cpb_cnt_minus1[0]; i++) {
1622
5
            length = hrd->initial_cpb_removal_delay_length_minus1 + 1;
1623
1624
5
            ubs(length, nal_initial_cpb_removal_delay[i], 1, i);
1625
5
            ubs(length, nal_initial_cpb_removal_offset[i], 1, i);
1626
1627
5
            if (hrd->sub_pic_hrd_params_present_flag ||
1628
2
                current->irap_cpb_params_present_flag) {
1629
3
                ubs(length, nal_initial_alt_cpb_removal_delay[i], 1, i);
1630
3
                ubs(length, nal_initial_alt_cpb_removal_offset[i], 1, i);
1631
            }
1632
        }
1633
    }
1634
5
    if (hrd->vcl_hrd_parameters_present_flag) {
1635
10
        for (i = 0; i <= hrd->cpb_cnt_minus1[0]; i++) {
1636
5
            length = hrd->initial_cpb_removal_delay_length_minus1 + 1;
1637
1638
5
            ubs(length, vcl_initial_cpb_removal_delay[i], 1, i);
1639
5
            ubs(length, vcl_initial_cpb_removal_offset[i], 1, i);
1640
1641
5
            if (hrd->sub_pic_hrd_params_present_flag ||
1642
2
                current->irap_cpb_params_present_flag) {
1643
3
                ubs(length, vcl_initial_alt_cpb_removal_delay[i], 1, i);
1644
3
                ubs(length, vcl_initial_alt_cpb_removal_offset[i], 1, i);
1645
            }
1646
        }
1647
    }
1648
1649
#ifdef READ
1650
    // payload_extension_present() - true if we are before the last 1-bit
1651
    // in the payload structure, which must be in the last byte.
1652
5
    end_pos = get_bits_count(rw);
1653
5
    bits_left = *payload_size * 8 - (end_pos - start_pos);
1654

5
    if (bits_left > 0 &&
1655
        (bits_left > 7 || ff_ctz(show_bits(rw, bits_left)) < bits_left - 1))
1656
        flag(use_alt_cpb_params_flag);
1657
    else
1658
5
        infer(use_alt_cpb_params_flag, 0);
1659
#else
1660
5
    if (current->use_alt_cpb_params_flag)
1661
        flag(use_alt_cpb_params_flag);
1662
#endif
1663
1664
5
    return 0;
1665
}
1666
1667
156
static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
1668
                                H265RawSEIPicTiming *current)
1669
{
1670
156
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1671
    const H265RawSPS *sps;
1672
    const H265RawHRDParameters *hrd;
1673
    int err, expected_source_scan_type, i, length;
1674
1675
156
    HEADER("Picture Timing");
1676
1677
156
    sps = h265->active_sps;
1678
156
    if (!sps) {
1679
        av_log(ctx->log_ctx, AV_LOG_ERROR,
1680
               "No active SPS for pic_timing.\n");
1681
        return AVERROR_INVALIDDATA;
1682
    }
1683
1684
156
    expected_source_scan_type = 2 -
1685
156
        2 * sps->profile_tier_level.general_interlaced_source_flag -
1686
156
        sps->profile_tier_level.general_progressive_source_flag;
1687
1688
156
    if (sps->vui.frame_field_info_present_flag) {
1689
60
        u(4, pic_struct, 0, 12);
1690

60
        u(2, source_scan_type,
1691
          expected_source_scan_type >= 0 ? expected_source_scan_type : 0,
1692
          expected_source_scan_type >= 0 ? expected_source_scan_type : 2);
1693
60
        flag(duplicate_flag);
1694
    } else {
1695
96
        infer(pic_struct, 0);
1696

96
        infer(source_scan_type,
1697
              expected_source_scan_type >= 0 ? expected_source_scan_type : 2);
1698
96
        infer(duplicate_flag, 0);
1699
    }
1700
1701
156
    if (sps->vui_parameters_present_flag &&
1702
156
        sps->vui.vui_hrd_parameters_present_flag)
1703
156
        hrd = &sps->vui.hrd_parameters;
1704
    else
1705
        hrd = NULL;
1706

156
    if (hrd && (hrd->nal_hrd_parameters_present_flag ||
1707
                hrd->vcl_hrd_parameters_present_flag)) {
1708
156
        length = hrd->au_cpb_removal_delay_length_minus1 + 1;
1709
156
        ub(length, au_cpb_removal_delay_minus1);
1710
1711
156
        length = hrd->dpb_output_delay_length_minus1 + 1;
1712
156
        ub(length, pic_dpb_output_delay);
1713
1714
156
        if (hrd->sub_pic_hrd_params_present_flag) {
1715
96
            length = hrd->dpb_output_delay_du_length_minus1 + 1;
1716
96
            ub(length, pic_dpb_output_du_delay);
1717
        }
1718
1719
156
        if (hrd->sub_pic_hrd_params_present_flag &&
1720
96
            hrd->sub_pic_cpb_params_in_pic_timing_sei_flag) {
1721
            // Each decoding unit must contain at least one slice segment.
1722
96
            ue(num_decoding_units_minus1, 0, HEVC_MAX_SLICE_SEGMENTS);
1723
96
            flag(du_common_cpb_removal_delay_flag);
1724
1725
96
            length = hrd->du_cpb_removal_delay_increment_length_minus1 + 1;
1726
96
            if (current->du_common_cpb_removal_delay_flag)
1727
                ub(length, du_common_cpb_removal_delay_increment_minus1);
1728
1729
480
            for (i = 0; i <= current->num_decoding_units_minus1; i++) {
1730
384
                ues(num_nalus_in_du_minus1[i],
1731
                    0, HEVC_MAX_SLICE_SEGMENTS, 1, i);
1732
384
                if (!current->du_common_cpb_removal_delay_flag &&
1733
384
                    i < current->num_decoding_units_minus1)
1734
288
                    ubs(length, du_cpb_removal_delay_increment_minus1[i], 1, i);
1735
            }
1736
        }
1737
    }
1738
1739
156
    return 0;
1740
}
1741
1742
static int FUNC(sei_pan_scan_rect)(CodedBitstreamContext *ctx, RWContext *rw,
1743
                                   H265RawSEIPanScanRect *current)
1744
{
1745
    int err, i;
1746
1747
    HEADER("Pan-Scan Rectangle");
1748
1749
    ue(pan_scan_rect_id, 0, UINT32_MAX - 1);
1750
    flag(pan_scan_rect_cancel_flag);
1751
1752
    if (!current->pan_scan_rect_cancel_flag) {
1753
        ue(pan_scan_cnt_minus1, 0, 2);
1754
1755
        for (i = 0; i <= current->pan_scan_cnt_minus1; i++) {
1756
            ses(pan_scan_rect_left_offset[i],   INT32_MIN + 1, INT32_MAX, 1, i);
1757
            ses(pan_scan_rect_right_offset[i],  INT32_MIN + 1, INT32_MAX, 1, i);
1758
            ses(pan_scan_rect_top_offset[i],    INT32_MIN + 1, INT32_MAX, 1, i);
1759
            ses(pan_scan_rect_bottom_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
1760
        }
1761
1762
        flag(pan_scan_rect_persistence_flag);
1763
    }
1764
1765
    return 0;
1766
}
1767
1768
static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext *rw,
1769
                                          H265RawSEIUserDataRegistered *current,
1770
                                          uint32_t *payload_size)
1771
{
1772
    int err, i, j;
1773
1774
    HEADER("User Data Registered ITU-T T.35");
1775
1776
    u(8, itu_t_t35_country_code, 0x00, 0xff);
1777
    if (current->itu_t_t35_country_code != 0xff)
1778
        i = 1;
1779
    else {
1780
        u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
1781
        i = 2;
1782
    }
1783
1784
#ifdef READ
1785
    if (*payload_size < i) {
1786
        av_log(ctx->log_ctx, AV_LOG_ERROR,
1787
               "Invalid SEI user data registered payload.\n");
1788
        return AVERROR_INVALIDDATA;
1789
    }
1790
    current->data_length = *payload_size - i;
1791
#else
1792
    *payload_size = i + current->data_length;
1793
#endif
1794
1795
    allocate(current->data, current->data_length);
1796
    for (j = 0; j < current->data_length; j++)
1797
        xu(8, itu_t_t35_payload_byte[i], current->data[j], 0x00, 0xff, 1, i + j);
1798
1799
    return 0;
1800
}
1801
1802
static int FUNC(sei_user_data_unregistered)(CodedBitstreamContext *ctx, RWContext *rw,
1803
                                            H265RawSEIUserDataUnregistered *current,
1804
                                            uint32_t *payload_size)
1805
{
1806
    int err, i;
1807
1808
    HEADER("User Data Unregistered");
1809
1810
#ifdef READ
1811
    if (*payload_size < 16) {
1812
        av_log(ctx->log_ctx, AV_LOG_ERROR,
1813
               "Invalid SEI user data unregistered payload.\n");
1814
        return AVERROR_INVALIDDATA;
1815
    }
1816
    current->data_length = *payload_size - 16;
1817
#else
1818
    *payload_size = 16 + current->data_length;
1819
#endif
1820
1821
    for (i = 0; i < 16; i++)
1822
        us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
1823
1824
    allocate(current->data, current->data_length);
1825
1826
    for (i = 0; i < current->data_length; i++)
1827
        xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
1828
1829
    return 0;
1830
}
1831
1832
5
static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
1833
                                    H265RawSEIRecoveryPoint *current)
1834
{
1835
    int err;
1836
1837
5
    HEADER("Recovery Point");
1838
1839
5
    se(recovery_poc_cnt, -32768, 32767);
1840
1841
5
    flag(exact_match_flag);
1842
5
    flag(broken_link_flag);
1843
1844
5
    return 0;
1845
}
1846
1847
static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
1848
                                         H265RawSEIDisplayOrientation *current)
1849
{
1850
    int err;
1851
1852
    HEADER("Display Orientation");
1853
1854
    flag(display_orientation_cancel_flag);
1855
    if (!current->display_orientation_cancel_flag) {
1856
        flag(hor_flip);
1857
        flag(ver_flip);
1858
        ub(16, anticlockwise_rotation);
1859
        flag(display_orientation_persistence_flag);
1860
    }
1861
1862
    return 0;
1863
}
1864
1865
3
static int FUNC(sei_active_parameter_sets)(CodedBitstreamContext *ctx, RWContext *rw,
1866
                                           H265RawSEIActiveParameterSets *current)
1867
{
1868
3
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1869
    const H265RawVPS *vps;
1870
    int err, i;
1871
1872
3
    HEADER("Active Parameter Sets");
1873
1874
3
    u(4, active_video_parameter_set_id, 0, HEVC_MAX_VPS_COUNT);
1875
3
    vps = h265->vps[current->active_video_parameter_set_id];
1876
3
    if (!vps) {
1877
        av_log(ctx->log_ctx, AV_LOG_ERROR, "VPS id %d not available for active "
1878
               "parameter sets.\n", current->active_video_parameter_set_id);
1879
        return AVERROR_INVALIDDATA;
1880
    }
1881
3
    h265->active_vps = vps;
1882
1883
3
    flag(self_contained_cvs_flag);
1884
3
    flag(no_parameter_set_update_flag);
1885
1886
3
    ue(num_sps_ids_minus1, 0, HEVC_MAX_SPS_COUNT - 1);
1887
6
    for (i = 0; i <= current->num_sps_ids_minus1; i++)
1888
3
        ues(active_seq_parameter_set_id[i], 0, HEVC_MAX_SPS_COUNT - 1, 1, i);
1889
1890
3
    for (i = vps->vps_base_layer_internal_flag;
1891
3
         i <= FFMIN(62, vps->vps_max_layers_minus1); i++) {
1892
        ues(layer_sps_idx[i], 0, current->num_sps_ids_minus1, 1, i);
1893
1894
        if (i == 0)
1895
            h265->active_sps = h265->sps[current->active_seq_parameter_set_id[current->layer_sps_idx[0]]];
1896
    }
1897
1898
3
    return 0;
1899
}
1900
1901
1366
static int FUNC(sei_decoded_picture_hash)(CodedBitstreamContext *ctx, RWContext *rw,
1902
                                          H265RawSEIDecodedPictureHash *current)
1903
{
1904
1366
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1905
1366
    const H265RawSPS *sps = h265->active_sps;
1906
    int err, c, i;
1907
1908
1366
    HEADER("Decoded Picture Hash");
1909
1910
1366
    if (!sps) {
1911
        av_log(ctx->log_ctx, AV_LOG_ERROR,
1912
               "No active SPS for decoded picture hash.\n");
1913
        return AVERROR_INVALIDDATA;
1914
    }
1915
1916
1366
    u(8, hash_type, 0, 2);
1917
1918

5464
    for (c = 0; c < (sps->chroma_format_idc == 0 ? 1 : 3); c++) {
1919
4098
        if (current->hash_type == 0) {
1920
63750
            for (i = 0; i < 16; i++)
1921
60000
                us(8, picture_md5[c][i], 0x00, 0xff, 2, c, i);
1922
348
        } else if (current->hash_type == 1) {
1923
            us(16, picture_crc[c], 0x0000, 0xffff, 1, c);
1924
348
        } else if (current->hash_type == 2) {
1925
348
            us(32, picture_checksum[c], 0x00000000, 0xffffffff, 1, c);
1926
        }
1927
    }
1928
1929
1366
    return 0;
1930
}
1931
1932
static int FUNC(sei_time_code)(CodedBitstreamContext *ctx, RWContext *rw,
1933
                               H265RawSEITimeCode *current)
1934
{
1935
    int err, i;
1936
1937
    HEADER("Time Code");
1938
1939
    u(2, num_clock_ts, 1, 3);
1940
1941
    for (i = 0; i < current->num_clock_ts; i++) {
1942
        flags(clock_timestamp_flag[i],   1, i);
1943
1944
        if (current->clock_timestamp_flag[i]) {
1945
            flags(units_field_based_flag[i], 1, i);
1946
            us(5, counting_type[i], 0, 6,    1, i);
1947
            flags(full_timestamp_flag[i],    1, i);
1948
            flags(discontinuity_flag[i],     1, i);
1949
            flags(cnt_dropped_flag[i],       1, i);
1950
1951
            ubs(9, n_frames[i], 1, i);
1952
1953
            if (current->full_timestamp_flag[i]) {
1954
                us(6, seconds_value[i], 0, 59, 1, i);
1955
                us(6, minutes_value[i], 0, 59, 1, i);
1956
                us(5, hours_value[i],   0, 23, 1, i);
1957
            } else {
1958
                flags(seconds_flag[i], 1, i);
1959
                if (current->seconds_flag[i]) {
1960
                    us(6, seconds_value[i], 0, 59, 1, i);
1961
                    flags(minutes_flag[i], 1, i);
1962
                    if (current->minutes_flag[i]) {
1963
                        us(6, minutes_value[i], 0, 59, 1, i);
1964
                        flags(hours_flag[i], 1, i);
1965
                        if (current->hours_flag[i])
1966
                            us(5, hours_value[i], 0, 23, 1, i);
1967
                    }
1968
                }
1969
            }
1970
1971
            ubs(5, time_offset_length[i], 1, i);
1972
            if (current->time_offset_length[i] > 0)
1973
                ibs(current->time_offset_length[i], time_offset_value[i], 1, i);
1974
            else
1975
                infer(time_offset_value[i], 0);
1976
        }
1977
    }
1978
1979
    return 0;
1980
}
1981
1982
static int FUNC(sei_mastering_display)(CodedBitstreamContext *ctx, RWContext *rw,
1983
                                       H265RawSEIMasteringDisplayColourVolume *current)
1984
{
1985
    int err, c;
1986
1987
    HEADER("Mastering Display Colour Volume");
1988
1989
    for (c = 0; c < 3; c++) {
1990
        us(16, display_primaries_x[c], 0, 50000, 1, c);
1991
        us(16, display_primaries_y[c], 0, 50000, 1, c);
1992
    }
1993
1994
    u(16, white_point_x, 0, 50000);
1995
    u(16, white_point_y, 0, 50000);
1996
1997
    u(32, max_display_mastering_luminance,
1998
      1, MAX_UINT_BITS(32));
1999
    u(32, min_display_mastering_luminance,
2000
      0, current->max_display_mastering_luminance - 1);
2001
2002
    return 0;
2003
}
2004
2005
static int FUNC(sei_content_light_level)(CodedBitstreamContext *ctx, RWContext *rw,
2006
                                         H265RawSEIContentLightLevelInfo *current)
2007
{
2008
    int err;
2009
2010
    HEADER("Content Light Level");
2011
2012
    ub(16, max_content_light_level);
2013
    ub(16, max_pic_average_light_level);
2014
2015
    return 0;
2016
}
2017
2018
static int FUNC(sei_alternative_transfer_characteristics)(CodedBitstreamContext *ctx,
2019
                                                          RWContext *rw,
2020
                                                          H265RawSEIAlternativeTransferCharacteristics *current)
2021
{
2022
    int err;
2023
2024
    HEADER("Alternative Transfer Characteristics");
2025
2026
    ub(8, preferred_transfer_characteristics);
2027
2028
    return 0;
2029
}
2030
2031
static int FUNC(sei_alpha_channel_info)(CodedBitstreamContext *ctx,
2032
                                        RWContext *rw,
2033
                                        H265RawSEIAlphaChannelInfo *current)
2034
{
2035
    int err, length;
2036
2037
    HEADER("Alpha Channel Information");
2038
2039
    flag(alpha_channel_cancel_flag);
2040
    if (!current->alpha_channel_cancel_flag) {
2041
        ub(3, alpha_channel_use_idc);
2042
        ub(3, alpha_channel_bit_depth_minus8);
2043
        length = current->alpha_channel_bit_depth_minus8 + 9;
2044
        ub(length, alpha_transparent_value);
2045
        ub(length, alpha_opaque_value);
2046
        flag(alpha_channel_incr_flag);
2047
        flag(alpha_channel_clip_flag);
2048
        if (current->alpha_channel_clip_flag)
2049
            flag(alpha_channel_clip_type_flag);
2050
    } else {
2051
       infer(alpha_channel_use_idc,   2);
2052
       infer(alpha_channel_incr_flag, 0);
2053
       infer(alpha_channel_clip_flag, 0);
2054
    }
2055
2056
    return 0;
2057
}
2058
2059
1535
static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
2060
                             H265RawSEIPayload *current, int prefix)
2061
{
2062
    int err, i;
2063
    int start_position, end_position;
2064
2065
#ifdef READ
2066
1535
    start_position = get_bits_count(rw);
2067
#else
2068
1535
    start_position = put_bits_count(rw);
2069
#endif
2070
2071



1535
    switch (current->payload_type) {
2072
#define SEI_TYPE_CHECK_VALID(name, prefix_valid, suffix_valid) do { \
2073
            if (prefix && !prefix_valid) { \
2074
                av_log(ctx->log_ctx, AV_LOG_ERROR, "SEI type %s invalid " \
2075
                       "as prefix SEI!\n", #name); \
2076
                return AVERROR_INVALIDDATA; \
2077
            } \
2078
            if (!prefix && !suffix_valid) { \
2079
                av_log(ctx->log_ctx, AV_LOG_ERROR, "SEI type %s invalid " \
2080
                       "as suffix SEI!\n", #name); \
2081
                return AVERROR_INVALIDDATA; \
2082
            } \
2083
        } while (0)
2084
#define SEI_TYPE_N(type, prefix_valid, suffix_valid, name) \
2085
    case HEVC_SEI_TYPE_ ## type: \
2086
        SEI_TYPE_CHECK_VALID(name, prefix_valid, suffix_valid); \
2087
        CHECK(FUNC(sei_ ## name)(ctx, rw, &current->payload.name)); \
2088
        break
2089
#define SEI_TYPE_S(type, prefix_valid, suffix_valid, name) \
2090
    case HEVC_SEI_TYPE_ ## type: \
2091
        SEI_TYPE_CHECK_VALID(name, prefix_valid, suffix_valid); \
2092
        CHECK(FUNC(sei_ ## name)(ctx, rw, &current->payload.name, \
2093
                                 &current->payload_size)); \
2094
        break
2095
2096

5
        SEI_TYPE_S(BUFFERING_PERIOD,         1, 0, buffering_period);
2097

156
        SEI_TYPE_N(PICTURE_TIMING,           1, 0, pic_timing);
2098
        SEI_TYPE_N(PAN_SCAN_RECT,            1, 0, pan_scan_rect);
2099
        SEI_TYPE_S(USER_DATA_REGISTERED_ITU_T_T35,
2100
                                             1, 1, user_data_registered);
2101
        SEI_TYPE_S(USER_DATA_UNREGISTERED,   1, 1, user_data_unregistered);
2102

5
        SEI_TYPE_N(RECOVERY_POINT,           1, 0, recovery_point);
2103
        SEI_TYPE_N(DISPLAY_ORIENTATION,      1, 0, display_orientation);
2104

3
        SEI_TYPE_N(ACTIVE_PARAMETER_SETS,    1, 0, active_parameter_sets);
2105

1366
        SEI_TYPE_N(DECODED_PICTURE_HASH,     0, 1, decoded_picture_hash);
2106
        SEI_TYPE_N(TIME_CODE,                1, 0, time_code);
2107
        SEI_TYPE_N(MASTERING_DISPLAY_INFO,   1, 0, mastering_display);
2108
        SEI_TYPE_N(CONTENT_LIGHT_LEVEL_INFO, 1, 0, content_light_level);
2109
        SEI_TYPE_N(ALTERNATIVE_TRANSFER_CHARACTERISTICS,
2110
                                             1, 0, alternative_transfer_characteristics);
2111
        SEI_TYPE_N(ALPHA_CHANNEL_INFO,       1, 0, alpha_channel_info);
2112
2113
#undef SEI_TYPE
2114
    default:
2115
        {
2116
#ifdef READ
2117
            current->payload.other.data_length = current->payload_size;
2118
#endif
2119
            allocate(current->payload.other.data, current->payload.other.data_length);
2120
2121
            for (i = 0; i < current->payload_size; i++)
2122
                xu(8, payload_byte[i], current->payload.other.data[i], 0, 255,
2123
                   1, i);
2124
        }
2125
    }
2126
2127
1535
    if (byte_alignment(rw)) {
2128
161
        fixed(1, bit_equal_to_one, 1);
2129
921
        while (byte_alignment(rw))
2130
760
            fixed(1, bit_equal_to_zero, 0);
2131
    }
2132
2133
#ifdef READ
2134
1535
    end_position = get_bits_count(rw);
2135
1535
    if (end_position < start_position + 8 * current->payload_size) {
2136
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Incorrect SEI payload length: "
2137
               "header %"PRIu32" bits, actually %d bits.\n",
2138
               8 * current->payload_size,
2139
               end_position - start_position);
2140
        return AVERROR_INVALIDDATA;
2141
    }
2142
#else
2143
1535
    end_position = put_bits_count(rw);
2144
1535
    current->payload_size = (end_position - start_position) >> 3;
2145
#endif
2146
2147
1535
    return 0;
2148
}
2149
2150
3070
static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
2151
                     H265RawSEI *current, int prefix)
2152
{
2153
    int err, k;
2154
2155
3070
    if (prefix)
2156
338
        HEADER("Prefix Supplemental Enhancement Information");
2157
    else
2158
2732
        HEADER("Suffix Supplemental Enhancement Information");
2159
2160
3070
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
2161
                                prefix ? HEVC_NAL_SEI_PREFIX
2162
                                       : HEVC_NAL_SEI_SUFFIX));
2163
2164
#ifdef READ
2165
1535
    for (k = 0; k < H265_MAX_SEI_PAYLOADS; k++) {
2166
1535
        uint32_t payload_type = 0;
2167
1535
        uint32_t payload_size = 0;
2168
        uint32_t tmp;
2169
2170
1535
        while (show_bits(rw, 8) == 0xff) {
2171
            fixed(8, ff_byte, 0xff);
2172
            payload_type += 255;
2173
        }
2174
1535
        xu(8, last_payload_type_byte, tmp, 0, 254, 0);
2175
1535
        payload_type += tmp;
2176
2177
1535
        while (show_bits(rw, 8) == 0xff) {
2178
            fixed(8, ff_byte, 0xff);
2179
            payload_size += 255;
2180
        }
2181
1535
        xu(8, last_payload_size_byte, tmp, 0, 254, 0);
2182
1535
        payload_size += tmp;
2183
2184
1535
        current->payload[k].payload_type = payload_type;
2185
1535
        current->payload[k].payload_size = payload_size;
2186
2187
1535
        CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k], prefix));
2188
2189
1535
        if (!cbs_h2645_read_more_rbsp_data(rw))
2190
1535
            break;
2191
    }
2192
1535
    if (k >= H265_MAX_SEI_PAYLOADS) {
2193
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
2194
               "SEI message: found %d.\n", k);
2195
        return AVERROR_INVALIDDATA;
2196
    }
2197
1535
    current->payload_count = k + 1;
2198
#else
2199
3070
    for (k = 0; k < current->payload_count; k++) {
2200
        PutBitContext start_state;
2201
        uint32_t tmp;
2202
        int need_size, i;
2203
2204
        // Somewhat clumsy: we write the payload twice when
2205
        // we don't know the size in advance.  This will mess
2206
        // with trace output, but is otherwise harmless.
2207
1535
        start_state = *rw;
2208
1535
        need_size = !current->payload[k].payload_size;
2209
3070
        for (i = 0; i < 1 + need_size; i++) {
2210
1535
            *rw = start_state;
2211
2212
1535
            tmp = current->payload[k].payload_type;
2213
1535
            while (tmp >= 255) {
2214
                fixed(8, ff_byte, 0xff);
2215
                tmp -= 255;
2216
            }
2217
1535
            xu(8, last_payload_type_byte, tmp, 0, 254, 0);
2218
2219
1535
            tmp = current->payload[k].payload_size;
2220
1535
            while (tmp >= 255) {
2221
                fixed(8, ff_byte, 0xff);
2222
                tmp -= 255;
2223
            }
2224
1535
            xu(8, last_payload_size_byte, tmp, 0, 254, 0);
2225
2226
1535
            CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k], prefix));
2227
        }
2228
    }
2229
#endif
2230
2231
3070
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
2232
2233
3070
    return 0;
2234
}