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"
39 #include "entrypoint.h"
42 #define DPB_MAX_SIZE 5
45 struct list_head list
;
46 struct pipe_video_buffer
*buffer
;
50 static const uint8_t Default_4x4_Intra
[16] = {
51 6, 13, 13, 20, 20, 20, 28, 28,
52 28, 28, 32, 32, 32, 37, 37, 42
55 static const uint8_t Default_4x4_Inter
[16] = {
56 10, 14, 14, 20, 20, 20, 24, 24,
57 24, 24, 27, 27, 27, 30, 30, 34
60 static const uint8_t Default_8x8_Intra
[64] = {
61 6, 10, 10, 13, 11, 13, 16, 16,
62 16, 16, 18, 18, 18, 18, 18, 23,
63 23, 23, 23, 23, 23, 25, 25, 25,
64 25, 25, 25, 25, 27, 27, 27, 27,
65 27, 27, 27, 27, 29, 29, 29, 29,
66 29, 29, 29, 31, 31, 31, 31, 31,
67 31, 33, 33, 33, 33, 33, 36, 36,
68 36, 36, 38, 38, 38, 40, 40, 42
71 static const uint8_t Default_8x8_Inter
[64] = {
72 9, 13, 13, 15, 13, 15, 17, 17,
73 17, 17, 19, 19, 19, 19, 19, 21,
74 21, 21, 21, 21, 21, 22, 22, 22,
75 22, 22, 22, 22, 24, 24, 24, 24,
76 24, 24, 24, 24, 25, 25, 25, 25,
77 25, 25, 25, 27, 27, 27, 27, 27,
78 27, 28, 28, 28, 28, 28, 30, 30,
79 30, 30, 32, 32, 32, 33, 33, 35
82 static void vid_dec_h264_Decode(vid_dec_PrivateType
*priv
, struct vl_vlc
*vlc
, unsigned min_bits_left
);
83 static void vid_dec_h264_EndFrame(vid_dec_PrivateType
*priv
);
84 static struct pipe_video_buffer
*vid_dec_h264_Flush(vid_dec_PrivateType
*priv
);
86 void vid_dec_h264_Init(vid_dec_PrivateType
*priv
)
88 priv
->picture
.base
.profile
= PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH
;
90 priv
->Decode
= vid_dec_h264_Decode
;
91 priv
->EndFrame
= vid_dec_h264_EndFrame
;
92 priv
->Flush
= vid_dec_h264_Flush
;
94 LIST_INITHEAD(&priv
->codec_data
.h264
.dpb_list
);
95 priv
->picture
.h264
.field_order_cnt
[0] = priv
->picture
.h264
.field_order_cnt
[1] = INT_MAX
;
98 static void vid_dec_h264_BeginFrame(vid_dec_PrivateType
*priv
)
100 //TODO: sane buffer handling
102 if (priv
->frame_started
)
105 vid_dec_NeedTarget(priv
);
107 priv
->picture
.h264
.num_ref_frames
= priv
->picture
.h264
.pps
->sps
->max_num_ref_frames
;
110 struct pipe_video_codec templat
= {};
111 omx_base_video_PortType
*port
;
113 port
= (omx_base_video_PortType
*)priv
->ports
[OMX_BASE_FILTER_INPUTPORT_INDEX
];
114 templat
.profile
= priv
->profile
;
115 templat
.entrypoint
= PIPE_VIDEO_ENTRYPOINT_BITSTREAM
;
116 templat
.chroma_format
= PIPE_VIDEO_CHROMA_FORMAT_420
;
117 templat
.max_references
= priv
->picture
.h264
.num_ref_frames
;
118 templat
.expect_chunked_decode
= true;
119 templat
.width
= port
->sPortParam
.format
.video
.nFrameWidth
;
120 templat
.height
= port
->sPortParam
.format
.video
.nFrameHeight
;
121 templat
.level
= priv
->picture
.h264
.pps
->sps
->level_idc
;
123 priv
->codec
= priv
->pipe
->create_video_codec(priv
->pipe
, &templat
);
125 priv
->codec
->begin_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
126 priv
->frame_started
= true;
129 static struct pipe_video_buffer
*vid_dec_h264_Flush(vid_dec_PrivateType
*priv
)
131 struct dpb_list
*entry
, *result
= NULL
;
132 struct pipe_video_buffer
*buf
;
134 /* search for the lowest poc and break on zeros */
135 LIST_FOR_EACH_ENTRY(entry
, &priv
->codec_data
.h264
.dpb_list
, list
) {
137 if (result
&& entry
->poc
== 0)
140 if (!result
|| entry
->poc
< result
->poc
)
147 buf
= result
->buffer
;
149 --priv
->codec_data
.h264
.dpb_num
;
150 LIST_DEL(&result
->list
);
156 static void vid_dec_h264_EndFrame(vid_dec_PrivateType
*priv
)
158 struct dpb_list
*entry
;
159 struct pipe_video_buffer
*tmp
;
160 bool top_field_first
;
162 if (!priv
->frame_started
)
165 priv
->codec
->end_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
166 priv
->frame_started
= false;
168 // TODO: implement frame number handling
169 priv
->picture
.h264
.frame_num_list
[0] = priv
->picture
.h264
.frame_num
;
170 priv
->picture
.h264
.field_order_cnt_list
[0][0] = priv
->picture
.h264
.frame_num
;
171 priv
->picture
.h264
.field_order_cnt_list
[0][1] = priv
->picture
.h264
.frame_num
;
173 top_field_first
= priv
->picture
.h264
.field_order_cnt
[0] < priv
->picture
.h264
.field_order_cnt
[1];
175 if (priv
->picture
.h264
.field_pic_flag
&& priv
->picture
.h264
.bottom_field_flag
!= top_field_first
)
178 /* add the decoded picture to the dpb list */
179 entry
= CALLOC_STRUCT(dpb_list
);
183 entry
->buffer
= priv
->target
;
184 entry
->poc
= MIN2(priv
->picture
.h264
.field_order_cnt
[0], priv
->picture
.h264
.field_order_cnt
[1]);
185 LIST_ADDTAIL(&entry
->list
, &priv
->codec_data
.h264
.dpb_list
);
186 ++priv
->codec_data
.h264
.dpb_num
;
188 priv
->picture
.h264
.field_order_cnt
[0] = priv
->picture
.h264
.field_order_cnt
[1] = INT_MAX
;
190 if (priv
->codec_data
.h264
.dpb_num
<= DPB_MAX_SIZE
)
193 tmp
= priv
->in_buffers
[0]->pInputPortPrivate
;
194 priv
->in_buffers
[0]->pInputPortPrivate
= vid_dec_h264_Flush(priv
);
196 priv
->frame_finished
= priv
->in_buffers
[0]->pInputPortPrivate
!= NULL
;
199 static void vui_parameters(struct vl_rbsp
*rbsp
)
204 static void scaling_list(struct vl_rbsp
*rbsp
, uint8_t *scalingList
, unsigned sizeOfScalingList
,
205 const uint8_t *defaultList
, const uint8_t *fallbackList
)
207 unsigned lastScale
= 8, nextScale
= 8;
210 /* (pic|seq)_scaling_list_present_flag[i] */
211 if (!vl_rbsp_u(rbsp
, 1)) {
213 memcpy(scalingList
, fallbackList
, sizeOfScalingList
);
217 for (i
= 0; i
< sizeOfScalingList
; ++i
) {
219 if (nextScale
!= 0) {
220 signed delta_scale
= vl_rbsp_se(rbsp
);
221 nextScale
= (lastScale
+ delta_scale
+ 256) % 256;
222 if (i
== 0 && nextScale
== 0) {
223 memcpy(scalingList
, defaultList
, sizeOfScalingList
);
227 scalingList
[i
] = nextScale
== 0 ? lastScale
: nextScale
;
228 lastScale
= scalingList
[i
];
232 static struct pipe_h264_sps
*seq_parameter_set_id(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
234 unsigned id
= vl_rbsp_ue(rbsp
);
235 if (id
>= Elements(priv
->codec_data
.h264
.sps
))
236 return NULL
; /* invalid seq_parameter_set_id */
238 return &priv
->codec_data
.h264
.sps
[id
];
241 static void seq_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
243 struct pipe_h264_sps
*sps
;
244 unsigned profile_idc
, level_idc
;
247 /* Sequence parameter set */
248 profile_idc
= vl_rbsp_u(rbsp
, 8);
250 /* constraint_set0_flag */
253 /* constraint_set1_flag */
256 /* constraint_set2_flag */
259 /* constraint_set3_flag */
262 /* constraint_set4_flag */
265 /* constraint_set5_flag */
268 /* reserved_zero_2bits */
272 level_idc
= vl_rbsp_u(rbsp
, 8);
274 sps
= seq_parameter_set_id(priv
, rbsp
);
278 memset(sps
, 0, sizeof(*sps
));
279 memset(sps
->ScalingList4x4
, 16, sizeof(sps
->ScalingList4x4
));
280 memset(sps
->ScalingList8x8
, 16, sizeof(sps
->ScalingList8x8
));
282 sps
->level_idc
= level_idc
;
284 if (profile_idc
== 100 || profile_idc
== 110 || profile_idc
== 122 || profile_idc
== 244 ||
285 profile_idc
== 44 || profile_idc
== 83 || profile_idc
== 86 || profile_idc
== 118 ||
286 profile_idc
== 128 || profile_idc
== 138) {
288 sps
->chroma_format_idc
= vl_rbsp_ue(rbsp
);
290 if (sps
->chroma_format_idc
== 3)
291 sps
->separate_colour_plane_flag
= vl_rbsp_u(rbsp
, 1);
293 sps
->bit_depth_luma_minus8
= vl_rbsp_ue(rbsp
);
295 sps
->bit_depth_chroma_minus8
= vl_rbsp_ue(rbsp
);
297 /* qpprime_y_zero_transform_bypass_flag */
300 sps
->seq_scaling_matrix_present_flag
= vl_rbsp_u(rbsp
, 1);
301 if (sps
->seq_scaling_matrix_present_flag
) {
303 scaling_list(rbsp
, sps
->ScalingList4x4
[0], 16, Default_4x4_Intra
, Default_4x4_Intra
);
304 scaling_list(rbsp
, sps
->ScalingList4x4
[1], 16, Default_4x4_Intra
, sps
->ScalingList4x4
[0]);
305 scaling_list(rbsp
, sps
->ScalingList4x4
[2], 16, Default_4x4_Intra
, sps
->ScalingList4x4
[1]);
306 scaling_list(rbsp
, sps
->ScalingList4x4
[3], 16, Default_4x4_Inter
, Default_4x4_Inter
);
307 scaling_list(rbsp
, sps
->ScalingList4x4
[4], 16, Default_4x4_Inter
, sps
->ScalingList4x4
[3]);
308 scaling_list(rbsp
, sps
->ScalingList4x4
[5], 16, Default_4x4_Inter
, sps
->ScalingList4x4
[4]);
310 scaling_list(rbsp
, sps
->ScalingList8x8
[0], 64, Default_8x8_Intra
, Default_8x8_Intra
);
311 scaling_list(rbsp
, sps
->ScalingList8x8
[1], 64, Default_8x8_Inter
, Default_8x8_Inter
);
312 if (sps
->chroma_format_idc
== 3) {
313 scaling_list(rbsp
, sps
->ScalingList8x8
[2], 64, Default_8x8_Intra
, sps
->ScalingList8x8
[0]);
314 scaling_list(rbsp
, sps
->ScalingList8x8
[3], 64, Default_8x8_Inter
, sps
->ScalingList8x8
[1]);
315 scaling_list(rbsp
, sps
->ScalingList8x8
[4], 64, Default_8x8_Intra
, sps
->ScalingList8x8
[2]);
316 scaling_list(rbsp
, sps
->ScalingList8x8
[5], 64, Default_8x8_Inter
, sps
->ScalingList8x8
[3]);
319 } else if (profile_idc
== 183)
320 sps
->chroma_format_idc
= 0;
322 sps
->chroma_format_idc
= 1;
324 sps
->log2_max_frame_num_minus4
= vl_rbsp_ue(rbsp
);
326 sps
->pic_order_cnt_type
= vl_rbsp_ue(rbsp
);
328 if (sps
->pic_order_cnt_type
== 0)
329 sps
->log2_max_pic_order_cnt_lsb_minus4
= vl_rbsp_ue(rbsp
);
330 else if (sps
->pic_order_cnt_type
== 1) {
331 sps
->delta_pic_order_always_zero_flag
= vl_rbsp_u(rbsp
, 1);
333 sps
->offset_for_non_ref_pic
= vl_rbsp_se(rbsp
);
335 sps
->offset_for_top_to_bottom_field
= vl_rbsp_se(rbsp
);
337 sps
->num_ref_frames_in_pic_order_cnt_cycle
= vl_rbsp_ue(rbsp
);
339 for (i
= 0; i
< sps
->num_ref_frames_in_pic_order_cnt_cycle
; ++i
)
340 sps
->offset_for_ref_frame
[i
] = vl_rbsp_se(rbsp
);
343 sps
->max_num_ref_frames
= vl_rbsp_ue(rbsp
);
345 /* gaps_in_frame_num_value_allowed_flag */
348 /* pic_width_in_mbs_minus1 */
351 /* pic_height_in_map_units_minus1 */
354 sps
->frame_mbs_only_flag
= vl_rbsp_u(rbsp
, 1);
355 if (!sps
->frame_mbs_only_flag
)
356 sps
->mb_adaptive_frame_field_flag
= vl_rbsp_u(rbsp
, 1);
358 sps
->direct_8x8_inference_flag
= vl_rbsp_u(rbsp
, 1);
360 /* frame_cropping_flag */
361 if (vl_rbsp_u(rbsp
, 1)) {
362 /* frame_crop_left_offset */
365 /* frame_crop_right_offset */
368 /* frame_crop_top_offset */
371 /* frame_crop_bottom_offset */
375 /* vui_parameters_present_flag */
376 if (vl_rbsp_u(rbsp
, 1))
377 vui_parameters(rbsp
);
380 static struct pipe_h264_pps
*pic_parameter_set_id(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
382 unsigned id
= vl_rbsp_ue(rbsp
);
383 if (id
>= Elements(priv
->codec_data
.h264
.pps
))
384 return NULL
; /* invalid pic_parameter_set_id */
386 return &priv
->codec_data
.h264
.pps
[id
];
389 static void picture_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
391 struct pipe_h264_sps
*sps
;
392 struct pipe_h264_pps
*pps
;
395 pps
= pic_parameter_set_id(priv
, rbsp
);
399 memset(pps
, 0, sizeof(*pps
));
401 sps
= pps
->sps
= seq_parameter_set_id(priv
, rbsp
);
405 memcpy(pps
->ScalingList4x4
, sps
->ScalingList4x4
, sizeof(pps
->ScalingList4x4
));
406 memcpy(pps
->ScalingList8x8
, sps
->ScalingList8x8
, sizeof(pps
->ScalingList8x8
));
408 pps
->entropy_coding_mode_flag
= vl_rbsp_u(rbsp
, 1);
410 pps
->bottom_field_pic_order_in_frame_present_flag
= vl_rbsp_u(rbsp
, 1);
412 pps
->num_slice_groups_minus1
= vl_rbsp_ue(rbsp
);
413 if (pps
->num_slice_groups_minus1
> 0) {
414 pps
->slice_group_map_type
= vl_rbsp_ue(rbsp
);
416 if (pps
->slice_group_map_type
== 0) {
418 for (i
= 0; i
<= pps
->num_slice_groups_minus1
; ++i
)
419 /* run_length_minus1[i] */
422 } else if (pps
->slice_group_map_type
== 2) {
424 for (i
= 0; i
<= pps
->num_slice_groups_minus1
; ++i
) {
428 /* bottom_right[i] */
432 } else if (pps
->slice_group_map_type
>= 3 && pps
->slice_group_map_type
<= 5) {
434 /* slice_group_change_direction_flag */
437 pps
->slice_group_change_rate_minus1
= vl_rbsp_ue(rbsp
);
439 } else if (pps
->slice_group_map_type
== 6) {
441 unsigned pic_size_in_map_units_minus1
;
443 pic_size_in_map_units_minus1
= vl_rbsp_ue(rbsp
);
445 for (i
= 0; i
<= pic_size_in_map_units_minus1
; ++i
)
446 /* slice_group_id[i] */
447 vl_rbsp_u(rbsp
, log2(pps
->num_slice_groups_minus1
+ 1));
451 pps
->num_ref_idx_l0_default_active_minus1
= vl_rbsp_ue(rbsp
);
453 pps
->num_ref_idx_l1_default_active_minus1
= vl_rbsp_ue(rbsp
);
455 pps
->weighted_pred_flag
= vl_rbsp_u(rbsp
, 1);
457 pps
->weighted_bipred_idc
= vl_rbsp_u(rbsp
, 2);
459 pps
->pic_init_qp_minus26
= vl_rbsp_se(rbsp
);
461 /* pic_init_qs_minus26 */
464 pps
->chroma_qp_index_offset
= vl_rbsp_se(rbsp
);
466 pps
->deblocking_filter_control_present_flag
= vl_rbsp_u(rbsp
, 1);
468 pps
->constrained_intra_pred_flag
= vl_rbsp_u(rbsp
, 1);
470 pps
->redundant_pic_cnt_present_flag
= vl_rbsp_u(rbsp
, 1);
472 if (vl_rbsp_more_data(rbsp
)) {
473 pps
->transform_8x8_mode_flag
= vl_rbsp_u(rbsp
, 1);
475 /* pic_scaling_matrix_present_flag */
476 if (vl_rbsp_u(rbsp
, 1)) {
478 scaling_list(rbsp
, pps
->ScalingList4x4
[0], 16, Default_4x4_Intra
,
479 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_4x4_Intra
);
480 scaling_list(rbsp
, pps
->ScalingList4x4
[1], 16, Default_4x4_Intra
, pps
->ScalingList4x4
[0]);
481 scaling_list(rbsp
, pps
->ScalingList4x4
[2], 16, Default_4x4_Intra
, pps
->ScalingList4x4
[1]);
482 scaling_list(rbsp
, pps
->ScalingList4x4
[3], 16, Default_4x4_Inter
,
483 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_4x4_Inter
);
484 scaling_list(rbsp
, pps
->ScalingList4x4
[4], 16, Default_4x4_Inter
, pps
->ScalingList4x4
[3]);
485 scaling_list(rbsp
, pps
->ScalingList4x4
[5], 16, Default_4x4_Inter
, pps
->ScalingList4x4
[4]);
487 if (pps
->transform_8x8_mode_flag
) {
488 scaling_list(rbsp
, pps
->ScalingList8x8
[0], 64, Default_8x8_Intra
,
489 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_8x8_Intra
);
490 scaling_list(rbsp
, pps
->ScalingList8x8
[1], 64, Default_8x8_Inter
,
491 sps
->seq_scaling_matrix_present_flag
? NULL
: Default_8x8_Inter
);
492 if (sps
->chroma_format_idc
== 3) {
493 scaling_list(rbsp
, pps
->ScalingList8x8
[2], 64, Default_8x8_Intra
, pps
->ScalingList8x8
[0]);
494 scaling_list(rbsp
, pps
->ScalingList8x8
[3], 64, Default_8x8_Inter
, pps
->ScalingList8x8
[1]);
495 scaling_list(rbsp
, pps
->ScalingList8x8
[4], 64, Default_8x8_Intra
, pps
->ScalingList8x8
[2]);
496 scaling_list(rbsp
, pps
->ScalingList8x8
[5], 64, Default_8x8_Inter
, pps
->ScalingList8x8
[3]);
501 pps
->second_chroma_qp_index_offset
= vl_rbsp_se(rbsp
);
505 static void ref_pic_list_mvc_modification(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
511 static void ref_pic_list_modification(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
512 enum pipe_h264_slice_type slice_type
)
514 unsigned modification_of_pic_nums_idc
;
516 if (slice_type
!= 2 && slice_type
!= 4) {
517 /* ref_pic_list_modification_flag_l0 */
518 if (vl_rbsp_u(rbsp
, 1)) {
520 modification_of_pic_nums_idc
= vl_rbsp_ue(rbsp
);
521 if (modification_of_pic_nums_idc
== 0 ||
522 modification_of_pic_nums_idc
== 1)
523 /* abs_diff_pic_num_minus1 */
525 else if (modification_of_pic_nums_idc
== 2)
526 /* long_term_pic_num */
528 } while (modification_of_pic_nums_idc
!= 3);
532 if (slice_type
== 1) {
533 /* ref_pic_list_modification_flag_l1 */
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);
549 static void pred_weight_table(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
550 struct pipe_h264_sps
*sps
, enum pipe_h264_slice_type slice_type
)
552 unsigned ChromaArrayType
= sps
->separate_colour_plane_flag
? 0 : sps
->chroma_format_idc
;
555 /* luma_log2_weight_denom */
558 if (ChromaArrayType
!= 0)
559 /* chroma_log2_weight_denom */
562 for (i
= 0; i
<= priv
->picture
.h264
.num_ref_idx_l0_active_minus1
; ++i
) {
563 /* luma_weight_l0_flag */
564 if (vl_rbsp_u(rbsp
, 1)) {
565 /* luma_weight_l0[i] */
567 /* luma_offset_l0[i] */
570 if (ChromaArrayType
!= 0) {
571 /* chroma_weight_l0_flag */
572 if (vl_rbsp_u(rbsp
, 1)) {
573 for (j
= 0; j
< 2; ++j
) {
574 /* chroma_weight_l0[i][j] */
576 /* chroma_offset_l0[i][j] */
583 if (slice_type
== 1) {
584 for (i
= 0; i
<= priv
->picture
.h264
.num_ref_idx_l1_active_minus1
; ++i
) {
585 /* luma_weight_l1_flag */
586 if (vl_rbsp_u(rbsp
, 1)) {
587 /* luma_weight_l1[i] */
589 /* luma_offset_l1[i] */
592 if (ChromaArrayType
!= 0) {
593 /* chroma_weight_l1_flag */
594 if (vl_rbsp_u(rbsp
, 1)) {
595 for (j
= 0; j
< 2; ++j
) {
596 /* chroma_weight_l1[i][j] */
598 /* chroma_offset_l1[i][j] */
607 static void dec_ref_pic_marking(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
610 unsigned memory_management_control_operation
;
613 /* no_output_of_prior_pics_flag */
615 /* long_term_reference_flag */
618 /* adaptive_ref_pic_marking_mode_flag */
619 if (vl_rbsp_u(rbsp
, 1)) {
621 memory_management_control_operation
= vl_rbsp_ue(rbsp
);
623 if (memory_management_control_operation
== 1 ||
624 memory_management_control_operation
== 3)
625 /* difference_of_pic_nums_minus1 */
628 if (memory_management_control_operation
== 2)
629 /* long_term_pic_num */
632 if (memory_management_control_operation
== 3 ||
633 memory_management_control_operation
== 6)
634 /* long_term_frame_idx */
637 if (memory_management_control_operation
== 4)
638 /* max_long_term_frame_idx_plus1 */
640 } while (memory_management_control_operation
!= 0);
645 static void slice_header(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
646 unsigned nal_ref_idc
, unsigned nal_unit_type
)
648 enum pipe_h264_slice_type slice_type
;
649 struct pipe_h264_pps
*pps
;
650 struct pipe_h264_sps
*sps
;
651 unsigned frame_num
, prevFrameNum
;
652 bool IdrPicFlag
= nal_unit_type
== 5;
654 if (IdrPicFlag
!= priv
->codec_data
.h264
.IdrPicFlag
)
655 vid_dec_h264_EndFrame(priv
);
657 priv
->codec_data
.h264
.IdrPicFlag
= IdrPicFlag
;
659 /* first_mb_in_slice */
662 slice_type
= vl_rbsp_ue(rbsp
) % 5;
664 pps
= pic_parameter_set_id(priv
, rbsp
);
672 if (pps
!= priv
->picture
.h264
.pps
)
673 vid_dec_h264_EndFrame(priv
);
675 priv
->picture
.h264
.pps
= pps
;
677 if (sps
->separate_colour_plane_flag
== 1 )
678 /* colour_plane_id */
681 frame_num
= vl_rbsp_u(rbsp
, sps
->log2_max_frame_num_minus4
+ 4);
683 if (frame_num
!= priv
->picture
.h264
.frame_num
)
684 vid_dec_h264_EndFrame(priv
);
686 prevFrameNum
= priv
->picture
.h264
.frame_num
;
687 priv
->picture
.h264
.frame_num
= frame_num
;
689 priv
->picture
.h264
.field_pic_flag
= 0;
690 priv
->picture
.h264
.bottom_field_flag
= 0;
692 if (!sps
->frame_mbs_only_flag
) {
693 unsigned field_pic_flag
= vl_rbsp_u(rbsp
, 1);
695 if (!field_pic_flag
&& field_pic_flag
!= priv
->picture
.h264
.field_pic_flag
)
696 vid_dec_h264_EndFrame(priv
);
698 priv
->picture
.h264
.field_pic_flag
= field_pic_flag
;
700 if (priv
->picture
.h264
.field_pic_flag
) {
701 unsigned bottom_field_flag
= vl_rbsp_u(rbsp
, 1);
703 if (bottom_field_flag
!= priv
->picture
.h264
.bottom_field_flag
)
704 vid_dec_h264_EndFrame(priv
);
706 priv
->picture
.h264
.bottom_field_flag
= bottom_field_flag
;
711 unsigned idr_pic_id
= vl_rbsp_ue(rbsp
);
713 if (idr_pic_id
!= priv
->codec_data
.h264
.idr_pic_id
)
714 vid_dec_h264_EndFrame(priv
);
716 priv
->codec_data
.h264
.idr_pic_id
= idr_pic_id
;
719 if (sps
->pic_order_cnt_type
== 0) {
720 unsigned log2_max_pic_order_cnt_lsb
= sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4;
721 unsigned max_pic_order_cnt_lsb
= 1 << log2_max_pic_order_cnt_lsb
;
722 unsigned pic_order_cnt_lsb
= vl_rbsp_u(rbsp
, log2_max_pic_order_cnt_lsb
);
723 unsigned pic_order_cnt_msb
;
725 if (pic_order_cnt_lsb
!= priv
->codec_data
.h264
.pic_order_cnt_lsb
)
726 vid_dec_h264_EndFrame(priv
);
729 priv
->codec_data
.h264
.pic_order_cnt_msb
= 0;
730 priv
->codec_data
.h264
.pic_order_cnt_lsb
= 0;
733 if ((pic_order_cnt_lsb
< priv
->codec_data
.h264
.pic_order_cnt_lsb
) &&
734 (priv
->codec_data
.h264
.pic_order_cnt_lsb
- pic_order_cnt_lsb
) >= (max_pic_order_cnt_lsb
/ 2))
735 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
+ max_pic_order_cnt_lsb
;
737 else if ((pic_order_cnt_lsb
> priv
->codec_data
.h264
.pic_order_cnt_lsb
) &&
738 (pic_order_cnt_lsb
- priv
->codec_data
.h264
.pic_order_cnt_lsb
) > (max_pic_order_cnt_lsb
/ 2))
739 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
- max_pic_order_cnt_lsb
;
742 pic_order_cnt_msb
= priv
->codec_data
.h264
.pic_order_cnt_msb
;
744 priv
->codec_data
.h264
.pic_order_cnt_msb
= pic_order_cnt_msb
;
745 priv
->codec_data
.h264
.pic_order_cnt_lsb
= pic_order_cnt_lsb
;
747 if (pps
->bottom_field_pic_order_in_frame_present_flag
&& !priv
->picture
.h264
.field_pic_flag
) {
748 unsigned delta_pic_order_cnt_bottom
= vl_rbsp_se(rbsp
);
750 if (delta_pic_order_cnt_bottom
!= priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
)
751 vid_dec_h264_EndFrame(priv
);
753 priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
= delta_pic_order_cnt_bottom
;
756 priv
->picture
.h264
.field_order_cnt
[0] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
757 priv
->picture
.h264
.field_order_cnt
[1] = pic_order_cnt_msb
+ pic_order_cnt_lsb
;
758 if (!priv
->picture
.h264
.field_pic_flag
)
759 priv
->picture
.h264
.field_order_cnt
[1] += priv
->codec_data
.h264
.delta_pic_order_cnt_bottom
;
761 } else if (sps
->pic_order_cnt_type
== 1) {
762 unsigned MaxFrameNum
= 1 << (sps
->log2_max_frame_num_minus4
+ 4);
763 unsigned FrameNumOffset
, absFrameNum
, expectedPicOrderCnt
;
765 if (!sps
->delta_pic_order_always_zero_flag
) {
766 unsigned delta_pic_order_cnt
[2];
768 delta_pic_order_cnt
[0] = vl_rbsp_se(rbsp
);
770 if (delta_pic_order_cnt
[0] != priv
->codec_data
.h264
.delta_pic_order_cnt
[0])
771 vid_dec_h264_EndFrame(priv
);
773 priv
->codec_data
.h264
.delta_pic_order_cnt
[0] = delta_pic_order_cnt
[0];
775 if (pps
->bottom_field_pic_order_in_frame_present_flag
&& !priv
->picture
.h264
.field_pic_flag
) {
776 delta_pic_order_cnt
[1] = vl_rbsp_se(rbsp
);
778 if (delta_pic_order_cnt
[1] != priv
->codec_data
.h264
.delta_pic_order_cnt
[1])
779 vid_dec_h264_EndFrame(priv
);
781 priv
->codec_data
.h264
.delta_pic_order_cnt
[1] = delta_pic_order_cnt
[1];
787 else if (prevFrameNum
> frame_num
)
788 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
+ MaxFrameNum
;
790 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
;
792 priv
->codec_data
.h264
.prevFrameNumOffset
= FrameNumOffset
;
794 if (sps
->num_ref_frames_in_pic_order_cnt_cycle
!= 0)
795 absFrameNum
= FrameNumOffset
+ frame_num
;
799 if (nal_ref_idc
== 0 && absFrameNum
> 0)
800 absFrameNum
= absFrameNum
- 1;
802 if (absFrameNum
> 0) {
803 unsigned picOrderCntCycleCnt
= (absFrameNum
- 1) / sps
->num_ref_frames_in_pic_order_cnt_cycle
;
804 unsigned frameNumInPicOrderCntCycle
= (absFrameNum
- 1) % sps
->num_ref_frames_in_pic_order_cnt_cycle
;
805 signed ExpectedDeltaPerPicOrderCntCycle
= 0;
808 for (i
= 0; i
< sps
->num_ref_frames_in_pic_order_cnt_cycle
; ++i
)
809 ExpectedDeltaPerPicOrderCntCycle
+= sps
->offset_for_ref_frame
[i
];
811 expectedPicOrderCnt
= picOrderCntCycleCnt
* ExpectedDeltaPerPicOrderCntCycle
;
812 for (i
= 0; i
<= frameNumInPicOrderCntCycle
; ++i
)
813 expectedPicOrderCnt
+= sps
->offset_for_ref_frame
[i
];
816 expectedPicOrderCnt
= 0;
818 if (nal_ref_idc
== 0)
819 expectedPicOrderCnt
+= sps
->offset_for_non_ref_pic
;
821 if (!priv
->picture
.h264
.field_pic_flag
) {
822 priv
->picture
.h264
.field_order_cnt
[0] = expectedPicOrderCnt
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
823 priv
->picture
.h264
.field_order_cnt
[1] = priv
->picture
.h264
.field_order_cnt
[0] +
824 sps
->offset_for_top_to_bottom_field
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[1];
826 } else if (!priv
->picture
.h264
.bottom_field_flag
)
827 priv
->picture
.h264
.field_order_cnt
[0] = expectedPicOrderCnt
+ priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
829 priv
->picture
.h264
.field_order_cnt
[1] = expectedPicOrderCnt
+ sps
->offset_for_top_to_bottom_field
+
830 priv
->codec_data
.h264
.delta_pic_order_cnt
[0];
832 } else if (sps
->pic_order_cnt_type
== 2) {
833 unsigned MaxFrameNum
= 1 << (sps
->log2_max_frame_num_minus4
+ 4);
834 unsigned FrameNumOffset
, tempPicOrderCnt
;
838 else if (prevFrameNum
> frame_num
)
839 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
+ MaxFrameNum
;
841 FrameNumOffset
= priv
->codec_data
.h264
.prevFrameNumOffset
;
843 priv
->codec_data
.h264
.prevFrameNumOffset
= FrameNumOffset
;
847 else if (nal_ref_idc
== 0)
848 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
) - 1;
850 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
);
852 if (!priv
->picture
.h264
.field_pic_flag
) {
853 priv
->picture
.h264
.field_order_cnt
[0] = tempPicOrderCnt
;
854 priv
->picture
.h264
.field_order_cnt
[1] = tempPicOrderCnt
;
856 } else if (!priv
->picture
.h264
.bottom_field_flag
)
857 priv
->picture
.h264
.field_order_cnt
[0] = tempPicOrderCnt
;
859 priv
->picture
.h264
.field_order_cnt
[1] = tempPicOrderCnt
;
862 if (pps
->redundant_pic_cnt_present_flag
)
863 /* redundant_pic_cnt */
866 if (slice_type
== PIPE_H264_SLICE_TYPE_B
)
867 /* direct_spatial_mv_pred_flag */
870 priv
->picture
.h264
.num_ref_idx_l0_active_minus1
= pps
->num_ref_idx_l0_default_active_minus1
;
871 priv
->picture
.h264
.num_ref_idx_l1_active_minus1
= pps
->num_ref_idx_l1_default_active_minus1
;
873 if (slice_type
== PIPE_H264_SLICE_TYPE_P
||
874 slice_type
== PIPE_H264_SLICE_TYPE_SP
||
875 slice_type
== PIPE_H264_SLICE_TYPE_B
) {
877 /* num_ref_idx_active_override_flag */
878 if (vl_rbsp_u(rbsp
, 1)) {
879 priv
->picture
.h264
.num_ref_idx_l0_active_minus1
= vl_rbsp_ue(rbsp
);
881 if (slice_type
== PIPE_H264_SLICE_TYPE_B
)
882 priv
->picture
.h264
.num_ref_idx_l1_active_minus1
= vl_rbsp_ue(rbsp
);
886 if (nal_unit_type
== 20 || nal_unit_type
== 21)
887 ref_pic_list_mvc_modification(priv
, rbsp
);
889 ref_pic_list_modification(priv
, rbsp
, slice_type
);
891 if ((pps
->weighted_pred_flag
&& (slice_type
== PIPE_H264_SLICE_TYPE_P
|| slice_type
== PIPE_H264_SLICE_TYPE_SP
)) ||
892 (pps
->weighted_bipred_idc
== 1 && slice_type
== PIPE_H264_SLICE_TYPE_B
))
893 pred_weight_table(priv
, rbsp
, sps
, slice_type
);
895 if (nal_ref_idc
!= 0)
896 dec_ref_pic_marking(priv
, rbsp
, IdrPicFlag
);
898 if (pps
->entropy_coding_mode_flag
&& slice_type
!= PIPE_H264_SLICE_TYPE_I
&& slice_type
!= PIPE_H264_SLICE_TYPE_SI
)
905 if (slice_type
== PIPE_H264_SLICE_TYPE_SP
|| slice_type
== PIPE_H264_SLICE_TYPE_SI
) {
906 if (slice_type
== PIPE_H264_SLICE_TYPE_SP
)
907 /* sp_for_switch_flag */
914 if (pps
->deblocking_filter_control_present_flag
) {
915 unsigned disable_deblocking_filter_idc
= vl_rbsp_ue(rbsp
);
917 if (disable_deblocking_filter_idc
!= 1) {
918 /* slice_alpha_c0_offset_div2 */
921 /* slice_beta_offset_div2 */
926 if (pps
->num_slice_groups_minus1
> 0 && pps
->slice_group_map_type
>= 3 && pps
->slice_group_map_type
<= 5)
927 /* slice_group_change_cycle */
931 static void vid_dec_h264_Decode(vid_dec_PrivateType
*priv
, struct vl_vlc
*vlc
, unsigned min_bits_left
)
933 unsigned nal_ref_idc
, nal_unit_type
;
935 if (!vl_vlc_search_byte(vlc
, vl_vlc_bits_left(vlc
) - min_bits_left
, 0x00))
938 if (vl_vlc_peekbits(vlc
, 24) != 0x000001) {
939 vl_vlc_eatbits(vlc
, 8);
944 unsigned bytes
= priv
->bytes_left
- (vl_vlc_bits_left(vlc
) / 8);
945 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
, &priv
->picture
.base
,
946 1, &priv
->slice
, &bytes
);
950 vl_vlc_eatbits(vlc
, 24);
952 /* forbidden_zero_bit */
953 vl_vlc_eatbits(vlc
, 1);
955 nal_ref_idc
= vl_vlc_get_uimsbf(vlc
, 2);
957 if (nal_ref_idc
!= priv
->codec_data
.h264
.nal_ref_idc
&&
958 (nal_ref_idc
* priv
->codec_data
.h264
.nal_ref_idc
) == 0)
959 vid_dec_h264_EndFrame(priv
);
961 priv
->codec_data
.h264
.nal_ref_idc
= nal_ref_idc
;
963 nal_unit_type
= vl_vlc_get_uimsbf(vlc
, 5);
965 if (nal_unit_type
!= 1 && nal_unit_type
!= 5)
966 vid_dec_h264_EndFrame(priv
);
968 if (nal_unit_type
== 7) {
970 vl_rbsp_init(&rbsp
, vlc
, ~0);
971 seq_parameter_set(priv
, &rbsp
);
973 } else if (nal_unit_type
== 8) {
975 vl_rbsp_init(&rbsp
, vlc
, ~0);
976 picture_parameter_set(priv
, &rbsp
);
978 } else if (nal_unit_type
== 1 || nal_unit_type
== 5) {
979 /* Coded slice of a non-IDR or IDR picture */
980 unsigned bits
= vl_vlc_valid_bits(vlc
);
981 unsigned bytes
= bits
/ 8 + 4;
984 const void *ptr
= buf
;
990 buf
[3] = (nal_ref_idc
<< 5) | nal_unit_type
;
991 for (i
= 4; i
< bytes
; ++i
)
992 buf
[i
] = vl_vlc_peekbits(vlc
, bits
) >> ((bytes
- i
- 1) * 8);
994 priv
->bytes_left
= (vl_vlc_bits_left(vlc
) - bits
) / 8;
995 priv
->slice
= vlc
->data
;
997 vl_rbsp_init(&rbsp
, vlc
, 128);
998 slice_header(priv
, &rbsp
, nal_ref_idc
, nal_unit_type
);
1000 vid_dec_h264_BeginFrame(priv
);
1002 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
, &priv
->picture
.base
,
1006 /* resync to byte boundary */
1007 vl_vlc_eatbits(vlc
, vl_vlc_valid_bits(vlc
) % 8);