• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static const uint8_t Default_8x8_Intra[64] = {
61    16, 16, 16, 16, 17, 18, 21, 24,
62    16, 16, 16, 16, 17, 19, 22, 25,
63    16, 16, 17, 18, 20, 22, 25, 29,
64    16, 16, 18, 21, 24, 27, 31, 36,
65    17, 17, 20, 24, 30, 35, 41, 47,
66    18, 19, 22, 27, 35, 44, 54, 65,
67    21, 22, 25, 31, 41, 54, 70, 88,
68    24, 25, 29, 36, 47, 65, 88, 115
69 };
70 
71 static const uint8_t Default_8x8_Inter[64] = {
72    16, 16, 16, 16, 17, 18, 20, 24,
73    16, 16, 16, 17, 18, 20, 24, 25,
74    16, 16, 17, 18, 20, 24, 25, 28,
75    16, 17, 18, 20, 24, 25, 28, 33,
76    17, 18, 20, 24, 25, 28, 33, 41,
77    18, 20, 24, 25, 28, 33, 41, 54,
78    20, 24, 25, 28, 33, 41, 54, 71,
79    24, 25, 28, 33, 41, 54, 71, 91
80 };
81 
82 struct dpb_list {
83    struct list_head list;
84    struct pipe_video_buffer *buffer;
85    OMX_TICKS timestamp;
86    unsigned poc;
87 };
88 
89 struct ref_pic_set {
90   unsigned  num_pics;
91   unsigned  num_neg_pics;
92   unsigned  num_pos_pics;
93   unsigned  num_delta_poc;
94   int  delta_poc[MAX_NUM_REF_PICS];
95   bool used[MAX_NUM_REF_PICS];
96 };
97 
is_idr_picture(unsigned nal_unit_type)98 static bool is_idr_picture(unsigned nal_unit_type)
99 {
100    return (nal_unit_type == NAL_UNIT_TYPE_IDR_W_RADL ||
101            nal_unit_type == NAL_UNIT_TYPE_IDR_N_LP);
102 }
103 
104 /* broken link access picture */
is_bla_picture(unsigned nal_unit_type)105 static bool is_bla_picture(unsigned nal_unit_type)
106 {
107    return (nal_unit_type == NAL_UNIT_TYPE_BLA_W_LP ||
108            nal_unit_type == NAL_UNIT_TYPE_BLA_W_RADL ||
109            nal_unit_type == NAL_UNIT_TYPE_BLA_N_LP);
110 }
111 
112 /* random access point picture */
is_rap_picture(unsigned nal_unit_type)113 static bool is_rap_picture(unsigned nal_unit_type)
114 {
115    return (nal_unit_type >= NAL_UNIT_TYPE_BLA_W_LP &&
116            nal_unit_type <= NAL_UNIT_TYPE_CRA);
117 }
118 
is_slice_picture(unsigned nal_unit_type)119 static bool is_slice_picture(unsigned nal_unit_type)
120 {
121    return (nal_unit_type <= NAL_UNIT_TYPE_RASL_R ||
122            is_rap_picture(nal_unit_type));
123 }
124 
set_poc(vid_dec_PrivateType * priv,unsigned nal_unit_type,int i)125 static void set_poc(vid_dec_PrivateType *priv,
126                     unsigned nal_unit_type, int i)
127 {
128    priv->picture.h265.CurrPicOrderCntVal = i;
129 
130    if (priv->codec_data.h265.temporal_id == 0 &&
131        (nal_unit_type == NAL_UNIT_TYPE_TRAIL_R ||
132         nal_unit_type == NAL_UNIT_TYPE_TSA_R ||
133         nal_unit_type == NAL_UNIT_TYPE_STSA_R ||
134         is_rap_picture(nal_unit_type)))
135       priv->codec_data.h265.slice_prev_poc = i;
136 }
137 
get_poc(vid_dec_PrivateType * priv)138 static unsigned get_poc(vid_dec_PrivateType *priv)
139 {
140    return priv->picture.h265.CurrPicOrderCntVal;
141 }
142 
profile_tier(struct vl_rbsp * rbsp)143 static void profile_tier(struct vl_rbsp *rbsp)
144 {
145    int i;
146 
147    /* general_profile_space */
148    vl_rbsp_u(rbsp, 2);
149 
150    /* general_tier_flag */
151    vl_rbsp_u(rbsp, 1);
152 
153    /* general_profile_idc */
154    vl_rbsp_u(rbsp, 5);
155 
156    /* general_profile_compatibility_flag */
157    for(i = 0; i < 32; ++i)
158       vl_rbsp_u(rbsp, 1);
159 
160    /* general_progressive_source_flag */
161    vl_rbsp_u(rbsp, 1);
162 
163    /* general_interlaced_source_flag */
164    vl_rbsp_u(rbsp, 1);
165 
166    /* general_non_packed_constraint_flag */
167    vl_rbsp_u(rbsp, 1);
168 
169    /* general_frame_only_constraint_flag */
170    vl_rbsp_u(rbsp, 1);
171 
172    /* general_reserved_zero_44bits */
173    vl_rbsp_u(rbsp, 16);
174    vl_rbsp_u(rbsp, 16);
175    vl_rbsp_u(rbsp, 12);
176 }
177 
profile_tier_level(struct vl_rbsp * rbsp,int max_sublayers_minus1)178 static unsigned profile_tier_level(struct vl_rbsp *rbsp,
179                                    int max_sublayers_minus1)
180 {
181    bool sub_layer_profile_present_flag[6];
182    bool sub_layer_level_present_flag[6];
183    unsigned level_idc;
184    int i;
185 
186    profile_tier(rbsp);
187 
188    /* general_level_idc */
189    level_idc = vl_rbsp_u(rbsp, 8);
190 
191    for (i = 0; i < max_sublayers_minus1; ++i) {
192       sub_layer_profile_present_flag[i] = vl_rbsp_u(rbsp, 1);
193       sub_layer_level_present_flag[i] = vl_rbsp_u(rbsp, 1);
194    }
195 
196    if (max_sublayers_minus1 > 0)
197       for (i = max_sublayers_minus1; i < 8; ++i)
198          /* reserved_zero_2bits */
199          vl_rbsp_u(rbsp, 2);
200 
201    for (i = 0; i < max_sublayers_minus1; ++i) {
202       if (sub_layer_profile_present_flag[i])
203          profile_tier(rbsp);
204 
205       if (sub_layer_level_present_flag[i])
206          /* sub_layer_level_idc */
207          vl_rbsp_u(rbsp, 8);
208    }
209 
210    return level_idc;
211 }
212 
scaling_list_data(vid_dec_PrivateType * priv,struct vl_rbsp * rbsp,struct pipe_h265_sps * sps)213 static void scaling_list_data(vid_dec_PrivateType *priv,
214                               struct vl_rbsp *rbsp, struct pipe_h265_sps *sps)
215 {
216    unsigned size_id, matrix_id;
217    unsigned scaling_list_len[4] = { 16, 64, 64, 64 };
218    uint8_t scaling_list4x4[6][64] = {  };
219    int i;
220 
221    uint8_t (*scaling_list_data[4])[6][64] = {
222         (uint8_t (*)[6][64])scaling_list4x4,
223         (uint8_t (*)[6][64])sps->ScalingList8x8,
224         (uint8_t (*)[6][64])sps->ScalingList16x16,
225         (uint8_t (*)[6][64])sps->ScalingList32x32
226    };
227    uint8_t (*scaling_list_dc_coeff[2])[6] = {
228       (uint8_t (*)[6])sps->ScalingListDCCoeff16x16,
229       (uint8_t (*)[6])sps->ScalingListDCCoeff32x32
230    };
231 
232    for (size_id = 0; size_id < 4; ++size_id) {
233 
234       for (matrix_id = 0; matrix_id < ((size_id == 3) ? 2 : 6); ++matrix_id) {
235          bool scaling_list_pred_mode_flag = vl_rbsp_u(rbsp, 1);
236 
237          if (!scaling_list_pred_mode_flag) {
238             /* scaling_list_pred_matrix_id_delta */;
239             unsigned matrix_id_with_delta = matrix_id - vl_rbsp_ue(rbsp);
240 
241             if (matrix_id != matrix_id_with_delta) {
242                memcpy((*scaling_list_data[size_id])[matrix_id],
243                       (*scaling_list_data[size_id])[matrix_id_with_delta],
244                       scaling_list_len[size_id]);
245                if (size_id > 1)
246                   (*scaling_list_dc_coeff[size_id - 2])[matrix_id] =
247                      (*scaling_list_dc_coeff[size_id - 2])[matrix_id_with_delta];
248             } else {
249                const uint8_t *d;
250 
251                if (size_id == 0)
252                   memset((*scaling_list_data[0])[matrix_id], 16, 16);
253                else {
254                   if (size_id < 3)
255                      d = (matrix_id < 3) ? Default_8x8_Intra : Default_8x8_Inter;
256                   else
257                      d = (matrix_id < 1) ? Default_8x8_Intra : Default_8x8_Inter;
258                   memcpy((*scaling_list_data[size_id])[matrix_id], d,
259                          scaling_list_len[size_id]);
260                }
261                if (size_id > 1)
262                   (*scaling_list_dc_coeff[size_id - 2])[matrix_id] = 16;
263             }
264          } else {
265             int next_coef = 8;
266             int coef_num = MIN2(64, (1 << (4 + (size_id << 1))));
267 
268             if (size_id > 1) {
269                /* scaling_list_dc_coef_minus8 */
270                next_coef = vl_rbsp_se(rbsp) + 8;
271                (*scaling_list_dc_coeff[size_id - 2])[matrix_id] = next_coef;
272             }
273 
274             for (i = 0; i < coef_num; ++i) {
275                /* scaling_list_delta_coef */
276                next_coef = (next_coef + vl_rbsp_se(rbsp) + 256) % 256;
277                (*scaling_list_data[size_id])[matrix_id][i] = next_coef;
278             }
279          }
280       }
281    }
282 
283    for (i = 0; i < 6; ++i)
284       memcpy(sps->ScalingList4x4[i], scaling_list4x4[i], 16);
285 
286    return;
287 }
288 
st_ref_pic_set(vid_dec_PrivateType * priv,struct vl_rbsp * rbsp,struct ref_pic_set * rps,struct pipe_h265_sps * sps,unsigned idx)289 static void st_ref_pic_set(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp,
290                            struct ref_pic_set *rps, struct pipe_h265_sps *sps,
291                            unsigned idx)
292 {
293    bool inter_rps_pred_flag;
294    unsigned delta_idx_minus1;
295    int delta_poc;
296    int i;
297 
298    inter_rps_pred_flag = (idx != 0) ? (vl_rbsp_u(rbsp, 1)) : false;
299 
300    if (inter_rps_pred_flag) {
301       struct ref_pic_set *ref_rps;
302       unsigned sign, abs;
303       int delta_rps;
304       bool used;
305       int j;
306 
307       if (idx == sps->num_short_term_ref_pic_sets)
308          delta_idx_minus1 = vl_rbsp_ue(rbsp);
309       else
310          delta_idx_minus1 = 0;
311 
312       ref_rps = (struct ref_pic_set *)
313          priv->codec_data.h265.ref_pic_set_list + idx - (delta_idx_minus1 + 1);
314 
315       /* delta_rps_sign */
316       sign = vl_rbsp_u(rbsp, 1);
317       /* abs_delta_rps_minus1 */
318       abs = vl_rbsp_ue(rbsp);
319       delta_rps = (1 - 2 * sign) * (abs + 1);
320 
321       rps->num_neg_pics = 0;
322       rps->num_pos_pics = 0;
323       rps->num_pics = 0;
324 
325       for(i = 0 ; i <= ref_rps->num_pics; ++i) {
326          /* used_by_curr_pic_flag */
327          if (!vl_rbsp_u(rbsp, 1))
328             /* use_delta_flag */
329             vl_rbsp_u(rbsp, 1);
330          else {
331             delta_poc = delta_rps +
332                ((i < ref_rps->num_pics)? ref_rps->delta_poc[i] : 0);
333             rps->delta_poc[rps->num_pics] = delta_poc;
334             rps->used[rps->num_pics] = true;
335             if (delta_poc < 0)
336                rps->num_neg_pics++;
337             else
338                rps->num_pos_pics++;
339             rps->num_pics++;
340          }
341       }
342 
343       rps->num_delta_poc = ref_rps->num_pics;
344 
345       /* sort delta poc */
346       for (i = 1; i < rps->num_pics; ++i) {
347          delta_poc = rps->delta_poc[i];
348          used = rps->used[i];
349          for (j = i - 1; j >= 0; j--) {
350             if (delta_poc < rps->delta_poc[j]) {
351                rps->delta_poc[j + 1] = rps->delta_poc[j];
352                rps->used[j + 1] = rps->used[j];
353                rps->delta_poc[j] = delta_poc;
354                rps->used[j] = used;
355             }
356          }
357       }
358 
359       for (i = 0 , j = rps->num_neg_pics - 1;
360            i < rps->num_neg_pics >> 1; i++, j--) {
361          delta_poc = rps->delta_poc[i];
362          used = rps->used[i];
363          rps->delta_poc[i] = rps->delta_poc[j];
364          rps->used[i] = rps->used[j];
365          rps->delta_poc[j] = delta_poc;
366          rps->used[j] = used;
367       }
368    } else {
369       /* num_negative_pics */
370       rps->num_neg_pics = vl_rbsp_ue(rbsp);
371       /* num_positive_pics */
372       rps->num_pos_pics = vl_rbsp_ue(rbsp);
373       rps->num_pics = rps->num_neg_pics + rps->num_pos_pics;
374 
375       delta_poc = 0;
376       for(i = 0 ; i < rps->num_neg_pics; ++i) {
377          /* delta_poc_s0_minus1 */
378          delta_poc -= (vl_rbsp_ue(rbsp) + 1);
379          rps->delta_poc[i] = delta_poc;
380          /* used_by_curr_pic_s0_flag */
381          rps->used[i] = vl_rbsp_u(rbsp, 1);
382       }
383 
384       delta_poc = 0;
385       for(i = rps->num_neg_pics; i < rps->num_pics; ++i) {
386          /* delta_poc_s1_minus1 */
387          delta_poc += (vl_rbsp_ue(rbsp) + 1);
388          rps->delta_poc[i] = delta_poc;
389          /* used_by_curr_pic_s1_flag */
390          rps->used[i] = vl_rbsp_u(rbsp, 1);
391       }
392    }
393 }
394 
seq_parameter_set_id(vid_dec_PrivateType * priv,struct vl_rbsp * rbsp)395 static struct pipe_h265_sps *seq_parameter_set_id(vid_dec_PrivateType *priv,
396                                                   struct vl_rbsp *rbsp)
397 {
398    unsigned id = vl_rbsp_ue(rbsp);
399 
400    if (id >= ARRAY_SIZE(priv->codec_data.h265.sps))
401       return NULL;
402 
403    return &priv->codec_data.h265.sps[id];
404 }
405 
seq_parameter_set(vid_dec_PrivateType * priv,struct vl_rbsp * rbsp)406 static void seq_parameter_set(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp)
407 {
408    struct pipe_h265_sps *sps;
409    int sps_max_sub_layers_minus1;
410    unsigned i;
411 
412    /* sps_video_parameter_set_id */
413    vl_rbsp_u(rbsp, 4);
414 
415    /* sps_max_sub_layers_minus1 */
416    sps_max_sub_layers_minus1 = vl_rbsp_u(rbsp, 3);
417 
418    assert(sps_max_sub_layers_minus1 <= 6);
419 
420    /* sps_temporal_id_nesting_flag */
421    vl_rbsp_u(rbsp, 1);
422 
423    priv->codec_data.h265.level_idc =
424       profile_tier_level(rbsp, sps_max_sub_layers_minus1);
425 
426    sps = seq_parameter_set_id(priv, rbsp);
427    if (!sps)
428       return;
429 
430    memset(sps, 0, sizeof(*sps));
431 
432    sps->chroma_format_idc = vl_rbsp_ue(rbsp);
433 
434    if (sps->chroma_format_idc == 3)
435       sps->separate_colour_plane_flag = vl_rbsp_u(rbsp, 1);
436 
437    priv->codec_data.h265.pic_width_in_luma_samples =
438       sps->pic_width_in_luma_samples = vl_rbsp_ue(rbsp);
439 
440    priv->codec_data.h265.pic_height_in_luma_samples =
441       sps->pic_height_in_luma_samples = vl_rbsp_ue(rbsp);
442 
443    /* conformance_window_flag */
444    if (vl_rbsp_u(rbsp, 1)) {
445       /* conf_win_left_offset */
446       vl_rbsp_ue(rbsp);
447       /* conf_win_right_offset */
448       vl_rbsp_ue(rbsp);
449       /* conf_win_top_offset */
450       vl_rbsp_ue(rbsp);
451       /* conf_win_bottom_offset */
452       vl_rbsp_ue(rbsp);
453    }
454 
455    sps->bit_depth_luma_minus8 = vl_rbsp_ue(rbsp);
456    sps->bit_depth_chroma_minus8 = vl_rbsp_ue(rbsp);
457    sps->log2_max_pic_order_cnt_lsb_minus4 = vl_rbsp_ue(rbsp);
458 
459    /* sps_sub_layer_ordering_info_present_flag */
460    i  = vl_rbsp_u(rbsp, 1) ? 0 : sps_max_sub_layers_minus1;
461    for (; i <= sps_max_sub_layers_minus1; ++i) {
462       sps->sps_max_dec_pic_buffering_minus1 = vl_rbsp_ue(rbsp);
463       /* sps_max_num_reorder_pics */
464       vl_rbsp_ue(rbsp);
465       /* sps_max_latency_increase_plus */
466       vl_rbsp_ue(rbsp);
467    }
468 
469    sps->log2_min_luma_coding_block_size_minus3 = vl_rbsp_ue(rbsp);
470    sps->log2_diff_max_min_luma_coding_block_size = vl_rbsp_ue(rbsp);
471    sps->log2_min_transform_block_size_minus2 = vl_rbsp_ue(rbsp);
472    sps->log2_diff_max_min_transform_block_size = vl_rbsp_ue(rbsp);
473    sps->max_transform_hierarchy_depth_inter = vl_rbsp_ue(rbsp);
474    sps->max_transform_hierarchy_depth_intra = vl_rbsp_ue(rbsp);
475 
476    sps->scaling_list_enabled_flag = vl_rbsp_u(rbsp, 1);
477    if (sps->scaling_list_enabled_flag)
478       /* sps_scaling_list_data_present_flag */
479       if (vl_rbsp_u(rbsp, 1))
480          scaling_list_data(priv, rbsp, sps);
481 
482    sps->amp_enabled_flag = vl_rbsp_u(rbsp, 1);
483    sps->sample_adaptive_offset_enabled_flag = vl_rbsp_u(rbsp, 1);
484    sps->pcm_enabled_flag = vl_rbsp_u(rbsp, 1);
485    if (sps->pcm_enabled_flag) {
486       sps->pcm_sample_bit_depth_luma_minus1 = vl_rbsp_u(rbsp, 4);
487       sps->pcm_sample_bit_depth_chroma_minus1 = vl_rbsp_u(rbsp, 4);
488       sps->log2_min_pcm_luma_coding_block_size_minus3 = vl_rbsp_ue(rbsp);
489       sps->log2_diff_max_min_pcm_luma_coding_block_size = vl_rbsp_ue(rbsp);
490       sps->pcm_loop_filter_disabled_flag = vl_rbsp_u(rbsp, 1);
491    }
492 
493    sps->num_short_term_ref_pic_sets = vl_rbsp_ue(rbsp);
494 
495    for (i = 0; i < sps->num_short_term_ref_pic_sets; ++i) {
496       struct ref_pic_set *rps;
497 
498       rps = (struct ref_pic_set *)
499          priv->codec_data.h265.ref_pic_set_list + i;
500       st_ref_pic_set(priv, rbsp, rps, sps, i);
501    }
502 
503    sps->long_term_ref_pics_present_flag = vl_rbsp_u(rbsp, 1);
504    if (sps->long_term_ref_pics_present_flag) {
505       sps->num_long_term_ref_pics_sps = vl_rbsp_ue(rbsp);
506       for (i = 0; i < sps->num_long_term_ref_pics_sps; ++i) {
507          /* lt_ref_pic_poc_lsb_sps */
508          vl_rbsp_u(rbsp, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
509          /* used_by_curr_pic_lt_sps_flag */
510          vl_rbsp_u(rbsp, 1);
511       }
512    }
513 
514    sps->sps_temporal_mvp_enabled_flag = vl_rbsp_u(rbsp, 1);
515    sps->strong_intra_smoothing_enabled_flag = vl_rbsp_u(rbsp, 1);
516 }
517 
pic_parameter_set_id(vid_dec_PrivateType * priv,struct vl_rbsp * rbsp)518 static struct pipe_h265_pps *pic_parameter_set_id(vid_dec_PrivateType *priv,
519                                                   struct vl_rbsp *rbsp)
520 {
521    unsigned id = vl_rbsp_ue(rbsp);
522 
523    if (id >= ARRAY_SIZE(priv->codec_data.h265.pps))
524       return NULL;
525 
526    return &priv->codec_data.h265.pps[id];
527 }
528 
picture_parameter_set(vid_dec_PrivateType * priv,struct vl_rbsp * rbsp)529 static void picture_parameter_set(vid_dec_PrivateType *priv,
530                                   struct vl_rbsp *rbsp)
531 {
532    struct pipe_h265_sps *sps;
533    struct pipe_h265_pps *pps;
534    int i;
535 
536    pps = pic_parameter_set_id(priv, rbsp);
537    if (!pps)
538       return;
539 
540    memset(pps, 0, sizeof(*pps));
541    sps = pps->sps = seq_parameter_set_id(priv, rbsp);
542    if (!sps)
543       return;
544 
545    pps->dependent_slice_segments_enabled_flag = vl_rbsp_u(rbsp, 1);
546    pps->output_flag_present_flag = vl_rbsp_u(rbsp, 1);
547    pps->num_extra_slice_header_bits = vl_rbsp_u(rbsp, 3);
548    pps->sign_data_hiding_enabled_flag = vl_rbsp_u(rbsp, 1);
549    pps->cabac_init_present_flag = vl_rbsp_u(rbsp, 1);
550 
551    pps->num_ref_idx_l0_default_active_minus1 = vl_rbsp_ue(rbsp);
552    pps->num_ref_idx_l1_default_active_minus1 = vl_rbsp_ue(rbsp);
553    pps->init_qp_minus26 = vl_rbsp_se(rbsp);
554    pps->constrained_intra_pred_flag = vl_rbsp_u(rbsp, 1);
555    pps->transform_skip_enabled_flag = vl_rbsp_u(rbsp, 1);
556 
557    pps->cu_qp_delta_enabled_flag = vl_rbsp_u(rbsp, 1);
558    if (pps->cu_qp_delta_enabled_flag)
559       pps->diff_cu_qp_delta_depth = vl_rbsp_ue(rbsp);
560 
561    pps->pps_cb_qp_offset = vl_rbsp_se(rbsp);
562    pps->pps_cr_qp_offset = vl_rbsp_se(rbsp);
563    pps->pps_slice_chroma_qp_offsets_present_flag = vl_rbsp_u(rbsp, 1);
564 
565    pps->weighted_pred_flag = vl_rbsp_u(rbsp, 1);
566    pps->weighted_bipred_flag = vl_rbsp_u(rbsp, 1);
567 
568    pps->transquant_bypass_enabled_flag = vl_rbsp_u(rbsp, 1);
569    pps->tiles_enabled_flag = vl_rbsp_u(rbsp, 1);
570    pps->entropy_coding_sync_enabled_flag = vl_rbsp_u(rbsp, 1);
571 
572    if (pps->tiles_enabled_flag) {
573       pps->num_tile_columns_minus1 = vl_rbsp_ue(rbsp);
574       pps->num_tile_rows_minus1 = vl_rbsp_ue(rbsp);
575 
576       pps->uniform_spacing_flag = vl_rbsp_u(rbsp, 1);
577       if (!pps->uniform_spacing_flag) {
578          for (i = 0; i < pps->num_tile_columns_minus1; ++i)
579             pps->column_width_minus1[i] = vl_rbsp_ue(rbsp);
580 
581          for (i = 0; i < pps->num_tile_rows_minus1; ++i)
582             pps->row_height_minus1[i] = vl_rbsp_ue(rbsp);
583       }
584 
585       if (!pps->num_tile_columns_minus1 || !pps->num_tile_rows_minus1)
586          pps->loop_filter_across_tiles_enabled_flag = vl_rbsp_u(rbsp, 1);
587    }
588 
589    pps->pps_loop_filter_across_slices_enabled_flag = vl_rbsp_u(rbsp, 1);
590 
591    pps->deblocking_filter_control_present_flag = vl_rbsp_u(rbsp, 1);
592    if (pps->deblocking_filter_control_present_flag) {
593       pps->deblocking_filter_override_enabled_flag = vl_rbsp_u(rbsp, 1);
594       pps->pps_deblocking_filter_disabled_flag = vl_rbsp_u(rbsp, 1);
595       if (!pps->pps_deblocking_filter_disabled_flag) {
596          pps->pps_beta_offset_div2 = vl_rbsp_se(rbsp);
597          pps->pps_tc_offset_div2 = vl_rbsp_se(rbsp);
598       }
599    }
600 
601    /* pps_scaling_list_data_present_flag */
602    if (vl_rbsp_u(rbsp, 1))
603       scaling_list_data(priv, rbsp, sps);
604 
605    pps->lists_modification_present_flag = vl_rbsp_u(rbsp, 1);
606    pps->log2_parallel_merge_level_minus2 = vl_rbsp_ue(rbsp);
607    pps->slice_segment_header_extension_present_flag = vl_rbsp_u(rbsp, 1);
608 }
609 
vid_dec_h265_BeginFrame(vid_dec_PrivateType * priv)610 static void vid_dec_h265_BeginFrame(vid_dec_PrivateType *priv)
611 {
612    if (priv->frame_started)
613       return;
614 
615    if (!priv->codec) {
616       struct pipe_video_codec templat = {};
617       omx_base_video_PortType *port = (omx_base_video_PortType *)
618          priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
619 
620       templat.profile = priv->profile;
621       templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
622       templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
623       templat.expect_chunked_decode = true;
624       templat.width = priv->codec_data.h265.pic_width_in_luma_samples;
625       templat.height = priv->codec_data.h265.pic_height_in_luma_samples;
626       templat.level =  priv->codec_data.h265.level_idc;
627       priv->codec = priv->pipe->create_video_codec(priv->pipe, &templat);
628 
629       /* disable transcode tunnel if video size is different from coded size */
630       if (priv->codec_data.h265.pic_width_in_luma_samples !=
631           port->sPortParam.format.video.nFrameWidth ||
632           priv->codec_data.h265.pic_height_in_luma_samples !=
633           port->sPortParam.format.video.nFrameHeight)
634          priv->disable_tunnel = true;
635    }
636 
637    vid_dec_NeedTarget(priv);
638 
639    if (priv->first_buf_in_frame)
640       priv->timestamp = priv->timestamps[0];
641    priv->first_buf_in_frame = false;
642 
643    priv->codec->begin_frame(priv->codec, priv->target, &priv->picture.base);
644    priv->frame_started = true;
645 }
646 
vid_dec_h265_Flush(vid_dec_PrivateType * priv,OMX_TICKS * timestamp)647 static struct pipe_video_buffer *vid_dec_h265_Flush(vid_dec_PrivateType *priv,
648                                                     OMX_TICKS *timestamp)
649 {
650    struct dpb_list *entry, *result = NULL;
651    struct pipe_video_buffer *buf;
652 
653    /* search for the lowest poc and break on zeros */
654    LIST_FOR_EACH_ENTRY(entry, &priv->codec_data.h265.dpb_list, list) {
655 
656       if (result && entry->poc == 0)
657          break;
658 
659       if (!result || entry->poc < result->poc)
660          result = entry;
661    }
662 
663    if (!result)
664       return NULL;
665 
666    buf = result->buffer;
667    if (timestamp)
668       *timestamp = result->timestamp;
669 
670    --priv->codec_data.h265.dpb_num;
671    LIST_DEL(&result->list);
672    FREE(result);
673 
674    return buf;
675 }
676 
vid_dec_h265_EndFrame(vid_dec_PrivateType * priv)677 static void vid_dec_h265_EndFrame(vid_dec_PrivateType *priv)
678 {
679    struct dpb_list *entry = NULL;
680    struct pipe_video_buffer *tmp;
681    struct ref_pic_set *rps;
682    int i;
683    OMX_TICKS timestamp;
684 
685    if (!priv->frame_started)
686       return;
687 
688    priv->picture.h265.NumPocStCurrBefore = 0;
689    priv->picture.h265.NumPocStCurrAfter = 0;
690    memset(priv->picture.h265.RefPicSetStCurrBefore, 0, 8);
691    memset(priv->picture.h265.RefPicSetStCurrAfter, 0, 8);
692    for (i = 0; i < MAX_NUM_REF_PICS; ++i) {
693       priv->picture.h265.ref[i] = NULL;
694       priv->picture.h265.PicOrderCntVal[i] = 0;
695    }
696 
697    rps = priv->codec_data.h265.rps;
698 
699    if (rps) {
700       unsigned bf = 0, af = 0;
701 
702       priv->picture.h265.NumDeltaPocsOfRefRpsIdx = rps->num_delta_poc;
703       for (i = 0; i < rps->num_pics; ++i) {
704          priv->picture.h265.PicOrderCntVal[i] =
705             rps->delta_poc[i] + get_poc(priv);
706 
707          LIST_FOR_EACH_ENTRY(entry, &priv->codec_data.h265.dpb_list, list) {
708             if (entry->poc == priv->picture.h265.PicOrderCntVal[i]) {
709                priv->picture.h265.ref[i] = entry->buffer;
710                break;
711             }
712          }
713 
714          if (rps->used[i]) {
715             if (i < rps->num_neg_pics) {
716                priv->picture.h265.NumPocStCurrBefore++;
717                priv->picture.h265.RefPicSetStCurrBefore[bf++] = i;
718             } else {
719                priv->picture.h265.NumPocStCurrAfter++;
720                priv->picture.h265.RefPicSetStCurrAfter[af++] = i;
721             }
722          }
723       }
724    }
725 
726    priv->codec->end_frame(priv->codec, priv->target, &priv->picture.base);
727    priv->frame_started = false;
728 
729    /* add the decoded picture to the dpb list */
730    entry = CALLOC_STRUCT(dpb_list);
731    if (!entry)
732       return;
733 
734    priv->first_buf_in_frame = true;
735    entry->buffer = priv->target;
736    entry->timestamp = priv->timestamp;
737    entry->poc = get_poc(priv);
738 
739    LIST_ADDTAIL(&entry->list, &priv->codec_data.h265.dpb_list);
740    ++priv->codec_data.h265.dpb_num;
741    priv->target = NULL;
742 
743    if (priv->codec_data.h265.dpb_num <= DPB_MAX_SIZE)
744       return;
745 
746    tmp = priv->in_buffers[0]->pInputPortPrivate;
747    priv->in_buffers[0]->pInputPortPrivate = vid_dec_h265_Flush(priv, &timestamp);
748    priv->in_buffers[0]->nTimeStamp = timestamp;
749    priv->target = tmp;
750    priv->frame_finished = priv->in_buffers[0]->pInputPortPrivate != NULL;
751    if (priv->frame_finished &&
752        (priv->in_buffers[0]->nFlags & OMX_BUFFERFLAG_EOS))
753       FREE(priv->codec_data.h265.ref_pic_set_list);
754 }
755 
slice_header(vid_dec_PrivateType * priv,struct vl_rbsp * rbsp,unsigned nal_unit_type)756 static void slice_header(vid_dec_PrivateType *priv, struct vl_rbsp *rbsp,
757                          unsigned nal_unit_type)
758 {
759    struct pipe_h265_pps *pps;
760    struct pipe_h265_sps *sps;
761    bool first_slice_segment_in_pic_flag;
762    bool dependent_slice_segment_flag = false;
763    struct ref_pic_set *rps;
764    unsigned poc_lsb, poc_msb, slice_prev_poc;
765    unsigned max_poc_lsb, prev_poc_lsb, prev_poc_msb;
766    unsigned num_st_rps;
767    int i;
768 
769    if (priv->picture.h265.IDRPicFlag != is_idr_picture(nal_unit_type))
770       vid_dec_h265_EndFrame(priv);
771 
772    priv->picture.h265.IDRPicFlag = is_idr_picture(nal_unit_type);
773 
774    first_slice_segment_in_pic_flag = vl_rbsp_u(rbsp, 1);
775 
776    if (is_rap_picture(nal_unit_type))
777       /* no_output_of_prior_pics_flag */
778       vl_rbsp_u(rbsp, 1);
779 
780    pps = pic_parameter_set_id(priv, rbsp);
781    if (!pps)
782       return;
783 
784    sps = pps->sps;
785    if (!sps)
786       return;
787 
788    if (pps != priv->picture.h265.pps)
789       vid_dec_h265_EndFrame(priv);
790 
791    priv->picture.h265.pps = pps;
792 
793    if (priv->picture.h265.RAPPicFlag != is_rap_picture(nal_unit_type))
794       vid_dec_h265_EndFrame(priv);
795    priv->picture.h265.RAPPicFlag = is_rap_picture(nal_unit_type);
796 
797    num_st_rps = sps->num_short_term_ref_pic_sets;
798 
799    if (priv->picture.h265.CurrRpsIdx != num_st_rps)
800       vid_dec_h265_EndFrame(priv);
801    priv->picture.h265.CurrRpsIdx = num_st_rps;
802 
803    if (!first_slice_segment_in_pic_flag) {
804       int size, num;
805       int bits_slice_segment_address = 0;
806 
807       if (pps->dependent_slice_segments_enabled_flag)
808          dependent_slice_segment_flag = vl_rbsp_u(rbsp, 1);
809 
810       size = 1 << (sps->log2_min_luma_coding_block_size_minus3 + 3 +
811                    sps->log2_diff_max_min_luma_coding_block_size);
812 
813       num = ((sps->pic_width_in_luma_samples + size - 1) / size) *
814             ((sps->pic_height_in_luma_samples + size - 1) / size);
815 
816       while (num > (1 << bits_slice_segment_address))
817          bits_slice_segment_address++;
818 
819       /* slice_segment_address */
820       vl_rbsp_u(rbsp, bits_slice_segment_address);
821    }
822 
823    if (dependent_slice_segment_flag)
824       return;
825 
826    for (i = 0; i < pps->num_extra_slice_header_bits; ++i)
827       /* slice_reserved_flag */
828       vl_rbsp_u(rbsp, 1);
829 
830    /* slice_type */
831    vl_rbsp_ue(rbsp);
832 
833    if (pps->output_flag_present_flag)
834       /* pic output flag */
835       vl_rbsp_u(rbsp, 1);
836 
837    if (sps->separate_colour_plane_flag)
838       /* colour_plane_id */
839       vl_rbsp_u(rbsp, 2);
840 
841    if (is_idr_picture(nal_unit_type)) {
842       set_poc(priv, nal_unit_type, 0);
843       return;
844    }
845 
846    /* slice_pic_order_cnt_lsb */
847    poc_lsb =
848       vl_rbsp_u(rbsp, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
849 
850    slice_prev_poc = (int)priv->codec_data.h265.slice_prev_poc;
851    max_poc_lsb = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
852 
853    prev_poc_lsb = slice_prev_poc & (max_poc_lsb - 1);
854    prev_poc_msb = slice_prev_poc - prev_poc_lsb;
855 
856    if ((poc_lsb < prev_poc_lsb) &&
857        ((prev_poc_lsb - poc_lsb ) >= (max_poc_lsb / 2)))
858       poc_msb = prev_poc_msb + max_poc_lsb;
859 
860    else if ((poc_lsb > prev_poc_lsb ) &&
861             ((poc_lsb - prev_poc_lsb) > (max_poc_lsb / 2)))
862       poc_msb = prev_poc_msb - max_poc_lsb;
863 
864    else
865       poc_msb = prev_poc_msb;
866 
867    if (is_bla_picture(nal_unit_type))
868       poc_msb = 0;
869 
870    if (get_poc(priv) != poc_msb + poc_lsb)
871       vid_dec_h265_EndFrame(priv);
872 
873    set_poc(priv, nal_unit_type, (poc_msb + poc_lsb));
874 
875    /* short_term_ref_pic_set_sps_flag */
876    if (!vl_rbsp_u(rbsp, 1)) {
877       rps = (struct ref_pic_set *)
878          priv->codec_data.h265.ref_pic_set_list + num_st_rps;
879       st_ref_pic_set(priv, rbsp, rps, sps, num_st_rps);
880 
881    } else if (num_st_rps > 1) {
882       int num_bits = 0;
883       unsigned idx;
884 
885       while ((1 << num_bits) < num_st_rps)
886          num_bits++;
887 
888       if (num_bits > 0)
889          /* short_term_ref_pic_set_idx */
890          idx = vl_rbsp_u(rbsp, num_bits);
891       else
892          idx = 0;
893 
894       rps = (struct ref_pic_set *)
895          priv->codec_data.h265.ref_pic_set_list + idx;
896    } else
897       rps = (struct ref_pic_set *)
898          priv->codec_data.h265.ref_pic_set_list;
899 
900    if (is_bla_picture(nal_unit_type)) {
901       rps->num_neg_pics = 0;
902       rps->num_pos_pics = 0;
903       rps->num_pics = 0;
904    }
905 
906    priv->codec_data.h265.rps = rps;
907 
908    return;
909 }
910 
vid_dec_h265_Decode(vid_dec_PrivateType * priv,struct vl_vlc * vlc,unsigned min_bits_left)911 static void vid_dec_h265_Decode(vid_dec_PrivateType *priv,
912                                 struct vl_vlc *vlc,
913                                 unsigned min_bits_left)
914 {
915    unsigned nal_unit_type;
916    unsigned nuh_layer_id;
917    unsigned nuh_temporal_id_plus1;
918 
919    if (!vl_vlc_search_byte(vlc, vl_vlc_bits_left(vlc) - min_bits_left, 0x00))
920       return;
921 
922    if (vl_vlc_peekbits(vlc, 24) != 0x000001) {
923       vl_vlc_eatbits(vlc, 8);
924       return;
925    }
926 
927    if (priv->slice) {
928       unsigned bytes = priv->bytes_left - (vl_vlc_bits_left(vlc) / 8);
929 
930       priv->codec->decode_bitstream(priv->codec, priv->target,
931                                     &priv->picture.base, 1,
932                                     &priv->slice, &bytes);
933       priv->slice = NULL;
934    }
935 
936    vl_vlc_eatbits(vlc, 24);
937 
938    /* forbidden_zero_bit */
939    vl_vlc_eatbits(vlc, 1);
940 
941    if (vl_vlc_valid_bits(vlc) < 15)
942       vl_vlc_fillbits(vlc);
943 
944    nal_unit_type = vl_vlc_get_uimsbf(vlc, 6);
945 
946    /* nuh_layer_id */
947    nuh_layer_id = vl_vlc_get_uimsbf(vlc, 6);
948 
949    /* nuh_temporal_id_plus1 */
950    nuh_temporal_id_plus1 = vl_vlc_get_uimsbf(vlc, 3);
951    priv->codec_data.h265.temporal_id = nuh_temporal_id_plus1 - 1;
952 
953    if (!is_slice_picture(nal_unit_type))
954       vid_dec_h265_EndFrame(priv);
955 
956    if (nal_unit_type == NAL_UNIT_TYPE_SPS) {
957       struct vl_rbsp rbsp;
958 
959       vl_rbsp_init(&rbsp, vlc, ~0);
960       seq_parameter_set(priv, &rbsp);
961 
962    } else if (nal_unit_type == NAL_UNIT_TYPE_PPS) {
963       struct vl_rbsp rbsp;
964 
965       vl_rbsp_init(&rbsp, vlc, ~0);
966       picture_parameter_set(priv, &rbsp);
967 
968    } else if (is_slice_picture(nal_unit_type)) {
969       unsigned bits = vl_vlc_valid_bits(vlc);
970       unsigned bytes = bits / 8 + 5;
971       struct vl_rbsp rbsp;
972       uint8_t buf[9];
973       const void *ptr = buf;
974       unsigned i;
975 
976       buf[0] = 0x0;
977       buf[1] = 0x0;
978       buf[2] = 0x1;
979       buf[3] = nal_unit_type << 1 | nuh_layer_id >> 5;
980       buf[4] = nuh_layer_id << 3 | nuh_temporal_id_plus1;
981       for (i = 5; i < bytes; ++i)
982          buf[i] = vl_vlc_peekbits(vlc, bits) >> ((bytes - i - 1) * 8);
983 
984       priv->bytes_left = (vl_vlc_bits_left(vlc) - bits) / 8;
985       priv->slice = vlc->data;
986 
987       vl_rbsp_init(&rbsp, vlc, 128);
988       slice_header(priv, &rbsp, nal_unit_type);
989 
990       vid_dec_h265_BeginFrame(priv);
991 
992       priv->codec->decode_bitstream(priv->codec, priv->target,
993                                     &priv->picture.base, 1,
994                                     &ptr, &bytes);
995    }
996 
997    /* resync to byte boundary */
998    vl_vlc_eatbits(vlc, vl_vlc_valid_bits(vlc) % 8);
999 }
1000 
vid_dec_h265_Init(vid_dec_PrivateType * priv)1001 void vid_dec_h265_Init(vid_dec_PrivateType *priv)
1002 {
1003    priv->picture.base.profile = PIPE_VIDEO_PROFILE_HEVC_MAIN;
1004 
1005    LIST_INITHEAD(&priv->codec_data.h265.dpb_list);
1006    priv->codec_data.h265.ref_pic_set_list = (struct ref_pic_set *)
1007       CALLOC(MAX_NUM_REF_PICS, sizeof(struct ref_pic_set));
1008 
1009    priv->Decode = vid_dec_h265_Decode;
1010    priv->EndFrame = vid_dec_h265_EndFrame;
1011    priv->Flush = vid_dec_h265_Flush;
1012    priv->first_buf_in_frame = true;
1013 }
1014