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