initial commit
[goodguy/history.git] / cinelerra-5.0 / 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 //      interrupted_lock = new Condition(0, "ResourceThread::interrupted_lock", 0);
168         item_lock = new Mutex("ResourceThread::item_lock");
169         audio_buffer = 0;
170         for(int i = 0; i < MAXCHANNELS; i++)
171                 temp_buffer[i] = 0;
172         timer = new Timer;
173         prev_x = -1;
174         prev_h = 0;
175         prev_l = 0;
176         operation_count = 0;
177         render_engine = 0;
178
179         audio_asset = 0;
180         audio_source = 0;
181         video_asset = 0;
182         video_source = 0;
183 }
184
185 ResourceThread::~ResourceThread()
186 {
187         stop();
188         delete draw_lock;
189 //      delete interrupted_lock;
190         delete item_lock;
191         delete temp_picon;
192         delete temp_picon2;
193         delete audio_buffer;
194         for(int i = 0; i < MAXCHANNELS; i++)
195                 delete temp_buffer[i];
196         delete timer;
197         delete render_engine;
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) items.remove_all_objects();
274                 operation_count++;
275                 item_lock->unlock();
276                 prev_x = -1;
277                 prev_h = 0;
278                 prev_l = 0;
279         }
280 }
281
282 void ResourceThread::start_draw()
283 {
284         interrupted = 0;
285 // Tag last audio item to cause refresh.
286         int i = items.total;
287         while( --i>=0 && items[i]->data_type!=TRACK_AUDIO );
288         if( i >= 0 ) items[i]->last = 1;
289         timer->update();
290         draw_lock->unlock();
291 }
292
293 void ResourceThread::run()
294 {
295         while(!done)
296         {
297
298                 draw_lock->lock("ResourceThread::run");
299
300                 while(!interrupted)
301                 {
302 // Pull off item
303                         item_lock->lock("ResourceThread::run");
304                         int total_items = items.size();
305                         ResourceThreadItem *item = 0;
306                         if(items.size())
307                         {
308                                 item = items[0];
309                                 items.remove_number(0);
310                         }
311                         item_lock->unlock();
312 //printf("ResourceThread::run %d %d\n", __LINE__, items.size());
313                         if(!total_items) break;
314
315                         switch( item->data_type ) {
316                         case TRACK_VIDEO:
317                                 do_video((VResourceThreadItem*)item);
318                                 break;
319                         case TRACK_AUDIO:
320                                 do_audio((AResourceThreadItem*)item);
321                                 break;
322                         }
323
324                         delete item;
325                 }
326
327                 get_audio_source(0);
328                 get_video_source(0);
329                 mwindow->age_caches();
330         }
331 }
332
333 void ResourceThread::stop()
334 {
335         if( !done ) {
336                 done = 1;
337                 interrupted = 1;
338                 draw_lock->unlock();
339                 join();
340         }
341 }
342
343
344 void ResourceThread::open_render_engine(EDL *nested_edl,
345         int do_audio,
346         int do_video)
347 {
348         if(render_engine && render_engine_id != nested_edl->id)
349         {
350                 delete render_engine;
351                 render_engine = 0;
352         }
353
354         if(!render_engine)
355         {
356                 TransportCommand command;
357                 if(do_audio)
358                         command.command = NORMAL_FWD;
359                 else
360                         command.command = CURRENT_FRAME;
361                 command.get_edl()->copy_all(nested_edl);
362                 command.change_type = CHANGE_ALL;
363                 command.realtime = 0;
364                 render_engine = new RenderEngine(0,
365                         mwindow->preferences, 0, 0, 0);
366                 render_engine_id = nested_edl->id;
367                 render_engine->set_vcache(mwindow->video_cache);
368                 render_engine->set_acache(mwindow->audio_cache);
369                 render_engine->arm_command(&command);
370         }
371 }
372
373 File *ResourceThread::get_audio_source(Asset *asset)
374 {
375         if( audio_asset && audio_asset != asset && (!asset ||
376                 strcmp(audio_asset->path, asset->path)) )
377         {
378                 mwindow->audio_cache->check_in(audio_asset);
379                 audio_source = 0;
380                 audio_asset = 0;
381
382         }
383         if( !audio_asset && asset )
384         {
385                 audio_asset = asset;
386                 audio_source = mwindow->audio_cache->check_out(asset, mwindow->edl);
387         }
388         return audio_source;
389 }
390
391 File *ResourceThread::get_video_source(Asset *asset)
392 {
393         if( video_asset && video_asset != asset && (!asset ||
394                 strcmp(video_asset->path, asset->path)) )
395         {
396                 mwindow->video_cache->check_in(video_asset);
397                 video_source = 0;
398                 video_asset = 0;
399
400         }
401         if( !video_asset && asset )
402         {
403                 video_asset = asset;
404                 video_source = mwindow->video_cache->check_out(asset, mwindow->edl);
405         }
406         return video_source;
407 }
408
409 void ResourceThread::do_video(VResourceThreadItem *item)
410 {
411         int source_w = 0;
412         int source_h = 0;
413         int source_id = -1;
414         int source_cmodel = -1;
415
416         if(item->indexable->is_asset)
417         {
418                 Asset *asset = (Asset*)item->indexable;
419                 source_w = asset->width;
420                 source_h = asset->height;
421                 source_id = asset->id;
422                 source_cmodel = BC_RGB888;
423         }
424         else
425         {
426                 EDL *nested_edl = (EDL*)item->indexable;
427                 source_w = nested_edl->session->output_w;
428                 source_h = nested_edl->session->output_h;
429                 source_id = nested_edl->id;
430                 source_cmodel = nested_edl->session->color_model;
431         }
432
433         if(temp_picon &&
434                 (temp_picon->get_w() != source_w ||
435                 temp_picon->get_h() != source_h ||
436                 temp_picon->get_color_model() != source_cmodel))
437         {
438                 delete temp_picon;
439                 temp_picon = 0;
440         }
441
442         if(!temp_picon)
443         {
444                 temp_picon = new VFrame(0,
445                         -1,
446                         source_w,
447                         source_h,
448                         source_cmodel,
449                         -1);
450         }
451
452 // Get temporary to copy cached frame to
453         if(temp_picon2 &&
454                 (temp_picon2->get_w() != item->picon_w ||
455                 temp_picon2->get_h() != item->picon_h))
456         {
457                 delete temp_picon2;
458                 temp_picon2 = 0;
459         }
460
461         if(!temp_picon2)
462         {
463                 temp_picon2 = new VFrame(0,
464                         -1,
465                         item->picon_w,
466                         item->picon_h,
467                         BC_RGB888,
468                         -1);
469         }
470
471
472
473 // Search frame cache again.
474
475         VFrame *picon_frame = 0;
476         int need_conversion = 0;
477         EDL *nested_edl = 0;
478         Asset *asset = 0;
479
480         picon_frame = mwindow->frame_cache->get_frame_ptr(item->position,
481                 item->layer,
482                 item->frame_rate,
483                 BC_RGB888,
484                 item->picon_w,
485                 item->picon_h,
486                 source_id);
487 //printf("search cache %ld,%d,%f,%dx%d,%d = %p\n",
488 //  item->position, item->layer, item->frame_rate,
489 //  item->picon_w, item->picon_h, source_id, picon_frame);
490         if( picon_frame )
491         {
492                 temp_picon2->copy_from(picon_frame);
493 // Unlock the get_frame_ptr command
494                 mwindow->frame_cache->unlock();
495         }
496         else
497         if(!item->indexable->is_asset)
498         {
499                 nested_edl = (EDL*)item->indexable;
500                 open_render_engine(nested_edl, 0, 1);
501
502                 int64_t source_position = (int64_t)(item->position *
503                         nested_edl->session->frame_rate /
504                         item->frame_rate);
505                 if(render_engine->vrender)
506                         render_engine->vrender->process_buffer(
507                                 temp_picon,
508                                 source_position,
509                                 0);
510
511                 need_conversion = 1;
512         }
513         else
514         {
515                 asset = (Asset*)item->indexable;
516                 File *source = get_video_source(asset);
517                 if(!source)
518                         return;
519
520                 source->set_layer(item->layer);
521                 int64_t normalized_position = (int64_t)(item->position *
522                         asset->frame_rate /
523                         item->frame_rate);
524                 source->set_video_position(normalized_position,
525                         0);
526
527                 source->read_frame(temp_picon);
528
529                 need_conversion = 1;
530         }
531
532         if(need_conversion)
533         {
534                 picon_frame = new VFrame(0,
535                         -1,
536                         item->picon_w,
537                         item->picon_h,
538                         BC_RGB888,
539                         -1);
540                 BC_CModels::transfer(picon_frame->get_rows(),
541                         temp_picon->get_rows(),
542                         0,
543                         0,
544                         0,
545                         0,
546                         0,
547                         0,
548                         0,
549                         0,
550                         temp_picon->get_w(),
551                         temp_picon->get_h(),
552                         0,
553                         0,
554                         picon_frame->get_w(),
555                         picon_frame->get_h(),
556                         source_cmodel,
557                         BC_RGB888,
558                         0,
559                         temp_picon->get_bytes_per_line(),
560                         picon_frame->get_bytes_per_line());
561                 temp_picon2->copy_from(picon_frame);
562                 mwindow->frame_cache->put_frame(picon_frame,
563                         item->position,
564                         item->layer,
565                         mwindow->edl->session->frame_rate,
566                         0,
567                         item->indexable);
568         }
569
570 // Allow escape here
571         if(interrupted)
572         {
573                 return;
574         }
575
576
577 // Draw the picon
578         mwindow->gui->lock_window("ResourceThread::do_video");
579
580 // It was interrupted while waiting.
581         if(interrupted)
582         {
583                 mwindow->gui->unlock_window();
584                 return;
585         }
586
587
588
589 // Test for pixmap existence first
590         if(item->operation_count == operation_count)
591         {
592                 ArrayList<ResourcePixmap*> &resource_pixmaps = gui->resource_pixmaps;
593                 int i = resource_pixmaps.total;
594                 while( --i >= 0 && resource_pixmaps[i] != item->pixmap );
595                 if( i >= 0 ) {
596                         item->pixmap->draw_vframe(temp_picon2,
597                                 item->picon_x,
598                                 item->picon_y,
599                                 item->picon_w,
600                                 item->picon_h,
601                                 0,
602                                 0);
603
604                         mwindow->gui->update(0, IGNORE_THREAD, 0, 0, 0, 0, 0);
605                 }
606         }
607
608         mwindow->gui->unlock_window();
609 }
610
611
612 #define BUFFERSIZE 65536
613 void ResourceThread::do_audio(AResourceThreadItem *item)
614 {
615 // Search again
616         WaveCacheItem *wave_item;
617         double high = 0;
618         double low = 0;
619         const int debug = 0;
620         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
621         if((wave_item = mwindow->wave_cache->get_wave(item->indexable->id,
622                 item->channel, item->start, item->end)))
623         {
624                 high = wave_item->high;
625                 low = wave_item->low;
626                 mwindow->wave_cache->unlock();
627         }
628         else
629         {
630                 int first_sample = 1;
631                 int64_t start = item->start;
632                 int64_t end = item->end;
633                 if(start == end) end = start + 1;
634                 double *buffer_samples = !audio_buffer ? 0 :
635                         audio_buffer->get_data();
636
637                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
638                 for(int64_t sample = start; sample < end; sample++)
639                 {
640                         double value;
641 // Get value from previous buffer
642                         if(audio_buffer &&
643                                 item->channel == audio_channel &&
644                                 item->indexable->id == audio_asset_id &&
645                                 sample >= audio_start &&
646                                 sample < audio_start + audio_samples)
647                         {
648                                 ;
649                         }
650                         else
651 // Load new buffer
652                         {
653                                 if(!audio_buffer) {
654                                         audio_buffer = new Samples(BUFFERSIZE);
655                                         buffer_samples = audio_buffer->get_data();
656                                 }
657
658                                 int64_t total_samples = item->indexable->get_audio_samples();
659                                 int fragment = BUFFERSIZE;
660                                 if(fragment + sample > total_samples)
661                                         fragment = total_samples - sample;
662
663                                 if(!item->indexable->is_asset)
664                                 {
665                                         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
666                                         open_render_engine((EDL*)item->indexable, 1, 0);
667                                         if(debug) printf("ResourceThread::do_audio %d %p\n", __LINE__, render_engine);
668                                         if(render_engine->arender)
669                                         {
670                                                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
671                                                 int source_channels = item->indexable->get_audio_channels();
672                                                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
673                                                 for(int i = 0; i < MAXCHANNELS; i++)
674                                                 {
675                                                         if(i < source_channels &&
676                                                                 !temp_buffer[i])
677                                                         {
678                                                                 temp_buffer[i] = new Samples(BUFFERSIZE);
679                                                         }
680                                                         else
681                                                         if(i >= source_channels &&
682                                                                 temp_buffer[i])
683                                                         {
684                                                                 delete temp_buffer[i];
685                                                                 temp_buffer[i] = 0;
686                                                         }
687                                                 }
688
689
690                                                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
691                                                 render_engine->arender->process_buffer(
692                                                         temp_buffer,
693                                                         fragment,
694                                                         sample);
695                                                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
696                                                 memcpy(buffer_samples,
697                                                         temp_buffer[item->channel]->get_data(),
698                                                         fragment * sizeof(double));
699                                         }
700                                         else
701                                         {
702                                                 if(debug) printf("ResourceThread::do_audio %d %d\n", __LINE__, fragment);
703                                                 if(fragment > 0) bzero(buffer_samples, sizeof(double) * fragment);
704                                                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
705
706                                         }
707                                 }
708                                 else
709                                 {
710                                         Asset *asset = (Asset*)item->indexable;
711                                         File *source = get_audio_source(asset);
712                                         if(!source)
713                                                 return;
714
715                                         source->set_channel(item->channel);
716                                         source->set_audio_position(sample);
717                                         source->read_samples(audio_buffer, fragment);
718                                 }
719
720                                 audio_asset_id = item->indexable->id;
721                                 audio_channel = item->channel;
722                                 audio_start = sample;
723                                 audio_samples = fragment;
724                         }
725
726
727                         value = buffer_samples[sample - audio_start];
728                         if(first_sample)
729                         {
730                                 high = low = value;
731                                 first_sample = 0;
732                         }
733                         else
734                         {
735                                 if(value > high)
736                                         high = value;
737                                 else
738                                 if(value < low)
739                                         low = value;
740                         }
741                 }
742                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
743
744 // If it's a nested EDL, store all the channels
745                 mwindow->wave_cache->put_wave(item->indexable,
746                         item->channel,
747                         item->start,
748                         item->end,
749                         high,
750                         low);
751                 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
752         }
753         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
754
755 // Allow escape here
756         if(interrupted)
757                 return;
758
759         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
760 // Draw the column
761         mwindow->gui->lock_window("ResourceThread::do_audio");
762         if(interrupted)
763         {
764                 mwindow->gui->unlock_window();
765                 return;
766         }
767
768         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
769         if(item->operation_count == operation_count)
770         {
771
772 // Test for pixmap existence first
773                 ArrayList<ResourcePixmap*> &resource_pixmaps = gui->resource_pixmaps;
774                 int i = resource_pixmaps.total;
775                 while( --i >= 0 && resource_pixmaps[i] != item->pixmap );
776                 if( i >= 0 )
777                 {
778                         if(prev_x == item->x - 1)
779                         {
780                                 high = MAX(high, prev_l);
781                                 low = MIN(low, prev_h);
782                         }
783                         prev_x = item->x;
784                         prev_h = high;
785                         prev_l = low;
786                         if(gui->pane[item->pane_number])
787                                 item->pixmap->draw_wave(
788                                         gui->pane[item->pane_number]->canvas,
789                                         item->x,
790                                         high,
791                                         low);
792                         if(timer->get_difference() > 250 || item->last)
793                         {
794                                 mwindow->gui->update(0, 3, 0, 0, 0, 0, 0);
795                                 timer->update();
796                         }
797                 }
798         }
799         if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
800
801         mwindow->gui->unlock_window();
802
803 }
804
805
806
807