4 * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "bcsignals.h"
28 #include "condition.h"
31 #include "edlsession.h"
34 #include "filesystem.h"
37 #include "indexable.h"
38 #include "indexfile.h"
39 #include "indexstate.h"
40 #include "indexthread.h"
42 #include "localsession.h"
43 #include "mainprogress.h"
44 #include "mwindowgui.h"
46 #include "preferences.h"
47 #include "removefile.h"
48 #include "renderengine.h"
49 #include "resourcepixmap.h"
52 #include "timelinepane.h"
53 #include "trackcanvas.h"
55 #include "transportque.h"
68 #include <sys/types.h>
70 #include <linux/iso_fs.h>
72 // check for isofs volume_id for dvd/cdrom
74 static int udf_volume_id(const char *path, char *fname)
77 if( stat(path,&st) ) return 1;
78 // search mounted devices
79 FILE *fp = fopen("/proc/mounts","r");
83 while( result && !feof(fp) && !ferror(fp) ) {
84 char devpath[BCTEXTLEN], mpath[BCTEXTLEN];
85 char options[BCTEXTLEN], line[BCTEXTLEN];
86 char fstype[64], zero1[16], zero2[16];
87 if( !fgets(&line[0], sizeof(line)-1, fp) ) break;
88 int n = sscanf(&line[0], "%s %s %s %s %s %s\n",
89 devpath, mpath, fstype, options, zero1, zero2);
90 if( n != 6 ) continue;
91 // check udf filesystems
92 if( strcmp(fstype,"udf") != 0 ) continue;
94 if( stat(devpath,&dst) ) continue;
95 if( st.st_dev != dst.st_rdev ) continue;
96 int fd = open(devpath,O_RDONLY);
97 if( fd < 0 ) continue;
98 struct iso_primary_descriptor id;
99 if( lseek(fd,0x8000,SEEK_SET) == 0x8000 )
100 n = read(fd,&id,sizeof(id));
102 if( n != sizeof(id) ) continue;
103 // look for magic number
104 if( strncmp(ISO_STANDARD_ID,id.id,sizeof(id.id)) ) continue;
105 // look for volume_id
106 if( !isalnum(id.volume_id[0]) ) continue;
107 char *bp = &id.volume_id[0], *cp = fname;
108 for( int i=0; i<(int)sizeof(id.volume_id); ++i ) *cp++ = *bp++;
109 while( --cp>=fname && *cp==' ' ) *cp = 0;
110 if( !*fname ) continue;
111 // fname = volume_id _ creation_date
112 ++cp; *cp++ = '_'; bp = &id.creation_date[0];
113 for( int i=0; i<(int)sizeof(id.creation_date)-1; ++i ) {
114 if( !isdigit(*bp) ) break;
118 if( cp-fname > 4 ) result = 0;
125 // Use native sampling rates for files so the same index can be used in
126 // multiple projects.
128 IndexFile::IndexFile(MWindow *mwindow)
130 //printf("IndexFile::IndexFile 1\n");
132 this->mwindow = mwindow;
133 //printf("IndexFile::IndexFile 2\n");
134 redraw_timer = new Timer;
137 IndexFile::IndexFile(MWindow *mwindow,
138 Indexable *indexable)
140 //printf("IndexFile::IndexFile 2\n");
142 this->mwindow = mwindow;
143 this->indexable = indexable;
144 redraw_timer = new Timer;
148 indexable->add_user();
149 source_channels = indexable->get_audio_channels();
150 source_samplerate = indexable->get_sample_rate();
151 source_length = indexable->get_audio_samples();
155 IndexFile::~IndexFile()
157 //printf("IndexFile::~IndexFile 1\n");
159 if(indexable) indexable->remove_user();
163 void IndexFile::reset()
175 IndexState* IndexFile::get_state()
177 IndexState *index_state = 0;
178 if(indexable) index_state = indexable->index_state;
184 int IndexFile::open_index()
186 IndexState *index_state = 0;
189 // use buffer if being built
190 index_state = get_state();
192 if(index_state->index_status == INDEX_BUILDING)
198 if(!(result = open_file()))
200 // opened existing file
208 index_state->index_status = INDEX_READY;
219 void IndexFile::delete_index(Preferences *preferences,
220 Indexable *indexable, const char *suffix)
222 char index_filename[BCTEXTLEN];
223 char source_filename[BCTEXTLEN];
224 const char *path = indexable->path;
226 get_index_filename(source_filename,
227 preferences->index_directory,
228 index_filename, path, suffix);
229 //printf("IndexFile::delete_index %s %s\n", source_filename, index_filename);
230 remove_file(index_filename);
233 int IndexFile::open_file()
237 const char *path = indexable->path;
240 //printf("IndexFile::open_file %f\n", indexable->get_frame_rate());
242 get_index_filename(source_filename,
243 mwindow->preferences->index_directory,
247 if(debug) printf("IndexFile::open_file %d index_filename=%s\n",
250 fd = fopen(index_filename, "rb");
253 // Index file already exists.
254 // Get its last size without changing the real asset status.
255 Indexable *test_indexable = new Indexable(0);
258 test_indexable->copy_indexable(indexable);
261 read_info(test_indexable);
264 if(fs.get_date(index_filename) < fs.get_date(test_indexable->path))
266 if(debug) printf("IndexFile::open_file %d index_date=" _LD " source_date=" _LD "\n",
268 fs.get_date(index_filename),
269 fs.get_date(test_indexable->path));
271 // index older than source
277 if(fs.get_size(test_indexable->path) != test_indexable->index_state->index_bytes)
279 // source file is a different size than index source file
280 if(debug) printf("IndexFile::open_file %d index_size=" _LD " source_size=" _LD "\n",
282 test_indexable->index_state->index_bytes,
283 fs.get_size(test_indexable->path));
290 if(debug) printf("IndexFile::open_file %d\n",
292 fseek(fd, 0, SEEK_END);
293 file_length = ftell(fd);
294 fseek(fd, 0, SEEK_SET);
297 test_indexable->Garbage::remove_user();
302 if(debug) printf("IndexFile::open_file %d index_filename=%s doesn't exist\n",
311 int IndexFile::open_source()
313 //printf("IndexFile::open_source %p %s\n", asset, asset->path);
315 if(indexable && indexable->is_asset)
317 if(!source) source = new File;
319 Asset *asset = (Asset*)indexable;
320 if(source->open_file(mwindow->preferences,
323 //printf("IndexFile::open_source() Couldn't open %s.\n", asset->path);
329 asset->index_state->index_bytes = fs.get_size(asset->path);
330 source_length = source->get_audio_length();
335 TransportCommand command;
336 command.command = NORMAL_FWD;
337 command.get_edl()->copy_all((EDL*)indexable);
338 command.change_type = CHANGE_ALL;
339 command.realtime = 0;
340 cache = new CICache(mwindow->preferences);
341 render_engine = new RenderEngine(0,
342 mwindow->preferences, 0, 0, 0);
343 render_engine->set_acache(cache);
344 render_engine->arm_command(&command);
346 indexable->index_state->index_bytes = fs.get_size(indexable->path);
352 void IndexFile::close_source()
357 delete render_engine;
364 int64_t IndexFile::get_required_scale()
369 // get scale of index file
370 // Total peaks which may be stored in buffer
371 int64_t peak_count = mwindow->preferences->index_size /
372 (2 * sizeof(float) * source_channels);
374 source_length / result > peak_count;
378 // Takes too long to draw from source on a CDROM. Make indexes for
384 int IndexFile::get_index_filename(char *source_filename,
385 char *index_directory,
386 char *index_filename,
387 const char *input_filename,
390 const char *input_fn = input_filename;
391 char volume_id[BCTEXTLEN];
392 // Replace mount/directory with volume_id if isofs
393 if( !udf_volume_id(input_filename, volume_id) )
395 char *cp = strrchr((char*)input_filename,'/');
396 if( cp ) input_fn = cp + 1;
397 for( cp=volume_id; *cp; ++cp );
398 *cp++ = '_'; strcpy(cp, input_fn);
399 input_fn = volume_id;
401 // Replace slashes and dots
403 int len = strlen(input_fn);
404 for(i = 0, j = 0; i < len; i++)
406 if(input_fn[i] != '/' &&
408 source_filename[j++] = input_fn[i];
412 source_filename[j++] = '_';
415 source_filename[j] = 0;
417 fs.join_names(index_filename, index_directory, source_filename);
418 strcat(index_filename, suffix ? suffix : ".idx");
422 int IndexFile::interrupt_index()
428 // Read data into buffers
430 int IndexFile::create_index(MainProgressBar *progress)
436 IndexState *index_state = get_state();
440 // open the source file
441 if(open_source()) return 1;
445 get_index_filename(source_filename,
446 mwindow->preferences->index_directory,
452 // Some file formats have their own sample index.
453 // Test for index in stream table of contents
454 if(source && !source->get_index(index_filename))
459 // Build index from scratch
465 index_state->index_zoom = get_required_scale();
468 // Indexes are now built for everything since it takes too long to draw
469 // from CDROM source.
471 // get amount to read at a time in floats
472 int64_t buffersize = 65536;
473 char string[BCTEXTLEN];
474 sprintf(string, _("Creating %s."), index_filename);
476 progress->update_title(string);
477 progress->update_length(source_length);
478 redraw_timer->update();
481 // thread out index thread
482 IndexThread *index_thread = new IndexThread(mwindow,
487 index_thread->start_build();
489 // current sample in source file
490 int64_t position = 0;
491 int64_t fragment_size = buffersize;
492 int current_buffer = 0;
495 // pass through file once
496 // printf("IndexFile::create_index %d source_length=" _LD " source=%p progress=%p\n",
502 while(position < source_length && !result)
505 if(source_length - position < fragment_size && fragment_size == buffersize) fragment_size = source_length - position;
507 index_thread->input_lock[current_buffer]->lock("IndexFile::create_index 1");
508 index_thread->input_len[current_buffer] = fragment_size;
511 int cancelled = progress->update(position);
512 //printf("IndexFile::create_index cancelled=%d\n", cancelled);
515 index_thread->interrupt_flag ||
523 if(source && !result)
527 !result && channel < source_channels;
530 // Read from source file
531 source->set_audio_position(position);
532 source->set_channel(channel);
534 if(source->read_samples(
535 index_thread->buffer_in[current_buffer][channel],
542 if(render_engine && !result)
545 if(render_engine->arender)
547 result = render_engine->arender->process_buffer(
548 index_thread->buffer_in[current_buffer],
554 for(int i = 0; i < source_channels; i++)
556 bzero(index_thread->buffer_in[current_buffer][i]->get_data(),
557 fragment_size * sizeof(double));
564 // Release buffer to thread
567 index_thread->output_lock[current_buffer]->unlock();
569 if(current_buffer >= TOTAL_INDEX_BUFFERS) current_buffer = 0;
570 position += fragment_size;
574 index_thread->input_lock[current_buffer]->unlock();
580 // end thread cleanly
581 index_thread->input_lock[current_buffer]->lock("IndexFile::create_index 2");
582 index_thread->last_buffer[current_buffer] = 1;
583 index_thread->output_lock[current_buffer]->unlock();
584 index_thread->stop_build();
601 mwindow->edl->set_index_file(indexable);
607 int IndexFile::redraw_edits(int force)
609 int64_t difference = redraw_timer->get_scaled_difference(1000);
611 if(difference > 250 || force)
613 IndexState *index_state = get_state();
614 redraw_timer->update();
615 // Can't lock window here since the window is only redrawn when the pixel
617 mwindow->gui->lock_window("IndexFile::redraw_edits");
618 mwindow->edl->set_index_file(indexable);
619 mwindow->gui->draw_indexes(indexable);
620 index_state->old_index_end = index_state->index_end;
621 mwindow->gui->unlock_window();
629 int IndexFile::draw_index(
631 ResourcePixmap *pixmap,
637 IndexState *index_state = get_state();
638 int pane_number = canvas->pane->number;
639 //index_state->dump();
642 if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
643 // check against index_end when being built
644 if(index_state->index_zoom == 0)
646 printf(_("IndexFile::draw_index: index has 0 zoom\n"));
649 if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
651 // test channel number
652 if(edit->channel > source_channels) return 1;
653 if(debug) printf("IndexFile::draw_index %d source_samplerate=%d "
654 "w=%d samplerate=" _LD " zoom_sample=" _LD "\n",
655 __LINE__, source_samplerate, w,
656 mwindow->edl->session->sample_rate,
657 mwindow->edl->local_session->zoom_sample);
659 // calculate a virtual x where the edit_x should be in floating point
660 double virtual_edit_x = 1.0 *
661 edit->track->from_units(edit->startproject) *
662 mwindow->edl->session->sample_rate /
663 mwindow->edl->local_session->zoom_sample -
664 mwindow->edl->local_session->view_start[pane_number];
666 // samples in segment to draw relative to asset
667 double asset_over_session = (double)source_samplerate /
668 mwindow->edl->session->sample_rate;
669 int64_t startsource = (int64_t)(((pixmap->pixmap_x - virtual_edit_x + x) *
670 mwindow->edl->local_session->zoom_sample +
673 // just in case we get a numerical error
674 if (startsource < 0) startsource = 0;
675 int64_t length = (int64_t)(w *
676 mwindow->edl->local_session->zoom_sample *
679 if(index_state->index_status == INDEX_BUILDING)
681 if(startsource + length > index_state->index_end)
682 length = index_state->index_end - startsource;
685 // length of index to read in samples * 2
686 int64_t lengthindex = length / index_state->index_zoom * 2;
687 // start of data in samples
688 int64_t startindex = startsource / index_state->index_zoom * 2;
689 // Clamp length of index to read by available data
690 if(startindex + lengthindex > index_state->get_index_size(edit->channel))
691 lengthindex = index_state->get_index_size(edit->channel) - startindex;
694 if(debug) printf("IndexFile::draw_index %d length=" _LD ""
695 " index_size=" _LD " lengthindex=" _LD "\n",
696 __LINE__, length, index_state->get_index_size(edit->channel),
698 if(lengthindex <= 0) return 0;
703 // Actual length read from file in bytes
705 // Start and length of fragment to read from file in bytes.
706 int64_t startfile, lengthfile;
708 int buffer_shared = 0;
709 int center_pixel = mwindow->edl->local_session->zoom_track / 2;
710 if(mwindow->edl->session->show_titles) center_pixel += mwindow->theme->get_image("title_bg_data")->get_h();
711 //int miny = center_pixel - mwindow->edl->local_session->zoom_track / 2;
712 //int maxy = center_pixel + mwindow->edl->local_session->zoom_track / 2;
714 // get zoom_sample relative to index zoomx
715 double index_frames_per_pixel = mwindow->edl->local_session->zoom_sample /
716 index_state->index_zoom *
719 // get channel offset
720 startindex += index_state->get_index_offset(edit->channel);
723 if(index_state->index_status == INDEX_BUILDING)
725 // index is in RAM, being built
726 buffer = &(index_state->index_buffer[startindex]);
731 // index is stored in a file
732 buffer = new float[lengthindex + 1];
734 startfile = index_state->index_start + startindex * sizeof(float);
735 lengthfile = lengthindex * sizeof(float);
738 if(startfile < file_length)
740 fseek(fd, startfile, SEEK_SET);
742 length_read = lengthfile;
743 if(startfile + length_read > file_length)
744 length_read = file_length - startfile;
746 (void)fread(buffer, length_read + sizeof(float), 1, fd);
749 if(length_read < lengthfile) {
750 int pos = length_read / sizeof(float);
751 int file_length = lengthfile / sizeof(float);
752 while( pos < file_length ) buffer[pos++] = 0;
758 canvas->set_color(mwindow->theme->audio_color);
761 double current_frame = 0;
762 float highsample = buffer[0];
763 float lowsample = buffer[1];
764 int prev_y1 = center_pixel;
765 int prev_y2 = center_pixel;
769 for(int bufferposition = 0;
770 bufferposition < lengthindex;
773 if(current_frame >= index_frames_per_pixel)
775 int next_y1 = (int)(center_pixel - highsample * mwindow->edl->local_session->zoom_y / 2);
776 int next_y2 = (int)(center_pixel - lowsample * mwindow->edl->local_session->zoom_y / 2);
777 if(next_y1 < 0) next_y1 = 0;
778 if(next_y1 > canvas->get_h()) next_y1 = canvas->get_h();
779 if(next_y2 < 0) next_y2 = 0;
780 if(next_y2 > canvas->get_h()) next_y2 = canvas->get_h();
781 if(next_y2 > center_pixel + mwindow->edl->local_session->zoom_y / 2 - 1)
782 next_y2 = center_pixel + mwindow->edl->local_session->zoom_y / 2 - 1;
783 if(next_y1 > center_pixel + mwindow->edl->local_session->zoom_y / 2 - 1)
784 next_y1 = center_pixel + mwindow->edl->local_session->zoom_y / 2 - 1;
793 // A different algorithm has to be used if it's 1 sample per pixel and the
794 // index is used. Now the min and max values are equal so we join the max samples.
795 if(mwindow->edl->local_session->zoom_sample == 1)
797 canvas->draw_line(x1 + x - 1, prev_y1, x1 + x, y1, pixmap);
801 // Extend line height if it doesn't connect to previous line
804 if(y1 > prev_y2) y1 = prev_y2 + 1;
805 if(y2 < prev_y1) y2 = prev_y1 - 1;
814 canvas->draw_line(x1 + x, y1, x1 + x, y2, pixmap);
816 current_frame -= index_frames_per_pixel;
820 highsample = buffer[bufferposition];
821 lowsample = buffer[bufferposition + 1];
825 highsample = MAX(highsample, buffer[bufferposition]);
826 lowsample = MIN(lowsample, buffer[bufferposition + 1]);
833 y1 = (int)(center_pixel - highsample * mwindow->edl->local_session->zoom_y / 2);
834 y2 = (int)(center_pixel - lowsample * mwindow->edl->local_session->zoom_y / 2);
835 canvas->draw_line(x1 + x, y1, x1 + x, y2, pixmap);
842 if(!buffer_shared) delete [] buffer;
844 if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
848 int IndexFile::close_index()
858 int IndexFile::remove_index()
860 IndexState *index_state = get_state();
861 if(index_state->index_status == INDEX_READY ||
862 index_state->index_status == INDEX_NOTTESTED)
865 remove(index_filename);
870 int IndexFile::read_info(Indexable *test_indexable)
874 // Store format in actual asset.
875 // If it's a nested EDL, we never want the format, just the index info.
876 if(!test_indexable) test_indexable = indexable;
879 IndexState *index_state = 0;
881 index_state = test_indexable->index_state;
886 if(index_state->index_status == INDEX_NOTTESTED)
888 // read start of index data
889 int temp = fread((char*)&(index_state->index_start), sizeof(int64_t), 1, fd);
890 //printf("IndexFile::read_info %d %f\n", __LINE__, test_indexable->get_frame_rate());
893 // read test_indexable info from index
896 data = new char[index_state->index_start];
897 temp = fread(data, index_state->index_start - sizeof(int64_t), 1, fd);
900 data[index_state->index_start - sizeof(int64_t)] = 0;
902 xml.read_from_string(data);
907 // Read the file format & index state.
908 if(test_indexable->is_asset)
910 Asset *asset = (Asset*)test_indexable;
913 //printf("IndexFile::read_info %d %f\n", __LINE__, asset->get_frame_rate());
915 if(asset->format == FILE_UNKNOWN)
917 if(debug) printf("IndexFile::read_info %d\n", __LINE__);
923 // Read only the index state for a nested EDL
925 if(debug) printf("IndexFile::read_info %d\n", __LINE__);
928 result = xml.read_tag();
931 if(xml.tag.title_is("INDEX"))
933 index_state->read_xml(&xml, source_channels);
934 if(debug) printf("IndexFile::read_info %d\n", __LINE__);
935 if(debug) index_state->dump();