Imported Upstream version 1.4
[deb_x265.git] / source / common / common.cpp
1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
3 *
4 * Authors: Deepthi Nandakumar <deepthi@multicorewareinc.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
23
24 #include "common.h"
25 #include "slice.h"
26 #include "threading.h"
27 #include "x265.h"
28
29 #if _WIN32
30 #include <sys/types.h>
31 #include <sys/timeb.h>
32 #else
33 #include <sys/time.h>
34 #endif
35
36 int64_t x265_mdate(void)
37 {
38 #if _WIN32
39 struct timeb tb;
40 ftime(&tb);
41 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
42 #else
43 struct timeval tv_date;
44 gettimeofday(&tv_date, NULL);
45 return (int64_t)tv_date.tv_sec * 1000000 + (int64_t)tv_date.tv_usec;
46 #endif
47 }
48
49 using namespace x265;
50
51 #define X265_ALIGNBYTES 32
52
53 #if _WIN32
54 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
55 #define _aligned_malloc __mingw_aligned_malloc
56 #define _aligned_free __mingw_aligned_free
57 #include "malloc.h"
58 #endif
59
60 void *x265_malloc(size_t size)
61 {
62 return _aligned_malloc(size, X265_ALIGNBYTES);
63 }
64
65 void x265_free(void *ptr)
66 {
67 if (ptr) _aligned_free(ptr);
68 }
69
70 #else // if _WIN32
71 void *x265_malloc(size_t size)
72 {
73 void *ptr;
74
75 if (posix_memalign((void**)&ptr, X265_ALIGNBYTES, size) == 0)
76 return ptr;
77 else
78 return NULL;
79 }
80
81 void x265_free(void *ptr)
82 {
83 if (ptr) free(ptr);
84 }
85
86 #endif // if _WIN32
87
88 /* Not a general-purpose function; multiplies input by -1/6 to convert
89 * qp to qscale. */
90 int x265_exp2fix8(double x)
91 {
92 int i = (int)(x * (-64.f / 6.f) + 512.5f);
93
94 if (i < 0) return 0;
95 if (i > 1023) return 0xffff;
96 return (x265_exp2_lut[i & 63] + 256) << (i >> 6) >> 8;
97 }
98
99 void x265_log(const x265_param *param, int level, const char *fmt, ...)
100 {
101 if (param && level > param->logLevel)
102 return;
103 const char *log_level;
104 switch (level)
105 {
106 case X265_LOG_ERROR:
107 log_level = "error";
108 break;
109 case X265_LOG_WARNING:
110 log_level = "warning";
111 break;
112 case X265_LOG_INFO:
113 log_level = "info";
114 break;
115 case X265_LOG_DEBUG:
116 log_level = "debug";
117 break;
118 case X265_LOG_FULL:
119 log_level = "full";
120 break;
121 default:
122 log_level = "unknown";
123 break;
124 }
125
126 fprintf(stderr, "x265 [%s]: ", log_level);
127 va_list arg;
128 va_start(arg, fmt);
129 vfprintf(stderr, fmt, arg);
130 va_end(arg);
131 }
132
133 double x265_ssim2dB(double ssim)
134 {
135 double inv_ssim = 1 - ssim;
136
137 if (inv_ssim <= 0.0000000001) /* Max 100dB */
138 return 100;
139
140 return -10.0 * log10(inv_ssim);
141 }
142
143 /* The qscale - qp conversion is specified in the standards.
144 * Approx qscale increases by 12% with every qp increment */
145 double x265_qScale2qp(double qScale)
146 {
147 return 12.0 + 6.0 * (double)X265_LOG2(qScale / 0.85);
148 }
149
150 double x265_qp2qScale(double qp)
151 {
152 return 0.85 * pow(2.0, (qp - 12.0) / 6.0);
153 }
154
155 uint32_t x265_picturePlaneSize(int csp, int width, int height, int plane)
156 {
157 uint32_t size = (uint32_t)(width >> x265_cli_csps[csp].width[plane]) * (height >> x265_cli_csps[csp].height[plane]);
158
159 return size;
160 }
161
162 char* x265_slurp_file(const char *filename)
163 {
164 if (!filename)
165 return NULL;
166
167 int bError = 0;
168 size_t fSize;
169 char *buf = NULL;
170
171 FILE *fh = fopen(filename, "rb");
172 if (!fh)
173 {
174 x265_log(NULL, X265_LOG_ERROR, "unable to open file %s\n", filename);
175 return NULL;
176 }
177
178 bError |= fseek(fh, 0, SEEK_END) < 0;
179 bError |= (fSize = ftell(fh)) <= 0;
180 bError |= fseek(fh, 0, SEEK_SET) < 0;
181 if (bError)
182 goto error;
183
184 buf = X265_MALLOC(char, fSize + 2);
185 if (!buf)
186 {
187 x265_log(NULL, X265_LOG_ERROR, "unable to allocate memory\n");
188 goto error;
189 }
190
191 bError |= fread(buf, 1, fSize, fh) != fSize;
192 if (buf[fSize - 1] != '\n')
193 buf[fSize++] = '\n';
194 buf[fSize] = 0;
195 fclose(fh);
196
197 if (bError)
198 {
199 x265_log(NULL, X265_LOG_ERROR, "unable to read the file\n");
200 X265_FREE(buf);
201 buf = NULL;
202 }
203 return buf;
204
205 error:
206 fclose(fh);
207 return NULL;
208 }