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 "util/u_video.h"
37 #include "vl/vl_rbsp.h"
38 #include "vl/vl_zscan.h"
40 #include "entrypoint.h"
43 #define DPB_MAX_SIZE 5
46 struct list_head list
;
47 struct pipe_video_buffer
*buffer
;
52 static const uint8_t Default_4x4_Intra
[16] = {
53 6, 13, 20, 28, 13, 20, 28, 32,
54 20, 28, 32, 37, 28, 32, 37, 42
57 static const uint8_t Default_4x4_Inter
[16] = {
58 10, 14, 20, 24, 14, 20, 24, 27,
59 20, 24, 27, 30, 24, 27, 30, 34
62 static const uint8_t Default_8x8_Intra
[64] = {
63 6, 10, 13, 16, 18, 23, 25, 27,
64 10, 11, 16, 18, 23, 25, 27, 29,
65 13, 16, 18, 23, 25, 27, 29, 31,
66 16, 18, 23, 25, 27, 29, 31, 33,
67 18, 23, 25, 27, 29, 31, 33, 36,
68 23, 25, 27, 29, 31, 33, 36, 38,
69 25, 27, 29, 31, 33, 36, 38, 40,
70 27, 29, 31, 33, 36, 38, 40, 42
73 static const uint8_t Default_8x8_Inter
[64] = {
74 9, 13, 15, 17, 19, 21, 22, 24,
75 13, 13, 17, 19, 21, 22, 24, 25,
76 15, 17, 19, 21, 22, 24, 25, 27,
77 17, 19, 21, 22, 24, 25, 27, 28,
78 19, 21, 22, 24, 25, 27, 28, 30,
79 21, 22, 24, 25, 27, 28, 30, 32,
80 22, 24, 25, 27, 28, 30, 32, 33,
81 24, 25, 27, 28, 30, 32, 33, 35
84 static void vid_dec_h264_Decode(vid_dec_PrivateType
*priv
, struct vl_vlc
*vlc
, unsigned min_bits_left
);
85 static void vid_dec_h264_EndFrame(vid_dec_PrivateType
*priv
);
86 static struct pipe_video_buffer
*vid_dec_h264_Flush(vid_dec_PrivateType
*priv
, OMX_TICKS
*timestamp
);
88 void vid_dec_h264_Init(vid_dec_PrivateType
*priv
)
90 priv
->picture
.base
.profile
= PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH
;
92 priv
->Decode
= vid_dec_h264_Decode
;
93 priv
->EndFrame
= vid_dec_h264_EndFrame
;
94 priv
->Flush
= vid_dec_h264_Flush
;
96 LIST_INITHEAD(&priv
->codec_data
.h264
.dpb_list
);
97 priv
->picture
.h264
.field_order_cnt
[0] = priv
->picture
.h264
.field_order_cnt
[1] = INT_MAX
;
98 priv
->first_buf_in_frame
= true;
101 static void vid_dec_h264_BeginFrame(vid_dec_PrivateType
*priv
)
103 //TODO: sane buffer handling
105 if (priv
->frame_started
)
109 struct pipe_video_codec templat
= {};
110 omx_base_video_PortType
*port
;
112 port
= (omx_base_video_PortType
*)priv
->ports
[OMX_BASE_FILTER_INPUTPORT_INDEX
];
113 templat
.profile
= priv
->profile
;
114 templat
.entrypoint
= PIPE_VIDEO_ENTRYPOINT_BITSTREAM
;
115 templat
.chroma_format
= PIPE_VIDEO_CHROMA_FORMAT_420
;
116 templat
.max_references
= priv
->picture
.h264
.num_ref_frames
;
117 templat
.expect_chunked_decode
= true;
118 templat
.width
= port
->sPortParam
.format
.video
.nFrameWidth
;
119 templat
.height
= port
->sPortParam
.format
.video
.nFrameHeight
;
120 templat
.level
= priv
->picture
.h264
.pps
->sps
->level_idc
;
122 priv
->codec
= priv
->pipe
->create_video_codec(priv
->pipe
, &templat
);
125 vid_dec_NeedTarget(priv
);
127 if (priv
->first_buf_in_frame
)
128 priv
->timestamp
= priv
->timestamps
[0];
129 priv
->first_buf_in_frame
= false;
131 priv
->picture
.h264
.num_ref_frames
= priv
->picture
.h264
.pps
->sps
->max_num_ref_frames
;
133 priv
->picture
.h264
.slice_count
= 0;
134 priv
->codec
->begin_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
135 priv
->frame_started
= true;
138 static struct pipe_video_buffer
*vid_dec_h264_Flush(vid_dec_PrivateType
*priv
,
139 OMX_TICKS
*timestamp
)
141 struct dpb_list
*entry
, *result
= NULL
;
142 struct pipe_video_buffer
*buf
;
144 /* search for the lowest poc and break on zeros */
145 LIST_FOR_EACH_ENTRY(entry
, &priv
->codec_data
.h264
.dpb_list
, list
) {
147 if (result
&& entry
->poc
== 0)
150 if (!result
|| entry
->poc
< result
->poc
)
157 buf
= result
->buffer
;
159 *timestamp
= result
->timestamp
;
161 --priv
->codec_data
.h264
.dpb_num
;
162 LIST_DEL(&result
->list
);
168 static void vid_dec_h264_EndFrame(vid_dec_PrivateType
*priv
)
170 struct dpb_list
*entry
;
171 struct pipe_video_buffer
*tmp
;
172 bool top_field_first
;
175 if (!priv
->frame_started
)
178 priv
->codec
->end_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
179 priv
->frame_started
= false;
181 // TODO: implement frame number handling
182 priv
->picture
.h264
.frame_num_list
[0] = priv
->picture
.h264
.frame_num
;
183 priv
->picture
.h264
.field_order_cnt_list
[0][0] = priv
->picture
.h264
.frame_num
;
184 priv
->picture
.h264
.field_order_cnt_list
[0][1] = priv
->picture
.h264
.frame_num
;
186 top_field_first
= priv
->picture
.h264
.field_order_cnt
[0] < priv
->picture
.h264
.field_order_cnt
[1];
188 if (priv
->picture
.h264
.field_pic_flag
&& priv
->picture
.h264
.bottom_field_flag
!= top_field_first
)
191 /* add the decoded picture to the dpb list */
192 entry
= CALLOC_STRUCT(dpb_list
);
196 priv
->first_buf_in_frame
= true;
197 entry
->buffer
= priv
->target
;
198 entry
->timestamp
= priv
->timestamp
;
199 entry
->poc
= MIN2(priv
->picture
.h264
.field_order_cnt
[0], priv
->picture
.h264
.field_order_cnt
[1]);
200 LIST_ADDTAIL(&entry
->list
, &priv
->codec_data
.h264
.dpb_list
);
201 ++priv
->codec_data
.h264
.dpb_num
;
203 priv
->picture
.h264
.field_order_cnt
[0] = priv
->picture
.h264
.field_order_cnt
[1] = INT_MAX
;
205 if (priv
->codec_data
.h264
.dpb_num
<= DPB_MAX_SIZE
)
208 tmp
= priv
->in_buffers
[0]->pInputPortPrivate
;
209 priv
->in_buffers
[0]->pInputPortPrivate
= vid_dec_h264_Flush(priv
, ×tamp
);
210 priv
->in_buffers
[0]->nTimeStamp
= timestamp
;
212 priv
->frame_finished
= priv
->in_buffers
[0]->pInputPortPrivate
!= NULL
;
215 static void vui_parameters(struct vl_rbsp
*rbsp
)
220 static void scaling_list(struct vl_rbsp
*rbsp
, uint8_t *scalingList
, unsigned sizeOfScalingList
,
221 const uint8_t *defaultList
, const uint8_t *fallbackList
)
223 unsigned lastScale
= 8, nextScale
= 8;
227 /* (pic|seq)_scaling_list_present_flag[i] */
228 if (!vl_rbsp_u(rbsp
, 1)) {
230 memcpy(scalingList
, fallbackList
, sizeOfScalingList
);
234 list
= (sizeOfScalingList
== 16) ? vl_zscan_normal_16
: vl_zscan_normal
;
235 for (i
= 0; i
< sizeOfScalingList
; ++i
) {
237 if (nextScale
!= 0) {
238 signed delta_scale
= vl_rbsp_se(rbsp
);
239 nextScale
= (lastScale
+ delta_scale
+ 256) % 256;
240 if (i
== 0 && nextScale
== 0) {
241 memcpy(scalingList
, defaultList
, sizeOfScalingList
);
245 scalingList
[list
[i
]] = nextScale
== 0 ? lastScale
: nextScale
;
246 lastScale
= scalingList
[list
[i
]];
250 static struct pipe_h264_sps
*seq_parameter_set_id(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
252 unsigned id
= vl_rbsp_ue(rbsp
);
253 if (id
>= ARRAY_SIZE(priv
->codec_data
.h264
.sps
))
254 return NULL
; /* invalid seq_parameter_set_id */
256 return &priv
->codec_data
.h264
.sps
[id
];
259 static void seq_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
261 struct pipe_h264_sps
*sps
;
262 unsigned profile_idc
, level_idc
;
265 /* Sequence parameter set */
266 profile_idc
= vl_rbsp_u(rbsp
, 8);
268 /* constraint_set0_flag */
271 /* constraint_set1_flag */
274 /* constraint_set2_flag */
277 /* constraint_set3_flag */
280 /* constraint_set4_flag */
283 /* constraint_set5_flag */
286 /* reserved_zero_2bits */
290 level_idc
= vl_rbsp_u(rbsp
, 8);
292 sps
= seq_parameter_set_id(priv
, rbsp
);
296 memset(sps
, 0, sizeof(*sps
));
297 memset(sps
->ScalingList4x4
, 16, sizeof(sps
->ScalingList4x4
));
298 memset(sps
->ScalingList8x8
, 16, sizeof(sps
->ScalingList8x8
));
300 sps
->level_idc
= level_idc
;
302 if (profile_idc
== 100 || profile_idc
== 110 || profile_idc
== 122 || profile_idc
== 244 ||
303 profile_idc
== 44 || profile_idc
== 83 || profile_idc
== 86 || profile_idc
== 118 ||
304 profile_idc
== 128 || profile_idc
== 138) {
306 sps
->chroma_format_idc
= vl_rbsp_ue(rbsp
);
308 if (sps
->chroma_format_idc
== 3)
309 sps
->separate_colour_plane_flag
= vl_rbsp_u(rbsp
, 1);
311 sps
->bit_depth_luma_minus8
= vl_rbsp_ue(rbsp
);
313 sps
->bit_depth_chroma_minus8
= vl_rbsp_ue(rbsp
);
315 /* qpprime_y_zero_transform_bypass_flag */
318 sps
->seq_scaling_matrix_present_flag
= vl_rbsp_u(rbsp
, 1);
319 if (sps
->seq_scaling_matrix_present_flag
) {
321 scaling_list(rbsp
, sps
->ScalingList4x4
[0], 16, Default_4x4_Intra
, Default_4x4_Intra
);
322 scaling_list(rbsp
, sps
->ScalingList4x4
[1], 16, Default_4x4_Intra
, sps
->ScalingList4x4
[0]);
323 scaling_list(rbsp
, sps
->ScalingList4x4
[2], 16, Default_4x4_Intra
, sps
->ScalingList4x4
[1]);
324 scaling_list(rbsp
, sps
->ScalingList4x4
[3], 16, Default_4x4_Inter
, Default_4x4_Inter
);
325 scaling_list(rbsp
, sps
->ScalingList4x4
[4], 16, Default_4x4_Inter
, sps
->ScalingList4x4
[3]);
326 scaling_list(rbsp
, sps
->ScalingList4x4
[5], 16, Default_4x4_Inter
, sps
->ScalingList4x4
[4]);
328 scaling_list(rbsp
, sps
->ScalingList8x8
[0], 64, Default_8x8_Intra
, Default_8x8_Intra
);
329 scaling_list(rbsp
, sps
->ScalingList8x8
[1], 64, Default_8x8_Inter
, Default_8x8_Inter
);
330 if (sps
->chroma_format_idc
== 3) {
331 scaling_list(rbsp
, sps
->ScalingList8x8
[2], 64, Default_8x8_Intra
, sps
->ScalingList8x8
[0]);
332 scaling_list(rbsp
, sps
->ScalingList8x8
[3], 64, Default_8x8_Inter
, sps
->ScalingList8x8
[1]);
333 scaling_list(rbsp
, sps
->ScalingList8x8
[4], 64, Default_8x8_Intra
, sps
->ScalingList8x8
[2]);
334 scaling_list(rbsp
, sps
->ScalingList8x8
[5], 64, Default_8x8_Inter
, sps
->ScalingList8x8
[3]);
337 } else if (profile_idc
== 183)
338 sps
->chroma_format_idc
= 0;
340 sps
->chroma_format_idc
= 1;
342 sps
->log2_max_frame_num_minus4
= vl_rbsp_ue(rbsp
);
344 sps
->pic_order_cnt_type
= vl_rbsp_ue(rbsp
);
346 if (sps
->pic_order_cnt_type
== 0)
347 sps
->log2_max_pic_order_cnt_lsb_minus4
= vl_rbsp_ue(rbsp
);
348 else if (sps
->pic_order_cnt_type
== 1) {
349 sps
->delta_pic_order_always_zero_flag
= vl_rbsp_u(rbsp
, 1);
351 sps
->offset_for_non_ref_pic
= vl_rbsp_se(rbsp
);
353 sps
->offset_for_top_to_bottom_field
= vl_rbsp_se(rbsp
);
355 sps
->num_ref_frames_in_pic_order_cnt_cycle
= vl_rbsp_ue(rbsp
);
357 for (i
= 0; i
< sps
->num_ref_frames_in_pic_order_cnt_cycle
; ++i
)
358 sps
->offset_for_ref_frame
[i
] = vl_rbsp_se(rbsp
);
361 sps
->max_num_ref_frames
= vl_rbsp_ue(rbsp
);
363 /* gaps_in_frame_num_value_allowed_flag */
366 /* pic_width_in_mbs_minus1 */
369 /* pic_height_in_map_units_minus1 */
372 sps
->frame_mbs_only_flag
= vl_rbsp_u(rbsp
, 1);
373 if (!sps
->frame_mbs_only_flag
)
374 sps
->mb_adaptive_frame_field_flag
= vl_rbsp_u(rbsp
, 1);
376 sps
->direct_8x8_inference_flag
= vl_rbsp_u(rbsp
, 1);
378 /* frame_cropping_flag */
379 if (vl_rbsp_u(rbsp
, 1)) {
380 /* frame_crop_left_offset */
383 /* frame_crop_right_offset */
386 /* frame_crop_top_offset */
389 /* frame_crop_bottom_offset */
393 /* vui_parameters_present_flag */
394 if (vl_rbsp_u(rbsp
, 1))
395 vui_parameters(rbsp
);
398 static struct pipe_h264_pps
*pic_parameter_set_id(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
400 unsigned id
= vl_rbsp_ue(rbsp
);
401 if (id
>= ARRAY_SIZE(priv
->codec_data
.h264
.pps
))
402 return NULL
; /* invalid pic_parameter_set_id */
404 return &priv
->codec_data
.h264
.pps
[id
];
407 static void picture_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
409 struct pipe_h264_sps
*sps
;
410 struct pipe_h264_pps
*pps
;
413 pps
= pic_parameter_set_id(priv
, rbsp
);
417 memset(pps
, 0, sizeof(*pps
));
419 sps
= pps
->sps
= seq_parameter_set_id(priv
, rbsp
);
423 memcpy(pps
->ScalingList4x4
, sps
->ScalingList4x4
, sizeof(pps
->ScalingList4x4
));
424 memcpy(pps
->ScalingList8x8
, sps
->ScalingList8x8
, sizeof(pps
->ScalingList8x8
));
426 pps
->entropy_coding_mode_flag
= vl_rbsp_u(rbsp
, 1);
428 pps
->bottom_field_pic_order_in_frame_present_flag
= vl_rbsp_u(rbsp
, 1);
430 pps
->num_slice_groups_minus1
= vl_rbsp_ue(rbsp
);
431 if (pps
->num_slice_groups_minus1
> 0) {
432 pps
->slice_group_map_type
= vl_rbsp_ue(rbsp
);
434 if (pps
->slice_group_map_type
== 0) {
436 for (i
= 0; i
<= pps
->num_slice_groups_minus1
; ++i
)
437 /* run_length_minus1[i] */
440 } else if (pps
->slice_group_map_type
== 2) {
442 for (i
= 0; i
<= pps
->num_slice_groups_minus1
; ++i
) {
446 /* bottom_right[i] */
450 } else if (pps
->slice_group_map_type
>= 3 && pps
->slice_group_map_type
<= 5) {
452 /* slice_group_change_direction_flag */
455 pps
->slice_group_change_rate_minus1
= vl_rbsp_ue(rbsp
);
457 } else if (pps
->slice_group_map_type
== 6) {
459 unsigned pic_size_in_map_units_minus1
;
461 pic_size_in_map_units_minus1
= vl_rbsp_ue(rbsp
);
463 for (i
= 0; i
<= pic_size_in_map_units_minus1
; ++i
)
464 /* slice_group_id[i] */
465 vl_rbsp_u(rbsp
, log2(pps
->num_slice_groups_minus1
+ 1));
469 pps
->num_ref_idx_l0_default_active_minus1
= vl_rbsp_ue(rbsp
);
471 pps
->num_ref_idx_l1_default_active_minus1
= vl_rbsp_ue(rbsp
);
473 pps
->weighted_pred_flag
= vl_rbsp_u(rbsp
, 1);
475 pps
->weighted_bipred_idc
= vl_rbsp_u(rbsp
, 2);
477 pps
->pic_init_qp_minus26
= vl_rbsp_se(rbsp
);
479 /* pic_init_qs_minus26 */
482 pps
->chroma_qp_index_offset
= vl_rbsp_se(rbsp
);
484 pps
->deblocking_filter_control_present_flag
= vl_rbsp_u(rbsp
, 1);
486 pps
->constrained_intra_pred_flag
= vl_rbsp_u(rbsp
, 1);
488 pps
->redundant_pic_cnt_present_flag
= vl_rbsp_u(rbsp
, 1);
490 if (vl_rbsp_more_data(rbsp
)) {
491 pps
->transform_8x8_mode_flag
= vl_rbsp_u(rbsp
, 1);
493 /* pic_scaling_matrix_present_flag */
494 if (vl_rbsp_u(rbsp
, 1)) {
496 scaling_list(rbsp
, pps
->ScalingList4x4
[0], 16, Default_4x4_Intra
,
497 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_4x4_Intra
);
498 scaling_list(rbsp
, pps
->ScalingList4x4
[1], 16, Default_4x4_Intra
, pps
->ScalingList4x4
[0]);
499 scaling_list(rbsp
, pps
->ScalingList4x4
[2], 16, Default_4x4_Intra
, pps
->ScalingList4x4
[1]);
500 scaling_list(rbsp
, pps
->ScalingList4x4
[3], 16, Default_4x4_Inter
,
501 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_4x4_Inter
);
502 scaling_list(rbsp
, pps
->ScalingList4x4
[4], 16, Default_4x4_Inter
, pps
->ScalingList4x4
[3]);
503 scaling_list(rbsp
, pps
->ScalingList4x4
[5], 16, Default_4x4_Inter
, pps
->ScalingList4x4
[4]);
505 if (pps
->transform_8x8_mode_flag
) {
506 scaling_list(rbsp
, pps
->ScalingList8x8
[0], 64, Default_8x8_Intra
,
507 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_8x8_Intra
);
508 scaling_list(rbsp
, pps
->ScalingList8x8
[1], 64, Default_8x8_Inter
,
509 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_8x8_Inter
);
510 if (sps
->chroma_format_idc
== 3) {
511 scaling_list(rbsp
, pps
->ScalingList8x8
[2], 64, Default_8x8_Intra
, pps
->ScalingList8x8
[0]);
512 scaling_list(rbsp
, pps
->ScalingList8x8
[3], 64, Default_8x8_Inter
, pps
->ScalingList8x8
[1]);
513 scaling_list(rbsp
, pps
->ScalingList8x8
[4], 64, Default_8x8_Intra
, pps
->ScalingList8x8
[2]);
514 scaling_list(rbsp
, pps
->ScalingList8x8
[5], 64, Default_8x8_Inter
, pps
->ScalingList8x8
[3]);
519 pps
->second_chroma_qp_index_offset
= vl_rbsp_se(rbsp
);
523 static void ref_pic_list_mvc_modification(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
529 static void ref_pic_list_modification(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
530 enum pipe_h264_slice_type slice_type
)
532 unsigned modification_of_pic_nums_idc
;
534 if (slice_type
!= 2 && slice_type
!= 4) {
535 /* ref_pic_list_modification_flag_l0 */
536 if (vl_rbsp_u(rbsp
, 1)) {
538 modification_of_pic_nums_idc
= vl_rbsp_ue(rbsp
);
539 if (modification_of_pic_nums_idc
== 0 ||
540 modification_of_pic_nums_idc
== 1)
541 /* abs_diff_pic_num_minus1 */
543 else if (modification_of_pic_nums_idc
== 2)
544 /* long_term_pic_num */
546 } while (modification_of_pic_nums_idc
!= 3);
550 if (slice_type
== 1) {
551 /* ref_pic_list_modification_flag_l1 */
552 if (vl_rbsp_u(rbsp
, 1)) {
554 modification_of_pic_nums_idc
= vl_rbsp_ue(rbsp
);
555 if (modification_of_pic_nums_idc
== 0 ||
556 modification_of_pic_nums_idc
== 1)
557 /* abs_diff_pic_num_minus1 */
559 else if (modification_of_pic_nums_idc
== 2)
560 /* long_term_pic_num */
562 } while (modification_of_pic_nums_idc
!= 3);
567 static void pred_weight_table(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
568 struct pipe_h264_sps
*sps
, enum pipe_h264_slice_type slice_type
)
570 unsigned ChromaArrayType
= sps
->separate_colour_plane_flag
? 0 : sps
->chroma_format_idc
;
573 /* luma_log2_weight_denom */
576 if (ChromaArrayType
!= 0)
577 /* chroma_log2_weight_denom */
580 for (i
= 0; i
<= priv
->picture
.h264
.num_ref_idx_l0_active_minus1
; ++i
) {
581 /* luma_weight_l0_flag */
582 if (vl_rbsp_u(rbsp
, 1)) {
583 /* luma_weight_l0[i] */
585 /* luma_offset_l0[i] */
588 if (ChromaArrayType
!= 0) {
589 /* chroma_weight_l0_flag */
590 if (vl_rbsp_u(rbsp
, 1)) {
591 for (j
= 0; j
< 2; ++j
) {
592 /* chroma_weight_l0[i][j] */
594 /* chroma_offset_l0[i][j] */
601 if (slice_type
== 1) {
602 for (i
= 0; i
<= priv
->picture
.h264
.num_ref_idx_l1_active_minus1
; ++i
) {
603 /* luma_weight_l1_flag */
604 if (vl_rbsp_u(rbsp
, 1)) {
605 /* luma_weight_l1[i] */
607 /* luma_offset_l1[i] */
610 if (ChromaArrayType
!= 0) {
611 /* chroma_weight_l1_flag */
612 if (vl_rbsp_u(rbsp
, 1)) {
613 for (j
= 0; j
< 2; ++j
) {
614 /* chroma_weight_l1[i][j] */
616 /* chroma_offset_l1[i][j] */
625 static void dec_ref_pic_marking(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
628 unsigned memory_management_control_operation
;
631 /* no_output_of_prior_pics_flag */
633 /* long_term_reference_flag */
636 /* adaptive_ref_pic_marking_mode_flag */
637 if (vl_rbsp_u(rbsp
, 1)) {
639 memory_management_control_operation
= vl_rbsp_ue(rbsp
);
641 if (memory_management_control_operation
== 1 ||
642 memory_management_control_operation
== 3)
643 /* difference_of_pic_nums_minus1 */
646 if (memory_management_control_operation
== 2)
647 /* long_term_pic_num */
650 if (memory_management_control_operation
== 3 ||
651 memory_management_control_operation
== 6)
652 /* long_term_frame_idx */
655 if (memory_management_control_operation
== 4)
656 /* max_long_term_frame_idx_plus1 */
658 } while (memory_management_control_operation
!= 0);
663 static void slice_header(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
664 unsigned nal_ref_idc
, unsigned nal_unit_type
)
666 enum pipe_h264_slice_type slice_type
;
667 struct pipe_h264_pps
*pps
;
668 struct pipe_h264_sps
*sps
;
669 unsigned frame_num
, prevFrameNum
;
670 bool IdrPicFlag
= nal_unit_type
== 5;
672 if (IdrPicFlag
!= priv
->codec_data
.h264
.IdrPicFlag
)
673 vid_dec_h264_EndFrame(priv
);
675 priv
->codec_data
.h264
.IdrPicFlag
= IdrPicFlag
;
677 /* first_mb_in_slice */
680 slice_type
= vl_rbsp_ue(rbsp
) % 5;
682 pps
= pic_parameter_set_id(priv
, rbsp
);
690 if (pps
!= priv
->picture
.h264
.pps
)
691 vid_dec_h264_EndFrame(priv
);
693 priv
->picture
.h264
.pps
= pps
;
695 if (sps
->separate_colour_plane_flag
== 1 )
696 /* colour_plane_id */
699 frame_num
= vl_rbsp_u(rbsp
, sps
->log2_max_frame_num_minus4
+ 4);
701 if (frame_num
!= priv
->picture
.h264
.frame_num
)
702 vid_dec_h264_EndFrame(priv
);
704 prevFrameNum
= priv
->picture
.h264
.frame_num
;
705 priv
->picture
.h264
.frame_num
= frame_num
;
707 priv
->picture
.h264
.field_pic_flag
= 0;
708 priv
->picture
.h264
.bottom_field_flag
= 0;
710 if (!sps
->frame_mbs_only_flag
) {
711 unsigned field_pic_flag
= vl_rbsp_u(rbsp
, 1);
713 if (!field_pic_flag
&& field_pic_flag
!= priv
->picture
.h264
.field_pic_flag
)
714 vid_dec_h264_EndFrame(priv
);
716 priv
->picture
.h264
.field_pic_flag
= field_pic_flag
;
718 if (priv
->picture
.h264
.field_pic_flag
) {
719 unsigned bottom_field_flag
= vl_rbsp_u(rbsp
, 1);
721 if (bottom_field_flag
!= priv
->picture
.h264
.bottom_field_flag
)
722 vid_dec_h264_EndFrame(priv
);
724 priv
->picture
.h264
.bottom_field_flag
= bottom_field_flag
;
729 unsigned idr_pic_id
= vl_rbsp_ue(rbsp
);
731 if (idr_pic_id
!= priv
->codec_data
.h264
.idr_pic_id
)
732 vid_dec_h264_EndFrame(priv
);
734 priv
->codec_data
.h264
.idr_pic_id
= idr_pic_id
;
737 if (sps
->pic_order_cnt_type
== 0) {
738 unsigned log2_max_pic_order_cnt_lsb
= sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4;
739 unsigned max_pic_order_cnt_lsb
= 1 << log2_max_pic_order_cnt_lsb
;
740 int pic_order_cnt_lsb
= vl_rbsp_u(rbsp
, log2_max_pic_order_cnt_lsb
);
741 int pic_order_cnt_msb
;
743 if (pic_order_cnt_lsb
!= priv
->codec_data
.h264
.pic_order_cnt_lsb
)
744 vid_dec_h264_EndFrame(priv
);
747 priv
->codec_data
.h264
.pic_order_cnt_msb
= 0;
748 priv
->codec_data
.h264
.pic_order_cnt_lsb
= 0;
751 if ((pic_order_cnt_lsb
< priv
->codec_data
.h264
.pic_order_cnt_lsb
) &&
752 (priv
->codec_data
.h264
.pic_order_cnt_lsb
- pic_order_cnt_lsb
) >= (max_pic_order_cnt_lsb
/ 2))
753 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
+ max_pic_order_cnt_lsb
;
755 else if ((pic_order_cnt_lsb
> priv
->codec_data
.h264
.pic_order_cnt_lsb
) &&
756 (pic_order_cnt_lsb
- priv
->codec_data
.h264
.pic_order_cnt_lsb
) > (max_pic_order_cnt_lsb
/ 2))
757 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
- max_pic_order_cnt_lsb
;
760 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
;
762 priv
->codec_data
.h264
.pic_order_cnt_msb
= pic_order_cnt_msb
;
763 priv
->codec_data
.h264
.pic_order_cnt_lsb
= pic_order_cnt_lsb
;
765 if (pps
->bottom_field_pic_order_in_frame_present_flag
&& !priv
->picture
.h264
.field_pic_flag
) {
766 unsigned delta_pic_order_cnt_bottom
= vl_rbsp_se(rbsp
);
768 if (delta_pic_order_cnt_bottom
!= priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
)
769 vid_dec_h264_EndFrame(priv
);
771 priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
= delta_pic_order_cnt_bottom
;
774 if (!priv
->picture
.h264
.field_pic_flag
) {
775 priv
->picture
.h264
.field_order_cnt
[0] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
776 priv
->picture
.h264
.field_order_cnt
[1] = priv
->picture
.h264
.field_order_cnt
[0] +
777 priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
;
778 } else if (!priv
->picture
.h264
.bottom_field_flag
)
779 priv
->picture
.h264
.field_order_cnt
[0] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
781 priv
->picture
.h264
.field_order_cnt
[1] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
783 } else if (sps
->pic_order_cnt_type
== 1) {
784 unsigned MaxFrameNum
= 1 << (sps
->log2_max_frame_num_minus4
+ 4);
785 unsigned FrameNumOffset
, absFrameNum
, expectedPicOrderCnt
;
787 if (!sps
->delta_pic_order_always_zero_flag
) {
788 unsigned delta_pic_order_cnt
[2];
790 delta_pic_order_cnt
[0] = vl_rbsp_se(rbsp
);
792 if (delta_pic_order_cnt
[0] != priv
->codec_data
.h264
.delta_pic_order_cnt
[0])
793 vid_dec_h264_EndFrame(priv
);
795 priv
->codec_data
.h264
.delta_pic_order_cnt
[0] = delta_pic_order_cnt
[0];
797 if (pps
->bottom_field_pic_order_in_frame_present_flag
&& !priv
->picture
.h264
.field_pic_flag
) {
798 delta_pic_order_cnt
[1] = vl_rbsp_se(rbsp
);
800 if (delta_pic_order_cnt
[1] != priv
->codec_data
.h264
.delta_pic_order_cnt
[1])
801 vid_dec_h264_EndFrame(priv
);
803 priv
->codec_data
.h264
.delta_pic_order_cnt
[1] = delta_pic_order_cnt
[1];
809 else if (prevFrameNum
> frame_num
)
810 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
+ MaxFrameNum
;
812 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
;
814 priv
->codec_data
.h264
.prevFrameNumOffset
= FrameNumOffset
;
816 if (sps
->num_ref_frames_in_pic_order_cnt_cycle
!= 0)
817 absFrameNum
= FrameNumOffset
+ frame_num
;
821 if (nal_ref_idc
== 0 && absFrameNum
> 0)
822 absFrameNum
= absFrameNum
- 1;
824 if (absFrameNum
> 0) {
825 unsigned picOrderCntCycleCnt
= (absFrameNum
- 1) / sps
->num_ref_frames_in_pic_order_cnt_cycle
;
826 unsigned frameNumInPicOrderCntCycle
= (absFrameNum
- 1) % sps
->num_ref_frames_in_pic_order_cnt_cycle
;
827 signed ExpectedDeltaPerPicOrderCntCycle
= 0;
830 for (i
= 0; i
< sps
->num_ref_frames_in_pic_order_cnt_cycle
; ++i
)
831 ExpectedDeltaPerPicOrderCntCycle
+= sps
->offset_for_ref_frame
[i
];
833 expectedPicOrderCnt
= picOrderCntCycleCnt
* ExpectedDeltaPerPicOrderCntCycle
;
834 for (i
= 0; i
<= frameNumInPicOrderCntCycle
; ++i
)
835 expectedPicOrderCnt
+= sps
->offset_for_ref_frame
[i
];
838 expectedPicOrderCnt
= 0;
840 if (nal_ref_idc
== 0)
841 expectedPicOrderCnt
+= sps
->offset_for_non_ref_pic
;
843 if (!priv
->picture
.h264
.field_pic_flag
) {
844 priv
->picture
.h264
.field_order_cnt
[0] = expectedPicOrderCnt
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
845 priv
->picture
.h264
.field_order_cnt
[1] = priv
->picture
.h264
.field_order_cnt
[0] +
846 sps
->offset_for_top_to_bottom_field
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[1];
848 } else if (!priv
->picture
.h264
.bottom_field_flag
)
849 priv
->picture
.h264
.field_order_cnt
[0] = expectedPicOrderCnt
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
851 priv
->picture
.h264
.field_order_cnt
[1] = expectedPicOrderCnt
+ sps
->offset_for_top_to_bottom_field
+
852 priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
854 } else if (sps
->pic_order_cnt_type
== 2) {
855 unsigned MaxFrameNum
= 1 << (sps
->log2_max_frame_num_minus4
+ 4);
856 unsigned FrameNumOffset
, tempPicOrderCnt
;
860 else if (prevFrameNum
> frame_num
)
861 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
+ MaxFrameNum
;
863 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
;
865 priv
->codec_data
.h264
.prevFrameNumOffset
= FrameNumOffset
;
869 else if (nal_ref_idc
== 0)
870 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
) - 1;
872 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
);
874 if (!priv
->picture
.h264
.field_pic_flag
) {
875 priv
->picture
.h264
.field_order_cnt
[0] = tempPicOrderCnt
;
876 priv
->picture
.h264
.field_order_cnt
[1] = tempPicOrderCnt
;
878 } else if (!priv
->picture
.h264
.bottom_field_flag
)
879 priv
->picture
.h264
.field_order_cnt
[0] = tempPicOrderCnt
;
881 priv
->picture
.h264
.field_order_cnt
[1] = tempPicOrderCnt
;
884 if (pps
->redundant_pic_cnt_present_flag
)
885 /* redundant_pic_cnt */
888 if (slice_type
== PIPE_H264_SLICE_TYPE_B
)
889 /* direct_spatial_mv_pred_flag */
892 priv
->picture
.h264
.num_ref_idx_l0_active_minus1
= pps
->num_ref_idx_l0_default_active_minus1
;
893 priv
->picture
.h264
.num_ref_idx_l1_active_minus1
= pps
->num_ref_idx_l1_default_active_minus1
;
895 if (slice_type
== PIPE_H264_SLICE_TYPE_P
||
896 slice_type
== PIPE_H264_SLICE_TYPE_SP
||
897 slice_type
== PIPE_H264_SLICE_TYPE_B
) {
899 /* num_ref_idx_active_override_flag */
900 if (vl_rbsp_u(rbsp
, 1)) {
901 priv
->picture
.h264
.num_ref_idx_l0_active_minus1
= vl_rbsp_ue(rbsp
);
903 if (slice_type
== PIPE_H264_SLICE_TYPE_B
)
904 priv
->picture
.h264
.num_ref_idx_l1_active_minus1
= vl_rbsp_ue(rbsp
);
908 if (nal_unit_type
== 20 || nal_unit_type
== 21)
909 ref_pic_list_mvc_modification(priv
, rbsp
);
911 ref_pic_list_modification(priv
, rbsp
, slice_type
);
913 if ((pps
->weighted_pred_flag
&& (slice_type
== PIPE_H264_SLICE_TYPE_P
|| slice_type
== PIPE_H264_SLICE_TYPE_SP
)) ||
914 (pps
->weighted_bipred_idc
== 1 && slice_type
== PIPE_H264_SLICE_TYPE_B
))
915 pred_weight_table(priv
, rbsp
, sps
, slice_type
);
917 if (nal_ref_idc
!= 0)
918 dec_ref_pic_marking(priv
, rbsp
, IdrPicFlag
);
920 if (pps
->entropy_coding_mode_flag
&& slice_type
!= PIPE_H264_SLICE_TYPE_I
&& slice_type
!= PIPE_H264_SLICE_TYPE_SI
)
927 if (slice_type
== PIPE_H264_SLICE_TYPE_SP
|| slice_type
== PIPE_H264_SLICE_TYPE_SI
) {
928 if (slice_type
== PIPE_H264_SLICE_TYPE_SP
)
929 /* sp_for_switch_flag */
936 if (pps
->deblocking_filter_control_present_flag
) {
937 unsigned disable_deblocking_filter_idc
= vl_rbsp_ue(rbsp
);
939 if (disable_deblocking_filter_idc
!= 1) {
940 /* slice_alpha_c0_offset_div2 */
943 /* slice_beta_offset_div2 */
948 if (pps
->num_slice_groups_minus1
> 0 && pps
->slice_group_map_type
>= 3 && pps
->slice_group_map_type
<= 5)
949 /* slice_group_change_cycle */
953 static void vid_dec_h264_Decode(vid_dec_PrivateType
*priv
, struct vl_vlc
*vlc
, unsigned min_bits_left
)
955 unsigned nal_ref_idc
, nal_unit_type
;
957 if (!vl_vlc_search_byte(vlc
, vl_vlc_bits_left(vlc
) - min_bits_left
, 0x00))
960 if (vl_vlc_peekbits(vlc
, 24) != 0x000001) {
961 vl_vlc_eatbits(vlc
, 8);
966 unsigned bytes
= priv
->bytes_left
- (vl_vlc_bits_left(vlc
) / 8);
967 ++priv
->picture
.h264
.slice_count
;
968 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
, &priv
->picture
.base
,
969 1, &priv
->slice
, &bytes
);
973 vl_vlc_eatbits(vlc
, 24);
975 /* forbidden_zero_bit */
976 vl_vlc_eatbits(vlc
, 1);
978 nal_ref_idc
= vl_vlc_get_uimsbf(vlc
, 2);
980 if (nal_ref_idc
!= priv
->codec_data
.h264
.nal_ref_idc
&&
981 (nal_ref_idc
* priv
->codec_data
.h264
.nal_ref_idc
) == 0)
982 vid_dec_h264_EndFrame(priv
);
984 priv
->codec_data
.h264
.nal_ref_idc
= nal_ref_idc
;
986 nal_unit_type
= vl_vlc_get_uimsbf(vlc
, 5);
988 if (nal_unit_type
!= 1 && nal_unit_type
!= 5)
989 vid_dec_h264_EndFrame(priv
);
991 if (nal_unit_type
== 7) {
993 vl_rbsp_init(&rbsp
, vlc
, ~0);
994 seq_parameter_set(priv
, &rbsp
);
996 } else if (nal_unit_type
== 8) {
998 vl_rbsp_init(&rbsp
, vlc
, ~0);
999 picture_parameter_set(priv
, &rbsp
);
1001 } else if (nal_unit_type
== 1 || nal_unit_type
== 5) {
1002 /* Coded slice of a non-IDR or IDR picture */
1003 unsigned bits
= vl_vlc_valid_bits(vlc
);
1004 unsigned bytes
= bits
/ 8 + 4;
1005 struct vl_rbsp rbsp
;
1007 const void *ptr
= buf
;
1013 buf
[3] = (nal_ref_idc
<< 5) | nal_unit_type
;
1014 for (i
= 4; i
< bytes
; ++i
)
1015 buf
[i
] = vl_vlc_peekbits(vlc
, bits
) >> ((bytes
- i
- 1) * 8);
1017 priv
->bytes_left
= (vl_vlc_bits_left(vlc
) - bits
) / 8;
1018 priv
->slice
= vlc
->data
;
1020 vl_rbsp_init(&rbsp
, vlc
, 128);
1021 slice_header(priv
, &rbsp
, nal_ref_idc
, nal_unit_type
);
1023 vid_dec_h264_BeginFrame(priv
);
1025 ++priv
->picture
.h264
.slice_count
;
1026 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
, &priv
->picture
.base
,
1030 /* resync to byte boundary */
1031 vl_vlc_eatbits(vlc
, vl_vlc_valid_bits(vlc
) % 8);