1 /**************************************************************************
3 * Copyright 2018 Advanced Micro Devices, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "vl/vl_vlc.h"
29 #include "va_private.h"
31 #define NUM_VP9_REFS 8
33 void vlVaHandlePictureParameterBufferVP9(vlVaDriver
*drv
, vlVaContext
*context
, vlVaBuffer
*buf
)
35 VADecPictureParameterBufferVP9
*vp9
= buf
->data
;
38 assert(buf
->size
>= sizeof(VADecPictureParameterBufferVP9
) && buf
->num_elements
== 1);
40 context
->desc
.vp9
.picture_parameter
.frame_width
= vp9
->frame_width
;
41 context
->desc
.vp9
.picture_parameter
.frame_height
= vp9
->frame_height
;
43 context
->desc
.vp9
.picture_parameter
.pic_fields
.subsampling_x
= vp9
->pic_fields
.bits
.subsampling_x
;
44 context
->desc
.vp9
.picture_parameter
.pic_fields
.subsampling_y
= vp9
->pic_fields
.bits
.subsampling_y
;
45 context
->desc
.vp9
.picture_parameter
.pic_fields
.frame_type
= vp9
->pic_fields
.bits
.frame_type
;
46 context
->desc
.vp9
.picture_parameter
.pic_fields
.show_frame
= vp9
->pic_fields
.bits
.show_frame
;
47 context
->desc
.vp9
.picture_parameter
.pic_fields
.error_resilient_mode
= vp9
->pic_fields
.bits
.error_resilient_mode
;
48 context
->desc
.vp9
.picture_parameter
.pic_fields
.intra_only
= vp9
->pic_fields
.bits
.intra_only
;
49 context
->desc
.vp9
.picture_parameter
.pic_fields
.allow_high_precision_mv
= vp9
->pic_fields
.bits
.allow_high_precision_mv
;
50 context
->desc
.vp9
.picture_parameter
.pic_fields
.mcomp_filter_type
= vp9
->pic_fields
.bits
.mcomp_filter_type
;
51 context
->desc
.vp9
.picture_parameter
.pic_fields
.frame_parallel_decoding_mode
= vp9
->pic_fields
.bits
.frame_parallel_decoding_mode
;
52 context
->desc
.vp9
.picture_parameter
.pic_fields
.reset_frame_context
= vp9
->pic_fields
.bits
.reset_frame_context
;
53 context
->desc
.vp9
.picture_parameter
.pic_fields
.refresh_frame_context
= vp9
->pic_fields
.bits
.refresh_frame_context
;
54 context
->desc
.vp9
.picture_parameter
.pic_fields
.frame_context_idx
= vp9
->pic_fields
.bits
.frame_context_idx
;
55 context
->desc
.vp9
.picture_parameter
.pic_fields
.segmentation_enabled
= vp9
->pic_fields
.bits
.segmentation_enabled
;
56 context
->desc
.vp9
.picture_parameter
.pic_fields
.segmentation_temporal_update
= vp9
->pic_fields
.bits
.segmentation_temporal_update
;
57 context
->desc
.vp9
.picture_parameter
.pic_fields
.segmentation_update_map
= vp9
->pic_fields
.bits
.segmentation_update_map
;
58 context
->desc
.vp9
.picture_parameter
.pic_fields
.last_ref_frame
= vp9
->pic_fields
.bits
.last_ref_frame
;
59 context
->desc
.vp9
.picture_parameter
.pic_fields
.last_ref_frame_sign_bias
= vp9
->pic_fields
.bits
.last_ref_frame_sign_bias
;
60 context
->desc
.vp9
.picture_parameter
.pic_fields
.golden_ref_frame
= vp9
->pic_fields
.bits
.golden_ref_frame
;
61 context
->desc
.vp9
.picture_parameter
.pic_fields
.golden_ref_frame_sign_bias
= vp9
->pic_fields
.bits
.golden_ref_frame_sign_bias
;
62 context
->desc
.vp9
.picture_parameter
.pic_fields
.alt_ref_frame
= vp9
->pic_fields
.bits
.alt_ref_frame
;
63 context
->desc
.vp9
.picture_parameter
.pic_fields
.alt_ref_frame_sign_bias
= vp9
->pic_fields
.bits
.alt_ref_frame_sign_bias
;
64 context
->desc
.vp9
.picture_parameter
.pic_fields
.lossless_flag
= vp9
->pic_fields
.bits
.lossless_flag
;
66 context
->desc
.vp9
.picture_parameter
.filter_level
= vp9
->filter_level
;
67 context
->desc
.vp9
.picture_parameter
.sharpness_level
= vp9
->sharpness_level
;
69 context
->desc
.vp9
.picture_parameter
.log2_tile_rows
= vp9
->log2_tile_rows
;
70 context
->desc
.vp9
.picture_parameter
.log2_tile_columns
= vp9
->log2_tile_columns
;
72 context
->desc
.vp9
.picture_parameter
.frame_header_length_in_bytes
= vp9
->frame_header_length_in_bytes
;
73 context
->desc
.vp9
.picture_parameter
.first_partition_size
= vp9
->first_partition_size
;
75 for (i
= 0; i
< 7; ++i
)
76 context
->desc
.vp9
.picture_parameter
.mb_segment_tree_probs
[i
] = vp9
->mb_segment_tree_probs
[i
];
77 for (i
= 0; i
< 3; ++i
)
78 context
->desc
.vp9
.picture_parameter
.segment_pred_probs
[i
] = vp9
->segment_pred_probs
[i
];
80 context
->desc
.vp9
.picture_parameter
.profile
= vp9
->profile
;
82 context
->desc
.vp9
.picture_parameter
.bit_depth
= vp9
->bit_depth
;
84 for (i
= 0 ; i
< NUM_VP9_REFS
; i
++) {
85 if (vp9
->pic_fields
.bits
.frame_type
== 0)
86 context
->desc
.vp9
.ref
[i
] = NULL
;
88 vlVaGetReferenceFrame(drv
, vp9
->reference_frames
[i
], &context
->desc
.vp9
.ref
[i
]);
91 if (!context
->decoder
&& !context
->templat
.max_references
)
92 context
->templat
.max_references
= NUM_VP9_REFS
;
95 void vlVaHandleSliceParameterBufferVP9(vlVaContext
*context
, vlVaBuffer
*buf
)
97 VASliceParameterBufferVP9
*vp9
= buf
->data
;
100 assert(buf
->size
>= sizeof(VASliceParameterBufferVP9
) && buf
->num_elements
== 1);
102 context
->desc
.vp9
.slice_parameter
.slice_data_size
= vp9
->slice_data_size
;
103 context
->desc
.vp9
.slice_parameter
.slice_data_offset
= vp9
->slice_data_offset
;
104 context
->desc
.vp9
.slice_parameter
.slice_data_flag
= vp9
->slice_data_flag
;
106 for (i
= 0; i
< 8; ++i
) {
107 context
->desc
.vp9
.slice_parameter
.seg_param
[i
].segment_flags
.segment_reference_enabled
=
108 vp9
->seg_param
[i
].segment_flags
.fields
.segment_reference_enabled
;
109 context
->desc
.vp9
.slice_parameter
.seg_param
[i
].segment_flags
.segment_reference
=
110 vp9
->seg_param
[i
].segment_flags
.fields
.segment_reference
;
111 context
->desc
.vp9
.slice_parameter
.seg_param
[i
].segment_flags
.segment_reference_skipped
=
112 vp9
->seg_param
[i
].segment_flags
.fields
.segment_reference_skipped
;
114 memcpy(context
->desc
.vp9
.slice_parameter
.seg_param
[i
].filter_level
, vp9
->seg_param
[i
].filter_level
, 4 * 2);
116 context
->desc
.vp9
.slice_parameter
.seg_param
[i
].luma_ac_quant_scale
= vp9
->seg_param
[i
].luma_ac_quant_scale
;
117 context
->desc
.vp9
.slice_parameter
.seg_param
[i
].luma_dc_quant_scale
= vp9
->seg_param
[i
].luma_dc_quant_scale
;
118 context
->desc
.vp9
.slice_parameter
.seg_param
[i
].chroma_ac_quant_scale
= vp9
->seg_param
[i
].chroma_ac_quant_scale
;
119 context
->desc
.vp9
.slice_parameter
.seg_param
[i
].chroma_dc_quant_scale
= vp9
->seg_param
[i
].chroma_dc_quant_scale
;
123 static unsigned vp9_u(struct vl_vlc
*vlc
, unsigned n
)
125 unsigned valid
= vl_vlc_valid_bits(vlc
);
131 vl_vlc_fillbits(vlc
);
133 return vl_vlc_get_uimsbf(vlc
, n
);
136 static signed vp9_s(struct vl_vlc
*vlc
, unsigned n
)
147 static void bitdepth_colorspace_sampling(struct vl_vlc
*vlc
, unsigned profile
)
161 static void frame_size(struct vl_vlc
*vlc
)
163 /* width_minus_one */
165 /* height_minus_one */
170 /* render_width_minus_one */
172 /* render_height_minus_one */
177 void vlVaDecoderVP9BitstreamHeader(vlVaContext
*context
, vlVaBuffer
*buf
)
181 bool frame_type
, show_frame
, error_resilient_mode
;
182 bool mode_ref_delta_enabled
, mode_ref_delta_update
= false;
185 vl_vlc_init(&vlc
, 1, (const void * const*)&buf
->data
,
186 (const unsigned *)&context
->desc
.vp9
.picture_parameter
.frame_header_length_in_bytes
);
189 if (vp9_u(&vlc
, 2) != 0x2)
192 profile
= vp9_u(&vlc
, 1) | vp9_u(&vlc
, 1) << 1;
195 profile
+= vp9_u(&vlc
, 1);
197 if (profile
!= 0 && profile
!= 2)
200 /* show_existing_frame */
204 frame_type
= vp9_u(&vlc
, 1);
205 show_frame
= vp9_u(&vlc
, 1);
206 error_resilient_mode
= vp9_u(&vlc
, 1);
208 if (frame_type
== 0) {
210 if (vp9_u(&vlc
, 24) != 0x498342)
213 bitdepth_colorspace_sampling(&vlc
, profile
);
216 bool intra_only
, size_in_refs
= false;
218 intra_only
= show_frame
? 0 : vp9_u(&vlc
, 1);
219 if (!error_resilient_mode
)
220 /* reset_frame_context */
225 if (vp9_u(&vlc
, 24) != 0x498342)
228 bitdepth_colorspace_sampling(&vlc
, profile
);
229 /* refresh_frame_flags */
233 /* refresh_frame_flags */
236 for (i
= 0; i
< 3; ++i
) {
242 for (i
= 0; i
< 3; ++i
) {
243 size_in_refs
= vp9_u(&vlc
, 1);
249 /* width/height_minus_one */
254 if (vp9_u(&vlc
, 1)) {
255 /* render_width/height_minus_one */
260 /* high_precision_mv */
262 /* filter_switchable */
268 if (!error_resilient_mode
) {
269 /* refresh_frame_context */
271 /* frame_parallel_decoding_mode */
274 /* frame_context_index */
281 /* sharpness_level */
284 mode_ref_delta_enabled
= vp9_u(&vlc
, 1);
285 if (mode_ref_delta_enabled
) {
286 mode_ref_delta_update
= vp9_u(&vlc
, 1);
287 if (mode_ref_delta_update
) {
288 for (i
= 0; i
< 4; ++i
) {
289 /* update_ref_delta */
294 for (i
= 0; i
< 2; ++i
) {
295 /* update_mode_delta */
302 context
->desc
.vp9
.picture_parameter
.mode_ref_delta_enabled
= mode_ref_delta_enabled
;
303 context
->desc
.vp9
.picture_parameter
.mode_ref_delta_update
= mode_ref_delta_update
;
307 context
->desc
.vp9
.picture_parameter
.base_qindex
= vp9_u(&vlc
, 8);
308 context
->desc
.vp9
.picture_parameter
.y_dc_delta_q
= vp9_u(&vlc
, 1) ? vp9_s(&vlc
, 4) : 0;
309 context
->desc
.vp9
.picture_parameter
.uv_ac_delta_q
= vp9_u(&vlc
, 1) ? vp9_s(&vlc
, 4) : 0;
310 context
->desc
.vp9
.picture_parameter
.uv_dc_delta_q
= vp9_u(&vlc
, 1) ? vp9_s(&vlc
, 4) : 0;
319 if (vp9_u(&vlc
, 1)) {
320 for (i
= 0; i
< 7; ++i
) {
322 if (vp9_u(&vlc
, 1)) {
328 /* temporal_update */
329 if (vp9_u(&vlc
, 1)) {
330 for (i
= 0; i
< 3; ++i
) {
340 if (vp9_u(&vlc
, 1)) {
343 for (i
= 0; i
< 8; ++i
) {
344 /* Use alternate quantizer */
345 if ((context
->desc
.vp9
.slice_parameter
.seg_param
[i
].alt_quant_enabled
= vp9_u(&vlc
, 1)))
346 context
->desc
.vp9
.slice_parameter
.seg_param
[i
].alt_quant
= vp9_s(&vlc
, 8);
347 /* Use alternate loop filter value */
348 if ((context
->desc
.vp9
.slice_parameter
.seg_param
[i
].alt_lf_enabled
= vp9_u(&vlc
, 1)))
349 context
->desc
.vp9
.slice_parameter
.seg_param
[i
].alt_lf
= vp9_s(&vlc
, 6);
350 /* Optional Segment reference frame */
353 /* Optional Segment skip mode */