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