| Fyodor Kyslov | dd7d599 | 2024-11-05 21:40:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_C2_SOFT_APV_COMMON_H__ |
| 18 | #define ANDROID_C2_SOFT_APV_COMMON_H__ |
| 19 | |
| 20 | typedef enum { |
| 21 | PIX_CHROMA_NA = 0xFFFFFFFF, |
| 22 | PIX_YUV_420P = 0x1, |
| 23 | PIX_YUV_422P = 0x2, |
| 24 | PIX_420_UV_INTL = 0x3, |
| 25 | PIX_YUV_422IBE = 0x4, |
| 26 | PIX_YUV_422ILE = 0x5, |
| 27 | PIX_YUV_444P = 0x6, |
| 28 | PIX_YUV_411P = 0x7, |
| 29 | PIX_GRAY = 0x8, |
| 30 | PIX_RGB_565 = 0x9, |
| 31 | PIX_RGB_24 = 0xa, |
| 32 | PIX_YUV_420SP_UV = 0xb, |
| 33 | PIX_YUV_420SP_VU = 0xc, |
| 34 | PIX_YUV_422SP_UV = 0xd, |
| 35 | PIX_YUV_422SP_VU = 0xe |
| 36 | } PIX_COLOR_FORMAT_T; |
| 37 | |
| 38 | #define CLIP_VAL(n, min, max) (((n) > (max)) ? (max) : (((n) < (min)) ? (min) : (n))) |
| 39 | #define ALIGN_VAL(val, align) ((((val) + (align) - 1) / (align)) * (align)) |
| 40 | |
| 41 | static int atomic_inc(volatile int* pcnt) { |
| 42 | int ret; |
| 43 | ret = *pcnt; |
| 44 | ret++; |
| 45 | *pcnt = ret; |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | static int atomic_dec(volatile int* pcnt) { |
| 50 | int ret; |
| 51 | ret = *pcnt; |
| 52 | ret--; |
| 53 | *pcnt = ret; |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | /* Function to allocate memory for picture buffer: |
| 58 | This function might need to modify according to O/S or CPU platform |
| 59 | */ |
| 60 | static void* picbuf_alloc(int size) { |
| 61 | return malloc(size); |
| 62 | } |
| 63 | |
| 64 | /* Function to free memory allocated for picture buffer: |
| 65 | This function might need to modify according to O/S or CPU platform |
| 66 | */ |
| 67 | static void picbuf_free(void* p) { |
| 68 | if (p) { |
| 69 | free(p); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static int imgb_addref(oapv_imgb_t* imgb) { |
| 74 | return atomic_inc(&imgb->refcnt); |
| 75 | } |
| 76 | |
| 77 | static int imgb_getref(oapv_imgb_t* imgb) { |
| 78 | return imgb->refcnt; |
| 79 | } |
| 80 | |
| 81 | static int imgb_release(oapv_imgb_t* imgb) { |
| 82 | int refcnt, i; |
| 83 | refcnt = atomic_dec(&imgb->refcnt); |
| 84 | if (refcnt == 0) { |
| 85 | for (i = 0; i < OAPV_MAX_CC; i++) { |
| 86 | if (imgb->baddr[i]) picbuf_free(imgb->baddr[i]); |
| 87 | } |
| 88 | free(imgb); |
| 89 | } |
| 90 | return refcnt; |
| 91 | } |
| 92 | |
| 93 | static oapv_imgb_t* imgb_create(int w, int h, int cs) { |
| 94 | int i, bd; |
| 95 | oapv_imgb_t* imgb; |
| 96 | |
| 97 | imgb = (oapv_imgb_t*)malloc(sizeof(oapv_imgb_t)); |
| 98 | if (imgb == NULL) goto ERR; |
| 99 | memset(imgb, 0, sizeof(oapv_imgb_t)); |
| 100 | |
| 101 | bd = OAPV_CS_GET_BYTE_DEPTH(cs); /* byte unit */ |
| 102 | |
| 103 | imgb->w[0] = w; |
| 104 | imgb->h[0] = h; |
| 105 | switch (OAPV_CS_GET_FORMAT(cs)) { |
| 106 | case OAPV_CF_YCBCR400: |
| 107 | imgb->w[1] = imgb->w[2] = w; |
| 108 | imgb->h[1] = imgb->h[2] = h; |
| 109 | imgb->np = 1; |
| 110 | break; |
| 111 | case OAPV_CF_YCBCR420: |
| 112 | imgb->w[1] = imgb->w[2] = (w + 1) >> 1; |
| 113 | imgb->h[1] = imgb->h[2] = (h + 1) >> 1; |
| 114 | imgb->np = 3; |
| 115 | break; |
| 116 | case OAPV_CF_YCBCR422: |
| 117 | imgb->w[1] = imgb->w[2] = (w + 1) >> 1; |
| 118 | imgb->h[1] = imgb->h[2] = h; |
| 119 | imgb->np = 3; |
| 120 | break; |
| 121 | case OAPV_CF_YCBCR444: |
| 122 | imgb->w[1] = imgb->w[2] = w; |
| 123 | imgb->h[1] = imgb->h[2] = h; |
| 124 | imgb->np = 3; |
| 125 | break; |
| 126 | case OAPV_CF_YCBCR4444: |
| 127 | imgb->w[1] = imgb->w[2] = imgb->w[3] = w; |
| 128 | imgb->h[1] = imgb->h[2] = imgb->h[3] = h; |
| 129 | imgb->np = 4; |
| 130 | break; |
| 131 | case OAPV_CF_PLANAR2: |
| 132 | imgb->w[1] = w; |
| 133 | imgb->h[1] = h; |
| 134 | imgb->np = 2; |
| 135 | break; |
| 136 | default: |
| 137 | goto ERR; |
| 138 | } |
| 139 | |
| 140 | for (i = 0; i < imgb->np; i++) { |
| 141 | // width and height need to be aligned to macroblock size |
| 142 | imgb->aw[i] = ALIGN_VAL(imgb->w[i], OAPV_MB_W); |
| 143 | imgb->s[i] = imgb->aw[i] * bd; |
| 144 | imgb->ah[i] = ALIGN_VAL(imgb->h[i], OAPV_MB_H); |
| 145 | imgb->e[i] = imgb->ah[i]; |
| 146 | |
| 147 | imgb->bsize[i] = imgb->s[i] * imgb->e[i]; |
| 148 | imgb->a[i] = imgb->baddr[i] = picbuf_alloc(imgb->bsize[i]); |
| 149 | memset(imgb->a[i], 0, imgb->bsize[i]); |
| 150 | } |
| 151 | imgb->cs = cs; |
| 152 | imgb->addref = imgb_addref; |
| 153 | imgb->getref = imgb_getref; |
| 154 | imgb->release = imgb_release; |
| 155 | |
| 156 | imgb->addref(imgb); /* increase reference count */ |
| 157 | return imgb; |
| 158 | |
| 159 | ERR: |
| 160 | if (imgb) { |
| 161 | for (int i = 0; i < OAPV_MAX_CC; i++) { |
| 162 | if (imgb->a[i]) picbuf_free(imgb->a[i]); |
| 163 | } |
| 164 | free(imgb); |
| 165 | } |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | #endif // ANDROID_C2_SOFT_APV_COMMON_H__ |