drag edithandle rework, lib x264/x265 update, dvb fixes, batchrender boot_defaults...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / indexthread.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 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 "asset.h"
23 #include "bcsignals.h"
24 #include "clip.h"
25 #include "condition.h"
26 #include "edl.h"
27 #include "edlsession.h"
28 #include "filexml.h"
29 #include "indexfile.h"
30 #include "indexstate.h"
31 #include "indexthread.h"
32 #include "language.h"
33 #include "maxchannels.h"
34 #include "mwindow.h"
35 #include "mwindowgui.h"
36 #include "preferences.h"
37 #include "mainsession.h"
38 #include "samples.h"
39 #include <unistd.h>
40 #include "trackcanvas.h"
41 #include "tracks.h"
42
43 // Read data from buffers and calculate peaks
44
45 IndexThread::IndexThread(MWindow *mwindow,
46         IndexFile *index_file,
47         char *index_filename,
48         int64_t buffer_size,
49         int64_t length_source)
50  : Thread(1, 0, 0)
51 {
52         this->buffer_size = buffer_size;
53         this->length_source = length_source;
54         this->mwindow = mwindow;
55         this->index_filename = index_filename;
56         this->index_file = index_file;
57
58 // initialization is completed in run
59         for(int i = 0; i < TOTAL_INDEX_BUFFERS; i++)
60         {
61 // Must allocate MAX_CHANNELS for a nested EDL
62                 int min_channels = MAX(MAX_CHANNELS, index_file->source_channels);
63                 buffer_in[i] = new Samples*[min_channels];
64                 bzero(buffer_in[i], sizeof(Samples*) * min_channels);
65
66                 output_lock[i] = new Condition(0, "IndexThread::output_lock");
67                 input_lock[i] = new Condition(1, "IndexThread::input_lock");
68
69                 for(int j = 0; j < index_file->source_channels; j++)
70                 {
71                         buffer_in[i][j] = new Samples(buffer_size);
72                 }
73         }
74
75 //printf("IndexThread::IndexThread %d\n", __LINE__);
76 //index_state->dump();
77
78         interrupt_flag = 0;
79 }
80
81 IndexThread::~IndexThread()
82 {
83         for(int i = 0; i < TOTAL_INDEX_BUFFERS; i++)
84         {
85                 for(int j = 0; j < index_file->source_channels; j++)
86                 {
87                         delete buffer_in[i][j];
88                 }
89                 delete [] buffer_in[i];
90                 delete output_lock[i];
91                 delete input_lock[i];
92         }
93 }
94
95 void IndexThread::start_build()
96 {
97         interrupt_flag = 0;
98         current_buffer = 0;
99         for(int i = 0; i <  TOTAL_INDEX_BUFFERS; i++) last_buffer[i] = 0;
100         start();
101 }
102
103 void IndexThread::stop_build()
104 {
105         join();
106 }
107
108 void IndexThread::run()
109 {
110         int done = 0;
111         IndexState *index_state = index_file->get_state();
112         int index_channels = index_file->source_channels;
113         index_state->add_audio_stream(index_channels, index_file->source_length);
114         index_state->init_scan(mwindow->preferences->index_size);
115         index_state->index_status = INDEX_BUILDING;
116
117         while(!interrupt_flag && !done) {
118                 output_lock[current_buffer]->lock("IndexThread::run");
119
120                 if(last_buffer[current_buffer]) done = 1;
121                 if(!interrupt_flag && !done) {
122 // process buffer
123                         int64_t len = input_len[current_buffer];
124                         for( int ch=0; ch<index_channels; ++ch ) {
125                                 double *samples = buffer_in[current_buffer][ch]->get_data();
126                                 index_state->put_data(ch,samples,len);
127                         }
128
129 // draw simultaneously with build
130                         index_file->redraw_edits(0);
131                 }
132
133                 input_lock[current_buffer]->unlock();
134                 current_buffer++;
135                 if(current_buffer >= TOTAL_INDEX_BUFFERS) current_buffer = 0;
136         }
137
138         index_file->redraw_edits(1);
139
140 // write the index file to disk
141         Asset *asset = index_file->indexable->is_asset ? (Asset*)index_file->indexable : 0;
142         index_state->create_index(index_filename, asset);
143 }
144
145
146