fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 Amlogic Corporation. |
| 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 | #ifndef _TOOS_UTILS_H_ |
| 17 | #define _TOOS_UTILS_H_ |
| 18 | |
| 19 | /** |
| 20 | * ROUND_UP_2: |
| 21 | * @num: integer value to round up |
| 22 | * |
| 23 | * Rounds an integer value up to the next multiple of 2. |
| 24 | */ |
| 25 | #define ROUND_UP_2(num) (((num)+1)&~1) |
| 26 | /** |
| 27 | * ROUND_UP_4: |
| 28 | * @num: integer value to round up |
| 29 | * |
| 30 | * Rounds an integer value up to the next multiple of 4. |
| 31 | */ |
| 32 | #define ROUND_UP_4(num) (((num)+3)&~3) |
| 33 | /** |
| 34 | * ROUND_UP_8: |
| 35 | * @num: integer value to round up |
| 36 | * |
| 37 | * Rounds an integer value up to the next multiple of 8. |
| 38 | */ |
| 39 | #define ROUND_UP_8(num) (((num)+7)&~7) |
| 40 | /** |
| 41 | * ROUND_UP_16: |
| 42 | * @num: integer value to round up |
| 43 | * |
| 44 | * Rounds an integer value up to the next multiple of 16. |
| 45 | */ |
| 46 | #define ROUND_UP_16(num) (((num)+15)&~15) |
| 47 | /** |
| 48 | * ROUND_UP_32: |
| 49 | * @num: integer value to round up |
| 50 | * |
| 51 | * Rounds an integer value up to the next multiple of 32. |
| 52 | */ |
| 53 | #define ROUND_UP_32(num) (((num)+31)&~31) |
| 54 | /** |
| 55 | * ROUND_UP_64: |
| 56 | * @num: integer value to round up |
| 57 | * |
| 58 | * Rounds an integer value up to the next multiple of 64. |
| 59 | */ |
| 60 | #define ROUND_UP_64(num) (((num)+63)&~63) |
| 61 | /** |
| 62 | * ROUND_UP_128: |
| 63 | * @num: integer value to round up |
| 64 | * |
| 65 | * Rounds an integer value up to the next multiple of 128. |
| 66 | * Since: 1.4 |
| 67 | */ |
| 68 | #define ROUND_UP_128(num) (((num)+127)&~127) |
| 69 | /** |
| 70 | * ROUND_UP_N: |
| 71 | * @num: integrer value to round up |
| 72 | * @align: a power of two to round up to |
| 73 | * |
| 74 | * Rounds an integer value up to the next multiple of @align. @align MUST be a |
| 75 | * power of two. |
| 76 | */ |
| 77 | #define ROUND_UP_N(num,align) ((((num) + ((align) - 1)) & ~((align) - 1))) |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * ROUND_DOWN_2: |
| 82 | * @num: integer value to round down |
| 83 | * |
| 84 | * Rounds an integer value down to the next multiple of 2. |
| 85 | */ |
| 86 | #define ROUND_DOWN_2(num) ((num)&(~1)) |
| 87 | /** |
| 88 | * ROUND_DOWN_4: |
| 89 | * @num: integer value to round down |
| 90 | * |
| 91 | * Rounds an integer value down to the next multiple of 4. |
| 92 | */ |
| 93 | #define ROUND_DOWN_4(num) ((num)&(~3)) |
| 94 | /** |
| 95 | * ROUND_DOWN_8: |
| 96 | * @num: integer value to round down |
| 97 | * |
| 98 | * Rounds an integer value down to the next multiple of 8. |
| 99 | */ |
| 100 | #define ROUND_DOWN_8(num) ((num)&(~7)) |
| 101 | /** |
| 102 | * ROUND_DOWN_16: |
| 103 | * @num: integer value to round down |
| 104 | * |
| 105 | * Rounds an integer value down to the next multiple of 16. |
| 106 | */ |
| 107 | #define ROUND_DOWN_16(num) ((num)&(~15)) |
| 108 | /** |
| 109 | * ROUND_DOWN_32: |
| 110 | * @num: integer value to round down |
| 111 | * |
| 112 | * Rounds an integer value down to the next multiple of 32. |
| 113 | */ |
| 114 | #define ROUND_DOWN_32(num) ((num)&(~31)) |
| 115 | /** |
| 116 | * ROUND_DOWN_64: |
| 117 | * @num: integer value to round down |
| 118 | * |
| 119 | * Rounds an integer value down to the next multiple of 64. |
| 120 | */ |
| 121 | #define ROUND_DOWN_64(num) ((num)&(~63)) |
| 122 | /** |
| 123 | * ROUND_DOWN_128: |
| 124 | * @num: integer value to round down |
| 125 | * |
| 126 | * Rounds an integer value down to the next multiple of 128. |
| 127 | * Since: 1.4 |
| 128 | */ |
| 129 | #define ROUND_DOWN_128(num) ((num)&(~127)) |
| 130 | /** |
| 131 | * ROUND_DOWN_N: |
| 132 | * @num: integrer value to round down |
| 133 | * @align: a power of two to round down to |
| 134 | * |
| 135 | * Rounds an integer value down to the next multiple of @align. @align MUST be a |
| 136 | * power of two. |
| 137 | */ |
| 138 | #define ROUND_DOWN_N(num,align) (((num) & ~((align) - 1))) |
| 139 | |
| 140 | /** |
| 141 | * @brief get unsigned int value from char p buffer |
| 142 | * |
| 143 | * @param p char buffer |
| 144 | * @return unsigned int |
| 145 | */ |
| 146 | unsigned int getU32( unsigned char *p ); |
| 147 | |
| 148 | /** |
| 149 | * @brief put unsigned int n to char *p |
| 150 | * |
| 151 | * @param p the char buffer |
| 152 | * @param n unsigned int value been put |
| 153 | * @return int return 4 bytes that is sizeof(int) |
| 154 | */ |
| 155 | int putU32( unsigned char *p, unsigned n ); |
| 156 | |
| 157 | /** |
| 158 | * @brief get signed int 64 bit value from char p buffer |
| 159 | * |
| 160 | * @param p char buffer |
| 161 | * @return signed int 64 bit value |
| 162 | */ |
| 163 | int64_t getS64( unsigned char *p ); |
| 164 | |
| 165 | /** |
| 166 | * @brief put int 64 bit value of n to char *p |
| 167 | * |
| 168 | * @param p the char buffer |
| 169 | * @param n int 64 bit value |
| 170 | * @return int 8 bytes that is sizeof(int64_t) |
| 171 | */ |
| 172 | int putS64( unsigned char *p, int64_t n ); |
| 173 | |
| 174 | #define TIME_SECOND (1000000000) |
| 175 | #define TIME_INVALID_VALUE ((int64_t) -1) |
| 176 | |
| 177 | #define TIME_IS_VALID(time) (((int64_t)(time)) != TIME_INVALID_VALUE) |
| 178 | /* timestamp debugging macros */ |
| 179 | /** |
| 180 | * TIME_FORMAT: (skip): |
| 181 | * |
| 182 | * A string that can be used in printf-like format strings to display a |
| 183 | * pts value in h:m:s format. Use TIME_ARGS() to construct |
| 184 | * the matching arguments. |
| 185 | * |
| 186 | * Example: |
| 187 | * printf(GST_TIME_FORMAT "\n", TIME_ARGS(ts)); |
| 188 | * ]| |
| 189 | */ |
| 190 | #define TIME_FORMAT "%u:%02u:%02u.%09u" |
| 191 | /** |
| 192 | * TIME_ARGS: (skip): |
| 193 | * |
| 194 | * Format pts for the #TIME_FORMAT format string. Note: pts will be |
| 195 | * evaluated more than once. |
| 196 | */ |
| 197 | #define TIME_ARGS(t) \ |
| 198 | TIME_IS_VALID (t) ? \ |
| 199 | (uint32_t) (((int64_t)(t)) / (TIME_SECOND * 60 * 60)) : 99, \ |
| 200 | TIME_IS_VALID (t) ? \ |
| 201 | (uint32_t) ((((int64_t)(t)) / (TIME_SECOND * 60)) % 60) : 99, \ |
| 202 | TIME_IS_VALID (t) ? \ |
| 203 | (uint32_t) ((((int64_t)(t)) / TIME_SECOND) % 60) : 99, \ |
| 204 | TIME_IS_VALID (t) ? \ |
| 205 | (uint32_t) (((int64_t)(t)) % TIME_SECOND) : 999999999 |
| 206 | #endif /*_TOOS_UTILS_H_*/ |