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(320)
58 ConvertRender::ConvertRender(MWindow *mwindow, const char *suffix)
61 this->mwindow = mwindow;
62 this->suffix = cstrdup(suffix);
65 progress_timer = new Timer;
67 counter_lock = new Mutex("ConvertDialog::counter_lock");
70 failed = 0; canceled = 0;
75 ConvertRender::~ConvertRender()
80 delete progress_timer;
81 delete convert_progress;
83 format_asset->remove_user();
87 void ConvertRender::reset()
89 for( int i=0,n=orig_idxbls.size(); i<n; ++i )
90 orig_idxbls[i]->remove_user();
91 orig_idxbls.remove_all();
92 for( int i=0,n=orig_copies.size(); i<n; ++i )
93 orig_copies[i]->remove_user();
94 orig_copies.remove_all();
95 for( int i=0,n=needed_idxbls.size(); i<n; ++i )
96 needed_idxbls[i]->remove_user();
97 needed_idxbls.remove_all();
98 for( int i=0,n=needed_copies.size(); i<n; ++i )
99 needed_copies[i]->remove_user();
100 needed_copies.remove_all();
103 void ConvertRender::to_convert_path(char *new_path, Indexable *idxbl)
105 strcpy(new_path, idxbl->path);
106 int n = strlen(suffix);
107 char *ep = new_path + strlen(new_path);
108 char *sfx = strrchr(new_path, '.');
110 // insert suffix, path.sfx => path+suffix-sfx.ext
111 char *bp = ep, *cp = (ep += n);
112 while( --bp > sfx ) *--cp = *bp;
116 // insert suffix, path => path+suffix.ext
119 for( const char *cp=suffix; --n>=0; ++cp ) *sfx++ = *cp;
121 const char *ext = format_asset->format == FILE_FFMPEG ?
122 format_asset->fformat :
123 File::get_tag(format_asset->format);
124 while( *ext ) *ep++ = *ext++;
128 int ConvertRender::from_convert_path(char *path, Indexable *idxbl)
130 strcpy(path, idxbl->path);
131 char *ep = path + strlen(path);
132 const char *ext = format_asset->format == FILE_FFMPEG ?
133 format_asset->fformat :
134 File::get_tag(format_asset->format);
135 const char *rp = ext + strlen(ext);
138 } while( rp>=ext && ep>=path && *ep == *rp );
139 if( rp >= ext || ep < path && *--ep != '.' ) return 1; // didnt find .ext
140 int n = strlen(suffix), len = 0;
141 char *lp = ep - n; // <suffix-chars>+.ext
142 for( ; --lp>=path ; ++len ) {
143 if( strncmp(lp, suffix, n) ) continue;
144 if( !len || lp[n] == '-' ) break;
146 if( lp < path ) return 1; // didnt find suffix
147 if( *(rp=lp+n) == '-' ) {
148 // remove suffix, path+suffix-sfx.ext >= path.sfx
150 while( rp < ep ) *lp++ = *rp++;
152 // remove suffix, path+suffix.ext => path
157 EDL *ConvertRender::convert_edl(EDL *edl, Indexable *idxbl)
159 Asset *copy_asset = edl->assets->get_asset(idxbl->path);
160 if( !copy_asset ) return 0;
161 if( !copy_asset->layers && !copy_asset->channels ) return 0;
162 // make a clip from 1st video track and audio tracks
163 EDL *copy_edl = new EDL(edl);
164 copy_edl->create_objects();
165 copy_edl->set_path(copy_asset->path);
166 char path[BCTEXTLEN];
167 FileSystem fs; fs.extract_name(path, copy_asset->path);
168 strcpy(copy_edl->local_session->clip_title, path);
169 strcpy(copy_edl->local_session->clip_notes, _("Transcode clip"));
170 int64_t video_frames = idxbl->get_video_frames();
171 double frame_rate = idxbl->get_frame_rate();
172 double video_length = video_frames / frame_rate;
173 int64_t audio_samples = idxbl->get_audio_samples();
174 int sample_rate = idxbl->get_sample_rate();
175 double audio_length = (double)audio_samples / sample_rate;
176 copy_edl->session->video_tracks =
177 video_frames > 0 ? 1 : 0;
178 copy_edl->session->audio_tracks =
179 audio_samples > 0 ? copy_asset->channels : 0;
180 copy_edl->create_default_tracks();
181 Track *current = copy_edl->tracks->first;
182 for( int vtrack=0; current; current=NEXT ) {
183 if( current->data_type != TRACK_VIDEO ) continue;
184 current->insert_asset(copy_asset, 0, video_length, 0, vtrack);
187 current = copy_edl->tracks->first;
188 for( int atrack=0; current; current=NEXT ) {
189 if( current->data_type != TRACK_AUDIO ) continue;
190 current->insert_asset(copy_asset, 0, audio_length, 0, atrack);
191 Autos *pan_autos = current->automation->autos[AUTOMATION_PAN];
192 PanAuto *pan_auto = (PanAuto*)pan_autos->get_auto_for_editing(-1);
193 for( int i=0; i < MAXCHANNELS; ++i ) pan_auto->values[i] = 0;
194 pan_auto->values[atrack++] = 1;
195 if( atrack >= MAXCHANNELS ) atrack = 0;
197 copy_edl->folder_no = AW_MEDIA_FOLDER;
201 int ConvertRender::add_original(EDL *edl, Indexable *idxbl)
203 char new_path[BCTEXTLEN];
204 // if idxbl already a convert
205 if( !from_convert_path(new_path, idxbl) ) return 0;
206 // don't convert if not readable
207 if( idxbl->is_asset && access(idxbl->path, R_OK) ) return 0;
208 // found readable unconverted asset
209 to_convert_path(new_path, idxbl);
210 // add to orig_idxbls & orig_copies if it isn't already there.
212 for( int i = 0; !got_it && i<orig_copies.size(); ++i )
213 got_it = !strcmp(orig_copies[i]->path, new_path);
214 if( got_it ) return 0;
216 orig_idxbls.append(idxbl);
218 Asset *convert = edl->assets->get_asset(new_path);
220 convert = new Asset(new_path);
222 if( fs.get_size(new_path) > 0 ) {
223 // copy already on disk
224 int64_t orig_mtime = fs.get_date(idxbl->path);
225 int64_t convert_mtime = fs.get_date(new_path);
226 // render needed if it is too old
227 if( orig_mtime < convert_mtime ) {
229 int ret = file.open_file(mwindow->preferences, convert, 1, 0);
230 // render not needed if can use copy
231 if( ret == FILE_OK ) {
232 mwindow->mainindexes->add_next_asset(0, convert);
233 mwindow->mainindexes->start_build();
240 // dont render if copy already an assets
244 orig_copies.append(convert);
246 convert->copy_format(format_asset, 0);
247 // new compression parameters
248 int64_t video_frames = idxbl->get_video_frames();
249 if( video_frames > 1 ) convert->video_data = 1;
250 int64_t audio_samples = idxbl->get_audio_samples();
251 if( audio_samples > 0 ) convert->audio_data = 1;
252 convert->folder_no = AW_MEDIA_FOLDER;
254 convert->width = idxbl->get_w();
255 if( convert->width & 1 ) ++convert->width;
256 convert->actual_width = convert->width;
257 convert->height = idxbl->get_h();
258 if( convert->height & 1 ) ++convert->height;
259 convert->actual_height = convert->height;
260 convert->frame_rate = mwindow->edl->session->frame_rate;
261 convert->sample_rate = mwindow->edl->session->sample_rate;
262 add_needed(idxbl, convert);
267 void ConvertRender::add_needed(Indexable *idxbl, Asset *convert)
269 needed_idxbls.append(idxbl);
271 needed_copies.append(convert);
276 int ConvertRender::is_canceled()
278 return progress->is_cancelled();
281 int ConvertRender::find_convertable_assets(EDL *edl)
284 Asset *orig_asset = edl->assets->first;
286 for( ; orig_asset; orig_asset=orig_asset->next ) {
287 if( add_original(edl, orig_asset) )
293 void ConvertRender::set_format(Asset *asset)
296 format_asset = new Asset();
297 format_asset->copy_from(asset, 0);
300 void ConvertRender::start_convert(float beep, int remove_originals)
303 this->remove_originals = remove_originals;
307 void ConvertRender::run()
309 mwindow->stop_brender();
312 failed = 0; canceled = 0;
314 progress_timer->update();
317 for( int i=0; !failed && !canceled && i<needed_copies.size(); ++i )
320 canceled = progress->is_cancelled();
321 printf(_("convert: failed=%d canceled=%d\n"), failed, canceled);
322 double elapsed_time = progress_timer->get_scaled_difference(1);
324 char elapsed[BCSTRLEN], text[BCSTRLEN];
325 Units::totext(elapsed, elapsed_time, TIME_HMS2);
326 printf(_("TranscodeRender::run: done in %s\n"), elapsed);
328 strcpy(text, _("transcode cancelled"));
330 strcpy(text, _("transcode failed"));
332 sprintf(text, _("transcode %d files, render time %s"),
333 needed_copies.size(), elapsed);
338 mwindow->finish_convert(remove_originals);
340 else if( !canceled ) {
341 eprintf(_("Error making transcode."));
344 if( !canceled && beep > 0 ) {
346 mwindow->beep(4000., 0.5, beep);
348 mwindow->beep(1000., 0.5, beep);
350 mwindow->beep(4000., 0.5, beep);
353 mwindow->beep(2000., 2.0, beep);
355 mwindow->restart_brender();
358 void ConvertRender::start_progress()
361 for( int i = 0; i < needed_idxbls.size(); i++ ) {
362 Indexable *orig_idxbl = needed_idxbls[i];
363 int64_t video_frames = orig_idxbl->get_video_frames();
364 double frame_rate = orig_idxbl->get_frame_rate();
365 double video_length = video_frames / frame_rate;
366 int64_t audio_samples = orig_idxbl->get_audio_samples();
367 int sample_rate = orig_idxbl->get_sample_rate();
368 double audio_length = (double)audio_samples / sample_rate;
369 double length = bmax(video_length, audio_length);
372 int64_t total_samples = total_len * format_asset->sample_rate;
373 mwindow->gui->lock_window("Render::start_progress");
374 progress = mwindow->mainprogress->
375 start_progress(_("Transcode files..."), total_samples);
376 mwindow->gui->unlock_window();
377 convert_progress = new ConvertProgress(mwindow, this);
378 convert_progress->start();
381 void ConvertRender::stop_progress(const char *msg)
383 mwindow->gui->lock_window("ConvertRender::stop_progress");
384 delete convert_progress;
385 convert_progress = 0;
387 mwindow->mainprogress->end_progress(progress);
389 mwindow->gui->show_message(msg);
390 mwindow->gui->update_default_message();
391 mwindow->gui->unlock_window();
395 ConvertPackageRenderer::ConvertPackageRenderer(ConvertRender *render)
398 this->render = render;
401 ConvertPackageRenderer::~ConvertPackageRenderer()
405 int ConvertPackageRenderer::get_master()
410 int ConvertPackageRenderer::get_result()
412 return render->result;
415 void ConvertPackageRenderer::set_result(int value)
418 render->result = value;
421 void ConvertPackageRenderer::set_progress(int64_t value)
423 render->counter_lock->lock("ConvertPackageRenderer::set_progress");
424 // Increase total rendered for all nodes
425 render->total_rendered += value;
426 render->counter_lock->unlock();
429 int ConvertPackageRenderer::progress_cancelled()
431 return render->progress && render->progress->is_cancelled();
434 ConvertProgress::ConvertProgress(MWindow *mwindow, ConvertRender *render)
437 this->mwindow = mwindow;
438 this->render = render;
442 ConvertProgress::~ConvertProgress()
448 void ConvertProgress::run()
450 Thread::disable_cancel();
452 if( render->total_rendered != last_value ) {
453 render->progress->update(render->total_rendered);
454 last_value = render->total_rendered;
457 Thread::enable_cancel();
459 Thread::disable_cancel();
463 void ConvertRender::create_copy(int i)
465 Indexable *orig_idxbl = needed_idxbls[i];
466 Asset *needed_copy = needed_copies[i];
467 int64_t video_frames = orig_idxbl->get_video_frames();
468 double frame_rate = orig_idxbl->get_frame_rate();
469 double video_length = video_frames / frame_rate;
470 int64_t audio_samples = orig_idxbl->get_audio_samples();
471 int sample_rate = orig_idxbl->get_sample_rate();
472 double audio_length = (double)audio_samples / sample_rate;
473 double length = bmax(video_length, audio_length);
474 EDL *edl = convert_edl(mwindow->edl, orig_idxbl);
475 ConvertPackageRenderer renderer(this);
476 renderer.initialize(mwindow, edl, mwindow->preferences, needed_copy);
477 PackageDispatcher dispatcher;
478 dispatcher.create_packages(mwindow, edl, mwindow->preferences,
479 SINGLE_PASS, needed_copy, 0, length, 0);
480 RenderPackage *package = dispatcher.get_package(0);
481 if( !renderer.render_package(package) ) {
482 Asset *asset = mwindow->edl->assets->update(needed_copy);
483 mwindow->mainindexes->add_next_asset(0, asset);
484 mwindow->mainindexes->start_build();
491 ConvertWindow::ConvertWindow(MWindow *mwindow, ConvertDialog *dialog, int x, int y)
492 : BC_Window(_(PROGRAM_NAME ": Transcode settings"), x, y, WIDTH, HEIGHT,
495 this->mwindow = mwindow;
496 this->dialog = dialog;
500 ConvertWindow::~ConvertWindow()
502 lock_window("ConvertWindow::~ConvertWindow");
508 void ConvertWindow::create_objects()
510 lock_window("ConvertWindow::create_objects");
511 int margin = mwindow->theme->widget_border;
512 int lmargin = margin + xS(10);
515 int y = margin + yS(10);
518 add_subwindow(text = new BC_Title(x, y,
519 _("Render untagged assets and replace in project")));
520 y += text->get_h() + margin + yS(10);
522 y += BC_Title::calculate_h(this, _("Tag suffix:")) + margin + yS(10);
525 format_tools = new ConvertFormatTools(mwindow, this, dialog->asset);
526 format_tools->create_objects(x, y, 1, 1, 1, 1, 0, 1, 0, 1, // skip the path
530 add_subwindow(text = new BC_Title(x, y1, _("Tag suffix:")));
531 x = format_tools->format_text->get_x();
532 add_subwindow(suffix_text = new ConvertSuffixText(this, dialog, x, y1));
534 y += margin + yS(10);
536 add_subwindow(remove_originals = new ConvertRemoveOriginals(this, x, y));
538 y += remove_originals->get_h() + margin;
540 add_subwindow(beep_on_done = new ConvertBeepOnDone(this, x, y));
541 x += beep_on_done->get_w() + margin + xS(10);
542 add_subwindow(new BC_Title(x, y+yS(10), _("Beep on done volume")));
543 // y += beep_on_done->get_h() + margin;
545 add_subwindow(new BC_OKButton(this));
546 add_subwindow(new BC_CancelButton(this));
551 ConvertSuffixText::ConvertSuffixText(ConvertWindow *gui,
552 ConvertDialog *dialog, int x, int y)
553 : BC_TextBox(x, y, 160, 1, dialog->suffix)
556 this->dialog = dialog;
559 ConvertSuffixText::~ConvertSuffixText()
563 int ConvertSuffixText::handle_event()
565 strcpy(dialog->suffix, get_text());
569 ConvertFormatTools::ConvertFormatTools(MWindow *mwindow, ConvertWindow *gui, Asset *asset)
570 : FormatTools(mwindow, gui, asset)
575 void ConvertFormatTools::update_format()
577 asset->save_defaults(mwindow->defaults, "CONVERT_", 1, 1, 0, 0, 0);
578 FormatTools::update_format();
582 ConvertMenuItem::ConvertMenuItem(MWindow *mwindow)
583 : BC_MenuItem(_("Transcode..."), _("Alt-e"), 'e')
585 this->mwindow = mwindow;
589 ConvertMenuItem::~ConvertMenuItem()
594 void ConvertMenuItem::create_objects()
596 dialog = new ConvertDialog(mwindow);
599 int ConvertMenuItem::handle_event()
601 mwindow->gui->unlock_window();
603 mwindow->gui->lock_window("ConvertMenuItem::handle_event");
608 ConvertDialog::ConvertDialog(MWindow *mwindow)
610 this->mwindow = mwindow;
613 strcpy(suffix, ".transcode");
614 // quicker than some, not as good as others
615 asset->format = FILE_FFMPEG;
616 strcpy(asset->fformat, "mp4");
617 strcpy(asset->vcodec, "h264.mp4");
618 asset->ff_video_bitrate = 2560000;
619 asset->video_data = 1;
620 strcpy(asset->acodec, "h264.mp4");
621 asset->ff_audio_bitrate = 256000;
622 asset->audio_data = 1;
623 remove_originals = 1;
627 ConvertDialog::~ConvertDialog()
630 asset->remove_user();
633 BC_Window* ConvertDialog::new_gui()
635 asset->format = FILE_FFMPEG;
636 asset->frame_rate = mwindow->edl->session->frame_rate;
637 asset->sample_rate = mwindow->edl->session->sample_rate;
638 asset->load_defaults(mwindow->defaults, "CONVERT_", 1, 1, 0, 1, 0);
639 remove_originals = mwindow->defaults->get("CONVERT_REMOVE_ORIGINALS", remove_originals);
640 beep = mwindow->defaults->get("CONVERT_BEEP", beep);
641 mwindow->defaults->get("CONVERT_SUFFIX", suffix);
642 mwindow->gui->lock_window("ConvertDialog::new_gui");
644 mwindow->gui->get_abs_cursor(cx, cy);
645 gui = new ConvertWindow(mwindow, this, cx - WIDTH/2, cy - HEIGHT/2);
646 gui->create_objects();
647 mwindow->gui->unlock_window();
651 void ConvertDialog::handle_close_event(int result)
654 mwindow->defaults->update("CONVERT_SUFFIX", suffix);
655 mwindow->defaults->update("CONVERT_REMOVE_ORIGINALS", remove_originals);
656 mwindow->defaults->update("CONVERT_BEEP", beep);
657 asset->save_defaults(mwindow->defaults, "CONVERT_", 1, 1, 0, 1, 0);
658 mwindow->start_convert(asset, suffix, beep, remove_originals);
662 ConvertRemoveOriginals::ConvertRemoveOriginals(ConvertWindow *gui, int x, int y)
663 : BC_CheckBox(x, y, gui->dialog->remove_originals, _("Remove originals from project"))
668 ConvertRemoveOriginals::~ConvertRemoveOriginals()
672 int ConvertRemoveOriginals::handle_event()
674 gui->dialog->remove_originals = get_value();
678 ConvertBeepOnDone::ConvertBeepOnDone(ConvertWindow *gui, int x, int y)
679 : BC_FPot(x, y, gui->dialog->beep*100.f, 0.f, 100.f)
684 int ConvertBeepOnDone::handle_event()
686 gui->dialog->beep = get_value()/100.f;
690 ConvertBeep::ConvertBeep(MWindow *mwindow)
693 this->mwindow = mwindow;
694 audio = new AudioDevice(mwindow);
699 ConvertBeep::~ConvertBeep()
705 void ConvertBeep::run()
708 int64_t bfrsz = BEEP_SAMPLE_RATE;
709 EDL *edl = mwindow->edl;
710 EDLSession *session = edl->session;
711 AudioOutConfig *aconfig = session->playback_config->aconfig;
712 audio->open_output(aconfig, BEEP_SAMPLE_RATE, bfrsz, channels, 0);
713 audio->start_playback();
715 double out0[bfrsz], out1[bfrsz], *out[2] = { out0, out1 };
716 const double two_pi = 2*M_PI;
717 int64_t audio_len = BEEP_SAMPLE_RATE * secs;
718 const double dt = two_pi * freq/BEEP_SAMPLE_RATE;
723 while( !interrupted ) {
724 int len = audio_len - audio_pos;
725 if( len <= 0 ) break;
726 if( len > bfrsz ) len = bfrsz;
728 for( int i=0; i<len; ++i,++k,th+=dt ) {
729 double t = th - two_pi;
731 out0[i] = out1[i] = sin(th) * gain;
733 audio->write_buffer(out, channels, len);
738 audio->set_last_buffer();
739 audio->stop_audio(interrupted ? 0 : 1);
745 void ConvertBeep::start()
747 if( running() ) return;
753 void ConvertBeep::stop(int wait)
755 if( running() && !interrupted ) {
757 audio->stop_audio(wait);
762 void ConvertBeep::tone(double freq, double secs, double gain)