1 /**************************************************************************
3 * Copyright 2013 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 **************************************************************************/
30 * Christian König <christian.koenig@amd.com>
36 #include "pipe/p_video_codec.h"
38 #include "util/u_video.h"
39 #include "util/u_memory.h"
41 #include "vl/vl_video_buffer.h"
43 #include "../../winsys/radeon/drm/radeon_winsys.h"
44 #include "radeon_video.h"
45 #include "radeon_vce.h"
47 static void session(struct rvce_encoder
*enc
)
49 RVCE_BEGIN(0x00000001); // session cmd
50 RVCE_CS(enc
->stream_handle
);
54 static void task_info(struct rvce_encoder
*enc
, uint32_t taskOperation
)
56 RVCE_BEGIN(0x00000002); // task info
57 RVCE_CS(0xffffffff); // offsetOfNextTaskInfo
58 RVCE_CS(taskOperation
); // taskOperation
59 RVCE_CS(0x00000000); // referencePictureDependency
60 RVCE_CS(0x00000000); // collocateFlagDependency
61 RVCE_CS(0x00000000); // feedbackIndex
62 RVCE_CS(0x00000000); // videoBitstreamRingIndex
66 static void feedback(struct rvce_encoder
*enc
)
68 RVCE_BEGIN(0x05000005); // feedback buffer
69 RVCE_WRITE(enc
->fb
->cs_handle
, enc
->fb
->domain
); // feedbackRingAddressHi
70 RVCE_CS(0x00000000); // feedbackRingAddressLo
71 RVCE_CS(0x00000001); // feedbackRingSize
75 static void create(struct rvce_encoder
*enc
)
77 task_info(enc
, 0x00000000);
79 RVCE_BEGIN(0x01000001); // create cmd
80 RVCE_CS(0x00000000); // encUseCircularBuffer
81 RVCE_CS(0x00000041); // encProfile
82 RVCE_CS(0x0000000a); // encLevel
83 RVCE_CS(0x00000000); // encPicStructRestriction
84 RVCE_CS(enc
->base
.width
); // encImageWidth
85 RVCE_CS(enc
->base
.height
); // encImageHeight
86 RVCE_CS(enc
->luma
->level
[0].pitch_bytes
); // encRefPicLumaPitch
87 RVCE_CS(enc
->chroma
->level
[0].pitch_bytes
); // encRefPicChromaPitch
88 RVCE_CS(align(enc
->luma
->npix_y
, 16) / 8); // encRefYHeightInQw
89 RVCE_CS(0x00000000); // encRefPic(Addr|Array)Mode, encPicStructRestriction, disableRDO
93 static void rate_control(struct rvce_encoder
*enc
)
95 RVCE_BEGIN(0x04000005); // rate control
96 RVCE_CS(enc
->pic
.rate_ctrl
.rate_ctrl_method
); // encRateControlMethod
97 RVCE_CS(enc
->pic
.rate_ctrl
.target_bitrate
); // encRateControlTargetBitRate
98 RVCE_CS(enc
->pic
.rate_ctrl
.peak_bitrate
); // encRateControlPeakBitRate
99 RVCE_CS(enc
->pic
.rate_ctrl
.frame_rate_num
); // encRateControlFrameRateNum
100 RVCE_CS(0x00000000); // encGOPSize
101 RVCE_CS(enc
->pic
.quant_i_frames
); // encQP_I
102 RVCE_CS(enc
->pic
.quant_p_frames
); // encQP_P
103 RVCE_CS(enc
->pic
.quant_b_frames
); // encQP_B
104 RVCE_CS(enc
->pic
.rate_ctrl
.vbv_buffer_size
); // encVBVBufferSize
105 RVCE_CS(enc
->pic
.rate_ctrl
.frame_rate_den
); // encRateControlFrameRateDen
106 RVCE_CS(0x00000000); // encVBVBufferLevel
107 RVCE_CS(0x00000000); // encMaxAUSize
108 RVCE_CS(0x00000000); // encQPInitialMode
109 RVCE_CS(enc
->pic
.rate_ctrl
.target_bits_picture
); // encTargetBitsPerPicture
110 RVCE_CS(enc
->pic
.rate_ctrl
.peak_bits_picture_integer
); // encPeakBitsPerPictureInteger
111 RVCE_CS(enc
->pic
.rate_ctrl
.peak_bits_picture_fraction
); // encPeakBitsPerPictureFractional
112 RVCE_CS(0x00000000); // encMinQP
113 RVCE_CS(0x00000033); // encMaxQP
114 RVCE_CS(0x00000000); // encSkipFrameEnable
115 RVCE_CS(0x00000000); // encFillerDataEnable
116 RVCE_CS(0x00000000); // encEnforceHRD
117 RVCE_CS(0x00000000); // encBPicsDeltaQP
118 RVCE_CS(0x00000000); // encReferenceBPicsDeltaQP
119 RVCE_CS(0x00000000); // encRateControlReInitDisable
123 static void config_extension(struct rvce_encoder
*enc
)
125 RVCE_BEGIN(0x04000001); // config extension
126 RVCE_CS(0x00000003); // encEnablePerfLogging
130 static void pic_control(struct rvce_encoder
*enc
)
132 unsigned encNumMBsPerSlice
;
134 encNumMBsPerSlice
= align(enc
->base
.width
, 16) / 16;
135 encNumMBsPerSlice
*= align(enc
->base
.height
, 16) / 16;
137 RVCE_BEGIN(0x04000002); // pic control
138 RVCE_CS(0x00000000); // encUseConstrainedIntraPred
139 RVCE_CS(0x00000000); // encCABACEnable
140 RVCE_CS(0x00000000); // encCABACIDC
141 RVCE_CS(0x00000000); // encLoopFilterDisable
142 RVCE_CS(0x00000000); // encLFBetaOffset
143 RVCE_CS(0x00000000); // encLFAlphaC0Offset
144 RVCE_CS(0x00000000); // encCropLeftOffset
145 RVCE_CS((align(enc
->base
.width
, 16) - enc
->base
.width
) >> 1); // encCropRightOffset
146 RVCE_CS(0x00000000); // encCropTopOffset
147 RVCE_CS((align(enc
->base
.height
, 16) - enc
->base
.height
) >> 1); // encCropBottomOffset
148 RVCE_CS(encNumMBsPerSlice
); // encNumMBsPerSlice
149 RVCE_CS(0x00000000); // encIntraRefreshNumMBsPerSlot
150 RVCE_CS(0x00000000); // encForceIntraRefresh
151 RVCE_CS(0x00000000); // encForceIMBPeriod
152 RVCE_CS(0x00000000); // encPicOrderCntType
153 RVCE_CS(0x00000000); // log2_max_pic_order_cnt_lsb_minus4
154 RVCE_CS(0x00000000); // encSPSID
155 RVCE_CS(0x00000000); // encPPSID
156 RVCE_CS(0x00000040); // encConstraintSetFlags
157 RVCE_CS(0x00000000); // encBPicPattern
158 RVCE_CS(0x00000000); // weightPredModeBPicture
159 RVCE_CS(0x00000001); // encNumberOfReferenceFrames
160 RVCE_CS(0x00000001); // encMaxNumRefFrames
161 RVCE_CS(0x00000000); // encNumDefaultActiveRefL0
162 RVCE_CS(0x00000000); // encNumDefaultActiveRefL1
163 RVCE_CS(0x00000000); // encSliceMode
164 RVCE_CS(0x00000000); // encMaxSliceSize
168 static void motion_estimation(struct rvce_encoder
*enc
)
170 RVCE_BEGIN(0x04000007); // motion estimation
171 RVCE_CS(0x00000001); // encIMEDecimationSearch
172 RVCE_CS(0x00000001); // motionEstHalfPixel
173 RVCE_CS(0x00000000); // motionEstQuarterPixel
174 RVCE_CS(0x00000000); // disableFavorPMVPoint
175 RVCE_CS(0x00000000); // forceZeroPointCenter
176 RVCE_CS(0x00000000); // LSMVert
177 RVCE_CS(0x00000010); // encSearchRangeX
178 RVCE_CS(0x00000010); // encSearchRangeY
179 RVCE_CS(0x00000010); // encSearch1RangeX
180 RVCE_CS(0x00000010); // encSearch1RangeY
181 RVCE_CS(0x00000000); // disable16x16Frame1
182 RVCE_CS(0x00000000); // disableSATD
183 RVCE_CS(0x00000000); // enableAMD
184 RVCE_CS(0x000000fe); // encDisableSubMode
185 RVCE_CS(0x00000000); // encIMESkipX
186 RVCE_CS(0x00000000); // encIMESkipY
187 RVCE_CS(0x00000000); // encEnImeOverwDisSubm
188 RVCE_CS(0x00000000); // encImeOverwDisSubmNo
189 RVCE_CS(0x00000001); // encIME2SearchRangeX
190 RVCE_CS(0x00000001); // encIME2SearchRangeY
191 RVCE_CS(0x00000000); // parallelModeSpeedupEnable
192 RVCE_CS(0x00000000); // fme0_encDisableSubMode
193 RVCE_CS(0x00000000); // fme1_encDisableSubMode
194 RVCE_CS(0x00000000); // imeSWSpeedupEnable
198 static void rdo(struct rvce_encoder
*enc
)
200 RVCE_BEGIN(0x04000008); // rdo
201 RVCE_CS(0x00000000); // encDisableTbePredIFrame
202 RVCE_CS(0x00000000); // encDisableTbePredPFrame
203 RVCE_CS(0x00000000); // useFmeInterpolY
204 RVCE_CS(0x00000000); // useFmeInterpolUV
205 RVCE_CS(0x00000000); // useFmeIntrapolY
206 RVCE_CS(0x00000000); // useFmeIntrapolUV
207 RVCE_CS(0x00000000); // useFmeInterpolY_1
208 RVCE_CS(0x00000000); // useFmeInterpolUV_1
209 RVCE_CS(0x00000000); // useFmeIntrapolY_1
210 RVCE_CS(0x00000000); // useFmeIntrapolUV_1
211 RVCE_CS(0x00000000); // enc16x16CostAdj
212 RVCE_CS(0x00000000); // encSkipCostAdj
213 RVCE_CS(0x00000000); // encForce16x16skip
214 RVCE_CS(0x00000000); // encDisableThresholdCalcA
215 RVCE_CS(0x00000000); // encLumaCoeffCost
216 RVCE_CS(0x00000000); // encLumaMBCoeffCost
217 RVCE_CS(0x00000000); // encChromaCoeffCost
221 static void frame_offset(struct rvce_encoder
*enc
, unsigned frame_num
,
222 unsigned *luma_offset
, unsigned *chroma_offset
)
224 unsigned pitch
= align(enc
->luma
->level
[0].pitch_bytes
, 128);
225 unsigned vpitch
= align(enc
->luma
->npix_y
, 16);
226 unsigned fsize
= pitch
* (vpitch
+ vpitch
/ 2);
227 unsigned base_offset
= RVCE_NUM_CPB_EXTRA_FRAMES
* fsize
;
229 *luma_offset
= base_offset
+ (frame_num
% RVCE_NUM_CPB_FRAMES
) * fsize
;
230 *chroma_offset
= *luma_offset
+ pitch
* vpitch
;
233 static void encode(struct rvce_encoder
*enc
)
236 unsigned luma_offset
, chroma_offset
;
238 task_info(enc
, 0x00000003);
240 RVCE_BEGIN(0x05000001); // context buffer
241 RVCE_READWRITE(enc
->cpb
.cs_handle
, enc
->cpb
.domain
); // encodeContextAddressHi
242 RVCE_CS(0x00000000); // encodeContextAddressLo
245 RVCE_BEGIN(0x05000004); // video bitstream buffer
246 RVCE_WRITE(enc
->bs_handle
, RADEON_DOMAIN_GTT
); // videoBitstreamRingAddressHi
247 RVCE_CS(0x00000000); // videoBitstreamRingAddressLo
248 RVCE_CS(enc
->bs_size
); // videoBitstreamRingSize
251 RVCE_BEGIN(0x03000001); // encode
252 RVCE_CS(0x00000000); // insertHeaders
253 RVCE_CS(0x00000000); // pictureStructure
254 RVCE_CS(enc
->bs_size
); // allowedMaxBitstreamSize
255 RVCE_CS(0x00000000); // forceRefreshMap
256 RVCE_CS(0x00000000); // insertAUD
257 RVCE_CS(0x00000000); // endOfSequence
258 RVCE_CS(0x00000000); // endOfStream
259 RVCE_READ(enc
->handle
, RADEON_DOMAIN_VRAM
); // inputPictureLumaAddressHi
260 RVCE_CS(enc
->luma
->level
[0].offset
); // inputPictureLumaAddressLo
261 RVCE_READ(enc
->handle
, RADEON_DOMAIN_VRAM
); // inputPictureChromaAddressHi
262 RVCE_CS(enc
->chroma
->level
[0].offset
); // inputPictureChromaAddressLo
263 RVCE_CS(align(enc
->luma
->npix_y
, 16)); // encInputFrameYPitch
264 RVCE_CS(enc
->luma
->level
[0].pitch_bytes
); // encInputPicLumaPitch
265 RVCE_CS(enc
->chroma
->level
[0].pitch_bytes
); // encInputPicChromaPitch
266 RVCE_CS(0x00000000); // encInputPic(Addr|Array)Mode
267 RVCE_CS(0x00000000); // encInputPicTileConfig
268 RVCE_CS(enc
->pic
.picture_type
); // encPicType
269 RVCE_CS(enc
->pic
.picture_type
== 3); // encIdrFlag
270 RVCE_CS(0x00000000); // encIdrPicId
271 RVCE_CS(0x00000000); // encMGSKeyPic
272 RVCE_CS(0x00000001); // encReferenceFlag
273 RVCE_CS(0x00000000); // encTemporalLayerIndex
274 RVCE_CS(0x00000000); // num_ref_idx_active_override_flag
275 RVCE_CS(0x00000000); // num_ref_idx_l0_active_minus1
276 RVCE_CS(0x00000000); // num_ref_idx_l1_active_minus1
277 for (i
= 0; i
< 4; ++i
) {
278 RVCE_CS(0x00000000); // encRefListModificationOp
279 RVCE_CS(0x00000000); // encRefListModificationNum
281 for (i
= 0; i
< 4; ++i
) {
282 RVCE_CS(0x00000000); // encDecodedPictureMarkingOp
283 RVCE_CS(0x00000000); // encDecodedPictureMarkingNum
284 RVCE_CS(0x00000000); // encDecodedPictureMarkingIdx
286 for (i
= 0; i
< 4; ++i
) {
287 RVCE_CS(0x00000000); // encDecodedRefBasePictureMarkingOp
288 RVCE_CS(0x00000000); // encDecodedRefBasePictureMarkingNum
291 RVCE_CS(0x00000000); // pictureStructure
293 if (enc
->pic
.picture_type
== PIPE_H264_ENC_PICTURE_TYPE_IDR
) {
294 RVCE_CS(0x00000000); // encPicType
295 RVCE_CS(0x00000000); // frameNumber
296 RVCE_CS(0x00000000); // pictureOrderCount
297 RVCE_CS(0xffffffff); // lumaOffset
298 RVCE_CS(0xffffffff); // chromaOffset
300 else if(enc
->pic
.picture_type
== PIPE_H264_ENC_PICTURE_TYPE_P
) {
301 frame_offset(enc
, enc
->pic
.frame_num
- 1, &luma_offset
, &chroma_offset
);
302 RVCE_CS(0x00000000); // encPicType
303 RVCE_CS(enc
->pic
.frame_num
- 1); // frameNumber
304 RVCE_CS(enc
->pic
.frame_num
- 1); // pictureOrderCount
305 RVCE_CS(luma_offset
); // lumaOffset
306 RVCE_CS(chroma_offset
); // chromaOffset
308 for (i
= 0; i
< 2; ++i
) {
309 RVCE_CS(0x00000000); // pictureStructure
310 RVCE_CS(0x00000000); // encPicType
311 RVCE_CS(0x00000000); // frameNumber
312 RVCE_CS(0x00000000); // pictureOrderCount
313 RVCE_CS(0xffffffff); // lumaOffset
314 RVCE_CS(0xffffffff); // chromaOffset
317 frame_offset(enc
, enc
->pic
.frame_num
, &luma_offset
, &chroma_offset
);
318 RVCE_CS(luma_offset
); // encReconstructedLumaOffset
319 RVCE_CS(chroma_offset
); // encReconstructedChromaOffset
320 RVCE_CS(0x00000000); // encColocBufferOffset
321 RVCE_CS(0x00000000); // encReconstructedRefBasePictureLumaOffset
322 RVCE_CS(0x00000000); // encReconstructedRefBasePictureChromaOffset
323 RVCE_CS(0x00000000); // encReferenceRefBasePictureLumaOffset
324 RVCE_CS(0x00000000); // encReferenceRefBasePictureChromaOffset
325 RVCE_CS(0x00000000); // pictureCount
326 RVCE_CS(0x00000000); // frameNumber
327 RVCE_CS(0x00000000); // pictureOrderCount
328 RVCE_CS(0x00000000); // numIPicRemainInRCGOP
329 RVCE_CS(0x00000000); // numPPicRemainInRCGOP
330 RVCE_CS(0x00000000); // numBPicRemainInRCGOP
331 RVCE_CS(0x00000000); // numIRPicRemainInRCGOP
332 RVCE_CS(0x00000000); // enableIntraRefresh
336 static void destroy(struct rvce_encoder
*enc
)
338 task_info(enc
, 0x00000001);
340 RVCE_BEGIN(0x02000001); // destroy
344 void radeon_vce_40_2_2_init(struct rvce_encoder
*enc
)
346 enc
->session
= session
;
347 enc
->create
= create
;
348 enc
->feedback
= feedback
;
349 enc
->rate_control
= rate_control
;
350 enc
->config_extension
= config_extension
;
351 enc
->pic_control
= pic_control
;
352 enc
->motion_estimation
= motion_estimation
;
354 enc
->encode
= encode
;
355 enc
->destroy
= destroy
;