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