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 // interrupted_lock = new Condition(0, "ResourceThread::interrupted_lock", 0);
168 item_lock = new Mutex("ResourceThread::item_lock");
170 for(int i = 0; i < MAXCHANNELS; i++)
185 ResourceThread::~ResourceThread()
189 // delete interrupted_lock;
194 for(int i = 0; i < MAXCHANNELS; i++)
195 delete temp_buffer[i];
197 delete render_engine;
200 void ResourceThread::create_objects()
206 void ResourceThread::add_picon(ResourcePixmap *pixmap,
215 Indexable *indexable)
217 item_lock->lock("ResourceThread::item_lock");
219 items.append(new VResourceThreadItem(pixmap,
233 void ResourceThread::add_wave(ResourcePixmap *pixmap,
235 Indexable *indexable,
238 int64_t source_start,
241 item_lock->lock("ResourceThread::item_lock");
243 items.append(new AResourceThreadItem(pixmap,
264 void ResourceThread::stop_draw(int reset)
269 item_lock->lock("ResourceThread::stop_draw");
271 //printf("ResourceThread::stop_draw %d %d\n", __LINE__, reset);
272 //BC_Signals::dump_stack();
273 if(reset) items.remove_all_objects();
282 void ResourceThread::start_draw()
285 // Tag last audio item to cause refresh.
287 while( --i>=0 && items[i]->data_type!=TRACK_AUDIO );
288 if( i >= 0 ) items[i]->last = 1;
293 void ResourceThread::run()
298 draw_lock->lock("ResourceThread::run");
303 item_lock->lock("ResourceThread::run");
304 int total_items = items.size();
305 ResourceThreadItem *item = 0;
309 items.remove_number(0);
312 //printf("ResourceThread::run %d %d\n", __LINE__, items.size());
313 if(!total_items) break;
315 switch( item->data_type ) {
317 do_video((VResourceThreadItem*)item);
320 do_audio((AResourceThreadItem*)item);
329 mwindow->age_caches();
333 void ResourceThread::stop()
344 void ResourceThread::open_render_engine(EDL *nested_edl,
348 if(render_engine && render_engine_id != nested_edl->id)
350 delete render_engine;
356 TransportCommand command;
358 command.command = NORMAL_FWD;
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);
373 File *ResourceThread::get_audio_source(Asset *asset)
375 if( audio_asset && audio_asset != asset && (!asset ||
376 strcmp(audio_asset->path, asset->path)) )
378 mwindow->audio_cache->check_in(audio_asset);
383 if( !audio_asset && asset )
386 audio_source = mwindow->audio_cache->check_out(asset, mwindow->edl);
391 File *ResourceThread::get_video_source(Asset *asset)
393 if( video_asset && video_asset != asset && (!asset ||
394 strcmp(video_asset->path, asset->path)) )
396 mwindow->video_cache->check_in(video_asset);
401 if( !video_asset && asset )
404 video_source = mwindow->video_cache->check_out(asset, mwindow->edl);
409 void ResourceThread::do_video(VResourceThreadItem *item)
414 int source_cmodel = -1;
416 if(item->indexable->is_asset)
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;
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;
434 (temp_picon->get_w() != source_w ||
435 temp_picon->get_h() != source_h ||
436 temp_picon->get_color_model() != source_cmodel))
444 temp_picon = new VFrame(0,
452 // Get temporary to copy cached frame to
454 (temp_picon2->get_w() != item->picon_w ||
455 temp_picon2->get_h() != item->picon_h))
463 temp_picon2 = new VFrame(0,
473 // Search frame cache again.
475 VFrame *picon_frame = 0;
476 int need_conversion = 0;
480 picon_frame = mwindow->frame_cache->get_frame_ptr(item->position,
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);
492 temp_picon2->copy_from(picon_frame);
493 // Unlock the get_frame_ptr command
494 mwindow->frame_cache->unlock();
497 if(!item->indexable->is_asset)
499 nested_edl = (EDL*)item->indexable;
500 open_render_engine(nested_edl, 0, 1);
502 int64_t source_position = (int64_t)(item->position *
503 nested_edl->session->frame_rate /
505 if(render_engine->vrender)
506 render_engine->vrender->process_buffer(
515 asset = (Asset*)item->indexable;
516 File *source = get_video_source(asset);
520 source->set_layer(item->layer);
521 int64_t normalized_position = (int64_t)(item->position *
524 source->set_video_position(normalized_position,
527 source->read_frame(temp_picon);
534 picon_frame = new VFrame(0,
540 BC_CModels::transfer(picon_frame->get_rows(),
541 temp_picon->get_rows(),
554 picon_frame->get_w(),
555 picon_frame->get_h(),
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,
565 mwindow->edl->session->frame_rate,
578 mwindow->gui->lock_window("ResourceThread::do_video");
580 // It was interrupted while waiting.
583 mwindow->gui->unlock_window();
589 // Test for pixmap existence first
590 if(item->operation_count == operation_count)
592 ArrayList<ResourcePixmap*> &resource_pixmaps = gui->resource_pixmaps;
593 int i = resource_pixmaps.total;
594 while( --i >= 0 && resource_pixmaps[i] != item->pixmap );
596 item->pixmap->draw_vframe(temp_picon2,
604 mwindow->gui->update(0, IGNORE_THREAD, 0, 0, 0, 0, 0);
608 mwindow->gui->unlock_window();
612 #define BUFFERSIZE 65536
613 void ResourceThread::do_audio(AResourceThreadItem *item)
616 WaveCacheItem *wave_item;
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)))
624 high = wave_item->high;
625 low = wave_item->low;
626 mwindow->wave_cache->unlock();
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();
637 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
638 for(int64_t sample = start; sample < end; sample++)
641 // Get value from previous buffer
643 item->channel == audio_channel &&
644 item->indexable->id == audio_asset_id &&
645 sample >= audio_start &&
646 sample < audio_start + audio_samples)
654 audio_buffer = new Samples(BUFFERSIZE);
655 buffer_samples = audio_buffer->get_data();
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;
663 if(!item->indexable->is_asset)
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)
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++)
675 if(i < source_channels &&
678 temp_buffer[i] = new Samples(BUFFERSIZE);
681 if(i >= source_channels &&
684 delete temp_buffer[i];
690 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
691 render_engine->arender->process_buffer(
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));
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__);
710 Asset *asset = (Asset*)item->indexable;
711 File *source = get_audio_source(asset);
715 source->set_channel(item->channel);
716 source->set_audio_position(sample);
717 source->read_samples(audio_buffer, fragment);
720 audio_asset_id = item->indexable->id;
721 audio_channel = item->channel;
722 audio_start = sample;
723 audio_samples = fragment;
727 value = buffer_samples[sample - audio_start];
742 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
744 // If it's a nested EDL, store all the channels
745 mwindow->wave_cache->put_wave(item->indexable,
751 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
753 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
759 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
761 mwindow->gui->lock_window("ResourceThread::do_audio");
764 mwindow->gui->unlock_window();
768 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
769 if(item->operation_count == operation_count)
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 );
778 if(prev_x == item->x - 1)
780 high = MAX(high, prev_l);
781 low = MIN(low, prev_h);
786 if(gui->pane[item->pane_number])
787 item->pixmap->draw_wave(
788 gui->pane[item->pane_number]->canvas,
792 if(timer->get_difference() > 250 || item->last)
794 mwindow->gui->update(0, 3, 0, 0, 0, 0, 0);
799 if(debug) printf("ResourceThread::do_audio %d\n", __LINE__);
801 mwindow->gui->unlock_window();