diff --git a/mpp/mpp_buffer_impl.cpp b/mpp/mpp_buffer_impl.cpp index 339a037e..59f4d8d5 100644 --- a/mpp/mpp_buffer_impl.cpp +++ b/mpp/mpp_buffer_impl.cpp @@ -284,7 +284,7 @@ MPP_RET mpp_buffer_group_init(MppBufferGroupImpl **group, const char *tag, MppBu break; } while (p->group_id != service.group_count); - mpp_alloctor_get(&p->allocator, &p->alloc_api); + mpp_alloctor_get(&p->allocator, &p->alloc_api, type); MPP_BUFFER_SERVICE_UNLOCK(); *group = p; diff --git a/osal/android/os_allocator.c b/osal/android/os_allocator.c index 55319ead..19043167 100644 --- a/osal/android/os_allocator.c +++ b/osal/android/os_allocator.c @@ -154,7 +154,7 @@ int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size) p = (allocator_ion *)ctx; - return os_malloc(&data->ptr, p->alignment, size); + return (MPP_RET)os_malloc(&data->ptr, p->alignment, size); } void os_allocator_free(void *ctx, MppBufferData *data) diff --git a/osal/inc/mpp_allocator.h b/osal/inc/mpp_allocator.h index e78ed2b8..ed92f3cc 100644 --- a/osal/inc/mpp_allocator.h +++ b/osal/inc/mpp_allocator.h @@ -34,7 +34,7 @@ typedef struct { extern "C" { #endif -MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api); +MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api, MppBufferType type); MPP_RET mpp_alloctor_put(MppAllocator *allocator); #ifdef __cplusplus diff --git a/osal/linux/os_allocator.c b/osal/linux/os_allocator.c index c119640a..557fd246 100644 --- a/osal/linux/os_allocator.c +++ b/osal/linux/os_allocator.c @@ -22,11 +22,16 @@ #include "mpp_mem.h" #include "mpp_log.h" +/* + * Linux only support MPP_BUFFER_TYPE_NORMAL so far + * we can support MPP_BUFFER_TYPE_V4L2 later + */ typedef struct { - size_t alignment; + size_t alignment; + MppBufferType type; } allocator_impl; -int os_allocator_open(void **ctx, size_t alignment) +MPP_RET os_allocator_open(void **ctx, size_t alignment, MppBufferType type) { allocator_impl *p = NULL; @@ -35,20 +40,29 @@ int os_allocator_open(void **ctx, size_t alignment) return MPP_ERR_NULL_PTR; } - p = mpp_malloc(allocator_impl, 1); - if (NULL == p) { - *ctx = NULL; - mpp_err("os_allocator_open Linux failed to allocate context\n"); - return MPP_ERR_MALLOC; + switch (type) { + case MPP_BUFFER_TYPE_NORMAL : { + p = mpp_malloc(allocator_impl, 1); + if (NULL == p) { + *ctx = NULL; + mpp_err("os_allocator_open Linux failed to allocate context\n"); + return MPP_ERR_MALLOC; + } + + p->alignment = alignment; + } break; + default : { + mpp_err("os_allocator_open Window do not accept type %d\n"); + } break; } - p->alignment = alignment; *ctx = p; return MPP_OK; } -int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size) +MPP_RET os_allocator_alloc(void *ctx, MppBufferData *data, size_t size) { + MPP_RET ret = MPP_OK; allocator_impl *p = NULL; if (NULL == ctx) { @@ -57,20 +71,37 @@ int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size) } p = (allocator_impl *)ctx; - return os_malloc(&data->ptr, p->alignment, size); + switch (p->type) { + case MPP_BUFFER_TYPE_NORMAL : { + ret = os_malloc(&data->ptr, p->alignment, size); + } break; + case MPP_BUFFER_TYPE_V4L2 : { + // TODO: support v4l2 vb2 buffer queue + mpp_err("os_allocator_alloc Linux MPP_BUFFER_TYPE_V4L2 will support later\n"); + ret = MPP_ERR_UNKNOW; + } break; + default : { + mpp_err("os_allocator_alloc Linux do not accept type %d\n", p->type); + ret = MPP_ERR_UNKNOW; + } break; + } + return ret; } -void os_allocator_free(void *ctx, MppBufferData *data) +MPP_RET os_allocator_free(void *ctx, MppBufferData *data) { (void) ctx; os_free(data->ptr); + return MPP_OK; } -void os_allocator_close(void *ctx) +MPP_RET os_allocator_close(void *ctx) { - if (ctx) + if (ctx) { mpp_free(ctx); - else - mpp_err("os_allocator_close Linux found NULL context input\n"); + return MPP_OK; + } + mpp_err("os_allocator_close Linux found NULL context input\n"); + return MPP_NOK; } diff --git a/osal/mpp_allocator.cpp b/osal/mpp_allocator.cpp index 9548037f..7de59b30 100644 --- a/osal/mpp_allocator.cpp +++ b/osal/mpp_allocator.cpp @@ -36,10 +36,10 @@ MPP_RET mpp_allocator_alloc(MppAllocator allocator, MppBufferData *data, size_t MppAllocatorImpl *palloc = (MppAllocatorImpl *)allocator; MPP_ALLOCATOR_LOCK(palloc); - int ret = os_allocator_alloc(palloc->allocator, data, size); + MPP_RET ret = os_allocator_alloc(palloc->allocator, data, size); MPP_ALLOCATOR_UNLOCK(palloc); - return (0 == ret) ? (MPP_OK) : (MPP_NOK); + return ret; } MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferData *data) @@ -58,11 +58,11 @@ MPP_RET mpp_allocator_free(MppAllocator allocator, MppBufferData *data) return MPP_OK; } -MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api) +MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api, MppBufferType type) { - if (NULL == allocator || NULL == api) { - mpp_err("mpp_alloctor_get invalid input: buffer %p api %p\n", - allocator, api); + if (NULL == allocator || NULL == api || type >= MPP_BUFFER_TYPE_BUTT) { + mpp_err("mpp_alloctor_get invalid input: buffer %p api %p type %d\n", + allocator, api, type); return MPP_ERR_UNKNOW; } @@ -80,13 +80,13 @@ MPP_RET mpp_alloctor_get(MppAllocator *allocator, MppAllocatorApi **api) } palloc->alignment = SZ_4K; - os_allocator_open(&palloc->allocator, palloc->alignment); + os_allocator_open(&palloc->allocator, palloc->alignment, type); papi->size = sizeof(papi->size); papi->version = 1; papi->alloc = mpp_allocator_alloc; papi->free = mpp_allocator_free; - palloc->api = papi; + palloc->api = papi; pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); diff --git a/osal/mpp_allocator_impl.h b/osal/mpp_allocator_impl.h index 891d639e..98805fae 100644 --- a/osal/mpp_allocator_impl.h +++ b/osal/mpp_allocator_impl.h @@ -18,9 +18,11 @@ #define __MPP_ALLOCATOR_IMPL_H__ #include "mpp_thread.h" +#include "mpp_buffer.h" typedef struct { pthread_mutex_t lock; + MppBufferType type; size_t alignment; void *allocator; void *api; diff --git a/osal/os_allocator.h b/osal/os_allocator.h index 8e513dea..d56ef5aa 100644 --- a/osal/os_allocator.h +++ b/osal/os_allocator.h @@ -23,10 +23,10 @@ extern "C" { #endif -int os_allocator_open(void **ctx, size_t alignment); -int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size); -void os_allocator_free(void *ctx, MppBufferData *data); -void os_allocator_close(void *ctx); +MPP_RET os_allocator_open(void **ctx, size_t alignment, MppBufferType type); +MPP_RET os_allocator_alloc(void *ctx, MppBufferData *data, size_t size); +MPP_RET os_allocator_free(void *ctx, MppBufferData *data); +MPP_RET os_allocator_close(void *ctx); #ifdef __cplusplus } diff --git a/osal/window/os_allocator.c b/osal/window/os_allocator.c index ad906f95..1f3637d7 100644 --- a/osal/window/os_allocator.c +++ b/osal/window/os_allocator.c @@ -20,11 +20,15 @@ #include "mpp_mem.h" #include "mpp_log.h" +/* + * window only support MPP_BUFFER_TYPE_NORMAL + */ + typedef struct { size_t alignment; } allocator_impl; -int os_allocator_open(void **ctx, size_t alignment) +MPP_RET os_allocator_open(void **ctx, size_t alignment, MppBufferType type) { allocator_impl *p = NULL; @@ -33,19 +37,27 @@ int os_allocator_open(void **ctx, size_t alignment) return MPP_ERR_NULL_PTR; } - p = mpp_malloc(allocator_impl, 1); - if (NULL == p) { - *ctx = NULL; - mpp_err("os_allocator_open Window failed to allocate context\n"); - return MPP_ERR_MALLOC; + switch (type) { + case MPP_BUFFER_TYPE_NORMAL : { + p = mpp_malloc(allocator_impl, 1); + if (NULL == p) { + *ctx = NULL; + mpp_err("os_allocator_open Window failed to allocate context\n"); + return MPP_ERR_MALLOC; + } + + p->alignment = alignment; + } break; + default : { + mpp_err("os_allocator_open Window do not accept type %d\n"); + } break; } - p->alignment = alignment; *ctx = p; return MPP_OK; } -int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size) +MPP_RET os_allocator_alloc(void *ctx, MppBufferData *data, size_t size) { allocator_impl *p = NULL; @@ -55,20 +67,23 @@ int os_allocator_alloc(void *ctx, MppBufferData *data, size_t size) } p = (allocator_impl *)ctx; - return os_malloc(&data->ptr, p->alignment, size); + return (MPP_RET)os_malloc(&data->ptr, p->alignment, size); } -void os_allocator_free(void *ctx, MppBufferData *data) +MPP_RET os_allocator_free(void *ctx, MppBufferData *data) { (void) ctx; os_free(data->ptr); + return MPP_OK; } -void os_allocator_close(void *ctx) +MPP_RET os_allocator_close(void *ctx) { - if (ctx) + if (ctx) { mpp_free(ctx); - else - mpp_err("os_allocator_close Window found NULL context input\n"); + return MPP_OK; + } + mpp_err("os_allocator_close Window found NULL context input\n"); + return MPP_NOK; }