st/omx/dec/h265: decoder size should follow from sps
[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 32
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 OMX_TICKS timestamp;
64 unsigned poc;
65 };
66
67 struct ref_pic_set {
68 unsigned num_pics;
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];
74 };
75
76 static bool is_idr_picture(unsigned nal_unit_type)
77 {
78 return (nal_unit_type == NAL_UNIT_TYPE_IDR_W_RADL ||
79 nal_unit_type == NAL_UNIT_TYPE_IDR_N_LP);
80 }
81
82 /* broken link access picture */
83 static bool is_bla_picture(unsigned nal_unit_type)
84 {
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);
88 }
89
90 /* random access point picture */
91 static bool is_rap_picture(unsigned nal_unit_type)
92 {
93 return (nal_unit_type >= NAL_UNIT_TYPE_BLA_W_LP &&
94 nal_unit_type <= NAL_UNIT_TYPE_CRA);
95 }
96
97 static bool is_slice_picture(unsigned nal_unit_type)
98 {
99 return (nal_unit_type <= NAL_UNIT_TYPE_RASL_R ||
100 is_rap_picture(nal_unit_type));
101 }
102
103 static void set_poc(vid_dec_PrivateType *priv,
104 unsigned nal_unit_type, int i)
105 {
106 priv->picture.h265.CurrPicOrderCntVal = i;
107
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;
114 }
115
116 static unsigned get_poc(vid_dec_PrivateType *priv)
117 {
118 return priv->picture.h265.CurrPicOrderCntVal;
119 }
120
121 static void profile_tier(struct vl_rbsp *rbsp)
122 {
123 int i;
124
125 /* general_profile_space */
126 vl_rbsp_u(rbsp, 2);
127
128 /* general_tier_flag */
129 vl_rbsp_u(rbsp, 1);
130
131 /* general_profile_idc */
132 vl_rbsp_u(rbsp, 5);
133
134 /* general_profile_compatibility_flag */
135 for(i = 0; i < 32; ++i)
136 vl_rbsp_u(rbsp, 1);
137
138 /* general_progressive_source_flag */
139 vl_rbsp_u(rbsp, 1);
140
141 /* general_interlaced_source_flag */
142 vl_rbsp_u(rbsp, 1);
143
144 /* general_non_packed_constraint_flag */
145 vl_rbsp_u(rbsp, 1);
146
147 /* general_frame_only_constraint_flag */
148 vl_rbsp_u(rbsp, 1);
149
150 /* general_reserved_zero_44bits */
151 vl_rbsp_u(rbsp, 16);
152 vl_rbsp_u(rbsp, 16);
153 vl_rbsp_u(rbsp, 12);
154 }
155
156 static unsigned profile_tier_level(struct vl_rbsp *rbsp,
157 int max_sublayers_minus1)
158 {
159 bool sub_layer_profile_present_flag[6];
160 bool sub_layer_level_present_flag[6];
161 unsigned level_idc;
162 int i;
163
164 profile_tier(rbsp);
165
166 /* general_level_idc */
167 level_idc = vl_rbsp_u(rbsp, 8);
168
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);
172 }
173
174 if (max_sublayers_minus1 > 0)
175 for (i = max_sublayers_minus1; i < 8; ++i)
176 /* reserved_zero_2bits */
177 vl_rbsp_u(rbsp, 2);
178
179 for (i = 0; i < max_sublayers_minus1; ++i) {
180 if (sub_layer_profile_present_flag[i])
181 profile_tier(rbsp);
182
183 if (sub_layer_level_present_flag[i])
184 /* sub_layer_level_idc */
185 vl_rbsp_u(rbsp, 8);
186 }
187
188 return level_idc;
189 }
190
191 static void scaling_list_data(void)
192 {
193 /* TODO */
194 assert(0);
195 }
196
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,
199 unsigned idx)
200 {
201 bool inter_rps_pred_flag;
202 unsigned delta_idx_minus1;
203 int delta_poc;
204 int i;
205
206 inter_rps_pred_flag = (idx != 0) ? (vl_rbsp_u(rbsp, 1)) : false;
207
208 if (inter_rps_pred_flag) {
209 struct ref_pic_set *ref_rps;
210 unsigned sign, abs;
211 int delta_rps;
212 bool used;
213 int j;
214
215 if (idx == sps->num_short_term_ref_pic_sets)
216 delta_idx_minus1 = vl_rbsp_ue(rbsp);
217 else
218 delta_idx_minus1 = 0;
219
220 ref_rps = (struct ref_pic_set *)
221 priv->codec_data.h265.ref_pic_set_list + idx - (delta_idx_minus1 + 1);
222
223 /* delta_rps_sign */
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);
228
229 rps->num_neg_pics = 0;
230 rps->num_pos_pics = 0;
231 rps->num_pics = 0;
232
233 for(i = 0 ; i <= ref_rps->num_pics; ++i) {
234 /* used_by_curr_pic_flag */
235 if (!vl_rbsp_u(rbsp, 1))
236 /* use_delta_flag */
237 vl_rbsp_u(rbsp, 1);
238 else {
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;
243 if (delta_poc < 0)
244 rps->num_neg_pics++;
245 else
246 rps->num_pos_pics++;
247 rps->num_pics++;
248 }
249 }
250
251 rps->num_delta_poc = ref_rps->num_pics;
252
253 /* sort delta poc */
254 for (i = 1; i < rps->num_pics; ++i) {
255 delta_poc = rps->delta_poc[i];
256 used = rps->used[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;
262 rps->used[j] = used;
263 }
264 }
265 }
266
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];
270 used = rps->used[i];
271 rps->delta_poc[i] = rps->delta_poc[j];
272 rps->used[i] = rps->used[j];
273 rps->delta_poc[j] = delta_poc;
274 rps->used[j] = used;
275 }
276 } else {
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;
282
283 delta_poc = 0;
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);
290 }
291
292 delta_poc = 0;
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);
299 }
300 }
301 }
302
303 static struct pipe_h265_sps *seq_parameter_set_id(vid_dec_PrivateType *priv,
304 struct vl_rbsp *rbsp)
305 {
306 unsigned id = vl_rbsp_ue(rbsp);
307
308 if (id >= ARRAY_SIZE(priv->codec_data.h265.sps))
309 return NULL;
310
311 return &priv->codec_data.h265.sps[id];
312 }
313
314 static void seq_parameter_set(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp)
315 {
316 struct pipe_h265_sps *sps;
317 int sps_max_sub_layers_minus1;
318 unsigned i;
319
320 /* sps_video_parameter_set_id */
321 vl_rbsp_u(rbsp, 4);
322
323 /* sps_max_sub_layers_minus1 */
324 sps_max_sub_layers_minus1 = vl_rbsp_u(rbsp, 3);
325
326 assert(sps_max_sub_layers_minus1 <= 6);
327
328 /* sps_temporal_id_nesting_flag */
329 vl_rbsp_u(rbsp, 1);
330
331 priv->codec_data.h265.level_idc =
332 profile_tier_level(rbsp, sps_max_sub_layers_minus1);
333
334 sps = seq_parameter_set_id(priv, rbsp);
335 if (!sps)
336 return;
337
338 memset(sps, 0, sizeof(*sps));
339
340 sps->chroma_format_idc = vl_rbsp_ue(rbsp);
341
342 if (sps->chroma_format_idc == 3)
343 sps->separate_colour_plane_flag = vl_rbsp_u(rbsp, 1);
344
345 priv->codec_data.h265.pic_width_in_luma_samples =
346 sps->pic_width_in_luma_samples = vl_rbsp_ue(rbsp);
347
348 priv->codec_data.h265.pic_height_in_luma_samples =
349 sps->pic_height_in_luma_samples = vl_rbsp_ue(rbsp);
350
351 /* conformance_window_flag */
352 if (vl_rbsp_u(rbsp, 1)) {
353 /* conf_win_left_offset */
354 vl_rbsp_ue(rbsp);
355 /* conf_win_right_offset */
356 vl_rbsp_ue(rbsp);
357 /* conf_win_top_offset */
358 vl_rbsp_ue(rbsp);
359 /* conf_win_bottom_offset */
360 vl_rbsp_ue(rbsp);
361 }
362
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);
366
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 */
372 vl_rbsp_ue(rbsp);
373 /* sps_max_latency_increase_plus */
374 vl_rbsp_ue(rbsp);
375 }
376
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);
383
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))
388 scaling_list_data();
389
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);
399 }
400
401 sps->num_short_term_ref_pic_sets = vl_rbsp_ue(rbsp);
402
403 for (i = 0; i < sps->num_short_term_ref_pic_sets; ++i) {
404 struct ref_pic_set *rps;
405
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);
409 }
410
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 */
418 vl_rbsp_u(rbsp, 1);
419 }
420 }
421
422 sps->sps_temporal_mvp_enabled_flag = vl_rbsp_u(rbsp, 1);
423 sps->strong_intra_smoothing_enabled_flag = vl_rbsp_u(rbsp, 1);
424 }
425
426 static struct pipe_h265_pps *pic_parameter_set_id(vid_dec_PrivateType *priv,
427 struct vl_rbsp *rbsp)
428 {
429 unsigned id = vl_rbsp_ue(rbsp);
430
431 if (id >= ARRAY_SIZE(priv->codec_data.h265.pps))
432 return NULL;
433
434 return &priv->codec_data.h265.pps[id];
435 }
436
437 static void picture_parameter_set(vid_dec_PrivateType *priv,
438 struct vl_rbsp *rbsp)
439 {
440 struct pipe_h265_sps *sps;
441 struct pipe_h265_pps *pps;
442 int i;
443
444 pps = pic_parameter_set_id(priv, rbsp);
445 if (!pps)
446 return;
447
448 memset(pps, 0, sizeof(*pps));
449 sps = pps->sps = seq_parameter_set_id(priv, rbsp);
450 if (!sps)
451 return;
452
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);
458
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);
464
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);
468
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);
472
473 pps->weighted_pred_flag = vl_rbsp_u(rbsp, 1);
474 pps->weighted_bipred_flag = vl_rbsp_u(rbsp, 1);
475
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);
479
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);
483
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);
488
489 for (i = 0; i < pps->num_tile_rows_minus1; ++i)
490 pps->row_height_minus1[i] = vl_rbsp_ue(rbsp);
491 }
492
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);
495 }
496
497 pps->pps_loop_filter_across_slices_enabled_flag = vl_rbsp_u(rbsp, 1);
498
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);
506 }
507 }
508
509 /* pps_scaling_list_data_present_flag */
510 if (vl_rbsp_u(rbsp, 1))
511 scaling_list_data();
512
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);
516 }
517
518 static void vid_dec_h265_BeginFrame(vid_dec_PrivateType *priv)
519 {
520 if (priv->frame_started)
521 return;
522
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;
527
528 if (!priv->codec) {
529 struct pipe_video_codec templat = {};
530
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);
539 }
540 priv->codec->begin_frame(priv->codec, priv->target, &priv->picture.base);
541 priv->frame_started = true;
542 }
543
544 static struct pipe_video_buffer *vid_dec_h265_Flush(vid_dec_PrivateType *priv,
545 OMX_TICKS *timestamp)
546 {
547 struct dpb_list *entry, *result = NULL;
548 struct pipe_video_buffer *buf;
549
550 /* search for the lowest poc and break on zeros */
551 LIST_FOR_EACH_ENTRY(entry, &priv->codec_data.h265.dpb_list, list) {
552
553 if (result && entry->poc == 0)
554 break;
555
556 if (!result || entry->poc < result->poc)
557 result = entry;
558 }
559
560 if (!result)
561 return NULL;
562
563 buf = result->buffer;
564 if (timestamp)
565 *timestamp = result->timestamp;
566
567 --priv->codec_data.h265.dpb_num;
568 LIST_DEL(&result->list);
569 FREE(result);
570
571 return buf;
572 }
573
574 static void vid_dec_h265_EndFrame(vid_dec_PrivateType *priv)
575 {
576 struct dpb_list *entry = NULL;
577 struct pipe_video_buffer *tmp;
578 struct ref_pic_set *rps;
579 int i;
580 OMX_TICKS timestamp;
581
582 if (!priv->frame_started)
583 return;
584
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;
592 }
593
594 rps = priv->codec_data.h265.rps;
595
596 if (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);
601
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;
605 break;
606 }
607 }
608
609 if (rps->used[i]) {
610 if (i < rps->num_neg_pics) {
611 priv->picture.h265.NumPocStCurrBefore++;
612 priv->picture.h265.RefPicSetStCurrBefore[i] = i;
613 } else {
614 int j = i - rps->num_neg_pics;
615 priv->picture.h265.NumPocStCurrAfter++;
616 priv->picture.h265.RefPicSetStCurrAfter[j] = i;
617 }
618 }
619 }
620 }
621
622 priv->codec->end_frame(priv->codec, priv->target, &priv->picture.base);
623 priv->frame_started = false;
624
625 /* add the decoded picture to the dpb list */
626 entry = CALLOC_STRUCT(dpb_list);
627 if (!entry)
628 return;
629
630 priv->first_buf_in_frame = true;
631 entry->buffer = priv->target;
632 entry->timestamp = priv->timestamp;
633 entry->poc = get_poc(priv);
634
635 LIST_ADDTAIL(&entry->list, &priv->codec_data.h265.dpb_list);
636 ++priv->codec_data.h265.dpb_num;
637 priv->target = NULL;
638
639 if (priv->codec_data.h265.dpb_num <= DPB_MAX_SIZE)
640 return;
641
642 tmp = priv->in_buffers[0]->pInputPortPrivate;
643 priv->in_buffers[0]->pInputPortPrivate = vid_dec_h265_Flush(priv, &timestamp);
644 priv->in_buffers[0]->nTimeStamp = timestamp;
645 priv->target = tmp;
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);
650 }
651
652 static void slice_header(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp,
653 unsigned nal_unit_type)
654 {
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;
662 unsigned num_st_rps;
663 int i;
664
665 if (priv->picture.h265.IDRPicFlag != is_idr_picture(nal_unit_type))
666 vid_dec_h265_EndFrame(priv);
667
668 priv->picture.h265.IDRPicFlag = is_idr_picture(nal_unit_type);
669
670 first_slice_segment_in_pic_flag = vl_rbsp_u(rbsp, 1);
671
672 if (is_rap_picture(nal_unit_type))
673 /* no_output_of_prior_pics_flag */
674 vl_rbsp_u(rbsp, 1);
675
676 pps = pic_parameter_set_id(priv, rbsp);
677 if (!pps)
678 return;
679
680 sps = pps->sps;
681 if (!sps)
682 return;
683
684 if (pps != priv->picture.h265.pps)
685 vid_dec_h265_EndFrame(priv);
686
687 priv->picture.h265.pps = pps;
688
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);
692
693 num_st_rps = sps->num_short_term_ref_pic_sets;
694
695 if (priv->picture.h265.CurrRpsIdx != num_st_rps)
696 vid_dec_h265_EndFrame(priv);
697 priv->picture.h265.CurrRpsIdx = num_st_rps;
698
699 if (!first_slice_segment_in_pic_flag) {
700 int size, num;
701 int bits_slice_segment_address = 0;
702
703 if (pps->dependent_slice_segments_enabled_flag)
704 dependent_slice_segment_flag = vl_rbsp_u(rbsp, 1);
705
706 size = 1 << (sps->log2_min_luma_coding_block_size_minus3 + 3 +
707 sps->log2_diff_max_min_luma_coding_block_size);
708
709 num = ((sps->pic_width_in_luma_samples + size - 1) / size) *
710 ((sps->pic_height_in_luma_samples + size - 1) / size);
711
712 while (num > (1 << bits_slice_segment_address))
713 bits_slice_segment_address++;
714
715 /* slice_segment_address */
716 vl_rbsp_u(rbsp, bits_slice_segment_address);
717 }
718
719 if (dependent_slice_segment_flag)
720 return;
721
722 for (i = 0; i < pps->num_extra_slice_header_bits; ++i)
723 /* slice_reserved_flag */
724 vl_rbsp_u(rbsp, 1);
725
726 /* slice_type */
727 vl_rbsp_ue(rbsp);
728
729 if (pps->output_flag_present_flag)
730 /* pic output flag */
731 vl_rbsp_u(rbsp, 1);
732
733 if (sps->separate_colour_plane_flag)
734 /* colour_plane_id */
735 vl_rbsp_u(rbsp, 2);
736
737 if (is_idr_picture(nal_unit_type)) {
738 set_poc(priv, nal_unit_type, 0);
739 return;
740 }
741
742 /* slice_pic_order_cnt_lsb */
743 poc_lsb =
744 vl_rbsp_u(rbsp, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
745
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);
748
749 prev_poc_lsb = slice_prev_poc & (max_poc_lsb - 1);
750 prev_poc_msb = slice_prev_poc - prev_poc_lsb;
751
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;
755
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;
759
760 else
761 poc_msb = prev_poc_msb;
762
763 if (is_bla_picture(nal_unit_type))
764 poc_msb = 0;
765
766 if (get_poc(priv) != poc_msb + poc_lsb)
767 vid_dec_h265_EndFrame(priv);
768
769 set_poc(priv, nal_unit_type, (poc_msb + poc_lsb));
770
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);
776
777 } else if (num_st_rps > 1) {
778 int num_bits = 0;
779 unsigned idx;
780
781 while ((1 << num_bits) < num_st_rps)
782 num_bits++;
783
784 if (num_bits > 0)
785 /* short_term_ref_pic_set_idx */
786 idx = vl_rbsp_u(rbsp, num_bits);
787 else
788 idx = 0;
789
790 rps = (struct ref_pic_set *)
791 priv->codec_data.h265.ref_pic_set_list + idx;
792 }
793
794 if (is_bla_picture(nal_unit_type)) {
795 rps->num_neg_pics = 0;
796 rps->num_pos_pics = 0;
797 rps->num_pics = 0;
798 }
799
800 priv->codec_data.h265.rps = rps;
801
802 return;
803 }
804
805 static void vid_dec_h265_Decode(vid_dec_PrivateType *priv,
806 struct vl_vlc *vlc,
807 unsigned min_bits_left)
808 {
809 unsigned nal_unit_type;
810 unsigned nuh_layer_id;
811 unsigned nuh_temporal_id_plus1;
812
813 if (!vl_vlc_search_byte(vlc, vl_vlc_bits_left(vlc) - min_bits_left, 0x00))
814 return;
815
816 if (vl_vlc_peekbits(vlc, 24) != 0x000001) {
817 vl_vlc_eatbits(vlc, 8);
818 return;
819 }
820
821 if (priv->slice) {
822 unsigned bytes = priv->bytes_left - (vl_vlc_bits_left(vlc) / 8);
823
824 priv->codec->decode_bitstream(priv->codec, priv->target,
825 &priv->picture.base, 1,
826 &priv->slice, &bytes);
827 priv->slice = NULL;
828 }
829
830 vl_vlc_eatbits(vlc, 24);
831
832 /* forbidden_zero_bit */
833 vl_vlc_eatbits(vlc, 1);
834
835 if (vl_vlc_valid_bits(vlc) < 15)
836 vl_vlc_fillbits(vlc);
837
838 nal_unit_type = vl_vlc_get_uimsbf(vlc, 6);
839
840 /* nuh_layer_id */
841 nuh_layer_id = vl_vlc_get_uimsbf(vlc, 6);
842
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;
846
847 if (!is_slice_picture(nal_unit_type))
848 vid_dec_h265_EndFrame(priv);
849
850 if (nal_unit_type == NAL_UNIT_TYPE_SPS) {
851 struct vl_rbsp rbsp;
852
853 vl_rbsp_init(&rbsp, vlc, ~0);
854 seq_parameter_set(priv, &rbsp);
855
856 } else if (nal_unit_type == NAL_UNIT_TYPE_PPS) {
857 struct vl_rbsp rbsp;
858
859 vl_rbsp_init(&rbsp, vlc, ~0);
860 picture_parameter_set(priv, &rbsp);
861
862 } else if (is_slice_picture(nal_unit_type)) {
863 unsigned bits = vl_vlc_valid_bits(vlc);
864 unsigned bytes = bits / 8 + 5;
865 struct vl_rbsp rbsp;
866 uint8_t buf[9];
867 const void *ptr = buf;
868 unsigned i;
869
870 buf[0] = 0x0;
871 buf[1] = 0x0;
872 buf[2] = 0x1;
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);
877
878 priv->bytes_left = (vl_vlc_bits_left(vlc) - bits) / 8;
879 priv->slice = vlc->data;
880
881 vl_rbsp_init(&rbsp, vlc, 128);
882 slice_header(priv, &rbsp, nal_unit_type);
883
884 vid_dec_h265_BeginFrame(priv);
885
886 priv->codec->decode_bitstream(priv->codec, priv->target,
887 &priv->picture.base, 1,
888 &ptr, &bytes);
889 }
890
891 /* resync to byte boundary */
892 vl_vlc_eatbits(vlc, vl_vlc_valid_bits(vlc) % 8);
893 }
894
895 void vid_dec_h265_Init(vid_dec_PrivateType *priv)
896 {
897 priv->picture.base.profile = PIPE_VIDEO_PROFILE_HEVC_MAIN;
898
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));
902
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;
907 }