| 1 |
#include "avm_cpuinfo.h" |
| 2 |
#include "avm_creators.h" |
| 3 |
#include "avm_output.h" |
| 4 |
#include "infotypes.h" |
| 5 |
#include "utils.h" |
| 6 |
#include "plugin.h" |
| 7 |
#include "version.h" |
| 8 |
#include <string.h> |
| 9 |
#include <stdlib.h> //atof |
| 10 |
#ifndef WIN32 |
| 11 |
#include <unistd.h> |
| 12 |
#include <sys/time.h> |
| 13 |
#else |
| 14 |
#include <windows.h> |
| 15 |
#endif |
| 16 |
#include <time.h> |
| 17 |
#ifdef HAVE_KSTAT |
| 18 |
#include <kstat.h> |
| 19 |
#endif |
| 20 |
#include <ctype.h> |
| 21 |
#include <stdio.h> |
| 22 |
|
| 23 |
|
| 24 |
#ifdef AVM_COMPATIBLE |
| 25 |
int64_t longcount_notsc(void) |
| 26 |
{ |
| 27 |
#ifndef WIN32 |
| 28 |
static const uint32_t limit= ~0U / 1000000; |
| 29 |
struct timeval tv; |
| 30 |
gettimeofday(&tv, 0); |
| 31 |
int64_t result; |
| 32 |
result=tv.tv_sec; |
| 33 |
result<<=32; |
| 34 |
result+=limit*tv.tv_usec; |
| 35 |
return result; |
| 36 |
#else |
| 37 |
return time(NULL); |
| 38 |
#endif |
| 39 |
} |
| 40 |
|
| 41 |
static uint_t localcount_notsc() |
| 42 |
{ |
| 43 |
#ifndef WIN32 |
| 44 |
static const uint32_t limit= ~0U / 1000000; |
| 45 |
struct timeval tv; |
| 46 |
gettimeofday(&tv, 0); |
| 47 |
return limit*(uint32_t)tv.tv_usec; |
| 48 |
#else |
| 49 |
return time(NULL); |
| 50 |
#endif |
| 51 |
} |
| 52 |
#endif |
| 53 |
|
| 54 |
AVM_BEGIN_NAMESPACE; |
| 55 |
|
| 56 |
BaseInfo::BaseInfo() |
| 57 |
{ |
| 58 |
} |
| 59 |
|
| 60 |
BaseInfo::BaseInfo(const char* info, const char* _about) |
| 61 |
: name(info), about(_about) |
| 62 |
{ |
| 63 |
} |
| 64 |
|
| 65 |
BaseInfo::~BaseInfo() |
| 66 |
{ |
| 67 |
} |
| 68 |
|
| 69 |
AttributeInfo::AttributeInfo(const char* n, const char* a, const char* const* o, int defitem) |
| 70 |
: BaseInfo(n, a), kind(Select), i_min(0), i_max(-1), i_default(defitem) |
| 71 |
{ |
| 72 |
while (*o) |
| 73 |
{ |
| 74 |
options.push_back(*o); |
| 75 |
o++; |
| 76 |
} |
| 77 |
i_max = (int)options.size(); |
| 78 |
} |
| 79 |
|
| 80 |
AttributeInfo::AttributeInfo() |
| 81 |
{ |
| 82 |
} |
| 83 |
|
| 84 |
AttributeInfo::AttributeInfo(const char* n, const char* a, Kind k, int minval, int maxval, int defval) |
| 85 |
: BaseInfo(n, (a) ? a : ""), kind(k), i_min(minval), i_max(maxval), i_default(defval) |
| 86 |
{ |
| 87 |
if (i_default == -1) |
| 88 |
i_default = (i_min + i_max) / 2; |
| 89 |
} |
| 90 |
|
| 91 |
AttributeInfo::AttributeInfo(const char* n, const char* a, float defval, float minval, float maxval) |
| 92 |
: BaseInfo(n, (a) ? a : ""), kind(Float), f_min(minval), f_max(maxval), f_default(defval) |
| 93 |
{ |
| 94 |
} |
| 95 |
|
| 96 |
AttributeInfo::~AttributeInfo() |
| 97 |
{ |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
bool AttributeInfo::IsAttr(const char* attribute) const |
| 102 |
{ |
| 103 |
return (strcmp(GetName(), attribute) == 0 |
| 104 |
|| strcmp(GetAbout(), attribute) == 0); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
CodecInfo::CodecInfo() :handle(0) //, codec(0) |
| 109 |
{ |
| 110 |
} |
| 111 |
|
| 112 |
CodecInfo::CodecInfo(const fourcc_t* array, const char* info, const char* path, const char* a, |
| 113 |
Kind _kind, const char* pn, Media _media, Direction _direction, const GUID* id, |
| 114 |
const avm::vector<AttributeInfo>& ei, |
| 115 |
const avm::vector<AttributeInfo>& di) |
| 116 |
: BaseInfo(info, a), fourcc(array[0]), privatename(pn), kind(_kind), |
| 117 |
media(_media), direction(_direction), dll(path), |
| 118 |
decoder_info(di), encoder_info(ei), |
| 119 |
handle(0)//, codec(0), |
| 120 |
{ |
| 121 |
if (id) |
| 122 |
guid = *id; |
| 123 |
|
| 124 |
if (!*array) // uncompressed codec |
| 125 |
{ |
| 126 |
fourcc_array.push_back(0); |
| 127 |
array++; |
| 128 |
} |
| 129 |
|
| 130 |
while (*array) |
| 131 |
{ |
| 132 |
fourcc_array.push_back(*array); |
| 133 |
array++; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
const GUID CodecInfo::FFMPEG_GUID = { FFMPEG, }; |
| 138 |
|
| 139 |
CodecInfo::CodecInfo(const CodecInfo& ci) :handle(0) |
| 140 |
{ |
| 141 |
operator=(ci); |
| 142 |
} |
| 143 |
|
| 144 |
CodecInfo::~CodecInfo() |
| 145 |
{ |
| 146 |
if (handle) |
| 147 |
{ |
| 148 |
//((PluginPrivate*)handle)->refcount--; |
| 149 |
//printf("HAND-\n"); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
const AttributeInfo* CodecInfo::FindAttribute(const char* attr, Direction dir) const |
| 154 |
{ |
| 155 |
if (attr) |
| 156 |
{ |
| 157 |
if (dir == Both || dir == Encode) |
| 158 |
{ |
| 159 |
avm::vector<AttributeInfo>::const_iterator it; |
| 160 |
for (it = encoder_info.begin(); it != encoder_info.end(); it++) |
| 161 |
if (it->IsAttr(attr)) |
| 162 |
return &(*it); |
| 163 |
} |
| 164 |
if (dir == Both || dir == Decode) |
| 165 |
{ |
| 166 |
avm::vector<AttributeInfo>::const_iterator it; |
| 167 |
for (it = decoder_info.begin(); it != decoder_info.end(); it++) |
| 168 |
if (it->IsAttr(attr)) |
| 169 |
return &(*it); |
| 170 |
} |
| 171 |
} |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
CodecInfo& CodecInfo::operator=(const CodecInfo& ci) |
| 176 |
{ |
| 177 |
name = ci.name; |
| 178 |
about = ci.about; |
| 179 |
handle = ci.handle; |
| 180 |
|
| 181 |
if (handle) |
| 182 |
{ |
| 183 |
//((PluginPrivate*)handle)->refcount++; |
| 184 |
//printf("HAND+\n"); |
| 185 |
} |
| 186 |
|
| 187 |
assert(handle == 0); |
| 188 |
#if 0 |
| 189 |
if (ci.handle) |
| 190 |
{ |
| 191 |
(plugin_internal*) handle = new plugin_internal; |
| 192 |
((plugin_internal*)handle)->dlhandle = ((plugin_internal*)ci.handle)->dlhandle; |
| 193 |
((plugin_internal*)handle)->plhandle = ((plugin_internal*)ci.handle)->plhandle; |
| 194 |
} |
| 195 |
else |
| 196 |
handle = 0; |
| 197 |
codec = ci.codec; |
| 198 |
#endif |
| 199 |
fourcc = ci.fourcc; |
| 200 |
fourcc_array = ci.fourcc_array; |
| 201 |
kind = ci.kind; |
| 202 |
media = ci.media; |
| 203 |
direction = ci.direction; |
| 204 |
decoder_info = ci.decoder_info; |
| 205 |
encoder_info = ci.encoder_info; |
| 206 |
modulename = ci.modulename; |
| 207 |
guid = ci.guid; |
| 208 |
dll = ci.dll; |
| 209 |
privatename = ci.privatename; |
| 210 |
return *this; |
| 211 |
} |
| 212 |
|
| 213 |
// CodecInfo::match is in codeckeeper.cpp - it's using Create functions |
| 214 |
|
| 215 |
IRtConfig::~IRtConfig() |
| 216 |
{ |
| 217 |
} |
| 218 |
|
| 219 |
#ifdef WIN32 |
| 220 |
#define rdtsc __asm _emit 0fh __asm _emit 031h |
| 221 |
#define cpuid __asm _emit 0fh __asm _emit 0a2h |
| 222 |
|
| 223 |
#define strncasecmp strnicmp |
| 224 |
#endif |
| 225 |
|
| 226 |
static void do_cpuid(uint_t *regs, int op) |
| 227 |
{ |
| 228 |
unsigned int ax, cx; |
| 229 |
*regs = 0; |
| 230 |
#if ARCH_X86_32 |
| 231 |
|
| 232 |
/* first check if cpuid is supported - ffmpeg */ |
| 233 |
__asm__ __volatile__ |
| 234 |
( |
| 235 |
/* See if CPUID instruction is supported ... */ |
| 236 |
/* ... Get copies of EFLAGS into eax and ecx */ |
| 237 |
"pushf\n\t" |
| 238 |
"popl %0\n\t" |
| 239 |
"movl %0, %1\n\t" |
| 240 |
|
| 241 |
/* ... Toggle the ID bit in one copy and store */ |
| 242 |
/* to the EFLAGS reg */ |
| 243 |
"xorl $0x200000, %0\n\t" |
| 244 |
"push %0\n\t" |
| 245 |
"popf\n\t" |
| 246 |
|
| 247 |
/* ... Get the (hopefully modified) EFLAGS */ |
| 248 |
"pushf\n\t" |
| 249 |
"popl %0\n\t" |
| 250 |
: "=a" (ax), "=c" (cx) |
| 251 |
: |
| 252 |
: "cc" |
| 253 |
); |
| 254 |
|
| 255 |
if (ax == cx) |
| 256 |
return; /* CPUID not supported */ |
| 257 |
|
| 258 |
ax = op; |
| 259 |
__asm__ __volatile__ |
| 260 |
( |
| 261 |
"pushl %%ebx; pushl %%ecx; pushl %%edx;" |
| 262 |
".byte 0x0f, 0xa2;" |
| 263 |
"movl %%eax, (%2);" |
| 264 |
"movl %%ebx, 4(%2);" |
| 265 |
"movl %%ecx, 8(%2);" |
| 266 |
"movl %%edx, 12(%2);" |
| 267 |
"popl %%edx; popl %%ecx; popl %%ebx;" |
| 268 |
: "=a" (ax) |
| 269 |
: "0" (ax), "S" (regs)); |
| 270 |
#endif |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
#if 0 |
| 275 |
static uint_t localcount_tsc() |
| 276 |
{ |
| 277 |
int a; |
| 278 |
#ifndef WIN32 |
| 279 |
#if ARCH_X86 |
| 280 |
__asm__ __volatile__("rdtsc\n\t" : "=a"(a) : : "edx"); |
| 281 |
#else |
| 282 |
a = 0; |
| 283 |
#endif |
| 284 |
#else |
| 285 |
__asm |
| 286 |
{ |
| 287 |
rdtsc |
| 288 |
mov a, eax |
| 289 |
} |
| 290 |
#endif |
| 291 |
return a; |
| 292 |
} |
| 293 |
|
| 294 |
static int64_t longcount_tsc() |
| 295 |
{ |
| 296 |
#ifndef WIN32 |
| 297 |
#if ARCH_X86 |
| 298 |
int64_t l; |
| 299 |
__asm__ __volatile__("rdtsc\n\t":"=A"(l)); |
| 300 |
return l; |
| 301 |
#else |
| 302 |
return 0; |
| 303 |
#endif |
| 304 |
#else |
| 305 |
unsigned long i; |
| 306 |
unsigned long j; |
| 307 |
__asm |
| 308 |
{ |
| 309 |
rdtsc |
| 310 |
mov i, eax |
| 311 |
mov j, edx |
| 312 |
} |
| 313 |
return ((int64_t)j<<32)+(int64_t)i; |
| 314 |
#endif |
| 315 |
} |
| 316 |
#endif |
| 317 |
|
| 318 |
static double old_freq() |
| 319 |
{ |
| 320 |
#if HAVE_KSTAT |
| 321 |
/* |
| 322 |
* try to extact the CPU speed from the Solaris kernel's kstat data |
| 323 |
*/ |
| 324 |
kstat_ctl_t *kc; |
| 325 |
kstat_t *ksp; |
| 326 |
kstat_named_t *kdata; |
| 327 |
int mhz = 0; |
| 328 |
|
| 329 |
kc = kstat_open(); |
| 330 |
if (kc != NULL) |
| 331 |
{ |
| 332 |
ksp = kstat_lookup(kc, "cpu_info", 0, "cpu_info0"); |
| 333 |
|
| 334 |
/* kstat found and name/value pairs? */ |
| 335 |
if (ksp != NULL && ksp->ks_type == KSTAT_TYPE_NAMED) |
| 336 |
{ |
| 337 |
/* read the kstat data from the kernel */ |
| 338 |
if (kstat_read(kc, ksp, NULL) != -1) |
| 339 |
{ |
| 340 |
/* |
| 341 |
* lookup desired "clock_MHz" entry, check the expected |
| 342 |
* data type |
| 343 |
*/ |
| 344 |
kdata = (kstat_named_t *)kstat_data_lookup(ksp, "clock_MHz"); |
| 345 |
if (kdata != NULL && kdata->data_type == KSTAT_DATA_INT32) |
| 346 |
mhz = kdata->value.i32; |
| 347 |
} |
| 348 |
} |
| 349 |
kstat_close(kc); |
| 350 |
} |
| 351 |
|
| 352 |
if (mhz > 0) |
| 353 |
return mhz * 1000.; |
| 354 |
#endif /* HAVE_KSTAT */ |
| 355 |
|
| 356 |
time_t i=time(NULL); |
| 357 |
int64_t x,y; |
| 358 |
while(i==time(NULL)); |
| 359 |
x=avm_get_time_us(); |
| 360 |
i++; |
| 361 |
while(i==time(NULL)); |
| 362 |
y=avm_get_time_us(); |
| 363 |
return (double)(y-x)/1000.; |
| 364 |
} |
| 365 |
|
| 366 |
CPU_Info::CPU_Info() |
| 367 |
{ |
| 368 |
AvmOutput::createAvmOutput(); |
| 369 |
} |
| 370 |
|
| 371 |
CPU_Info::~CPU_Info() |
| 372 |
{ |
| 373 |
delete AvmOutput(); |
| 374 |
} |
| 375 |
|
| 376 |
void CPU_Info::Init() |
| 377 |
|
| 378 |
{ |
| 379 |
char line[200]; |
| 380 |
char model[200]="unknown"; |
| 381 |
char flags[500]=""; |
| 382 |
char* s, *value; |
| 383 |
bool is_AMD=false; |
| 384 |
|
| 385 |
freq = -1; |
| 386 |
have_tsc = have_mmx = have_mmxext = have_sse = false; |
| 387 |
|
| 388 |
FILE *f = fopen("/proc/cpuinfo", "r"); |
| 389 |
if (!f) |
| 390 |
{ |
| 391 |
uint_t regs[4]; |
| 392 |
do_cpuid(regs, 0); |
| 393 |
is_AMD = ((regs[1] == 0x68747541) && |
| 394 |
(regs[2] == 0x444d4163) && |
| 395 |
(regs[3] == 0x69746e65)); |
| 396 |
|
| 397 |
do_cpuid(regs, 1); |
| 398 |
have_tsc=regs[3] & 0x00000010; |
| 399 |
have_mmx=regs[3] & 0x00800000; |
| 400 |
have_mmxext=regs[3] & 0x02000000; |
| 401 |
have_sse=regs[3] & 0x02000000; |
| 402 |
do_cpuid(regs, 0x80000000); |
| 403 |
if (regs[0] >= 0x80000001) |
| 404 |
{ |
| 405 |
do_cpuid(regs, 0x80000001); |
| 406 |
|
| 407 |
if(is_AMD && (regs[3] & 0x00400000)) |
| 408 |
have_mmxext=1; |
| 409 |
} |
| 410 |
|
| 411 |
freq = old_freq(); |
| 412 |
return; |
| 413 |
} |
| 414 |
|
| 415 |
while (fgets(line, 200, f) != NULL) |
| 416 |
{ |
| 417 |
/* NOTE: the ':' is the only character we can rely on */ |
| 418 |
value = strchr(line,':'); |
| 419 |
if (!value) |
| 420 |
continue; |
| 421 |
/* terminate the valuename */ |
| 422 |
*value++ = '\0'; |
| 423 |
/* skip any leading spaces */ |
| 424 |
while (*value == ' ') |
| 425 |
value++; |
| 426 |
|
| 427 |
s = strchr(value, '\n'); |
| 428 |
if (s) |
| 429 |
*s = '\0'; |
| 430 |
|
| 431 |
if (!strncasecmp(line, "cpu MHz",strlen("cpu MHz"))) |
| 432 |
{ |
| 433 |
freq = atof(value); |
| 434 |
freq *= 1000.; |
| 435 |
|
| 436 |
} |
| 437 |
if (!strncasecmp(line, "model name", strlen("model name"))) |
| 438 |
strncpy(model, value, sizeof(model)-1); |
| 439 |
if (!strncasecmp(line, "flags", strlen("flags")) || |
| 440 |
!strncasecmp(line, "features", strlen("features"))) |
| 441 |
strncpy(flags, value, sizeof(flags)-1); |
| 442 |
continue; |
| 443 |
} |
| 444 |
fclose(f); |
| 445 |
|
| 446 |
AVM_WRITE("init", 0, "Avifile %s\n", AVIFILE_BUILD); |
| 447 |
AVM_WRITE("init", 0, "Available CPU flags: %s\n", flags); |
| 448 |
have_tsc = (strstr(flags, "tsc") != 0); |
| 449 |
have_mmx = (strstr(flags, "mmx") != 0); |
| 450 |
have_sse = (strstr(flags, "sse") != 0); |
| 451 |
have_mmxext = (strstr(flags, "mmxext") != 0) | have_sse; |
| 452 |
|
| 453 |
if (freq < 0) |
| 454 |
freq = old_freq(); |
| 455 |
if (have_tsc) |
| 456 |
AVM_WRITE("init", 0, "%.2f MHz %s %sdetected\n", freq/1000., model, |
| 457 |
strstr(model, "rocessor") ? "" : "processor "); |
| 458 |
} |
| 459 |
|
| 460 |
AVM_END_NAMESPACE; |