add system build to autoconf/automake
[goodguy/history.git] / cinelerra-5.1 / cinelerra / file.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <ctype.h>
28 #include <limits.h>
29 // work arounds (centos)
30 #include <lzma.h>
31 #ifndef INT64_MAX
32 #define INT64_MAX 9223372036854775807LL
33 #endif
34
35 #include "asset.h"
36 #include "bchash.h"
37 #include "bcsignals.h"
38 #include "byteorder.h"
39 #include "cache.inc"
40 #include "condition.h"
41 #include "errorbox.h"
42 #include "fileac3.h"
43 #include "filebase.h"
44 #include "filecr2.h"
45 #include "filedb.h"
46 #include "filedv.h"
47 #include "fileexr.h"
48 #include "fileffmpeg.h"
49 #include "fileflac.h"
50 #include "filegif.h"
51 #include "file.h"
52 #include "filejpeg.h"
53 #include "filempeg.h"
54 #undef HAVE_STDLIB_H // automake conflict
55 #include "fileogg.h"
56 #include "filepng.h"
57 #include "filescene.h"
58 #include "filesndfile.h"
59 #include "filetga.h"
60 #include "filethread.h"
61 #include "filetiff.h"
62 #include "filevorbis.h"
63 #include "filexml.h"
64 #include "formatwindow.h"
65 #include "formattools.h"
66 #include "framecache.h"
67 #include "language.h"
68 #include "mainprogress.inc"
69 #include "mutex.h"
70 #include "mwindow.h"
71 #include "packagingengine.h"
72 #include "pluginserver.h"
73 #include "preferences.h"
74 #include "samples.h"
75 #include "vframe.h"
76
77 //suppress noref warning
78 void *vorbis0_ov_callbacks[] = {
79  &OV_CALLBACKS_DEFAULT, &OV_CALLBACKS_NOCLOSE,
80  &OV_CALLBACKS_STREAMONLY, &OV_CALLBACKS_STREAMONLY_NOCLOSE,
81 };
82
83 File::File()
84 {
85         cpus = 1;
86         asset = new Asset;
87         format_completion = new Condition(1, "File::format_completion");
88         write_lock = new Condition(1, "File::write_lock");
89         frame_cache = new FrameCache;
90         reset_parameters();
91 }
92
93 File::~File()
94 {
95         if(getting_options)
96         {
97                 if(format_window) format_window->set_done(0);
98                 format_completion->lock("File::~File");
99                 format_completion->unlock();
100         }
101
102         if(temp_frame) delete temp_frame;
103
104
105         close_file(0);
106
107         asset->Garbage::remove_user();
108         delete format_completion;
109         delete write_lock;
110         delete frame_cache;
111 }
112
113 void File::reset_parameters()
114 {
115         file = 0;
116         audio_thread = 0;
117         video_thread = 0;
118         getting_options = 0;
119         format_window = 0;
120         temp_frame = 0;
121         current_sample = 0;
122         current_frame = 0;
123         current_channel = 0;
124         current_program = 0;
125         current_layer = 0;
126         normalized_sample = 0;
127         use_cache = 0;
128         preferences = 0;
129         playback_subtitle = -1;
130         interpolate_raw = 1;
131
132
133         temp_samples_buffer = 0;
134         temp_frame_buffer = 0;
135         current_frame_buffer = 0;
136         audio_ring_buffers = 0;
137         video_ring_buffers = 0;
138         video_buffer_size = 0;
139 }
140
141 int File::raise_window()
142 {
143         if(getting_options && format_window)
144         {
145                 format_window->raise_window();
146                 format_window->flush();
147         }
148         return 0;
149 }
150
151 void File::close_window()
152 {
153         if(getting_options)
154         {
155                 format_window->lock_window("File::close_window");
156                 format_window->set_done(1);
157                 format_window->unlock_window();
158                 getting_options = 0;
159         }
160 }
161
162 int File::get_options(FormatTools *format,
163         int audio_options, int video_options)
164 {
165         BC_WindowBase *parent_window = format->window;
166         //ArrayList<PluginServer*> *plugindb = format->plugindb;
167         Asset *asset = format->asset;
168
169         getting_options = 1;
170         format_completion->lock("File::get_options");
171         switch(asset->format)
172         {
173                 case FILE_AC3:
174                         FileAC3::get_parameters(parent_window,
175                                 asset,
176                                 format_window,
177                                 audio_options,
178                                 video_options);
179                         break;
180 #ifdef HAVE_DV
181                 case FILE_RAWDV:
182                         FileDV::get_parameters(parent_window,
183                                 asset,
184                                 format_window,
185                                 audio_options,
186                                 video_options);
187                         break;
188 #endif
189                 case FILE_PCM:
190                 case FILE_WAV:
191                 case FILE_AU:
192                 case FILE_AIFF:
193                 case FILE_SND:
194                         FileSndFile::get_parameters(parent_window,
195                                 asset,
196                                 format_window,
197                                 audio_options,
198                                 video_options);
199                         break;
200                 case FILE_FFMPEG:
201                         FileFFMPEG::get_parameters(parent_window,
202                                 asset,
203                                 format_window,
204                                 audio_options,
205                                 video_options);
206                         break;
207                 case FILE_AMPEG:
208                 case FILE_VMPEG:
209                         FileMPEG::get_parameters(parent_window,
210                                 asset,
211                                 format_window,
212                                 audio_options,
213                                 video_options);
214                         break;
215                 case FILE_JPEG:
216                 case FILE_JPEG_LIST:
217                         FileJPEG::get_parameters(parent_window,
218                                 asset,
219                                 format_window,
220                                 audio_options,
221                                 video_options);
222                         break;
223                 case FILE_EXR:
224                 case FILE_EXR_LIST:
225                         FileEXR::get_parameters(parent_window,
226                                 asset,
227                                 format_window,
228                                 audio_options,
229                                 video_options);
230                         break;
231                 case FILE_FLAC:
232                         FileFLAC::get_parameters(parent_window,
233                                 asset,
234                                 format_window,
235                                 audio_options,
236                                 video_options);
237                         break;
238                 case FILE_PNG:
239                 case FILE_PNG_LIST:
240                         FilePNG::get_parameters(parent_window,
241                                 asset,
242                                 format_window,
243                                 audio_options,
244                                 video_options);
245                         break;
246                 case FILE_TGA:
247                 case FILE_TGA_LIST:
248                         FileTGA::get_parameters(parent_window,
249                                 asset,
250                                 format_window,
251                                 audio_options,
252                                 video_options);
253                         break;
254                 case FILE_TIFF:
255                 case FILE_TIFF_LIST:
256                         FileTIFF::get_parameters(parent_window,
257                                 asset,
258                                 format_window,
259                                 audio_options,
260                                 video_options);
261                         break;
262                 case FILE_OGG:
263                         FileOGG::get_parameters(parent_window,
264                                 asset,
265                                 format_window,
266                                 audio_options,
267                                 video_options);
268                         break;
269                 default:
270                         break;
271         }
272
273         if(!format_window)
274         {
275                 ErrorBox *errorbox = new ErrorBox(_(PROGRAM_NAME ": Error"),
276                         parent_window->get_abs_cursor_x(1),
277                         parent_window->get_abs_cursor_y(1));
278                 format_window = errorbox;
279                 getting_options = 1;
280                 if(audio_options)
281                         errorbox->create_objects(_("This format doesn't support audio."));
282                 else
283                 if(video_options)
284                         errorbox->create_objects(_("This format doesn't support video."));
285                 errorbox->run_window();
286                 delete errorbox;
287         }
288
289         getting_options = 0;
290         format_window = 0;
291         format_completion->unlock();
292         return 0;
293 }
294
295
296
297
298
299
300
301
302
303
304 int File::set_processors(int cpus)   // Set the number of cpus for certain codecs
305 {
306         if( cpus > 8 )          // mpegvideo max_threads = 16, more causes errs
307                 cpus = 8;       //  8 cpus ought to decode just about anything
308 // Set all instances so gets work.
309         this->cpus = cpus;
310
311         return 0;
312 }
313
314 int File::set_preload(int64_t size)
315 {
316         this->playback_preload = size;
317         return 0;
318 }
319
320 void File::set_subtitle(int value)
321 {
322         this->playback_subtitle = value;
323         if( file ) file->set_subtitle(value);
324 }
325
326 void File::set_interpolate_raw(int value)
327 {
328         this->interpolate_raw = value;
329 }
330
331 void File::set_white_balance_raw(int value)
332 {
333         this->white_balance_raw = value;
334 }
335
336
337 void File::set_cache_frames(int value)
338 {
339 // caching only done locally
340         if(!video_thread)
341                 use_cache = value;
342 }
343
344 int File::purge_cache()
345 {
346 // caching only done locally
347         int result = 0;
348         if( frame_cache->cache_items() > 0 )
349         {
350                 frame_cache->remove_all();
351                 result = 1;
352         }
353         return result;
354 }
355
356 int File::delete_oldest()
357 {
358 // caching only done locally
359         return frame_cache->delete_oldest();
360 }
361
362
363
364
365
366
367
368
369
370
371
372 int File::open_file(Preferences *preferences,
373         Asset *asset,
374         int rd,
375         int wr)
376 {
377         const int debug = 0;
378
379         this->preferences = preferences;
380         this->asset->copy_from(asset, 1);
381         this->rd = rd;
382         this->wr = wr;
383         file = 0;
384
385         if(debug) printf("File::open_file %d\n", __LINE__);
386
387         if(debug) printf("File::open_file %p %d\n", this, __LINE__);
388
389         switch(this->asset->format)
390         {
391 // get the format now
392 // If you add another format to case 0, you also need to add another case for the
393 // file format #define.
394                 case FILE_UNKNOWN:
395                         if(FileDB::check_sig(this->asset)) {
396 // MediaDb file
397                                 file = new FileDB(this->asset, this);
398                                 break;
399                         }
400 // if early probe enabled
401                         if( preferences->ffmpeg_early_probe &&
402                             FileFFMPEG::check_sig(this->asset)) {
403                                 file = new FileFFMPEG(this->asset, this);
404                                 break;
405                         }
406
407                         FILE *stream;
408                         if(!(stream = fopen(this->asset->path, "rb"))) {
409 // file not found
410                                 return 1;
411                         }
412
413                         char test[16];  memset(test,0,sizeof(test)); // int result =
414                         fread(test, 16, 1, stream);
415
416                         if(FileScene::check_sig(this->asset, test)) {
417 // scene file
418                                 fclose(stream);
419                                 file = new FileScene(this->asset, this);
420                                 break;
421                         }
422 #ifdef HAVE_DV
423                         if(FileDV::check_sig(this->asset)) {
424 // libdv
425                                 fclose(stream);
426                                 file = new FileDV(this->asset, this);
427                                 break;
428                         }
429 #endif
430                         if(FileSndFile::check_sig(this->asset)) {
431 // libsndfile
432                                 fclose(stream);
433                                 file = new FileSndFile(this->asset, this);
434                                 break;
435                         }
436                         if(FilePNG::check_sig(this->asset)) {
437 // PNG file
438                                 fclose(stream);
439                                 file = new FilePNG(this->asset, this);
440                                 break;
441                         }
442                         if(FileJPEG::check_sig(this->asset)) {
443 // JPEG file
444                                 fclose(stream);
445                                 file = new FileJPEG(this->asset, this);
446                                 break;
447                         }
448                         if(FileGIF::check_sig(this->asset)) {
449 // GIF file
450                                 fclose(stream);
451                                 file = new FileGIF(this->asset, this);
452                                 break;
453                         }
454                         if(FileEXR::check_sig(this->asset, test)) {
455 // EXR file
456                                 fclose(stream);
457                                 file = new FileEXR(this->asset, this);
458                                 break;
459                         }
460                         if(FileFLAC::check_sig(this->asset, test)) {
461 // FLAC file
462                                 fclose(stream);
463                                 file = new FileFLAC(this->asset, this);
464                                 break;
465                         }
466                         if(FileCR2::check_sig(this->asset)) {
467 // CR2 file
468                                 fclose(stream);
469                                 file = new FileCR2(this->asset, this);
470                                 break;
471                         }
472                         if(FileTGA::check_sig(this->asset)) {
473 // TGA file
474                                 fclose(stream);
475                                 file = new FileTGA(this->asset, this);
476                                 break;
477                         }
478                         if(FileTIFF::check_sig(this->asset)) {
479 // TIFF file
480                                 fclose(stream);
481                                 file = new FileTIFF(this->asset, this);
482                                 break;
483                         }
484                         if(FileOGG::check_sig(this->asset)) {
485 // OGG file
486                                 fclose(stream);
487                                 file = new FileOGG(this->asset, this);
488                                 break;
489                         }
490                         if(FileVorbis::check_sig(this->asset)) {
491 // VorbisFile file
492                                 fclose(stream);
493                                 file = new FileVorbis(this->asset, this);
494                                 break;
495                         }
496                         if(FileMPEG::check_sig(this->asset)) {
497 // MPEG file
498                                 fclose(stream);
499                                 file = new FileMPEG(this->asset, this);
500                                 break;
501                         }
502                         if( test[0] == '<' && (
503                                 !strncmp(&test[1],"EDL>",4) ||
504                                 !strncmp(&test[1],"HTAL>",5) ||
505                                 !strncmp(&test[1],"?xml",4) ) ) {
506 // XML file
507                                 fclose(stream);
508                                 return FILE_IS_XML;
509                         }    // can't load project file
510                         if( !preferences->ffmpeg_early_probe &&
511                             FileFFMPEG::check_sig(this->asset) ) {
512                                 fclose(stream);
513                                 file = new FileFFMPEG(this->asset, this);
514                                 break;
515                         }
516 // PCM file
517                         fclose(stream);
518                         return FILE_UNRECOGNIZED_CODEC;
519                         break;
520
521 // format already determined
522                 case FILE_AC3:
523                         file = new FileAC3(this->asset, this);
524                         break;
525
526                 case FILE_SCENE:
527                         file = new FileScene(this->asset, this);
528                         break;
529
530                 case FILE_FFMPEG:
531                         file = new FileFFMPEG(this->asset, this);
532                         break;
533
534                 case FILE_PCM:
535                 case FILE_WAV:
536                 case FILE_AU:
537                 case FILE_AIFF:
538                 case FILE_SND:
539 //printf("File::open_file 1\n");
540                         file = new FileSndFile(this->asset, this);
541                         break;
542
543                 case FILE_PNG:
544                 case FILE_PNG_LIST:
545                         file = new FilePNG(this->asset, this);
546                         break;
547
548                 case FILE_JPEG:
549                 case FILE_JPEG_LIST:
550                         file = new FileJPEG(this->asset, this);
551                         break;
552
553                 case FILE_GIF:
554                 case FILE_GIF_LIST:
555                         file = new FileGIF(this->asset, this);
556                         break;
557
558                 case FILE_EXR:
559                 case FILE_EXR_LIST:
560                         file = new FileEXR(this->asset, this);
561                         break;
562
563                 case FILE_FLAC:
564                         file = new FileFLAC(this->asset, this);
565                         break;
566
567                 case FILE_CR2:
568                 case FILE_CR2_LIST:
569                         file = new FileCR2(this->asset, this);
570                         break;
571
572                 case FILE_TGA_LIST:
573                 case FILE_TGA:
574                         file = new FileTGA(this->asset, this);
575                         break;
576
577                 case FILE_TIFF:
578                 case FILE_TIFF_LIST:
579                         file = new FileTIFF(this->asset, this);
580                         break;
581
582                 case FILE_DB:
583                         file = new FileDB(this->asset, this);
584                         break;
585
586                 case FILE_MPEG:
587                 case FILE_AMPEG:
588                 case FILE_VMPEG:
589                         file = new FileMPEG(this->asset, this);
590                         break;
591
592                 case FILE_OGG:
593                         file = new FileOGG(this->asset, this);
594                         break;
595
596                 case FILE_VORBIS:
597                         file = new FileVorbis(this->asset, this);
598                         break;
599 #ifdef HAVE_DV
600                 case FILE_RAWDV:
601                         file = new FileDV(this->asset, this);
602                         break;
603 #endif
604 // try plugins
605                 default:
606                         return 1;
607                         break;
608         }
609
610
611 // Reopen file with correct parser and get header.
612         if(file->open_file(rd, wr)) {
613                 delete file;  file = 0;
614                 return FILE_NOT_FOUND;
615         }
616
617
618
619 // Set extra writing parameters to mandatory settings.
620         if( wr ) {
621                 if(this->asset->dither) file->set_dither();
622         }
623
624         if( rd ) {
625 // one frame image file, no specific length
626                 if( !this->asset->audio_data && this->asset->video_data &&
627                     this->asset->video_length <= 1 )
628                         this->asset->video_length = -1;
629         }
630
631 // Synchronize header parameters
632         asset->copy_from(this->asset, 1);
633 //asset->dump();
634
635         if(debug) printf("File::open_file %d file=%p\n", __LINE__, file);
636 // sleep(1);
637
638         return FILE_OK;
639 }
640
641 void File::delete_temp_samples_buffer()
642 {
643
644         if(temp_samples_buffer) {
645                 for(int j = 0; j < audio_ring_buffers; j++) {
646                         for(int i = 0; i < asset->channels; i++) {
647                                 delete temp_samples_buffer[j][i];
648                         }
649                         delete [] temp_samples_buffer[j];
650                 }
651
652                 delete [] temp_samples_buffer;
653                 temp_samples_buffer = 0;
654                 audio_ring_buffers = 0;
655         }
656 }
657
658 void File::delete_temp_frame_buffer()
659 {
660
661         if(temp_frame_buffer) {
662                 for(int k = 0; k < video_ring_buffers; k++) {
663                         for(int i = 0; i < asset->layers; i++) {
664                                 for(int j = 0; j < video_buffer_size; j++) {
665                                         delete temp_frame_buffer[k][i][j];
666                                 }
667                                 delete [] temp_frame_buffer[k][i];
668                         }
669                         delete [] temp_frame_buffer[k];
670                 }
671
672                 delete [] temp_frame_buffer;
673                 temp_frame_buffer = 0;
674                 video_ring_buffers = 0;
675                 video_buffer_size = 0;
676         }
677 }
678
679 int File::close_file(int ignore_thread)
680 {
681         const int debug = 0;
682
683         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
684
685         if(!ignore_thread) {
686                 stop_audio_thread();
687                 stop_video_thread();
688         }
689
690
691         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
692         if(file) {
693 // The file's asset is a copy of the argument passed to open_file so the
694 // user must copy lengths from the file's asset.
695                 if(asset && wr) {
696                         asset->audio_length = current_sample;
697                         asset->video_length = current_frame;
698                 }
699
700                 file->close_file();
701                 delete file;
702         }
703         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
704
705         delete_temp_samples_buffer();
706         delete_temp_frame_buffer();
707         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
708
709         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
710
711         reset_parameters();
712         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
713         return 0;
714 }
715
716
717
718 int File::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
719 {
720         return !file ? -1 : file->get_index(index_file, progress_bar);
721 }
722
723
724
725 int File::start_audio_thread(int buffer_size, int ring_buffers)
726 {
727         this->audio_ring_buffers = ring_buffers;
728
729
730         if(!audio_thread)
731         {
732                 audio_thread = new FileThread(this, 1, 0);
733                 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
734         }
735         return 0;
736 }
737
738 int File::start_video_thread(int buffer_size,
739         int color_model,
740         int ring_buffers,
741         int compressed)
742 {
743         this->video_ring_buffers = ring_buffers;
744         this->video_buffer_size = buffer_size;
745
746         if(!video_thread)
747         {
748                 video_thread = new FileThread(this, 0, 1);
749                 video_thread->start_writing(buffer_size,
750                         color_model,
751                         ring_buffers,
752                         compressed);
753         }
754         return 0;
755 }
756
757 int File::start_video_decode_thread()
758 {
759 // Currently, CR2 is the only one which won't work asynchronously, so
760 // we're not using a virtual function yet.
761         if(!video_thread /* && asset->format != FILE_CR2 */)
762         {
763                 video_thread = new FileThread(this, 0, 1);
764                 video_thread->start_reading();
765                 use_cache = 0;
766         }
767         return 0;
768 }
769
770 int File::stop_audio_thread()
771 {
772         if(audio_thread)
773         {
774                 audio_thread->stop_writing();
775                 delete audio_thread;
776                 audio_thread = 0;
777         }
778         return 0;
779 }
780
781 int File::stop_video_thread()
782 {
783         if(video_thread)
784         {
785                 video_thread->stop_reading();
786                 video_thread->stop_writing();
787                 delete video_thread;
788                 video_thread = 0;
789         }
790         return 0;
791 }
792
793 FileThread* File::get_video_thread()
794 {
795         return video_thread;
796 }
797
798 int File::set_channel(int channel)
799 {
800         if(file && channel < asset->channels)
801         {
802                 current_channel = channel;
803                 return 0;
804         }
805         else
806                 return 1;
807 }
808
809 int File::get_channel()
810 {
811         return current_channel;
812 }
813
814 // if no>=0, sets new program
815 //  returns current program
816 int File::set_program(int no)
817 {
818         int result = file ? file->set_program(no) : current_program;
819         current_program = no < 0 ? result : no;
820         return result;
821 }
822
823 int File::get_cell_time(int no, double &time)
824 {
825         return file ? file->get_cell_time(no, time) : -1;
826 }
827
828 int File::get_system_time(int64_t &tm)
829 {
830         return file ? file->get_system_time(tm) : -1;
831 }
832
833 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
834 {
835         return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
836 }
837
838 int File::get_video_pid(int track)
839 {
840         return file ? file->get_video_pid(track) : -1;
841 }
842
843
844
845 int File::get_video_info(int track, int &pid, double &framerate,
846                 int &width, int &height, char *title)
847 {
848         return !file ? -1 :
849                  file->get_video_info(track, pid, framerate, width, height, title);
850 }
851
852 int File::select_video_stream(Asset *asset, int vstream)
853 {
854         return !file ? -1 :
855                  file->select_video_stream(asset, vstream);
856 }
857
858 int File::select_audio_stream(Asset *asset, int astream)
859 {
860         return !file ? -1 :
861                  file->select_audio_stream(asset, astream);
862 }
863
864
865 int File::get_thumbnail(int stream,
866         int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
867 {
868         return file->get_thumbnail(stream, position, thumbnail, ww, hh);
869 }
870
871 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
872 {
873         return file->set_skimming(track, skim, fn, vp);
874 }
875
876 int File::skim_video(int track, void *vp, skim_fn fn)
877 {
878         return file->skim_video(track, vp, fn);
879 }
880
881
882 int File::set_layer(int layer, int is_thread)
883 {
884         if(file && layer < asset->layers)
885         {
886                 if(!is_thread && video_thread)
887                 {
888                         video_thread->set_layer(layer);
889                 }
890                 else
891                 {
892                         current_layer = layer;
893                 }
894                 return 0;
895         }
896         else
897                 return 1;
898 }
899
900 int64_t File::get_audio_length()
901 {
902         int64_t result = asset->audio_length;
903         int64_t base_samplerate = -1;
904         if(result > 0)
905         {
906                 if(base_samplerate > 0)
907                         return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
908                 else
909                         return result;
910         }
911         else
912                 return -1;
913 }
914
915 int64_t File::get_video_length()
916 {
917         int64_t result = asset->video_length;
918         float base_framerate = -1;
919         if(result > 0)
920         {
921                 if(base_framerate > 0)
922                         return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
923                 else
924                         return result;
925         }
926         else
927                 return -1;  // infinity
928 }
929
930
931 int64_t File::get_video_position()
932 {
933         float base_framerate = -1;
934         if(base_framerate > 0)
935                 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
936         else
937                 return current_frame;
938 }
939
940 int64_t File::get_audio_position()
941 {
942 //      int64_t base_samplerate = -1;
943 //      if(base_samplerate > 0)
944 //      {
945 //              if(normalized_sample_rate == base_samplerate)
946 //                      return normalized_sample;
947 //              else
948 //                      return (int64_t)((double)current_sample /
949 //                              asset->sample_rate *
950 //                              base_samplerate +
951 //                              0.5);
952 //      }
953 //      else
954                 return current_sample;
955 }
956
957
958
959 int File::set_audio_position(int64_t position)
960 {
961         int result = 0;
962
963         if(!file) return 1;
964
965 #define REPOSITION(x, y) \
966         (labs((x) - (y)) > 1)
967
968         float base_samplerate = asset->sample_rate;
969         current_sample = normalized_sample = position;
970
971 // printf("File::set_audio_position %d normalized_sample=%ld\n",
972 // __LINE__,
973 // normalized_sample);
974                 result = file->set_audio_position(current_sample);
975
976                 if(result)
977                         printf("File::set_audio_position position=%jd"
978                                 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
979                                 position, base_samplerate, asset, asset->sample_rate);
980
981 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
982
983         return result;
984 }
985
986 int File::set_video_position(int64_t position,
987         int is_thread)
988 {
989         int result = 0;
990         if(!file) return 0;
991
992 // Convert to file's rate
993 //      if(base_framerate > 0)
994 //              position = (int64_t)((double)position /
995 //                      base_framerate *
996 //                      asset->frame_rate +
997 //                      0.5);
998
999
1000         if(video_thread && !is_thread)
1001         {
1002 // Call thread.  Thread calls this again to set the file state.
1003                 video_thread->set_video_position(position);
1004         }
1005         else
1006         if(current_frame != position)
1007         {
1008                 if(file)
1009                 {
1010                         current_frame = position;
1011                         result = file->set_video_position(current_frame);
1012                 }
1013         }
1014
1015         return result;
1016 }
1017
1018 // No resampling here.
1019 int File::write_samples(Samples **buffer, int64_t len)
1020 {
1021         int result = 1;
1022
1023         if(file)
1024         {
1025                 write_lock->lock("File::write_samples");
1026
1027 // Convert to arrays for backwards compatability
1028                 double *temp[asset->channels];
1029                 for(int i = 0; i < asset->channels; i++)
1030                 {
1031                         temp[i] = buffer[i]->get_data();
1032                 }
1033
1034                 result = file->write_samples(temp, len);
1035                 current_sample += len;
1036                 normalized_sample += len;
1037                 asset->audio_length += len;
1038                 write_lock->unlock();
1039         }
1040         return result;
1041 }
1042
1043
1044
1045
1046
1047 // Can't put any cmodel abstraction here because the filebase couldn't be
1048 // parallel.
1049 int File::write_frames(VFrame ***frames, int len)
1050 {
1051 //printf("File::write_frames %d\n", __LINE__);
1052 //PRINT_TRACE
1053 // Store the counters in temps so the filebase can choose to overwrite them.
1054         int result;
1055         int current_frame_temp = current_frame;
1056         int video_length_temp = asset->video_length;
1057
1058         write_lock->lock("File::write_frames");
1059
1060 //PRINT_TRACE
1061         result = file->write_frames(frames, len);
1062 //PRINT_TRACE
1063
1064         current_frame = current_frame_temp + len;
1065         asset->video_length = video_length_temp + len;
1066         write_lock->unlock();
1067 //PRINT_TRACE
1068         return result;
1069 }
1070
1071 // Only called by FileThread
1072 int File::write_compressed_frame(VFrame *buffer)
1073 {
1074         int result = 0;
1075         write_lock->lock("File::write_compressed_frame");
1076         result = file->write_compressed_frame(buffer);
1077         current_frame++;
1078         asset->video_length++;
1079         write_lock->unlock();
1080         return result;
1081 }
1082
1083
1084 int File::write_audio_buffer(int64_t len)
1085 {
1086         int result = 0;
1087         if(audio_thread)
1088         {
1089                 result = audio_thread->write_buffer(len);
1090         }
1091         return result;
1092 }
1093
1094 int File::write_video_buffer(int64_t len)
1095 {
1096         int result = 0;
1097         if(video_thread)
1098         {
1099                 result = video_thread->write_buffer(len);
1100         }
1101
1102         return result;
1103 }
1104
1105 Samples** File::get_audio_buffer()
1106 {
1107         if(audio_thread) return audio_thread->get_audio_buffer();
1108         return 0;
1109 }
1110
1111 VFrame*** File::get_video_buffer()
1112 {
1113         if(video_thread)
1114         {
1115                 VFrame*** result = video_thread->get_video_buffer();
1116
1117                 return result;
1118         }
1119
1120         return 0;
1121 }
1122
1123
1124 int File::read_samples(Samples *samples, int64_t len)
1125 {
1126 // Never try to read more samples than exist in the file
1127         if (current_sample + len > asset->audio_length) {
1128                 len = asset->audio_length - current_sample;
1129         }
1130         if(len <= 0) return 0;
1131
1132         int result = 0;
1133         const int debug = 0;
1134         if(debug) PRINT_TRACE
1135
1136         if(debug) PRINT_TRACE
1137
1138         double *buffer = samples->get_data();
1139
1140         int64_t base_samplerate = asset->sample_rate;
1141
1142         if(file)
1143         {
1144 // Resample recursively calls this with the asset sample rate
1145                 if(base_samplerate == 0) base_samplerate = asset->sample_rate;
1146
1147                 if(debug) PRINT_TRACE
1148                 result = file->read_samples(buffer, len);
1149
1150                 if(debug) PRINT_TRACE
1151                 current_sample += len;
1152
1153                 normalized_sample += len;
1154         }
1155         if(debug) PRINT_TRACE
1156
1157         return result;
1158 }
1159
1160 int File::read_frame(VFrame *frame, int is_thread)
1161 {
1162         const int debug = 0;
1163
1164         if(debug) PRINT_TRACE
1165
1166 //printf("File::read_frame %d\n", __LINE__);
1167
1168         if(video_thread && !is_thread) return video_thread->read_frame(frame);
1169
1170 //printf("File::read_frame %d\n", __LINE__);
1171         if(debug) PRINT_TRACE
1172         if( !file ) return 1;
1173         if(debug) PRINT_TRACE
1174         int supported_colormodel = colormodel_supported(frame->get_color_model());
1175         int advance_position = 1;
1176         int cache_active = use_cache || asset->video_length < 0 ? 1 : 0;
1177         int64_t cache_position = asset->video_length >= 0 ? current_frame : -1;
1178 // Test cache
1179         if( cache_active && frame_cache->get_frame(frame, cache_position,
1180                         current_layer, asset->frame_rate) )
1181         {
1182 // Can't advance position if cache used.
1183 //printf("File::read_frame %d\n", __LINE__);
1184                 advance_position = 0;
1185         }
1186 // Need temp
1187         else if(frame->get_color_model() != BC_COMPRESSED &&
1188                 (supported_colormodel != frame->get_color_model() ||
1189                 frame->get_w() != asset->width ||
1190                 frame->get_h() != asset->height))
1191         {
1192
1193 //                      printf("File::read_frame %d\n", __LINE__);
1194 // Can't advance position here because it needs to be added to cache
1195                 if(temp_frame)
1196                 {
1197                         if(!temp_frame->params_match(asset->width, asset->height, supported_colormodel))
1198                         {
1199                                 delete temp_frame;
1200                                 temp_frame = 0;
1201                         }
1202                 }
1203
1204 //                      printf("File::read_frame %d\n", __LINE__);
1205                 if(!temp_frame)
1206                 {
1207                         temp_frame = new VFrame(0,
1208                                 -1,
1209                                 asset->width,
1210                                 asset->height,
1211                                 supported_colormodel,
1212                                 -1);
1213                 }
1214
1215 //                      printf("File::read_frame %d\n", __LINE__);
1216                 temp_frame->copy_stacks(frame);
1217                 int result = file->read_frame(temp_frame);
1218                 if( result && frame->get_status() > 0 )
1219                         frame->set_status(-1);
1220 //for(int i = 0; i < 1000 * 1000; i++) ((float*)temp_frame->get_rows()[0])[i] = 1.0;
1221 // printf("File::read_frame %d %d %d %d %d %d\n",
1222 // temp_frame->get_color_model(),
1223 // temp_frame->get_w(),
1224 // temp_frame->get_h(),
1225 // frame->get_color_model(),
1226 // frame->get_w(),
1227 // frame->get_h());
1228                 BC_CModels::transfer(frame->get_rows(), temp_frame->get_rows(),
1229                         frame->get_y(), frame->get_u(), frame->get_v(),
1230                         temp_frame->get_y(), temp_frame->get_u(), temp_frame->get_v(),
1231                         0, 0, temp_frame->get_w(), temp_frame->get_h(),
1232                         0, 0, frame->get_w(), frame->get_h(),
1233                         temp_frame->get_color_model(),
1234                         frame->get_color_model(), 0, temp_frame->get_w(),
1235                         frame->get_w());
1236 //                      printf("File::read_frame %d\n", __LINE__);
1237         }
1238         else
1239         {
1240 // Can't advance position here because it needs to be added to cache
1241 //printf("File::read_frame %d\n", __LINE__);
1242                 int result = file->read_frame(frame);
1243                 if( result && frame->get_status() > 0 )
1244                         frame->set_status(-1);
1245 //for(int i = 0; i < 100 * 1000; i++) ((float*)frame->get_rows()[0])[i] = 1.0;
1246         }
1247
1248         if( cache_active && advance_position && frame->get_status() > 0 )
1249                 frame_cache->put_frame(frame, cache_position,
1250                         current_layer, asset->frame_rate, 1, 0);
1251 //printf("File::read_frame %d\n", __LINE__);
1252
1253         if(advance_position) current_frame++;
1254         if(debug) PRINT_TRACE
1255         return 0;
1256 }
1257
1258 int File::can_copy_from(Asset *asset,
1259         int64_t position,
1260         int output_w,
1261         int output_h)
1262 {
1263         if(!asset) return 0;
1264
1265         if(file)
1266         {
1267                 return asset->width == output_w &&
1268                         asset->height == output_h &&
1269                         file->can_copy_from(asset, position);
1270         }
1271         else
1272                 return 0;
1273 }
1274
1275 // Fill in queries about formats when adding formats here.
1276
1277
1278 int File::strtoformat(const char *format)
1279 {
1280         return strtoformat(0, format);
1281 }
1282
1283 int File::strtoformat(ArrayList<PluginServer*> *plugindb, const char *format)
1284 {
1285         if(!strcasecmp(format, _(AC3_NAME))) return FILE_AC3;
1286         if(!strcasecmp(format, _(SCENE_NAME))) return FILE_SCENE;
1287         if(!strcasecmp(format, _(WAV_NAME))) return FILE_WAV;
1288         if(!strcasecmp(format, _(PCM_NAME))) return FILE_PCM;
1289         if(!strcasecmp(format, _(AU_NAME))) return FILE_AU;
1290         if(!strcasecmp(format, _(AIFF_NAME))) return FILE_AIFF;
1291         if(!strcasecmp(format, _(SND_NAME))) return FILE_SND;
1292         if(!strcasecmp(format, _(PNG_NAME))) return FILE_PNG;
1293         if(!strcasecmp(format, _(PNG_LIST_NAME))) return FILE_PNG_LIST;
1294         if(!strcasecmp(format, _(TIFF_NAME))) return FILE_TIFF;
1295         if(!strcasecmp(format, _(TIFF_LIST_NAME))) return FILE_TIFF_LIST;
1296         if(!strcasecmp(format, _(JPEG_NAME))) return FILE_JPEG;
1297         if(!strcasecmp(format, _(JPEG_LIST_NAME))) return FILE_JPEG_LIST;
1298         if(!strcasecmp(format, _(EXR_NAME))) return FILE_EXR;
1299         if(!strcasecmp(format, _(EXR_LIST_NAME))) return FILE_EXR_LIST;
1300         if(!strcasecmp(format, _(FLAC_NAME))) return FILE_FLAC;
1301         if(!strcasecmp(format, _(CR2_NAME))) return FILE_CR2;
1302         if(!strcasecmp(format, _(CR2_LIST_NAME))) return FILE_CR2_LIST;
1303         if(!strcasecmp(format, _(MPEG_NAME))) return FILE_MPEG;
1304         if(!strcasecmp(format, _(AMPEG_NAME))) return FILE_AMPEG;
1305         if(!strcasecmp(format, _(VMPEG_NAME))) return FILE_VMPEG;
1306         if(!strcasecmp(format, _(TGA_NAME))) return FILE_TGA;
1307         if(!strcasecmp(format, _(TGA_LIST_NAME))) return FILE_TGA_LIST;
1308         if(!strcasecmp(format, _(OGG_NAME))) return FILE_OGG;
1309         if(!strcasecmp(format, _(VORBIS_NAME))) return FILE_VORBIS;
1310         if(!strcasecmp(format, _(RAWDV_NAME))) return FILE_RAWDV;
1311         if(!strcasecmp(format, _(FFMPEG_NAME))) return FILE_FFMPEG;
1312         if(!strcasecmp(format, _(DBASE_NAME))) return FILE_DB;
1313
1314         return 0;
1315 }
1316
1317
1318 const char* File::formattostr(int format)
1319 {
1320         return formattostr(0, format);
1321 }
1322
1323 const char* File::formattostr(ArrayList<PluginServer*> *plugindb, int format)
1324 {
1325         switch(format)
1326         {
1327                 case FILE_SCENE:        return _(SCENE_NAME);
1328                 case FILE_AC3:          return _(AC3_NAME);
1329                 case FILE_WAV:          return _(WAV_NAME);
1330                 case FILE_PCM:          return _(PCM_NAME);
1331                 case FILE_AU:           return _(AU_NAME);
1332                 case FILE_AIFF:         return _(AIFF_NAME);
1333                 case FILE_SND:          return _(SND_NAME);
1334                 case FILE_PNG:          return _(PNG_NAME);
1335                 case FILE_PNG_LIST:     return _(PNG_LIST_NAME);
1336                 case FILE_JPEG:         return _(JPEG_NAME);
1337                 case FILE_JPEG_LIST:    return _(JPEG_LIST_NAME);
1338                 case FILE_CR2:          return _(CR2_NAME);
1339                 case FILE_CR2_LIST:     return _(CR2_LIST_NAME);
1340                 case FILE_FLAC:         return _(FLAC_NAME);
1341                 case FILE_EXR:          return _(EXR_NAME);
1342                 case FILE_EXR_LIST:     return _(EXR_LIST_NAME);
1343                 case FILE_MPEG:         return _(MPEG_NAME);
1344                 case FILE_AMPEG:        return _(AMPEG_NAME);
1345                 case FILE_VMPEG:        return _(VMPEG_NAME);
1346                 case FILE_TGA:          return _(TGA_NAME);
1347                 case FILE_TGA_LIST:     return _(TGA_LIST_NAME);
1348                 case FILE_TIFF:         return _(TIFF_NAME);
1349                 case FILE_TIFF_LIST:    return _(TIFF_LIST_NAME);
1350                 case FILE_OGG:          return _(OGG_NAME);
1351                 case FILE_VORBIS:       return _(VORBIS_NAME);
1352                 case FILE_RAWDV:        return _(RAWDV_NAME);
1353                 case FILE_FFMPEG:       return _(FFMPEG_NAME);
1354                 case FILE_DB:           return _(DBASE_NAME);
1355         }
1356         return _("Unknown");
1357 }
1358
1359 int File::strtobits(const char *bits)
1360 {
1361         if(!strcasecmp(bits, _(NAME_8BIT))) return BITSLINEAR8;
1362         if(!strcasecmp(bits, _(NAME_16BIT))) return BITSLINEAR16;
1363         if(!strcasecmp(bits, _(NAME_24BIT))) return BITSLINEAR24;
1364         if(!strcasecmp(bits, _(NAME_32BIT))) return BITSLINEAR32;
1365         if(!strcasecmp(bits, _(NAME_ULAW))) return BITSULAW;
1366         if(!strcasecmp(bits, _(NAME_ADPCM))) return BITS_ADPCM;
1367         if(!strcasecmp(bits, _(NAME_FLOAT))) return BITSFLOAT;
1368         return BITSLINEAR16;
1369 }
1370
1371 const char* File::bitstostr(int bits)
1372 {
1373 //printf("File::bitstostr\n");
1374         switch(bits)
1375         {
1376                 case BITSLINEAR8:       return (NAME_8BIT);
1377                 case BITSLINEAR16:      return (NAME_16BIT);
1378                 case BITSLINEAR24:      return (NAME_24BIT);
1379                 case BITSLINEAR32:      return (NAME_32BIT);
1380                 case BITSULAW:          return (NAME_ULAW);
1381                 case BITS_ADPCM:        return (NAME_ADPCM);
1382                 case BITSFLOAT:         return (NAME_FLOAT);
1383         }
1384         return _("Unknown");
1385 }
1386
1387
1388
1389 int File::str_to_byteorder(const char *string)
1390 {
1391         if(!strcasecmp(string, _("Lo Hi"))) return 1;
1392         return 0;
1393 }
1394
1395 const char* File::byteorder_to_str(int byte_order)
1396 {
1397         if(byte_order) return _("Lo Hi");
1398         return _("Hi Lo");
1399 }
1400
1401 int File::bytes_per_sample(int bits)
1402 {
1403         switch(bits)
1404         {
1405                 case BITSLINEAR8:       return 1;
1406                 case BITSLINEAR16:      return 2;
1407                 case BITSLINEAR24:      return 3;
1408                 case BITSLINEAR32:      return 4;
1409                 case BITSULAW:          return 1;
1410         }
1411         return 1;
1412 }
1413
1414
1415
1416
1417
1418 int File::get_best_colormodel(int driver)
1419 {
1420         return get_best_colormodel(asset, driver);
1421 }
1422
1423 int File::get_best_colormodel(Asset *asset, int driver)
1424 {
1425         switch(asset->format)
1426         {
1427 #ifdef HAVE_FIREWIRE
1428                 case FILE_RAWDV:        return FileDV::get_best_colormodel(asset, driver);
1429 #endif
1430                 case FILE_MPEG:         return FileMPEG::get_best_colormodel(asset, driver);
1431                 case FILE_JPEG:
1432                 case FILE_JPEG_LIST:    return FileJPEG::get_best_colormodel(asset, driver);
1433                 case FILE_EXR:
1434                 case FILE_EXR_LIST:     return FileEXR::get_best_colormodel(asset, driver);
1435                 case FILE_PNG:
1436                 case FILE_PNG_LIST:     return FilePNG::get_best_colormodel(asset, driver);
1437                 case FILE_TGA:
1438                 case FILE_TGA_LIST:     return FileTGA::get_best_colormodel(asset, driver);
1439                 case FILE_CR2:
1440                 case FILE_CR2_LIST:     return FileCR2::get_best_colormodel(asset, driver);
1441                 case FILE_DB:           return FileDB::get_best_colormodel(asset, driver);
1442                 case FILE_FFMPEG:       return FileFFMPEG::get_best_colormodel(asset, driver);
1443         }
1444
1445         return BC_RGB888;
1446 }
1447
1448
1449 int File::colormodel_supported(int colormodel)
1450 {
1451         if(file)
1452                 return file->colormodel_supported(colormodel);
1453
1454         return BC_RGB888;
1455 }
1456
1457
1458 int64_t File::file_memory_usage()
1459 {
1460         return file ? file->base_memory_usage() : 0;
1461 }
1462
1463 int64_t File::get_memory_usage()
1464 {
1465         int64_t result = 0;
1466
1467         result += file_memory_usage();
1468         if(temp_frame) result += temp_frame->get_data_size();
1469         result += frame_cache->get_memory_usage();
1470         if(video_thread) result += video_thread->get_memory_usage();
1471
1472         if(result < MIN_CACHEITEM_SIZE) result = MIN_CACHEITEM_SIZE;
1473         return result;
1474 }
1475
1476
1477 int File::supports_video(ArrayList<PluginServer*> *plugindb, char *format)
1478 {
1479         int format_i = strtoformat(plugindb, format);
1480
1481         return supports_video(format_i);
1482         return 0;
1483 }
1484
1485 int File::supports_audio(ArrayList<PluginServer*> *plugindb, char *format)
1486 {
1487         int format_i = strtoformat(plugindb, format);
1488
1489         return supports_audio(format_i);
1490         return 0;
1491 }
1492
1493
1494 int File::supports_video(int format)
1495 {
1496 //printf("File::supports_video %d\n", format);
1497         switch(format)
1498         {
1499                 case FILE_OGG:
1500                 case FILE_JPEG:
1501                 case FILE_JPEG_LIST:
1502                 case FILE_CR2:
1503                 case FILE_CR2_LIST:
1504                 case FILE_EXR:
1505                 case FILE_EXR_LIST:
1506                 case FILE_PNG:
1507                 case FILE_PNG_LIST:
1508                 case FILE_TGA:
1509                 case FILE_TGA_LIST:
1510                 case FILE_TIFF:
1511                 case FILE_TIFF_LIST:
1512                 case FILE_VMPEG:
1513                 case FILE_FFMPEG:
1514                 case FILE_RAWDV:
1515                         return 1;
1516         }
1517         return 0;
1518 }
1519
1520 int File::supports_audio(int format)
1521 {
1522         switch(format)
1523         {
1524                 case FILE_AC3:
1525                 case FILE_FLAC:
1526                 case FILE_PCM:
1527                 case FILE_WAV:
1528                 case FILE_OGG:
1529                 case FILE_VORBIS:
1530                 case FILE_AMPEG:
1531                 case FILE_AU:
1532                 case FILE_AIFF:
1533                 case FILE_SND:
1534                 case FILE_FFMPEG:
1535                         return 1;
1536         }
1537         return 0;
1538 }
1539
1540 const char* File::get_tag(int format)
1541 {
1542         switch(format)
1543         {
1544                 case FILE_AC3:          return "ac3";
1545                 case FILE_AIFF:         return "aif";
1546                 case FILE_AMPEG:        return "mp3";
1547                 case FILE_AU:           return "au";
1548                 case FILE_RAWDV:        return "dv";
1549                 case FILE_DB:           return "db";
1550                 case FILE_EXR:          return "exr";
1551                 case FILE_EXR_LIST:     return "exr";
1552                 case FILE_FLAC:         return "flac";
1553                 case FILE_JPEG:         return "jpg";
1554                 case FILE_JPEG_LIST:    return "jpg";
1555                 case FILE_OGG:          return "ogg";
1556                 case FILE_PCM:          return "pcm";
1557                 case FILE_PNG:          return "png";
1558                 case FILE_PNG_LIST:     return "png";
1559                 case FILE_TGA:          return "tga";
1560                 case FILE_TGA_LIST:     return "tga";
1561                 case FILE_TIFF:         return "tif";
1562                 case FILE_TIFF_LIST:    return "tif";
1563                 case FILE_VMPEG:        return "m2v";
1564                 case FILE_VORBIS:       return "ogg";
1565                 case FILE_WAV:          return "wav";
1566                 case FILE_FFMPEG:       return "ffmpg";
1567         }
1568         return 0;
1569 }
1570
1571 const char* File::get_prefix(int format)
1572 {
1573         switch(format) {
1574         case FILE_PCM:          return "PCM";
1575         case FILE_WAV:          return "WAV";
1576         case FILE_PNG:          return "PNG";
1577         case FILE_JPEG:         return "JPEG";
1578         case FILE_TIFF:         return "TIFF";
1579         case FILE_GIF:          return "GIF";
1580         case FILE_JPEG_LIST:    return "JPEG_LIST";
1581         case FILE_AU:           return "AU";
1582         case FILE_AIFF:         return "AIFF";
1583         case FILE_SND:          return "SND";
1584         case FILE_TGA_LIST:     return "TGA_LIST";
1585         case FILE_TGA:          return "TGA";
1586         case FILE_MPEG:         return "MPEG";
1587         case FILE_AMPEG:        return "AMPEG";
1588         case FILE_VMPEG:        return "VMPEG";
1589         case FILE_RAWDV:        return "RAWDV";
1590         case FILE_TIFF_LIST:    return "TIFF_LIST";
1591         case FILE_PNG_LIST:     return "PNG_LIST";
1592         case FILE_AC3:          return "AC3";
1593         case FILE_EXR:          return "EXR";
1594         case FILE_EXR_LIST:     return "EXR_LIST";
1595         case FILE_CR2:          return "CR2";
1596         case FILE_OGG:          return "OGG";
1597         case FILE_VORBIS:       return "VORBIS";
1598         case FILE_FLAC:         return "FLAC";
1599         case FILE_FFMPEG:       return "FFMPEG";
1600         case FILE_SCENE:        return "SCENE";
1601         case FILE_CR2_LIST:     return "CR2_LIST";
1602         case FILE_GIF_LIST:     return "GIF_LIST";
1603         case FILE_DB:           return "DB";
1604         }
1605         return _("UNKNOWN");
1606 }
1607
1608
1609 PackagingEngine *File::new_packaging_engine(Asset *asset)
1610 {
1611         PackagingEngine *result;
1612         switch (asset->format)
1613         {
1614                 case FILE_OGG:
1615                         result = (PackagingEngine*)new PackagingEngineOGG();
1616                         break;
1617                 default:
1618                         result = (PackagingEngine*) new PackagingEngineDefault();
1619                         break;
1620         }
1621
1622         return result;
1623 }
1624
1625
1626 int File::record_fd()
1627 {
1628         return file ? file->record_fd() : -1;
1629 }
1630
1631
1632 void File::get_exe_path(char *result, char *bnp)
1633 {
1634 // Get executable path, basename
1635         int len = readlink("/proc/self/exe", result, BCTEXTLEN-1);
1636         if( len >= 0 ) {
1637                 result[len] = 0;
1638                 char *ptr = strrchr(result, '/');
1639                 if( ptr ) *ptr++ = 0; else ptr = result;
1640                 if( bnp ) strncpy(bnp, ptr, BCTEXTLEN);
1641         }
1642         else {
1643                 *result = 0;
1644                 if( bnp ) *bnp = 0;
1645         }
1646 }
1647
1648 void File::getenv_path(char *result, const char *path)
1649 {
1650         char *rp = result, *ep = rp + BCTEXTLEN-1;
1651         const char *cp = path;
1652 // any env var can be used here to expand a path as:
1653 //   "path...$id...": and id is a word as alpha,alnum...
1654 //   expands as "path...getenv(id)..."
1655 // CIN_PATH, CIN_LIB are set in main.C,
1656         for( int ch=*cp++; ch && rp < ep; ch=*cp++ ) {
1657                 if( ch == '$' && isalpha(*cp) ) { // scan alpha,alnum...
1658                         const char *bp = cp;  char *p = rp;
1659                         while( p < ep && *bp && (isalnum(*bp) || *bp == '_') ) *p++ = *bp++;
1660                         *p = 0;
1661                         const char *envp = getenv(rp); // read env value
1662                         if( !envp ) continue;
1663                         while( *envp && rp < ep ) *rp++ = *envp++;
1664                         cp = bp;                 // subst id=value
1665                         continue;
1666                 }
1667                 *rp++ = ch;
1668         }
1669         *rp = 0;
1670 }
1671
1672 char File::cinexe_path[BCTEXTLEN];
1673 char File::cinpkg_path[BCTEXTLEN];
1674 char File::cindat_path[BCTEXTLEN];
1675 char File::cinlib_path[BCTEXTLEN];
1676 char File::cincfg_path[BCTEXTLEN];
1677 char File::cinplg_path[BCTEXTLEN];
1678 char File::cinlad_path[BCTEXTLEN];
1679 char File::cinlcl_path[BCTEXTLEN];
1680
1681 void File::init_cin_path()
1682 {
1683         char env_path[BCTEXTLEN], env_pkg[BCTEXTLEN];
1684 // these values are advertised for forks/shell scripts
1685         get_exe_path(env_path, env_pkg);
1686         snprintf(cinexe_path, sizeof(cinexe_path), "CIN_PATH=%s", env_path);
1687         putenv(cinexe_path);
1688         snprintf(cinpkg_path, sizeof(cinpkg_path), "CIN_PKG=%s", env_pkg);
1689         putenv(cinpkg_path);
1690
1691         getenv_path(env_path, CINDAT_DIR);
1692         snprintf(cindat_path, sizeof(cindat_path), "CIN_DAT=%s", env_path);
1693         putenv(cindat_path);
1694
1695         getenv_path(env_path, CINLIB_DIR);
1696         snprintf(cinlib_path, sizeof(cinlib_path), "CIN_LIB=%s", env_path);
1697         putenv(cinlib_path);
1698
1699         getenv_path(env_path, CONFIG_DIR);
1700         snprintf(cincfg_path, sizeof(cincfg_path), "CIN_CONFIG=%s", env_path);
1701         putenv(cincfg_path);
1702
1703         getenv_path(env_path, PLUGIN_DIR);
1704         snprintf(cinplg_path, sizeof(cinplg_path), "CIN_PLUGIN=%s", env_path);
1705         putenv(cinplg_path);
1706
1707         getenv_path(env_path, LADSPA_DIR);
1708         snprintf(cinlad_path, sizeof(cinlad_path), "CIN_LADSPA=%s", env_path);
1709         putenv(cinlad_path);
1710
1711         getenv_path(env_path, LOCALE_DIR);
1712         snprintf(cinlcl_path, sizeof(cinlcl_path), "CIN_LOCALE=%s", env_path);
1713         putenv(cinlcl_path);
1714 }
1715
1716