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