/*******************************************************************************
* Copyright (C) 2018 Cadence Design Systems, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to use this Software with Cadence processor cores only and 
* not with any other processors and platforms, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************/

/*******************************************************************************
 * xa-class-base.c
 *
 * Generic audio codec task implementation
 *
 ******************************************************************************/

#define MODULE_TAG                      BASE

/*******************************************************************************
 * Includes
 ******************************************************************************/

#include "xf.h"
#include "xa-class-base.h"

/*******************************************************************************
 * Tracing configuration
 ******************************************************************************/

TRACE_TAG(INIT, 1);
TRACE_TAG(WARNING, 1);
TRACE_TAG(SETUP, 1);
TRACE_TAG(EXEC, 1);

/*******************************************************************************
 * Internal functions definitions
 ******************************************************************************/

/* ...codec pre-initialization */
static XA_ERRORCODE xa_base_preinit(XACodecBase *base, u32 core)
{
    WORD32      n;

    /* ...codec must be empty */
    XF_CHK_ERR(base->state == 0, XA_API_FATAL_INVALID_CMD);

    /* ...get API structure size */
    XA_API(base, XA_API_CMD_GET_API_SIZE, 0, &n);

    /* ...allocate memory for codec API structure (4-bytes aligned) */
    XMALLOC(&base->api, n, 4, core);

    /* ...set default config parameters */
    XA_API(base, XA_API_CMD_INIT, XA_CMD_TYPE_INIT_API_PRE_CONFIG_PARAMS, NULL);

    /* ...get memory info tables size */
    if (XA_API(base, XA_API_CMD_GET_MEMTABS_SIZE, 0, &n), n != 0)
    {
        /* ...allocate memory for tables (4-bytes aligned) */
        XMALLOC(&base->mem_tabs, n, 4, core);

        /* ...set pointer for process memory tables */
        XA_API(base, XA_API_CMD_SET_MEMTABS_PTR, 0, base->mem_tabs.addr);
    }
    
    TRACE(INIT, _b("Codec[%p] pre-initialization completed"), base);
    
    return XA_NO_ERROR;
}

/* ...post-initialization setup */
static XA_ERRORCODE xa_base_postinit(XACodecBase *base, u32 core)
{
    WORD32  n, i;
    
    /* ...issue post-config command and determine the buffer requirements */
    XA_API(base, XA_API_CMD_INIT, XA_CMD_TYPE_INIT_API_POST_CONFIG_PARAMS, NULL);

    /* ...get number of memory tables required */
    XA_API(base, XA_API_CMD_GET_N_MEMTABS, 0, &n);

    /* ...set scratch buffer in advance (as codec not necessarily exposes it) */
    base->scratch = XF_CORE_DATA(core)->scratch;

    /* ...allocate memory buffers */
    for (i = 0; i < n; i++)
    {
        WORD32      size, align, type;

        TRACE(1, _b("i = %u (of %u)"), (u32)i, (u32)n);
        
        /* ...get memory type */
        XA_API(base, XA_API_CMD_GET_MEM_INFO_TYPE, i, &type);

        /* ...get memory size of i-th buffer */
        XA_API(base, XA_API_CMD_GET_MEM_INFO_SIZE, i, &size);

        /* ...get alignment */
        XA_API(base, XA_API_CMD_GET_MEM_INFO_ALIGNMENT, i, &align);

        /* ...process individual buffer */
        switch (type)
        {
        case XA_MEMTYPE_SCRATCH:
            /* ...scratch memory is shared among all codecs; check its validity */
            XF_CHK_ERR(size <= XF_CFG_CODEC_SCRATCHMEM_SIZE, XA_API_FATAL_MEM_ALLOC);

            /* ...make sure alignment is sane */
            XF_CHK_ERR((XF_CFG_CODEC_SCRATCHMEM_ALIGN & (align - 1)) == 0, XA_API_FATAL_MEM_ALIGN);

            /* ...set the scratch memory pointer */
            XA_API(base, XA_API_CMD_SET_MEM_PTR, i, base->scratch);

            TRACE(INIT, _b("Mem tab %d: sz=%d al=%d ty=%d Scratch memory (%p)"), i, size, align, type, base->scratch);

            break;

        case XA_MEMTYPE_PERSIST:
            /* ...allocate persistent memory */
            XMALLOC(&base->persist, size, align, core);

            /* ...and set the pointer instantly */
            XA_API(base, XA_API_CMD_SET_MEM_PTR, i, base->persist.addr);

            TRACE(INIT, _b("Mem tab %d: sz=%d al=%d ty=%d Persistent memory (%p)"), i, size, align, type, base->persist.addr);

            break;

        case XA_MEMTYPE_INPUT:
        case XA_MEMTYPE_OUTPUT:
            /* ...input/output buffer specification; pass to codec function */
            CODEC_API(base, memtab, i, type, size, align, core);

            break;

        default:
            /* ...unrecognized memory type */
            TRACE(ERROR, _x("Invalid memory type: [%d]=(%u, %u, %u)"), i, type, size, align);
            return XA_API_FATAL_INVALID_CMD_TYPE;
        }
    }

    TRACE(INIT, _b("Codec[%p] post-initialization completed (api:%p[%u])"), base, base->api.addr, base->api.size);

    return XA_NO_ERROR;
}

