GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_h264_syntax_template.c Lines: 533 773 69.0 %
Date: 2019-11-22 03:34:36 Branches: 497 1158 42.9 %

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
1700
static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
20
{
21
    int err;
22
23
1700
    fixed(1, rbsp_stop_one_bit, 1);
24
8885
    while (byte_alignment(rw) != 0)
25
7185
        fixed(1, rbsp_alignment_zero_bit, 0);
26
27
1700
    return 0;
28
}
29
30
6616
static int FUNC(nal_unit_header)(CodedBitstreamContext *ctx, RWContext *rw,
31
                                 H264RawNALUnitHeader *current,
32
                                 uint32_t valid_type_mask)
33
{
34
    int err;
35
36
6616
    fixed(1, forbidden_zero_bit, 0);
37
6616
    ub(2, nal_ref_idc);
38
6616
    ub(5, nal_unit_type);
39
40
6616
    if (!(1 << current->nal_unit_type & valid_type_mask)) {
41
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid NAL unit type %d.\n",
42
               current->nal_unit_type);
43
        return AVERROR_INVALIDDATA;
44
    }
45
46
6616
    if (current->nal_unit_type == 14 ||
47
6616
        current->nal_unit_type == 20 ||
48
6616
        current->nal_unit_type == 21) {
49
        if (current->nal_unit_type != 21)
50
            flag(svc_extension_flag);
51
        else
52
            flag(avc_3d_extension_flag);
53
54
        if (current->svc_extension_flag) {
55
            av_log(ctx->log_ctx, AV_LOG_ERROR, "SVC not supported.\n");
56
            return AVERROR_PATCHWELCOME;
57
58
        } else if (current->avc_3d_extension_flag) {
59
            av_log(ctx->log_ctx, AV_LOG_ERROR, "3DAVC not supported.\n");
60
            return AVERROR_PATCHWELCOME;
61
62
        } else {
63
            av_log(ctx->log_ctx, AV_LOG_ERROR, "MVC not supported.\n");
64
            return AVERROR_PATCHWELCOME;
65
        }
66
    }
67
68
6616
    return 0;
69
}
70
71
static int FUNC(scaling_list)(CodedBitstreamContext *ctx, RWContext *rw,
72
                              H264RawScalingList *current,
73
                              int size_of_scaling_list)
74
{
75
    int err, i, scale;
76
77
    scale = 8;
78
    for (i = 0; i < size_of_scaling_list; i++) {
79
        ses(delta_scale[i], -128, +127, 1, i);
80
        scale = (scale + current->delta_scale[i] + 256) % 256;
81
        if (scale == 0)
82
            break;
83
    }
84
85
    return 0;
86
}
87
88
6
static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
89
                                H264RawHRD *current)
90
{
91
    int err, i;
92
93
6
    ue(cpb_cnt_minus1, 0, 31);
94
6
    ub(4, bit_rate_scale);
95
6
    ub(4, cpb_size_scale);
96
97
12
    for (i = 0; i <= current->cpb_cnt_minus1; i++) {
98
6
        ues(bit_rate_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
99
6
        ues(cpb_size_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
100
6
        flags(cbr_flag[i], 1, i);
101
    }
102
103
6
    ub(5, initial_cpb_removal_delay_length_minus1);
104
6
    ub(5, cpb_removal_delay_length_minus1);
105
6
    ub(5, dpb_output_delay_length_minus1);
106
6
    ub(5, time_offset_length);
107
108
6
    return 0;
109
}
110
111
4
static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
112
                                H264RawVUI *current, H264RawSPS *sps)
113
{
114
    int err;
115
116
4
    flag(aspect_ratio_info_present_flag);
117
4
    if (current->aspect_ratio_info_present_flag) {
118
        ub(8, aspect_ratio_idc);
119
        if (current->aspect_ratio_idc == 255) {
120
            ub(16, sar_width);
121
            ub(16, sar_height);
122
        }
123
    } else {
124
4
        infer(aspect_ratio_idc, 0);
125
    }
126
127
4
    flag(overscan_info_present_flag);
128
4
    if (current->overscan_info_present_flag)
129
        flag(overscan_appropriate_flag);
130
131
4
    flag(video_signal_type_present_flag);
132
4
    if (current->video_signal_type_present_flag) {
133
        ub(3, video_format);
134
        flag(video_full_range_flag);
135
        flag(colour_description_present_flag);
136
        if (current->colour_description_present_flag) {
137
            ub(8, colour_primaries);
138
            ub(8, transfer_characteristics);
139
            ub(8, matrix_coefficients);
140
        } else {
141
            infer(colour_primaries,         2);
142
            infer(transfer_characteristics, 2);
143
            infer(matrix_coefficients,      2);
144
        }
145
    } else {
146
4
        infer(video_format,             5);
147
4
        infer(video_full_range_flag,    0);
148
4
        infer(colour_primaries,         2);
149
4
        infer(transfer_characteristics, 2);
150
4
        infer(matrix_coefficients,      2);
151
    }
152
153
4
    flag(chroma_loc_info_present_flag);
154
4
    if (current->chroma_loc_info_present_flag) {
155
        ue(chroma_sample_loc_type_top_field,    0, 5);
156
        ue(chroma_sample_loc_type_bottom_field, 0, 5);
157
    } else {
158
4
        infer(chroma_sample_loc_type_top_field,    0);
159
4
        infer(chroma_sample_loc_type_bottom_field, 0);
160
    }
161
162
4
    flag(timing_info_present_flag);
163
4
    if (current->timing_info_present_flag) {
164
4
        u(32, num_units_in_tick, 1, UINT32_MAX);
165
4
        u(32, time_scale,        1, UINT32_MAX);
166
4
        flag(fixed_frame_rate_flag);
167
    } else {
168
        infer(fixed_frame_rate_flag, 0);
169
    }
170
171
4
    flag(nal_hrd_parameters_present_flag);
172
4
    if (current->nal_hrd_parameters_present_flag)
173
4
        CHECK(FUNC(hrd_parameters)(ctx, rw, &current->nal_hrd_parameters));
174
175
4
    flag(vcl_hrd_parameters_present_flag);
176
4
    if (current->vcl_hrd_parameters_present_flag)
177
2
        CHECK(FUNC(hrd_parameters)(ctx, rw, &current->vcl_hrd_parameters));
178
179
4
    if (current->nal_hrd_parameters_present_flag ||
180
        current->vcl_hrd_parameters_present_flag)
181
4
        flag(low_delay_hrd_flag);
182
    else
183
        infer(low_delay_hrd_flag, 1 - current->fixed_frame_rate_flag);
184
185
4
    flag(pic_struct_present_flag);
186
187
4
    flag(bitstream_restriction_flag);
188
4
    if (current->bitstream_restriction_flag) {
189
4
        flag(motion_vectors_over_pic_boundaries_flag);
190
4
        ue(max_bytes_per_pic_denom, 0, 16);
191
4
        ue(max_bits_per_mb_denom,   0, 16);
192
        // The current version of the standard constrains this to be in
193
        // [0,15], but older versions allow 16.
194
4
        ue(log2_max_mv_length_horizontal, 0, 16);
195
4
        ue(log2_max_mv_length_vertical,   0, 16);
196
4
        ue(max_num_reorder_frames,  0, H264_MAX_DPB_FRAMES);
197
4
        ue(max_dec_frame_buffering, 0, H264_MAX_DPB_FRAMES);
198
    } else {
199
        infer(motion_vectors_over_pic_boundaries_flag, 1);
200
        infer(max_bytes_per_pic_denom, 2);
201
        infer(max_bits_per_mb_denom,   1);
202
        infer(log2_max_mv_length_horizontal, 15);
203
        infer(log2_max_mv_length_vertical,   15);
204
205
        if ((sps->profile_idc ==  44 || sps->profile_idc ==  86 ||
206
             sps->profile_idc == 100 || sps->profile_idc == 110 ||
207
             sps->profile_idc == 122 || sps->profile_idc == 244) &&
208
            sps->constraint_set3_flag) {
209
            infer(max_num_reorder_frames,  0);
210
            infer(max_dec_frame_buffering, 0);
211
        } else {
212
            infer(max_num_reorder_frames,  H264_MAX_DPB_FRAMES);
213
            infer(max_dec_frame_buffering, H264_MAX_DPB_FRAMES);
214
        }
215
    }
216
217
4
    return 0;
218
}
219
220
36
static int FUNC(vui_parameters_default)(CodedBitstreamContext *ctx,
221
                                        RWContext *rw, H264RawVUI *current,
222
                                        H264RawSPS *sps)
223
{
224
36
    infer(aspect_ratio_idc, 0);
225
226
36
    infer(video_format,             5);
227
36
    infer(video_full_range_flag,    0);
228
36
    infer(colour_primaries,         2);
229
36
    infer(transfer_characteristics, 2);
230
36
    infer(matrix_coefficients,      2);
231
232
36
    infer(chroma_sample_loc_type_top_field,    0);
233
36
    infer(chroma_sample_loc_type_bottom_field, 0);
234
235
36
    infer(fixed_frame_rate_flag, 0);
236
36
    infer(low_delay_hrd_flag,    1);
237
238
36
    infer(pic_struct_present_flag, 0);
239
240
36
    infer(motion_vectors_over_pic_boundaries_flag, 1);
241
36
    infer(max_bytes_per_pic_denom, 2);
242
36
    infer(max_bits_per_mb_denom,   1);
243
36
    infer(log2_max_mv_length_horizontal, 15);
244
36
    infer(log2_max_mv_length_vertical,   15);
245
246

36
    if ((sps->profile_idc ==  44 || sps->profile_idc ==  86 ||
247

36
         sps->profile_idc == 100 || sps->profile_idc == 110 ||
248

36
         sps->profile_idc == 122 || sps->profile_idc == 244) &&
249
        sps->constraint_set3_flag) {
250
        infer(max_num_reorder_frames,  0);
251
        infer(max_dec_frame_buffering, 0);
252
    } else {
253
36
        infer(max_num_reorder_frames,  H264_MAX_DPB_FRAMES);
254
36
        infer(max_dec_frame_buffering, H264_MAX_DPB_FRAMES);
255
    }
256
257
36
    return 0;
258
}
259
260
40
static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
261
                     H264RawSPS *current)
