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"
36 #include "indexable.h"
37 #include "indexfile.h"
38 #include "indexstate.h"
39 #include "indexthread.h"
41 #include "localsession.h"
42 #include "mainprogress.h"
43 #include "mwindowgui.h"
45 #include "preferences.h"
46 #include "removefile.h"
47 #include "renderengine.h"
48 #include "resourcepixmap.h"
51 #include "timelinepane.h"
52 #include "trackcanvas.h"
54 #include "transportque.h"
67 #include <sys/types.h>
69 #include <linux/iso_fs.h>
71 // check for isofs volume_id for dvd/cdrom
73 static int udf_volume_id(const char *path, char *fname)
76 if( stat(path,&st) ) return 1;
77 // search mounted devices
78 FILE *fp = fopen("/proc/mounts","r");
82 while( result && !feof(fp) && !ferror(fp) ) {
83 char devpath[BCTEXTLEN], mpath[BCTEXTLEN];
84 char options[BCTEXTLEN], line[BCTEXTLEN];
85 char fstype[64], zero1[16], zero2[16];
86 if( !fgets(&line[0], sizeof(line)-1, fp) ) break;
87 int n = sscanf(&line[0], "%s %s %s %s %s %s\n",
88 devpath, mpath, fstype, options, zero1, zero2);
89 if( n != 6 ) continue;
90 // check udf filesystems
91 if( strcmp(fstype,"udf") != 0 ) continue;
93 if( stat(devpath,&dst) ) continue;
94 if( st.st_dev != dst.st_rdev ) continue;
95 int fd = open(devpath,O_RDONLY);
96 if( fd < 0 ) continue;
97 struct iso_primary_descriptor id;
98 if( lseek(fd,0x8000,SEEK_SET) == 0x8000 )
99 n = read(fd,&id,sizeof(id));
101 if( n != sizeof(id) ) continue;
102 // look for magic number
103 if( strncmp(ISO_STANDARD_ID,id.id,sizeof(id.id)) ) continue;
104 // look for volume_id
105 if( !isalnum(id.volume_id[0]) ) continue;
106 char *bp = &id.volume_id[0], *cp = fname;
107 for( int i=0; i<(int)sizeof(id.volume_id); ++i ) *cp++ = *bp++;
108 while( --cp>=fname && *cp==' ' ) *cp = 0;
109 if( !*fname ) continue;
110 // fname = volume_id _ creation_date
111 ++cp; *cp++ = '_'; bp = &id.creation_date[0];
112 for( int i=0; i<(int)sizeof(id.creation_date)-1; ++i ) {
113 if( !isdigit(*bp) ) break;
117 if( cp-fname > 4 ) result = 0;
124 // Use native sampling rates for files so the same index can be used in
125 // multiple projects.
127 IndexFile::IndexFile(MWindow *mwindow)
129 //printf("IndexFile::IndexFile 1\n");
131 this->mwindow = mwindow;
132 //printf("IndexFile::IndexFile 2\n");
133 redraw_timer = new Timer;
136 IndexFile::IndexFile(MWindow *mwindow,
137 Indexable *indexable)
139 //printf("IndexFile::IndexFile 2\n");
141 this->mwindow = mwindow;
142 this->indexable = indexable;
143 redraw_timer = new Timer;
147 indexable->add_user();
148 source_channels = indexable->get_audio_channels();
149 source_samplerate = indexable->get_sample_rate();
150 source_length = indexable->get_audio_samples();
154 IndexFile::~IndexFile()
156 //printf("IndexFile::~IndexFile 1\n");
158 if(indexable) indexable->remove_user();
162 void IndexFile::reset()
174 IndexState* IndexFile::get_state()
176 IndexState *index_state = 0;
177 if(indexable) index_state = indexable->index_state;
183 int IndexFile::open_index()
185 IndexState *index_state = 0;
188 // use buffer if being built
189 index_state = get_state();
191 if(index_state->index_status == INDEX_BUILDING)
197 if(!(result = open_file()))
199 // opened existing file
207 index_state->index_status = INDEX_READY;
218 void IndexFile::delete_index(Preferences *preferences,
219 Indexable *indexable, const char *suffix)
221 char index_filename[BCTEXTLEN];
222 char source_filename[BCTEXTLEN];
223 const char *path = indexable->path;
225 get_index_filename(source_filename,
226 preferences->index_directory,
227 index_filename, path, suffix);
228 //printf("IndexFile::delete_index %s %s\n", source_filename, index_filename);
229 remove_file(index_filename);
232 int IndexFile::open_file()
236 const char *path = indexable->path;
239 //printf("IndexFile::open_file %f\n", indexable->get_frame_rate());
241 get_index_filename(source_filename,
242 mwindow->preferences->index_directory,
246 if(debug) printf("IndexFile::open_file %d index_filename=%s\n",
249 fd = fopen(index_filename, "rb");
252 // Index file already exists.
253 // Get its last size without changing the real asset status.
254 Indexable *test_indexable = new Indexable(0);
256 test_indexable->copy_indexable(indexable);
257 read_info(test_indexable);
258 IndexState *index_state = test_indexable->index_state;
261 if(fs.get_date(index_filename) < fs.get_date(test_indexable->path))
263 if(debug) printf("IndexFile::open_file %d index_date=%jd source_date=%jd\n",
265 fs.get_date(index_filename),
266 fs.get_date(test_indexable->path));
268 // index older than source
274 if(fs.get_size(test_indexable->path) != index_state->index_bytes)
276 // source file is a different size than index source file
277 if(debug) printf("IndexFile::open_file %d index_size=%jd source_size=%jd\n",
279 index_state->index_bytes,
280 fs.get_size(test_indexable->path));
287 if(debug) printf("IndexFile::open_file %d\n",
289 fseek(fd, 0, SEEK_END);
290 file_length = ftell(fd);
291 fseek(fd, 0, SEEK_SET);
294 test_indexable->Garbage::remove_user();
299 if(debug) printf("IndexFile::open_file %d index_filename=%s doesn't exist\n",
308 int IndexFile::open_source()
310 //printf("IndexFile::open_source %p %s\n", asset, asset->path);
312 if(indexable && indexable->is_asset)
314 if(!source) source = new File;
316 Asset *asset = (Asset*)indexable;
317 if(source->open_file(mwindow->preferences,
320 //printf("IndexFile::open_source() Couldn't open %s.\n", asset->path);
326 asset->index_state->index_bytes = fs.get_size(asset->path);
327 source_length = source->get_audio_length();
332 TransportCommand command;
333 command.command = NORMAL_FWD;
334 command.get_edl()->copy_all((EDL*)indexable);
335 command.change_type = CHANGE_ALL;
336 command.realtime = 0;
337 cache = new CICache(mwindow->preferences);
338 render_engine = new RenderEngine(0,
339 mwindow->preferences, 0, 0);
340 render_engine->set_acache(cache);
341 render_engine->arm_command(&command);
343 indexable->index_state->index_bytes = fs.get_size(indexable->path);
349 void IndexFile::close_source()
354 delete render_engine;
361 int64_t IndexFile::get_required_scale()
366 // get scale of index file
367 // Total peaks which may be stored in buffer
368 int64_t peak_count = mwindow->preferences->index_size /
369 (2 * sizeof(float) * source_channels);
371 source_length / result > peak_count;
375 // Takes too long to draw from source on a CDROM. Make indexes for
381 int IndexFile::get_index_filename(char *source_filename,
382 char *index_directory,
383 char *index_filename,
384 const char *input_filename,
387 const char *input_fn = input_filename;
388 char volume_id[BCTEXTLEN];
389 // Replace mount/directory with volume_id if isofs
390 if( !udf_volume_id(input_filename, volume_id) )
392 char *cp = strrchr((char*)input_filename,'/');
393 if( cp ) input_fn = cp + 1;
394 for( cp=volume_id; *cp; ++cp );
395 *cp++ = '_'; strcpy(cp, input_fn);
396 input_fn = volume_id;
398 // Replace slashes and dots
400 int len = strlen(input_fn);
401 for(i = 0, j = 0; i < len; i++)
403 if(input_fn[i] != '/' &&
405 source_filename[j++] = input_fn[i];
409 source_filename[j++] = '_';
412 source_filename[j] = 0;
414 fs.join_names(index_filename, index_directory, source_filename);
415 strcat(index_filename, suffix ? suffix : ".idx");
419 int IndexFile::interrupt_index()
425 // Read data into buffers
427 int IndexFile::create_index(MainProgressBar *progress)
434 // open the source file
435 if(open_source()) return 1;
439 get_index_filename(source_filename,
440 mwindow->preferences->index_directory,
446 // Some file formats have their own sample index.
447 // Test for index in stream table of contents
448 if(source && !source->get_index(this, progress))
450 IndexState *index_state = get_state();
451 index_state->index_status = INDEX_READY;
455 // Build index from scratch
459 // Indexes are now built for everything since it takes too long to draw
460 // from CDROM source.
462 // get amount to read at a time in floats
463 int64_t buffersize = 65536;
464 char string[BCTEXTLEN];
465 sprintf(string, _("Creating %s."), index_filename);
467 progress->update_title(string);
468 progress->update_length(source_length);
469 redraw_timer->update();
472 // thread out index thread
473 IndexThread *index_thread = new IndexThread(mwindow,
478 index_thread->start_build();
480 // current sample in source file
481 int64_t position = 0;
482 int64_t fragment_size = buffersize;
483 int current_buffer = 0;
486 // pass through file once
487 // printf("IndexFile::create_index %d source_length=%jd source=%p progress=%p\n",
493 while(position < source_length && !result)
496 if(source_length - position < fragment_size && fragment_size == buffersize) fragment_size = source_length - position;
498 index_thread->input_lock[current_buffer]->lock("IndexFile::create_index 1");
499 index_thread->input_len[current_buffer] = fragment_size;
502 int cancelled = progress->update(position);
503 //printf("IndexFile::create_index cancelled=%d\n", cancelled);
506 index_thread->interrupt_flag ||
514 if(source && !result)
518 !result && channel < source_channels;
521 // Read from source file
522 source->set_audio_position(position);
523 source->set_channel(channel);
525 if(source->read_samples(
526 index_thread->buffer_in[current_buffer][channel],
533 if(render_engine && !result)
536 if(render_engine->arender)
538 result = render_engine->arender->process_buffer(
539 index_thread->buffer_in[current_buffer],
545 for(int i = 0; i < source_channels; i++)
547 bzero(index_thread->buffer_in[current_buffer][i]->get_data(),
548 fragment_size * sizeof(double));
555 // Release buffer to thread
558 index_thread->output_lock[current_buffer]->unlock();
560 if(current_buffer >= TOTAL_INDEX_BUFFERS) current_buffer = 0;
561 position += fragment_size;
565 index_thread->input_lock[current_buffer]->unlock();
571 // end thread cleanly
572 index_thread->input_lock[current_buffer]->lock("IndexFile::create_index 2");
573 index_thread->last_buffer[current_buffer] = 1;
574 index_thread->output_lock[current_buffer]->unlock();
575 index_thread->stop_build();
592 mwindow->edl->set_index_file(indexable);
598 int IndexFile::redraw_edits(int force)
600 int64_t difference = redraw_timer->get_scaled_difference(1000);
602 if(difference > 250 || force)
604 redraw_timer->update();
605 mwindow->gui->lock_window("IndexFile::redraw_edits");
606 mwindow->edl->set_index_file(indexable);
607 mwindow->gui->draw_indexes(indexable);
608 mwindow->gui->unlock_window();
616 int IndexFile::draw_index(
618 ResourcePixmap *pixmap,
624 IndexState *index_state = get_state();
625 int pane_number = canvas->pane->number;
626 //index_state->dump();
629 if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
630 if(index_state->index_zoom == 0)
632 printf(_("IndexFile::draw_index: index has 0 zoom\n"));
635 if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
637 // test channel number
638 if(edit->channel > source_channels) return 1;
639 if(debug) printf("IndexFile::draw_index %d source_samplerate=%d "
640 "w=%d samplerate=%jd zoom_sample=%jd\n",
641 __LINE__, source_samplerate, w,
642 mwindow->edl->session->sample_rate,
643 mwindow->edl->local_session->zoom_sample);
645 // calculate a virtual x where the edit_x should be in floating point
646 double virtual_edit_x = 1.0 *
647 edit->track->from_units(edit->startproject) *
648 mwindow->edl->session->sample_rate /
649 mwindow->edl->local_session->zoom_sample -
650 mwindow->edl->local_session->view_start[pane_number];
652 // samples in segment to draw relative to asset
653 double asset_over_session = (double)source_samplerate /
654 mwindow->edl->session->sample_rate;
655 int64_t startsource = (int64_t)(((pixmap->pixmap_x - virtual_edit_x + x) *
656 mwindow->edl->local_session->zoom_sample +
659 // just in case we get a numerical error
660 if (startsource < 0) startsource = 0;
661 int64_t length = (int64_t)(w *
662 mwindow->edl->local_session->zoom_sample *
664 int64_t lengthindex = length / index_state->index_zoom * 2;
665 int64_t startindex = startsource / index_state->index_zoom * 2;
666 // length of index to read in floats
667 // length of index available in floats
668 int64_t endindex = index_state->index_status == INDEX_BUILDING ?
669 index_state->get_channel_used(edit->channel) * 2 :
670 index_state->get_index_size(edit->channel);
671 // Clamp length of index to read by available data
672 if(startindex + lengthindex >= endindex )
673 lengthindex = endindex - startindex;
674 if( lengthindex <= 0 ) return 0;
676 // Actual length read from file in bytes
678 // Start and length of fragment to read from file in bytes.
679 int64_t startfile, lengthfile;
681 int buffer_shared = 0;
682 int center_pixel = mwindow->edl->local_session->zoom_track / 2;
683 if( mwindow->edl->session->show_titles )
684 center_pixel += mwindow->theme->get_image("title_bg_data")->get_h();
685 //int miny = center_pixel - mwindow->edl->local_session->zoom_track / 2;
686 //int maxy = center_pixel + mwindow->edl->local_session->zoom_track / 2;
688 // get zoom_sample relative to index zoomx
689 double index_frames_per_pixel = mwindow->edl->local_session->zoom_sample /
690 index_state->index_zoom *
695 if(index_state->index_status == INDEX_BUILDING)
697 // index is in RAM, being built
698 buffer = index_state->get_channel_buffer(edit->channel);
699 if( !buffer ) return 0;
700 buffer += startindex;
705 // add channel offset
706 startindex += index_state->get_index_offset(edit->channel);
707 // index is stored in a file
708 buffer = new float[lengthindex + 1];
710 startfile = index_state->index_start + startindex * sizeof(float);
711 lengthfile = lengthindex * sizeof(float);
714 if(startfile < file_length)
716 fseek(fd, startfile, SEEK_SET);
718 length_read = lengthfile;
719 if(startfile + length_read > file_length)
720 length_read = file_length - startfile;
722 (void)fread(buffer, length_read + sizeof(float), 1, fd);
725 if(length_read < lengthfile) {
726 int pos = length_read / sizeof(float);
727 int file_length = lengthfile / sizeof(float);
728 while( pos < file_length ) buffer[pos++] = 0;
732 canvas->set_color(mwindow->theme->audio_color);
734 double current_frame = 0;
735 float highsample = buffer[0];
736 float lowsample = buffer[1];
737 int prev_y1 = center_pixel;
738 int prev_y2 = center_pixel;
740 int zoom_y = mwindow->edl->local_session->zoom_y, zoom_y2 = zoom_y / 2;
741 int max_y = center_pixel + zoom_y2 - 1;
744 for(int bufferposition = 0;
745 bufferposition < lengthindex;
748 if(current_frame >= index_frames_per_pixel)
751 int y1 = (int)(center_pixel - highsample * zoom_y2);
752 int y2 = (int)(center_pixel - lowsample * zoom_y2);
753 CLAMP(y1, 0, max_y); int next_y1 = y1;
754 CLAMP(y2, 0, max_y); int next_y2 = y2;
755 //printf("draw_line (%f,%f) = %d,%d, %d,%d\n", lowsample, highsample, x1 + x, y1, x1 + x, y2);
758 // A different algorithm has to be used if it's 1 sample per pixel and the
759 // index is used. Now the min and max values are equal so we join the max samples.
760 if(mwindow->edl->local_session->zoom_sample == 1)
762 canvas->draw_line(x1 + x - 1, prev_y1, x1 + x, y1, pixmap);
766 // Extend line height if it doesn't connect to previous line
769 if(y1 > prev_y2) y1 = prev_y2 + 1;
770 if(y2 < prev_y1) y2 = prev_y1 - 1;
779 canvas->draw_line(x1 + x, y1, x1 + x, y2, pixmap);
781 current_frame -= index_frames_per_pixel;
785 highsample = buffer[bufferposition];
786 lowsample = buffer[bufferposition + 1];
790 highsample = MAX(highsample, buffer[bufferposition]);
791 lowsample = MIN(lowsample, buffer[bufferposition + 1]);
798 y1 = (int)(center_pixel - highsample * zoom_y2);
799 y2 = (int)(center_pixel - lowsample * zoom_y2);
800 canvas->draw_line(x1 + x, y1, x1 + x, y2, pixmap);
807 if(!buffer_shared) delete [] buffer;
809 if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
813 int IndexFile::close_index()
823 int IndexFile::remove_index()
825 IndexState *index_state = get_state();
826 if(index_state->index_status == INDEX_READY ||
827 index_state->index_status == INDEX_NOTTESTED)
830 remove(index_filename);
835 int IndexFile::read_info(Indexable *test_indexable)
839 // Store format in actual asset.
840 // If it's a nested EDL, we never want the format, just the index info.
841 if(!test_indexable) test_indexable = indexable;
842 if(!test_indexable) return 1;
844 IndexState * index_state = test_indexable->index_state;
845 if(index_state->index_status == INDEX_NOTTESTED)
847 // read start of index data
848 int temp = fread((char*)&(index_state->index_start), sizeof(int64_t), 1, fd);
849 //printf("IndexFile::read_info %d %f\n", __LINE__, test_indexable->get_frame_rate());
852 // read test_indexable info from index
855 data = new char[index_state->index_start];
856 temp = fread(data, index_state->index_start - sizeof(int64_t), 1, fd);
859 data[index_state->index_start - sizeof(int64_t)] = 0;
861 xml.read_from_string(data);
866 // Read the file format & index state.
867 if(test_indexable->is_asset)
869 Asset *test_asset = (Asset *)test_indexable;
870 Asset *asset = new Asset;
873 //printf("IndexFile::read_info %d %f\n", __LINE__, asset->get_frame_rate());
875 if( asset->format == FILE_UNKNOWN ||
876 test_asset->format != asset->format ) {
877 if(debug) printf("IndexFile::read_info %d\n", __LINE__);
880 asset->remove_user();
881 if( ret ) return ret;
885 // Read only the index state for a nested EDL
887 if(debug) printf("IndexFile::read_info %d\n", __LINE__);
890 result = xml.read_tag();
893 if(xml.tag.title_is("INDEX"))
895 index_state->read_xml(&xml, source_channels);
896 if(debug) printf("IndexFile::read_info %d\n", __LINE__);
897 if(debug) index_state->dump();