John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
Jim Bankoski | 2b8dc06 | 2012-11-29 16:36:10 -0800 | [diff] [blame] | 11 | #ifndef VP9_COMMON_VP9_COMMON_H_ |
| 12 | #define VP9_COMMON_VP9_COMMON_H_ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 13 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 14 | /* Interface header for common constant data structures and lookup tables */ |
| 15 | |
Dmitry Kovalev | 7f99c3c | 2013-03-05 14:12:16 -0800 | [diff] [blame] | 16 | #include <assert.h> |
| 17 | |
| 18 | #include "./vpx_config.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 19 | #include "vpx_mem/vpx_mem.h" |
Ronald S. Bultje | 4d0ec7a | 2012-12-10 12:09:07 -0800 | [diff] [blame] | 20 | #include "vpx/vpx_integer.h" |
Alex Converse | ffd3d48 | 2014-02-03 12:12:01 -0800 | [diff] [blame] | 21 | #include "vp9/common/vp9_systemdependent.h" |
Ronald S. Bultje | 4cca47b | 2012-12-18 15:31:19 -0800 | [diff] [blame] | 22 | |
James Zern | 0940c9c | 2014-01-18 12:16:11 -0800 | [diff] [blame] | 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
Ronald S. Bultje | c394166 | 2013-01-05 18:20:25 -0800 | [diff] [blame] | 27 | #define MIN(x, y) (((x) < (y)) ? (x) : (y)) |
| 28 | #define MAX(x, y) (((x) > (y)) ? (x) : (y)) |
| 29 | |
Dmitry Kovalev | b755925 | 2013-07-08 14:54:04 -0700 | [diff] [blame] | 30 | #define ROUND_POWER_OF_TWO(value, n) \ |
| 31 | (((value) + (1 << ((n) - 1))) >> (n)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 32 | |
Dmitry Kovalev | b755925 | 2013-07-08 14:54:04 -0700 | [diff] [blame] | 33 | #define ALIGN_POWER_OF_TWO(value, n) \ |
| 34 | (((value) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1)) |
Dmitry Kovalev | 3603dfb | 2013-03-07 12:24:35 -0800 | [diff] [blame] | 35 | |
| 36 | // Only need this for fixed-size arrays, for structs just assign. |
| 37 | #define vp9_copy(dest, src) { \ |
| 38 | assert(sizeof(dest) == sizeof(src)); \ |
| 39 | vpx_memcpy(dest, src, sizeof(src)); \ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 40 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 41 | |
Dmitry Kovalev | 3603dfb | 2013-03-07 12:24:35 -0800 | [diff] [blame] | 42 | // Use this for variably-sized arrays. |
| 43 | #define vp9_copy_array(dest, src, n) { \ |
| 44 | assert(sizeof(*dest) == sizeof(*src)); \ |
| 45 | vpx_memcpy(dest, src, n * sizeof(*src)); \ |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 46 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 47 | |
Yaowu Xu | ed09580 | 2014-05-13 10:07:45 -0700 | [diff] [blame] | 48 | #define vp9_zero(dest) vpx_memset(&(dest), 0, sizeof(dest)) |
Jingning Han | c8e48f4 | 2013-10-15 16:09:28 -0700 | [diff] [blame] | 49 | #define vp9_zero_array(dest, n) vpx_memset(dest, 0, n * sizeof(*dest)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 50 | |
Ronald S. Bultje | aac73df | 2013-02-06 12:45:28 -0800 | [diff] [blame] | 51 | static INLINE uint8_t clip_pixel(int val) { |
Jim Bankoski | 512f9b6 | 2014-08-01 06:28:00 -0700 | [diff] [blame] | 52 | return (val > 255) ? 255 : (val < 0) ? 0 : val; |
Ronald S. Bultje | 4d0ec7a | 2012-12-10 12:09:07 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Dmitry Kovalev | 2891d70 | 2013-03-11 17:02:27 -0700 | [diff] [blame] | 55 | static INLINE int clamp(int value, int low, int high) { |
| 56 | return value < low ? low : (value > high ? high : value); |
| 57 | } |
| 58 | |
Dmitry Kovalev | dc54180 | 2013-05-08 18:13:46 -0700 | [diff] [blame] | 59 | static INLINE double fclamp(double value, double low, double high) { |
| 60 | return value < low ? low : (value > high ? high : value); |
| 61 | } |
| 62 | |
Alex Converse | ffd3d48 | 2014-02-03 12:12:01 -0800 | [diff] [blame] | 63 | static INLINE int get_unsigned_bits(unsigned int num_values) { |
| 64 | return num_values > 0 ? get_msb(num_values) + 1 : 0; |
Dmitry Kovalev | 9467571 | 2013-06-25 11:52:44 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Deb Mukherjee | 5acfafb | 2014-08-26 12:35:15 -0700 | [diff] [blame] | 67 | #if CONFIG_VP9_HIGHBITDEPTH |
Deb Mukherjee | d50716f | 2014-10-02 15:43:27 -0700 | [diff] [blame] | 68 | static INLINE uint16_t clip_pixel_highbd(int val, int bd) { |
Deb Mukherjee | 81a8138 | 2014-09-15 12:59:19 -0700 | [diff] [blame] | 69 | switch (bd) { |
| 70 | case 8: |
| 71 | default: |
| 72 | return (uint16_t)clamp(val, 0, 255); |
| 73 | case 10: |
| 74 | return (uint16_t)clamp(val, 0, 1023); |
| 75 | case 12: |
| 76 | return (uint16_t)clamp(val, 0, 4095); |
| 77 | } |
| 78 | } |
| 79 | |
Deb Mukherjee | 872b207 | 2014-09-30 14:29:34 -0700 | [diff] [blame] | 80 | // Note: |
| 81 | // tran_low_t is the datatype used for final transform coefficients. |
| 82 | // tran_high_t is the datatype used for intermediate transform stages. |
| 83 | typedef int64_t tran_high_t; |
| 84 | typedef int32_t tran_low_t; |
| 85 | |
Deb Mukherjee | 5acfafb | 2014-08-26 12:35:15 -0700 | [diff] [blame] | 86 | #define CONVERT_TO_SHORTPTR(x) ((uint16_t*)(((uintptr_t)x) << 1)) |
| 87 | #define CONVERT_TO_BYTEPTR(x) ((uint8_t*)(((uintptr_t)x) >> 1 )) |
Deb Mukherjee | 872b207 | 2014-09-30 14:29:34 -0700 | [diff] [blame] | 88 | |
| 89 | #else |
| 90 | |
| 91 | // Note: |
| 92 | // tran_low_t is the datatype used for final transform coefficients. |
| 93 | // tran_high_t is the datatype used for intermediate transform stages. |
| 94 | typedef int32_t tran_high_t; |
| 95 | typedef int16_t tran_low_t; |
Deb Mukherjee | 5acfafb | 2014-08-26 12:35:15 -0700 | [diff] [blame] | 96 | #endif // CONFIG_VP9_HIGHBITDEPTH |
| 97 | |
Dmitry Kovalev | 8e6ce6b | 2013-06-28 10:36:20 -0700 | [diff] [blame] | 98 | #if CONFIG_DEBUG |
| 99 | #define CHECK_MEM_ERROR(cm, lval, expr) do { \ |
| 100 | lval = (expr); \ |
| 101 | if (!lval) \ |
| 102 | vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, \ |
| 103 | "Failed to allocate "#lval" at %s:%d", \ |
| 104 | __FILE__, __LINE__); \ |
| 105 | } while (0) |
| 106 | #else |
| 107 | #define CHECK_MEM_ERROR(cm, lval, expr) do { \ |
| 108 | lval = (expr); \ |
| 109 | if (!lval) \ |
| 110 | vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, \ |
| 111 | "Failed to allocate "#lval); \ |
| 112 | } while (0) |
| 113 | #endif |
| 114 | |
Dmitry Kovalev | a53075f | 2013-10-23 17:24:17 -0700 | [diff] [blame] | 115 | #define VP9_SYNC_CODE_0 0x49 |
| 116 | #define VP9_SYNC_CODE_1 0x83 |
| 117 | #define VP9_SYNC_CODE_2 0x42 |
| 118 | |
| 119 | #define VP9_FRAME_MARKER 0x2 |
Dmitry Kovalev | 18c83b3 | 2013-05-28 18:07:54 -0700 | [diff] [blame] | 120 | |
| 121 | |
James Zern | 0940c9c | 2014-01-18 12:16:11 -0800 | [diff] [blame] | 122 | #ifdef __cplusplus |
| 123 | } // extern "C" |
| 124 | #endif |
| 125 | |
Ronald S. Bultje | 4cca47b | 2012-12-18 15:31:19 -0800 | [diff] [blame] | 126 | #endif // VP9_COMMON_VP9_COMMON_H_ |