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
)
108 vid_dec_NeedTarget(priv
);
109 if (priv
->first_buf_in_frame
)
110 priv
->timestamp
= priv
->timestamps
[0];
111 priv
->first_buf_in_frame
= false;
113 priv
->picture
.h264
.num_ref_frames
= priv
->picture
.h264
.pps
->sps
->max_num_ref_frames
;
116 struct pipe_video_codec templat
= {};
117 omx_base_video_PortType
*port
;
119 port
= (omx_base_video_PortType
*)priv
->ports
[OMX_BASE_FILTER_INPUTPORT_INDEX
];
120 templat
.profile
= priv
->profile
;
121 templat
.entrypoint
= PIPE_VIDEO_ENTRYPOINT_BITSTREAM
;
122 templat
.chroma_format
= PIPE_VIDEO_CHROMA_FORMAT_420
;
123 templat
.max_references
= priv
->picture
.h264
.num_ref_frames
;
124 templat
.expect_chunked_decode
= true;
125 templat
.width
= port
->sPortParam
.format
.video
.nFrameWidth
;
126 templat
.height
= port
->sPortParam
.format
.video
.nFrameHeight
;
127 templat
.level
= priv
->picture
.h264
.pps
->sps
->level_idc
;
129 priv
->codec
= priv
->pipe
->create_video_codec(priv
->pipe
, &templat
);
131 priv
->picture
.h264
.slice_count
= 0;
132 priv
->codec
->begin_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
133 priv
->frame_started
= true;
136 static struct pipe_video_buffer
*vid_dec_h264_Flush(vid_dec_PrivateType
*priv
,
137 OMX_TICKS
*timestamp
)
139 struct dpb_list
*entry
, *result
= NULL
;
140 struct pipe_video_buffer
*buf
;
142 /* search for the lowest poc and break on zeros */
143 LIST_FOR_EACH_ENTRY(entry
, &priv
->codec_data
.h264
.dpb_list
, list
) {
145 if (result
&& entry
->poc
== 0)
148 if (!result
|| entry
->poc
< result
->poc
)
155 buf
= result
->buffer
;
157 *timestamp
= result
->timestamp
;
159 --priv
->codec_data
.h264
.dpb_num
;
160 LIST_DEL(&result
->list
);
166 static void vid_dec_h264_EndFrame(vid_dec_PrivateType
*priv
)
168 struct dpb_list
*entry
;
169 struct pipe_video_buffer
*tmp
;
170 bool top_field_first
;
173 if (!priv
->frame_started
)
176 priv
->codec
->end_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
177 priv
->frame_started
= false;
179 // TODO: implement frame number handling
180 priv
->picture
.h264
.frame_num_list
[0] = priv
->picture
.h264
.frame_num
;
181 priv
->picture
.h264
.field_order_cnt_list
[0][0] = priv
->picture
.h264
.frame_num
;
182 priv
->picture
.h264
.field_order_cnt_list
[0][1] = priv
->picture
.h264
.frame_num
;
184 top_field_first
= priv
->picture
.h264
.field_order_cnt
[0] < priv
->picture
.h264
.field_order_cnt
[1];
186 if (priv
->picture
.h264
.field_pic_flag
&& priv
->picture
.h264
.bottom_field_flag
!= top_field_first
)
189 /* add the decoded picture to the dpb list */
190 entry
= CALLOC_STRUCT(dpb_list
);
194 priv
->first_buf_in_frame
= true;
195 entry
->buffer
= priv
->target
;
196 entry
->timestamp
= priv
->timestamp
;
197 entry
->poc
= MIN2(priv
->picture
.h264
.field_order_cnt
[0], priv
->picture
.h264
.field_order_cnt
[1]);
198 LIST_ADDTAIL(&entry
->list
, &priv
->codec_data
.h264
.dpb_list
);
199 ++priv
->codec_data
.h264
.dpb_num
;
201 priv
->picture
.h264
.field_order_cnt
[0] = priv
->picture
.h264
.field_order_cnt
[1] = INT_MAX
;
203 if (priv
->codec_data
.h264
.dpb_num
<= DPB_MAX_SIZE
)
206 tmp
= priv
->in_buffers
[0]->pInputPortPrivate
;
207 priv
->in_buffers
[0]->pInputPortPrivate
= vid_dec_h264_Flush(priv
, ×tamp
);
208 priv
->in_buffers
[0]->nTimeStamp
= timestamp
;
210 priv
->frame_finished
= priv
->in_buffers
[0]->pInputPortPrivate
!= NULL
;
213 static void vui_parameters(struct vl_rbsp
*rbsp
)
218 static void scaling_list(struct vl_rbsp
*rbsp
, uint8_t *scalingList
, unsigned sizeOfScalingList
,
219 const uint8_t *defaultList
, const uint8_t *fallbackList
)
221 unsigned lastScale
= 8, nextScale
= 8;
225 /* (pic|seq)_scaling_list_present_flag[i] */
226 if (!vl_rbsp_u(rbsp
, 1)) {
228 memcpy(scalingList
, fallbackList
, sizeOfScalingList
);
232 list
= (sizeOfScalingList
== 16) ? vl_zscan_normal_16
: vl_zscan_normal
;
233 for (i
= 0; i
< sizeOfScalingList
; ++i
) {
235 if (nextScale
!= 0) {
236 signed delta_scale
= vl_rbsp_se(rbsp
);
237 nextScale
= (lastScale
+ delta_scale
+ 256) % 256;
238 if (i
== 0 && nextScale
== 0) {
239 memcpy(scalingList
, defaultList
, sizeOfScalingList
);
243 scalingList
[list
[i
]] = nextScale
== 0 ? lastScale
: nextScale
;
244 lastScale
= scalingList
[list
[i
]];
248 static struct pipe_h264_sps
*seq_parameter_set_id(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
250 unsigned id
= vl_rbsp_ue(rbsp
);
251 if (id
>= ARRAY_SIZE(priv
->codec_data
.h264
.sps
))
252 return NULL
; /* invalid seq_parameter_set_id */
254 return &priv
->codec_data
.h264
.sps
[id
];
257 static void seq_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
259 struct pipe_h264_sps
*sps
;
260 unsigned profile_idc
, level_idc
;
263 /* Sequence parameter set */
264 profile_idc
= vl_rbsp_u(rbsp
, 8);
266 /* constraint_set0_flag */
269 /* constraint_set1_flag */
272 /* constraint_set2_flag */
275 /* constraint_set3_flag */
278 /* constraint_set4_flag */
281 /* constraint_set5_flag */
284 /* reserved_zero_2bits */
288 level_idc
= vl_rbsp_u(rbsp
, 8);
290 sps
= seq_parameter_set_id(priv
, rbsp
);
294 memset(sps
, 0, sizeof(*sps
));
295 memset(sps
->ScalingList4x4
, 16, sizeof(sps
->ScalingList4x4
));
296 memset(sps
->ScalingList8x8
, 16, sizeof(sps
->ScalingList8x8
));
298 sps
->level_idc
= level_idc
;
300 if (profile_idc
== 100 || profile_idc
== 110 || profile_idc
== 122 || profile_idc
== 244 ||
301 profile_idc
== 44 || profile_idc
== 83 || profile_idc
== 86 || profile_idc
== 118 ||
302 profile_idc
== 128 || profile_idc
== 138) {
304 sps
->chroma_format_idc
= vl_rbsp_ue(rbsp
);
306 if (sps
->chroma_format_idc
== 3)
307 sps
->separate_colour_plane_flag
= vl_rbsp_u(rbsp
, 1);
309 sps
->bit_depth_luma_minus8
= vl_rbsp_ue(rbsp
);
311 sps
->bit_depth_chroma_minus8
= vl_rbsp_ue(rbsp
);
313 /* qpprime_y_zero_transform_bypass_flag */
316 sps
->seq_scaling_matrix_present_flag
= vl_rbsp_u(rbsp
, 1);
317 if (sps
->seq_scaling_matrix_present_flag
) {
319 scaling_list(rbsp
, sps
->ScalingList4x4
[0], 16, Default_4x4_Intra
, Default_4x4_Intra
);
320 scaling_list(rbsp
, sps
->ScalingList4x4
[1], 16, Default_4x4_Intra
, sps
->ScalingList4x4
[0]);
321 scaling_list(rbsp
, sps
->ScalingList4x4
[2], 16, Default_4x4_Intra
, sps
->ScalingList4x4
[1]);
322 scaling_list(rbsp
, sps
->ScalingList4x4
[3], 16, Default_4x4_Inter
, Default_4x4_Inter
);
323 scaling_list(rbsp
, sps
->ScalingList4x4
[4], 16, Default_4x4_Inter
, sps
->ScalingList4x4
[3]);
324 scaling_list(rbsp
, sps
->ScalingList4x4
[5], 16, Default_4x4_Inter
, sps
->ScalingList4x4
[4]);
326 scaling_list(rbsp
, sps
->ScalingList8x8
[0], 64, Default_8x8_Intra
, Default_8x8_Intra
);
327 scaling_list(rbsp
, sps
->ScalingList8x8
[1], 64, Default_8x8_Inter
, Default_8x8_Inter
);
328 if (sps
->chroma_format_idc
== 3) {
329 scaling_list(rbsp
, sps
->ScalingList8x8
[2], 64, Default_8x8_Intra
, sps
->ScalingList8x8
[0]);
330 scaling_list(rbsp
, sps
->ScalingList8x8
[3], 64, Default_8x8_Inter
, sps
->ScalingList8x8
[1]);
331 scaling_list(rbsp
, sps
->ScalingList8x8
[4], 64, Default_8x8_Intra
, sps
->ScalingList8x8
[2]);
332 scaling_list(rbsp
, sps
->ScalingList8x8
[5], 64, Default_8x8_Inter
, sps
->ScalingList8x8
[3]);
335 } else if (profile_idc
== 183)
336 sps
->chroma_format_idc
= 0;
338 sps
->chroma_format_idc
= 1;
340 sps
->log2_max_frame_num_minus4
= vl_rbsp_ue(rbsp
);
342 sps
->pic_order_cnt_type
= vl_rbsp_ue(rbsp
);
344 if (sps
->pic_order_cnt_type
== 0)
345 sps
->log2_max_pic_order_cnt_lsb_minus4
= vl_rbsp_ue(rbsp
);
346 else if (sps
->pic_order_cnt_type
== 1) {
347 sps
->delta_pic_order_always_zero_flag
= vl_rbsp_u(rbsp
, 1);
349 sps
->offset_for_non_ref_pic
= vl_rbsp_se(rbsp
);
351 sps
->offset_for_top_to_bottom_field
= vl_rbsp_se(rbsp
);
353 sps
->num_ref_frames_in_pic_order_cnt_cycle
= vl_rbsp_ue(rbsp
);
355 for (i
= 0; i
< sps
->num_ref_frames_in_pic_order_cnt_cycle
; ++i
)
356 sps
->offset_for_ref_frame
[i
] = vl_rbsp_se(rbsp
);
359 sps
->max_num_ref_frames
= vl_rbsp_ue(rbsp
);
361 /* gaps_in_frame_num_value_allowed_flag */
364 /* pic_width_in_mbs_minus1 */
367 /* pic_height_in_map_units_minus1 */
370 sps
->frame_mbs_only_flag
= vl_rbsp_u(rbsp
, 1);
371 if (!sps
->frame_mbs_only_flag
)
372 sps
->mb_adaptive_frame_field_flag
= vl_rbsp_u(rbsp
, 1);
374 sps
->direct_8x8_inference_flag
= vl_rbsp_u(rbsp
, 1);
376 /* frame_cropping_flag */
377 if (vl_rbsp_u(rbsp
, 1)) {
378 /* frame_crop_left_offset */
381 /* frame_crop_right_offset */
384 /* frame_crop_top_offset */
387 /* frame_crop_bottom_offset */
391 /* vui_parameters_present_flag */
392 if (vl_rbsp_u(rbsp
, 1))
393 vui_parameters(rbsp
);
396 static struct pipe_h264_pps
*pic_parameter_set_id(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
398 unsigned id
= vl_rbsp_ue(rbsp
);
399 if (id
>= ARRAY_SIZE(priv
->codec_data
.h264
.pps
))
400 return NULL
; /* invalid pic_parameter_set_id */
402 return &priv
->codec_data
.h264
.pps
[id
];
405 static void picture_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
407 struct pipe_h264_sps
*sps
;
408 struct pipe_h264_pps
*pps
;
411 pps
= pic_parameter_set_id(priv
, rbsp
);
415 memset(pps
, 0, sizeof(*pps
));
417 sps
= pps
->sps
= seq_parameter_set_id(priv
, rbsp
);
421 memcpy(pps
->ScalingList4x4
, sps
->ScalingList4x4
, sizeof(pps
->ScalingList4x4
));
422 memcpy(pps
->ScalingList8x8
, sps
->ScalingList8x8
, sizeof(pps
->ScalingList8x8
));
424 pps
->entropy_coding_mode_flag
= vl_rbsp_u(rbsp
, 1);
426 pps
->bottom_field_pic_order_in_frame_present_flag
= vl_rbsp_u(rbsp
, 1);
428 pps
->num_slice_groups_minus1
= vl_rbsp_ue(rbsp
);
429 if (pps
->num_slice_groups_minus1
> 0) {
430 pps
->slice_group_map_type
= vl_rbsp_ue(rbsp
);
432 if (pps
->slice_group_map_type
== 0) {
434 for (i
= 0; i
<= pps
->num_slice_groups_minus1
; ++i
)
435 /* run_length_minus1[i] */
438 } else if (pps
->slice_group_map_type
== 2) {
440 for (i
= 0; i
<= pps
->num_slice_groups_minus1
; ++i
) {
444 /* bottom_right[i] */
448 } else if (pps
->slice_group_map_type
>= 3 && pps
->slice_group_map_type
<= 5) {
450 /* slice_group_change_direction_flag */
453 pps
->slice_group_change_rate_minus1
= vl_rbsp_ue(rbsp
);
455 } else if (pps
->slice_group_map_type
== 6) {
457 unsigned pic_size_in_map_units_minus1
;
459 pic_size_in_map_units_minus1
= vl_rbsp_ue(rbsp
);
461 for (i
= 0; i
<= pic_size_in_map_units_minus1
; ++i
)
462 /* slice_group_id[i] */
463 vl_rbsp_u(rbsp
, log2(pps
->num_slice_groups_minus1
+ 1));
467 pps
->num_ref_idx_l0_default_active_minus1
= vl_rbsp_ue(rbsp
);
469 pps
->num_ref_idx_l1_default_active_minus1
= vl_rbsp_ue(rbsp
);
471 pps
->weighted_pred_flag
= vl_rbsp_u(rbsp
, 1);
473 pps
->weighted_bipred_idc
= vl_rbsp_u(rbsp
, 2);
475 pps
->pic_init_qp_minus26
= vl_rbsp_se(rbsp
);
477 /* pic_init_qs_minus26 */
480 pps
->chroma_qp_index_offset
= vl_rbsp_se(rbsp
);
482 pps
->deblocking_filter_control_present_flag
= vl_rbsp_u(rbsp
, 1);
484 pps
->constrained_intra_pred_flag
= vl_rbsp_u(rbsp
, 1);
486 pps
->redundant_pic_cnt_present_flag
= vl_rbsp_u(rbsp
, 1);
488 if (vl_rbsp_more_data(rbsp
)) {
489 pps
->transform_8x8_mode_flag
= vl_rbsp_u(rbsp
, 1);
491 /* pic_scaling_matrix_present_flag */
492 if (vl_rbsp_u(rbsp
, 1)) {
494 scaling_list(rbsp
, pps
->ScalingList4x4
[0], 16, Default_4x4_Intra
,
495 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_4x4_Intra
);
496 scaling_list(rbsp
, pps
->ScalingList4x4
[1], 16, Default_4x4_Intra
, pps
->ScalingList4x4
[0]);
497 scaling_list(rbsp
, pps
->ScalingList4x4
[2], 16, Default_4x4_Intra
, pps
->ScalingList4x4
[1]);
498 scaling_list(rbsp
, pps
->ScalingList4x4
[3], 16, Default_4x4_Inter
,
499 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_4x4_Inter
);
500 scaling_list(rbsp
, pps
->ScalingList4x4
[4], 16, Default_4x4_Inter
, pps
->ScalingList4x4
[3]);
501 scaling_list(rbsp
, pps
->ScalingList4x4
[5], 16, Default_4x4_Inter
, pps
->ScalingList4x4
[4]);
503 if (pps
->transform_8x8_mode_flag
) {
504 scaling_list(rbsp
, pps
->ScalingList8x8
[0], 64, Default_8x8_Intra
,
505 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_8x8_Intra
);
506 scaling_list(rbsp
, pps
->ScalingList8x8
[1], 64, Default_8x8_Inter
,
507 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_8x8_Inter
);
508 if (sps
->chroma_format_idc
== 3) {
509 scaling_list(rbsp
, pps
->ScalingList8x8
[2], 64, Default_8x8_Intra
, pps
->ScalingList8x8
[0]);
510 scaling_list(rbsp
, pps
->ScalingList8x8
[3], 64, Default_8x8_Inter
, pps
->ScalingList8x8
[1]);
511 scaling_list(rbsp
, pps
->ScalingList8x8
[4], 64, Default_8x8_Intra
, pps
->ScalingList8x8
[2]);
512 scaling_list(rbsp
, pps
->ScalingList8x8
[5], 64, Default_8x8_Inter
, pps
->ScalingList8x8
[3]);
517 pps
->second_chroma_qp_index_offset
= vl_rbsp_se(rbsp
);
521 static void ref_pic_list_mvc_modification(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
527 static void ref_pic_list_modification(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
528 enum pipe_h264_slice_type slice_type
)
530 unsigned modification_of_pic_nums_idc
;
532 if (slice_type
!= 2 && slice_type
!= 4) {
533 /* ref_pic_list_modification_flag_l0 */
534 if (vl_rbsp_u(rbsp
, 1)) {
536 modification_of_pic_nums_idc
= vl_rbsp_ue(rbsp
);
537 if (modification_of_pic_nums_idc
== 0 ||
538 modification_of_pic_nums_idc
== 1)
539 /* abs_diff_pic_num_minus1 */
541 else if (modification_of_pic_nums_idc
== 2)
542 /* long_term_pic_num */
544 } while (modification_of_pic_nums_idc
!= 3);
548 if (slice_type
== 1) {
549 /* ref_pic_list_modification_flag_l1 */
550 if (vl_rbsp_u(rbsp
, 1)) {
552 modification_of_pic_nums_idc
= vl_rbsp_ue(rbsp
);
553 if (modification_of_pic_nums_idc
== 0 ||
554 modification_of_pic_nums_idc
== 1)
555 /* abs_diff_pic_num_minus1 */
557 else if (modification_of_pic_nums_idc
== 2)
558 /* long_term_pic_num */
560 } while (modification_of_pic_nums_idc
!= 3);
565 static void pred_weight_table(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
566 struct pipe_h264_sps
*sps
, enum pipe_h264_slice_type slice_type
)
568 unsigned ChromaArrayType
= sps
->separate_colour_plane_flag
? 0 : sps
->chroma_format_idc
;
571 /* luma_log2_weight_denom */
574 if (ChromaArrayType
!= 0)
575 /* chroma_log2_weight_denom */
578 for (i
= 0; i
<= priv
->picture
.h264
.num_ref_idx_l0_active_minus1
; ++i
) {
579 /* luma_weight_l0_flag */
580 if (vl_rbsp_u(rbsp
, 1)) {
581 /* luma_weight_l0[i] */
583 /* luma_offset_l0[i] */
586 if (ChromaArrayType
!= 0) {
587 /* chroma_weight_l0_flag */
588 if (vl_rbsp_u(rbsp
, 1)) {
589 for (j
= 0; j
< 2; ++j
) {
590 /* chroma_weight_l0[i][j] */
592 /* chroma_offset_l0[i][j] */
599 if (slice_type
== 1) {
600 for (i
= 0; i
<= priv
->picture
.h264
.num_ref_idx_l1_active_minus1
; ++i
) {
601 /* luma_weight_l1_flag */
602 if (vl_rbsp_u(rbsp
, 1)) {
603 /* luma_weight_l1[i] */
605 /* luma_offset_l1[i] */
608 if (ChromaArrayType
!= 0) {
609 /* chroma_weight_l1_flag */
610 if (vl_rbsp_u(rbsp
, 1)) {
611 for (j
= 0; j
< 2; ++j
) {
612 /* chroma_weight_l1[i][j] */
614 /* chroma_offset_l1[i][j] */
623 static void dec_ref_pic_marking(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
626 unsigned memory_management_control_operation
;
629 /* no_output_of_prior_pics_flag */
631 /* long_term_reference_flag */
634 /* adaptive_ref_pic_marking_mode_flag */
635 if (vl_rbsp_u(rbsp
, 1)) {
637 memory_management_control_operation
= vl_rbsp_ue(rbsp
);
639 if (memory_management_control_operation
== 1 ||
640 memory_management_control_operation
== 3)
641 /* difference_of_pic_nums_minus1 */
644 if (memory_management_control_operation
== 2)
645 /* long_term_pic_num */
648 if (memory_management_control_operation
== 3 ||
649 memory_management_control_operation
== 6)
650 /* long_term_frame_idx */
653 if (memory_management_control_operation
== 4)
654 /* max_long_term_frame_idx_plus1 */
656 } while (memory_management_control_operation
!= 0);
661 static void slice_header(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
662 unsigned nal_ref_idc
, unsigned nal_unit_type
)
664 enum pipe_h264_slice_type slice_type
;
665 struct pipe_h264_pps
*pps
;
666 struct pipe_h264_sps
*sps
;
667 unsigned frame_num
, prevFrameNum
;
668 bool IdrPicFlag
= nal_unit_type
== 5;
670 if (IdrPicFlag
!= priv
->codec_data
.h264
.IdrPicFlag
)
671 vid_dec_h264_EndFrame(priv
);
673 priv
->codec_data
.h264
.IdrPicFlag
= IdrPicFlag
;
675 /* first_mb_in_slice */
678 slice_type
= vl_rbsp_ue(rbsp
) % 5;
680 pps
= pic_parameter_set_id(priv
, rbsp
);
688 if (pps
!= priv
->picture
.h264
.pps
)
689 vid_dec_h264_EndFrame(priv
);
691 priv
->picture
.h264
.pps
= pps
;
693 if (sps
->separate_colour_plane_flag
== 1 )
694 /* colour_plane_id */
697 frame_num
= vl_rbsp_u(rbsp
, sps
->log2_max_frame_num_minus4
+ 4);
699 if (frame_num
!= priv
->picture
.h264
.frame_num
)
700 vid_dec_h264_EndFrame(priv
);
702 prevFrameNum
= priv
->picture
.h264
.frame_num
;
703 priv
->picture
.h264
.frame_num
= frame_num
;
705 priv
->picture
.h264
.field_pic_flag
= 0;
706 priv
->picture
.h264
.bottom_field_flag
= 0;
708 if (!sps
->frame_mbs_only_flag
) {
709 unsigned field_pic_flag
= vl_rbsp_u(rbsp
, 1);
711 if (!field_pic_flag
&& field_pic_flag
!= priv
->picture
.h264
.field_pic_flag
)
712 vid_dec_h264_EndFrame(priv
);
714 priv
->picture
.h264
.field_pic_flag
= field_pic_flag
;
716 if (priv
->picture
.h264
.field_pic_flag
) {
717 unsigned bottom_field_flag
= vl_rbsp_u(rbsp
, 1);
719 if (bottom_field_flag
!= priv
->picture
.h264
.bottom_field_flag
)
720 vid_dec_h264_EndFrame(priv
);
722 priv
->picture
.h264
.bottom_field_flag
= bottom_field_flag
;
727 unsigned idr_pic_id
= vl_rbsp_ue(rbsp
);
729 if (idr_pic_id
!= priv
->codec_data
.h264
.idr_pic_id
)
730 vid_dec_h264_EndFrame(priv
);
732 priv
->codec_data
.h264
.idr_pic_id
= idr_pic_id
;
735 if (sps
->pic_order_cnt_type
== 0) {
736 unsigned log2_max_pic_order_cnt_lsb
= sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4;
737 unsigned max_pic_order_cnt_lsb
= 1 << log2_max_pic_order_cnt_lsb
;
738 unsigned pic_order_cnt_lsb
= vl_rbsp_u(rbsp
, log2_max_pic_order_cnt_lsb
);
739 unsigned pic_order_cnt_msb
;
741 if (pic_order_cnt_lsb
!= priv
->codec_data
.h264
.pic_order_cnt_lsb
)
742 vid_dec_h264_EndFrame(priv
);
745 priv
->codec_data
.h264
.pic_order_cnt_msb
= 0;
746 priv
->codec_data
.h264
.pic_order_cnt_lsb
= 0;
749 if ((pic_order_cnt_lsb
< priv
->codec_data
.h264
.pic_order_cnt_lsb
) &&
750 (priv
->codec_data
.h264
.pic_order_cnt_lsb
- pic_order_cnt_lsb
) >= (max_pic_order_cnt_lsb
/ 2))
751 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
+ max_pic_order_cnt_lsb
;
753 else if ((pic_order_cnt_lsb
> priv
->codec_data
.h264
.pic_order_cnt_lsb
) &&
754 (pic_order_cnt_lsb
- priv
->codec_data
.h264
.pic_order_cnt_lsb
) > (max_pic_order_cnt_lsb
/ 2))
755 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
- max_pic_order_cnt_lsb
;
758 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
;
760 priv
->codec_data
.h264
.pic_order_cnt_msb
= pic_order_cnt_msb
;
761 priv
->codec_data
.h264
.pic_order_cnt_lsb
= pic_order_cnt_lsb
;
763 if (pps
->bottom_field_pic_order_in_frame_present_flag
&& !priv
->picture
.h264
.field_pic_flag
) {
764 unsigned delta_pic_order_cnt_bottom
= vl_rbsp_se(rbsp
);
766 if (delta_pic_order_cnt_bottom
!= priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
)
767 vid_dec_h264_EndFrame(priv
);
769 priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
= delta_pic_order_cnt_bottom
;
772 if (!priv
->picture
.h264
.field_pic_flag
) {
773 priv
->picture
.h264
.field_order_cnt
[0] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
774 priv
->picture
.h264
.field_order_cnt
[1] = priv
->picture
.h264
.field_order_cnt
[0] +
775 priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
;
776 } else if (!priv
->picture
.h264
.bottom_field_flag
)
777 priv
->picture
.h264
.field_order_cnt
[0] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
779 priv
->picture
.h264
.field_order_cnt
[1] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
781 } else if (sps
->pic_order_cnt_type
== 1) {
782 unsigned MaxFrameNum
= 1 << (sps
->log2_max_frame_num_minus4
+ 4);
783 unsigned FrameNumOffset
, absFrameNum
, expectedPicOrderCnt
;
785 if (!sps
->delta_pic_order_always_zero_flag
) {
786 unsigned delta_pic_order_cnt
[2];
788 delta_pic_order_cnt
[0] = vl_rbsp_se(rbsp
);
790 if (delta_pic_order_cnt
[0] != priv
->codec_data
.h264
.delta_pic_order_cnt
[0])
791 vid_dec_h264_EndFrame(priv
);
793 priv
->codec_data
.h264
.delta_pic_order_cnt
[0] = delta_pic_order_cnt
[0];
795 if (pps
->bottom_field_pic_order_in_frame_present_flag
&& !priv
->picture
.h264
.field_pic_flag
) {
796 delta_pic_order_cnt
[1] = vl_rbsp_se(rbsp
);
798 if (delta_pic_order_cnt
[1] != priv
->codec_data
.h264
.delta_pic_order_cnt
[1])
799 vid_dec_h264_EndFrame(priv
);
801 priv
->codec_data
.h264
.delta_pic_order_cnt
[1] = delta_pic_order_cnt
[1];
807 else if (prevFrameNum
> frame_num
)
808 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
+ MaxFrameNum
;
810 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
;
812 priv
->codec_data
.h264
.prevFrameNumOffset
= FrameNumOffset
;
814 if (sps
->num_ref_frames_in_pic_order_cnt_cycle
!= 0)
815 absFrameNum
= FrameNumOffset
+ frame_num
;
819 if (nal_ref_idc
== 0 && absFrameNum
> 0)
820 absFrameNum
= absFrameNum
- 1;
822 if (absFrameNum
> 0) {
823 unsigned picOrderCntCycleCnt
= (absFrameNum
- 1) / sps
->num_ref_frames_in_pic_order_cnt_cycle
;
824 unsigned frameNumInPicOrderCntCycle
= (absFrameNum
- 1) % sps
->num_ref_frames_in_pic_order_cnt_cycle
;
825 signed ExpectedDeltaPerPicOrderCntCycle
= 0;
828 for (i
= 0; i
< sps
->num_ref_frames_in_pic_order_cnt_cycle
; ++i
)
829 ExpectedDeltaPerPicOrderCntCycle
+= sps
->offset_for_ref_frame
[i
];
831 expectedPicOrderCnt
= picOrderCntCycleCnt
* ExpectedDeltaPerPicOrderCntCycle
;
832 for (i
= 0; i
<= frameNumInPicOrderCntCycle
; ++i
)
833 expectedPicOrderCnt
+= sps
->offset_for_ref_frame
[i
];
836 expectedPicOrderCnt
= 0;
838 if (nal_ref_idc
== 0)
839 expectedPicOrderCnt
+= sps
->offset_for_non_ref_pic
;
841 if (!priv
->picture
.h264
.field_pic_flag
) {
842 priv
->picture
.h264
.field_order_cnt
[0] = expectedPicOrderCnt
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
843 priv
->picture
.h264
.field_order_cnt
[1] = priv
->picture
.h264
.field_order_cnt
[0] +
844 sps
->offset_for_top_to_bottom_field
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[1];
846 } else if (!priv
->picture
.h264
.bottom_field_flag
)
847 priv
->picture
.h264
.field_order_cnt
[0] = expectedPicOrderCnt
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
849 priv
->picture
.h264
.field_order_cnt
[1] = expectedPicOrderCnt
+ sps
->offset_for_top_to_bottom_field
+
850 priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
852 } else if (sps
->pic_order_cnt_type
== 2) {
853 unsigned MaxFrameNum
= 1 << (sps
->log2_max_frame_num_minus4
+ 4);
854 unsigned FrameNumOffset
, tempPicOrderCnt
;
858 else if (prevFrameNum
> frame_num
)
859 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
+ MaxFrameNum
;
861 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
;
863 priv
->codec_data
.h264
.prevFrameNumOffset
= FrameNumOffset
;
867 else if (nal_ref_idc
== 0)
868 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
) - 1;
870 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
);
872 if (!priv
->picture
.h264
.field_pic_flag
) {
873 priv
->picture
.h264
.field_order_cnt
[0] = tempPicOrderCnt
;
874 priv
->picture
.h264
.field_order_cnt
[1] = tempPicOrderCnt
;
876 } else if (!priv
->picture
.h264
.bottom_field_flag
)
877 priv
->picture
.h264
.field_order_cnt
[0] = tempPicOrderCnt
;
879 priv
->picture
.h264
.field_order_cnt
[1] = tempPicOrderCnt
;
882 if (pps
->redundant_pic_cnt_present_flag
)
883 /* redundant_pic_cnt */
886 if (slice_type
== PIPE_H264_SLICE_TYPE_B
)
887 /* direct_spatial_mv_pred_flag */
890 priv
->picture
.h264
.num_ref_idx_l0_active_minus1
= pps
->num_ref_idx_l0_default_active_minus1
;
891 priv
->picture
.h264
.num_ref_idx_l1_active_minus1
= pps
->num_ref_idx_l1_default_active_minus1
;
893 if (slice_type
== PIPE_H264_SLICE_TYPE_P
||
894 slice_type
== PIPE_H264_SLICE_TYPE_SP
||
895 slice_type
== PIPE_H264_SLICE_TYPE_B
) {
897 /* num_ref_idx_active_override_flag */
898 if (vl_rbsp_u(rbsp
, 1)) {
899 priv
->picture
.h264
.num_ref_idx_l0_active_minus1
= vl_rbsp_ue(rbsp
);
901 if (slice_type
== PIPE_H264_SLICE_TYPE_B
)
902 priv
->picture
.h264
.num_ref_idx_l1_active_minus1
= vl_rbsp_ue(rbsp
);
906 if (nal_unit_type
== 20 || nal_unit_type
== 21)
907 ref_pic_list_mvc_modification(priv
, rbsp
);
909 ref_pic_list_modification(priv
, rbsp
, slice_type
);
911 if ((pps
->weighted_pred_flag
&& (slice_type
== PIPE_H264_SLICE_TYPE_P
|| slice_type
== PIPE_H264_SLICE_TYPE_SP
)) ||
912 (pps
->weighted_bipred_idc
== 1 && slice_type
== PIPE_H264_SLICE_TYPE_B
))
913 pred_weight_table(priv
, rbsp
, sps
, slice_type
);
915 if (nal_ref_idc
!= 0)
916 dec_ref_pic_marking(priv
, rbsp
, IdrPicFlag
);
918 if (pps
->entropy_coding_mode_flag
&& slice_type
!= PIPE_H264_SLICE_TYPE_I
&& slice_type
!= PIPE_H264_SLICE_TYPE_SI
)
925 if (slice_type
== PIPE_H264_SLICE_TYPE_SP
|| slice_type
== PIPE_H264_SLICE_TYPE_SI
) {
926 if (slice_type
== PIPE_H264_SLICE_TYPE_SP
)
927 /* sp_for_switch_flag */
934 if (pps
->deblocking_filter_control_present_flag
) {
935 unsigned disable_deblocking_filter_idc
= vl_rbsp_ue(rbsp
);
937 if (disable_deblocking_filter_idc
!= 1) {
938 /* slice_alpha_c0_offset_div2 */
941 /* slice_beta_offset_div2 */
946 if (pps
->num_slice_groups_minus1
> 0 && pps
->slice_group_map_type
>= 3 && pps
->slice_group_map_type
<= 5)
947 /* slice_group_change_cycle */
951 static void vid_dec_h264_Decode(vid_dec_PrivateType
*priv
, struct vl_vlc
*vlc
, unsigned min_bits_left
)
953 unsigned nal_ref_idc
, nal_unit_type
;
955 if (!vl_vlc_search_byte(vlc
, vl_vlc_bits_left(vlc
) - min_bits_left
, 0x00))
958 if (vl_vlc_peekbits(vlc
, 24) != 0x000001) {
959 vl_vlc_eatbits(vlc
, 8);
964 unsigned bytes
= priv
->bytes_left
- (vl_vlc_bits_left(vlc
) / 8);
965 ++priv
->picture
.h264
.slice_count
;
966 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
, &priv
->picture
.base
,
967 1, &priv
->slice
, &bytes
);
971 vl_vlc_eatbits(vlc
, 24);
973 /* forbidden_zero_bit */
974 vl_vlc_eatbits(vlc
, 1);
976 nal_ref_idc
= vl_vlc_get_uimsbf(vlc
, 2);
978 if (nal_ref_idc
!= priv
->codec_data
.h264
.nal_ref_idc
&&
979 (nal_ref_idc
* priv
->codec_data
.h264
.nal_ref_idc
) == 0)
980 vid_dec_h264_EndFrame(priv
);
982 priv
->codec_data
.h264
.nal_ref_idc
= nal_ref_idc
;
984 nal_unit_type
= vl_vlc_get_uimsbf(vlc
, 5);
986 if (nal_unit_type
!= 1 && nal_unit_type
!= 5)
987 vid_dec_h264_EndFrame(priv
);
989 if (nal_unit_type
== 7) {
991 vl_rbsp_init(&rbsp
, vlc
, ~0);
992 seq_parameter_set(priv
, &rbsp
);
994 } else if (nal_unit_type
== 8) {
996 vl_rbsp_init(&rbsp
, vlc
, ~0);
997 picture_parameter_set(priv
, &rbsp
);
999 } else if (nal_unit_type
== 1 || nal_unit_type
== 5) {
1000 /* Coded slice of a non-IDR or IDR picture */
1001 unsigned bits
= vl_vlc_valid_bits(vlc
);
1002 unsigned bytes
= bits
/ 8 + 4;
1003 struct vl_rbsp rbsp
;
1005 const void *ptr
= buf
;
1011 buf
[3] = (nal_ref_idc
<< 5) | nal_unit_type
;
1012 for (i
= 4; i
< bytes
; ++i
)
1013 buf
[i
] = vl_vlc_peekbits(vlc
, bits
) >> ((bytes
- i
- 1) * 8);
1015 priv
->bytes_left
= (vl_vlc_bits_left(vlc
) - bits
) / 8;
1016 priv
->slice
= vlc
->data
;
1018 vl_rbsp_init(&rbsp
, vlc
, 128);
1019 slice_header(priv
, &rbsp
, nal_ref_idc
, nal_unit_type
);
1021 vid_dec_h264_BeginFrame(priv
);
1023 ++priv
->picture
.h264
.slice_count
;
1024 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
, &priv
->picture
.base
,
1028 /* resync to byte boundary */
1029 vl_vlc_eatbits(vlc
, vl_vlc_valid_bits(vlc
) % 8);