4 * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
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.
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.
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
24 #include "bcsignals.h"
28 #include "condition.h"
31 #include "edlsession.h"
33 #include "framecache.h"
36 #include "mwindowgui.h"
37 #include "renderengine.h"
38 #include "resourcethread.h"
39 #include "resourcepixmap.h"
41 #include "timelinepane.h"
42 #include "trackcanvas.h"
43 #include "transportque.h"
46 #include "wavecache.h"
51 ResourceThreadItem::ResourceThreadItem(ResourcePixmap *pixmap,
57 this->pane_number = pane_number;
58 this->data_type = data_type;
59 this->pixmap = pixmap;
60 this->indexable = indexable;
62 // Assets are garbage collected so they don't need to be replicated.
63 this->operation_count = operation_count;
64 indexable->Garbage::add_user();
68 ResourceThreadItem::~ResourceThreadItem()
70 indexable->Garbage::remove_user();
79 VResourceThreadItem::VResourceThreadItem(ResourcePixmap *pixmap,
90 : ResourceThreadItem(pixmap,
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;
105 VResourceThreadItem::~VResourceThreadItem()
116 AResourceThreadItem::AResourceThreadItem(ResourcePixmap *pixmap,
118 Indexable *indexable,
124 : ResourceThreadItem(pixmap,
131 this->channel = channel;
136 AResourceThreadItem::~AResourceThreadItem()
156 ResourceThread::ResourceThread(MWindow *mwindow, MWindowGUI *gui)
159 //printf("ResourceThread::ResourceThread %d %p\n", __LINE__, this);
160 this->mwindow = mwindow;
166 draw_lock = new Condition(0, "ResourceThread::draw_lock", 0);
167 source_lock = new Condition(1, "ResourceThread::source_lock", 0);
168 item_lock = new Mutex("ResourceThread::item_lock");
170 for(int i = 0; i < MAXCHANNELS; i++)
185 ResourceThread::~ResourceThread()
194 for(int i = 0; i < MAXCHANNELS; i++)
195 delete temp_buffer[i];
197 delete render_engine;
198 if( audio_asset ) audio_asset->remove_user();
199 if( video_asset ) video_asset->remove_user();
202 void ResourceThread::create_objects()
208 void ResourceThread::add_picon(ResourcePixmap *pixmap,
217 Indexable *indexable)
219 item_lock->lock("ResourceThread::item_lock");
221 items.append(new VResourceThreadItem(pixmap,
235 void ResourceThread::add_wave(ResourcePixmap *pixmap,
237 Indexable *indexable,
240 int64_t source_start,
243 item_lock->lock("ResourceThread::item_lock");
245 items.append(new AResourceThreadItem(pixmap,
266 void ResourceThread::stop_draw(int reset)
271 item_lock->lock("ResourceThread::stop_draw");
273 //printf("ResourceThread::stop_draw %d %d\n", __LINE__, reset);
274 //BC_Signals::dump_stack();
275 if(reset) items.remove_all_objects();
284 void ResourceThread::start_draw()
287 // Tag last audio item to cause refresh.
289 while( --i>=0 && items[i]->data_type!=TRACK_AUDIO );
290 if( i >= 0 ) items[i]->last = 1;
295 void ResourceThread::run()
300 draw_lock->lock("ResourceThread::run");
301 source_lock->lock("ResourceThread::run");
305 item_lock->lock("ResourceThread::run");
306 int total_items = items.size();
307 ResourceThreadItem *item = 0;
311 items.remove_number(0);
314 //printf("ResourceThread::run %d %d\n", __LINE__, items.size());
315 if(!total_items) break;
317 switch( item->data_type ) {
319 do_video((VResourceThreadItem*)item);
322 do_audio((AResourceThreadItem*)item);
331 mwindow->age_caches();
332 source_lock->unlock();
336 void ResourceThread::stop()
347 void ResourceThread::open_render_engine(EDL *nested_edl,
351 if(render_engine && render_engine_id != nested_edl->id)
353 delete render_engine;
359 TransportCommand command;
361 command.command = NORMAL_FWD;
363 command.command = CURRENT_FRAME;
364 command.get_edl()->copy_all(nested_edl);
365 command.change_type = CHANGE_ALL;
366 command.realtime = 0;
367 render_engine = new RenderEngine(0,
368 mwindow->preferences, 0, 0);
369 render_engine_id = nested_edl->id;
370 render_engine->set_vcache(mwindow->video_cache);
371 render_engine->set_acache(mwindow->audio_cache);
372 render_engine->arm_command(&command);
376 File *ResourceThread::get_audio_source(Asset *asset)
378 if( interrupted ) asset = 0;
380 if( audio_asset && audio_asset != asset && (!asset ||
381 strcmp(audio_asset->path, asset->path)) )
383 mwindow->audio_cache->check_in(audio_asset);
385 audio_asset->remove_user();
389 if( !audio_asset && asset )
392 audio_asset->add_user();
393 audio_source = mwindow->audio_cache->check_out(asset, mwindow->edl);
398 File *ResourceThread::get_video_source(Asset *asset)
400 if( interrupted ) asset = 0;
402 if( video_asset && video_asset != asset && (!asset ||
403 strcmp(video_asset->path, asset->path)) )
405 mwindow->video_cache->check_in(video_asset);
407 video_asset->remove_user();
411 if( !video_asset && asset )
414 video_asset->add_user();
415 video_source = mwindow->video_cache->check_out(asset, mwindow->edl);
420 void ResourceThread::do_video(VResourceThreadItem *item)
425 int source_cmodel = -1;
427 if(item->indexable->is_asset)
429 Asset *asset = (Asset*)item->indexable;
430 source_w = asset->width;
431 source_h = asset->height;
432 source_id = asset->id;
433 source_cmodel = BC_RGB888;
437 EDL *nested_edl = (EDL*)item->indexable;
438 source_w = nested_edl->session->output_w;
439 source_h = nested_edl->session->output_h;
440 source_id = nested_edl->id;
441 source_cmodel = nested_edl->session->color_model;
445 (temp_picon->get_w() != source_w ||
446 temp_picon->get_h() != source_h ||
447 temp_picon->get_color_model() != source_cmodel))
455 temp_picon = new VFrame(0,
463 // Get temporary to copy cached frame to
465 (temp_picon2->get_w() != item->picon_w ||
466 temp_picon2->get_h() != item->picon_h))
474 temp_picon2 = new VFrame(0,
484 // Search frame cache again.
486 VFrame *picon_frame = 0;
487 int need_conversion = 0;
491 picon_frame = mwindow->frame_cache->get_frame_ptr(item->position,
498 //printf("search cache %ld,%d,%f,%dx%d,%d = %p\n",
499 // item->position, item->layer, item->frame_rate,
500 // item->picon_w, item->picon_h, source_id, picon_frame);
503 temp_picon2->copy_from(picon_frame);
504 // Unlock the get_frame_ptr command
505 mwindow->frame_cache->unlock();
508 if(!item->indexable->is_asset)
510 nested_edl = (EDL*)item->indexable;
511 open_render_engine(nested_edl, 0, 1);
513 int64_t source_position = (int64_t)(item->position *
514 nested_edl->session->frame_rate /
516 if(render_engine->vrender)
517 render_engine->vrender->process_buffer(
526 asset = (Asset*)item->indexable;
527 File *source = get_video_source(asset);
531 source->set_layer(item->layer);
532 int64_t normalized_position = (int64_t)(item->position *
535 source->set_video_position(normalized_position,
538 source->read_frame(temp_picon);
545 picon_frame = new VFrame(0,
551 BC_CModels::transfer(picon_frame->get_rows(),
552 temp_picon->get_rows(),
565 picon_frame->get_w(),
566 picon_frame->get_h(),
570 temp_picon->get_bytes_per_line(),
571 picon_frame->get_bytes_per_line());
572 temp_picon2->copy_from(picon_frame);
573 mwindow->frame_cache->put_frame(picon_frame,
576 mwindow->edl->session->frame_rate,
589 mwindow->gui->lock_window("ResourceThread::do_video");
591 // It was interrupted while waiting.
594 mwindow->gui->unlock_window();
600 // Test for pixmap existence first
601 if(item->operation_count == operation_count)
603 ArrayList<ResourcePixmap*> &resource_pixmaps = gui->resource_pixmaps;
604 int i = resource_pixmaps.total;
605 while( --i >= 0 && resource_pixmaps[i] != item->pixmap );
607 item->pixmap->draw_vframe(temp_picon2,
615 mwindow->gui->update(0, IGNORE_THREAD, 0, 0, 0, 0, 0);
619 mwindow->gui->unlock_window();
623 #define BUFFERSIZE 65536
624 void ResourceThread::do_audio(AResourceThreadItem *item)
627 WaveCacheItem *wave_item;
631 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
632 if((wave_item = mwindow->wave_cache->get_wave(item->indexable->id,
633 item->channel, item->start, item->end)))
635 high = wave_item->high;
636 low = wave_item->low;
637 mwindow->wave_cache->unlock();
641 int first_sample = 1;
642 int64_t start = item->start;
643 int64_t end = item->end;
644 if(start == end) end = start + 1;
645 double *buffer_samples = !audio_buffer ? 0 :
646 audio_buffer->get_data();
648 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
649 for(int64_t sample = start; sample < end; sample++)
652 // Get value from previous buffer
654 item->channel == audio_channel &&
655 item->indexable->id == audio_asset_id &&
656 sample >= audio_start &&
657 sample < audio_start + audio_samples)
665 audio_buffer = new Samples(BUFFERSIZE);
666 buffer_samples = audio_buffer->get_data();
669 int64_t total_samples = item->indexable->get_audio_samples();
670 int fragment = BUFFERSIZE;
671 if(fragment + sample > total_samples)
672 fragment = total_samples - sample;
674 if(!item->indexable->is_asset)
676 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
677 open_render_engine((EDL*)item->indexable, 1, 0);
678 if(debug) printf("ResourceThread::do_audio %d %p\n", __LINE__, render_engine);
679 if(render_engine->arender)
681 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
682 int source_channels = item->indexable->get_audio_channels();
683 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
684 for(int i = 0; i < MAXCHANNELS; i++)
686 if(i < source_channels &&
689 temp_buffer[i] = new Samples(BUFFERSIZE);
692 if(i >= source_channels &&
695 delete temp_buffer[i];
701 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
702 render_engine->arender->process_buffer(
706 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
707 memcpy(buffer_samples,
708 temp_buffer[item->channel]->get_data(),
709 fragment * sizeof(double));
713 if(debug) printf("ResourceThread::do_audio %d %d\n", __LINE__, fragment);
714 if(fragment > 0) bzero(buffer_samples, sizeof(double) * fragment);
715 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
721 Asset *asset = (Asset*)item->indexable;
722 File *source = get_audio_source(asset);
726 source->set_channel(item->channel);
727 source->set_audio_position(sample);
728 source->read_samples(audio_buffer, fragment);
731 audio_asset_id = item->indexable->id;
732 audio_channel = item->channel;
733 audio_start = sample;
734 audio_samples = fragment;
738 value = buffer_samples[sample - audio_start];
753 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
755 // If it's a nested EDL, store all the channels
756 mwindow->wave_cache->put_wave(item->indexable,
762 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
764 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
770 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
772 mwindow->gui->lock_window("ResourceThread::do_audio");
775 mwindow->gui->unlock_window();
779 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
780 if(item->operation_count == operation_count)
783 // Test for pixmap existence first
784 ArrayList<ResourcePixmap*> &resource_pixmaps = gui->resource_pixmaps;
785 int i = resource_pixmaps.total;
786 while( --i >= 0 && resource_pixmaps[i] != item->pixmap );
789 if(prev_x == item->x - 1)
791 high = MAX(high, prev_l);
792 low = MIN(low, prev_h);
797 if(gui->pane[item->pane_number])
798 item->pixmap->draw_wave(
799 gui->pane[item->pane_number]->canvas,
803 if(timer->get_difference() > 250 || item->last)
805 mwindow->gui->update(0, 3, 0, 0, 0, 0, 0);
810 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
812 mwindow->gui->unlock_window();