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
28 #include "automation.h"
30 #include "bcsignals.h"
33 #include "bccmodels.h"
38 #include "edlsession.h"
40 #include "filesystem.h"
41 #include "floatauto.h"
42 #include "floatautos.h"
43 #include "framecache.h"
44 #include "indexfile.h"
46 #include "localsession.h"
48 #include "mwindowgui.h"
49 #include "preferences.h"
50 #include "renderengine.h"
51 #include "resourcethread.h"
52 #include "resourcepixmap.h"
55 #include "timelinepane.h"
57 #include "trackcanvas.h"
58 #include "transportque.h"
61 #include "wavecache.h"
64 ResourcePixmap::ResourcePixmap(MWindow *mwindow,
70 : BC_Pixmap(gui, w, h)
74 this->mwindow = mwindow;
76 this->pane_number = pane_number;
77 startsource = edit->startsource;
78 data_type = edit->track->data_type;
80 source_framerate = edit->asset->frame_rate;
81 source_samplerate = edit->asset->sample_rate;
84 if( edit->nested_edl ) {
85 source_framerate = edit->nested_edl->session->frame_rate;
86 source_samplerate = edit->nested_edl->session->sample_rate;
89 project_framerate = edit->edl->session->frame_rate;
90 project_samplerate = edit->edl->session->sample_rate;
91 edit_id = edit->id; pixmap_w = w; pixmap_h = h;
94 ResourcePixmap::~ResourcePixmap()
99 void ResourcePixmap::reset()
111 void ResourcePixmap::resize(int w, int h)
113 int new_w = (w > get_w()) ? w : get_w();
114 int new_h = (h > get_h()) ? h : get_h();
116 BC_Pixmap::resize(new_w, new_h);
119 void ResourcePixmap::update_settings(Edit *edit,
120 int64_t edit_x, int64_t edit_w,
121 int64_t pixmap_x, int64_t pixmap_w, int64_t pixmap_h)
123 this->edit_id = edit->id;
124 this->edit_x = edit_x;
125 this->pixmap_x = pixmap_x;
126 this->pixmap_w = pixmap_w;
127 this->pixmap_h = pixmap_h;
129 startsource = edit->startsource;
131 source_framerate = edit->asset->frame_rate;
133 if( edit->nested_edl )
134 source_framerate = edit->nested_edl->session->frame_rate;
136 source_samplerate = edit->asset->sample_rate;
137 else if( edit->nested_edl )
138 source_samplerate = edit->nested_edl->session->sample_rate;
140 project_framerate = edit->edl->session->frame_rate;
141 project_samplerate = edit->edl->session->sample_rate;
142 zoom_sample = mwindow->edl->local_session->zoom_sample;
143 zoom_track = mwindow->edl->local_session->zoom_track;
144 zoom_y = mwindow->edl->local_session->zoom_y;
147 void ResourcePixmap::draw_data(TrackCanvas *canvas,
148 Edit *edit, int64_t edit_x, int64_t edit_w,
149 int64_t pixmap_x, int64_t pixmap_w, int64_t pixmap_h,
150 int mode, int indexes_only)
152 // Get new areas to fill in relative to pixmap
153 // Area to redraw relative to pixmap
157 // Ignore if called by resourcethread.
158 // if( mode == IGNORE_THREAD ) return;
161 if( edit->track->show_titles() )
162 y += mwindow->theme->get_image("title_bg_data")->get_h();
164 // If want indexes only & index can't be drawn, don't do anything.
166 int64_t index_zoom = 0;
167 Indexable *indexable = 0;
168 if( edit->asset ) indexable = edit->asset;
169 if( edit->nested_edl ) indexable = edit->nested_edl;
170 if( indexable && indexes_only ) {
171 IndexFile indexfile(mwindow, indexable);
172 if( !indexfile.open_index() ) {
173 index_zoom = indexable->index_state->index_zoom;
174 indexfile.close_index();
178 if( data_type == TRACK_AUDIO ) {
179 double asset_over_session = (double)indexable->get_sample_rate() /
180 mwindow->edl->session->sample_rate;
181 if( index_zoom <= mwindow->edl->local_session->zoom_sample *
191 /* Incremental drawing is not possible with resource thread */
194 refresh_w = pixmap_w;
196 // Draw background image
198 mwindow->theme->draw_resource_bg(canvas,
205 refresh_x + refresh_w,
206 mwindow->edl->local_session->zoom_track + y);
207 //printf("ResourcePixmap::draw_data 70\n");
210 // Draw media which already exists
211 Track *track = edit->track;
213 switch( track->data_type )
216 draw_audio_resource(canvas,
223 draw_video_resource(canvas,
235 draw_subttl_resource(canvas,
246 VFrame *ResourcePixmap::change_title_color(VFrame *title_bg, int color, int bg_color)
248 if( color < 0 || color == bg_color ) return title_bg;
249 int colormodel = title_bg->get_color_model();
250 int bpp = BC_CModels::calculate_pixelsize(colormodel);
251 int tw = title_bg->get_w(), th = title_bg->get_h();
252 VFrame *title_bar = new VFrame(tw, th, colormodel);
253 uint8_t br = (bg_color>>16), cr = (color>>16);
254 uint8_t bg = (bg_color>>8), cg = (color>>8);
255 uint8_t bb = (bg_color>>0), cb = (color>>0);
256 int dr = cr-br, dg = cg-bg, db = cb-bb;
257 uint8_t **bar_rows = title_bar->get_rows();
258 uint8_t **rows = title_bg->get_rows();
259 for( int y=0; y<th; ++y ) {
260 uint8_t *bp = rows[y], *cp = bar_rows[y];
261 for( int x=0; x<tw; ++x ) {
265 if( bpp > 3 ) *cp++ = *bp++;
271 void ResourcePixmap::draw_title(TrackCanvas *canvas,
272 Edit *edit, int64_t edit_x, int64_t edit_w,
273 int64_t pixmap_x, int64_t pixmap_w)
275 // coords relative to pixmap
276 int64_t total_x = edit_x - pixmap_x, total_w = edit_w;
277 int64_t x = total_x, w = total_w;
278 int left_margin = 10;
280 if( x < 0 ) { w -= -x; x = 0; }
281 if( w > pixmap_w ) w -= w - pixmap_w;
283 VFrame *title_bg = mwindow->theme->get_image("title_bg_data");
284 VFrame *title_bar = title_bg;
285 int color = mwindow->get_title_color(edit), bg_color = -1;
287 bg_color = mwindow->theme->get_color_title_bg();
288 if( bg_color == color ) color = -1;
290 if( color >= 0 && color != bg_color )
291 title_bar = change_title_color(title_bg, color, bg_color);
292 canvas->draw_3segmenth(x, 0, w, total_x, total_w, title_bar, this);
293 if( title_bar != title_bg ) delete title_bar;
295 // if( total_x > -BC_INFINITY ) {
296 char title[BCTEXTLEN];
297 edit->get_title(title);
298 canvas->set_color(mwindow->theme->title_color);
299 canvas->set_font(mwindow->theme->title_font);
301 // Justify the text on the left boundary of the edit if it is visible.
302 // Otherwise justify it on the left side of the screen.
303 int text_x = total_x + left_margin;
304 text_x = MAX(left_margin, text_x);
305 //printf("ResourcePixmap::draw_title 1 %d\n", text_x);
306 canvas->draw_text(text_x,
307 canvas->get_text_ascent(mwindow->theme->title_font) + 2,
308 title, strlen(title), this);
313 // Need to draw one more x
314 void ResourcePixmap::draw_audio_resource(TrackCanvas *canvas, Edit *edit, int x, int w)
317 if( !edit->asset && !edit->nested_edl ) return;
318 Indexable *indexable = 0;
319 if( edit->asset ) indexable = edit->asset;
320 if( edit->nested_edl ) indexable = edit->nested_edl;
321 // printf("ResourcePixmap::draw_audio_resource %d x=%d w=%d\n", __LINE__, x, w);
324 IndexState *index_state = indexable->index_state;
325 double asset_over_session = (double)indexable->get_sample_rate() /
326 mwindow->edl->session->sample_rate;
328 // Develop strategy for drawing
329 // printf("ResourcePixmap::draw_audio_resource %d %p %d\n",
332 // index_state->index_status);
333 switch( index_state->index_status )
335 case INDEX_NOTTESTED:
338 // Disabled. All files have an index.
339 // case INDEX_TOOSMALL:
340 // draw_audio_source(canvas, edit, x, w);
345 IndexFile indexfile(mwindow, indexable);
346 if( !indexfile.open_index() ) {
347 if( index_state->index_zoom >
348 mwindow->edl->local_session->zoom_sample *
349 asset_over_session ) {
350 //printf("ResourcePixmap::draw_audio_resource %d\n", __LINE__);
352 draw_audio_source(canvas, edit, x, w);
355 //printf("ResourcePixmap::draw_audio_resource %d\n", __LINE__);
356 indexfile.draw_index(canvas,
364 indexfile.close_index();
373 void ResourcePixmap::draw_audio_source(TrackCanvas *canvas, Edit *edit, int x, int w)
376 Indexable *indexable = edit->get_source();
377 int center_pixel = mwindow->edl->local_session->zoom_track / 2;
378 if( edit->track->show_titles() )
379 center_pixel += mwindow->theme->get_image("title_bg_data")->get_h();
380 int64_t scale_y = mwindow->edl->local_session->zoom_y;
381 int y_max = center_pixel + scale_y / 2 - 1;
383 double project_zoom = mwindow->edl->local_session->zoom_sample;
384 FloatAutos *speed_autos = !edit->track->has_speed() ? 0 :
385 (FloatAutos *)edit->track->automation->autos[AUTOMATION_SPEED];
386 int64_t edit_position = (x + pixmap_x - edit_x) * project_zoom;
387 int64_t start_position = edit->startsource;
388 start_position += !speed_autos ? edit_position :
389 speed_autos->automation_integral(edit->startproject, edit_position, PLAY_FORWARD);
390 int64_t end_position = edit->startsource;
391 edit_position = (x + w + pixmap_x - edit_x) * project_zoom;
392 end_position += !speed_autos ? edit_position :
393 speed_autos->automation_integral(edit->startproject, edit_position, PLAY_FORWARD);
395 double session_sample_rate = mwindow->edl->session->sample_rate;
396 double asset_over_session = (double)indexable->get_sample_rate() / session_sample_rate;
397 start_position *= asset_over_session;
398 end_position *= asset_over_session;
399 int sample_size = end_position - start_position;
400 if( sample_size < 0 ) sample_size = 0;
401 int source_samples = sample_size + 1;
403 // Single sample zoom
404 if( mwindow->edl->local_session->zoom_sample == 1 ) {
407 canvas->set_color(mwindow->theme->audio_color);
409 if( indexable->is_asset ) {
410 mwindow->gui->unlock_window();
411 File *source = mwindow->audio_cache->check_out(edit->asset, mwindow->edl);
412 mwindow->gui->lock_window("draw_audio_source");
415 printf(_("ResourcePixmap::draw_audio_source: failed to check out %s for drawing.\n"), edit->asset->path);
419 source->set_audio_position(start_position);
420 source->set_channel(edit->channel);
421 buffer = new Samples(source_samples);
422 result = source->read_samples(buffer, source_samples);
423 mwindow->audio_cache->check_in(edit->asset);
426 if( mwindow->gui->render_engine &&
427 mwindow->gui->render_engine_id != indexable->id ) {
428 delete mwindow->gui->render_engine;
429 mwindow->gui->render_engine = 0;
432 if( !mwindow->gui->render_engine ) {
433 TransportCommand command;
434 command.command = NORMAL_FWD;
435 command.get_edl()->copy_all(edit->nested_edl);
436 command.change_type = CHANGE_ALL;
437 command.realtime = 0;
438 mwindow->gui->render_engine = new RenderEngine(0,
439 mwindow->preferences, 0, 0);
440 mwindow->gui->render_engine_id = edit->nested_edl->id;
441 mwindow->gui->render_engine->set_acache(mwindow->audio_cache);
442 mwindow->gui->render_engine->arm_command(&command);
445 if( mwindow->gui->render_engine->arender ) {
446 Samples *buffers[MAX_CHANNELS];
447 memset(buffers, 0, sizeof(buffers));
448 int nch = indexable->get_audio_channels(), ch = edit->channel;
449 for( int i=0; i<nch; ++i )
450 buffers[i] = new Samples(source_samples);
451 mwindow->gui->render_engine->arender->process_buffer(
452 buffers, source_samples, start_position);
453 for( int i=0; i<nch; ++i )
454 if( i != ch ) delete buffers[i];
455 buffer = buffers[ch];
460 double *samples = buffer->get_data();
461 int y1 = center_pixel - samples[0] * scale_y / 2;
462 int y2 = CLIP(y1, 0, y_max);
464 for( int x0=0; x0<w; ++x0 ) {
465 int x1 = x0 + x, x2 = x1 + 1;
466 edit_position = (x1 + pixmap_x - edit_x) * project_zoom;
467 int64_t speed_position = edit->startsource;
468 speed_position += !speed_autos ? edit_position :
469 speed_autos->automation_integral(
470 edit->startproject, edit_position, PLAY_FORWARD);
471 int j = speed_position * asset_over_session - start_position;
472 CLAMP(j, 0, sample_size);
474 y1 = center_pixel - samples[j] * scale_y / 2;
475 y2 = CLIP(y1, 0, y_max);
476 //printf("ResourcePixmap::draw_audio_source %d %d %d\n", __LINE__, y1, y2);
477 canvas->draw_line(x0, y0, x2, y2, this);
484 edit_position = (x + pixmap_x - edit_x) * project_zoom;
485 int64_t speed_position = edit->startsource;
486 speed_position += !speed_autos ? edit_position :
487 speed_autos->automation_integral(
488 edit->startproject, edit_position, PLAY_FORWARD);
489 int64_t next_position = asset_over_session * speed_position;
490 // Multiple sample zoom
491 int first_pixel = 1, prev_y1 = -1, prev_y2 = y_max;
492 canvas->set_color(mwindow->theme->audio_color);
495 // Draw each pixel from the cache
496 //printf("ResourcePixmap::draw_audio_source %d x=%d w=%d\n", __LINE__, x, w);
497 for( int x2=x+w; x<x2; ++x ) {
498 int64_t prev_position = next_position;
499 edit_position = (x + pixmap_x - edit_x) * project_zoom;
500 speed_position = edit->startsource;
501 speed_position += !speed_autos ? edit_position :
502 speed_autos->automation_integral(
503 edit->startproject, edit_position, PLAY_FORWARD);
504 next_position = speed_position * asset_over_session;
505 // Starting sample of pixel relative to asset rate.
506 WaveCacheItem *item = mwindow->wave_cache->get_wave(indexable->id,
507 edit->channel, prev_position, next_position);
509 //printf("ResourcePixmap::draw_audio_source %d\n", __LINE__);
510 int y_lo = (int)(center_pixel - item->low * scale_y / 2);
511 int y1 = CLIP(y_lo, 0, y_max);
512 int y_hi = (int)(center_pixel - item->high * scale_y / 2);
513 int y2 = CLIP(y_hi, 0, y_max);
515 y_lo = MIN(y1,prev_y2);
516 y_hi = MAX(y2,prev_y1);
520 y_lo = y1; y_hi = y2;
522 prev_y1 = y1; prev_y2 = y2;
523 canvas->draw_line(x, y_lo, x, y_hi, this);
524 //printf("ResourcePixmap::draw_audio_source %d %d %d %d\n", __LINE__, x, y1, y2);
525 mwindow->wave_cache->unlock();
528 //printf("ResourcePixmap::draw_audio_source %d\n", __LINE__);
529 gui->resource_thread->add_wave(this,
530 canvas->pane->number, indexable, x,
531 edit->channel, prev_position, next_position);
532 first_pixel = 1; prev_y1 = -1; prev_y2 = y_max;
537 canvas->test_timer();
540 void ResourcePixmap::draw_wave(TrackCanvas *canvas,
541 int x, double high, double low)
544 if( mwindow->edl->session->show_titles )
545 top_pixel += mwindow->theme->get_image("title_bg_data")->get_h();
546 int center_pixel = mwindow->edl->local_session->zoom_track / 2 + top_pixel;
547 int bottom_pixel = top_pixel + mwindow->edl->local_session->zoom_track;
548 int y1 = (int)(center_pixel -
549 low * mwindow->edl->local_session->zoom_y / 2);
550 int y2 = (int)(center_pixel -
551 high * mwindow->edl->local_session->zoom_y / 2);
552 CLAMP(y1, top_pixel, bottom_pixel);
553 CLAMP(y2, top_pixel, bottom_pixel);
554 canvas->set_color(mwindow->theme->audio_color);
555 canvas->draw_line(x, y1, x, y2, this);
559 void ResourcePixmap::draw_video_resource(TrackCanvas *canvas,
560 Edit *edit, int64_t edit_x, int64_t edit_w, int64_t pixmap_x, int64_t pixmap_w,
561 int refresh_x, int refresh_w, int mode)
564 //BC_Signals::dump_stack();
566 // pixels spanned by a picon
567 int64_t picon_w = Units::round(edit->picon_w());
568 int64_t picon_h = edit->picon_h();
569 // if( picon_w <= 0 || picon_w > edit_w ) return;
570 // Don't draw video if picon is empty, or edit only hairline
571 if( picon_w < 1 || edit_w < 2 ) return;
572 // or bigger than edit and fills at less than 1.5 percent timeline
573 if( picon_w > edit_w && edit_w < canvas->get_w()/64 ) return;
575 // Frames spanned by a picon
576 double frame_w = edit->frame_w();
577 // pixels spanned by a frame
578 if( frame_w < picon_w ) frame_w = picon_w;
579 // Current pixel relative to pixmap
581 if( edit->track->show_titles() )
582 y += mwindow->theme->get_image("title_bg_data")->get_h();
584 // Frame in project touched by current pixel
585 FloatAutos *speed_autos = !edit->track->has_speed() ? 0 :
586 (FloatAutos *)edit->track->automation->autos[AUTOMATION_SPEED];
587 Indexable *indexable = edit->get_source();
588 double session_sample_rate = mwindow->edl->session->sample_rate;
589 double project_zoom = mwindow->edl->local_session->zoom_sample / session_sample_rate;
590 int skip_frames = Units::to_int64(((int64_t)refresh_x + pixmap_x - edit_x) / frame_w);
591 int x = Units::to_int64(skip_frames * frame_w) + edit_x - pixmap_x;
593 // Draw only cached frames
594 while( x < refresh_x + refresh_w ) {
595 int64_t edit_position =
596 edit->track->to_units((x + pixmap_x - edit_x) * project_zoom, 0);
597 int64_t speed_position = edit->startsource;
598 speed_position += !speed_autos ? edit_position :
599 speed_autos->automation_integral(
600 edit->startproject, edit_position, PLAY_FORWARD);
601 VFrame *picon_frame = indexable->id < 0 ? 0 :
602 mwindow->frame_cache->get_frame_ptr(speed_position, edit->channel,
603 mwindow->edl->session->frame_rate, BC_RGB888,
604 picon_w, picon_h, indexable->id);
606 draw_vframe(picon_frame, x, y, picon_w, picon_h, 0, 0);
607 mwindow->frame_cache->unlock();
609 else if( mode != IGNORE_THREAD ) {
610 // Set picon thread to draw in background
611 // printf("ResourcePixmap::draw_video_resource %d %d %lld\n",
612 // __LINE__, mwindow->frame_cache->total(), source_frame);
613 gui->resource_thread->add_picon(this, canvas->pane->number, x, y,
614 picon_w, picon_h, mwindow->edl->session->frame_rate,
615 speed_position, edit->channel, indexable);
618 canvas->test_timer();
624 void ResourcePixmap::draw_subttl_resource(TrackCanvas *canvas, Edit *edit, int x, int w)
626 SEdit *sedit = (SEdit *)edit;
627 char *text = sedit->get_text();
628 if( !*text || w < 10 ) return;
629 int center_pixel = canvas->resource_h() / 2;
630 if( edit->track->show_titles() )
631 center_pixel += mwindow->theme->get_image("title_bg_data")->get_h();
632 int64_t scale_y = mwindow->edl->local_session->zoom_y;
634 if( x0 < 0 ) x0 = -x0;
635 int x1 = (int)(pixmap_x - x0 + x);
636 int y_max = center_pixel + scale_y / 2 - 1;
637 int font = MEDIUMFONT, color = WHITE;
638 canvas->set_font(font);
639 canvas->set_color(color);
640 int ch = canvas->get_text_height(font);
641 int hh = canvas->get_text_height(font,text) + ch/2;
642 int y1 = y_max - hh - 10;
644 canvas->draw_text(x1, y1, text, -1, this);
647 void ResourcePixmap::dump()
649 printf("ResourcePixmap %p\n", this);
650 printf(" edit %jx edit_x %jd pixmap_x %jd pixmap_w %jd visible %d\n",
651 edit_id, edit_x, pixmap_x, pixmap_w, visible);