262
{
263
    int err, i;
264
265
40
    HEADER("Sequence Parameter Set");
266
267
40
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
268
                                1 << H264_NAL_SPS));
269
270
40
    ub(8, profile_idc);
271
272
40
    flag(constraint_set0_flag);
273
40
    flag(constraint_set1_flag);
274
40
    flag(constraint_set2_flag);
275
40
    flag(constraint_set3_flag);
276
40
    flag(constraint_set4_flag);
277
40
    flag(constraint_set5_flag);
278
279
40
    u(2, reserved_zero_2bits,  0, 0);
280
281
40
    ub(8, level_idc);
282
283
40
    ue(seq_parameter_set_id, 0, 31);
284
285

40
    if (current->profile_idc == 100 || current->profile_idc == 110 ||
286

40
        current->profile_idc == 122 || current->profile_idc == 244 ||
287

40
        current->profile_idc ==  44 || current->profile_idc ==  83 ||
288

40
        current->profile_idc ==  86 || current->profile_idc == 118 ||
289

40
        current->profile_idc == 128 || current->profile_idc == 138) {
290
        ue(chroma_format_idc, 0, 3);
291
292
        if (current->chroma_format_idc == 3)
293
            flag(separate_colour_plane_flag);
294
        else
295
            infer(separate_colour_plane_flag, 0);
296
297
        ue(bit_depth_luma_minus8,   0, 6);
298
        ue(bit_depth_chroma_minus8, 0, 6);
299
300
        flag(qpprime_y_zero_transform_bypass_flag);
301
302
        flag(seq_scaling_matrix_present_flag);
303
        if (current->seq_scaling_matrix_present_flag) {
304
            for (i = 0; i < ((current->chroma_format_idc != 3) ? 8 : 12); i++) {
305
                flags(seq_scaling_list_present_flag[i], 1, i);
306
                if (current->seq_scaling_list_present_flag[i]) {
307
                    if (i < 6)
308
                        CHECK(FUNC(scaling_list)(ctx, rw,
309
                                                 &current->scaling_list_4x4[i],
310
                                                 16));
311
                    else
312
                        CHECK(FUNC(scaling_list)(ctx, rw,
313
                                                 &current->scaling_list_8x8[i - 6],
314
                                                 64));
315
                }
316
            }
317
        }
318
    } else {
319
40
        infer(chroma_format_idc, current->profile_idc == 183 ? 0 : 1);
320
321
40
        infer(separate_colour_plane_flag, 0);
322
40
        infer(bit_depth_luma_minus8,      0);
323
40
        infer(bit_depth_chroma_minus8,    0);
324
    }
325
326
40
    ue(log2_max_frame_num_minus4, 0, 12);
327
40
    ue(pic_order_cnt_type, 0, 2);
328
329
40
    if (current->pic_order_cnt_type == 0) {
330
23
        ue(log2_max_pic_order_cnt_lsb_minus4, 0, 12);
331
17
    } else if (current->pic_order_cnt_type == 1) {
332
8
        flag(delta_pic_order_always_zero_flag);
333
8
        se(offset_for_non_ref_pic,         INT32_MIN + 1, INT32_MAX);
334
8
        se(offset_for_top_to_bottom_field, INT32_MIN + 1, INT32_MAX);
335
8
        ue(num_ref_frames_in_pic_order_cnt_cycle, 0, 255);
336
337
16
        for (i = 0; i < current->num_ref_frames_in_pic_order_cnt_cycle; i++)
338
8
            ses(offset_for_ref_frame[i], INT32_MIN + 1, INT32_MAX, 1, i);
339
    }
340
341
40
    ue(max_num_ref_frames, 0, H264_MAX_DPB_FRAMES);
342
40
    flag(gaps_in_frame_num_allowed_flag);
343
344
40
    ue(pic_width_in_mbs_minus1,        0, H264_MAX_MB_WIDTH);
345
40
    ue(pic_height_in_map_units_minus1, 0, H264_MAX_MB_HEIGHT);
346
347
40
    flag(frame_mbs_only_flag);
348
40
    if (!current->frame_mbs_only_flag)
349
11
        flag(mb_adaptive_frame_field_flag);
350
351
40
    flag(direct_8x8_inference_flag);
352
353
40
    flag(frame_cropping_flag);
