3 * Copyright (C) 2015 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "audiodevice.h"
30 #include "edlsession.h"
32 #include "filesystem.h"
33 #include "formattools.h"
34 #include "indexfile.h"
36 #include "localsession.h"
37 #include "mainerror.h"
38 #include "mainindexes.h"
39 #include "mainprogress.h"
43 #include "mwindowgui.h"
44 #include "packagedispatcher.h"
45 #include "packagerenderer.h"
48 #include "preferences.h"
55 #define HEIGHT yS(360)
58 ConvertRender::ConvertRender(MWindow *mwindow)
61 this->mwindow = mwindow;
65 progress_timer = new Timer;
67 counter_lock = new Mutex("ConvertDialog::counter_lock");
70 failed = 0; canceled = 0;
77 ConvertRender::~ConvertRender()
82 renderer->set_result(1);
90 delete progress_timer;
91 delete convert_progress;
93 format_asset->remove_user();
97 void ConvertRender::reset()
99 for( int i=0,n=orig_idxbls.size(); i<n; ++i )
100 orig_idxbls[i]->remove_user();
101 orig_idxbls.remove_all();
102 for( int i=0,n=orig_copies.size(); i<n; ++i )
103 orig_copies[i]->remove_user();
104 orig_copies.remove_all();
105 for( int i=0,n=needed_idxbls.size(); i<n; ++i )
106 needed_idxbls[i]->remove_user();
107 needed_idxbls.remove_all();
108 for( int i=0,n=needed_copies.size(); i<n; ++i )
109 needed_copies[i]->remove_user();
110 needed_copies.remove_all();
113 void ConvertRender::to_convert_path(char *new_path, Indexable *idxbl)
116 char *bp = idxbl->path, *cp = strrchr(bp, '/');
118 char filename[BCTEXTLEN], proxy_path[BCTEXTLEN];
119 strcpy(filename, bp);
120 File::getenv_path(proxy_path, mwindow->preferences->nested_proxy_path);
121 sprintf(new_path, "%s/%s", proxy_path, filename);
124 strcpy(new_path, idxbl->path);
125 int n = strlen(suffix);
126 char *ep = new_path + strlen(new_path);
127 char *sfx = strrchr(new_path, '.');
129 // insert suffix, path.sfx => path+suffix-sfx.ext
130 char *bp = ep, *cp = (ep += n);
131 while( --bp > sfx ) *--cp = *bp;
135 // insert suffix, path => path+suffix.ext
138 for( const char *cp=suffix; --n>=0; ++cp ) *sfx++ = *cp;
140 const char *ext = format_asset->format == FILE_FFMPEG ?
141 format_asset->fformat :
142 File::get_tag(format_asset->format);
143 while( *ext ) *ep++ = *ext++;
147 int ConvertRender::from_convert_path(char *path, Indexable *idxbl)
149 strcpy(path, idxbl->path);
150 char *ep = path + strlen(path);
151 const char *ext = format_asset->format == FILE_FFMPEG ?
152 format_asset->fformat :
153 File::get_tag(format_asset->format);
154 const char *rp = ext + strlen(ext);
157 } while( rp>=ext && ep>=path && *ep == *rp );
158 if( rp >= ext || ep < path && *--ep != '.' ) return 1; // didnt find .ext
159 int n = strlen(suffix), len = 0;
160 char *lp = ep - n; // <suffix-chars>+.ext
161 for( ; --lp>=path ; ++len ) {
162 if( strncmp(lp, suffix, n) ) continue;
163 if( !len || lp[n] == '-' ) break;
165 if( lp < path ) return 1; // didnt find suffix
166 if( *(rp=lp+n) == '-' ) {
167 // remove suffix, path+suffix-sfx.ext >= path.sfx
169 while( rp < ep ) *lp++ = *rp++;
171 // remove suffix, path+suffix.ext => path
176 double ConvertRender::get_video_length(Indexable *idxbl)
178 int64_t video_frames = idxbl->get_video_frames();
179 double frame_rate = idxbl->get_frame_rate();
180 if( video_frames < 0 && mwindow->edl->session->si_useduration )
181 video_frames = mwindow->edl->session->si_duration * frame_rate;
182 if( video_frames < 0 ) video_frames = 1;
183 return !video_frames ? 0 : video_frames / frame_rate;
186 double ConvertRender::get_audio_length(Indexable *idxbl)
188 int64_t audio_samples = idxbl->get_audio_samples();
189 return !audio_samples ? 0 :
190 (double)audio_samples / idxbl->get_sample_rate();
193 double ConvertRender::get_length(Indexable *idxbl)
195 return bmax(get_video_length(idxbl), get_audio_length(idxbl));
198 int ConvertRender::match_format(Asset *asset)
201 return format_asset->audio_data == asset->audio_data &&
202 format_asset->video_data == asset->video_data ? 1 : 0;
205 EDL *ConvertRender::convert_edl(EDL *edl, Indexable *idxbl)
207 Asset *copy_asset = edl->assets->get_asset(idxbl->path);
208 if( !copy_asset ) return 0;
209 if( !copy_asset->layers && !copy_asset->channels ) return 0;
210 // make a clip from 1st video track and audio tracks
211 EDL *copy_edl = new EDL(edl);
212 copy_edl->create_objects();
213 copy_edl->set_path(copy_asset->path);
214 char path[BCTEXTLEN];
215 FileSystem fs; fs.extract_name(path, copy_asset->path);
216 strcpy(copy_edl->local_session->clip_title, path);
217 strcpy(copy_edl->local_session->clip_notes, _("Transcode clip"));
219 double video_length = get_video_length(idxbl);
220 double audio_length = get_audio_length(idxbl);
221 copy_edl->session->video_tracks =
222 video_length > 0 ? 1 : 0;
223 copy_edl->session->audio_tracks =
224 audio_length > 0 ? copy_asset->channels : 0;
226 copy_edl->create_default_tracks();
227 Track *current = copy_edl->session->video_tracks ?
228 copy_edl->tracks->first : 0;
229 for( int vtrack=0; current; current=NEXT ) {
230 if( current->data_type != TRACK_VIDEO ) continue;
231 current->insert_asset(copy_asset, 0, video_length, 0, vtrack);
234 current = copy_edl->session->audio_tracks ?
235 copy_edl->tracks->first : 0;
236 for( int atrack=0; current; current=NEXT ) {
237 if( current->data_type != TRACK_AUDIO ) continue;
238 current->insert_asset(copy_asset, 0, audio_length, 0, atrack);
239 Autos *pan_autos = current->automation->autos[AUTOMATION_PAN];
240 PanAuto *pan_auto = (PanAuto*)pan_autos->get_auto_for_editing(-1);
241 for( int i=0; i < MAXCHANNELS; ++i ) pan_auto->values[i] = 0;
242 pan_auto->values[atrack++] = 1;
243 if( atrack >= MAXCHANNELS ) atrack = 0;
245 copy_edl->folder_no = AW_MEDIA_FOLDER;
249 int ConvertRender::add_original(EDL *edl, Indexable *idxbl)
251 char new_path[BCTEXTLEN];
252 // if idxbl already a convert
253 if( !from_convert_path(new_path, idxbl) ) return 0;
254 // don't convert if not readable
255 if( idxbl->is_asset && access(idxbl->path, R_OK) ) return 0;
256 // found readable unconverted asset
257 to_convert_path(new_path, idxbl);
258 // add to orig_idxbls & orig_copies if it isn't already there.
260 for( int i = 0; !got_it && i<orig_copies.size(); ++i )
261 got_it = !strcmp(orig_copies[i]->path, new_path);
262 if( got_it ) return 0;
264 orig_idxbls.append(idxbl);
266 Asset *convert = edl->assets->get_asset(new_path);
268 convert = new Asset(new_path);
270 if( fs.get_size(new_path) > 0 ) {
271 // copy already on disk
272 int64_t orig_mtime = fs.get_date(idxbl->path);
273 int64_t convert_mtime = fs.get_date(new_path);
274 // render needed if it is too old
275 if( orig_mtime < convert_mtime ) {
277 int ret = file.open_file(mwindow->preferences, convert, 1, 0);
278 // render not needed if can use copy
279 if( ret == FILE_OK ) {
280 if( match_format(file.asset) ) {
281 mwindow->mainindexes->add_indexable(convert);
282 mwindow->mainindexes->start_build();
291 else if( match_format(convert) ) {
292 // dont render if copy already an assets
299 eprintf(_("transcode target file exists but is incorrect format:\n%s\n"
300 "remove file from disk before transcode to new format.\n"), new_path);
303 orig_copies.append(convert);
305 convert->copy_format(format_asset, 0);
306 // new compression parameters
307 convert->video_data = format_asset->video_data;
308 if( convert->video_data ) {
310 convert->width = idxbl->get_w();
311 if( convert->width & 1 ) ++convert->width;
312 convert->actual_width = convert->width;
313 convert->height = idxbl->get_h();
314 if( convert->height & 1 ) ++convert->height;
315 convert->actual_height = convert->height;
316 convert->frame_rate = mwindow->edl->session->frame_rate;
318 convert->audio_data = format_asset->audio_data;
319 if( convert->audio_data ) {
320 convert->sample_rate = mwindow->edl->session->sample_rate;
322 convert->folder_no = AW_MEDIA_FOLDER;
323 add_needed(idxbl, convert);
328 void ConvertRender::add_needed(Indexable *idxbl, Asset *convert)
330 needed_idxbls.append(idxbl);
332 needed_copies.append(convert);
337 int ConvertRender::is_canceled()
339 return progress->is_cancelled();
342 int ConvertRender::find_convertable_assets(EDL *edl)
345 Asset *orig_asset = edl->assets->first;
347 for( ; orig_asset; orig_asset=orig_asset->next ) {
348 int ret = add_original(edl, orig_asset);
349 if( ret < 0 ) return -1;
355 void ConvertRender::set_format(Asset *asset, const char *suffix, int to_proxy)
357 delete [] this->suffix;
358 this->suffix = cstrdup(suffix);
360 format_asset = new Asset();
361 format_asset->copy_from(asset, 0);
362 this->to_proxy = to_proxy;
365 void ConvertRender::start_convert(float beep, int remove_originals)
368 this->remove_originals = remove_originals;
372 void ConvertRender::run()
374 mwindow->stop_brender();
377 failed = 0; canceled = 0;
379 progress_timer->update();
382 for( int i=0; !failed && !canceled && i<needed_copies.size(); ++i )
385 canceled = progress->is_cancelled();
386 printf(_("convert: failed=%d canceled=%d\n"), failed, canceled);
387 double elapsed_time = progress_timer->get_scaled_difference(1);
389 char elapsed[BCSTRLEN], text[BCSTRLEN];
390 Units::totext(elapsed, elapsed_time, TIME_HMS2);
391 printf(_("TranscodeRender::run: done in %s\n"), elapsed);
393 strcpy(text, _("transcode cancelled"));
395 strcpy(text, _("transcode failed"));
397 sprintf(text, _("transcode %d files, render time %s"),
398 needed_copies.size(), elapsed);
403 mwindow->finish_convert(remove_originals);
405 else if( !canceled ) {
406 eprintf(_("Error making transcode."));
409 if( !canceled && beep > 0 ) {
411 mwindow->beep(4000., 0.5, beep);
413 mwindow->beep(1000., 0.5, beep);
415 mwindow->beep(4000., 0.5, beep);
418 mwindow->beep(2000., 2.0, beep);
420 mwindow->restart_brender();
423 void ConvertRender::start_progress()
426 for( int i = 0; i < needed_idxbls.size(); i++ ) {
427 Indexable *orig_idxbl = needed_idxbls[i];
428 double length = get_length(orig_idxbl);
431 int64_t total_samples = total_len * format_asset->sample_rate;
432 mwindow->gui->lock_window("Render::start_progress");
433 progress = mwindow->mainprogress->
434 start_progress(_("Transcode files..."), total_samples);
435 mwindow->gui->unlock_window();
436 convert_progress = new ConvertProgress(mwindow, this);
437 convert_progress->start();
440 void ConvertRender::stop_progress(const char *msg)
442 delete convert_progress; convert_progress = 0;
443 mwindow->gui->lock_window("ConvertRender::stop_progress");
445 mwindow->mainprogress->end_progress(progress);
447 mwindow->gui->show_message(msg);
448 mwindow->gui->update_default_message();
449 mwindow->gui->unlock_window();
453 ConvertPackageRenderer::ConvertPackageRenderer(ConvertRender *render)
456 this->render = render;
459 ConvertPackageRenderer::~ConvertPackageRenderer()
463 int ConvertPackageRenderer::get_master()
468 int ConvertPackageRenderer::get_result()
470 return render->result;
473 void ConvertPackageRenderer::set_result(int value)
476 render->result = value;
479 void ConvertPackageRenderer::set_progress(int64_t value)
481 render->counter_lock->lock("ConvertPackageRenderer::set_progress");
482 // Increase total rendered for all nodes
483 render->total_rendered += value;
484 render->counter_lock->unlock();
487 int ConvertPackageRenderer::progress_cancelled()
489 return render->progress && render->progress->is_cancelled();
492 ConvertProgress::ConvertProgress(MWindow *mwindow, ConvertRender *render)
495 this->mwindow = mwindow;
496 this->render = render;
500 ConvertProgress::~ConvertProgress()
506 void ConvertProgress::run()
508 Thread::disable_cancel();
510 if( render->total_rendered != last_value ) {
511 render->progress->update(render->total_rendered);
512 last_value = render->total_rendered;
515 Thread::enable_cancel();
517 Thread::disable_cancel();
521 void ConvertRender::create_copy(int i)
523 Indexable *orig_idxbl = needed_idxbls[i];
524 Asset *needed_copy = needed_copies[i];
525 EDL *edl = convert_edl(mwindow->edl, orig_idxbl);
526 double length = get_length(orig_idxbl);
527 renderer = new ConvertPackageRenderer(this);
528 renderer->initialize(mwindow, edl, mwindow->preferences, needed_copy);
529 PackageDispatcher dispatcher;
530 dispatcher.create_packages(mwindow, edl, mwindow->preferences,
531 SINGLE_PASS, needed_copy, 0, length, 0);
532 RenderPackage *package = dispatcher.get_package(0);
533 if( !renderer->render_package(package) ) {
534 Asset *asset = mwindow->edl->assets->update(needed_copy);
535 mwindow->mainindexes->add_indexable(asset);
536 mwindow->mainindexes->start_build();
540 delete renderer; renderer = 0;
544 ConvertWindow::ConvertWindow(MWindow *mwindow, ConvertDialog *dialog, int x, int y)
545 : BC_Window(_(PROGRAM_NAME ": Transcode settings"), x, y, WIDTH, HEIGHT,
548 this->mwindow = mwindow;
549 this->dialog = dialog;
551 // *** CONTEXT_HELP ***
552 context_help_set_keyword("Transcode");
555 ConvertWindow::~ConvertWindow()
557 lock_window("ConvertWindow::~ConvertWindow");
563 void ConvertWindow::create_objects()
565 lock_window("ConvertWindow::create_objects");
566 int margin = mwindow->theme->widget_border;
567 int lmargin = margin + xS(10);
570 int y = margin + yS(10);
573 add_subwindow(text = new BC_Title(x, y,
574 _("Render untagged assets and replace in project")));
575 y += text->get_h() + margin + yS(10);
577 y += BC_Title::calculate_h(this, _("Tag suffix:")) + margin + yS(10);
580 format_tools = new ConvertFormatTools(mwindow, this, dialog->asset);
581 format_tools->create_objects(x, y, 1, 1, 1, 1, 0, 1, 0, 1, // skip the path
585 add_subwindow(text = new BC_Title(x, y1, _("Tag suffix:")));
586 x = format_tools->format_text->get_x();
587 add_subwindow(suffix_text = new ConvertSuffixText(this, dialog, x, y1));
589 y += margin + yS(10);
591 add_subwindow(remove_originals = new ConvertRemoveOriginals(this, x, y));
593 y += remove_originals->get_h() + margin;
594 add_subwindow(to_proxy_path = new ConvertToProxyPath(this, x, y));
595 y += to_proxy_path->get_h() + margin;
597 add_subwindow(beep_on_done = new ConvertBeepOnDone(this, x, y));
598 x += beep_on_done->get_w() + margin + xS(10);
599 add_subwindow(new BC_Title(x, y+yS(10), _("Beep on done volume")));
600 // y += beep_on_done->get_h() + margin;
602 add_subwindow(new BC_OKButton(this));
603 add_subwindow(new BC_CancelButton(this));
608 ConvertSuffixText::ConvertSuffixText(ConvertWindow *gui,
609 ConvertDialog *dialog, int x, int y)
610 : BC_TextBox(x, y, 160, 1, dialog->suffix)
613 this->dialog = dialog;
616 ConvertSuffixText::~ConvertSuffixText()
620 int ConvertSuffixText::handle_event()
622 strcpy(dialog->suffix, get_text());
626 ConvertFormatTools::ConvertFormatTools(MWindow *mwindow, ConvertWindow *gui, Asset *asset)
627 : FormatTools(mwindow, gui, asset)
632 void ConvertFormatTools::update_format()
634 asset->save_defaults(mwindow->defaults, "CONVERT_", 1, 1, 0, 0, 0);
635 FormatTools::update_format();
639 ConvertMenuItem::ConvertMenuItem(MWindow *mwindow)
640 : BC_MenuItem(_("Transcode..."), _("Alt-e"), 'e')
642 this->mwindow = mwindow;
646 ConvertMenuItem::~ConvertMenuItem()
651 void ConvertMenuItem::create_objects()
653 dialog = new ConvertDialog(mwindow);
656 int ConvertMenuItem::handle_event()
658 mwindow->gui->unlock_window();
660 mwindow->gui->lock_window("ConvertMenuItem::handle_event");
665 ConvertDialog::ConvertDialog(MWindow *mwindow)
667 this->mwindow = mwindow;
670 strcpy(suffix, ".transcode");
671 // quicker than some, not as good as others
672 asset->format = FILE_FFMPEG;
673 strcpy(asset->fformat, "mp4");
674 strcpy(asset->vcodec, "h264.mp4");
675 asset->ff_video_bitrate = 2560000;
676 asset->video_data = 1;
677 strcpy(asset->acodec, "h264.mp4");
678 asset->ff_audio_bitrate = 256000;
679 asset->audio_data = 1;
680 remove_originals = 1;
685 ConvertDialog::~ConvertDialog()
688 asset->remove_user();
691 BC_Window* ConvertDialog::new_gui()
693 asset->format = FILE_FFMPEG;
694 asset->frame_rate = mwindow->edl->session->frame_rate;
695 asset->sample_rate = mwindow->edl->session->sample_rate;
696 asset->load_defaults(mwindow->defaults, "CONVERT_", 1, 1, 0, 1, 0);
697 remove_originals = mwindow->defaults->get("CONVERT_REMOVE_ORIGINALS", remove_originals);
698 beep = mwindow->defaults->get("CONVERT_BEEP", beep);
699 to_proxy = mwindow->defaults->get("CONVERT_TO_PROXY", to_proxy);
700 mwindow->defaults->get("CONVERT_SUFFIX", suffix);
701 mwindow->gui->lock_window("ConvertDialog::new_gui");
703 mwindow->gui->get_abs_cursor(cx, cy);
704 gui = new ConvertWindow(mwindow, this, cx - WIDTH/2, cy - HEIGHT/2);
705 gui->create_objects();
706 mwindow->gui->unlock_window();
710 void ConvertDialog::handle_close_event(int result)
713 mwindow->defaults->update("CONVERT_SUFFIX", suffix);
714 mwindow->defaults->update("CONVERT_REMOVE_ORIGINALS", remove_originals);
715 mwindow->defaults->update("CONVERT_BEEP", beep);
716 mwindow->defaults->update("CONVERT_TO_PROXY", to_proxy);
717 asset->save_defaults(mwindow->defaults, "CONVERT_", 1, 1, 0, 1, 0);
718 mwindow->start_convert(asset, suffix, beep, to_proxy, remove_originals);
722 ConvertRemoveOriginals::ConvertRemoveOriginals(ConvertWindow *gui, int x, int y)
723 : BC_CheckBox(x, y, gui->dialog->remove_originals, _("Remove originals from project"))
728 ConvertRemoveOriginals::~ConvertRemoveOriginals()
732 int ConvertRemoveOriginals::handle_event()
734 gui->dialog->remove_originals = get_value();
738 ConvertToProxyPath::ConvertToProxyPath(ConvertWindow *gui, int x, int y)
739 : BC_CheckBox(x, y, gui->dialog->to_proxy, _("Into Nested Proxy directory"))
744 ConvertToProxyPath::~ConvertToProxyPath()
748 int ConvertToProxyPath::handle_event()
750 gui->dialog->to_proxy = get_value();
754 ConvertBeepOnDone::ConvertBeepOnDone(ConvertWindow *gui, int x, int y)
755 : BC_FPot(x, y, gui->dialog->beep*100.f, 0.f, 100.f)
760 int ConvertBeepOnDone::handle_event()
762 gui->dialog->beep = get_value()/100.f;