blob: 6801dd3a2b6b84d00bc7078a740739a11d98c9fc [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * 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 Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
Jim Bankoski2b8dc062012-11-29 16:36:10 -080011#ifndef VP9_COMMON_VP9_COMMON_H_
12#define VP9_COMMON_VP9_COMMON_H_
John Koleszar0ea50ce2010-05-18 11:58:33 -040013
John Koleszar0ea50ce2010-05-18 11:58:33 -040014/* Interface header for common constant data structures and lookup tables */
15
Dmitry Kovalev7f99c3c2013-03-05 14:12:16 -080016#include <assert.h>
17
18#include "./vpx_config.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040019#include "vpx_mem/vpx_mem.h"
Ronald S. Bultje4d0ec7a2012-12-10 12:09:07 -080020#include "vpx/vpx_integer.h"
Alex Converseffd3d482014-02-03 12:12:01 -080021#include "vp9/common/vp9_systemdependent.h"
Ronald S. Bultje4cca47b2012-12-18 15:31:19 -080022
James Zern0940c9c2014-01-18 12:16:11 -080023#ifdef __cplusplus
24extern "C" {
25#endif
26
Ronald S. Bultjec3941662013-01-05 18:20:25 -080027#define MIN(x, y) (((x) < (y)) ? (x) : (y))
28#define MAX(x, y) (((x) > (y)) ? (x) : (y))
29
Dmitry Kovalevb7559252013-07-08 14:54:04 -070030#define ROUND_POWER_OF_TWO(value, n) \
31 (((value) + (1 << ((n) - 1))) >> (n))
John Koleszar0ea50ce2010-05-18 11:58:33 -040032
Dmitry Kovalevb7559252013-07-08 14:54:04 -070033#define ALIGN_POWER_OF_TWO(value, n) \
34 (((value) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
Dmitry Kovalev3603dfb2013-03-07 12:24:35 -080035
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 Koleszarc6b90392012-07-13 15:21:29 -070040 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040041
Dmitry Kovalev3603dfb2013-03-07 12:24:35 -080042// 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 Koleszarc6b90392012-07-13 15:21:29 -070046 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040047
Yaowu Xued095802014-05-13 10:07:45 -070048#define vp9_zero(dest) vpx_memset(&(dest), 0, sizeof(dest))
Jingning Hanc8e48f42013-10-15 16:09:28 -070049#define vp9_zero_array(dest, n) vpx_memset(dest, 0, n * sizeof(*dest))
John Koleszar0ea50ce2010-05-18 11:58:33 -040050
Ronald S. Bultjeaac73df2013-02-06 12:45:28 -080051static INLINE uint8_t clip_pixel(int val) {
Jim Bankoski512f9b62014-08-01 06:28:00 -070052 return (val > 255) ? 255 : (val < 0) ? 0 : val;
Ronald S. Bultje4d0ec7a2012-12-10 12:09:07 -080053}
54
Dmitry Kovalev2891d702013-03-11 17:02:27 -070055static INLINE int clamp(int value, int low, int high) {
56 return value < low ? low : (value > high ? high : value);
57}
58
Dmitry Kovalevdc541802013-05-08 18:13:46 -070059static INLINE double fclamp(double value, double low, double high) {
60 return value < low ? low : (value > high ? high : value);
61}
62
Alex Converseffd3d482014-02-03 12:12:01 -080063static INLINE int get_unsigned_bits(unsigned int num_values) {
64 return num_values > 0 ? get_msb(num_values) + 1 : 0;
Dmitry Kovalev94675712013-06-25 11:52:44 -070065}
66
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070067#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjeed50716f2014-10-02 15:43:27 -070068static INLINE uint16_t clip_pixel_highbd(int val, int bd) {
Deb Mukherjee81a81382014-09-15 12:59:19 -070069 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 Mukherjee872b2072014-09-30 14:29:34 -070080// 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.
83typedef int64_t tran_high_t;
84typedef int32_t tran_low_t;
85
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070086#define CONVERT_TO_SHORTPTR(x) ((uint16_t*)(((uintptr_t)x) << 1))
87#define CONVERT_TO_BYTEPTR(x) ((uint8_t*)(((uintptr_t)x) >> 1 ))
Deb Mukherjee872b2072014-09-30 14:29:34 -070088
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.
94typedef int32_t tran_high_t;
95typedef int16_t tran_low_t;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070096#endif // CONFIG_VP9_HIGHBITDEPTH
97
Dmitry Kovalev8e6ce6b2013-06-28 10:36:20 -070098#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 Kovaleva53075f2013-10-23 17:24:17 -0700115#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 Kovalev18c83b32013-05-28 18:07:54 -0700120
121
James Zern0940c9c2014-01-18 12:16:11 -0800122#ifdef __cplusplus
123} // extern "C"
124#endif
125
Ronald S. Bultje4cca47b2012-12-18 15:31:19 -0800126#endif // VP9_COMMON_VP9_COMMON_H_