354
40
    if (current->frame_cropping_flag) {
355
2
        ue(frame_crop_left_offset,   0, H264_MAX_WIDTH);
356
2
        ue(frame_crop_right_offset,  0, H264_MAX_WIDTH);
357
2
        ue(frame_crop_top_offset,    0, H264_MAX_HEIGHT);
358
2
        ue(frame_crop_bottom_offset, 0, H264_MAX_HEIGHT);
359
    }
360
361
40
    flag(vui_parameters_present_flag);
362
40
    if (current->vui_parameters_present_flag)
363
4
        CHECK(FUNC(vui_parameters)(ctx, rw, &current->vui, current));
364
    else
365
36
        CHECK(FUNC(vui_parameters_default)(ctx, rw, &current->vui, current));
366
367
40
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
368
369
40
    return 0;
370
}
371
372
static int FUNC(sps_extension)(CodedBitstreamContext *ctx, RWContext *rw,
373
                               H264RawSPSExtension *current)
374
{
375
    int err;
376
377
    HEADER("Sequence Parameter Set Extension");
378
379
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
380
                                1 << H264_NAL_SPS_EXT));
381
382
    ue(seq_parameter_set_id, 0, 31);
383
384
    ue(aux_format_idc, 0, 3);
385
386
    if (current->aux_format_idc != 0) {
387
        int bits;
388
389
        ue(bit_depth_aux_minus8, 0, 4);
390
        flag(alpha_incr_flag);
391
392
        bits = current->bit_depth_aux_minus8 + 9;
393
        ub(bits, alpha_opaque_value);
394
        ub(bits, alpha_transparent_value);
395
    }
396
397
    flag(additional_extension_flag);
398
399
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
400
401
    return 0;
402
}
403
404
1182
static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
405
                     H264RawPPS *current)
406
{
407
1182
    CodedBitstreamH264Context *h264 = ctx->priv_data;
408
    const H264RawSPS *sps;
409
    int err, i;
410
411
1182
    HEADER("Picture Parameter Set");
412
413
1182
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
414
                                1 << H264_NAL_PPS));
415
416
1182
    ue(pic_parameter_set_id, 0, 255);
417
1182
    ue(seq_parameter_set_id, 0, 31);
418
419
1182
    sps = h264->sps[current->seq_parameter_set_id];
420
1182
    if (!sps) {
421
        av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
422
               current->seq_parameter_set_id);
423
        return AVERROR_INVALIDDATA;
424
    }
425
426
1182
    flag(entropy_coding_mode_flag);
427
1182
    flag(bottom_field_pic_order_in_frame_present_flag);
428
429
1182
    ue(num_slice_groups_minus1, 0, 7);
430
1182
    if (current->num_slice_groups_minus1 > 0) {
431
        unsigned int pic_size;
432
        int iGroup;
433
434
14
        pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
435
14
                   (sps->pic_height_in_map_units_minus1 + 1);
436
437
14
        ue(slice_group_map_type, 0, 6);
438
439
14
        if (current->slice_group_map_type == 0) {
440
18
            for (iGroup = 0; iGroup <= current->num_slice_groups_minus1; iGroup++)
441
16
                ues(run_length_minus1[iGroup], 0, pic_size - 1, 1, iGroup);
442
443
12
        } else if (current->slice_group_map_type == 2) {
444
6
            for (iGroup = 0; iGroup < current->num_slice_groups_minus1; iGroup++) {
445
4
                ues(top_left[iGroup],       0, pic_size - 1, 1, iGroup);
446
4
                ues(bottom_right[iGroup],
447
                    current->top_left[iGroup], pic_size - 1, 1, iGroup);
448
            }
449
10
        } else if (current->slice_group_map_type == 3 ||
450
8
                   current->slice_group_map_type == 4 ||
451
6
                   current->slice_group_map_type == 5) {
452
6
            flag(slice_group_change_direction_flag);
453
6
            ue(slice_group_change_rate_minus1, 0, pic_size - 1);
454
4
        } else if (current->slice_group_map_type == 6) {
455
2
            ue(pic_size_in_map_units_minus1, pic_size - 1, pic_size - 1);
456
457
2
            allocate(current->slice_group_id,
458
                     current->pic_size_in_map_units_minus1 + 1);
459
200
            for (i = 0; i <= current->pic_size_in_map_units_minus1; i++)
460
198
                us(av_log2(2 * current->num_slice_groups_minus1 + 1),
461
                   slice_group_id[i], 0, current->num_slice_groups_minus1, 1, i);
462
        }
463
    }
464
465
1182
    ue(num_ref_idx_l0_default_active_minus1, 0, 31);
466
1182
    ue(num_ref_idx_l1_default_active_minus1, 0, 31);
467
468
1182
    flag(weighted_pred_flag);
469
1182
    u(2, weighted_bipred_idc, 0, 2);
470
471
1182
    se(pic_init_qp_minus26, -26 - 6 * sps->bit_depth_luma_minus8, +25);
472
1182
    se(pic_init_qs_minus26, -26, +25);
473
1182
    se(chroma_qp_index_offset, -12, +12);
474
475
1182
    flag(deblocking_filter_control_present_flag);
476
1182
    flag(constrained_intra_pred_flag);
477
1182
    flag(redundant_pic_cnt_present_flag);
478
479
1182
    if (more_rbsp_data(current->more_rbsp_data))
480
    {
481
        flag(transform_8x8_mode_flag);
482
483
        flag(pic_scaling_matrix_present_flag);
484
        if (current->pic_scaling_matrix_present_flag) {
485
            for (i = 0; i < 6 + (((sps->chroma_format_idc != 3) ? 2 : 6) *
486
                                 current->transform_8x8_mode_flag); i++) {
487
                flags(pic_scaling_list_present_flag[i], 1, i);
488
                if (current->pic_scaling_list_present_flag[i]) {
489
                    if (i < 6)
490
                        CHECK(FUNC(scaling_list)(ctx, rw,
491
                                                 &current->scaling_list_4x4[i],
492
                                                 16));
493
                    else
494
                        CHECK(FUNC(scaling_list)(ctx, rw,
495
                                                 &current->scaling_list_8x8[i - 6],
496
                                                 64));
497
                }
498
            }
499
        }
500
501
        se(second_chroma_qp_index_offset, -12, +12);
502
    } else {
503
1182
        infer(transform_8x8_mode_flag, 0);
504
1182
        infer(pic_scaling_matrix_present_flag, 0);
505
1182
        infer(second_chroma_qp_index_offset, current->chroma_qp_index_offset);
506
    }
507
508
1182
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
509
510
1182
    return 0;
511
}
512
513
2
static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
514
                                      H264RawSEIBufferingPeriod *current)
515
{
516
2
    CodedBitstreamH264Context *h264 = ctx->priv_data;
517
    const H264RawSPS *sps;
518
    int err, i, length;
519
520
2
    HEADER("Buffering Period");
521
522
2
    ue(seq_parameter_set_id, 0, 31);
523
524
2
    sps = h264->sps[current->seq_parameter_set_id];
525
2
    if (!sps) {
526
        av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
527
               current->seq_parameter_set_id);
528
        return AVERROR_INVALIDDATA;
529
    }
530
2
    h264->active_sps = sps;
