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