Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

file.C

Go to the documentation of this file.
00001 #include "asset.h"
00002 #include "bcsignals.h"
00003 #include "byteorder.h"
00004 #include "cache.inc"
00005 #include "condition.h"
00006 #include "edit.h"
00007 #include "errorbox.h"
00008 #include "file.h"
00009 #include "fileac3.h"
00010 #include "fileavi.h"
00011 #include "filedv.h"
00012 #include "fileogg.h"
00013 #include "filebase.h"
00014 #include "filecr2.h"
00015 #include "fileexr.h"
00016 #include "fileogg.h"
00017 #include "filexml.h"
00018 #include "filejpeg.h"
00019 #include "filemov.h"
00020 #include "filempeg.h"
00021 #include "fileogg.h"
00022 #include "filepng.h"
00023 #include "filesndfile.h"
00024 #include "filetga.h"
00025 #include "filethread.h"
00026 #include "filetiff.h"
00027 #include "filevorbis.h"
00028 #include "fileyuv.h"
00029 #include "formatwindow.h"
00030 #include "formattools.h"
00031 #include "framecache.h"
00032 #include "language.h"
00033 #include "pluginserver.h"
00034 #include "resample.h"
00035 #include "stringfile.h"
00036 #include "vframe.h"
00037 
00038 
00039 
00040 
00041 File::File()
00042 {
00043         cpus = 1;
00044         asset = new Asset;
00045         format_completion = new Mutex("File::format_completion");
00046         write_lock = new Condition(1, "File::write_lock");
00047         frame_cache = new FrameCache;
00048         reset_parameters();
00049 }
00050 
00051 File::~File()
00052 {
00053         if(getting_options)
00054         {
00055                 if(format_window) format_window->set_done(0);
00056                 format_completion->lock("File::~File");
00057                 format_completion->unlock();
00058         }
00059 
00060         if(temp_frame) delete temp_frame;
00061 
00062         close_file(0);
00063         reset_parameters();
00064         delete asset;
00065         delete format_completion;
00066         delete write_lock;
00067         if(frame_cache) delete frame_cache;
00068 }
00069 
00070 void File::reset_parameters()
00071 {
00072         file = 0;
00073         audio_thread = 0;
00074         video_thread = 0;
00075         getting_options = 0;
00076         format_window = 0;
00077         temp_frame = 0;
00078         current_sample = 0;
00079         current_frame = 0;
00080         current_channel = 0;
00081         current_layer = 0;
00082         normalized_sample = 0;
00083         normalized_sample_rate = 0;
00084         resample = 0;
00085         resample_float = 0;
00086         use_cache = 0;
00087         preferences = 0;
00088 }
00089 
00090 int File::raise_window()
00091 {
00092         if(getting_options && format_window)
00093         {
00094                 format_window->raise_window();
00095                 format_window->flush();
00096         }
00097         return 0;
00098 }
00099 
00100 void File::close_window()
00101 {
00102         if(getting_options)
00103         {
00104                 format_window->lock_window("File::close_window");
00105                 format_window->set_done(1);
00106                 format_window->unlock_window();
00107                 getting_options = 0;
00108         }
00109 }
00110 
00111 int File::get_options(FormatTools *format, 
00112                       int audio_options, 
00113                       int video_options,
00114                       int lock_compressor)
00115 {
00116         BC_WindowBase *parent_window = format->window;
00117         ArrayList<PluginServer*> *plugindb = format->plugindb;
00118         Asset *asset = format->asset;
00119 
00120         getting_options = 1;
00121         format_completion->lock("File::get_options");
00122         switch(asset->format)
00123         {
00124                 case FILE_AC3:
00125                         FileAC3::get_parameters(parent_window,
00126                                 asset,
00127                                 format_window,
00128                                 audio_options,
00129                                 video_options);
00130                         break;
00131                 case FILE_RAWDV:
00132                         FileDV::get_parameters(parent_window,
00133                                 asset,
00134                                 format_window,
00135                                 audio_options,
00136                                 video_options);
00137                         break;
00138                 case FILE_PCM:
00139                 case FILE_WAV:
00140                 case FILE_AU:
00141                 case FILE_AIFF:
00142                 case FILE_SND:
00143                         FileSndFile::get_parameters(parent_window, 
00144                                 asset, 
00145                                 format_window, 
00146                                 audio_options, 
00147                                 video_options);
00148                         break;
00149                 case FILE_MOV:
00150                         FileMOV::get_parameters(parent_window, 
00151                                 asset, 
00152                                 format_window, 
00153                                 audio_options, 
00154                                 video_options,
00155                                 lock_compressor);
00156                         break;
00157                 case FILE_AMPEG:
00158                 case FILE_VMPEG:
00159                         FileMPEG::get_parameters(parent_window, 
00160                                 asset, 
00161                                 format_window, 
00162                                 audio_options, 
00163                                 video_options);
00164                         break;
00165                 case FILE_AVI:
00166                         FileMOV::get_parameters(parent_window, 
00167                                 asset, 
00168                                 format_window, 
00169                                 audio_options, 
00170                                 video_options,
00171                                 lock_compressor);
00172                         break;
00173                 case FILE_AVI_LAVTOOLS:
00174                 case FILE_AVI_ARNE2:
00175                 case FILE_AVI_ARNE1:
00176                 case FILE_AVI_AVIFILE:
00177                         FileAVI::get_parameters(parent_window, 
00178                                 asset, 
00179                                 format_window, 
00180                                 audio_options, 
00181                                 video_options,
00182                                 lock_compressor);
00183                         break;
00184                 case FILE_JPEG:
00185                 case FILE_JPEG_LIST:
00186                         FileJPEG::get_parameters(parent_window, 
00187                                 asset, 
00188                                 format_window, 
00189                                 audio_options, 
00190                                 video_options);
00191                         break;
00192                 case FILE_EXR:
00193                 case FILE_EXR_LIST:
00194                         FileEXR::get_parameters(parent_window, 
00195                                 asset, 
00196                                 format_window, 
00197                                 audio_options, 
00198                                 video_options);
00199                         break;
00200                 case FILE_YUV:
00201                         FileYUV::get_parameters(parent_window,
00202                                 asset,
00203                                 format_window,
00204                                 video_options,
00205                                 format);
00206                         break;
00207                 case FILE_PNG:
00208                 case FILE_PNG_LIST:
00209                         FilePNG::get_parameters(parent_window, 
00210                                 asset, 
00211                                 format_window, 
00212                                 audio_options, 
00213                                 video_options);
00214                         break;
00215                 case FILE_TGA:
00216                 case FILE_TGA_LIST:
00217                         FileTGA::get_parameters(parent_window, 
00218                                 asset, 
00219                                 format_window, 
00220                                 audio_options, 
00221                                 video_options);
00222                         break;
00223                 case FILE_TIFF:
00224                 case FILE_TIFF_LIST:
00225                         FileTIFF::get_parameters(parent_window, 
00226                                 asset, 
00227                                 format_window, 
00228                                 audio_options, 
00229                                 video_options);
00230                         break;
00231                 case FILE_OGG:
00232                         FileOGG::get_parameters(parent_window,
00233                                 asset,
00234                                 format_window,
00235                                 audio_options,
00236                                 video_options);
00237                         break;
00238                 default:
00239                         break;
00240         }
00241 
00242         if(!format_window)
00243         {
00244                 ErrorBox *errorbox = new ErrorBox(PROGRAM_NAME ": Error",
00245                         parent_window->get_abs_cursor_x(1),
00246                         parent_window->get_abs_cursor_y(1));
00247                 format_window = errorbox;
00248                 getting_options = 1;
00249                 if(audio_options)
00250                         errorbox->create_objects(_("This format doesn't support audio."));
00251                 else
00252                 if(video_options)
00253                         errorbox->create_objects(_("This format doesn't support video."));
00254                 errorbox->run_window();
00255                 delete errorbox;
00256         }
00257 
00258         getting_options = 0;
00259         format_window = 0;
00260         format_completion->unlock();
00261         return 0;
00262 }
00263 
00264 void File::set_asset(Asset *asset)
00265 {
00266         *this->asset = *asset;
00267 }
00268 
00269 int File::set_processors(int cpus)   // Set the number of cpus for certain codecs
00270 {
00271         this->cpus = cpus;
00272         return 0;
00273 }
00274 
00275 int File::set_preload(int64_t size)
00276 {
00277         this->playback_preload = size;
00278         return 0;
00279 }
00280 
00281 void File::set_cache_frames(int value)
00282 {
00283         use_cache = value;
00284 }
00285 
00286 int File::purge_cache()
00287 {
00288         return frame_cache->delete_oldest();
00289 }
00290 
00291 
00292 
00293 
00294 
00295 
00296 
00297 
00298 
00299 
00300 
00301 int File::open_file(Preferences *preferences, 
00302         Asset *asset, 
00303         int rd, 
00304         int wr,
00305         int64_t base_samplerate,
00306         float base_framerate)
00307 {
00308         this->preferences = preferences;
00309         *this->asset = *asset;
00310         file = 0;
00311 
00312 
00313 SET_TRACE
00314 //printf("File::open_file 1 %s %d\n", asset->path, asset->format);
00315         switch(this->asset->format)
00316         {
00317 // get the format now
00318 // If you add another format to case 0, you also need to add another case for the
00319 // file format #define.
00320                 case FILE_UNKNOWN:
00321                         FILE *stream;
00322                         if(!(stream = fopen(this->asset->path, "rb")))
00323                         {
00324 // file not found
00325                                 return 1;
00326                         }
00327 
00328                         char test[16];
00329                         fread(test, 16, 1, stream);
00330 
00331 SET_TRACE
00332                         if(FileDV::check_sig(this->asset))
00333                         {
00334 // libdv
00335                                 fclose(stream);
00336                                 file = new FileDV(this->asset, this);
00337                         }
00338                         else if(FileSndFile::check_sig(this->asset))
00339                         {
00340 // libsndfile
00341                                 fclose(stream);
00342                                 file = new FileSndFile(this->asset, this);
00343                         }
00344                         else
00345                         if(FilePNG::check_sig(this->asset))
00346                         {
00347 // PNG file
00348                                 fclose(stream);
00349                                 file = new FilePNG(this->asset, this);
00350                         }
00351                         else
00352                         if(FileJPEG::check_sig(this->asset))
00353                         {
00354 // JPEG file
00355                                 fclose(stream);
00356                                 file = new FileJPEG(this->asset, this);
00357                         }
00358                         else
00359                         if(FileEXR::check_sig(this->asset, test))
00360                         {
00361 // EXR file
00362                                 fclose(stream);
00363                                 file = new FileEXR(this->asset, this);
00364                         }
00365                         else
00366                         if(FileYUV::check_sig(this->asset))
00367                         {
00368 // YUV file
00369                                 fclose(stream);
00370                                 file = new FileYUV(this->asset, this);
00371                         }
00372                         else
00373                         if(FileCR2::check_sig(this->asset))
00374                         {
00375 // JPEG file
00376                                 fclose(stream);
00377                                 file = new FileCR2(this->asset, this);
00378                         }
00379                         else
00380                         if(FileTGA::check_sig(this->asset))
00381                         {
00382 // TGA file
00383                                 fclose(stream);
00384                                 file = new FileTGA(this->asset, this);
00385                         }
00386                         else
00387                         if(FileTIFF::check_sig(this->asset))
00388                         {
00389 // TIFF file
00390                                 fclose(stream);
00391                                 file = new FileTIFF(this->asset, this);
00392                         }
00393                         else
00394                         if(FileOGG::check_sig(this->asset))
00395                         {
00396 // OGG file
00397                                 fclose(stream);
00398                                 file = new FileOGG(this->asset, this);
00399                         }
00400                         else
00401                         if(FileVorbis::check_sig(this->asset))
00402                         {
00403 // VorbisFile file
00404                                 fclose(stream);
00405                                 file = new FileVorbis(this->asset, this);
00406                         }
00407                         else
00408                         if(FileOGG::check_sig(this->asset))
00409                         {
00410 // OGG file.  Doesn't always work with pure audio files.
00411                                 fclose(stream);
00412                                 file = new FileOGG(this->asset, this);
00413                         }
00414                         else
00415                         if(FileMPEG::check_sig(this->asset))
00416                         {
00417 // MPEG file
00418                                 fclose(stream);
00419                                 file = new FileMPEG(this->asset, this);
00420                         }
00421                         else
00422                         if(test[0] == '<' && test[1] == 'E' && test[2] == 'D' && test[3] == 'L' && test[4] == '>' ||
00423                                 test[0] == '<' && test[1] == 'H' && test[2] == 'T' && test[3] == 'A' && test[4] == 'L' && test[5] == '>' ||
00424                                 test[0] == '<' && test[1] == '?' && test[2] == 'x' && test[3] == 'm' && test[4] == 'l')
00425                         {
00426 SET_TRACE
00427 // XML file
00428                                 fclose(stream);
00429 SET_TRACE
00430                                 return FILE_IS_XML;
00431                         }    // can't load project file
00432                         else
00433                         if(FileMOV::check_sig(this->asset))
00434                         {
00435 // MOV file
00436 // should be last because quicktime lacks a magic number
00437                                 fclose(stream);
00438                                 file = new FileMOV(this->asset, this);
00439                         }
00440                         else
00441                         {
00442 // PCM file
00443                                 fclose(stream);
00444                                 return FILE_UNRECOGNIZED_CODEC;
00445                         }   // need more info
00446                         break;
00447 
00448 // format already determined
00449                 case FILE_AC3:
00450                         file = new FileAC3(this->asset, this);
00451                         break;
00452 
00453                 case FILE_PCM:
00454                 case FILE_WAV:
00455                 case FILE_AU:
00456                 case FILE_AIFF:
00457                 case FILE_SND:
00458 //printf("File::open_file 1\n");
00459                         file = new FileSndFile(this->asset, this);
00460                         break;
00461 
00462                 case FILE_PNG:
00463                 case FILE_PNG_LIST:
00464                         file = new FilePNG(this->asset, this);
00465                         break;
00466 
00467                 case FILE_JPEG:
00468                 case FILE_JPEG_LIST:
00469                         file = new FileJPEG(this->asset, this);
00470                         break;
00471 
00472                 case FILE_EXR:
00473                 case FILE_EXR_LIST:
00474                         file = new FileEXR(this->asset, this);
00475                         break;
00476 
00477                 case FILE_YUV:
00478                         file = new FileYUV(this->asset, this);
00479                         break;
00480 
00481                 case FILE_CR2:
00482                         file = new FileCR2(this->asset, this);
00483                         break;
00484 
00485                 case FILE_TGA_LIST:
00486                 case FILE_TGA:
00487                         file = new FileTGA(this->asset, this);
00488                         break;
00489 
00490                 case FILE_TIFF:
00491                 case FILE_TIFF_LIST:
00492                         file = new FileTIFF(this->asset, this);
00493                         break;
00494 
00495                 case FILE_MOV:
00496                         file = new FileMOV(this->asset, this);
00497                         break;
00498 
00499                 case FILE_MPEG:
00500                 case FILE_AMPEG:
00501                 case FILE_VMPEG:
00502                         file = new FileMPEG(this->asset, this);
00503                         break;
00504 
00505                 case FILE_OGG:
00506                         file = new FileOGG(this->asset, this);
00507                         break;
00508 
00509                 case FILE_VORBIS:
00510                         file = new FileVorbis(this->asset, this);
00511                         break;
00512 
00513                 case FILE_AVI:
00514                         file = new FileMOV(this->asset, this);
00515                         break;
00516 
00517                 case FILE_AVI_LAVTOOLS:
00518                 case FILE_AVI_ARNE2:
00519                 case FILE_AVI_ARNE1:
00520                 case FILE_AVI_AVIFILE:
00521                         file = new FileAVI(this->asset, this);
00522                         break;
00523 
00524                 case FILE_RAWDV:
00525                         file = new FileDV(this->asset, this);
00526                         break;
00527 
00528 // try plugins
00529                 default:
00530                         return 1;
00531                         break;
00532         }
00533 //printf("File::open_file 2\n");
00534 
00535 // Reopen file with correct parser and get header.
00536         if(file->open_file(rd, wr))
00537         {
00538 //printf("File::open_file 2.5\n");
00539                 delete file;
00540                 file = 0;
00541         }
00542 //printf("File::open_file 3\n");
00543 
00544 
00545 // Set extra writing parameters to mandatory settings.
00546         if(file && wr)
00547         {
00548                 if(this->asset->dither) file->set_dither();
00549         }
00550 
00551 // Synchronize header parameters
00552         *asset = *this->asset;
00553 
00554         if(file)
00555                 return FILE_OK;
00556         else
00557                 return FILE_NOT_FOUND;
00558 }
00559 
00560 int File::close_file(int ignore_thread)
00561 {
00562         if(!ignore_thread)
00563         {
00564                 stop_audio_thread();
00565                 stop_video_thread();
00566         }
00567 
00568         if(file) 
00569         {
00570 // The file's asset is a copy of the argument passed to open_file so the
00571 // user must copy lengths from the file's asset.
00572                 if(asset && file->wr)
00573                 {
00574                         asset->audio_length = current_sample;
00575                         asset->video_length = current_frame;
00576                 }
00577                 file->close_file();
00578                 delete file;
00579         }
00580 
00581         if(resample) delete resample;
00582         if(resample_float) delete resample_float;
00583 
00584         reset_parameters();
00585         return 0;
00586 }
00587 
00588 
00589 
00590 int File::get_index(char *index_path)
00591 {
00592         if(file)
00593         {
00594                 return file->get_index(index_path);
00595         }
00596         return 1;
00597 }
00598 
00599 
00600 
00601 int File::start_audio_thread(int64_t buffer_size, int ring_buffers)
00602 {
00603         audio_thread = new FileThread(this, 1, 0);
00604         audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
00605         return 0;
00606 }
00607 
00608 int File::start_video_thread(int64_t buffer_size, 
00609         int color_model, 
00610         int ring_buffers, 
00611         int compressed)
00612 {
00613         video_thread = new FileThread(this, 0, 1);
00614 //printf("File::start_video_thread 1\n");
00615         video_thread->start_writing(buffer_size, 
00616                 color_model, 
00617                 ring_buffers, 
00618                 compressed);
00619 //printf("File::start_video_thread 2\n");
00620         return 0;
00621 }
00622 
00623 int File::stop_audio_thread()
00624 {
00625 //printf("File::stop_audio_thread 1\n");
00626         if(audio_thread)
00627         {
00628                 audio_thread->stop_writing();
00629                 delete audio_thread;
00630                 audio_thread = 0;
00631         }
00632         return 0;
00633 }
00634 
00635 int File::stop_video_thread()
00636 {
00637 //printf("File::stop_video_thread 1\n");
00638         if(video_thread)
00639         {
00640                 video_thread->stop_writing();
00641                 delete video_thread;
00642                 video_thread = 0;
00643         }
00644         return 0;
00645 }
00646 
00647 int File::lock_read()
00648 {
00649 //      read_lock.lock();
00650         return 0;
00651 }
00652 
00653 int File::unlock_read()
00654 {
00655 //      read_lock.unlock();
00656         return 0;
00657 }
00658 
00659 int File::set_channel(int channel) 
00660 {
00661         if(file && channel < asset->channels)
00662         {
00663                 current_channel = channel;
00664                 return 0;
00665         }
00666         else
00667                 return 1;
00668 }
00669 
00670 int File::set_layer(int layer) 
00671 {
00672         if(file && layer < asset->layers)
00673         {
00674                 current_layer = layer;
00675 //printf("File::set_layer 1 %d\n", layer);
00676                 return 0; 
00677         }
00678         else
00679                 return 1;
00680 }
00681 
00682 int64_t File::get_audio_length(int64_t base_samplerate) 
00683 { 
00684         int64_t result = asset->audio_length;
00685         if(result > 0)
00686         {
00687                 if(base_samplerate > 0)
00688                         return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
00689                 else
00690                         return result;
00691         }
00692         else
00693                 return -1;
00694 }
00695 
00696 int64_t File::get_video_length(float base_framerate)
00697 { 
00698         int64_t result = asset->video_length;
00699         if(result > 0)
00700         {
00701                 if(base_framerate > 0)
00702                         return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5); 
00703                 else
00704                         return result;
00705         }
00706         else
00707                 return -1;  // infinity
00708 }
00709 
00710 
00711 int64_t File::get_video_position(float base_framerate) 
00712 {
00713         if(base_framerate > 0)
00714                 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
00715         else
00716                 return current_frame;
00717 }
00718 
00719 int64_t File::get_audio_position(int64_t base_samplerate) 
00720 { 
00721         if(base_samplerate > 0)
00722         {
00723                 if(normalized_sample_rate == base_samplerate)
00724                         return normalized_sample;
00725                 else
00726                         return (int64_t)((double)current_sample / 
00727                                 asset->sample_rate * 
00728                                 base_samplerate + 
00729                                 0.5);
00730         }
00731         else
00732                 return current_sample;
00733 }
00734 
00735 
00736 
00737 // The base samplerate must be nonzero if the base samplerate in the calling
00738 // function is expected to change as this forces the resampler to reset.
00739 
00740 int File::set_audio_position(int64_t position, float base_samplerate) 
00741 {
00742         int result = 0;
00743 
00744         if(!file) return 1;
00745 
00746 #define REPOSITION(x, y) \
00747         (labs((x) - (y)) > 1)
00748 
00749 
00750 
00751         if((base_samplerate && REPOSITION(normalized_sample, position)) ||
00752                 (!base_samplerate && REPOSITION(current_sample, position)))
00753         {
00754 // Can't reset resampler since one seek operation is done 
00755 // for every channel to be read at the same position.
00756 
00757 // Use a conditional reset for just the case of different base_samplerates
00758                 if(base_samplerate > 0)
00759                 {
00760                         if(normalized_sample_rate &&
00761                                 normalized_sample_rate != base_samplerate && 
00762                                 resample)
00763                                 resample->reset(-1);
00764 
00765                         normalized_sample = position;
00766                         normalized_sample_rate = (int64_t)((base_samplerate > 0) ? 
00767                                 base_samplerate : 
00768                                 asset->sample_rate);
00769 
00770 // Convert position to file's rate
00771                         if(base_samplerate > 0)
00772                                 current_sample = Units::round((double)position / 
00773                                         base_samplerate * 
00774                                         asset->sample_rate);
00775                 }
00776                 else
00777                 {
00778                         current_sample = position;
00779                         normalized_sample = Units::round((double)position / 
00780                                         asset->sample_rate * 
00781                                         normalized_sample_rate);
00782 // Can not set the normalized sample rate since this would reset the resampler.
00783                 }
00784 
00785                 result = file->set_audio_position(current_sample);
00786 
00787                 if(result)
00788                         printf("File::set_audio_position position=%d base_samplerate=%f asset=%p asset->sample_rate=%d\n",
00789                                 position, base_samplerate, asset, asset->sample_rate);
00790         }
00791 
00792 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
00793 
00794         return result;
00795 }
00796 
00797 int File::set_video_position(int64_t position, float base_framerate) 
00798 {
00799         int result = 0;
00800         if(!file) return 0;
00801 
00802 // Convert to file's rate
00803         if(base_framerate > 0)
00804                 position = (int64_t)((double)position / 
00805                         base_framerate * 
00806                         asset->frame_rate + 
00807                         0.5);
00808 
00809 
00810         if(current_frame != position && file)
00811         {
00812                 current_frame = position;
00813                 result = file->set_video_position(current_frame);
00814         }
00815 
00816         return result;
00817 }
00818 
00819 // No resampling here.
00820 int File::write_samples(double **buffer, int64_t len)
00821 { 
00822         int result = 1;
00823         
00824         if(file)
00825         {
00826                 write_lock->lock("File::write_samples");
00827                 result = file->write_samples(buffer, len);
00828                 current_sample += len;
00829                 normalized_sample += len;
00830                 asset->audio_length += len;
00831                 write_lock->unlock();
00832         }
00833         return result;
00834 }
00835 
00836 // Can't put any cmodel abstraction here because the filebase couldn't be
00837 // parallel.
00838 int File::write_frames(VFrame ***frames, int len)
00839 {
00840 // Store the counters in temps so the filebase can choose to overwrite them.
00841         int result;
00842         int current_frame_temp = current_frame;
00843         int video_length_temp = asset->video_length;
00844         write_lock->lock("File::write_frames");
00845 
00846 
00847 
00848 
00849         result = file->write_frames(frames, len);
00850 
00851 
00852 
00853 
00854 
00855         current_frame = current_frame_temp + len;
00856         asset->video_length = video_length_temp + len;
00857         write_lock->unlock();
00858         return result;
00859 }
00860 
00861 int File::write_compressed_frame(VFrame *buffer)
00862 {
00863         int result = 0;
00864         write_lock->lock("File::write_compressed_frame");
00865         result = file->write_compressed_frame(buffer);
00866         current_frame++;
00867         asset->video_length++;
00868         write_lock->unlock();
00869         return result;
00870 }
00871 
00872 
00873 int File::write_audio_buffer(int64_t len)
00874 {
00875         int result = 0;
00876         if(audio_thread)
00877         {
00878                 result = audio_thread->write_buffer(len);
00879         }
00880         return result;
00881 }
00882 
00883 int File::write_video_buffer(int64_t len)
00884 {
00885         int result = 0;
00886         if(video_thread)
00887         {
00888                 result = video_thread->write_buffer(len);
00889         }
00890 
00891         return result;
00892 }
00893 
00894 double** File::get_audio_buffer()
00895 {
00896         if(audio_thread) return audio_thread->get_audio_buffer();
00897         return 0;
00898 }
00899 
00900 VFrame*** File::get_video_buffer()
00901 {
00902         if(video_thread) return video_thread->get_video_buffer();
00903         return 0;
00904 }
00905 
00906 
00907 int File::read_samples(double *buffer, int64_t len, int64_t base_samplerate, float *buffer_float)
00908 {
00909         int result = 0;
00910 //printf("File::read_samples 1\n");
00911 
00912 // Never try to read more samples than exist in the file
00913         if (current_sample + len > asset->audio_length) {
00914                 len = asset->audio_length - current_sample;
00915         }
00916 
00917 // Load with resampling 
00918         if(file)
00919         {
00920 // Resample recursively calls this with the asset sample rate
00921                 if(base_samplerate == 0) base_samplerate = asset->sample_rate;
00922 
00923 //printf("File::read_samples 2 %d %d\n", base_samplerate, asset->sample_rate);
00924                 if(base_samplerate != asset->sample_rate)
00925                 {
00926 //printf("File::read_samples 3\n");
00927 //struct timeval start_time;
00928 //gettimeofday(&start_time, 0);
00929                         if (!file->prefer_samples_float())
00930                         {
00931                                 if(!resample)
00932                                 {
00933         //printf("File::read_samples 4\n");
00934                                         resample = new Resample(this, asset->channels);
00935                                 }
00936 
00937         //printf("File::read_samples 5\n");
00938                                 current_sample += resample->resample(buffer, 
00939                                         len, 
00940                                         asset->sample_rate, 
00941                                         base_samplerate,
00942                                         current_channel,
00943                                         current_sample,
00944                                         normalized_sample);
00945         //printf("File::read_samples 6\n");
00946                         } else
00947                         {
00948                                 if(!resample_float)
00949                                 {
00950         //printf("File::read_samples 4\n");
00951                                         resample_float = new Resample_float(this, asset->channels);
00952                                 }
00953 
00954         //printf("File::read_samples 5\n");
00955                                 current_sample += resample_float->resample(buffer, 
00956                                         len, 
00957                                         asset->sample_rate, 
00958                                         base_samplerate,
00959                                         current_channel,
00960                                         current_sample,
00961                                         normalized_sample);
00962         //printf("File::read_samples 6\n");
00963 
00964                         }
00965 //printf("diff2: %lli\n", get_difference(&start_time));
00966 
00967                 }
00968                 else
00969 // Load directly
00970                 {
00971 //printf("File::read_samples 7\n");
00972                         if (buffer_float && file->prefer_samples_float())
00973                                 result = file->read_samples_float(buffer_float, len);
00974                         else
00975                                 result = file->read_samples(buffer, len);
00976 //printf("File::read_samples 8\n");
00977                         current_sample += len;
00978                 }
00979 
00980                 normalized_sample += len;
00981         }
00982         return result;
00983 }
00984 
00985 int File::read_compressed_frame(VFrame *buffer)
00986 {
00987         int result = 1;
00988         if(file)
00989                 result = file->read_compressed_frame(buffer);
00990         current_frame++;
00991         return result;
00992 }
00993 
00994 int64_t File::compressed_frame_size()
00995 {
00996         if(file)
00997                 return file->compressed_frame_size();
00998         else 
00999                 return 0;
01000 }
01001 
01002 
01003 
01004 
01005 int File::read_frame(VFrame *frame)
01006 {
01007         if(file)
01008         {
01009                 int supported_colormodel = colormodel_supported(frame->get_color_model());
01010                 int advance_position = 1;
01011 
01012 
01013                 // Test cache
01014                 if(use_cache &&
01015                    frame_cache->get_frame(frame,
01016                                           current_frame,
01017                                           asset->frame_rate)) {
01018                         // Can't advance position if cache used.
01019                         advance_position = 0;
01020                 }
01021                 else if(frame->get_color_model() != BC_COMPRESSED &&
01022                         (supported_colormodel != frame->get_color_model() ||
01023                          frame->get_w() != asset->width ||
01024                          frame->get_h() != asset->height)) {
01025                         // Need temp                            
01026                         // Can't advance position here because it needs to be added to cache
01027                         if(temp_frame) {
01028                                 if(!temp_frame->params_match(asset->width, asset->height, supported_colormodel)) {
01029                                         delete temp_frame;
01030                                         temp_frame = 0;
01031                                 }
01032                         }
01033                         
01034                         if(!temp_frame) {
01035                                 temp_frame = new VFrame(0,
01036                                                         asset->width,
01037                                                         asset->height,
01038                                                         supported_colormodel);
01039                         }
01040                         
01041                         file->read_frame(temp_frame);
01042                         //struct timeval start_time;
01043                         //gettimeofday(&start_time, 0);
01044                         //printf("tarnsfering from : %i to %i\n", frame->get_color_model(),temp_frame->get_color_model());
01045 
01046 // FUTURE: convert from YUV planar if cmodel_is_planar(temp_frame)
01047                         cmodel_transfer(frame->get_rows(), 
01048                                         temp_frame->get_rows(),
01049                                         0,
01050                                         0,
01051                                         0,
01052                                         0,
01053                                         0,
01054                                         0,
01055                                         0, 
01056                                         0, 
01057                                         temp_frame->get_w(), 
01058                                         temp_frame->get_h(),
01059                                         0, 
01060                                         0, 
01061                                         frame->get_w(), 
01062                                         frame->get_h(),
01063                                         temp_frame->get_color_model(), 
01064                                         frame->get_color_model(),
01065                                         0,
01066                                         temp_frame->get_w(),
01067                                         frame->get_w());
01068                         //      int64_t dif= get_difference(&start_time);
01069                         //      printf("diff: %lli\n", dif);
01070                         
01071                 }
01072                 else {
01073                         // Can't advance position here because it needs to be added to cache
01074                         file->read_frame(frame);
01075                 }
01076                 
01077                 if(use_cache) frame_cache->put_frame(frame,
01078                                                      current_frame,
01079                                                      asset->frame_rate,
01080                                                      1);
01081                 
01082                 if(advance_position) current_frame++;
01083                 return 0;
01084         }
01085         else
01086                 return 1;
01087 }
01088 
01089 int File::can_copy_from(Edit *edit, int64_t position, int output_w, int output_h)
01090 {
01091         if(file)
01092         {
01093                 return edit->asset->width == output_w &&
01094                         edit->asset->height == output_h &&
01095                         file->can_copy_from(edit, position);
01096         }
01097         else
01098                 return 0;
01099 }
01100 
01101 // Fill in queries about formats when adding formats here.
01102 
01103 
01104 int File::strtoformat(char *format)
01105 {
01106         return strtoformat(0, format);
01107 }
01108 
01109 int File::strtoformat(ArrayList<PluginServer*> *plugindb, char *format)
01110 {
01111         if(!strcasecmp(format, _(AC3_NAME))) return FILE_AC3;
01112         else
01113         if(!strcasecmp(format, _(WAV_NAME))) return FILE_WAV;
01114         else
01115         if(!strcasecmp(format, _(PCM_NAME))) return FILE_PCM;
01116         else
01117         if(!strcasecmp(format, _(AU_NAME))) return FILE_AU;
01118         else
01119         if(!strcasecmp(format, _(AIFF_NAME))) return FILE_AIFF;
01120         else
01121         if(!strcasecmp(format, _(SND_NAME))) return FILE_SND;
01122         else
01123         if(!strcasecmp(format, _(PNG_NAME))) return FILE_PNG;
01124         else
01125         if(!strcasecmp(format, _(PNG_LIST_NAME))) return FILE_PNG_LIST;
01126         else
01127         if(!strcasecmp(format, _(TIFF_NAME))) return FILE_TIFF;
01128         else
01129         if(!strcasecmp(format, _(TIFF_LIST_NAME))) return FILE_TIFF_LIST;
01130         else
01131         if(!strcasecmp(format, _(JPEG_NAME))) return FILE_JPEG;
01132         else
01133         if(!strcasecmp(format, _(JPEG_LIST_NAME))) return FILE_JPEG_LIST;
01134         else
01135         if(!strcasecmp(format, _(EXR_NAME))) return FILE_EXR;
01136         else
01137         if(!strcasecmp(format, _(EXR_LIST_NAME))) return FILE_EXR_LIST;
01138         else
01139         if(!strcasecmp(format, _(YUV_NAME))) return FILE_YUV;
01140         else
01141         if(!strcasecmp(format, _(CR2_NAME))) return FILE_CR2;
01142         else
01143         if(!strcasecmp(format, _(MPEG_NAME))) return FILE_MPEG;
01144         else
01145         if(!strcasecmp(format, _(AMPEG_NAME))) return FILE_AMPEG;
01146         else
01147         if(!strcasecmp(format, _(VMPEG_NAME))) return FILE_VMPEG;
01148         else
01149         if(!strcasecmp(format, _(TGA_NAME))) return FILE_TGA;
01150         else
01151         if(!strcasecmp(format, _(TGA_LIST_NAME))) return FILE_TGA_LIST;
01152         else
01153         if(!strcasecmp(format, _(MOV_NAME))) return FILE_MOV;
01154         else
01155         if(!strcasecmp(format, _(AVI_NAME))) return FILE_AVI;
01156         else
01157         if(!strcasecmp(format, _(AVI_LAVTOOLS_NAME))) return FILE_AVI_LAVTOOLS;
01158         else
01159         if(!strcasecmp(format, _(AVI_ARNE2_NAME))) return FILE_AVI_ARNE2;
01160         else
01161         if(!strcasecmp(format, _(AVI_ARNE1_NAME))) return FILE_AVI_ARNE1;
01162         else
01163         if(!strcasecmp(format, _(AVI_AVIFILE_NAME))) return FILE_AVI_AVIFILE;
01164         else
01165         if(!strcasecmp(format, _(OGG_NAME))) return FILE_OGG;
01166         else
01167         if(!strcasecmp(format, _(VORBIS_NAME))) return FILE_VORBIS;
01168         else
01169         if(!strcasecmp(format, _(RAWDV_NAME))) return FILE_RAWDV;
01170         return 0;
01171 }
01172 
01173 char* File::formattostr(int format)
01174 {
01175         return formattostr(0, format);
01176 }
01177 
01178 char* File::formattostr(ArrayList<PluginServer*> *plugindb, int format)
01179 {
01180         switch(format)
01181         {
01182                 case FILE_AC3:
01183                         return _(AC3_NAME);
01184                         break;
01185                 case FILE_WAV:
01186                         return _(WAV_NAME);
01187                         break;
01188                 case FILE_PCM:
01189                         return _(PCM_NAME);
01190                         break;
01191                 case FILE_AU:
01192                         return _(AU_NAME);
01193                         break;
01194                 case FILE_AIFF:
01195                         return _(AIFF_NAME);
01196                         break;
01197                 case FILE_SND:
01198                         return _(SND_NAME);
01199                         break;
01200                 case FILE_PNG:
01201                         return _(PNG_NAME);
01202                         break;
01203                 case FILE_PNG_LIST:
01204                         return _(PNG_LIST_NAME);
01205                         break;
01206                 case FILE_JPEG:
01207                         return _(JPEG_NAME);
01208                         break;
01209                 case FILE_JPEG_LIST:
01210                         return _(JPEG_LIST_NAME);
01211                         break;
01212                 case FILE_CR2:
01213                         return _(CR2_NAME);
01214                         break;
01215                 case FILE_EXR:
01216                         return _(EXR_NAME);
01217                         break;
01218                 case FILE_EXR_LIST:
01219                         return _(EXR_LIST_NAME);
01220                         break;
01221                 case FILE_YUV:
01222                         return _(YUV_NAME);
01223                         break;
01224                 case FILE_MPEG:
01225                         return _(MPEG_NAME);
01226                         break;
01227                 case FILE_AMPEG:
01228                         return _(AMPEG_NAME);
01229                         break;
01230                 case FILE_VMPEG:
01231                         return _(VMPEG_NAME);
01232                         break;
01233                 case FILE_TGA:
01234                         return _(TGA_NAME);
01235                         break;
01236                 case FILE_TGA_LIST:
01237                         return _(TGA_LIST_NAME);
01238                         break;
01239                 case FILE_TIFF:
01240                         return _(TIFF_NAME);
01241                         break;
01242                 case FILE_TIFF_LIST:
01243                         return _(TIFF_LIST_NAME);
01244                         break;
01245                 case FILE_MOV:
01246                         return _(MOV_NAME);
01247                         break;
01248                 case FILE_AVI_LAVTOOLS:
01249                         return _(AVI_LAVTOOLS_NAME);
01250                         break;
01251                 case FILE_AVI:
01252                         return _(AVI_NAME);
01253                         break;
01254                 case FILE_AVI_ARNE2:
01255                         return _(AVI_ARNE2_NAME);
01256                         break;
01257                 case FILE_AVI_ARNE1:
01258                         return _(AVI_ARNE1_NAME);
01259                         break;
01260                 case FILE_AVI_AVIFILE:
01261                         return _(AVI_AVIFILE_NAME);
01262                         break;
01263                 case FILE_OGG:
01264                         return _(OGG_NAME);
01265                         break;
01266                 case FILE_VORBIS:
01267                         return _(VORBIS_NAME);
01268                         break;
01269                 case FILE_RAWDV:
01270                         return _(RAWDV_NAME);
01271                         break;
01272                 default:
01273                         return _("Unknown");
01274                         break;
01275         }
01276         return "Unknown";
01277 }
01278 
01279 int File::strtobits(char *bits)
01280 {
01281         if(!strcasecmp(bits, _(NAME_8BIT))) return BITSLINEAR8;
01282         if(!strcasecmp(bits, _(NAME_16BIT))) return BITSLINEAR16;
01283         if(!strcasecmp(bits, _(NAME_24BIT))) return BITSLINEAR24;
01284         if(!strcasecmp(bits, _(NAME_32BIT))) return BITSLINEAR32;
01285         if(!strcasecmp(bits, _(NAME_ULAW))) return BITSULAW;
01286         if(!strcasecmp(bits, _(NAME_ADPCM))) return BITS_ADPCM;
01287         if(!strcasecmp(bits, _(NAME_FLOAT))) return BITSFLOAT;
01288         if(!strcasecmp(bits, _(NAME_IMA4))) return BITSIMA4;
01289         return BITSLINEAR16;
01290 }
01291 
01292 char* File::bitstostr(int bits)
01293 {
01294 //printf("File::bitstostr\n");
01295         switch(bits)
01296         {
01297                 case BITSLINEAR8:
01298                         return (NAME_8BIT);
01299                         break;
01300                 case BITSLINEAR16:
01301                         return (NAME_16BIT);
01302                         break;
01303                 case BITSLINEAR24:
01304                         return (NAME_24BIT);
01305                         break;
01306                 case BITSLINEAR32:
01307                         return (NAME_32BIT);
01308                         break;
01309                 case BITSULAW:
01310                         return (NAME_ULAW);
01311                         break;
01312                 case BITS_ADPCM:
01313                         return (NAME_ADPCM);
01314                         break;
01315                 case BITSFLOAT:
01316                         return (NAME_FLOAT);
01317                         break;
01318                 case BITSIMA4:
01319                         return (NAME_IMA4);
01320                         break;
01321         }
01322         return "Unknown";
01323 }
01324 
01325 
01326 
01327 int File::str_to_byteorder(char *string)
01328 {
01329         if(!strcasecmp(string, _("Lo Hi"))) return 1;
01330         return 0;
01331 }
01332 
01333 char* File::byteorder_to_str(int byte_order)
01334 {
01335         if(byte_order) return _("Lo Hi");
01336         return _("Hi Lo");
01337 }
01338 
01339 int File::bytes_per_sample(int bits)
01340 {
01341         switch(bits)
01342         {
01343                 case BITSLINEAR8:
01344                         return 1;
01345                         break;
01346                 case BITSLINEAR16:
01347                         return 2;
01348                         break;
01349                 case BITSLINEAR24:
01350                         return 3;
01351                         break;
01352                 case BITSLINEAR32:
01353                         return 4;
01354                         break;
01355                 case BITSULAW:
01356                         return 1;
01357                         break;
01358                 case BITSIMA4:
01359                         return 1;
01360                         break;
01361         }
01362         return 1;
01363 }
01364 
01365 
01366 
01367 
01368 
01369 int File::get_best_colormodel(int driver)
01370 {
01371         return get_best_colormodel(asset, driver);
01372 }
01373 
01374 int File::get_best_colormodel(Asset *asset, int driver)
01375 {
01376         switch(asset->format)
01377         {
01378                 case FILE_RAWDV:
01379                         return FileDV::get_best_colormodel(asset, driver);
01380                         break;
01381 
01382                 case FILE_MOV:
01383                         return FileMOV::get_best_colormodel(asset, driver);
01384                         break;
01385                 
01386                 case FILE_AVI:
01387                         return FileMOV::get_best_colormodel(asset, driver);
01388                         break;
01389 
01390                 case FILE_MPEG:
01391                         return FileMPEG::get_best_colormodel(asset, driver);
01392                         break;
01393                 
01394                 case FILE_JPEG:
01395                 case FILE_JPEG_LIST:
01396                         return FileJPEG::get_best_colormodel(asset, driver);
01397                         break;
01398 
01399                 case FILE_EXR:
01400                 case FILE_EXR_LIST:
01401                         return FileEXR::get_best_colormodel(asset, driver);
01402                         break;
01403                 
01404                 case FILE_YUV:
01405                         return FileYUV::get_best_colormodel(asset, driver);
01406                         break;
01407 
01408                 case FILE_PNG:
01409                 case FILE_PNG_LIST:
01410                         return FilePNG::get_best_colormodel(asset, driver);
01411                         break;
01412                 
01413                 case FILE_TGA:
01414                 case FILE_TGA_LIST:
01415                         return FileTGA::get_best_colormodel(asset, driver);
01416                         break;
01417         }
01418 
01419         return BC_RGB888;
01420 }
01421 
01422 
01423 int File::colormodel_supported(int colormodel)
01424 {
01425         if(file)
01426                 return file->colormodel_supported(colormodel);
01427 
01428         return BC_RGB888;
01429 }
01430 
01431 
01432 int File::get_memory_usage()
01433 {
01434         int result = 0;
01435         if(temp_frame) result += temp_frame->get_data_size();
01436         if(file) result += file->get_memory_usage();
01437         result += frame_cache->get_memory_usage();
01438 
01439         if(result < MIN_CACHEITEM_SIZE) result = MIN_CACHEITEM_SIZE;
01440         return result;
01441 }
01442 
01443 FrameCache* File::get_frame_cache()
01444 {
01445         return frame_cache;
01446 }
01447 
01448 int File::supports_video(ArrayList<PluginServer*> *plugindb, char *format)
01449 {
01450         int i, format_i = strtoformat(plugindb, format);
01451         
01452         return supports_video(format_i);
01453         return 0;
01454 }
01455 
01456 int File::supports_audio(ArrayList<PluginServer*> *plugindb, char *format)
01457 {
01458         int i, format_i = strtoformat(plugindb, format);
01459 
01460         return supports_audio(format_i);
01461         return 0;
01462 }
01463 
01464 
01465 int File::supports_video(int format)
01466 {
01467 //printf("File::supports_video %d\n", format);
01468         switch(format)
01469         {
01470                 case FILE_OGG:
01471                 case FILE_MOV:
01472                 case FILE_JPEG:
01473                 case FILE_JPEG_LIST:
01474                 case FILE_CR2:
01475                 case FILE_EXR:
01476                 case FILE_EXR_LIST:
01477                 case FILE_YUV:
01478                 case FILE_PNG:
01479                 case FILE_PNG_LIST:
01480                 case FILE_TGA:
01481                 case FILE_TGA_LIST:
01482                 case FILE_TIFF:
01483                 case FILE_TIFF_LIST:
01484                 case FILE_VMPEG:
01485                 case FILE_AVI_LAVTOOLS:
01486                 case FILE_AVI_ARNE2:
01487                 case FILE_AVI:
01488                 case FILE_AVI_ARNE1:
01489                 case FILE_AVI_AVIFILE:
01490                 case FILE_RAWDV:
01491                         return 1;
01492                         break;
01493 
01494                 default:
01495                         return 0;
01496                         break;
01497         }
01498 }
01499 
01500 int File::supports_audio(int format)
01501 {
01502         switch(format)
01503         {
01504                 case FILE_AC3:
01505                 case FILE_PCM:
01506                 case FILE_WAV:
01507                 case FILE_MOV:
01508                 case FILE_OGG:
01509                 case FILE_VORBIS:
01510                 case FILE_AMPEG:
01511                 case FILE_AU:
01512                 case FILE_AIFF:
01513                 case FILE_SND:
01514                 case FILE_AVI:
01515                 case FILE_AVI_LAVTOOLS:
01516                 case FILE_AVI_ARNE2:
01517                 case FILE_AVI_ARNE1:
01518                 case FILE_AVI_AVIFILE:
01519                 case FILE_RAWDV:
01520                         return 1;
01521                 
01522                 default:
01523                         return 0;
01524                         break;
01525         }
01526 }
01527 

Generated on Sun Jan 8 13:38:54 2006 for Cinelerra-svn by  doxygen 1.4.4