Debian Codegraph @

lib/codeckeeper.cpp

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
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_register_all();
298
299 uncompressed_FillPlugins(video_codecs);
300
301 if (getenv("AVIPLUGIN_PATH"))
302 plugin_def_path = getenv("AVIPLUGIN_PATH");
303
304 struct dirent *dp;
305 int disc = 0;
306 // for linux prefer memory leakless opendir
307 #if defined(HAVE_OPENDIR)
308 DIR *dir = opendir(plugin_def_path);
309 if (dir != NULL) while ((dp = readdir(dir)) != NULL)
310 {
311 #elif defined(HAVE_SCANDIR)
312 struct dirent **namelist = 0;
313 int n;
314 n = scandir(plugin_def_path, &namelist, 0, alphasort);
315 if(n>0)
316 while(n--)
317 {
318 dp = namelist[n];
319 #else
320 #error No 'opendir' nor 'scandir' present - needs to be resolved...
321 #endif // HAVE_SCANDIR
322 char* name=dp->d_name;
323 if (strlen(name)<4)
324 continue;
325 // find just .so named libraries/plugins
326 if (strcmp(name+strlen(name)-3, ".so"))
327 continue;
328 avm::string dllname = name;
329 avm::string fullname = plugin_def_path;
330 fullname += "/";
331 fullname += dllname;
332
333 const struct plugin_map {
334 const char* name;
335 void (*fill)(avm::vector<CodecInfo>& ci);
336 } pm[] = {
337 { "ac3pass", ac3pass_FillPlugins },
338 { "audiodec", audiodec_FillPlugins },
339 { "divx4", divx4_FillPlugins },
340 { "ffmpeg", ffmpeg_FillPlugins },
341 { "mad_audiodec", mad_FillPlugins },
342 { "mp3lame_audioenc", mp3lame_FillPlugins },
343 { "mp3lamebin_audioenc", mp3lamebin_FillPlugins },
344 { "mpeg_audiodec", mpeg_audiodec_FillPlugins },
345 { "osmjpeg", ijpg_FillPlugins },
346 { "vorbis", vorbis_FillPlugins },
347 { "win32", win32_FillPlugins },
348 { "xvid4", xvid4_FillPlugins }, // MUST BE before standalone xvid!
349 { "xvid", xvid_FillPlugins },
350 { 0 }
351 };
352
353 /**
354 * This chunk of code eliminates the need to open several shared
355 * libraries while starting every application
356 */
357 const plugin_map* c;
358 for (c = pm; c->name; c++)
359 {
360 if (dllname.substr(0, (uint_t)strlen(c->name)) == c->name)
361 {
362 avm::vector<CodecInfo> ci;
363 c->fill(ci);
364 plugin_add_list(ci, fullname);
365 #if ARCH_X86_32
366 if (strcmp(c->name, "win32") == 0)
367 Keeper.init();
368 #endif
369 break;
370 }
371 }
372 disc++;
373 if (c->name)
374 continue; // found in the table above
375
376 //cout << "Loading " << fullname.c_str() << endl;
377 // third party plugin
378 #if 0
379 // FIXME
380 codec_plugin_t* plugin = plugin_open(fullname.c_str());
381 if (plugin && plugin->register_codecs)
382 {
383 avm::vector<CodecInfo> ci;
384 plugin->register_codecs(ci);
385 PluginAddList(ci, fullname);
386 }
387
388 plugin_close(dlclose(handle);
389 #endif
390 #if defined(HAVE_SCANDIR) && !defined(HAVE_OPENDIR)
391 free(dp);
392 #endif
393 }
394
395 #if defined(HAVE_OPENDIR)
396 if (dir)
397 closedir(dir);
398 #elif defined(HAVE_SCANDIR)
399 if (namelist)
400 free(namelist);
401 #endif
402
403 AVM_WRITE("codec keeper", "Found %d plugins (%s,A:%" PRIsz ",V:%" PRIsz ")\n",
404 disc, plugin_def_path, audio_codecs.size(), video_codecs.size());
405 // hack
406 avm::string sol;
407
408 // move just numbers...
409 video_order.resize(video_codecs.size());
410 for (size_t v = 0; v < video_order.size(); ++v)
411 video_order[v] = &video_codecs[v];
412 audio_order.resize(audio_codecs.size());
413 for (size_t a = 0; a < audio_order.size(); ++a)
414 audio_order[a] = &audio_codecs[a];
415
416 // use by default - but each codec might use it's own order
417 sol = RegReadString("aviplay", "codecs-video", "ff");
418 sort_codec_list(video_order, sol.c_str());
419
420 sol = RegReadString("aviplay", "codecs-audio", "ff");
421 sort_codec_list(audio_order, sol.c_str());
422 }
423
424 const avm::string& CodecGetError()
425 {
426 return last_error;
427 }
428
429 void CodecInfo::Get(avm::vector<const CodecInfo*>& infos,
430 CodecInfo::Media media,
431 Direction dir, fourcc_t fcc)
432 {
433 plugin_fill();
434 const avm::vector<CodecInfo*>& v = (media == Video)
435 ? video_order : audio_order;
436
437 //infos.clear(); // ??? unsure
438 for (size_t i = 0; i < v.size(); ++i)
439 {
440 if (v[i]->direction != Both
441 && dir != Both
442 && dir != v[i]->direction)
443 continue;
444
445 // check if fcc match is requested
446 // for the encoding the first fcc must be matching
447 if (fcc != ANY
448 && ((dir == Encode && fcc != v[i]->fourcc)
449 || v.invalid == v[i]->fourcc_array.find(fcc)))
450 continue;
451
452 infos.push_back(v[i]);
453 }
454 //printf("Selected %d infos\n", infos.size());
455 }
456
457
458 IVideoDecoder* CreateDecoderVideo(const BITMAPINFOHEADER& bh, int depth,
459 int flip, const char* privcname)
460 {
461 plugin_fill();
462 if (bh.biCompression == 0xffffffff)
463 return 0;
464
465 avm::vector<CodecInfo*>::const_iterator it;
466 for (it = video_order.begin(); it != video_order.end(); ++it)
467 {
468 const CodecInfo& ci = *(*it);
469 if (!(ci.direction & CodecInfo::Decode)
470 || (privcname && strcmp(ci.GetPrivateName(), privcname) != 0))
471 continue;
472 avm::vector<fourcc_t>::const_iterator iv;
473 for (iv = ci.fourcc_array.begin(); iv != ci.fourcc_array.end(); ++iv)
474 {
475 if (bh.biCompression == *iv)
476 {
477 codec_plugin_t* plugin;
478
479 switch (ci.kind)
480 {
481 case CodecInfo::Source:
482 return new Unc_Decoder(ci, bh, flip);
483 default:
484 plugin = plugin_open(ci);
485 //AVM_WRITE("CodecKeeper", "Modname %s %p\n", ci.modulename.c_str(), plugin);
486 if (plugin && plugin->video_decoder)
487 {
488 IVideoDecoder* vd = plugin->video_decoder(ci, bh, flip);
489 if (!vd && (fourcc_t)bh.biCompression != ci.fourcc)
490 {
491 // try again with default codec for this
492 // fourcc array
493 AVM_WRITE("codec keeper",
494 "Trying to use %.4s instead of %.4s\n",
495 (const char*)&ci.fourcc,
496 (const char*)&bh.biCompression);
497 BITMAPINFOHEADER dbh(bh);
498 dbh.biCompression = ci.fourcc;
499 vd = plugin->video_decoder(ci, dbh, flip);
500 }
501
502 if (vd)
503 {
504 //it->handle = handle;
505 AVM_WRITE("codec keeper",
506 "Created video decoder: %s\n",
507 ci.GetName());
508 return vd;
509 }
510 plugin_get_error(plugin);
511 }
512 plugin_close(ci);
513 }//switch
514 }//if
515 }//for
516 }//for
517
518 char s[5];
519 last_error.sprintf("Unknown codec 0x%x = \"%.4s\"", bh.biCompression,
520 avm_set_le32(s, bh.biCompression));
521 AVM_WRITE("codec keeper", "CreateVideoDecoder(): %s\n", last_error.c_str());
522 return 0;
523 }
524
525 IVideoEncoder* CreateEncoderVideo(const CodecInfo& ci,
526 const BITMAPINFOHEADER& bh)
527 {
528 if (!(ci.direction & CodecInfo::Encode))
529 return 0;
530 if (video_codecs.find(ci) == avm::vector<CodecInfo>::invalid)
531 {
532 AVM_WRITE("codec keeper", "Failed to find this CodecInfo in list\n");
533 return 0;
534 }
535
536 codec_plugin_t* plugin;
537 switch(ci.kind)
538 {
539 case CodecInfo::Source:
540 try { return new Unc_Encoder(ci, bh.biCompression, bh);
541 } catch (FatalError& e) { e.PrintAll(); }
542 break;
543 default:
544 plugin = plugin_open(ci);
545 if (plugin && plugin->video_encoder)
546 {
547 IVideoEncoder* ve = plugin->video_encoder(ci, ci.fourcc, bh);
548 if (ve)
549 {
550 // video_codecs[index] is same as ci, the only difference is in permissions
551 //video_codecs[index].handle = handle;
552 return ve;
553 }
554 plugin_get_error(plugin);
555 }
556 plugin_close(ci);
557 }
558 return 0;
559 }
560
561 IVideoEncoder* CreateEncoderVideo(fourcc_t compressor,
562 const BITMAPINFOHEADER& bh, const char* cname)
563 {
564 plugin_fill();
565
566 avm::vector<CodecInfo*>::const_iterator it;
567 for (it = video_order.begin(); it != video_order.end(); ++it)
568 {
569 const CodecInfo& ci = *(*it);
570 if (!(ci.direction & CodecInfo::Encode)
571 || (cname && strcmp(ci.GetName(), cname) != 0))
572 continue;
573 //AVM_WRITE("codec keeper", 0, "Check dir:%d 0x%x (%.4s) cname:(%s)\n", ci.direction,
574 // compressor, (const char*) &compressor, cname);
575 if ((cname && cname[0] && !compressor) || ci.fourcc == compressor)
576 if (IVideoEncoder* ve = CreateEncoderVideo(ci, bh))
577 {
578 AVM_WRITE("codec keeper", "%s video encoder created\n",
579 ci.GetName());
580 return ve;
581 }
582 }
583
584 char s[5];
585 last_error.sprintf("Unknown codec 0x%x = \"%.4s\"", compressor,
586 avm_set_le32(s, compressor));
587 AVM_WRITE("codec keeper", "CreateVideoEncoder(): %s\n", last_error.c_str());
588 return 0;
589 }
590
591 IVideoEncoder* CreateEncoderVideo(const VideoEncoderInfo& info)
592 {
593 const char* cname = info.cname.size() ? info.cname.c_str() : 0;
594 return CreateEncoderVideo(info.compressor, info.header, cname);
595 }
596
597 IAudioEncoder* CreateEncoderAudio(const CodecInfo& ci, const WAVEFORMATEX* fmt)
598 {
599 if (!(ci.direction & CodecInfo::Encode))
600 return 0;
601 if (audio_codecs.find(ci) == avm::vector<CodecInfo>::invalid)
602 {
603 AVM_WRITE("codec keeper", "Failed to find this CodecInfo in list\n");
604 return 0;
605 }
606
607 codec_plugin_t* plugin = plugin_open(ci);
608 if (plugin && plugin->audio_encoder)
609 {
610 if (IAudioEncoder* ae = plugin->audio_encoder(ci, ci.fourcc, fmt))
611 return ae;
612 plugin_get_error(plugin);
613 }
614 plugin_close(ci);
615
616 return 0;
617 }
618
619 IAudioEncoder* CreateEncoderAudio(fourcc_t compressor,
620 const WAVEFORMATEX* format)
621 {
622 plugin_fill();
623
624 avm::vector<CodecInfo*>::const_iterator it;
625 for (it = audio_order.begin(); it != audio_order.end(); ++it)
626 {
627 const CodecInfo& ci = *(*it);
628 if ((ci.direction & CodecInfo::Encode)
629 && ci.fourcc_array.find(compressor) != avm::vector<fourcc_t>::invalid)
630 if (IAudioEncoder* ae = CreateEncoderAudio(ci, format))
631 return ae;
632 }
633 last_error.sprintf("No audio decoder for ID 0x%x", format->wFormatTag);
634 return 0;
635 }
636
637 IAudioDecoder* CreateDecoderAudio(const WAVEFORMATEX* format,
638 const char* privcname)
639 {
640 plugin_fill();
641
642 avm::vector<CodecInfo*>::const_iterator it;
643 for (it = audio_order.begin(); it != audio_order.end(); ++it)
644 {
645 const CodecInfo& ci = *(*it);
646 if (!(ci.direction & CodecInfo::Decode)
647 || (privcname && strcmp(ci.GetPrivateName(), privcname) != 0))
648 continue;
649
650 avm::vector<fourcc_t>::const_iterator iv;
651 for (iv = ci.fourcc_array.begin(); iv != ci.fourcc_array.end(); ++iv)
652 {
653 //printf("Audio search %x for %x\n", format->wFormatTag, *iv);
654 if (unsigned(format->wFormatTag) == *iv)
655 {
656 codec_plugin_t* plugin;
657 switch(ci.kind)
658 {
659 default:
660 if (format->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
661 {
662 // check GUID
663 const WAVEFORMATEXTENSIBLE& wfe = *(const WAVEFORMATEXTENSIBLE*)format;
664 if (memcmp(&wfe.SubFormat, &ci.guid, sizeof(GUID)) != 0)
665 continue;
666 }
667 plugin = plugin_open(ci);
668 if (plugin && plugin->audio_decoder)
669 {
670 IAudioDecoder* ad = plugin->audio_decoder(ci, format);
671 if (ad)
672 {
673 //ci.handle = handle;
674 AVM_WRITE("codec keeper",
675 "%s audio decoder created\n",
676 ci.GetName());
677 return ad;
678 }
679
680 plugin_get_error(plugin);
681 }
682 plugin_close(ci);
683 continue;
684 }
685 }
686 }
687 }
688
689 last_error.sprintf("No audio decoder for ID 0x%x", format->wFormatTag);
690 return 0;
691 }
692
693 int CodecGetAttr(const CodecInfo& info, const char* attribute, float* value)
694 {
695 int result = -1;
696 codec_plugin_t* plugin = plugin_open(info);
697
698 if (plugin && plugin->get_attr_float)
699 result = plugin->get_attr_float(info, attribute, value);
700 plugin_close(info);
701
702 return result;
703 }
704
705 int CodecSetAttr(const CodecInfo& info, const char* attribute, float value)
706 {
707 int result = -1;
708 codec_plugin_t* plugin = plugin_open(info);
709
710 if (plugin && plugin->set_attr_float)
711 result = plugin->set_attr_float(info, attribute, value);
712 plugin_close(info);
713
714 return result;
715 }
716
717 int CodecGetAttr(const CodecInfo& info, const char* attribute, int* value)
718 {
719 int result = -1;
720 codec_plugin_t* plugin = plugin_open(info);
721
722 if (plugin && plugin->get_attr_int)
723 result = plugin->get_attr_int(info, attribute, value);
724 plugin_close(info);
725
726 return result;
727 }
728
729 int CodecSetAttr(const CodecInfo& info, const char* attribute, int value)
730 {
731 int result = -1;
732 codec_plugin_t* plugin = plugin_open(info);
733
734 if (plugin && plugin->set_attr_int)
735 result = plugin->set_attr_int(info, attribute, value);
736 plugin_close(info);
737
738 return result;
739 }
740
741 int CodecGetAttr(const CodecInfo& info, const char* attribute,
742 const char** value)
743 {
744 int result = -1;
745 codec_plugin_t* plugin = plugin_open(info);
746
747 if (plugin && plugin->get_attr_string)
748 result = plugin->get_attr_string(info, attribute, value);
749 plugin_close(info);
750
751 return result;
752 }
753
754 int CodecSetAttr(const CodecInfo& info, const char* attribute,
755 const char* value)
756 {
757 int result = -1;
758 codec_plugin_t* plugin = plugin_open(info);
759 if (plugin && plugin->set_attr_string)
760 result = plugin->set_attr_string(info, attribute, value);
761 plugin_close(info);
762 return result;
763 }
764
765 void FreeDecoderAudio(IAudioDecoder* decoder)
766 {
767 if (decoder)
768 {
769 const CodecInfo& info = decoder->GetCodecInfo();
770 AVM_WRITE("codec keeper", 1, "FreeAudioDecoder() %s\n", info.GetName());
771 delete decoder;
772 plugin_close(info);
773 }
774 }
775
776 void FreeEncoderAudio(IAudioEncoder* encoder)
777 {
778 if (encoder)
779 {
780 const CodecInfo& info = encoder->GetCodecInfo();
781 AVM_WRITE("codec keeper", 1, "FreeAudioEncoder() %s\n", info.GetName());
782 delete encoder;
783 plugin_close(info);
784 }
785 }
786
787 void FreeDecoderVideo(IVideoDecoder* decoder)
788 {
789 if (decoder)
790 {
791 const CodecInfo& info = decoder->GetCodecInfo();
792 AVM_WRITE("codec keeper", 1, "FreeVideoDecoder() %s\n", info.GetName());
793 delete decoder;
794 plugin_close(info);
795 }
796 }
797
798 void FreeEncoderVideo(IVideoEncoder* encoder)
799 {
800 if (encoder)
801 {
802 const CodecInfo& info = encoder->GetCodecInfo();
803 AVM_WRITE("codec keeper", 1, "FreeVideoEncoder() %s\n", info.GetName());
804 delete encoder;
805 plugin_close(info);
806 }
807 }
808
809 int SortVideoCodecs(const char* orderlist)
810 {
811 return sort_codec_list(video_order, orderlist);
812 }
813
814 int SortAudioCodecs(const char* orderlist)
815 {
816 return sort_codec_list(audio_order, orderlist);
817 }
818
819 int PluginGetAttrFloat(const CodecInfo& info, const char* attribute,
820 float* value)
821 {
822 const AttributeInfo* attr = info.FindAttribute(attribute);
823 const char* n = info.GetPrivateName();
824
825 if (attr && attr->GetKind() == AttributeInfo::Float)
826 {
827 *value = RegReadFloat(n, attribute, attr->GetDefaultFloat());
828 return 0;
829 }
830 AVM_WRITE(n, "GetAttrFloat unsupported attribute or incorrect value %s = %f\n", attribute, *value);\
831 return -1;
832 }
833
834 int PluginSetAttrFloat(const CodecInfo& info, const char* attribute, float value)
835 {
836 const AttributeInfo* attr = info.FindAttribute(attribute);
837 const char* n = info.GetPrivateName();
838
839 if (attr && attr->GetKind() == AttributeInfo::Float
840 && attr->IsValid(value))
841 return RegWriteFloat(n, attribute, value);
842
843 AVM_WRITE(n, "SetAttrFloat unsupported attribute or incorrect value %s = %f\n", attribute, value);
844 return -1;
845 }
846
847 int PluginGetAttrInt(const CodecInfo& info, const char* attribute, int* value)
848 {
849 const AttributeInfo* attr = info.FindAttribute(attribute);
850 const char* n = info.GetPrivateName();
851
852 if (attr && (attr->GetKind() == AttributeInfo::Integer
853 || attr->GetKind() == AttributeInfo::Select))
854 {
855 *value = RegReadInt(n, attribute, attr->GetDefault());
856 return 0;
857 }
858 AVM_WRITE(n, "GetAttrInt unsupported attribute or incorrect value %s = %d\n", attribute, *value);\
859 return -1;
860 }
861
862 int PluginSetAttrInt(const CodecInfo& info, const char* attribute, int value)
863 {
864 const AttributeInfo* attr = info.FindAttribute(attribute);
865 const char* n = info.GetPrivateName();
866
867 if (attr && (attr->GetKind() == AttributeInfo::Integer
868 || attr->GetKind() == AttributeInfo::Select)
869 && attr->IsValid(value))
870 {
871 return RegWriteInt(n, attribute, value);
872 }
873
874 AVM_WRITE(n, "SetAttrInt unsupported attribute or incorrect value %s = %d\n", attribute, value);
875 return -1;
876 }
877
878 int PluginGetAttrString(const CodecInfo& info, const char* attribute,
879 const char** value)
880 {
881 const AttributeInfo* attr = info.FindAttribute(attribute);
882 const char* n = info.GetPrivateName();
883
884 if (value && attr && (attr->GetKind() == AttributeInfo::String))
885 {
886 *value = RegReadString(n, attribute, "");
887 return 0;
888 }
889 AVM_WRITE(n, "GetAttrStr unsupported attribute or incorrect value %s = %p\n", attribute, value);\
890 return -1;
891 }
892
893 int PluginSetAttrString(const CodecInfo& info, const char* attribute,
894 const char* value)
895 {
896 const AttributeInfo* attr = info.FindAttribute(attribute);
897 const char* n = info.GetPrivateName();
898
899 if (value && attr && (attr->GetKind() == AttributeInfo::String))
900 return RegWriteString(n, attribute, value);
901
902 AVM_WRITE(n, "SetAttrString unsupported attribute or incorrect value %s = %p\n", attribute, value);
903 return -1;
904 }
905
906 const CodecInfo* CodecInfo::match(fourcc_t fcccodec, CodecInfo::Media cimedia,
907 const CodecInfo* start,
908 CodecInfo::Direction cdir)
909 {
910 if (video_codecs.size() == 0 && audio_codecs.size() == 0)
911 {
912 BITMAPINFOHEADER bi;
913 bi.biCompression = 0xffffffff;
914 // just to fill video_codecs list
915 CreateDecoderVideo(bi, 0, 0);
916 }
917
918 const avm::vector<CodecInfo>& c =
919 (cimedia == Video) ? video_codecs : audio_codecs;
920
921 for (size_t i = 0; i < c.size(); ++i)
922 {
923 if (start)
924 {
925 if (&c[i] == start)
926 start = 0;
927 continue;
928 }
929
930 const CodecInfo& ci = c[i];
931
932 if (((int)ci.direction & (int)cdir) != (int)cdir)
933 continue;
934
935 //printf("CHECK DIR %d %x %x %s\n", ci.direction, fcccodec, ci.fourcc, ci.GetName());
936 if (cdir == Encode)
937 {
938 // for encoding we want exact match - the first fourcc
939 if (fcccodec == ci.fourcc)
940 return &ci;
941 }
942 else
943 {
944 for (size_t j = 0; j < ci.fourcc_array.size(); j++)
945 {
946 //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);
947 if (fcccodec == ci.fourcc_array[j])
948 return &ci;
949 }
950 }
951 }
952 return 0;
953 }
954
955 const CodecInfo* CodecInfo::match(CodecInfo::Media cimedia, const char* pname)
956 {
957 if (video_codecs.size() == 0 && audio_codecs.size() == 0)
958 {
959 BITMAPINFOHEADER bi;
960 bi.biCompression = 0xffffffff;
961 // just to fill video_codecs list
962 CreateDecoderVideo(bi, 0, 0);
963 }
964
965 const avm::vector<CodecInfo>& c = (cimedia == Video )
966 ? video_codecs : audio_codecs;
967 for (size_t i = 0; i < c.size(); ++i)
968 {
969 const CodecInfo& ci = c[i];
970
971 if (strcmp(ci.GetPrivateName(), pname) == 0
972 || strcmp(ci.GetName(), pname) == 0)
973 return &ci;
974 }
975
976 return 0;
977 }
978
979 AVM_END_NAMESPACE;
980
981 int GetAvifileVersion()
982 {
983 return AVIFILE_VERSION;
984 }