531
532
2
    if (sps->vui.nal_hrd_parameters_present_flag) {
533
4
        for (i = 0; i <= sps->vui.nal_hrd_parameters.cpb_cnt_minus1; i++) {
534
2
            length = sps->vui.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
535
2
            xu(length, initial_cpb_removal_delay[SchedSelIdx],
536
               current->nal.initial_cpb_removal_delay[i],
537
               1, MAX_UINT_BITS(length), 1, i);
538
2
            xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
539
               current->nal.initial_cpb_removal_delay_offset[i],
540
               0, MAX_UINT_BITS(length), 1, i);
541
        }
542
    }
543
544
2
    if (sps->vui.vcl_hrd_parameters_present_flag) {
545
2
        for (i = 0; i <= sps->vui.vcl_hrd_parameters.cpb_cnt_minus1; i++) {
546
1
            length = sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
547
1
            xu(length, initial_cpb_removal_delay[SchedSelIdx],
548
               current->vcl.initial_cpb_removal_delay[i],
549
               1, MAX_UINT_BITS(length), 1, i);
550
1
            xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
551
               current->vcl.initial_cpb_removal_delay_offset[i],
552
               0, MAX_UINT_BITS(length), 1, i);
553
        }
554
    }
555
556
2
    return 0;
557
}
558
559
static int FUNC(sei_pic_timestamp)(CodedBitstreamContext *ctx, RWContext *rw,
560
                                   H264RawSEIPicTimestamp *current,
561
                                   const H264RawSPS *sps)
562
{
563
    uint8_t time_offset_length;
564
    int err;
565
566
    u(2, ct_type, 0, 2);
567
    flag(nuit_field_based_flag);
568
    u(5, counting_type, 0, 6);
569
    flag(full_timestamp_flag);
570
    flag(discontinuity_flag);
571
    flag(cnt_dropped_flag);
572
    ub(8, n_frames);
573
    if (current->full_timestamp_flag) {
574
            u(6, seconds_value, 0, 59);
575
            u(6, minutes_value, 0, 59);
576
            u(5, hours_value,   0, 23);
577
    } else {
578
        flag(seconds_flag);
579
        if (current->seconds_flag) {
580
            u(6, seconds_value, 0, 59);
581
            flag(minutes_flag);
582
            if (current->minutes_flag) {
583
                u(6, minutes_value, 0, 59);
584
                flag(hours_flag);
585
                if (current->hours_flag)
586
                    u(5, hours_value, 0, 23);
587
            }
588
        }
589
    }
590
591
    if (sps->vui.nal_hrd_parameters_present_flag)
592
        time_offset_length = sps->vui.nal_hrd_parameters.time_offset_length;
593
    else if (sps->vui.vcl_hrd_parameters_present_flag)
594
        time_offset_length = sps->vui.vcl_hrd_parameters.time_offset_length;
595
    else
596
        time_offset_length = 24;
597
598
    if (time_offset_length > 0)
599
        ib(time_offset_length, time_offset);
600
    else
601
        infer(time_offset, 0);
602
603
    return 0;
604
}
605
606
283
static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
607
                                H264RawSEIPicTiming *current)
608
{
609
283
    CodedBitstreamH264Context *h264 = ctx->priv_data;
610
    const H264RawSPS *sps;
611
    int err;
612
613
283
    HEADER("Picture Timing");
614
615
283
    sps = h264->active_sps;
616
283
    if (!sps) {
617
        // If there is exactly one possible SPS but it is not yet active
618
        // then just assume that it should be the active one.
619
        int i, k = -1;
620
        for (i = 0; i < H264_MAX_SPS_COUNT; i++) {
621
            if (h264->sps[i]) {
622
                if (k >= 0) {
623
                    k = -1;
624
                    break;
625
                }
626
                k = i;
627
            }
628
        }
629
        if (k >= 0)
630
            sps = h264->sps[k];
631
    }
632
283
    if (!sps) {
633
        av_log(ctx->log_ctx, AV_LOG_ERROR,
634
               "No active SPS for pic_timing.\n");
635
        return AVERROR_INVALIDDATA;
636
    }
637
638
283
    if (sps->vui.nal_hrd_parameters_present_flag ||
639
        sps->vui.vcl_hrd_parameters_present_flag) {
640
        const H264RawHRD *hrd;
641
642
283
        if (sps->vui.nal_hrd_parameters_present_flag)
643
283
            hrd = &sps->vui.nal_hrd_parameters;
644
        else if (sps->vui.vcl_hrd_parameters_present_flag)
645
            hrd = &sps->vui.vcl_hrd_parameters;
646
        else {
647
            av_log(ctx->log_ctx, AV_LOG_ERROR,
648
                   "No HRD parameters for pic_timing.\n");
649
            return AVERROR_INVALIDDATA;
650
        }
651
652
283
        ub(hrd->cpb_removal_delay_length_minus1 + 1, cpb_removal_delay);
653
283
        ub(hrd->dpb_output_delay_length_minus1 + 1, dpb_output_delay);
654
    }
655
656
283
    if (sps->vui.pic_struct_present_flag) {
657
        static const uint8_t num_clock_ts[9] = {
658
            1, 1, 1, 2, 2, 3, 3, 2, 3
659
        };
660
        int i;
661
662
278
        u(4, pic_struct, 0, 8);
663
278
        if (current->pic_struct > 8)
664
            return AVERROR_INVALIDDATA;
665
666
972
        for (i = 0; i < num_clock_ts[current->pic_struct]; i++) {
667
694
            flags(clock_timestamp_flag[i], 1, i);
668
694
            if (current->clock_timestamp_flag[i])
669
                CHECK(FUNC(sei_pic_timestamp)(ctx, rw,
670
                                              &current->timestamp[i], sps));
671
        }
672
    }
673
674
283
    return 0;
675
}
676
677
5
static int FUNC(sei_pan_scan_rect)(CodedBitstreamContext *ctx, RWContext *rw,
678
                                   H264RawSEIPanScanRect *current)
679
{
680
    int err, i;
681
682
5
    HEADER("Pan-Scan Rectangle");
683
684
5
    ue(pan_scan_rect_id, 0, UINT32_MAX - 1);
685
5
    flag(pan_scan_rect_cancel_flag);
686
687
5
    if (!current->pan_scan_rect_cancel_flag) {
688
5
        ue(pan_scan_cnt_minus1, 0, 2);
689
690
10
        for (i = 0; i <= current->pan_scan_cnt_minus1; i++) {
691
5
            ses(pan_scan_rect_left_offset[i],   INT32_MIN + 1, INT32_MAX, 1, i);
692
5
            ses(pan_scan_rect_right_offset[i],  INT32_MIN + 1, INT32_MAX, 1, i);
693
5
            ses(pan_scan_rect_top_offset[i],    INT32_MIN + 1, INT32_MAX, 1, i);
694
5
            ses(pan_scan_rect_bottom_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
695
        }
696
697
5
        ue(pan_scan_rect_repetition_period, 0, 16384);
698
    }
699
700
5
    return 0;
701
}
702
703
5
static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext *rw,
704
                                          H264RawSEIUserDataRegistered *current,
705
                                          uint32_t *payload_size)
