John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 10 | */ |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 11 | #ifndef AOM_AOM_AOM_INTEGER_H_ |
| 12 | #define AOM_AOM_AOM_INTEGER_H_ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 13 | |
| 14 | /* get ptrdiff_t, size_t, wchar_t, NULL */ |
| 15 | #include <stddef.h> |
| 16 | |
Jim Bankoski | 07a6752 | 2014-08-12 16:28:08 -0700 | [diff] [blame] | 17 | #if defined(_MSC_VER) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 18 | #define AOM_FORCE_INLINE __forceinline |
| 19 | #define AOM_INLINE __inline |
Jim Bankoski | 07a6752 | 2014-08-12 16:28:08 -0700 | [diff] [blame] | 20 | #else |
James Zern | 7dec515 | 2016-10-17 12:39:34 -0700 | [diff] [blame] | 21 | #define AOM_FORCE_INLINE __inline__ __attribute__((always_inline)) |
Jim Bankoski | 07a6752 | 2014-08-12 16:28:08 -0700 | [diff] [blame] | 22 | // TODO(jbb): Allow a way to force inline off for older compilers. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 23 | #define AOM_INLINE inline |
Jim Bankoski | 07a6752 | 2014-08-12 16:28:08 -0700 | [diff] [blame] | 24 | #endif |
| 25 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 26 | #if defined(AOM_EMULATE_INTTYPES) |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 27 | typedef signed char int8_t; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 28 | typedef signed short int16_t; |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 29 | typedef signed int int32_t; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 30 | |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 31 | typedef unsigned char uint8_t; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 32 | typedef unsigned short uint16_t; |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 33 | typedef unsigned int uint32_t; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 34 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 35 | #ifndef _UINTPTR_T_DEFINED |
John Koleszar | 83b1d90 | 2012-11-05 12:37:14 -0800 | [diff] [blame] | 36 | typedef size_t uintptr_t; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 37 | #endif |
| 38 | |
John Koleszar | 1df0314 | 2010-05-20 14:44:18 -0400 | [diff] [blame] | 39 | #else |
| 40 | |
| 41 | /* Most platforms have the C99 standard integer types. */ |
| 42 | |
James Zern | c87f32c | 2014-09-30 16:06:23 -0700 | [diff] [blame] | 43 | #if defined(__cplusplus) |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 44 | #if !defined(__STDC_FORMAT_MACROS) |
| 45 | #define __STDC_FORMAT_MACROS |
| 46 | #endif |
| 47 | #if !defined(__STDC_LIMIT_MACROS) |
| 48 | #define __STDC_LIMIT_MACROS |
| 49 | #endif |
James Zern | c87f32c | 2014-09-30 16:06:23 -0700 | [diff] [blame] | 50 | #endif // __cplusplus |
| 51 | |
John Koleszar | 1df0314 | 2010-05-20 14:44:18 -0400 | [diff] [blame] | 52 | #include <stdint.h> |
John Koleszar | 1df0314 | 2010-05-20 14:44:18 -0400 | [diff] [blame] | 53 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 54 | #endif |
| 55 | |
John Koleszar | 4ead98f | 2011-05-05 10:53:24 -0400 | [diff] [blame] | 56 | /* VS2010 defines stdint.h, but not inttypes.h */ |
Tom Finegan | 7320fdd | 2014-02-10 15:30:43 -0800 | [diff] [blame] | 57 | #if defined(_MSC_VER) && _MSC_VER < 1800 |
John Koleszar | 4ead98f | 2011-05-05 10:53:24 -0400 | [diff] [blame] | 58 | #define PRId64 "I64d" |
| 59 | #else |
| 60 | #include <inttypes.h> |
| 61 | #endif |
| 62 | |
Yaowu Xu | 9105897 | 2017-11-24 16:26:18 -0800 | [diff] [blame] | 63 | #if !defined(INT8_MAX) |
| 64 | #define INT8_MAX 127 |
| 65 | #endif |
| 66 | |
| 67 | #if !defined(INT32_MAX) |
Yaowu Xu | d74a21e | 2017-11-24 22:34:43 -0800 | [diff] [blame] | 68 | #define INT32_MAX 2147483647 |
Yaowu Xu | 9105897 | 2017-11-24 16:26:18 -0800 | [diff] [blame] | 69 | #endif |
| 70 | |
| 71 | #if !defined(INT32_MIN) |
Yaowu Xu | e65e12f | 2017-11-27 07:56:37 -0800 | [diff] [blame] | 72 | #define INT32_MIN (-2147483647 - 1) |
Yaowu Xu | 9105897 | 2017-11-24 16:26:18 -0800 | [diff] [blame] | 73 | #endif |
| 74 | |
Sebastien Alaiwan | ed9e0d0 | 2017-07-13 10:44:58 +0200 | [diff] [blame] | 75 | #define NELEMENTS(x) (int)(sizeof(x) / sizeof(x[0])) |
| 76 | |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 77 | #if defined(__cplusplus) |
| 78 | extern "C" { |
| 79 | #endif // __cplusplus |
| 80 | |
Wan-Teh Chang | 27208c6 | 2018-04-30 16:27:42 -0700 | [diff] [blame] | 81 | // Returns size of uint64_t when encoded using LEB128. |
Tom Finegan | 9540d59 | 2018-02-06 10:04:31 -0800 | [diff] [blame] | 82 | size_t aom_uleb_size_in_bytes(uint64_t value); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 83 | |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 84 | // Returns 0 on success, -1 on decode failure. |
| 85 | // On success, 'value' stores the decoded LEB128 value and 'length' stores |
Tom Finegan | ccb8cc4 | 2018-02-22 14:08:13 -0800 | [diff] [blame] | 86 | // the number of bytes decoded. |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 87 | int aom_uleb_decode(const uint8_t *buffer, size_t available, uint64_t *value, |
| 88 | size_t *length); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 89 | |
| 90 | // Encodes LEB128 integer. Returns 0 when successful, and -1 upon failure. |
Tom Finegan | 9540d59 | 2018-02-06 10:04:31 -0800 | [diff] [blame] | 91 | int aom_uleb_encode(uint64_t value, size_t available, uint8_t *coded_value, |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 92 | size_t *coded_size); |
| 93 | |
| 94 | // Encodes LEB128 integer to size specified. Returns 0 when successful, and -1 |
| 95 | // upon failure. |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 96 | // Note: This will write exactly pad_to_size bytes; if the value cannot be |
| 97 | // encoded in this many bytes, then this will fail. |
Tom Finegan | 9540d59 | 2018-02-06 10:04:31 -0800 | [diff] [blame] | 98 | int aom_uleb_encode_fixed_size(uint64_t value, size_t available, |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 99 | size_t pad_to_size, uint8_t *coded_value, |
| 100 | size_t *coded_size); |
| 101 | |
| 102 | #if defined(__cplusplus) |
| 103 | } // extern "C" |
| 104 | #endif // __cplusplus |
| 105 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 106 | #endif // AOM_AOM_AOM_INTEGER_H_ |