radeonsi: rename enable_s3tc -> enable_compressed_formats
[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 "r600_pipe_common.h"
44 #include "radeon_video.h"
45 #include "radeon_vce.h"
46
47 static const unsigned profiles[7] = { 66, 77, 88, 100, 110, 122, 244 };
48
49 static void session(struct rvce_encoder *enc)
50 {
51 RVCE_BEGIN(0x00000001); // session cmd
52 RVCE_CS(enc->stream_handle);
53 RVCE_END();
54 }
55
56 static void task_info(struct rvce_encoder *enc, uint32_t taskOperation)
57 {
58 RVCE_BEGIN(0x00000002); // task info
59 RVCE_CS(0xffffffff); // offsetOfNextTaskInfo
60 RVCE_CS(taskOperation); // taskOperation
61 RVCE_CS(0x00000000); // referencePictureDependency
62 RVCE_CS(0x00000000); // collocateFlagDependency
63 RVCE_CS(0x00000000); // feedbackIndex
64 RVCE_CS(0x00000000); // videoBitstreamRingIndex
65 RVCE_END();
66 }
67
68 static void feedback(struct rvce_encoder *enc)
69 {
70 RVCE_BEGIN(0x05000005); // feedback buffer
71 RVCE_WRITE(enc->fb->res->cs_buf, enc->fb->res->domains); // feedbackRingAddressHi
72 RVCE_CS(0x00000000); // feedbackRingAddressLo
73 RVCE_CS(0x00000001); // feedbackRingSize
74 RVCE_END();
75 }
76
77 static void create(struct rvce_encoder *enc)
78 {
79 task_info(enc, 0x00000000);
80
81 RVCE_BEGIN(0x01000001); // create cmd
82 RVCE_CS(0x00000000); // encUseCircularBuffer
83 RVCE_CS(profiles[enc->base.profile -
84 PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE]); // encProfile
85 RVCE_CS(enc->base.level); // encLevel
86 RVCE_CS(0x00000000); // encPicStructRestriction
87 RVCE_CS(enc->base.width); // encImageWidth
88 RVCE_CS(enc->base.height); // encImageHeight
89 RVCE_CS(enc->luma->level[0].pitch_bytes); // encRefPicLumaPitch
90 RVCE_CS(enc->chroma->level[0].pitch_bytes); // encRefPicChromaPitch
91 RVCE_CS(align(enc->luma->npix_y, 16) / 8); // encRefYHeightInQw
92 RVCE_CS(0x00000000); // encRefPic(Addr|Array)Mode, encPicStructRestriction, disableRDO
93 RVCE_END();
94 }
95
96 static void rate_control(struct rvce_encoder *enc)
97 {
98 RVCE_BEGIN(0x04000005); // rate control
99 RVCE_CS(enc->pic.rate_ctrl.rate_ctrl_method); // encRateControlMethod
100 RVCE_CS(enc->pic.rate_ctrl.target_bitrate); // encRateControlTargetBitRate
101 RVCE_CS(enc->pic.rate_ctrl.peak_bitrate); // encRateControlPeakBitRate
102 RVCE_CS(enc->pic.rate_ctrl.frame_rate_num); // encRateControlFrameRateNum
103 RVCE_CS(0x00000000); // encGOPSize
104 RVCE_CS(enc->pic.quant_i_frames); // encQP_I
105 RVCE_CS(enc->pic.quant_p_frames); // encQP_P
106 RVCE_CS(enc->pic.quant_b_frames); // encQP_B
107 RVCE_CS(enc->pic.rate_ctrl.vbv_buffer_size); // encVBVBufferSize
108 RVCE_CS(enc->pic.rate_ctrl.frame_rate_den); // encRateControlFrameRateDen
109 RVCE_CS(0x00000000); // encVBVBufferLevel
110 RVCE_CS(0x00000000); // encMaxAUSize
111 RVCE_CS(0x00000000); // encQPInitialMode
112 RVCE_CS(enc->pic.rate_ctrl.target_bits_picture); // encTargetBitsPerPicture
113 RVCE_CS(enc->pic.rate_ctrl.peak_bits_picture_integer); // encPeakBitsPerPictureInteger
114 RVCE_CS(enc->pic.rate_ctrl.peak_bits_picture_fraction); // encPeakBitsPerPictureFractional
115 RVCE_CS(0x00000000); // encMinQP
116 RVCE_CS(0x00000033); // encMaxQP
117 RVCE_CS(0x00000000); // encSkipFrameEnable
118 RVCE_CS(0x00000000); // encFillerDataEnable
119 RVCE_CS(0x00000000); // encEnforceHRD
120 RVCE_CS(0x00000000); // encBPicsDeltaQP
121 RVCE_CS(0x00000000); // encReferenceBPicsDeltaQP
122 RVCE_CS(0x00000000); // encRateControlReInitDisable
123 RVCE_END();
124 }
125
126 static void config_extension(struct rvce_encoder *enc)
127 {
128 RVCE_BEGIN(0x04000001); // config extension
129 RVCE_CS(0x00000003); // encEnablePerfLogging
130 RVCE_END();
131 }
132
133 static void pic_control(struct rvce_encoder *enc)
134 {
135 unsigned encNumMBsPerSlice;
136
137 encNumMBsPerSlice = align(enc->base.width, 16) / 16;
138 encNumMBsPerSlice *= align(enc->base.height, 16) / 16;
139
140 RVCE_BEGIN(0x04000002); // pic control
141 RVCE_CS(0x00000000); // encUseConstrainedIntraPred
142 RVCE_CS(0x00000000); // encCABACEnable
143 RVCE_CS(0x00000000); // encCABACIDC
144 RVCE_CS(0x00000000); // encLoopFilterDisable
145 RVCE_CS(0x00000000); // encLFBetaOffset
146 RVCE_CS(0x00000000); // encLFAlphaC0Offset
147 RVCE_CS(0x00000000); // encCropLeftOffset
148 RVCE_CS((align(enc->base.width, 16) - enc->base.width) >> 1); // encCropRightOffset
149 RVCE_CS(0x00000000); // encCropTopOffset
150 RVCE_CS((align(enc->base.height, 16) - enc->base.height) >> 1); // encCropBottomOffset
151 RVCE_CS(encNumMBsPerSlice); // encNumMBsPerSlice
152 RVCE_CS(0x00000000); // encIntraRefreshNumMBsPerSlot
153 RVCE_CS(0x00000000); // encForceIntraRefresh
154 RVCE_CS(0x00000000); // encForceIMBPeriod
155 RVCE_CS(0x00000000); // encPicOrderCntType
156 RVCE_CS(0x00000000); // log2_max_pic_order_cnt_lsb_minus4
157 RVCE_CS(0x00000000); // encSPSID
158 RVCE_CS(0x00000000); // encPPSID
159 RVCE_CS(0x00000040); // encConstraintSetFlags
160 RVCE_CS(MAX2(enc->base.max_references, 1) - 1); // encBPicPattern
161 RVCE_CS(0x00000000); // weightPredModeBPicture
162 RVCE_CS(MIN2(enc->base.max_references, 2)); // encNumberOfReferenceFrames
163 RVCE_CS(enc->base.max_references + 1); // encMaxNumRefFrames
164 RVCE_CS(0x00000001); // encNumDefaultActiveRefL0
165 RVCE_CS(0x00000001); // encNumDefaultActiveRefL1
166 RVCE_CS(0x00000000); // encSliceMode
167 RVCE_CS(0x00000000); // encMaxSliceSize
168 RVCE_END();
169 }
170
171 static void motion_estimation(struct rvce_encoder *enc)
172 {
173 RVCE_BEGIN(0x04000007); // motion estimation
174 RVCE_CS(0x00000001); // encIMEDecimationSearch
175 RVCE_CS(0x00000001); // motionEstHalfPixel
176 RVCE_CS(0x00000000); // motionEstQuarterPixel
177 RVCE_CS(0x00000000); // disableFavorPMVPoint
178 RVCE_CS(0x00000000); // forceZeroPointCenter
179 RVCE_CS(0x00000000); // LSMVert
180 RVCE_CS(0x00000010); // encSearchRangeX
181 RVCE_CS(0x00000010); // encSearchRangeY
182 RVCE_CS(0x00000010); // encSearch1RangeX
183 RVCE_CS(0x00000010); // encSearch1RangeY
184 RVCE_CS(0x00000000); // disable16x16Frame1
185 RVCE_CS(0x00000000); // disableSATD
186 RVCE_CS(0x00000000); // enableAMD
187 RVCE_CS(0x000000fe); // encDisableSubMode
188 RVCE_CS(0x00000000); // encIMESkipX
189 RVCE_CS(0x00000000); // encIMESkipY
190 RVCE_CS(0x00000000); // encEnImeOverwDisSubm
191 RVCE_CS(0x00000000); // encImeOverwDisSubmNo
192 RVCE_CS(0x00000001); // encIME2SearchRangeX
193 RVCE_CS(0x00000001); // encIME2SearchRangeY
194 RVCE_CS(0x00000000); // parallelModeSpeedupEnable
195 RVCE_CS(0x00000000); // fme0_encDisableSubMode
196 RVCE_CS(0x00000000); // fme1_encDisableSubMode
197 RVCE_CS(0x00000000); // imeSWSpeedupEnable
198 RVCE_END();
199 }
200
201 static void rdo(struct rvce_encoder *enc)
202 {
203 RVCE_BEGIN(0x04000008); // rdo
204 RVCE_CS(0x00000000); // encDisableTbePredIFrame
205 RVCE_CS(0x00000000); // encDisableTbePredPFrame
206 RVCE_CS(0x00000000); // useFmeInterpolY
207 RVCE_CS(0x00000000); // useFmeInterpolUV
208 RVCE_CS(0x00000000); // useFmeIntrapolY
209 RVCE_CS(0x00000000); // useFmeIntrapolUV
210 RVCE_CS(0x00000000); // useFmeInterpolY_1
211 RVCE_CS(0x00000000); // useFmeInterpolUV_1
212 RVCE_CS(0x00000000); // useFmeIntrapolY_1
213 RVCE_CS(0x00000000); // useFmeIntrapolUV_1
214 RVCE_CS(0x00000000); // enc16x16CostAdj
215 RVCE_CS(0x00000000); // encSkipCostAdj
216 RVCE_CS(0x00000000); // encForce16x16skip
217 RVCE_CS(0x00000000); // encDisableThresholdCalcA
218 RVCE_CS(0x00000000); // encLumaCoeffCost
219 RVCE_CS(0x00000000); // encLumaMBCoeffCost
220 RVCE_CS(0x00000000); // encChromaCoeffCost
221 RVCE_END();
222 }
223
224 static void vui(struct rvce_encoder *enc)
225 {
226 int i;
227
228 RVCE_BEGIN(0x04000009); // vui
229 RVCE_CS(0x00000000); //aspectRatioInfoPresentFlag
230 RVCE_CS(0x00000000); //aspectRatioInfo.aspectRatioIdc
231 RVCE_CS(0x00000000); //aspectRatioInfo.sarWidth
232 RVCE_CS(0x00000000); //aspectRatioInfo.sarHeight
233 RVCE_CS(0x00000000); //overscanInfoPresentFlag
234 RVCE_CS(0x00000000); //overScanInfo.overscanAppropFlag
235 RVCE_CS(0x00000000); //videoSignalTypePresentFlag
236 RVCE_CS(0x00000005); //videoSignalTypeInfo.videoFormat
237 RVCE_CS(0x00000000); //videoSignalTypeInfo.videoFullRangeFlag
238 RVCE_CS(0x00000000); //videoSignalTypeInfo.colorDescriptionPresentFlag
239 RVCE_CS(0x00000002); //videoSignalTypeInfo.colorPrim
240 RVCE_CS(0x00000002); //videoSignalTypeInfo.transferChar
241 RVCE_CS(0x00000002); //videoSignalTypeInfo.matrixCoef
242 RVCE_CS(0x00000000); //chromaLocInfoPresentFlag
243 RVCE_CS(0x00000000); //chromaLocInfo.chromaLocTop
244 RVCE_CS(0x00000000); //chromaLocInfo.chromaLocBottom
245 RVCE_CS(0x00000001); //timingInfoPresentFlag
246 RVCE_CS(enc->pic.rate_ctrl.frame_rate_den); //timingInfo.numUnitsInTick
247 RVCE_CS(enc->pic.rate_ctrl.frame_rate_num * 2); //timingInfo.timeScale;
248 RVCE_CS(0x00000001); //timingInfo.fixedFrameRateFlag
249 RVCE_CS(0x00000000); //nalHRDParametersPresentFlag
250 RVCE_CS(0x00000000); //hrdParam.cpbCntMinus1
251 RVCE_CS(0x00000004); //hrdParam.bitRateScale
252 RVCE_CS(0x00000006); //hrdParam.cpbSizeScale
253 for (i = 0; i < 32; i++) {
254 RVCE_CS(0x00000000); //hrdParam.bitRateValueMinus
255 RVCE_CS(0x00000000); //hrdParam.cpbSizeValueMinus
256 RVCE_CS(0x00000000); //hrdParam.cbrFlag
257 }
258 RVCE_CS(0x00000017); //hrdParam.initialCpbRemovalDelayLengthMinus1
259 RVCE_CS(0x00000017); //hrdParam.cpbRemovalDelayLengthMinus1
260 RVCE_CS(0x00000017); //hrdParam.dpbOutputDelayLengthMinus1
261 RVCE_CS(0x00000018); //hrdParam.timeOffsetLength
262 RVCE_CS(0x00000000); //lowDelayHRDFlag
263 RVCE_CS(0x00000000); //picStructPresentFlag
264 RVCE_CS(0x00000000); //bitstreamRestrictionPresentFlag
265 RVCE_CS(0x00000001); //bitstreamRestrictions.motionVectorsOverPicBoundariesFlag
266 RVCE_CS(0x00000002); //bitstreamRestrictions.maxBytesPerPicDenom
267 RVCE_CS(0x00000001); //bitstreamRestrictions.maxBitsPerMbDenom
268 RVCE_CS(0x00000010); //bitstreamRestrictions.log2MaxMvLengthHori
269 RVCE_CS(0x00000010); //bitstreamRestrictions.log2MaxMvLengthVert
270 RVCE_CS(0x00000003); //bitstreamRestrictions.numReorderFrames
271 RVCE_CS(0x00000003); //bitstreamRestrictions.maxDecFrameBuffering
272 RVCE_END();
273 }
274
275 static void encode(struct rvce_encoder *enc)
276 {
277 int i;
278 unsigned luma_offset, chroma_offset;
279
280 task_info(enc, 0x00000003);
281
282 RVCE_BEGIN(0x05000001); // context buffer
283 RVCE_READWRITE(enc->cpb.res->cs_buf, enc->cpb.res->domains); // encodeContextAddressHi
284 RVCE_CS(0x00000000); // encodeContextAddressLo
285 RVCE_END();
286
287 RVCE_BEGIN(0x05000004); // video bitstream buffer
288 RVCE_WRITE(enc->bs_handle, RADEON_DOMAIN_GTT); // videoBitstreamRingAddressHi
289 RVCE_CS(0x00000000); // videoBitstreamRingAddressLo
290 RVCE_CS(enc->bs_size); // videoBitstreamRingSize
291 RVCE_END();
292
293 RVCE_BEGIN(0x03000001); // encode
294 RVCE_CS(0x00000000); // insertHeaders
295 RVCE_CS(0x00000000); // pictureStructure
296 RVCE_CS(enc->bs_size); // allowedMaxBitstreamSize
297 RVCE_CS(0x00000000); // forceRefreshMap
298 RVCE_CS(0x00000000); // insertAUD
299 RVCE_CS(0x00000000); // endOfSequence
300 RVCE_CS(0x00000000); // endOfStream
301 RVCE_READ(enc->handle, RADEON_DOMAIN_VRAM); // inputPictureLumaAddressHi
302 RVCE_CS(enc->luma->level[0].offset); // inputPictureLumaAddressLo
303 RVCE_READ(enc->handle, RADEON_DOMAIN_VRAM); // inputPictureChromaAddressHi
304 RVCE_CS(enc->chroma->level[0].offset); // inputPictureChromaAddressLo
305 RVCE_CS(align(enc->luma->npix_y, 16)); // encInputFrameYPitch
306 RVCE_CS(enc->luma->level[0].pitch_bytes); // encInputPicLumaPitch
307 RVCE_CS(enc->chroma->level[0].pitch_bytes); // encInputPicChromaPitch
308 RVCE_CS(0x00000000); // encInputPic(Addr|Array)Mode
309 RVCE_CS(0x00000000); // encInputPicTileConfig
310 RVCE_CS(enc->pic.picture_type); // encPicType
311 RVCE_CS(enc->pic.picture_type == PIPE_H264_ENC_PICTURE_TYPE_IDR); // encIdrFlag
312 RVCE_CS(0x00000000); // encIdrPicId
313 RVCE_CS(0x00000000); // encMGSKeyPic
314 RVCE_CS(!enc->pic.not_referenced); // encReferenceFlag
315 RVCE_CS(0x00000000); // encTemporalLayerIndex
316 RVCE_CS(0x00000000); // num_ref_idx_active_override_flag
317 RVCE_CS(0x00000000); // num_ref_idx_l0_active_minus1
318 RVCE_CS(0x00000000); // num_ref_idx_l1_active_minus1
319
320 i = enc->pic.frame_num - enc->pic.ref_idx_l0;
321 if (i > 1 && enc->pic.picture_type == PIPE_H264_ENC_PICTURE_TYPE_P) {
322 RVCE_CS(0x00000001); // encRefListModificationOp
323 RVCE_CS(i - 1); // encRefListModificationNum
324 } else {
325 RVCE_CS(0x00000000); // encRefListModificationOp
326 RVCE_CS(0x00000000); // encRefListModificationNum
327 }
328
329 for (i = 0; i < 3; ++i) {
330 RVCE_CS(0x00000000); // encRefListModificationOp
331 RVCE_CS(0x00000000); // encRefListModificationNum
332 }
333 for (i = 0; i < 4; ++i) {
334 RVCE_CS(0x00000000); // encDecodedPictureMarkingOp
335 RVCE_CS(0x00000000); // encDecodedPictureMarkingNum
336 RVCE_CS(0x00000000); // encDecodedPictureMarkingIdx
337 RVCE_CS(0x00000000); // encDecodedRefBasePictureMarkingOp
338 RVCE_CS(0x00000000); // encDecodedRefBasePictureMarkingNum
339 }
340
341 // encReferencePictureL0[0]
342 RVCE_CS(0x00000000); // pictureStructure
343 if(enc->pic.picture_type == PIPE_H264_ENC_PICTURE_TYPE_P ||
344 enc->pic.picture_type == PIPE_H264_ENC_PICTURE_TYPE_B) {
345 struct rvce_cpb_slot *l0 = l0_slot(enc);
346 rvce_frame_offset(enc, l0, &luma_offset, &chroma_offset);
347 RVCE_CS(l0->picture_type); // encPicType
348 RVCE_CS(l0->frame_num); // frameNumber
349 RVCE_CS(l0->pic_order_cnt); // pictureOrderCount
350 RVCE_CS(luma_offset); // lumaOffset
351 RVCE_CS(chroma_offset); // chromaOffset
352 } else {
353 RVCE_CS(0x00000000); // encPicType
354 RVCE_CS(0x00000000); // frameNumber
355 RVCE_CS(0x00000000); // pictureOrderCount
356 RVCE_CS(0xffffffff); // lumaOffset
357 RVCE_CS(0xffffffff); // chromaOffset
358 }
359
360 // encReferencePictureL0[1]
361 RVCE_CS(0x00000000); // pictureStructure
362 RVCE_CS(0x00000000); // encPicType
363 RVCE_CS(0x00000000); // frameNumber
364 RVCE_CS(0x00000000); // pictureOrderCount
365 RVCE_CS(0xffffffff); // lumaOffset
366 RVCE_CS(0xffffffff); // chromaOffset
367
368 // encReferencePictureL1[0]
369 RVCE_CS(0x00000000); // pictureStructure
370 if(enc->pic.picture_type == PIPE_H264_ENC_PICTURE_TYPE_B) {
371 struct rvce_cpb_slot *l1 = l1_slot(enc);
372 rvce_frame_offset(enc, l1, &luma_offset, &chroma_offset);
373 RVCE_CS(l1->picture_type); // encPicType
374 RVCE_CS(l1->frame_num); // frameNumber
375 RVCE_CS(l1->pic_order_cnt); // pictureOrderCount
376 RVCE_CS(luma_offset); // lumaOffset
377 RVCE_CS(chroma_offset); // chromaOffset
378 } else {
379 RVCE_CS(0x00000000); // encPicType
380 RVCE_CS(0x00000000); // frameNumber
381 RVCE_CS(0x00000000); // pictureOrderCount
382 RVCE_CS(0xffffffff); // lumaOffset
383 RVCE_CS(0xffffffff); // chromaOffset
384 }
385
386 rvce_frame_offset(enc, current_slot(enc), &luma_offset, &chroma_offset);
387 RVCE_CS(luma_offset); // encReconstructedLumaOffset
388 RVCE_CS(chroma_offset); // encReconstructedChromaOffset
389 RVCE_CS(0x00000000); // encColocBufferOffset
390 RVCE_CS(0x00000000); // encReconstructedRefBasePictureLumaOffset
391 RVCE_CS(0x00000000); // encReconstructedRefBasePictureChromaOffset
392 RVCE_CS(0x00000000); // encReferenceRefBasePictureLumaOffset
393 RVCE_CS(0x00000000); // encReferenceRefBasePictureChromaOffset
394 RVCE_CS(0x00000000); // pictureCount
395 RVCE_CS(enc->pic.frame_num); // frameNumber
396 RVCE_CS(enc->pic.pic_order_cnt); // pictureOrderCount
397 RVCE_CS(0x00000000); // numIPicRemainInRCGOP
398 RVCE_CS(0x00000000); // numPPicRemainInRCGOP
399 RVCE_CS(0x00000000); // numBPicRemainInRCGOP
400 RVCE_CS(0x00000000); // numIRPicRemainInRCGOP
401 RVCE_CS(0x00000000); // enableIntraRefresh
402 RVCE_END();
403 }
404
405 static void destroy(struct rvce_encoder *enc)
406 {
407 task_info(enc, 0x00000001);
408
409 RVCE_BEGIN(0x02000001); // destroy
410 RVCE_END();
411 }
412
413 void radeon_vce_40_2_2_init(struct rvce_encoder *enc)
414 {
415 enc->session = session;
416 enc->create = create;
417 enc->feedback = feedback;
418 enc->rate_control = rate_control;
419 enc->config_extension = config_extension;
420 enc->pic_control = pic_control;
421 enc->motion_estimation = motion_estimation;
422 enc->rdo = rdo;
423 enc->vui = vui;
424 enc->encode = encode;
425 enc->destroy = destroy;
426 }