13 #include "filesndfile.h"
15 #include "filethread.h"
17 #include "filevorbis.h"
18 #include "formatwindow.h"
19 #include "pluginserver.h"
21 #include "stringfile.h"
26 #define _(String) gettext(String)
27 #define gettext_noop(String) String
28 #define N_(String) gettext_noop (String)
42 if(format_window) format_window->set_done(0);
43 format_completion.lock("File::~File");
44 format_completion.unlock();
62 void File::reset_parameters()
76 normalized_sample = 0;
77 normalized_sample_rate = 0;
82 int File::raise_window()
84 if(getting_options && format_window)
86 format_window->raise_window();
87 format_window->flush();
92 void File::close_window()
96 format_window->lock_window("File::close_window");
97 format_window->set_done(1);
98 format_window->unlock_window();
103 int File::get_options(BC_WindowBase *parent_window,
104 ArrayList<PluginServer*> *plugindb,
111 format_completion.lock("File::get_options");
112 switch(asset->format)
119 FileSndFile::get_parameters(parent_window,
126 FileMOV::get_parameters(parent_window,
135 FileMPEG::get_parameters(parent_window,
142 FileMOV::get_parameters(parent_window,
149 case FILE_AVI_LAVTOOLS:
152 case FILE_AVI_AVIFILE:
153 FileAVI::get_parameters(parent_window,
162 FileJPEG::get_parameters(parent_window,
170 FilePNG::get_parameters(parent_window,
178 FileTGA::get_parameters(parent_window,
186 FileTIFF::get_parameters(parent_window,
193 FileVorbis::get_parameters(parent_window,
205 ErrorBox *errorbox = new ErrorBox(PROGRAM_NAME ": Error",
206 parent_window->get_abs_cursor_x(),
207 parent_window->get_abs_cursor_y());
208 format_window = errorbox;
211 errorbox->create_objects(_("This format doesn't support audio."));
214 errorbox->create_objects(_("This format doesn't support video."));
215 errorbox->run_window();
221 format_completion.unlock();
225 void File::set_asset(Asset *asset)
227 *this->asset = *asset;
230 int File::set_processors(int cpus) // Set the number of cpus for certain codecs
232 //printf("File::set_processors 1 %d\n", cpus);
237 int File::set_preload(int64_t size)
239 this->playback_preload = size;
243 int File::open_file(ArrayList<PluginServer*> *plugindb,
247 int64_t base_samplerate,
248 float base_framerate)
250 *this->asset = *asset;
254 //printf("File::open_file 1 %s %d\n", asset->path, asset->format);
255 switch(this->asset->format)
257 // get the format now
258 // If you add another format to case 0, you also need to add another case for the
259 // file format #define.
262 if(!(stream = fopen(this->asset->path, "rb")))
269 fread(test, 16, 1, stream);
271 // if(FileAVI::check_sig(this->asset))
274 // file = new FileAVI(this->asset, this);
277 if(FileSndFile::check_sig(this->asset))
281 file = new FileSndFile(this->asset, this);
284 if(FilePNG::check_sig(this->asset))
288 file = new FilePNG(this->asset, this);
291 if(FileJPEG::check_sig(this->asset))
295 file = new FileJPEG(this->asset, this);
298 if(FileTGA::check_sig(this->asset))
302 file = new FileTGA(this->asset, this);
305 if(FileTIFF::check_sig(this->asset))
309 file = new FileTIFF(this->asset, this);
312 if(FileVorbis::check_sig(this->asset))
316 file = new FileVorbis(this->asset, this);
319 if(FileMPEG::check_sig(this->asset))
323 file = new FileMPEG(this->asset, this);
326 if(test[0] == '<' && test[1] == 'E' && test[2] == 'D' && test[3] == 'L' && test[4] == '>' ||
327 test[0] == '<' && test[1] == 'H' && test[2] == 'T' && test[3] == 'A' && test[4] == 'L' && test[5] == '>' ||
328 test[0] == '<' && test[1] == '?' && test[2] == 'x' && test[3] == 'm' && test[4] == 'l')
333 } // can't load project file
335 if(FileMOV::check_sig(this->asset))
338 // should be last because quicktime lacks a magic number
340 file = new FileMOV(this->asset, this);
346 return FILE_UNRECOGNIZED_CODEC;
350 // format already determined
356 //printf("File::open_file 1\n");
357 file = new FileSndFile(this->asset, this);
362 file = new FilePNG(this->asset, this);
367 file = new FileJPEG(this->asset, this);
372 file = new FileTGA(this->asset, this);
377 file = new FileTIFF(this->asset, this);
381 file = new FileMOV(this->asset, this);
387 file = new FileMPEG(this->asset, this);
391 file = new FileVorbis(this->asset, this);
395 file = new FileMOV(this->asset, this);
398 case FILE_AVI_LAVTOOLS:
401 case FILE_AVI_AVIFILE:
402 file = new FileAVI(this->asset, this);
410 //printf("File::open_file 2\n");
412 // Reopen file with correct parser and get header.
413 if(file->open_file(rd, wr))
415 //printf("File::open_file 2.5\n");
419 //printf("File::open_file 3\n");
422 // Set extra writing parameters to mandatory settings.
425 if(this->asset->dither) file->set_dither();
428 // Synchronize header parameters
429 *asset = *this->asset;
434 return FILE_NOT_FOUND;
437 int File::close_file(int ignore_thread)
447 // The file's asset is a copy of the argument passed to open_file so the
448 // user must copy lengths from the file's asset.
449 if(asset && file->wr)
451 asset->audio_length = current_sample;
452 asset->video_length = current_frame;
458 if(resample) delete resample;
464 int File::start_audio_thread(int64_t buffer_size, int ring_buffers)
466 audio_thread = new FileThread(this, 1, 0);
467 audio_thread->start_writing(buffer_size, 0, ring_buffers, 0);
471 int File::start_video_thread(int64_t buffer_size,
476 video_thread = new FileThread(this, 0, 1);
477 //printf("File::start_video_thread 1\n");
478 video_thread->start_writing(buffer_size,
482 //printf("File::start_video_thread 2\n");
486 int File::stop_audio_thread()
488 //printf("File::stop_audio_thread 1\n");
491 audio_thread->stop_writing();
498 int File::stop_video_thread()
500 //printf("File::stop_video_thread 1\n");
503 video_thread->stop_writing();
510 int File::lock_read()
516 int File::unlock_read()
518 // read_lock.unlock();
522 int File::set_channel(int channel)
524 if(file && channel < asset->channels)
526 current_channel = channel;
533 int File::set_layer(int layer)
535 if(file && layer < asset->layers)
537 current_layer = layer;
538 //printf("File::set_layer 1 %d\n", layer);
545 int64_t File::get_audio_length(int64_t base_samplerate)
547 int64_t result = asset->audio_length;
550 if(base_samplerate > 0)
551 return (int64_t)((double)result / asset->sample_rate * base_samplerate + 0.5);
559 int64_t File::get_video_length(float base_framerate)
561 int64_t result = asset->video_length;
564 if(base_framerate > 0)
565 return (int64_t)((double)result / asset->frame_rate * base_framerate + 0.5);
570 return -1; // infinity
574 int64_t File::get_video_position(float base_framerate)
576 if(base_framerate > 0)
577 return (int64_t)((double)current_frame / asset->frame_rate * base_framerate + 0.5);
579 return current_frame;
582 int64_t File::get_audio_position(int64_t base_samplerate)
584 if(base_samplerate > 0)
586 if(normalized_sample_rate == base_samplerate)
587 return normalized_sample;
589 return (int64_t)((double)current_sample /
595 return current_sample;
600 // The base samplerate must be nonzero if the base samplerate in the calling
601 // function is expected to change as this forces the resampler to reset.
603 int File::set_audio_position(int64_t position, float base_samplerate)
609 #define REPOSITION(x, y) \
610 (labs((x) - (y)) > 1)
614 if((base_samplerate && REPOSITION(normalized_sample, position)) ||
615 (!base_samplerate && REPOSITION(current_sample, position)))
617 // Can't reset resampler since one seek operation is done
618 // for every channel to be read at the same position.
620 // Use a conditional reset for just the case of different base_samplerates
621 if(base_samplerate > 0)
623 if(normalized_sample_rate &&
624 normalized_sample_rate != base_samplerate &&
628 normalized_sample = position;
629 normalized_sample_rate = (int64_t)((base_samplerate > 0) ?
633 // Convert position to file's rate
634 if(base_samplerate > 0)
635 current_sample = Units::round((double)position /
641 current_sample = position;
642 normalized_sample = Units::round((double)position /
644 normalized_sample_rate);
645 // Can not set the normalized sample rate since this would reset the resampler.
648 result = file->set_audio_position(current_sample);
651 printf("File::set_audio_position position=%d base_samplerate=%f asset=%p asset->sample_rate=%d\n",
652 position, base_samplerate, asset, asset->sample_rate);
655 //printf("File::set_audio_position %d %d %d\n", current_channel, current_sample, position);
660 int File::set_video_position(int64_t position, float base_framerate)
665 // Convert to file's rate
666 if(base_framerate > 0)
667 position = (int64_t)((double)position / base_framerate * asset->frame_rate + 0.5);
669 if(current_frame != position && file)
671 current_frame = position;
672 result = file->set_video_position(current_frame);
678 // No resampling here.
679 int File::write_samples(double **buffer, int64_t len)
685 write_lock.lock("File::write_samples");
686 result = file->write_samples(buffer, len);
687 current_sample += len;
688 normalized_sample += len;
689 asset->audio_length += len;
695 // Can't put any cmodel abstraction here because the filebase couldn't be
697 int File::write_frames(VFrame ***frames, int len)
699 // Store the counters in temps so the filebase can choose to overwrite them.
701 int current_frame_temp = current_frame;
702 int video_length_temp = asset->video_length;
703 write_lock.lock("File::write_frames");
708 result = file->write_frames(frames, len);
714 current_frame = current_frame_temp + len;
715 asset->video_length = video_length_temp + len;
720 int File::write_compressed_frame(VFrame *buffer)
723 write_lock.lock("File::write_compressed_frame");
724 result = file->write_compressed_frame(buffer);
726 asset->video_length++;
732 int File::write_audio_buffer(int64_t len)
737 result = audio_thread->write_buffer(len);
742 int File::write_video_buffer(int64_t len)
747 result = video_thread->write_buffer(len);
753 double** File::get_audio_buffer()
755 if(audio_thread) return audio_thread->get_audio_buffer();
759 VFrame*** File::get_video_buffer()
761 if(video_thread) return video_thread->get_video_buffer();
766 int File::read_samples(double *buffer, int64_t len, int64_t base_samplerate)
769 //printf("File::read_samples 1\n");
771 // Load with resampling
774 // Resample recursively calls this with the asset sample rate
775 if(base_samplerate == 0) base_samplerate = asset->sample_rate;
777 //printf("File::read_samples 2 %d %d\n", base_samplerate, asset->sample_rate);
778 if(base_samplerate != asset->sample_rate)
780 //printf("File::read_samples 3\n");
783 //printf("File::read_samples 4\n");
784 resample = new Resample(this, asset->channels);
787 //printf("File::read_samples 5\n");
788 current_sample += resample->resample(buffer,
795 //printf("File::read_samples 6\n");
800 //printf("File::read_samples 7\n");
801 result = file->read_samples(buffer, len);
802 //printf("File::read_samples 8\n");
803 current_sample += len;
806 normalized_sample += len;
811 int File::read_compressed_frame(VFrame *buffer)
815 result = file->read_compressed_frame(buffer);
820 int64_t File::compressed_frame_size()
823 return file->compressed_frame_size();
828 // Return a pointer to a frame in the video file for drawing purposes.
829 // The temporary frame is created by the file handler so that still frame
830 // files don't have to copy to a new buffer.
831 VFrame* File::read_frame(int color_model)
834 //printf("File::read_frame 1\n");
837 //printf("File::read_frame 2\n");
839 (return_frame->get_w() != asset->width ||
840 return_frame->get_h() != asset->height ||
841 return_frame->get_color_model() != color_model))
847 //printf("File::read_frame 3\n");
850 return_frame = new VFrame(0,
855 //printf("File::read_frame 4\n");
857 read_frame(return_frame);
858 //printf("File::read_frame 5\n");
859 result = return_frame;
861 //printf("File::read_frame 6\n");
867 int File::read_frame(VFrame *frame)
871 //printf("File::read_frame 1\n");
872 int supported_colormodel = colormodel_supported(frame->get_color_model());
874 //printf("File::read_frame 1 %d %d\n", supported_colormodel, frame->get_color_model());
876 //printf("File::read_frame 2\n");
877 if(frame->get_color_model() != BC_COMPRESSED &&
878 (supported_colormodel != frame->get_color_model() ||
879 frame->get_w() != asset->width ||
880 frame->get_h() != asset->height))
882 //printf("File::read_frame 3\n");
885 if(!temp_frame->params_match(asset->width, asset->height, supported_colormodel))
891 //printf("File::read_frame 4 %p %d %d %d\n", asset , asset->width, asset->height, supported_colormodel);
895 temp_frame = new VFrame(0,
898 supported_colormodel);
901 //printf("File::read_frame 5 %d %d\n",
902 // temp_frame->get_color_model(),
903 // frame->get_color_model());
904 file->read_frame(temp_frame);
905 cmodel_transfer(frame->get_rows(),
906 temp_frame->get_rows(),
921 temp_frame->get_color_model(),
922 frame->get_color_model(),
926 //printf("File::read_frame 6\n");
930 //printf("File::read_frame 7\n");
931 file->read_frame(frame);
932 //printf("File::read_frame 8\n");
935 //printf("File::read_frame 9\n");
937 //printf("File::read_frame 2 %d\n", supported_colormodel);
944 int File::can_copy_from(Edit *edit, int64_t position, int output_w, int output_h)
948 return edit->asset->width == output_w &&
949 edit->asset->height == output_h &&
950 file->can_copy_from(edit, position);
956 // Fill in queries about formats when adding formats here.
959 int File::strtoformat(char *format)
961 return strtoformat(0, format);
964 int File::strtoformat(ArrayList<PluginServer*> *plugindb, char *format)
966 if(!strcasecmp(format, _(WAV_NAME))) return FILE_WAV;
968 if(!strcasecmp(format, _(PCM_NAME))) return FILE_PCM;
970 if(!strcasecmp(format, _(AU_NAME))) return FILE_AU;
972 if(!strcasecmp(format, _(AIFF_NAME))) return FILE_AIFF;
974 if(!strcasecmp(format, _(SND_NAME))) return FILE_SND;
976 if(!strcasecmp(format, _(PNG_NAME))) return FILE_PNG;
978 if(!strcasecmp(format, _(PNG_LIST_NAME))) return FILE_PNG_LIST;
980 if(!strcasecmp(format, _(TIFF_NAME))) return FILE_TIFF;
982 if(!strcasecmp(format, _(TIFF_LIST_NAME))) return FILE_TIFF_LIST;
984 if(!strcasecmp(format, _(JPEG_NAME))) return FILE_JPEG;
986 if(!strcasecmp(format, _(JPEG_LIST_NAME))) return FILE_JPEG_LIST;
988 if(!strcasecmp(format, _(MPEG_NAME))) return FILE_MPEG;
990 if(!strcasecmp(format, _(AMPEG_NAME))) return FILE_AMPEG;
992 if(!strcasecmp(format, _(VMPEG_NAME))) return FILE_VMPEG;
994 if(!strcasecmp(format, _(TGA_NAME))) return FILE_TGA;
996 if(!strcasecmp(format, _(TGA_LIST_NAME))) return FILE_TGA_LIST;
998 if(!strcasecmp(format, _(MOV_NAME))) return FILE_MOV;
1000 if(!strcasecmp(format, _(AVI_NAME))) return FILE_AVI;
1002 if(!strcasecmp(format, _(AVI_LAVTOOLS_NAME))) return FILE_AVI_LAVTOOLS;
1004 if(!strcasecmp(format, _(AVI_ARNE2_NAME))) return FILE_AVI_ARNE2;
1006 if(!strcasecmp(format, _(AVI_ARNE1_NAME))) return FILE_AVI_ARNE1;
1008 if(!strcasecmp(format, _(AVI_AVIFILE_NAME))) return FILE_AVI_AVIFILE;
1010 if(!strcasecmp(format, _(VORBIS_NAME))) return FILE_VORBIS;
1015 char* File::formattostr(int format)
1017 return formattostr(0, format);
1020 char* File::formattostr(ArrayList<PluginServer*> *plugindb, int format)
1034 return _(AIFF_NAME);
1043 return _(PNG_LIST_NAME);
1046 return _(JPEG_NAME);
1048 case FILE_JPEG_LIST:
1049 return _(JPEG_LIST_NAME);
1052 return _(MPEG_NAME);
1055 return _(AMPEG_NAME);
1058 return _(VMPEG_NAME);
1064 return _(TGA_LIST_NAME);
1067 return _(TIFF_NAME);
1069 case FILE_TIFF_LIST:
1070 return _(TIFF_LIST_NAME);
1075 case FILE_AVI_LAVTOOLS:
1076 return _(AVI_LAVTOOLS_NAME);
1081 case FILE_AVI_ARNE2:
1082 return _(AVI_ARNE2_NAME);
1084 case FILE_AVI_ARNE1:
1085 return _(AVI_ARNE1_NAME);
1087 case FILE_AVI_AVIFILE:
1088 return _(AVI_AVIFILE_NAME);
1091 return _(VORBIS_NAME);
1095 return _("Unknown");
1101 int File::strtobits(char *bits)
1103 if(!strcasecmp(bits, _(NAME_8BIT))) return BITSLINEAR8;
1104 if(!strcasecmp(bits, _(NAME_16BIT))) return BITSLINEAR16;
1105 if(!strcasecmp(bits, _(NAME_24BIT))) return BITSLINEAR24;
1106 if(!strcasecmp(bits, _(NAME_32BIT))) return BITSLINEAR32;
1107 if(!strcasecmp(bits, _(NAME_ULAW))) return BITSULAW;
1108 if(!strcasecmp(bits, _(NAME_ADPCM))) return BITS_ADPCM;
1109 if(!strcasecmp(bits, _(NAME_FLOAT))) return BITSFLOAT;
1110 if(!strcasecmp(bits, _(NAME_IMA4))) return BITSIMA4;
1111 return BITSLINEAR16;
1114 char* File::bitstostr(int bits)
1116 //printf("File::bitstostr\n");
1123 return (NAME_16BIT);
1126 return (NAME_24BIT);
1129 return (NAME_32BIT);
1135 return (NAME_ADPCM);
1138 return (NAME_FLOAT);
1149 int File::str_to_byteorder(char *string)
1151 if(!strcasecmp(string, _("Lo Hi"))) return 1;
1155 char* File::byteorder_to_str(int byte_order)
1157 if(byte_order) return _("Lo Hi");
1161 int File::bytes_per_sample(int bits)
1191 int File::get_best_colormodel(int driver)
1193 return get_best_colormodel(asset, driver);
1196 int File::get_best_colormodel(Asset *asset, int driver)
1198 switch(asset->format)
1201 return FileMOV::get_best_colormodel(asset, driver);
1205 return FileMPEG::get_best_colormodel(asset, driver);
1209 case FILE_JPEG_LIST:
1210 return FileJPEG::get_best_colormodel(asset, driver);
1215 return FilePNG::get_best_colormodel(asset, driver);
1220 return FileTGA::get_best_colormodel(asset, driver);
1228 int File::colormodel_supported(int colormodel)
1231 return file->colormodel_supported(colormodel);
1240 int File::supports_video(ArrayList<PluginServer*> *plugindb, char *format)
1242 int i, format_i = strtoformat(plugindb, format);
1244 return supports_video(format_i);
1246 // for(i = 0; i < plugindb->total; i++)
1248 // if(plugindb->values[i]->fileio &&
1249 // !strcmp(plugindb->values[i]->title, format))
1251 // if(plugindb->values[i]->video) return 1;
1258 int File::supports_audio(ArrayList<PluginServer*> *plugindb, char *format)
1260 int i, format_i = strtoformat(plugindb, format);
1262 return supports_audio(format_i);
1264 // for(i = 0; i < plugindb->total; i++)
1266 // if(plugindb->values[i]->fileio &&
1267 // !strcmp(plugindb->values[i]->title, format))
1269 // if(plugindb->values[i]->audio) return 1;
1277 int File::supports_video(int format)
1279 //printf("File::supports_video %d\n", format);
1284 case FILE_JPEG_LIST:
1290 case FILE_TIFF_LIST:
1292 case FILE_AVI_LAVTOOLS:
1293 case FILE_AVI_ARNE2:
1295 case FILE_AVI_ARNE1:
1296 case FILE_AVI_AVIFILE:
1306 int File::supports_audio(int format)
1319 case FILE_AVI_LAVTOOLS:
1320 case FILE_AVI_ARNE2:
1321 case FILE_AVI_ARNE1:
1322 case FILE_AVI_AVIFILE: