<commit_before>/**
 *  Copyright (C) 2009-2011 Texas Instruments, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sosal/sosal.h>
#include <dvp/dvp.h>
#include <dvp/dvp_debug.h>
#include <dvp_kgb.h>

// dims mutex_t
static mutex_t dims_mutex = MUTEX_INITIAL;

void dvp_free(DVP_Handle handle, DVP_PTR ptr, DVP_U32 numElem, DVP_U32 sizeElem)
{
    DVP_Dim_t dims[1] = { {{{sizeElem, numElem, 1}}} };
    dvp_mem_free(handle, DVP_MTYPE_DEFAULT, dimof(dims), 2, dims, &ptr);
}

DVP_PTR dvp_malloc(DVP_Handle handle, DVP_U32 size)
{
    DVP_Dim_t dims[1] = { {{{size, 1, 1}}} };
    DVP_PTR ptrs[1] = {NULL};
    DVP_Dim_t strs[1] = { {{{0,0,0}}} };
    DVP_MemType_e mtype = DVP_MTYPE_DEFAULT; // The default type per platform is defined in the dvp_types.h
    dvp_mem_calloc(handle, mtype, dimof(ptrs), 1, dims, ptrs, strs);
    return ptrs[0];
}

DVP_PTR dvp_calloc(DVP_Handle handle, DVP_U32 numElem, DVP_U32 sizeElem)
{
    DVP_Dim_t dims[1] = { {{{sizeElem, numElem, 1}}} };
    DVP_PTR ptrs[1] = {NULL};
    DVP_Dim_t strs[1] = { {{{0,0,0}}} };
    DVP_MemType_e mtype = DVP_MTYPE_DEFAULT; // The default type per platform is defined in the dvp_types.h
    dvp_mem_calloc(handle, mtype, dimof(ptrs), 2, dims, ptrs, strs);
    return ptrs[0];
}

DVP_BOOL DVP_Image_Validate(DVP_Image_t *pImage,
                            DVP_U32 bufAlign,
                            DVP_U32 strideMultiple,
                            DVP_U32 widthMultiple,
                            DVP_U32 heigthMultiple,
                            fourcc_t *colors,
                            DVP_U32 numColors)
{
    DVP_U32 p;
    if (pImage == NULL)
    {
        DVP_PRINT(DVP_ZONE_ERROR, "Image is null\n");
        return DVP_FALSE;
    }

    if (bufAlign == 0 ||
        strideMultiple == 0 ||
        widthMultiple == 0 ||
        heigthMultiple == 0)
    {
        DVP_PRINT(DVP_ZONE_ERROR, "Aligns or Multiples are zero\n");
        return DVP_FALSE;
    }

    if (pImage->planes == 0 ||
        pImage->width == 0 ||
        pImage->height == 0 ||
        pImage->bufHeight == 0 ||
        pImage->bufWidth == 0 ||
        pImage->color == 0 ||
        pImage->memType < DVP_MTYPE_MPUCACHED_VIRTUAL ||
        pImage->memType > DVP_MTYPE_MAX ||
        pImage->numBytes == 0 ||
        pImage->x_stride == 0 ||
        pImage->y_stride == 0)
    {
        DVP_PRINT(DVP_ZONE_ERROR, "Image attribute was zero\n");
        return DVP_FALSE;
    }

    if ((pImage->y_stride % strideMultiple) ||
        (pImage->width % widthMultiple) ||
        (pImage->height % heigthMultiple))
    {
        DVP_PRINT(DVP_ZONE_ERROR, "Image attribute did not meet aligns or multiples\n");
        return DVP_FALSE;
    }

    for (p = 0; p < pImage->planes; p++)
    {
        size_t range = abs(pImage->y_stride) * pImage->bufHeight;
        size_t line = pImage->width * pImage->x_stride;
        DVP_U08 *pLo = pImage->pBuffer[p];
        DVP_U08 *pHi = pImage->pBuffer[p] + range;

        // is the pointer NULL for either?
        if (pImage->pBuffer[p] == NULL || pImage->pData[p] == NULL)
        {
            DVP_PRINT(DVP_ZONE_ERROR, "Image Plane Pointer is null\n");
            return DVP_FALSE;
        }

        // if the pointer not aligned?
        if ((size_t)pImage->pData[p] % bufAlign)
        {
            DVP_PRINT(DVP_ZONE_ERROR, "Image Plane Pointer is not aligned\n");
            return DVP_FALSE;
        }

        // check to make sure the pointer is within the image somewhere
        if (!(pLo <= pImage->pData[p] && pImage->pData[p] < pHi))
        {
            DVP_PRINT(DVP_ZONE_ERROR, "Image Plane Pointer out of bounds\n");
            return DVP_FALSE;
        }

        // if the image is strided, check to make sure it's not in the badlands.
        if (abs(pImage->y_stride) > line && pImage->bufWidth == pImage->width)
        {
            size_t offset = (size_t)(pImage->pData[p] - pImage->pBuffer[p]);
            if ((offset % abs(pImage->y_stride)) > line)
            {
                DVP_PRINT(DVP_ZONE_ERROR, "Image Plane Pointer is in badlands.\n");
                return DVP_FALSE;
            }
        }
    }

    if (numColors > 0 && colors != NULL)
    {
        DVP_U32 c;
        DVP_BOOL colorMatch = DVP_FALSE;
        for (c = 0; c < numColors; c++)
            if (colors[c] == pImage->color)
                colorMatch = DVP_TRUE;
        if (colorMatch == DVP_FALSE)
        {
            DVP_PRINT(DVP_ZONE_ERROR, "Image did not match colors\n");
            return DVP_FALSE;
        }
    }
    DVP_PRINT(DVP_ZONE_MEM, "Image %p validated!\n", pImage);
    return DVP_TRUE;
}

void DVP_Image_Dup(DVP_Image_t *dst, DVP_Image_t *src)
{
    memcpy(dst, src, sizeof(DVP_Image_t));
}

DVP_U32 DVP_Image_PlaneSize(DVP_Image_t *pImage, DVP_U32 plane)
{
    DVP_U32 lineSize = 0;
    DVP_U32 planeSize = 0;

    if (pImage == NULL)
        return 0;

    lineSize = pImage->x_stride * pImage->bufWidth;
    planeSize = pImage->y_stride * pImage->bufHeight; // potentially overestimated

    if (plane > 0)
    {
        switch (pImage->color)
        {
            case FOURCC_NV12:
            case FOURCC_NV21:
                // lineSize == lineSize // due to macropixel * w/2
                planeSize = lineSize * pImage->bufHeight/2;
                break;
            case FOURCC_YU16:
            case FOURCC_YV16:
                // lineSize == lineSize // due to macropixel * w/2
                planeSize = lineSize * pImage->bufHeight;
                break;
            case FOURCC_YV12:
            case FOURCC_IYUV:
                if (lineSize == (DVP_U32)abs(pImage->y_stride)) // non-TILED
                    lineSize /= 2;
                planeSize = lineSize * pImage->bufHeight/2;
                break;
            case FOURCC_YUV9:
            case FOURCC_YVU9:
                if (lineSize == (DVP_U32)abs(pImage->y_stride)) // non-TILED
                    lineSize /= 4;
                planeSize = lineSize * pImage->bufHeight/4;<commit_msg>[LINE] Line Size compute function<commit_after>/**
 *  Copyright (C) 2009-2011 Texas Instruments, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sosal/sosal.h>
#include <dvp/dvp.h>
#include <dvp/dvp_debug.h>
#include <dvp_kgb.h>

// dims mutex_t
static mutex_t dims_mutex = MUTEX_INITIAL;

void dvp_free(DVP_Handle handle, DVP_PTR ptr, DVP_U32 numElem, DVP_U32 sizeElem)
{
    DVP_Dim_t dims[1] = { {{{sizeElem, numElem, 1}}} };
    dvp_mem_free(handle, DVP_MTYPE_DEFAULT, dimof(dims), 2, dims, &ptr);
}

DVP_PTR dvp_malloc(DVP_Handle handle, DVP_U32 size)
{
    DVP_Dim_t dims[1] = { {{{size, 1, 1}}} };
    DVP_PTR ptrs[1] = {NULL};
    DVP_Dim_t strs[1] = { {{{0,0,0}}} };
    DVP_MemType_e mtype = DVP_MTYPE_DEFAULT; // The default type per platform is defined in the dvp_types.h
    dvp_mem_calloc(handle, mtype, dimof(ptrs), 1, dims, ptrs, strs);
    return ptrs[0];
}

DVP_PTR dvp_calloc(DVP_Handle handle, DVP_U32 numElem, DVP_U32 sizeElem)
{
    DVP_Dim_t dims[1] = { {{{sizeElem, numElem, 1}}} };
    DVP_PTR ptrs[1] = {NULL};
    DVP_Dim_t strs[1] = { {{{0,0,0}}} };
    DVP_MemType_e mtype = DVP_MTYPE_DEFAULT; // The default type per platform is defined in the dvp_types.h
    dvp_mem_calloc(handle, mtype, dimof(ptrs), 2, dims, ptrs, strs);
    return ptrs[0];
}

DVP_BOOL DVP_Image_Validate(DVP_Image_t *pImage,
                            DVP_U32 bufAlign,
                            DVP_U32 strideMultiple,
                            DVP_U32 widthMultiple,
                            DVP_U32 heigthMultiple,
                            fourcc_t *colors,
                            DVP_U32 numColors)
{
    DVP_U32 p;
    if (pImage == NULL)
    {
        DVP_PRINT(DVP_ZONE_ERROR, "Image is null\n");
        return DVP_FALSE;
    }

    if (bufAlign == 0 ||
        strideMultiple == 0 ||
        widthMultiple == 0 ||
        heigthMultiple == 0)
    {
        DVP_PRINT(DVP_ZONE_ERROR, "Aligns or Multiples are zero\n");
        return DVP_FALSE;
    }

    if (pImage->planes == 0 ||
        pImage->width == 0 ||
        pImage->height == 0 ||
        pImage->bufHeight == 0 ||
        pImage->bufWidth == 0 ||
        pImage->color == 0 ||
        pImage->memType < DVP_MTYPE_MPUCACHED_VIRTUAL ||
        pImage->memType > DVP_MTYPE_MAX ||
        pImage->numBytes == 0 ||
        pImage->x_stride == 0 ||
        pImage->y_stride == 0)
    {
        DVP_PRINT(DVP_ZONE_ERROR, "Image attribute was zero\n");
        return DVP_FALSE;
    }

    if ((pImage->y_stride % strideMultiple) ||
        (pImage->width % widthMultiple) ||
        (pImage->height % heigthMultiple))
    {
        DVP_PRINT(DVP_ZONE_ERROR, "Image attribute did not meet aligns or multiples\n");
        return DVP_FALSE;
    }

    for (p = 0; p < pImage->planes; p++)
    {
        size_t range = abs(pImage->y_stride) * pImage->bufHeight;
        size_t line = pImage->width * pImage->x_stride;
        DVP_U08 *pLo = pImage->pBuffer[p];
        DVP_U08 *pHi = pImage->pBuffer[p] + range;

        // is the pointer NULL for either?
        if (pImage->pBuffer[p] == NULL || pImage->pData[p] == NULL)
        {
            DVP_PRINT(DVP_ZONE_ERROR, "Image Plane Pointer is null\n");
            return DVP_FALSE;
        }

        // if the pointer not aligned?
        if ((size_t)pImage->pData[p] % bufAlign)
        {
            DVP_PRINT(DVP_ZONE_ERROR, "Image Plane Pointer is not aligned\n");
            return DVP_FALSE;
        }

        // check to make sure the pointer is within the image somewhere
        if (!(pLo <= pImage->pData[p] && pImage->pData[p] < pHi))
        {
            DVP_PRINT(DVP_ZONE_ERROR, "Image Plane Pointer out of bounds\n");
            return DVP_FALSE;
        }

        // if the image is strided, check to make sure it's not in the badlands.
        if (abs(pImage->y_stride) > line && pImage->bufWidth == pImage->width)
        {
            size_t offset = (size_t)(pImage->pData[p] - pImage->pBuffer[p]);
            if ((offset % abs(pImage->y_stride)) > line)
            {
                DVP_PRINT(DVP_ZONE_ERROR, "Image Plane Pointer is in badlands.\n");
                return DVP_FALSE;
            }
        }
    }

    if (numColors > 0 && colors != NULL)
    {
        DVP_U32 c;
        DVP_BOOL colorMatch = DVP_FALSE;
        for (c = 0; c < numColors; c++)
            if (colors[c] == pImage->color)
                colorMatch = DVP_TRUE;
        if (colorMatch == DVP_FALSE)
        {
            DVP_PRINT(DVP_ZONE_ERROR, "Image did not match colors\n");
            return DVP_FALSE;
        }
    }
    DVP_PRINT(DVP_ZONE_MEM, "Image %p validated!\n", pImage);
    return DVP_TRUE;
}

void DVP_Image_Dup(DVP_Image_t *dst, DVP_Image_t *src)
{
    memcpy(dst, src, sizeof(DVP_Image_t));
}

DVP_U32 DVP_Image_LineSize(DVP_Image_t *pImage, DVP_U32 plane)
{
    DVP_U32 lineSize = 0;
    if (pImage != NULL && plane < pImage->planes)
    {
        // assume it's normal, this could be overstated.
        lineSize = pImage->x_stride * pImage->bufWidth;
        if (plane > 0)
        {
            // for subsampled secondary planes...
            switch(pImage->color)
            {
                case FOURCC_NV12:
                case FOURCC_NV21:
                    // lineSize == lineSize // due to macropixel * w/2
                    break;
                case FOURCC_YU16:
                case FOURCC_YV16:
                    // lineSize == lineSize // due to macropixel * w/2
                    break;
                case FOURCC_IYUV:
                case FOURCC_YV12:
                    if (lineSize == (DVP_U32)abs(pImage->y_stride)) // non-TILED
                        lineSize /= 2;
                    break;
                case FOURCC_YUV9:
                case FOURCC_YVU9:
                    if (lineSize == (DVP_U32)abs(pImage->y_stride)) // non-TILED
                        lineSize /= 4;
                    break;
                default:
                    break;
            }
        }
        else if (pImage->color == FOURCC_BIN1)
            lineSize = pImage->width / 8;
    }
    return lineSize;
}

DVP_U32 DVP_Image_PlaneSize(DVP_Image_t *pImage, DVP_U32 plane)
{
    DVP_U32 lineSize = 0;
    DVP_U32 planeSize = 0;

    if (pImage == NULL)
        return 0;

    lineSize = DVP_Image_LineSize(pImage, plane);
    planeSize = pImage->y_stride * pImage->bufHeight; // potentially overestimated

    if (plane > 0)
    {
        switch (pImage->color)
        {
            case FOURCC_NV12:
            case FOURCC_NV21:
                planeSize = lineSize * pImage->bufHeight/2;
                break;
            case FOURCC_YU16:
            case FOURCC_YV16:
                planeSize = lineSize * pImage->bufHeight;
                break;
            case FOURCC_YV12:
            case FOURCC_IYUV:
                planeSize = lineSize * pImage->bufHeight/2;
                break;
            case FOURCC_YUV9:
            case FOURCC_YVU9:
                planeSize = lineSize * pImage->bufHeight/4;
{'fraction_non_alphanumeric': 0.11195513275592787, 'fraction_numerical': 0.01760613374982252, 'mean_word_length': 1.9520117351215422, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 114, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '102689', 'n_tokens_mistral': 5008, 'n_tokens_neox': 4766, 'n_words': 1139}