svga: Add ASTC formats to format table.
[mesa.git] / src / gallium / drivers / radeon / radeon_vce.c
index a7dfcda481650f0f2c8b3e7d812e1b45daae132b..8a60441c056d4bbbd52776460a200fcecba470ce 100644 (file)
 
 #include "vl/vl_video_buffer.h"
 
-#include "../../winsys/radeon/drm/radeon_winsys.h"
 #include "r600_pipe_common.h"
 #include "radeon_video.h"
 #include "radeon_vce.h"
 
+#define FW_40_2_2 ((40 << 24) | (2 << 16) | (2 << 8))
+#define FW_50_0_1 ((50 << 24) | (0 << 16) | (1 << 8))
+#define FW_50_1_2 ((50 << 24) | (1 << 16) | (2 << 8))
+#define FW_50_10_2 ((50 << 24) | (10 << 16) | (2 << 8))
+#define FW_50_17_3 ((50 << 24) | (17 << 16) | (3 << 8))
+#define FW_52_0_3 ((52 << 24) | (0 << 16) | (3 << 8))
+
 /**
  * flush commands to the hardware
  */
 static void flush(struct rvce_encoder *enc)
 {
-       enc->ws->cs_flush(enc->cs, RADEON_FLUSH_ASYNC, 0);
+       enc->ws->cs_flush(enc->cs, RADEON_FLUSH_ASYNC, NULL, 0);
+       enc->task_info_idx = 0;
+       enc->bs_idx = 0;
 }
 
 #if 0
 static void dump_feedback(struct rvce_encoder *enc, struct rvid_buffer *fb)
 {
-       uint32_t *ptr = enc->ws->buffer_map(fb->cs_handle, enc->cs, PIPE_TRANSFER_READ_WRITE);
+       uint32_t *ptr = enc->ws->buffer_map(fb->res->cs_buf, enc->cs, PIPE_TRANSFER_READ_WRITE);
        unsigned i = 0;
        fprintf(stderr, "\n");
        fprintf(stderr, "encStatus:\t\t\t%08x\n", ptr[i++]);
@@ -75,7 +83,7 @@ static void dump_feedback(struct rvce_encoder *enc, struct rvid_buffer *fb)
        fprintf(stderr, "seiPrivatePackageOffset:\t%08x\n", ptr[i++]);
        fprintf(stderr, "seiPrivatePackageSize:\t\t%08x\n", ptr[i++]);
        fprintf(stderr, "\n");
-       enc->ws->buffer_unmap(fb->cs_handle);
+       enc->ws->buffer_unmap(fb->res->cs_buf);
 }
 #endif
 
@@ -87,7 +95,7 @@ static void reset_cpb(struct rvce_encoder *enc)
        unsigned i;
 
        LIST_INITHEAD(&enc->cpb_slots);
-       for (i = 0; i < RVCE_NUM_CPB_FRAMES; ++i) {
+       for (i = 0; i < enc->cpb_num; ++i) {
                struct rvce_cpb_slot *slot = &enc->cpb_array[i];
                slot->index = i;
                slot->picture_type = PIPE_H264_ENC_PICTURE_TYPE_SKIP;
@@ -130,6 +138,97 @@ static void sort_cpb(struct rvce_encoder *enc)
        }
 }
 
