1 /**************************************************************************
3 * Copyright 2013 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Christian König <christian.koenig@amd.com>
34 #include "pipe/p_video_codec.h"
35 #include "util/u_memory.h"
36 #include "vl/vl_rbsp.h"
38 #include "entrypoint.h"
41 #define DPB_MAX_SIZE 5
44 struct list_head list
;
45 struct pipe_video_buffer
*buffer
;
49 static const uint8_t Default_4x4_Intra
[16] = {
50 6, 13, 13, 20, 20, 20, 28, 28,
51 28, 28, 32, 32, 32, 37, 37, 42
54 static const uint8_t Default_4x4_Inter
[16] = {
55 10, 14, 14, 20, 20, 20, 24, 24,
56 24, 24, 27, 27, 27, 30, 30, 34
59 static const uint8_t Default_8x8_Intra
[64] = {
60 6, 10, 10, 13, 11, 13, 16, 16,
61 16, 16, 18, 18, 18, 18, 18, 23,
62 23, 23, 23, 23, 23, 25, 25, 25,
63 25, 25, 25, 25, 27, 27, 27, 27,
64 27, 27, 27, 27, 29, 29, 29, 29,
65 29, 29, 29, 31, 31, 31, 31, 31,
66 31, 33, 33, 33, 33, 33, 36, 36,
67 36, 36, 38, 38, 38, 40, 40, 42
70 static const uint8_t Default_8x8_Inter
[64] = {
71 9, 13, 13, 15, 13, 15, 17, 17,
72 17, 17, 19, 19, 19, 19, 19, 21,
73 21, 21, 21, 21, 21, 22, 22, 22,
74 22, 22, 22, 22, 24, 24, 24, 24,
75 24, 24, 24, 24, 25, 25, 25, 25,
76 25, 25, 25, 27, 27, 27, 27, 27,
77 27, 28, 28, 28, 28, 28, 30, 30,
78 30, 30, 32, 32, 32, 33, 33, 35
81 static void vid_dec_h264_Decode(vid_dec_PrivateType
*priv
, struct vl_vlc
*vlc
, unsigned min_bits_left
);
82 static void vid_dec_h264_EndFrame(vid_dec_PrivateType
*priv
);
83 static struct pipe_video_buffer
*vid_dec_h264_Flush(vid_dec_PrivateType
*priv
);
85 void vid_dec_h264_Init(vid_dec_PrivateType
*priv
)
87 priv
->picture
.base
.profile
= PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH
;
89 priv
->Decode
= vid_dec_h264_Decode
;
90 priv
->EndFrame
= vid_dec_h264_EndFrame
;
91 priv
->Flush
= vid_dec_h264_Flush
;
93 LIST_INITHEAD(&priv
->codec_data
.h264
.dpb_list
);
94 priv
->picture
.h264
.field_order_cnt
[0] = priv
->picture
.h264
.field_order_cnt
[1] = INT_MAX
;
97 static void vid_dec_h264_BeginFrame(vid_dec_PrivateType
*priv
)
99 //TODO: sane buffer handling
101 if (priv
->frame_started
)
104 vid_dec_NeedTarget(priv
);
106 priv
->picture
.h264
.num_ref_frames
= priv
->picture
.h264
.pps
->sps
->max_num_ref_frames
;
108 priv
->codec
->begin_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
109 priv
->frame_started
= true;
112 static struct pipe_video_buffer
*vid_dec_h264_Flush(vid_dec_PrivateType
*priv
)
114 struct dpb_list
*entry
, *result
= NULL
;
115 struct pipe_video_buffer
*buf
;
117 /* search for the lowest poc and break on zeros */
118 LIST_FOR_EACH_ENTRY(entry
, &priv
->codec_data
.h264
.dpb_list
, list
) {
120 if (result
&& entry
->poc
== 0)
123 if (!result
|| entry
->poc
< result
->poc
)
130 buf
= result
->buffer
;
132 --priv
->codec_data
.h264
.dpb_num
;
133 LIST_DEL(&result
->list
);
139 static void vid_dec_h264_EndFrame(vid_dec_PrivateType
*priv
)
141 struct dpb_list
*entry
;
142 struct pipe_video_buffer
*tmp
;
143 bool top_field_first
;
145 if (!priv
->frame_started
)
148 priv
->codec
->end_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
149 priv
->frame_started
= false;
151 // TODO: implement frame number handling
152 priv
->picture
.h264
.frame_num_list
[0] = priv
->picture
.h264
.frame_num
;
153 priv
->picture
.h264
.field_order_cnt_list
[0][0] = priv
->picture
.h264
.frame_num
;
154 priv
->picture
.h264
.field_order_cnt_list
[0][1] = priv
->picture
.h264
.frame_num
;
156 top_field_first
= priv
->picture
.h264
.field_order_cnt
[0] < priv
->picture
.h264
.field_order_cnt
[1];
158 if (priv
->picture
.h264
.field_pic_flag
&& priv
->picture
.h264
.bottom_field_flag
!= top_field_first
)
161 /* add the decoded picture to the dpb list */
162 entry
= CALLOC_STRUCT(dpb_list
);
166 entry
->buffer
= priv
->target
;
167 entry
->poc
= MIN2(priv
->picture
.h264
.field_order_cnt
[0], priv
->picture
.h264
.field_order_cnt
[1]);
168 LIST_ADDTAIL(&entry
->list
, &priv
->codec_data
.h264
.dpb_list
);
169 ++priv
->codec_data
.h264
.dpb_num
;
171 priv
->picture
.h264
.field_order_cnt
[0] = priv
->picture
.h264
.field_order_cnt
[1] = INT_MAX
;
173 if (priv
->codec_data
.h264
.dpb_num
<= DPB_MAX_SIZE
)
176 tmp
= priv
->in_buffers
[0]->pInputPortPrivate
;
177 priv
->in_buffers
[0]->pInputPortPrivate
= vid_dec_h264_Flush(priv
);
179 priv
->frame_finished
= priv
->in_buffers
[0]->pInputPortPrivate
!= NULL
;
182 static void vui_parameters(struct vl_rbsp
*rbsp
)
187 static void scaling_list(struct vl_rbsp
*rbsp
, uint8_t *scalingList
, unsigned sizeOfScalingList
,
188 const uint8_t *defaultList
, const uint8_t *fallbackList
)
190 unsigned lastScale
= 8, nextScale
= 8;
193 /* (pic|seq)_scaling_list_present_flag[i] */
194 if (!vl_rbsp_u(rbsp
, 1)) {
196 memcpy(scalingList
, fallbackList
, sizeOfScalingList
);
200 for (i
= 0; i
< sizeOfScalingList
; ++i
) {
202 if (nextScale
!= 0) {
203 signed delta_scale
= vl_rbsp_se(rbsp
);
204 nextScale
= (lastScale
+ delta_scale
+ 256) % 256;
205 if (i
== 0 && nextScale
== 0) {
206 memcpy(scalingList
, defaultList
, sizeOfScalingList
);
210 scalingList
[i
] = nextScale
== 0 ? lastScale
: nextScale
;
211 lastScale
= scalingList
[i
];
215 static struct pipe_h264_sps
*seq_parameter_set_id(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
217 unsigned id
= vl_rbsp_ue(rbsp
);
218 if (id
>= Elements(priv
->codec_data
.h264
.sps
))
219 return NULL
; /* invalid seq_parameter_set_id */
221 return &priv
->codec_data
.h264
.sps
[id
];
224 static void seq_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
226 struct pipe_h264_sps
*sps
;
227 unsigned profile_idc
;
230 /* Sequence parameter set */
231 profile_idc
= vl_rbsp_u(rbsp
, 8);
233 /* constraint_set0_flag */
236 /* constraint_set1_flag */
239 /* constraint_set2_flag */
242 /* constraint_set3_flag */
245 /* constraint_set4_flag */
248 /* constraint_set5_flag */
251 /* reserved_zero_2bits */
257 sps
= seq_parameter_set_id(priv
, rbsp
);
261 memset(sps
, 0, sizeof(*sps
));
262 memset(sps
->ScalingList4x4
, 16, sizeof(sps
->ScalingList4x4
));
263 memset(sps
->ScalingList8x8
, 16, sizeof(sps
->ScalingList8x8
));
265 if (profile_idc
== 100 || profile_idc
== 110 || profile_idc
== 122 || profile_idc
== 244 ||
266 profile_idc
== 44 || profile_idc
== 83 || profile_idc
== 86 || profile_idc
== 118 ||
267 profile_idc
== 128 || profile_idc
== 138) {
269 sps
->chroma_format_idc
= vl_rbsp_ue(rbsp
);
271 if (sps
->chroma_format_idc
== 3)
272 sps
->separate_colour_plane_flag
= vl_rbsp_u(rbsp
, 1);
274 sps
->bit_depth_luma_minus8
= vl_rbsp_ue(rbsp
);
276 sps
->bit_depth_chroma_minus8
= vl_rbsp_ue(rbsp
);
278 /* qpprime_y_zero_transform_bypass_flag */
281 sps
->seq_scaling_matrix_present_flag
= vl_rbsp_u(rbsp
, 1);
282 if (sps
->seq_scaling_matrix_present_flag
) {
284 scaling_list(rbsp
, sps
->ScalingList4x4
[0], 16, Default_4x4_Intra
, Default_4x4_Intra
);
285 scaling_list(rbsp
, sps
->ScalingList4x4
[1], 16, Default_4x4_Intra
, sps
->ScalingList4x4
[0]);
286 scaling_list(rbsp
, sps
->ScalingList4x4
[2], 16, Default_4x4_Intra
, sps
->ScalingList4x4
[1]);
287 scaling_list(rbsp
, sps
->ScalingList4x4
[3], 16, Default_4x4_Inter
, Default_4x4_Inter
);
288 scaling_list(rbsp
, sps
->ScalingList4x4
[4], 16, Default_4x4_Inter
, sps
->ScalingList4x4
[3]);
289 scaling_list(rbsp
, sps
->ScalingList4x4
[5], 16, Default_4x4_Inter
, sps
->ScalingList4x4
[4]);
291 scaling_list(rbsp
, sps
->ScalingList8x8
[0], 64, Default_8x8_Intra
, Default_8x8_Intra
);
292 scaling_list(rbsp
, sps
->ScalingList8x8
[1], 64, Default_8x8_Inter
, Default_8x8_Inter
);
293 if (sps
->chroma_format_idc
== 3) {
294 scaling_list(rbsp
, sps
->ScalingList8x8
[2], 64, Default_8x8_Intra
, sps
->ScalingList8x8
[0]);
295 scaling_list(rbsp
, sps
->ScalingList8x8
[3], 64, Default_8x8_Inter
, sps
->ScalingList8x8
[1]);
296 scaling_list(rbsp
, sps
->ScalingList8x8
[4], 64, Default_8x8_Intra
, sps
->ScalingList8x8
[2]);
297 scaling_list(rbsp
, sps
->ScalingList8x8
[5], 64, Default_8x8_Inter
, sps
->ScalingList8x8
[3]);
300 } else if (profile_idc
== 183)
301 sps
->chroma_format_idc
= 0;
303 sps
->chroma_format_idc
= 1;
305 sps
->log2_max_frame_num_minus4
= vl_rbsp_ue(rbsp
);
307 sps
->pic_order_cnt_type
= vl_rbsp_ue(rbsp
);
309 if (sps
->pic_order_cnt_type
== 0)
310 sps
->log2_max_pic_order_cnt_lsb_minus4
= vl_rbsp_ue(rbsp
);
311 else if (sps
->pic_order_cnt_type
== 1) {
312 sps
->delta_pic_order_always_zero_flag
= vl_rbsp_u(rbsp
, 1);
314 sps
->offset_for_non_ref_pic
= vl_rbsp_se(rbsp
);
316 sps
->offset_for_top_to_bottom_field
= vl_rbsp_se(rbsp
);
318 sps
->num_ref_frames_in_pic_order_cnt_cycle
= vl_rbsp_ue(rbsp
);
320 for (i
= 0; i
< sps
->num_ref_frames_in_pic_order_cnt_cycle
; ++i
)
321 sps
->offset_for_ref_frame
[i
] = vl_rbsp_se(rbsp
);
324 sps
->max_num_ref_frames
= vl_rbsp_ue(rbsp
);
326 /* gaps_in_frame_num_value_allowed_flag */
329 /* pic_width_in_mbs_minus1 */
332 /* pic_height_in_map_units_minus1 */
335 sps
->frame_mbs_only_flag
= vl_rbsp_u(rbsp
, 1);
336 if (!sps
->frame_mbs_only_flag
)
337 sps
->mb_adaptive_frame_field_flag
= vl_rbsp_u(rbsp
, 1);
339 sps
->direct_8x8_inference_flag
= vl_rbsp_u(rbsp
, 1);
341 /* frame_cropping_flag */
342 if (vl_rbsp_u(rbsp
, 1)) {
343 /* frame_crop_left_offset */
346 /* frame_crop_right_offset */
349 /* frame_crop_top_offset */
352 /* frame_crop_bottom_offset */
356 /* vui_parameters_present_flag */
357 if (vl_rbsp_u(rbsp
, 1))
358 vui_parameters(rbsp
);
361 static struct pipe_h264_pps
*pic_parameter_set_id(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
363 unsigned id
= vl_rbsp_ue(rbsp
);
364 if (id
>= Elements(priv
->codec_data
.h264
.pps
))
365 return NULL
; /* invalid pic_parameter_set_id */
367 return &priv
->codec_data
.h264
.pps
[id
];
370 static void picture_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
372 struct pipe_h264_sps
*sps
;
373 struct pipe_h264_pps
*pps
;
376 pps
= pic_parameter_set_id(priv
, rbsp
);
380 memset(pps
, 0, sizeof(*pps
));
382 sps
= pps
->sps
= seq_parameter_set_id(priv
, rbsp
);
386 memcpy(pps
->ScalingList4x4
, sps
->ScalingList4x4
, sizeof(pps
->ScalingList4x4
));
387 memcpy(pps
->ScalingList8x8
, sps
->ScalingList8x8
, sizeof(pps
->ScalingList8x8
));
389 pps
->entropy_coding_mode_flag
= vl_rbsp_u(rbsp
, 1);
391 pps
->bottom_field_pic_order_in_frame_present_flag
= vl_rbsp_u(rbsp
, 1);
393 pps
->num_slice_groups_minus1
= vl_rbsp_ue(rbsp
);
394 if (pps
->num_slice_groups_minus1
> 0) {
395 pps
->slice_group_map_type
= vl_rbsp_ue(rbsp
);
397 if (pps
->slice_group_map_type
== 0) {
399 for (i
= 0; i
<= pps
->num_slice_groups_minus1
; ++i
)
400 /* run_length_minus1[i] */
403 } else if (pps
->slice_group_map_type
== 2) {
405 for (i
= 0; i
<= pps
->num_slice_groups_minus1
; ++i
) {
409 /* bottom_right[i] */
413 } else if (pps
->slice_group_map_type
>= 3 && pps
->slice_group_map_type
<= 5) {
415 /* slice_group_change_direction_flag */
418 pps
->slice_group_change_rate_minus1
= vl_rbsp_ue(rbsp
);
420 } else if (pps
->slice_group_map_type
== 6) {
422 unsigned pic_size_in_map_units_minus1
;
424 pic_size_in_map_units_minus1
= vl_rbsp_ue(rbsp
);
426 for (i
= 0; i
<= pic_size_in_map_units_minus1
; ++i
)
427 /* slice_group_id[i] */
428 vl_rbsp_u(rbsp
, log2(pps
->num_slice_groups_minus1
+ 1));
432 pps
->num_ref_idx_l0_default_active_minus1
= vl_rbsp_ue(rbsp
);
434 pps
->num_ref_idx_l1_default_active_minus1
= vl_rbsp_ue(rbsp
);
436 pps
->weighted_pred_flag
= vl_rbsp_u(rbsp
, 1);
438 pps
->weighted_bipred_idc
= vl_rbsp_u(rbsp
, 2);
440 pps
->pic_init_qp_minus26
= vl_rbsp_se(rbsp
);
442 /* pic_init_qs_minus26 */
445 pps
->chroma_qp_index_offset
= vl_rbsp_se(rbsp
);
447 pps
->deblocking_filter_control_present_flag
= vl_rbsp_u(rbsp
, 1);
449 pps
->constrained_intra_pred_flag
= vl_rbsp_u(rbsp
, 1);
451 pps
->redundant_pic_cnt_present_flag
= vl_rbsp_u(rbsp
, 1);
453 if (vl_rbsp_more_data(rbsp
)) {
454 pps
->transform_8x8_mode_flag
= vl_rbsp_u(rbsp
, 1);
456 /* pic_scaling_matrix_present_flag */
457 if (vl_rbsp_u(rbsp
, 1)) {
459 scaling_list(rbsp
, pps
->ScalingList4x4
[0], 16, Default_4x4_Intra
,
460 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_4x4_Intra
);
461 scaling_list(rbsp
, pps
->ScalingList4x4
[1], 16, Default_4x4_Intra
, pps
->ScalingList4x4
[0]);
462 scaling_list(rbsp
, pps
->ScalingList4x4
[2], 16, Default_4x4_Intra
, pps
->ScalingList4x4
[1]);
463 scaling_list(rbsp
, pps
->ScalingList4x4
[3], 16, Default_4x4_Inter
,
464 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_4x4_Inter
);
465 scaling_list(rbsp
, pps
->ScalingList4x4
[4], 16, Default_4x4_Inter
, pps
->ScalingList4x4
[3]);
466 scaling_list(rbsp
, pps
->ScalingList4x4
[5], 16, Default_4x4_Inter
, pps
->ScalingList4x4
[4]);
468 if (pps
->transform_8x8_mode_flag
) {
469 scaling_list(rbsp
, pps
->ScalingList8x8
[0], 64, Default_8x8_Intra
,
470 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_8x8_Intra
);
471 scaling_list(rbsp
, pps
->ScalingList8x8
[1], 64, Default_8x8_Inter
,
472 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_8x8_Inter
);
473 if (sps
->chroma_format_idc
== 3) {
474 scaling_list(rbsp
, pps
->ScalingList8x8
[2], 64, Default_8x8_Intra
, pps
->ScalingList8x8
[0]);
475 scaling_list(rbsp
, pps
->ScalingList8x8
[3], 64, Default_8x8_Inter
, pps
->ScalingList8x8
[1]);
476 scaling_list(rbsp
, pps
->ScalingList8x8
[4], 64, Default_8x8_Intra
, pps
->ScalingList8x8
[2]);
477 scaling_list(rbsp
, pps
->ScalingList8x8
[5], 64, Default_8x8_Inter
, pps
->ScalingList8x8
[3]);
482 pps
->second_chroma_qp_index_offset
= vl_rbsp_se(rbsp
);
486 static void ref_pic_list_mvc_modification(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
492 static void ref_pic_list_modification(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
493 enum pipe_h264_slice_type slice_type
)
495 unsigned modification_of_pic_nums_idc
;
497 if (slice_type
!= 2 && slice_type
!= 4) {
498 /* ref_pic_list_modification_flag_l0 */
499 if (vl_rbsp_u(rbsp
, 1)) {
501 modification_of_pic_nums_idc
= vl_rbsp_ue(rbsp
);
502 if (modification_of_pic_nums_idc
== 0 ||
503 modification_of_pic_nums_idc
== 1)
504 /* abs_diff_pic_num_minus1 */
506 else if (modification_of_pic_nums_idc
== 2)
507 /* long_term_pic_num */
509 } while (modification_of_pic_nums_idc
!= 3);
513 if (slice_type
== 1) {
514 /* ref_pic_list_modification_flag_l1 */
515 if (vl_rbsp_u(rbsp
, 1)) {
517 modification_of_pic_nums_idc
= vl_rbsp_ue(rbsp
);
518 if (modification_of_pic_nums_idc
== 0 ||
519 modification_of_pic_nums_idc
== 1)
520 /* abs_diff_pic_num_minus1 */
522 else if (modification_of_pic_nums_idc
== 2)
523 /* long_term_pic_num */
525 } while (modification_of_pic_nums_idc
!= 3);
530 static void pred_weight_table(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
531 struct pipe_h264_sps
*sps
, enum pipe_h264_slice_type slice_type
)
533 unsigned ChromaArrayType
= sps
->separate_colour_plane_flag
? 0 : sps
->chroma_format_idc
;
536 /* luma_log2_weight_denom */
539 if (ChromaArrayType
!= 0)
540 /* chroma_log2_weight_denom */
543 for (i
= 0; i
<= priv
->picture
.h264
.num_ref_idx_l0_active_minus1
; ++i
) {
544 /* luma_weight_l0_flag */
545 if (vl_rbsp_u(rbsp
, 1)) {
546 /* luma_weight_l0[i] */
548 /* luma_offset_l0[i] */
551 if (ChromaArrayType
!= 0) {
552 /* chroma_weight_l0_flag */
553 if (vl_rbsp_u(rbsp
, 1)) {
554 for (j
= 0; j
< 2; ++j
) {
555 /* chroma_weight_l0[i][j] */
557 /* chroma_offset_l0[i][j] */
564 if (slice_type
== 1) {
565 for (i
= 0; i
<= priv
->picture
.h264
.num_ref_idx_l1_active_minus1
; ++i
) {
566 /* luma_weight_l1_flag */
567 if (vl_rbsp_u(rbsp
, 1)) {
568 /* luma_weight_l1[i] */
570 /* luma_offset_l1[i] */
573 if (ChromaArrayType
!= 0) {
574 /* chroma_weight_l1_flag */
575 if (vl_rbsp_u(rbsp
, 1)) {
576 for (j
= 0; j
< 2; ++j
) {
577 /* chroma_weight_l1[i][j] */
579 /* chroma_offset_l1[i][j] */
588 static void dec_ref_pic_marking(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
591 unsigned memory_management_control_operation
;
594 /* no_output_of_prior_pics_flag */
596 /* long_term_reference_flag */
599 /* adaptive_ref_pic_marking_mode_flag */
600 if (vl_rbsp_u(rbsp
, 1)) {
602 memory_management_control_operation
= vl_rbsp_ue(rbsp
);
604 if (memory_management_control_operation
== 1 ||
605 memory_management_control_operation
== 3)
606 /* difference_of_pic_nums_minus1 */
609 if (memory_management_control_operation
== 2)
610 /* long_term_pic_num */
613 if (memory_management_control_operation
== 3 ||
614 memory_management_control_operation
== 6)
615 /* long_term_frame_idx */
618 if (memory_management_control_operation
== 4)
619 /* max_long_term_frame_idx_plus1 */
621 } while (memory_management_control_operation
!= 0);
626 static void slice_header(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
627 unsigned nal_ref_idc
, unsigned nal_unit_type
)
629 enum pipe_h264_slice_type slice_type
;
630 struct pipe_h264_pps
*pps
;
631 struct pipe_h264_sps
*sps
;
632 unsigned frame_num
, prevFrameNum
;
633 bool IdrPicFlag
= nal_unit_type
== 5;
635 if (IdrPicFlag
!= priv
->codec_data
.h264
.IdrPicFlag
)
636 vid_dec_h264_EndFrame(priv
);
638 priv
->codec_data
.h264
.IdrPicFlag
= IdrPicFlag
;
640 /* first_mb_in_slice */
643 slice_type
= vl_rbsp_ue(rbsp
) % 5;
645 pps
= pic_parameter_set_id(priv
, rbsp
);
653 if (pps
!= priv
->picture
.h264
.pps
)
654 vid_dec_h264_EndFrame(priv
);
656 priv
->picture
.h264
.pps
= pps
;
658 if (sps
->separate_colour_plane_flag
== 1 )
659 /* colour_plane_id */
662 frame_num
= vl_rbsp_u(rbsp
, sps
->log2_max_frame_num_minus4
+ 4);
664 if (frame_num
!= priv
->picture
.h264
.frame_num
)
665 vid_dec_h264_EndFrame(priv
);
667 prevFrameNum
= priv
->picture
.h264
.frame_num
;
668 priv
->picture
.h264
.frame_num
= frame_num
;
670 priv
->picture
.h264
.field_pic_flag
= 0;
671 priv
->picture
.h264
.bottom_field_flag
= 0;
673 if (!sps
->frame_mbs_only_flag
) {
674 unsigned field_pic_flag
= vl_rbsp_u(rbsp
, 1);
676 if (!field_pic_flag
&& field_pic_flag
!= priv
->picture
.h264
.field_pic_flag
)
677 vid_dec_h264_EndFrame(priv
);
679 priv
->picture
.h264
.field_pic_flag
= field_pic_flag
;
681 if (priv
->picture
.h264
.field_pic_flag
) {
682 unsigned bottom_field_flag
= vl_rbsp_u(rbsp
, 1);
684 if (bottom_field_flag
!= priv
->picture
.h264
.bottom_field_flag
)
685 vid_dec_h264_EndFrame(priv
);
687 priv
->picture
.h264
.bottom_field_flag
= bottom_field_flag
;
692 unsigned idr_pic_id
= vl_rbsp_ue(rbsp
);
694 if (idr_pic_id
!= priv
->codec_data
.h264
.idr_pic_id
)
695 vid_dec_h264_EndFrame(priv
);
697 priv
->codec_data
.h264
.idr_pic_id
= idr_pic_id
;
700 if (sps
->pic_order_cnt_type
== 0) {
701 unsigned log2_max_pic_order_cnt_lsb
= sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4;
702 unsigned max_pic_order_cnt_lsb
= 1 << log2_max_pic_order_cnt_lsb
;
703 unsigned pic_order_cnt_lsb
= vl_rbsp_u(rbsp
, log2_max_pic_order_cnt_lsb
);
704 unsigned pic_order_cnt_msb
;
706 if (pic_order_cnt_lsb
!= priv
->codec_data
.h264
.pic_order_cnt_lsb
)
707 vid_dec_h264_EndFrame(priv
);
710 priv
->codec_data
.h264
.pic_order_cnt_msb
= 0;
711 priv
->codec_data
.h264
.pic_order_cnt_lsb
= 0;
714 if ((pic_order_cnt_lsb
< priv
->codec_data
.h264
.pic_order_cnt_lsb
) &&
715 (priv
->codec_data
.h264
.pic_order_cnt_lsb
- pic_order_cnt_lsb
) >= (max_pic_order_cnt_lsb
/ 2))
716 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
+ max_pic_order_cnt_lsb
;
718 else if ((pic_order_cnt_lsb
> priv
->codec_data
.h264
.pic_order_cnt_lsb
) &&
719 (pic_order_cnt_lsb
- priv
->codec_data
.h264
.pic_order_cnt_lsb
) > (max_pic_order_cnt_lsb
/ 2))
720 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
- max_pic_order_cnt_lsb
;
723 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
;
725 priv
->codec_data
.h264
.pic_order_cnt_msb
= pic_order_cnt_msb
;
726 priv
->codec_data
.h264
.pic_order_cnt_lsb
= pic_order_cnt_lsb
;
728 if (pps
->bottom_field_pic_order_in_frame_present_flag
&& !priv
->picture
.h264
.field_pic_flag
) {
729 unsigned delta_pic_order_cnt_bottom
= vl_rbsp_se(rbsp
);
731 if (delta_pic_order_cnt_bottom
!= priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
)
732 vid_dec_h264_EndFrame(priv
);
734 priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
= delta_pic_order_cnt_bottom
;
737 priv
->picture
.h264
.field_order_cnt
[0] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
738 priv
->picture
.h264
.field_order_cnt
[1] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
739 if (!priv
->picture
.h264
.field_pic_flag
)
740 priv
->picture
.h264
.field_order_cnt
[1] += priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
;
742 } else if (sps
->pic_order_cnt_type
== 1) {
743 unsigned MaxFrameNum
= 1 << (sps
->log2_max_frame_num_minus4
+ 4);
744 unsigned FrameNumOffset
, absFrameNum
, expectedPicOrderCnt
;
746 if (!sps
->delta_pic_order_always_zero_flag
) {
747 unsigned delta_pic_order_cnt
[2];
749 delta_pic_order_cnt
[0] = vl_rbsp_se(rbsp
);
751 if (delta_pic_order_cnt
[0] != priv
->codec_data
.h264
.delta_pic_order_cnt
[0])
752 vid_dec_h264_EndFrame(priv
);
754 priv
->codec_data
.h264
.delta_pic_order_cnt
[0] = delta_pic_order_cnt
[0];
756 if (pps
->bottom_field_pic_order_in_frame_present_flag
&& !priv
->picture
.h264
.field_pic_flag
) {
757 delta_pic_order_cnt
[1] = vl_rbsp_se(rbsp
);
759 if (delta_pic_order_cnt
[1] != priv
->codec_data
.h264
.delta_pic_order_cnt
[1])
760 vid_dec_h264_EndFrame(priv
);
762 priv
->codec_data
.h264
.delta_pic_order_cnt
[1] = delta_pic_order_cnt
[1];
768 else if (prevFrameNum
> frame_num
)
769 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
+ MaxFrameNum
;
771 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
;
773 priv
->codec_data
.h264
.prevFrameNumOffset
= FrameNumOffset
;
775 if (sps
->num_ref_frames_in_pic_order_cnt_cycle
!= 0)
776 absFrameNum
= FrameNumOffset
+ frame_num
;
780 if (nal_ref_idc
== 0 && absFrameNum
> 0)
781 absFrameNum
= absFrameNum
- 1;
783 if (absFrameNum
> 0) {
784 unsigned picOrderCntCycleCnt
= (absFrameNum
- 1) / sps
->num_ref_frames_in_pic_order_cnt_cycle
;
785 unsigned frameNumInPicOrderCntCycle
= (absFrameNum
- 1) % sps
->num_ref_frames_in_pic_order_cnt_cycle
;
786 signed ExpectedDeltaPerPicOrderCntCycle
= 0;
789 for (i
= 0; i
< sps
->num_ref_frames_in_pic_order_cnt_cycle
; ++i
)
790 ExpectedDeltaPerPicOrderCntCycle
+= sps
->offset_for_ref_frame
[i
];
792 expectedPicOrderCnt
= picOrderCntCycleCnt
* ExpectedDeltaPerPicOrderCntCycle
;
793 for (i
= 0; i
<= frameNumInPicOrderCntCycle
; ++i
)
794 expectedPicOrderCnt
+= sps
->offset_for_ref_frame
[i
];
797 expectedPicOrderCnt
= 0;
799 if (nal_ref_idc
== 0)
800 expectedPicOrderCnt
+= sps
->offset_for_non_ref_pic
;
802 if (!priv
->picture
.h264
.field_pic_flag
) {
803 priv
->picture
.h264
.field_order_cnt
[0] = expectedPicOrderCnt
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
804 priv
->picture
.h264
.field_order_cnt
[1] = priv
->picture
.h264
.field_order_cnt
[0] +
805 sps
->offset_for_top_to_bottom_field
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[1];
807 } else if (!priv
->picture
.h264
.bottom_field_flag
)
808 priv
->picture
.h264
.field_order_cnt
[0] = expectedPicOrderCnt
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
810 priv
->picture
.h264
.field_order_cnt
[1] = expectedPicOrderCnt
+ sps
->offset_for_top_to_bottom_field
+
811 priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
813 } else if (sps
->pic_order_cnt_type
== 2) {
814 unsigned MaxFrameNum
= 1 << (sps
->log2_max_frame_num_minus4
+ 4);
815 unsigned FrameNumOffset
, tempPicOrderCnt
;
819 else if (prevFrameNum
> frame_num
)
820 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
+ MaxFrameNum
;
822 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
;
824 priv
->codec_data
.h264
.prevFrameNumOffset
= FrameNumOffset
;
828 else if (nal_ref_idc
== 0)
829 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
) - 1;
831 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
);
833 if (!priv
->picture
.h264
.field_pic_flag
) {
834 priv
->picture
.h264
.field_order_cnt
[0] = tempPicOrderCnt
;
835 priv
->picture
.h264
.field_order_cnt
[1] = tempPicOrderCnt
;
837 } else if (!priv
->picture
.h264
.bottom_field_flag
)
838 priv
->picture
.h264
.field_order_cnt
[0] = tempPicOrderCnt
;
840 priv
->picture
.h264
.field_order_cnt
[1] = tempPicOrderCnt
;
843 if (pps
->redundant_pic_cnt_present_flag
)
844 /* redundant_pic_cnt */
847 if (slice_type
== PIPE_H264_SLICE_TYPE_B
)
848 /* direct_spatial_mv_pred_flag */
851 priv
->picture
.h264
.num_ref_idx_l0_active_minus1
= pps
->num_ref_idx_l0_default_active_minus1
;
852 priv
->picture
.h264
.num_ref_idx_l1_active_minus1
= pps
->num_ref_idx_l1_default_active_minus1
;
854 if (slice_type
== PIPE_H264_SLICE_TYPE_P
||
855 slice_type
== PIPE_H264_SLICE_TYPE_SP
||
856 slice_type
== PIPE_H264_SLICE_TYPE_B
) {
858 /* num_ref_idx_active_override_flag */
859 if (vl_rbsp_u(rbsp
, 1)) {
860 priv
->picture
.h264
.num_ref_idx_l0_active_minus1
= vl_rbsp_ue(rbsp
);
862 if (slice_type
== PIPE_H264_SLICE_TYPE_B
)
863 priv
->picture
.h264
.num_ref_idx_l1_active_minus1
= vl_rbsp_ue(rbsp
);
867 if (nal_unit_type
== 20 || nal_unit_type
== 21)
868 ref_pic_list_mvc_modification(priv
, rbsp
);
870 ref_pic_list_modification(priv
, rbsp
, slice_type
);
872 if ((pps
->weighted_pred_flag
&& (slice_type
== PIPE_H264_SLICE_TYPE_P
|| slice_type
== PIPE_H264_SLICE_TYPE_SP
)) ||
873 (pps
->weighted_bipred_idc
== 1 && slice_type
== PIPE_H264_SLICE_TYPE_B
))
874 pred_weight_table(priv
, rbsp
, sps
, slice_type
);
876 if (nal_ref_idc
!= 0)
877 dec_ref_pic_marking(priv
, rbsp
, IdrPicFlag
);
879 if (pps
->entropy_coding_mode_flag
&& slice_type
!= PIPE_H264_SLICE_TYPE_I
&& slice_type
!= PIPE_H264_SLICE_TYPE_SI
)
886 if (slice_type
== PIPE_H264_SLICE_TYPE_SP
|| slice_type
== PIPE_H264_SLICE_TYPE_SI
) {
887 if (slice_type
== PIPE_H264_SLICE_TYPE_SP
)
888 /* sp_for_switch_flag */
895 if (pps
->deblocking_filter_control_present_flag
) {
896 unsigned disable_deblocking_filter_idc
= vl_rbsp_ue(rbsp
);
898 if (disable_deblocking_filter_idc
!= 1) {
899 /* slice_alpha_c0_offset_div2 */
902 /* slice_beta_offset_div2 */
907 if (pps
->num_slice_groups_minus1
> 0 && pps
->slice_group_map_type
>= 3 && pps
->slice_group_map_type
<= 5)
908 /* slice_group_change_cycle */
912 static void vid_dec_h264_Decode(vid_dec_PrivateType
*priv
, struct vl_vlc
*vlc
, unsigned min_bits_left
)
914 unsigned nal_ref_idc
, nal_unit_type
;
916 if (!vl_vlc_search_byte(vlc
, vl_vlc_bits_left(vlc
) - min_bits_left
, 0x00))
919 if (vl_vlc_peekbits(vlc
, 24) != 0x000001) {
920 vl_vlc_eatbits(vlc
, 8);
925 unsigned bytes
= priv
->bytes_left
- (vl_vlc_bits_left(vlc
) / 8);
926 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
, &priv
->picture
.base
,
927 1, &priv
->slice
, &bytes
);
931 vl_vlc_eatbits(vlc
, 24);
933 /* forbidden_zero_bit */
934 vl_vlc_eatbits(vlc
, 1);
936 nal_ref_idc
= vl_vlc_get_uimsbf(vlc
, 2);
938 if (nal_ref_idc
!= priv
->codec_data
.h264
.nal_ref_idc
&&
939 (nal_ref_idc
* priv
->codec_data
.h264
.nal_ref_idc
) == 0)
940 vid_dec_h264_EndFrame(priv
);
942 priv
->codec_data
.h264
.nal_ref_idc
= nal_ref_idc
;
944 nal_unit_type
= vl_vlc_get_uimsbf(vlc
, 5);
946 if (nal_unit_type
!= 1 && nal_unit_type
!= 5)
947 vid_dec_h264_EndFrame(priv
);
949 if (nal_unit_type
== 7) {
951 vl_rbsp_init(&rbsp
, vlc
, ~0);
952 seq_parameter_set(priv
, &rbsp
);
954 } else if (nal_unit_type
== 8) {
956 vl_rbsp_init(&rbsp
, vlc
, ~0);
957 picture_parameter_set(priv
, &rbsp
);
959 } else if (nal_unit_type
== 1 || nal_unit_type
== 5) {
960 /* Coded slice of a non-IDR or IDR picture */
961 unsigned bits
= vl_vlc_valid_bits(vlc
);
962 unsigned bytes
= bits
/ 8 + 4;
965 const void *ptr
= buf
;
971 buf
[3] = (nal_ref_idc
<< 5) | nal_unit_type
;
972 for (i
= 4; i
< bytes
; ++i
)
973 buf
[i
] = vl_vlc_peekbits(vlc
, bits
) >> ((bytes
- i
- 1) * 8);
975 priv
->bytes_left
= (vl_vlc_bits_left(vlc
) - bits
) / 8;
976 priv
->slice
= vlc
->data
;
978 vl_rbsp_init(&rbsp
, vlc
, 128);
979 slice_header(priv
, &rbsp
, nal_ref_idc
, nal_unit_type
);
981 vid_dec_h264_BeginFrame(priv
);
983 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
, &priv
->picture
.base
,
987 /* resync to byte boundary */
988 vl_vlc_eatbits(vlc
, vl_vlc_valid_bits(vlc
) % 8);