digiKam
util.h
Go to the documentation of this file.
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 #define LIBDE265_DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
47 #define likely(x) __builtin_expect(!!(x), 1)
48 #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 #define ALIGNED_32( var ) LIBDE265_DECLARE_ALIGNED( var, 32 )
63 #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 #define Clip1_8bit(value) ((value)<0 ? 0 : (value)>255 ? 255 : (value))
112 #define Clip_BitDepth(value, bit_depth) ((value)<0 ? 0 : (value)>((1<<bit_depth)-1) ? ((1<<bit_depth)-1) : (value))
113 #define Clip3(low,high,value) ((value)<(low) ? (low) : (value)>(high) ? (high) : (value))
114 #define Sign(value) (((value)<0) ? -1 : ((value)>0) ? 1 : 0)
115 #define abs_value(a) (((a)<0) ? -(a) : (a))
116 #define libde265_min(a,b) (((a)<(b)) ? (a) : (b))
117 #define libde265_max(a,b) (((a)>(b)) ? (a) : (b))
118 
119 LIBDE265_INLINE static int ceil_div(int num,int denom)
120 {
121  num += denom-1;
122  return num/denom;
123 }
124 
125 LIBDE265_INLINE static int ceil_log2(int val)
126 {
127  int n=0;
128  while (val > (1<<n)) {
129  n++;
130  }
131 
132  return n;
133 }
134 
135 LIBDE265_INLINE static int Log2(int v)
136 {
137  int n=0;
138  while (v>1) {
139  n++;
140  v>>=1;
141  }
142 
143  return n;
144 }
145 
146 LIBDE265_INLINE static int Log2SizeToArea(int v)
147 {
148  return (1<<(v<<1));
149 }
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 {
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 #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 #define logdebug(a,b, ...) { }
210 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 #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
#define LIBDE265_INLINE
Definition: de265.h:66
QStringView prefix
Definition: itemviewutilities.cpp:593
Definition: image.h:222
void copy_subimage(uint8_t *dst, int dststride, const uint8_t *src, int srcstride, int w, int h)
#define enable_logging(x)
Definition: util.h:183
#define loginfo(a, b,...)
Definition: util.h:202
#define logtrace(a, b,...)
Definition: util.h:216
#define log_set_current_POC(poc)
Definition: util.h:190
#define disable_logging(x)
Definition: util.h:184
#define logerror(a, b,...)
Definition: util.h:196
void log2fh(FILE *fh, const char *string,...)
void debug_set_image_output(void(*)(const struct de265_image *, int slot))
LogModule
Definition: util.h:158
@ LogSAO
Definition: util.h:166
@ LogIntraPred
Definition: util.h:168
@ LogDPB
Definition: util.h:162
@ LogSymbols
Definition: util.h:170
@ LogHighlevel
Definition: util.h:159
@ LogSlice
Definition: util.h:161
@ LogPixels
Definition: util.h:169
@ LogSEI
Definition: util.h:167
@ LogDeblock
Definition: util.h:165
@ LogEncoderMetadata
Definition: util.h:173
@ LogTransform
Definition: util.h:164
@ LogCABAC
Definition: util.h:171
@ LogEncoder
Definition: util.h:172
@ LogMotion
Definition: util.h:163
@ NUMBER_OF_LogModules
Definition: util.h:174
@ LogHeaders
Definition: util.h:160
void printBlk(const char *title, const int32_t *data, int blksize, int stride, const std::string &prefix=" ")
void debug_show_image(const struct de265_image *, int slot)
#define logdebug(a, b,...)
Definition: util.h:209
bool logdebug_enabled(enum LogModule module)
Definition: util.h:210