+/**
+ * get number of cpbs based on dpb
+ */
+static unsigned get_cpb_num(struct rvce_encoder *enc)
+{
+       unsigned w = align(enc->base.width, 16) / 16;
+       unsigned h = align(enc->base.height, 16) / 16;
+       unsigned dpb;
+
+       switch (enc->base.level) {
+       case 10:
+               dpb = 396;
+               break;
+       case 11:
+               dpb = 900;
+               break;
+       case 12:
+       case 13:
+       case 20:
+               dpb = 2376;
+               break;
+       case 21:
+               dpb = 4752;
+               break;
+       case 22:
+       case 30:
+               dpb = 8100;
+               break;
+       case 31:
+               dpb = 18000;
+               break;
+       case 32:
+               dpb = 20480;
+               break;
+       case 40:
+       case 41:
+               dpb = 32768;
+               break;
+       default:
+       case 42:
+               dpb = 34816;
+               break;
+       case 50:
+               dpb = 110400;
+               break;
+       case 51:
+               dpb = 184320;
+               break;
+       }
+
+       return MIN2(dpb / (w * h), 16);
+}
+
+/**
+ * Get the slot for the currently encoded frame
+ */
+struct rvce_cpb_slot *current_slot(struct rvce_encoder *enc)
+{
+       return LIST_ENTRY(struct rvce_cpb_slot, enc->cpb_slots.prev, list);
+}
+
+/**
+ * Get the slot for L0
+ */
+struct rvce_cpb_slot *l0_slot(struct rvce_encoder *enc)
+{
+       return LIST_ENTRY(struct rvce_cpb_slot, enc->cpb_slots.next, list);
+}
+
+/**
+ * Get the slot for L1
+ */
+struct rvce_cpb_slot *l1_slot(struct rvce_encoder *enc)
+{
+       return LIST_ENTRY(struct rvce_cpb_slot, enc->cpb_slots.next->next, list);
+}
+
+/**
+ * Calculate the offsets into the CPB
+ */
+void rvce_frame_offset(struct rvce_encoder *enc, struct rvce_cpb_slot *slot,
+                      signed *luma_offset, signed *chroma_offset)
+{
+       unsigned pitch = align(enc->luma->level[0].pitch_bytes, 128);
+       unsigned vpitch = align(enc->luma->npix_y, 16);
+       unsigned fsize = pitch * (vpitch + vpitch / 2);
+
+       *luma_offset = slot->index * fsize;
+       *chroma_offset = *luma_offset + pitch * vpitch;
+}
+
 /**
  * destroy this video encoder
  */
@@ -138,7 +237,7 @@ static void rvce_destroy(struct pipe_video_codec *encoder)
        struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
        if (enc->stream_handle) {
                struct rvid_buffer fb;
-               rvid_create_buffer(enc->ws, &fb, 512, RADEON_DOMAIN_GTT);
+               rvid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING);
                enc->fb = &fb;
                enc->session(enc);
                enc->feedback(enc);
