2fb0a7a2060559580f75b606db23c5ccabc9bad9
1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Deepthi Nandakumar <deepthi@multicorewareinc.com>
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.
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.
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.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
26 #include "threading.h"
30 #include <sys/types.h>
31 #include <sys/timeb.h>
36 int64_t x265_mdate(void)
41 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
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
;
51 #define X265_ALIGNBYTES 32
54 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
55 #define _aligned_malloc __mingw_aligned_malloc
56 #define _aligned_free __mingw_aligned_free
60 void *x265_malloc(size_t size
)
62 return _aligned_malloc(size
, X265_ALIGNBYTES
);
65 void x265_free(void *ptr
)
67 if (ptr
) _aligned_free(ptr
);
71 void *x265_malloc(size_t size
)
75 if (posix_memalign((void**)&ptr
, X265_ALIGNBYTES
, size
) == 0)
81 void x265_free(void *ptr
)
88 /* Not a general-purpose function; multiplies input by -1/6 to convert
90 int x265_exp2fix8(double x
)
92 int i
= (int)(x
* (-64.f
/ 6.f
) + 512.5f
);
95 if (i
> 1023) return 0xffff;
96 return (x265_exp2_lut
[i
& 63] + 256) << (i
>> 6) >> 8;
99 void x265_log(const x265_param
*param
, int level
, const char *fmt
, ...)
101 if (param
&& level
> param
->logLevel
)
103 const char *log_level
;
109 case X265_LOG_WARNING
:
110 log_level
= "warning";
122 log_level
= "unknown";
126 fprintf(stderr
, "x265 [%s]: ", log_level
);
129 vfprintf(stderr
, fmt
, arg
);
133 double x265_ssim2dB(double ssim
)
135 double inv_ssim
= 1 - ssim
;
137 if (inv_ssim
<= 0.0000000001) /* Max 100dB */
140 return -10.0 * log10(inv_ssim
);
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
)
147 return 12.0 + 6.0 * (double)X265_LOG2(qScale
/ 0.85);
150 double x265_qp2qScale(double qp
)
152 return 0.85 * pow(2.0, (qp
- 12.0) / 6.0);
155 uint32_t x265_picturePlaneSize(int csp
, int width
, int height
, int plane
)
157 uint32_t size
= (uint32_t)(width
>> x265_cli_csps
[csp
].width
[plane
]) * (height
>> x265_cli_csps
[csp
].height
[plane
]);
162 char* x265_slurp_file(const char *filename
)
171 FILE *fh
= fopen(filename
, "rb");
174 x265_log(NULL
, X265_LOG_ERROR
, "unable to open file %s\n", filename
);
178 bError
|= fseek(fh
, 0, SEEK_END
) < 0;
179 bError
|= (fSize
= ftell(fh
)) <= 0;
180 bError
|= fseek(fh
, 0, SEEK_SET
) < 0;
184 buf
= X265_MALLOC(char, fSize
+ 2);
187 x265_log(NULL
, X265_LOG_ERROR
, "unable to allocate memory\n");
191 bError
|= fread(buf
, 1, fSize
, fh
) != fSize
;
192 if (buf
[fSize
- 1] != '\n')
199 x265_log(NULL
, X265_LOG_ERROR
, "unable to read the file\n");