/*******************************************************************************
 * Commands processing
 ******************************************************************************/

/* ...SET-PARAM processing (enabled in all states) */
XA_ERRORCODE xa_base_set_param(XACodecBase *base, xf_message_t *m)
{
    xf_set_param_msg_t     *cmd = m->buffer;
    xf_set_param_item_t    *param = &cmd->item[0];
    WORD32                  n, i;

    /* ...calculate total amount of parameters */
    n = m->length / sizeof(*param);

    /* ...check the message length is sane */
    XF_CHK_ERR(m->length == XF_SET_PARAM_CMD_LEN(n), XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...apply all parameters; pass to codec-specific function */
    for (i = 0; i < n; i++)
    {
        TRACE(SETUP, _b("set-param[%p]: [%u]=%u"), base, param[i].id, param[i].value);

        if (base->setparam)
        {
            CODEC_API(base, setparam, param[i].id, &param[i].value);
        }
        else
        {
            XA_API(base, XA_API_CMD_SET_CONFIG_PARAM, param[i].id, &param[i].value);
        }
    }

    /* ...check if we need to do post-initialization */
    if ((base->state & XA_BASE_FLAG_POSTINIT) == 0)
    {
        /* ...do post-initialization step */
        XA_CHK(xa_base_postinit(base, XF_MSG_DST_CORE(m->id)));
        
        /* ...mark the codec static configuration is set */
        base->state ^= XA_BASE_FLAG_POSTINIT | XA_BASE_FLAG_RUNTIME_INIT;
    }
    
    /* ...complete message processing; output buffer is empty */
    xf_response_ok(m);

    return XA_NO_ERROR;
}

/* ...GET-PARAM message processing (enabled in all states) */
XA_ERRORCODE xa_base_get_param(XACodecBase *base, xf_message_t *m)
{
    xf_get_param_msg_t *cmd = m->buffer;
    u32                *id = &cmd->c.id[0];
    u32                *value = &cmd->r.value[0];
    u32                 n, i;

    /* ...calculate amount of parameters */
    n = m->length / sizeof(*id);

    /* ...check input parameter length */
    XF_CHK_ERR(XF_GET_PARAM_CMD_LEN(n) == m->length, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...retrieve the collection of codec  parameters */
    for (i = 0; i < n; i++)
    {
        /* ...place the result into same location */
        if (base->getparam)
        {
            CODEC_API(base, getparam, id[i], &value[i]);
        }
        else
        {
            XA_API(base, XA_API_CMD_GET_CONFIG_PARAM, id[i], &value[i]);
        }
    }

    /* ...complete message specifying output buffer size */
    xf_response_data(m, XF_GET_PARAM_RSP_LEN(n));

    return XA_NO_ERROR;
}

/* ...SET-PARAM-EXT processing (enabled in all states) */
XA_ERRORCODE xa_base_set_param_ext(XACodecBase *base, xf_message_t *m)
{
    xf_ext_param_msg_t *cmd = m->buffer;
    u16                 length = m->length;
    u16                 remaining = (length + 3) & ~3;
    u16                 i;
    
    for (i = 0; TRACE_CFG(SETUP) && i < remaining; i += 16)
    {
        TRACE(SETUP, _b("[%03x]: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X"),
              i, 
              ((u8 *)m->buffer)[i + 0], ((u8 *)m->buffer)[i + 1],
              ((u8 *)m->buffer)[i + 2], ((u8 *)m->buffer)[i + 3],
              ((u8 *)m->buffer)[i + 4], ((u8 *)m->buffer)[i + 5],
              ((u8 *)m->buffer)[i + 6], ((u8 *)m->buffer)[i + 7],
              ((u8 *)m->buffer)[i + 8], ((u8 *)m->buffer)[i + 9],
              ((u8 *)m->buffer)[i + 10], ((u8 *)m->buffer)[i + 11],
              ((u8 *)m->buffer)[i + 12], ((u8 *)m->buffer)[i + 13],
              ((u8 *)m->buffer)[i + 14], ((u8 *)m->buffer)[i + 15]);
    }    

    /* ...process all parameters encapsulated in buffer */
    while (remaining >= sizeof(*cmd))
    {
        u16     id = cmd->desc.id;
        u16     dlen = cmd->desc.length;
        u16     dsize = (dlen + 3) & ~3;
        u16     pad = dlen & 3;

        /* ...cut-off descriptor header */
        remaining -= sizeof(*cmd);

        TRACE(SETUP, _b("remaining:%u, desc_size:%u"), (u32)remaining, (u32)dsize);
        
        /* ...make sure length is sufficient */        
        XF_CHK_ERR(remaining >= dsize, XA_API_FATAL_INVALID_CMD_TYPE);
        
        /* ...pad remaining bytes with zeroes */
        (pad ? memset(cmd->data + dlen, 0, 4 - pad) : 0);

        TRACE(SETUP, _b("set-ext-param[%p]: [%u]:%u - [%02X:%02X:%02X:%02X:...]"), base, id, dsize, cmd->data[0], cmd->data[1], cmd->data[2], cmd->data[3]);

        /* ...apply parameter */
        XA_API(base, XA_API_CMD_SET_CONFIG_PARAM, id, cmd->data);

        /* ...move to next item (keep 4-bytes alignment for descriptor) */
        cmd = (xf_ext_param_msg_t *)(&cmd->data[0] + dsize), remaining -= dsize;
    }
    
    /* ...check the message is fully processed */
    XF_CHK_ERR(remaining == 0, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...complete message processing; output buffer is empty */
    //xf_response_ok(m);

    /* ...unfortunately, it looks like a bug of the library that updates the memory
     * and leaves it in a dirty state causing subsequent cache inconsistency - tbd
     */
    xf_response_data(m, length);

    return XA_NO_ERROR;
}

/* ...GET-PARAM-EXT message processing (enabled in all states) */
XA_ERRORCODE xa_base_get_param_ext(XACodecBase *base, xf_message_t *m)
{
    xf_ext_param_msg_t *cmd = m->buffer;
    u32                 length = m->length;
    u32                 remaining = (length + 3) & ~3;
    int                 i;

    for (i = 0; TRACE_CFG(SETUP) && i < remaining; i += 16)
    {
        TRACE(SETUP, _b("[%03x]: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X"),
              i, 
              ((u8 *)m->buffer)[i + 0], ((u8 *)m->buffer)[i + 1],
              ((u8 *)m->buffer)[i + 2], ((u8 *)m->buffer)[i + 3],
              ((u8 *)m->buffer)[i + 4], ((u8 *)m->buffer)[i + 5],
              ((u8 *)m->buffer)[i + 6], ((u8 *)m->buffer)[i + 7],
              ((u8 *)m->buffer)[i + 8], ((u8 *)m->buffer)[i + 9],
              ((u8 *)m->buffer)[i + 10], ((u8 *)m->buffer)[i + 11],
              ((u8 *)m->buffer)[i + 12], ((u8 *)m->buffer)[i + 13],
              ((u8 *)m->buffer)[i + 14], ((u8 *)m->buffer)[i + 15]);
    }
    
    /* ...process all parameters encapsulated in buffer */
    while (remaining >= sizeof(*cmd))
    {
        u16     id = cmd->desc.id;
        u16     len = cmd->desc.length;
        u16     size = (len + 3) & ~3;
        u8      pad = len & 3;

        /* ...cut-off command header */
        remaining -= sizeof(*cmd);

        /* ...make sure data buffer has sufficient length */
        XF_CHK_ERR(remaining >= size, XA_API_FATAL_INVALID_CMD_TYPE);
        
        /* ...retrieve parameter from buffer (care about alignment? - tbd) */
        XA_API(base, XA_API_CMD_GET_CONFIG_PARAM, id, cmd->data);

        /* ...pad remaininig bytes with zeroes */
        (pad ? memset(cmd->data + len, 0, 4 - pad) : 0);

        TRACE(SETUP, _b("get-ext-param[%p]: [%u]:%u - [%02X:%02X:%02X:%02X:...]"), base, id, size, cmd->data[0], cmd->data[1], cmd->data[2], cmd->data[3]);
        
        /* ...move to next item (alignment issues? - tbd) */
        cmd = (xf_ext_param_msg_t *)(&cmd->data[0] + size), remaining -= size;
    }
    
    /* ...check the message is fully processed */
    XF_CHK_ERR(remaining == 0, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...complete message processing; output buffer has the same length */
    xf_response_data(m, length);

    return XA_NO_ERROR;
}

/*******************************************************************************
 * Command/data processing functions
 ******************************************************************************/

/* ...generic codec data processing */
static XA_ERRORCODE xa_base_process(XACodecBase *base)
{
    XA_ERRORCODE    error;
    WORD32          done;

    /* ...clear internal scheduling flag */
    base->state &= ~XA_BASE_FLAG_SCHEDULE;
        
    /* ...codec-specific preprocessing (buffer maintenance) */
    if ((error = CODEC_API(base, preprocess)) != XA_NO_ERROR)
    {
        /* ...return non-fatal codec error */
        return error;
    }

    /* ...execution step */
    if (base->state & XA_BASE_FLAG_RUNTIME_INIT)
    {
        /* ...kick initialization process */
        XA_API(base, XA_API_CMD_INIT, XA_CMD_TYPE_INIT_PROCESS, NULL);

        /* ...check if initialization is completed */
        XA_API(base, XA_API_CMD_INIT, XA_CMD_TYPE_INIT_DONE_QUERY, &done);

        TRACE(EXEC, _b("Initialization result: %d"), done);

        /* ...switch to execution state if required */
        (done ? base->state ^= XA_BASE_FLAG_RUNTIME_INIT | XA_BASE_FLAG_EXECUTION : 0);
    }
    else if (base->state & XA_BASE_FLAG_EXECUTION)
    {
        TRACE(1, _b("do exec"));
        
        /* ...execute decoding process */
        XA_API(base, XA_API_CMD_EXECUTE, XA_CMD_TYPE_DO_EXECUTE, NULL);

        /* ...check for end-of-stream condition */
        XA_API(base, XA_API_CMD_EXECUTE, XA_CMD_TYPE_DONE_QUERY, &done);

        TRACE(EXEC, _b("Execution result: %d"), done);

        /* ...mark the output path is done to release all queued buffers */
        (done ? base->state ^= XA_BASE_FLAG_EXECUTION | XA_BASE_FLAG_COMPLETED : 0);
    }

    /* ...codec-specific buffer post-processing */
    return CODEC_API(base, postprocess, done);
}
                              
/* ...message-processing function (component entry point) */
static int xa_base_command(xf_component_t *component, xf_message_t *m)
{
    XACodecBase    *base = (XACodecBase *) component;
    u32             cmd;
    
    /* ...invoke data-processing function if message is null */
    if (m == NULL)
    {
        XF_CHK_ERR(!XA_ERROR_SEVERITY(xa_base_process(base)), -EPIPE);
        return 0;
    }

    /* ...process the command */
    TRACE(EXEC, _b("[%p]:state[%X]:(%X, %d, %p)"), base, base->state, m->opcode, m->length, m->buffer);

    /* ...bail out if this is forced termination command (I do have a map; maybe I'd better have a hook? - tbd) */
    if ((cmd = XF_OPCODE_TYPE(m->opcode)) == XF_OPCODE_TYPE(XF_UNREGISTER))
    {
        TRACE(INIT, _b("force component[%p] termination"), base);
        return -1;    
    }
    
    /* ...check opcode is valid */
    XF_CHK_ERR(cmd < base->command_num, -EINVAL);

    /* ...and has a hook */
    XF_CHK_ERR(base->command[cmd] != NULL, -EINVAL);
    
    /* ...pass control to specific command */
    XF_CHK_ERR(!XA_ERROR_SEVERITY(base->command[cmd](base, m)), -EPIPE);

    /* ...execution completed successfully */
    return 0;
}

/*******************************************************************************
 * Base codec API
 ******************************************************************************/

/* ...data processing scheduling */
void xa_base_schedule(XACodecBase *base, u32 dts)
{
    if ((base->state & XA_BASE_FLAG_SCHEDULE) == 0)
    {
        /* ...schedule component task execution */
        xf_component_schedule(&base->component, dts);

        /* ...and put scheduling flag */
        base->state ^= XA_BASE_FLAG_SCHEDULE;
    }
    else
    {
        TRACE(EXEC, _b("codec[%p] processing pending"), base);
    }
}

/* ...cancel data processing */
void xa_base_cancel(XACodecBase *base)
{
    if (base->state & XA_BASE_FLAG_SCHEDULE)
    {
        /* ...cancel scheduled codec task */
        xf_component_cancel(&base->component);

        /* ...and clear scheduling flag */
        base->state ^= XA_BASE_FLAG_SCHEDULE;

        TRACE(EXEC, _b("codec[%p] processing cancelled"), base);
    }
}

/* ...base codec destructor */
void xa_base_destroy(XACodecBase *base, u32 size, u32 core)
{
    /* ...deallocate all resources */
    xf_mm_free_buffer(&base->persist, core);
    xf_mm_free_buffer(&base->mem_tabs, core);
    xf_mm_free_buffer(&base->api, core);

    /* ...destroy codec structure (and task) itself */
    xf_mem_free(base, size, core, 0);

    TRACE(INIT, _b("codec[%p]:%u destroyed"), base, core);
}

/* ...generic codec initialization routine */
XACodecBase * xa_base_factory(u32 core, u32 size, xa_codec_func_t process)
{
    XACodecBase    *base;

    /* ...make sure the size is sane */
    XF_CHK_ERR(size >= sizeof(XACodecBase), NULL);
    
    /* ...allocate local memory for codec structure */
    XF_CHK_ERR(base = xf_mem_alloc(size, 0, core, 0), NULL);
 
    /* ...reset codec memory */
    memset(base, 0, size);

    /* ...set low-level codec API function */
    base->process = process;

    /* ...set message processing function */
    base->component.entry = xa_base_command;

    /* ...do basic initialization */
    if (xa_base_preinit(base, core) != XA_NO_ERROR)
    {
        /* ...initialization failed for some reason; do cleanup */
        xa_base_destroy(base, size, core);

        return NULL;
    }   

    /* ...initialization completed successfully */
    TRACE(INIT, _b("Codec[%p]:%u initialized"), base, core);
    
    return base;
}