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
;
51 static const uint8_t Default_4x4_Intra
[16] = {
52 6, 13, 13, 20, 20, 20, 28, 28,
53 28, 28, 32, 32, 32, 37, 37, 42
56 static const uint8_t Default_4x4_Inter
[16] = {
57 10, 14, 14, 20, 20, 20, 24, 24,
58 24, 24, 27, 27, 27, 30, 30, 34
61 static const uint8_t Default_8x8_Intra
[64] = {
62 6, 10, 10, 13, 11, 13, 16, 16,
63 16, 16, 18, 18, 18, 18, 18, 23,
64 23, 23, 23, 23, 23, 25, 25, 25,
65 25, 25, 25, 25, 27, 27, 27, 27,
66 27, 27, 27, 27, 29, 29, 29, 29,
67 29, 29, 29, 31, 31, 31, 31, 31,
68 31, 33, 33, 33, 33, 33, 36, 36,
69 36, 36, 38, 38, 38, 40, 40, 42
72 static const uint8_t Default_8x8_Inter
[64] = {
73 9, 13, 13, 15, 13, 15, 17, 17,
74 17, 17, 19, 19, 19, 19, 19, 21,
75 21, 21, 21, 21, 21, 22, 22, 22,
76 22, 22, 22, 22, 24, 24, 24, 24,
77 24, 24, 24, 24, 25, 25, 25, 25,
78 25, 25, 25, 27, 27, 27, 27, 27,
79 27, 28, 28, 28, 28, 28, 30, 30,
80 30, 30, 32, 32, 32, 33, 33, 35
83 static void vid_dec_h264_Decode(vid_dec_PrivateType
*priv
, struct vl_vlc
*vlc
, unsigned min_bits_left
);
84 static void vid_dec_h264_EndFrame(vid_dec_PrivateType
*priv
);
85 static struct pipe_video_buffer
*vid_dec_h264_Flush(vid_dec_PrivateType
*priv
);
87 void vid_dec_h264_Init(vid_dec_PrivateType
*priv
)
89 priv
->picture
.base
.profile
= PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH
;
91 priv
->Decode
= vid_dec_h264_Decode
;
92 priv
->EndFrame
= vid_dec_h264_EndFrame
;
93 priv
->Flush
= vid_dec_h264_Flush
;
95 LIST_INITHEAD(&priv
->codec_data
.h264
.dpb_list
);
96 priv
->picture
.h264
.field_order_cnt
[0] = priv
->picture
.h264
.field_order_cnt
[1] = INT_MAX
;
99 static void vid_dec_h264_BeginFrame(vid_dec_PrivateType
*priv
)
101 //TODO: sane buffer handling
103 if (priv
->frame_started
)
106 vid_dec_NeedTarget(priv
);
108 priv
->picture
.h264
.num_ref_frames
= priv
->picture
.h264
.pps
->sps
->max_num_ref_frames
;
111 struct pipe_video_codec templat
= {};
112 omx_base_video_PortType
*port
;
114 port
= (omx_base_video_PortType
*)priv
->ports
[OMX_BASE_FILTER_INPUTPORT_INDEX
];
115 templat
.profile
= priv
->profile
;
116 templat
.entrypoint
= PIPE_VIDEO_ENTRYPOINT_BITSTREAM
;
117 templat
.chroma_format
= PIPE_VIDEO_CHROMA_FORMAT_420
;
118 templat
.max_references
= priv
->picture
.h264
.num_ref_frames
;
119 templat
.expect_chunked_decode
= true;
120 templat
.width
= port
->sPortParam
.format
.video
.nFrameWidth
;
121 templat
.height
= port
->sPortParam
.format
.video
.nFrameHeight
;
122 templat
.level
= priv
->picture
.h264
.pps
->sps
->level_idc
;
124 priv
->codec
= priv
->pipe
->create_video_codec(priv
->pipe
, &templat
);
126 priv
->codec
->begin_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
127 priv
->frame_started
= true;
130 static struct pipe_video_buffer
*vid_dec_h264_Flush(vid_dec_PrivateType
*priv
)
132 struct dpb_list
*entry
, *result
= NULL
;
133 struct pipe_video_buffer
*buf
;
135 /* search for the lowest poc and break on zeros */
136 LIST_FOR_EACH_ENTRY(entry
, &priv
->codec_data
.h264
.dpb_list
, list
) {
138 if (result
&& entry
->poc
== 0)
141 if (!result
|| entry
->poc
< result
->poc
)
148 buf
= result
->buffer
;
150 --priv
->codec_data
.h264
.dpb_num
;
151 LIST_DEL(&result
->list
);
157 static void vid_dec_h264_EndFrame(vid_dec_PrivateType
*priv
)
159 struct dpb_list
*entry
;
160 struct pipe_video_buffer
*tmp
;
161 bool top_field_first
;
163 if (!priv
->frame_started
)
166 priv
->codec
->end_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
167 priv
->frame_started
= false;
169 // TODO: implement frame number handling
170 priv
->picture
.h264
.frame_num_list
[0] = priv
->picture
.h264
.frame_num
;
171 priv
->picture
.h264
.field_order_cnt_list
[0][0] = priv
->picture
.h264
.frame_num
;
172 priv
->picture
.h264
.field_order_cnt_list
[0][1] = priv
->picture
.h264
.frame_num
;
174 top_field_first
= priv
->picture
.h264
.field_order_cnt
[0] < priv
->picture
.h264
.field_order_cnt
[1];
176 if (priv
->picture
.h264
.field_pic_flag
&& priv
->picture
.h264
.bottom_field_flag
!= top_field_first
)
179 /* add the decoded picture to the dpb list */
180 entry
= CALLOC_STRUCT(dpb_list
);
184 entry
->buffer
= priv
->target
;
185 entry
->poc
= MIN2(priv
->picture
.h264
.field_order_cnt
[0], priv
->picture
.h264
.field_order_cnt
[1]);
186 LIST_ADDTAIL(&entry
->list
, &priv
->codec_data
.h264
.dpb_list
);
187 ++priv
->codec_data
.h264
.dpb_num
;
189 priv
->picture
.h264
.field_order_cnt
[0] = priv
->picture
.h264
.field_order_cnt
[1] = INT_MAX
;
191 if (priv
->codec_data
.h264
.dpb_num
<= DPB_MAX_SIZE
)
194 tmp
= priv
->in_buffers
[0]->pInputPortPrivate
;
195 priv
->in_buffers
[0]->pInputPortPrivate
= vid_dec_h264_Flush(priv
);
197 priv
->frame_finished
= priv
->in_buffers
[0]->pInputPortPrivate
!= NULL
;
200 static void vui_parameters(struct vl_rbsp
*rbsp
)
205 static void scaling_list(struct vl_rbsp
*rbsp
, uint8_t *scalingList
, unsigned sizeOfScalingList
,
206 const uint8_t *defaultList
, const uint8_t *fallbackList
)
208 unsigned lastScale
= 8, nextScale
= 8;
212 /* (pic|seq)_scaling_list_present_flag[i] */
213 if (!vl_rbsp_u(rbsp
, 1)) {
215 memcpy(scalingList
, fallbackList
, sizeOfScalingList
);
219 list
= (sizeOfScalingList
== 16) ? vl_zscan_normal_16
: vl_zscan_normal
;
220 for (i
= 0; i
< sizeOfScalingList
; ++i
) {
222 if (nextScale
!= 0) {
223 signed delta_scale
= vl_rbsp_se(rbsp
);
224 nextScale
= (lastScale
+ delta_scale
+ 256) % 256;
225 if (i
== 0 && nextScale
== 0) {
226 memcpy(scalingList
, defaultList
, sizeOfScalingList
);
230 scalingList
[list
[i
]] = nextScale
== 0 ? lastScale
: nextScale
;
231 lastScale
= scalingList
[list
[i
]];
235 static struct pipe_h264_sps
*seq_parameter_set_id(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
237 unsigned id
= vl_rbsp_ue(rbsp
);
238 if (id
>= Elements(priv
->codec_data
.h264
.sps
))
239 return NULL
; /* invalid seq_parameter_set_id */
241 return &priv
->codec_data
.h264
.sps
[id
];
244 static void seq_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
246 struct pipe_h264_sps
*sps
;
247 unsigned profile_idc
, level_idc
;
250 /* Sequence parameter set */
251 profile_idc
= vl_rbsp_u(rbsp
, 8);
253 /* constraint_set0_flag */
256 /* constraint_set1_flag */
259 /* constraint_set2_flag */
262 /* constraint_set3_flag */
265 /* constraint_set4_flag */
268 /* constraint_set5_flag */
271 /* reserved_zero_2bits */
275 level_idc
= vl_rbsp_u(rbsp
, 8);
277 sps
= seq_parameter_set_id(priv
, rbsp
);
281 memset(sps
, 0, sizeof(*sps
));
282 memset(sps
->ScalingList4x4
, 16, sizeof(sps
->ScalingList4x4
));
283 memset(sps
->ScalingList8x8
, 16, sizeof(sps
->ScalingList8x8
));
285 sps
->level_idc
= level_idc
;
287 if (profile_idc
== 100 || profile_idc
== 110 || profile_idc
== 122 || profile_idc
== 244 ||
288 profile_idc
== 44 || profile_idc
== 83 || profile_idc
== 86 || profile_idc
== 118 ||
289 profile_idc
== 128 || profile_idc
== 138) {
291 sps
->chroma_format_idc
= vl_rbsp_ue(rbsp
);
293 if (sps
->chroma_format_idc
== 3)
294 sps
->separate_colour_plane_flag
= vl_rbsp_u(rbsp
, 1);
296 sps
->bit_depth_luma_minus8
= vl_rbsp_ue(rbsp
);
298 sps
->bit_depth_chroma_minus8
= vl_rbsp_ue(rbsp
);
300 /* qpprime_y_zero_transform_bypass_flag */
303 sps
->seq_scaling_matrix_present_flag
= vl_rbsp_u(rbsp
, 1);
304 if (sps
->seq_scaling_matrix_present_flag
) {
306 scaling_list(rbsp
, sps
->ScalingList4x4
[0], 16, Default_4x4_Intra
, Default_4x4_Intra
);
307 scaling_list(rbsp
, sps
->ScalingList4x4
[1], 16, Default_4x4_Intra
, sps
->ScalingList4x4
[0]);
308 scaling_list(rbsp
, sps
->ScalingList4x4
[2], 16, Default_4x4_Intra
, sps
->ScalingList4x4
[1]);
309 scaling_list(rbsp
, sps
->ScalingList4x4
[3], 16, Default_4x4_Inter
, Default_4x4_Inter
);
310 scaling_list(rbsp
, sps
->ScalingList4x4
[4], 16, Default_4x4_Inter
, sps
->ScalingList4x4
[3]);
311 scaling_list(rbsp
, sps
->ScalingList4x4
[5], 16, Default_4x4_Inter
, sps
->ScalingList4x4
[4]);
313 scaling_list(rbsp
, sps
->ScalingList8x8
[0], 64, Default_8x8_Intra
, Default_8x8_Intra
);
314 scaling_list(rbsp
, sps
->ScalingList8x8
[1], 64, Default_8x8_Inter
, Default_8x8_Inter
);
315 if (sps
->chroma_format_idc
== 3) {
316 scaling_list(rbsp
, sps
->ScalingList8x8
[2], 64, Default_8x8_Intra
, sps
->ScalingList8x8
[0]);
317 scaling_list(rbsp
, sps
->ScalingList8x8
[3], 64, Default_8x8_Inter
, sps
->ScalingList8x8
[1]);
318 scaling_list(rbsp
, sps
->ScalingList8x8
[4], 64, Default_8x8_Intra
, sps
->ScalingList8x8
[2]);
319 scaling_list(rbsp
, sps
->ScalingList8x8
[5], 64, Default_8x8_Inter
, sps
->ScalingList8x8
[3]);
322 } else if (profile_idc
== 183)
323 sps
->chroma_format_idc
= 0;
325 sps
->chroma_format_idc
= 1;
327 sps
->log2_max_frame_num_minus4
= vl_rbsp_ue(rbsp
);
329 sps
->pic_order_cnt_type
= vl_rbsp_ue(rbsp
);
331 if (sps
->pic_order_cnt_type
== 0)
332 sps
->log2_max_pic_order_cnt_lsb_minus4
= vl_rbsp_ue(rbsp
);
333 else if (sps
->pic_order_cnt_type
== 1) {
334 sps
->delta_pic_order_always_zero_flag
= vl_rbsp_u(rbsp
, 1);
336 sps
->offset_for_non_ref_pic
= vl_rbsp_se(rbsp
);
338 sps
->offset_for_top_to_bottom_field
= vl_rbsp_se(rbsp
);
340 sps
->num_ref_frames_in_pic_order_cnt_cycle
= vl_rbsp_ue(rbsp
);
342 for (i
= 0; i
< sps
->num_ref_frames_in_pic_order_cnt_cycle
; ++i
)
343 sps
->offset_for_ref_frame
[i
] = vl_rbsp_se(rbsp
);
346 sps
->max_num_ref_frames
= vl_rbsp_ue(rbsp
);
348 /* gaps_in_frame_num_value_allowed_flag */
351 /* pic_width_in_mbs_minus1 */
354 /* pic_height_in_map_units_minus1 */
357 sps
->frame_mbs_only_flag
= vl_rbsp_u(rbsp
, 1);
358 if (!sps
->frame_mbs_only_flag
)
359 sps
->mb_adaptive_frame_field_flag
= vl_rbsp_u(rbsp
, 1);
361 sps
->direct_8x8_inference_flag
= vl_rbsp_u(rbsp
, 1);
363 /* frame_cropping_flag */
364 if (vl_rbsp_u(rbsp
, 1)) {
365 /* frame_crop_left_offset */
368 /* frame_crop_right_offset */
371 /* frame_crop_top_offset */
374 /* frame_crop_bottom_offset */
378 /* vui_parameters_present_flag */
379 if (vl_rbsp_u(rbsp
, 1))
380 vui_parameters(rbsp
);
383 static struct pipe_h264_pps
*pic_parameter_set_id(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
385 unsigned id
= vl_rbsp_ue(rbsp
);
386 if (id
>= Elements(priv
->codec_data
.h264
.pps
))
387 return NULL
; /* invalid pic_parameter_set_id */
389 return &priv
->codec_data
.h264
.pps
[id
];
392 static void picture_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
394 struct pipe_h264_sps
*sps
;
395 struct pipe_h264_pps
*pps
;
398 pps
= pic_parameter_set_id(priv
, rbsp
);
402 memset(pps
, 0, sizeof(*pps
));
404 sps
= pps
->sps
= seq_parameter_set_id(priv
, rbsp
);
408 memcpy(pps
->ScalingList4x4
, sps
->ScalingList4x4
, sizeof(pps
->ScalingList4x4
));
409 memcpy(pps
->ScalingList8x8
, sps
->ScalingList8x8
, sizeof(pps
->ScalingList8x8
));
411 pps
->entropy_coding_mode_flag
= vl_rbsp_u(rbsp
, 1);
413 pps
->bottom_field_pic_order_in_frame_present_flag
= vl_rbsp_u(rbsp
, 1);
415 pps
->num_slice_groups_minus1
= vl_rbsp_ue(rbsp
);
416 if (pps
->num_slice_groups_minus1
> 0) {
417 pps
->slice_group_map_type
= vl_rbsp_ue(rbsp
);
419 if (pps
->slice_group_map_type
== 0) {
421 for (i
= 0; i
<= pps
->num_slice_groups_minus1
; ++i
)
422 /* run_length_minus1[i] */
425 } else if (pps
->slice_group_map_type
== 2) {
427 for (i
= 0; i
<= pps
->num_slice_groups_minus1
; ++i
) {
431 /* bottom_right[i] */
435 } else if (pps
->slice_group_map_type
>= 3 && pps
->slice_group_map_type
<= 5) {
437 /* slice_group_change_direction_flag */
440 pps
->slice_group_change_rate_minus1
= vl_rbsp_ue(rbsp
);
442 } else if (pps
->slice_group_map_type
== 6) {
444 unsigned pic_size_in_map_units_minus1
;
446 pic_size_in_map_units_minus1
= vl_rbsp_ue(rbsp
);
448 for (i
= 0; i
<= pic_size_in_map_units_minus1
; ++i
)
449 /* slice_group_id[i] */
450 vl_rbsp_u(rbsp
, log2(pps
->num_slice_groups_minus1
+ 1));
454 pps
->num_ref_idx_l0_default_active_minus1
= vl_rbsp_ue(rbsp
);
456 pps
->num_ref_idx_l1_default_active_minus1
= vl_rbsp_ue(rbsp
);
458 pps
->weighted_pred_flag
= vl_rbsp_u(rbsp
, 1);
460 pps
->weighted_bipred_idc
= vl_rbsp_u(rbsp
, 2);
462 pps
->pic_init_qp_minus26
= vl_rbsp_se(rbsp
);
464 /* pic_init_qs_minus26 */
467 pps
->chroma_qp_index_offset
= vl_rbsp_se(rbsp
);
469 pps
->deblocking_filter_control_present_flag
= vl_rbsp_u(rbsp
, 1);
471 pps
->constrained_intra_pred_flag
= vl_rbsp_u(rbsp
, 1);
473 pps
->redundant_pic_cnt_present_flag
= vl_rbsp_u(rbsp
, 1);
475 if (vl_rbsp_more_data(rbsp
)) {
476 pps
->transform_8x8_mode_flag
= vl_rbsp_u(rbsp
, 1);
478 /* pic_scaling_matrix_present_flag */
479 if (vl_rbsp_u(rbsp
, 1)) {
481 scaling_list(rbsp
, pps
->ScalingList4x4
[0], 16, Default_4x4_Intra
,
482 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_4x4_Intra
);
483 scaling_list(rbsp
, pps
->ScalingList4x4
[1], 16, Default_4x4_Intra
, pps
->ScalingList4x4
[0]);
484 scaling_list(rbsp
, pps
->ScalingList4x4
[2], 16, Default_4x4_Intra
, pps
->ScalingList4x4
[1]);
485 scaling_list(rbsp
, pps
->ScalingList4x4
[3], 16, Default_4x4_Inter
,
486 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_4x4_Inter
);
487 scaling_list(rbsp
, pps
->ScalingList4x4
[4], 16, Default_4x4_Inter
, pps
->ScalingList4x4
[3]);
488 scaling_list(rbsp
, pps
->ScalingList4x4
[5], 16, Default_4x4_Inter
, pps
->ScalingList4x4
[4]);
490 if (pps
->transform_8x8_mode_flag
) {
491 scaling_list(rbsp
, pps
->ScalingList8x8
[0], 64, Default_8x8_Intra
,
492 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_8x8_Intra
);
493 scaling_list(rbsp
, pps
->ScalingList8x8
[1], 64, Default_8x8_Inter
,
494 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_8x8_Inter
);
495 if (sps
->chroma_format_idc
== 3) {
496 scaling_list(rbsp
, pps
->ScalingList8x8
[2], 64, Default_8x8_Intra
, pps
->ScalingList8x8
[0]);
497 scaling_list(rbsp
, pps
->ScalingList8x8
[3], 64, Default_8x8_Inter
, pps
->ScalingList8x8
[1]);
498 scaling_list(rbsp
, pps
->ScalingList8x8
[4], 64, Default_8x8_Intra
, pps
->ScalingList8x8
[2]);
499 scaling_list(rbsp
, pps
->ScalingList8x8
[5], 64, Default_8x8_Inter
, pps
->ScalingList8x8
[3]);
504 pps
->second_chroma_qp_index_offset
= vl_rbsp_se(rbsp
);
508 static void ref_pic_list_mvc_modification(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
514 static void ref_pic_list_modification(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
515 enum pipe_h264_slice_type slice_type
)
517 unsigned modification_of_pic_nums_idc
;
519 if (slice_type
!= 2 && slice_type
!= 4) {
520 /* ref_pic_list_modification_flag_l0 */
521 if (vl_rbsp_u(rbsp
, 1)) {
523 modification_of_pic_nums_idc
= vl_rbsp_ue(rbsp
);
524 if (modification_of_pic_nums_idc
== 0 ||
525 modification_of_pic_nums_idc
== 1)
526 /* abs_diff_pic_num_minus1 */
528 else if (modification_of_pic_nums_idc
== 2)
529 /* long_term_pic_num */
531 } while (modification_of_pic_nums_idc
!= 3);
535 if (slice_type
== 1) {
536 /* ref_pic_list_modification_flag_l1 */
537 if (vl_rbsp_u(rbsp
, 1)) {
539 modification_of_pic_nums_idc
= vl_rbsp_ue(rbsp
);
540 if (modification_of_pic_nums_idc
== 0 ||
541 modification_of_pic_nums_idc
== 1)
542 /* abs_diff_pic_num_minus1 */
544 else if (modification_of_pic_nums_idc
== 2)
545 /* long_term_pic_num */
547 } while (modification_of_pic_nums_idc
!= 3);
552 static void pred_weight_table(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
553 struct pipe_h264_sps
*sps
, enum pipe_h264_slice_type slice_type
)
555 unsigned ChromaArrayType
= sps
->separate_colour_plane_flag
? 0 : sps
->chroma_format_idc
;
558 /* luma_log2_weight_denom */
561 if (ChromaArrayType
!= 0)
562 /* chroma_log2_weight_denom */
565 for (i
= 0; i
<= priv
->picture
.h264
.num_ref_idx_l0_active_minus1
; ++i
) {
566 /* luma_weight_l0_flag */
567 if (vl_rbsp_u(rbsp
, 1)) {
568 /* luma_weight_l0[i] */
570 /* luma_offset_l0[i] */
573 if (ChromaArrayType
!= 0) {
574 /* chroma_weight_l0_flag */
575 if (vl_rbsp_u(rbsp
, 1)) {
576 for (j
= 0; j
< 2; ++j
) {
577 /* chroma_weight_l0[i][j] */
579 /* chroma_offset_l0[i][j] */
586 if (slice_type
== 1) {
587 for (i
= 0; i
<= priv
->picture
.h264
.num_ref_idx_l1_active_minus1
; ++i
) {
588 /* luma_weight_l1_flag */
589 if (vl_rbsp_u(rbsp
, 1)) {
590 /* luma_weight_l1[i] */
592 /* luma_offset_l1[i] */
595 if (ChromaArrayType
!= 0) {
596 /* chroma_weight_l1_flag */
597 if (vl_rbsp_u(rbsp
, 1)) {
598 for (j
= 0; j
< 2; ++j
) {
599 /* chroma_weight_l1[i][j] */
601 /* chroma_offset_l1[i][j] */
610 static void dec_ref_pic_marking(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
613 unsigned memory_management_control_operation
;
616 /* no_output_of_prior_pics_flag */
618 /* long_term_reference_flag */
621 /* adaptive_ref_pic_marking_mode_flag */
622 if (vl_rbsp_u(rbsp
, 1)) {
624 memory_management_control_operation
= vl_rbsp_ue(rbsp
);
626 if (memory_management_control_operation
== 1 ||
627 memory_management_control_operation
== 3)
628 /* difference_of_pic_nums_minus1 */
631 if (memory_management_control_operation
== 2)
632 /* long_term_pic_num */
635 if (memory_management_control_operation
== 3 ||
636 memory_management_control_operation
== 6)
637 /* long_term_frame_idx */
640 if (memory_management_control_operation
== 4)
641 /* max_long_term_frame_idx_plus1 */
643 } while (memory_management_control_operation
!= 0);
648 static void slice_header(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
649 unsigned nal_ref_idc
, unsigned nal_unit_type
)
651 enum pipe_h264_slice_type slice_type
;
652 struct pipe_h264_pps
*pps
;
653 struct pipe_h264_sps
*sps
;
654 unsigned frame_num
, prevFrameNum
;
655 bool IdrPicFlag
= nal_unit_type
== 5;
657 if (IdrPicFlag
!= priv
->codec_data
.h264
.IdrPicFlag
)
658 vid_dec_h264_EndFrame(priv
);
660 priv
->codec_data
.h264
.IdrPicFlag
= IdrPicFlag
;
662 /* first_mb_in_slice */
665 slice_type
= vl_rbsp_ue(rbsp
) % 5;
667 pps
= pic_parameter_set_id(priv
, rbsp
);
675 if (pps
!= priv
->picture
.h264
.pps
)
676 vid_dec_h264_EndFrame(priv
);
678 priv
->picture
.h264
.pps
= pps
;
680 if (sps
->separate_colour_plane_flag
== 1 )
681 /* colour_plane_id */
684 frame_num
= vl_rbsp_u(rbsp
, sps
->log2_max_frame_num_minus4
+ 4);
686 if (frame_num
!= priv
->picture
.h264
.frame_num
)
687 vid_dec_h264_EndFrame(priv
);
689 prevFrameNum
= priv
->picture
.h264
.frame_num
;
690 priv
->picture
.h264
.frame_num
= frame_num
;
692 priv
->picture
.h264
.field_pic_flag
= 0;
693 priv
->picture
.h264
.bottom_field_flag
= 0;
695 if (!sps
->frame_mbs_only_flag
) {
696 unsigned field_pic_flag
= vl_rbsp_u(rbsp
, 1);
698 if (!field_pic_flag
&& field_pic_flag
!= priv
->picture
.h264
.field_pic_flag
)
699 vid_dec_h264_EndFrame(priv
);
701 priv
->picture
.h264
.field_pic_flag
= field_pic_flag
;
703 if (priv
->picture
.h264
.field_pic_flag
) {
704 unsigned bottom_field_flag
= vl_rbsp_u(rbsp
, 1);
706 if (bottom_field_flag
!= priv
->picture
.h264
.bottom_field_flag
)
707 vid_dec_h264_EndFrame(priv
);
709 priv
->picture
.h264
.bottom_field_flag
= bottom_field_flag
;
714 unsigned idr_pic_id
= vl_rbsp_ue(rbsp
);
716 if (idr_pic_id
!= priv
->codec_data
.h264
.idr_pic_id
)
717 vid_dec_h264_EndFrame(priv
);
719 priv
->codec_data
.h264
.idr_pic_id
= idr_pic_id
;
722 if (sps
->pic_order_cnt_type
== 0) {
723 unsigned log2_max_pic_order_cnt_lsb
= sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4;
724 unsigned max_pic_order_cnt_lsb
= 1 << log2_max_pic_order_cnt_lsb
;
725 unsigned pic_order_cnt_lsb
= vl_rbsp_u(rbsp
, log2_max_pic_order_cnt_lsb
);
726 unsigned pic_order_cnt_msb
;
728 if (pic_order_cnt_lsb
!= priv
->codec_data
.h264
.pic_order_cnt_lsb
)
729 vid_dec_h264_EndFrame(priv
);
732 priv
->codec_data
.h264
.pic_order_cnt_msb
= 0;
733 priv
->codec_data
.h264
.pic_order_cnt_lsb
= 0;
736 if ((pic_order_cnt_lsb
< priv
->codec_data
.h264
.pic_order_cnt_lsb
) &&
737 (priv
->codec_data
.h264
.pic_order_cnt_lsb
- pic_order_cnt_lsb
) >= (max_pic_order_cnt_lsb
/ 2))
738 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
+ max_pic_order_cnt_lsb
;
740 else if ((pic_order_cnt_lsb
> priv
->codec_data
.h264
.pic_order_cnt_lsb
) &&
741 (pic_order_cnt_lsb
- priv
->codec_data
.h264
.pic_order_cnt_lsb
) > (max_pic_order_cnt_lsb
/ 2))
742 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
- max_pic_order_cnt_lsb
;
745 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
;
747 priv
->codec_data
.h264
.pic_order_cnt_msb
= pic_order_cnt_msb
;
748 priv
->codec_data
.h264
.pic_order_cnt_lsb
= pic_order_cnt_lsb
;
750 if (pps
->bottom_field_pic_order_in_frame_present_flag
&& !priv
->picture
.h264
.field_pic_flag
) {
751 unsigned delta_pic_order_cnt_bottom
= vl_rbsp_se(rbsp
);
753 if (delta_pic_order_cnt_bottom
!= priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
)
754 vid_dec_h264_EndFrame(priv
);
756 priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
= delta_pic_order_cnt_bottom
;
759 if (!priv
->picture
.h264
.field_pic_flag
) {
760 priv
->picture
.h264
.field_order_cnt
[0] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
761 priv
->picture
.h264
.field_order_cnt
[1] = priv
->picture
.h264
.field_order_cnt
[0] +
762 priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
;
763 } else if (!priv
->picture
.h264
.bottom_field_flag
)
764 priv
->picture
.h264
.field_order_cnt
[0] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
766 priv
->picture
.h264
.field_order_cnt
[1] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
768 } else if (sps
->pic_order_cnt_type
== 1) {
769 unsigned MaxFrameNum
= 1 << (sps
->log2_max_frame_num_minus4
+ 4);
770 unsigned FrameNumOffset
, absFrameNum
, expectedPicOrderCnt
;
772 if (!sps
->delta_pic_order_always_zero_flag
) {
773 unsigned delta_pic_order_cnt
[2];
775 delta_pic_order_cnt
[0] = vl_rbsp_se(rbsp
);
777 if (delta_pic_order_cnt
[0] != priv
->codec_data
.h264
.delta_pic_order_cnt
[0])
778 vid_dec_h264_EndFrame(priv
);
780 priv
->codec_data
.h264
.delta_pic_order_cnt
[0] = delta_pic_order_cnt
[0];
782 if (pps
->bottom_field_pic_order_in_frame_present_flag
&& !priv
->picture
.h264
.field_pic_flag
) {
783 delta_pic_order_cnt
[1] = vl_rbsp_se(rbsp
);
785 if (delta_pic_order_cnt
[1] != priv
->codec_data
.h264
.delta_pic_order_cnt
[1])
786 vid_dec_h264_EndFrame(priv
);
788 priv
->codec_data
.h264
.delta_pic_order_cnt
[1] = delta_pic_order_cnt
[1];
794 else if (prevFrameNum
> frame_num
)
795 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
+ MaxFrameNum
;
797 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
;
799 priv
->codec_data
.h264
.prevFrameNumOffset
= FrameNumOffset
;
801 if (sps
->num_ref_frames_in_pic_order_cnt_cycle
!= 0)
802 absFrameNum
= FrameNumOffset
+ frame_num
;
806 if (nal_ref_idc
== 0 && absFrameNum
> 0)
807 absFrameNum
= absFrameNum
- 1;
809 if (absFrameNum
> 0) {
810 unsigned picOrderCntCycleCnt
= (absFrameNum
- 1) / sps
->num_ref_frames_in_pic_order_cnt_cycle
;
811 unsigned frameNumInPicOrderCntCycle
= (absFrameNum
- 1) % sps
->num_ref_frames_in_pic_order_cnt_cycle
;
812 signed ExpectedDeltaPerPicOrderCntCycle
= 0;
815 for (i
= 0; i
< sps
->num_ref_frames_in_pic_order_cnt_cycle
; ++i
)
816 ExpectedDeltaPerPicOrderCntCycle
+= sps
->offset_for_ref_frame
[i
];
818 expectedPicOrderCnt
= picOrderCntCycleCnt
* ExpectedDeltaPerPicOrderCntCycle
;
819 for (i
= 0; i
<= frameNumInPicOrderCntCycle
; ++i
)
820 expectedPicOrderCnt
+= sps
->offset_for_ref_frame
[i
];
823 expectedPicOrderCnt
= 0;
825 if (nal_ref_idc
== 0)
826 expectedPicOrderCnt
+= sps
->offset_for_non_ref_pic
;
828 if (!priv
->picture
.h264
.field_pic_flag
) {
829 priv
->picture
.h264
.field_order_cnt
[0] = expectedPicOrderCnt
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
830 priv
->picture
.h264
.field_order_cnt
[1] = priv
->picture
.h264
.field_order_cnt
[0] +
831 sps
->offset_for_top_to_bottom_field
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[1];
833 } else if (!priv
->picture
.h264
.bottom_field_flag
)
834 priv
->picture
.h264
.field_order_cnt
[0] = expectedPicOrderCnt
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
836 priv
->picture
.h264
.field_order_cnt
[1] = expectedPicOrderCnt
+ sps
->offset_for_top_to_bottom_field
+
837 priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
839 } else if (sps
->pic_order_cnt_type
== 2) {
840 unsigned MaxFrameNum
= 1 << (sps
->log2_max_frame_num_minus4
+ 4);
841 unsigned FrameNumOffset
, tempPicOrderCnt
;
845 else if (prevFrameNum
> frame_num
)
846 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
+ MaxFrameNum
;
848 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
;
850 priv
->codec_data
.h264
.prevFrameNumOffset
= FrameNumOffset
;
854 else if (nal_ref_idc
== 0)
855 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
) - 1;
857 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
);
859 if (!priv
->picture
.h264
.field_pic_flag
) {
860 priv
->picture
.h264
.field_order_cnt
[0] = tempPicOrderCnt
;
861 priv
->picture
.h264
.field_order_cnt
[1] = tempPicOrderCnt
;
863 } else if (!priv
->picture
.h264
.bottom_field_flag
)
864 priv
->picture
.h264
.field_order_cnt
[0] = tempPicOrderCnt
;
866 priv
->picture
.h264
.field_order_cnt
[1] = tempPicOrderCnt
;
869 if (pps
->redundant_pic_cnt_present_flag
)
870 /* redundant_pic_cnt */
873 if (slice_type
== PIPE_H264_SLICE_TYPE_B
)
874 /* direct_spatial_mv_pred_flag */
877 priv
->picture
.h264
.num_ref_idx_l0_active_minus1
= pps
->num_ref_idx_l0_default_active_minus1
;
878 priv
->picture
.h264
.num_ref_idx_l1_active_minus1
= pps
->num_ref_idx_l1_default_active_minus1
;
880 if (slice_type
== PIPE_H264_SLICE_TYPE_P
||
881 slice_type
== PIPE_H264_SLICE_TYPE_SP
||
882 slice_type
== PIPE_H264_SLICE_TYPE_B
) {
884 /* num_ref_idx_active_override_flag */
885 if (vl_rbsp_u(rbsp
, 1)) {
886 priv
->picture
.h264
.num_ref_idx_l0_active_minus1
= vl_rbsp_ue(rbsp
);
888 if (slice_type
== PIPE_H264_SLICE_TYPE_B
)
889 priv
->picture
.h264
.num_ref_idx_l1_active_minus1
= vl_rbsp_ue(rbsp
);
893 if (nal_unit_type
== 20 || nal_unit_type
== 21)
894 ref_pic_list_mvc_modification(priv
, rbsp
);
896 ref_pic_list_modification(priv
, rbsp
, slice_type
);
898 if ((pps
->weighted_pred_flag
&& (slice_type
== PIPE_H264_SLICE_TYPE_P
|| slice_type
== PIPE_H264_SLICE_TYPE_SP
)) ||
899 (pps
->weighted_bipred_idc
== 1 && slice_type
== PIPE_H264_SLICE_TYPE_B
))
900 pred_weight_table(priv
, rbsp
, sps
, slice_type
);
902 if (nal_ref_idc
!= 0)
903 dec_ref_pic_marking(priv
, rbsp
, IdrPicFlag
);
905 if (pps
->entropy_coding_mode_flag
&& slice_type
!= PIPE_H264_SLICE_TYPE_I
&& slice_type
!= PIPE_H264_SLICE_TYPE_SI
)
912 if (slice_type
== PIPE_H264_SLICE_TYPE_SP
|| slice_type
== PIPE_H264_SLICE_TYPE_SI
) {
913 if (slice_type
== PIPE_H264_SLICE_TYPE_SP
)
914 /* sp_for_switch_flag */
921 if (pps
->deblocking_filter_control_present_flag
) {
922 unsigned disable_deblocking_filter_idc
= vl_rbsp_ue(rbsp
);
924 if (disable_deblocking_filter_idc
!= 1) {
925 /* slice_alpha_c0_offset_div2 */
928 /* slice_beta_offset_div2 */
933 if (pps
->num_slice_groups_minus1
> 0 && pps
->slice_group_map_type
>= 3 && pps
->slice_group_map_type
<= 5)
934 /* slice_group_change_cycle */
938 static void vid_dec_h264_Decode(vid_dec_PrivateType
*priv
, struct vl_vlc
*vlc
, unsigned min_bits_left
)
940 unsigned nal_ref_idc
, nal_unit_type
;
942 if (!vl_vlc_search_byte(vlc
, vl_vlc_bits_left(vlc
) - min_bits_left
, 0x00))
945 if (vl_vlc_peekbits(vlc
, 24) != 0x000001) {
946 vl_vlc_eatbits(vlc
, 8);
951 unsigned bytes
= priv
->bytes_left
- (vl_vlc_bits_left(vlc
) / 8);
952 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
, &priv
->picture
.base
,
953 1, &priv
->slice
, &bytes
);
957 vl_vlc_eatbits(vlc
, 24);
959 /* forbidden_zero_bit */
960 vl_vlc_eatbits(vlc
, 1);
962 nal_ref_idc
= vl_vlc_get_uimsbf(vlc
, 2);
964 if (nal_ref_idc
!= priv
->codec_data
.h264
.nal_ref_idc
&&
965 (nal_ref_idc
* priv
->codec_data
.h264
.nal_ref_idc
) == 0)
966 vid_dec_h264_EndFrame(priv
);
968 priv
->codec_data
.h264
.nal_ref_idc
= nal_ref_idc
;
970 nal_unit_type
= vl_vlc_get_uimsbf(vlc
, 5);
972 if (nal_unit_type
!= 1 && nal_unit_type
!= 5)
973 vid_dec_h264_EndFrame(priv
);
975 if (nal_unit_type
== 7) {
977 vl_rbsp_init(&rbsp
, vlc
, ~0);
978 seq_parameter_set(priv
, &rbsp
);
980 } else if (nal_unit_type
== 8) {
982 vl_rbsp_init(&rbsp
, vlc
, ~0);
983 picture_parameter_set(priv
, &rbsp
);
985 } else if (nal_unit_type
== 1 || nal_unit_type
== 5) {
986 /* Coded slice of a non-IDR or IDR picture */
987 unsigned bits
= vl_vlc_valid_bits(vlc
);
988 unsigned bytes
= bits
/ 8 + 4;
991 const void *ptr
= buf
;
997 buf
[3] = (nal_ref_idc
<< 5) | nal_unit_type
;
998 for (i
= 4; i
< bytes
; ++i
)
999 buf
[i
] = vl_vlc_peekbits(vlc
, bits
) >> ((bytes
- i
- 1) * 8);
1001 priv
->bytes_left
= (vl_vlc_bits_left(vlc
) - bits
) / 8;
1002 priv
->slice
= vlc
->data
;
1004 vl_rbsp_init(&rbsp
, vlc
, 128);
1005 slice_header(priv
, &rbsp
, nal_ref_idc
, nal_unit_type
);
1007 vid_dec_h264_BeginFrame(priv
);
1009 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
, &priv
->picture
.base
,
1013 /* resync to byte boundary */
1014 vl_vlc_eatbits(vlc
, vl_vlc_valid_bits(vlc
) % 8);