1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
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
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef VP9_COMMON_VP9_COMMON_H_
12#define VP9_COMMON_VP9_COMMON_H_
13
14/* Interface header for common constant data structures and lookup tables */
15
16#include <assert.h>
17
18#include "./vpx_config.h"
19#include "vpx_mem/vpx_mem.h"
20#include "vpx/vpx_integer.h"
21#include "vp9/common/vp9_systemdependent.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27#define MIN(x, y) (((x) < (y)) ? (x) : (y))
28#define MAX(x, y) (((x) > (y)) ? (x) : (y))
29
30#define ROUND_POWER_OF_TWO(value, n) \
31    (((value) + (1 << ((n) - 1))) >> (n))
32
33#define ALIGN_POWER_OF_TWO(value, n) \
34    (((value) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
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));  \
40  }
41
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)); \
46  }
47
48#define vp9_zero(dest) vpx_memset(&dest, 0, sizeof(dest))
49#define vp9_zero_array(dest, n) vpx_memset(dest, 0, n * sizeof(*dest))
50
51static INLINE uint8_t clip_pixel(int val) {
52  return (val > 255) ? 255u : (val < 0) ? 0u : val;
53}
54
55static INLINE int clamp(int value, int low, int high) {
56  return value < low ? low : (value > high ? high : value);
57}
58
59static INLINE double fclamp(double value, double low, double high) {
60  return value < low ? low : (value > high ? high : value);
61}
62
63static INLINE int get_unsigned_bits(unsigned int num_values) {
64  return num_values > 0 ? get_msb(num_values) + 1 : 0;
65}
66
67#if CONFIG_DEBUG
68#define CHECK_MEM_ERROR(cm, lval, expr) do { \
69  lval = (expr); \
70  if (!lval) \
71    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, \
72                       "Failed to allocate "#lval" at %s:%d", \
73                       __FILE__, __LINE__); \
74  } while (0)
75#else
76#define CHECK_MEM_ERROR(cm, lval, expr) do { \
77  lval = (expr); \
78  if (!lval) \
79    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, \
80                       "Failed to allocate "#lval); \
81  } while (0)
82#endif
83
84#define VP9_SYNC_CODE_0 0x49
85#define VP9_SYNC_CODE_1 0x83
86#define VP9_SYNC_CODE_2 0x42
87
88#define VP9_FRAME_MARKER 0x2
89
90
91#ifdef __cplusplus
92}  // extern "C"
93#endif
94
95#endif  // VP9_COMMON_VP9_COMMON_H_
96