| 1 |
#include "avm_cpuinfo.h" |
| 2 |
#include "avm_except.h" // catch only |
| 3 |
#include "avm_creators.h" |
| 4 |
#include "videodecoder.h" |
| 5 |
#include "videoencoder.h" |
| 6 |
#include "audiodecoder.h" |
| 7 |
#include "audioencoder.h" |
| 8 |
#include "plugin.h" |
| 9 |
#include "configfile.h" |
| 10 |
#include "utils.h" |
| 11 |
#include "avm_output.h" |
| 12 |
#include "aviplay.h" // GetAvifileVersion |
| 13 |
#include "version.h" |
| 14 |
|
| 15 |
#include "../plugins/libac3pass/fillplugins.h" |
| 16 |
#include "../plugins/libaudiodec/fillplugins.h" |
| 17 |
#include "../plugins/libdivx4/fillplugins.h" |
| 18 |
#include "../plugins/libffmpeg/fillplugins.h" |
| 19 |
#include "../plugins/libmad/fillplugins.h" |
| 20 |
#include "../plugins/libmp3lame_audioenc/fillplugins.h" |
| 21 |
#include "../plugins/libmp3lamebin_audioenc/fillplugins.h" |
| 22 |
#include "../plugins/libmpeg_audiodec/fillplugins.h" |
| 23 |
#include "../plugins/libvorbis/fillplugins.h" |
| 24 |
#include "../plugins/libwin32/fillplugins.h" |
| 25 |
#include "../plugins/libxvid/fillplugins.h" |
| 26 |
#include "../plugins/libxvid4/fillplugins.h" |
| 27 |
#include "../samples/mjpeg_plugin/fillplugins.h" |
| 28 |
#include "Uncompressed.h" |
| 29 |
|
| 30 |
#include <string.h> |
| 31 |
#include <dirent.h> |
| 32 |
#include <dlfcn.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <stdio.h> |
| 35 |
|
| 36 |
#ifndef RTLD_PARENT |
| 37 |
#define RTLD_PARENT 0 |
| 38 |
#endif |
| 39 |
|
| 40 |
#if 1 |
| 41 |
// gdb catch point |
| 42 |
extern "C" void trapbug(void) |
| 43 |
{ |
| 44 |
static int a = 0; |
| 45 |
a++; |
| 46 |
} |
| 47 |
#endif |
| 48 |
|
| 49 |
avm::vector<avm::CodecInfo> video_codecs; |
| 50 |
avm::vector<avm::CodecInfo> audio_codecs; |
| 51 |
|
| 52 |
#define Debug if(0) |
| 53 |
static bool pluginit = false; |
| 54 |
|
| 55 |
const char* plugin_def_path = PLUGIN_PATH; |
| 56 |
static avm::string last_error; |
| 57 |
|
| 58 |
AVM_BEGIN_NAMESPACE; |
| 59 |
|
| 60 |
static avm::vector<CodecInfo*> video_order; |
| 61 |
static avm::vector<CodecInfo*> audio_order; |
| 62 |
|
| 63 |
#if ARCH_X86_32 |
| 64 |
/* |
| 65 |
* LDT_Keeper MUST be here |
| 66 |
* LDT must be modified before program creates first thread |
| 67 |
*/ |
| 68 |
|
| 69 |
// including c-file is slightly unusual, but |
| 70 |
// we prevent to have duplicated code |
| 71 |
#include "../plugins/libwin32/loader/ldt_keeper.c" |
| 72 |
|
| 73 |
struct LDT_Keeper |
| 74 |
{ |
| 75 |
ldt_fs_t* ldt_fs; |
| 76 |
|
| 77 |
LDT_Keeper() : ldt_fs(0) {} |
| 78 |
~LDT_Keeper() { if (ldt_fs) Restore_LDT_Keeper(ldt_fs); } |
| 79 |
// to be initialized only when Win32 plugin is available |
| 80 |
void init() |
| 81 |
{ |
| 82 |
if (!ldt_fs) |
| 83 |
{ |
| 84 |
ldt_fs = Setup_LDT_Keeper(); |
| 85 |
if (ldt_fs) |
| 86 |
AVM_WRITE("LDT keeper", "Installed fs segment: %p\n", |
| 87 |
ldt_fs->fs_seg); |
| 88 |
} |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
static LDT_Keeper Keeper; |
| 93 |
#endif |
| 94 |
|
| 95 |
static void plugin_close(const CodecInfo& ci) |
| 96 |
{ |
| 97 |
if (PluginPrivate* pi = (PluginPrivate*) ci.handle) |
| 98 |
{ |
| 99 |
//printf("PLUGINCLOSE %p c:%d\n", pi, (pi)?pi->refcount:12345); |
| 100 |
if (--pi->refcount <= 0 && pi->dlhandle) |
| 101 |
{ |
| 102 |
if (((codec_plugin_t*)pi->fchandle) |
| 103 |
&& ((codec_plugin_t*)pi->fchandle)->error) |
| 104 |
{ |
| 105 |
free(((codec_plugin_t*)pi->fchandle)->error); |
| 106 |
((codec_plugin_t*)pi->fchandle)->error = 0; |
| 107 |
} |
| 108 |
dlclose(pi->dlhandle); |
| 109 |
delete pi; |
| 110 |
ci.handle = 0; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
codec_plugin_t* plugin_open(const CodecInfo& ci) |
| 116 |
{ |
| 117 |
const char* name = ci.modulename.c_str(); |
| 118 |
PluginPrivate* pi = (PluginPrivate*) ci.handle; |
| 119 |
if (!pi) |
| 120 |
{ |
| 121 |
pi = new PluginPrivate(); |
| 122 |
pi->dlhandle = dlopen(name, RTLD_LAZY | RTLD_PARENT); |
| 123 |
if (!pi->dlhandle) |
| 124 |
{ |
| 125 |
AVM_WRITE("codec keeper", |
| 126 |
"WARNING: plugin %s could not be opened: %s\n", |
| 127 |
name, dlerror()); |
| 128 |
delete pi; |
| 129 |
return 0; |
| 130 |
} |
| 131 |
pi->fchandle = 0; |
| 132 |
pi->refcount = 0; |
| 133 |
ci.handle = pi; |
| 134 |
} |
| 135 |
|
| 136 |
pi->refcount++; |
| 137 |
if (!pi->fchandle) |
| 138 |
{ |
| 139 |
char plgn[100]; |
| 140 |
strcpy(plgn, "avm_codec_plugin_"); |
| 141 |
const char* nx = strrchr(name, '/'); |
| 142 |
strncat(plgn, nx + 1, 50); |
| 143 |
char* x = strchr(plgn, '.'); |
| 144 |
*x = 0; |
| 145 |
codec_plugin_t* plugin = (codec_plugin_t*) dlsym(pi->dlhandle, plgn); |
| 146 |
|
| 147 |
if (!plugin || (plugin->version != PLUGIN_API_VERSION)) |
| 148 |
{ |
| 149 |
if (plugin) { |
| 150 |
AVM_WRITE("codec keeper", |
| 151 |
"WARNING: plugin %s has version %d, expected %d (should be removed)\n", |
| 152 |
name, plugin->version, PLUGIN_API_VERSION); |
| 153 |
} else { |
| 154 |
AVM_WRITE("codec keeper", |
| 155 |
"WARNING: plugin %s is in incompatible format\n", |
| 156 |
name); |
| 157 |
} |
| 158 |
plugin_close(ci); |
| 159 |
return 0; |
| 160 |
} |
| 161 |
pi->fchandle = plugin; |
| 162 |
} |
| 163 |
return (codec_plugin_t*) pi->fchandle; |
| 164 |
} |
| 165 |
|
| 166 |
static void plugin_add_list(avm::vector<CodecInfo>& ci, |
| 167 |
const avm::string& fullname) |
| 168 |
{ |
| 169 |
size_t vp = 0; |
| 170 |
for (size_t i = 0; i < ci.size(); i++) |
| 171 |
{ |
| 172 |
ci[i].modulename = fullname; |
| 173 |
if (ci[i].media == CodecInfo::Video) |
| 174 |
{ |
| 175 |
vp++; |
| 176 |
video_codecs.push_back(ci[i]); |
| 177 |
} |
| 178 |
else |
| 179 |
audio_codecs.push_back(ci[i]); |
| 180 |
} |
| 181 |
AVM_WRITE("codec keeper", 1, "%s: A/V %" PRIsz "/%" PRIsz "\n", |
| 182 |
fullname.c_str(), ci.size() - vp, vp); |
| 183 |
} |
| 184 |
|
| 185 |
static void plugin_get_error(codec_plugin_t* plugin) |
| 186 |
{ |
| 187 |
if (plugin && plugin->error) |
| 188 |
AVM_WRITE("codec keeper", "%s\n", plugin->error); |
| 189 |
} |
| 190 |
|
| 191 |
// we have to avoid prefixed positive matches |
| 192 |
// i.e. ffmpeg_divx will not match ffmpeg |
| 193 |
static const char* sort_str(const char* haystack, const char* needle) |
| 194 |
{ |
| 195 |
const char* r = haystack; |
| 196 |
size_t nl = strlen(needle); |
| 197 |
|
| 198 |
if (!strlen(haystack) || !nl) |
| 199 |
return 0; |
| 200 |
//printf("HAY %s %s\n", haystack, needle); |
| 201 |
while ((r = strstr(r, needle)) != NULL) |
| 202 |
{ |
| 203 |
//printf("r %50s %s %p\n", r, haystack, needle); |
| 204 |
if (r == haystack || r[-1] == ',') |
| 205 |
{ |
| 206 |
r += nl; |
| 207 |
if (!*r || *r == ',') |
| 208 |
return r; |
| 209 |
} |
| 210 |
r++; // next char (also skips ',') |
| 211 |
} |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
static int sort_codec_list(avm::vector<CodecInfo*>& codecs, |
| 216 |
const char* orderlist) |
| 217 |
{ |
| 218 |
// simple stupid bubble sort - this code is not supposed |
| 219 |
// to be executed very often... |
| 220 |
// |
| 221 |
// only CodecInfos' found in the list will be swaped |
| 222 |
// rest of them will stay in the unspecified order |
| 223 |
|
| 224 |
///return 0; // disabled for now |
| 225 |
|
| 226 |
if (!orderlist) |
| 227 |
return 0; |
| 228 |
|
| 229 |
// always be sure list contains all codecs |
| 230 |
// so repeat all of them after passed parameter |
| 231 |
avm::string tl = orderlist; |
| 232 |
for (size_t i = 0; i < codecs.size(); ++i) |
| 233 |
{ |
| 234 |
const char* p = sort_str(tl.c_str(), codecs[i]->GetPrivateName()); |
| 235 |
if (!p) |
| 236 |
p = sort_str(tl.c_str(), codecs[i]->GetName()); |
| 237 |
if (!p) |
| 238 |
{ |
| 239 |
tl += ","; |
| 240 |
tl += codecs[i]->GetPrivateName(); |
| 241 |
//printf("MISSING %s\n", codecs[i]->GetPrivateName()); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
orderlist = tl.c_str(); |
| 246 |
int changed = 0; |
| 247 |
size_t len = strlen(orderlist); |
| 248 |
//printf("Sort for list of %d codecs with: %s \n", codecs.size(), orderlist); |
| 249 |
for (size_t i = 0; i < codecs.size(); ++i) |
| 250 |
{ |
| 251 |
//printf("Order %d: %s (%s)\n", i, codecs[i]->GetPrivateName(), codecs[i]->GetName()); |
| 252 |
const char* p = sort_str(orderlist, codecs[i]->GetPrivateName()); |
| 253 |
if (!p) // tagged to the first position |
| 254 |
p = sort_str(orderlist, codecs[i]->GetName()); |
| 255 |
if (!p) // tagged to the first position |
| 256 |
continue;//orderlist + len; // last position |
| 257 |
size_t s = 0; |
| 258 |
for (size_t j = i + 1; j < codecs.size(); ++j) |
| 259 |
{ |
| 260 |
//cout << " Compare with " << codecs[j].name.c_str() << endl; |
| 261 |
const char* r = sort_str(orderlist, codecs[j]->GetPrivateName()); |
| 262 |
if (!r) |
| 263 |
r = sort_str(orderlist, codecs[j]->GetName()); |
| 264 |
//cout << " ptr: " << (void*) r << " p: " << (void*) p << endl; |
| 265 |
if (r && r < p) |
| 266 |
{ |
| 267 |
//printf("FOUND %.10s %.10s pn:%s\n", p, r, codecs[j]->GetPrivateName()); |
| 268 |
s = j; |
| 269 |
p = r; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if (s > 0) |
| 274 |
{ |
| 275 |
//printf("Swap %s <> %s\n", codecs[s]->GetPrivateName(), codecs[i]->GetPrivateName()); |
| 276 |
CodecInfo* t = codecs[s]; |
| 277 |
codecs[s] = codecs[i]; |
| 278 |
codecs[i] = t; |
| 279 |
changed++; |
| 280 |
} |
| 281 |
} |
| 282 |
//for (unsigned i = 0; i < codecs.size(); i++) |
| 283 |
// printf("Sorted %d: %s (%s)\n", i, codecs[i]->GetPrivateName(), codecs[i]->GetName()); |
| 284 |
return changed; |
| 285 |
} |
| 286 |
|
| 287 |
static void plugin_fill() |
| 288 |
{ |
| 289 |
if (pluginit) |
| 290 |
return; |
| 291 |
|
| 292 |
pluginit = true; |
| 293 |
video_codecs.clear(); |
| 294 |
audio_codecs.clear(); |
| 295 |
|
| 296 |
// FFMPEG initialization |
| 297 |
avcodec_init(); |
| 298 |
avcodec_register_all(); |
| 299 |
|
| 300 |
uncompressed_FillPlugins(video_codecs); |
| 301 |
|
| 302 |
if (getenv("AVIPLUGIN_PATH")) |
| 303 |
plugin_def_path = getenv("AVIPLUGIN_PATH"); |
| 304 |
|
| 305 |
struct dirent *dp; |
| 306 |
int disc = 0; |
| 307 |
// for linux prefer memory leakless opendir |
| 308 |
#if defined(HAVE_OPENDIR) |
| 309 |
DIR *dir = opendir(plugin_def_path); |
| 310 |
if (dir != NULL) while ((dp = readdir(dir)) != NULL) |
| 311 |
{ |
| 312 |
#elif defined(HAVE_SCANDIR) |
| 313 |
struct dirent **namelist = 0; |
| 314 |
int n; |
| 315 |
n = scandir(plugin_def_path, &namelist, 0, alphasort); |
| 316 |
if(n>0) |
| 317 |
while(n--) |
| 318 |
{ |
| 319 |
dp = namelist[n]; |
| 320 |
#else |
| 321 |
#error No 'opendir' nor 'scandir' present - needs to be resolved... |
| 322 |
#endif // HAVE_SCANDIR |
| 323 |
char* name=dp->d_name; |
| 324 |
if (strlen(name)<4) |
| 325 |
continue; |
| 326 |
// find just .so named libraries/plugins |
| 327 |
if (strcmp(name+strlen(name)-3, ".so")) |
| 328 |
continue; |
| 329 |
avm::string dllname = name; |
| 330 |
avm::string fullname = plugin_def_path; |
| 331 |
fullname += "/"; |
| 332 |
fullname += dllname; |
| 333 |
|
| 334 |
const struct plugin_map { |
| 335 |
const char* name; |
| 336 |
void (*fill)(avm::vector<CodecInfo>& ci); |
| 337 |
} pm[] = { |
| 338 |
{ "ac3pass", ac3pass_FillPlugins }, |
| 339 |
{ "audiodec", audiodec_FillPlugins }, |
| 340 |
{ "divx4", divx4_FillPlugins }, |
| 341 |
{ "ffmpeg", ffmpeg_FillPlugins }, |
| 342 |
{ "mad_audiodec", mad_FillPlugins }, |
| 343 |
{ "mp3lame_audioenc", mp3lame_FillPlugins }, |
| 344 |
{ "mp3lamebin_audioenc", mp3lamebin_FillPlugins }, |
| 345 |
{ "mpeg_audiodec", mpeg_audiodec_FillPlugins }, |
| 346 |
{ "osmjpeg", ijpg_FillPlugins }, |
| 347 |
{ "vorbis", vorbis_FillPlugins }, |
| 348 |
{ "win32", win32_FillPlugins }, |
| 349 |
{ "xvid4", xvid4_FillPlugins }, // MUST BE before standalone xvid! |
| 350 |
{ "xvid", xvid_FillPlugins }, |
| 351 |
{ 0 } |
| 352 |
}; |
| 353 |
|
| 354 |
/** |
| 355 |
* This chunk of code eliminates the need to open several shared |
| 356 |
* libraries while starting every application |
| 357 |
*/ |
| 358 |
const plugin_map* c; |
| 359 |
for (c = pm; c->name; c++) |
| 360 |
{ |
| 361 |
if (dllname.substr(0, (uint_t)strlen(c->name)) == c->name) |
| 362 |
{ |
| 363 |
avm::vector<CodecInfo> ci; |
| 364 |
c->fill(ci); |
| 365 |
plugin_add_list(ci, fullname); |
| 366 |
#if ARCH_X86_32 |
| 367 |
if (strcmp(c->name, "win32") == 0) |
| 368 |
Keeper.init(); |
| 369 |
#endif |
| 370 |
break; |
| 371 |
} |
| 372 |
} |
| 373 |
disc++; |
| 374 |
if (c->name) |
| 375 |
continue; // found in the table above |
| 376 |
|
| 377 |
//cout << "Loading " << fullname.c_str() << endl; |
| 378 |
// third party plugin |
| 379 |
#if 0 |
| 380 |
// FIXME |
| 381 |
codec_plugin_t* plugin = plugin_open(fullname.c_str()); |
| 382 |
if (plugin && plugin->register_codecs) |
| 383 |
{ |
| 384 |
avm::vector<CodecInfo> ci; |
| 385 |
plugin->register_codecs(ci); |
| 386 |
PluginAddList(ci, fullname); |
| 387 |
} |
| 388 |
|
| 389 |
plugin_close(dlclose(handle); |
| 390 |
#endif |
| 391 |
#if defined(HAVE_SCANDIR) && !defined(HAVE_OPENDIR) |
| 392 |
free(dp); |
| 393 |
#endif |
| 394 |
} |
| 395 |
|
| 396 |
#if defined(HAVE_OPENDIR) |
| 397 |
if (dir) |
| 398 |
closedir(dir); |
| 399 |
#elif defined(HAVE_SCANDIR) |
| 400 |
if (namelist) |
| 401 |
free(namelist); |
| 402 |
#endif |
| 403 |
|
| 404 |
AVM_WRITE("codec keeper", "Found %d plugins (%s,A:%" PRIsz ",V:%" PRIsz ")\n", |
| 405 |
disc, plugin_def_path, audio_codecs.size(), video_codecs.size()); |
| 406 |
// hack |
| 407 |
avm::string sol; |
| 408 |
|
| 409 |
// move just numbers... |
| 410 |
video_order.resize(video_codecs.size()); |
| 411 |
for (size_t v = 0; v < video_order.size(); ++v) |
| 412 |
video_order[v] = &video_codecs[v]; |
| 413 |
audio_order.resize(audio_codecs.size()); |
| 414 |
for (size_t a = 0; a < audio_order.size(); ++a) |
| 415 |
audio_order[a] = &audio_codecs[a]; |
| 416 |
|
| 417 |
// use by default - but each codec might use it's own order |
| 418 |
sol = RegReadString("aviplay", "codecs-video", "ff"); |
| 419 |
sort_codec_list(video_order, sol.c_str()); |
| 420 |
|
| 421 |
sol = RegReadString("aviplay", "codecs-audio", "ff"); |
| 422 |
sort_codec_list(audio_order, sol.c_str()); |
| 423 |
} |
| 424 |
|
| 425 |
const avm::string& CodecGetError() |
| 426 |
{ |
| 427 |
return last_error; |
| 428 |
} |
| 429 |
|
| 430 |
void CodecInfo::Get(avm::vector<const CodecInfo*>& infos, |
| 431 |
CodecInfo::Media media, |
| 432 |
Direction dir, fourcc_t fcc) |
| 433 |
{ |
| 434 |
plugin_fill(); |
| 435 |
const avm::vector<CodecInfo*>& v = (media == Video) |
| 436 |
? video_order : audio_order; |
| 437 |
|
| 438 |
//infos.clear(); // ??? unsure |
| 439 |
for (size_t i = 0; i < v.size(); ++i) |
| 440 |
{ |
| 441 |
if (v[i]->direction != Both |
| 442 |
&& dir != Both |
| 443 |
&& dir != v[i]->direction) |
| 444 |
continue; |
| 445 |
|
| 446 |
// check if fcc match is requested |
| 447 |
// for the encoding the first fcc must be matching |
| 448 |
if (fcc != ANY |
| 449 |
&& ((dir == Encode && fcc != v[i]->fourcc) |
| 450 |
|| v.invalid == v[i]->fourcc_array.find(fcc))) |
| 451 |
continue; |
| 452 |
|
| 453 |
infos.push_back(v[i]); |
| 454 |
} |
| 455 |
//printf("Selected %d infos\n", infos.size()); |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
IVideoDecoder* CreateDecoderVideo(const BITMAPINFOHEADER& bh, int depth, |
| 460 |
int flip, const char* privcname) |
| 461 |
{ |
| 462 |
plugin_fill(); |
| 463 |
if (bh.biCompression == 0xffffffff) |
| 464 |
return 0; |
| 465 |
|
| 466 |
avm::vector<CodecInfo*>::const_iterator it; |
| 467 |
for (it = video_order.begin(); it != video_order.end(); ++it) |
| 468 |
{ |
| 469 |
const CodecInfo& ci = *(*it); |
| 470 |
if (!(ci.direction & CodecInfo::Decode) |
| 471 |
|| (privcname && strcmp(ci.GetPrivateName(), privcname) != 0)) |
| 472 |
continue; |
| 473 |
avm::vector<fourcc_t>::const_iterator iv; |
| 474 |
for (iv = ci.fourcc_array.begin(); iv != ci.fourcc_array.end(); ++iv) |
| 475 |
{ |
| 476 |
if (bh.biCompression == *iv) |
| 477 |
{ |
| 478 |
codec_plugin_t* plugin; |
| 479 |
|
| 480 |
switch (ci.kind) |
| 481 |
{ |
| 482 |
case CodecInfo::Source: |
| 483 |
return new Unc_Decoder(ci, bh, flip); |
| 484 |
default: |
| 485 |
plugin = plugin_open(ci); |
| 486 |
//AVM_WRITE("CodecKeeper", "Modname %s %p\n", ci.modulename.c_str(), plugin); |
| 487 |
if (plugin && plugin->video_decoder) |
| 488 |
{ |
| 489 |
IVideoDecoder* vd = plugin->video_decoder(ci, bh, flip); |
| 490 |
if (!vd && (fourcc_t)bh.biCompression != ci.fourcc) |
| 491 |
{ |
| 492 |
// try again with default codec for this |
| 493 |
// fourcc array |
| 494 |
AVM_WRITE("codec keeper", |
| 495 |
"Trying to use %.4s instead of %.4s\n", |
| 496 |
(const char*)&ci.fourcc, |
| 497 |
(const char*)&bh.biCompression); |
| 498 |
BITMAPINFOHEADER dbh(bh); |
| 499 |
dbh.biCompression = ci.fourcc; |
| 500 |
vd = plugin->video_decoder(ci, dbh, flip); |
| 501 |
} |
| 502 |
|
| 503 |
if (vd) |
| 504 |
{ |
| 505 |
//it->handle = handle; |
| 506 |
AVM_WRITE("codec keeper", |
| 507 |
"Created video decoder: %s\n", |
| 508 |
ci.GetName()); |
| 509 |
return vd; |
| 510 |
} |
| 511 |
plugin_get_error(plugin); |
| 512 |
} |
| 513 |
plugin_close(ci); |
| 514 |
}//switch |
| 515 |
}//if |
| 516 |
}//for |
| 517 |
}//for |
| 518 |
|
| 519 |
char s[5]; |
| 520 |
last_error.sprintf("Unknown codec 0x%x = \"%.4s\"", bh.biCompression, |
| 521 |
avm_set_le32(s, bh.biCompression)); |
| 522 |
AVM_WRITE("codec keeper", "CreateVideoDecoder(): %s\n", last_error.c_str()); |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
IVideoEncoder* CreateEncoderVideo(const CodecInfo& ci, |
| 527 |
const BITMAPINFOHEADER& bh) |
| 528 |
{ |
| 529 |
if (!(ci.direction & CodecInfo::Encode)) |
| 530 |
return 0; |
| 531 |
if (video_codecs.find(ci) == avm::vector<CodecInfo>::invalid) |
| 532 |
{ |
| 533 |
AVM_WRITE("codec keeper", "Failed to find this CodecInfo in list\n"); |
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
codec_plugin_t* plugin; |
| 538 |
switch(ci.kind) |
| 539 |
{ |
| 540 |
case CodecInfo::Source: |
| 541 |
try { return new Unc_Encoder(ci, bh.biCompression, bh); |
| 542 |
} catch (FatalError& e) { e.PrintAll(); } |
| 543 |
break; |
| 544 |
default: |
| 545 |
plugin = plugin_open(ci); |
| 546 |
if (plugin && plugin->video_encoder) |
| 547 |
{ |
| 548 |
IVideoEncoder* ve = plugin->video_encoder(ci, ci.fourcc, bh); |
| 549 |
if (ve) |
| 550 |
{ |
| 551 |
// video_codecs[index] is same as ci, the only difference is in permissions |
| 552 |
//video_codecs[index].handle = handle; |
| 553 |
return ve; |
| 554 |
} |
| 555 |
plugin_get_error(plugin); |
| 556 |
} |
| 557 |
plugin_close(ci); |
| 558 |
} |
| 559 |
return 0; |
| 560 |
} |
| 561 |
|
| 562 |
IVideoEncoder* CreateEncoderVideo(fourcc_t compressor, |
| 563 |
const BITMAPINFOHEADER& bh, const char* cname) |
| 564 |
{ |
| 565 |
plugin_fill(); |
| 566 |
|
| 567 |
avm::vector<CodecInfo*>::const_iterator it; |
| 568 |
for (it = video_order.begin(); it != video_order.end(); ++it) |
| 569 |
{ |
| 570 |
const CodecInfo& ci = *(*it); |
| 571 |
if (!(ci.direction & CodecInfo::Encode) |
| 572 |
|| (cname && strcmp(ci.GetName(), cname) != 0)) |
| 573 |
continue; |
| 574 |
//AVM_WRITE("codec keeper", 0, "Check dir:%d 0x%x (%.4s) cname:(%s)\n", ci.direction, |
| 575 |
// compressor, (const char*) &compressor, cname); |
| 576 |
if ((cname && cname[0] && !compressor) || ci.fourcc == compressor) |
| 577 |
if (IVideoEncoder* ve = CreateEncoderVideo(ci, bh)) |
| 578 |
{ |
| 579 |
AVM_WRITE("codec keeper", "%s video encoder created\n", |
| 580 |
ci.GetName()); |
| 581 |
return ve; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
char s[5]; |
| 586 |
last_error.sprintf("Unknown codec 0x%x = \"%.4s\"", compressor, |
| 587 |
avm_set_le32(s, compressor)); |
| 588 |
AVM_WRITE("codec keeper", "CreateVideoEncoder(): %s\n", last_error.c_str()); |
| 589 |
return 0; |
| 590 |
} |
| 591 |
|
| 592 |
IVideoEncoder* CreateEncoderVideo(const VideoEncoderInfo& info) |
| 593 |
{ |
| 594 |
const char* cname = info.cname.size() ? info.cname.c_str() : 0; |
| 595 |
return CreateEncoderVideo(info.compressor, info.header, cname); |
| 596 |
} |
| 597 |
|
| 598 |
IAudioEncoder* CreateEncoderAudio(const CodecInfo& ci, const WAVEFORMATEX* fmt) |
| 599 |
{ |
| 600 |
if (!(ci.direction & CodecInfo::Encode)) |
| 601 |
return 0; |
| 602 |
if (audio_codecs.find(ci) == avm::vector<CodecInfo>::invalid) |
| 603 |
{ |
| 604 |
AVM_WRITE("codec keeper", "Failed to find this CodecInfo in list\n"); |
| 605 |
return 0; |
| 606 |
} |
| 607 |
|
| 608 |
codec_plugin_t* plugin = plugin_open(ci); |
| 609 |
if (plugin && plugin->audio_encoder) |
| 610 |
{ |
| 611 |
if (IAudioEncoder* ae = plugin->audio_encoder(ci, ci.fourcc, fmt)) |
| 612 |
return ae; |
| 613 |
plugin_get_error(plugin); |
| 614 |
} |
| 615 |
plugin_close(ci); |
| 616 |
|
| 617 |
return 0; |
| 618 |
} |
| 619 |
|
| 620 |
IAudioEncoder* CreateEncoderAudio(fourcc_t compressor, |
| 621 |
const WAVEFORMATEX* format) |
| 622 |
{ |
| 623 |
plugin_fill(); |
| 624 |
|
| 625 |
avm::vector<CodecInfo*>::const_iterator it; |
| 626 |
for (it = audio_order.begin(); it != audio_order.end(); ++it) |
| 627 |
{ |
| 628 |
const CodecInfo& ci = *(*it); |
| 629 |
if ((ci.direction & CodecInfo::Encode) |
| 630 |
&& ci.fourcc_array.find(compressor) != avm::vector<fourcc_t>::invalid) |
| 631 |
if (IAudioEncoder* ae = CreateEncoderAudio(ci, format)) |
| 632 |
return ae; |
| 633 |
} |
| 634 |
last_error.sprintf("No audio decoder for ID 0x%x", format->wFormatTag); |
| 635 |
return 0; |
| 636 |
} |
| 637 |
|
| 638 |
IAudioDecoder* CreateDecoderAudio(const WAVEFORMATEX* format, |
| 639 |
const char* privcname) |
| 640 |
{ |
| 641 |
plugin_fill(); |
| 642 |
|
| 643 |
avm::vector<CodecInfo*>::const_iterator it; |
| 644 |
for (it = audio_order.begin(); it != audio_order.end(); ++it) |
| 645 |
{ |
| 646 |
const CodecInfo& ci = *(*it); |
| 647 |
if (!(ci.direction & CodecInfo::Decode) |
| 648 |
|| (privcname && strcmp(ci.GetPrivateName(), privcname) != 0)) |
| 649 |
continue; |
| 650 |
|
| 651 |
avm::vector<fourcc_t>::const_iterator iv; |
| 652 |
for (iv = ci.fourcc_array.begin(); iv != ci.fourcc_array.end(); ++iv) |
| 653 |
{ |
| 654 |
//printf("Audio search %x for %x\n", format->wFormatTag, *iv); |
| 655 |
if (unsigned(format->wFormatTag) == *iv) |
| 656 |
{ |
| 657 |
codec_plugin_t* plugin; |
| 658 |
switch(ci.kind) |
| 659 |
{ |
| 660 |
default: |
| 661 |
if (format->wFormatTag == WAVE_FORMAT_EXTENSIBLE) |
| 662 |
{ |
| 663 |
// check GUID |
| 664 |
const WAVEFORMATEXTENSIBLE& wfe = *(const WAVEFORMATEXTENSIBLE*)format; |
| 665 |
if (memcmp(&wfe.SubFormat, &ci.guid, sizeof(GUID)) != 0) |
| 666 |
continue; |
| 667 |
} |
| 668 |
plugin = plugin_open(ci); |
| 669 |
if (plugin && plugin->audio_decoder) |
| 670 |
{ |
| 671 |
IAudioDecoder* ad = plugin->audio_decoder(ci, format); |
| 672 |
if (ad) |
| 673 |
{ |
| 674 |
//ci.handle = handle; |
| 675 |
AVM_WRITE("codec keeper", |
| 676 |
"%s audio decoder created\n", |
| 677 |
ci.GetName()); |
| 678 |
return ad; |
| 679 |
} |
| 680 |
|
| 681 |
plugin_get_error(plugin); |
| 682 |
} |
| 683 |
plugin_close(ci); |
| 684 |
continue; |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
last_error.sprintf("No audio decoder for ID 0x%x", format->wFormatTag); |
| 691 |
return 0; |
| 692 |
} |
| 693 |
|
| 694 |
int CodecGetAttr(const CodecInfo& info, const char* attribute, float* value) |
| 695 |
{ |
| 696 |
int result = -1; |
| 697 |
codec_plugin_t* plugin = plugin_open(info); |
| 698 |
|
| 699 |
if (plugin && plugin->get_attr_float) |
| 700 |
result = plugin->get_attr_float(info, attribute, value); |
| 701 |
plugin_close(info); |
| 702 |
|
| 703 |
return result; |
| 704 |
} |
| 705 |
|
| 706 |
int CodecSetAttr(const CodecInfo& info, const char* attribute, float value) |
| 707 |
{ |
| 708 |
int result = -1; |
| 709 |
codec_plugin_t* plugin = plugin_open(info); |
| 710 |
|
| 711 |
if (plugin && plugin->set_attr_float) |
| 712 |
result = plugin->set_attr_float(info, attribute, value); |
| 713 |
plugin_close(info); |
| 714 |
|
| 715 |
return result; |
| 716 |
} |
| 717 |
|
| 718 |
int CodecGetAttr(const CodecInfo& info, const char* attribute, int* value) |
| 719 |
{ |
| 720 |
int result = -1; |
| 721 |
codec_plugin_t* plugin = plugin_open(info); |
| 722 |
|
| 723 |
if (plugin && plugin->get_attr_int) |
| 724 |
result = plugin->get_attr_int(info, attribute, value); |
| 725 |
plugin_close(info); |
| 726 |
|
| 727 |
return result; |
| 728 |
} |
| 729 |
|
| 730 |
int CodecSetAttr(const CodecInfo& info, const char* attribute, int value) |
| 731 |
{ |
| 732 |
int result = -1; |
| 733 |
codec_plugin_t* plugin = plugin_open(info); |
| 734 |
|
| 735 |
if (plugin && plugin->set_attr_int) |
| 736 |
result = plugin->set_attr_int(info, attribute, value); |
| 737 |
plugin_close(info); |
| 738 |
|
| 739 |
return result; |
| 740 |
} |
| 741 |
|
| 742 |
int CodecGetAttr(const CodecInfo& info, const char* attribute, |
| 743 |
const char** value) |
| 744 |
{ |
| 745 |
int result = -1; |
| 746 |
codec_plugin_t* plugin = plugin_open(info); |
| 747 |
|
| 748 |
if (plugin && plugin->get_attr_string) |
| 749 |
result = plugin->get_attr_string(info, attribute, value); |
| 750 |
plugin_close(info); |
| 751 |
|
| 752 |
return result; |
| 753 |
} |
| 754 |
|
| 755 |
int CodecSetAttr(const CodecInfo& info, const char* attribute, |
| 756 |
const char* value) |
| 757 |
{ |
| 758 |
int result = -1; |
| 759 |
codec_plugin_t* plugin = plugin_open(info); |
| 760 |
if (plugin && plugin->set_attr_string) |
| 761 |
result = plugin->set_attr_string(info, attribute, value); |
| 762 |
plugin_close(info); |
| 763 |
return result; |
| 764 |
} |
| 765 |
|
| 766 |
void FreeDecoderAudio(IAudioDecoder* decoder) |
| 767 |
{ |
| 768 |
if (decoder) |
| 769 |
{ |
| 770 |
const CodecInfo& info = decoder->GetCodecInfo(); |
| 771 |
AVM_WRITE("codec keeper", 1, "FreeAudioDecoder() %s\n", info.GetName()); |
| 772 |
delete decoder; |
| 773 |
plugin_close(info); |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
void FreeEncoderAudio(IAudioEncoder* encoder) |
| 778 |
{ |
| 779 |
if (encoder) |
| 780 |
{ |
| 781 |
const CodecInfo& info = encoder->GetCodecInfo(); |
| 782 |
AVM_WRITE("codec keeper", 1, "FreeAudioEncoder() %s\n", info.GetName()); |
| 783 |
delete encoder; |
| 784 |
plugin_close(info); |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
void FreeDecoderVideo(IVideoDecoder* decoder) |
| 789 |
{ |
| 790 |
if (decoder) |
| 791 |
{ |
| 792 |
const CodecInfo& info = decoder->GetCodecInfo(); |
| 793 |
AVM_WRITE("codec keeper", 1, "FreeVideoDecoder() %s\n", info.GetName()); |
| 794 |
delete decoder; |
| 795 |
plugin_close(info); |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
void FreeEncoderVideo(IVideoEncoder* encoder) |
| 800 |
{ |
| 801 |
if (encoder) |
| 802 |
{ |
| 803 |
const CodecInfo& info = encoder->GetCodecInfo(); |
| 804 |
AVM_WRITE("codec keeper", 1, "FreeVideoEncoder() %s\n", info.GetName()); |
| 805 |
delete encoder; |
| 806 |
plugin_close(info); |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
int SortVideoCodecs(const char* orderlist) |
| 811 |
{ |
| 812 |
return sort_codec_list(video_order, orderlist); |
| 813 |
} |
| 814 |
|
| 815 |
int SortAudioCodecs(const char* orderlist) |
| 816 |
{ |
| 817 |
return sort_codec_list(audio_order, orderlist); |
| 818 |
} |
| 819 |
|
| 820 |
int PluginGetAttrFloat(const CodecInfo& info, const char* attribute, |
| 821 |
float* value) |
| 822 |
{ |
| 823 |
const AttributeInfo* attr = info.FindAttribute(attribute); |
| 824 |
const char* n = info.GetPrivateName(); |
| 825 |
|
| 826 |
if (attr && attr->GetKind() == AttributeInfo::Float) |
| 827 |
{ |
| 828 |
*value = RegReadFloat(n, attribute, attr->GetDefaultFloat()); |
| 829 |
return 0; |
| 830 |
} |
| 831 |
AVM_WRITE(n, "GetAttrFloat unsupported attribute or incorrect value %s = %f\n", attribute, *value);\ |
| 832 |
return -1; |
| 833 |
} |
| 834 |
|
| 835 |
int PluginSetAttrFloat(const CodecInfo& info, const char* attribute, float value) |
| 836 |
{ |
| 837 |
const AttributeInfo* attr = info.FindAttribute(attribute); |
| 838 |
const char* n = info.GetPrivateName(); |
| 839 |
|
| 840 |
if (attr && attr->GetKind() == AttributeInfo::Float |
| 841 |
&& attr->IsValid(value)) |
| 842 |
return RegWriteFloat(n, attribute, value); |
| 843 |
|
| 844 |
AVM_WRITE(n, "SetAttrFloat unsupported attribute or incorrect value %s = %f\n", attribute, value); |
| 845 |
return -1; |
| 846 |
} |
| 847 |
|
| 848 |
int PluginGetAttrInt(const CodecInfo& info, const char* attribute, int* value) |
| 849 |
{ |
| 850 |
const AttributeInfo* attr = info.FindAttribute(attribute); |
| 851 |
const char* n = info.GetPrivateName(); |
| 852 |
|
| 853 |
if (attr && (attr->GetKind() == AttributeInfo::Integer |
| 854 |
|| attr->GetKind() == AttributeInfo::Select)) |
| 855 |
{ |
| 856 |
*value = RegReadInt(n, attribute, attr->GetDefault()); |
| 857 |
return 0; |
| 858 |
} |
| 859 |
AVM_WRITE(n, "GetAttrInt unsupported attribute or incorrect value %s = %d\n", attribute, *value);\ |
| 860 |
return -1; |
| 861 |
} |
| 862 |
|
| 863 |
int PluginSetAttrInt(const CodecInfo& info, const char* attribute, int value) |
| 864 |
{ |
| 865 |
const AttributeInfo* attr = info.FindAttribute(attribute); |
| 866 |
const char* n = info.GetPrivateName(); |
| 867 |
|
| 868 |
if (attr && (attr->GetKind() == AttributeInfo::Integer |
| 869 |
|| attr->GetKind() == AttributeInfo::Select) |
| 870 |
&& attr->IsValid(value)) |
| 871 |
{ |
| 872 |
return RegWriteInt(n, attribute, value); |
| 873 |
} |
| 874 |
|
| 875 |
AVM_WRITE(n, "SetAttrInt unsupported attribute or incorrect value %s = %d\n", attribute, value); |
| 876 |
return -1; |
| 877 |
} |
| 878 |
|
| 879 |
int PluginGetAttrString(const CodecInfo& info, const char* attribute, |
| 880 |
const char** value) |
| 881 |
{ |
| 882 |
const AttributeInfo* attr = info.FindAttribute(attribute); |
| 883 |
const char* n = info.GetPrivateName(); |
| 884 |
|
| 885 |
if (value && attr && (attr->GetKind() == AttributeInfo::String)) |
| 886 |
{ |
| 887 |
*value = RegReadString(n, attribute, ""); |
| 888 |
return 0; |
| 889 |
} |
| 890 |
AVM_WRITE(n, "GetAttrStr unsupported attribute or incorrect value %s = %p\n", attribute, value);\ |
| 891 |
return -1; |
| 892 |
} |
| 893 |
|
| 894 |
int PluginSetAttrString(const CodecInfo& info, const char* attribute, |
| 895 |
const char* value) |
| 896 |
{ |
| 897 |
const AttributeInfo* attr = info.FindAttribute(attribute); |
| 898 |
const char* n = info.GetPrivateName(); |
| 899 |
|
| 900 |
if (value && attr && (attr->GetKind() == AttributeInfo::String)) |
| 901 |
return RegWriteString(n, attribute, value); |
| 902 |
|
| 903 |
AVM_WRITE(n, "SetAttrString unsupported attribute or incorrect value %s = %p\n", attribute, value); |
| 904 |
return -1; |
| 905 |
} |
| 906 |
|
| 907 |
const CodecInfo* CodecInfo::match(fourcc_t fcccodec, CodecInfo::Media cimedia, |
| 908 |
const CodecInfo* start, |
| 909 |
CodecInfo::Direction cdir) |
| 910 |
{ |
| 911 |
if (video_codecs.size() == 0 && audio_codecs.size() == 0) |
| 912 |
{ |
| 913 |
BITMAPINFOHEADER bi; |
| 914 |
bi.biCompression = 0xffffffff; |
| 915 |
// just to fill video_codecs list |
| 916 |
CreateDecoderVideo(bi, 0, 0); |
| 917 |
} |
| 918 |
|
| 919 |
const avm::vector<CodecInfo>& c = |
| 920 |
(cimedia == Video) ? video_codecs : audio_codecs; |
| 921 |
|
| 922 |
for (size_t i = 0; i < c.size(); ++i) |
| 923 |
{ |
| 924 |
if (start) |
| 925 |
{ |
| 926 |
if (&c[i] == start) |
| 927 |
start = 0; |
| 928 |
continue; |
| 929 |
} |
| 930 |
|
| 931 |
const CodecInfo& ci = c[i]; |
| 932 |
|
| 933 |
if (((int)ci.direction & (int)cdir) != (int)cdir) |
| 934 |
continue; |
| 935 |
|
| 936 |
//printf("CHECK DIR %d %x %x %s\n", ci.direction, fcccodec, ci.fourcc, ci.GetName()); |
| 937 |
if (cdir == Encode) |
| 938 |
{ |
| 939 |
// for encoding we want exact match - the first fourcc |
| 940 |
if (fcccodec == ci.fourcc) |
| 941 |
return &ci; |
| 942 |
} |
| 943 |
else |
| 944 |
{ |
| 945 |
for (size_t j = 0; j < ci.fourcc_array.size(); j++) |
| 946 |
{ |
| 947 |
//printf("SEARCH FOR %.4s (0x%x) %.4s (0x%x) %d:%d\n", (char*)&fcccodec, fcccodec, (char*)&ci.fourcc_array[j], ci.fourcc_array[j], i, j); |
| 948 |
if (fcccodec == ci.fourcc_array[j]) |
| 949 |
return &ci; |
| 950 |
} |
| 951 |
} |
| 952 |
} |
| 953 |
return 0; |
| 954 |
} |
| 955 |
|
| 956 |
const CodecInfo* CodecInfo::match(CodecInfo::Media cimedia, const char* pname) |
| 957 |
{ |
| 958 |
if (video_codecs.size() == 0 && audio_codecs.size() == 0) |
| 959 |
{ |
| 960 |
BITMAPINFOHEADER bi; |
| 961 |
bi.biCompression = 0xffffffff; |
| 962 |
// just to fill video_codecs list |
| 963 |
CreateDecoderVideo(bi, 0, 0); |
| 964 |
} |
| 965 |
|
| 966 |
const avm::vector<CodecInfo>& c = (cimedia == Video ) |
| 967 |
? video_codecs : audio_codecs; |
| 968 |
for (size_t i = 0; i < c.size(); ++i) |
| 969 |
{ |
| 970 |
const CodecInfo& ci = c[i]; |
| 971 |
|
| 972 |
if (strcmp(ci.GetPrivateName(), pname) == 0 |
| 973 |
|| strcmp(ci.GetName(), pname) == 0) |
| 974 |
return &ci; |
| 975 |
} |
| 976 |
|
| 977 |
return 0; |
| 978 |
} |
| 979 |
|
| 980 |
AVM_END_NAMESPACE; |
| 981 |
|
| 982 |
int GetAvifileVersion() |
| 983 |
{ |
| 984 |
return AVIFILE_VERSION; |
| 985 |
} |