706
{
707
    int err, i, j;
708
709
5
    HEADER("User Data Registered ITU-T T.35");
710
711
5
    u(8, itu_t_t35_country_code, 0x00, 0xff);
712
5
    if (current->itu_t_t35_country_code != 0xff)
713
5
        i = 1;
714
    else {
715
        u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
716
        i = 2;
717
    }
718
719
#ifdef READ
720
5
    if (*payload_size < i) {
721
        av_log(ctx->log_ctx, AV_LOG_ERROR,
722
               "Invalid SEI user data registered payload.\n");
723
        return AVERROR_INVALIDDATA;
724
    }
725
5
    current->data_length = *payload_size - i;
726
#else
727
5
    *payload_size = i + current->data_length;
728
#endif
729
730
5
    allocate(current->data, current->data_length);
731
88
    for (j = 0; j < current->data_length; j++)
732
83
        xu(8, itu_t_t35_payload_byte[i], current->data[j], 0x00, 0xff, 1, i + j);
733
734
5
    return 0;
735
}
736
737
static int FUNC(sei_user_data_unregistered)(CodedBitstreamContext *ctx, RWContext *rw,
738
                                            H264RawSEIUserDataUnregistered *current,
739
                                            uint32_t *payload_size)
740
{
741
    int err, i;
742
743
    HEADER("User Data Unregistered");
744
745
#ifdef READ
746
    if (*payload_size < 16) {
747
        av_log(ctx->log_ctx, AV_LOG_ERROR,
748
               "Invalid SEI user data unregistered payload.\n");
749
        return AVERROR_INVALIDDATA;
750
    }
751
    current->data_length = *payload_size - 16;
752
#else
753
    *payload_size = 16 + current->data_length;
754
#endif
755
756
    for (i = 0; i < 16; i++)
757
        us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
758
759
    allocate(current->data, current->data_length);
760
761
    for (i = 0; i < current->data_length; i++)
762
        xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
763
764
    return 0;
765
}
766
767
1
static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
768
                                    H264RawSEIRecoveryPoint *current)
769
{
770
    int err;
771
772
1
    HEADER("Recovery Point");
773
774
1
    ue(recovery_frame_cnt, 0, 65535);
775
1
    flag(exact_match_flag);
776
1
    flag(broken_link_flag);
777
1
    u(2, changing_slice_group_idc, 0, 2);
778
779
1
    return 0;
780
}
781
782
1
static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
783
                                         H264RawSEIDisplayOrientation *current)
784
{
785
    int err;
786
787
1
    HEADER("Display Orientation");
788
789
1
    flag(display_orientation_cancel_flag);
790
1
    if (!current->display_orientation_cancel_flag) {
791
1
        flag(hor_flip);
792
1
        flag(ver_flip);
793
1
        ub(16, anticlockwise_rotation);
794
1
        ue(display_orientation_repetition_period, 0, 16384);
795
1
        flag(display_orientation_extension_flag);
796
    }
797
798
1
    return 0;
799
}
800
801
1
static int FUNC(sei_mastering_display_colour_volume)(CodedBitstreamContext *ctx, RWContext *rw,
802
                                                     H264RawSEIMasteringDisplayColourVolume *current)
803
{
804
    int err, c;
805
806
1
    HEADER("Mastering Display Colour Volume");
807
808
4
    for (c = 0; c < 3; c++) {
809
3
        us(16, display_primaries_x[c], 0, 50000, 1, c);
810
3
        us(16, display_primaries_y[c], 0, 50000, 1, c);
811
    }
812
813
1
    u(16, white_point_x, 0, 50000);
814
1
    u(16, white_point_y, 0, 50000);
815
816
1
    u(32, max_display_mastering_luminance, 1, MAX_UINT_BITS(32));
817
1
    u(32, min_display_mastering_luminance, 0, current->max_display_mastering_luminance - 1);
818
819
1
    return 0;
820
}
821
822
static int FUNC(sei_alternative_transfer_characteristics)(CodedBitstreamContext *ctx,
823
                                                          RWContext *rw,
824
                                                          H264RawSEIAlternativeTransferCharacteristics *current)
825
{
826
    int err;
827
828
    HEADER("Alternative Transfer Characteristics");
829
830
    ub(8, preferred_transfer_characteristics);
831
832
    return 0;
833
}
834
835
303
static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
836
                             H264RawSEIPayload *current)
837
{
838
    int err, i;
839
    int start_position, end_position;
840
841
#ifdef READ
842
303
    start_position = get_bits_count(rw);
843
#else
844
304
    start_position = put_bits_count(rw);
845
#endif
846
847


303
    switch (current->payload_type) {
848
2
    case H264_SEI_TYPE_BUFFERING_PERIOD:
849
2
        CHECK(FUNC(sei_buffering_period)
850
              (ctx, rw, &current->payload.buffering_period));
851
2
        break;
852
283
    case H264_SEI_TYPE_PIC_TIMING:
853
283
        CHECK(FUNC(sei_pic_timing)
854
              (ctx, rw, &current->payload.pic_timing));
855
283
        break;
856
5
    case H264_SEI_TYPE_PAN_SCAN_RECT:
857
5
        CHECK(FUNC(sei_pan_scan_rect)
858
              (ctx, rw, &current->payload.pan_scan_rect));
859
5
        break;
860
    case H264_SEI_TYPE_FILLER_PAYLOAD:
861
        {
862
            for (i = 0; i  < current->payload_size; i++)
863
                fixed(8, ff_byte, 0xff);
864
        }
865
        break;
866
5
    case H264_SEI_TYPE_USER_DATA_REGISTERED:
867
5
        CHECK(FUNC(sei_user_data_registered)
868
              (ctx, rw, &current->payload.user_data_registered, &current->payload_size));
869
5
        break;
870
    case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
871
        CHECK(FUNC(sei_user_data_unregistered)
872
              (ctx, rw, &current->payload.user_data_unregistered, &current->payload_size));
873
        break;
874
1
    case H264_SEI_TYPE_RECOVERY_POINT:
875
1
        CHECK(FUNC(sei_recovery_point)
876
              (ctx, rw, &current->payload.recovery_point));
877
1
        break;
878
1
    case H264_SEI_TYPE_DISPLAY_ORIENTATION:
879
1
        CHECK(FUNC(sei_display_orientation)
880
              (ctx, rw, &current->payload.display_orientation));
881
1
        break;
882
1
    case H264_SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
883
1
        CHECK(FUNC(sei_mastering_display_colour_volume)
884
              (ctx, rw, &current->payload.mastering_display_colour_volume));
885
1
        break;
886
    case H264_SEI_TYPE_ALTERNATIVE_TRANSFER:
887
        CHECK(FUNC(sei_alternative_transfer_characteristics)
888
              (ctx, rw, &current->payload.alternative_transfer_characteristics));
889
        break;
890
5
    default:
891
        {
892
#ifdef READ
893
5
            current->payload.other.data_length = current->payload_size;
894
#endif
895
5
            allocate(current->payload.other.data, current->payload.other.data_length);
896
1749
            for (i = 0; i < current->payload.other.data_length; i++)
897
1744
                xu(8, payload_byte[i], current->payload.other.data[i], 0, 255, 1, i);
898
        }
899
    }
900
901
303
    if (byte_alignment(rw)) {
902
282
        fixed(1, bit_equal_to_one, 1);
903
436
        while (byte_alignment(rw))
904
154
            fixed(1, bit_equal_to_zero, 0);
905
    }
906
907
#ifdef READ
908
303
    end_position = get_bits_count(rw);
909
303
    if (end_position < start_position + 8 * current->payload_size) {
910
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Incorrect SEI payload length: "
911
               "header %"PRIu32" bits, actually %d bits.\n",
912
               8 * current->payload_size,
913
               end_position - start_position);
914
        return AVERROR_INVALIDDATA;
915
    }
