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