prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / cinelerra / indexfile.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5  * 
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.
10  * 
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.
15  * 
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
19  * 
20  */
21
22 #include "arender.h"
23 #include "asset.h"
24 #include "bcsignals.h"
25 #include "bctimer.h"
26 #include "cache.h"
27 #include "clip.h"
28 #include "condition.h"
29 #include "edit.h"
30 #include "edl.h"
31 #include "edlsession.h"
32 #include "errorbox.h"
33 #include "file.h"
34 #include "filesystem.h"
35 #include "filexml.h"
36 #include "format.inc"
37 #include "indexable.h"
38 #include "indexfile.h"
39 #include "indexstate.h"
40 #include "indexthread.h"
41 #include "language.h"
42 #include "localsession.h"
43 #include "mainprogress.h"
44 #include "mwindowgui.h"
45 #include "mwindow.h"
46 #include "preferences.h"
47 #include "removefile.h"
48 #include "renderengine.h"
49 #include "resourcepixmap.h"
50 #include "samples.h"
51 #include "theme.h"
52 #include "timelinepane.h"
53 #include "trackcanvas.h"
54 #include "tracks.h"
55 #include "transportque.h"
56 #include "vframe.h"
57
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <stdint.h>
62 #include <unistd.h>
63 #include <string.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <ctype.h>
67
68 #include <sys/types.h>
69 #include <sys/stat.h>
70 #include <linux/iso_fs.h>
71
72 // check for isofs volume_id for dvd/cdrom
73
74 static int udf_volume_id(const char *path, char *fname)
75 {
76         struct stat st;
77         if( stat(path,&st) ) return 1;
78         // search mounted devices
79         FILE *fp = fopen("/proc/mounts","r");
80         if( !fp ) return 1;
81
82         int result = 1;
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;
93                 struct stat dst;
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));
101                 close(fd);
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;
115                         *cp++ = *bp++;
116                 }
117                 *cp++ = 0;
118                 if( cp-fname > 4 ) result = 0;
119         }
120
121         fclose(fp);
122         return result;
123 }
124
125 // Use native sampling rates for files so the same index can be used in
126 // multiple projects.
127
128 IndexFile::IndexFile(MWindow *mwindow)
129 {
130 //printf("IndexFile::IndexFile 1\n");
131         reset();
132         this->mwindow = mwindow;
133 //printf("IndexFile::IndexFile 2\n");
134         redraw_timer = new Timer;
135 }
136
137 IndexFile::IndexFile(MWindow *mwindow, 
138         Indexable *indexable)
139 {
140 //printf("IndexFile::IndexFile 2\n");
141         reset();
142         this->mwindow = mwindow;
143         this->indexable = indexable;
144         redraw_timer = new Timer;
145
146         if(indexable)
147         {
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();
152         }
153 }
154
155 IndexFile::~IndexFile()
156 {
157 //printf("IndexFile::~IndexFile 1\n");
158         delete redraw_timer;
159         if(indexable) indexable->remove_user();
160         close_source();
161 }
162
163 void IndexFile::reset()
164 {
165         fd = 0;
166         source = 0;
167         interrupt_flag = 0;
168         source_length = 0;
169         source_channels = 0;
170         indexable = 0;
171         render_engine = 0;
172         cache = 0;
173 }
174
175 IndexState* IndexFile::get_state()
176 {
177         IndexState *index_state = 0;
178         if(indexable) index_state = indexable->index_state;
179         return index_state;
180 }
181
182
183
184 int IndexFile::open_index()
185 {
186         IndexState *index_state = 0;
187         int result = 0;
188
189 // use buffer if being built
190         index_state = get_state();
191
192         if(index_state->index_status == INDEX_BUILDING)
193         {
194 // use buffer
195                 result = 0;
196         }
197         else
198         if(!(result = open_file()))
199         {
200 // opened existing file
201                 if(read_info())
202                 {
203                         result = 1;
204                         close_index();
205                 }
206                 else
207                 {
208                         index_state->index_status = INDEX_READY;
209                 }
210         }
211         else
212         {
213                 result = 1;
214         }
215
216         return result;
217 }
218
219 void IndexFile::delete_index(Preferences *preferences, 
220         Indexable *indexable, const char *suffix)
221 {
222         char index_filename[BCTEXTLEN];
223         char source_filename[BCTEXTLEN];
224         const char *path = indexable->path;
225
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);
231 }
232
233 int IndexFile::open_file()
234 {
235         int result = 0;
236         const int debug = 0;
237         const char *path = indexable->path;
238
239
240 //printf("IndexFile::open_file %f\n", indexable->get_frame_rate());
241
242         get_index_filename(source_filename, 
243                 mwindow->preferences->index_directory,
244                 index_filename, 
245                 path);
246
247         if(debug) printf("IndexFile::open_file %d index_filename=%s\n", 
248                 __LINE__,
249                 index_filename);
250         fd = fopen(index_filename, "rb");
251         if( fd != 0 )
252         {
253 // Index file already exists.
254 // Get its last size without changing the real asset status.
255                 Indexable *test_indexable = new Indexable(0);
256                 if(indexable)
257                         test_indexable->copy_indexable(indexable);
258                 read_info(test_indexable);
259                 IndexState *index_state = test_indexable->index_state;
260
261                 FileSystem fs;
262                 if(fs.get_date(index_filename) < fs.get_date(test_indexable->path))
263                 {
264                         if(debug) printf("IndexFile::open_file %d index_date=" _LD " source_date=" _LD "\n",
265                                 __LINE__,
266                                 fs.get_date(index_filename),
267                                 fs.get_date(test_indexable->path));
268
269 // index older than source
270                         result = 2;
271                         fclose(fd);
272                         fd = 0;
273                 }
274                 else
275                 if(fs.get_size(test_indexable->path) != index_state->index_bytes)
276                 {
277 // source file is a different size than index source file
278                         if(debug) printf("IndexFile::open_file %d index_size=" _LD " source_size=" _LD "\n",
279                                 __LINE__,
280                                 index_state->index_bytes,
281                                 fs.get_size(test_indexable->path));
282                         result = 2;
283                         fclose(fd);     
284                         fd = 0;
285                 }
286                 else
287                 {
288                         if(debug) printf("IndexFile::open_file %d\n",
289                                 __LINE__);
290                         fseek(fd, 0, SEEK_END);
291                         file_length = ftell(fd);
292                         fseek(fd, 0, SEEK_SET);
293                         result = 0;
294                 }
295                 test_indexable->Garbage::remove_user();
296         }
297         else
298         {
299 // doesn't exist
300                 if(debug) printf("IndexFile::open_file %d index_filename=%s doesn't exist\n", 
301                         __LINE__,
302                         index_filename);
303                 result = 1;
304         }
305
306         return result;
307 }
308
309 int IndexFile::open_source()
310 {
311 //printf("IndexFile::open_source %p %s\n", asset, asset->path);
312         int result = 0;
313         if(indexable && indexable->is_asset)
314         {
315                 if(!source) source = new File;
316
317                 Asset *asset = (Asset*)indexable;
318                 if(source->open_file(mwindow->preferences, 
319                         asset, 1, 0))
320                 {
321                         //printf("IndexFile::open_source() Couldn't open %s.\n", asset->path);
322                         result = 1;
323                 }
324                 else
325                 {
326                         FileSystem fs;
327                         asset->index_state->index_bytes = fs.get_size(asset->path);
328                         source_length = source->get_audio_length();
329                 }
330         }
331         else
332         {
333                 TransportCommand command;
334                 command.command = NORMAL_FWD;
335                 command.get_edl()->copy_all((EDL*)indexable);
336                 command.change_type = CHANGE_ALL;
337                 command.realtime = 0;
338                 cache = new CICache(mwindow->preferences);
339                 render_engine = new RenderEngine(0,
340                         mwindow->preferences, 0, 0, 0);
341                 render_engine->set_acache(cache);
342                 render_engine->arm_command(&command);
343                 FileSystem fs;
344                 indexable->index_state->index_bytes = fs.get_size(indexable->path);
345         }
346
347         return result;
348 }
349
350 void IndexFile::close_source()
351 {
352         delete source;
353         source = 0;
354
355         delete render_engine;
356         render_engine = 0;
357
358         delete cache;
359         cache = 0;
360 }
361
362 int64_t IndexFile::get_required_scale()
363 {
364         int64_t result = 1;
365         
366
367 // get scale of index file
368 // Total peaks which may be stored in buffer
369         int64_t peak_count = mwindow->preferences->index_size / 
370                 (2 * sizeof(float) * source_channels);
371         for(result = 1; 
372                 source_length / result > peak_count; 
373                 result *= 2)
374                 ;
375
376 // Takes too long to draw from source on a CDROM.  Make indexes for
377 // everything.
378
379         return result;
380 }
381
382 int IndexFile::get_index_filename(char *source_filename, 
383         char *index_directory, 
384         char *index_filename, 
385         const char *input_filename,
386         const char *suffix)
387 {
388         const char *input_fn = input_filename;
389         char volume_id[BCTEXTLEN];
390 // Replace mount/directory with volume_id if isofs
391         if( !udf_volume_id(input_filename, volume_id) )
392         {
393                 char *cp = strrchr((char*)input_filename,'/');
394                 if( cp ) input_fn = cp + 1;
395                 for( cp=volume_id; *cp; ++cp );
396                 *cp++ = '_';  strcpy(cp, input_fn);
397                 input_fn = volume_id;
398         }
399 // Replace slashes and dots
400         int i, j;
401         int len = strlen(input_fn);
402         for(i = 0, j = 0; i < len; i++)
403         {
404                 if(input_fn[i] != '/' &&
405                         input_fn[i] != '.')
406                         source_filename[j++] = input_fn[i];
407                 else
408                 {
409                         if(i > 0)
410                                 source_filename[j++] = '_';
411                 }
412         }
413         source_filename[j] = 0;
414         FileSystem fs;
415         fs.join_names(index_filename, index_directory, source_filename);
416         strcat(index_filename, suffix ? suffix : ".idx");
417         return 0;
418 }
419
420 int IndexFile::interrupt_index()
421 {
422         interrupt_flag = 1;
423         return 0;
424 }
425
426 // Read data into buffers
427
428 int IndexFile::create_index(MainProgressBar *progress)
429 {
430         int result = 0;
431 SET_TRACE
432
433         interrupt_flag = 0;
434
435 // open the source file
436         if(open_source()) return 1;
437
438 SET_TRACE
439
440         get_index_filename(source_filename, 
441                 mwindow->preferences->index_directory, 
442                 index_filename, 
443                 indexable->path);
444
445 SET_TRACE
446
447 // Some file formats have their own sample index.
448 // Test for index in stream table of contents
449         if(source && !source->get_index(index_filename))
450         {
451                 IndexState *index_state = get_state();
452                 index_state->index_status = INDEX_READY;
453                 redraw_edits(1);
454         }
455         else
456 // Build index from scratch
457         {
458 SET_TRACE
459
460 // Indexes are now built for everything since it takes too long to draw
461 // from CDROM source.
462
463 // get amount to read at a time in floats
464                 int64_t buffersize = 65536;
465                 char string[BCTEXTLEN];
466                 sprintf(string, _("Creating %s."), index_filename);
467
468                 progress->update_title(string);
469                 progress->update_length(source_length);
470                 redraw_timer->update();
471 SET_TRACE
472
473 // thread out index thread
474                 IndexThread *index_thread = new IndexThread(mwindow, 
475                         this, 
476                         index_filename, 
477                         buffersize, 
478                         source_length);
479                 index_thread->start_build();
480
481 // current sample in source file
482                 int64_t position = 0;
483                 int64_t fragment_size = buffersize;
484                 int current_buffer = 0;
485
486
487 // pass through file once
488 // printf("IndexFile::create_index %d source_length=" _LD " source=%p progress=%p\n", 
489 // __LINE__, 
490 // source_length,
491 // source,
492 // progress);
493 SET_TRACE
494                 while(position < source_length && !result)
495                 {
496 SET_TRACE
497                         if(source_length - position < fragment_size && fragment_size == buffersize) fragment_size = source_length - position;
498
499                         index_thread->input_lock[current_buffer]->lock("IndexFile::create_index 1");
500                         index_thread->input_len[current_buffer] = fragment_size;
501
502 SET_TRACE
503                         int cancelled = progress->update(position);
504 //printf("IndexFile::create_index cancelled=%d\n", cancelled);
505 SET_TRACE
506                         if(cancelled || 
507                                 index_thread->interrupt_flag || 
508                                 interrupt_flag)
509                         {
510                                 result = 3;
511                         }
512
513
514 SET_TRACE
515                         if(source && !result)
516                         {
517 SET_TRACE
518                                 for(int channel = 0; 
519                                         !result && channel < source_channels; 
520                                         channel++)
521                                 {
522 // Read from source file
523                                         source->set_audio_position(position);
524                                         source->set_channel(channel);
525
526                                         if(source->read_samples(
527                                                 index_thread->buffer_in[current_buffer][channel],
528                                                 fragment_size)) 
529                                                 result = 1;
530                                 }
531 SET_TRACE
532                         }
533                         else
534                         if(render_engine && !result)
535                         {
536 SET_TRACE
537                                 if(render_engine->arender)
538                                 {
539                                         result = render_engine->arender->process_buffer(
540                                                 index_thread->buffer_in[current_buffer], 
541                                                 fragment_size,
542                                                 position);
543                                 }
544                                 else
545                                 {
546                                         for(int i = 0; i < source_channels; i++)
547                                         {
548                                                 bzero(index_thread->buffer_in[current_buffer][i]->get_data(),
549                                                         fragment_size * sizeof(double));
550                                         }
551                                 }
552 SET_TRACE
553                         }
554 SET_TRACE
555
556 // Release buffer to thread
557                         if(!result)
558                         {
559                                 index_thread->output_lock[current_buffer]->unlock();
560                                 current_buffer++;
561                                 if(current_buffer >= TOTAL_INDEX_BUFFERS) current_buffer = 0;
562                                 position += fragment_size;
563                         }
564                         else
565                         {
566                                 index_thread->input_lock[current_buffer]->unlock();
567                         }
568 SET_TRACE
569                 }
570
571
572 // end thread cleanly
573                 index_thread->input_lock[current_buffer]->lock("IndexFile::create_index 2");
574                 index_thread->last_buffer[current_buffer] = 1;
575                 index_thread->output_lock[current_buffer]->unlock();
576                 index_thread->stop_build();
577
578
579                 delete index_thread;
580
581         }
582
583
584
585         close_source();
586
587
588
589         open_index();
590
591         close_index();
592
593         mwindow->edl->set_index_file(indexable);
594         return 0;
595 }
596
597
598
599 int IndexFile::redraw_edits(int force)
600 {
601         int64_t difference = redraw_timer->get_scaled_difference(1000);
602
603         if(difference > 250 || force)
604         {
605                 redraw_timer->update();
606                 mwindow->gui->lock_window("IndexFile::redraw_edits");
607                 mwindow->edl->set_index_file(indexable);
608                 mwindow->gui->draw_indexes(indexable);
609                 mwindow->gui->unlock_window();
610         }
611         return 0;
612 }
613
614
615
616
617 int IndexFile::draw_index(
618         TrackCanvas *canvas,
619         ResourcePixmap *pixmap, 
620         Edit *edit, 
621         int x, 
622         int w)
623 {
624         const int debug = 0;
625         IndexState *index_state = get_state();
626         int pane_number = canvas->pane->number;
627 //index_state->dump();
628
629 SET_TRACE
630         if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
631         if(index_state->index_zoom == 0)
632         {
633                 printf(_("IndexFile::draw_index: index has 0 zoom\n"));
634                 return 0;
635         }
636         if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
637
638 // test channel number
639         if(edit->channel > source_channels) return 1;
640         if(debug) printf("IndexFile::draw_index %d source_samplerate=%d "
641                         "w=%d samplerate=" _LD " zoom_sample=" _LD "\n", 
642                 __LINE__, source_samplerate, w,
643                 mwindow->edl->session->sample_rate,
644                 mwindow->edl->local_session->zoom_sample);
645
646 // calculate a virtual x where the edit_x should be in floating point
647         double virtual_edit_x = 1.0 * 
648                 edit->track->from_units(edit->startproject) * 
649                 mwindow->edl->session->sample_rate /
650                 mwindow->edl->local_session->zoom_sample - 
651                 mwindow->edl->local_session->view_start[pane_number];
652
653 // samples in segment to draw relative to asset
654         double asset_over_session = (double)source_samplerate / 
655                 mwindow->edl->session->sample_rate;
656         int64_t startsource = (int64_t)(((pixmap->pixmap_x - virtual_edit_x + x) * 
657                 mwindow->edl->local_session->zoom_sample + 
658                 edit->startsource) * 
659                 asset_over_session);
660 // just in case we get a numerical error 
661         if (startsource < 0) startsource = 0;
662         int64_t length = (int64_t)(w * 
663                 mwindow->edl->local_session->zoom_sample * 
664                 asset_over_session);
665         int64_t lengthindex = length / index_state->index_zoom * 2;
666         int64_t startindex = startsource / index_state->index_zoom * 2;  
667 // length of index to read in floats
668 // length of index available in floats
669         int64_t endindex = index_state->index_status == INDEX_BUILDING ?
670                 index_state->get_channel_used(edit->channel) * 2 :
671                 index_state->get_index_size(edit->channel);
672 // Clamp length of index to read by available data
673         if(startindex + lengthindex >= endindex )
674                 lengthindex = endindex - startindex;
675         if( lengthindex <= 0 ) return 0;
676
677 // Actual length read from file in bytes
678         int64_t length_read;   
679 // Start and length of fragment to read from file in bytes.
680         int64_t startfile, lengthfile;
681         float *buffer = 0;
682         int buffer_shared = 0;
683         int center_pixel = mwindow->edl->local_session->zoom_track / 2;
684         if( mwindow->edl->session->show_titles )
685                 center_pixel += mwindow->theme->get_image("title_bg_data")->get_h();
686         //int miny = center_pixel - mwindow->edl->local_session->zoom_track / 2;
687         //int maxy = center_pixel + mwindow->edl->local_session->zoom_track / 2;
688         int x1 = 0, y1, y2;
689 // get zoom_sample relative to index zoomx
690         double index_frames_per_pixel = mwindow->edl->local_session->zoom_sample / 
691                 index_state->index_zoom * 
692                 asset_over_session;
693
694
695
696         if(index_state->index_status == INDEX_BUILDING)
697         {
698 // index is in RAM, being built
699                 buffer = index_state->get_channel_buffer(edit->channel);
700                 if( !buffer ) return 0;
701                 buffer += startindex;
702                 buffer_shared = 1;
703         }
704         else
705         {
706 // add channel offset
707                 startindex += index_state->get_index_offset(edit->channel);
708 // index is stored in a file
709                 buffer = new float[lengthindex + 1];
710                 buffer_shared = 0;
711                 startfile = index_state->index_start + startindex * sizeof(float);
712                 lengthfile = lengthindex * sizeof(float);
713                 length_read = 0;
714
715                 if(startfile < file_length)
716                 {
717                         fseek(fd, startfile, SEEK_SET);
718
719                         length_read = lengthfile;
720                         if(startfile + length_read > file_length)
721                                 length_read = file_length - startfile;
722
723                         (void)fread(buffer, length_read + sizeof(float), 1, fd);
724                 }
725
726                 if(length_read < lengthfile) {
727                         int pos = length_read / sizeof(float);
728                         int file_length = lengthfile / sizeof(float);
729                         while( pos < file_length ) buffer[pos++] = 0;
730                 }
731         }
732
733         canvas->set_color(mwindow->theme->audio_color);
734
735         double current_frame = 0;
736         float highsample = buffer[0];
737         float lowsample = buffer[1];
738         int prev_y1 = center_pixel;
739         int prev_y2 = center_pixel;
740         int first_frame = 1;
741         int zoom_y = mwindow->edl->local_session->zoom_y, zoom_y2 = zoom_y / 2;
742         int max_y = canvas->get_h();
743         int zmax_y = center_pixel + zoom_y2 - 1;
744         if( zmax_y < max_y ) max_y = zmax_y;
745 SET_TRACE
746
747         for(int bufferposition = 0; 
748                 bufferposition < lengthindex; 
749                 bufferposition += 2)
750         {
751                 if(current_frame >= index_frames_per_pixel)
752                 {
753  
754                         int y1 = (int)(center_pixel - highsample * zoom_y2);
755                         int y2 = (int)(center_pixel - lowsample * zoom_y2);
756                         CLAMP(y1, 0, max_y);  int next_y1 = y1;
757                         CLAMP(y2, 0, max_y);  int next_y2 = y2;
758 //printf("draw_line (%f,%f) = %d,%d,  %d,%d\n", lowsample, highsample, x1 + x, y1, x1 + x, y2);
759
760 //SET_TRACE
761 // A different algorithm has to be used if it's 1 sample per pixel and the
762 // index is used.  Now the min and max values are equal so we join the max samples.
763                         if(mwindow->edl->local_session->zoom_sample == 1)
764                         {
765                                 canvas->draw_line(x1 + x - 1, prev_y1, x1 + x, y1, pixmap);
766                         }
767                         else
768                         {
769 // Extend line height if it doesn't connect to previous line
770                                 if(!first_frame)
771                                 {
772                                         if(y1 > prev_y2) y1 = prev_y2 + 1;
773                                         if(y2 < prev_y1) y2 = prev_y1 - 1;
774                                 }
775                                 else
776                                 {
777                                         first_frame = 0;
778                                 }
779
780
781
782                                 canvas->draw_line(x1 + x, y1, x1 + x, y2, pixmap);
783                         }
784                         current_frame -= index_frames_per_pixel;
785                         x1++;
786                         prev_y1 = next_y1;
787                         prev_y2 = next_y2;
788                         highsample = buffer[bufferposition];
789                         lowsample = buffer[bufferposition + 1];
790                 }
791
792                 current_frame++;
793                 highsample = MAX(highsample, buffer[bufferposition]);
794                 lowsample = MIN(lowsample, buffer[bufferposition + 1]);
795         }
796 SET_TRACE
797
798 // Get last column
799         if(current_frame)
800         {
801                 y1 = (int)(center_pixel - highsample * zoom_y2);
802                 y2 = (int)(center_pixel - lowsample * zoom_y2);
803                 canvas->draw_line(x1 + x, y1, x1 + x, y2, pixmap);
804         }
805
806 SET_TRACE
807
808
809
810         if(!buffer_shared) delete [] buffer;
811 SET_TRACE
812         if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
813         return 0;
814 }
815
816 int IndexFile::close_index()
817 {
818         if(fd)
819         {
820                 fclose(fd);
821                 fd = 0;
822         }
823         return 0;
824 }
825
826 int IndexFile::remove_index()
827 {
828         IndexState *index_state = get_state();
829         if(index_state->index_status == INDEX_READY || 
830                 index_state->index_status == INDEX_NOTTESTED)
831         {
832                 close_index();
833                 remove(index_filename);
834         }
835         return 0;
836 }
837
838 int IndexFile::read_info(Indexable *test_indexable)
839 {
840         const int debug = 0;
841
842 // Store format in actual asset.
843 // If it's a nested EDL, we never want the format, just the index info.
844         if(!test_indexable) test_indexable = indexable;
845         if(!test_indexable) return 1;
846         
847         IndexState * index_state = test_indexable->index_state;
848         if(index_state->index_status == INDEX_NOTTESTED)
849         {
850 // read start of index data
851                 int temp = fread((char*)&(index_state->index_start), sizeof(int64_t), 1, fd);
852 //printf("IndexFile::read_info %d %f\n", __LINE__, test_indexable->get_frame_rate());
853
854                 if(!temp) return 1;
855 // read test_indexable info from index
856                 char *data;
857                 
858                 data = new char[index_state->index_start];
859                 temp = fread(data, index_state->index_start - sizeof(int64_t), 1, fd);
860                 if(!temp) return 1;
861
862                 data[index_state->index_start - sizeof(int64_t)] = 0;
863                 FileXML xml;
864                 xml.read_from_string(data);
865                 delete [] data;
866
867
868
869 // Read the file format & index state.
870                 if(test_indexable->is_asset)
871                 {
872                         Asset *asset = (Asset*)test_indexable;
873                         asset->read(&xml);
874
875 //printf("IndexFile::read_info %d %f\n", __LINE__, asset->get_frame_rate());
876
877                         if(asset->format == FILE_UNKNOWN)
878                         {
879 if(debug) printf("IndexFile::read_info %d\n", __LINE__);
880                                 return 1;
881                         }
882                 }
883                 else
884                 {
885 // Read only the index state for a nested EDL
886                         int result = 0;
887 if(debug) printf("IndexFile::read_info %d\n", __LINE__);
888                         while(!result)
889                         {
890                                 result = xml.read_tag();
891                                 if(!result)
892                                 {
893                                         if(xml.tag.title_is("INDEX"))
894                                         {
895                                                 index_state->read_xml(&xml, source_channels);
896 if(debug) printf("IndexFile::read_info %d\n", __LINE__);
897 if(debug) index_state->dump();
898                                                 result = 1;
899                                         }
900                                 }
901                         }
902                 }
903         }
904
905         return 0;
906 }
907
908
909
910
911
912
913
914