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