@@ -180,26 +279,23 @@ static void rvce_begin_frame(struct pipe_video_codec *encoder,
        if (!enc->stream_handle) {
                struct rvid_buffer fb;
                enc->stream_handle = rvid_alloc_stream_handle();
-               rvid_create_buffer(enc->ws, &fb, 512, RADEON_DOMAIN_GTT);
+               rvid_create_buffer(enc->screen, &fb, 512, PIPE_USAGE_STAGING);
                enc->fb = &fb;
                enc->session(enc);
                enc->create(enc);
-               enc->rate_control(enc);
-               need_rate_control = false;
-               enc->config_extension(enc);
-               enc->motion_estimation(enc);
-               enc->rdo(enc);
-               enc->pic_control(enc);
+               enc->config(enc);
                enc->feedback(enc);
                flush(enc);
                //dump_feedback(enc, &fb);
                rvid_destroy_buffer(&fb);
+               need_rate_control = false;
        }
 
-       enc->session(enc);
-
-       if (need_rate_control)
-               enc->rate_control(enc);
+       if (need_rate_control) {
+               enc->session(enc);
+               enc->config(enc);
+               flush(enc);
+       }
 }
 
 static void rvce_encode_bitstream(struct pipe_video_codec *encoder,
@@ -212,10 +308,12 @@ static void rvce_encode_bitstream(struct pipe_video_codec *encoder,
        enc->bs_size = destination->width0;
 
        *fb = enc->fb = CALLOC_STRUCT(rvid_buffer);
-       if (!rvid_create_buffer(enc->ws, enc->fb, 512, RADEON_DOMAIN_GTT)) {
+       if (!rvid_create_buffer(enc->screen, enc->fb, 512, PIPE_USAGE_STAGING)) {
                RVID_ERR("Can't create feedback buffer.\n");
                return;
        }
+       if (!enc->cs->cdw)
+               enc->session(enc);
        enc->encode(enc);
        enc->feedback(enc);
 }
@@ -228,14 +326,17 @@ static void rvce_end_frame(struct pipe_video_codec *encoder,
        struct rvce_cpb_slot *slot = LIST_ENTRY(
                struct rvce_cpb_slot, enc->cpb_slots.prev, list);
 
-       flush(enc);
+       if (!enc->dual_inst || enc->bs_idx > 1)
+               flush(enc);
 
        /* update the CPB backtrack with the just encoded frame */
-       LIST_DEL(&slot->list);
        slot->picture_type = enc->pic.picture_type;
        slot->frame_num = enc->pic.frame_num;
        slot->pic_order_cnt = enc->pic.pic_order_cnt;
-       LIST_ADD(&slot->list, &enc->cpb_slots);
+       if (!enc->pic.not_referenced) {
+               LIST_DEL(&slot->list);
+               LIST_ADD(&slot->list, &enc->cpb_slots);
+       }
 }
 
 static void rvce_get_feedback(struct pipe_video_codec *encoder,
@@ -245,7 +346,7 @@ static void rvce_get_feedback(struct pipe_video_codec *encoder,
        struct rvid_buffer *fb = feedback;
 
        if (size) {
-               uint32_t *ptr = enc->ws->buffer_map(fb->cs_handle, enc->cs, PIPE_TRANSFER_READ_WRITE);
+               uint32_t *ptr = enc->ws->buffer_map(fb->res->cs_buf, enc->cs, PIPE_TRANSFER_READ_WRITE);
 
                if (ptr[1]) {
                        *size = ptr[4] - ptr[9];
@@ -253,7 +354,7 @@ static void rvce_get_feedback(struct pipe_video_codec *encoder,
                        *size = 0;
                }
 
-               enc->ws->buffer_unmap(fb->cs_handle);
+               enc->ws->buffer_unmap(fb->res->cs_buf);
        }
        //dump_feedback(enc, fb);
        rvid_destroy_buffer(fb);
@@ -265,9 +366,13 @@ static void rvce_get_feedback(struct pipe_video_codec *encoder,
  */
 static void rvce_flush(struct pipe_video_codec *encoder)
 {
+       struct rvce_encoder *enc = (struct rvce_encoder*)encoder;
+
+       flush(enc);
 }
 
-static void rvce_cs_flush(void *ctx, unsigned flags)
+static void rvce_cs_flush(void *ctx, unsigned flags,
+                         struct pipe_fence_handle **fence)
 {
        // just ignored
 }
@@ -278,9 +383,10 @@ struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
                                             rvce_get_buffer get_buffer)
 {
        struct r600_common_screen *rscreen = (struct r600_common_screen *)context->screen;
+       struct r600_common_context *rctx = (struct r600_common_context*)context;
        struct rvce_encoder *enc;
        struct pipe_video_buffer *tmp_buf, templat = {};
-       struct radeon_surface *tmp_surf;
+       struct radeon_surf *tmp_surf;
        unsigned cpb_size;
 
        if (!rscreen->info.vce_fw_version) {
@@ -296,6 +402,19 @@ struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
        if (!enc)
                return NULL;
 
+       if (rscreen->info.drm_major == 3)
+               enc->use_vm = true;
+       if ((rscreen->info.drm_major > 2) || (rscreen->info.drm_minor >= 42))
+               enc->use_vui = true;
+       if (rscreen->info.family >= CHIP_TONGA &&
+             rscreen->info.family != CHIP_STONEY)
+               enc->dual_pipe = true;
+       /* TODO enable B frame with dual instance */
+       if ((rscreen->info.family >= CHIP_TONGA) &&
+               (templ->max_references == 1) &&
+               (rscreen->info.vce_harvest_config == 0))
+               enc->dual_inst = true;
+
        enc->base = *templ;
        enc->base.context = context;
 
@@ -307,14 +426,14 @@ struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
        enc->base.get_feedback = rvce_get_feedback;
        enc->get_buffer = get_buffer;
 
+       enc->screen = context->screen;
        enc->ws = ws;
-       enc->cs = ws->cs_create(ws, RING_VCE, NULL);
+       enc->cs = ws->cs_create(rctx->ctx, RING_VCE, rvce_cs_flush, enc, NULL);
        if (!enc->cs) {
                RVID_ERR("Can't get command submission context.\n");
                goto error;
        }
 
-       enc->ws->cs_set_flush_callback(enc->cs, rvce_cs_flush, enc);
        templat.buffer_format = PIPE_FORMAT_NV12;
        templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
        templat.width = enc->base.width;
@@ -325,24 +444,49 @@ struct pipe_video_codec *rvce_create_encoder(struct pipe_context *context,
                goto error;
        }
 
+       enc->cpb_num = get_cpb_num(enc);
+       if (!enc->cpb_num)
+               goto error;
+
        get_buffer(((struct vl_video_buffer *)tmp_buf)->resources[0], NULL, &tmp_surf);
        cpb_size = align(tmp_surf->level[0].pitch_bytes, 128);
        cpb_size = cpb_size * align(tmp_surf->npix_y, 16);
        cpb_size = cpb_size * 3 / 2;
-       cpb_size = cpb_size * RVCE_NUM_CPB_FRAMES;
+       cpb_size = cpb_size * enc->cpb_num;
+       if (enc->dual_pipe)
+               cpb_size +=  RVCE_MAX_AUX_BUFFER_NUM *
+                       RVCE_MAX_BITSTREAM_OUTPUT_ROW_SIZE * 2;
        tmp_buf->destroy(tmp_buf);
-       if (!rvid_create_buffer(enc->ws, &enc->cpb, cpb_size, RADEON_DOMAIN_VRAM)) {
+       if (!rvid_create_buffer(enc->screen, &enc->cpb, cpb_size, PIPE_USAGE_DEFAULT)) {
                RVID_ERR("Can't create CPB buffer.\n");
                goto error;
        }
 
-       enc->cpb_array = CALLOC(RVCE_NUM_CPB_FRAMES, sizeof(struct rvce_cpb_slot));
+       enc->cpb_array = CALLOC(enc->cpb_num, sizeof(struct rvce_cpb_slot));
        if (!enc->cpb_array)
                goto error;
 
        reset_cpb(enc);
 
-       radeon_vce_40_2_2_init(enc);
+       switch (rscreen->info.vce_fw_version) {
+       case FW_40_2_2:
+               radeon_vce_40_2_2_init(enc);
+               break;
+
+       case FW_50_0_1:
+       case FW_50_1_2:
+       case FW_50_10_2:
+       case FW_50_17_3:
+               radeon_vce_50_init(enc);
+               break;
+
+       case FW_52_0_3:
+               radeon_vce_52_init(enc);
+               break;
+
+       default:
+               goto error;
+       }
 
        return &enc->base;
 
@@ -362,5 +506,37 @@ error:
  */
 bool rvce_is_fw_version_supported(struct r600_common_screen *rscreen)
 {
-       return rscreen->info.vce_fw_version == ((40 << 24) | (2 << 16) | (2 << 8));
+       switch (rscreen->info.vce_fw_version) {
+       case FW_40_2_2:
+       case FW_50_0_1:
+       case FW_50_1_2:
+       case FW_50_10_2:
+       case FW_50_17_3:
+       case FW_52_0_3:
+               return true;
+       default:
+               return false;
+       }
+}
+
+/**
+ * Add the buffer as relocation to the current command submission
+ */
+void rvce_add_buffer(struct rvce_encoder *enc, struct radeon_winsys_cs_handle *buf,
+                     enum radeon_bo_usage usage, enum radeon_bo_domain domain,
+                     signed offset)
+{
+       int reloc_idx;
+
+       reloc_idx = enc->ws->cs_add_buffer(enc->cs, buf, usage, domain, RADEON_PRIO_VCE);
+       if (enc->use_vm) {
+               uint64_t addr;
+               addr = enc->ws->buffer_get_virtual_address(buf);
+               addr = addr + offset;
+               RVCE_CS(addr >> 32);
+               RVCE_CS(addr);
+       } else {
+               RVCE_CS(reloc_idx * 4);
+               RVCE_CS(offset);
+       }
 }