r956: README.BUILD - add more library recommendations
[cinelerra_cv/ct.git] / cinelerra / file.C
blobf15fa5f85fd913cf1c9d7565d8b6a4ab75a9ea5c
1 #include "asset.h"
2 #include "bcsignals.h"
3 #include "byteorder.h"
4 #include "cache.inc"
5 #include "condition.h"
6 #include "edit.h"
7 #include "errorbox.h"
8 #include "file.h"
9 #include "fileac3.h"
10 #include "fileavi.h"
11 #include "filedv.h"
12 #include "fileogg.h"
13 #include "filebase.h"
14 #include "filecr2.h"
15 #include "fileexr.h"
16 #include "fileogg.h"
17 #include "filexml.h"
18 #include "filejpeg.h"
19 #include "filemov.h"
20 #include "filempeg.h"
21 #include "fileogg.h"
22 #include "filepng.h"
23 #include "filesndfile.h"
24 #include "filetga.h"
25 #include "filethread.h"
26 #include "filetiff.h"
27 #include "filevorbis.h"
28 #include "fileyuv.h"
29 #include "formatwindow.h"
30 #include "formattools.h"
31 #include "framecache.h"
32 #include "language.h"
33 #include "mutex.h"
34 #include "pluginserver.h"
35 #include "resample.h"
36 #include "stringfile.h"
37 #include "vframe.h"
42 File::File()
44         cpus = 1;
45         asset = new Asset;
46         format_completion = new Mutex("File::format_completion");
47         write_lock = new Condition(1, "File::write_lock");
48         frame_cache = new FrameCache;
49         reset_parameters();
52 File::~File()
54         if(getting_options)
55         {
56                 if(format_window) format_window->set_done(0);
57                 format_completion->lock("File::~File");
58                 format_completion->unlock();
59         }
61         if(temp_frame) delete temp_frame;
63         close_file(0);
64         Garbage::delete_object(asset);
65         delete format_completion;
66         delete write_lock;
67         if(frame_cache) delete frame_cache;
70 void File::reset_parameters()
72         file = 0;
73         audio_thread = 0;
74         video_thread = 0;
75         getting_options = 0;
76         format_window = 0;
77         temp_frame = 0;
78         current_sample = 0;
79         current_frame = 0;
80         current_channel = 0;
81         current_layer = 0;
82         normalized_sample = 0;
83         normalized_sample_rate = 0;
84         resample = 0;
85         resample_float = 0;
86         use_cache = 0;
87         preferences = 0;
88         playback_subtitle = -1;
89         interpolate_raw = 1;
92 int File::raise_window()
94         if(getting_options && format_window)
95         {
96                 format_window->raise_window();
97                 format_window->flush();
98         }
99         return 0;
102 void File::close_window()
104         if(getting_options)
105         {
106                 format_window->lock_window("File::close_window");
107                 format_window->set_done(1);
108                 format_window->unlock_window();
109                 getting_options = 0;
110         }
113 int File::get_options(FormatTools *format,  
114         int audio_options,
115         int video_options)
117         BC_WindowBase *parent_window = format->window;
118         ArrayList<PluginServer*> *plugindb = format->plugindb;
119         Asset *asset = format->asset;
121         getting_options = 1;
122         format_completion->lock("File::get_options");
123         switch(asset->format)
124         {
125                 case FILE_AC3:
126                         FileAC3::get_parameters(parent_window,
127                                 asset,
128                                 format_window,
129                                 audio_options,
130                                 video_options);
131                         break;
132                 case FILE_RAWDV:
133                         FileDV::get_parameters(parent_window,
134                                 asset,
135                                 format_window,
136                                 audio_options,
137                                 video_options);
138                         break;
139                 case FILE_PCM:
140                 case FILE_WAV:
141                 case FILE_AU:
142                 case FILE_AIFF:
143                 case FILE_SND:
144                         FileSndFile::get_parameters(parent_window, 
145                                 asset, 
146                                 format_window, 
147                                 audio_options, 
148                                 video_options);
149                         break;
150                 case FILE_MOV:
151                         FileMOV::get_parameters(parent_window, 
152                                 asset, 
153                                 format_window, 
154                                 audio_options, 
155                                 video_options,
156                                 format->locked_compressor);
157                         break;
158                 case FILE_AMPEG:
159                 case FILE_VMPEG:
160                         FileMPEG::get_parameters(parent_window, 
161                                 asset, 
162                                 format_window, 
163                                 audio_options, 
164                                 video_options);
165                         break;
166                 case FILE_AVI:
167                         FileMOV::get_parameters(parent_window, 
168                                 asset, 
169                                 format_window, 
170                                 audio_options, 
171                                 video_options,
172                                 format->locked_compressor);
173                         break;
174                 case FILE_AVI_LAVTOOLS:
175                 case FILE_AVI_ARNE2:
176                 case FILE_AVI_ARNE1:
177                 case FILE_AVI_AVIFILE:
178                         FileAVI::get_parameters(parent_window, 
179                                 asset, 
180                                 format_window, 
181                                 audio_options, 
182                                 video_options,
183                                 format->locked_compressor);
184                         break;
185                 case FILE_JPEG:
186                 case FILE_JPEG_LIST:
187                         FileJPEG::get_parameters(parent_window, 
188                                 asset, 
189                                 format_window, 
190                                 audio_options, 
191                                 video_options);
192                         break;
193                 case FILE_EXR:
194                 case FILE_EXR_LIST:
195                         FileEXR::get_parameters(parent_window, 
196                                 asset, 
197                                 format_window, 
198                                 audio_options, 
199                                 video_options);
200                         break;
201                 case FILE_YUV:
202                         FileYUV::get_parameters(parent_window,
203                                 asset,
204                                 format_window,
205                                 video_options,
206                                 format);
207                         break;
208                 case FILE_PNG:
209                 case FILE_PNG_LIST:
210                         FilePNG::get_parameters(parent_window, 
211                                 asset, 
212                                 format_window, 
213                                 audio_options, 
214                                 video_options);
215                         break;
216                 case FILE_TGA:
217                 case FILE_TGA_LIST:
218                         FileTGA::get_parameters(parent_window, 
219                                 asset, 
220                                 format_window, 
221                                 audio_options, 
222                                 video_options);
223                         break;
224                 case FILE_TIFF:
225                 case FILE_TIFF_LIST:
226                         FileTIFF::get_parameters(parent_window, 
227                                 asset, 
228                                 format_window, 
229                                 audio_options, 
230                                 video_options);
231                         break;
232                 case FILE_OGG:
233                         FileOGG::get_parameters(parent_window,
234                                 asset,
235                                 format_window,
236                                 audio_options,
237                                 video_options);
238                         break;
239                 default:
240                         break;
241         }
243         if(!format_window)
244         {
245                 ErrorBox *errorbox = new ErrorBox(PROGRAM_NAME ": Error",
246                         parent_window->get_abs_cursor_x(1),
247                         parent_window->get_abs_cursor_y(1));
248                 format_window = errorbox;
249                 getting_options = 1;
250                 if(audio_options)
251                         errorbox->create_objects(_("This format doesn't support audio."));
252                 else
253                 if(video_options)
254                         errorbox->create_objects(_("This format doesn't support video."));
255                 errorbox->run_window();
256                 delete errorbox;
257         }
259         getting_options = 0;
260         format_window = 0;
261         format_completion->unlock();
262         return 0;
265 void File::set_asset(Asset *asset)
267         this->asset->copy_from(asset, 1);
270 int File::set_processors(int cpus)   // Set the number of cpus for certain codecs
272         this->cpus = cpus;
273         return 0;
276 int File::set_preload(int64_t size)
278         this->playback_preload = size;
279         return 0;
282 void File::set_subtitle(int value)
284         this->playback_subtitle = value;
287 void File::set_interpolate_raw(int value)
289         this->interpolate_raw = value;
292 void File::set_white_balance_raw(int value)
294         this->white_balance_raw = value;
297 void File::set_cache_frames(int value)
299         use_cache = value;
302 int File::purge_cache()
304         return frame_cache->delete_oldest();
317 int File::open_file(Preferences *preferences, 
318         Asset *asset, 
319         int rd, 
320         int wr,
321         int64_t base_samplerate,
322         float base_framerate)
324         this->preferences = preferences;
325         this->asset->copy_from(asset, 1);
326         file = 0;
329         switch(this->asset->format)
330         {
331 // get the format now
332 // If you add another format to case 0, you also need to add another case for the
333 // file format #define.
334                 case FILE_UNKNOWN:
335                         FILE *stream;
336                         if(!(stream = fopen(this->asset->path, "rb")))
337                         {
338 // file not found
339                                 return 1;
340                         }
342                         char test[16];
343                         fread(test, 16, 1, stream);
345                         if(FileDV::check_sig(this->asset))
346                         {
347 // libdv
348                                 fclose(stream);
349                                 file = new FileDV(this->asset, this);
350                         }
351                         else if(FileSndFile::check_sig(this->asset))
352                         {
353 // libsndfile
354                                 fclose(stream);
355                                 file = new FileSndFile(this->asset, this);
356                         }
357                         else
358                         if(FilePNG::check_sig(this->asset))
359                         {
360 // PNG file
361                                 fclose(stream);
362                                 file = new FilePNG(this->asset, this);
363                         }
364                         else
365                         if(FileJPEG::check_sig(this->asset))
366                         {
367 // JPEG file
368                                 fclose(stream);
369                                 file = new FileJPEG(this->asset, this);
370                         }
371                         else
372                         if(FileEXR::check_sig(this->asset, test))
373                         {
374 // EXR file
375                                 fclose(stream);
376                                 file = new FileEXR(this->asset, this);
377                         }
378                         else
379                         if(FileYUV::check_sig(this->asset))
380                         {
381 // YUV file
382                                 fclose(stream);
383                                 file = new FileYUV(this->asset, this);
384                         }
385                         else
386                         if(FileCR2::check_sig(this->asset))
387                         {
388 // JPEG file
389                                 fclose(stream);
390                                 file = new FileCR2(this->asset, this);
391                         }
392                         else
393                         if(FileTGA::check_sig(this->asset))
394                         {
395 // TGA file
396                                 fclose(stream);
397                                 file = new FileTGA(this->asset, this);
398                         }
399                         else
400                         if(FileTIFF::check_sig(this->asset))
401                         {
402 // TIFF file
403                                 fclose(stream);
404                                 file = new FileTIFF(this->asset, this);
405                         }
406                         else
407                         if(FileOGG::check_sig(this->asset))
408                         {
409 // OGG file
410                                 fclose(stream);
411                                 file = new FileOGG(this->asset, this);
412                         }
413                         else
414                         if(FileVorbis::check_sig(this->asset))
415                         {
416 // VorbisFile file
417                                 fclose(stream);
418                                 file = new FileVorbis(this->asset, this);
419                         }
420                         else
421                         if(FileOGG::check_sig(this->asset))
422                         {
423 // OGG file.  Doesn't always work with pure audio files.
424                                 fclose(stream);
425                                 file = new FileOGG(this->asset, this);
426                         }
427                         else
428                         if(FileMPEG::check_sig(this->asset))
429                         {
430 // MPEG file
431                                 fclose(stream);
432                                 file = new FileMPEG(this->asset, this);
433                         }
434                         else
435                         if(test[0] == '<' && test[1] == 'E' && test[2] == 'D' && test[3] == 'L' && test[4] == '>' ||
436                                 test[0] == '<' && test[1] == 'H' && test[2] == 'T' && test[3] == 'A' && test[4] == 'L' && test[5] == '>' ||
437                                 test[0] == '<' && test[1] == '?' && test[2] == 'x' && test[3] == 'm' && test[4] == 'l')
438                         {
439 // XML file
440                                 fclose(stream);
441                                 return FILE_IS_XML;
442                         }    // can't load project file
443                         else
444                         if(FileMOV::check_sig(this->asset))
445                         {
446 // MOV file
447 // should be last because quicktime lacks a magic number
448                                 fclose(stream);
449                                 file = new FileMOV(this->asset, this);
450                         }
451                         else
452                         {
453 // PCM file
454                                 fclose(stream);
455                                 return FILE_UNRECOGNIZED_CODEC;
456                         }   // need more info
457                         break;
459 // format already determined
460                 case FILE_AC3:
461                         file = new FileAC3(this->asset, this);
462                         break;
464                 case FILE_PCM:
465                 case FILE_WAV:
466                 case FILE_AU:
467                 case FILE_AIFF:
468                 case FILE_SND:
469 //printf("File::open_file 1\n");
470                         file = new FileSndFile(this->asset, this);
471                         break;
473                 case FILE_PNG:
474                 case FILE_PNG_LIST:
475                         file = new FilePNG(this->asset, this);
476                         break;
478                 case FILE_JPEG:
479                 case FILE_JPEG_LIST:
480                         file = new FileJPEG(this->asset, this);
481                         break;
483                 case FILE_EXR:
484                 case FILE_EXR_LIST:
485                         file = new FileEXR(this->asset, this);
486                         break;
488                 case FILE_YUV:
489                         file = new FileYUV(this->asset, this);
490                         break;
492                 case FILE_CR2:
493                         file = new FileCR2(this->asset, this);
494                         break;
496                 case FILE_TGA_LIST:
497                 case FILE_TGA:
498                         file = new FileTGA(this->asset, this);
499                         break;
501                 case FILE_TIFF:
502                 case FILE_TIFF_LIST:
503                         file = new FileTIFF(this->asset, this);
504                         break;
506                 case FILE_MOV:
507                         file = new FileMOV(this->asset, this);
508                         break;
510                 case FILE_MPEG:
511                 case FILE_AMPEG:
512                 case FILE_VMPEG:
513                         file = new FileMPEG(this->asset, this);
514                         break;
516                 case FILE_OGG:
517                         file = new FileOGG(this->asset, this);
518                         break;
520                 case FILE_VORBIS:
521                         file = new FileVorbis(this->asset, this);
522                         break;
524                 case FILE_AVI:
525                         file = new FileMOV(this->asset, this);
526                         break;
528                 case FILE_AVI_LAVTOOLS:
529                 case FILE_AVI_ARNE2:
530                 case FILE_AVI_ARNE1:
531                 case FILE_AVI_AVIFILE:
532                         file = new FileAVI(this->asset, this);
533                         break;
535                 case FILE_RAWDV:
536                         file = new FileDV(this->asset, this);
537                         break;
539 // try plugins
540                 default:
541                         return 1;
542                         break;
543         }
545 // Reopen file with correct parser and get header.
546         if(file->open_file(rd, wr))
547         {
548                 delete file;
549                 file = 0;
550         }
553 // Set extra writing parameters to mandatory settings.
554         if(file && wr)
555         {
556                 if(this->asset->dither) file->set_dither();
557         }
560 // Synchronize header parameters
561         if(file)
562         {
563                 asset->copy_from(this->asset, 1);
564         }
566         if(file)
567                 return FILE_OK;
568         else
569                 return FILE_NOT_FOUND;
572 int File::close_file(int ignore_thread)
574         if(!ignore_thread)
575         {
576                 stop_audio_thread();
577                 stop_video_thread();
578         }
580         if(file) 
581         {
582 // The file's asset is a copy of the argument passed to open_file so the
583 // user must copy lengths from the file's asset.
584                 if(asset && file->wr)
585                 {
586                         asset->audio_length = current_sample;
587                         asset->video_length = current_frame;
588                 }
589                 file->close_file();
590                 delete file;
591         }
593         if(resample) delete resample;
594         if(resample_float) delete resample_float;
596         reset_parameters();
597         return 0;
602 int File::get_index(char *index_path)
604         if(file)
605         {
606                 return file->get_index(index_path);
607         }
608         return 1;
613 int File::start_audio_thread(int64_t buffer_size, int ring_buffers)
615         if(!audio_thread)
616         {
617                 audio_thread = new FileThread(this, 1, 0);
618                 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
619         }
620         return 0;
623 int File::start_video_thread(int64_t buffer_size, 
624         int color_model, 
625         int ring_buffers, 
626         int compressed)
628         if(!video_thread)
629         {
630                 video_thread = new FileThread(this, 0, 1);
631                 video_thread->start_writing(buffer_size, 
632                         color_model, 
633                         ring_buffers, 
634                         compressed);
635         }
636         return 0;
639 int File::start_video_decode_thread()
641 // Currently, CR2 is the only one which won't work asynchronously, so
642 // we're not using a virtual function yet.
643         if(!video_thread && asset->format != FILE_CR2)
644         {
645                 video_thread = new FileThread(this, 0, 1);
646                 video_thread->start_reading();
647         }
650 int File::stop_audio_thread()
652         if(audio_thread)
653         {
654                 audio_thread->stop_writing();
655                 delete audio_thread;
656                 audio_thread = 0;
657         }
658         return 0;
661 int File::stop_video_thread()
663         if(video_thread)
664         {
665                 video_thread->stop_reading();
666                 video_thread->stop_writing();
667                 delete video_thread;
668                 video_thread = 0;
669         }
670         return 0;
673 FileThread* File::get_video_thread()
675         return video_thread;
678 int File::set_channel(int channel) 
680         if(file && channel < asset->channels)
681         {
682                 current_channel = channel;
683                 return 0;
684         }
685         else
686                 return 1;
689 int File::set_layer(int layer, int is_thread) 
691         if(file && layer < asset->layers)
692         {
693                 if(!is_thread && video_thread)
694                 {
695                         video_thread->set_layer(layer);
696                 }
697                 else
698                 {
699                         current_layer = layer;
700                 }
701                 return 0; 
702         }
703         else
704                 return 1;
707 int64_t File::get_audio_length(int64_t base_samplerate) 
709         int64_t result = asset->audio_length;
710         if(result > 0)
711         {
712                 if(base_samplerate > 0)
713                         return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
714                 else
715                         return result;
716         }
717         else
718                 return -1;
721 int64_t File::get_video_length(float base_framerate)
723         int64_t result = asset->video_length;
724         if(result > 0)
725         {
726                 if(base_framerate > 0)
727                         return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5); 
728                 else
729                         return result;
730         }
731         else
732                 return -1;  // infinity
736 int64_t File::get_video_position(float base_framerate) 
738         if(base_framerate > 0)
739                 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
740         else
741                 return current_frame;
744 int64_t File::get_audio_position(int64_t base_samplerate) 
746         if(base_samplerate > 0)
747         {
748                 if(normalized_sample_rate == base_samplerate)
749                         return normalized_sample;
750                 else
751                         return (int64_t)((double)current_sample / 
752                                 asset->sample_rate * 
753                                 base_samplerate + 
754                                 0.5);
755         }
756         else
757                 return current_sample;
762 // The base samplerate must be nonzero if the base samplerate in the calling
763 // function is expected to change as this forces the resampler to reset.
765 int File::set_audio_position(int64_t position, float base_samplerate) 
767         int result = 0;
769         if(!file) return 1;
771 #define REPOSITION(x, y) \
772         (labs((x) - (y)) > 1)
776         if((base_samplerate && REPOSITION(normalized_sample, position)) ||
777                 (!base_samplerate && REPOSITION(current_sample, position)))
778         {
779 // Can't reset resampler since one seek operation is done 
780 // for every channel to be read at the same position.
782 // Use a conditional reset for just the case of different base_samplerates
783                 if(base_samplerate > 0)
784                 {
785                         if(normalized_sample_rate &&
786                                 normalized_sample_rate != base_samplerate && 
787                                 resample)
788                                 resample->reset(-1);
790                         normalized_sample = position;
791                         normalized_sample_rate = (int64_t)((base_samplerate > 0) ? 
792                                 base_samplerate : 
793                                 asset->sample_rate);
795 // Convert position to file's rate
796                         if(base_samplerate > 0)
797                                 current_sample = Units::round((double)position / 
798                                         base_samplerate * 
799                                         asset->sample_rate);
800                 }
801                 else
802                 {
803                         current_sample = position;
804                         normalized_sample = Units::round((double)position / 
805                                         asset->sample_rate * 
806                                         normalized_sample_rate);
807 // Can not set the normalized sample rate since this would reset the resampler.
808                 }
810                 result = file->set_audio_position(current_sample);
812                 if(result)
813                         printf("File::set_audio_position position=%d base_samplerate=%f asset=%p asset->sample_rate=%d\n",
814                                 position, base_samplerate, asset, asset->sample_rate);
815         }
817 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
819         return result;
822 int File::set_video_position(int64_t position, float base_framerate, int is_thread) 
824         int result = 0;
825         if(!file) return 0;
827 // Convert to file's rate
828         if(base_framerate > 0)
829                 position = (int64_t)((double)position / 
830                         base_framerate * 
831                         asset->frame_rate + 
832                         0.5);
835         if(video_thread && !is_thread)
836         {
837 // Call thread.  Thread calls this again to set the file state.
838                 video_thread->set_video_position(position);
839         }
840         else
841         if(current_frame != position)
842         {
843                 if(file)
844                 {
845                         current_frame = position;
846                         result = file->set_video_position(current_frame);
847                 }
848         }
850         return result;
853 // No resampling here.
854 int File::write_samples(double **buffer, int64_t len)
856         int result = 1;
857         
858         if(file)
859         {
860                 write_lock->lock("File::write_samples");
861                 result = file->write_samples(buffer, len);
862                 current_sample += len;
863                 normalized_sample += len;
864                 asset->audio_length += len;
865                 write_lock->unlock();
866         }
867         return result;
870 // Can't put any cmodel abstraction here because the filebase couldn't be
871 // parallel.
872 int File::write_frames(VFrame ***frames, int len)
874 // Store the counters in temps so the filebase can choose to overwrite them.
875         int result;
876         int current_frame_temp = current_frame;
877         int video_length_temp = asset->video_length;
878         write_lock->lock("File::write_frames");
883         result = file->write_frames(frames, len);
889         current_frame = current_frame_temp + len;
890         asset->video_length = video_length_temp + len;
891         write_lock->unlock();
892         return result;
895 int File::write_compressed_frame(VFrame *buffer)
897         int result = 0;
898         write_lock->lock("File::write_compressed_frame");
899         result = file->write_compressed_frame(buffer);
900         current_frame++;
901         asset->video_length++;
902         write_lock->unlock();
903         return result;
907 int File::write_audio_buffer(int64_t len)
909         int result = 0;
910         if(audio_thread)
911         {
912                 result = audio_thread->write_buffer(len);
913         }
914         return result;
917 int File::write_video_buffer(int64_t len)
919         int result = 0;
920         if(video_thread)
921         {
922                 result = video_thread->write_buffer(len);
923         }
925         return result;
928 double** File::get_audio_buffer()
930         if(audio_thread) return audio_thread->get_audio_buffer();
931         return 0;
934 VFrame*** File::get_video_buffer()
936         if(video_thread) return video_thread->get_video_buffer();
937         return 0;
941 int File::read_samples(double *buffer, int64_t len, int64_t base_samplerate, float *buffer_float)
943         int result = 0;
944         if(len < 0) return 0;
946 // Never try to read more samples than exist in the file
947         if (current_sample + len > asset->audio_length) {
948                 len = asset->audio_length - current_sample;
949         }
951 // Load with resampling 
952         if(file)
953         {
954 // Resample recursively calls this with the asset sample rate
955                 if(base_samplerate == 0) base_samplerate = asset->sample_rate;
957 //printf("File::read_samples 2 %d %d\n", base_samplerate, asset->sample_rate);
958                 if(base_samplerate != asset->sample_rate)
959                 {
960 //printf("File::read_samples 3\n");
961 //struct timeval start_time;
962 //gettimeofday(&start_time, 0);
963                         if (!file->prefer_samples_float())
964                         {
965                                 if(!resample)
966                                 {
967         //printf("File::read_samples 4\n");
968                                         resample = new Resample(this, asset->channels);
969                                 }
971         //printf("File::read_samples 5\n");
972                                 current_sample += resample->resample(buffer, 
973                                         len, 
974                                         asset->sample_rate, 
975                                         base_samplerate,
976                                         current_channel,
977                                         current_sample,
978                                         normalized_sample);
979         //printf("File::read_samples 6\n");
980                         } else
981                         {
982                                 if(!resample_float)
983                                 {
984         //printf("File::read_samples 4\n");
985                                         resample_float = new Resample_float(this, asset->channels);
986                                 }
988         //printf("File::read_samples 5\n");
989                                 current_sample += resample_float->resample(buffer, 
990                                         len, 
991                                         asset->sample_rate, 
992                                         base_samplerate,
993                                         current_channel,
994                                         current_sample,
995                                         normalized_sample);
996         //printf("File::read_samples 6\n");
998                         }
999 //printf("diff2: %lli\n", get_difference(&start_time));
1001                 }
1002                 else
1003 // Load directly
1004                 {
1005 //printf("File::read_samples 7\n");
1006                         if (buffer_float && file->prefer_samples_float())
1007                                 result = file->read_samples_float(buffer_float, len);
1008                         else
1009                                 result = file->read_samples(buffer, len);
1010 //printf("File::read_samples 8\n");
1011                         current_sample += len;
1012                 }
1014                 normalized_sample += len;
1015         }
1016         return result;
1019 int File::read_compressed_frame(VFrame *buffer)
1021         int result = 1;
1022         if(file)
1023                 result = file->read_compressed_frame(buffer);
1024         current_frame++;
1025         return result;
1028 int64_t File::compressed_frame_size()
1030         if(file)
1031                 return file->compressed_frame_size();
1032         else 
1033                 return 0;
1039 int File::read_frame(VFrame *frame, int is_thread)
1041         if(video_thread && !is_thread) return video_thread->read_frame(frame);
1043         if(file)
1044         {
1045                 int supported_colormodel = colormodel_supported(frame->get_color_model());
1046                 int advance_position = 1;
1048 // Test cache
1049                 if(use_cache &&
1050                         frame_cache->get_frame(frame,
1051                                 current_frame,
1052                                 current_layer,
1053                                 asset->frame_rate))
1054                 {
1055 // Can't advance position if cache used.
1056                         advance_position = 0;
1057                 }
1058                 else
1059 // Need temp
1060                 if(frame->get_color_model() != BC_COMPRESSED &&
1061                         (supported_colormodel != frame->get_color_model() ||
1062                         frame->get_w() != asset->width ||
1063                         frame->get_h() != asset->height))
1064                 {
1066 // Can't advance position here because it needs to be added to cache
1067                         if(temp_frame)
1068                         {
1069                                 if(!temp_frame->params_match(asset->width, asset->height, supported_colormodel))
1070                                 {
1071                                         delete temp_frame;
1072                                         temp_frame = 0;
1073                                 }
1074                         }
1076                         if(!temp_frame)
1077                         {
1078                                 temp_frame = new VFrame(0,
1079                                         asset->width,
1080                                         asset->height,
1081                                         supported_colormodel);
1082                         }
1084                         temp_frame->copy_stacks(frame);
1085                         file->read_frame(temp_frame);
1086                         cmodel_transfer(frame->get_rows(), 
1087                                 temp_frame->get_rows(),
1088                                 temp_frame->get_y(),
1089                                 temp_frame->get_u(),
1090                                 temp_frame->get_v(),
1091                                 frame->get_y(),
1092                                 frame->get_u(),
1093                                 frame->get_v(),
1094                                 0, 
1095                                 0, 
1096                                 temp_frame->get_w(), 
1097                                 temp_frame->get_h(),
1098                                 0, 
1099                                 0, 
1100                                 frame->get_w(), 
1101                                 frame->get_h(),
1102                                 temp_frame->get_color_model(), 
1103                                 frame->get_color_model(),
1104                                 0,
1105                                 temp_frame->get_w(),
1106                                 frame->get_w());
1107                 }
1108                 else
1109                 {
1110 // Can't advance position here because it needs to be added to cache
1111                         file->read_frame(frame);
1112                 }
1114                 if(use_cache) frame_cache->put_frame(frame,
1115                         current_frame,
1116                         current_layer,
1117                         asset->frame_rate,
1118                         1);
1119 // printf("File::read_frame\n");
1120 // frame->dump_params();
1122                 if(advance_position) current_frame++;
1123                 return 0;
1124         }
1125         else
1126                 return 1;
1129 int File::can_copy_from(Edit *edit, int64_t position, int output_w, int output_h)
1131         if(file)
1132         {
1133                 return edit->asset->width == output_w &&
1134                         edit->asset->height == output_h &&
1135                         file->can_copy_from(edit, position);
1136         }
1137         else
1138                 return 0;
1141 // Fill in queries about formats when adding formats here.
1144 int File::strtoformat(char *format)
1146         return strtoformat(0, format);
1149 int File::strtoformat(ArrayList<PluginServer*> *plugindb, char *format)
1151         if(!strcasecmp(format, _(AC3_NAME))) return FILE_AC3;
1152         else
1153         if(!strcasecmp(format, _(WAV_NAME))) return FILE_WAV;
1154         else
1155         if(!strcasecmp(format, _(PCM_NAME))) return FILE_PCM;
1156         else
1157         if(!strcasecmp(format, _(AU_NAME))) return FILE_AU;
1158         else
1159         if(!strcasecmp(format, _(AIFF_NAME))) return FILE_AIFF;
1160         else
1161         if(!strcasecmp(format, _(SND_NAME))) return FILE_SND;
1162         else
1163         if(!strcasecmp(format, _(PNG_NAME))) return FILE_PNG;
1164         else
1165         if(!strcasecmp(format, _(PNG_LIST_NAME))) return FILE_PNG_LIST;
1166         else
1167         if(!strcasecmp(format, _(TIFF_NAME))) return FILE_TIFF;
1168         else
1169         if(!strcasecmp(format, _(TIFF_LIST_NAME))) return FILE_TIFF_LIST;
1170         else
1171         if(!strcasecmp(format, _(JPEG_NAME))) return FILE_JPEG;
1172         else
1173         if(!strcasecmp(format, _(JPEG_LIST_NAME))) return FILE_JPEG_LIST;
1174         else
1175         if(!strcasecmp(format, _(EXR_NAME))) return FILE_EXR;
1176         else
1177         if(!strcasecmp(format, _(EXR_LIST_NAME))) return FILE_EXR_LIST;
1178         else
1179         if(!strcasecmp(format, _(YUV_NAME))) return FILE_YUV;
1180         else
1181         if(!strcasecmp(format, _(CR2_NAME))) return FILE_CR2;
1182         else
1183         if(!strcasecmp(format, _(MPEG_NAME))) return FILE_MPEG;
1184         else
1185         if(!strcasecmp(format, _(AMPEG_NAME))) return FILE_AMPEG;
1186         else
1187         if(!strcasecmp(format, _(VMPEG_NAME))) return FILE_VMPEG;
1188         else
1189         if(!strcasecmp(format, _(TGA_NAME))) return FILE_TGA;
1190         else
1191         if(!strcasecmp(format, _(TGA_LIST_NAME))) return FILE_TGA_LIST;
1192         else
1193         if(!strcasecmp(format, _(MOV_NAME))) return FILE_MOV;
1194         else
1195         if(!strcasecmp(format, _(AVI_NAME))) return FILE_AVI;
1196         else
1197         if(!strcasecmp(format, _(AVI_LAVTOOLS_NAME))) return FILE_AVI_LAVTOOLS;
1198         else
1199         if(!strcasecmp(format, _(AVI_ARNE2_NAME))) return FILE_AVI_ARNE2;
1200         else
1201         if(!strcasecmp(format, _(AVI_ARNE1_NAME))) return FILE_AVI_ARNE1;
1202         else
1203         if(!strcasecmp(format, _(AVI_AVIFILE_NAME))) return FILE_AVI_AVIFILE;
1204         else
1205         if(!strcasecmp(format, _(OGG_NAME))) return FILE_OGG;
1206         else
1207         if(!strcasecmp(format, _(VORBIS_NAME))) return FILE_VORBIS;
1208         else
1209         if(!strcasecmp(format, _(RAWDV_NAME))) return FILE_RAWDV;
1210         return 0;
1213 char* File::formattostr(int format)
1215         return formattostr(0, format);
1218 char* File::formattostr(ArrayList<PluginServer*> *plugindb, int format)
1220         switch(format)
1221         {
1222                 case FILE_AC3:
1223                         return _(AC3_NAME);
1224                         break;
1225                 case FILE_WAV:
1226                         return _(WAV_NAME);
1227                         break;
1228                 case FILE_PCM:
1229                         return _(PCM_NAME);
1230                         break;
1231                 case FILE_AU:
1232                         return _(AU_NAME);
1233                         break;
1234                 case FILE_AIFF:
1235                         return _(AIFF_NAME);
1236                         break;
1237                 case FILE_SND:
1238                         return _(SND_NAME);
1239                         break;
1240                 case FILE_PNG:
1241                         return _(PNG_NAME);
1242                         break;
1243                 case FILE_PNG_LIST:
1244                         return _(PNG_LIST_NAME);
1245                         break;
1246                 case FILE_JPEG:
1247                         return _(JPEG_NAME);
1248                         break;
1249                 case FILE_JPEG_LIST:
1250                         return _(JPEG_LIST_NAME);
1251                         break;
1252                 case FILE_CR2:
1253                         return _(CR2_NAME);
1254                         break;
1255                 case FILE_EXR:
1256                         return _(EXR_NAME);
1257                         break;
1258                 case FILE_EXR_LIST:
1259                         return _(EXR_LIST_NAME);
1260                         break;
1261                 case FILE_YUV:
1262                         return _(YUV_NAME);
1263                         break;
1264                 case FILE_MPEG:
1265                         return _(MPEG_NAME);
1266                         break;
1267                 case FILE_AMPEG:
1268                         return _(AMPEG_NAME);
1269                         break;
1270                 case FILE_VMPEG:
1271                         return _(VMPEG_NAME);
1272                         break;
1273                 case FILE_TGA:
1274                         return _(TGA_NAME);
1275                         break;
1276                 case FILE_TGA_LIST:
1277                         return _(TGA_LIST_NAME);
1278                         break;
1279                 case FILE_TIFF:
1280                         return _(TIFF_NAME);
1281                         break;
1282                 case FILE_TIFF_LIST:
1283                         return _(TIFF_LIST_NAME);
1284                         break;
1285                 case FILE_MOV:
1286                         return _(MOV_NAME);
1287                         break;
1288                 case FILE_AVI_LAVTOOLS:
1289                         return _(AVI_LAVTOOLS_NAME);
1290                         break;
1291                 case FILE_AVI:
1292                         return _(AVI_NAME);
1293                         break;
1294                 case FILE_AVI_ARNE2:
1295                         return _(AVI_ARNE2_NAME);
1296                         break;
1297                 case FILE_AVI_ARNE1:
1298                         return _(AVI_ARNE1_NAME);
1299                         break;
1300                 case FILE_AVI_AVIFILE:
1301                         return _(AVI_AVIFILE_NAME);
1302                         break;
1303                 case FILE_OGG:
1304                         return _(OGG_NAME);
1305                         break;
1306                 case FILE_VORBIS:
1307                         return _(VORBIS_NAME);
1308                         break;
1309                 case FILE_RAWDV:
1310                         return _(RAWDV_NAME);
1311                         break;
1312                 default:
1313                         return _("Unknown");
1314                         break;
1315         }
1316         return "Unknown";
1319 int File::strtobits(char *bits)
1321         if(!strcasecmp(bits, _(NAME_8BIT))) return BITSLINEAR8;
1322         if(!strcasecmp(bits, _(NAME_16BIT))) return BITSLINEAR16;
1323         if(!strcasecmp(bits, _(NAME_24BIT))) return BITSLINEAR24;
1324         if(!strcasecmp(bits, _(NAME_32BIT))) return BITSLINEAR32;
1325         if(!strcasecmp(bits, _(NAME_ULAW))) return BITSULAW;
1326         if(!strcasecmp(bits, _(NAME_ADPCM))) return BITS_ADPCM;
1327         if(!strcasecmp(bits, _(NAME_FLOAT))) return BITSFLOAT;
1328         if(!strcasecmp(bits, _(NAME_IMA4))) return BITSIMA4;
1329         return BITSLINEAR16;
1332 char* File::bitstostr(int bits)
1334 //printf("File::bitstostr\n");
1335         switch(bits)
1336         {
1337                 case BITSLINEAR8:
1338                         return (NAME_8BIT);
1339                         break;
1340                 case BITSLINEAR16:
1341                         return (NAME_16BIT);
1342                         break;
1343                 case BITSLINEAR24:
1344                         return (NAME_24BIT);
1345                         break;
1346                 case BITSLINEAR32:
1347                         return (NAME_32BIT);
1348                         break;
1349                 case BITSULAW:
1350                         return (NAME_ULAW);
1351                         break;
1352                 case BITS_ADPCM:
1353                         return (NAME_ADPCM);
1354                         break;
1355                 case BITSFLOAT:
1356                         return (NAME_FLOAT);
1357                         break;
1358                 case BITSIMA4:
1359                         return (NAME_IMA4);
1360                         break;
1361         }
1362         return "Unknown";
1367 int File::str_to_byteorder(char *string)
1369         if(!strcasecmp(string, _("Lo Hi"))) return 1;
1370         return 0;
1373 char* File::byteorder_to_str(int byte_order)
1375         if(byte_order) return _("Lo Hi");
1376         return _("Hi Lo");
1379 int File::bytes_per_sample(int bits)
1381         switch(bits)
1382         {
1383                 case BITSLINEAR8:
1384                         return 1;
1385                         break;
1386                 case BITSLINEAR16:
1387                         return 2;
1388                         break;
1389                 case BITSLINEAR24:
1390                         return 3;
1391                         break;
1392                 case BITSLINEAR32:
1393                         return 4;
1394                         break;
1395                 case BITSULAW:
1396                         return 1;
1397                         break;
1398                 case BITSIMA4:
1399                         return 1;
1400                         break;
1401         }
1402         return 1;
1409 int File::get_best_colormodel(int driver)
1411         return get_best_colormodel(asset, driver);
1414 int File::get_best_colormodel(Asset *asset, int driver)
1416         switch(asset->format)
1417         {
1418                 case FILE_RAWDV:
1419                         return FileDV::get_best_colormodel(asset, driver);
1420                         break;
1422                 case FILE_MOV:
1423                         return FileMOV::get_best_colormodel(asset, driver);
1424                         break;
1425                 
1426         case FILE_AVI:
1427                         return FileMOV::get_best_colormodel(asset, driver);
1428                         break;
1430                 case FILE_MPEG:
1431                         return FileMPEG::get_best_colormodel(asset, driver);
1432                         break;
1433                 
1434                 case FILE_JPEG:
1435                 case FILE_JPEG_LIST:
1436                         return FileJPEG::get_best_colormodel(asset, driver);
1437                         break;
1439                 case FILE_EXR:
1440                 case FILE_EXR_LIST:
1441                         return FileEXR::get_best_colormodel(asset, driver);
1442                         break;
1443                 
1444                 case FILE_YUV:
1445                         return FileYUV::get_best_colormodel(asset, driver);
1446                         break;
1448                 case FILE_PNG:
1449                 case FILE_PNG_LIST:
1450                         return FilePNG::get_best_colormodel(asset, driver);
1451                         break;
1452                 
1453                 case FILE_TGA:
1454                 case FILE_TGA_LIST:
1455                         return FileTGA::get_best_colormodel(asset, driver);
1456                         break;
1457         }
1459         return BC_RGB888;
1463 int File::colormodel_supported(int colormodel)
1465         if(file)
1466                 return file->colormodel_supported(colormodel);
1468         return BC_RGB888;
1472 int64_t File::get_memory_usage() 
1474         int64_t result = 0;
1475         if(temp_frame) result += temp_frame->get_data_size();
1476         if(file) result += file->get_memory_usage();
1477         result += frame_cache->get_memory_usage();
1478         if(video_thread) result += video_thread->get_memory_usage();
1480         if(result < MIN_CACHEITEM_SIZE) result = MIN_CACHEITEM_SIZE;
1481         return result;
1484 FrameCache* File::get_frame_cache()
1486         return frame_cache;
1489 int File::supports_video(ArrayList<PluginServer*> *plugindb, char *format)
1491         int i, format_i = strtoformat(plugindb, format);
1492         
1493         return supports_video(format_i);
1494         return 0;
1497 int File::supports_audio(ArrayList<PluginServer*> *plugindb, char *format)
1499         int i, format_i = strtoformat(plugindb, format);
1501         return supports_audio(format_i);
1502         return 0;
1506 int File::supports_video(int format)
1508 //printf("File::supports_video %d\n", format);
1509         switch(format)
1510         {
1511                 case FILE_OGG:
1512                 case FILE_MOV:
1513                 case FILE_JPEG:
1514                 case FILE_JPEG_LIST:
1515                 case FILE_CR2:
1516                 case FILE_EXR:
1517                 case FILE_EXR_LIST:
1518                 case FILE_YUV:
1519                 case FILE_PNG:
1520                 case FILE_PNG_LIST:
1521                 case FILE_TGA:
1522                 case FILE_TGA_LIST:
1523                 case FILE_TIFF:
1524                 case FILE_TIFF_LIST:
1525                 case FILE_VMPEG:
1526                 case FILE_AVI_LAVTOOLS:
1527                 case FILE_AVI_ARNE2:
1528                 case FILE_AVI:
1529                 case FILE_AVI_ARNE1:
1530                 case FILE_AVI_AVIFILE:
1531                 case FILE_RAWDV:
1532                         return 1;
1533                         break;
1535                 default:
1536                         return 0;
1537                         break;
1538         }
1541 int File::supports_audio(int format)
1543         switch(format)
1544         {
1545                 case FILE_AC3:
1546                 case FILE_PCM:
1547                 case FILE_WAV:
1548                 case FILE_MOV:
1549                 case FILE_OGG:
1550                 case FILE_VORBIS:
1551                 case FILE_AMPEG:
1552                 case FILE_AU:
1553                 case FILE_AIFF:
1554                 case FILE_SND:
1555                 case FILE_AVI:
1556                 case FILE_AVI_LAVTOOLS:
1557                 case FILE_AVI_ARNE2:
1558                 case FILE_AVI_ARNE1:
1559                 case FILE_AVI_AVIFILE:
1560                 case FILE_RAWDV:
1561                         return 1;
1562                 
1563                 default:
1564                         return 0;
1565                         break;
1566         }
1569 char* File::get_tag(int format)
1571         switch(format)
1572         {
1573                 case FILE_AC3:          return "ac3";
1574                 case FILE_AIFF:         return "aif";
1575                 case FILE_AMPEG:        return "mp3";
1576                 case FILE_AU:           return "au";
1577                 case FILE_AVI:          return "avi";
1578                 case FILE_EXR:          return "exr";
1579                 case FILE_EXR_LIST:     return "exr";
1580                 case FILE_JPEG:         return "jpg";
1581                 case FILE_JPEG_LIST:    return "jpg";
1582                 case FILE_MOV:          return "mov";
1583                 case FILE_OGG:          return "ogg";
1584                 case FILE_PCM:          return "pcm";
1585                 case FILE_PNG:          return "png";
1586                 case FILE_PNG_LIST:     return "png";
1587                 case FILE_TGA:          return "tga";
1588                 case FILE_TGA_LIST:     return "tga";
1589                 case FILE_TIFF:         return "tif";
1590                 case FILE_TIFF_LIST:    return "tif";
1591                 case FILE_VMPEG:        return "m2v";
1592                 case FILE_VORBIS:       return "ogg";
1593                 case FILE_WAV:          return "wav";
1594         }
1595         return 0;
1598 PackagingEngine *File::new_packaging_engine(Asset *asset)
1600         PackagingEngine *result;
1601         switch (asset->format)
1602         {
1603                 case FILE_OGG:
1604                         result = (PackagingEngine*)new PackagingEngineOGG();
1605                         break;
1606                 default:
1607                         result = (PackagingEngine*) new PackagingEngineDefault();
1608                         break;
1609         }
1611         return result;