transportque tweaks for shuttle, fixes videoscope/undo hangs
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / resourcethread.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 "datatype.h"
30 #include "edl.h"
31 #include "edlsession.h"
32 #include "file.h"
33 #include "framecache.h"
34 #include "mutex.h"
35 #include "mwindow.h"
36 #include "mwindowgui.h"
37 #include "renderengine.h"
38 #include "resourcethread.h"
39 #include "resourcepixmap.h"
40 #include "samples.h"
41 #include "timelinepane.h"
42 #include "trackcanvas.h"
43 #include "transportque.h"
44 #include "vframe.h"
45 #include "vrender.h"
46 #include "wavecache.h"
47
48
49 #include <unistd.h>
50
51 ResourceThreadItem::ResourceThreadItem(ResourcePixmap *pixmap,
52         int pane_number,
53         Indexable *indexable,
54         int data_type,
55         int operation_count)
56 {
57         this->pane_number = pane_number;
58         this->data_type = data_type;
59         this->pixmap = pixmap;
60         this->indexable = indexable;
61
62 // Assets are garbage collected so they don't need to be replicated.
63         this->operation_count = operation_count;
64         indexable->Garbage::add_user();
65         last = 0;
66 }
67
68 ResourceThreadItem::~ResourceThreadItem()
69 {
70         indexable->Garbage::remove_user();
71 }
72
73
74
75
76
77
78
79 VResourceThreadItem::VResourceThreadItem(ResourcePixmap *pixmap,
80         int pane_number,
81         int picon_x,
82         int picon_y,
83         int picon_w,
84         int picon_h,
85         double frame_rate,
86         int64_t position,
87         int layer,
88         Indexable *indexable,
89         int operation_count)
90  : ResourceThreadItem(pixmap,
91         pane_number,
92         indexable,
93         TRACK_VIDEO,
94         operation_count)
95 {
96         this->picon_x = picon_x;
97         this->picon_y = picon_y;
98         this->picon_w = picon_w;
99         this->picon_h = picon_h;
100         this->frame_rate = frame_rate;
101         this->position = position;
102         this->layer = layer;
103 }
104
105 VResourceThreadItem::~VResourceThreadItem()
106 {
107 }
108
109
110
111
112
113
114
115
116 AResourceThreadItem::AResourceThreadItem(ResourcePixmap *pixmap,
117         int pane_number,
118         Indexable *indexable,
119         int x,
120         int channel,
121         int64_t start,
122         int64_t end,
123         int operation_count)
124  : ResourceThreadItem(pixmap,
125         pane_number,
126         indexable,
127         TRACK_AUDIO,
128         operation_count)
129 {
130         this->x = x;
131         this->channel = channel;
132         this->start = start;
133         this->end = end;
134 }
135
136 AResourceThreadItem::~AResourceThreadItem()
137 {
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 ResourceThread::ResourceThread(MWindow *mwindow, MWindowGUI *gui)
157  : Thread(1, 0, 0)
158 {
159 //printf("ResourceThread::ResourceThread %d %p\n", __LINE__, this);
160         this->mwindow = mwindow;
161         this->gui = gui;
162         interrupted = 1;
163         done = 1;
164         temp_picon = 0;
165         temp_picon2 = 0;
166         draw_lock = new Condition(0, "ResourceThread::draw_lock", 0);
167         item_lock = new Mutex("ResourceThread::item_lock");
168         audio_buffer = 0;
169         for(int i = 0; i < MAXCHANNELS; i++)
170                 temp_buffer[i] = 0;
171         timer = new Timer;
172         prev_x = -1;
173         prev_h = 0;
174         prev_l = 0;
175         operation_count = 0;
176         render_engine = 0;
177
178         audio_asset = 0;
179         audio_source = 0;
180         video_asset = 0;
181         video_source = 0;
182 }
183
184 ResourceThread::~ResourceThread()
185 {
186         stop();
187         delete draw_lock;
188         delete item_lock;
189         delete temp_picon;
190         delete temp_picon2;
191         delete audio_buffer;
192         for(int i = 0; i < MAXCHANNELS; i++)
193                 delete temp_buffer[i];
194         delete timer;
195         delete render_engine;
196         if( audio_asset ) audio_asset->remove_user();
197         if( video_asset ) video_asset->remove_user();
198 }
199
200 void ResourceThread::create_objects()
201 {
202         done = 0;
203         Thread::start();
204 }
205
206 void ResourceThread::add_picon(ResourcePixmap *pixmap,
207         int pane_number,
208         int picon_x,
209         int picon_y,
210         int picon_w,
211         int picon_h,
212         double frame_rate,
213         int64_t position,
214         int layer,
215         Indexable *indexable)
216 {
217         item_lock->lock("ResourceThread::item_lock");
218
219         items.append(new VResourceThreadItem(pixmap,
220                 pane_number,
221                 picon_x,
222                 picon_y,
223                 picon_w,
224                 picon_h,
225                 frame_rate,
226                 position,
227                 layer,
228                 indexable,
229                 operation_count));
230         item_lock->unlock();
231 }
232
233 void ResourceThread::add_wave(ResourcePixmap *pixmap,
234         int pane_number,
235         Indexable *indexable,
236         int x,
237         int channel,
238         int64_t source_start,
239         int64_t source_end)
240 {
241         item_lock->lock("ResourceThread::item_lock");
242
243         items.append(new AResourceThreadItem(pixmap,
244                 pane_number,
245                 indexable,
246                 x,
247                 channel,
248                 source_start,
249                 source_end,
250                 operation_count));
251         item_lock->unlock();
252 }
253
254
255
256
257
258
259
260
261
262
263
264 void ResourceThread::stop_draw(int reset)
265 {
266         if(!interrupted)
267         {
268                 interrupted = 1;
269                 item_lock->lock("ResourceThread::stop_draw");
270
271 //printf("ResourceThread::stop_draw %d %d\n", __LINE__, reset);
272 //BC_Signals::dump_stack();
273                 if( reset ) {
274                         items.remove_all_objects();
275                         ++operation_count;
276                 }
277                 item_lock->unlock();
278                 prev_x = -1;
279                 prev_h = 0;
280                 prev_l = 0;
281         }
282 }
283
284 void ResourceThread::start_draw()
285 {
286         interrupted = 0;
287 // Tag last audio item to cause refresh.
288         int i = items.total;
289         while( --i>=0 && items[i]->data_type!=TRACK_AUDIO );
290         if( i >= 0 ) items[i]->last = 1;
291         timer->update();
292         draw_lock->unlock();
293 }
294
295 void ResourceThread::run()
296 {
297         while(!done)
298         {
299
300                 draw_lock->lock("ResourceThread::run");
301                 while(!interrupted)
302                 {
303 // Pull off item
304                         item_lock->lock("ResourceThread::run");
305                         int total_items = items.size();
306                         ResourceThreadItem *item = 0;
307                         if(items.size())
308                         {
309                                 item = items[0];
310                                 items.remove_number(0);
311                         }
312                         item_lock->unlock();
313 //printf("ResourceThread::run %d %d\n", __LINE__, items.size());
314                         if(!total_items) break;
315
316                         switch( item->data_type ) {
317                         case TRACK_VIDEO:
318                                 do_video((VResourceThreadItem*)item);
319                                 break;
320                         case TRACK_AUDIO:
321                                 do_audio((AResourceThreadItem*)item);
322                                 break;
323                         }
324
325                         delete item;
326                 }
327
328                 get_audio_source(0);
329                 get_video_source(0);
330                 mwindow->age_caches();
331         }
332 }
333
334 void ResourceThread::stop()
335 {
336         if( !done ) {
337                 done = 1;
338                 interrupted = 1;
339                 draw_lock->unlock();
340         }
341         join();
342 }
343
344
345 void ResourceThread::open_render_engine(EDL *nested_edl,
346         int do_audio,
347         int do_video)
348 {
349         if(render_engine && render_engine_id != nested_edl->id)
350         {
351                 delete render_engine;
352                 render_engine = 0;
353         }
354
355         if(!render_engine)
356         {
357                 TransportCommand command;
358                 if(do_audio)
359                         command.command = NORMAL_FWD;
360                 else
361                         command.command = CURRENT_FRAME;
362                 command.get_edl()->copy_all(nested_edl);
363                 command.change_type = CHANGE_ALL;
364                 command.realtime = 0;
365                 render_engine = new RenderEngine(0,
366                         mwindow->preferences, 0, 0);
367                 render_engine_id = nested_edl->id;
368                 render_engine->set_vcache(mwindow->video_cache);
369                 render_engine->set_acache(mwindow->audio_cache);
370                 render_engine->arm_command(&command);
371         }
372 }
373
374 File *ResourceThread::get_audio_source(Asset *asset)
375 {
376         if( interrupted ) asset = 0;
377
378         if( audio_asset && audio_asset != asset && (!asset ||
379                 strcmp(audio_asset->path, asset->path)) )
380         {
381                 mwindow->audio_cache->check_in(audio_asset);
382                 audio_source = 0;
383                 audio_asset->remove_user();
384                 audio_asset = 0;
385
386         }
387         if( !audio_asset && asset )
388         {
389                 audio_asset = asset;
390                 audio_asset->add_user();
391                 audio_source = mwindow->audio_cache->check_out(asset, mwindow->edl);
392         }
393         return audio_source;
394 }
395
396 File *ResourceThread::get_video_source(Asset *asset)
397 {
398         if( interrupted ) asset = 0;
399
400         if( video_asset && video_asset != asset && (!asset ||
401                 strcmp(video_asset->path, asset->path)) )
402         {
403                 mwindow->video_cache->check_in(video_asset);
404                 video_source = 0;
405                 video_asset->remove_user();
406                 video_asset = 0;
407
408         }
409         if( !video_asset && asset )
410         {
411                 video_asset = asset;
412                 video_asset->add_user();
413                 video_source = mwindow->video_cache->check_out(asset, mwindow->edl);
414         }
415         return video_source;
416 }
417
418 void ResourceThread::do_video(VResourceThreadItem *item)
419 {
420         int source_w = 0;
421         int source_h = 0;
422         int source_id = -1;
423         int source_cmodel = -1;
424
425         if(item->indexable->is_asset)
426         {
427                 Asset *asset = (Asset*)item->indexable;
428                 source_w = asset->width;
429                 source_h = asset->height;
430                 source_id = asset->id;
431                 source_cmodel = BC_RGB888;
432         }
433         else
434         {
435                 EDL *nested_edl = (EDL*)item->indexable;
436                 source_w = nested_edl->session->output_w;
437                 source_h = nested_edl->session->output_h;
438                 source_id = nested_edl->id;
439                 source_cmodel = nested_edl->session->color_model;
440         }
441
442         if(temp_picon &&
443                 (temp_picon->get_w() != source_w ||
444                 temp_picon->get_h() != source_h ||
445                 temp_picon->get_color_model() != source_cmodel))
446         {
447                 delete temp_picon;
448                 temp_picon = 0;
449         }
450
451         if(!temp_picon)
452         {
453                 temp_picon = new VFrame(0, -1, source_w, source_h, source_cmodel, -1);
454         }
455
456 // Get temporary to copy cached frame to
457         if(temp_picon2 &&
458                 (temp_picon2->get_w() != item->picon_w ||
459                 temp_picon2->get_h() != item->picon_h))
460         {
461                 delete temp_picon2;
462                 temp_picon2 = 0;
463         }
464
465         if(!temp_picon2)
466         {
467                 temp_picon2 = new VFrame( item->picon_w, item->picon_h, BC_RGB888, 0);
468         }
469
470
471
472 // Search frame cache again.
473
474         VFrame *picon_frame = 0;
475         int need_conversion = 0;
476         EDL *nested_edl = 0;
477         Asset *asset = 0;
478
479         picon_frame = mwindow->frame_cache->get_frame_ptr(item->position,
480                 item->layer, item->frame_rate, BC_RGB888,
481                 item->picon_w, item->picon_h, source_id);
482 //printf("search cache %ld,%d,%f,%dx%d,%d = %p\n",
483 //  item->position, item->layer, item->frame_rate,
484 //  item->picon_w, item->picon_h, source_id, picon_frame);
485         if( picon_frame ) {
486                 temp_picon2->copy_from(picon_frame);
487 // Unlock the get_frame_ptr command
488                 mwindow->frame_cache->unlock();
489         }
490         else
491         if(!item->indexable->is_asset)
492         {
493                 nested_edl = (EDL*)item->indexable;
494                 open_render_engine(nested_edl, 0, 1);
495
496                 int64_t source_position = (int64_t)(item->position *
497                         nested_edl->session->frame_rate /
498                         item->frame_rate);
499                 if(render_engine->vrender)
500                         render_engine->vrender->process_buffer(
501                                 temp_picon,
502                                 source_position,
503                                 0);
504
505                 need_conversion = 1;
506         }
507         else
508         {
509                 asset = (Asset*)item->indexable;
510                 File *source = get_video_source(asset);
511                 if(!source)
512                         return;
513
514                 source->set_layer(item->layer);
515                 int64_t normalized_position = (int64_t)(item->position *
516                         asset->frame_rate /
517                         item->frame_rate);
518                 source->set_video_position(normalized_position,
519                         0);
520
521                 source->read_frame(temp_picon);
522
523                 need_conversion = 1;
524         }
525
526         if(need_conversion)
527         {
528                 picon_frame = new VFrame(item->picon_w, item->picon_h, BC_RGB888, 0);
529                 BC_CModels::transfer(picon_frame->get_rows(), temp_picon->get_rows(),
530                         0, 0, 0,  0, 0, 0,
531                         0, 0, temp_picon->get_w(), temp_picon->get_h(),
532                         0, 0, picon_frame->get_w(), picon_frame->get_h(),
533                         source_cmodel, BC_RGB888, 0,
534                         temp_picon->get_bytes_per_line(),
535                         picon_frame->get_bytes_per_line());
536                 temp_picon2->copy_from(picon_frame);
537                 mwindow->frame_cache->put_frame(picon_frame, item->position, item->layer,
538                         mwindow->edl->session->frame_rate, 0, item->indexable);
539         }
540
541 // Allow escape here
542         if(interrupted)
543                 return;
544
545 // Draw the picon
546         mwindow->gui->lock_window("ResourceThread::do_video");
547
548 // It was interrupted while waiting.
549         if(interrupted)
550         {
551                 mwindow->gui->unlock_window();
552                 return;
553         }
554
555
556
557 // Test for pixmap existence first
558         if(item->operation_count == operation_count)
559         {
560                 ArrayList<ResourcePixmap*> &resource_pixmaps = gui->resource_pixmaps;
561                 int i = resource_pixmaps.total;
562                 while( --i >= 0 && resource_pixmaps[i] != item->pixmap );
563                 if( i >= 0 ) {
564                         item->pixmap->draw_vframe(temp_picon2,
565                                 item->picon_x,
566                                 item->picon_y,
567                                 item->picon_w,
568                                 item->picon_h,
569                                 0,
570                                 0);
571
572                         mwindow->gui->update(0, IGNORE_THREAD, 0, 0, 0, 0, 0);
573                 }
574         }
575
576         mwindow->gui->unlock_window();
577 }
578
579
580 #define BUFFERSIZE 65536
581 void ResourceThread::do_audio(AResourceThreadItem *item)
582 {
583 // Search again
584         WaveCacheItem *wave_item;
585         double high = 0;
586         double low = 0;
587         const int debug = 0;
588         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
589         if((wave_item = mwindow->wave_cache->get_wave(item->indexable->id,
590                 item->channel, item->start, item->end)))
591         {
592                 high = wave_item->high;
593                 low = wave_item->low;
594                 mwindow->wave_cache->unlock();
595         }
596         else
597         {
598                 int first_sample = 1;
599                 int64_t start = item->start;
600                 int64_t end = item->end;
601                 if(start == end) end = start + 1;
602                 double *buffer_samples = !audio_buffer ? 0 :
603                         audio_buffer->get_data();
604
605                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
606                 for(int64_t sample = start; sample < end; sample++)
607                 {
608                         double value;
609 // Get value from previous buffer
610                         if(audio_buffer &&
611                                 item->channel == audio_channel &&
612                                 item->indexable->id == audio_asset_id &&
613                                 sample >= audio_start &&
614                                 sample < audio_start + audio_samples)
615                         {
616                                 ;
617                         }
618                         else
619 // Load new buffer
620                         {
621                                 if(!audio_buffer) {
622                                         audio_buffer = new Samples(BUFFERSIZE);
623                                         buffer_samples = audio_buffer->get_data();
624                                 }
625
626                                 int64_t total_samples = item->indexable->get_audio_samples();
627                                 int fragment = BUFFERSIZE;
628                                 if(fragment + sample > total_samples)
629                                         fragment = total_samples - sample;
630
631                                 if(!item->indexable->is_asset)
632                                 {
633                                         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
634                                         open_render_engine((EDL*)item->indexable, 1, 0);
635                                         if(debug) printf("ResourceThread::do_audio %d %p\n", __LINE__, render_engine);
636                                         if(render_engine->arender)
637                                         {
638                                                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
639                                                 int source_channels = item->indexable->get_audio_channels();
640                                                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
641                                                 for(int i = 0; i < MAXCHANNELS; i++)
642                                                 {
643                                                         if(i < source_channels &&
644                                                                 !temp_buffer[i])
645                                                         {
646                                                                 temp_buffer[i] = new Samples(BUFFERSIZE);
647                                                         }
648                                                         else
649                                                         if(i >= source_channels &&
650                                                                 temp_buffer[i])
651                                                         {
652                                                                 delete temp_buffer[i];
653                                                                 temp_buffer[i] = 0;
654                                                         }
655                                                 }
656
657
658                                                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
659                                                 render_engine->arender->process_buffer(
660                                                         temp_buffer,
661                                                         fragment,
662                                                         sample);
663                                                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
664                                                 memcpy(buffer_samples,
665                                                         temp_buffer[item->channel]->get_data(),
666                                                         fragment * sizeof(double));
667                                         }
668                                         else
669                                         {
670                                                 if(debug) printf("ResourceThread::do_audio %d %d\n", __LINE__, fragment);
671                                                 if(fragment > 0) bzero(buffer_samples, sizeof(double) * fragment);
672                                                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
673
674                                         }
675                                 }
676                                 else
677                                 {
678                                         Asset *asset = (Asset*)item->indexable;
679                                         File *source = get_audio_source(asset);
680                                         if(!source)
681                                                 return;
682
683                                         source->set_channel(item->channel);
684                                         source->set_audio_position(sample);
685                                         source->read_samples(audio_buffer, fragment);
686                                 }
687
688                                 audio_asset_id = item->indexable->id;
689                                 audio_channel = item->channel;
690                                 audio_start = sample;
691                                 audio_samples = fragment;
692                         }
693
694
695                         value = buffer_samples[sample - audio_start];
696                         if(first_sample)
697                         {
698                                 high = low = value;
699                                 first_sample = 0;
700                         }
701                         else
702                         {
703                                 if(value > high)
704                                         high = value;
705                                 else
706                                 if(value < low)
707                                         low = value;
708                         }
709                 }
710                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
711
712 // If it's a nested EDL, store all the channels
713                 mwindow->wave_cache->put_wave(item->indexable,
714                         item->channel,
715                         item->start,
716                         item->end,
717                         high,
718                         low);
719                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
720         }
721         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
722
723 // Allow escape here
724         if(interrupted)
725                 return;
726
727         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
728 // Draw the column
729         mwindow->gui->lock_window("ResourceThread::do_audio");
730         if(interrupted)
731         {
732                 mwindow->gui->unlock_window();
733                 return;
734         }
735
736         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
737         if(item->operation_count == operation_count)
738         {
739
740 // Test for pixmap existence first
741                 ArrayList<ResourcePixmap*> &resource_pixmaps = gui->resource_pixmaps;
742                 int i = resource_pixmaps.total;
743                 while( --i >= 0 && resource_pixmaps[i] != item->pixmap );
744                 if( i >= 0 )
745                 {
746                         if(prev_x == item->x - 1)
747                         {
748                                 high = MAX(high, prev_l);
749                                 low = MIN(low, prev_h);
750                         }
751                         prev_x = item->x;
752                         prev_h = high;
753                         prev_l = low;
754                         if(gui->pane[item->pane_number])
755                                 item->pixmap->draw_wave(
756                                         gui->pane[item->pane_number]->canvas,
757                                         item->x,
758                                         high,
759                                         low);
760                         if(timer->get_difference() > 250 || item->last)
761                         {
762                                 mwindow->gui->update(0, IGNORE_THREAD, 0, 0, 0, 0, 0);
763                                 timer->update();
764                         }
765                 }
766         }
767         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
768
769         mwindow->gui->unlock_window();
770
771 }
772
773
774
775