ff tooltip, shcmds doc, bld_prepare fix, edl load_xml fix
[goodguy/history.git] / cinelerra-5.1 / 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 "indexable.h"
37 #include "indexfile.h"
38 #include "indexstate.h"
39 #include "indexthread.h"
40 #include "language.h"
41 #include "localsession.h"
42 #include "mainprogress.h"
43 #include "mwindowgui.h"
44 #include "mwindow.h"
45 #include "preferences.h"
46 #include "removefile.h"
47 #include "renderengine.h"
48 #include "resourcepixmap.h"
49 #include "samples.h"
50 #include "theme.h"
51 #include "timelinepane.h"
52 #include "trackcanvas.h"
53 #include "tracks.h"
54 #include "transportque.h"
55 #include "vframe.h"
56
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <stdint.h>
61 #include <unistd.h>
62 #include <string.h>
63 #include <errno.h>
64 #include <fcntl.h>
65 #include <ctype.h>
66
67 #include <sys/types.h>
68 #include <sys/stat.h>
69 #include <linux/iso_fs.h>
70
71 // check for isofs volume_id for dvd/cdrom
72
73 static int udf_volume_id(const char *path, char *fname)
74 {
75         struct stat st;
76         if( stat(path,&st) ) return 1;
77         // search mounted devices
78         FILE *fp = fopen("/proc/mounts","r");
79         if( !fp ) return 1;
80
81         int result = 1;
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;
92                 struct stat dst;
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));
100                 close(fd);
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;
114                         *cp++ = *bp++;
115                 }
116                 *cp++ = 0;
117                 if( cp-fname > 4 ) result = 0;
118         }
119
120         fclose(fp);
121         return result;
122 }
123
124 // Use native sampling rates for files so the same index can be used in
125 // multiple projects.
126
127 IndexFile::IndexFile(MWindow *mwindow)
128 {
129 //printf("IndexFile::IndexFile 1\n");
130         reset();
131         this->mwindow = mwindow;
132 //printf("IndexFile::IndexFile 2\n");
133         redraw_timer = new Timer;
134 }
135
136 IndexFile::IndexFile(MWindow *mwindow,
137         Indexable *indexable)
138 {
139 //printf("IndexFile::IndexFile 2\n");
140         reset();
141         this->mwindow = mwindow;
142         this->indexable = indexable;
143         redraw_timer = new Timer;
144
145         if(indexable)
146         {
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();
151         }
152 }
153
154 IndexFile::~IndexFile()
155 {
156 //printf("IndexFile::~IndexFile 1\n");
157         delete redraw_timer;
158         if(indexable) indexable->remove_user();
159         close_source();
160 }
161
162 void IndexFile::reset()
163 {
164         fd = 0;
165         source = 0;
166         interrupt_flag = 0;
167         source_length = 0;
168         source_channels = 0;
169         indexable = 0;
170         render_engine = 0;
171         cache = 0;
172 }
173
174 IndexState* IndexFile::get_state()
175 {
176         IndexState *index_state = 0;
177         if(indexable) index_state = indexable->index_state;
178         return index_state;
179 }
180
181
182
183 int IndexFile::open_index()
184 {
185         IndexState *index_state = 0;
186         int result = 0;
187
188 // use buffer if being built
189         index_state = get_state();
190
191         if(index_state->index_status == INDEX_BUILDING)
192         {
193 // use buffer
194                 result = 0;
195         }
196         else
197         if(!(result = open_file()))
198         {
199 // opened existing file
200                 if(read_info())
201                 {
202                         result = 1;
203                         close_index();
204                 }
205                 else
206                 {
207                         index_state->index_status = INDEX_READY;
208                 }
209         }
210         else
211         {
212                 result = 1;
213         }
214
215         return result;
216 }
217
218 void IndexFile::delete_index(Preferences *preferences,
219         Indexable *indexable, const char *suffix)
220 {
221         char index_filename[BCTEXTLEN];
222         char source_filename[BCTEXTLEN];
223         const char *path = indexable->path;
224
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);
230 }
231
232 int IndexFile::open_file()
233 {
234         int result = 0;
235         const int debug = 0;
236         const char *path = indexable->path;
237
238
239 //printf("IndexFile::open_file %f\n", indexable->get_frame_rate());
240
241         get_index_filename(source_filename,
242                 mwindow->preferences->index_directory,
243                 index_filename,
244                 path);
245
246         if(debug) printf("IndexFile::open_file %d index_filename=%s\n",
247                 __LINE__,
248                 index_filename);
249         fd = fopen(index_filename, "rb");
250         if( fd != 0 )
251         {
252 // Index file already exists.
253 // Get its last size without changing the real asset status.
254                 Indexable *test_indexable = new Indexable(0);
255                 if(indexable)
256                         test_indexable->copy_indexable(indexable);
257                 read_info(test_indexable);
258                 IndexState *index_state = test_indexable->index_state;
259
260                 FileSystem fs;
261                 if(fs.get_date(index_filename) < fs.get_date(test_indexable->path))
262                 {
263                         if(debug) printf("IndexFile::open_file %d index_date=%jd source_date=%jd\n",
264                                 __LINE__,
265                                 fs.get_date(index_filename),
266                                 fs.get_date(test_indexable->path));
267
268 // index older than source
269                         result = 2;
270                         fclose(fd);
271                         fd = 0;
272                 }
273                 else
274                 if(fs.get_size(test_indexable->path) != index_state->index_bytes)
275                 {
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",
278                                 __LINE__,
279                                 index_state->index_bytes,
280                                 fs.get_size(test_indexable->path));
281                         result = 2;
282                         fclose(fd);
283                         fd = 0;
284                 }
285                 else
286                 {
287                         if(debug) printf("IndexFile::open_file %d\n",
288                                 __LINE__);
289                         fseek(fd, 0, SEEK_END);
290                         file_length = ftell(fd);
291                         fseek(fd, 0, SEEK_SET);
292                         result = 0;
293                 }
294                 test_indexable->Garbage::remove_user();
295         }
296         else
297         {
298 // doesn't exist
299                 if(debug) printf("IndexFile::open_file %d index_filename=%s doesn't exist\n",
300                         __LINE__,
301                         index_filename);
302                 result = 1;
303         }
304
305         return result;
306 }
307
308 int IndexFile::open_source()
309 {
310 //printf("IndexFile::open_source %p %s\n", asset, asset->path);
311         int result = 0;
312         if(indexable && indexable->is_asset)
313         {
314                 if(!source) source = new File;
315
316                 Asset *asset = (Asset*)indexable;
317                 if(source->open_file(mwindow->preferences,
318                         asset, 1, 0))
319                 {
320                         //printf("IndexFile::open_source() Couldn't open %s.\n", asset->path);
321                         result = 1;
322                 }
323                 else
324                 {
325                         FileSystem fs;
326                         asset->index_state->index_bytes = fs.get_size(asset->path);
327                         source_length = source->get_audio_length();
328                 }
329         }
330         else
331         {
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);
342                 FileSystem fs;
343                 indexable->index_state->index_bytes = fs.get_size(indexable->path);
344         }
345
346         return result;
347 }
348
349 void IndexFile::close_source()
350 {
351         delete source;
352         source = 0;
353
354         delete render_engine;
355         render_engine = 0;
356
357         delete cache;
358         cache = 0;
359 }
360
361 int64_t IndexFile::get_required_scale()
362 {
363         int64_t result = 1;
364
365
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);
370         for(result = 1;
371                 source_length / result > peak_count;
372                 result *= 2)
373                 ;
374
375 // Takes too long to draw from source on a CDROM.  Make indexes for
376 // everything.
377
378         return result;
379 }
380
381 int IndexFile::get_index_filename(char *source_filename,
382         char *index_directory,
383         char *index_filename,
384         const char *input_filename,
385         const char *suffix)
386 {
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) )
391         {
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;
397         }
398 // Replace slashes and dots
399         int i, j;
400         int len = strlen(input_fn);
401         for(i = 0, j = 0; i < len; i++)
402         {
403                 if(input_fn[i] != '/' &&
404                         input_fn[i] != '.')
405                         source_filename[j++] = input_fn[i];
406                 else
407                 {
408                         if(i > 0)
409                                 source_filename[j++] = '_';
410                 }
411         }
412         source_filename[j] = 0;
413         FileSystem fs;
414         fs.join_names(index_filename, index_directory, source_filename);
415         strcat(index_filename, suffix ? suffix : ".idx");
416         return 0;
417 }
418
419 int IndexFile::interrupt_index()
420 {
421         interrupt_flag = 1;
422         return 0;
423 }
424
425 // Read data into buffers
426
427 int IndexFile::create_index(MainProgressBar *progress)
428 {
429         int result = 0;
430 SET_TRACE
431
432         interrupt_flag = 0;
433
434 // open the source file
435         if(open_source()) return 1;
436
437 SET_TRACE
438
439         get_index_filename(source_filename,
440                 mwindow->preferences->index_directory,
441                 index_filename,
442                 indexable->path);
443
444 SET_TRACE
445
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))
449         {
450                 IndexState *index_state = get_state();
451                 index_state->index_status = INDEX_READY;
452                 redraw_edits(1);
453         }
454         else
455 // Build index from scratch
456         {
457 SET_TRACE
458
459 // Indexes are now built for everything since it takes too long to draw
460 // from CDROM source.
461
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);
466
467                 progress->update_title(string);
468                 progress->update_length(source_length);
469                 redraw_timer->update();
470 SET_TRACE
471
472 // thread out index thread
473                 IndexThread *index_thread = new IndexThread(mwindow,
474                         this,
475                         index_filename,
476                         buffersize,
477                         source_length);
478                 index_thread->start_build();
479
480 // current sample in source file
481                 int64_t position = 0;
482                 int64_t fragment_size = buffersize;
483                 int current_buffer = 0;
484
485
486 // pass through file once
487 // printf("IndexFile::create_index %d source_length=%jd source=%p progress=%p\n",
488 // __LINE__,
489 // source_length,
490 // source,
491 // progress);
492 SET_TRACE
493                 while(position < source_length && !result)
494                 {
495 SET_TRACE
496                         if(source_length - position < fragment_size && fragment_size == buffersize) fragment_size = source_length - position;
497
498                         index_thread->input_lock[current_buffer]->lock("IndexFile::create_index 1");
499                         index_thread->input_len[current_buffer] = fragment_size;
500
501 SET_TRACE
502                         int cancelled = progress->update(position);
503 //printf("IndexFile::create_index cancelled=%d\n", cancelled);
504 SET_TRACE
505                         if(cancelled ||
506                                 index_thread->interrupt_flag ||
507                                 interrupt_flag)
508                         {
509                                 result = 3;
510                         }
511
512
513 SET_TRACE
514                         if(source && !result)
515                         {
516 SET_TRACE
517                                 for(int channel = 0;
518                                         !result && channel < source_channels;
519                                         channel++)
520                                 {
521 // Read from source file
522                                         source->set_audio_position(position);
523                                         source->set_channel(channel);
524
525                                         if(source->read_samples(
526                                                 index_thread->buffer_in[current_buffer][channel],
527                                                 fragment_size))
528                                                 result = 1;
529                                 }
530 SET_TRACE
531                         }
532                         else
533                         if(render_engine && !result)
534                         {
535 SET_TRACE
536                                 if(render_engine->arender)
537                                 {
538                                         result = render_engine->arender->process_buffer(
539                                                 index_thread->buffer_in[current_buffer],
540                                                 fragment_size,
541                                                 position);
542                                 }
543                                 else
544                                 {
545                                         for(int i = 0; i < source_channels; i++)
546                                         {
547                                                 bzero(index_thread->buffer_in[current_buffer][i]->get_data(),
548                                                         fragment_size * sizeof(double));
549                                         }
550                                 }
551 SET_TRACE
552                         }
553 SET_TRACE
554
555 // Release buffer to thread
556                         if(!result)
557                         {
558                                 index_thread->output_lock[current_buffer]->unlock();
559                                 current_buffer++;
560                                 if(current_buffer >= TOTAL_INDEX_BUFFERS) current_buffer = 0;
561                                 position += fragment_size;
562                         }
563                         else
564                         {
565                                 index_thread->input_lock[current_buffer]->unlock();
566                         }
567 SET_TRACE
568                 }
569
570
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();
576
577
578                 delete index_thread;
579
580         }
581
582
583
584         close_source();
585
586
587
588         open_index();
589
590         close_index();
591
592         mwindow->edl->set_index_file(indexable);
593         return 0;
594 }
595
596
597
598 int IndexFile::redraw_edits(int force)
599 {
600         int64_t difference = redraw_timer->get_scaled_difference(1000);
601
602         if(difference > 250 || force)
603         {
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();
609         }
610         return 0;
611 }
612
613
614
615
616 int IndexFile::draw_index(
617         TrackCanvas *canvas,
618         ResourcePixmap *pixmap,
619         Edit *edit,
620         int x,
621         int w)
622 {
623         const int debug = 0;
624         IndexState *index_state = get_state();
625         int pane_number = canvas->pane->number;
626 //index_state->dump();
627
628 SET_TRACE
629         if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
630         if(index_state->index_zoom == 0)
631         {
632                 printf(_("IndexFile::draw_index: index has 0 zoom\n"));
633                 return 0;
634         }
635         if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
636
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);
644
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];
651
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 +
657                 edit->startsource) *
658                 asset_over_session);
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 *
663                 asset_over_session);
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;
675
676 // Actual length read from file in bytes
677         int64_t length_read;
678 // Start and length of fragment to read from file in bytes.
679         int64_t startfile, lengthfile;
680         float *buffer = 0;
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;
687         int x1 = 0, y1, y2;
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 *
691                 asset_over_session;
692
693
694
695         if(index_state->index_status == INDEX_BUILDING)
696         {
697 // index is in RAM, being built
698                 buffer = index_state->get_channel_buffer(edit->channel);
699                 if( !buffer ) return 0;
700                 buffer += startindex;
701                 buffer_shared = 1;
702         }
703         else
704         {
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];
709                 buffer_shared = 0;
710                 startfile = index_state->index_start + startindex * sizeof(float);
711                 lengthfile = lengthindex * sizeof(float);
712                 length_read = 0;
713
714                 if(startfile < file_length)
715                 {
716                         fseek(fd, startfile, SEEK_SET);
717
718                         length_read = lengthfile;
719                         if(startfile + length_read > file_length)
720                                 length_read = file_length - startfile;
721
722                         (void)fread(buffer, length_read + sizeof(float), 1, fd);
723                 }
724
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;
729                 }
730         }
731
732         canvas->set_color(mwindow->theme->audio_color);
733
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;
739         int first_frame = 1;
740         int zoom_y = mwindow->edl->local_session->zoom_y, zoom_y2 = zoom_y / 2;
741         int max_y = center_pixel + zoom_y2 - 1;
742 SET_TRACE
743
744         for(int bufferposition = 0;
745                 bufferposition < lengthindex;
746                 bufferposition += 2)
747         {
748                 if(current_frame >= index_frames_per_pixel)
749                 {
750
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);
756
757 //SET_TRACE
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)
761                         {
762                                 canvas->draw_line(x1 + x - 1, prev_y1, x1 + x, y1, pixmap);
763                         }
764                         else
765                         {
766 // Extend line height if it doesn't connect to previous line
767                                 if(!first_frame)
768                                 {
769                                         if(y1 > prev_y2) y1 = prev_y2 + 1;
770                                         if(y2 < prev_y1) y2 = prev_y1 - 1;
771                                 }
772                                 else
773                                 {
774                                         first_frame = 0;
775                                 }
776
777
778
779                                 canvas->draw_line(x1 + x, y1, x1 + x, y2, pixmap);
780                         }
781                         current_frame -= index_frames_per_pixel;
782                         x1++;
783                         prev_y1 = next_y1;
784                         prev_y2 = next_y2;
785                         highsample = buffer[bufferposition];
786                         lowsample = buffer[bufferposition + 1];
787                 }
788
789                 current_frame++;
790                 highsample = MAX(highsample, buffer[bufferposition]);
791                 lowsample = MIN(lowsample, buffer[bufferposition + 1]);
792         }
793 SET_TRACE
794
795 // Get last column
796         if(current_frame)
797         {
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);
801         }
802
803 SET_TRACE
804
805
806
807         if(!buffer_shared) delete [] buffer;
808 SET_TRACE
809         if(debug) printf("IndexFile::draw_index %d\n", __LINE__);
810         return 0;
811 }
812
813 int IndexFile::close_index()
814 {
815         if(fd)
816         {
817                 fclose(fd);
818                 fd = 0;
819         }
820         return 0;
821 }
822
823 int IndexFile::remove_index()
824 {
825         IndexState *index_state = get_state();
826         if(index_state->index_status == INDEX_READY ||
827                 index_state->index_status == INDEX_NOTTESTED)
828         {
829                 close_index();
830                 remove(index_filename);
831         }
832         return 0;
833 }
834
835 int IndexFile::read_info(Indexable *test_indexable)
836 {
837         const int debug = 0;
838
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;
843
844         IndexState * index_state = test_indexable->index_state;
845         if(index_state->index_status == INDEX_NOTTESTED)
846         {
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());
850
851                 if(!temp) return 1;
852 // read test_indexable info from index
853                 char *data;
854
855                 data = new char[index_state->index_start];
856                 temp = fread(data, index_state->index_start - sizeof(int64_t), 1, fd);
857                 if(!temp) return 1;
858
859                 data[index_state->index_start - sizeof(int64_t)] = 0;
860                 FileXML xml;
861                 xml.read_from_string(data);
862                 delete [] data;
863
864
865
866 // Read the file format & index state.
867                 if(test_indexable->is_asset)
868                 {
869                         Asset *test_asset = (Asset *)test_indexable;
870                         Asset *asset = new Asset;
871                         asset->read(&xml);
872                         int ret = 0;
873 //printf("IndexFile::read_info %d %f\n", __LINE__, asset->get_frame_rate());
874
875                         if( asset->format == FILE_UNKNOWN ||
876                             test_asset->format != asset->format ) {
877 if(debug) printf("IndexFile::read_info %d\n", __LINE__);
878                                 ret = 1;
879                         }
880                         asset->remove_user();
881                         if( ret ) return ret;
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