radeon/vce: remove RVCE_NUM_CPB_EXTRA_FRAMES
[mesa.git] / src / gallium / drivers / radeon / radeon_vce_40_2_2.c
1 /**************************************************************************
2 *
3 * Copyright 2013 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Christian König <christian.koenig@amd.com>
31 *
32 */
33
34 #include <stdio.h>
35
36 #include "pipe/p_video_codec.h"
37
38 #include "util/u_video.h"
39 #include "util/u_memory.h"
40
41 #include "vl/vl_video_buffer.h"
42
43 #include "../../winsys/radeon/drm/radeon_winsys.h"
44 #include "radeon_video.h"
45 #include "radeon_vce.h"
46
47 static void session(struct rvce_encoder *enc)
48 {
49 RVCE_BEGIN(0x00000001); // session cmd
50 RVCE_CS(enc->stream_handle);
51 RVCE_END();
52 }
53
54 static void task_info(struct rvce_encoder *enc, uint32_t taskOperation)
55 {
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
63 RVCE_END();
64 }
65
66 static void feedback(struct rvce_encoder *enc)
67 {
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
72 RVCE_END();
73 }
74
75 static void create(struct rvce_encoder *enc)
76 {
77 task_info(enc, 0x00000000);
78
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
90 RVCE_END();
91 }
92
93 static void rate_control(struct rvce_encoder *enc)
94 {
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
120 RVCE_END();
121 }
122
123 static void config_extension(struct rvce_encoder *enc)
124 {
125 RVCE_BEGIN(0x04000001); // config extension
126 RVCE_CS(0x00000003); // encEnablePerfLogging
127 RVCE_END();
128 }
129
130 static void pic_control(struct rvce_encoder *enc)
131 {
132 unsigned encNumMBsPerSlice;
133
134 encNumMBsPerSlice = align(enc->base.width, 16) / 16;
135 encNumMBsPerSlice *= align(enc->base.height, 16) / 16;
136
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
165 RVCE_END();
166 }
167
168 static void motion_estimation(struct rvce_encoder *enc)
169 {
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
195 RVCE_END();
196 }
197
198 static void rdo(struct rvce_encoder *enc)
199 {
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
218 RVCE_END();
219 }
220
221 static void frame_offset(struct rvce_encoder *enc, unsigned frame_num,
222 unsigned *luma_offset, unsigned *chroma_offset)
223 {
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
228 *luma_offset = (frame_num % RVCE_NUM_CPB_FRAMES) * fsize;
229 *chroma_offset = *luma_offset + pitch * vpitch;
230 }
231
232 static void encode(struct rvce_encoder *enc)
233 {
234 int i;
235 unsigned luma_offset, chroma_offset;
236
237 task_info(enc, 0x00000003);
238
239 RVCE_BEGIN(0x05000001); // context buffer
240 RVCE_READWRITE(enc->cpb.cs_handle, enc->cpb.domain); // encodeContextAddressHi
241 RVCE_CS(0x00000000); // encodeContextAddressLo
242 RVCE_END();
243
244 RVCE_BEGIN(0x05000004); // video bitstream buffer
245 RVCE_WRITE(enc->bs_handle, RADEON_DOMAIN_GTT); // videoBitstreamRingAddressHi
246 RVCE_CS(0x00000000); // videoBitstreamRingAddressLo
247 RVCE_CS(enc->bs_size); // videoBitstreamRingSize
248 RVCE_END();
249
250 RVCE_BEGIN(0x03000001); // encode
251 RVCE_CS(0x00000000); // insertHeaders
252 RVCE_CS(0x00000000); // pictureStructure
253 RVCE_CS(enc->bs_size); // allowedMaxBitstreamSize
254 RVCE_CS(0x00000000); // forceRefreshMap
255 RVCE_CS(0x00000000); // insertAUD
256 RVCE_CS(0x00000000); // endOfSequence
257 RVCE_CS(0x00000000); // endOfStream
258 RVCE_READ(enc->handle, RADEON_DOMAIN_VRAM); // inputPictureLumaAddressHi
259 RVCE_CS(enc->luma->level[0].offset); // inputPictureLumaAddressLo
260 RVCE_READ(enc->handle, RADEON_DOMAIN_VRAM); // inputPictureChromaAddressHi
261 RVCE_CS(enc->chroma->level[0].offset); // inputPictureChromaAddressLo
262 RVCE_CS(align(enc->luma->npix_y, 16)); // encInputFrameYPitch
263 RVCE_CS(enc->luma->level[0].pitch_bytes); // encInputPicLumaPitch
264 RVCE_CS(enc->chroma->level[0].pitch_bytes); // encInputPicChromaPitch
265 RVCE_CS(0x00000000); // encInputPic(Addr|Array)Mode
266 RVCE_CS(0x00000000); // encInputPicTileConfig
267 RVCE_CS(enc->pic.picture_type); // encPicType
268 RVCE_CS(enc->pic.picture_type == 3); // encIdrFlag
269 RVCE_CS(0x00000000); // encIdrPicId
270 RVCE_CS(0x00000000); // encMGSKeyPic
271 RVCE_CS(0x00000001); // encReferenceFlag
272 RVCE_CS(0x00000000); // encTemporalLayerIndex
273 RVCE_CS(0x00000000); // num_ref_idx_active_override_flag
274 RVCE_CS(0x00000000); // num_ref_idx_l0_active_minus1
275 RVCE_CS(0x00000000); // num_ref_idx_l1_active_minus1
276 for (i = 0; i < 4; ++i) {
277 RVCE_CS(0x00000000); // encRefListModificationOp
278 RVCE_CS(0x00000000); // encRefListModificationNum
279 }
280 for (i = 0; i < 4; ++i) {
281 RVCE_CS(0x00000000); // encDecodedPictureMarkingOp
282 RVCE_CS(0x00000000); // encDecodedPictureMarkingNum
283 RVCE_CS(0x00000000); // encDecodedPictureMarkingIdx
284 }
285 for (i = 0; i < 4; ++i) {
286 RVCE_CS(0x00000000); // encDecodedRefBasePictureMarkingOp
287 RVCE_CS(0x00000000); // encDecodedRefBasePictureMarkingNum
288 }
289
290 RVCE_CS(0x00000000); // pictureStructure
291
292 if (enc->pic.picture_type == PIPE_H264_ENC_PICTURE_TYPE_IDR) {
293 RVCE_CS(0x00000000); // encPicType
294 RVCE_CS(0x00000000); // frameNumber
295 RVCE_CS(0x00000000); // pictureOrderCount
296 RVCE_CS(0xffffffff); // lumaOffset
297 RVCE_CS(0xffffffff); // chromaOffset
298 }
299 else if(enc->pic.picture_type == PIPE_H264_ENC_PICTURE_TYPE_P) {
300 frame_offset(enc, enc->pic.frame_num - 1, &luma_offset, &chroma_offset);
301 RVCE_CS(0x00000000); // encPicType
302 RVCE_CS(enc->pic.frame_num - 1); // frameNumber
303 RVCE_CS(enc->pic.frame_num - 1); // pictureOrderCount
304 RVCE_CS(luma_offset); // lumaOffset
305 RVCE_CS(chroma_offset); // chromaOffset
306 }
307 for (i = 0; i < 2; ++i) {
308 RVCE_CS(0x00000000); // pictureStructure
309 RVCE_CS(0x00000000); // encPicType
310 RVCE_CS(0x00000000); // frameNumber
311 RVCE_CS(0x00000000); // pictureOrderCount
312 RVCE_CS(0xffffffff); // lumaOffset
313 RVCE_CS(0xffffffff); // chromaOffset
314 }
315
316 frame_offset(enc, enc->pic.frame_num, &luma_offset, &chroma_offset);
317 RVCE_CS(luma_offset); // encReconstructedLumaOffset
318 RVCE_CS(chroma_offset); // encReconstructedChromaOffset
319 RVCE_CS(0x00000000); // encColocBufferOffset
320 RVCE_CS(0x00000000); // encReconstructedRefBasePictureLumaOffset
321 RVCE_CS(0x00000000); // encReconstructedRefBasePictureChromaOffset
322 RVCE_CS(0x00000000); // encReferenceRefBasePictureLumaOffset
323 RVCE_CS(0x00000000); // encReferenceRefBasePictureChromaOffset
324 RVCE_CS(0x00000000); // pictureCount
325 RVCE_CS(0x00000000); // frameNumber
326 RVCE_CS(0x00000000); // pictureOrderCount
327 RVCE_CS(0x00000000); // numIPicRemainInRCGOP
328 RVCE_CS(0x00000000); // numPPicRemainInRCGOP
329 RVCE_CS(0x00000000); // numBPicRemainInRCGOP
330 RVCE_CS(0x00000000); // numIRPicRemainInRCGOP
331 RVCE_CS(0x00000000); // enableIntraRefresh
332 RVCE_END();
333 }
334
335 static void destroy(struct rvce_encoder *enc)
336 {
337 task_info(enc, 0x00000001);
338
339 RVCE_BEGIN(0x02000001); // destroy
340 RVCE_END();
341 }
342
343 void radeon_vce_40_2_2_init(struct rvce_encoder *enc)
344 {
345 enc->session = session;
346 enc->create = create;
347 enc->feedback = feedback;
348 enc->rate_control = rate_control;
349 enc->config_extension = config_extension;
350 enc->pic_control = pic_control;
351 enc->motion_estimation = motion_estimation;
352 enc->rdo = rdo;
353 enc->encode = encode;
354 enc->destroy = destroy;
355 }