916
#else
917
304
    end_position = put_bits_count(rw);
918
304
    current->payload_size = (end_position - start_position) / 8;
919
#endif
920
921
303
    return 0;
922
}
923
924
283
static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
925
                     H264RawSEI *current)
926
{
927
    int err, k;
928
929
283
    HEADER("Supplemental Enhancement Information");
930
931
283
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
932
                                1 << H264_NAL_SEI));
933
934
#ifdef READ
935
303
    for (k = 0; k < H264_MAX_SEI_PAYLOADS; k++) {
936
303
        uint32_t payload_type = 0;
937
303
        uint32_t payload_size = 0;
938
        uint32_t tmp;
939
940
323
        while (show_bits(rw, 8) == 0xff) {
941
20
            fixed(8, ff_byte, 0xff);
942
20
            payload_type += 255;
943
        }
944
303
        xu(8, last_payload_type_byte, tmp, 0, 254, 0);
945
303
        payload_type += tmp;
946
947
308
        while (show_bits(rw, 8) == 0xff) {
948
5
            fixed(8, ff_byte, 0xff);
949
5
            payload_size += 255;
950
        }
951
303
        xu(8, last_payload_size_byte, tmp, 0, 254, 0);
952
303
        payload_size += tmp;
953
954
303
        current->payload[k].payload_type = payload_type;
955
303
        current->payload[k].payload_size = payload_size;
956
957
303
        CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
958
959
303
        if (!cbs_h2645_read_more_rbsp_data(rw))
960
283
            break;
961
    }
962
283
    if (k >= H264_MAX_SEI_PAYLOADS) {
963
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
964
               "SEI message: found %d.\n", k);
965
        return AVERROR_INVALIDDATA;
966
    }
967
283
    current->payload_count = k + 1;
968
#else
969
586
    for (k = 0; k < current->payload_count; k++) {
970
        PutBitContext start_state;
971
        uint32_t tmp;
972
        int need_size, i;
973
974
        // Somewhat clumsy: we write the payload twice when
975
        // we don't know the size in advance.  This will mess
976
        // with trace output, but is otherwise harmless.
977
303
        start_state = *rw;
978
303
        need_size = !current->payload[k].payload_size;
979
607
        for (i = 0; i < 1 + need_size; i++) {
980
304
            *rw = start_state;
981
982
304
            tmp = current->payload[k].payload_type;
983
328
            while (tmp >= 255) {
984
24
                fixed(8, ff_byte, 0xff);
985
24
                tmp -= 255;
986
            }
987
304
            xu(8, last_payload_type_byte, tmp, 0, 254, 0);
988
989
304
            tmp = current->payload[k].payload_size;
990
309
            while (tmp >= 255) {
991
5
                fixed(8, ff_byte, 0xff);
992
5
                tmp -= 255;
993
            }
994
304
            xu(8, last_payload_size_byte, tmp, 0, 254, 0);
995
996
304
            CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
997
        }
998
    }
999
#endif
1000
1001
283
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
1002
1003
283
    return 0;
1004
}
1005
1006
195
static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
1007
                     H264RawAUD *current)
1008
{
1009
    int err;
1010
1011
195
    HEADER("Access Unit Delimiter");
1012
1013
195
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
1014
                                1 << H264_NAL_AUD));
1015
1016
195
    ub(3, primary_pic_type);
1017
1018
195
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
1019
1020
195
    return 0;
1021
}
1022
1023
4913
static int FUNC(ref_pic_list_modification)(CodedBitstreamContext *ctx, RWContext *rw,
1024
                                           H264RawSliceHeader *current)
1025
{
1026
4913
    CodedBitstreamH264Context *h264 = ctx->priv_data;
1027
4913
    const H264RawSPS *sps = h264->active_sps;
1028
    int err, i, mopn;
1029
1030
4913
    if (current->slice_type % 5 != 2 &&
1031
4631
        current->slice_type % 5 != 4) {
1032
4631
        flag(ref_pic_list_modification_flag_l0);
1033
4631
        if (current->ref_pic_list_modification_flag_l0) {
1034
1517
            for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
1035
1517
                xue(modification_of_pic_nums_idc,
1036
                    current->rplm_l0[i].modification_of_pic_nums_idc, 0, 3, 0);
1037
1038
1517
                mopn = current->rplm_l0[i].modification_of_pic_nums_idc;
1039
1517
                if (mopn == 3)
1040
486
                    break;
1041
1042

1031
                if (mopn == 0 || mopn == 1)
1043
1024
                    xue(abs_diff_pic_num_minus1,
1044
                        current->rplm_l0[i].abs_diff_pic_num_minus1,
1045
                        0, (1 + current->field_pic_flag) *
1046
                        (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
1047
7
                else if (mopn == 2)
1048
7
                    xue(long_term_pic_num,
1049
                        current->rplm_l0[i].long_term_pic_num,
1050
                        0, sps->max_num_ref_frames - 1, 0);
1051
            }
1052
        }
1053
    }
1054
1055
4913
    if (current->slice_type % 5 == 1) {
1056
1068
        flag(ref_pic_list_modification_flag_l1);
1057
1068
        if (current->ref_pic_list_modification_flag_l1) {
1058
            for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
1059
                xue(modification_of_pic_nums_idc,
1060
                    current->rplm_l1[i].modification_of_pic_nums_idc, 0, 3, 0);
1061
1062
                mopn = current->rplm_l1[i].modification_of_pic_nums_idc;
1063
                if (mopn == 3)
1064
                    break;
1065
1066
                if (mopn == 0 || mopn == 1)
1067
                    xue(abs_diff_pic_num_minus1,
1068
                        current->rplm_l1[i].abs_diff_pic_num_minus1,
1069
                        0, (1 + current->field_pic_flag) *
1070
                        (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
1071
                else if (mopn == 2)
1072
                    xue(long_term_pic_num,
1073
                        current->rplm_l1[i].long_term_pic_num,
1074
                        0, sps->max_num_ref_frames - 1, 0);
1075
            }
1076
        }
1077
    }
1078
1079
4913
    return 0;
1080
}
1081
1082
84
static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
1083
                                   H264RawSliceHeader *current)
1084
{
1085
84
    CodedBitstreamH264Context *h264 = ctx->priv_data;
1086
84
    const H264RawSPS *sps = h264->active_sps;
1087
    int chroma;
1088
    int err, i, j;
1089
1090
84
    ue(luma_log2_weight_denom, 0, 7);
1091
1092

84
    chroma = !sps->separate_colour_plane_flag && sps->chroma_format_idc != 0;
1093
84
    if (chroma)
1094
84
        ue(chroma_log2_weight_denom, 0, 7);
1095
1096
494
    for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
1097
410
        flags(luma_weight_l0_flag[i], 1, i);
1098
410
        if (current->luma_weight_l0_flag[i]) {
1099
218
            ses(luma_weight_l0[i], -128, +127, 1, i);
1100
218
            ses(luma_offset_l0[i], -128, +127, 1, i);
1101
        }
1102
410
        if (chroma) {
1103
410
            flags(chroma_weight_l0_flag[i], 1, i);
1104
410
            if (current->chroma_weight_l0_flag[i]) {
1105
624
                for (j = 0; j < 2; j++) {
1106
416
                    ses(chroma_weight_l0[i][j], -128, +127, 2, i, j);
1107
416
                    ses(chroma_offset_l0[i][j], -128, +127, 2, i, j);
1108
                }
1109
            }
1110
        }
1111
    }
1112
1113
84
    if (current->slice_type % 5 == 1) {
1114
        for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
1115
            flags(luma_weight_l1_flag[i], 1, i);
1116
            if (current->luma_weight_l1_flag[i]) {
1117
                ses(luma_weight_l1[i], -128, +127, 1, i);
1118
                ses(luma_offset_l1[i], -128, +127, 1, i);
1119
            }
1120
            if (chroma) {
1121
                flags(chroma_weight_l1_flag[i], 1, i);
1122
                if (current->chroma_weight_l1_flag[i]) {
1123
                    for (j = 0; j < 2; j++) {
1124
                        ses(chroma_weight_l1[i][j], -128, +127, 2, i, j);
1125
                        ses(chroma_offset_l1[i][j], -128, +127, 2, i, j);
1126
                    }
1127
                }
1128
            }
1129
        }
1130
    }
