]> git.sesse.net Git - ffmpeg/blob - libavcodec/cbs_h264_syntax_template.c
avfilter/vf_edgedetect: add canny mode
[ffmpeg] / libavcodec / cbs_h264_syntax_template.c
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 static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
20 {
21     int err;
22
23     fixed(1, rbsp_stop_one_bit, 1);
24     while (byte_alignment(rw) != 0)
25         fixed(1, rbsp_alignment_zero_bit, 0);
26
27     return 0;
28 }
29
30 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     u(1, forbidden_zero_bit, 0, 0);
37     u(2, nal_ref_idc,        0, 3);
38     u(5, nal_unit_type,      0, 31);
39
40     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     if (current->nal_unit_type == 14 ||
47         current->nal_unit_type == 20 ||
48         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     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 static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
89                                 H264RawHRD *current)
90 {
91     int err, i;
92
93     ue(cpb_cnt_minus1, 0, 31);
94     u(4, bit_rate_scale, 0, 15);
95     u(4, cpb_size_scale, 0, 15);
96
97     for (i = 0; i <= current->cpb_cnt_minus1; i++) {
98         ues(bit_rate_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
99         ues(cpb_size_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
100         flags(cbr_flag[i], 1, i);
101     }
102
103     u(5, initial_cpb_removal_delay_length_minus1, 0, 31);
104     u(5, cpb_removal_delay_length_minus1,         0, 31);
105     u(5, dpb_output_delay_length_minus1,          0, 31);
106     u(5, time_offset_length,                      0, 31);
107
108     return 0;
109 }
110
111 static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
112                                 H264RawVUI *current, H264RawSPS *sps)
113 {
114     int err;
115
116     flag(aspect_ratio_info_present_flag);
117     if (current->aspect_ratio_info_present_flag) {
118         u(8, aspect_ratio_idc, 0, 255);
119         if (current->aspect_ratio_idc == 255) {
120             u(16, sar_width,  0, 65535);
121             u(16, sar_height, 0, 65535);
122         }
123     } else {
124         infer(aspect_ratio_idc, 0);
125     }
126
127     flag(overscan_info_present_flag);
128     if (current->overscan_info_present_flag)
129         flag(overscan_appropriate_flag);
130
131     flag(video_signal_type_present_flag);
132     if (current->video_signal_type_present_flag) {
133         u(3, video_format, 0, 7);
134         flag(video_full_range_flag);
135         flag(colour_description_present_flag);
136         if (current->colour_description_present_flag) {
137             u(8, colour_primaries,         0, 255);
138             u(8, transfer_characteristics, 0, 255);
139             u(8, matrix_coefficients,      0, 255);
140         }
141     } else {
142         infer(video_format,             5);
143         infer(video_full_range_flag,    0);
144         infer(colour_primaries,         2);
145         infer(transfer_characteristics, 2);
146         infer(matrix_coefficients,      2);
147     }
148
149     flag(chroma_loc_info_present_flag);
150     if (current->chroma_loc_info_present_flag) {
151         ue(chroma_sample_loc_type_top_field,    0, 5);
152         ue(chroma_sample_loc_type_bottom_field, 0, 5);
153     } else {
154         infer(chroma_sample_loc_type_top_field,    0);
155         infer(chroma_sample_loc_type_bottom_field, 0);
156     }
157
158     flag(timing_info_present_flag);
159     if (current->timing_info_present_flag) {
160         u(32, num_units_in_tick, 1, UINT32_MAX);
161         u(32, time_scale,        1, UINT32_MAX);
162         flag(fixed_frame_rate_flag);
163     } else {
164         infer(fixed_frame_rate_flag, 0);
165     }
166
167     flag(nal_hrd_parameters_present_flag);
168     if (current->nal_hrd_parameters_present_flag)
169         CHECK(FUNC(hrd_parameters)(ctx, rw, &current->nal_hrd_parameters));
170
171     flag(vcl_hrd_parameters_present_flag);
172     if (current->vcl_hrd_parameters_present_flag)
173         CHECK(FUNC(hrd_parameters)(ctx, rw, &current->vcl_hrd_parameters));
174
175     if (current->nal_hrd_parameters_present_flag ||
176         current->vcl_hrd_parameters_present_flag)
177         flag(low_delay_hrd_flag);
178     else
179         infer(low_delay_hrd_flag, 1 - current->fixed_frame_rate_flag);
180
181     flag(pic_struct_present_flag);
182
183     flag(bitstream_restriction_flag);
184     if (current->bitstream_restriction_flag) {
185         flag(motion_vectors_over_pic_boundaries_flag);
186         ue(max_bytes_per_pic_denom, 0, 16);
187         ue(max_bits_per_mb_denom,   0, 16);
188         ue(log2_max_mv_length_horizontal, 0, 16);
189         ue(log2_max_mv_length_vertical,   0, 16);
190         ue(max_num_reorder_frames,  0, H264_MAX_DPB_FRAMES);
191         ue(max_dec_frame_buffering, 0, H264_MAX_DPB_FRAMES);
192     } else {
193         infer(motion_vectors_over_pic_boundaries_flag, 1);
194         infer(max_bytes_per_pic_denom, 2);
195         infer(max_bits_per_mb_denom,   1);
196         infer(log2_max_mv_length_horizontal, 16);
197         infer(log2_max_mv_length_vertical,   16);
198
199         if ((sps->profile_idc ==  44 || sps->profile_idc ==  86 ||
200              sps->profile_idc == 110 || sps->profile_idc == 110 ||
201              sps->profile_idc == 122 || sps->profile_idc == 244) &&
202             sps->constraint_set3_flag) {
203             infer(max_num_reorder_frames,  0);
204             infer(max_dec_frame_buffering, 0);
205         } else {
206             infer(max_num_reorder_frames,  H264_MAX_DPB_FRAMES);
207             infer(max_dec_frame_buffering, H264_MAX_DPB_FRAMES);
208         }
209     }
210
211     return 0;
212 }
213
214 static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
215                      H264RawSPS *current)
216 {
217     int err, i;
218
219     HEADER("Sequence Parameter Set");
220
221     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
222                                 1 << H264_NAL_SPS));
223
224     u(8, profile_idc, 0, 255);
225
226     flag(constraint_set0_flag);
227     flag(constraint_set1_flag);
228     flag(constraint_set2_flag);
229     flag(constraint_set3_flag);
230     flag(constraint_set4_flag);
231     flag(constraint_set5_flag);
232
233     u(2, reserved_zero_2bits,  0, 0);
234
235     u(8, level_idc, 0, 255);
236
237     ue(seq_parameter_set_id, 0, 31);
238
239     if (current->profile_idc == 100 || current->profile_idc == 110 ||
240         current->profile_idc == 122 || current->profile_idc == 244 ||
241         current->profile_idc ==  44 || current->profile_idc ==  83 ||
242         current->profile_idc ==  86 || current->profile_idc == 118 ||
243         current->profile_idc == 128 || current->profile_idc == 138) {
244         ue(chroma_format_idc, 0, 3);
245
246         if (current->chroma_format_idc == 3)
247             flag(separate_colour_plane_flag);
248         else
249             infer(separate_colour_plane_flag, 0);
250
251         ue(bit_depth_luma_minus8,   0, 6);
252         ue(bit_depth_chroma_minus8, 0, 6);
253
254         flag(qpprime_y_zero_transform_bypass_flag);
255
256         flag(seq_scaling_matrix_present_flag);
257         if (current->seq_scaling_matrix_present_flag) {
258             for (i = 0; i < ((current->chroma_format_idc != 3) ? 8 : 12); i++) {
259                 flags(seq_scaling_list_present_flag[i], 1, i);
260                 if (current->seq_scaling_list_present_flag[i]) {
261                     if (i < 6)
262                         CHECK(FUNC(scaling_list)(ctx, rw,
263                                                  &current->scaling_list_4x4[i],
264                                                  16));
265                     else
266                         CHECK(FUNC(scaling_list)(ctx, rw,
267                                                  &current->scaling_list_8x8[i - 6],
268                                                  64));
269                 }
270             }
271         }
272     } else {
273         infer(chroma_format_idc, current->profile_idc == 183 ? 0 : 1);
274
275         infer(separate_colour_plane_flag, 0);
276         infer(bit_depth_luma_minus8,      0);
277         infer(bit_depth_chroma_minus8,    0);
278     }
279
280     ue(log2_max_frame_num_minus4, 0, 12);
281     ue(pic_order_cnt_type, 0, 2);
282
283     if (current->pic_order_cnt_type == 0) {
284         ue(log2_max_pic_order_cnt_lsb_minus4, 0, 12);
285     } else if (current->pic_order_cnt_type == 1) {
286         flag(delta_pic_order_always_zero_flag);
287         se(offset_for_non_ref_pic,         INT32_MIN + 1, INT32_MAX);
288         se(offset_for_top_to_bottom_field, INT32_MIN + 1, INT32_MAX);
289         ue(num_ref_frames_in_pic_order_cnt_cycle, 0, 255);
290
291         for (i = 0; i < current->num_ref_frames_in_pic_order_cnt_cycle; i++)
292             ses(offset_for_ref_frame[i], INT32_MIN + 1, INT32_MAX, 1, i);
293     }
294
295     ue(max_num_ref_frames, 0, H264_MAX_DPB_FRAMES);
296     flag(gaps_in_frame_num_allowed_flag);
297
298     ue(pic_width_in_mbs_minus1,        0, H264_MAX_MB_WIDTH);
299     ue(pic_height_in_map_units_minus1, 0, H264_MAX_MB_HEIGHT);
300
301     flag(frame_mbs_only_flag);
302     if (!current->frame_mbs_only_flag)
303         flag(mb_adaptive_frame_field_flag);
304
305     flag(direct_8x8_inference_flag);
306
307     flag(frame_cropping_flag);
308     if (current->frame_cropping_flag) {
309         ue(frame_crop_left_offset,   0, H264_MAX_WIDTH);
310         ue(frame_crop_right_offset,  0, H264_MAX_WIDTH);
311         ue(frame_crop_top_offset,    0, H264_MAX_HEIGHT);
312         ue(frame_crop_bottom_offset, 0, H264_MAX_HEIGHT);
313     }
314
315     flag(vui_parameters_present_flag);
316     if (current->vui_parameters_present_flag)
317         CHECK(FUNC(vui_parameters)(ctx, rw, &current->vui, current));
318
319     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
320
321     return 0;
322 }
323
324 static int FUNC(sps_extension)(CodedBitstreamContext *ctx, RWContext *rw,
325                                H264RawSPSExtension *current)
326 {
327     int err;
328
329     HEADER("Sequence Parameter Set Extension");
330
331     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
332                                 1 << H264_NAL_SPS_EXT));
333
334     ue(seq_parameter_set_id, 0, 31);
335
336     ue(aux_format_idc, 0, 3);
337
338     if (current->aux_format_idc != 0) {
339         int bits;
340
341         ue(bit_depth_aux_minus8, 0, 4);
342         flag(alpha_incr_flag);
343
344         bits = current->bit_depth_aux_minus8 + 9;
345         u(bits, alpha_opaque_value,      0, MAX_UINT_BITS(bits));
346         u(bits, alpha_transparent_value, 0, MAX_UINT_BITS(bits));
347     }
348
349     flag(additional_extension_flag);
350
351     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
352
353     return 0;
354 }
355
356 static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
357                      H264RawPPS *current)
358 {
359     CodedBitstreamH264Context *h264 = ctx->priv_data;
360     const H264RawSPS *sps;
361     int err, i;
362
363     HEADER("Picture Parameter Set");
364
365     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
366                                 1 << H264_NAL_PPS));
367
368     ue(pic_parameter_set_id, 0, 255);
369     ue(seq_parameter_set_id, 0, 31);
370
371     sps = h264->sps[current->seq_parameter_set_id];
372     if (!sps) {
373         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
374                current->seq_parameter_set_id);
375         return AVERROR_INVALIDDATA;
376     }
377
378     flag(entropy_coding_mode_flag);
379     flag(bottom_field_pic_order_in_frame_present_flag);
380
381     ue(num_slice_groups_minus1, 0, 7);
382     if (current->num_slice_groups_minus1 > 0) {
383         unsigned int pic_size;
384         int iGroup;
385
386         pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
387                    (sps->pic_height_in_map_units_minus1 + 1);
388
389         ue(slice_group_map_type, 0, 6);
390
391         if (current->slice_group_map_type == 0) {
392             for (iGroup = 0; iGroup <= current->num_slice_groups_minus1; iGroup++)
393                 ues(run_length_minus1[iGroup], 0, pic_size - 1, 1, iGroup);
394
395         } else if (current->slice_group_map_type == 2) {
396             for (iGroup = 0; iGroup < current->num_slice_groups_minus1; iGroup++) {
397                 ues(top_left[iGroup],       0, pic_size - 1, 1, iGroup);
398                 ues(bottom_right[iGroup],
399                     current->top_left[iGroup], pic_size - 1, 1, iGroup);
400             }
401         } else if (current->slice_group_map_type == 3 ||
402                    current->slice_group_map_type == 4 ||
403                    current->slice_group_map_type == 5) {
404             flag(slice_group_change_direction_flag);
405             ue(slice_group_change_rate_minus1, 0, pic_size - 1);
406         } else if (current->slice_group_map_type == 6) {
407             ue(pic_size_in_map_units_minus1, pic_size - 1, pic_size - 1);
408
409             allocate(current->slice_group_id,
410                      current->pic_size_in_map_units_minus1 + 1);
411             for (i = 0; i <= current->pic_size_in_map_units_minus1; i++)
412                 us(av_log2(2 * current->num_slice_groups_minus1 + 1),
413                    slice_group_id[i], 0, current->num_slice_groups_minus1, 1, i);
414         }
415     }
416
417     ue(num_ref_idx_l0_default_active_minus1, 0, 31);
418     ue(num_ref_idx_l1_default_active_minus1, 0, 31);
419
420     flag(weighted_pred_flag);
421     u(2, weighted_bipred_idc, 0, 2);
422
423     se(pic_init_qp_minus26, -26 - 6 * sps->bit_depth_luma_minus8, +25);
424     se(pic_init_qs_minus26, -26, +25);
425     se(chroma_qp_index_offset, -12, +12);
426
427     flag(deblocking_filter_control_present_flag);
428     flag(constrained_intra_pred_flag);
429     flag(redundant_pic_cnt_present_flag);
430
431     if (more_rbsp_data(current->more_rbsp_data))
432     {
433         flag(transform_8x8_mode_flag);
434
435         flag(pic_scaling_matrix_present_flag);
436         if (current->pic_scaling_matrix_present_flag) {
437             for (i = 0; i < 6 + (((sps->chroma_format_idc != 3) ? 2 : 6) *
438                                  current->transform_8x8_mode_flag); i++) {
439                 flags(pic_scaling_list_present_flag[i], 1, i);
440                 if (current->pic_scaling_list_present_flag[i]) {
441                     if (i < 6)
442                         CHECK(FUNC(scaling_list)(ctx, rw,
443                                                  &current->scaling_list_4x4[i],
444                                                  16));
445                     else
446                         CHECK(FUNC(scaling_list)(ctx, rw,
447                                                  &current->scaling_list_8x8[i - 6],
448                                                  64));
449                 }
450             }
451         }
452
453         se(second_chroma_qp_index_offset, -12, +12);
454     } else {
455         infer(transform_8x8_mode_flag, 0);
456         infer(pic_scaling_matrix_present_flag, 0);
457         infer(second_chroma_qp_index_offset, current->chroma_qp_index_offset);
458     }
459
460     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
461
462     return 0;
463 }
464
465 static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
466                                       H264RawSEIBufferingPeriod *current)
467 {
468     CodedBitstreamH264Context *h264 = ctx->priv_data;
469     const H264RawSPS *sps;
470     int err, i, length;
471
472     ue(seq_parameter_set_id, 0, 31);
473
474     sps = h264->sps[current->seq_parameter_set_id];
475     if (!sps) {
476         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
477                current->seq_parameter_set_id);
478         return AVERROR_INVALIDDATA;
479     }
480     h264->active_sps = sps;
481
482     if (sps->vui.nal_hrd_parameters_present_flag) {
483         for (i = 0; i <= sps->vui.nal_hrd_parameters.cpb_cnt_minus1; i++) {
484             length = sps->vui.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
485             xu(length, initial_cpb_removal_delay[SchedSelIdx],
486                current->nal.initial_cpb_removal_delay[i],
487                1, MAX_UINT_BITS(length), 1, i);
488             xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
489                current->nal.initial_cpb_removal_delay_offset[i],
490                0, MAX_UINT_BITS(length), 1, i);
491         }
492     }
493
494     if (sps->vui.vcl_hrd_parameters_present_flag) {
495         for (i = 0; i <= sps->vui.vcl_hrd_parameters.cpb_cnt_minus1; i++) {
496             length = sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
497             xu(length, initial_cpb_removal_delay[SchedSelIdx],
498                current->vcl.initial_cpb_removal_delay[i],
499                1, MAX_UINT_BITS(length), 1, i);
500             xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
501                current->vcl.initial_cpb_removal_delay_offset[i],
502                0, MAX_UINT_BITS(length), 1, i);
503         }
504     }
505
506     return 0;
507 }
508
509 static int FUNC(sei_pic_timestamp)(CodedBitstreamContext *ctx, RWContext *rw,
510                                    H264RawSEIPicTimestamp *current)
511 {
512     CodedBitstreamH264Context *h264 = ctx->priv_data;
513     const H264RawSPS *sps;
514     uint8_t time_offset_length;
515     int err;
516
517     u(2, ct_type, 0, 2);
518     flag(nuit_field_based_flag);
519     u(5, counting_type, 0, 6);
520     flag(full_timestamp_flag);
521     flag(discontinuity_flag);
522     flag(cnt_dropped_flag);
523     u(8, n_frames, 0, 255);
524     if (current->full_timestamp_flag) {
525             u(6, seconds_value, 0, 59);
526             u(6, minutes_value, 0, 59);
527             u(5, hours_value,   0, 23);
528     } else {
529         flag(seconds_flag);
530         if (current->seconds_flag) {
531             u(6, seconds_value, 0, 59);
532             flag(minutes_flag);
533             if (current->minutes_flag) {
534                 u(6, minutes_value, 0, 59);
535                 flag(hours_flag);
536                 if (current->hours_flag)
537                     u(5, hours_value, 0, 23);
538             }
539         }
540     }
541
542     sps = h264->active_sps;
543     if (sps->vui.nal_hrd_parameters_present_flag)
544         time_offset_length = sps->vui.nal_hrd_parameters.time_offset_length;
545     else if (sps->vui.vcl_hrd_parameters_present_flag)
546         time_offset_length = sps->vui.vcl_hrd_parameters.time_offset_length;
547     else
548         time_offset_length = 24;
549
550     if (time_offset_length > 0)
551         u(time_offset_length, time_offset,
552           0, MAX_UINT_BITS(time_offset_length));
553     else
554         infer(time_offset, 0);
555
556     return 0;
557 }
558
559 static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
560                                 H264RawSEIPicTiming *current)
561 {
562     CodedBitstreamH264Context *h264 = ctx->priv_data;
563     const H264RawSPS *sps;
564     int err;
565
566     sps = h264->active_sps;
567     if (!sps) {
568         // If there is exactly one possible SPS but it is not yet active
569         // then just assume that it should be the active one.
570         int i, k = -1;
571         for (i = 0; i < H264_MAX_SPS_COUNT; i++) {
572             if (h264->sps[i]) {
573                 if (k >= 0) {
574                     k = -1;
575                     break;
576                 }
577                 k = i;
578             }
579         }
580         if (k >= 0)
581             sps = h264->sps[k];
582     }
583     if (!sps) {
584         av_log(ctx->log_ctx, AV_LOG_ERROR,
585                "No active SPS for pic_timing.\n");
586         return AVERROR_INVALIDDATA;
587     }
588
589     if (sps->vui.nal_hrd_parameters_present_flag ||
590         sps->vui.vcl_hrd_parameters_present_flag) {
591         const H264RawHRD *hrd;
592
593         if (sps->vui.nal_hrd_parameters_present_flag)
594             hrd = &sps->vui.nal_hrd_parameters;
595         else if (sps->vui.vcl_hrd_parameters_present_flag)
596             hrd = &sps->vui.vcl_hrd_parameters;
597         else {
598             av_log(ctx->log_ctx, AV_LOG_ERROR,
599                    "No HRD parameters for pic_timing.\n");
600             return AVERROR_INVALIDDATA;
601         }
602
603         u(hrd->cpb_removal_delay_length_minus1 + 1, cpb_removal_delay,
604           0, MAX_UINT_BITS(hrd->cpb_removal_delay_length_minus1 + 1));
605         u(hrd->dpb_output_delay_length_minus1 + 1, dpb_output_delay,
606           0, MAX_UINT_BITS(hrd->dpb_output_delay_length_minus1 + 1));
607     }
608
609     if (sps->vui.pic_struct_present_flag) {
610         static const int num_clock_ts[9] = {
611             1, 1, 1, 2, 2, 3, 3, 2, 3
612         };
613         int i;
614
615         u(4, pic_struct, 0, 8);
616         if (current->pic_struct > 8)
617             return AVERROR_INVALIDDATA;
618
619         for (i = 0; i < num_clock_ts[current->pic_struct]; i++) {
620             flags(clock_timestamp_flag[i], 1, i);
621             if (current->clock_timestamp_flag[i])
622                 CHECK(FUNC(sei_pic_timestamp)(ctx, rw, &current->timestamp[i]));
623         }
624     }
625
626     return 0;
627 }
628
629 static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext *rw,
630                                           H264RawSEIUserDataRegistered *current,
631                                           uint32_t *payload_size)
632 {
633     int err, i, j;
634
635     u(8, itu_t_t35_country_code, 0x00, 0xff);
636     if (current->itu_t_t35_country_code != 0xff)
637         i = 1;
638     else {
639         u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
640         i = 2;
641     }
642
643 #ifdef READ
644     if (*payload_size < i) {
645         av_log(ctx->log_ctx, AV_LOG_ERROR,
646                "Invalid SEI user data registered payload.\n");
647         return AVERROR_INVALIDDATA;
648     }
649     current->data_length = *payload_size - i;
650 #else
651     *payload_size = i + current->data_length;
652 #endif
653
654     allocate(current->data, current->data_length);
655     for (j = 0; j < current->data_length; j++)
656         xu(8, itu_t_t35_payload_byte[i], current->data[j], 0x00, 0xff, 1, i + j);
657
658     return 0;
659 }
660
661 static int FUNC(sei_user_data_unregistered)(CodedBitstreamContext *ctx, RWContext *rw,
662                                             H264RawSEIUserDataUnregistered *current,
663                                             uint32_t *payload_size)
664 {
665     int err, i;
666
667 #ifdef READ
668     if (*payload_size < 16) {
669         av_log(ctx->log_ctx, AV_LOG_ERROR,
670                "Invalid SEI user data unregistered payload.\n");
671         return AVERROR_INVALIDDATA;
672     }
673     current->data_length = *payload_size - 16;
674 #else
675     *payload_size = 16 + current->data_length;
676 #endif
677
678     for (i = 0; i < 16; i++)
679         us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
680
681     allocate(current->data, current->data_length);
682
683     for (i = 0; i < current->data_length; i++)
684         xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
685
686     return 0;
687 }
688
689 static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
690                                     H264RawSEIRecoveryPoint *current)
691 {
692     int err;
693
694     ue(recovery_frame_cnt, 0, 65535);
695     flag(exact_match_flag);
696     flag(broken_link_flag);
697     u(2, changing_slice_group_idc, 0, 2);
698
699     return 0;
700 }
701
702 static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
703                                          H264RawSEIDisplayOrientation *current)
704 {
705     int err;
706
707     flag(display_orientation_cancel_flag);
708     if (!current->display_orientation_cancel_flag) {
709         flag(hor_flip);
710         flag(ver_flip);
711         u(16, anticlockwise_rotation, 0, 65535);
712         ue(display_orientation_repetition_period, 0, 16384);
713         flag(display_orientation_extension_flag);
714     }
715
716     return 0;
717 }
718
719 static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
720                              H264RawSEIPayload *current)
721 {
722     int err, i;
723     int start_position, end_position;
724
725 #ifdef READ
726     start_position = get_bits_count(rw);
727 #else
728     start_position = put_bits_count(rw);
729 #endif
730
731     switch (current->payload_type) {
732     case H264_SEI_TYPE_BUFFERING_PERIOD:
733         CHECK(FUNC(sei_buffering_period)
734               (ctx, rw, &current->payload.buffering_period));
735         break;
736     case H264_SEI_TYPE_PIC_TIMING:
737         CHECK(FUNC(sei_pic_timing)
738               (ctx, rw, &current->payload.pic_timing));
739         break;
740     case H264_SEI_TYPE_FILLER_PAYLOAD:
741         {
742             for (i = 0; i  < current->payload_size; i++)
743                 fixed(8, ff_byte, 0xff);
744         }
745         break;
746     case H264_SEI_TYPE_USER_DATA_REGISTERED:
747         CHECK(FUNC(sei_user_data_registered)
748               (ctx, rw, &current->payload.user_data_registered, &current->payload_size));
749         break;
750     case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
751         CHECK(FUNC(sei_user_data_unregistered)
752               (ctx, rw, &current->payload.user_data_unregistered, &current->payload_size));
753         break;
754     case H264_SEI_TYPE_RECOVERY_POINT:
755         CHECK(FUNC(sei_recovery_point)
756               (ctx, rw, &current->payload.recovery_point));
757         break;
758     case H264_SEI_TYPE_DISPLAY_ORIENTATION:
759         CHECK(FUNC(sei_display_orientation)
760               (ctx, rw, &current->payload.display_orientation));
761         break;
762     default:
763         {
764             allocate(current->payload.other.data, current->payload_size);
765             for (i = 0; i < current->payload_size; i++)
766                 xu(8, payload_byte, current->payload.other.data[i], 0, 255, 1, i);
767         }
768     }
769
770     if (byte_alignment(rw)) {
771         fixed(1, bit_equal_to_one, 1);
772         while (byte_alignment(rw))
773             fixed(1, bit_equal_to_zero, 0);
774     }
775
776 #ifdef READ
777     end_position = get_bits_count(rw);
778     if (end_position < start_position + 8 * current->payload_size) {
779         av_log(ctx->log_ctx, AV_LOG_ERROR, "Incorrect SEI payload length: "
780                "header %"PRIu32" bits, actually %d bits.\n",
781                8 * current->payload_size,
782                end_position - start_position);
783         return AVERROR_INVALIDDATA;
784     }
785 #else
786     end_position = put_bits_count(rw);
787     current->payload_size = (end_position - start_position) / 8;
788 #endif
789
790     return 0;
791 }
792
793 static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
794                      H264RawSEI *current)
795 {
796     int err, k;
797
798     HEADER("Supplemental Enhancement Information");
799
800     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
801                                 1 << H264_NAL_SEI));
802
803 #ifdef READ
804     for (k = 0; k < H264_MAX_SEI_PAYLOADS; k++) {
805         uint32_t payload_type = 0;
806         uint32_t payload_size = 0;
807         uint32_t tmp;
808
809         while (show_bits(rw, 8) == 0xff) {
810             fixed(8, ff_byte, 0xff);
811             payload_type += 255;
812         }
813         xu(8, last_payload_type_byte, tmp, 0, 254, 0);
814         payload_type += tmp;
815
816         while (show_bits(rw, 8) == 0xff) {
817             fixed(8, ff_byte, 0xff);
818             payload_size += 255;
819         }
820         xu(8, last_payload_size_byte, tmp, 0, 254, 0);
821         payload_size += tmp;
822
823         current->payload[k].payload_type = payload_type;
824         current->payload[k].payload_size = payload_size;
825
826         CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
827
828         if (!cbs_h2645_read_more_rbsp_data(rw))
829             break;
830     }
831     if (k >= H264_MAX_SEI_PAYLOADS) {
832         av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
833                "SEI message: found %d.\n", k);
834         return AVERROR_INVALIDDATA;
835     }
836     current->payload_count = k + 1;
837 #else
838     for (k = 0; k < current->payload_count; k++) {
839         PutBitContext start_state;
840         uint32_t tmp;
841         int need_size, i;
842
843         // Somewhat clumsy: we write the payload twice when
844         // we don't know the size in advance.  This will mess
845         // with trace output, but is otherwise harmless.
846         start_state = *rw;
847         need_size = !current->payload[k].payload_size;
848         for (i = 0; i < 1 + need_size; i++) {
849             *rw = start_state;
850
851             tmp = current->payload[k].payload_type;
852             while (tmp >= 255) {
853                 fixed(8, ff_byte, 0xff);
854                 tmp -= 255;
855             }
856             xu(8, last_payload_type_byte, tmp, 0, 254, 0);
857
858             tmp = current->payload[k].payload_size;
859             while (tmp >= 255) {
860                 fixed(8, ff_byte, 0xff);
861                 tmp -= 255;
862             }
863             xu(8, last_payload_size_byte, tmp, 0, 254, 0);
864
865             CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
866         }
867     }
868 #endif
869
870     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
871
872     return 0;
873 }
874
875 static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
876                      H264RawAUD *current)
877 {
878     int err;
879
880     HEADER("Access Unit Delimiter");
881
882     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
883                                 1 << H264_NAL_AUD));
884
885     u(3, primary_pic_type, 0, 7);
886
887     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
888
889     return 0;
890 }
891
892 static int FUNC(ref_pic_list_modification)(CodedBitstreamContext *ctx, RWContext *rw,
893                                            H264RawSliceHeader *current)
894 {
895     CodedBitstreamH264Context *h264 = ctx->priv_data;
896     const H264RawSPS *sps = h264->active_sps;
897     int err, i, mopn;
898
899     if (current->slice_type % 5 != 2 &&
900         current->slice_type % 5 != 4) {
901         flag(ref_pic_list_modification_flag_l0);
902         if (current->ref_pic_list_modification_flag_l0) {
903             for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
904                 xue(modification_of_pic_nums_idc,
905                     current->rplm_l0[i].modification_of_pic_nums_idc, 0, 3, 0);
906
907                 mopn = current->rplm_l0[i].modification_of_pic_nums_idc;
908                 if (mopn == 3)
909                     break;
910
911                 if (mopn == 0 || mopn == 1)
912                     xue(abs_diff_pic_num_minus1,
913                         current->rplm_l0[i].abs_diff_pic_num_minus1,
914                         0, (1 + current->field_pic_flag) *
915                         (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
916                 else if (mopn == 2)
917                     xue(long_term_pic_num,
918                         current->rplm_l0[i].long_term_pic_num,
919                         0, sps->max_num_ref_frames - 1, 0);
920             }
921         }
922     }
923
924     if (current->slice_type % 5 == 1) {
925         flag(ref_pic_list_modification_flag_l1);
926         if (current->ref_pic_list_modification_flag_l1) {
927             for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
928                 xue(modification_of_pic_nums_idc,
929                     current->rplm_l1[i].modification_of_pic_nums_idc, 0, 3, 0);
930
931                 mopn = current->rplm_l1[i].modification_of_pic_nums_idc;
932                 if (mopn == 3)
933                     break;
934
935                 if (mopn == 0 || mopn == 1)
936                     xue(abs_diff_pic_num_minus1,
937                         current->rplm_l1[i].abs_diff_pic_num_minus1,
938                         0, (1 + current->field_pic_flag) *
939                         (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
940                 else if (mopn == 2)
941                     xue(long_term_pic_num,
942                         current->rplm_l1[i].long_term_pic_num,
943                         0, sps->max_num_ref_frames - 1, 0);
944             }
945         }
946     }
947
948     return 0;
949 }
950
951 static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
952                                    H264RawSliceHeader *current)
953 {
954     CodedBitstreamH264Context *h264 = ctx->priv_data;
955     const H264RawSPS *sps = h264->active_sps;
956     int chroma;
957     int err, i, j;
958
959     ue(luma_log2_weight_denom, 0, 7);
960
961     chroma = !sps->separate_colour_plane_flag && sps->chroma_format_idc != 0;
962     if (chroma)
963         ue(chroma_log2_weight_denom, 0, 7);
964
965     for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
966         flags(luma_weight_l0_flag[i], 1, i);
967         if (current->luma_weight_l0_flag[i]) {
968             ses(luma_weight_l0[i], -128, +127, 1, i);
969             ses(luma_offset_l0[i], -128, +127, 1, i);
970         }
971         if (chroma) {
972             flags(chroma_weight_l0_flag[i], 1, i);
973             if (current->chroma_weight_l0_flag[i]) {
974                 for (j = 0; j < 2; j++) {
975                     ses(chroma_weight_l0[i][j], -128, +127, 2, i, j);
976                     ses(chroma_offset_l0[i][j], -128, +127, 2, i, j);
977                 }
978             }
979         }
980     }
981
982     if (current->slice_type % 5 == 1) {
983         for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
984             flags(luma_weight_l1_flag[i], 1, i);
985             if (current->luma_weight_l1_flag[i]) {
986                 ses(luma_weight_l1[i], -128, +127, 1, i);
987                 ses(luma_offset_l1[i], -128, +127, 1, i);
988             }
989             if (chroma) {
990                 flags(chroma_weight_l1_flag[i], 1, i);
991                 if (current->chroma_weight_l1_flag[i]) {
992                     for (j = 0; j < 2; j++) {
993                         ses(chroma_weight_l1[i][j], -128, +127, 2, i, j);
994                         ses(chroma_offset_l1[i][j], -128, +127, 2, i, j);
995                     }
996                 }
997             }
998         }
999     }
1000
1001     return 0;
1002 }
1003
1004 static int FUNC(dec_ref_pic_marking)(CodedBitstreamContext *ctx, RWContext *rw,
1005                                      H264RawSliceHeader *current, int idr_pic_flag)
1006 {
1007     CodedBitstreamH264Context *h264 = ctx->priv_data;
1008     const H264RawSPS *sps = h264->active_sps;
1009     int err, i;
1010     uint32_t mmco;
1011
1012     if (idr_pic_flag) {
1013         flag(no_output_of_prior_pics_flag);
1014         flag(long_term_reference_flag);
1015     } else {
1016         flag(adaptive_ref_pic_marking_mode_flag);
1017         if (current->adaptive_ref_pic_marking_mode_flag) {
1018             for (i = 0; i < H264_MAX_MMCO_COUNT; i++) {
1019                 xue(memory_management_control_operation,
1020                     current->mmco[i].memory_management_control_operation,
1021                     0, 6, 0);
1022
1023                 mmco = current->mmco[i].memory_management_control_operation;
1024                 if (mmco == 0)
1025                     break;
1026
1027                 if (mmco == 1 || mmco == 3)
1028                     xue(difference_of_pic_nums_minus1,
1029                         current->mmco[i].difference_of_pic_nums_minus1,
1030                         0, INT32_MAX, 0);
1031                 if (mmco == 2)
1032                     xue(long_term_pic_num,
1033                         current->mmco[i].long_term_pic_num,
1034                         0, sps->max_num_ref_frames - 1, 0);
1035                 if (mmco == 3 || mmco == 6)
1036                     xue(long_term_frame_idx,
1037                         current->mmco[i].long_term_frame_idx,
1038                         0, sps->max_num_ref_frames - 1, 0);
1039                 if (mmco == 4)
1040                     xue(max_long_term_frame_idx_plus1,
1041                         current->mmco[i].max_long_term_frame_idx_plus1,
1042                         0, sps->max_num_ref_frames, 0);
1043             }
1044             if (i == H264_MAX_MMCO_COUNT) {
1045                 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many "
1046                        "memory management control operations.\n");
1047                 return AVERROR_INVALIDDATA;
1048             }
1049         }
1050     }
1051
1052     return 0;
1053 }
1054
1055 static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
1056                               H264RawSliceHeader *current)
1057 {
1058     CodedBitstreamH264Context *h264 = ctx->priv_data;
1059     const H264RawSPS *sps;
1060     const H264RawPPS *pps;
1061     int err;
1062     int idr_pic_flag;
1063     int slice_type_i, slice_type_p, slice_type_b;
1064     int slice_type_si, slice_type_sp;
1065
1066     HEADER("Slice Header");
1067
1068     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
1069                                 1 << H264_NAL_SLICE     |
1070                                 1 << H264_NAL_IDR_SLICE |
1071                                 1 << H264_NAL_AUXILIARY_SLICE));
1072
1073     if (current->nal_unit_header.nal_unit_type == H264_NAL_AUXILIARY_SLICE) {
1074         if (!h264->last_slice_nal_unit_type) {
1075             av_log(ctx->log_ctx, AV_LOG_ERROR, "Auxiliary slice "
1076                    "is not decodable without the main picture "
1077                    "in the same access unit.\n");
1078             return AVERROR_INVALIDDATA;
1079         }
1080     } else {
1081         h264->last_slice_nal_unit_type =
1082             current->nal_unit_header.nal_unit_type;
1083     }
1084     idr_pic_flag = h264->last_slice_nal_unit_type == H264_NAL_IDR_SLICE;
1085
1086     ue(first_mb_in_slice, 0, H264_MAX_MB_PIC_SIZE - 1);
1087     ue(slice_type, 0, 9);
1088
1089     slice_type_i  = current->slice_type % 5 == 2;
1090     slice_type_p  = current->slice_type % 5 == 0;
1091     slice_type_b  = current->slice_type % 5 == 1;
1092     slice_type_si = current->slice_type % 5 == 4;
1093     slice_type_sp = current->slice_type % 5 == 3;
1094
1095     if (idr_pic_flag && !(slice_type_i || slice_type_si)) {
1096         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid slice type %d "
1097                "for IDR picture.\n", current->slice_type);
1098         return AVERROR_INVALIDDATA;
1099     }
1100
1101     ue(pic_parameter_set_id, 0, 255);
1102
1103     pps = h264->pps[current->pic_parameter_set_id];
1104     if (!pps) {
1105         av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
1106                current->pic_parameter_set_id);
1107         return AVERROR_INVALIDDATA;
1108     }
1109     h264->active_pps = pps;
1110
1111     sps = h264->sps[pps->seq_parameter_set_id];
1112     if (!sps) {
1113         av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
1114                pps->seq_parameter_set_id);
1115         return AVERROR_INVALIDDATA;
1116     }
1117     h264->active_sps = sps;
1118
1119     if (sps->separate_colour_plane_flag)
1120         u(2, colour_plane_id, 0, 2);
1121
1122     u(sps->log2_max_frame_num_minus4 + 4, frame_num,
1123       0, MAX_UINT_BITS(sps->log2_max_frame_num_minus4 + 4));
1124
1125     if (!sps->frame_mbs_only_flag) {
1126         flag(field_pic_flag);
1127         if (current->field_pic_flag)
1128             flag(bottom_field_flag);
1129         else
1130             infer(bottom_field_flag, 0);
1131     } else {
1132         infer(field_pic_flag,    0);
1133         infer(bottom_field_flag, 0);
1134     }
1135
1136     if (idr_pic_flag)
1137         ue(idr_pic_id, 0, 65535);
1138
1139     if (sps->pic_order_cnt_type == 0) {
1140         u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, pic_order_cnt_lsb,
1141           0, MAX_UINT_BITS(sps->log2_max_pic_order_cnt_lsb_minus4 + 4));
1142         if (pps->bottom_field_pic_order_in_frame_present_flag &&
1143             !current->field_pic_flag)
1144             se(delta_pic_order_cnt_bottom, INT32_MIN + 1, INT32_MAX);
1145
1146     } else if (sps->pic_order_cnt_type == 1) {
1147         if (!sps->delta_pic_order_always_zero_flag) {
1148             se(delta_pic_order_cnt[0], INT32_MIN + 1, INT32_MAX);
1149             if (pps->bottom_field_pic_order_in_frame_present_flag &&
1150                 !current->field_pic_flag)
1151                 se(delta_pic_order_cnt[1], INT32_MIN + 1, INT32_MAX);
1152             else
1153                 infer(delta_pic_order_cnt[1], 0);
1154         } else {
1155             infer(delta_pic_order_cnt[0], 0);
1156             infer(delta_pic_order_cnt[1], 0);
1157         }
1158     }
1159
1160     if (pps->redundant_pic_cnt_present_flag)
1161         ue(redundant_pic_cnt, 0, 127);
1162
1163     if (slice_type_b)
1164         flag(direct_spatial_mv_pred_flag);
1165
1166     if (slice_type_p || slice_type_sp || slice_type_b) {
1167         flag(num_ref_idx_active_override_flag);
1168         if (current->num_ref_idx_active_override_flag) {
1169             ue(num_ref_idx_l0_active_minus1, 0, 31);
1170             if (slice_type_b)
1171                 ue(num_ref_idx_l1_active_minus1, 0, 31);
1172         } else {
1173             infer(num_ref_idx_l0_active_minus1,
1174                   pps->num_ref_idx_l0_default_active_minus1);
1175             infer(num_ref_idx_l1_active_minus1,
1176                   pps->num_ref_idx_l1_default_active_minus1);
1177         }
1178     }
1179
1180     if (current->nal_unit_header.nal_unit_type == 20 ||
1181         current->nal_unit_header.nal_unit_type == 21) {
1182         av_log(ctx->log_ctx, AV_LOG_ERROR, "MVC / 3DAVC not supported.\n");
1183         return AVERROR_PATCHWELCOME;
1184     } else {
1185         CHECK(FUNC(ref_pic_list_modification)(ctx, rw, current));
1186     }
1187
1188     if ((pps->weighted_pred_flag && (slice_type_p || slice_type_sp)) ||
1189         (pps->weighted_bipred_idc == 1 && slice_type_b)) {
1190         CHECK(FUNC(pred_weight_table)(ctx, rw, current));
1191     }
1192
1193     if (current->nal_unit_header.nal_ref_idc != 0) {
1194         CHECK(FUNC(dec_ref_pic_marking)(ctx, rw, current, idr_pic_flag));
1195     }
1196
1197     if (pps->entropy_coding_mode_flag &&
1198         !slice_type_i && !slice_type_si) {
1199         ue(cabac_init_idc, 0, 2);
1200     }
1201
1202     se(slice_qp_delta, - 51 - 6 * sps->bit_depth_luma_minus8,
1203                        + 51 + 6 * sps->bit_depth_luma_minus8);
1204     if (slice_type_sp || slice_type_si) {
1205         if (slice_type_sp)
1206             flag(sp_for_switch_flag);
1207         se(slice_qs_delta, -51, +51);
1208     }
1209
1210     if (pps->deblocking_filter_control_present_flag) {
1211         ue(disable_deblocking_filter_idc, 0, 2);
1212         if (current->disable_deblocking_filter_idc != 1) {
1213             se(slice_alpha_c0_offset_div2, -6, +6);
1214             se(slice_beta_offset_div2,     -6, +6);
1215         } else {
1216             infer(slice_alpha_c0_offset_div2, 0);
1217             infer(slice_beta_offset_div2,     0);
1218         }
1219     } else {
1220         infer(disable_deblocking_filter_idc, 0);
1221         infer(slice_alpha_c0_offset_div2,    0);
1222         infer(slice_beta_offset_div2,        0);
1223     }
1224
1225     if (pps->num_slice_groups_minus1 > 0 &&
1226         pps->slice_group_map_type >= 3 &&
1227         pps->slice_group_map_type <= 5) {
1228         unsigned int pic_size, max, bits;
1229
1230         pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
1231                    (sps->pic_height_in_map_units_minus1 + 1);
1232         max = (pic_size + pps->slice_group_change_rate_minus1) /
1233               (pps->slice_group_change_rate_minus1 + 1);
1234         bits = av_log2(2 * max - 1);
1235
1236         u(bits, slice_group_change_cycle, 0, max);
1237     }
1238
1239     if (pps->entropy_coding_mode_flag) {
1240         while (byte_alignment(rw))
1241             fixed(1, cabac_alignment_one_bit, 1);
1242     }
1243
1244     return 0;
1245 }
1246
1247 static int FUNC(filler)(CodedBitstreamContext *ctx, RWContext *rw,
1248                         H264RawFiller *current)
1249 {
1250     int err;
1251
1252     HEADER("Filler Data");
1253
1254     CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
1255                                 1 << H264_NAL_FILLER_DATA));
1256
1257 #ifdef READ
1258     while (show_bits(rw, 8) == 0xff) {
1259         fixed(8, ff_byte, 0xff);
1260         ++current->filler_size;
1261     }
1262 #else
1263     {
1264         uint32_t i;
1265         for (i = 0; i < current->filler_size; i++)
1266             fixed(8, ff_byte, 0xff);
1267     }
1268 #endif
1269
1270     CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
1271
1272     return 0;
1273 }