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