17e555aa11e8e6221c93afda84d10e0effba9125
[goodguy/history.git] / cinelerra-5.0 / 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 "fileavi.h"
44 #include "filebase.h"
45 #include "filecr2.h"
46 #include "filedb.h"
47 #include "filedv.h"
48 #include "fileexr.h"
49 #include "fileffmpeg.h"
50 #include "fileflac.h"
51 #include "filefork.h"
52 #include "filegif.h"
53 #include "file.h"
54 #include "filejpeg.h"
55 #include "filemov.h"
56 #include "filempeg.h"
57 #undef HAVE_STDLIB_H // automake conflict
58 #include "fileogg.h"
59 #include "filepng.h"
60 #include "filescene.h"
61 #include "fileserver.h"
62 #include "filesndfile.h"
63 #include "filetga.h"
64 #include "filethread.h"
65 #include "filetiff.h"
66 #include "filevorbis.h"
67 #include "filexml.h"
68 #include "format.inc"
69 #include "formatwindow.h"
70 #include "formattools.h"
71 #include "framecache.h"
72 #include "language.h"
73 #include "mutex.h"
74 #include "mwindow.h"
75 #include "packagingengine.h"
76 #include "pluginserver.h"
77 #include "preferences.h"
78 #include "samples.h"
79 #include "stringfile.h"
80 #include "vframe.h"
81
82 //suppress noref warning
83 void *vorbis0_ov_callbacks[] = {
84  &OV_CALLBACKS_DEFAULT, &OV_CALLBACKS_NOCLOSE,
85  &OV_CALLBACKS_STREAMONLY, &OV_CALLBACKS_STREAMONLY_NOCLOSE,
86 };
87
88 File::File()
89 {
90         cpus = 1;
91         asset = new Asset;
92         format_completion = new Condition(1, "File::format_completion");
93         write_lock = new Condition(1, "File::write_lock");
94         frame_cache = new FrameCache;
95 #ifdef USE_FILEFORK
96         forked = new Mutex("File::forked",0);
97 #endif
98         reset_parameters();
99 }
100
101 File::~File()
102 {
103         if(getting_options)
104         {
105                 if(format_window) format_window->set_done(0);
106                 format_completion->lock("File::~File");
107                 format_completion->unlock();
108         }
109
110         if(temp_frame) delete temp_frame;
111
112
113         close_file(0);
114
115         asset->Garbage::remove_user();
116         delete format_completion;
117         delete write_lock;
118 #ifdef USE_FILEFORK
119         delete forked;
120 #endif
121         delete frame_cache;
122 }
123
124 void File::reset_parameters()
125 {
126 #ifdef USE_FILEFORK
127         file_fork = 0;
128         is_fork = 0;
129 #endif
130
131         file = 0;
132         audio_thread = 0;
133         video_thread = 0;
134         getting_options = 0;
135         format_window = 0;
136         temp_frame = 0;
137         current_sample = 0;
138         current_frame = 0;
139         current_channel = 0;
140         current_program = 0;
141         current_layer = 0;
142         normalized_sample = 0;
143         use_cache = 0;
144         preferences = 0;
145         playback_subtitle = -1;
146         interpolate_raw = 1;
147
148
149         temp_samples_buffer = 0;
150         temp_frame_buffer = 0;
151         current_frame_buffer = 0;
152         audio_ring_buffers = 0;
153         video_ring_buffers = 0;
154         video_buffer_size = 0;
155 }
156
157 int File::raise_window()
158 {
159         if(getting_options && format_window)
160         {
161                 format_window->raise_window();
162                 format_window->flush();
163         }
164         return 0;
165 }
166
167 void File::close_window()
168 {
169         if(getting_options)
170         {
171                 format_window->lock_window("File::close_window");
172                 format_window->set_done(1);
173                 format_window->unlock_window();
174                 getting_options = 0;
175         }
176 }
177
178 int File::get_options(FormatTools *format,
179         int audio_options, int video_options)
180 {
181         BC_WindowBase *parent_window = format->window;
182         //ArrayList<PluginServer*> *plugindb = format->plugindb;
183         Asset *asset = format->asset;
184         const char *locked_compressor = format->locked_compressor;
185
186         getting_options = 1;
187         format_completion->lock("File::get_options");
188         switch(asset->format)
189         {
190                 case FILE_AC3:
191                         FileAC3::get_parameters(parent_window,
192                                 asset,
193                                 format_window,
194                                 audio_options,
195                                 video_options);
196                         break;
197                 case FILE_RAWDV:
198                         FileDV::get_parameters(parent_window,
199                                 asset,
200                                 format_window,
201                                 audio_options,
202                                 video_options);
203                         break;
204                 case FILE_PCM:
205                 case FILE_WAV:
206                 case FILE_AU:
207                 case FILE_AIFF:
208                 case FILE_SND:
209                         FileSndFile::get_parameters(parent_window, 
210                                 asset, 
211                                 format_window, 
212                                 audio_options, 
213                                 video_options);
214                         break;
215                 case FILE_MOV:
216                         FileMOV::get_parameters(parent_window, 
217                                 asset, 
218                                 format_window, 
219                                 audio_options, 
220                                 video_options,
221                                 locked_compressor);
222                         break;
223                 case FILE_FFMPEG:
224                         FileFFMPEG::get_parameters(parent_window, 
225                                 asset, 
226                                 format_window, 
227                                 audio_options, 
228                                 video_options);
229                         break;
230                 case FILE_AMPEG:
231                 case FILE_VMPEG:
232                         FileMPEG::get_parameters(parent_window, 
233                                 asset, 
234                                 format_window, 
235                                 audio_options, 
236                                 video_options);
237                         break;
238                 case FILE_AVI:
239                         FileMOV::get_parameters(parent_window, 
240                                 asset, 
241                                 format_window, 
242                                 audio_options, 
243                                 video_options,
244                                 locked_compressor);
245                         break;
246                 case FILE_AVI_LAVTOOLS:
247                 case FILE_AVI_ARNE2:
248                 case FILE_AVI_ARNE1:
249                 case FILE_AVI_AVIFILE:
250                         FileAVI::get_parameters(parent_window, 
251                                 asset, 
252                                 format_window, 
253                                 audio_options, 
254                                 video_options,
255                                 locked_compressor);
256                         break;
257                 case FILE_JPEG:
258                 case FILE_JPEG_LIST:
259                         FileJPEG::get_parameters(parent_window, 
260                                 asset, 
261                                 format_window, 
262                                 audio_options, 
263                                 video_options);
264                         break;
265                 case FILE_EXR:
266                 case FILE_EXR_LIST:
267                         FileEXR::get_parameters(parent_window, 
268                                 asset, 
269                                 format_window, 
270                                 audio_options, 
271                                 video_options);
272                         break;
273                 case FILE_FLAC:
274                         FileFLAC::get_parameters(parent_window, 
275                                 asset, 
276                                 format_window, 
277                                 audio_options, 
278                                 video_options);
279                         break;
280                 case FILE_PNG:
281                 case FILE_PNG_LIST:
282                         FilePNG::get_parameters(parent_window, 
283                                 asset, 
284                                 format_window, 
285                                 audio_options, 
286                                 video_options);
287                         break;
288                 case FILE_TGA:
289                 case FILE_TGA_LIST:
290                         FileTGA::get_parameters(parent_window, 
291                                 asset, 
292                                 format_window, 
293                                 audio_options, 
294                                 video_options);
295                         break;
296                 case FILE_TIFF:
297                 case FILE_TIFF_LIST:
298                         FileTIFF::get_parameters(parent_window, 
299                                 asset, 
300                                 format_window, 
301                                 audio_options, 
302                                 video_options);
303                         break;
304                 case FILE_OGG:
305                         FileOGG::get_parameters(parent_window,
306                                 asset,
307                                 format_window,
308                                 audio_options,
309                                 video_options);
310                         break;
311                 default:
312                         break;
313         }
314
315         if(!format_window)
316         {
317                 ErrorBox *errorbox = new ErrorBox(PROGRAM_NAME ": Error",
318                         parent_window->get_abs_cursor_x(1),
319                         parent_window->get_abs_cursor_y(1));
320                 format_window = errorbox;
321                 getting_options = 1;
322                 if(audio_options)
323                         errorbox->create_objects(_("This format doesn't support audio."));
324                 else
325                 if(video_options)
326                         errorbox->create_objects(_("This format doesn't support video."));
327                 errorbox->run_window();
328                 delete errorbox;
329         }
330
331         getting_options = 0;
332         format_window = 0;
333         format_completion->unlock();
334         return 0;
335 }
336
337
338
339
340
341
342
343
344
345
346 int File::set_processors(int cpus)   // Set the number of cpus for certain codecs
347 {
348         if( cpus > 8 )          // mpegvideo max_threads = 16, more causes errs
349                 cpus = 8;       //  8 cpus ought to decode just about anything
350 #ifdef USE_FILEFORK
351         if(file_fork)
352         {
353                 FileForker this_is(*forked);
354                 file_fork->send_command(FileFork::SET_PROCESSORS, (unsigned char*)&cpus, sizeof(cpus));
355                 file_fork->read_result();
356         }
357 #endif
358
359 // Set all instances so gets work.
360         this->cpus = cpus;
361
362         return 0;
363 }
364
365 int File::set_preload(int64_t size)
366 {
367 #ifdef USE_FILEFORK
368         if(file_fork)
369         {
370                 FileForker this_is(*forked);
371                 file_fork->send_command(FileFork::SET_PRELOAD, (unsigned char*)&size, sizeof(size));
372                 file_fork->read_result();
373         }
374
375 #endif
376
377         this->playback_preload = size;
378         return 0;
379 }
380
381 void File::set_subtitle(int value)
382 {
383 #ifdef USE_FILEFORK
384         if(file_fork)
385         {
386                 FileForker this_is(*forked);
387                 file_fork->send_command(FileFork::SET_SUBTITLE, (unsigned char*)&value, sizeof(value));
388                 file_fork->read_result();
389         }
390
391 #endif
392         this->playback_subtitle = value;
393         if( file ) file->set_subtitle(value);
394 }
395
396 void File::set_interpolate_raw(int value)
397 {
398 #ifdef USE_FILEFORK
399         if(file_fork)
400         {
401                 FileForker this_is(*forked);
402                 file_fork->send_command(FileFork::SET_INTERPOLATE_RAW, (unsigned char*)&value, sizeof(value));
403                 file_fork->read_result();
404         }
405
406 #endif
407
408         this->interpolate_raw = value;
409 }
410
411 void File::set_white_balance_raw(int value)
412 {
413 #ifdef USE_FILEFORK
414         if(file_fork)
415         {
416                 FileForker this_is(*forked);
417                 file_fork->send_command(FileFork::SET_WHITE_BALANCE_RAW, (unsigned char*)&value, sizeof(value));
418                 file_fork->read_result();
419         }
420 #endif
421
422         this->white_balance_raw = value;
423 }
424
425
426 void File::set_cache_frames(int value)
427 {
428 // caching only done locally
429         if(!video_thread)
430                 use_cache = value;
431 }
432
433 int File::purge_cache()
434 {
435 // caching only done locally
436         int result = 0;
437         if( frame_cache->cache_items() > 0 )
438         {
439                 frame_cache->remove_all();
440                 result = 1;
441         }
442         return result;
443 }
444
445 int File::delete_oldest()
446 {
447 // caching only done locally
448         return frame_cache->delete_oldest();
449 }
450
451
452
453
454
455
456
457
458
459
460
461 int File::open_file(Preferences *preferences, 
462         Asset *asset, 
463         int rd, 
464         int wr)
465 {
466         int result = 0;
467         const int debug = 0;
468
469         this->preferences = preferences;
470         this->asset->copy_from(asset, 1);
471         this->rd = rd;
472         this->wr = wr;
473         file = 0;
474
475         if(debug) printf("File::open_file %d\n", __LINE__);
476
477 #ifdef USE_FILEFORK
478         if( !is_fork && MWindow::file_server && (rd || wr) ) {
479                 FileForker this_is(*forked);
480 // printf("File::open_file %d file_server=%p rd=%d wr=%d %d\n", 
481 // __LINE__, 
482 // MWindow::file_server,
483 // rd, 
484 // wr, 
485 // asset->ms_quantization);
486                 file_fork = MWindow::file_server->new_filefork();
487 //printf("File::open_file %d\n", __LINE__);
488
489 // Send the asset
490 // Convert to hash table
491                 BC_Hash table;
492                 asset->save_defaults(&table, "", 1, 1, 1, 1, 1);
493 // Convert to string
494                 char *string = 0;
495                 table.save_string(string);
496                 int buffer_size = sizeof(int) * 7 + strlen(string) + 1;
497                 unsigned char *buffer = new unsigned char[buffer_size];
498                 int offset = 0;
499                 *(int*)(buffer + offset) = rd;
500                 offset += sizeof(int);
501                 *(int*)(buffer + offset) = wr;
502                 offset += sizeof(int);
503                 *(int*)(buffer + offset) = cpus;
504                 offset += sizeof(int);
505                 *(int*)(buffer + offset) = white_balance_raw;
506                 offset += sizeof(int);
507                 *(int*)(buffer + offset) = interpolate_raw;
508                 offset += sizeof(int);
509                 *(int*)(buffer + offset) = playback_subtitle;
510                 offset += sizeof(int);
511                 *(int*)(buffer + offset) = current_program;
512                 offset += sizeof(int);
513                 memcpy(buffer + offset, string, strlen(string) + 1);
514 //printf("File::open_file %d\n", __LINE__);
515                 file_fork->send_command(FileFork::OPEN_FILE, 
516                         buffer, 
517                         buffer_size);
518                 delete [] buffer;
519                 delete [] string;
520 //printf("File::open_file %d\n", __LINE__);
521
522 // Get the updated asset from the fork
523                 result = file_fork->read_result();
524 //printf("File::open_file %d\n", __LINE__);
525                 if(!result)
526                 {
527                         table.load_string((char*)file_fork->result_data);
528
529                         asset->load_defaults(&table, "", 1, 1, 1, 1, 1);
530                         this->asset->load_defaults(&table, "", 1, 1, 1, 1, 1);
531 //this->asset->dump();
532                 }
533 //printf("File::open_file %d\n", __LINE__);
534
535
536 // If it's a scene renderer, close it & reopen it locally to get the 
537 // full OpenGL support.
538 // Just doing 2D for now.  Should be forked in case Festival crashes.
539 //              if(rd && this->asset->format == FILE_SCENE)
540 //              {
541 // //printf("File::open_file %p %d\n", this, __LINE__);
542 //                      close_file(0);
543 // // Lie to get it to work properly
544 //                      is_fork = 1;
545 //              }
546 //              else
547                 {
548                         return result;
549                 }
550         }
551 #endif
552
553
554         if(debug) printf("File::open_file %p %d\n", this, __LINE__);
555
556         switch(this->asset->format)
557         {
558 // get the format now
559 // If you add another format to case 0, you also need to add another case for the
560 // file format #define.
561                 case FILE_UNKNOWN:
562                         if(FileDB::check_sig(this->asset)) {
563 // MediaDb file
564                                 file = new FileDB(this->asset, this);
565                                 break;
566                         }
567 // if early probe enabled
568                         if( preferences->ffmpeg_early_probe &&
569                             FileFFMPEG::check_sig(this->asset)) {
570                                 file = new FileFFMPEG(this->asset, this);
571                                 break;
572                         }
573
574                         FILE *stream;
575                         if(!(stream = fopen(this->asset->path, "rb"))) {
576 // file not found
577                                 return 1;
578                         }
579
580                         char test[16];
581                         result = fread(test, 16, 1, stream);
582
583                         if(FileScene::check_sig(this->asset, test)) {
584 // libsndfile
585                                 fclose(stream);
586                                 file = new FileScene(this->asset, this);
587                                 break;
588                         }
589                         if(FileDV::check_sig(this->asset)) {
590 // libdv
591                                 fclose(stream);
592                                 file = new FileDV(this->asset, this);
593                                 break;
594                         }
595                         if(FileSndFile::check_sig(this->asset)) {
596 // libsndfile
597                                 fclose(stream);
598                                 file = new FileSndFile(this->asset, this);
599                                 break;
600                         }
601                         if(FilePNG::check_sig(this->asset)) {
602 // PNG file
603                                 fclose(stream);
604                                 file = new FilePNG(this->asset, this);
605                                 break;
606                         }
607                         if(FileJPEG::check_sig(this->asset)) {
608 // JPEG file
609                                 fclose(stream);
610                                 file = new FileJPEG(this->asset, this);
611                                 break;
612                         }
613                         if(FileGIF::check_sig(this->asset)) {
614 // GIF file
615                                 fclose(stream);
616                                 file = new FileGIF(this->asset, this);
617                                 break;
618                         }
619                         if(FileEXR::check_sig(this->asset, test)) {
620 // EXR file
621                                 fclose(stream);
622                                 file = new FileEXR(this->asset, this);
623                                 break;
624                         }
625                         if(FileFLAC::check_sig(this->asset, test)) {
626 // FLAC file
627                                 fclose(stream);
628                                 file = new FileFLAC(this->asset, this);
629                                 break;
630                         }
631                         if(FileCR2::check_sig(this->asset)) {
632 // CR2 file
633                                 fclose(stream);
634                                 file = new FileCR2(this->asset, this);
635                                 break;
636                         }
637                         if(FileTGA::check_sig(this->asset)) {
638 // TGA file
639                                 fclose(stream);
640                                 file = new FileTGA(this->asset, this);
641                                 break;
642                         }
643                         if(FileTIFF::check_sig(this->asset)) {
644 // TIFF file
645                                 fclose(stream);
646                                 file = new FileTIFF(this->asset, this);
647                                 break;
648                         }
649                         if(FileOGG::check_sig(this->asset)) {
650 // OGG file
651                                 fclose(stream);
652                                 file = new FileOGG(this->asset, this);
653                                 break;
654                         }
655                         if(FileVorbis::check_sig(this->asset)) {
656 // VorbisFile file
657                                 fclose(stream);
658                                 file = new FileVorbis(this->asset, this);
659                                 break;
660                         }
661                         if(FileMPEG::check_sig(this->asset)) {
662 // MPEG file
663                                 fclose(stream);
664                                 file = new FileMPEG(this->asset, this);
665                                 break;
666                         }
667                         if( test[0] == '<' && (
668                                 !strncmp(&test[1],"EDL>",4) ||
669                                 !strncmp(&test[1],"HTAL>",5) ||
670                                 !strncmp(&test[1],"?xml",4) ) ) {
671 // XML file
672                                 fclose(stream);
673                                 return FILE_IS_XML;
674                         }    // can't load project file
675                         if(FileMOV::check_sig(this->asset)) {
676 // MOV file
677 // should be last because quicktime lacks a magic number
678                                 fclose(stream);
679                                 file = new FileMOV(this->asset, this);
680                                 break;
681                         }
682                         if( !preferences->ffmpeg_early_probe &&
683                             FileFFMPEG::check_sig(this->asset) ) {
684                                 fclose(stream);
685                                 file = new FileFFMPEG(this->asset, this);
686                                 break;
687                         }
688 // PCM file
689                         fclose(stream);
690                         return FILE_UNRECOGNIZED_CODEC;
691                         break;
692
693 // format already determined
694                 case FILE_AC3:
695                         file = new FileAC3(this->asset, this);
696                         break;
697
698                 case FILE_SCENE:
699                         file = new FileScene(this->asset, this);
700                         break;
701
702                 case FILE_FFMPEG:
703                         file = new FileFFMPEG(this->asset, this);
704                         break;
705
706                 case FILE_PCM:
707                 case FILE_WAV:
708                 case FILE_AU:
709                 case FILE_AIFF:
710                 case FILE_SND:
711 //printf("File::open_file 1\n");
712                         file = new FileSndFile(this->asset, this);
713                         break;
714
715                 case FILE_PNG:
716                 case FILE_PNG_LIST:
717                         file = new FilePNG(this->asset, this);
718                         break;
719
720                 case FILE_JPEG:
721                 case FILE_JPEG_LIST:
722                         file = new FileJPEG(this->asset, this);
723                         break;
724
725                 case FILE_GIF:
726                 case FILE_GIF_LIST:
727                         file = new FileGIF(this->asset, this);
728                         break;
729
730                 case FILE_EXR:
731                 case FILE_EXR_LIST:
732                         file = new FileEXR(this->asset, this);
733                         break;
734
735                 case FILE_FLAC:
736                         file = new FileFLAC(this->asset, this);
737                         break;
738
739                 case FILE_CR2:
740                 case FILE_CR2_LIST:
741                         file = new FileCR2(this->asset, this);
742                         break;
743
744                 case FILE_TGA_LIST:
745                 case FILE_TGA:
746                         file = new FileTGA(this->asset, this);
747                         break;
748
749                 case FILE_TIFF:
750                 case FILE_TIFF_LIST:
751                         file = new FileTIFF(this->asset, this);
752                         break;
753
754                 case FILE_DB:
755                         file = new FileDB(this->asset, this);
756                         break;
757
758                 case FILE_MOV:
759                         file = new FileMOV(this->asset, this);
760                         break;
761
762                 case FILE_MPEG:
763                 case FILE_AMPEG:
764                 case FILE_VMPEG:
765                         file = new FileMPEG(this->asset, this);
766                         break;
767
768                 case FILE_OGG:
769                         file = new FileOGG(this->asset, this);
770                         break;
771
772                 case FILE_VORBIS:
773                         file = new FileVorbis(this->asset, this);
774                         break;
775
776                 case FILE_AVI:
777                         file = new FileMOV(this->asset, this);
778                         break;
779
780                 case FILE_AVI_LAVTOOLS:
781                 case FILE_AVI_ARNE2:
782                 case FILE_AVI_ARNE1:
783                 case FILE_AVI_AVIFILE:
784                         file = new FileAVI(this->asset, this);
785                         break;
786
787                 case FILE_RAWDV:
788                         file = new FileDV(this->asset, this);
789                         break;
790
791 // try plugins
792                 default:
793                         return 1;
794                         break;
795         }
796
797
798 // Reopen file with correct parser and get header.
799         if(file->open_file(rd, wr)) {
800                 delete file;
801                 return FILE_NOT_FOUND;
802         }
803
804
805
806 // Set extra writing parameters to mandatory settings.
807         if( wr ) {
808                 if(this->asset->dither) file->set_dither();
809         }
810
811         if( rd ) {
812 // one frame image file, no specific length
813                 if( !this->asset->audio_data && this->asset->video_data &&
814                     this->asset->video_length <= 1 )
815                         this->asset->video_length = -1;
816         }
817
818 // Synchronize header parameters
819         asset->copy_from(this->asset, 1);
820 //asset->dump();
821
822         if(debug) printf("File::open_file %d file=%p\n", __LINE__, file);
823 // sleep(1);
824
825         return FILE_OK;
826 }
827
828 void File::delete_temp_samples_buffer()
829 {
830
831         if(temp_samples_buffer) {
832                 for(int j = 0; j < audio_ring_buffers; j++) {
833                         for(int i = 0; i < asset->channels; i++) {
834                                 delete temp_samples_buffer[j][i];
835                         }
836                         delete [] temp_samples_buffer[j];
837                 }
838
839                 delete [] temp_samples_buffer;
840                 temp_samples_buffer = 0;
841                 audio_ring_buffers = 0;
842         }
843 }
844
845 void File::delete_temp_frame_buffer()
846 {
847         
848         if(temp_frame_buffer) {
849                 for(int k = 0; k < video_ring_buffers; k++) {
850                         for(int i = 0; i < asset->layers; i++) {
851                                 for(int j = 0; j < video_buffer_size; j++) {
852                                         delete temp_frame_buffer[k][i][j];
853                                 }
854                                 delete [] temp_frame_buffer[k][i];
855                         }
856                         delete [] temp_frame_buffer[k];
857                 }
858
859                 delete [] temp_frame_buffer;
860                 temp_frame_buffer = 0;
861                 video_ring_buffers = 0;
862                 video_buffer_size = 0;
863         }
864 }
865
866 int File::close_file(int ignore_thread)
867 {
868         const int debug = 0;
869
870 #ifdef USE_FILEFORK
871         if(debug) printf("File::close_file file=%p file_fork=%p %d\n", file, file_fork, __LINE__);
872
873         if(file_fork) {
874                 FileForker this_is(*forked);
875                 file_fork->send_command(FileFork::CLOSE_FILE, 0, 0);
876                 file_fork->read_result();
877
878                 if(asset && wr) {
879                         asset->audio_length = current_sample = *(int64_t*)file_fork->result_data;
880                         asset->video_length = current_frame = *(int64_t*)(file_fork->result_data + sizeof(int64_t));
881                 }
882
883                 if(debug) printf("File::close_file:%d current_sample=" _LD " current_frame=" _LD "\n", 
884                         __LINE__,
885                         current_sample,
886                         current_frame);
887
888                 delete file_fork;
889                 file_fork = 0;
890         }
891 #endif
892
893         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
894
895         if(!ignore_thread) {
896                 stop_audio_thread();
897                 stop_video_thread();
898         }
899
900
901         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
902         if(file) {
903 // The file's asset is a copy of the argument passed to open_file so the
904 // user must copy lengths from the file's asset.
905                 if(asset && wr) {
906                         asset->audio_length = current_sample;
907                         asset->video_length = current_frame;
908                 }
909
910                 file->close_file();
911                 delete file;
912         }
913         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
914
915         delete_temp_samples_buffer();
916         delete_temp_frame_buffer();
917         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
918
919         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
920
921         reset_parameters();
922         if(debug) printf("File::close_file file=%p %d\n", file, __LINE__);
923         return 0;
924 }
925
926
927
928 int File::get_index(char *index_path)
929 {
930 #ifdef USE_FILEFORK
931         if(file_fork) {
932                 FileForker this_is(*forked);
933                 file_fork->send_command(FileFork::GET_INDEX, (unsigned char*)index_path, strlen(index_path) + 1);
934                 int result = file_fork->read_result();
935                 return result;
936         }
937 #endif
938
939         if(file) {
940                 return file->get_index(index_path);
941         }
942         return 1;
943 }
944
945
946
947 int File::start_audio_thread(int buffer_size, int ring_buffers)
948 {
949         this->audio_ring_buffers = ring_buffers;
950
951 #ifdef USE_FILEFORK
952         if(file_fork)
953         {
954                 FileForker this_is(*forked);
955                 unsigned char buffer[sizeof(int) * 2];
956                 int *ibfr = (int *)buffer;
957                 ibfr[0] = buffer_size;
958                 ibfr[1] = audio_ring_buffers;
959                 file_fork->send_command(FileFork::START_AUDIO_THREAD, buffer, sizeof(buffer));
960                 int result = file_fork->read_result();
961
962
963 //printf("File::start_audio_thread %d file_fork->result_data=%p\n", __LINE__, file_fork->result_data);
964 // Create server copy of buffer
965                 delete_temp_samples_buffer();
966 //printf("File::start_audio_thread %d\n", __LINE__);
967                 temp_samples_buffer = new Samples**[audio_ring_buffers];
968 //printf("File::start_audio_thread %d\n", __LINE__);
969                 for(int i = 0; i < audio_ring_buffers; i++)
970                 {
971 //printf("File::start_audio_thread %d\n", __LINE__);
972                         temp_samples_buffer[i] = new Samples*[asset->channels];
973 //printf("File::start_audio_thread %d\n", __LINE__);
974                         for(int j = 0; j < asset->channels; j++)
975                         {
976                                 int offset = i * Samples::filefork_size() * asset->channels +
977                                         j * Samples::filefork_size();
978 //printf("File::start_audio_thread %d j=%d offset=%d\n", __LINE__, j, offset);
979                                 temp_samples_buffer[i][j] = new Samples;
980                                 temp_samples_buffer[i][j]->from_filefork(
981                                         file_fork->result_data +
982                                         offset);
983 //printf("File::start_audio_thread %d\n", __LINE__);
984                         }
985                 }
986                 
987                 return result;
988         }
989 #endif
990
991         
992         if(!audio_thread)
993         {
994                 audio_thread = new FileThread(this, 1, 0);
995                 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
996         }
997         return 0;
998 }
999
1000 int File::start_video_thread(int buffer_size, 
1001         int color_model, 
1002         int ring_buffers, 
1003         int compressed)
1004 {
1005         this->video_ring_buffers = ring_buffers;
1006         this->video_buffer_size = buffer_size;
1007
1008 #ifdef USE_FILEFORK
1009         if(file_fork)
1010         {
1011                 FileForker this_is(*forked);
1012 // This resets variables
1013                 delete_temp_frame_buffer();
1014
1015                 this->video_ring_buffers = ring_buffers;
1016                 this->video_buffer_size = buffer_size;
1017
1018                 unsigned char buffer[sizeof(int) * 4];
1019                 int *ibfr = (int *)buffer;
1020                 ibfr[0] = buffer_size;
1021                 ibfr[1] = color_model;
1022                 ibfr[2] = video_ring_buffers;
1023                 ibfr[3] = compressed;
1024 // Buffers are allocated
1025                 file_fork->send_command(FileFork::START_VIDEO_THREAD, 
1026                         buffer, 
1027                         sizeof(buffer));
1028                 int result = file_fork->read_result();
1029
1030
1031 // Create server copy of buffer
1032 //printf("File::start_video_thread %d %d\n", __LINE__, video_ring_buffers);
1033                 temp_frame_buffer = new VFrame***[video_ring_buffers];
1034                 for(int i = 0; i < video_ring_buffers; i++)
1035                 {
1036                         temp_frame_buffer[i] = new VFrame**[asset->layers];
1037                         for(int j = 0; j < asset->layers; j++)
1038                         {
1039                                 temp_frame_buffer[i][j] = new VFrame*[video_buffer_size];
1040 //printf("File::start_video_thread %d %p\n", __LINE__, temp_frame_buffer[i][j]);
1041                                 for(int k = 0; k < video_buffer_size; k++)
1042                                 {
1043                                         temp_frame_buffer[i][j][k] = new VFrame;
1044                                         temp_frame_buffer[i][j][k]->from_filefork(file_fork->result_data + 
1045                                                 i * asset->layers * video_buffer_size * VFrame::filefork_size() + 
1046                                                 j * video_buffer_size * VFrame::filefork_size() +
1047                                                 k * VFrame::filefork_size());
1048                                 }
1049                         }
1050                 }
1051
1052
1053                 return result;
1054         }
1055 #endif
1056
1057
1058
1059         if(!video_thread)
1060         {
1061                 video_thread = new FileThread(this, 0, 1);
1062                 video_thread->start_writing(buffer_size, 
1063                         color_model, 
1064                         ring_buffers, 
1065                         compressed);
1066         }
1067         return 0;
1068 }
1069
1070 int File::start_video_decode_thread()
1071 {
1072 #ifdef USE_FILEFORK
1073         if(file_fork)
1074         {
1075                 FileForker this_is(*forked);
1076                 file_fork->send_command(FileFork::START_VIDEO_DECODE_THREAD, 0, 0);
1077                 file_fork->read_result();
1078                 return 0;
1079         }
1080 #endif
1081
1082
1083 // Currently, CR2 is the only one which won't work asynchronously, so
1084 // we're not using a virtual function yet.
1085         if(!video_thread /* && asset->format != FILE_CR2 */)
1086         {
1087                 video_thread = new FileThread(this, 0, 1);
1088                 video_thread->start_reading();
1089                 use_cache = 0;
1090         }
1091         return 0;
1092 }
1093
1094 int File::stop_audio_thread()
1095 {
1096 #ifdef USE_FILEFORK
1097         if(file_fork)
1098         {
1099                 file_fork->send_command(FileFork::STOP_AUDIO_THREAD, 0, 0);
1100                 file_fork->read_result();
1101                 return 0;
1102         }
1103 #endif
1104
1105         if(audio_thread)
1106         {
1107                 audio_thread->stop_writing();
1108                 delete audio_thread;
1109                 audio_thread = 0;
1110         }
1111         return 0;
1112 }
1113
1114 int File::stop_video_thread()
1115 {
1116 #ifdef USE_FILEFORK
1117         if(file_fork)
1118         {
1119                 FileForker this_is(*forked);
1120                 file_fork->send_command(FileFork::STOP_VIDEO_THREAD, 0, 0);
1121                 file_fork->read_result();
1122                 return 0;
1123         }
1124 #endif
1125
1126         if(video_thread)
1127         {
1128                 video_thread->stop_reading();
1129                 video_thread->stop_writing();
1130                 delete video_thread;
1131                 video_thread = 0;
1132         }
1133         return 0;
1134 }
1135
1136 FileThread* File::get_video_thread()
1137 {
1138         return video_thread;
1139 }
1140
1141 int File::set_channel(int channel) 
1142 {
1143 #ifdef USE_FILEFORK
1144         if(file_fork)
1145         {
1146                 FileForker this_is(*forked);
1147 // Set it locally for get_channel
1148                 current_channel = channel;
1149                 file_fork->send_command(FileFork::SET_CHANNEL, (unsigned char*)&channel, sizeof(channel));
1150                 int result = file_fork->read_result();
1151                 return result;
1152         }
1153 #endif
1154
1155         if(file && channel < asset->channels)
1156         {
1157                 current_channel = channel;
1158                 return 0;
1159         }
1160         else
1161                 return 1;
1162 }
1163
1164 int File::get_channel()
1165 {
1166         return current_channel;
1167 }
1168
1169 // if no>=0, sets new program
1170 //  returns current program
1171 int File::set_program(int no)
1172 {
1173 #ifdef USE_FILEFORK
1174         if(file_fork)
1175         {
1176                 FileForker this_is(*forked);
1177                 file_fork->send_command(FileFork::SET_PROGRAM, (unsigned char*)&no, sizeof(no));
1178                 int result = file_fork->read_result();
1179                 current_program = no < 0 ? result : no;
1180                 return result;
1181         }
1182 #endif
1183         int result = file ? file->set_program(no) : current_program;
1184         current_program = no < 0 ? result : no;
1185         return result;
1186 }
1187
1188 int File::get_cell_time(int no, double &time)
1189 {
1190 #ifdef USE_FILEFORK
1191         if(file_fork)
1192         {
1193                 FileForker this_is(*forked);
1194                 file_fork->send_command(FileFork::GET_CELL_TIME, (unsigned char*)&no, sizeof(no));
1195                 int result = file_fork->read_result();
1196                 time = *(double *)file_fork->result_data;
1197                 return result;
1198         }
1199 #endif
1200
1201         return file ? file->get_cell_time(no, time) : -1;
1202 }
1203
1204 int File::get_system_time(int64_t &tm)
1205 {
1206 #ifdef USE_FILEFORK
1207         if(file_fork)
1208         {
1209                 FileForker this_is(*forked);
1210                 file_fork->send_command(FileFork::GET_STT_TIME, 0, 0);
1211                 int result = file_fork->read_result();
1212                 tm = *(int64_t *)file_fork->result_data;
1213                 return result;
1214         }
1215 #endif
1216
1217         return file ? file->get_system_time(tm) : -1;
1218 }
1219
1220 int File::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
1221 {
1222 #ifdef USE_FILEFORK
1223         if(file_fork)
1224         {
1225                 FileForker this_is(*forked);
1226                 unsigned char buffer[2*sizeof(int)];
1227                 int offset = 0;
1228                 *(int*)(buffer + offset) = vstream;
1229                 offset += sizeof(int);
1230                 *(int*)(buffer + offset) = astream;
1231                 file_fork->send_command(FileFork::GET_AUDIO4VIDEO, buffer, sizeof(buffer));
1232                 int result = file_fork->read_result();
1233                 channel_mask = *(int64_t *)file_fork->result_data;
1234                 return result;
1235         }
1236 #endif
1237
1238         return file ? file->get_audio_for_video(vstream, astream, channel_mask) : -1;
1239 }
1240
1241 int File::get_video_pid(int track)
1242 {
1243 #ifdef USE_FILEFORK
1244         if(file_fork)
1245         {
1246                 FileForker this_is(*forked);
1247                 file_fork->send_command(FileFork::GET_VIDEO_PID,
1248                                 (unsigned char*)&track, sizeof(track));
1249                 int result = file_fork->read_result();
1250                 return result;
1251         }
1252 #endif
1253
1254         return file ? file->get_video_pid(track) : -1;
1255 }
1256
1257
1258
1259 int File::get_video_info(int track, int &pid, double &framerate,
1260                 int &width, int &height, char *title)
1261 {
1262 #ifdef USE_FILEFORK
1263         if(file_fork)
1264         {
1265                 FileForker this_is(*forked);
1266                 file_fork->send_command(FileFork::GET_VIDEO_INFO,
1267                                 (unsigned char*)&track, sizeof(track));
1268                 int result = file_fork->read_result();
1269                 if( !result ) {
1270                         unsigned char *bp = file_fork->result_data;
1271                         framerate = *(double*)bp;  bp += sizeof(framerate);
1272                         pid       = *(int *)  bp;  bp += sizeof(pid);
1273                         width     = *(int *)  bp;  bp += sizeof(width);
1274                         height    = *(int *)  bp;  bp += sizeof(height);
1275                         strcpy(title, (char *)bp);
1276                 }
1277                 return result;
1278         }
1279 #endif
1280
1281         return !file ? -1 :
1282                  file->get_video_info(track, pid, framerate, width, height, title);
1283 }
1284
1285 int File::select_video_stream(Asset *asset, int vstream)
1286 {
1287 #ifdef USE_FILEFORK
1288         if(file_fork)
1289         {
1290                 FileForker this_is(*forked);
1291                 file_fork->send_command(FileFork::SELECT_VIDEO_STREAM,
1292                                 (unsigned char*)&vstream, sizeof(vstream));
1293                 int result = file_fork->read_result();
1294                 if( !result ) {
1295                         unsigned char *bp = file_fork->result_data;
1296                         asset->frame_rate    = *(double*)  bp;  bp += sizeof(asset->frame_rate);
1297                         asset->video_length = *(int64_t *) bp;  bp += sizeof(asset->video_length);
1298                         asset->width        = *(int *)     bp;  bp += sizeof(asset->width);
1299                         asset->height       = *(int *)     bp;  bp += sizeof(asset->height);
1300                 }
1301         }
1302 #endif
1303         return !file ? -1 :
1304                  file->select_video_stream(asset, vstream);
1305 }
1306
1307 int File::select_audio_stream(Asset *asset, int astream)
1308 {
1309 #ifdef USE_FILEFORK
1310         if(file_fork)
1311         {
1312                 FileForker this_is(*forked);
1313                 file_fork->send_command(FileFork::SELECT_AUDIO_STREAM,
1314                                 (unsigned char*)&astream, sizeof(astream));
1315                 int result = file_fork->read_result();
1316                 if( !result ) {
1317                         unsigned char *bp = file_fork->result_data;
1318                         asset->audio_length = *(int64_t *) bp;  bp += sizeof(asset->audio_length);
1319                         asset->sample_rate  = *(int *)     bp;  bp += sizeof(asset->sample_rate);
1320                 }
1321         }
1322 #endif
1323         return !file ? -1 :
1324                  file->select_audio_stream(asset, astream);
1325 }
1326
1327
1328 int File::get_thumbnail(int stream,
1329         int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
1330 {
1331         return file->get_thumbnail(stream, position, thumbnail, ww, hh);
1332 }
1333
1334 int File::set_skimming(int track, int skim, skim_fn fn, void *vp)
1335 {
1336         return file->set_skimming(track, skim, fn, vp);
1337 }
1338
1339 int File::skim_video(int track, void *vp, skim_fn fn)
1340 {
1341         return file->skim_video(track, vp, fn);
1342 }
1343
1344
1345 int File::set_layer(int layer, int is_thread) 
1346 {
1347 #ifdef USE_FILEFORK
1348 // thread should only call in the fork
1349         if(file_fork && !is_fork && !is_thread)
1350         {
1351                 FileForker this_is(*forked);
1352                 file_fork->send_command(FileFork::SET_LAYER, (unsigned char*)&layer, sizeof(layer));
1353                 int result = file_fork->read_result();
1354                 current_layer = layer;
1355                 return result;
1356         }
1357 #endif
1358
1359         if(file && layer < asset->layers)
1360         {
1361                 if(!is_thread && video_thread)
1362                 {
1363                         video_thread->set_layer(layer);
1364                 }
1365                 else
1366                 {
1367                         current_layer = layer;
1368                 }
1369                 return 0; 
1370         }
1371         else
1372                 return 1;
1373 }
1374
1375 int64_t File::get_audio_length()
1376 {
1377 #ifdef USE_FILEFORK
1378         if(file_fork)
1379         {
1380                 FileForker this_is(*forked);
1381                 file_fork->send_command(FileFork::GET_AUDIO_LENGTH, 0, 0);
1382                 int64_t result = file_fork->read_result();
1383                 return result;
1384         }
1385 #endif
1386
1387         int64_t result = asset->audio_length;
1388         int64_t base_samplerate = -1;
1389         if(result > 0)
1390         {
1391                 if(base_samplerate > 0)
1392                         return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
1393                 else
1394                         return result;
1395         }
1396         else
1397                 return -1;
1398 }
1399
1400 int64_t File::get_video_length()
1401
1402 #ifdef USE_FILEFORK
1403         if(file_fork)
1404         {
1405                 FileForker this_is(*forked);
1406                 file_fork->send_command(FileFork::GET_VIDEO_LENGTH, 0, 0);
1407                 int64_t result = file_fork->read_result();
1408                 return result;
1409         }
1410 #endif
1411
1412
1413         int64_t result = asset->video_length;
1414         float base_framerate = -1;
1415         if(result > 0)
1416         {
1417                 if(base_framerate > 0)
1418                         return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5); 
1419                 else
1420                         return result;
1421         }
1422         else
1423                 return -1;  // infinity
1424 }
1425
1426
1427 int64_t File::get_video_position() 
1428 {
1429 #ifdef USE_FILEFORK
1430         if(file_fork)
1431         {
1432                 FileForker this_is(*forked);
1433                 file_fork->send_command(FileFork::GET_VIDEO_POSITION, 0, 0);
1434                 int64_t result = file_fork->read_result();
1435                 return result;
1436         }
1437 #endif
1438
1439         float base_framerate = -1;
1440         if(base_framerate > 0)
1441                 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
1442         else
1443                 return current_frame;
1444 }
1445
1446 int64_t File::get_audio_position() 
1447 {
1448 #ifdef USE_FILEFORK
1449         if(file_fork)
1450         {
1451                 FileForker this_is(*forked);
1452                 file_fork->send_command(FileFork::GET_AUDIO_POSITION, 0, 0);
1453                 int64_t result = file_fork->read_result();
1454                 return result;
1455         }
1456 #endif
1457
1458
1459 //      int64_t base_samplerate = -1;
1460 //      if(base_samplerate > 0)
1461 //      {
1462 //              if(normalized_sample_rate == base_samplerate)
1463 //                      return normalized_sample;
1464 //              else
1465 //                      return (int64_t)((double)current_sample / 
1466 //                              asset->sample_rate * 
1467 //                              base_samplerate + 
1468 //                              0.5);
1469 //      }
1470 //      else
1471                 return current_sample;
1472 }
1473
1474
1475
1476 int File::set_audio_position(int64_t position) 
1477 {
1478 #ifdef USE_FILEFORK
1479         if(file_fork)
1480         {
1481                 FileForker this_is(*forked);
1482                 file_fork->send_command(FileFork::SET_AUDIO_POSITION, 
1483                         (unsigned char*)&position, 
1484                         sizeof(position));
1485                 int result = file_fork->read_result();
1486                 return result;
1487         }
1488 #endif
1489
1490         int result = 0;
1491
1492         if(!file) return 1;
1493
1494 #define REPOSITION(x, y) \
1495         (labs((x) - (y)) > 1)
1496
1497         float base_samplerate = asset->sample_rate;
1498                 current_sample = normalized_sample = position;
1499
1500 // printf("File::set_audio_position %d normalized_sample=%ld\n", 
1501 // __LINE__, 
1502 // normalized_sample);
1503                 result = file->set_audio_position(current_sample);
1504
1505                 if(result)
1506                         printf("File::set_audio_position position=" _LD
1507                                 " base_samplerate=%f asset=%p asset->sample_rate=%d\n",
1508                                 position, base_samplerate, asset, asset->sample_rate);
1509 //      }
1510
1511 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
1512
1513         return result;
1514 }
1515
1516 int File::set_video_position(int64_t position, 
1517         int is_thread) 
1518 {
1519 #ifdef USE_FILEFORK
1520 // Thread should only call in the fork
1521         if(file_fork && !is_fork && !is_thread)
1522         {
1523                 FileForker this_is(*forked);
1524 //printf("File::set_video_position %d %lld\n", __LINE__, position);
1525                 file_fork->send_command(FileFork::SET_VIDEO_POSITION, (unsigned char*)&position, sizeof(position));
1526                 int result = file_fork->read_result();
1527                 return result;
1528         }
1529 #endif
1530
1531         int result = 0;
1532         if(!file) return 0;
1533
1534 // Convert to file's rate
1535 //      if(base_framerate > 0)
1536 //              position = (int64_t)((double)position / 
1537 //                      base_framerate * 
1538 //                      asset->frame_rate + 
1539 //                      0.5);
1540
1541
1542         if(video_thread && !is_thread)
1543         {
1544 // Call thread.  Thread calls this again to set the file state.
1545                 video_thread->set_video_position(position);
1546         }
1547         else
1548         if(current_frame != position)
1549         {
1550                 if(file)
1551                 {
1552                         current_frame = position;
1553                         result = file->set_video_position(current_frame);
1554                 }
1555         }
1556
1557         return result;
1558 }
1559
1560 // No resampling here.
1561 int File::write_samples(Samples **buffer, int64_t len)
1562 {
1563 #ifdef USE_FILEFORK
1564         if(file_fork)
1565         {
1566                 FileForker this_is(*forked);
1567                 int entry_size = Samples::filefork_size();
1568                 int buffer_size = entry_size * asset->channels + sizeof(int64_t);
1569                 unsigned char fork_buffer[buffer_size];
1570                 for(int i = 0; i < asset->channels; i++)
1571                 {
1572                         buffer[i]->to_filefork(fork_buffer + entry_size * i);
1573                 }
1574
1575                 *(int64_t*)(fork_buffer + 
1576                         entry_size * asset->channels) = len;
1577
1578                 file_fork->send_command(FileFork::WRITE_SAMPLES, 
1579                         fork_buffer, 
1580                         buffer_size);
1581                 int result = file_fork->read_result();
1582                 return result;
1583         }
1584 #endif
1585
1586
1587
1588
1589         int result = 1;
1590
1591         if(file)
1592         {
1593                 write_lock->lock("File::write_samples");
1594
1595 // Convert to arrays for backwards compatability
1596                 double *temp[asset->channels];
1597                 for(int i = 0; i < asset->channels; i++)
1598                 {
1599                         temp[i] = buffer[i]->get_data();
1600                 }
1601
1602                 result = file->write_samples(temp, len);
1603                 current_sample += len;
1604                 normalized_sample += len;
1605                 asset->audio_length += len;
1606                 write_lock->unlock();
1607         }
1608         return result;
1609 }
1610
1611
1612
1613
1614
1615 // Can't put any cmodel abstraction here because the filebase couldn't be
1616 // parallel.
1617 int File::write_frames(VFrame ***frames, int len)
1618 {
1619 //printf("File::write_frames %d\n", __LINE__);
1620 #ifdef USE_FILEFORK
1621         if(file_fork)
1622         {
1623                 FileForker this_is(*forked);
1624 //printf("File::write_frames %d\n", __LINE__);
1625                 int entry_size = frames[0][0]->filefork_size();
1626                 unsigned char fork_buffer[entry_size * asset->layers * len + sizeof(int)];
1627                 for(int i = 0; i < asset->layers; i++)
1628                 {
1629                         for(int j = 0; j < len; j++)
1630                         {
1631 // printf("File::write_frames %d " _LD " %d\n", 
1632 // __LINE__, 
1633 // frames[i][j]->get_number(), 
1634 // frames[i][j]->get_keyframe());
1635                                 frames[i][j]->to_filefork(fork_buffer + 
1636                                         sizeof(int) +
1637                                         entry_size * len * i +
1638                                         entry_size * j);
1639                         }
1640                 }
1641
1642                 
1643 //PRINT_TRACE
1644 // Frames per layer
1645                 int *fbfr = (int *)fork_buffer;
1646                 fbfr[0] = len;
1647 //PRINT_TRACE
1648
1649                 file_fork->send_command(FileFork::WRITE_FRAMES, 
1650                         fork_buffer, 
1651                         sizeof(fork_buffer));
1652 //PRINT_TRACE
1653                 int result = file_fork->read_result();
1654
1655
1656 //printf("File::write_frames %d\n", __LINE__);
1657                 return result;
1658         }
1659
1660
1661 #endif // USE_FILEFORK
1662
1663
1664 //PRINT_TRACE
1665 // Store the counters in temps so the filebase can choose to overwrite them.
1666         int result;
1667         int current_frame_temp = current_frame;
1668         int video_length_temp = asset->video_length;
1669
1670         write_lock->lock("File::write_frames");
1671
1672 //PRINT_TRACE
1673         result = file->write_frames(frames, len);
1674 //PRINT_TRACE
1675
1676         current_frame = current_frame_temp + len;
1677         asset->video_length = video_length_temp + len;
1678         write_lock->unlock();
1679 //PRINT_TRACE
1680         return result;
1681 }
1682
1683 // Only called by FileThread
1684 int File::write_compressed_frame(VFrame *buffer)
1685 {
1686         int result = 0;
1687         write_lock->lock("File::write_compressed_frame");
1688         result = file->write_compressed_frame(buffer);
1689         current_frame++;
1690         asset->video_length++;
1691         write_lock->unlock();
1692         return result;
1693 }
1694
1695
1696 int File::write_audio_buffer(int64_t len)
1697 {
1698 #ifdef USE_FILEFORK
1699         if(file_fork)
1700         {
1701                 FileForker this_is(*forked);
1702                 file_fork->send_command(FileFork::WRITE_AUDIO_BUFFER, (unsigned char*)&len, sizeof(len));
1703                 int result = file_fork->read_result();
1704                 return result;
1705         }
1706 #endif
1707
1708         int result = 0;
1709         if(audio_thread)
1710         {
1711                 result = audio_thread->write_buffer(len);
1712         }
1713         return result;
1714 }
1715
1716 int File::write_video_buffer(int64_t len)
1717 {
1718 #ifdef USE_FILEFORK
1719         if(file_fork)
1720         {
1721                 FileForker this_is(*forked);
1722 // Copy over sequence numbers for background rendering
1723 // frame sizes for direct copy
1724 //printf("File::write_video_buffer %d\n", __LINE__);
1725                 int fork_buffer_size = sizeof(int64_t) +
1726                         VFrame::filefork_size() * asset->layers * len;
1727                 unsigned char fork_buffer[fork_buffer_size];
1728                 int64_t *fbfr = (int64_t *)fork_buffer;
1729                 fbfr[0] = len;
1730
1731                 for(int i = 0; i < asset->layers; i++)
1732                 {
1733                         for(int j = 0; j < len; j++)
1734                         {
1735 // Send memory state
1736                                 current_frame_buffer[i][j]->to_filefork(fork_buffer + 
1737                                         sizeof(int64_t) +
1738                                         VFrame::filefork_size() * (len * i + j));
1739 // printf("File::write_video_buffer %d size=%d %d %02x %02x %02x %02x %02x %02x %02x %02x\n", 
1740 // __LINE__, 
1741 // current_frame_buffer[i][j]->get_shmid(),
1742 // current_frame_buffer[i][j]->get_compressed_size(),
1743 // current_frame_buffer[i][j]->get_data()[0],
1744 // current_frame_buffer[i][j]->get_data()[1],
1745 // current_frame_buffer[i][j]->get_data()[2],
1746 // current_frame_buffer[i][j]->get_data()[3],
1747 // current_frame_buffer[i][j]->get_data()[4],
1748 // current_frame_buffer[i][j]->get_data()[5],
1749 // current_frame_buffer[i][j]->get_data()[6],
1750 // current_frame_buffer[i][j]->get_data()[7]);
1751                         }
1752                 }
1753
1754 //printf("File::write_video_buffer %d\n", __LINE__);
1755                 file_fork->send_command(FileFork::WRITE_VIDEO_BUFFER, 
1756                         fork_buffer, 
1757                         fork_buffer_size);
1758 //printf("File::write_video_buffer %d\n", __LINE__);
1759                 int result = file_fork->read_result();
1760 //printf("File::write_video_buffer %d\n", __LINE__);
1761                 return result;
1762         }
1763 #endif
1764
1765         int result = 0;
1766         if(video_thread)
1767         {
1768                 result = video_thread->write_buffer(len);
1769         }
1770
1771         return result;
1772 }
1773
1774 Samples** File::get_audio_buffer()
1775 {
1776 #ifdef USE_FILEFORK
1777         if(file_fork)
1778         {
1779                 FileForker this_is(*forked);
1780                 file_fork->send_command(FileFork::GET_AUDIO_BUFFER, 0, 0);
1781                 int result = file_fork->read_result();
1782
1783 // Read parameters for a Samples buffer & create it in File
1784 //              delete_temp_samples_buffer();
1785 //              if(!temp_samples_buffer) 
1786 //              {
1787 //                      temp_samples_buffer = new Samples**[ring_buffers];
1788 //                      for(int i = 0; i < ring_buffers; i++) temp_samples_buffer[i] = 0;
1789 //              }
1790 //              
1791 //              
1792 //              temp_samples_buffer  = new Samples*[asset->channels];
1793 //              for(int i = 0; i < asset->channels; i++)
1794 //              {
1795 //                      temp_samples_buffer[i] = new Samples;
1796 //                      temp_samples_buffer[i]->from_filefork(file_fork->result_data + 
1797 //                              i * Samples::filefork_size());
1798 //              }
1799
1800                 return temp_samples_buffer[result];
1801         }
1802 #endif
1803
1804         if(audio_thread) return audio_thread->get_audio_buffer();
1805         return 0;
1806 }
1807
1808 VFrame*** File::get_video_buffer()
1809 {
1810 #ifdef USE_FILEFORK
1811         if(file_fork)
1812         {
1813                 FileForker this_is(*forked);
1814
1815                 file_fork->send_command(FileFork::GET_VIDEO_BUFFER, 0, 0);
1816                 int result = file_fork->read_result();
1817
1818 // Read parameters for a VFrame buffer & create it in File
1819 //              delete_temp_frame_buffer();
1820
1821
1822 //              temp_frame_size = *(int*)(file_fork->result_data + 
1823 //                      file_fork->result_bytes - 
1824 //                      sizeof(int));
1825 // 
1826 // //printf("File::get_video_buffer %d %p %d\n", __LINE__, this, asset->layers);
1827 //              temp_frame_buffer = new VFrame**[asset->layers];
1828 // 
1829 //              for(int i = 0; i < asset->layers; i++)
1830 //              {
1831 // 
1832 //                      temp_frame_buffer[i] = new VFrame*[temp_frame_size];
1833 // 
1834 //                      for(int j = 0; j < temp_frame_size; j++)
1835 //                      {
1836 // 
1837 //                              temp_frame_buffer[i][j] = new VFrame;
1838 // printf("File::get_video_buffer %d %p\n", __LINE__, temp_frame_buffer[i][j]);
1839 // 
1840 //                              temp_frame_buffer[i][j]->from_filefork(file_fork->result_data + 
1841 //                                      i * temp_frame_size * VFrame::filefork_size() +
1842 //                                      j * VFrame::filefork_size());
1843 // 
1844 //                      }
1845 //              }
1846 // 
1847
1848                 current_frame_buffer = temp_frame_buffer[result];
1849
1850                 return current_frame_buffer;
1851         }
1852 #endif
1853
1854         if(video_thread) 
1855         {
1856                 VFrame*** result = video_thread->get_video_buffer();
1857
1858                 return result;
1859         }
1860
1861         return 0;
1862 }
1863
1864
1865 int File::read_samples(Samples *samples, int64_t len)
1866 {
1867 // Never try to read more samples than exist in the file
1868         if (current_sample + len > asset->audio_length) {
1869                 len = asset->audio_length - current_sample;
1870         }
1871         if(len <= 0) return 0;
1872
1873         int result = 0;
1874         const int debug = 0;
1875         if(debug) PRINT_TRACE
1876
1877 #ifdef USE_FILEFORK
1878         if(file_fork)
1879         {
1880                 FileForker this_is(*forked);
1881                 int buffer_bytes = Samples::filefork_size() + sizeof(int64_t);
1882                 unsigned char buffer[buffer_bytes];
1883                 samples->to_filefork(buffer);
1884                 *(int64_t*)(buffer + Samples::filefork_size()) = len;
1885                 if(debug) PRINT_TRACE
1886                 file_fork->send_command(FileFork::READ_SAMPLES, 
1887                         buffer, 
1888                         buffer_bytes);
1889                 if(debug) PRINT_TRACE
1890                 int result = file_fork->read_result();
1891
1892 // Crashed
1893                 if(result && !file_fork->child_running())
1894                 {
1895                         delete file_fork;
1896                         result = open_file(preferences, asset, rd, wr);
1897                 }
1898
1899                 return result;
1900         }
1901 #endif
1902
1903         if(debug) PRINT_TRACE
1904
1905         double *buffer = samples->get_data();
1906
1907         int64_t base_samplerate = asset->sample_rate;
1908
1909         if(file)
1910         {
1911 // Resample recursively calls this with the asset sample rate
1912                 if(base_samplerate == 0) base_samplerate = asset->sample_rate;
1913
1914                 if(debug) PRINT_TRACE
1915                 result = file->read_samples(buffer, len);
1916
1917                 if(debug) PRINT_TRACE
1918                 current_sample += len;
1919
1920                 normalized_sample += len;
1921         }
1922         if(debug) PRINT_TRACE
1923
1924         return result;
1925 }
1926
1927
1928 int File::read_frame(VFrame *frame, int is_thread)
1929 {
1930         const int debug = 0;
1931
1932         if(debug) PRINT_TRACE
1933
1934 #ifdef USE_FILEFORK
1935 // is_thread is only true in the fork
1936         if(file_fork && !is_fork && !is_thread)
1937         {
1938                 FileForker this_is(*forked);
1939                 unsigned char fork_buffer[VFrame::filefork_size()];
1940                 if(debug) PRINT_TRACE
1941
1942                 frame->to_filefork(fork_buffer);
1943                 file_fork->send_command(FileFork::READ_FRAME, 
1944                         fork_buffer, 
1945                         VFrame::filefork_size());
1946
1947                 int result = file_fork->read_result();
1948
1949
1950 // Crashed
1951                 if(result && !file_fork->child_running())
1952                 {
1953                         delete file_fork;
1954                         result = open_file(preferences, asset, rd, wr);
1955                 }
1956                 else
1957                 if(!result && 
1958                         frame->get_color_model() == BC_COMPRESSED)
1959                 {
1960 // Get compressed data from socket
1961 //printf("File::read_frame %d %d\n", __LINE__, file_fork->result_bytes);
1962                         int header_size = sizeof(int) * 2;
1963                         if(file_fork->result_bytes > header_size)
1964                         {
1965 //printf("File::read_frame %d %d\n", __LINE__, file_fork->result_bytes);
1966                                 frame->allocate_compressed_data(file_fork->result_bytes - header_size);
1967                                 frame->set_compressed_size(file_fork->result_bytes - header_size);
1968                                 frame->set_keyframe(*(int*)(file_fork->result_data + sizeof(int)));
1969                                 memcpy(frame->get_data(), 
1970                                         file_fork->result_data + header_size,
1971                                         file_fork->result_bytes - header_size);
1972                         }
1973                         else
1974 // Get compressed data size
1975                         {
1976                                 frame->set_compressed_size(*(int*)file_fork->result_data);
1977                                 frame->set_keyframe(*(int*)(file_fork->result_data + sizeof(int)));
1978 //printf("File::read_frame %d %d\n", __LINE__, *(int*)(file_fork->result_data + sizeof(int)));
1979                         }
1980                 }
1981
1982                 return result;
1983         }
1984 #endif
1985
1986
1987 //printf("File::read_frame %d\n", __LINE__);
1988
1989         if(video_thread && !is_thread) return video_thread->read_frame(frame);
1990
1991 //printf("File::read_frame %d\n", __LINE__);
1992         if(debug) PRINT_TRACE
1993         if(file)
1994         {
1995                 if(debug) PRINT_TRACE
1996                 int supported_colormodel = colormodel_supported(frame->get_color_model());
1997                 int advance_position = 1;
1998
1999 // Test cache
2000                 if(use_cache && !is_fork &&
2001                         frame_cache->get_frame(frame,
2002                                 current_frame,
2003                                 current_layer,
2004                                 asset->frame_rate))
2005                 {
2006 // Can't advance position if cache used.
2007 //printf("File::read_frame %d\n", __LINE__);
2008                         advance_position = 0;
2009                 }
2010                 else
2011 // Need temp
2012                 if(frame->get_color_model() != BC_COMPRESSED &&
2013                         (supported_colormodel != frame->get_color_model() ||
2014                         frame->get_w() != asset->width ||
2015                         frame->get_h() != asset->height))
2016                 {
2017
2018 //                      printf("File::read_frame %d\n", __LINE__);
2019 // Can't advance position here because it needs to be added to cache
2020                         if(temp_frame)
2021                         {
2022                                 if(!temp_frame->params_match(asset->width, asset->height, supported_colormodel))
2023                                 {
2024                                         delete temp_frame;
2025                                         temp_frame = 0;
2026                                 }
2027                         }
2028
2029 //                      printf("File::read_frame %d\n", __LINE__);
2030                         if(!temp_frame)
2031                         {
2032                                 temp_frame = new VFrame(0,
2033                                         -1,
2034                                         asset->width,
2035                                         asset->height,
2036                                         supported_colormodel,
2037                                         -1);
2038                         }
2039
2040 //                      printf("File::read_frame %d\n", __LINE__);
2041                         temp_frame->copy_stacks(frame);
2042                         file->read_frame(temp_frame);
2043 //for(int i = 0; i < 1000 * 1000; i++) ((float*)temp_frame->get_rows()[0])[i] = 1.0;
2044 // printf("File::read_frame %d %d %d %d %d %d\n", 
2045 // temp_frame->get_color_model(), 
2046 // temp_frame->get_w(),
2047 // temp_frame->get_h(),
2048 // frame->get_color_model(),
2049 // frame->get_w(),
2050 // frame->get_h());
2051                         BC_CModels::transfer(frame->get_rows(), 
2052                                 temp_frame->get_rows(),
2053                                 frame->get_y(),
2054                                 frame->get_u(),
2055                                 frame->get_v(),
2056                                 temp_frame->get_y(),
2057                                 temp_frame->get_u(),
2058                                 temp_frame->get_v(),
2059                                 0, 
2060                                 0, 
2061                                 temp_frame->get_w(), 
2062                                 temp_frame->get_h(),
2063                                 0, 
2064                                 0, 
2065                                 frame->get_w(), 
2066                                 frame->get_h(),
2067                                 temp_frame->get_color_model(), 
2068                                 frame->get_color_model(),
2069                                 0,
2070                                 temp_frame->get_w(),
2071                                 frame->get_w());
2072 //                      printf("File::read_frame %d\n", __LINE__);
2073                 }
2074                 else
2075                 {
2076 // Can't advance position here because it needs to be added to cache
2077 //printf("File::read_frame %d\n", __LINE__);
2078                         file->read_frame(frame);
2079 //for(int i = 0; i < 100 * 1000; i++) ((float*)frame->get_rows()[0])[i] = 1.0;
2080                 }
2081
2082 //printf("File::read_frame %d use_cache=%d\n", __LINE__, use_cache);
2083                 if(use_cache && !is_fork)
2084                         frame_cache->put_frame(frame,
2085                                 current_frame, current_layer,
2086                                 asset->frame_rate, 1, 0);
2087 //printf("File::read_frame %d\n", __LINE__);
2088
2089                 if(advance_position) current_frame++;
2090                 if(debug) PRINT_TRACE
2091                 return 0;
2092         }
2093         else
2094                 return 1;
2095 }
2096
2097 int File::can_copy_from(Asset *asset, 
2098         int64_t position, 
2099         int output_w, 
2100         int output_h)
2101 {
2102         if(!asset) return 0;
2103
2104 #ifdef USE_FILEFORK
2105         if(file_fork)
2106         {
2107                 FileForker this_is(*forked);
2108                 FileXML xml;
2109                 asset->write(&xml, 1, "");
2110                 xml.terminate_string();
2111                 const char *xml_string = xml.string();
2112                 long xml_length = strlen(xml_string);
2113                 int buffer_size = xml_length + 1 + 
2114                         sizeof(int64_t) +
2115                         sizeof(int) + 
2116                         sizeof(int);
2117                 unsigned char *buffer = new unsigned char[buffer_size];
2118                 *(int64_t*)(buffer) = position;
2119                 *(int*)(buffer + sizeof(int64_t)) = output_w;
2120                 *(int*)(buffer + sizeof(int64_t) + sizeof(int)) = output_h;
2121                 memcpy(buffer + 
2122                         sizeof(int64_t) +
2123                         sizeof(int) + 
2124                         sizeof(int), 
2125                         xml_string, 
2126                         xml_length + 1);
2127
2128                 file_fork->send_command(FileFork::CAN_COPY_FROM, 
2129                         buffer, 
2130                         buffer_size);
2131                 int result = file_fork->read_result();
2132                 return result;
2133         }
2134 #endif
2135
2136
2137         if(file)
2138         {
2139                 return asset->width == output_w &&
2140                         asset->height == output_h &&
2141                         file->can_copy_from(asset, position);
2142         }
2143         else
2144                 return 0;
2145 }
2146
2147 // Fill in queries about formats when adding formats here.
2148
2149
2150 int File::strtoformat(const char *format)
2151 {
2152         return strtoformat(0, format);
2153 }
2154
2155 int File::strtoformat(ArrayList<PluginServer*> *plugindb, const char *format)
2156 {
2157         if(!strcasecmp(format, _(AC3_NAME))) return FILE_AC3;
2158         if(!strcasecmp(format, _(SCENE_NAME))) return FILE_SCENE;
2159         if(!strcasecmp(format, _(WAV_NAME))) return FILE_WAV;
2160         if(!strcasecmp(format, _(PCM_NAME))) return FILE_PCM;
2161         if(!strcasecmp(format, _(AU_NAME))) return FILE_AU;
2162         if(!strcasecmp(format, _(AIFF_NAME))) return FILE_AIFF;
2163         if(!strcasecmp(format, _(SND_NAME))) return FILE_SND;
2164         if(!strcasecmp(format, _(PNG_NAME))) return FILE_PNG;
2165         if(!strcasecmp(format, _(PNG_LIST_NAME))) return FILE_PNG_LIST;
2166         if(!strcasecmp(format, _(TIFF_NAME))) return FILE_TIFF;
2167         if(!strcasecmp(format, _(TIFF_LIST_NAME))) return FILE_TIFF_LIST;
2168         if(!strcasecmp(format, _(JPEG_NAME))) return FILE_JPEG;
2169         if(!strcasecmp(format, _(JPEG_LIST_NAME))) return FILE_JPEG_LIST;
2170         if(!strcasecmp(format, _(EXR_NAME))) return FILE_EXR;
2171         if(!strcasecmp(format, _(EXR_LIST_NAME))) return FILE_EXR_LIST;
2172         if(!strcasecmp(format, _(FLAC_NAME))) return FILE_FLAC;
2173         if(!strcasecmp(format, _(CR2_NAME))) return FILE_CR2;
2174         if(!strcasecmp(format, _(CR2_LIST_NAME))) return FILE_CR2_LIST;
2175         if(!strcasecmp(format, _(MPEG_NAME))) return FILE_MPEG;
2176         if(!strcasecmp(format, _(AMPEG_NAME))) return FILE_AMPEG;
2177         if(!strcasecmp(format, _(VMPEG_NAME))) return FILE_VMPEG;
2178         if(!strcasecmp(format, _(TGA_NAME))) return FILE_TGA;
2179         if(!strcasecmp(format, _(TGA_LIST_NAME))) return FILE_TGA_LIST;
2180         if(!strcasecmp(format, _(MOV_NAME))) return FILE_MOV;
2181         if(!strcasecmp(format, _(AVI_NAME))) return FILE_AVI;
2182         if(!strcasecmp(format, _(AVI_LAVTOOLS_NAME))) return FILE_AVI_LAVTOOLS;
2183         if(!strcasecmp(format, _(AVI_ARNE2_NAME))) return FILE_AVI_ARNE2;
2184         if(!strcasecmp(format, _(AVI_ARNE1_NAME))) return FILE_AVI_ARNE1;
2185         if(!strcasecmp(format, _(AVI_AVIFILE_NAME))) return FILE_AVI_AVIFILE;
2186         if(!strcasecmp(format, _(OGG_NAME))) return FILE_OGG;
2187         if(!strcasecmp(format, _(VORBIS_NAME))) return FILE_VORBIS;
2188         if(!strcasecmp(format, _(RAWDV_NAME))) return FILE_RAWDV;
2189         if(!strcasecmp(format, _(FFMPEG_NAME))) return FILE_FFMPEG;
2190         if(!strcasecmp(format, _(DBASE_NAME))) return FILE_DB;
2191
2192         return 0;
2193 }
2194
2195
2196 const char* File::formattostr(int format)
2197 {
2198         return formattostr(0, format);
2199 }
2200
2201 const char* File::formattostr(ArrayList<PluginServer*> *plugindb, int format)
2202 {
2203         switch(format)
2204         {
2205                 case FILE_SCENE:        return _(SCENE_NAME);
2206                 case FILE_AC3:          return _(AC3_NAME);
2207                 case FILE_WAV:          return _(WAV_NAME);
2208                 case FILE_PCM:          return _(PCM_NAME);
2209                 case FILE_AU:           return _(AU_NAME);
2210                 case FILE_AIFF:         return _(AIFF_NAME);
2211                 case FILE_SND:          return _(SND_NAME);
2212                 case FILE_PNG:          return _(PNG_NAME);
2213                 case FILE_PNG_LIST:     return _(PNG_LIST_NAME);
2214                 case FILE_JPEG:         return _(JPEG_NAME);
2215                 case FILE_JPEG_LIST:    return _(JPEG_LIST_NAME);
2216                 case FILE_CR2:          return _(CR2_NAME);
2217                 case FILE_CR2_LIST:     return _(CR2_LIST_NAME);
2218                 case FILE_FLAC:         return _(FLAC_NAME);
2219                 case FILE_EXR:          return _(EXR_NAME);
2220                 case FILE_EXR_LIST:     return _(EXR_LIST_NAME);
2221                 case FILE_MPEG:         return _(MPEG_NAME);
2222                 case FILE_AMPEG:        return _(AMPEG_NAME);
2223                 case FILE_VMPEG:        return _(VMPEG_NAME);
2224                 case FILE_TGA:          return _(TGA_NAME);
2225                 case FILE_TGA_LIST:     return _(TGA_LIST_NAME);
2226                 case FILE_TIFF:         return _(TIFF_NAME);
2227                 case FILE_TIFF_LIST:    return _(TIFF_LIST_NAME);
2228                 case FILE_MOV:          return _(MOV_NAME);
2229                 case FILE_AVI_LAVTOOLS: return _(AVI_LAVTOOLS_NAME);
2230                 case FILE_AVI:          return _(AVI_NAME);
2231                 case FILE_AVI_ARNE2:    return _(AVI_ARNE2_NAME);
2232                 case FILE_AVI_ARNE1:    return _(AVI_ARNE1_NAME);
2233                 case FILE_AVI_AVIFILE:  return _(AVI_AVIFILE_NAME);
2234                 case FILE_OGG:          return _(OGG_NAME);
2235                 case FILE_VORBIS:       return _(VORBIS_NAME);
2236                 case FILE_RAWDV:        return _(RAWDV_NAME);
2237                 case FILE_FFMPEG:       return _(FFMPEG_NAME);
2238                 case FILE_DB:           return _(DBASE_NAME);
2239         }
2240         return "Unknown";
2241 }
2242
2243 int File::strtobits(const char *bits)
2244 {
2245         if(!strcasecmp(bits, _(NAME_8BIT))) return BITSLINEAR8;
2246         if(!strcasecmp(bits, _(NAME_16BIT))) return BITSLINEAR16;
2247         if(!strcasecmp(bits, _(NAME_24BIT))) return BITSLINEAR24;
2248         if(!strcasecmp(bits, _(NAME_32BIT))) return BITSLINEAR32;
2249         if(!strcasecmp(bits, _(NAME_ULAW))) return BITSULAW;
2250         if(!strcasecmp(bits, _(NAME_ADPCM))) return BITS_ADPCM;
2251         if(!strcasecmp(bits, _(NAME_FLOAT))) return BITSFLOAT;
2252         if(!strcasecmp(bits, _(NAME_IMA4))) return BITSIMA4;
2253         return BITSLINEAR16;
2254 }
2255
2256 const char* File::bitstostr(int bits)
2257 {
2258 //printf("File::bitstostr\n");
2259         switch(bits)
2260         {
2261                 case BITSLINEAR8:       return (NAME_8BIT);
2262                 case BITSLINEAR16:      return (NAME_16BIT);
2263                 case BITSLINEAR24:      return (NAME_24BIT);
2264                 case BITSLINEAR32:      return (NAME_32BIT);
2265                 case BITSULAW:          return (NAME_ULAW);
2266                 case BITS_ADPCM:        return (NAME_ADPCM);
2267                 case BITSFLOAT:         return (NAME_FLOAT);
2268                 case BITSIMA4:          return (NAME_IMA4);
2269         }
2270         return "Unknown";
2271 }
2272
2273
2274
2275 int File::str_to_byteorder(const char *string)
2276 {
2277         if(!strcasecmp(string, _("Lo Hi"))) return 1;
2278         return 0;
2279 }
2280
2281 const char* File::byteorder_to_str(int byte_order)
2282 {
2283         if(byte_order) return _("Lo Hi");
2284         return _("Hi Lo");
2285 }
2286
2287 int File::bytes_per_sample(int bits)
2288 {
2289         switch(bits)
2290         {
2291                 case BITSLINEAR8:       return 1;
2292                 case BITSLINEAR16:      return 2;
2293                 case BITSLINEAR24:      return 3;
2294                 case BITSLINEAR32:      return 4;
2295                 case BITSULAW:          return 1;
2296                 case BITSIMA4:          return 1;
2297         }
2298         return 1;
2299 }
2300
2301
2302
2303
2304
2305 int File::get_best_colormodel(int driver)
2306 {
2307         return get_best_colormodel(asset, driver);
2308 }
2309
2310 int File::get_best_colormodel(Asset *asset, int driver)
2311 {
2312         switch(asset->format)
2313         {
2314                 case FILE_RAWDV:        return FileDV::get_best_colormodel(asset, driver);
2315                 case FILE_MOV:          return FileMOV::get_best_colormodel(asset, driver);
2316                 case FILE_AVI:          return FileMOV::get_best_colormodel(asset, driver);
2317                 case FILE_MPEG:         return FileMPEG::get_best_colormodel(asset, driver);
2318                 case FILE_JPEG:
2319                 case FILE_JPEG_LIST:    return FileJPEG::get_best_colormodel(asset, driver);
2320                 case FILE_EXR:
2321                 case FILE_EXR_LIST:     return FileEXR::get_best_colormodel(asset, driver);
2322                 case FILE_PNG:
2323                 case FILE_PNG_LIST:     return FilePNG::get_best_colormodel(asset, driver);
2324                 case FILE_TGA:
2325                 case FILE_TGA_LIST:     return FileTGA::get_best_colormodel(asset, driver);
2326                 case FILE_CR2:
2327                 case FILE_CR2_LIST:     return FileCR2::get_best_colormodel(asset, driver);
2328                 case FILE_DB:           return FileDB::get_best_colormodel(asset, driver);
2329         }
2330
2331         return BC_RGB888;
2332 }
2333
2334
2335 int File::colormodel_supported(int colormodel)
2336 {
2337 #ifdef USE_FILEFORK
2338         if(file_fork)
2339         {
2340                 FileForker this_is(*forked);
2341                 unsigned char buffer[sizeof(int)];
2342                 int *ibfr = (int *)buffer;
2343                 ibfr[0] = colormodel;
2344
2345                 file_fork->send_command(FileFork::COLORMODEL_SUPPORTED, 
2346                         buffer, 
2347                         sizeof(int));
2348                 int result = file_fork->read_result();
2349                 return result;
2350         }
2351 #endif
2352
2353
2354         if(file)
2355                 return file->colormodel_supported(colormodel);
2356
2357         return BC_RGB888;
2358 }
2359
2360
2361 int64_t File::file_memory_usage()
2362 {
2363         return file ? file->base_memory_usage() : 0;
2364 }
2365
2366 int64_t File::get_memory_usage() 
2367 {
2368         int64_t result = 0;
2369
2370 #ifdef USE_FILEFORK
2371         if(file_fork)
2372         {
2373                 FileForker this_is(*forked);
2374                 file_fork->send_command(FileFork::FILE_MEMORY_USAGE, 0, 0);
2375                 result = file_fork->read_result();
2376         }
2377         else
2378 #endif
2379         result += file_memory_usage();
2380         if(temp_frame) result += temp_frame->get_data_size();
2381         result += frame_cache->get_memory_usage();
2382         if(video_thread) result += video_thread->get_memory_usage();
2383
2384         if(result < MIN_CACHEITEM_SIZE) result = MIN_CACHEITEM_SIZE;
2385         return result;
2386 }
2387
2388
2389 int File::supports_video(ArrayList<PluginServer*> *plugindb, char *format)
2390 {
2391         int format_i = strtoformat(plugindb, format);
2392         
2393         return supports_video(format_i);
2394         return 0;
2395 }
2396
2397 int File::supports_audio(ArrayList<PluginServer*> *plugindb, char *format)
2398 {
2399         int format_i = strtoformat(plugindb, format);
2400
2401         return supports_audio(format_i);
2402         return 0;
2403 }
2404
2405
2406 int File::supports_video(int format)
2407 {
2408 //printf("File::supports_video %d\n", format);
2409         switch(format)
2410         {
2411                 case FILE_OGG:
2412                 case FILE_MOV:
2413                 case FILE_JPEG:
2414                 case FILE_JPEG_LIST:
2415                 case FILE_CR2:
2416                 case FILE_CR2_LIST:
2417                 case FILE_EXR:
2418                 case FILE_EXR_LIST:
2419                 case FILE_PNG:
2420                 case FILE_PNG_LIST:
2421                 case FILE_TGA:
2422                 case FILE_TGA_LIST:
2423                 case FILE_TIFF:
2424                 case FILE_TIFF_LIST:
2425                 case FILE_VMPEG:
2426                 case FILE_AVI_LAVTOOLS:
2427                 case FILE_AVI_ARNE2:
2428                 case FILE_AVI:
2429                 case FILE_AVI_ARNE1:
2430                 case FILE_AVI_AVIFILE:
2431                 case FILE_FFMPEG:
2432                 case FILE_RAWDV:
2433                         return 1;
2434         }
2435         return 0;
2436 }
2437
2438 int File::supports_audio(int format)
2439 {
2440         switch(format)
2441         {
2442                 case FILE_AC3:
2443                 case FILE_FLAC:
2444                 case FILE_PCM:
2445                 case FILE_WAV:
2446                 case FILE_MOV:
2447                 case FILE_OGG:
2448                 case FILE_VORBIS:
2449                 case FILE_AMPEG:
2450                 case FILE_AU:
2451                 case FILE_AIFF:
2452                 case FILE_SND:
2453                 case FILE_AVI:
2454                 case FILE_AVI_LAVTOOLS:
2455                 case FILE_AVI_ARNE2:
2456                 case FILE_AVI_ARNE1:
2457                 case FILE_AVI_AVIFILE:
2458                 case FILE_FFMPEG:
2459                         return 1;
2460         }
2461         return 0;
2462 }
2463
2464 const char* File::get_tag(int format)
2465 {
2466         switch(format)
2467         {
2468                 case FILE_AC3:          return "ac3";
2469                 case FILE_AIFF:         return "aif";
2470                 case FILE_AMPEG:        return "mp3";
2471                 case FILE_AU:           return "au";
2472                 case FILE_AVI:          return "avi";
2473                 case FILE_RAWDV:        return "dv";
2474                 case FILE_DB:           return "db";
2475                 case FILE_EXR:          return "exr";
2476                 case FILE_EXR_LIST:     return "exr";
2477                 case FILE_FLAC:         return "flac";
2478                 case FILE_JPEG:         return "jpg";
2479                 case FILE_JPEG_LIST:    return "jpg";
2480                 case FILE_MOV:          return "mov/mp4";
2481                 case FILE_OGG:          return "ogg";
2482                 case FILE_PCM:          return "pcm";
2483                 case FILE_PNG:          return "png";
2484                 case FILE_PNG_LIST:     return "png";
2485                 case FILE_TGA:          return "tga";
2486                 case FILE_TGA_LIST:     return "tga";
2487                 case FILE_TIFF:         return "tif";
2488                 case FILE_TIFF_LIST:    return "tif";
2489                 case FILE_VMPEG:        return "m2v";
2490                 case FILE_VORBIS:       return "ogg";
2491                 case FILE_WAV:          return "wav";
2492                 case FILE_FFMPEG:       return "media";
2493         }
2494         return 0;
2495 }
2496
2497 const char* File::get_prefix(int format)
2498 {
2499         switch(format) {
2500         case FILE_PCM:          return "PCM";
2501         case FILE_WAV:          return "WAV";
2502         case FILE_MOV:          return "MOV";
2503         case FILE_PNG:          return "PNG";
2504         case FILE_JPEG:         return "JPEG";
2505         case FILE_TIFF:         return "TIFF";
2506         case FILE_GIF:          return "GIF";
2507         case FILE_JPEG_LIST:    return "JPEG_LIST";
2508         case FILE_AU:           return "AU";
2509         case FILE_AIFF:         return "AIFF";
2510         case FILE_SND:          return "SND";
2511         case FILE_AVI_LAVTOOLS: return "AVI_LAVTOOLS";
2512         case FILE_TGA_LIST:     return "TGA_LIST";
2513         case FILE_TGA:          return "TGA";
2514         case FILE_MPEG:         return "MPEG";
2515         case FILE_AMPEG:        return "AMPEG";
2516         case FILE_VMPEG:        return "VMPEG";
2517         case FILE_RAWDV:        return "RAWDV";
2518         case FILE_AVI_ARNE2:    return "AVI_ARNE2";
2519         case FILE_AVI_ARNE1:    return "AVI_ARNE1";
2520         case FILE_AVI_AVIFILE:  return "AVI_AVIFILE";
2521         case FILE_TIFF_LIST:    return "TIFF_LIST";
2522         case FILE_PNG_LIST:     return "PNG_LIST";
2523         case FILE_AVI:          return "AVI";
2524         case FILE_AC3:          return "AC3";
2525         case FILE_EXR:          return "EXR";
2526         case FILE_EXR_LIST:     return "EXR_LIST";
2527         case FILE_CR2:          return "CR2";
2528         case FILE_OGG:          return "OGG";
2529         case FILE_VORBIS:       return "VORBIS";
2530         case FILE_FLAC:         return "FLAC";
2531         case FILE_FFMPEG:       return "FFMPEG";
2532         case FILE_SCENE:        return "SCENE";
2533         case FILE_CR2_LIST:     return "CR2_LIST";
2534         case FILE_GIF_LIST:     return "GIF_LIST";
2535         case FILE_DB:           return "DB";
2536         }
2537         return "UNKNOWN";
2538 }
2539
2540
2541 PackagingEngine *File::new_packaging_engine(Asset *asset)
2542 {
2543         PackagingEngine *result;
2544         switch (asset->format)
2545         {
2546                 case FILE_OGG:
2547                         result = (PackagingEngine*)new PackagingEngineOGG();
2548                         break;
2549                 default:
2550                         result = (PackagingEngine*) new PackagingEngineDefault();
2551                         break;
2552         }
2553
2554         return result;
2555 }
2556
2557
2558 int File::record_fd()
2559 {
2560         return file ? file->record_fd() : -1;
2561 }
2562
2563