Debian Codegraph @

lib/codeckeeper.cpp

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