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