st/omx/dec/h265: increase dpb max size to 32
[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 sps->pic_width_in_luma_samples = vl_rbsp_ue(rbsp);
346
347 sps->pic_height_in_luma_samples = vl_rbsp_ue(rbsp);
348
349 /* conformance_window_flag */
350 if (vl_rbsp_u(rbsp, 1)) {
351 /* conf_win_left_offset */
352 vl_rbsp_ue(rbsp);
353 /* conf_win_right_offset */
354 vl_rbsp_ue(rbsp);
355 /* conf_win_top_offset */
356 vl_rbsp_ue(rbsp);
357 /* conf_win_bottom_offset */
358 vl_rbsp_ue(rbsp);
359 }
360
361 sps->bit_depth_luma_minus8 = vl_rbsp_ue(rbsp);
362 sps->bit_depth_chroma_minus8 = vl_rbsp_ue(rbsp);
363 sps->log2_max_pic_order_cnt_lsb_minus4 = vl_rbsp_ue(rbsp);
364
365 /* sps_sub_layer_ordering_info_present_flag */
366 i = vl_rbsp_u(rbsp, 1) ? 0 : sps_max_sub_layers_minus1;
367 for (; i <= sps_max_sub_layers_minus1; ++i) {
368 sps->sps_max_dec_pic_buffering_minus1 = vl_rbsp_ue(rbsp);
369 /* sps_max_num_reorder_pics */
370 vl_rbsp_ue(rbsp);
371 /* sps_max_latency_increase_plus */
372 vl_rbsp_ue(rbsp);
373 }
374
375 sps->log2_min_luma_coding_block_size_minus3 = vl_rbsp_ue(rbsp);
376 sps->log2_diff_max_min_luma_coding_block_size = vl_rbsp_ue(rbsp);
377 sps->log2_min_transform_block_size_minus2 = vl_rbsp_ue(rbsp);
378 sps->log2_diff_max_min_transform_block_size = vl_rbsp_ue(rbsp);
379 sps->max_transform_hierarchy_depth_inter = vl_rbsp_ue(rbsp);
380 sps->max_transform_hierarchy_depth_intra = vl_rbsp_ue(rbsp);
381
382 sps->scaling_list_enabled_flag = vl_rbsp_u(rbsp, 1);
383 if (sps->scaling_list_enabled_flag)
384 /* sps_scaling_list_data_present_flag */
385 if (vl_rbsp_u(rbsp, 1))
386 scaling_list_data();
387
388 sps->amp_enabled_flag = vl_rbsp_u(rbsp, 1);
389 sps->sample_adaptive_offset_enabled_flag = vl_rbsp_u(rbsp, 1);
390 sps->pcm_enabled_flag = vl_rbsp_u(rbsp, 1);
391 if (sps->pcm_enabled_flag) {
392 sps->pcm_sample_bit_depth_luma_minus1 = vl_rbsp_u(rbsp, 4);
393 sps->pcm_sample_bit_depth_chroma_minus1 = vl_rbsp_u(rbsp, 4);
394 sps->log2_min_pcm_luma_coding_block_size_minus3 = vl_rbsp_ue(rbsp);
395 sps->log2_diff_max_min_pcm_luma_coding_block_size = vl_rbsp_ue(rbsp);
396 sps->pcm_loop_filter_disabled_flag = vl_rbsp_u(rbsp, 1);
397 }
398
399 sps->num_short_term_ref_pic_sets = vl_rbsp_ue(rbsp);
400
401 for (i = 0; i < sps->num_short_term_ref_pic_sets; ++i) {
402 struct ref_pic_set *rps;
403
404 rps = (struct ref_pic_set *)
405 priv->codec_data.h265.ref_pic_set_list + i;
406 st_ref_pic_set(priv, rbsp, rps, sps, i);
407 }
408
409 sps->long_term_ref_pics_present_flag = vl_rbsp_u(rbsp, 1);
410 if (sps->long_term_ref_pics_present_flag) {
411 sps->num_long_term_ref_pics_sps = vl_rbsp_ue(rbsp);
412 for (i = 0; i < sps->num_long_term_ref_pics_sps; ++i) {
413 /* lt_ref_pic_poc_lsb_sps */
414 vl_rbsp_u(rbsp, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
415 /* used_by_curr_pic_lt_sps_flag */
416 vl_rbsp_u(rbsp, 1);
417 }
418 }
419
420 sps->sps_temporal_mvp_enabled_flag = vl_rbsp_u(rbsp, 1);
421 sps->strong_intra_smoothing_enabled_flag = vl_rbsp_u(rbsp, 1);
422 }
423
424 static struct pipe_h265_pps *pic_parameter_set_id(vid_dec_PrivateType *priv,
425 struct vl_rbsp *rbsp)
426 {
427 unsigned id = vl_rbsp_ue(rbsp);
428
429 if (id >= ARRAY_SIZE(priv->codec_data.h265.pps))
430 return NULL;
431
432 return &priv->codec_data.h265.pps[id];
433 }
434
435 static void picture_parameter_set(vid_dec_PrivateType *priv,
436 struct vl_rbsp *rbsp)
437 {
438 struct pipe_h265_sps *sps;
439 struct pipe_h265_pps *pps;
440 int i;
441
442 pps = pic_parameter_set_id(priv, rbsp);
443 if (!pps)
444 return;
445
446 memset(pps, 0, sizeof(*pps));
447 sps = pps->sps = seq_parameter_set_id(priv, rbsp);
448 if (!sps)
449 return;
450
451 pps->dependent_slice_segments_enabled_flag = vl_rbsp_u(rbsp, 1);
452 pps->output_flag_present_flag = vl_rbsp_u(rbsp, 1);
453 pps->num_extra_slice_header_bits = vl_rbsp_u(rbsp, 3);
454 pps->sign_data_hiding_enabled_flag = vl_rbsp_u(rbsp, 1);
455 pps->cabac_init_present_flag = vl_rbsp_u(rbsp, 1);
456
457 pps->num_ref_idx_l0_default_active_minus1 = vl_rbsp_ue(rbsp);
458 pps->num_ref_idx_l1_default_active_minus1 = vl_rbsp_ue(rbsp);
459 pps->init_qp_minus26 = vl_rbsp_se(rbsp);
460 pps->constrained_intra_pred_flag = vl_rbsp_u(rbsp, 1);
461 pps->transform_skip_enabled_flag = vl_rbsp_u(rbsp, 1);
462
463 pps->cu_qp_delta_enabled_flag = vl_rbsp_u(rbsp, 1);
464 if (pps->cu_qp_delta_enabled_flag)
465 pps->diff_cu_qp_delta_depth = vl_rbsp_ue(rbsp);
466
467 pps->pps_cb_qp_offset = vl_rbsp_se(rbsp);
468 pps->pps_cr_qp_offset = vl_rbsp_se(rbsp);
469 pps->pps_slice_chroma_qp_offsets_present_flag = vl_rbsp_u(rbsp, 1);
470
471 pps->weighted_pred_flag = vl_rbsp_u(rbsp, 1);
472 pps->weighted_bipred_flag = vl_rbsp_u(rbsp, 1);
473
474 pps->transquant_bypass_enabled_flag = vl_rbsp_u(rbsp, 1);
475 pps->tiles_enabled_flag = vl_rbsp_u(rbsp, 1);
476 pps->entropy_coding_sync_enabled_flag = vl_rbsp_u(rbsp, 1);
477
478 if (pps->tiles_enabled_flag) {
479 pps->num_tile_columns_minus1 = vl_rbsp_ue(rbsp);
480 pps->num_tile_rows_minus1 = vl_rbsp_ue(rbsp);
481
482 pps->uniform_spacing_flag = vl_rbsp_u(rbsp, 1);
483 if (!pps->uniform_spacing_flag) {
484 for (i = 0; i < pps->num_tile_columns_minus1; ++i)
485 pps->column_width_minus1[i] = vl_rbsp_ue(rbsp);
486
487 for (i = 0; i < pps->num_tile_rows_minus1; ++i)
488 pps->row_height_minus1[i] = vl_rbsp_ue(rbsp);
489 }
490
491 if (!pps->num_tile_columns_minus1 || !pps->num_tile_rows_minus1)
492 pps->loop_filter_across_tiles_enabled_flag = vl_rbsp_u(rbsp, 1);
493 }
494
495 pps->pps_loop_filter_across_slices_enabled_flag = vl_rbsp_u(rbsp, 1);
496
497 pps->deblocking_filter_control_present_flag = vl_rbsp_u(rbsp, 1);
498 if (pps->deblocking_filter_control_present_flag) {
499 pps->deblocking_filter_override_enabled_flag = vl_rbsp_u(rbsp, 1);
500 pps->pps_deblocking_filter_disabled_flag = vl_rbsp_u(rbsp, 1);
501 if (!pps->pps_deblocking_filter_disabled_flag) {
502 pps->pps_beta_offset_div2 = vl_rbsp_se(rbsp);
503 pps->pps_tc_offset_div2 = vl_rbsp_se(rbsp);
504 }
505 }
506
507 /* pps_scaling_list_data_present_flag */
508 if (vl_rbsp_u(rbsp, 1))
509 scaling_list_data();
510
511 pps->lists_modification_present_flag = vl_rbsp_u(rbsp, 1);
512 pps->log2_parallel_merge_level_minus2 = vl_rbsp_ue(rbsp);
513 pps->slice_segment_header_extension_present_flag = vl_rbsp_u(rbsp, 1);
514 }
515
516 static void vid_dec_h265_BeginFrame(vid_dec_PrivateType *priv)
517 {
518 if (priv->frame_started)
519 return;
520
521 vid_dec_NeedTarget(priv);
522 if (priv->first_buf_in_frame)
523 priv->timestamp = priv->timestamps[0];
524 priv->first_buf_in_frame = false;
525
526 if (!priv->codec) {
527 struct pipe_video_codec templat = {};
528 omx_base_video_PortType *port;
529
530 port = (omx_base_video_PortType *)
531 priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
532 templat.profile = priv->profile;
533 templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
534 templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
535 templat.expect_chunked_decode = true;
536 templat.width = align(port->sPortParam.format.video.nFrameWidth, 4);
537 templat.height = align(port->sPortParam.format.video.nFrameHeight, 4);
538 templat.level = priv->codec_data.h265.level_idc;
539 priv->codec = priv->pipe->create_video_codec(priv->pipe, &templat);
540 }
541 priv->codec->begin_frame(priv->codec, priv->target, &priv->picture.base);
542 priv->frame_started = true;
543 }
544
545 static struct pipe_video_buffer *vid_dec_h265_Flush(vid_dec_PrivateType *priv,
546 OMX_TICKS *timestamp)
547 {
548 struct dpb_list *entry, *result = NULL;
549 struct pipe_video_buffer *buf;
550
551 /* search for the lowest poc and break on zeros */
552 LIST_FOR_EACH_ENTRY(entry, &priv->codec_data.h265.dpb_list, list) {
553
554 if (result && entry->poc == 0)
555 break;
556
557 if (!result || entry->poc < result->poc)
558 result = entry;
559 }
560
561 if (!result)
562 return NULL;
563
564 buf = result->buffer;
565 if (timestamp)
566 *timestamp = result->timestamp;
567
568 --priv->codec_data.h265.dpb_num;
569 LIST_DEL(&result->list);
570 FREE(result);
571
572 return buf;
573 }
574
575 static void vid_dec_h265_EndFrame(vid_dec_PrivateType *priv)
576 {
577 struct dpb_list *entry = NULL;
578 struct pipe_video_buffer *tmp;
579 struct ref_pic_set *rps;
580 int i;
581 OMX_TICKS timestamp;
582
583 if (!priv->frame_started)
584 return;
585
586 priv->picture.h265.NumPocStCurrBefore = 0;
587 priv->picture.h265.NumPocStCurrAfter = 0;
588 memset(priv->picture.h265.RefPicSetStCurrBefore, 0, 8);
589 memset(priv->picture.h265.RefPicSetStCurrAfter, 0, 8);
590 for (i = 0; i < MAX_NUM_REF_PICS; ++i) {
591 priv->picture.h265.ref[i] = NULL;
592 priv->picture.h265.PicOrderCntVal[i] = 0;
593 }
594
595 rps = priv->codec_data.h265.rps;
596
597 if (rps) {
598 priv->picture.h265.NumDeltaPocsOfRefRpsIdx = rps->num_delta_poc;
599 for (i = 0; i < rps->num_pics; ++i) {
600 priv->picture.h265.PicOrderCntVal[i] =
601 rps->delta_poc[i] + get_poc(priv);
602
603 LIST_FOR_EACH_ENTRY(entry, &priv->codec_data.h265.dpb_list, list) {
604 if (entry->poc == priv->picture.h265.PicOrderCntVal[i]) {
605 priv->picture.h265.ref[i] = entry->buffer;
606 break;
607 }
608 }
609
610 if (rps->used[i]) {
611 if (i < rps->num_neg_pics) {
612 priv->picture.h265.NumPocStCurrBefore++;
613 priv->picture.h265.RefPicSetStCurrBefore[i] = i;
614 } else {
615 int j = i - rps->num_neg_pics;
616 priv->picture.h265.NumPocStCurrAfter++;
617 priv->picture.h265.RefPicSetStCurrAfter[j] = i;
618 }
619 }
620 }
621 }
622
623 priv->codec->end_frame(priv->codec, priv->target, &priv->picture.base);
624 priv->frame_started = false;
625
626 /* add the decoded picture to the dpb list */
627 entry = CALLOC_STRUCT(dpb_list);
628 if (!entry)
629 return;
630
631 priv->first_buf_in_frame = true;
632 entry->buffer = priv->target;
633 entry->timestamp = priv->timestamp;
634 entry->poc = get_poc(priv);
635
636 LIST_ADDTAIL(&entry->list, &priv->codec_data.h265.dpb_list);
637 ++priv->codec_data.h265.dpb_num;
638 priv->target = NULL;
639
640 if (priv->codec_data.h265.dpb_num <= DPB_MAX_SIZE)
641 return;
642
643 tmp = priv->in_buffers[0]->pInputPortPrivate;
644 priv->in_buffers[0]->pInputPortPrivate = vid_dec_h265_Flush(priv, &timestamp);
645 priv->in_buffers[0]->nTimeStamp = timestamp;
646 priv->target = tmp;
647 priv->frame_finished = priv->in_buffers[0]->pInputPortPrivate != NULL;
648 if (priv->frame_finished &&
649 (priv->in_buffers[0]->nFlags & OMX_BUFFERFLAG_EOS))
650 FREE(priv->codec_data.h265.ref_pic_set_list);
651 }
652
653 static void slice_header(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp,
654 unsigned nal_unit_type)
655 {
656 struct pipe_h265_pps *pps;
657 struct pipe_h265_sps *sps;
658 bool first_slice_segment_in_pic_flag;
659 bool dependent_slice_segment_flag = false;
660 struct ref_pic_set *rps = NULL;
661 unsigned poc_lsb, poc_msb, slice_prev_poc;
662 unsigned max_poc_lsb, prev_poc_lsb, prev_poc_msb;
663 unsigned num_st_rps;
664 int i;
665
666 if (priv->picture.h265.IDRPicFlag != is_idr_picture(nal_unit_type))
667 vid_dec_h265_EndFrame(priv);
668
669 priv->picture.h265.IDRPicFlag = is_idr_picture(nal_unit_type);
670
671 first_slice_segment_in_pic_flag = vl_rbsp_u(rbsp, 1);
672
673 if (is_rap_picture(nal_unit_type))
674 /* no_output_of_prior_pics_flag */
675 vl_rbsp_u(rbsp, 1);
676
677 pps = pic_parameter_set_id(priv, rbsp);
678 if (!pps)
679 return;
680
681 sps = pps->sps;
682 if (!sps)
683 return;
684
685 if (pps != priv->picture.h265.pps)
686 vid_dec_h265_EndFrame(priv);
687
688 priv->picture.h265.pps = pps;
689
690 if (priv->picture.h265.RAPPicFlag != is_rap_picture(nal_unit_type))
691 vid_dec_h265_EndFrame(priv);
692 priv->picture.h265.RAPPicFlag = is_rap_picture(nal_unit_type);
693
694 num_st_rps = sps->num_short_term_ref_pic_sets;
695
696 if (priv->picture.h265.CurrRpsIdx != num_st_rps)
697 vid_dec_h265_EndFrame(priv);
698 priv->picture.h265.CurrRpsIdx = num_st_rps;
699
700 if (!first_slice_segment_in_pic_flag) {
701 int size, num;
702 int bits_slice_segment_address = 0;
703
704 if (pps->dependent_slice_segments_enabled_flag)
705 dependent_slice_segment_flag = vl_rbsp_u(rbsp, 1);
706
707 size = 1 << (sps->log2_min_luma_coding_block_size_minus3 + 3 +
708 sps->log2_diff_max_min_luma_coding_block_size);
709
710 num = ((sps->pic_width_in_luma_samples + size - 1) / size) *
711 ((sps->pic_height_in_luma_samples + size - 1) / size);
712
713 while (num > (1 << bits_slice_segment_address))
714 bits_slice_segment_address++;
715
716 /* slice_segment_address */
717 vl_rbsp_u(rbsp, bits_slice_segment_address);
718 }
719
720 if (dependent_slice_segment_flag)
721 return;
722
723 for (i = 0; i < pps->num_extra_slice_header_bits; ++i)
724 /* slice_reserved_flag */
725 vl_rbsp_u(rbsp, 1);
726
727 /* slice_type */
728 vl_rbsp_ue(rbsp);
729
730 if (pps->output_flag_present_flag)
731 /* pic output flag */
732 vl_rbsp_u(rbsp, 1);
733
734 if (sps->separate_colour_plane_flag)
735 /* colour_plane_id */
736 vl_rbsp_u(rbsp, 2);
737
738 if (is_idr_picture(nal_unit_type)) {
739 set_poc(priv, nal_unit_type, 0);
740 return;
741 }
742
743 /* slice_pic_order_cnt_lsb */
744 poc_lsb =
745 vl_rbsp_u(rbsp, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
746
747 slice_prev_poc = (int)priv->codec_data.h265.slice_prev_poc;
748 max_poc_lsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
749
750 prev_poc_lsb = slice_prev_poc & (max_poc_lsb - 1);
751 prev_poc_msb = slice_prev_poc - prev_poc_lsb;
752
753 if ((poc_lsb < prev_poc_lsb) &&
754 ((prev_poc_lsb - poc_lsb ) >= (max_poc_lsb / 2)))
755 poc_msb = prev_poc_msb + max_poc_lsb;
756
757 else if ((poc_lsb > prev_poc_lsb ) &&
758 ((poc_lsb - prev_poc_lsb) > (max_poc_lsb / 2)))
759 poc_msb = prev_poc_msb - max_poc_lsb;
760
761 else
762 poc_msb = prev_poc_msb;
763
764 if (is_bla_picture(nal_unit_type))
765 poc_msb = 0;
766
767 if (get_poc(priv) != poc_msb + poc_lsb)
768 vid_dec_h265_EndFrame(priv);
769
770 set_poc(priv, nal_unit_type, (poc_msb + poc_lsb));
771
772 /* short_term_ref_pic_set_sps_flag */
773 if (!vl_rbsp_u(rbsp, 1)) {
774 rps = (struct ref_pic_set *)
775 priv->codec_data.h265.ref_pic_set_list + num_st_rps;
776 st_ref_pic_set(priv, rbsp, rps, sps, num_st_rps);
777
778 } else if (num_st_rps > 1) {
779 int num_bits = 0;
780 unsigned idx;
781
782 while ((1 << num_bits) < num_st_rps)
783 num_bits++;
784
785 if (num_bits > 0)
786 /* short_term_ref_pic_set_idx */
787 idx = vl_rbsp_u(rbsp, num_bits);
788 else
789 idx = 0;
790
791 rps = (struct ref_pic_set *)
792 priv->codec_data.h265.ref_pic_set_list + idx;
793 }
794
795 if (is_bla_picture(nal_unit_type)) {
796 rps->num_neg_pics = 0;
797 rps->num_pos_pics = 0;
798 rps->num_pics = 0;
799 }
800
801 priv->codec_data.h265.rps = rps;
802
803 return;
804 }
805
806 static void vid_dec_h265_Decode(vid_dec_PrivateType *priv,
807 struct vl_vlc *vlc,
808 unsigned min_bits_left)
809 {
810 unsigned nal_unit_type;
811 unsigned nuh_layer_id;
812 unsigned nuh_temporal_id_plus1;
813
814 if (!vl_vlc_search_byte(vlc, vl_vlc_bits_left(vlc) - min_bits_left, 0x00))
815 return;
816
817 if (vl_vlc_peekbits(vlc, 24) != 0x000001) {
818 vl_vlc_eatbits(vlc, 8);
819 return;
820 }
821
822 if (priv->slice) {
823 unsigned bytes = priv->bytes_left - (vl_vlc_bits_left(vlc) / 8);
824
825 priv->codec->decode_bitstream(priv->codec, priv->target,
826 &priv->picture.base, 1,
827 &priv->slice, &bytes);
828 priv->slice = NULL;
829 }
830
831 vl_vlc_eatbits(vlc, 24);
832
833 /* forbidden_zero_bit */
834 vl_vlc_eatbits(vlc, 1);
835
836 if (vl_vlc_valid_bits(vlc) < 15)
837 vl_vlc_fillbits(vlc);
838
839 nal_unit_type = vl_vlc_get_uimsbf(vlc, 6);
840
841 /* nuh_layer_id */
842 nuh_layer_id = vl_vlc_get_uimsbf(vlc, 6);
843
844 /* nuh_temporal_id_plus1 */
845 nuh_temporal_id_plus1 = vl_vlc_get_uimsbf(vlc, 3);
846 priv->codec_data.h265.temporal_id = nuh_temporal_id_plus1 - 1;
847
848 if (!is_slice_picture(nal_unit_type))
849 vid_dec_h265_EndFrame(priv);
850
851 if (nal_unit_type == NAL_UNIT_TYPE_SPS) {
852 struct vl_rbsp rbsp;
853
854 vl_rbsp_init(&rbsp, vlc, ~0);
855 seq_parameter_set(priv, &rbsp);
856
857 } else if (nal_unit_type == NAL_UNIT_TYPE_PPS) {
858 struct vl_rbsp rbsp;
859
860 vl_rbsp_init(&rbsp, vlc, ~0);
861 picture_parameter_set(priv, &rbsp);
862
863 } else if (is_slice_picture(nal_unit_type)) {
864 unsigned bits = vl_vlc_valid_bits(vlc);
865 unsigned bytes = bits / 8 + 5;
866 struct vl_rbsp rbsp;
867 uint8_t buf[9];
868 const void *ptr = buf;
869 unsigned i;
870
871 buf[0] = 0x0;
872 buf[1] = 0x0;
873 buf[2] = 0x1;
874 buf[3] = nal_unit_type << 1 | nuh_layer_id >> 5;
875 buf[4] = nuh_layer_id << 3 | nuh_temporal_id_plus1;
876 for (i = 5; i < bytes; ++i)
877 buf[i] = vl_vlc_peekbits(vlc, bits) >> ((bytes - i - 1) * 8);
878
879 priv->bytes_left = (vl_vlc_bits_left(vlc) - bits) / 8;
880 priv->slice = vlc->data;
881
882 vl_rbsp_init(&rbsp, vlc, 128);
883 slice_header(priv, &rbsp, nal_unit_type);
884
885 vid_dec_h265_BeginFrame(priv);
886
887 priv->codec->decode_bitstream(priv->codec, priv->target,
888 &priv->picture.base, 1,
889 &ptr, &bytes);
890 }
891
892 /* resync to byte boundary */
893 vl_vlc_eatbits(vlc, vl_vlc_valid_bits(vlc) % 8);
894 }
895
896 void vid_dec_h265_Init(vid_dec_PrivateType *priv)
897 {
898 priv->picture.base.profile = PIPE_VIDEO_PROFILE_HEVC_MAIN;
899
900 LIST_INITHEAD(&priv->codec_data.h265.dpb_list);
901 priv->codec_data.h265.ref_pic_set_list = (struct ref_pic_set *)
902 CALLOC(MAX_NUM_REF_PICS, sizeof(struct ref_pic_set));
903
904 priv->Decode = vid_dec_h265_Decode;
905 priv->EndFrame = vid_dec_h265_EndFrame;
906 priv->Flush = vid_dec_h265_Flush;
907 priv->first_buf_in_frame = true;
908 }