Coverage Report

Created: 2025-11-14 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libde265/libde265/util.h
Line
Count
Source
1
/*
2
 * H.265 video codec.
3
 * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4
 *
5
 * This file is part of libde265.
6
 *
7
 * libde265 is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libde265 is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libde265.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#ifndef DE265_UTIL_H
22
#define DE265_UTIL_H
23
24
#ifdef HAVE_CONFIG_H
25
#include "config.h"
26
#endif
27
28
#ifndef _MSC_VER
29
#include <inttypes.h>
30
#endif
31
32
#include <stdio.h>
33
#include <string>
34
35
#include "libde265/de265.h"
36
37
#ifdef __GNUC__
38
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
39
#endif
40
41
#ifdef _MSC_VER
42
#define LIBDE265_DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
43
#define likely(x)      (x)
44
#define unlikely(x)    (x)
45
#else
46
8.75M
#define LIBDE265_DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
47
20.3M
#define likely(x)      __builtin_expect(!!(x), 1)
48
3.15M
#define unlikely(x)    __builtin_expect(!!(x), 0)
49
#endif
50
51
#if defined(__GNUC__) && (__GNUC__ >= 4)
52
#define LIBDE265_CHECK_RESULT __attribute__ ((warn_unused_result))
53
#elif defined(_MSC_VER) && (_MSC_VER >= 1700)
54
#define LIBDE265_CHECK_RESULT _Check_return_
55
#else
56
#define LIBDE265_CHECK_RESULT
57
#endif
58
59
// Be careful with these alignment instructions. They only specify the alignment within
60
// a struct. But they cannot make sure that the base address of the struct has the same alignment
61
// when it is dynamically allocated.
62
3.64M
#define ALIGNED_32( var ) LIBDE265_DECLARE_ALIGNED( var, 32 )
63
5.11M
#define ALIGNED_16( var ) LIBDE265_DECLARE_ALIGNED( var, 16 )
64
#define ALIGNED_8( var )  LIBDE265_DECLARE_ALIGNED( var, 8 )
65
#define ALIGNED_4( var )  LIBDE265_DECLARE_ALIGNED( var, 4 )
66
67
// C++11 specific features
68
#if defined(_MSC_VER) || (!__clang__ && __GNUC__ && GCC_VERSION < 40600)
69
#define FOR_LOOP(type, var, list)   for each (type var in list)
70
#undef FOR_LOOP_AUTO_SUPPORT
71
#else
72
#define FOR_LOOP(type, var, list)   for (type var : list)
73
#define FOR_LOOP_AUTO_SUPPORT 1
74
#endif
75
76
#ifdef USE_STD_TR1_NAMESPACE
77
#include <tr1/memory>
78
namespace std { using namespace std::tr1; }
79
#endif
80
81
#ifdef NEED_STD_MOVE_FALLBACK
82
// Provide fallback variant of "std::move" for older compilers with
83
// incomplete/broken C++11 support.
84
namespace std {
85
86
template<typename _Tp>
87
inline typename std::remove_reference<_Tp>::type&& move(_Tp&& __t) {
88
  return static_cast<typename std::remove_reference<_Tp>::type&&>(__t);
89
}
90
91
}  // namespace std
92
#endif
93
94
#ifdef NEED_NULLPTR_FALLBACK
95
// Compilers with partial/incomplete support for C++11 don't know about
96
// "nullptr". A simple alias should be fine for our use case.
97
#define nullptr NULL
98
#endif
99
100
#ifdef _MSC_VER
101
  #ifdef _CPPRTTI
102
  #define RTTI_ENABLED
103
  #endif
104
#else
105
  #ifdef __GXX_RTTI
106
  #define RTTI_ENABLED
107
  #endif
108
#endif
109
110
//inline uint8_t Clip1_8bit(int16_t value) { if (value<=0) return 0; else if (value>=255) return 255; else return value; }
111
45.9M
#define Clip1_8bit(value) ((value)<0 ? 0 : (value)>255 ? 255 : (value))
112
214M
#define Clip_BitDepth(value, bit_depth) ((value)<0 ? 0 : (value)>((1<<bit_depth)-1) ? ((1<<bit_depth)-1) : (value))
113
585M
#define Clip3(low,high,value) ((value)<(low) ? (low) : (value)>(high) ? (high) : (value))
114
8.73M
#define Sign(value) (((value)<0) ? -1 : ((value)>0) ? 1 : 0)
115
54.7M
#define abs_value(a) (((a)<0) ? -(a) : (a))
116
13.2M
#define libde265_min(a,b) (((a)<(b)) ? (a) : (b))
117
9.20M
#define libde265_max(a,b) (((a)>(b)) ? (a) : (b))
118
119
LIBDE265_INLINE static int ceil_div(int num,int denom)
120
12.1k
{
121
12.1k
  num += denom-1;
122
12.1k
  return num/denom;
123
12.1k
}
Unexecuted instantiation: de265.cc:ceil_div(int, int)
Unexecuted instantiation: decctx.cc:ceil_div(int, int)
Unexecuted instantiation: contextmodel.cc:ceil_div(int, int)
Unexecuted instantiation: deblock.cc:ceil_div(int, int)
Unexecuted instantiation: cabac.cc:ceil_div(int, int)
Unexecuted instantiation: fallback.cc:ceil_div(int, int)
Unexecuted instantiation: fallback-dct.cc:ceil_div(int, int)
Unexecuted instantiation: fallback-motion.cc:ceil_div(int, int)
Unexecuted instantiation: dpb.cc:ceil_div(int, int)
Unexecuted instantiation: image.cc:ceil_div(int, int)
Unexecuted instantiation: nal-parser.cc:ceil_div(int, int)
Unexecuted instantiation: pps.cc:ceil_div(int, int)
Unexecuted instantiation: sao.cc:ceil_div(int, int)
Unexecuted instantiation: sei.cc:ceil_div(int, int)
Unexecuted instantiation: slice.cc:ceil_div(int, int)
Unexecuted instantiation: refpic.cc:ceil_div(int, int)
Unexecuted instantiation: intrapred.cc:ceil_div(int, int)
Unexecuted instantiation: motion.cc:ceil_div(int, int)
sps.cc:ceil_div(int, int)
Line
Count
Source
120
12.1k
{
121
12.1k
  num += denom-1;
122
12.1k
  return num/denom;
123
12.1k
}
Unexecuted instantiation: threads.cc:ceil_div(int, int)
Unexecuted instantiation: transform.cc:ceil_div(int, int)
Unexecuted instantiation: util.cc:ceil_div(int, int)
Unexecuted instantiation: vps.cc:ceil_div(int, int)
Unexecuted instantiation: vui.cc:ceil_div(int, int)
Unexecuted instantiation: sse-dct.cc:ceil_div(int, int)
Unexecuted instantiation: sse-motion.cc:ceil_div(int, int)
124
125
LIBDE265_INLINE static int ceil_log2(int val)
126
3.68k
{
127
3.68k
  int n=0;
128
10.6k
  while (val > (1<<n)) {
129
6.94k
    n++;
130
6.94k
  }
131
132
3.68k
  return n;
133
3.68k
}
Unexecuted instantiation: de265.cc:ceil_log2(int)
Unexecuted instantiation: decctx.cc:ceil_log2(int)
Unexecuted instantiation: contextmodel.cc:ceil_log2(int)
Unexecuted instantiation: deblock.cc:ceil_log2(int)
Unexecuted instantiation: cabac.cc:ceil_log2(int)
Unexecuted instantiation: fallback.cc:ceil_log2(int)
Unexecuted instantiation: fallback-dct.cc:ceil_log2(int)
Unexecuted instantiation: fallback-motion.cc:ceil_log2(int)
Unexecuted instantiation: dpb.cc:ceil_log2(int)
Unexecuted instantiation: image.cc:ceil_log2(int)
Unexecuted instantiation: nal-parser.cc:ceil_log2(int)
Unexecuted instantiation: pps.cc:ceil_log2(int)
Unexecuted instantiation: sao.cc:ceil_log2(int)
Unexecuted instantiation: sei.cc:ceil_log2(int)
slice.cc:ceil_log2(int)
Line
Count
Source
126
3.68k
{
127
3.68k
  int n=0;
128
10.6k
  while (val > (1<<n)) {
129
6.94k
    n++;
130
6.94k
  }
131
132
3.68k
  return n;
133
3.68k
}
Unexecuted instantiation: refpic.cc:ceil_log2(int)
Unexecuted instantiation: intrapred.cc:ceil_log2(int)
Unexecuted instantiation: motion.cc:ceil_log2(int)
Unexecuted instantiation: sps.cc:ceil_log2(int)
Unexecuted instantiation: threads.cc:ceil_log2(int)
Unexecuted instantiation: transform.cc:ceil_log2(int)
Unexecuted instantiation: util.cc:ceil_log2(int)
Unexecuted instantiation: vps.cc:ceil_log2(int)
Unexecuted instantiation: vui.cc:ceil_log2(int)
Unexecuted instantiation: sse-dct.cc:ceil_log2(int)
Unexecuted instantiation: sse-motion.cc:ceil_log2(int)
134
135
LIBDE265_INLINE static int Log2(int v)
136
9.84M
{
137
9.84M
  int n=0;
138
30.7M
  while (v>1) {
139
20.9M
    n++;
140
20.9M
    v>>=1;
141
20.9M
  }
142
143
9.84M
  return n;
144
9.84M
}
Unexecuted instantiation: de265.cc:Log2(int)
Unexecuted instantiation: decctx.cc:Log2(int)
Unexecuted instantiation: contextmodel.cc:Log2(int)
Unexecuted instantiation: deblock.cc:Log2(int)
Unexecuted instantiation: cabac.cc:Log2(int)
Unexecuted instantiation: fallback.cc:Log2(int)
fallback-dct.cc:Log2(int)
Line
Count
Source
136
1.57M
{
137
1.57M
  int n=0;
138
4.98M
  while (v>1) {
139
3.41M
    n++;
140
3.41M
    v>>=1;
141
3.41M
  }
142
143
1.57M
  return n;
144
1.57M
}
Unexecuted instantiation: fallback-motion.cc:Log2(int)
Unexecuted instantiation: dpb.cc:Log2(int)
Unexecuted instantiation: image.cc:Log2(int)
Unexecuted instantiation: nal-parser.cc:Log2(int)
Unexecuted instantiation: pps.cc:Log2(int)
Unexecuted instantiation: sao.cc:Log2(int)
Unexecuted instantiation: sei.cc:Log2(int)
Unexecuted instantiation: slice.cc:Log2(int)
Unexecuted instantiation: refpic.cc:Log2(int)
intrapred.cc:Log2(int)
Line
Count
Source
136
3.52M
{
137
3.52M
  int n=0;
138
10.9M
  while (v>1) {
139
7.41M
    n++;
140
7.41M
    v>>=1;
141
7.41M
  }
142
143
3.52M
  return n;
144
3.52M
}
Unexecuted instantiation: motion.cc:Log2(int)
Unexecuted instantiation: sps.cc:Log2(int)
Unexecuted instantiation: threads.cc:Log2(int)
transform.cc:Log2(int)
Line
Count
Source
136
4.74M
{
137
4.74M
  int n=0;
138
14.8M
  while (v>1) {
139
10.0M
    n++;
140
10.0M
    v>>=1;
141
10.0M
  }
142
143
4.74M
  return n;
144
4.74M
}
Unexecuted instantiation: util.cc:Log2(int)
Unexecuted instantiation: vps.cc:Log2(int)
Unexecuted instantiation: vui.cc:Log2(int)
Unexecuted instantiation: sse-dct.cc:Log2(int)
Unexecuted instantiation: sse-motion.cc:Log2(int)
145
146
LIBDE265_INLINE static int Log2SizeToArea(int v)
147
0
{
148
0
  return (1<<(v<<1));
149
0
}
Unexecuted instantiation: de265.cc:Log2SizeToArea(int)
Unexecuted instantiation: decctx.cc:Log2SizeToArea(int)
Unexecuted instantiation: contextmodel.cc:Log2SizeToArea(int)
Unexecuted instantiation: deblock.cc:Log2SizeToArea(int)
Unexecuted instantiation: cabac.cc:Log2SizeToArea(int)
Unexecuted instantiation: fallback.cc:Log2SizeToArea(int)
Unexecuted instantiation: fallback-dct.cc:Log2SizeToArea(int)
Unexecuted instantiation: fallback-motion.cc:Log2SizeToArea(int)
Unexecuted instantiation: dpb.cc:Log2SizeToArea(int)
Unexecuted instantiation: image.cc:Log2SizeToArea(int)
Unexecuted instantiation: nal-parser.cc:Log2SizeToArea(int)
Unexecuted instantiation: pps.cc:Log2SizeToArea(int)
Unexecuted instantiation: sao.cc:Log2SizeToArea(int)
Unexecuted instantiation: sei.cc:Log2SizeToArea(int)
Unexecuted instantiation: slice.cc:Log2SizeToArea(int)
Unexecuted instantiation: refpic.cc:Log2SizeToArea(int)
Unexecuted instantiation: intrapred.cc:Log2SizeToArea(int)
Unexecuted instantiation: motion.cc:Log2SizeToArea(int)
Unexecuted instantiation: sps.cc:Log2SizeToArea(int)
Unexecuted instantiation: threads.cc:Log2SizeToArea(int)
Unexecuted instantiation: transform.cc:Log2SizeToArea(int)
Unexecuted instantiation: util.cc:Log2SizeToArea(int)
Unexecuted instantiation: vps.cc:Log2SizeToArea(int)
Unexecuted instantiation: vui.cc:Log2SizeToArea(int)
Unexecuted instantiation: sse-dct.cc:Log2SizeToArea(int)
Unexecuted instantiation: sse-motion.cc:Log2SizeToArea(int)
150
151
void copy_subimage(uint8_t* dst,int dststride,
152
                   const uint8_t* src,int srcstride,
153
                   int w, int h);
154
155
156
// === logging ===
157
158
enum LogModule {
159
  LogHighlevel,
160
  LogHeaders,
161
  LogSlice,
162
  LogDPB,
163
  LogMotion,
164
  LogTransform,
165
  LogDeblock,
166
  LogSAO,
167
  LogSEI,
168
  LogIntraPred,
169
  LogPixels,
170
  LogSymbols,
171
  LogCABAC,
172
  LogEncoder,
173
  LogEncoderMetadata,
174
  NUMBER_OF_LogModules
175
};
176
177
178
#if defined(DE265_LOG_ERROR) || defined(DE265_LOG_INFO) || defined(DE265_LOG_DEBUG) || defined(DE265_LOG_TRACE)
179
# define DE265_LOGGING 1
180
void enable_logging(enum LogModule);
181
void disable_logging(enum LogModule);
182
#else
183
#define enable_logging(x) { }
184
#define disable_logging(x) { }
185
#endif
186
187
#ifdef DE265_LOGGING
188
void log_set_current_POC(int poc);
189
#else
190
#define log_set_current_POC(poc) { }
191
#endif
192
193
#ifdef DE265_LOG_ERROR
194
void logerror(enum LogModule module, const char* string, ...);
195
#else
196
#define logerror(a,b, ...) { }
197
#endif
198
199
#ifdef DE265_LOG_INFO
200
void loginfo (enum LogModule module, const char* string, ...);
201
#else
202
168k
#define loginfo(a,b, ...) { }
203
#endif
204
205
#ifdef DE265_LOG_DEBUG
206
void logdebug(enum LogModule module, const char* string, ...);
207
bool logdebug_enabled(enum LogModule module);
208
#else
209
6.43M
#define logdebug(a,b, ...) { }
210
0
inline bool logdebug_enabled(enum LogModule module) { return false; }
211
#endif
212
213
#ifdef DE265_LOG_TRACE
214
void logtrace(enum LogModule module, const char* string, ...);
215
#else
216
2.63G
#define logtrace(a,b, ...) { }
217
#endif
218
219
void log2fh(FILE* fh, const char* string, ...);
220
221
222
void printBlk(const char* title,const int32_t* data, int blksize, int stride, const std::string& prefix="  ");
223
void printBlk(const char* title,const int16_t* data, int blksize, int stride, const std::string& prefix="  ");
224
void printBlk(const char* title,const uint8_t* data, int blksize, int stride, const std::string& prefix="  ");
225
226
void debug_set_image_output(void (*)(const struct de265_image*, int slot));
227
void debug_show_image(const struct de265_image*, int slot);
228
229
#endif