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