st/omx/dec/h265: add slice header
[mesa.git] / src / gallium / state_trackers / omx / vid_dec_h265.c
1 /**************************************************************************
2 *
3 * Copyright 2016 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
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"
32
33 #include "entrypoint.h"
34 #include "vid_dec.h"
35
36 #define DPB_MAX_SIZE 16
37 #define MAX_NUM_REF_PICS 16
38
39 enum {
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,
58 };
59
60 struct dpb_list {
61 struct list_head list;
62 struct pipe_video_buffer *buffer;
63 unsigned poc;
64 };
65
66 struct ref_pic_set {
67 unsigned num_pics;
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];
73 };
74
75 static bool is_idr_picture(unsigned nal_unit_type)
76 {
77 return (nal_unit_type == NAL_UNIT_TYPE_IDR_W_RADL ||
78 nal_unit_type == NAL_UNIT_TYPE_IDR_N_LP);
79 }
80
81 /* broken link access picture */
82 static bool is_bla_picture(unsigned nal_unit_type)
83 {
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);
87 }
88
89 /* random access point picture */
90 static bool is_rap_picture(unsigned nal_unit_type)
91 {
92 return (nal_unit_type >= NAL_UNIT_TYPE_BLA_W_LP &&
93 nal_unit_type <= NAL_UNIT_TYPE_CRA);
94 }
95
96 static bool is_slice_picture(unsigned nal_unit_type)
97 {
98 return (nal_unit_type <= NAL_UNIT_TYPE_RASL_R ||
99 is_rap_picture(nal_unit_type));
100 }
101
102 static void set_poc(vid_dec_PrivateType *priv,
103 unsigned nal_unit_type, int i)
104 {
105 priv->picture.h265.CurrPicOrderCntVal = i;
106
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;
113 }
114
115 static unsigned get_poc(vid_dec_PrivateType *priv)
116 {
117 return priv->picture.h265.CurrPicOrderCntVal;
118 }
119
120 static void profile_tier(struct vl_rbsp *rbsp)
121 {
122 int i;
123
124 /* general_profile_space */
125 vl_rbsp_u(rbsp, 2);
126
127 /* general_tier_flag */
128 vl_rbsp_u(rbsp, 1);
129
130 /* general_profile_idc */
131 vl_rbsp_u(rbsp, 5);
132
133 /* general_profile_compatibility_flag */
134 for(i = 0; i < 32; ++i)
135 vl_rbsp_u(rbsp, 1);
136
137 /* general_progressive_source_flag */
138 vl_rbsp_u(rbsp, 1);
139
140 /* general_interlaced_source_flag */
141 vl_rbsp_u(rbsp, 1);
142
143 /* general_non_packed_constraint_flag */
144 vl_rbsp_u(rbsp, 1);
145
146 /* general_frame_only_constraint_flag */
147 vl_rbsp_u(rbsp, 1);
148
149 /* general_reserved_zero_44bits */
150 vl_rbsp_u(rbsp, 16);
151 vl_rbsp_u(rbsp, 16);
152 vl_rbsp_u(rbsp, 12);
153 }
154
155 static unsigned profile_tier_level(struct vl_rbsp *rbsp,
156 int max_sublayers_minus1)
157 {
158 bool sub_layer_profile_present_flag[6];
159 bool sub_layer_level_present_flag[6];
160 unsigned level_idc;
161 int i;
162
163 profile_tier(rbsp);
164
165 /* general_level_idc */
166 level_idc = vl_rbsp_u(rbsp, 8);
167
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);
171 }
172
173 if (max_sublayers_minus1 > 0)
174 for (i = max_sublayers_minus1; i < 8; ++i)
175 /* reserved_zero_2bits */
176 vl_rbsp_u(rbsp, 2);
177
178 for (i = 0; i < max_sublayers_minus1; ++i) {
179 if (sub_layer_profile_present_flag[i])
180 profile_tier(rbsp);
181
182 if (sub_layer_level_present_flag[i])
183 /* sub_layer_level_idc */
184 vl_rbsp_u(rbsp, 8);
185 }
186
187 return level_idc;
188 }
189
190 static void scaling_list_data(void)
191 {
192 /* TODO */
193 assert(0);
194 }
195
196 static void st_ref_pic_set(void)
197 {
198 /* TODO */
199 }
200
201 static struct pipe_h265_sps *seq_parameter_set_id(vid_dec_PrivateType *priv,
202 struct vl_rbsp *rbsp)
203 {
204 unsigned id = vl_rbsp_ue(rbsp);
205
206 if (id >= ARRAY_SIZE(priv->codec_data.h265.sps))
207 return NULL;
208
209 return &priv->codec_data.h265.sps[id];
210 }
211
212 static void seq_parameter_set(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp)
213 {
214 struct pipe_h265_sps *sps;
215 int sps_max_sub_layers_minus1;
216 unsigned i;
217
218 /* sps_video_parameter_set_id */
219 vl_rbsp_u(rbsp, 4);
220
221 /* sps_max_sub_layers_minus1 */
222 sps_max_sub_layers_minus1 = vl_rbsp_u(rbsp, 3);
223
224 assert(sps_max_sub_layers_minus1 <= 6);
225
226 /* sps_temporal_id_nesting_flag */
227 vl_rbsp_u(rbsp, 1);
228
229 priv->codec_data.h265.level_idc =
230 profile_tier_level(rbsp, sps_max_sub_layers_minus1);
231
232 sps = seq_parameter_set_id(priv, rbsp);
233 if (!sps)
234 return;
235
236 memset(sps, 0, sizeof(*sps));
237
238 sps->chroma_format_idc = vl_rbsp_ue(rbsp);
239
240 if (sps->chroma_format_idc == 3)
241 sps->separate_colour_plane_flag = vl_rbsp_u(rbsp, 1);
242
243 sps->pic_width_in_luma_samples = vl_rbsp_ue(rbsp);
244
245 sps->pic_height_in_luma_samples = vl_rbsp_ue(rbsp);
246
247 /* conformance_window_flag */
248 if (vl_rbsp_u(rbsp, 1)) {
249 /* conf_win_left_offset */
250 vl_rbsp_ue(rbsp);
251 /* conf_win_right_offset */
252 vl_rbsp_ue(rbsp);
253 /* conf_win_top_offset */
254 vl_rbsp_ue(rbsp);
255 /* conf_win_bottom_offset */
256 vl_rbsp_ue(rbsp);
257 }
258
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);
262
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 */
268 vl_rbsp_ue(rbsp);
269 /* sps_max_latency_increase_plus */
270 vl_rbsp_ue(rbsp);
271 }
272
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);
279
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))
284 scaling_list_data();
285
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);
295 }
296
297 sps->num_short_term_ref_pic_sets = vl_rbsp_ue(rbsp);
298
299 for (i = 0; i < sps->num_short_term_ref_pic_sets; ++i) {
300 struct ref_pic_set *rps;
301
302 rps = (struct ref_pic_set *)
303 priv->codec_data.h265.ref_pic_set_list + i;
304 st_ref_pic_set();
305 }
306
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 */
314 vl_rbsp_u(rbsp, 1);
315 }
316 }
317
318 sps->sps_temporal_mvp_enabled_flag = vl_rbsp_u(rbsp, 1);
319 sps->strong_intra_smoothing_enabled_flag = vl_rbsp_u(rbsp, 1);
320 }
321
322 static struct pipe_h265_pps *pic_parameter_set_id(vid_dec_PrivateType *priv,
323 struct vl_rbsp *rbsp)
324 {
325 unsigned id = vl_rbsp_ue(rbsp);
326
327 if (id >= ARRAY_SIZE(priv->codec_data.h265.pps))
328 return NULL;
329
330 return &priv->codec_data.h265.pps[id];
331 }
332
333 static void picture_parameter_set(vid_dec_PrivateType *priv,
334 struct vl_rbsp *rbsp)
335 {
336 struct pipe_h265_sps *sps;
337 struct pipe_h265_pps *pps;
338 int i;
339
340 pps = pic_parameter_set_id(priv, rbsp);
341 if (!pps)
342 return;
343
344 memset(pps, 0, sizeof(*pps));
345 sps = pps->sps = seq_parameter_set_id(priv, rbsp);
346 if (!sps)
347 return;
348
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);
354
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);
360
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);
364
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);
368
369 pps->weighted_pred_flag = vl_rbsp_u(rbsp, 1);
370 pps->weighted_bipred_flag = vl_rbsp_u(rbsp, 1);
371
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);
375
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);
379
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);
384
385 for (i = 0; i < pps->num_tile_rows_minus1; ++i)
386 pps->row_height_minus1[i] = vl_rbsp_ue(rbsp);
387 }
388
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);
391 }
392
393 pps->pps_loop_filter_across_slices_enabled_flag = vl_rbsp_u(rbsp, 1);
394
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);
402 }
403 }
404
405 /* pps_scaling_list_data_present_flag */
406 if (vl_rbsp_u(rbsp, 1))
407 scaling_list_data();
408
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);
412 }
413
414 static void vid_dec_h265_BeginFrame(vid_dec_PrivateType *priv)
415 {
416 if (priv->frame_started)
417 return;
418
419 vid_dec_NeedTarget(priv);
420
421 if (!priv->codec) {
422 struct pipe_video_codec templat = {};
423 omx_base_video_PortType *port;
424
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);
435 }
436 priv->codec->begin_frame(priv->codec, priv->target, &priv->picture.base);
437 priv->frame_started = true;
438 }
439
440 static struct pipe_video_buffer *vid_dec_h265_Flush(vid_dec_PrivateType *priv,
441 OMX_TICKS *timestamp)
442 {
443 struct dpb_list *entry, *result = NULL;
444 struct pipe_video_buffer *buf;
445
446 /* search for the lowest poc and break on zeros */
447 LIST_FOR_EACH_ENTRY(entry, &priv->codec_data.h265.dpb_list, list) {
448
449 if (result && entry->poc == 0)
450 break;
451
452 if (!result || entry->poc < result->poc)
453 result = entry;
454 }
455
456 if (!result)
457 return NULL;
458
459 buf = result->buffer;
460
461 --priv->codec_data.h265.dpb_num;
462 LIST_DEL(&result->list);
463 FREE(result);
464
465 return buf;
466 }
467
468 static void vid_dec_h265_EndFrame(vid_dec_PrivateType *priv)
469 {
470 struct dpb_list *entry = NULL;
471 struct pipe_video_buffer *tmp;
472
473 if (!priv->frame_started)
474 return;
475
476 priv->codec->end_frame(priv->codec, priv->target, &priv->picture.base);
477 priv->frame_started = false;
478
479 /* add the decoded picture to the dpb list */
480 entry = CALLOC_STRUCT(dpb_list);
481 if (!entry)
482 return;
483
484 entry->buffer = priv->target;
485 entry->poc = get_poc(priv);
486
487 LIST_ADDTAIL(&entry->list, &priv->codec_data.h265.dpb_list);
488 ++priv->codec_data.h265.dpb_num;
489 priv->target = NULL;
490
491 if (priv->codec_data.h265.dpb_num <= DPB_MAX_SIZE)
492 return;
493
494 tmp = priv->in_buffers[0]->pInputPortPrivate;
495 priv->in_buffers[0]->pInputPortPrivate = vid_dec_h265_Flush(priv, NULL);
496 priv->target = tmp;
497 priv->frame_finished = priv->in_buffers[0]->pInputPortPrivate != NULL;
498 }
499
500 static void slice_header(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp,
501 unsigned nal_unit_type)
502 {
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;
510 unsigned num_st_rps;
511 int i;
512
513 if (priv->picture.h265.IDRPicFlag != is_idr_picture(nal_unit_type))
514 vid_dec_h265_EndFrame(priv);
515
516 priv->picture.h265.IDRPicFlag = is_idr_picture(nal_unit_type);
517
518 first_slice_segment_in_pic_flag = vl_rbsp_u(rbsp, 1);
519
520 if (is_rap_picture(nal_unit_type))
521 /* no_output_of_prior_pics_flag */
522 vl_rbsp_u(rbsp, 1);
523
524 pps = pic_parameter_set_id(priv, rbsp);
525 if (!pps)
526 return;
527
528 sps = pps->sps;
529 if (!sps)
530 return;
531
532 if (pps != priv->picture.h265.pps)
533 vid_dec_h265_EndFrame(priv);
534
535 priv->picture.h265.pps = pps;
536
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);
540
541 num_st_rps = sps->num_short_term_ref_pic_sets;
542
543 if (priv->picture.h265.CurrRpsIdx != num_st_rps)
544 vid_dec_h265_EndFrame(priv);
545 priv->picture.h265.CurrRpsIdx = num_st_rps;
546
547 if (!first_slice_segment_in_pic_flag) {
548 int size, num;
549 int bits_slice_segment_address = 0;
550
551 if (pps->dependent_slice_segments_enabled_flag)
552 dependent_slice_segment_flag = vl_rbsp_u(rbsp, 1);
553
554 size = 1 << (sps->log2_min_luma_coding_block_size_minus3 + 3 +
555 sps->log2_diff_max_min_luma_coding_block_size);
556
557 num = ((sps->pic_width_in_luma_samples + size - 1) / size) *
558 ((sps->pic_height_in_luma_samples + size - 1) / size);
559
560 while (num > (1 << bits_slice_segment_address))
561 bits_slice_segment_address++;
562
563 /* slice_segment_address */
564 vl_rbsp_u(rbsp, bits_slice_segment_address);
565 }
566
567 if (dependent_slice_segment_flag)
568 return;
569
570 for (i = 0; i < pps->num_extra_slice_header_bits; ++i)
571 /* slice_reserved_flag */
572 vl_rbsp_u(rbsp, 1);
573
574 /* slice_type */
575 vl_rbsp_ue(rbsp);
576
577 if (pps->output_flag_present_flag)
578 /* pic output flag */
579 vl_rbsp_u(rbsp, 1);
580
581 if (sps->separate_colour_plane_flag)
582 /* colour_plane_id */
583 vl_rbsp_u(rbsp, 2);
584
585 if (is_idr_picture(nal_unit_type)) {
586 set_poc(priv, nal_unit_type, 0);
587 return;
588 }
589
590 /* slice_pic_order_cnt_lsb */
591 poc_lsb =
592 vl_rbsp_u(rbsp, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
593
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);
596
597 prev_poc_lsb = slice_prev_poc & (max_poc_lsb - 1);
598 prev_poc_msb = slice_prev_poc - prev_poc_lsb;
599
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;
603
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;
607
608 else
609 poc_msb = prev_poc_msb;
610
611 if (is_bla_picture(nal_unit_type))
612 poc_msb = 0;
613
614 if (get_poc(priv) != poc_msb + poc_lsb)
615 vid_dec_h265_EndFrame(priv);
616
617 set_poc(priv, nal_unit_type, (poc_msb + poc_lsb));
618
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;
623 st_ref_pic_set();
624
625 } else if (num_st_rps > 1) {
626 int num_bits = 0;
627 unsigned idx;
628
629 while ((1 << num_bits) < num_st_rps)
630 num_bits++;
631
632 if (num_bits > 0)
633 /* short_term_ref_pic_set_idx */
634 idx = vl_rbsp_u(rbsp, num_bits);
635 else
636 idx = 0;
637
638 rps = (struct ref_pic_set *)
639 priv->codec_data.h265.ref_pic_set_list + idx;
640 }
641
642 if (is_bla_picture(nal_unit_type)) {
643 rps->num_neg_pics = 0;
644 rps->num_pos_pics = 0;
645 rps->num_pics = 0;
646 }
647
648 priv->codec_data.h265.rps = rps;
649
650 return;
651 }
652
653 static void vid_dec_h265_Decode(vid_dec_PrivateType *priv,
654 struct vl_vlc *vlc,
655 unsigned min_bits_left)
656 {
657 unsigned nal_unit_type;
658 unsigned nuh_layer_id;
659 unsigned nuh_temporal_id_plus1;
660
661 if (!vl_vlc_search_byte(vlc, vl_vlc_bits_left(vlc) - min_bits_left, 0x00))
662 return;
663
664 if (vl_vlc_peekbits(vlc, 24) != 0x000001) {
665 vl_vlc_eatbits(vlc, 8);
666 return;
667 }
668
669 if (priv->slice) {
670 unsigned bytes = priv->bytes_left - (vl_vlc_bits_left(vlc) / 8);
671
672 priv->codec->decode_bitstream(priv->codec, priv->target,
673 &priv->picture.base, 1,
674 &priv->slice, &bytes);
675 priv->slice = NULL;
676 }
677
678 vl_vlc_eatbits(vlc, 24);
679
680 /* forbidden_zero_bit */
681 vl_vlc_eatbits(vlc, 1);
682
683 if (vl_vlc_valid_bits(vlc) < 15)
684 vl_vlc_fillbits(vlc);
685
686 nal_unit_type = vl_vlc_get_uimsbf(vlc, 6);
687
688 /* nuh_layer_id */
689 nuh_layer_id = vl_vlc_get_uimsbf(vlc, 6);
690
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;
694
695 if (!is_slice_picture(nal_unit_type))
696 vid_dec_h265_EndFrame(priv);
697
698 if (nal_unit_type == NAL_UNIT_TYPE_SPS) {
699 struct vl_rbsp rbsp;
700
701 vl_rbsp_init(&rbsp, vlc, ~0);
702 seq_parameter_set(priv, &rbsp);
703
704 } else if (nal_unit_type == NAL_UNIT_TYPE_PPS) {
705 struct vl_rbsp rbsp;
706
707 vl_rbsp_init(&rbsp, vlc, ~0);
708 picture_parameter_set(priv, &rbsp);
709
710 } else if (is_slice_picture(nal_unit_type)) {
711 unsigned bits = vl_vlc_valid_bits(vlc);
712 unsigned bytes = bits / 8 + 5;
713 struct vl_rbsp rbsp;
714 uint8_t buf[9];
715 const void *ptr = buf;
716 unsigned i;
717
718 buf[0] = 0x0;
719 buf[1] = 0x0;
720 buf[2] = 0x1;
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);
725
726 priv->bytes_left = (vl_vlc_bits_left(vlc) - bits) / 8;
727 priv->slice = vlc->data;
728
729 vl_rbsp_init(&rbsp, vlc, 128);
730 slice_header(priv, &rbsp, nal_unit_type);
731
732 vid_dec_h265_BeginFrame(priv);
733
734 priv->codec->decode_bitstream(priv->codec, priv->target,
735 &priv->picture.base, 1,
736 &ptr, &bytes);
737 }
738
739 /* resync to byte boundary */
740 vl_vlc_eatbits(vlc, vl_vlc_valid_bits(vlc) % 8);
741 }
742
743 void vid_dec_h265_Init(vid_dec_PrivateType *priv)
744 {
745 priv->picture.base.profile = PIPE_VIDEO_PROFILE_HEVC_MAIN;
746
747 LIST_INITHEAD(&priv->codec_data.h265.dpb_list);
748
749 priv->Decode = vid_dec_h265_Decode;
750 priv->EndFrame = vid_dec_h265_EndFrame;
751 priv->Flush = vid_dec_h265_Flush;
752 }