Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / cinelerra / file.C
blob2429172b1b3f9b9456e479f9c3cf2711a4dade4d
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 = Asset_GC(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         delete format_completion;
65         delete write_lock;
66         if(frame_cache) delete frame_cache;
69 void File::reset_parameters()
71         file = 0;
72         audio_thread = 0;
73         video_thread = 0;
74         getting_options = 0;
75         format_window = 0;
76         temp_frame = 0;
77         current_sample = 0;
78         current_frame = 0;
79         current_channel = 0;
80         current_layer = 0;
81         normalized_sample = 0;
82         normalized_sample_rate = 0;
83         resample = 0;
84         resample_float = 0;
85         use_cache = 0;
86         preferences = 0;
87         playback_subtitle = -1;
88         interpolate_raw = 1;
91 int File::raise_window()
93         if(getting_options && format_window)
94         {
95                 format_window->raise_window();
96                 format_window->flush();
97         }
98         return 0;
101 void File::close_window()
103         if(getting_options)
104         {
105                 format_window->lock_window("File::close_window");
106                 format_window->set_done(1);
107                 format_window->unlock_window();
108                 getting_options = 0;
109         }
112 int File::get_options(FormatTools *format,  
113         int audio_options,
114         int video_options)
116         BC_WindowBase *parent_window = format->window;
117         ArrayList<PluginServer*> *plugindb = format->plugindb;
118         Asset_GC asset = format->asset;
120         getting_options = 1;
121         format_completion->lock("File::get_options");
122         switch(asset->format)
123         {
124                 case FILE_AC3:
125                         FileAC3::get_parameters(parent_window,
126                                 asset,
127                                 format_window,
128                                 audio_options,
129                                 video_options);
130                         break;
131                 case FILE_RAWDV:
132                         FileDV::get_parameters(parent_window,
133                                 asset,
134                                 format_window,
135                                 audio_options,
136                                 video_options);
137                         break;
138                 case FILE_PCM:
139                 case FILE_WAV:
140                 case FILE_AU:
141                 case FILE_AIFF:
142                 case FILE_SND:
143                         FileSndFile::get_parameters(parent_window, 
144                                 asset, 
145                                 format_window, 
146                                 audio_options, 
147                                 video_options);
148                         break;
149                 case FILE_MOV:
150                         FileMOV::get_parameters(parent_window, 
151                                 asset, 
152                                 format_window, 
153                                 audio_options, 
154                                 video_options,
155                                 format->locked_compressor);
156                         break;
157                 case FILE_AMPEG:
158                 case FILE_VMPEG:
159                         FileMPEG::get_parameters(parent_window, 
160                                 asset, 
161                                 format_window, 
162                                 audio_options, 
163                                 video_options);
164                         break;
165                 case FILE_AVI:
166                         FileMOV::get_parameters(parent_window, 
167                                 asset, 
168                                 format_window, 
169                                 audio_options, 
170                                 video_options,
171                                 format->locked_compressor);
172                         break;
173                 case FILE_AVI_LAVTOOLS:
174                 case FILE_AVI_ARNE2:
175                 case FILE_AVI_ARNE1:
176                 case FILE_AVI_AVIFILE:
177                         FileAVI::get_parameters(parent_window, 
178                                 asset, 
179                                 format_window, 
180                                 audio_options, 
181                                 video_options,
182                                 format->locked_compressor);
183                         break;
184                 case FILE_JPEG:
185                 case FILE_JPEG_LIST:
186                         FileJPEG::get_parameters(parent_window, 
187                                 asset, 
188                                 format_window, 
189                                 audio_options, 
190                                 video_options);
191                         break;
192                 case FILE_EXR:
193                 case FILE_EXR_LIST:
194                         FileEXR::get_parameters(parent_window, 
195                                 asset, 
196                                 format_window, 
197                                 audio_options, 
198                                 video_options);
199                         break;
200                 case FILE_YUV:
201                         FileYUV::get_parameters(parent_window,
202                                 asset,
203                                 format_window,
204                                 video_options,
205                                 format);
206                         break;
207                 case FILE_PNG:
208                 case FILE_PNG_LIST:
209                         FilePNG::get_parameters(parent_window, 
210                                 asset, 
211                                 format_window, 
212                                 audio_options, 
213                                 video_options);
214                         break;
215                 case FILE_TGA:
216                 case FILE_TGA_LIST:
217                         FileTGA::get_parameters(parent_window, 
218                                 asset, 
219                                 format_window, 
220                                 audio_options, 
221                                 video_options);
222                         break;
223                 case FILE_TIFF:
224                 case FILE_TIFF_LIST:
225                         FileTIFF::get_parameters(parent_window, 
226                                 asset, 
227                                 format_window, 
228                                 audio_options, 
229                                 video_options);
230                         break;
231                 case FILE_OGG:
232                         FileOGG::get_parameters(parent_window,
233                                 asset,
234                                 format_window,
235                                 audio_options,
236                                 video_options);
237                         break;
238                 default:
239                         break;
240         }
242         if(!format_window)
243         {
244                 ErrorBox *errorbox = new ErrorBox(PROGRAM_NAME ": Error",
245                         parent_window->get_abs_cursor_x(1),
246                         parent_window->get_abs_cursor_y(1));
247                 format_window = errorbox;
248                 getting_options = 1;
249                 if(audio_options)
250                         errorbox->create_objects(_("This format doesn't support audio."));
251                 else
252                 if(video_options)
253                         errorbox->create_objects(_("This format doesn't support video."));
254                 errorbox->run_window();
255                 delete errorbox;
256         }
258         getting_options = 0;
259         format_window = 0;
260         format_completion->unlock();
261         return 0;
264 void File::set_asset(Asset_GC asset)
266         this->asset->copy_from(*asset, 1);
269 int File::set_processors(int cpus)   // Set the number of cpus for certain codecs
271         this->cpus = cpus;
272         return 0;
275 int File::set_preload(int64_t size)
277         this->playback_preload = size;
278         return 0;
281 void File::set_subtitle(int value)
283         this->playback_subtitle = value;
286 void File::set_interpolate_raw(int value)
288         this->interpolate_raw = value;
291 void File::set_white_balance_raw(int value)
293         this->white_balance_raw = value;
296 void File::set_cache_frames(int value)
298         use_cache = value;
301 int File::purge_cache()
303         return frame_cache->delete_oldest();
316 int File::open_file(Preferences *preferences, 
317         Asset_GC asset, 
318         int rd, 
319         int wr,
320         int64_t base_samplerate,
321         float base_framerate)
323         this->preferences = preferences;
324         this->asset->copy_from(*asset, 1);
325         file = 0;
328         switch(this->asset->format)
329         {
330 // get the format now
331 // If you add another format to case 0, you also need to add another case for the
332 // file format #define.
333                 case FILE_UNKNOWN:
334                         FILE *stream;
335                         if(!(stream = fopen(this->asset->path, "rb")))
336                         {
337 // file not found
338                                 return 1;
339                         }
341                         char test[16];
342                         fread(test, 16, 1, stream);
344                         if(FileDV::check_sig(this->asset))
345                         {
346 // libdv
347                                 fclose(stream);
348                                 file = new FileDV(this->asset, this);
349                         }
350                         else if(FileSndFile::check_sig(this->asset))
351                         {
352 // libsndfile
353                                 fclose(stream);
354                                 file = new FileSndFile(this->asset, this);
355                         }
356                         else
357                         if(FilePNG::check_sig(this->asset))
358                         {
359 // PNG file
360                                 fclose(stream);
361                                 file = new FilePNG(this->asset, this);
362                         }
363                         else
364                         if(FileJPEG::check_sig(this->asset))
365                         {
366 // JPEG file
367                                 fclose(stream);
368                                 file = new FileJPEG(this->asset, this);
369                         }
370                         else
371                         if(FileEXR::check_sig(this->asset, test))
372                         {
373 // EXR file
374                                 fclose(stream);
375                                 file = new FileEXR(this->asset, this);
376                         }
377                         else
378                         if(FileYUV::check_sig(this->asset))
379                         {
380 // YUV file
381                                 fclose(stream);
382                                 file = new FileYUV(this->asset, this);
383                         }
384                         else
385                         if(FileCR2::check_sig(this->asset))
386                         {
387 // JPEG file
388                                 fclose(stream);
389                                 file = new FileCR2(this->asset, this);
390                         }
391                         else
392                         if(FileTGA::check_sig(this->asset))
393                         {
394 // TGA file
395                                 fclose(stream);
396                                 file = new FileTGA(this->asset, this);
397                         }
398                         else
399                         if(FileTIFF::check_sig(this->asset))
400                         {
401 // TIFF file
402                                 fclose(stream);
403                                 file = new FileTIFF(this->asset, this);
404                         }
405                         else
406                         if(FileOGG::check_sig(this->asset))
407                         {
408 // OGG file
409                                 fclose(stream);
410                                 file = new FileOGG(this->asset, this);
411                         }
412                         else
413                         if(FileVorbis::check_sig(this->asset))
414                         {
415 // VorbisFile file
416                                 fclose(stream);
417                                 file = new FileVorbis(this->asset, this);
418                         }
419                         else
420                         if(FileOGG::check_sig(this->asset))
421                         {
422 // OGG file.  Doesn't always work with pure audio files.
423                                 fclose(stream);
424                                 file = new FileOGG(this->asset, this);
425                         }
426                         else
427                         if(FileMPEG::check_sig(this->asset))
428                         {
429 // MPEG file
430                                 fclose(stream);
431                                 file = new FileMPEG(this->asset, this);
432                         }
433                         else
434                         if(test[0] == '<' && test[1] == 'E' && test[2] == 'D' && test[3] == 'L' && test[4] == '>' ||
435                                 test[0] == '<' && test[1] == 'H' && test[2] == 'T' && test[3] == 'A' && test[4] == 'L' && test[5] == '>' ||
436                                 test[0] == '<' && test[1] == '?' && test[2] == 'x' && test[3] == 'm' && test[4] == 'l')
437                         {
438 // XML file
439                                 fclose(stream);
440                                 return FILE_IS_XML;
441                         }    // can't load project file
442                         else
443                         if(FileMOV::check_sig(this->asset))
444                         {
445 // MOV file
446 // should be last because quicktime lacks a magic number
447                                 fclose(stream);
448                                 file = new FileMOV(this->asset, this);
449                         }
450                         else
451                         {
452 // PCM file
453                                 fclose(stream);
454                                 return FILE_UNRECOGNIZED_CODEC;
455                         }   // need more info
456                         break;
458 // format already determined
459                 case FILE_AC3:
460                         file = new FileAC3(this->asset, this);
461                         break;
463                 case FILE_PCM:
464                 case FILE_WAV:
465                 case FILE_AU:
466                 case FILE_AIFF:
467                 case FILE_SND:
468 //printf("File::open_file 1\n");
469                         file = new FileSndFile(this->asset, this);
470                         break;
472                 case FILE_PNG:
473                 case FILE_PNG_LIST:
474                         file = new FilePNG(this->asset, this);
475                         break;
477                 case FILE_JPEG:
478                 case FILE_JPEG_LIST:
479                         file = new FileJPEG(this->asset, this);
480                         break;
482                 case FILE_EXR:
483                 case FILE_EXR_LIST:
484                         file = new FileEXR(this->asset, this);
485                         break;
487                 case FILE_YUV:
488                         file = new FileYUV(this->asset, this);
489                         break;
491                 case FILE_CR2:
492                         file = new FileCR2(this->asset, this);
493                         break;
495                 case FILE_TGA_LIST:
496                 case FILE_TGA:
497                         file = new FileTGA(this->asset, this);
498                         break;
500                 case FILE_TIFF:
501                 case FILE_TIFF_LIST:
502                         file = new FileTIFF(this->asset, this);
503                         break;
505                 case FILE_MOV:
506                         file = new FileMOV(this->asset, this);
507                         break;
509                 case FILE_MPEG:
510                 case FILE_AMPEG:
511                 case FILE_VMPEG:
512                         file = new FileMPEG(this->asset, this);
513                         break;
515                 case FILE_OGG:
516                         file = new FileOGG(this->asset, this);
517                         break;
519                 case FILE_VORBIS:
520                         file = new FileVorbis(this->asset, this);
521                         break;
523                 case FILE_AVI:
524                         file = new FileMOV(this->asset, this);
525                         break;
527                 case FILE_AVI_LAVTOOLS:
528                 case FILE_AVI_ARNE2:
529                 case FILE_AVI_ARNE1:
530                 case FILE_AVI_AVIFILE:
531                         file = new FileAVI(this->asset, this);
532                         break;
534                 case FILE_RAWDV:
535                         file = new FileDV(this->asset, this);
536                         break;
538 // try plugins
539                 default:
540                         return 1;
541                         break;
542         }
544 // Reopen file with correct parser and get header.
545         if(file->open_file(rd, wr))
546         {
547                 delete file;
548                 file = 0;
549         }
552 // Set extra writing parameters to mandatory settings.
553         if(file && wr)
554         {
555                 if(this->asset->dither) file->set_dither();
556         }
559 // Synchronize header parameters
560         if(file)
561         {
562                 asset->copy_from(*this->asset, 1);
563         }
565         if(file)
566                 return FILE_OK;
567         else
568                 return FILE_NOT_FOUND;
571 int File::close_file(int ignore_thread)
573         if(!ignore_thread)
574         {
575                 stop_audio_thread();
576                 stop_video_thread();
577         }
579         if(file) 
580         {
581 // The file's asset is a copy of the argument passed to open_file so the
582 // user must copy lengths from the file's asset.
583                 if(asset && file->wr)
584                 {
585                         asset->audio_length = current_sample;
586                         asset->video_length = current_frame;
587                 }
588                 file->close_file();
589                 delete file;
590         }
592         if(resample) delete resample;
593         if(resample_float) delete resample_float;
595         reset_parameters();
596         return 0;
601 int File::get_index(char *index_path)
603         if(file)
604         {
605                 return file->get_index(index_path);
606         }
607         return 1;
612 int File::start_audio_thread(int64_t buffer_size, int ring_buffers)
614         if(!audio_thread)
615         {
616                 audio_thread = new FileThread(this, 1, 0);
617                 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
618         }
619         return 0;
622 int File::start_video_thread(int64_t buffer_size, 
623         int color_model, 
624         int ring_buffers, 
625         int compressed)
627         if(!video_thread)
628         {
629                 video_thread = new FileThread(this, 0, 1);
630                 video_thread->start_writing(buffer_size, 
631                         color_model, 
632                         ring_buffers, 
633                         compressed);
634         }
635         return 0;
638 int File::start_video_decode_thread()
640 // Currently, CR2 is the only one which won't work asynchronously, so
641 // we're not using a virtual function yet.
642         if(!video_thread && asset->format != FILE_CR2)
643         {
644                 video_thread = new FileThread(this, 0, 1);
645                 video_thread->start_reading();
646         }
649 int File::stop_audio_thread()
651         if(audio_thread)
652         {
653                 audio_thread->stop_writing();
654                 delete audio_thread;
655                 audio_thread = 0;
656         }
657         return 0;
660 int File::stop_video_thread()
662         if(video_thread)
663         {
664                 video_thread->stop_reading();
665                 video_thread->stop_writing();
666                 delete video_thread;
667                 video_thread = 0;
668         }
669         return 0;
672 FileThread* File::get_video_thread()
674         return video_thread;
677 int File::set_channel(int channel) 
679         if(file && channel < asset->channels)
680         {
681                 current_channel = channel;
682                 return 0;
683         }
684         else
685                 return 1;
688 int File::set_layer(int layer, int is_thread) 
690         if(file && layer < asset->layers)
691         {
692                 if(!is_thread && video_thread)
693                 {
694                         video_thread->set_layer(layer);
695                 }
696                 else
697                 {
698                         current_layer = layer;
699                 }
700                 return 0; 
701         }
702         else
703                 return 1;
706 int64_t File::get_audio_length(int64_t base_samplerate) 
708         int64_t result = asset->audio_length;
709         if(result > 0)
710         {
711                 if(base_samplerate > 0)
712                         return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
713                 else
714                         return result;
715         }
716         else
717                 return -1;
720 int64_t File::get_video_length(float base_framerate)
722         int64_t result = asset->video_length;
723         if(result > 0)
724         {
725                 if(base_framerate > 0)
726                         return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5); 
727                 else
728                         return result;
729         }
730         else
731                 return -1;  // infinity
735 int64_t File::get_video_position(float base_framerate) 
737         if(base_framerate > 0)
738                 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
739         else
740                 return current_frame;
743 int64_t File::get_audio_position(int64_t base_samplerate) 
745         if(base_samplerate > 0)
746         {
747                 if(normalized_sample_rate == base_samplerate)
748                         return normalized_sample;
749                 else
750                         return (int64_t)((double)current_sample / 
751                                 asset->sample_rate * 
752                                 base_samplerate + 
753                                 0.5);
754         }
755         else
756                 return current_sample;
761 // The base samplerate must be nonzero if the base samplerate in the calling
762 // function is expected to change as this forces the resampler to reset.
764 int File::set_audio_position(int64_t position, float base_samplerate) 
766         int result = 0;
768         if(!file) return 1;
770 #define REPOSITION(x, y) \
771         (labs((x) - (y)) > 1)
775         if((base_samplerate && REPOSITION(normalized_sample, position)) ||
776                 (!base_samplerate && REPOSITION(current_sample, position)))
777         {
778 // Can't reset resampler since one seek operation is done 
779 // for every channel to be read at the same position.
781 // Use a conditional reset for just the case of different base_samplerates
782                 if(base_samplerate > 0)
783                 {
784                         if(normalized_sample_rate &&
785                                 normalized_sample_rate != base_samplerate && 
786                                 resample)
787                                 resample->reset(-1);
789                         normalized_sample = position;
790                         normalized_sample_rate = (int64_t)((base_samplerate > 0) ? 
791                                 base_samplerate : 
792                                 asset->sample_rate);
794 // Convert position to file's rate
795                         if(base_samplerate > 0)
796                                 current_sample = Units::round((double)position / 
797                                         base_samplerate * 
798                                         asset->sample_rate);
799                 }
800                 else
801                 {
802                         current_sample = position;
803                         normalized_sample = Units::round((double)position / 
804                                         asset->sample_rate * 
805                                         normalized_sample_rate);
806 // Can not set the normalized sample rate since this would reset the resampler.
807                 }
809                 result = file->set_audio_position(current_sample);
811                 if(result)
812                         printf("File::set_audio_position position=%d base_samplerate=%f asset=%p asset->sample_rate=%d\n",
813                                 position, base_samplerate, asset.get(), asset->sample_rate);
814         }
816 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
818         return result;
821 int File::set_video_position(int64_t position, float base_framerate, int is_thread) 
823         int result = 0;
824         if(!file) return 0;
826 // Convert to file's rate
827         if(base_framerate > 0)
828                 position = (int64_t)((double)position / 
829                         base_framerate * 
830                         asset->frame_rate + 
831                         0.5);
834         if(video_thread && !is_thread)
835         {
836 // Call thread.  Thread calls this again to set the file state.
837                 video_thread->set_video_position(position);
838         }
839         else
840         if(current_frame != position)
841         {
842                 if(file)
843                 {
844                         current_frame = position;
845                         result = file->set_video_position(current_frame);
846                 }
847         }
849         return result;
852 // No resampling here.
853 int File::write_samples(double **buffer, int64_t len)
855         int result = 1;
856         
857         if(file)
858         {
859                 write_lock->lock("File::write_samples");
860                 result = file->write_samples(buffer, len);
861                 current_sample += len;
862                 normalized_sample += len;
863                 asset->audio_length += len;
864                 write_lock->unlock();
865         }
866         return result;
869 // Can't put any cmodel abstraction here because the filebase couldn't be
870 // parallel.
871 int File::write_frames(VFrame ***frames, int len)
873 // Store the counters in temps so the filebase can choose to overwrite them.
874         int result;
875         int current_frame_temp = current_frame;
876         int video_length_temp = asset->video_length;
877         write_lock->lock("File::write_frames");
882         result = file->write_frames(frames, len);
888         current_frame = current_frame_temp + len;
889         asset->video_length = video_length_temp + len;
890         write_lock->unlock();
891         return result;
894 int File::write_compressed_frame(VFrame *buffer)
896         int result = 0;
897         write_lock->lock("File::write_compressed_frame");
898         result = file->write_compressed_frame(buffer);
899         current_frame++;
900         asset->video_length++;
901         write_lock->unlock();
902         return result;
906 int File::write_audio_buffer(int64_t len)
908         int result = 0;
909         if(audio_thread)
910         {
911                 result = audio_thread->write_buffer(len);
912         }
913         return result;
916 int File::write_video_buffer(int64_t len)
918         int result = 0;
919         if(video_thread)
920         {
921                 result = video_thread->write_buffer(len);
922         }
924         return result;
927 double** File::get_audio_buffer()
929         if(audio_thread) return audio_thread->get_audio_buffer();
930         return 0;
933 VFrame*** File::get_video_buffer()
935         if(video_thread) return video_thread->get_video_buffer();
936         return 0;
940 int File::read_samples(double *buffer, int64_t len, int64_t base_samplerate, float *buffer_float)
942         int result = 0;
943         if(len < 0) return 0;
945 // Never try to read more samples than exist in the file
946         if (current_sample + len > asset->audio_length) {
947                 len = asset->audio_length - current_sample;
948         }
950 // Load with resampling 
951         if(file)
952         {
953 // Resample recursively calls this with the asset sample rate
954                 if(base_samplerate == 0) base_samplerate = asset->sample_rate;
956 //printf("File::read_samples 2 %d %d\n", base_samplerate, asset->sample_rate);
957                 if(base_samplerate != asset->sample_rate)
958                 {
959 //printf("File::read_samples 3\n");
960 //struct timeval start_time;
961 //gettimeofday(&start_time, 0);
962                         if (!file->prefer_samples_float())
963                         {
964                                 if(!resample)
965                                 {
966         //printf("File::read_samples 4\n");
967                                         resample = new Resample(this, asset->channels);
968                                 }
970         //printf("File::read_samples 5\n");
971                                 current_sample += resample->resample(buffer, 
972                                         len, 
973                                         asset->sample_rate, 
974                                         base_samplerate,
975                                         current_channel,
976                                         current_sample,
977                                         normalized_sample);
978         //printf("File::read_samples 6\n");
979                         } else
980                         {
981                                 if(!resample_float)
982                                 {
983         //printf("File::read_samples 4\n");
984                                         resample_float = new Resample_float(this, asset->channels);
985                                 }
987         //printf("File::read_samples 5\n");
988                                 current_sample += resample_float->resample(buffer, 
989                                         len, 
990                                         asset->sample_rate, 
991                                         base_samplerate,
992                                         current_channel,
993                                         current_sample,
994                                         normalized_sample);
995         //printf("File::read_samples 6\n");
997                         }
998 //printf("diff2: %lli\n", get_difference(&start_time));
1000                 }
1001                 else
1002 // Load directly
1003                 {
1004 //printf("File::read_samples 7\n");
1005                         if (buffer_float && file->prefer_samples_float())
1006                                 result = file->read_samples_float(buffer_float, len);
1007                         else
1008                                 result = file->read_samples(buffer, len);
1009 //printf("File::read_samples 8\n");
1010                         current_sample += len;
1011                 }
1013                 normalized_sample += len;
1014         }
1015         return result;
1018 int File::read_compressed_frame(VFrame *buffer)
1020         int result = 1;
1021         if(file)
1022                 result = file->read_compressed_frame(buffer);
1023         current_frame++;
1024         return result;
1027 int64_t File::compressed_frame_size()
1029         if(file)
1030                 return file->compressed_frame_size();
1031         else 
1032                 return 0;
1038 int File::read_frame(VFrame *frame, int is_thread)
1040         if(video_thread && !is_thread) return video_thread->read_frame(frame);
1042         if(file)
1043         {
1044                 int supported_colormodel = colormodel_supported(frame->get_color_model());
1045                 int advance_position = 1;
1047 // Test cache
1048                 if(use_cache &&
1049                         frame_cache->get_frame(frame,
1050                                 current_frame,
1051                                 current_layer,
1052                                 asset->frame_rate))
1053                 {
1054 // Can't advance position if cache used.
1055                         advance_position = 0;
1056                 }
1057                 else
1058 // Need temp
1059                 if(frame->get_color_model() != BC_COMPRESSED &&
1060                         (supported_colormodel != frame->get_color_model() ||
1061                         frame->get_w() != asset->width ||
1062                         frame->get_h() != asset->height))
1063                 {
1065 // Can't advance position here because it needs to be added to cache
1066                         if(temp_frame)
1067                         {
1068                                 if(!temp_frame->params_match(asset->width, asset->height, supported_colormodel))
1069                                 {
1070                                         delete temp_frame;
1071                                         temp_frame = 0;
1072                                 }
1073                         }
1075                         if(!temp_frame)
1076                         {
1077                                 temp_frame = new VFrame(0,
1078                                         asset->width,
1079                                         asset->height,
1080                                         supported_colormodel);
1081                         }
1083                         temp_frame->copy_stacks(frame);
1084                         file->read_frame(temp_frame);
1085                         cmodel_transfer(frame->get_rows(), 
1086                                 temp_frame->get_rows(),
1087                                 temp_frame->get_y(),
1088                                 temp_frame->get_u(),
1089                                 temp_frame->get_v(),
1090                                 frame->get_y(),
1091                                 frame->get_u(),
1092                                 frame->get_v(),
1093                                 0, 
1094                                 0, 
1095                                 temp_frame->get_w(), 
1096                                 temp_frame->get_h(),
1097                                 0, 
1098                                 0, 
1099                                 frame->get_w(), 
1100                                 frame->get_h(),
1101                                 temp_frame->get_color_model(), 
1102                                 frame->get_color_model(),
1103                                 0,
1104                                 temp_frame->get_w(),
1105                                 frame->get_w());
1106                 }
1107                 else
1108                 {
1109 // Can't advance position here because it needs to be added to cache
1110                         file->read_frame(frame);
1111                 }
1113                 if(use_cache) frame_cache->put_frame(frame,
1114                         current_frame,
1115                         current_layer,
1116                         asset->frame_rate,
1117                         1);
1118 // printf("File::read_frame\n");
1119 // frame->dump_params();
1121                 if(advance_position) current_frame++;
1122                 return 0;
1123         }
1124         else
1125                 return 1;
1128 int File::can_copy_from(Edit *edit, int64_t position, int output_w, int output_h)
1130         if(file)
1131         {
1132                 return edit->asset->width == output_w &&
1133                         edit->asset->height == output_h &&
1134                         file->can_copy_from(edit, position);
1135         }
1136         else
1137                 return 0;
1140 // Fill in queries about formats when adding formats here.
1143 int File::strtoformat(char *format)
1145         return strtoformat(0, format);
1148 int File::strtoformat(ArrayList<PluginServer*> *plugindb, char *format)
1150         if(!strcasecmp(format, _(AC3_NAME))) return FILE_AC3;
1151         else
1152         if(!strcasecmp(format, _(WAV_NAME))) return FILE_WAV;
1153         else
1154         if(!strcasecmp(format, _(PCM_NAME))) return FILE_PCM;
1155         else
1156         if(!strcasecmp(format, _(AU_NAME))) return FILE_AU;
1157         else
1158         if(!strcasecmp(format, _(AIFF_NAME))) return FILE_AIFF;
1159         else
1160         if(!strcasecmp(format, _(SND_NAME))) return FILE_SND;
1161         else
1162         if(!strcasecmp(format, _(PNG_NAME))) return FILE_PNG;
1163         else
1164         if(!strcasecmp(format, _(PNG_LIST_NAME))) return FILE_PNG_LIST;
1165         else
1166         if(!strcasecmp(format, _(TIFF_NAME))) return FILE_TIFF;
1167         else
1168         if(!strcasecmp(format, _(TIFF_LIST_NAME))) return FILE_TIFF_LIST;
1169         else
1170         if(!strcasecmp(format, _(JPEG_NAME))) return FILE_JPEG;
1171         else
1172         if(!strcasecmp(format, _(JPEG_LIST_NAME))) return FILE_JPEG_LIST;
1173         else
1174         if(!strcasecmp(format, _(EXR_NAME))) return FILE_EXR;
1175         else
1176         if(!strcasecmp(format, _(EXR_LIST_NAME))) return FILE_EXR_LIST;
1177         else
1178         if(!strcasecmp(format, _(YUV_NAME))) return FILE_YUV;
1179         else
1180         if(!strcasecmp(format, _(CR2_NAME))) return FILE_CR2;
1181         else
1182         if(!strcasecmp(format, _(MPEG_NAME))) return FILE_MPEG;
1183         else
1184         if(!strcasecmp(format, _(AMPEG_NAME))) return FILE_AMPEG;
1185         else
1186         if(!strcasecmp(format, _(VMPEG_NAME))) return FILE_VMPEG;
1187         else
1188         if(!strcasecmp(format, _(TGA_NAME))) return FILE_TGA;
1189         else
1190         if(!strcasecmp(format, _(TGA_LIST_NAME))) return FILE_TGA_LIST;
1191         else
1192         if(!strcasecmp(format, _(MOV_NAME))) return FILE_MOV;
1193         else
1194         if(!strcasecmp(format, _(AVI_NAME))) return FILE_AVI;
1195         else
1196         if(!strcasecmp(format, _(AVI_LAVTOOLS_NAME))) return FILE_AVI_LAVTOOLS;
1197         else
1198         if(!strcasecmp(format, _(AVI_ARNE2_NAME))) return FILE_AVI_ARNE2;
1199         else
1200         if(!strcasecmp(format, _(AVI_ARNE1_NAME))) return FILE_AVI_ARNE1;
1201         else
1202         if(!strcasecmp(format, _(AVI_AVIFILE_NAME))) return FILE_AVI_AVIFILE;
1203         else
1204         if(!strcasecmp(format, _(OGG_NAME))) return FILE_OGG;
1205         else
1206         if(!strcasecmp(format, _(VORBIS_NAME))) return FILE_VORBIS;
1207         else
1208         if(!strcasecmp(format, _(RAWDV_NAME))) return FILE_RAWDV;
1209         return 0;
1212 char* File::formattostr(int format)
1214         return formattostr(0, format);
1217 char* File::formattostr(ArrayList<PluginServer*> *plugindb, int format)
1219         switch(format)
1220         {
1221                 case FILE_AC3:
1222                         return _(AC3_NAME);
1223                         break;
1224                 case FILE_WAV:
1225                         return _(WAV_NAME);
1226                         break;
1227                 case FILE_PCM:
1228                         return _(PCM_NAME);
1229                         break;
1230                 case FILE_AU:
1231                         return _(AU_NAME);
1232                         break;
1233                 case FILE_AIFF:
1234                         return _(AIFF_NAME);
1235                         break;
1236                 case FILE_SND:
1237                         return _(SND_NAME);
1238                         break;
1239                 case FILE_PNG:
1240                         return _(PNG_NAME);
1241                         break;
1242                 case FILE_PNG_LIST:
1243                         return _(PNG_LIST_NAME);
1244                         break;
1245                 case FILE_JPEG:
1246                         return _(JPEG_NAME);
1247                         break;
1248                 case FILE_JPEG_LIST:
1249                         return _(JPEG_LIST_NAME);
1250                         break;
1251                 case FILE_CR2:
1252                         return _(CR2_NAME);
1253                         break;
1254                 case FILE_EXR:
1255                         return _(EXR_NAME);
1256                         break;
1257                 case FILE_EXR_LIST:
1258                         return _(EXR_LIST_NAME);
1259                         break;
1260                 case FILE_YUV:
1261                         return _(YUV_NAME);
1262                         break;
1263                 case FILE_MPEG:
1264                         return _(MPEG_NAME);
1265                         break;
1266                 case FILE_AMPEG:
1267                         return _(AMPEG_NAME);
1268                         break;
1269                 case FILE_VMPEG:
1270                         return _(VMPEG_NAME);
1271                         break;
1272                 case FILE_TGA:
1273                         return _(TGA_NAME);
1274                         break;
1275                 case FILE_TGA_LIST:
1276                         return _(TGA_LIST_NAME);
1277                         break;
1278                 case FILE_TIFF:
1279                         return _(TIFF_NAME);
1280                         break;
1281                 case FILE_TIFF_LIST:
1282                         return _(TIFF_LIST_NAME);
1283                         break;
1284                 case FILE_MOV:
1285                         return _(MOV_NAME);
1286                         break;
1287                 case FILE_AVI_LAVTOOLS:
1288                         return _(AVI_LAVTOOLS_NAME);
1289                         break;
1290                 case FILE_AVI:
1291                         return _(AVI_NAME);
1292                         break;
1293                 case FILE_AVI_ARNE2:
1294                         return _(AVI_ARNE2_NAME);
1295                         break;
1296                 case FILE_AVI_ARNE1:
1297                         return _(AVI_ARNE1_NAME);
1298                         break;
1299                 case FILE_AVI_AVIFILE:
1300                         return _(AVI_AVIFILE_NAME);
1301                         break;
1302                 case FILE_OGG:
1303                         return _(OGG_NAME);
1304                         break;
1305                 case FILE_VORBIS:
1306                         return _(VORBIS_NAME);
1307                         break;
1308                 case FILE_RAWDV:
1309                         return _(RAWDV_NAME);
1310                         break;
1311                 default:
1312                         return _("Unknown");
1313                         break;
1314         }
1315         return "Unknown";
1318 int File::strtobits(char *bits)
1320         if(!strcasecmp(bits, _(NAME_8BIT))) return BITSLINEAR8;
1321         if(!strcasecmp(bits, _(NAME_16BIT))) return BITSLINEAR16;
1322         if(!strcasecmp(bits, _(NAME_24BIT))) return BITSLINEAR24;
1323         if(!strcasecmp(bits, _(NAME_32BIT))) return BITSLINEAR32;
1324         if(!strcasecmp(bits, _(NAME_ULAW))) return BITSULAW;
1325         if(!strcasecmp(bits, _(NAME_ADPCM))) return BITS_ADPCM;
1326         if(!strcasecmp(bits, _(NAME_FLOAT))) return BITSFLOAT;
1327         if(!strcasecmp(bits, _(NAME_IMA4))) return BITSIMA4;
1328         return BITSLINEAR16;
1331 char* File::bitstostr(int bits)
1333 //printf("File::bitstostr\n");
1334         switch(bits)
1335         {
1336                 case BITSLINEAR8:
1337                         return (NAME_8BIT);
1338                         break;
1339                 case BITSLINEAR16:
1340                         return (NAME_16BIT);
1341                         break;
1342                 case BITSLINEAR24:
1343                         return (NAME_24BIT);
1344                         break;
1345                 case BITSLINEAR32:
1346                         return (NAME_32BIT);
1347                         break;
1348                 case BITSULAW:
1349                         return (NAME_ULAW);
1350                         break;
1351                 case BITS_ADPCM:
1352                         return (NAME_ADPCM);
1353                         break;
1354                 case BITSFLOAT:
1355                         return (NAME_FLOAT);
1356                         break;
1357                 case BITSIMA4:
1358                         return (NAME_IMA4);
1359                         break;
1360         }
1361         return "Unknown";
1366 int File::str_to_byteorder(char *string)
1368         if(!strcasecmp(string, _("Lo Hi"))) return 1;
1369         return 0;
1372 char* File::byteorder_to_str(int byte_order)
1374         if(byte_order) return _("Lo Hi");
1375         return _("Hi Lo");
1378 int File::bytes_per_sample(int bits)
1380         switch(bits)
1381         {
1382                 case BITSLINEAR8:
1383                         return 1;
1384                         break;
1385                 case BITSLINEAR16:
1386                         return 2;
1387                         break;
1388                 case BITSLINEAR24:
1389                         return 3;
1390                         break;
1391                 case BITSLINEAR32:
1392                         return 4;
1393                         break;
1394                 case BITSULAW:
1395                         return 1;
1396                         break;
1397                 case BITSIMA4:
1398                         return 1;
1399                         break;
1400         }
1401         return 1;
1408 int File::get_best_colormodel(int driver)
1410         return get_best_colormodel(asset, driver);
1413 int File::get_best_colormodel(Asset_GC asset, int driver)
1415         switch(asset->format)
1416         {
1417                 case FILE_RAWDV:
1418                         return FileDV::get_best_colormodel(asset, driver);
1419                         break;
1421                 case FILE_MOV:
1422                         return FileMOV::get_best_colormodel(asset, driver);
1423                         break;
1424                 
1425         case FILE_AVI:
1426                         return FileMOV::get_best_colormodel(asset, driver);
1427                         break;
1429                 case FILE_MPEG:
1430                         return FileMPEG::get_best_colormodel(asset, driver);
1431                         break;
1432                 
1433                 case FILE_JPEG:
1434                 case FILE_JPEG_LIST:
1435                         return FileJPEG::get_best_colormodel(asset, driver);
1436                         break;
1438                 case FILE_EXR:
1439                 case FILE_EXR_LIST:
1440                         return FileEXR::get_best_colormodel(asset, driver);
1441                         break;
1442                 
1443                 case FILE_YUV:
1444                         return FileYUV::get_best_colormodel(asset, driver);
1445                         break;
1447                 case FILE_PNG:
1448                 case FILE_PNG_LIST:
1449                         return FilePNG::get_best_colormodel(asset, driver);
1450                         break;
1451                 
1452                 case FILE_TGA:
1453                 case FILE_TGA_LIST:
1454                         return FileTGA::get_best_colormodel(asset, driver);
1455                         break;
1456         }
1458         return BC_RGB888;
1462 int File::colormodel_supported(int colormodel)
1464         if(file)
1465                 return file->colormodel_supported(colormodel);
1467         return BC_RGB888;
1471 int64_t File::get_memory_usage() 
1473         int64_t result = 0;
1474         if(temp_frame) result += temp_frame->get_data_size();
1475         if(file) result += file->get_memory_usage();
1476         result += frame_cache->get_memory_usage();
1477         if(video_thread) result += video_thread->get_memory_usage();
1479         if(result < MIN_CACHEITEM_SIZE) result = MIN_CACHEITEM_SIZE;
1480         return result;
1483 FrameCache* File::get_frame_cache()
1485         return frame_cache;
1488 int File::supports_video(ArrayList<PluginServer*> *plugindb, char *format)
1490         int i, format_i = strtoformat(plugindb, format);
1491         
1492         return supports_video(format_i);
1493         return 0;
1496 int File::supports_audio(ArrayList<PluginServer*> *plugindb, char *format)
1498         int i, format_i = strtoformat(plugindb, format);
1500         return supports_audio(format_i);
1501         return 0;
1505 int File::supports_video(int format)
1507 //printf("File::supports_video %d\n", format);
1508         switch(format)
1509         {
1510                 case FILE_OGG:
1511                 case FILE_MOV:
1512                 case FILE_JPEG:
1513                 case FILE_JPEG_LIST:
1514                 case FILE_CR2:
1515                 case FILE_EXR:
1516                 case FILE_EXR_LIST:
1517                 case FILE_YUV:
1518                 case FILE_PNG:
1519                 case FILE_PNG_LIST:
1520                 case FILE_TGA:
1521                 case FILE_TGA_LIST:
1522                 case FILE_TIFF:
1523                 case FILE_TIFF_LIST:
1524                 case FILE_VMPEG:
1525                 case FILE_AVI_LAVTOOLS:
1526                 case FILE_AVI_ARNE2:
1527                 case FILE_AVI:
1528                 case FILE_AVI_ARNE1:
1529                 case FILE_AVI_AVIFILE:
1530                 case FILE_RAWDV:
1531                         return 1;
1532                         break;
1534                 default:
1535                         return 0;
1536                         break;
1537         }
1540 int File::supports_audio(int format)
1542         switch(format)
1543         {
1544                 case FILE_AC3:
1545                 case FILE_PCM:
1546                 case FILE_WAV:
1547                 case FILE_MOV:
1548                 case FILE_OGG:
1549                 case FILE_VORBIS:
1550                 case FILE_AMPEG:
1551                 case FILE_AU:
1552                 case FILE_AIFF:
1553                 case FILE_SND:
1554                 case FILE_AVI:
1555                 case FILE_AVI_LAVTOOLS:
1556                 case FILE_AVI_ARNE2:
1557                 case FILE_AVI_ARNE1:
1558                 case FILE_AVI_AVIFILE:
1559                 case FILE_RAWDV:
1560                         return 1;
1561                 
1562                 default:
1563                         return 0;
1564                         break;
1565         }
1568 char* File::get_tag(int format)
1570         switch(format)
1571         {
1572                 case FILE_AC3:          return "ac3";
1573                 case FILE_AIFF:         return "aif";
1574                 case FILE_AMPEG:        return "mp3";
1575                 case FILE_AU:           return "au";
1576                 case FILE_AVI:          return "avi";
1577                 case FILE_RAWDV:        return "dv";
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                 case FILE_YUV:          return "m2v";
1595         }
1596         return 0;
1599 PackagingEngine *File::new_packaging_engine(Asset_GC asset)
1601         PackagingEngine *result;
1602         switch (asset->format)
1603         {
1604                 case FILE_OGG:
1605                         result = (PackagingEngine*)new PackagingEngineOGG();
1606                         break;
1607                 default:
1608                         result = (PackagingEngine*) new PackagingEngineDefault();
1609                         break;
1610         }
1612         return result;
1624 //      Local Variables:
1625 //      mode: C++
1626 //      c-file-style: "linux"
1627 //      End: