1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2013 Advanced Micro Devices, Inc. 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 16bf215546Sopenharmony_ci * of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR 22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci **************************************************************************/ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci#include "radeon_vce.h" 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci#include "pipe/p_video_codec.h" 31bf215546Sopenharmony_ci#include "radeon_video.h" 32bf215546Sopenharmony_ci#include "radeonsi/si_pipe.h" 33bf215546Sopenharmony_ci#include "util/u_memory.h" 34bf215546Sopenharmony_ci#include "util/u_video.h" 35bf215546Sopenharmony_ci#include "vl/vl_video_buffer.h" 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci#include <stdio.h> 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci#define FW_40_2_2 ((40 << 24) | (2 << 16) | (2 << 8)) 40bf215546Sopenharmony_ci#define FW_50_0_1 ((50 << 24) | (0 << 16) | (1 << 8)) 41bf215546Sopenharmony_ci#define FW_50_1_2 ((50 << 24) | (1 << 16) | (2 << 8)) 42bf215546Sopenharmony_ci#define FW_50_10_2 ((50 << 24) | (10 << 16) | (2 << 8)) 43bf215546Sopenharmony_ci#define FW_50_17_3 ((50 << 24) | (17 << 16) | (3 << 8)) 44bf215546Sopenharmony_ci#define FW_52_0_3 ((52 << 24) | (0 << 16) | (3 << 8)) 45bf215546Sopenharmony_ci#define FW_52_4_3 ((52 << 24) | (4 << 16) | (3 << 8)) 46bf215546Sopenharmony_ci#define FW_52_8_3 ((52 << 24) | (8 << 16) | (3 << 8)) 47bf215546Sopenharmony_ci#define FW_53 (53 << 24) 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ci/** 50bf215546Sopenharmony_ci * flush commands to the hardware 51bf215546Sopenharmony_ci */ 52bf215546Sopenharmony_cistatic void flush(struct rvce_encoder *enc) 53bf215546Sopenharmony_ci{ 54bf215546Sopenharmony_ci enc->ws->cs_flush(&enc->cs, PIPE_FLUSH_ASYNC, NULL); 55bf215546Sopenharmony_ci enc->task_info_idx = 0; 56bf215546Sopenharmony_ci enc->bs_idx = 0; 57bf215546Sopenharmony_ci} 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci#if 0 60bf215546Sopenharmony_cistatic void dump_feedback(struct rvce_encoder *enc, struct rvid_buffer *fb) 61bf215546Sopenharmony_ci{ 62bf215546Sopenharmony_ci uint32_t *ptr = enc->ws->buffer_map(fb->res->buf, &enc->cs, PIPE_MAP_READ_WRITE); 63bf215546Sopenharmony_ci unsigned i = 0; 64bf215546Sopenharmony_ci fprintf(stderr, "\n"); 65bf215546Sopenharmony_ci fprintf(stderr, "encStatus:\t\t\t%08x\n", ptr[i++]); 66bf215546Sopenharmony_ci fprintf(stderr, "encHasBitstream:\t\t%08x\n", ptr[i++]); 67bf215546Sopenharmony_ci fprintf(stderr, "encHasAudioBitstream:\t\t%08x\n", ptr[i++]); 68bf215546Sopenharmony_ci fprintf(stderr, "encBitstreamOffset:\t\t%08x\n", ptr[i++]); 69bf215546Sopenharmony_ci fprintf(stderr, "encBitstreamSize:\t\t%08x\n", ptr[i++]); 70bf215546Sopenharmony_ci fprintf(stderr, "encAudioBitstreamOffset:\t%08x\n", ptr[i++]); 71bf215546Sopenharmony_ci fprintf(stderr, "encAudioBitstreamSize:\t\t%08x\n", ptr[i++]); 72bf215546Sopenharmony_ci fprintf(stderr, "encExtrabytes:\t\t\t%08x\n", ptr[i++]); 73bf215546Sopenharmony_ci fprintf(stderr, "encAudioExtrabytes:\t\t%08x\n", ptr[i++]); 74bf215546Sopenharmony_ci fprintf(stderr, "videoTimeStamp:\t\t\t%08x\n", ptr[i++]); 75bf215546Sopenharmony_ci fprintf(stderr, "audioTimeStamp:\t\t\t%08x\n", ptr[i++]); 76bf215546Sopenharmony_ci fprintf(stderr, "videoOutputType:\t\t%08x\n", ptr[i++]); 77bf215546Sopenharmony_ci fprintf(stderr, "attributeFlags:\t\t\t%08x\n", ptr[i++]); 78bf215546Sopenharmony_ci fprintf(stderr, "seiPrivatePackageOffset:\t%08x\n", ptr[i++]); 79bf215546Sopenharmony_ci fprintf(stderr, "seiPrivatePackageSize:\t\t%08x\n", ptr[i++]); 80bf215546Sopenharmony_ci fprintf(stderr, "\n"); 81bf215546Sopenharmony_ci enc->ws->buffer_unmap(fb->res->buf); 82bf215546Sopenharmony_ci} 83bf215546Sopenharmony_ci#endif 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci/** 86bf215546Sopenharmony_ci * reset the CPB handling 87bf215546Sopenharmony_ci */ 88bf215546Sopenharmony_cistatic void reset_cpb(struct rvce_encoder *enc) 89bf215546Sopenharmony_ci{ 90bf215546Sopenharmony_ci unsigned i; 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci list_inithead(&enc->cpb_slots); 93bf215546Sopenharmony_ci for (i = 0; i < enc->cpb_num; ++i) { 94bf215546Sopenharmony_ci struct rvce_cpb_slot *slot = &enc->cpb_array[i]; 95bf215546Sopenharmony_ci slot->index = i; 96bf215546Sopenharmony_ci slot->picture_type = PIPE_H2645_ENC_PICTURE_TYPE_SKIP; 97bf215546Sopenharmony_ci slot->frame_num = 0; 98bf215546Sopenharmony_ci slot->pic_order_cnt = 0; 99bf215546Sopenharmony_ci list_addtail(&slot->list, &enc->cpb_slots); 100bf215546Sopenharmony_ci } 101bf215546Sopenharmony_ci} 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci/** 104bf215546Sopenharmony_ci * sort l0 and l1 to the top of the list 105bf215546Sopenharmony_ci */ 106bf215546Sopenharmony_cistatic void sort_cpb(struct rvce_encoder *enc) 107bf215546Sopenharmony_ci{ 108bf215546Sopenharmony_ci struct rvce_cpb_slot *i, *l0 = NULL, *l1 = NULL; 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_ci LIST_FOR_EACH_ENTRY (i, &enc->cpb_slots, list) { 111bf215546Sopenharmony_ci if (i->frame_num == enc->pic.ref_idx_l0_list[0]) 112bf215546Sopenharmony_ci l0 = i; 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci if (i->frame_num == enc->pic.ref_idx_l1_list[0]) 115bf215546Sopenharmony_ci l1 = i; 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci if (enc->pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P && l0) 118bf215546Sopenharmony_ci break; 119bf215546Sopenharmony_ci 120bf215546Sopenharmony_ci if (enc->pic.picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B && l0 && l1) 121bf215546Sopenharmony_ci break; 122bf215546Sopenharmony_ci } 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci if (l1) { 125bf215546Sopenharmony_ci list_del(&l1->list); 126bf215546Sopenharmony_ci list_add(&l1->list, &enc->cpb_slots); 127bf215546Sopenharmony_ci } 128bf215546Sopenharmony_ci 129bf215546Sopenharmony_ci if (l0) { 130bf215546Sopenharmony_ci list_del(&l0->list); 131bf215546Sopenharmony_ci list_add(&l0->list, &enc->cpb_slots); 132bf215546Sopenharmony_ci } 133bf215546Sopenharmony_ci} 134bf215546Sopenharmony_ci 135bf215546Sopenharmony_ci/** 136bf215546Sopenharmony_ci * get number of cpbs based on dpb 137bf215546Sopenharmony_ci */ 138bf215546Sopenharmony_cistatic unsigned get_cpb_num(struct rvce_encoder *enc) 139bf215546Sopenharmony_ci{ 140bf215546Sopenharmony_ci unsigned w = align(enc->base.width, 16) / 16; 141bf215546Sopenharmony_ci unsigned h = align(enc->base.height, 16) / 16; 142bf215546Sopenharmony_ci unsigned dpb; 143bf215546Sopenharmony_ci 144bf215546Sopenharmony_ci switch (enc->base.level) { 145bf215546Sopenharmony_ci case 10: 146bf215546Sopenharmony_ci dpb = 396; 147bf215546Sopenharmony_ci break; 148bf215546Sopenharmony_ci case 11: 149bf215546Sopenharmony_ci dpb = 900; 150bf215546Sopenharmony_ci break; 151bf215546Sopenharmony_ci case 12: 152bf215546Sopenharmony_ci case 13: 153bf215546Sopenharmony_ci case 20: 154bf215546Sopenharmony_ci dpb = 2376; 155bf215546Sopenharmony_ci break; 156bf215546Sopenharmony_ci case 21: 157bf215546Sopenharmony_ci dpb = 4752; 158bf215546Sopenharmony_ci break; 159bf215546Sopenharmony_ci case 22: 160bf215546Sopenharmony_ci case 30: 161bf215546Sopenharmony_ci dpb = 8100; 162bf215546Sopenharmony_ci break; 163bf215546Sopenharmony_ci case 31: 164bf215546Sopenharmony_ci dpb = 18000; 165bf215546Sopenharmony_ci break; 166bf215546Sopenharmony_ci case 32: 167bf215546Sopenharmony_ci dpb = 20480; 168bf215546Sopenharmony_ci break; 169bf215546Sopenharmony_ci case 40: 170bf215546Sopenharmony_ci case 41: 171bf215546Sopenharmony_ci dpb = 32768; 172bf215546Sopenharmony_ci break; 173bf215546Sopenharmony_ci case 42: 174bf215546Sopenharmony_ci dpb = 34816; 175bf215546Sopenharmony_ci break; 176bf215546Sopenharmony_ci case 50: 177bf215546Sopenharmony_ci dpb = 110400; 178bf215546Sopenharmony_ci break; 179bf215546Sopenharmony_ci default: 180bf215546Sopenharmony_ci case 51: 181bf215546Sopenharmony_ci case 52: 182bf215546Sopenharmony_ci dpb = 184320; 183bf215546Sopenharmony_ci break; 184bf215546Sopenharmony_ci } 185bf215546Sopenharmony_ci 186bf215546Sopenharmony_ci return MIN2(dpb / (w * h), 16); 187bf215546Sopenharmony_ci} 188bf215546Sopenharmony_ci 189bf215546Sopenharmony_ci/** 190bf215546Sopenharmony_ci * Get the slot for the currently encoded frame 191bf215546Sopenharmony_ci */ 192bf215546Sopenharmony_cistruct rvce_cpb_slot *si_current_slot(struct rvce_encoder *enc) 193bf215546Sopenharmony_ci{ 194bf215546Sopenharmony_ci return list_entry(enc->cpb_slots.prev, struct rvce_cpb_slot, list); 195bf215546Sopenharmony_ci} 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci/** 198bf215546Sopenharmony_ci * Get the slot for L0 199bf215546Sopenharmony_ci */ 200bf215546Sopenharmony_cistruct rvce_cpb_slot *si_l0_slot(struct rvce_encoder *enc) 201bf215546Sopenharmony_ci{ 202bf215546Sopenharmony_ci return list_entry(enc->cpb_slots.next, struct rvce_cpb_slot, list); 203bf215546Sopenharmony_ci} 204bf215546Sopenharmony_ci 205bf215546Sopenharmony_ci/** 206bf215546Sopenharmony_ci * Get the slot for L1 207bf215546Sopenharmony_ci */ 208bf215546Sopenharmony_cistruct rvce_cpb_slot *si_l1_slot(struct rvce_encoder *enc) 209bf215546Sopenharmony_ci{ 210bf215546Sopenharmony_ci return list_entry(enc->cpb_slots.next->next, struct rvce_cpb_slot, list); 211bf215546Sopenharmony_ci} 212bf215546Sopenharmony_ci 213bf215546Sopenharmony_ci/** 214bf215546Sopenharmony_ci * Calculate the offsets into the CPB 215bf215546Sopenharmony_ci */ 216bf215546Sopenharmony_civoid si_vce_frame_offset(struct rvce_encoder *enc, struct rvce_cpb_slot *slot, signed *luma_offset, 217bf215546Sopenharmony_ci signed *chroma_offset) 218bf215546Sopenharmony_ci{ 219bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)enc->screen; 220bf215546Sopenharmony_ci unsigned pitch, vpitch, fsize; 221bf215546Sopenharmony_ci 222bf215546Sopenharmony_ci if (sscreen->info.gfx_level < GFX9) { 223bf215546Sopenharmony_ci pitch = align(enc->luma->u.legacy.level[0].nblk_x * enc->luma->bpe, 128); 224bf215546Sopenharmony_ci vpitch = align(enc->luma->u.legacy.level[0].nblk_y, 16); 225bf215546Sopenharmony_ci } else { 226bf215546Sopenharmony_ci pitch = align(enc->luma->u.gfx9.surf_pitch * enc->luma->bpe, 256); 227bf215546Sopenharmony_ci vpitch = align(enc->luma->u.gfx9.surf_height, 16); 228bf215546Sopenharmony_ci } 229bf215546Sopenharmony_ci fsize = pitch * (vpitch + vpitch / 2); 230bf215546Sopenharmony_ci 231bf215546Sopenharmony_ci *luma_offset = slot->index * fsize; 232bf215546Sopenharmony_ci *chroma_offset = *luma_offset + pitch * vpitch; 233bf215546Sopenharmony_ci} 234bf215546Sopenharmony_ci 235bf215546Sopenharmony_ci/** 236bf215546Sopenharmony_ci * destroy this video encoder 237bf215546Sopenharmony_ci */ 238bf215546Sopenharmony_cistatic void rvce_destroy(struct pipe_video_codec *encoder) 239bf215546Sopenharmony_ci{ 240bf215546Sopenharmony_ci struct rvce_encoder *enc = (struct rvce_encoder *)encoder; 241bf215546Sopenharmony_ci if (enc->stream_handle) { 242bf215546Sopenharmony_ci struct rvid_buffer fb; 243bf215546Sopenharmony_ci si_vid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING); 244bf215546Sopenharmony_ci enc->fb = &fb; 245bf215546Sopenharmony_ci enc->session(enc); 246bf215546Sopenharmony_ci enc->destroy(enc); 247bf215546Sopenharmony_ci flush(enc); 248bf215546Sopenharmony_ci si_vid_destroy_buffer(&fb); 249bf215546Sopenharmony_ci } 250bf215546Sopenharmony_ci si_vid_destroy_buffer(&enc->cpb); 251bf215546Sopenharmony_ci enc->ws->cs_destroy(&enc->cs); 252bf215546Sopenharmony_ci FREE(enc->cpb_array); 253bf215546Sopenharmony_ci FREE(enc); 254bf215546Sopenharmony_ci} 255bf215546Sopenharmony_ci 256bf215546Sopenharmony_cistatic void rvce_begin_frame(struct pipe_video_codec *encoder, struct pipe_video_buffer *source, 257bf215546Sopenharmony_ci struct pipe_picture_desc *picture) 258bf215546Sopenharmony_ci{ 259bf215546Sopenharmony_ci struct rvce_encoder *enc = (struct rvce_encoder *)encoder; 260bf215546Sopenharmony_ci struct vl_video_buffer *vid_buf = (struct vl_video_buffer *)source; 261bf215546Sopenharmony_ci struct pipe_h264_enc_picture_desc *pic = (struct pipe_h264_enc_picture_desc *)picture; 262bf215546Sopenharmony_ci 263bf215546Sopenharmony_ci bool need_rate_control = 264bf215546Sopenharmony_ci enc->pic.rate_ctrl[0].rate_ctrl_method != pic->rate_ctrl[0].rate_ctrl_method || 265bf215546Sopenharmony_ci enc->pic.quant_i_frames != pic->quant_i_frames || 266bf215546Sopenharmony_ci enc->pic.quant_p_frames != pic->quant_p_frames || 267bf215546Sopenharmony_ci enc->pic.quant_b_frames != pic->quant_b_frames || 268bf215546Sopenharmony_ci enc->pic.rate_ctrl[0].target_bitrate != pic->rate_ctrl[0].target_bitrate || 269bf215546Sopenharmony_ci enc->pic.rate_ctrl[0].frame_rate_num != pic->rate_ctrl[0].frame_rate_num || 270bf215546Sopenharmony_ci enc->pic.rate_ctrl[0].frame_rate_den != pic->rate_ctrl[0].frame_rate_den; 271bf215546Sopenharmony_ci 272bf215546Sopenharmony_ci enc->pic = *pic; 273bf215546Sopenharmony_ci enc->si_get_pic_param(enc, pic); 274bf215546Sopenharmony_ci 275bf215546Sopenharmony_ci enc->get_buffer(vid_buf->resources[0], &enc->handle, &enc->luma); 276bf215546Sopenharmony_ci enc->get_buffer(vid_buf->resources[1], NULL, &enc->chroma); 277bf215546Sopenharmony_ci 278bf215546Sopenharmony_ci if (pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_IDR) 279bf215546Sopenharmony_ci reset_cpb(enc); 280bf215546Sopenharmony_ci else if (pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_P || 281bf215546Sopenharmony_ci pic->picture_type == PIPE_H2645_ENC_PICTURE_TYPE_B) 282bf215546Sopenharmony_ci sort_cpb(enc); 283bf215546Sopenharmony_ci 284bf215546Sopenharmony_ci if (!enc->stream_handle) { 285bf215546Sopenharmony_ci struct rvid_buffer fb; 286bf215546Sopenharmony_ci enc->stream_handle = si_vid_alloc_stream_handle(); 287bf215546Sopenharmony_ci si_vid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING); 288bf215546Sopenharmony_ci enc->fb = &fb; 289bf215546Sopenharmony_ci enc->session(enc); 290bf215546Sopenharmony_ci enc->create(enc); 291bf215546Sopenharmony_ci enc->config(enc); 292bf215546Sopenharmony_ci enc->feedback(enc); 293bf215546Sopenharmony_ci flush(enc); 294bf215546Sopenharmony_ci // dump_feedback(enc, &fb); 295bf215546Sopenharmony_ci si_vid_destroy_buffer(&fb); 296bf215546Sopenharmony_ci need_rate_control = false; 297bf215546Sopenharmony_ci } 298bf215546Sopenharmony_ci 299bf215546Sopenharmony_ci if (need_rate_control) { 300bf215546Sopenharmony_ci enc->session(enc); 301bf215546Sopenharmony_ci enc->config(enc); 302bf215546Sopenharmony_ci flush(enc); 303bf215546Sopenharmony_ci } 304bf215546Sopenharmony_ci} 305bf215546Sopenharmony_ci 306bf215546Sopenharmony_cistatic void rvce_encode_bitstream(struct pipe_video_codec *encoder, 307bf215546Sopenharmony_ci struct pipe_video_buffer *source, 308bf215546Sopenharmony_ci struct pipe_resource *destination, void **fb) 309bf215546Sopenharmony_ci{ 310bf215546Sopenharmony_ci struct rvce_encoder *enc = (struct rvce_encoder *)encoder; 311bf215546Sopenharmony_ci enc->get_buffer(destination, &enc->bs_handle, NULL); 312bf215546Sopenharmony_ci enc->bs_size = destination->width0; 313bf215546Sopenharmony_ci 314bf215546Sopenharmony_ci *fb = enc->fb = CALLOC_STRUCT(rvid_buffer); 315bf215546Sopenharmony_ci if (!si_vid_create_buffer(enc->screen, enc->fb, 512, PIPE_USAGE_STAGING)) { 316bf215546Sopenharmony_ci RVID_ERR("Can't create feedback buffer.\n"); 317bf215546Sopenharmony_ci return; 318bf215546Sopenharmony_ci } 319bf215546Sopenharmony_ci if (!radeon_emitted(&enc->cs, 0)) 320bf215546Sopenharmony_ci enc->session(enc); 321bf215546Sopenharmony_ci enc->encode(enc); 322bf215546Sopenharmony_ci enc->feedback(enc); 323bf215546Sopenharmony_ci} 324bf215546Sopenharmony_ci 325bf215546Sopenharmony_cistatic void rvce_end_frame(struct pipe_video_codec *encoder, struct pipe_video_buffer *source, 326bf215546Sopenharmony_ci struct pipe_picture_desc *picture) 327bf215546Sopenharmony_ci{ 328bf215546Sopenharmony_ci struct rvce_encoder *enc = (struct rvce_encoder *)encoder; 329bf215546Sopenharmony_ci struct rvce_cpb_slot *slot = list_entry(enc->cpb_slots.prev, struct rvce_cpb_slot, list); 330bf215546Sopenharmony_ci 331bf215546Sopenharmony_ci if (!enc->dual_inst || enc->bs_idx > 1) 332bf215546Sopenharmony_ci flush(enc); 333bf215546Sopenharmony_ci 334bf215546Sopenharmony_ci /* update the CPB backtrack with the just encoded frame */ 335bf215546Sopenharmony_ci slot->picture_type = enc->pic.picture_type; 336bf215546Sopenharmony_ci slot->frame_num = enc->pic.frame_num; 337bf215546Sopenharmony_ci slot->pic_order_cnt = enc->pic.pic_order_cnt; 338bf215546Sopenharmony_ci if (!enc->pic.not_referenced) { 339bf215546Sopenharmony_ci list_del(&slot->list); 340bf215546Sopenharmony_ci list_add(&slot->list, &enc->cpb_slots); 341bf215546Sopenharmony_ci } 342bf215546Sopenharmony_ci} 343bf215546Sopenharmony_ci 344bf215546Sopenharmony_cistatic void rvce_get_feedback(struct pipe_video_codec *encoder, void *feedback, unsigned *size) 345bf215546Sopenharmony_ci{ 346bf215546Sopenharmony_ci struct rvce_encoder *enc = (struct rvce_encoder *)encoder; 347bf215546Sopenharmony_ci struct rvid_buffer *fb = feedback; 348bf215546Sopenharmony_ci 349bf215546Sopenharmony_ci if (size) { 350bf215546Sopenharmony_ci uint32_t *ptr = enc->ws->buffer_map(enc->ws, fb->res->buf, &enc->cs, 351bf215546Sopenharmony_ci PIPE_MAP_READ_WRITE | RADEON_MAP_TEMPORARY); 352bf215546Sopenharmony_ci 353bf215546Sopenharmony_ci if (ptr[1]) { 354bf215546Sopenharmony_ci *size = ptr[4] - ptr[9]; 355bf215546Sopenharmony_ci } else { 356bf215546Sopenharmony_ci *size = 0; 357bf215546Sopenharmony_ci } 358bf215546Sopenharmony_ci 359bf215546Sopenharmony_ci enc->ws->buffer_unmap(enc->ws, fb->res->buf); 360bf215546Sopenharmony_ci } 361bf215546Sopenharmony_ci // dump_feedback(enc, fb); 362bf215546Sopenharmony_ci si_vid_destroy_buffer(fb); 363bf215546Sopenharmony_ci FREE(fb); 364bf215546Sopenharmony_ci} 365bf215546Sopenharmony_ci 366bf215546Sopenharmony_ci/** 367bf215546Sopenharmony_ci * flush any outstanding command buffers to the hardware 368bf215546Sopenharmony_ci */ 369bf215546Sopenharmony_cistatic void rvce_flush(struct pipe_video_codec *encoder) 370bf215546Sopenharmony_ci{ 371bf215546Sopenharmony_ci struct rvce_encoder *enc = (struct rvce_encoder *)encoder; 372bf215546Sopenharmony_ci 373bf215546Sopenharmony_ci flush(enc); 374bf215546Sopenharmony_ci} 375bf215546Sopenharmony_ci 376bf215546Sopenharmony_cistatic void rvce_cs_flush(void *ctx, unsigned flags, struct pipe_fence_handle **fence) 377bf215546Sopenharmony_ci{ 378bf215546Sopenharmony_ci // just ignored 379bf215546Sopenharmony_ci} 380bf215546Sopenharmony_ci 381bf215546Sopenharmony_cistruct pipe_video_codec *si_vce_create_encoder(struct pipe_context *context, 382bf215546Sopenharmony_ci const struct pipe_video_codec *templ, 383bf215546Sopenharmony_ci struct radeon_winsys *ws, rvce_get_buffer get_buffer) 384bf215546Sopenharmony_ci{ 385bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)context->screen; 386bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)context; 387bf215546Sopenharmony_ci struct rvce_encoder *enc; 388bf215546Sopenharmony_ci struct pipe_video_buffer *tmp_buf, templat = {}; 389bf215546Sopenharmony_ci struct radeon_surf *tmp_surf; 390bf215546Sopenharmony_ci unsigned cpb_size; 391bf215546Sopenharmony_ci 392bf215546Sopenharmony_ci if (!sscreen->info.vce_fw_version) { 393bf215546Sopenharmony_ci RVID_ERR("Kernel doesn't supports VCE!\n"); 394bf215546Sopenharmony_ci return NULL; 395bf215546Sopenharmony_ci 396bf215546Sopenharmony_ci } else if (!si_vce_is_fw_version_supported(sscreen)) { 397bf215546Sopenharmony_ci RVID_ERR("Unsupported VCE fw version loaded!\n"); 398bf215546Sopenharmony_ci return NULL; 399bf215546Sopenharmony_ci } 400bf215546Sopenharmony_ci 401bf215546Sopenharmony_ci enc = CALLOC_STRUCT(rvce_encoder); 402bf215546Sopenharmony_ci if (!enc) 403bf215546Sopenharmony_ci return NULL; 404bf215546Sopenharmony_ci 405bf215546Sopenharmony_ci if (sscreen->info.is_amdgpu) 406bf215546Sopenharmony_ci enc->use_vm = true; 407bf215546Sopenharmony_ci 408bf215546Sopenharmony_ci enc->use_vui = true; 409bf215546Sopenharmony_ci 410bf215546Sopenharmony_ci if (sscreen->info.family >= CHIP_TONGA && sscreen->info.family != CHIP_STONEY && 411bf215546Sopenharmony_ci sscreen->info.family != CHIP_POLARIS11 && sscreen->info.family != CHIP_POLARIS12 && 412bf215546Sopenharmony_ci sscreen->info.family != CHIP_VEGAM) 413bf215546Sopenharmony_ci enc->dual_pipe = true; 414bf215546Sopenharmony_ci /* TODO enable B frame with dual instance */ 415bf215546Sopenharmony_ci if ((sscreen->info.family >= CHIP_TONGA) && (templ->max_references == 1) && 416bf215546Sopenharmony_ci (sscreen->info.vce_harvest_config == 0)) 417bf215546Sopenharmony_ci enc->dual_inst = true; 418bf215546Sopenharmony_ci 419bf215546Sopenharmony_ci enc->base = *templ; 420bf215546Sopenharmony_ci enc->base.context = context; 421bf215546Sopenharmony_ci 422bf215546Sopenharmony_ci enc->base.destroy = rvce_destroy; 423bf215546Sopenharmony_ci enc->base.begin_frame = rvce_begin_frame; 424bf215546Sopenharmony_ci enc->base.encode_bitstream = rvce_encode_bitstream; 425bf215546Sopenharmony_ci enc->base.end_frame = rvce_end_frame; 426bf215546Sopenharmony_ci enc->base.flush = rvce_flush; 427bf215546Sopenharmony_ci enc->base.get_feedback = rvce_get_feedback; 428bf215546Sopenharmony_ci enc->get_buffer = get_buffer; 429bf215546Sopenharmony_ci 430bf215546Sopenharmony_ci enc->screen = context->screen; 431bf215546Sopenharmony_ci enc->ws = ws; 432bf215546Sopenharmony_ci 433bf215546Sopenharmony_ci if (!ws->cs_create(&enc->cs, sctx->ctx, AMD_IP_VCE, rvce_cs_flush, enc, false)) { 434bf215546Sopenharmony_ci RVID_ERR("Can't get command submission context.\n"); 435bf215546Sopenharmony_ci goto error; 436bf215546Sopenharmony_ci } 437bf215546Sopenharmony_ci 438bf215546Sopenharmony_ci templat.buffer_format = PIPE_FORMAT_NV12; 439bf215546Sopenharmony_ci templat.width = enc->base.width; 440bf215546Sopenharmony_ci templat.height = enc->base.height; 441bf215546Sopenharmony_ci templat.interlaced = false; 442bf215546Sopenharmony_ci if (!(tmp_buf = context->create_video_buffer(context, &templat))) { 443bf215546Sopenharmony_ci RVID_ERR("Can't create video buffer.\n"); 444bf215546Sopenharmony_ci goto error; 445bf215546Sopenharmony_ci } 446bf215546Sopenharmony_ci 447bf215546Sopenharmony_ci enc->cpb_num = get_cpb_num(enc); 448bf215546Sopenharmony_ci if (!enc->cpb_num) 449bf215546Sopenharmony_ci goto error; 450bf215546Sopenharmony_ci 451bf215546Sopenharmony_ci get_buffer(((struct vl_video_buffer *)tmp_buf)->resources[0], NULL, &tmp_surf); 452bf215546Sopenharmony_ci 453bf215546Sopenharmony_ci cpb_size = (sscreen->info.gfx_level < GFX9) 454bf215546Sopenharmony_ci ? align(tmp_surf->u.legacy.level[0].nblk_x * tmp_surf->bpe, 128) * 455bf215546Sopenharmony_ci align(tmp_surf->u.legacy.level[0].nblk_y, 32) 456bf215546Sopenharmony_ci : 457bf215546Sopenharmony_ci 458bf215546Sopenharmony_ci align(tmp_surf->u.gfx9.surf_pitch * tmp_surf->bpe, 256) * 459bf215546Sopenharmony_ci align(tmp_surf->u.gfx9.surf_height, 32); 460bf215546Sopenharmony_ci 461bf215546Sopenharmony_ci cpb_size = cpb_size * 3 / 2; 462bf215546Sopenharmony_ci cpb_size = cpb_size * enc->cpb_num; 463bf215546Sopenharmony_ci if (enc->dual_pipe) 464bf215546Sopenharmony_ci cpb_size += RVCE_MAX_AUX_BUFFER_NUM * RVCE_MAX_BITSTREAM_OUTPUT_ROW_SIZE * 2; 465bf215546Sopenharmony_ci tmp_buf->destroy(tmp_buf); 466bf215546Sopenharmony_ci if (!si_vid_create_buffer(enc->screen, &enc->cpb, cpb_size, PIPE_USAGE_DEFAULT)) { 467bf215546Sopenharmony_ci RVID_ERR("Can't create CPB buffer.\n"); 468bf215546Sopenharmony_ci goto error; 469bf215546Sopenharmony_ci } 470bf215546Sopenharmony_ci 471bf215546Sopenharmony_ci enc->cpb_array = CALLOC(enc->cpb_num, sizeof(struct rvce_cpb_slot)); 472bf215546Sopenharmony_ci if (!enc->cpb_array) 473bf215546Sopenharmony_ci goto error; 474bf215546Sopenharmony_ci 475bf215546Sopenharmony_ci reset_cpb(enc); 476bf215546Sopenharmony_ci 477bf215546Sopenharmony_ci switch (sscreen->info.vce_fw_version) { 478bf215546Sopenharmony_ci case FW_40_2_2: 479bf215546Sopenharmony_ci si_vce_40_2_2_init(enc); 480bf215546Sopenharmony_ci break; 481bf215546Sopenharmony_ci 482bf215546Sopenharmony_ci case FW_50_0_1: 483bf215546Sopenharmony_ci case FW_50_1_2: 484bf215546Sopenharmony_ci case FW_50_10_2: 485bf215546Sopenharmony_ci case FW_50_17_3: 486bf215546Sopenharmony_ci si_vce_50_init(enc); 487bf215546Sopenharmony_ci break; 488bf215546Sopenharmony_ci 489bf215546Sopenharmony_ci case FW_52_0_3: 490bf215546Sopenharmony_ci case FW_52_4_3: 491bf215546Sopenharmony_ci case FW_52_8_3: 492bf215546Sopenharmony_ci si_vce_52_init(enc); 493bf215546Sopenharmony_ci break; 494bf215546Sopenharmony_ci 495bf215546Sopenharmony_ci default: 496bf215546Sopenharmony_ci if ((sscreen->info.vce_fw_version & (0xff << 24)) >= FW_53) { 497bf215546Sopenharmony_ci si_vce_52_init(enc); 498bf215546Sopenharmony_ci } else 499bf215546Sopenharmony_ci goto error; 500bf215546Sopenharmony_ci } 501bf215546Sopenharmony_ci 502bf215546Sopenharmony_ci return &enc->base; 503bf215546Sopenharmony_ci 504bf215546Sopenharmony_cierror: 505bf215546Sopenharmony_ci enc->ws->cs_destroy(&enc->cs); 506bf215546Sopenharmony_ci 507bf215546Sopenharmony_ci si_vid_destroy_buffer(&enc->cpb); 508bf215546Sopenharmony_ci 509bf215546Sopenharmony_ci FREE(enc->cpb_array); 510bf215546Sopenharmony_ci FREE(enc); 511bf215546Sopenharmony_ci return NULL; 512bf215546Sopenharmony_ci} 513bf215546Sopenharmony_ci 514bf215546Sopenharmony_ci/** 515bf215546Sopenharmony_ci * check if kernel has the right fw version loaded 516bf215546Sopenharmony_ci */ 517bf215546Sopenharmony_cibool si_vce_is_fw_version_supported(struct si_screen *sscreen) 518bf215546Sopenharmony_ci{ 519bf215546Sopenharmony_ci switch (sscreen->info.vce_fw_version) { 520bf215546Sopenharmony_ci case FW_40_2_2: 521bf215546Sopenharmony_ci case FW_50_0_1: 522bf215546Sopenharmony_ci case FW_50_1_2: 523bf215546Sopenharmony_ci case FW_50_10_2: 524bf215546Sopenharmony_ci case FW_50_17_3: 525bf215546Sopenharmony_ci case FW_52_0_3: 526bf215546Sopenharmony_ci case FW_52_4_3: 527bf215546Sopenharmony_ci case FW_52_8_3: 528bf215546Sopenharmony_ci return true; 529bf215546Sopenharmony_ci default: 530bf215546Sopenharmony_ci if ((sscreen->info.vce_fw_version & (0xff << 24)) >= FW_53) 531bf215546Sopenharmony_ci return true; 532bf215546Sopenharmony_ci else 533bf215546Sopenharmony_ci return false; 534bf215546Sopenharmony_ci } 535bf215546Sopenharmony_ci} 536bf215546Sopenharmony_ci 537bf215546Sopenharmony_ci/** 538bf215546Sopenharmony_ci * Add the buffer as relocation to the current command submission 539bf215546Sopenharmony_ci */ 540bf215546Sopenharmony_civoid si_vce_add_buffer(struct rvce_encoder *enc, struct pb_buffer *buf, unsigned usage, 541bf215546Sopenharmony_ci enum radeon_bo_domain domain, signed offset) 542bf215546Sopenharmony_ci{ 543bf215546Sopenharmony_ci int reloc_idx; 544bf215546Sopenharmony_ci 545bf215546Sopenharmony_ci reloc_idx = enc->ws->cs_add_buffer(&enc->cs, buf, usage | RADEON_USAGE_SYNCHRONIZED, domain); 546bf215546Sopenharmony_ci if (enc->use_vm) { 547bf215546Sopenharmony_ci uint64_t addr; 548bf215546Sopenharmony_ci addr = enc->ws->buffer_get_virtual_address(buf); 549bf215546Sopenharmony_ci addr = addr + offset; 550bf215546Sopenharmony_ci RVCE_CS(addr >> 32); 551bf215546Sopenharmony_ci RVCE_CS(addr); 552bf215546Sopenharmony_ci } else { 553bf215546Sopenharmony_ci offset += enc->ws->buffer_get_reloc_offset(buf); 554bf215546Sopenharmony_ci RVCE_CS(reloc_idx * 4); 555bf215546Sopenharmony_ci RVCE_CS(offset); 556bf215546Sopenharmony_ci } 557bf215546Sopenharmony_ci} 558