Skip to content

Instantly share code, notes, and snippets.

@msg7086
Last active August 29, 2015 14:21
Show Gist options
  • Save msg7086/6fef5d2a3bdef01fa1ca to your computer and use it in GitHub Desktop.
Save msg7086/6fef5d2a3bdef01fa1ca to your computer and use it in GitHub Desktop.
# HG changeset patch
# Parent 46687cf14e993c9e6e8df3839e276bb175a4d9a9
diff -r 46687cf14e99 source/CMakeLists.txt
--- a/source/CMakeLists.txt Tue May 12 21:01:59 2015 -0700
+++ b/source/CMakeLists.txt Tue May 12 21:23:50 2015 -0700
@@ -521,18 +521,20 @@
endif(NOT HAVE_GETOPT_H)
if(XCODE)
- # Xcode seems unable to link the CLI with libs, so link as one targget
- add_executable(cli ../COPYING ${InputFiles} ${OutputFiles} ${FilterFiles} ${GETOPT} x265.cpp x265.h x265cli.h
+ # Xcode seems unable to link the CLI with libs, so link as one target
+ add_executable(cli ../COPYING ${InputFiles} ${OutputFiles} ${FilterFiles} ${GETOPT} x265.cpp x265_shim.cpp x265.h x265cli.h
$<TARGET_OBJECTS:encoder> $<TARGET_OBJECTS:common> ${YASM_OBJS} ${YASM_SRCS})
else()
- add_executable(cli ../COPYING ${InputFiles} ${OutputFiles} ${FilterFiles} ${GETOPT} ${X265_RC_FILE} x265.cpp x265.h x265cli.h)
- if(WIN32 OR NOT ENABLE_SHARED OR INTEL_CXX)
- # The CLI cannot link to the shared library on Windows, it
- # requires internal APIs not exported from the DLL
- target_link_libraries(cli x265-static ${PLATFORM_LIBS})
- else()
- target_link_libraries(cli x265-shared ${PLATFORM_LIBS})
- endif()
+ add_executable(cli ../COPYING ${InputFiles} ${OutputFiles} ${FilterFiles} ${GETOPT} ${X265_RC_FILE} x265.cpp x265_shim.cpp x265.h x265cli.h
+ common/common.cpp common/constants.cpp common/threading.cpp)
+ # if(WIN32 OR NOT ENABLE_SHARED OR INTEL_CXX)
+ # # The CLI cannot link to the shared library on Windows, it
+ # # requires internal APIs not exported from the DLL
+ # target_link_libraries(cli x265-static ${PLATFORM_LIBS})
+ # else()
+ # target_link_libraries(cli x265-shared ${PLATFORM_LIBS})
+ # endif()
+ target_link_libraries(cli ${PLATFORM_LIBS})
endif()
set_target_properties(cli PROPERTIES OUTPUT_NAME x265)
if(LINKER_OPTIONS)
diff -r 46687cf14e99 source/common/common.h
--- a/source/common/common.h Tue May 12 21:01:59 2015 -0700
+++ b/source/common/common.h Tue May 12 21:23:50 2015 -0700
@@ -426,7 +426,8 @@
void x265_free(void *ptr);
char* x265_slurp_file(const char *filename);
-void x265_setup_primitives(x265_param* param, int cpu); /* primitives.cpp */
+void x265_setup_primitives(x265_param* param, int cpuid); /* primitives.cpp */
+int x265_detect_cpu(x265_param* param, int cpuid); /* primitives.cpp */
#include "constants.h"
diff -r 46687cf14e99 source/common/primitives.cpp
--- a/source/common/primitives.cpp Tue May 12 21:01:59 2015 -0700
+++ b/source/common/primitives.cpp Tue May 12 21:23:50 2015 -0700
@@ -190,10 +190,9 @@
/* cpuid >= 0 - force CPU type
* cpuid < 0 - auto-detect if uninitialized */
-void x265_setup_primitives(x265_param *param, int cpuid)
+void x265_setup_primitives(x265_param* param, int cpuid)
{
- if (cpuid < 0)
- cpuid = x265::cpu_detect();
+ cpuid = x265_detect_cpu(param, cpuid);
// initialize global variables
if (!primitives.pu[0].sad)
@@ -215,6 +214,12 @@
setupAliasPrimitives(primitives);
}
+}
+
+int x265_detect_cpu(x265_param* param, int cpuid)
+{
+ if (cpuid < 0)
+ cpuid = x265::cpu_detect();
if (param->logLevel >= X265_LOG_INFO)
{
@@ -247,6 +252,8 @@
sprintf(p, " none!");
x265_log(param, X265_LOG_INFO, "%s\n", buf);
}
+
+ return cpuid;
}
#if ENABLE_ASSEMBLY
diff -r 46687cf14e99 source/encoder/api.cpp
--- a/source/encoder/api.cpp Tue May 12 21:01:59 2015 -0700
+++ b/source/encoder/api.cpp Tue May 12 21:23:50 2015 -0700
@@ -271,6 +271,7 @@
&x265_encoder_log,
&x265_encoder_close,
&x265_cleanup,
+ &x265_detect_cpu,
x265_version_str,
x265_build_info_str,
x265_max_bit_depth,
diff -r 46687cf14e99 source/x265.cpp
--- a/source/x265.cpp Tue May 12 21:01:59 2015 -0700
+++ b/source/x265.cpp Tue May 12 21:23:50 2015 -0700
@@ -215,7 +215,7 @@
{
bool bError = 0;
int help = 0;
- int inputBitDepth = 8;
+ int inputBitDepth = 0;
int outputBitDepth = 0;
int reconFileBitDepth = 0;
const char *inputfn = NULL;
@@ -232,9 +232,16 @@
}
/* Presets are applied before all other options. */
+#define OPT(longname) \
+ else if (!strcmp(long_options[long_options_index].name, longname))
+#define OPT2(name1, name2) \
+ else if (!strcmp(long_options[long_options_index].name, name1) || \
+ !strcmp(long_options[long_options_index].name, name2))
+
for (optind = 0;; )
{
- int c = getopt_long(argc, argv, short_options, long_options, NULL);
+ int long_options_index = -1;
+ int c = getopt_long(argc, argv, short_options, long_options, &long_options_index);
if (c == -1)
break;
else if (c == 'p')
@@ -244,14 +251,20 @@
else if (c == 'D')
outputBitDepth = atoi(optarg);
else if (c == '?')
- showHelp(param);
+ showHelp(param, api);
+ else if (long_options_index < 0)
+ ;
+ OPT("input-depth") inputBitDepth = (uint32_t)x265_atoi(optarg, bError);
}
+ if (outputBitDepth == 0)
+ outputBitDepth = inputBitDepth;
+
api = x265_api_get(outputBitDepth);
if (!api)
{
- x265_log(NULL, X265_LOG_WARNING, "falling back to default bit-depth\n");
- api = x265_api_get(0);
+ x265_log(NULL, X265_LOG_ERROR, "unable to load x265 library\n");
+ return true;
}
param = api->param_alloc();
@@ -277,12 +290,12 @@
switch (c)
{
case 'h':
- showHelp(param);
+ showHelp(param, api);
break;
case 'V':
- printVersion(param);
- x265_setup_primitives(param, -1);
+ printVersion(param, api);
+ api->detect_cpu(param, -1);
exit(0);
default:
@@ -310,11 +323,6 @@
x265_log(NULL, X265_LOG_WARNING, "short option '%c' unrecognized\n", c);
return true;
}
-#define OPT(longname) \
- else if (!strcmp(long_options[long_options_index].name, longname))
-#define OPT2(name1, name2) \
- else if (!strcmp(long_options[long_options_index].name, name1) || \
- !strcmp(long_options[long_options_index].name, name2))
if (0) ;
OPT2("frame-skip", "seek") this->seek = (uint32_t)x265_atoi(optarg, bError);
@@ -351,9 +359,13 @@
x265_log(NULL, X265_LOG_ERROR, "invalid argument: %s = %s\n", name, optarg);
return true;
}
-#undef OPT
}
}
+#undef OPT
+#undef OPT2
+
+ if (inputBitDepth == 0)
+ inputBitDepth = 8;
if (optind < argc && !inputfn)
inputfn = argv[optind++];
@@ -366,7 +378,7 @@
}
if (argc <= 1 || help)
- showHelp(param);
+ showHelp(param, api);
if (inputfn == NULL || outputfn == NULL)
{
diff -r 46687cf14e99 source/x265.h
--- a/source/x265.h Tue May 12 21:01:59 2015 -0700
+++ b/source/x265.h Tue May 12 21:23:50 2015 -0700
@@ -1316,6 +1316,7 @@
void (*encoder_log)(x265_encoder*, int, char**);
void (*encoder_close)(x265_encoder*);
void (*cleanup)(void);
+ int (*detect_cpu)(x265_param*, int);
const char* version_str;
const char* build_info_str;
int max_bit_depth;
diff -r 46687cf14e99 source/x265_shim.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/source/x265_shim.cpp Tue May 12 21:23:50 2015 -0700
@@ -0,0 +1,157 @@
+/*****************************************************************************
+ * Copyright (C) 2015 x265 project
+ *
+ * Authors:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
+ *
+ * This program is also available under a commercial proprietary license.
+ * For more information, contact us at license @ x265.com.
+ *****************************************************************************/
+
+#include "common.h"
+#include "cpu.h"
+
+typedef const x265_api* (*api_get_func)(int bitDepth);
+
+#define xstr(s) str(s)
+#define str(s) #s
+
+#if _WIN32
+#include <windows.h>
+#define ext ".dll"
+#define dlopen(h, libname) HMODULE h = LoadLibraryA(libname)
+#else
+#include <dlfcn.h>
+#define HMODULE void*
+#define LoadLibraryA(libname) dlopen(libname, RTLD_LAZY | RTLD_LOCAL);
+#define GetProcAddress dlsym
+ #if MACOS
+ #define ext ".dylib"
+ #else
+ #define ext ".so"
+ #endif
+#endif
+
+const x265_api* x265_api_get(int bitDepth)
+{
+ const x265_api* api = NULL;
+
+ if (!bitDepth)
+ {
+ for (bitDepth = 8; bitDepth <= 10; bitDepth += 2) // Since we don't do 9-bit
+ {
+ api = x265_api_get(bitDepth);
+ if (api)
+ return api;
+ }
+ return NULL;
+ }
+
+ char libname[1024];
+ const char* method = "x265_api_get_" xstr(X265_BUILD);
+
+ if (bitDepth == 10 || bitDepth == 8)
+ snprintf(libname, 1024, "libx265_%dbit" ext, bitDepth);
+ else
+ {
+ x265_log(NULL, X265_LOG_WARNING, "bitdepth %d not supported\n", bitDepth);
+ return NULL;
+ }
+
+ x265_log(NULL, X265_LOG_INFO, "loading %s...\n", libname);
+ HMODULE h = LoadLibraryA(libname);
+ if (h)
+ {
+ api_get_func get = (api_get_func)GetProcAddress(h, method);
+ if (get)
+ api = get(0);
+ else
+ x265_log(NULL, X265_LOG_WARNING, "Unable to bind %s from %s\n", method, libname);
+ }
+ else
+ x265_log(NULL, X265_LOG_WARNING, "Unable to open %s\n", libname);
+
+ if (api && bitDepth != api->max_bit_depth)
+ {
+ x265_log(NULL, X265_LOG_WARNING, "%s does not support requested bitDepth %d\n", libname, bitDepth);
+ return NULL;
+ }
+
+ return api;
+}
+namespace x265 {
+
+int x265_atoi(const char* str, bool& bError)
+{
+ char *end;
+ int v = strtol(str, &end, 0);
+
+ if (end == str || *end != '\0')
+ bError = true;
+ return v;
+}
+static const int fixedRatios[][2] =
+{
+ { 1, 1 },
+ { 12, 11 },
+ { 10, 11 },
+ { 16, 11 },
+ { 40, 33 },
+ { 24, 11 },
+ { 20, 11 },
+ { 32, 11 },
+ { 80, 33 },
+ { 18, 11 },
+ { 15, 11 },
+ { 64, 33 },
+ { 160, 99 },
+ { 4, 3 },
+ { 3, 2 },
+ { 2, 1 },
+};
+void setParamAspectRatio(x265_param* p, int width, int height)
+{
+ p->vui.aspectRatioIdc = X265_EXTENDED_SAR;
+ p->vui.sarWidth = width;
+ p->vui.sarHeight = height;
+ for (size_t i = 0; i < sizeof(fixedRatios) / sizeof(fixedRatios[0]); i++)
+ {
+ if (width == fixedRatios[i][0] && height == fixedRatios[i][1])
+ {
+ p->vui.aspectRatioIdc = (int)i + 1;
+ return;
+ }
+ }
+}
+
+void getParamAspectRatio(x265_param* p, int& width, int& height)
+{
+ if (!p->vui.aspectRatioIdc)
+ width = height = 0;
+ else if ((size_t)p->vui.aspectRatioIdc <= sizeof(fixedRatios) / sizeof(fixedRatios[0]))
+ {
+ width = fixedRatios[p->vui.aspectRatioIdc - 1][0];
+ height = fixedRatios[p->vui.aspectRatioIdc - 1][1];
+ }
+ else if (p->vui.aspectRatioIdc == X265_EXTENDED_SAR)
+ {
+ width = p->vui.sarWidth;
+ height = p->vui.sarHeight;
+ }
+ else
+ width = height = 0;
+}
+}
diff -r 46687cf14e99 source/x265cli.h
--- a/source/x265cli.h Tue May 12 21:01:59 2015 -0700
+++ b/source/x265cli.h Tue May 12 21:23:50 2015 -0700
@@ -231,10 +231,10 @@
{ 0, 0, 0, 0 }
};
-static void printVersion(x265_param *param)
+static void printVersion(x265_param *param, const x265_api* api)
{
- x265_log(param, X265_LOG_INFO, "HEVC encoder version %s\n", x265_version_str);
- x265_log(param, X265_LOG_INFO, "build info %s\n", x265_build_info_str);
+ x265_log(param, X265_LOG_INFO, "HEVC encoder version %s\n", api->version_str);
+ x265_log(param, X265_LOG_INFO, "build info %s\n", api->build_info_str);
#ifdef ENABLE_LSMASH
x265_log(param, X265_LOG_INFO, "(lsmash %d.%d.%d)\n", LSMASH_VERSION_MAJOR, LSMASH_VERSION_MINOR, LSMASH_VERSION_MICRO);
#endif
@@ -246,11 +246,11 @@
}
-static void showHelp(x265_param *param)
+static void showHelp(x265_param *param, const x265_api* api)
{
int level = param->logLevel;
- x265_param_default(param);
- printVersion(param);
+ api->param_default(param);
+ printVersion(param, api);
#define OPT(value) (value ? "enabled" : "disabled")
#define H0 printf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment