1bf215546Sopenharmony_ci /**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2010 Thomas Balling Sørensen & Orasanu Lucian.
4bf215546Sopenharmony_ci * Copyright 2014 Advanced Micro Devices, Inc.
5bf215546Sopenharmony_ci * All Rights Reserved.
6bf215546Sopenharmony_ci *
7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
9bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
10bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
11bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
12bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
13bf215546Sopenharmony_ci * the following conditions:
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
16bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
17bf215546Sopenharmony_ci * of the Software.
18bf215546Sopenharmony_ci *
19bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22bf215546Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
23bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26bf215546Sopenharmony_ci *
27bf215546Sopenharmony_ci **************************************************************************/
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "util/u_video.h"
30bf215546Sopenharmony_ci#include "util/u_handle_table.h"
31bf215546Sopenharmony_ci#include "va_private.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cistatic void vlVaGetPastReferenceFrame(vlVaDriver *drv, VASurfaceID surface_id,
34bf215546Sopenharmony_ci                      struct pipe_video_buffer **ref_frame)
35bf215546Sopenharmony_ci{
36bf215546Sopenharmony_ci   vlVaSurface *surf = handle_table_get(drv->htab, surface_id);
37bf215546Sopenharmony_ci   if (surf) {
38bf215546Sopenharmony_ci      *ref_frame = surf->obsolete_buf;
39bf215546Sopenharmony_ci      surf->obsolete_buf = NULL;
40bf215546Sopenharmony_ci   }
41bf215546Sopenharmony_ci   else
42bf215546Sopenharmony_ci      *ref_frame = NULL;
43bf215546Sopenharmony_ci}
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_cistatic void resetReferencePictureDesc(struct pipe_h264_picture_desc *h264,
46bf215546Sopenharmony_ci                                      unsigned int i)
47bf215546Sopenharmony_ci{
48bf215546Sopenharmony_ci   struct h264_private *private = h264->priv;
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci   h264->ref[i] = NULL;
51bf215546Sopenharmony_ci   h264->frame_num_list[i] = 0;
52bf215546Sopenharmony_ci   h264->is_long_term[i] = 0;
53bf215546Sopenharmony_ci   h264->top_is_reference[i] = 0;
54bf215546Sopenharmony_ci   h264->bottom_is_reference[i] = 0;
55bf215546Sopenharmony_ci   h264->field_order_cnt_list[i][0] = 0;
56bf215546Sopenharmony_ci   h264->field_order_cnt_list[i][1] = 0;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   private->past_ref[i] = NULL;
59bf215546Sopenharmony_ci}
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_civoid vlVaHandlePictureParameterBufferH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
62bf215546Sopenharmony_ci{
63bf215546Sopenharmony_ci   VAPictureParameterBufferH264 *h264 = buf->data;
64bf215546Sopenharmony_ci   struct h264_private *private = &context->h264;
65bf215546Sopenharmony_ci   unsigned int top_or_bottom_field;
66bf215546Sopenharmony_ci   bool is_ref;
67bf215546Sopenharmony_ci   unsigned i;
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   assert(buf->size >= sizeof(VAPictureParameterBufferH264) && buf->num_elements == 1);
70bf215546Sopenharmony_ci   context->desc.h264.slice_count = 0;
71bf215546Sopenharmony_ci   context->desc.h264.priv = private;
72bf215546Sopenharmony_ci   /*CurrPic*/
73bf215546Sopenharmony_ci   context->desc.h264.field_order_cnt[0] = h264->CurrPic.TopFieldOrderCnt;
74bf215546Sopenharmony_ci   context->desc.h264.field_order_cnt[1] = h264->CurrPic.BottomFieldOrderCnt;
75bf215546Sopenharmony_ci   /*ReferenceFrames[16]*/
76bf215546Sopenharmony_ci   /*picture_width_in_mbs_minus1*/
77bf215546Sopenharmony_ci   /*picture_height_in_mbs_minus1*/
78bf215546Sopenharmony_ci   context->desc.h264.pps->sps->bit_depth_luma_minus8 = h264->bit_depth_luma_minus8;
79bf215546Sopenharmony_ci   context->desc.h264.pps->sps->bit_depth_chroma_minus8 = h264->bit_depth_chroma_minus8;
80bf215546Sopenharmony_ci   context->desc.h264.num_ref_frames = h264->num_ref_frames;
81bf215546Sopenharmony_ci   context->desc.h264.pps->sps->chroma_format_idc = h264->seq_fields.bits.chroma_format_idc;
82bf215546Sopenharmony_ci   /*residual_colour_transform_flag*/
83bf215546Sopenharmony_ci   /*gaps_in_frame_num_value_allowed_flag*/
84bf215546Sopenharmony_ci   context->desc.h264.pps->sps->frame_mbs_only_flag =
85bf215546Sopenharmony_ci      h264->seq_fields.bits.frame_mbs_only_flag;
86bf215546Sopenharmony_ci   context->desc.h264.pps->sps->mb_adaptive_frame_field_flag =
87bf215546Sopenharmony_ci      h264->seq_fields.bits.mb_adaptive_frame_field_flag;
88bf215546Sopenharmony_ci   context->desc.h264.pps->sps->direct_8x8_inference_flag =
89bf215546Sopenharmony_ci      h264->seq_fields.bits.direct_8x8_inference_flag;
90bf215546Sopenharmony_ci   context->desc.h264.pps->sps->MinLumaBiPredSize8x8 =
91bf215546Sopenharmony_ci      h264->seq_fields.bits.MinLumaBiPredSize8x8;
92bf215546Sopenharmony_ci   context->desc.h264.pps->sps->log2_max_frame_num_minus4 =
93bf215546Sopenharmony_ci      h264->seq_fields.bits.log2_max_frame_num_minus4;
94bf215546Sopenharmony_ci   context->desc.h264.pps->sps->pic_order_cnt_type =
95bf215546Sopenharmony_ci      h264->seq_fields.bits.pic_order_cnt_type;
96bf215546Sopenharmony_ci   context->desc.h264.pps->sps->log2_max_pic_order_cnt_lsb_minus4 =
97bf215546Sopenharmony_ci      h264->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4;
98bf215546Sopenharmony_ci   context->desc.h264.pps->sps->delta_pic_order_always_zero_flag =
99bf215546Sopenharmony_ci      h264->seq_fields.bits.delta_pic_order_always_zero_flag;
100bf215546Sopenharmony_ci   /*num_slice_groups_minus1*/
101bf215546Sopenharmony_ci   /*slice_group_map_type*/
102bf215546Sopenharmony_ci   /*slice_group_change_rate_minus1*/
103bf215546Sopenharmony_ci   context->desc.h264.pps->pic_init_qp_minus26 =
104bf215546Sopenharmony_ci      h264->pic_init_qp_minus26;
105bf215546Sopenharmony_ci   context->desc.h264.pps->pic_init_qs_minus26 =
106bf215546Sopenharmony_ci      h264->pic_init_qs_minus26;
107bf215546Sopenharmony_ci   context->desc.h264.pps->chroma_qp_index_offset =
108bf215546Sopenharmony_ci      h264->chroma_qp_index_offset;
109bf215546Sopenharmony_ci   context->desc.h264.pps->second_chroma_qp_index_offset =
110bf215546Sopenharmony_ci      h264->second_chroma_qp_index_offset;
111bf215546Sopenharmony_ci   context->desc.h264.pps->entropy_coding_mode_flag =
112bf215546Sopenharmony_ci      h264->pic_fields.bits.entropy_coding_mode_flag;
113bf215546Sopenharmony_ci   context->desc.h264.pps->weighted_pred_flag =
114bf215546Sopenharmony_ci      h264->pic_fields.bits.weighted_pred_flag;
115bf215546Sopenharmony_ci   context->desc.h264.pps->weighted_bipred_idc =
116bf215546Sopenharmony_ci      h264->pic_fields.bits.weighted_bipred_idc;
117bf215546Sopenharmony_ci   context->desc.h264.pps->transform_8x8_mode_flag =
118bf215546Sopenharmony_ci      h264->pic_fields.bits.transform_8x8_mode_flag;
119bf215546Sopenharmony_ci   context->desc.h264.field_pic_flag =
120bf215546Sopenharmony_ci      h264->pic_fields.bits.field_pic_flag;
121bf215546Sopenharmony_ci   context->desc.h264.pps->constrained_intra_pred_flag =
122bf215546Sopenharmony_ci      h264->pic_fields.bits.constrained_intra_pred_flag;
123bf215546Sopenharmony_ci   context->desc.h264.pps->bottom_field_pic_order_in_frame_present_flag =
124bf215546Sopenharmony_ci      h264->pic_fields.bits.pic_order_present_flag;
125bf215546Sopenharmony_ci   context->desc.h264.pps->deblocking_filter_control_present_flag =
126bf215546Sopenharmony_ci      h264->pic_fields.bits.deblocking_filter_control_present_flag;
127bf215546Sopenharmony_ci   context->desc.h264.pps->redundant_pic_cnt_present_flag =
128bf215546Sopenharmony_ci      h264->pic_fields.bits.redundant_pic_cnt_present_flag;
129bf215546Sopenharmony_ci   /*reference_pic_flag*/
130bf215546Sopenharmony_ci   context->desc.h264.frame_num = h264->frame_num;
131bf215546Sopenharmony_ci   context->desc.h264.is_reference = h264->pic_fields.bits.reference_pic_flag;
132bf215546Sopenharmony_ci   context->desc.h264.bottom_field_flag =
133bf215546Sopenharmony_ci      h264->pic_fields.bits.field_pic_flag &&
134bf215546Sopenharmony_ci      (h264->CurrPic.flags & VA_PICTURE_H264_BOTTOM_FIELD) != 0;
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci   if (context->decoder && (context->templat.max_references != context->desc.h264.num_ref_frames)) {
137bf215546Sopenharmony_ci      context->templat.max_references = MIN2(context->desc.h264.num_ref_frames, 16);
138bf215546Sopenharmony_ci      context->decoder->destroy(context->decoder);
139bf215546Sopenharmony_ci      context->decoder = NULL;
140bf215546Sopenharmony_ci   } else if (!context->decoder && context->desc.h264.num_ref_frames > 0)
141bf215546Sopenharmony_ci      context->templat.max_references = MIN2(context->desc.h264.num_ref_frames, 16);
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci   for (i = 0; i < context->templat.max_references; ++i) {
144bf215546Sopenharmony_ci      if ((h264->ReferenceFrames[i].flags & VA_PICTURE_H264_INVALID) ||
145bf215546Sopenharmony_ci          (h264->ReferenceFrames[i].picture_id == VA_INVALID_SURFACE)) {
146bf215546Sopenharmony_ci         resetReferencePictureDesc(&context->desc.h264, i);
147bf215546Sopenharmony_ci         break;
148bf215546Sopenharmony_ci      }
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci      vlVaGetReferenceFrame(drv, h264->ReferenceFrames[i].picture_id, &context->desc.h264.ref[i]);
151bf215546Sopenharmony_ci      vlVaGetPastReferenceFrame(drv, h264->ReferenceFrames[i].picture_id, &private->past_ref[i]);
152bf215546Sopenharmony_ci      context->desc.h264.frame_num_list[i] = h264->ReferenceFrames[i].frame_idx;
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci      top_or_bottom_field = h264->ReferenceFrames[i].flags &
155bf215546Sopenharmony_ci         (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD);
156bf215546Sopenharmony_ci      is_ref = !!(h264->ReferenceFrames[i].flags &
157bf215546Sopenharmony_ci         (VA_PICTURE_H264_SHORT_TERM_REFERENCE | VA_PICTURE_H264_LONG_TERM_REFERENCE));
158bf215546Sopenharmony_ci      context->desc.h264.is_long_term[i] = !!(h264->ReferenceFrames[i].flags &
159bf215546Sopenharmony_ci          VA_PICTURE_H264_LONG_TERM_REFERENCE);
160bf215546Sopenharmony_ci      context->desc.h264.top_is_reference[i] =
161bf215546Sopenharmony_ci         !!(h264->ReferenceFrames[i].flags & VA_PICTURE_H264_TOP_FIELD) ||
162bf215546Sopenharmony_ci         ((!top_or_bottom_field) && is_ref);
163bf215546Sopenharmony_ci      context->desc.h264.bottom_is_reference[i] =
164bf215546Sopenharmony_ci         !!(h264->ReferenceFrames[i].flags & VA_PICTURE_H264_BOTTOM_FIELD) ||
165bf215546Sopenharmony_ci         ((!top_or_bottom_field) && is_ref);
166bf215546Sopenharmony_ci      context->desc.h264.field_order_cnt_list[i][0] =
167bf215546Sopenharmony_ci         top_or_bottom_field != VA_PICTURE_H264_BOTTOM_FIELD ?
168bf215546Sopenharmony_ci         h264->ReferenceFrames[i].TopFieldOrderCnt: INT_MAX;
169bf215546Sopenharmony_ci      context->desc.h264.field_order_cnt_list[i][1] =
170bf215546Sopenharmony_ci         top_or_bottom_field != VA_PICTURE_H264_TOP_FIELD ?
171bf215546Sopenharmony_ci         h264->ReferenceFrames[i].BottomFieldOrderCnt: INT_MAX;
172bf215546Sopenharmony_ci   }
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   /* Make sure remaining elements are clean */
175bf215546Sopenharmony_ci   for (; i < 16; ++i)
176bf215546Sopenharmony_ci      resetReferencePictureDesc(&context->desc.h264, i);
177bf215546Sopenharmony_ci}
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_civoid vlVaHandleIQMatrixBufferH264(vlVaContext *context, vlVaBuffer *buf)
180bf215546Sopenharmony_ci{
181bf215546Sopenharmony_ci   VAIQMatrixBufferH264 *h264 = buf->data;
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci   assert(buf->size >= sizeof(VAIQMatrixBufferH264) && buf->num_elements == 1);
184bf215546Sopenharmony_ci   memcpy(&context->desc.h264.pps->ScalingList4x4, h264->ScalingList4x4, 6 * 16);
185bf215546Sopenharmony_ci   memcpy(&context->desc.h264.pps->ScalingList8x8, h264->ScalingList8x8, 2 * 64);
186bf215546Sopenharmony_ci}
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_civoid vlVaHandleSliceParameterBufferH264(vlVaContext *context, vlVaBuffer *buf)
189bf215546Sopenharmony_ci{
190bf215546Sopenharmony_ci   VASliceParameterBufferH264 *h264 = buf->data;
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci   assert(buf->size >= sizeof(VASliceParameterBufferH264) && buf->num_elements == 1);
193bf215546Sopenharmony_ci   context->desc.h264.slice_count += buf->num_elements;
194bf215546Sopenharmony_ci   context->desc.h264.num_ref_idx_l0_active_minus1 =
195bf215546Sopenharmony_ci      h264->num_ref_idx_l0_active_minus1;
196bf215546Sopenharmony_ci   context->desc.h264.num_ref_idx_l1_active_minus1 =
197bf215546Sopenharmony_ci      h264->num_ref_idx_l1_active_minus1;
198bf215546Sopenharmony_ci}
199