1131
1132
84
    return 0;
1133
}
1134
1135
3843
static int FUNC(dec_ref_pic_marking)(CodedBitstreamContext *ctx, RWContext *rw,
1136
                                     H264RawSliceHeader *current, int idr_pic_flag)
1137
{
1138
3843
    CodedBitstreamH264Context *h264 = ctx->priv_data;
1139
3843
    const H264RawSPS *sps = h264->active_sps;
1140
    int err, i;
1141
    uint32_t mmco;
1142
1143
3843
    if (idr_pic_flag) {
1144
58
        flag(no_output_of_prior_pics_flag);
1145
58
        flag(long_term_reference_flag);
1146
    } else {
1147
3785
        flag(adaptive_ref_pic_marking_mode_flag);
1148
3785
        if (current->adaptive_ref_pic_marking_mode_flag) {
1149
413
            for (i = 0; i < H264_MAX_MMCO_COUNT; i++) {
1150
413
                xue(memory_management_control_operation,
1151
                    current->mmco[i].memory_management_control_operation,
1152
                    0, 6, 0);
1153
1154
413
                mmco = current->mmco[i].memory_management_control_operation;
1155
413
                if (mmco == 0)
1156
206
                    break;
1157
1158

207
                if (mmco == 1 || mmco == 3)
1159
166
                    xue(difference_of_pic_nums_minus1,
1160
                        current->mmco[i].difference_of_pic_nums_minus1,
1161
                        0, INT32_MAX, 0);
1162
207
                if (mmco == 2)
1163
                    xue(long_term_pic_num,
1164
                        current->mmco[i].long_term_pic_num,
1165
                        0, sps->max_num_ref_frames - 1, 0);
1166

207
                if (mmco == 3 || mmco == 6)
1167
28
                    xue(long_term_frame_idx,
1168
                        current->mmco[i].long_term_frame_idx,
1169
                        0, sps->max_num_ref_frames - 1, 0);
1170
207
                if (mmco == 4)
1171
2
                    xue(max_long_term_frame_idx_plus1,
1172
                        current->mmco[i].max_long_term_frame_idx_plus1,
1173
                        0, sps->max_num_ref_frames, 0);
1174
            }
1175
206
            if (i == H264_MAX_MMCO_COUNT) {
1176
                av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many "
1177
                       "memory management control operations.\n");
1178
                return AVERROR_INVALIDDATA;
1179
            }
1180
        }
1181
    }
1182
1183
3843
    return 0;
1184
}
1185
1186
4913
static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
1187
                              H264RawSliceHeader *current)
1188
{
1189
4913
    CodedBitstreamH264Context *h264 = ctx->priv_data;
1190
    const H264RawSPS *sps;
1191
    const H264RawPPS *pps;
1192
    int err;
1193
    int idr_pic_flag;
1194
    int slice_type_i, slice_type_p, slice_type_b;
1195
    int slice_type_si, slice_type_sp;
1196
1197
4913
    HEADER("Slice Header");
1198
1199
4913
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
1200
                                1 << H264_NAL_SLICE     |
1201
                                1 << H264_NAL_IDR_SLICE |
1202
                                1 << H264_NAL_AUXILIARY_SLICE));
1203
1204
4913
    if (current->nal_unit_header.nal_unit_type == H264_NAL_AUXILIARY_SLICE) {
1205
        if (!h264->last_slice_nal_unit_type) {
1206
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Auxiliary slice "
1207
                   "is not decodable without the main picture "
1208
                   "in the same access unit.\n");
1209
            return AVERROR_INVALIDDATA;
1210
        }
1211
        idr_pic_flag = h264->last_slice_nal_unit_type == H264_NAL_IDR_SLICE;
1212
    } else {
1213
4913
        idr_pic_flag = current->nal_unit_header.nal_unit_type == H264_NAL_IDR_SLICE;
1214
    }
1215
1216
4913
    ue(first_mb_in_slice, 0, H264_MAX_MB_PIC_SIZE - 1);
1217
4913
    ue(slice_type, 0, 9);
1218
1219
4913
    slice_type_i  = current->slice_type % 5 == 2;
1220
4913
    slice_type_p  = current->slice_type % 5 == 0;
1221
4913
    slice_type_b  = current->slice_type % 5 == 1;
1222
4913
    slice_type_si = current->slice_type % 5 == 4;
1223
4913
    slice_type_sp = current->slice_type % 5 == 3;
1224
1225

4913
    if (idr_pic_flag && !(slice_type_i || slice_type_si)) {
1226
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid slice type %d "
1227
               "for IDR picture.\n", current->slice_type);
1228
        return AVERROR_INVALIDDATA;
1229
    }
1230
1231
4913
    ue(pic_parameter_set_id, 0, 255);
1232
1233
4913
    pps = h264->pps[current->pic_parameter_set_id];
1234
4913
    if (!pps) {
1235
        av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
1236
               current->pic_parameter_set_id);
1237
        return AVERROR_INVALIDDATA;
1238
    }
1239
4913
    h264->active_pps = pps;
1240
1241
4913
    sps = h264->sps[pps->seq_parameter_set_id];
1242
4913
    if (!sps) {
1243
        av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
1244
               pps->seq_parameter_set_id);
1245
        return AVERROR_INVALIDDATA;
1246
    }
1247
4913
    h264->active_sps = sps;
1248
1249
4913
    if (sps->separate_colour_plane_flag)
1250
        u(2, colour_plane_id, 0, 2);
1251
1252
4913
    ub(sps->log2_max_frame_num_minus4 + 4, frame_num);
1253
1254
4913
    if (!sps->frame_mbs_only_flag) {
1255
351
        flag(field_pic_flag);
1256
351
        if (current->field_pic_flag)
1257
254
            flag(bottom_field_flag);
1258
        else
1259
97
            infer(bottom_field_flag, 0);
1260
    } else {
1261
4562
        infer(field_pic_flag,    0);
1262
4562
        infer(bottom_field_flag, 0);
1263
    }
1264
1265
4913
    if (idr_pic_flag)
1266
58
        ue(idr_pic_id, 0, 65535);
