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