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