1 /**************************************************************************
3 * Copyright 2016 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 **************************************************************************/
28 #include "pipe/p_video_codec.h"
29 #include "util/u_memory.h"
30 #include "util/u_video.h"
31 #include "vl/vl_rbsp.h"
33 #include "entrypoint.h"
36 #define DPB_MAX_SIZE 32
37 #define MAX_NUM_REF_PICS 16
40 NAL_UNIT_TYPE_TRAIL_N
= 0,
41 NAL_UNIT_TYPE_TRAIL_R
= 1,
42 NAL_UNIT_TYPE_TSA_N
= 2,
43 NAL_UNIT_TYPE_TSA_R
= 3,
44 NAL_UNIT_TYPE_STSA_N
= 4,
45 NAL_UNIT_TYPE_STSA_R
= 5,
46 NAL_UNIT_TYPE_RADL_N
= 6,
47 NAL_UNIT_TYPE_RADL_R
= 7,
48 NAL_UNIT_TYPE_RASL_N
= 8,
49 NAL_UNIT_TYPE_RASL_R
= 9,
50 NAL_UNIT_TYPE_BLA_W_LP
= 16,
51 NAL_UNIT_TYPE_BLA_W_RADL
= 17,
52 NAL_UNIT_TYPE_BLA_N_LP
= 18,
53 NAL_UNIT_TYPE_IDR_W_RADL
= 19,
54 NAL_UNIT_TYPE_IDR_N_LP
= 20,
55 NAL_UNIT_TYPE_CRA
= 21,
56 NAL_UNIT_TYPE_SPS
= 33,
57 NAL_UNIT_TYPE_PPS
= 34,
61 struct list_head list
;
62 struct pipe_video_buffer
*buffer
;
69 unsigned num_neg_pics
;
70 unsigned num_pos_pics
;
71 unsigned num_delta_poc
;
72 int delta_poc
[MAX_NUM_REF_PICS
];
73 bool used
[MAX_NUM_REF_PICS
];
76 static bool is_idr_picture(unsigned nal_unit_type
)
78 return (nal_unit_type
== NAL_UNIT_TYPE_IDR_W_RADL
||
79 nal_unit_type
== NAL_UNIT_TYPE_IDR_N_LP
);
82 /* broken link access picture */
83 static bool is_bla_picture(unsigned nal_unit_type
)
85 return (nal_unit_type
== NAL_UNIT_TYPE_BLA_W_LP
||
86 nal_unit_type
== NAL_UNIT_TYPE_BLA_W_RADL
||
87 nal_unit_type
== NAL_UNIT_TYPE_BLA_N_LP
);
90 /* random access point picture */
91 static bool is_rap_picture(unsigned nal_unit_type
)
93 return (nal_unit_type
>= NAL_UNIT_TYPE_BLA_W_LP
&&
94 nal_unit_type
<= NAL_UNIT_TYPE_CRA
);
97 static bool is_slice_picture(unsigned nal_unit_type
)
99 return (nal_unit_type
<= NAL_UNIT_TYPE_RASL_R
||
100 is_rap_picture(nal_unit_type
));
103 static void set_poc(vid_dec_PrivateType
*priv
,
104 unsigned nal_unit_type
, int i
)
106 priv
->picture
.h265
.CurrPicOrderCntVal
= i
;
108 if (priv
->codec_data
.h265
.temporal_id
== 0 &&
109 (nal_unit_type
== NAL_UNIT_TYPE_TRAIL_R
||
110 nal_unit_type
== NAL_UNIT_TYPE_TSA_R
||
111 nal_unit_type
== NAL_UNIT_TYPE_STSA_R
||
112 is_rap_picture(nal_unit_type
)))
113 priv
->codec_data
.h265
.slice_prev_poc
= i
;
116 static unsigned get_poc(vid_dec_PrivateType
*priv
)
118 return priv
->picture
.h265
.CurrPicOrderCntVal
;
121 static void profile_tier(struct vl_rbsp
*rbsp
)
125 /* general_profile_space */
128 /* general_tier_flag */
131 /* general_profile_idc */
134 /* general_profile_compatibility_flag */
135 for(i
= 0; i
< 32; ++i
)
138 /* general_progressive_source_flag */
141 /* general_interlaced_source_flag */
144 /* general_non_packed_constraint_flag */
147 /* general_frame_only_constraint_flag */
150 /* general_reserved_zero_44bits */
156 static unsigned profile_tier_level(struct vl_rbsp
*rbsp
,
157 int max_sublayers_minus1
)
159 bool sub_layer_profile_present_flag
[6];
160 bool sub_layer_level_present_flag
[6];
166 /* general_level_idc */
167 level_idc
= vl_rbsp_u(rbsp
, 8);
169 for (i
= 0; i
< max_sublayers_minus1
; ++i
) {
170 sub_layer_profile_present_flag
[i
] = vl_rbsp_u(rbsp
, 1);
171 sub_layer_level_present_flag
[i
] = vl_rbsp_u(rbsp
, 1);
174 if (max_sublayers_minus1
> 0)
175 for (i
= max_sublayers_minus1
; i
< 8; ++i
)
176 /* reserved_zero_2bits */
179 for (i
= 0; i
< max_sublayers_minus1
; ++i
) {
180 if (sub_layer_profile_present_flag
[i
])
183 if (sub_layer_level_present_flag
[i
])
184 /* sub_layer_level_idc */
191 static void scaling_list_data(void)
197 static void st_ref_pic_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
198 struct ref_pic_set
*rps
, struct pipe_h265_sps
*sps
,
201 bool inter_rps_pred_flag
;
202 unsigned delta_idx_minus1
;
206 inter_rps_pred_flag
= (idx
!= 0) ? (vl_rbsp_u(rbsp
, 1)) : false;
208 if (inter_rps_pred_flag
) {
209 struct ref_pic_set
*ref_rps
;
215 if (idx
== sps
->num_short_term_ref_pic_sets
)
216 delta_idx_minus1
= vl_rbsp_ue(rbsp
);
218 delta_idx_minus1
= 0;
220 ref_rps
= (struct ref_pic_set
*)
221 priv
->codec_data
.h265
.ref_pic_set_list
+ idx
- (delta_idx_minus1
+ 1);
224 sign
= vl_rbsp_u(rbsp
, 1);
225 /* abs_delta_rps_minus1 */
226 abs
= vl_rbsp_ue(rbsp
);
227 delta_rps
= (1 - 2 * sign
) * (abs
+ 1);
229 rps
->num_neg_pics
= 0;
230 rps
->num_pos_pics
= 0;
233 for(i
= 0 ; i
<= ref_rps
->num_pics
; ++i
) {
234 /* used_by_curr_pic_flag */
235 if (!vl_rbsp_u(rbsp
, 1))
239 delta_poc
= delta_rps
+
240 ((i
< ref_rps
->num_pics
)? ref_rps
->delta_poc
[i
] : 0);
241 rps
->delta_poc
[rps
->num_pics
] = delta_poc
;
242 rps
->used
[rps
->num_pics
] = true;
251 rps
->num_delta_poc
= ref_rps
->num_pics
;
254 for (i
= 1; i
< rps
->num_pics
; ++i
) {
255 delta_poc
= rps
->delta_poc
[i
];
257 for (j
= i
- 1; j
>= 0; j
--) {
258 if (delta_poc
< rps
->delta_poc
[j
]) {
259 rps
->delta_poc
[j
+ 1] = rps
->delta_poc
[j
];
260 rps
->used
[j
+ 1] = rps
->used
[j
];
261 rps
->delta_poc
[j
] = delta_poc
;
267 for (i
= 0 , j
= rps
->num_neg_pics
- 1;
268 i
< rps
->num_neg_pics
>> 1; i
++, j
--) {
269 delta_poc
= rps
->delta_poc
[i
];
271 rps
->delta_poc
[i
] = rps
->delta_poc
[j
];
272 rps
->used
[i
] = rps
->used
[j
];
273 rps
->delta_poc
[j
] = delta_poc
;
277 /* num_negative_pics */
278 rps
->num_neg_pics
= vl_rbsp_ue(rbsp
);
279 /* num_positive_pics */
280 rps
->num_pos_pics
= vl_rbsp_ue(rbsp
);
281 rps
->num_pics
= rps
->num_neg_pics
+ rps
->num_pos_pics
;
284 for(i
= 0 ; i
< rps
->num_neg_pics
; ++i
) {
285 /* delta_poc_s0_minus1 */
286 delta_poc
-= (vl_rbsp_ue(rbsp
) + 1);
287 rps
->delta_poc
[i
] = delta_poc
;
288 /* used_by_curr_pic_s0_flag */
289 rps
->used
[i
] = vl_rbsp_u(rbsp
, 1);
293 for(i
= rps
->num_neg_pics
; i
< rps
->num_pics
; ++i
) {
294 /* delta_poc_s1_minus1 */
295 delta_poc
+= (vl_rbsp_ue(rbsp
) + 1);
296 rps
->delta_poc
[i
] = delta_poc
;
297 /* used_by_curr_pic_s1_flag */
298 rps
->used
[i
] = vl_rbsp_u(rbsp
, 1);
303 static struct pipe_h265_sps
*seq_parameter_set_id(vid_dec_PrivateType
*priv
,
304 struct vl_rbsp
*rbsp
)
306 unsigned id
= vl_rbsp_ue(rbsp
);
308 if (id
>= ARRAY_SIZE(priv
->codec_data
.h265
.sps
))
311 return &priv
->codec_data
.h265
.sps
[id
];
314 static void seq_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
316 struct pipe_h265_sps
*sps
;
317 int sps_max_sub_layers_minus1
;
320 /* sps_video_parameter_set_id */
323 /* sps_max_sub_layers_minus1 */
324 sps_max_sub_layers_minus1
= vl_rbsp_u(rbsp
, 3);
326 assert(sps_max_sub_layers_minus1
<= 6);
328 /* sps_temporal_id_nesting_flag */
331 priv
->codec_data
.h265
.level_idc
=
332 profile_tier_level(rbsp
, sps_max_sub_layers_minus1
);
334 sps
= seq_parameter_set_id(priv
, rbsp
);
338 memset(sps
, 0, sizeof(*sps
));
340 sps
->chroma_format_idc
= vl_rbsp_ue(rbsp
);
342 if (sps
->chroma_format_idc
== 3)
343 sps
->separate_colour_plane_flag
= vl_rbsp_u(rbsp
, 1);
345 priv
->codec_data
.h265
.pic_width_in_luma_samples
=
346 sps
->pic_width_in_luma_samples
= vl_rbsp_ue(rbsp
);
348 priv
->codec_data
.h265
.pic_height_in_luma_samples
=
349 sps
->pic_height_in_luma_samples
= vl_rbsp_ue(rbsp
);
351 /* conformance_window_flag */
352 if (vl_rbsp_u(rbsp
, 1)) {
353 /* conf_win_left_offset */
355 /* conf_win_right_offset */
357 /* conf_win_top_offset */
359 /* conf_win_bottom_offset */
363 sps
->bit_depth_luma_minus8
= vl_rbsp_ue(rbsp
);
364 sps
->bit_depth_chroma_minus8
= vl_rbsp_ue(rbsp
);
365 sps
->log2_max_pic_order_cnt_lsb_minus4
= vl_rbsp_ue(rbsp
);
367 /* sps_sub_layer_ordering_info_present_flag */
368 i
= vl_rbsp_u(rbsp
, 1) ? 0 : sps_max_sub_layers_minus1
;
369 for (; i
<= sps_max_sub_layers_minus1
; ++i
) {
370 sps
->sps_max_dec_pic_buffering_minus1
= vl_rbsp_ue(rbsp
);
371 /* sps_max_num_reorder_pics */
373 /* sps_max_latency_increase_plus */
377 sps
->log2_min_luma_coding_block_size_minus3
= vl_rbsp_ue(rbsp
);
378 sps
->log2_diff_max_min_luma_coding_block_size
= vl_rbsp_ue(rbsp
);
379 sps
->log2_min_transform_block_size_minus2
= vl_rbsp_ue(rbsp
);
380 sps
->log2_diff_max_min_transform_block_size
= vl_rbsp_ue(rbsp
);
381 sps
->max_transform_hierarchy_depth_inter
= vl_rbsp_ue(rbsp
);
382 sps
->max_transform_hierarchy_depth_intra
= vl_rbsp_ue(rbsp
);
384 sps
->scaling_list_enabled_flag
= vl_rbsp_u(rbsp
, 1);
385 if (sps
->scaling_list_enabled_flag
)
386 /* sps_scaling_list_data_present_flag */
387 if (vl_rbsp_u(rbsp
, 1))
390 sps
->amp_enabled_flag
= vl_rbsp_u(rbsp
, 1);
391 sps
->sample_adaptive_offset_enabled_flag
= vl_rbsp_u(rbsp
, 1);
392 sps
->pcm_enabled_flag
= vl_rbsp_u(rbsp
, 1);
393 if (sps
->pcm_enabled_flag
) {
394 sps
->pcm_sample_bit_depth_luma_minus1
= vl_rbsp_u(rbsp
, 4);
395 sps
->pcm_sample_bit_depth_chroma_minus1
= vl_rbsp_u(rbsp
, 4);
396 sps
->log2_min_pcm_luma_coding_block_size_minus3
= vl_rbsp_ue(rbsp
);
397 sps
->log2_diff_max_min_pcm_luma_coding_block_size
= vl_rbsp_ue(rbsp
);
398 sps
->pcm_loop_filter_disabled_flag
= vl_rbsp_u(rbsp
, 1);
401 sps
->num_short_term_ref_pic_sets
= vl_rbsp_ue(rbsp
);
403 for (i
= 0; i
< sps
->num_short_term_ref_pic_sets
; ++i
) {
404 struct ref_pic_set
*rps
;
406 rps
= (struct ref_pic_set
*)
407 priv
->codec_data
.h265
.ref_pic_set_list
+ i
;
408 st_ref_pic_set(priv
, rbsp
, rps
, sps
, i
);
411 sps
->long_term_ref_pics_present_flag
= vl_rbsp_u(rbsp
, 1);
412 if (sps
->long_term_ref_pics_present_flag
) {
413 sps
->num_long_term_ref_pics_sps
= vl_rbsp_ue(rbsp
);
414 for (i
= 0; i
< sps
->num_long_term_ref_pics_sps
; ++i
) {
415 /* lt_ref_pic_poc_lsb_sps */
416 vl_rbsp_u(rbsp
, sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4);
417 /* used_by_curr_pic_lt_sps_flag */
422 sps
->sps_temporal_mvp_enabled_flag
= vl_rbsp_u(rbsp
, 1);
423 sps
->strong_intra_smoothing_enabled_flag
= vl_rbsp_u(rbsp
, 1);
426 static struct pipe_h265_pps
*pic_parameter_set_id(vid_dec_PrivateType
*priv
,
427 struct vl_rbsp
*rbsp
)
429 unsigned id
= vl_rbsp_ue(rbsp
);
431 if (id
>= ARRAY_SIZE(priv
->codec_data
.h265
.pps
))
434 return &priv
->codec_data
.h265
.pps
[id
];
437 static void picture_parameter_set(vid_dec_PrivateType
*priv
,
438 struct vl_rbsp
*rbsp
)
440 struct pipe_h265_sps
*sps
;
441 struct pipe_h265_pps
*pps
;
444 pps
= pic_parameter_set_id(priv
, rbsp
);
448 memset(pps
, 0, sizeof(*pps
));
449 sps
= pps
->sps
= seq_parameter_set_id(priv
, rbsp
);
453 pps
->dependent_slice_segments_enabled_flag
= vl_rbsp_u(rbsp
, 1);
454 pps
->output_flag_present_flag
= vl_rbsp_u(rbsp
, 1);
455 pps
->num_extra_slice_header_bits
= vl_rbsp_u(rbsp
, 3);
456 pps
->sign_data_hiding_enabled_flag
= vl_rbsp_u(rbsp
, 1);
457 pps
->cabac_init_present_flag
= vl_rbsp_u(rbsp
, 1);
459 pps
->num_ref_idx_l0_default_active_minus1
= vl_rbsp_ue(rbsp
);
460 pps
->num_ref_idx_l1_default_active_minus1
= vl_rbsp_ue(rbsp
);
461 pps
->init_qp_minus26
= vl_rbsp_se(rbsp
);
462 pps
->constrained_intra_pred_flag
= vl_rbsp_u(rbsp
, 1);
463 pps
->transform_skip_enabled_flag
= vl_rbsp_u(rbsp
, 1);
465 pps
->cu_qp_delta_enabled_flag
= vl_rbsp_u(rbsp
, 1);
466 if (pps
->cu_qp_delta_enabled_flag
)
467 pps
->diff_cu_qp_delta_depth
= vl_rbsp_ue(rbsp
);
469 pps
->pps_cb_qp_offset
= vl_rbsp_se(rbsp
);
470 pps
->pps_cr_qp_offset
= vl_rbsp_se(rbsp
);
471 pps
->pps_slice_chroma_qp_offsets_present_flag
= vl_rbsp_u(rbsp
, 1);
473 pps
->weighted_pred_flag
= vl_rbsp_u(rbsp
, 1);
474 pps
->weighted_bipred_flag
= vl_rbsp_u(rbsp
, 1);
476 pps
->transquant_bypass_enabled_flag
= vl_rbsp_u(rbsp
, 1);
477 pps
->tiles_enabled_flag
= vl_rbsp_u(rbsp
, 1);
478 pps
->entropy_coding_sync_enabled_flag
= vl_rbsp_u(rbsp
, 1);
480 if (pps
->tiles_enabled_flag
) {
481 pps
->num_tile_columns_minus1
= vl_rbsp_ue(rbsp
);
482 pps
->num_tile_rows_minus1
= vl_rbsp_ue(rbsp
);
484 pps
->uniform_spacing_flag
= vl_rbsp_u(rbsp
, 1);
485 if (!pps
->uniform_spacing_flag
) {
486 for (i
= 0; i
< pps
->num_tile_columns_minus1
; ++i
)
487 pps
->column_width_minus1
[i
] = vl_rbsp_ue(rbsp
);
489 for (i
= 0; i
< pps
->num_tile_rows_minus1
; ++i
)
490 pps
->row_height_minus1
[i
] = vl_rbsp_ue(rbsp
);
493 if (!pps
->num_tile_columns_minus1
|| !pps
->num_tile_rows_minus1
)
494 pps
->loop_filter_across_tiles_enabled_flag
= vl_rbsp_u(rbsp
, 1);
497 pps
->pps_loop_filter_across_slices_enabled_flag
= vl_rbsp_u(rbsp
, 1);
499 pps
->deblocking_filter_control_present_flag
= vl_rbsp_u(rbsp
, 1);
500 if (pps
->deblocking_filter_control_present_flag
) {
501 pps
->deblocking_filter_override_enabled_flag
= vl_rbsp_u(rbsp
, 1);
502 pps
->pps_deblocking_filter_disabled_flag
= vl_rbsp_u(rbsp
, 1);
503 if (!pps
->pps_deblocking_filter_disabled_flag
) {
504 pps
->pps_beta_offset_div2
= vl_rbsp_se(rbsp
);
505 pps
->pps_tc_offset_div2
= vl_rbsp_se(rbsp
);
509 /* pps_scaling_list_data_present_flag */
510 if (vl_rbsp_u(rbsp
, 1))
513 pps
->lists_modification_present_flag
= vl_rbsp_u(rbsp
, 1);
514 pps
->log2_parallel_merge_level_minus2
= vl_rbsp_ue(rbsp
);
515 pps
->slice_segment_header_extension_present_flag
= vl_rbsp_u(rbsp
, 1);
518 static void vid_dec_h265_BeginFrame(vid_dec_PrivateType
*priv
)
520 if (priv
->frame_started
)
523 vid_dec_NeedTarget(priv
);
524 if (priv
->first_buf_in_frame
)
525 priv
->timestamp
= priv
->timestamps
[0];
526 priv
->first_buf_in_frame
= false;
529 struct pipe_video_codec templat
= {};
531 templat
.profile
= priv
->profile
;
532 templat
.entrypoint
= PIPE_VIDEO_ENTRYPOINT_BITSTREAM
;
533 templat
.chroma_format
= PIPE_VIDEO_CHROMA_FORMAT_420
;
534 templat
.expect_chunked_decode
= true;
535 templat
.width
= priv
->codec_data
.h265
.pic_width_in_luma_samples
;
536 templat
.height
= priv
->codec_data
.h265
.pic_height_in_luma_samples
;
537 templat
.level
= priv
->codec_data
.h265
.level_idc
;
538 priv
->codec
= priv
->pipe
->create_video_codec(priv
->pipe
, &templat
);
540 priv
->codec
->begin_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
541 priv
->frame_started
= true;
544 static struct pipe_video_buffer
*vid_dec_h265_Flush(vid_dec_PrivateType
*priv
,
545 OMX_TICKS
*timestamp
)
547 struct dpb_list
*entry
, *result
= NULL
;
548 struct pipe_video_buffer
*buf
;
550 /* search for the lowest poc and break on zeros */
551 LIST_FOR_EACH_ENTRY(entry
, &priv
->codec_data
.h265
.dpb_list
, list
) {
553 if (result
&& entry
->poc
== 0)
556 if (!result
|| entry
->poc
< result
->poc
)
563 buf
= result
->buffer
;
565 *timestamp
= result
->timestamp
;
567 --priv
->codec_data
.h265
.dpb_num
;
568 LIST_DEL(&result
->list
);
574 static void vid_dec_h265_EndFrame(vid_dec_PrivateType
*priv
)
576 struct dpb_list
*entry
= NULL
;
577 struct pipe_video_buffer
*tmp
;
578 struct ref_pic_set
*rps
;
582 if (!priv
->frame_started
)
585 priv
->picture
.h265
.NumPocStCurrBefore
= 0;
586 priv
->picture
.h265
.NumPocStCurrAfter
= 0;
587 memset(priv
->picture
.h265
.RefPicSetStCurrBefore
, 0, 8);
588 memset(priv
->picture
.h265
.RefPicSetStCurrAfter
, 0, 8);
589 for (i
= 0; i
< MAX_NUM_REF_PICS
; ++i
) {
590 priv
->picture
.h265
.ref
[i
] = NULL
;
591 priv
->picture
.h265
.PicOrderCntVal
[i
] = 0;
594 rps
= priv
->codec_data
.h265
.rps
;
597 priv
->picture
.h265
.NumDeltaPocsOfRefRpsIdx
= rps
->num_delta_poc
;
598 for (i
= 0; i
< rps
->num_pics
; ++i
) {
599 priv
->picture
.h265
.PicOrderCntVal
[i
] =
600 rps
->delta_poc
[i
] + get_poc(priv
);
602 LIST_FOR_EACH_ENTRY(entry
, &priv
->codec_data
.h265
.dpb_list
, list
) {
603 if (entry
->poc
== priv
->picture
.h265
.PicOrderCntVal
[i
]) {
604 priv
->picture
.h265
.ref
[i
] = entry
->buffer
;
610 if (i
< rps
->num_neg_pics
) {
611 priv
->picture
.h265
.NumPocStCurrBefore
++;
612 priv
->picture
.h265
.RefPicSetStCurrBefore
[i
] = i
;
614 int j
= i
- rps
->num_neg_pics
;
615 priv
->picture
.h265
.NumPocStCurrAfter
++;
616 priv
->picture
.h265
.RefPicSetStCurrAfter
[j
] = i
;
622 priv
->codec
->end_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
623 priv
->frame_started
= false;
625 /* add the decoded picture to the dpb list */
626 entry
= CALLOC_STRUCT(dpb_list
);
630 priv
->first_buf_in_frame
= true;
631 entry
->buffer
= priv
->target
;
632 entry
->timestamp
= priv
->timestamp
;
633 entry
->poc
= get_poc(priv
);
635 LIST_ADDTAIL(&entry
->list
, &priv
->codec_data
.h265
.dpb_list
);
636 ++priv
->codec_data
.h265
.dpb_num
;
639 if (priv
->codec_data
.h265
.dpb_num
<= DPB_MAX_SIZE
)
642 tmp
= priv
->in_buffers
[0]->pInputPortPrivate
;
643 priv
->in_buffers
[0]->pInputPortPrivate
= vid_dec_h265_Flush(priv
, ×tamp
);
644 priv
->in_buffers
[0]->nTimeStamp
= timestamp
;
646 priv
->frame_finished
= priv
->in_buffers
[0]->pInputPortPrivate
!= NULL
;
647 if (priv
->frame_finished
&&
648 (priv
->in_buffers
[0]->nFlags
& OMX_BUFFERFLAG_EOS
))
649 FREE(priv
->codec_data
.h265
.ref_pic_set_list
);
652 static void slice_header(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
653 unsigned nal_unit_type
)
655 struct pipe_h265_pps
*pps
;
656 struct pipe_h265_sps
*sps
;
657 bool first_slice_segment_in_pic_flag
;
658 bool dependent_slice_segment_flag
= false;
659 struct ref_pic_set
*rps
= NULL
;
660 unsigned poc_lsb
, poc_msb
, slice_prev_poc
;
661 unsigned max_poc_lsb
, prev_poc_lsb
, prev_poc_msb
;
665 if (priv
->picture
.h265
.IDRPicFlag
!= is_idr_picture(nal_unit_type
))
666 vid_dec_h265_EndFrame(priv
);
668 priv
->picture
.h265
.IDRPicFlag
= is_idr_picture(nal_unit_type
);
670 first_slice_segment_in_pic_flag
= vl_rbsp_u(rbsp
, 1);
672 if (is_rap_picture(nal_unit_type
))
673 /* no_output_of_prior_pics_flag */
676 pps
= pic_parameter_set_id(priv
, rbsp
);
684 if (pps
!= priv
->picture
.h265
.pps
)
685 vid_dec_h265_EndFrame(priv
);
687 priv
->picture
.h265
.pps
= pps
;
689 if (priv
->picture
.h265
.RAPPicFlag
!= is_rap_picture(nal_unit_type
))
690 vid_dec_h265_EndFrame(priv
);
691 priv
->picture
.h265
.RAPPicFlag
= is_rap_picture(nal_unit_type
);
693 num_st_rps
= sps
->num_short_term_ref_pic_sets
;
695 if (priv
->picture
.h265
.CurrRpsIdx
!= num_st_rps
)
696 vid_dec_h265_EndFrame(priv
);
697 priv
->picture
.h265
.CurrRpsIdx
= num_st_rps
;
699 if (!first_slice_segment_in_pic_flag
) {
701 int bits_slice_segment_address
= 0;
703 if (pps
->dependent_slice_segments_enabled_flag
)
704 dependent_slice_segment_flag
= vl_rbsp_u(rbsp
, 1);
706 size
= 1 << (sps
->log2_min_luma_coding_block_size_minus3
+ 3 +
707 sps
->log2_diff_max_min_luma_coding_block_size
);
709 num
= ((sps
->pic_width_in_luma_samples
+ size
- 1) / size
) *
710 ((sps
->pic_height_in_luma_samples
+ size
- 1) / size
);
712 while (num
> (1 << bits_slice_segment_address
))
713 bits_slice_segment_address
++;
715 /* slice_segment_address */
716 vl_rbsp_u(rbsp
, bits_slice_segment_address
);
719 if (dependent_slice_segment_flag
)
722 for (i
= 0; i
< pps
->num_extra_slice_header_bits
; ++i
)
723 /* slice_reserved_flag */
729 if (pps
->output_flag_present_flag
)
730 /* pic output flag */
733 if (sps
->separate_colour_plane_flag
)
734 /* colour_plane_id */
737 if (is_idr_picture(nal_unit_type
)) {
738 set_poc(priv
, nal_unit_type
, 0);
742 /* slice_pic_order_cnt_lsb */
744 vl_rbsp_u(rbsp
, sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4);
746 slice_prev_poc
= (int)priv
->codec_data
.h265
.slice_prev_poc
;
747 max_poc_lsb
= 1 << (sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4);
749 prev_poc_lsb
= slice_prev_poc
& (max_poc_lsb
- 1);
750 prev_poc_msb
= slice_prev_poc
- prev_poc_lsb
;
752 if ((poc_lsb
< prev_poc_lsb
) &&
753 ((prev_poc_lsb
- poc_lsb
) >= (max_poc_lsb
/ 2)))
754 poc_msb
= prev_poc_msb
+ max_poc_lsb
;
756 else if ((poc_lsb
> prev_poc_lsb
) &&
757 ((poc_lsb
- prev_poc_lsb
) > (max_poc_lsb
/ 2)))
758 poc_msb
= prev_poc_msb
- max_poc_lsb
;
761 poc_msb
= prev_poc_msb
;
763 if (is_bla_picture(nal_unit_type
))
766 if (get_poc(priv
) != poc_msb
+ poc_lsb
)
767 vid_dec_h265_EndFrame(priv
);
769 set_poc(priv
, nal_unit_type
, (poc_msb
+ poc_lsb
));
771 /* short_term_ref_pic_set_sps_flag */
772 if (!vl_rbsp_u(rbsp
, 1)) {
773 rps
= (struct ref_pic_set
*)
774 priv
->codec_data
.h265
.ref_pic_set_list
+ num_st_rps
;
775 st_ref_pic_set(priv
, rbsp
, rps
, sps
, num_st_rps
);
777 } else if (num_st_rps
> 1) {
781 while ((1 << num_bits
) < num_st_rps
)
785 /* short_term_ref_pic_set_idx */
786 idx
= vl_rbsp_u(rbsp
, num_bits
);
790 rps
= (struct ref_pic_set
*)
791 priv
->codec_data
.h265
.ref_pic_set_list
+ idx
;
794 if (is_bla_picture(nal_unit_type
)) {
795 rps
->num_neg_pics
= 0;
796 rps
->num_pos_pics
= 0;
800 priv
->codec_data
.h265
.rps
= rps
;
805 static void vid_dec_h265_Decode(vid_dec_PrivateType
*priv
,
807 unsigned min_bits_left
)
809 unsigned nal_unit_type
;
810 unsigned nuh_layer_id
;
811 unsigned nuh_temporal_id_plus1
;
813 if (!vl_vlc_search_byte(vlc
, vl_vlc_bits_left(vlc
) - min_bits_left
, 0x00))
816 if (vl_vlc_peekbits(vlc
, 24) != 0x000001) {
817 vl_vlc_eatbits(vlc
, 8);
822 unsigned bytes
= priv
->bytes_left
- (vl_vlc_bits_left(vlc
) / 8);
824 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
,
825 &priv
->picture
.base
, 1,
826 &priv
->slice
, &bytes
);
830 vl_vlc_eatbits(vlc
, 24);
832 /* forbidden_zero_bit */
833 vl_vlc_eatbits(vlc
, 1);
835 if (vl_vlc_valid_bits(vlc
) < 15)
836 vl_vlc_fillbits(vlc
);
838 nal_unit_type
= vl_vlc_get_uimsbf(vlc
, 6);
841 nuh_layer_id
= vl_vlc_get_uimsbf(vlc
, 6);
843 /* nuh_temporal_id_plus1 */
844 nuh_temporal_id_plus1
= vl_vlc_get_uimsbf(vlc
, 3);
845 priv
->codec_data
.h265
.temporal_id
= nuh_temporal_id_plus1
- 1;
847 if (!is_slice_picture(nal_unit_type
))
848 vid_dec_h265_EndFrame(priv
);
850 if (nal_unit_type
== NAL_UNIT_TYPE_SPS
) {
853 vl_rbsp_init(&rbsp
, vlc
, ~0);
854 seq_parameter_set(priv
, &rbsp
);
856 } else if (nal_unit_type
== NAL_UNIT_TYPE_PPS
) {
859 vl_rbsp_init(&rbsp
, vlc
, ~0);
860 picture_parameter_set(priv
, &rbsp
);
862 } else if (is_slice_picture(nal_unit_type
)) {
863 unsigned bits
= vl_vlc_valid_bits(vlc
);
864 unsigned bytes
= bits
/ 8 + 5;
867 const void *ptr
= buf
;
873 buf
[3] = nal_unit_type
<< 1 | nuh_layer_id
>> 5;
874 buf
[4] = nuh_layer_id
<< 3 | nuh_temporal_id_plus1
;
875 for (i
= 5; i
< bytes
; ++i
)
876 buf
[i
] = vl_vlc_peekbits(vlc
, bits
) >> ((bytes
- i
- 1) * 8);
878 priv
->bytes_left
= (vl_vlc_bits_left(vlc
) - bits
) / 8;
879 priv
->slice
= vlc
->data
;
881 vl_rbsp_init(&rbsp
, vlc
, 128);
882 slice_header(priv
, &rbsp
, nal_unit_type
);
884 vid_dec_h265_BeginFrame(priv
);
886 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
,
887 &priv
->picture
.base
, 1,
891 /* resync to byte boundary */
892 vl_vlc_eatbits(vlc
, vl_vlc_valid_bits(vlc
) % 8);
895 void vid_dec_h265_Init(vid_dec_PrivateType
*priv
)
897 priv
->picture
.base
.profile
= PIPE_VIDEO_PROFILE_HEVC_MAIN
;
899 LIST_INITHEAD(&priv
->codec_data
.h265
.dpb_list
);
900 priv
->codec_data
.h265
.ref_pic_set_list
= (struct ref_pic_set
*)
901 CALLOC(MAX_NUM_REF_PICS
, sizeof(struct ref_pic_set
));
903 priv
->Decode
= vid_dec_h265_Decode
;
904 priv
->EndFrame
= vid_dec_h265_EndFrame
;
905 priv
->Flush
= vid_dec_h265_Flush
;
906 priv
->first_buf_in_frame
= true;