• 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 
vlVaGetPastReferenceFrame(vlVaDriver * drv,VASurfaceID surface_id,struct pipe_video_buffer ** ref_frame)33 static void vlVaGetPastReferenceFrame(vlVaDriver *drv, VASurfaceID surface_id,
34                       struct pipe_video_buffer **ref_frame)
35 {
36    vlVaSurface *surf = handle_table_get(drv->htab, surface_id);
37    if (surf) {
38       *ref_frame = surf->obsolete_buf;
39       surf->obsolete_buf = NULL;
40    }
41    else
42       *ref_frame = NULL;
43 }
44 
resetReferencePictureDesc(struct pipe_h264_picture_desc * h264,unsigned int i)45 static void resetReferencePictureDesc(struct pipe_h264_picture_desc *h264,
46                                       unsigned int i)
47 {
48    struct h264_private *private = h264->priv;
49 
50    h264->ref[i] = NULL;
51    h264->frame_num_list[i] = 0;
52    h264->is_long_term[i] = 0;
53    h264->top_is_reference[i] = 0;
54    h264->bottom_is_reference[i] = 0;
55    h264->field_order_cnt_list[i][0] = 0;
56    h264->field_order_cnt_list[i][1] = 0;
57 
58    private->past_ref[i] = NULL;
59 }
60 
vlVaHandlePictureParameterBufferH264(vlVaDriver * drv,vlVaContext * context,vlVaBuffer * buf)61 void vlVaHandlePictureParameterBufferH264(vlVaDriver *drv, vlVaContext *context, vlVaBuffer *buf)
62 {
63    VAPictureParameterBufferH264 *h264 = buf->data;
64    struct h264_private *private = &context->h264;
65    unsigned int top_or_bottom_field;
66    bool is_ref;
67    unsigned i;
68 
69    assert(buf->size >= sizeof(VAPictureParameterBufferH264) && buf->num_elements == 1);
70    context->desc.h264.slice_count = 0;
71    context->desc.h264.priv = private;
72    /*CurrPic*/
73    context->desc.h264.field_order_cnt[0] = h264->CurrPic.TopFieldOrderCnt;
74    context->desc.h264.field_order_cnt[1] = h264->CurrPic.BottomFieldOrderCnt;
75    /*ReferenceFrames[16]*/
76    /*picture_width_in_mbs_minus1*/
77    /*picture_height_in_mbs_minus1*/
78    context->desc.h264.pps->sps->bit_depth_luma_minus8 = h264->bit_depth_luma_minus8;
79    context->desc.h264.pps->sps->bit_depth_chroma_minus8 = h264->bit_depth_chroma_minus8;
80    context->desc.h264.num_ref_frames = h264->num_ref_frames;
81    context->desc.h264.pps->sps->chroma_format_idc = h264->seq_fields.bits.chroma_format_idc;
82    /*residual_colour_transform_flag*/
83    /*gaps_in_frame_num_value_allowed_flag*/
84    context->desc.h264.pps->sps->frame_mbs_only_flag =
85       h264->seq_fields.bits.frame_mbs_only_flag;
86    context->desc.h264.pps->sps->mb_adaptive_frame_field_flag =
87       h264->seq_fields.bits.mb_adaptive_frame_field_flag;
88    context->desc.h264.pps->sps->direct_8x8_inference_flag =
89       h264->seq_fields.bits.direct_8x8_inference_flag;
90    context->desc.h264.pps->sps->MinLumaBiPredSize8x8 =
91       h264->seq_fields.bits.MinLumaBiPredSize8x8;
92    context->desc.h264.pps->sps->log2_max_frame_num_minus4 =
93       h264->seq_fields.bits.log2_max_frame_num_minus4;
94    context->desc.h264.pps->sps->pic_order_cnt_type =
95       h264->seq_fields.bits.pic_order_cnt_type;
96    context->desc.h264.pps->sps->log2_max_pic_order_cnt_lsb_minus4 =
97       h264->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4;
98    context->desc.h264.pps->sps->delta_pic_order_always_zero_flag =
99       h264->seq_fields.bits.delta_pic_order_always_zero_flag;
100    /*num_slice_groups_minus1*/
101    /*slice_group_map_type*/
102    /*slice_group_change_rate_minus1*/
103    context->desc.h264.pps->pic_init_qp_minus26 =
104       h264->pic_init_qp_minus26;
105    context->desc.h264.pps->pic_init_qs_minus26 =
106       h264->pic_init_qs_minus26;
107    context->desc.h264.pps->chroma_qp_index_offset =
108       h264->chroma_qp_index_offset;
109    context->desc.h264.pps->second_chroma_qp_index_offset =
110       h264->second_chroma_qp_index_offset;
111    context->desc.h264.pps->entropy_coding_mode_flag =
112       h264->pic_fields.bits.entropy_coding_mode_flag;
113    context->desc.h264.pps->weighted_pred_flag =
114       h264->pic_fields.bits.weighted_pred_flag;
115    context->desc.h264.pps->weighted_bipred_idc =
116       h264->pic_fields.bits.weighted_bipred_idc;
117    context->desc.h264.pps->transform_8x8_mode_flag =
118       h264->pic_fields.bits.transform_8x8_mode_flag;
119    context->desc.h264.field_pic_flag =
120       h264->pic_fields.bits.field_pic_flag;
121    context->desc.h264.pps->constrained_intra_pred_flag =
122       h264->pic_fields.bits.constrained_intra_pred_flag;
123    context->desc.h264.pps->bottom_field_pic_order_in_frame_present_flag =
124       h264->pic_fields.bits.pic_order_present_flag;
125    context->desc.h264.pps->deblocking_filter_control_present_flag =
126       h264->pic_fields.bits.deblocking_filter_control_present_flag;
127    context->desc.h264.pps->redundant_pic_cnt_present_flag =
128       h264->pic_fields.bits.redundant_pic_cnt_present_flag;
129    /*reference_pic_flag*/
130    context->desc.h264.frame_num = h264->frame_num;
131    context->desc.h264.is_reference = h264->pic_fields.bits.reference_pic_flag;
132    context->desc.h264.bottom_field_flag =
133       h264->pic_fields.bits.field_pic_flag &&
134       (h264->CurrPic.flags & VA_PICTURE_H264_BOTTOM_FIELD) != 0;
135 
136    if (context->decoder && (context->templat.max_references != context->desc.h264.num_ref_frames)) {
137       context->templat.max_references = MIN2(context->desc.h264.num_ref_frames, 16);
138       context->decoder->destroy(context->decoder);
139       context->decoder = NULL;
140    } else if (!context->decoder && context->desc.h264.num_ref_frames > 0)
141       context->templat.max_references = MIN2(context->desc.h264.num_ref_frames, 16);
142 
143    for (i = 0; i < context->templat.max_references; ++i) {
144       if ((h264->ReferenceFrames[i].flags & VA_PICTURE_H264_INVALID) ||
145           (h264->ReferenceFrames[i].picture_id == VA_INVALID_SURFACE)) {
146          resetReferencePictureDesc(&context->desc.h264, i);
147          break;
148       }
149 
150       vlVaGetReferenceFrame(drv, h264->ReferenceFrames[i].picture_id, &context->desc.h264.ref[i]);
151       vlVaGetPastReferenceFrame(drv, h264->ReferenceFrames[i].picture_id, &private->past_ref[i]);
152       context->desc.h264.frame_num_list[i] = h264->ReferenceFrames[i].frame_idx;
153 
154       top_or_bottom_field = h264->ReferenceFrames[i].flags &
155          (VA_PICTURE_H264_TOP_FIELD | VA_PICTURE_H264_BOTTOM_FIELD);
156       is_ref = !!(h264->ReferenceFrames[i].flags &
157          (VA_PICTURE_H264_SHORT_TERM_REFERENCE | VA_PICTURE_H264_LONG_TERM_REFERENCE));
158       context->desc.h264.is_long_term[i] = !!(h264->ReferenceFrames[i].flags &
159           VA_PICTURE_H264_LONG_TERM_REFERENCE);
160       context->desc.h264.top_is_reference[i] =
161          !!(h264->ReferenceFrames[i].flags & VA_PICTURE_H264_TOP_FIELD) ||
162          ((!top_or_bottom_field) && is_ref);
163       context->desc.h264.bottom_is_reference[i] =
164          !!(h264->ReferenceFrames[i].flags & VA_PICTURE_H264_BOTTOM_FIELD) ||
165          ((!top_or_bottom_field) && is_ref);
166       context->desc.h264.field_order_cnt_list[i][0] =
167          top_or_bottom_field != VA_PICTURE_H264_BOTTOM_FIELD ?
168          h264->ReferenceFrames[i].TopFieldOrderCnt: INT_MAX;
169       context->desc.h264.field_order_cnt_list[i][1] =
170          top_or_bottom_field != VA_PICTURE_H264_TOP_FIELD ?
171          h264->ReferenceFrames[i].BottomFieldOrderCnt: INT_MAX;
172    }
173 
174    /* Make sure remaining elements are clean */
175    for (; i < 16; ++i)
176       resetReferencePictureDesc(&context->desc.h264, i);
177 }
178 
vlVaHandleIQMatrixBufferH264(vlVaContext * context,vlVaBuffer * buf)179 void vlVaHandleIQMatrixBufferH264(vlVaContext *context, vlVaBuffer *buf)
180 {
181    VAIQMatrixBufferH264 *h264 = buf->data;
182 
183    assert(buf->size >= sizeof(VAIQMatrixBufferH264) && buf->num_elements == 1);
184    memcpy(&context->desc.h264.pps->ScalingList4x4, h264->ScalingList4x4, 6 * 16);
185    memcpy(&context->desc.h264.pps->ScalingList8x8, h264->ScalingList8x8, 2 * 64);
186 }
187 
vlVaHandleSliceParameterBufferH264(vlVaContext * context,vlVaBuffer * buf)188 void vlVaHandleSliceParameterBufferH264(vlVaContext *context, vlVaBuffer *buf)
189 {
190    VASliceParameterBufferH264 *h264 = buf->data;
191 
192    assert(buf->size >= sizeof(VASliceParameterBufferH264) && buf->num_elements == 1);
193    context->desc.h264.slice_count += buf->num_elements;
194    context->desc.h264.num_ref_idx_l0_active_minus1 =
195       h264->num_ref_idx_l0_active_minus1;
196    context->desc.h264.num_ref_idx_l1_active_minus1 =
197       h264->num_ref_idx_l1_active_minus1;
198 }
199