1267
1268
4913
    if (sps->pic_order_cnt_type == 0) {
1269
2491
        ub(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, pic_order_cnt_lsb);
1270
2491
        if (pps->bottom_field_pic_order_in_frame_present_flag &&
1271
90
            !current->field_pic_flag)
1272
90
            se(delta_pic_order_cnt_bottom, INT32_MIN + 1, INT32_MAX);
1273
1274
2422
    } else if (sps->pic_order_cnt_type == 1) {
1275
2281
        if (!sps->delta_pic_order_always_zero_flag) {
1276
423
            se(delta_pic_order_cnt[0], INT32_MIN + 1, INT32_MAX);
1277
423
            if (pps->bottom_field_pic_order_in_frame_present_flag &&
1278
                !current->field_pic_flag)
1279
                se(delta_pic_order_cnt[1], INT32_MIN + 1, INT32_MAX);
1280
            else
1281
423
                infer(delta_pic_order_cnt[1], 0);
1282
        } else {
1283
1858
            infer(delta_pic_order_cnt[0], 0);
1284
1858
            infer(delta_pic_order_cnt[1], 0);
1285
        }
1286
    }
1287
1288
4913
    if (pps->redundant_pic_cnt_present_flag)
1289
        ue(redundant_pic_cnt, 0, 127);
1290
    else
1291
4913
        infer(redundant_pic_cnt, 0);
1292
1293
4913
    if (current->nal_unit_header.nal_unit_type != H264_NAL_AUXILIARY_SLICE
1294
4913
        && !current->redundant_pic_cnt)
1295
4913
        h264->last_slice_nal_unit_type =
1296
4913
            current->nal_unit_header.nal_unit_type;
1297
1298
4913
    if (slice_type_b)
1299
1068
        flag(direct_spatial_mv_pred_flag);
1300
1301

4913
    if (slice_type_p || slice_type_sp || slice_type_b) {
1302
4631
        flag(num_ref_idx_active_override_flag);
1303
4631
        if (current->num_ref_idx_active_override_flag) {
1304
2180
            ue(num_ref_idx_l0_active_minus1, 0, 31);
1305
2180
            if (slice_type_b)
1306
1008
                ue(num_ref_idx_l1_active_minus1, 0, 31);
1307
        } else {
1308
2451
            infer(num_ref_idx_l0_active_minus1,
1309
                  pps->num_ref_idx_l0_default_active_minus1);
1310
2451
            infer(num_ref_idx_l1_active_minus1,
1311
                  pps->num_ref_idx_l1_default_active_minus1);
1312
        }
1313
    }
1314
1315
4913
    if (current->nal_unit_header.nal_unit_type == 20 ||
1316
4913
        current->nal_unit_header.nal_unit_type == 21) {
1317
        av_log(ctx->log_ctx, AV_LOG_ERROR, "MVC / 3DAVC not supported.\n");
1318
        return AVERROR_PATCHWELCOME;
1319
    } else {
1320
4913
        CHECK(FUNC(ref_pic_list_modification)(ctx, rw, current));
1321
    }
1322
1323

4913
    if ((pps->weighted_pred_flag && (slice_type_p || slice_type_sp)) ||
1324

4829
        (pps->weighted_bipred_idc == 1 && slice_type_b)) {
1325
84
        CHECK(FUNC(pred_weight_table)(ctx, rw, current));
1326
    }
1327
1328
4913
    if (current->nal_unit_header.nal_ref_idc != 0) {
1329
3843
        CHECK(FUNC(dec_ref_pic_marking)(ctx, rw, current, idr_pic_flag));
1330
    }
1331
1332

4913
    if (pps->entropy_coding_mode_flag &&
1333
1120
        !slice_type_i && !slice_type_si) {
1334
1120
        ue(cabac_init_idc, 0, 2);
1335
    }
1336
1337
4913
    se(slice_qp_delta, - 51 - 6 * sps->bit_depth_luma_minus8,
1338
                       + 51 + 6 * sps->bit_depth_luma_minus8);
1339

4913
    if (slice_type_sp || slice_type_si) {
1340
39
        if (slice_type_sp)
1341
39
            flag(sp_for_switch_flag);
1342
39
        se(slice_qs_delta, -51, +51);
1343
    }
1344
1345
4913
    if (pps->deblocking_filter_control_present_flag) {
1346
2909
        ue(disable_deblocking_filter_idc, 0, 2);
1347
2909
        if (current->disable_deblocking_filter_idc != 1) {
1348
2058
            se(slice_alpha_c0_offset_div2, -6, +6);
1349
2058
            se(slice_beta_offset_div2,     -6, +6);
1350
        } else {
1351
851
            infer(slice_alpha_c0_offset_div2, 0);
1352
851
            infer(slice_beta_offset_div2,     0);
1353
        }
1354
    } else {
1355
2004
        infer(disable_deblocking_filter_idc, 0);
1356
2004
        infer(slice_alpha_c0_offset_div2,    0);
1357
2004
        infer(slice_beta_offset_div2,        0);
1358
    }
1359
1360
4913
    if (pps->num_slice_groups_minus1 > 0 &&
1361
1619
        pps->slice_group_map_type >= 3 &&
1362
664
        pps->slice_group_map_type <= 5) {
1363
        unsigned int pic_size, max, bits;
1364
1365
314
        pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
1366
314
                   (sps->pic_height_in_map_units_minus1 + 1);
1367
314
        max = (pic_size + pps->slice_group_change_rate_minus1) /
1368
314
              (pps->slice_group_change_rate_minus1 + 1);
1369
314
        bits = av_log2(2 * max - 1);
1370
1371
314
        u(bits, slice_group_change_cycle, 0, max);
1372
    }
1373
1374
4913
    if (pps->entropy_coding_mode_flag) {
1375
5066
        while (byte_alignment(rw))
1376
3866
            fixed(1, cabac_alignment_one_bit, 1);
1377
    }
1378
1379
4913
    return 0;
1380
}
1381
1382
static int FUNC(filler)(CodedBitstreamContext *ctx, RWContext *rw,
1383
                        H264RawFiller *current)
1384
{
1385
    int err;
1386
1387
    HEADER("Filler Data");
1388
1389
    CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
1390
                                1 << H264_NAL_FILLER_DATA));
1391
1392
#ifdef READ
1393
    while (show_bits(rw, 8) == 0xff) {
1394
        fixed(8, ff_byte, 0xff);
1395
        ++current->filler_size;
1396
    }
1397
#else
1398
    {
1399
        uint32_t i;
1400
        for (i = 0; i < current->filler_size; i++)
1401
            fixed(8, ff_byte, 0xff);
1402
    }
1403
#endif
1404
1405
    CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
1406
1407
    return 0;
1408
}
1409
1410
1
static int FUNC(end_of_sequence)(CodedBitstreamContext *ctx, RWContext *rw,
1411
                                 H264RawNALUnitHeader *current)
1412
{
1413
1
    HEADER("End of Sequence");
1414
1415
1
    return FUNC(nal_unit_header)(ctx, rw, current,
1416
                                 1 << H264_NAL_END_SEQUENCE);
1417
}
1418
1419
4
static int FUNC(end_of_stream)(CodedBitstreamContext *ctx, RWContext *rw,
1420
                               H264RawNALUnitHeader *current)
1421
{
1422
4
    HEADER("End of Stream");
1423
1424
4
    return FUNC(nal_unit_header)(ctx, rw, current,
1425
                                 1 << H264_NAL_END_STREAM);
1426
}