1 /*
2 * Copyright 2013 Advanced Micro Devices, Inc.
3 * Authors:
4 * Christian König <[email protected]>
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include <stdio.h>
9
10 #include "pipe/p_video_codec.h"
11
12 #include "util/u_video.h"
13 #include "util/u_memory.h"
14
15 #include "vl/vl_video_buffer.h"
16
17 #include "r600_pipe_common.h"
18 #include "radeon_video.h"
19 #include "radeon_vce.h"
20
21 #define FW_40_2_2 ((40 << 24) | (2 << 16) | (2 << 8))
22 #define FW_50_0_1 ((50 << 24) | (0 << 16) | (1 << 8))
23 #define FW_50_1_2 ((50 << 24) | (1 << 16) | (2 << 8))
24 #define FW_50_10_2 ((50 << 24) | (10 << 16) | (2 << 8))
25 #define FW_50_17_3 ((50 << 24) | (17 << 16) | (3 << 8))
26 #define FW_52_0_3 ((52 << 24) | (0 << 16) | (3 << 8))
27 #define FW_52_4_3 ((52 << 24) | (4 << 16) | (3 << 8))
28 #define FW_52_8_3 ((52 << 24) | (8 << 16) | (3 << 8))
29 #define FW_53 (53 << 24)
30
31 /* version specific function for getting parameters */
32 static void (*get_pic_param)(struct rvce_encoder *enc,
33 struct pipe_h264_enc_picture_desc *pic) = NULL;
34
35 /**
36 * flush commands to the hardware
37 */
flush(struct rvce_encoder * enc)38 static void flush(struct rvce_encoder *enc)
39 {
40 enc->ws->cs_flush(&enc->cs, PIPE_FLUSH_ASYNC, NULL);
41 enc->task_info_idx = 0;
42 enc->bs_idx = 0;
43 }
44
45 #if 0
46 static void dump_feedback(struct rvce_encoder *enc, struct rvid_buffer *fb)
47 {
48 uint32_t *ptr = enc->ws->buffer_map(fb->res->buf, &enc->cs, PIPE_MAP_READ_WRITE);
49 unsigned i = 0;
50 fprintf(stderr, "\n");
51 fprintf(stderr, "encStatus:\t\t\t%08x\n", ptr[i++]);
52 fprintf(stderr, "encHasBitstream:\t\t%08x\n", ptr[i++]);
53 fprintf(stderr, "encHasAudioBitstream:\t\t%08x\n", ptr[i++]);
54 fprintf(stderr, "encBitstreamOffset:\t\t%08x\n", ptr[i++]);
55 fprintf(stderr, "encBitstreamSize:\t\t%08x\n", ptr[i++]);
56 fprintf(stderr, "encAudioBitstreamOffset:\t%08x\n", ptr[i++]);
57 fprintf(stderr, "encAudioBitstreamSize:\t\t%08x\n", ptr[i++]);
58 fprintf(stderr, "encExtrabytes:\t\t\t%08x\n", ptr[i++]);
59 fprintf(stderr, "encAudioExtrabytes:\t\t%08x\n", ptr[i++]);
60 fprintf(stderr, "videoTimeStamp:\t\t\t%08x\n", ptr[i++]);
61 fprintf(stderr, "audioTimeStamp:\t\t\t%08x\n", ptr[i++]);
62 fprintf(stderr, "videoOutputType:\t\t%08x\n", ptr[i++]);
63 fprintf(stderr, "attributeFlags:\t\t\t%08x\n", ptr[i++]);
64 fprintf(stderr, "seiPrivatePackageOffset:\t%08x\n", ptr[i++]);
65 fprintf(stderr, "seiPrivatePackageSize:\t\t%08x\n", ptr[i++]);
66 fprintf(stderr, "\n");
67 enc->ws->buffer_unmap(fb->res->buf);
68 }
69 #endif
70
71 /**
72 * reset the CPB handling
73 */
reset_cpb(struct rvce_encoder * enc)74 static void reset_cpb(struct rvce_encoder *enc)
75 {
76 unsigned i;
77
78 list_inithead(&enc->cpb_slots);
79 for (i = 0; i < enc->cpb_num; ++i) {
80 struct rvce_cpb_slot *slot = &enc->cpb_array[i];
81 slot->index = i;
82 slot->picture_type = PIPE_H2645_ENC_PICTURE_TYPE_SKIP;
83 slot->frame_num = 0;
84 slot->pic_order_cnt = 0;
85 list_addtail(&slot->list, &enc->cpb_slots);
86 }
87 }
88
89 /**
90 * sort l0 and l1 to the top of the list
91 */
sort_cpb(struct rvce_encoder * enc)92 static void sort_cpb(struct rvce_encoder *enc)
93 {
94 struct rvce_cpb_slot *i, *l0 = NULL, *l1 = NULL;
95
96 LIST_FOR_EACH_ENTRY(i, &enc->cpb_slots, list) {
97 if (i->frame_num == enc->pic.ref_idx_l0_list[0])
98 l0 = i;
99
100 if (i->frame_num == enc->pic.ref_idx_l1_list[0])
101 l1 = i;
102
103 if (enc->pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P && l0)
104 break;
105
106 if (enc->pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B &&
107 l0 && l1)
108 break;
109 }
110
111 if (l1) {
112 list_del(&l1->list);
113 list_add(&l1->list, &enc->cpb_slots);
114 }
115
116 if (l0) {
117 list_del(&l0->list);
118 list_add(&l0->list, &enc->cpb_slots);
119 }
120 }
121
122 /**
123 * get number of cpbs based on dpb
124 */
get_cpb_num(struct rvce_encoder * enc)125 static unsigned get_cpb_num(struct rvce_encoder *enc)
126 {
127 unsigned w = align(enc->base.width, 16) / 16;
128 unsigned h = align(enc->base.height, 16) / 16;
129 unsigned dpb;
130
131 switch (enc->base.level) {
132 case 10:
133 dpb = 396;
134 break;
135 case 11:
136 dpb = 900;
137 break;
138 case 12:
139 case 13:
140 case 20:
141 dpb = 2376;
142 break;
143 case 21:
144 dpb = 4752;
145 break;
146 case 22:
147 case 30:
148 dpb = 8100;
149 break;
150 case 31:
151 dpb = 18000;
152 break;
153 case 32:
154 dpb = 20480;
155 break;
156 case 40:
157 case 41:
158 dpb = 32768;
159 break;
160 case 42:
161 dpb = 34816;
162 break;
163 case 50:
164 dpb = 110400;
165 break;
166 default:
167 case 51:
168 case 52:
169 dpb = 184320;
170 break;
171 }
172
173 return MIN2(dpb / (w * h), 16);
174 }
175
176 /**
177 * Get the slot for the currently encoded frame
178 */
current_slot(struct rvce_encoder * enc)179 struct rvce_cpb_slot *current_slot(struct rvce_encoder *enc)
180 {
181 return list_entry(enc->cpb_slots.prev, struct rvce_cpb_slot, list);
182 }
183
184 /**
185 * Get the slot for L0
186 */
l0_slot(struct rvce_encoder * enc)187 struct rvce_cpb_slot *l0_slot(struct rvce_encoder *enc)
188 {
189 return list_entry(enc->cpb_slots.next, struct rvce_cpb_slot, list);
190 }
191
192 /**
193 * Get the slot for L1
194 */
l1_slot(struct rvce_encoder * enc)195 struct rvce_cpb_slot *l1_slot(struct rvce_encoder *enc)
196 {
197 return list_entry(enc->cpb_slots.next->next, struct rvce_cpb_slot, list);
198 }
199
200 /**
201 * Calculate the offsets into the CPB
202 */
rvce_frame_offset(struct rvce_encoder * enc,struct rvce_cpb_slot * slot,signed * luma_offset,signed * chroma_offset)203 void rvce_frame_offset(struct rvce_encoder *enc, struct rvce_cpb_slot *slot,
204 signed *luma_offset, signed *chroma_offset)
205 {
206 unsigned pitch, vpitch, fsize;
207
208 pitch = align(enc->luma->u.legacy.level[0].nblk_x * enc->luma->bpe, 128);
209 vpitch = align(enc->luma->u.legacy.level[0].nblk_y, 16);
210 fsize = pitch * (vpitch + vpitch / 2);
211
212 *luma_offset = slot->index * fsize;
213 *chroma_offset = *luma_offset + pitch * vpitch;
214 }
215
216 /**
217 * destroy this video encoder
218 */
rvce_destroy(struct pipe_video_codec * encoder)219 static void rvce_destroy(struct pipe_video_codec *encoder)
220 {
221 struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
222 if (enc->stream_handle) {
223 struct rvid_buffer fb;
224 rvid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING);
225 enc->fb = &fb;
226 enc->session(enc);
227 enc->feedback(enc);
228 enc->destroy(enc);
229 flush(enc);
230 rvid_destroy_buffer(&fb);
231 }
232 rvid_destroy_buffer(&enc->cpb);
233 enc->ws->cs_destroy(&enc->cs);
234 FREE(enc->cpb_array);
235 FREE(enc);
236 }
237
rvce_begin_frame(struct pipe_video_codec * encoder,struct pipe_video_buffer * source,struct pipe_picture_desc * picture)238 static void rvce_begin_frame(struct pipe_video_codec *encoder,
239 struct pipe_video_buffer *source,
240 struct pipe_picture_desc *picture)
241 {
242 struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
243 struct vl_video_buffer *vid_buf = (struct vl_video_buffer *)source;
244 struct pipe_h264_enc_picture_desc *pic = (struct pipe_h264_enc_picture_desc *)picture;
245
246 bool need_rate_control =
247 enc->pic.rate_ctrl[0].rate_ctrl_method != pic->rate_ctrl[0].rate_ctrl_method ||
248 enc->pic.quant_i_frames != pic->quant_i_frames ||
249 enc->pic.quant_p_frames != pic->quant_p_frames ||
250 enc->pic.quant_b_frames != pic->quant_b_frames;
251
252 enc->pic = *pic;
253 get_pic_param(enc, pic);
254
255 enc->get_buffer(vid_buf->resources[0], &enc->handle, &enc->luma);
256 enc->get_buffer(vid_buf->resources[1], NULL, &enc->chroma);
257
258 if (pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR)
259 reset_cpb(enc);
260 else if (pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P ||
261 pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B)
262 sort_cpb(enc);
263
264 if (!enc->stream_handle) {
265 struct rvid_buffer fb;
266 enc->stream_handle = rvid_alloc_stream_handle();
267 rvid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING);
268 enc->fb = &fb;
269 enc->session(enc);
270 enc->create(enc);
271 enc->config(enc);
272 enc->feedback(enc);
273 flush(enc);
274 //dump_feedback(enc, &fb);
275 rvid_destroy_buffer(&fb);
276 need_rate_control = false;
277 }
278
279 if (need_rate_control) {
280 enc->session(enc);
281 enc->config(enc);
282 flush(enc);
283 }
284 }
285
rvce_encode_bitstream(struct pipe_video_codec * encoder,struct pipe_video_buffer * source,struct pipe_resource * destination,void ** fb)286 static void rvce_encode_bitstream(struct pipe_video_codec *encoder,
287 struct pipe_video_buffer *source,
288 struct pipe_resource *destination,
289 void **fb)
290 {
291 struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
292 enc->get_buffer(destination, &enc->bs_handle, NULL);
293 enc->bs_size = destination->width0;
294
295 *fb = enc->fb = CALLOC_STRUCT(rvid_buffer);
296 if (!rvid_create_buffer(enc->screen, enc->fb, 512, PIPE_USAGE_STAGING)) {
297 RVID_ERR("Can't create feedback buffer.\n");
298 return;
299 }
300 if (!radeon_emitted(&enc->cs, 0))
301 enc->session(enc);
302 enc->encode(enc);
303 enc->feedback(enc);
304 }
305
rvce_end_frame(struct pipe_video_codec * encoder,struct pipe_video_buffer * source,struct pipe_picture_desc * picture)306 static int rvce_end_frame(struct pipe_video_codec *encoder,
307 struct pipe_video_buffer *source,
308 struct pipe_picture_desc *picture)
309 {
310 struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
311 struct rvce_cpb_slot *slot = list_entry(enc->cpb_slots.prev,
312 struct rvce_cpb_slot,
313 list);
314
315 if (!enc->dual_inst || enc->bs_idx > 1)
316 flush(enc);
317
318 /* update the CPB backtrack with the just encoded frame */
319 slot->picture_type = enc->pic.picture_type;
320 slot->frame_num = enc->pic.frame_num;
321 slot->pic_order_cnt = enc->pic.pic_order_cnt;
322 if (!enc->pic.not_referenced) {
323 list_del(&slot->list);
324 list_add(&slot->list, &enc->cpb_slots);
325 }
326 return 0;
327 }
328
rvce_get_feedback(struct pipe_video_codec * encoder,void * feedback,unsigned * size,struct pipe_enc_feedback_metadata * metadata)329 static void rvce_get_feedback(struct pipe_video_codec *encoder,
330 void *feedback, unsigned *size,
331 struct pipe_enc_feedback_metadata* metadata)
332 {
333 struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
334 struct rvid_buffer *fb = feedback;
335
336 if (size) {
337 uint32_t *ptr = enc->ws->buffer_map(enc->ws,
338 fb->res->buf, &enc->cs,
339 PIPE_MAP_READ_WRITE | RADEON_MAP_TEMPORARY);
340
341 if (ptr[1]) {
342 *size = ptr[4] - ptr[9];
343 } else {
344 *size = 0;
345 }
346
347 enc->ws->buffer_unmap(enc->ws, fb->res->buf);
348 }
349 //dump_feedback(enc, fb);
350 rvid_destroy_buffer(fb);
351 FREE(fb);
352 }
353
354 /**
355 * flush any outstanding command buffers to the hardware
356 */
rvce_flush(struct pipe_video_codec * encoder)357 static void rvce_flush(struct pipe_video_codec *encoder)
358 {
359 struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
360
361 flush(enc);
362 }
363
rvce_cs_flush(void * ctx,unsigned flags,struct pipe_fence_handle ** fence)364 static void rvce_cs_flush(void *ctx, unsigned flags,
365 struct pipe_fence_handle **fence)
366 {
367 // just ignored
368 }
369
rvce_create_encoder(struct pipe_context * context,const struct pipe_video_codec * templ,struct radeon_winsys * ws,rvce_get_buffer get_buffer)370 struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
371 const struct pipe_video_codec *templ,
372 struct radeon_winsys* ws,
373 rvce_get_buffer get_buffer)
374 {
375 struct r600_common_screen *rscreen = (struct r600_common_screen *)context->screen;
376 struct r600_common_context *rctx = (struct r600_common_context*)context;
377 struct rvce_encoder *enc;
378 struct pipe_video_buffer *tmp_buf, templat = {};
379 struct radeon_surf *tmp_surf;
380 unsigned cpb_size;
381
382 if (!rscreen->info.vce_fw_version) {
383 RVID_ERR("Kernel doesn't supports VCE!\n");
384 return NULL;
385
386 } else if (!rvce_is_fw_version_supported(rscreen)) {
387 RVID_ERR("Unsupported VCE fw version loaded!\n");
388 return NULL;
389 }
390
391 enc = CALLOC_STRUCT(rvce_encoder);
392 if (!enc)
393 return NULL;
394
395 enc->use_vui = true;
396
397 enc->base = *templ;
398 enc->base.context = context;
399
400 enc->base.destroy = rvce_destroy;
401 enc->base.begin_frame = rvce_begin_frame;
402 enc->base.encode_bitstream = rvce_encode_bitstream;
403 enc->base.end_frame = rvce_end_frame;
404 enc->base.flush = rvce_flush;
405 enc->base.get_feedback = rvce_get_feedback;
406 enc->get_buffer = get_buffer;
407
408 enc->screen = context->screen;
409 enc->ws = ws;
410
411 if (!ws->cs_create(&enc->cs, rctx->ctx, AMD_IP_VCE, rvce_cs_flush, enc)) {
412 RVID_ERR("Can't get command submission context.\n");
413 goto error;
414 }
415
416 templat.buffer_format = PIPE_FORMAT_NV12;
417 templat.width = enc->base.width;
418 templat.height = enc->base.height;
419 templat.interlaced = false;
420 if (!(tmp_buf = context->create_video_buffer(context, &templat))) {
421 RVID_ERR("Can't create video buffer.\n");
422 goto error;
423 }
424
425 enc->cpb_num = get_cpb_num(enc);
426 if (!enc->cpb_num)
427 goto error;
428
429 get_buffer(((struct vl_video_buffer *)tmp_buf)->resources[0], NULL, &tmp_surf);
430
431 cpb_size = align(tmp_surf->u.legacy.level[0].nblk_x * tmp_surf->bpe, 128) *
432 align(tmp_surf->u.legacy.level[0].nblk_y, 32);
433
434 cpb_size = cpb_size * 3 / 2;
435 cpb_size = cpb_size * enc->cpb_num;
436 if (enc->dual_pipe)
437 cpb_size += RVCE_MAX_AUX_BUFFER_NUM *
438 RVCE_MAX_BITSTREAM_OUTPUT_ROW_SIZE * 2;
439 tmp_buf->destroy(tmp_buf);
440 if (!rvid_create_buffer(enc->screen, &enc->cpb, cpb_size, PIPE_USAGE_DEFAULT)) {
441 RVID_ERR("Can't create CPB buffer.\n");
442 goto error;
443 }
444
445 enc->cpb_array = CALLOC(enc->cpb_num, sizeof(struct rvce_cpb_slot));
446 if (!enc->cpb_array)
447 goto error;
448
449 reset_cpb(enc);
450
451 goto error;
452
453 return &enc->base;
454
455 error:
456 enc->ws->cs_destroy(&enc->cs);
457
458 rvid_destroy_buffer(&enc->cpb);
459
460 FREE(enc->cpb_array);
461 FREE(enc);
462 return NULL;
463 }
464
465 /**
466 * check if kernel has the right fw version loaded
467 */
rvce_is_fw_version_supported(struct r600_common_screen * rscreen)468 bool rvce_is_fw_version_supported(struct r600_common_screen *rscreen)
469 {
470 switch (rscreen->info.vce_fw_version) {
471 case FW_40_2_2:
472 case FW_50_0_1:
473 case FW_50_1_2:
474 case FW_50_10_2:
475 case FW_50_17_3:
476 case FW_52_0_3:
477 case FW_52_4_3:
478 case FW_52_8_3:
479 return true;
480 default:
481 if ((rscreen->info.vce_fw_version & (0xff << 24)) == FW_53)
482 return true;
483 else
484 return false;
485 }
486 }
487
488 /**
489 * Add the buffer as relocation to the current command submission
490 */
rvce_add_buffer(struct rvce_encoder * enc,struct pb_buffer_lean * buf,unsigned usage,enum radeon_bo_domain domain,signed offset)491 void rvce_add_buffer(struct rvce_encoder *enc, struct pb_buffer_lean *buf,
492 unsigned usage, enum radeon_bo_domain domain,
493 signed offset)
494 {
495 int reloc_idx;
496
497 reloc_idx = enc->ws->cs_add_buffer(&enc->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED,
498 domain);
499 if (enc->use_vm) {
500 uint64_t addr;
501 addr = enc->ws->buffer_get_virtual_address(buf);
502 addr = addr + offset;
503 RVCE_CS(addr >> 32);
504 RVCE_CS(addr);
505 } else {
506 offset += enc->ws->buffer_get_reloc_offset(buf);
507 RVCE_CS(reloc_idx * 4);
508 RVCE_CS(offset);
509 }
510 }
511