df2dc057c8b7f34d63bf538ba260ad4f48f3bbf6
[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(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp,
197 struct ref_pic_set *rps, struct pipe_h265_sps *sps,
198 unsigned idx)
199 {
200 bool inter_rps_pred_flag;
201 unsigned delta_idx_minus1;
202 int delta_poc;
203 int i;
204
205 inter_rps_pred_flag = (idx != 0) ? (vl_rbsp_u(rbsp, 1)) : false;
206
207 if (inter_rps_pred_flag) {
208 struct ref_pic_set *ref_rps;
209 unsigned sign, abs;
210 int delta_rps;
211 bool used;
212 int j;
213
214 if (idx == sps->num_short_term_ref_pic_sets)
215 delta_idx_minus1 = vl_rbsp_ue(rbsp);
216 else
217 delta_idx_minus1 = 0;
218
219 ref_rps = (struct ref_pic_set *)
220 priv->codec_data.h265.ref_pic_set_list + idx - (delta_idx_minus1 + 1);
221
222 /* delta_rps_sign */
223 sign = vl_rbsp_u(rbsp, 1);
224 /* abs_delta_rps_minus1 */
225 abs = vl_rbsp_ue(rbsp);
226 delta_rps = (1 - 2 * sign) * (abs + 1);
227
228 rps->num_neg_pics = 0;
229 rps->num_pos_pics = 0;
230 rps->num_pics = 0;
231
232 for(i = 0 ; i <= ref_rps->num_pics; ++i) {
233 /* used_by_curr_pic_flag */
234 if (!vl_rbsp_u(rbsp, 1))
235 /* use_delta_flag */
236 vl_rbsp_u(rbsp, 1);
237 else {
238 delta_poc = delta_rps +
239 ((i < ref_rps->num_pics)? ref_rps->delta_poc[i] : 0);
240 rps->delta_poc[rps->num_pics] = delta_poc;
241 rps->used[rps->num_pics] = true;
242 if (delta_poc < 0)
243 rps->num_neg_pics++;
244 else
245 rps->num_pos_pics++;
246 rps->num_pics++;
247 }
248 }
249
250 rps->num_delta_poc = ref_rps->num_pics;
251
252 /* sort delta poc */
253 for (i = 1; i < rps->num_pics; ++i) {
254 delta_poc = rps->delta_poc[i];
255 used = rps->used[i];
256 for (j = i - 1; j >= 0; j--) {
257 if (delta_poc < rps->delta_poc[j]) {
258 rps->delta_poc[j + 1] = rps->delta_poc[j];
259 rps->used[j + 1] = rps->used[j];
260 rps->delta_poc[j] = delta_poc;
261 rps->used[j] = used;
262 }
263 }
264 }
265
266 for (i = 0 , j = rps->num_neg_pics - 1;
267 i < rps->num_neg_pics >> 1; i++, j--) {
268 delta_poc = rps->delta_poc[i];
269 used = rps->used[i];
270 rps->delta_poc[i] = rps->delta_poc[j];
271 rps->used[i] = rps->used[j];
272 rps->delta_poc[j] = delta_poc;
273 rps->used[j] = used;
274 }
275 } else {
276 /* num_negative_pics */
277 rps->num_neg_pics = vl_rbsp_ue(rbsp);
278 /* num_positive_pics */
279 rps->num_pos_pics = vl_rbsp_ue(rbsp);
280 rps->num_pics = rps->num_neg_pics + rps->num_pos_pics;
281
282 delta_poc = 0;
283 for(i = 0 ; i < rps->num_neg_pics; ++i) {
284 /* delta_poc_s0_minus1 */
285 delta_poc -= (vl_rbsp_ue(rbsp) + 1);
286 rps->delta_poc[i] = delta_poc;
287 /* used_by_curr_pic_s0_flag */
288 rps->used[i] = vl_rbsp_u(rbsp, 1);
289 }
290
291 delta_poc = 0;
292 for(i = rps->num_neg_pics; i < rps->num_pics; ++i) {
293 /* delta_poc_s1_minus1 */
294 delta_poc += (vl_rbsp_ue(rbsp) + 1);
295 rps->delta_poc[i] = delta_poc;
296 /* used_by_curr_pic_s1_flag */
297 rps->used[i] = vl_rbsp_u(rbsp, 1);
298 }
299 }
300 }
301
302 static struct pipe_h265_sps *seq_parameter_set_id(vid_dec_PrivateType *priv,
303 struct vl_rbsp *rbsp)
304 {
305 unsigned id = vl_rbsp_ue(rbsp);
306
307 if (id >= ARRAY_SIZE(priv->codec_data.h265.sps))
308 return NULL;
309
310 return &priv->codec_data.h265.sps[id];
311 }
312
313 static void seq_parameter_set(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp)
314 {
315 struct pipe_h265_sps *sps;
316 int sps_max_sub_layers_minus1;
317 unsigned i;
318
319 /* sps_video_parameter_set_id */
320 vl_rbsp_u(rbsp, 4);
321
322 /* sps_max_sub_layers_minus1 */
323 sps_max_sub_layers_minus1 = vl_rbsp_u(rbsp, 3);
324
325 assert(sps_max_sub_layers_minus1 <= 6);
326
327 /* sps_temporal_id_nesting_flag */
328 vl_rbsp_u(rbsp, 1);
329
330 priv->codec_data.h265.level_idc =
331 profile_tier_level(rbsp, sps_max_sub_layers_minus1);
332
333 sps = seq_parameter_set_id(priv, rbsp);
334 if (!sps)
335 return;
336
337 memset(sps, 0, sizeof(*sps));
338
339 sps->chroma_format_idc = vl_rbsp_ue(rbsp);
340
341 if (sps->chroma_format_idc == 3)
342 sps->separate_colour_plane_flag = vl_rbsp_u(rbsp, 1);
343
344 sps->pic_width_in_luma_samples = vl_rbsp_ue(rbsp);
345
346 sps->pic_height_in_luma_samples = vl_rbsp_ue(rbsp);
347
348 /* conformance_window_flag */
349 if (vl_rbsp_u(rbsp, 1)) {
350 /* conf_win_left_offset */
351 vl_rbsp_ue(rbsp);
352 /* conf_win_right_offset */
353 vl_rbsp_ue(rbsp);
354 /* conf_win_top_offset */
355 vl_rbsp_ue(rbsp);
356 /* conf_win_bottom_offset */
357 vl_rbsp_ue(rbsp);
358 }
359
360 sps->bit_depth_luma_minus8 = vl_rbsp_ue(rbsp);
361 sps->bit_depth_chroma_minus8 = vl_rbsp_ue(rbsp);
362 sps->log2_max_pic_order_cnt_lsb_minus4 = vl_rbsp_ue(rbsp);
363
364 /* sps_sub_layer_ordering_info_present_flag */
365 i = vl_rbsp_u(rbsp, 1) ? 0 : sps_max_sub_layers_minus1;
366 for (; i <= sps_max_sub_layers_minus1; ++i) {
367 sps->sps_max_dec_pic_buffering_minus1 = vl_rbsp_ue(rbsp);
368 /* sps_max_num_reorder_pics */
369 vl_rbsp_ue(rbsp);
370 /* sps_max_latency_increase_plus */
371 vl_rbsp_ue(rbsp);
372 }
373
374 sps->log2_min_luma_coding_block_size_minus3 = vl_rbsp_ue(rbsp);
375 sps->log2_diff_max_min_luma_coding_block_size = vl_rbsp_ue(rbsp);
376 sps->log2_min_transform_block_size_minus2 = vl_rbsp_ue(rbsp);
377 sps->log2_diff_max_min_transform_block_size = vl_rbsp_ue(rbsp);
378 sps->max_transform_hierarchy_depth_inter = vl_rbsp_ue(rbsp);
379 sps->max_transform_hierarchy_depth_intra = vl_rbsp_ue(rbsp);
380
381 sps->scaling_list_enabled_flag = vl_rbsp_u(rbsp, 1);
382 if (sps->scaling_list_enabled_flag)
383 /* sps_scaling_list_data_present_flag */
384 if (vl_rbsp_u(rbsp, 1))
385 scaling_list_data();
386
387 sps->amp_enabled_flag = vl_rbsp_u(rbsp, 1);
388 sps->sample_adaptive_offset_enabled_flag = vl_rbsp_u(rbsp, 1);
389 sps->pcm_enabled_flag = vl_rbsp_u(rbsp, 1);
390 if (sps->pcm_enabled_flag) {
391 sps->pcm_sample_bit_depth_luma_minus1 = vl_rbsp_u(rbsp, 4);
392 sps->pcm_sample_bit_depth_chroma_minus1 = vl_rbsp_u(rbsp, 4);
393 sps->log2_min_pcm_luma_coding_block_size_minus3 = vl_rbsp_ue(rbsp);
394 sps->log2_diff_max_min_pcm_luma_coding_block_size = vl_rbsp_ue(rbsp);
395 sps->pcm_loop_filter_disabled_flag = vl_rbsp_u(rbsp, 1);
396 }
397
398 sps->num_short_term_ref_pic_sets = vl_rbsp_ue(rbsp);
399
400 for (i = 0; i < sps->num_short_term_ref_pic_sets; ++i) {
401 struct ref_pic_set *rps;
402
403 rps = (struct ref_pic_set *)
404 priv->codec_data.h265.ref_pic_set_list + i;
405 st_ref_pic_set(priv, rbsp, rps, sps, i);
406 }
407
408 sps->long_term_ref_pics_present_flag = vl_rbsp_u(rbsp, 1);
409 if (sps->long_term_ref_pics_present_flag) {
410 sps->num_long_term_ref_pics_sps = vl_rbsp_ue(rbsp);
411 for (i = 0; i < sps->num_long_term_ref_pics_sps; ++i) {
412 /* lt_ref_pic_poc_lsb_sps */
413 vl_rbsp_u(rbsp, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
414 /* used_by_curr_pic_lt_sps_flag */
415 vl_rbsp_u(rbsp, 1);
416 }
417 }
418
419 sps->sps_temporal_mvp_enabled_flag = vl_rbsp_u(rbsp, 1);
420 sps->strong_intra_smoothing_enabled_flag = vl_rbsp_u(rbsp, 1);
421 }
422
423 static struct pipe_h265_pps *pic_parameter_set_id(vid_dec_PrivateType *priv,
424 struct vl_rbsp *rbsp)
425 {
426 unsigned id = vl_rbsp_ue(rbsp);
427
428 if (id >= ARRAY_SIZE(priv->codec_data.h265.pps))
429 return NULL;
430
431 return &priv->codec_data.h265.pps[id];
432 }
433
434 static void picture_parameter_set(vid_dec_PrivateType *priv,
435 struct vl_rbsp *rbsp)
436 {
437 struct pipe_h265_sps *sps;
438 struct pipe_h265_pps *pps;
439 int i;
440
441 pps = pic_parameter_set_id(priv, rbsp);
442 if (!pps)
443 return;
444
445 memset(pps, 0, sizeof(*pps));
446 sps = pps->sps = seq_parameter_set_id(priv, rbsp);
447 if (!sps)
448 return;
449
450 pps->dependent_slice_segments_enabled_flag = vl_rbsp_u(rbsp, 1);
451 pps->output_flag_present_flag = vl_rbsp_u(rbsp, 1);
452 pps->num_extra_slice_header_bits = vl_rbsp_u(rbsp, 3);
453 pps->sign_data_hiding_enabled_flag = vl_rbsp_u(rbsp, 1);
454 pps->cabac_init_present_flag = vl_rbsp_u(rbsp, 1);
455
456 pps->num_ref_idx_l0_default_active_minus1 = vl_rbsp_ue(rbsp);
457 pps->num_ref_idx_l1_default_active_minus1 = vl_rbsp_ue(rbsp);
458 pps->init_qp_minus26 = vl_rbsp_se(rbsp);
459 pps->constrained_intra_pred_flag = vl_rbsp_u(rbsp, 1);
460 pps->transform_skip_enabled_flag = vl_rbsp_u(rbsp, 1);
461
462 pps->cu_qp_delta_enabled_flag = vl_rbsp_u(rbsp, 1);
463 if (pps->cu_qp_delta_enabled_flag)
464 pps->diff_cu_qp_delta_depth = vl_rbsp_ue(rbsp);
465
466 pps->pps_cb_qp_offset = vl_rbsp_se(rbsp);
467 pps->pps_cr_qp_offset = vl_rbsp_se(rbsp);
468 pps->pps_slice_chroma_qp_offsets_present_flag = vl_rbsp_u(rbsp, 1);
469
470 pps->weighted_pred_flag = vl_rbsp_u(rbsp, 1);
471 pps->weighted_bipred_flag = vl_rbsp_u(rbsp, 1);
472
473 pps->transquant_bypass_enabled_flag = vl_rbsp_u(rbsp, 1);
474 pps->tiles_enabled_flag = vl_rbsp_u(rbsp, 1);
475 pps->entropy_coding_sync_enabled_flag = vl_rbsp_u(rbsp, 1);
476
477 if (pps->tiles_enabled_flag) {
478 pps->num_tile_columns_minus1 = vl_rbsp_ue(rbsp);
479 pps->num_tile_rows_minus1 = vl_rbsp_ue(rbsp);
480
481 pps->uniform_spacing_flag = vl_rbsp_u(rbsp, 1);
482 if (!pps->uniform_spacing_flag) {
483 for (i = 0; i < pps->num_tile_columns_minus1; ++i)
484 pps->column_width_minus1[i] = vl_rbsp_ue(rbsp);
485
486 for (i = 0; i < pps->num_tile_rows_minus1; ++i)
487 pps->row_height_minus1[i] = vl_rbsp_ue(rbsp);
488 }
489
490 if (!pps->num_tile_columns_minus1 || !pps->num_tile_rows_minus1)
491 pps->loop_filter_across_tiles_enabled_flag = vl_rbsp_u(rbsp, 1);
492 }
493
494 pps->pps_loop_filter_across_slices_enabled_flag = vl_rbsp_u(rbsp, 1);
495
496 pps->deblocking_filter_control_present_flag = vl_rbsp_u(rbsp, 1);
497 if (pps->deblocking_filter_control_present_flag) {
498 pps->deblocking_filter_override_enabled_flag = vl_rbsp_u(rbsp, 1);
499 pps->pps_deblocking_filter_disabled_flag = vl_rbsp_u(rbsp, 1);
500 if (!pps->pps_deblocking_filter_disabled_flag) {
501 pps->pps_beta_offset_div2 = vl_rbsp_se(rbsp);
502 pps->pps_tc_offset_div2 = vl_rbsp_se(rbsp);
503 }
504 }
505
506 /* pps_scaling_list_data_present_flag */
507 if (vl_rbsp_u(rbsp, 1))
508 scaling_list_data();
509
510 pps->lists_modification_present_flag = vl_rbsp_u(rbsp, 1);
511 pps->log2_parallel_merge_level_minus2 = vl_rbsp_ue(rbsp);
512 pps->slice_segment_header_extension_present_flag = vl_rbsp_u(rbsp, 1);
513 }
514
515 static void vid_dec_h265_BeginFrame(vid_dec_PrivateType *priv)
516 {
517 if (priv->frame_started)
518 return;
519
520 vid_dec_NeedTarget(priv);
521
522 if (!priv->codec) {
523 struct pipe_video_codec templat = {};
524 omx_base_video_PortType *port;
525
526 port = (omx_base_video_PortType *)
527 priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
528 templat.profile = priv->profile;
529 templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
530 templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
531 templat.expect_chunked_decode = true;
532 templat.width = align(port->sPortParam.format.video.nFrameWidth, 4);
533 templat.height = align(port->sPortParam.format.video.nFrameHeight, 4);
534 templat.level = priv->codec_data.h265.level_idc;
535 priv->codec = priv->pipe->create_video_codec(priv->pipe, &templat);
536 }
537 priv->codec->begin_frame(priv->codec, priv->target, &priv->picture.base);
538 priv->frame_started = true;
539 }
540
541 static struct pipe_video_buffer *vid_dec_h265_Flush(vid_dec_PrivateType *priv,
542 OMX_TICKS *timestamp)
543 {
544 struct dpb_list *entry, *result = NULL;
545 struct pipe_video_buffer *buf;
546
547 /* search for the lowest poc and break on zeros */
548 LIST_FOR_EACH_ENTRY(entry, &priv->codec_data.h265.dpb_list, list) {
549
550 if (result && entry->poc == 0)
551 break;
552
553 if (!result || entry->poc < result->poc)
554 result = entry;
555 }
556
557 if (!result)
558 return NULL;
559
560 buf = result->buffer;
561
562 --priv->codec_data.h265.dpb_num;
563 LIST_DEL(&result->list);
564 FREE(result);
565
566 return buf;
567 }
568
569 static void vid_dec_h265_EndFrame(vid_dec_PrivateType *priv)
570 {
571 struct dpb_list *entry = NULL;
572 struct pipe_video_buffer *tmp;
573
574 if (!priv->frame_started)
575 return;
576
577 priv->codec->end_frame(priv->codec, priv->target, &priv->picture.base);
578 priv->frame_started = false;
579
580 /* add the decoded picture to the dpb list */
581 entry = CALLOC_STRUCT(dpb_list);
582 if (!entry)
583 return;
584
585 entry->buffer = priv->target;
586 entry->poc = get_poc(priv);
587
588 LIST_ADDTAIL(&entry->list, &priv->codec_data.h265.dpb_list);
589 ++priv->codec_data.h265.dpb_num;
590 priv->target = NULL;
591
592 if (priv->codec_data.h265.dpb_num <= DPB_MAX_SIZE)
593 return;
594
595 tmp = priv->in_buffers[0]->pInputPortPrivate;
596 priv->in_buffers[0]->pInputPortPrivate = vid_dec_h265_Flush(priv, NULL);
597 priv->target = tmp;
598 priv->frame_finished = priv->in_buffers[0]->pInputPortPrivate != NULL;
599 if (priv->frame_finished &&
600 (priv->in_buffers[0]->nFlags & OMX_BUFFERFLAG_EOS))
601 FREE(priv->codec_data.h265.ref_pic_set_list);
602 }
603
604 static void slice_header(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp,
605 unsigned nal_unit_type)
606 {
607 struct pipe_h265_pps *pps;
608 struct pipe_h265_sps *sps;
609 bool first_slice_segment_in_pic_flag;
610 bool dependent_slice_segment_flag = false;
611 struct ref_pic_set *rps = NULL;
612 unsigned poc_lsb, poc_msb, slice_prev_poc;
613 unsigned max_poc_lsb, prev_poc_lsb, prev_poc_msb;
614 unsigned num_st_rps;
615 int i;
616
617 if (priv->picture.h265.IDRPicFlag != is_idr_picture(nal_unit_type))
618 vid_dec_h265_EndFrame(priv);
619
620 priv->picture.h265.IDRPicFlag = is_idr_picture(nal_unit_type);
621
622 first_slice_segment_in_pic_flag = vl_rbsp_u(rbsp, 1);
623
624 if (is_rap_picture(nal_unit_type))
625 /* no_output_of_prior_pics_flag */
626 vl_rbsp_u(rbsp, 1);
627
628 pps = pic_parameter_set_id(priv, rbsp);
629 if (!pps)
630 return;
631
632 sps = pps->sps;
633 if (!sps)
634 return;
635
636 if (pps != priv->picture.h265.pps)
637 vid_dec_h265_EndFrame(priv);
638
639 priv->picture.h265.pps = pps;
640
641 if (priv->picture.h265.RAPPicFlag != is_rap_picture(nal_unit_type))
642 vid_dec_h265_EndFrame(priv);
643 priv->picture.h265.RAPPicFlag = is_rap_picture(nal_unit_type);
644
645 num_st_rps = sps->num_short_term_ref_pic_sets;
646
647 if (priv->picture.h265.CurrRpsIdx != num_st_rps)
648 vid_dec_h265_EndFrame(priv);
649 priv->picture.h265.CurrRpsIdx = num_st_rps;
650
651 if (!first_slice_segment_in_pic_flag) {
652 int size, num;
653 int bits_slice_segment_address = 0;
654
655 if (pps->dependent_slice_segments_enabled_flag)
656 dependent_slice_segment_flag = vl_rbsp_u(rbsp, 1);
657
658 size = 1 << (sps->log2_min_luma_coding_block_size_minus3 + 3 +
659 sps->log2_diff_max_min_luma_coding_block_size);
660
661 num = ((sps->pic_width_in_luma_samples + size - 1) / size) *
662 ((sps->pic_height_in_luma_samples + size - 1) / size);
663
664 while (num > (1 << bits_slice_segment_address))
665 bits_slice_segment_address++;
666
667 /* slice_segment_address */
668 vl_rbsp_u(rbsp, bits_slice_segment_address);
669 }
670
671 if (dependent_slice_segment_flag)
672 return;
673
674 for (i = 0; i < pps->num_extra_slice_header_bits; ++i)
675 /* slice_reserved_flag */
676 vl_rbsp_u(rbsp, 1);
677
678 /* slice_type */
679 vl_rbsp_ue(rbsp);
680
681 if (pps->output_flag_present_flag)
682 /* pic output flag */
683 vl_rbsp_u(rbsp, 1);
684
685 if (sps->separate_colour_plane_flag)
686 /* colour_plane_id */
687 vl_rbsp_u(rbsp, 2);
688
689 if (is_idr_picture(nal_unit_type)) {
690 set_poc(priv, nal_unit_type, 0);
691 return;
692 }
693
694 /* slice_pic_order_cnt_lsb */
695 poc_lsb =
696 vl_rbsp_u(rbsp, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
697
698 slice_prev_poc = (int)priv->codec_data.h265.slice_prev_poc;
699 max_poc_lsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
700
701 prev_poc_lsb = slice_prev_poc & (max_poc_lsb - 1);
702 prev_poc_msb = slice_prev_poc - prev_poc_lsb;
703
704 if ((poc_lsb < prev_poc_lsb) &&
705 ((prev_poc_lsb - poc_lsb ) >= (max_poc_lsb / 2)))
706 poc_msb = prev_poc_msb + max_poc_lsb;
707
708 else if ((poc_lsb > prev_poc_lsb ) &&
709 ((poc_lsb - prev_poc_lsb) > (max_poc_lsb / 2)))
710 poc_msb = prev_poc_msb - max_poc_lsb;
711
712 else
713 poc_msb = prev_poc_msb;
714
715 if (is_bla_picture(nal_unit_type))
716 poc_msb = 0;
717
718 if (get_poc(priv) != poc_msb + poc_lsb)
719 vid_dec_h265_EndFrame(priv);
720
721 set_poc(priv, nal_unit_type, (poc_msb + poc_lsb));
722
723 /* short_term_ref_pic_set_sps_flag */
724 if (!vl_rbsp_u(rbsp, 1)) {
725 rps = (struct ref_pic_set *)
726 priv->codec_data.h265.ref_pic_set_list + num_st_rps;
727 st_ref_pic_set(priv, rbsp, rps, sps, num_st_rps);
728
729 } else if (num_st_rps > 1) {
730 int num_bits = 0;
731 unsigned idx;
732
733 while ((1 << num_bits) < num_st_rps)
734 num_bits++;
735
736 if (num_bits > 0)
737 /* short_term_ref_pic_set_idx */
738 idx = vl_rbsp_u(rbsp, num_bits);
739 else
740 idx = 0;
741
742 rps = (struct ref_pic_set *)
743 priv->codec_data.h265.ref_pic_set_list + idx;
744 }
745
746 if (is_bla_picture(nal_unit_type)) {
747 rps->num_neg_pics = 0;
748 rps->num_pos_pics = 0;
749 rps->num_pics = 0;
750 }
751
752 priv->codec_data.h265.rps = rps;
753
754 return;
755 }
756
757 static void vid_dec_h265_Decode(vid_dec_PrivateType *priv,
758 struct vl_vlc *vlc,
759 unsigned min_bits_left)
760 {
761 unsigned nal_unit_type;
762 unsigned nuh_layer_id;
763 unsigned nuh_temporal_id_plus1;
764
765 if (!vl_vlc_search_byte(vlc, vl_vlc_bits_left(vlc) - min_bits_left, 0x00))
766 return;
767
768 if (vl_vlc_peekbits(vlc, 24) != 0x000001) {
769 vl_vlc_eatbits(vlc, 8);
770 return;
771 }
772
773 if (priv->slice) {
774 unsigned bytes = priv->bytes_left - (vl_vlc_bits_left(vlc) / 8);
775
776 priv->codec->decode_bitstream(priv->codec, priv->target,
777 &priv->picture.base, 1,
778 &priv->slice, &bytes);
779 priv->slice = NULL;
780 }
781
782 vl_vlc_eatbits(vlc, 24);
783
784 /* forbidden_zero_bit */
785 vl_vlc_eatbits(vlc, 1);
786
787 if (vl_vlc_valid_bits(vlc) < 15)
788 vl_vlc_fillbits(vlc);
789
790 nal_unit_type = vl_vlc_get_uimsbf(vlc, 6);
791
792 /* nuh_layer_id */
793 nuh_layer_id = vl_vlc_get_uimsbf(vlc, 6);
794
795 /* nuh_temporal_id_plus1 */
796 nuh_temporal_id_plus1 = vl_vlc_get_uimsbf(vlc, 3);
797 priv->codec_data.h265.temporal_id = nuh_temporal_id_plus1 - 1;
798
799 if (!is_slice_picture(nal_unit_type))
800 vid_dec_h265_EndFrame(priv);
801
802 if (nal_unit_type == NAL_UNIT_TYPE_SPS) {
803 struct vl_rbsp rbsp;
804
805 vl_rbsp_init(&rbsp, vlc, ~0);
806 seq_parameter_set(priv, &rbsp);
807
808 } else if (nal_unit_type == NAL_UNIT_TYPE_PPS) {
809 struct vl_rbsp rbsp;
810
811 vl_rbsp_init(&rbsp, vlc, ~0);
812 picture_parameter_set(priv, &rbsp);
813
814 } else if (is_slice_picture(nal_unit_type)) {
815 unsigned bits = vl_vlc_valid_bits(vlc);
816 unsigned bytes = bits / 8 + 5;
817 struct vl_rbsp rbsp;
818 uint8_t buf[9];
819 const void *ptr = buf;
820 unsigned i;
821
822 buf[0] = 0x0;
823 buf[1] = 0x0;
824 buf[2] = 0x1;
825 buf[3] = nal_unit_type << 1 | nuh_layer_id >> 5;
826 buf[4] = nuh_layer_id << 3 | nuh_temporal_id_plus1;
827 for (i = 5; i < bytes; ++i)
828 buf[i] = vl_vlc_peekbits(vlc, bits) >> ((bytes - i - 1) * 8);
829
830 priv->bytes_left = (vl_vlc_bits_left(vlc) - bits) / 8;
831 priv->slice = vlc->data;
832
833 vl_rbsp_init(&rbsp, vlc, 128);
834 slice_header(priv, &rbsp, nal_unit_type);
835
836 vid_dec_h265_BeginFrame(priv);
837
838 priv->codec->decode_bitstream(priv->codec, priv->target,
839 &priv->picture.base, 1,
840 &ptr, &bytes);
841 }
842
843 /* resync to byte boundary */
844 vl_vlc_eatbits(vlc, vl_vlc_valid_bits(vlc) % 8);
845 }
846
847 void vid_dec_h265_Init(vid_dec_PrivateType *priv)
848 {
849 priv->picture.base.profile = PIPE_VIDEO_PROFILE_HEVC_MAIN;
850
851 LIST_INITHEAD(&priv->codec_data.h265.dpb_list);
852 priv->codec_data.h265.ref_pic_set_list = (struct ref_pic_set *)
853 CALLOC(MAX_NUM_REF_PICS, sizeof(struct ref_pic_set));
854
855 priv->Decode = vid_dec_h265_Decode;
856 priv->EndFrame = vid_dec_h265_EndFrame;
857 priv->Flush = vid_dec_h265_Flush;
858 }