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 16
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
;
68 unsigned num_neg_pics
;
69 unsigned num_pos_pics
;
70 unsigned num_delta_poc
;
71 int delta_poc
[MAX_NUM_REF_PICS
];
72 bool used
[MAX_NUM_REF_PICS
];
75 static bool is_idr_picture(unsigned nal_unit_type
)
77 return (nal_unit_type
== NAL_UNIT_TYPE_IDR_W_RADL
||
78 nal_unit_type
== NAL_UNIT_TYPE_IDR_N_LP
);
81 /* broken link access picture */
82 static bool is_bla_picture(unsigned nal_unit_type
)
84 return (nal_unit_type
== NAL_UNIT_TYPE_BLA_W_LP
||
85 nal_unit_type
== NAL_UNIT_TYPE_BLA_W_RADL
||
86 nal_unit_type
== NAL_UNIT_TYPE_BLA_N_LP
);
89 /* random access point picture */
90 static bool is_rap_picture(unsigned nal_unit_type
)
92 return (nal_unit_type
>= NAL_UNIT_TYPE_BLA_W_LP
&&
93 nal_unit_type
<= NAL_UNIT_TYPE_CRA
);
96 static bool is_slice_picture(unsigned nal_unit_type
)
98 return (nal_unit_type
<= NAL_UNIT_TYPE_RASL_R
||
99 is_rap_picture(nal_unit_type
));
102 static void set_poc(vid_dec_PrivateType
*priv
,
103 unsigned nal_unit_type
, int i
)
105 priv
->picture
.h265
.CurrPicOrderCntVal
= i
;
107 if (priv
->codec_data
.h265
.temporal_id
== 0 &&
108 (nal_unit_type
== NAL_UNIT_TYPE_TRAIL_R
||
109 nal_unit_type
== NAL_UNIT_TYPE_TSA_R
||
110 nal_unit_type
== NAL_UNIT_TYPE_STSA_R
||
111 is_rap_picture(nal_unit_type
)))
112 priv
->codec_data
.h265
.slice_prev_poc
= i
;
115 static unsigned get_poc(vid_dec_PrivateType
*priv
)
117 return priv
->picture
.h265
.CurrPicOrderCntVal
;
120 static void profile_tier(struct vl_rbsp
*rbsp
)
124 /* general_profile_space */
127 /* general_tier_flag */
130 /* general_profile_idc */
133 /* general_profile_compatibility_flag */
134 for(i
= 0; i
< 32; ++i
)
137 /* general_progressive_source_flag */
140 /* general_interlaced_source_flag */
143 /* general_non_packed_constraint_flag */
146 /* general_frame_only_constraint_flag */
149 /* general_reserved_zero_44bits */
155 static unsigned profile_tier_level(struct vl_rbsp
*rbsp
,
156 int max_sublayers_minus1
)
158 bool sub_layer_profile_present_flag
[6];
159 bool sub_layer_level_present_flag
[6];
165 /* general_level_idc */
166 level_idc
= vl_rbsp_u(rbsp
, 8);
168 for (i
= 0; i
< max_sublayers_minus1
; ++i
) {
169 sub_layer_profile_present_flag
[i
] = vl_rbsp_u(rbsp
, 1);
170 sub_layer_level_present_flag
[i
] = vl_rbsp_u(rbsp
, 1);
173 if (max_sublayers_minus1
> 0)
174 for (i
= max_sublayers_minus1
; i
< 8; ++i
)
175 /* reserved_zero_2bits */
178 for (i
= 0; i
< max_sublayers_minus1
; ++i
) {
179 if (sub_layer_profile_present_flag
[i
])
182 if (sub_layer_level_present_flag
[i
])
183 /* sub_layer_level_idc */
190 static void scaling_list_data(void)
196 static void st_ref_pic_set(void)
201 static struct pipe_h265_sps
*seq_parameter_set_id(vid_dec_PrivateType
*priv
,
202 struct vl_rbsp
*rbsp
)
204 unsigned id
= vl_rbsp_ue(rbsp
);
206 if (id
>= ARRAY_SIZE(priv
->codec_data
.h265
.sps
))
209 return &priv
->codec_data
.h265
.sps
[id
];
212 static void seq_parameter_set(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
)
214 struct pipe_h265_sps
*sps
;
215 int sps_max_sub_layers_minus1
;
218 /* sps_video_parameter_set_id */
221 /* sps_max_sub_layers_minus1 */
222 sps_max_sub_layers_minus1
= vl_rbsp_u(rbsp
, 3);
224 assert(sps_max_sub_layers_minus1
<= 6);
226 /* sps_temporal_id_nesting_flag */
229 priv
->codec_data
.h265
.level_idc
=
230 profile_tier_level(rbsp
, sps_max_sub_layers_minus1
);
232 sps
= seq_parameter_set_id(priv
, rbsp
);
236 memset(sps
, 0, sizeof(*sps
));
238 sps
->chroma_format_idc
= vl_rbsp_ue(rbsp
);
240 if (sps
->chroma_format_idc
== 3)
241 sps
->separate_colour_plane_flag
= vl_rbsp_u(rbsp
, 1);
243 sps
->pic_width_in_luma_samples
= vl_rbsp_ue(rbsp
);
245 sps
->pic_height_in_luma_samples
= vl_rbsp_ue(rbsp
);
247 /* conformance_window_flag */
248 if (vl_rbsp_u(rbsp
, 1)) {
249 /* conf_win_left_offset */
251 /* conf_win_right_offset */
253 /* conf_win_top_offset */
255 /* conf_win_bottom_offset */
259 sps
->bit_depth_luma_minus8
= vl_rbsp_ue(rbsp
);
260 sps
->bit_depth_chroma_minus8
= vl_rbsp_ue(rbsp
);
261 sps
->log2_max_pic_order_cnt_lsb_minus4
= vl_rbsp_ue(rbsp
);
263 /* sps_sub_layer_ordering_info_present_flag */
264 i
= vl_rbsp_u(rbsp
, 1) ? 0 : sps_max_sub_layers_minus1
;
265 for (; i
<= sps_max_sub_layers_minus1
; ++i
) {
266 sps
->sps_max_dec_pic_buffering_minus1
= vl_rbsp_ue(rbsp
);
267 /* sps_max_num_reorder_pics */
269 /* sps_max_latency_increase_plus */
273 sps
->log2_min_luma_coding_block_size_minus3
= vl_rbsp_ue(rbsp
);
274 sps
->log2_diff_max_min_luma_coding_block_size
= vl_rbsp_ue(rbsp
);
275 sps
->log2_min_transform_block_size_minus2
= vl_rbsp_ue(rbsp
);
276 sps
->log2_diff_max_min_transform_block_size
= vl_rbsp_ue(rbsp
);
277 sps
->max_transform_hierarchy_depth_inter
= vl_rbsp_ue(rbsp
);
278 sps
->max_transform_hierarchy_depth_intra
= vl_rbsp_ue(rbsp
);
280 sps
->scaling_list_enabled_flag
= vl_rbsp_u(rbsp
, 1);
281 if (sps
->scaling_list_enabled_flag
)
282 /* sps_scaling_list_data_present_flag */
283 if (vl_rbsp_u(rbsp
, 1))
286 sps
->amp_enabled_flag
= vl_rbsp_u(rbsp
, 1);
287 sps
->sample_adaptive_offset_enabled_flag
= vl_rbsp_u(rbsp
, 1);
288 sps
->pcm_enabled_flag
= vl_rbsp_u(rbsp
, 1);
289 if (sps
->pcm_enabled_flag
) {
290 sps
->pcm_sample_bit_depth_luma_minus1
= vl_rbsp_u(rbsp
, 4);
291 sps
->pcm_sample_bit_depth_chroma_minus1
= vl_rbsp_u(rbsp
, 4);
292 sps
->log2_min_pcm_luma_coding_block_size_minus3
= vl_rbsp_ue(rbsp
);
293 sps
->log2_diff_max_min_pcm_luma_coding_block_size
= vl_rbsp_ue(rbsp
);
294 sps
->pcm_loop_filter_disabled_flag
= vl_rbsp_u(rbsp
, 1);
297 sps
->num_short_term_ref_pic_sets
= vl_rbsp_ue(rbsp
);
299 for (i
= 0; i
< sps
->num_short_term_ref_pic_sets
; ++i
) {
300 struct ref_pic_set
*rps
;
302 rps
= (struct ref_pic_set
*)
303 priv
->codec_data
.h265
.ref_pic_set_list
+ i
;
307 sps
->long_term_ref_pics_present_flag
= vl_rbsp_u(rbsp
, 1);
308 if (sps
->long_term_ref_pics_present_flag
) {
309 sps
->num_long_term_ref_pics_sps
= vl_rbsp_ue(rbsp
);
310 for (i
= 0; i
< sps
->num_long_term_ref_pics_sps
; ++i
) {
311 /* lt_ref_pic_poc_lsb_sps */
312 vl_rbsp_u(rbsp
, sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4);
313 /* used_by_curr_pic_lt_sps_flag */
318 sps
->sps_temporal_mvp_enabled_flag
= vl_rbsp_u(rbsp
, 1);
319 sps
->strong_intra_smoothing_enabled_flag
= vl_rbsp_u(rbsp
, 1);
322 static struct pipe_h265_pps
*pic_parameter_set_id(vid_dec_PrivateType
*priv
,
323 struct vl_rbsp
*rbsp
)
325 unsigned id
= vl_rbsp_ue(rbsp
);
327 if (id
>= ARRAY_SIZE(priv
->codec_data
.h265
.pps
))
330 return &priv
->codec_data
.h265
.pps
[id
];
333 static void picture_parameter_set(vid_dec_PrivateType
*priv
,
334 struct vl_rbsp
*rbsp
)
336 struct pipe_h265_sps
*sps
;
337 struct pipe_h265_pps
*pps
;
340 pps
= pic_parameter_set_id(priv
, rbsp
);
344 memset(pps
, 0, sizeof(*pps
));
345 sps
= pps
->sps
= seq_parameter_set_id(priv
, rbsp
);
349 pps
->dependent_slice_segments_enabled_flag
= vl_rbsp_u(rbsp
, 1);
350 pps
->output_flag_present_flag
= vl_rbsp_u(rbsp
, 1);
351 pps
->num_extra_slice_header_bits
= vl_rbsp_u(rbsp
, 3);
352 pps
->sign_data_hiding_enabled_flag
= vl_rbsp_u(rbsp
, 1);
353 pps
->cabac_init_present_flag
= vl_rbsp_u(rbsp
, 1);
355 pps
->num_ref_idx_l0_default_active_minus1
= vl_rbsp_ue(rbsp
);
356 pps
->num_ref_idx_l1_default_active_minus1
= vl_rbsp_ue(rbsp
);
357 pps
->init_qp_minus26
= vl_rbsp_se(rbsp
);
358 pps
->constrained_intra_pred_flag
= vl_rbsp_u(rbsp
, 1);
359 pps
->transform_skip_enabled_flag
= vl_rbsp_u(rbsp
, 1);
361 pps
->cu_qp_delta_enabled_flag
= vl_rbsp_u(rbsp
, 1);
362 if (pps
->cu_qp_delta_enabled_flag
)
363 pps
->diff_cu_qp_delta_depth
= vl_rbsp_ue(rbsp
);
365 pps
->pps_cb_qp_offset
= vl_rbsp_se(rbsp
);
366 pps
->pps_cr_qp_offset
= vl_rbsp_se(rbsp
);
367 pps
->pps_slice_chroma_qp_offsets_present_flag
= vl_rbsp_u(rbsp
, 1);
369 pps
->weighted_pred_flag
= vl_rbsp_u(rbsp
, 1);
370 pps
->weighted_bipred_flag
= vl_rbsp_u(rbsp
, 1);
372 pps
->transquant_bypass_enabled_flag
= vl_rbsp_u(rbsp
, 1);
373 pps
->tiles_enabled_flag
= vl_rbsp_u(rbsp
, 1);
374 pps
->entropy_coding_sync_enabled_flag
= vl_rbsp_u(rbsp
, 1);
376 if (pps
->tiles_enabled_flag
) {
377 pps
->num_tile_columns_minus1
= vl_rbsp_ue(rbsp
);
378 pps
->num_tile_rows_minus1
= vl_rbsp_ue(rbsp
);
380 pps
->uniform_spacing_flag
= vl_rbsp_u(rbsp
, 1);
381 if (!pps
->uniform_spacing_flag
) {
382 for (i
= 0; i
< pps
->num_tile_columns_minus1
; ++i
)
383 pps
->column_width_minus1
[i
] = vl_rbsp_ue(rbsp
);
385 for (i
= 0; i
< pps
->num_tile_rows_minus1
; ++i
)
386 pps
->row_height_minus1
[i
] = vl_rbsp_ue(rbsp
);
389 if (!pps
->num_tile_columns_minus1
|| !pps
->num_tile_rows_minus1
)
390 pps
->loop_filter_across_tiles_enabled_flag
= vl_rbsp_u(rbsp
, 1);
393 pps
->pps_loop_filter_across_slices_enabled_flag
= vl_rbsp_u(rbsp
, 1);
395 pps
->deblocking_filter_control_present_flag
= vl_rbsp_u(rbsp
, 1);
396 if (pps
->deblocking_filter_control_present_flag
) {
397 pps
->deblocking_filter_override_enabled_flag
= vl_rbsp_u(rbsp
, 1);
398 pps
->pps_deblocking_filter_disabled_flag
= vl_rbsp_u(rbsp
, 1);
399 if (!pps
->pps_deblocking_filter_disabled_flag
) {
400 pps
->pps_beta_offset_div2
= vl_rbsp_se(rbsp
);
401 pps
->pps_tc_offset_div2
= vl_rbsp_se(rbsp
);
405 /* pps_scaling_list_data_present_flag */
406 if (vl_rbsp_u(rbsp
, 1))
409 pps
->lists_modification_present_flag
= vl_rbsp_u(rbsp
, 1);
410 pps
->log2_parallel_merge_level_minus2
= vl_rbsp_ue(rbsp
);
411 pps
->slice_segment_header_extension_present_flag
= vl_rbsp_u(rbsp
, 1);
414 static void vid_dec_h265_BeginFrame(vid_dec_PrivateType
*priv
)
416 if (priv
->frame_started
)
419 vid_dec_NeedTarget(priv
);
422 struct pipe_video_codec templat
= {};
423 omx_base_video_PortType
*port
;
425 port
= (omx_base_video_PortType
*)
426 priv
->ports
[OMX_BASE_FILTER_INPUTPORT_INDEX
];
427 templat
.profile
= priv
->profile
;
428 templat
.entrypoint
= PIPE_VIDEO_ENTRYPOINT_BITSTREAM
;
429 templat
.chroma_format
= PIPE_VIDEO_CHROMA_FORMAT_420
;
430 templat
.expect_chunked_decode
= true;
431 templat
.width
= align(port
->sPortParam
.format
.video
.nFrameWidth
, 4);
432 templat
.height
= align(port
->sPortParam
.format
.video
.nFrameHeight
, 4);
433 templat
.level
= priv
->codec_data
.h265
.level_idc
;
434 priv
->codec
= priv
->pipe
->create_video_codec(priv
->pipe
, &templat
);
436 priv
->codec
->begin_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
437 priv
->frame_started
= true;
440 static struct pipe_video_buffer
*vid_dec_h265_Flush(vid_dec_PrivateType
*priv
,
441 OMX_TICKS
*timestamp
)
443 struct dpb_list
*entry
, *result
= NULL
;
444 struct pipe_video_buffer
*buf
;
446 /* search for the lowest poc and break on zeros */
447 LIST_FOR_EACH_ENTRY(entry
, &priv
->codec_data
.h265
.dpb_list
, list
) {
449 if (result
&& entry
->poc
== 0)
452 if (!result
|| entry
->poc
< result
->poc
)
459 buf
= result
->buffer
;
461 --priv
->codec_data
.h265
.dpb_num
;
462 LIST_DEL(&result
->list
);
468 static void vid_dec_h265_EndFrame(vid_dec_PrivateType
*priv
)
470 struct dpb_list
*entry
= NULL
;
471 struct pipe_video_buffer
*tmp
;
473 if (!priv
->frame_started
)
476 priv
->codec
->end_frame(priv
->codec
, priv
->target
, &priv
->picture
.base
);
477 priv
->frame_started
= false;
479 /* add the decoded picture to the dpb list */
480 entry
= CALLOC_STRUCT(dpb_list
);
484 entry
->buffer
= priv
->target
;
485 entry
->poc
= get_poc(priv
);
487 LIST_ADDTAIL(&entry
->list
, &priv
->codec_data
.h265
.dpb_list
);
488 ++priv
->codec_data
.h265
.dpb_num
;
491 if (priv
->codec_data
.h265
.dpb_num
<= DPB_MAX_SIZE
)
494 tmp
= priv
->in_buffers
[0]->pInputPortPrivate
;
495 priv
->in_buffers
[0]->pInputPortPrivate
= vid_dec_h265_Flush(priv
, NULL
);
497 priv
->frame_finished
= priv
->in_buffers
[0]->pInputPortPrivate
!= NULL
;
500 static void slice_header(vid_dec_PrivateType
*priv
, struct vl_rbsp
*rbsp
,
501 unsigned nal_unit_type
)
503 struct pipe_h265_pps
*pps
;
504 struct pipe_h265_sps
*sps
;
505 bool first_slice_segment_in_pic_flag
;
506 bool dependent_slice_segment_flag
= false;
507 struct ref_pic_set
*rps
= NULL
;
508 unsigned poc_lsb
, poc_msb
, slice_prev_poc
;
509 unsigned max_poc_lsb
, prev_poc_lsb
, prev_poc_msb
;
513 if (priv
->picture
.h265
.IDRPicFlag
!= is_idr_picture(nal_unit_type
))
514 vid_dec_h265_EndFrame(priv
);
516 priv
->picture
.h265
.IDRPicFlag
= is_idr_picture(nal_unit_type
);
518 first_slice_segment_in_pic_flag
= vl_rbsp_u(rbsp
, 1);
520 if (is_rap_picture(nal_unit_type
))
521 /* no_output_of_prior_pics_flag */
524 pps
= pic_parameter_set_id(priv
, rbsp
);
532 if (pps
!= priv
->picture
.h265
.pps
)
533 vid_dec_h265_EndFrame(priv
);
535 priv
->picture
.h265
.pps
= pps
;
537 if (priv
->picture
.h265
.RAPPicFlag
!= is_rap_picture(nal_unit_type
))
538 vid_dec_h265_EndFrame(priv
);
539 priv
->picture
.h265
.RAPPicFlag
= is_rap_picture(nal_unit_type
);
541 num_st_rps
= sps
->num_short_term_ref_pic_sets
;
543 if (priv
->picture
.h265
.CurrRpsIdx
!= num_st_rps
)
544 vid_dec_h265_EndFrame(priv
);
545 priv
->picture
.h265
.CurrRpsIdx
= num_st_rps
;
547 if (!first_slice_segment_in_pic_flag
) {
549 int bits_slice_segment_address
= 0;
551 if (pps
->dependent_slice_segments_enabled_flag
)
552 dependent_slice_segment_flag
= vl_rbsp_u(rbsp
, 1);
554 size
= 1 << (sps
->log2_min_luma_coding_block_size_minus3
+ 3 +
555 sps
->log2_diff_max_min_luma_coding_block_size
);
557 num
= ((sps
->pic_width_in_luma_samples
+ size
- 1) / size
) *
558 ((sps
->pic_height_in_luma_samples
+ size
- 1) / size
);
560 while (num
> (1 << bits_slice_segment_address
))
561 bits_slice_segment_address
++;
563 /* slice_segment_address */
564 vl_rbsp_u(rbsp
, bits_slice_segment_address
);
567 if (dependent_slice_segment_flag
)
570 for (i
= 0; i
< pps
->num_extra_slice_header_bits
; ++i
)
571 /* slice_reserved_flag */
577 if (pps
->output_flag_present_flag
)
578 /* pic output flag */
581 if (sps
->separate_colour_plane_flag
)
582 /* colour_plane_id */
585 if (is_idr_picture(nal_unit_type
)) {
586 set_poc(priv
, nal_unit_type
, 0);
590 /* slice_pic_order_cnt_lsb */
592 vl_rbsp_u(rbsp
, sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4);
594 slice_prev_poc
= (int)priv
->codec_data
.h265
.slice_prev_poc
;
595 max_poc_lsb
= 1 << (sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4);
597 prev_poc_lsb
= slice_prev_poc
& (max_poc_lsb
- 1);
598 prev_poc_msb
= slice_prev_poc
- prev_poc_lsb
;
600 if ((poc_lsb
< prev_poc_lsb
) &&
601 ((prev_poc_lsb
- poc_lsb
) >= (max_poc_lsb
/ 2)))
602 poc_msb
= prev_poc_msb
+ max_poc_lsb
;
604 else if ((poc_lsb
> prev_poc_lsb
) &&
605 ((poc_lsb
- prev_poc_lsb
) > (max_poc_lsb
/ 2)))
606 poc_msb
= prev_poc_msb
- max_poc_lsb
;
609 poc_msb
= prev_poc_msb
;
611 if (is_bla_picture(nal_unit_type
))
614 if (get_poc(priv
) != poc_msb
+ poc_lsb
)
615 vid_dec_h265_EndFrame(priv
);
617 set_poc(priv
, nal_unit_type
, (poc_msb
+ poc_lsb
));
619 /* short_term_ref_pic_set_sps_flag */
620 if (!vl_rbsp_u(rbsp
, 1)) {
621 rps
= (struct ref_pic_set
*)
622 priv
->codec_data
.h265
.ref_pic_set_list
+ num_st_rps
;
625 } else if (num_st_rps
> 1) {
629 while ((1 << num_bits
) < num_st_rps
)
633 /* short_term_ref_pic_set_idx */
634 idx
= vl_rbsp_u(rbsp
, num_bits
);
638 rps
= (struct ref_pic_set
*)
639 priv
->codec_data
.h265
.ref_pic_set_list
+ idx
;
642 if (is_bla_picture(nal_unit_type
)) {
643 rps
->num_neg_pics
= 0;
644 rps
->num_pos_pics
= 0;
648 priv
->codec_data
.h265
.rps
= rps
;
653 static void vid_dec_h265_Decode(vid_dec_PrivateType
*priv
,
655 unsigned min_bits_left
)
657 unsigned nal_unit_type
;
658 unsigned nuh_layer_id
;
659 unsigned nuh_temporal_id_plus1
;
661 if (!vl_vlc_search_byte(vlc
, vl_vlc_bits_left(vlc
) - min_bits_left
, 0x00))
664 if (vl_vlc_peekbits(vlc
, 24) != 0x000001) {
665 vl_vlc_eatbits(vlc
, 8);
670 unsigned bytes
= priv
->bytes_left
- (vl_vlc_bits_left(vlc
) / 8);
672 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
,
673 &priv
->picture
.base
, 1,
674 &priv
->slice
, &bytes
);
678 vl_vlc_eatbits(vlc
, 24);
680 /* forbidden_zero_bit */
681 vl_vlc_eatbits(vlc
, 1);
683 if (vl_vlc_valid_bits(vlc
) < 15)
684 vl_vlc_fillbits(vlc
);
686 nal_unit_type
= vl_vlc_get_uimsbf(vlc
, 6);
689 nuh_layer_id
= vl_vlc_get_uimsbf(vlc
, 6);
691 /* nuh_temporal_id_plus1 */
692 nuh_temporal_id_plus1
= vl_vlc_get_uimsbf(vlc
, 3);
693 priv
->codec_data
.h265
.temporal_id
= nuh_temporal_id_plus1
- 1;
695 if (!is_slice_picture(nal_unit_type
))
696 vid_dec_h265_EndFrame(priv
);
698 if (nal_unit_type
== NAL_UNIT_TYPE_SPS
) {
701 vl_rbsp_init(&rbsp
, vlc
, ~0);
702 seq_parameter_set(priv
, &rbsp
);
704 } else if (nal_unit_type
== NAL_UNIT_TYPE_PPS
) {
707 vl_rbsp_init(&rbsp
, vlc
, ~0);
708 picture_parameter_set(priv
, &rbsp
);
710 } else if (is_slice_picture(nal_unit_type
)) {
711 unsigned bits
= vl_vlc_valid_bits(vlc
);
712 unsigned bytes
= bits
/ 8 + 5;
715 const void *ptr
= buf
;
721 buf
[3] = nal_unit_type
<< 1 | nuh_layer_id
>> 5;
722 buf
[4] = nuh_layer_id
<< 3 | nuh_temporal_id_plus1
;
723 for (i
= 5; i
< bytes
; ++i
)
724 buf
[i
] = vl_vlc_peekbits(vlc
, bits
) >> ((bytes
- i
- 1) * 8);
726 priv
->bytes_left
= (vl_vlc_bits_left(vlc
) - bits
) / 8;
727 priv
->slice
= vlc
->data
;
729 vl_rbsp_init(&rbsp
, vlc
, 128);
730 slice_header(priv
, &rbsp
, nal_unit_type
);
732 vid_dec_h265_BeginFrame(priv
);
734 priv
->codec
->decode_bitstream(priv
->codec
, priv
->target
,
735 &priv
->picture
.base
, 1,
739 /* resync to byte boundary */
740 vl_vlc_eatbits(vlc
, vl_vlc_valid_bits(vlc
) % 8);
743 void vid_dec_h265_Init(vid_dec_PrivateType
*priv
)
745 priv
->picture
.base
.profile
= PIPE_VIDEO_PROFILE_HEVC_MAIN
;
747 LIST_INITHEAD(&priv
->codec_data
.h265
.dpb_list
);
749 priv
->Decode
= vid_dec_h265_Decode
;
750 priv
->EndFrame
= vid_dec_h265_EndFrame
;
751 priv
->Flush
= vid_dec_h265_Flush
;