3 * Copyright (C) 2016-2020 William Morrow
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published
7 * by 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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include "edlsession.h"
31 #include "interlacemodes.h"
33 #include "mainerror.h"
36 #include "mwindowgui.h"
38 #include "pluginset.h"
39 #include "preferences.h"
47 #if !defined(__FreeBSD__)
49 #if !defined(__NetBSD__)
50 #include <sys/statfs.h>
53 #include <sys/param.h>
54 #include <sys/mount.h>
57 #if defined(__NetBSD__)
58 #include <sys/statvfs.h>
60 #define statfs statvfs
66 // selected by timezone
67 #define BD_1920x1080_2997i 0
68 #define BD_1920x1080_25i 3
70 static struct bd_format {
76 // framerates are frames, not fields, per second, *=not standard
77 { "1920x1080 29.97i", 1920,1080, 29.97, 1, ILACE_MODE_TOP_FIRST },
78 { "1920x1080 29.97p*", 1920,1080, 29.97, 1, ILACE_MODE_FAKE_INTERLACE },
79 { "1920x1080 24p", 1920,1080, 24., 1, ILACE_MODE_NOTINTERLACED },
80 { "1920x1080 25i", 1920,1080, 25., 1, ILACE_MODE_TOP_FIRST },
81 { "1920x1080 25p*", 1920,1080, 25., 1, ILACE_MODE_FAKE_INTERLACE },
82 { "1920x1080 23.976p", 1920,1080, 23.976, 1, ILACE_MODE_NOTINTERLACED },
83 { "1440x1080 29.97i", 1440,1080, 29.97, -1, ILACE_MODE_TOP_FIRST },
84 { "1440x1080 25i", 1440,1080, 25., -1, ILACE_MODE_TOP_FIRST },
85 { "1440x1080 24p", 1440,1080, 24., -1, ILACE_MODE_NOTINTERLACED },
86 { "1440x1080 23.976p", 1440,1080, 23.976,-1, ILACE_MODE_NOTINTERLACED },
87 { "1280x720 59.94p", 1280,720, 59.94, 1, ILACE_MODE_NOTINTERLACED },
88 { "1280x720 50p", 1280,720, 50., 1, ILACE_MODE_NOTINTERLACED },
89 { "1280x720 29.97p*", 1280,720, 29.97, 1, ILACE_MODE_NOTINTERLACED },
90 { "1280x720 25p*", 1280,720, 25., 1, ILACE_MODE_NOTINTERLACED },
91 { "1280x720 24p", 1280,720, 24., 1, ILACE_MODE_NOTINTERLACED },
92 { "1280x720 23.976p", 1280,720, 23.976, 1, ILACE_MODE_NOTINTERLACED },
93 { "720x576 25i", 720,576, 25., 0, ILACE_MODE_BOTTOM_FIRST },
94 { "720x576 25p*", 720,576, 25., 0, ILACE_MODE_NOTINTERLACED },
95 { "720x480 29.97i", 720,480, 29.97, 0, ILACE_MODE_BOTTOM_FIRST },
96 { "720x480 29.97p*", 720,480, 29.97, 0, ILACE_MODE_NOTINTERLACED },
99 static struct bd_profile {
103 {"bluray_lpcm.m2ts"},
107 const int64_t CreateBD_Thread::BD_SIZE = 25000000000;
108 const int CreateBD_Thread::BD_STREAMS = 1;
109 const int CreateBD_Thread::BD_WIDTH = 1920;
110 const int CreateBD_Thread::BD_HEIGHT = 1080;
111 const double CreateBD_Thread::BD_WIDE_ASPECT_WIDTH = 16.;
112 const double CreateBD_Thread::BD_WIDE_ASPECT_HEIGHT = 9.;
113 const double CreateBD_Thread::BD_ASPECT_WIDTH = 4.;
114 const double CreateBD_Thread::BD_ASPECT_HEIGHT = 3.;
115 const double CreateBD_Thread::BD_FRAMERATE = 24000. / 1001.;
116 //const int CreateBD_Thread::BD_MAX_BITRATE = 40000000;
117 const int CreateBD_Thread::BD_MAX_BITRATE = 10000000;
118 const int CreateBD_Thread::BD_CHANNELS = 2;
119 const int CreateBD_Thread::BD_WIDE_CHANNELS = 6;
120 const double CreateBD_Thread::BD_SAMPLERATE = 48000;
121 const double CreateBD_Thread::BD_KAUDIO_RATE = 192;
122 const int CreateBD_Thread::BD_INTERLACE_MODE = ILACE_MODE_NOTINTERLACED;
124 CreateBD_MenuItem::CreateBD_MenuItem(MWindow *mwindow)
125 : BC_MenuItem(_("BD Render..."), _("Ctrl-Shift-D"), 'D')
129 this->mwindow = mwindow;
132 int CreateBD_MenuItem::handle_event()
134 mwindow->create_bd->start();
138 BD_BatchRenderJob::BD_BatchRenderJob(Preferences *preferences, int labeled, int farmed)
139 : BatchRenderJob("BD_JOB", preferences, labeled, farmed)
143 int BD_BatchRenderJob::get_udfs_mount(char *udfs, char *mopts, char *mntpt)
146 // default: mount -t udf -o loop $dir/bd.udfs $dir/udfs
147 strcpy(udfs,"$dir/bd.udfs");
148 strcpy(mopts,"-t udf -o loop $dir/bd.udfs ");
149 strcpy(mntpt,"$dir/udfs");
150 const char *home = getenv("HOME");
151 if( !home ) return ret;
152 FILE *fp = fopen("/etc/fstab","r");
153 if( !fp ) return ret;
154 int len = strlen(home);
155 char line[BCTEXTLEN], typ[BCTEXTLEN], file[BCTEXTLEN];
156 char mpnt[BCTEXTLEN], opts[BCTEXTLEN];
157 while( fgets(line,sizeof(line),fp) ) {
158 // search "/etc/fstab" for: $HOME/img_file $HOME/bluray udf noauto,loop,rw,user
159 if( line[0] == '#' || line[0] == ';' ) continue;
160 if( sscanf(line,"%s %s %s %s ", &file[0], &mpnt[0], &typ[0], &opts[0]) != 4 )
162 if( strcmp("udf", typ) ) continue;
163 if( strncmp(home, file, len) || file[len] != '/' ) continue;
164 if( strncmp(home, mpnt, len) ) continue;
165 if( strcmp(&mpnt[len], "/bluray") ) continue;
166 int loop = 0, user = 0, rw = 0, noauto = 0;
167 char *op = opts, *ep = op + sizeof(opts)-1;
168 while( op < ep && *op ) {
169 char opt[BCTEXTLEN], *cp = opt;
170 while( op < ep && *op && (*cp=*op++)!=',' ) ++cp;
172 if( !strcmp("loop", opt) ) loop = 1;
173 else if( !strcmp("user", opt) ) user = 1;
174 else if( !strcmp("noauto", opt) ) noauto = 1;
175 else if( !strcmp("rw", opt) ) rw = 1;
177 if( loop && user && rw && noauto ) {
191 char *BD_BatchRenderJob::create_script(EDL *edl, ArrayList<Indexable *> *idxbls)
193 char script[BCTEXTLEN];
194 strcpy(script, edl_path);
197 char *bp = strrchr(script,'/');
200 strcpy(bp, "/bd.sh");
201 fd = open(script, O_WRONLY+O_CREAT+O_TRUNC, 0755);
204 fp = fdopen(fd, "w");
206 char err[BCTEXTLEN], msg[BCTEXTLEN];
207 strerror_r(errno, err, sizeof(err));
208 sprintf(msg, _("Unable to save: %s\n-- %s"), script, err);
209 MainError::show_error(msg);
212 char udfs[BCTEXTLEN], mopts[BCTEXTLEN], mntpt[BCTEXTLEN];
213 int is_usr_mnt = get_udfs_mount(udfs, mopts, mntpt);
214 const char *exec_path = File::get_cinlib_path();
215 fprintf(fp,"#!/bin/bash -ex\n");
216 fprintf(fp,"sdir=`dirname $0`\n");
217 fprintf(fp,"dir=`cd \"$sdir\"; pwd`\n");
218 fprintf(fp,"PATH=$PATH:%s\n",exec_path);
219 fprintf(fp,"mkdir -p $dir/udfs\n");
220 fprintf(fp,"sz=`du -cb $dir/bd.m2ts* | tail -1 | sed -e 's/[ ].*//'`\n");
221 fprintf(fp,"blks=$((sz/2048 + 4096))\n");
222 fprintf(fp,"rm -f %s\n", udfs);
223 fprintf(fp,"if [ -f $dir/bd.meta ]; then\n");
224 fprintf(fp,"sed -i \"s|bd.m2ts|$dir\/bd.m2ts|g\" $dir/bd.meta\n");
225 fprintf(fp,"tsmuxer $dir/bd.meta $dir/bd.iso \n");
226 fprintf(fp,"mv $dir/bd.iso $dir/bd.udfs\n");
227 fprintf(fp,"else\n");
228 fprintf(fp,"mkudffs -b 2048 %s $blks\n", udfs);
229 fprintf(fp,"mount %s%s\n", mopts, mntpt);
230 fprintf(fp,"bdwrite %s $dir/bd.m2ts*\n",mntpt);
231 fprintf(fp,"umount %s\n",mntpt);
234 fprintf(fp,"mv -f %s $dir/bd.udfs\n", udfs);
235 fprintf(fp,"echo To burn bluray, load writable media and run:\n");
236 fprintf(fp,"echo for WORM: growisofs -dvd-compat -Z /dev/bd=$dir/bd.udfs\n");
237 fprintf(fp,"echo for RW: dd if=$dir/bd.udfs of=/dev/bd bs=2048000\n");
238 fprintf(fp,"kill $$\n");
241 return cstrdup(script);
245 CreateBD_Thread::CreateBD_Thread(MWindow *mwindow)
248 this->mwindow = mwindow;
250 this->use_deinterlace = 0;
251 this->use_scale = Rescale::none;
252 this->use_histogram = 0;
253 this->use_inverse_telecine = 0;
254 this->use_wide_audio = 0;
255 this->use_resize_tracks = 0;
256 this->use_labeled = 0;
257 this->use_farmed = 0;
258 this->use_tsmuxer = 0;
260 strcpy(use_profile,"bluray.m2ts");
262 this->bd_size = BD_SIZE;
263 this->bd_width = BD_WIDTH;
264 this->bd_height = BD_HEIGHT;
265 this->bd_aspect_width = BD_WIDE_ASPECT_WIDTH;
266 this->bd_aspect_height = BD_WIDE_ASPECT_HEIGHT;
267 this->bd_framerate = BD_FRAMERATE;
268 this->bd_samplerate = BD_SAMPLERATE;
269 this->bd_max_bitrate = BD_MAX_BITRATE;
270 this->bd_kaudio_rate = BD_KAUDIO_RATE;
271 this->max_w = this->max_h = 0;
272 this->batchrender = 0;
275 CreateBD_Thread::~CreateBD_Thread()
280 int CreateBD_Thread::create_bd_jobs(ArrayList<BatchRenderJob*> *jobs, const char *asset_dir)
282 EDL *edl = mwindow->edl;
283 if( !edl || !edl->session ) {
285 sprintf(msg, _("No EDL/Session"));
286 MainError::show_error(msg);
289 EDLSession *session = edl->session;
291 double total_length = edl->tracks->total_length();
292 if( total_length <= 0 ) {
294 sprintf(msg, _("No content: %s"), asset_title);
295 MainError::show_error(msg);
299 if( mkdir(asset_dir, 0777) ) {
300 char err[BCTEXTLEN], msg[BCTEXTLEN];
301 strerror_r(errno, err, sizeof(err));
302 sprintf(msg, _("Unable to create directory: %s\n-- %s"), asset_dir, err);
303 MainError::show_error(msg);
307 double old_samplerate = session->sample_rate;
308 double old_framerate = session->frame_rate;
310 session->video_channels = BD_STREAMS;
311 session->video_tracks = BD_STREAMS;
312 session->frame_rate = bd_framerate;
313 session->output_w = bd_width;
314 session->output_h = bd_height;
315 session->aspect_w = bd_aspect_width;
316 session->aspect_h = bd_aspect_height;
317 session->sample_rate = bd_samplerate;
318 session->audio_channels = session->audio_tracks =
319 use_wide_audio ? BD_WIDE_CHANNELS : BD_CHANNELS;
320 session->interlace_mode = bd_interlace_mode;
322 session->audio_channels = session->audio_tracks =
323 !use_wide_audio ? BD_CHANNELS : BD_WIDE_CHANNELS;
324 for( int i=0; i<MAX_CHANNELS; ++i )
325 session->achannel_positions[i] = default_audio_channel_position(i, session->audio_channels);
326 int audio_mapping = edl->tracks->recordable_audio_tracks() == BD_WIDE_CHANNELS &&
327 !use_wide_audio ? MWindow::AUDIO_5_1_TO_2 : MWindow::AUDIO_1_TO_1;
328 mwindow->remap_audio(audio_mapping);
330 double new_samplerate = session->sample_rate;
331 double new_framerate = session->frame_rate;
334 edl->resample(old_samplerate, new_samplerate, TRACK_AUDIO);
335 edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
337 int64_t aud_size = ((bd_kaudio_rate * total_length)/8 + 1000-1) * 1000;
338 int64_t vid_size = bd_size*0.96 - aud_size;
339 int64_t vid_bitrate = (vid_size * 8) / total_length;
340 vid_bitrate /= 1000; vid_bitrate *= 1000;
341 if( vid_bitrate > bd_max_bitrate ) vid_bitrate = bd_max_bitrate;
343 char xml_filename[BCTEXTLEN];
344 sprintf(xml_filename, "%s/bd.xml", asset_dir);
346 edl->save_xml(&xml_file, xml_filename);
347 xml_file.terminate_string();
348 if( xml_file.write_to_file(xml_filename) ) {
350 sprintf(msg, _("Unable to save: %s"), xml_filename);
351 MainError::show_error(msg);
356 char meta_script[BCTEXTLEN];
357 strcpy(meta_script, "/");
360 char *bp = strrchr(meta_script,'/');
363 char script_filename[BCTEXTLEN];
364 sprintf(script_filename, "%s/bd.meta", asset_dir);
365 strcpy(bp, script_filename);
366 fd = open(meta_script, O_WRONLY+O_CREAT+O_TRUNC, 0755);
369 fp = fdopen(fd, "w");
371 char err[BCTEXTLEN], msg[BCTEXTLEN];
372 strerror_r(errno, err, sizeof(err));
373 sprintf(msg, _("Unable to save: %s\n-- %s"), meta_script, err);
374 MainError::show_error(msg);
379 fprintf(fp,"MUXOPT --blu-ray --hdmv-descriptors --auto-chapters=5\n");
380 fprintf(fp,"V_MPEG4/ISO/AVC, bd.m2ts, track=4113\n");
381 if(!strcmp(use_profile, "bluray.m2ts"))
382 fprintf(fp,"A_AC3, bd.m2ts, track=4352\n");
383 if(!strcmp(use_profile, "bluray_lpcm.m2ts"))
384 fprintf(fp,"A_LPCM, bd.m2ts, track=4352\n");
385 if(!strcmp(use_profile, "bluray_truehd.m2ts"))
386 fprintf(fp,"A_MLP, bd.m2ts, track=4352\n");
392 BatchRenderJob *job = new BD_BatchRenderJob(mwindow->preferences,
393 use_labeled, use_farmed);
395 strcpy(&job->edl_path[0], xml_filename);
396 Asset *asset = job->asset;
398 asset->layers = BD_STREAMS;
399 asset->frame_rate = session->frame_rate;
400 asset->width = session->output_w;
401 asset->height = session->output_h;
402 asset->aspect_ratio = session->aspect_w / session->aspect_h;
403 asset->interlace_mode = session->interlace_mode;
405 char option_path[BCTEXTLEN];
406 sprintf(&asset->path[0],"%s/bd.m2ts", asset_dir);
407 asset->format = FILE_FFMPEG;
408 strcpy(asset->fformat, "m2ts");
410 asset->audio_data = 1;
411 strcpy(asset->acodec, use_profile);
412 //mwindow->defaults->get("DEFAULT_BLURAY_ACODEC", asset->acodec);
413 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
414 FFMPEG::load_options(option_path, asset->ff_audio_options,
415 sizeof(asset->ff_audio_options));
416 asset->ff_audio_bitrate = bd_kaudio_rate * 1000;
418 asset->video_data = 1;
419 const char *vcodec = use_profile;
420 switch( asset->interlace_mode ) {
421 case ILACE_MODE_TOP_FIRST: vcodec = "bluray_tff.m2ts"; break;
422 case ILACE_MODE_BOTTOM_FIRST: vcodec = "bluray_bff.m2ts"; break;
423 case ILACE_MODE_FAKE_INTERLACE : vcodec = "bluray_fakeinterlace.m2ts"; break;
425 strcpy(asset->vcodec, vcodec);
426 //mwindow->defaults->get("DEFAULT_BLURAY_VCODEC", asset->vcodec);
427 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
428 FFMPEG::load_options(option_path, asset->ff_video_options,
429 sizeof(asset->ff_video_options));
430 asset->ff_video_bitrate = vid_bitrate;
431 asset->ff_video_quality = -1;
435 void CreateBD_Thread::handle_close_event(int result)
438 mwindow->defaults->update("WORK_DIRECTORY", tmp_path);
439 mwindow->batch_render->load_defaults(mwindow->defaults);
440 mwindow->undo->update_undo_before();
441 KeyFrame keyframe; char data[BCTEXTLEN];
442 if( use_deinterlace ) {
443 sprintf(data,"<DEINTERLACE MODE=1>");
444 keyframe.set_data(data);
445 insert_video_plugin("Deinterlace", &keyframe);
447 if( use_inverse_telecine ) {
448 sprintf(data,"<IVTC FRAME_OFFSET=0 FIRST_FIELD=0 "
449 "AUTOMATIC=1 AUTO_THRESHOLD=2.0e+00 PATTERN=2>");
450 keyframe.set_data(data);
451 insert_video_plugin("Inverse Telecine", &keyframe);
453 if( use_scale != Rescale::none ) {
454 double bd_aspect = bd_aspect_height > 0 ? bd_aspect_width / bd_aspect_height : 1;
456 Tracks *tracks = mwindow->edl->tracks;
457 for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
458 if( vtrk->data_type != TRACK_VIDEO ) continue;
459 if( !vtrk->is_armed() ) continue;
460 vtrk->expand_view = 1;
461 PluginSet *plugin_set = new PluginSet(mwindow->edl, vtrk);
462 vtrk->plugin_set.append(plugin_set);
463 Edits *edits = vtrk->edits;
464 for( Edit *edit=edits->first; edit; edit=edit->next ) {
465 Indexable *indexable = edit->get_source();
466 if( !indexable ) continue;
467 Rescale in(indexable);
468 Rescale out(bd_width, bd_height, bd_aspect);
469 float src_w, src_h, dst_w, dst_h;
470 in.rescale(out,use_scale, src_w,src_h, dst_w,dst_h);
471 sprintf(data,"<SCALERATIO TYPE=%d"
472 " IN_W=%d IN_H=%d IN_ASPECT_RATIO=%f"
473 " OUT_W=%d OUT_H=%d OUT_ASPECT_RATIO=%f"
474 " SRC_X=%f SRC_Y=%f SRC_W=%f SRC_H=%f"
475 " DST_X=%f DST_Y=%f DST_W=%f DST_H=%f>", use_scale,
476 in.w, in.h, in.aspect, out.w, out.h, out.aspect,
477 0., 0., src_w, src_h, 0., 0., dst_w, dst_h);
478 keyframe.set_data(data);
479 plugin_set->insert_plugin(_("Scale Ratio"),
480 edit->startproject, edit->length,
481 PLUGIN_STANDALONE, 0, &keyframe, 0);
486 if( use_resize_tracks )
488 if( use_histogram ) {
490 sprintf(data, "<HISTOGRAM OUTPUT_MIN_0=0 OUTPUT_MAX_0=1 "
491 "OUTPUT_MIN_1=0 OUTPUT_MAX_1=1 "
492 "OUTPUT_MIN_2=0 OUTPUT_MAX_2=1 "
493 "OUTPUT_MIN_3=0 OUTPUT_MAX_3=1 "
494 "AUTOMATIC=0 THRESHOLD=9.0-01 PLOT=0 SPLIT=0>"
495 "<POINTS></POINTS><POINTS></POINTS><POINTS></POINTS>"
496 "<POINTS><POINT X=6.0e-02 Y=0>"
497 "<POINT X=9.4e-01 Y=1></POINTS>");
499 sprintf(data, "<HISTOGRAM AUTOMATIC=0 THRESHOLD=1.0e-01 "
500 "PLOT=0 SPLIT=0 W=440 H=500 PARADE=0 MODE=3 "
501 "LOW_OUTPUT_0=0 HIGH_OUTPUT_0=1 LOW_INPUT_0=0 HIGH_INPUT_0=1 GAMMA_0=1 "
502 "LOW_OUTPUT_1=0 HIGH_OUTPUT_1=1 LOW_INPUT_1=0 HIGH_INPUT_1=1 GAMMA_1=1 "
503 "LOW_OUTPUT_2=0 HIGH_OUTPUT_2=1 LOW_INPUT_2=0 HIGH_INPUT_2=1 GAMMA_2=1 "
504 "LOW_OUTPUT_3=0 HIGH_OUTPUT_3=1 LOW_INPUT_3=0.044 HIGH_INPUT_3=0.956 "
507 keyframe.set_data(data);
508 insert_video_plugin("Histogram", &keyframe);
511 char asset_dir[BCTEXTLEN], jobs_path[BCTEXTLEN];
512 snprintf(asset_dir, sizeof(asset_dir), "%s/%s", tmp_path, asset_title);
513 snprintf(jobs_path, sizeof(jobs_path), "%s/bd.jobs", asset_dir);
514 //batchrender->tsmuxered = use_tsmuxer;
515 mwindow->batch_render->reset(jobs_path);
516 int ret = create_bd_jobs(&mwindow->batch_render->jobs, asset_dir);
517 mwindow->undo->update_undo_after(_("create bd"), LOAD_ALL);
518 mwindow->resync_guis();
520 mwindow->batch_render->save_jobs();
521 mwindow->batch_render->start(-use_farmed, -use_labeled);
524 BC_Window* CreateBD_Thread::new_gui()
526 strcpy(tmp_path, "/tmp");
527 mwindow->defaults->get("WORK_DIRECTORY", tmp_path);
528 memset(asset_title,0,sizeof(asset_title));
529 time_t dt; time(&dt);
530 struct tm dtm; localtime_r(&dt, &dtm);
531 sprintf(asset_title, "bd_%02d%02d%02d-%02d%02d%02d",
532 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
533 dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
535 use_scale = Rescale::none;
537 use_inverse_telecine = 0;
539 use_resize_tracks = 0;
543 use_standard = !strcmp(mwindow->default_std(),"NTSC") ?
544 BD_1920x1080_2997i : BD_1920x1080_25i;
547 bd_height = BD_HEIGHT;
548 bd_aspect_width = BD_WIDE_ASPECT_WIDTH;
549 bd_aspect_height = BD_WIDE_ASPECT_HEIGHT;
550 bd_framerate = BD_FRAMERATE;
551 bd_samplerate = BD_SAMPLERATE;
552 bd_max_bitrate = BD_MAX_BITRATE;
553 bd_kaudio_rate = BD_KAUDIO_RATE;
554 bd_interlace_mode = BD_INTERLACE_MODE;
555 max_w = 0; max_h = 0;
557 int has_standard = -1;
559 EDLSession *session = mwindow->edl->session;
560 // match the session to any known standard
561 for( int i=0; i<(int)(sizeof(bd_formats)/sizeof(bd_formats[0])); ++i ) {
562 if( !EQUIV(session->frame_rate, bd_formats[i].framerate) ) continue;
563 if( session->output_w != bd_formats[i].w ) continue;
564 if( session->output_h != bd_formats[i].h ) continue;
565 has_standard = i; break;
568 if( has_standard >= 0 )
569 use_standard = has_standard;
572 int scr_x = mwindow->gui->get_screen_x(0, -1);
573 int scr_w = mwindow->gui->get_screen_w(0, -1);
574 int scr_h = mwindow->gui->get_screen_h(0, -1);
575 int w = xS(560), h = yS(340);
576 int x = scr_x + scr_w/2 - w/2, y = scr_h/2 - h/2;
578 gui = new CreateBD_GUI(this, x, y, w, h);
579 gui->create_objects();
581 if( getuid() != 0 ) {
582 mwindow->show_warning(
583 &mwindow->preferences->bd_warn_root,
584 _("Must be root to mount UDFS images\n"));
590 CreateBD_OK::CreateBD_OK(CreateBD_GUI *gui, int x, int y)
594 set_tooltip(_("end setup, start batch render"));
597 CreateBD_OK::~CreateBD_OK()
601 int CreateBD_OK::button_press_event()
603 if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
610 int CreateBD_OK::keypress_event()
612 return context_help_check_and_show();
616 CreateBD_Cancel::CreateBD_Cancel(CreateBD_GUI *gui, int x, int y)
617 : BC_CancelButton(x, y)
622 CreateBD_Cancel::~CreateBD_Cancel()
626 int CreateBD_Cancel::button_press_event()
628 if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
636 CreateBD_DiskSpace::CreateBD_DiskSpace(CreateBD_GUI *gui, int x, int y)
637 : BC_Title(x, y, "", MEDIUMFONT, GREEN)
642 CreateBD_DiskSpace::~CreateBD_DiskSpace()
646 int64_t CreateBD_DiskSpace::tmp_path_space()
648 const char *path = gui->thread->tmp_path;
649 if( access(path,R_OK+W_OK) ) return 0;
651 if( statfs(path, &sfs) ) return 0;
652 return (int64_t)sfs.f_bsize * sfs.f_bfree;
655 void CreateBD_DiskSpace::update()
657 static const char *suffix[] = { "", "KB", "MB", "GB", "TB", "PB" };
658 int64_t disk_space = tmp_path_space();
659 double media_size = 100e9, msz = 0, m = 1;
661 if( sscanf(gui->media_size->get_text(), "%lf%s", &msz, sfx) == 2 ) {
662 int i = sizeof(suffix)/sizeof(suffix[0]);
663 while( --i >= 0 && strcmp(sfx, suffix[i]) );
664 while( --i >= 0 ) m *= 1000;
665 media_size = msz * m;
667 int color = disk_space < media_size*2 ? RED : GREEN;
669 for( int64_t space=disk_space; i<5 && (space/=1000)>0; disk_space=space, ++i );
670 char text[BCTEXTLEN];
671 sprintf(text, "%s%3jd%s", _("disk space: "), disk_space, suffix[i]);
672 gui->disk_space->BC_Title::update(text);
673 gui->disk_space->set_color(color);
676 CreateBD_TmpPath::CreateBD_TmpPath(CreateBD_GUI *gui, int x, int y, int w)
677 : BC_TextBox(x, y, w, 1, -(int)sizeof(gui->thread->tmp_path),
678 gui->thread->tmp_path, 1, MEDIUMFONT)
683 CreateBD_TmpPath::~CreateBD_TmpPath()
687 int CreateBD_TmpPath::handle_event()
690 gui->disk_space->update();
695 CreateBD_AssetTitle::CreateBD_AssetTitle(CreateBD_GUI *gui, int x, int y, int w)
696 : BC_TextBox(x, y, w, 1, -(int)sizeof(gui->thread->asset_title),
697 gui->thread->asset_title, 1, MEDIUMFONT)
702 CreateBD_AssetTitle::~CreateBD_AssetTitle()
706 int CreateBD_AssetTitle::handle_event()
712 CreateBD_Deinterlace::CreateBD_Deinterlace(CreateBD_GUI *gui, int x, int y)
713 : BC_CheckBox(x, y, &gui->thread->use_deinterlace, _("Deinterlace"))
718 CreateBD_Deinterlace::~CreateBD_Deinterlace()
722 int CreateBD_Deinterlace::handle_event()
725 gui->need_inverse_telecine->set_value(0);
726 gui->thread->use_inverse_telecine = 0;
728 return BC_CheckBox::handle_event();
732 CreateBD_InverseTelecine::CreateBD_InverseTelecine(CreateBD_GUI *gui, int x, int y)
733 : BC_CheckBox(x, y, &gui->thread->use_inverse_telecine, _("Inverse Telecine"))
738 CreateBD_InverseTelecine::~CreateBD_InverseTelecine()
742 int CreateBD_InverseTelecine::handle_event()
745 gui->need_deinterlace->set_value(0);
746 gui->thread->use_deinterlace = 0;
748 return BC_CheckBox::handle_event();
752 CreateBD_ResizeTracks::CreateBD_ResizeTracks(CreateBD_GUI *gui, int x, int y)
753 : BC_CheckBox(x, y, &gui->thread->use_resize_tracks, _("Resize Tracks"))
758 CreateBD_ResizeTracks::~CreateBD_ResizeTracks()
763 CreateBD_Histogram::CreateBD_Histogram(CreateBD_GUI *gui, int x, int y)
764 : BC_CheckBox(x, y, &gui->thread->use_histogram, _("Histogram"))
769 CreateBD_Histogram::~CreateBD_Histogram()
773 CreateBD_LabelChapters::CreateBD_LabelChapters(CreateBD_GUI *gui, int x, int y)
774 : BC_CheckBox(x, y, &gui->thread->use_labeled, _("Chapters at Labels"))
779 CreateBD_LabelChapters::~CreateBD_LabelChapters()
783 CreateBD_UseRenderFarm::CreateBD_UseRenderFarm(CreateBD_GUI *gui, int x, int y)
784 : BC_CheckBox(x, y, &gui->thread->use_farmed, _("Use render farm"))
789 CreateBD_UseRenderFarm::~CreateBD_UseRenderFarm()
794 CreateBD_WideAudio::CreateBD_WideAudio(CreateBD_GUI *gui, int x, int y)
795 : BC_CheckBox(x, y, &gui->thread->use_wide_audio, _("Audio 5.1"))
800 CreateBD_WideAudio::~CreateBD_WideAudio()
804 CreateBD_UseTsmuxer::CreateBD_UseTsmuxer(CreateBD_GUI *gui, int x, int y)
805 : BC_CheckBox(x, y, &gui->thread->use_tsmuxer, _("use tsmuxer"))
810 CreateBD_UseTsmuxer::~CreateBD_UseTsmuxer()
815 CreateBD_GUI::CreateBD_GUI(CreateBD_Thread *thread, int x, int y, int w, int h)
816 : BC_Window(_(PROGRAM_NAME ": Create BD"), x, y, w, h, xS(50), yS(50), 1, 0, 1)
818 this->thread = thread;
819 at_x = at_y = tmp_x = tmp_y = 0;
820 ok_x = ok_y = ok_w = ok_h = 0;
821 cancel_x = cancel_y = cancel_w = cancel_h = 0;
829 need_deinterlace = 0;
830 need_inverse_telecine = 0;
831 need_resize_tracks = 0;
840 // *** CONTEXT_HELP ***
841 context_help_set_keyword("DVD and Bluray Creation");
844 CreateBD_GUI::~CreateBD_GUI()
848 void CreateBD_GUI::create_objects()
851 int xs10 = xS(10), xs35 = xS(35);
852 int xs60 = xS(60), xs160 = xS(160), xs170 = xS(170);
853 int ys5 = yS(5), ys10 = yS(10);
854 lock_window("CreateBD_GUI::create_objects");
855 int pady = BC_TextBox::calculate_h(this, MEDIUMFONT, 0, 1) + ys5;
856 int padx = BC_Title::calculate_w(this, (char*)"X", MEDIUMFONT);
857 int x = padx/2, y = pady/2;
858 BC_Title *title = new BC_Title(x, y, _("Title:"), MEDIUMFONT, YELLOW);
859 add_subwindow(title);
860 at_x = x + title->get_w(); at_y = y;
861 asset_title = new CreateBD_AssetTitle(this, at_x, at_y, get_w()-at_x-xs10);
862 add_subwindow(asset_title);
863 y += title->get_h() + pady/2;
864 title = new BC_Title(x, y, _("Work path:"), MEDIUMFONT, YELLOW);
865 add_subwindow(title);
866 tmp_x = x + title->get_w(); tmp_y = y;
867 tmp_path = new CreateBD_TmpPath(this, tmp_x, tmp_y, get_w()-tmp_x-xs35);
868 add_subwindow(tmp_path);
869 btmp_path = new BrowseButton(thread->mwindow->theme, this, tmp_path,
870 tmp_x+tmp_path->get_w(), tmp_y, "/tmp",
871 _("Work path"), _("Select a Work directory:"), 1);
872 add_subwindow(btmp_path);
873 y += title->get_h() + pady/2;
874 disk_space = new CreateBD_DiskSpace(this, x, y);
875 add_subwindow(disk_space);
876 int x0 = get_w() - xs170;
877 title = new BC_Title(x0, y, _("Media:"), MEDIUMFONT, YELLOW);
878 add_subwindow(title);
879 int x1 = x0+title->get_w()+padx;
880 media_size = new CreateBD_MediaSize(this, x1, y);
881 media_size->create_objects();
882 media_sizes.append(new BC_ListBoxItem("25GB"));
883 media_sizes.append(new BC_ListBoxItem("50GB"));
884 media_size->update_list(&media_sizes);
885 media_size->update(media_sizes[0]->get_text());
886 disk_space->update();
887 y += disk_space->get_h() + pady/2;
889 title = new BC_Title(x, y, _("Profile:"), MEDIUMFONT, YELLOW);
890 add_subwindow(title);
892 x += title->get_w()+padx;
893 profile = new CreateBD_Profile(this, x, y);
894 profile->create_objects();
895 profiles.append(new BC_ListBoxItem("bluray.m2ts"));
896 profiles.append(new BC_ListBoxItem("bluray_lpcm.m2ts"));
897 /* profiles.append(new BC_ListBoxItem("bluray_truehd.m2ts")); */
898 profile->update_list(&profiles);
899 profile->update(profiles[0]->get_text());
901 x += profile->get_w()+padx;
902 need_tsmuxer = new CreateBD_UseTsmuxer(this, x, y);
903 add_subwindow(need_tsmuxer);
904 y += need_tsmuxer->get_h() + pady;
906 title = new BC_Title(start_x, y, _("Format:"), MEDIUMFONT, YELLOW);
907 add_subwindow(title);
908 standard = new CreateBD_Format(this, title->get_w() + padx, y);
909 add_subwindow(standard);
910 standard->create_objects();
911 standard->set_text(bd_formats[thread->use_standard].name);
913 title = new BC_Title(x0, y, _("Scale:"), MEDIUMFONT, YELLOW);
914 add_subwindow(title);
915 x1 = x0+title->get_w()+padx;
916 scale = new CreateBD_Scale(this, x1, y);
917 add_subwindow(scale);
918 scale->create_objects();
919 y += standard->get_h() + pady/2;
921 need_deinterlace = new CreateBD_Deinterlace(this, start_x, y);
922 add_subwindow(need_deinterlace);
923 y += need_deinterlace->get_h() + pady/2;
924 need_histogram = new CreateBD_Histogram(this, start_x, y);
925 add_subwindow(need_histogram);
926 y += need_histogram->get_h() + pady/2;
927 non_standard = new BC_Title(x1, y+ys5, "", MEDIUMFONT, RED);
928 add_subwindow(non_standard);
930 need_inverse_telecine = new CreateBD_InverseTelecine(this, x1, y);
931 add_subwindow(need_inverse_telecine);
932 y += need_inverse_telecine->get_h() + pady/2;
933 need_wide_audio = new CreateBD_WideAudio(this, x1, y);
934 add_subwindow(need_wide_audio);
935 y += need_wide_audio->get_h() + pady/2;
936 need_resize_tracks = new CreateBD_ResizeTracks(this, x1, y);
937 add_subwindow(need_resize_tracks);
939 need_labeled = new CreateBD_LabelChapters(this, x1, y);
940 add_subwindow(need_labeled);
941 y += need_labeled->get_h() + pady/2;
942 need_farmed = new CreateBD_UseRenderFarm(this, x1, y);
943 add_subwindow(need_farmed);
944 ok_w = BC_OKButton::calculate_w();
945 ok_h = BC_OKButton::calculate_h();
947 ok_y = get_h() - ok_h - xs10;
948 ok = new CreateBD_OK(this, ok_x, ok_y);
950 cancel_w = BC_CancelButton::calculate_w();
951 cancel_h = BC_CancelButton::calculate_h();
952 cancel_x = get_w() - cancel_w - xs10,
953 cancel_y = get_h() - cancel_h - ys10;
954 cancel = new CreateBD_Cancel(this, cancel_x, cancel_y);
955 add_subwindow(cancel);
960 int CreateBD_GUI::resize_event(int w, int h)
962 int xs10 = xS(10), xs35 = xS(35);
964 asset_title->reposition_window(at_x, at_y, get_w()-at_x-xs10);
965 tmp_path->reposition_window(tmp_x, tmp_y, get_w()-tmp_x-xs35);
966 btmp_path->reposition_window(tmp_x+tmp_path->get_w(), tmp_y);
967 ok_y = h - ok_h - ys10;
968 ok->reposition_window(ok_x, ok_y);
969 cancel_x = w - cancel_w - xs10,
970 cancel_y = h - cancel_h - ys10;
971 cancel->reposition_window(cancel_x, cancel_y);
975 int CreateBD_GUI::translation_event()
980 int CreateBD_GUI::close_event()
986 void CreateBD_GUI::update()
988 scale->set_value(thread->use_scale);
989 need_deinterlace->set_value(thread->use_deinterlace);
990 need_inverse_telecine->set_value(thread->use_inverse_telecine);
991 need_resize_tracks->set_value(thread->use_resize_tracks);
992 need_histogram->set_value(thread->use_histogram);
993 need_wide_audio->set_value(thread->use_wide_audio);
994 need_labeled->set_value(thread->use_labeled);
995 need_farmed->set_value(thread->use_farmed);
996 need_tsmuxer->set_value(thread->use_tsmuxer);
999 int CreateBD_Thread::
1000 insert_video_plugin(const char *title, KeyFrame *default_keyframe)
1002 Tracks *tracks = mwindow->edl->tracks;
1003 for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
1004 if( vtrk->data_type != TRACK_VIDEO ) continue;
1005 if( !vtrk->is_armed() ) continue;
1006 vtrk->expand_view = 1;
1007 PluginSet *plugin_set = new PluginSet(mwindow->edl, vtrk);
1008 vtrk->plugin_set.append(plugin_set);
1009 Edits *edits = vtrk->edits;
1010 for( Edit *edit=edits->first; edit; edit=edit->next ) {
1011 plugin_set->insert_plugin(_(title),
1012 edit->startproject, edit->length,
1013 PLUGIN_STANDALONE, 0, default_keyframe, 0);
1020 int CreateBD_Thread::
1023 Tracks *tracks = mwindow->edl->tracks;
1024 int trk_w = max_w, trk_h = max_h;
1025 if( trk_w < bd_width ) trk_w = bd_width;
1026 if( trk_h < bd_height ) trk_h = bd_height;
1027 for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
1028 if( vtrk->data_type != TRACK_VIDEO ) continue;
1029 if( !vtrk->is_armed() ) continue;
1030 vtrk->track_w = trk_w;
1031 vtrk->track_h = trk_h;
1036 int CreateBD_Thread::
1039 // reset only probed options
1040 use_deinterlace = 0;
1041 use_scale = Rescale::none;
1042 use_resize_tracks = 0;
1048 if( !mwindow->edl ) return 1;
1050 bd_width = bd_formats[use_standard].w;
1051 bd_height = bd_formats[use_standard].h;
1052 bd_framerate = bd_formats[use_standard].framerate;
1053 int wide = bd_formats[use_standard].wide;
1054 bd_aspect_width = wide < 0 ? 1. :
1055 wide > 0 ? BD_WIDE_ASPECT_WIDTH : BD_ASPECT_WIDTH;
1056 bd_aspect_height = wide < 0 ? 1. :
1057 wide > 0 ? BD_WIDE_ASPECT_HEIGHT : BD_ASPECT_HEIGHT;
1058 bd_interlace_mode = bd_formats[use_standard].interlaced;
1059 double bd_aspect = bd_aspect_width / bd_aspect_height;
1061 Tracks *tracks = mwindow->edl->tracks;
1062 max_w = 0; max_h = 0;
1063 int has_deinterlace = 0, has_scale = 0;
1064 for( Track *trk=tracks->first; trk; trk=trk->next ) {
1065 if( !trk->is_armed() ) continue;
1066 Edits *edits = trk->edits;
1067 switch( trk->data_type ) {
1069 for( Edit *edit=edits->first; edit; edit=edit->next ) {
1070 if( edit->silence() ) continue;
1071 Indexable *indexable = edit->get_source();
1072 int w = indexable->get_w();
1073 if( w > max_w ) max_w = w;
1074 if( w != bd_width ) use_scale = Rescale::scaled;
1075 int h = indexable->get_h();
1076 if( h > max_h ) max_h = h;
1077 if( h != bd_height ) use_scale = Rescale::scaled;
1079 MWindow::create_aspect_ratio(aw, ah, w, h);
1080 double aspect = ah > 0 ? aw / ah : 1;
1081 if( wide >= 0 && !EQUIV(aspect, bd_aspect) )
1082 use_scale = Rescale::scaled;
1084 for( int i=0; i<trk->plugin_set.size(); ++i ) {
1085 for( Plugin *plugin = (Plugin*)trk->plugin_set[i]->first;
1086 plugin; plugin=(Plugin*)plugin->next ) {
1087 if( !strcmp(plugin->title, "Deinterlace") )
1088 has_deinterlace = 1;
1089 if( !strcmp(plugin->title, "Auto Scale") ||
1090 !strcmp(plugin->title, "Scale Ratio") ||
1091 !strcmp(plugin->title, "Scale") )
1099 use_scale = Rescale::none;
1100 if( use_scale != Rescale::none ) {
1101 if( max_w != bd_width ) use_resize_tracks = 1;
1102 if( max_h != bd_height ) use_resize_tracks = 1;
1104 for( Track *trk=tracks->first; trk && !use_resize_tracks; trk=trk->next ) {
1105 if( !trk->is_armed() ) continue;
1106 switch( trk->data_type ) {
1108 if( trk->track_w != max_w ) use_resize_tracks = 1;
1109 if( trk->track_h != max_h ) use_resize_tracks = 1;
1113 if( !has_deinterlace && max_h > 2*bd_height ) use_deinterlace = 1;
1115 if( tracks->recordable_audio_tracks() == BD_WIDE_CHANNELS )
1122 CreateBD_FormatItem::CreateBD_FormatItem(CreateBD_Format *popup,
1123 int standard, const char *name)
1126 this->popup = popup;
1127 this->standard = standard;
1130 CreateBD_FormatItem::~CreateBD_FormatItem()
1134 int CreateBD_FormatItem::handle_event()
1136 const char *text = get_text();
1137 int standard = this->standard;
1138 if( standard < 0 ) {
1139 BC_SubMenu *submenu = get_submenu();
1140 CreateBD_FormatItem *sub_item0 =
1141 (CreateBD_FormatItem *)submenu->get_item(0);
1142 standard = sub_item0->standard;
1143 text = sub_item0->get_text();
1145 popup->gui->thread->use_standard = standard;
1146 popup->set_text(text);
1147 int n = strlen(text)-1;
1148 int not_standard = n >= 0 && text[n] == '*' ? 1 : 0;
1149 popup->gui->non_standard->update(not_standard ? _("* non-standard format") : "", 0);
1150 return popup->handle_event();
1154 CreateBD_Format::CreateBD_Format(CreateBD_GUI *gui, int x, int y)
1155 : BC_PopupMenu(x, y, xS(200), bd_formats[gui->thread->use_standard].name, 1)
1160 CreateBD_Format::~CreateBD_Format()
1164 void CreateBD_Format::create_objects()
1166 BC_SubMenu *submenu = 0;
1167 CreateBD_FormatItem *item = 0;
1169 for( int i=0; i<(int)(sizeof(bd_formats)/sizeof(bd_formats[0])); ++i ) {
1170 if( ww != bd_formats[i].w || hh != bd_formats[i].h ) {
1171 ww = bd_formats[i].w; hh = bd_formats[i].h;
1172 char string[BCSTRLEN];
1173 sprintf(string, "%dx%d", ww,hh);
1174 add_item(item = new CreateBD_FormatItem(this, -1, string));
1175 item->add_submenu(submenu = new BC_SubMenu());
1177 submenu->add_submenuitem(new CreateBD_FormatItem(this, i, bd_formats[i].name));
1181 int CreateBD_Format::handle_event()
1183 gui->thread->option_presets();
1189 CreateBD_ScaleItem::CreateBD_ScaleItem(CreateBD_Scale *popup,
1190 int scale, const char *text)
1193 this->popup = popup;
1194 this->scale = scale;
1197 CreateBD_ScaleItem::~CreateBD_ScaleItem()
1201 int CreateBD_ScaleItem::handle_event()
1203 popup->gui->thread->use_scale = scale;
1204 popup->set_value(scale);
1205 return popup->handle_event();
1209 CreateBD_Scale::CreateBD_Scale(CreateBD_GUI *gui, int x, int y)
1210 : BC_PopupMenu(x, y, xS(140), "", 1)
1215 CreateBD_Scale::~CreateBD_Scale()
1219 void CreateBD_Scale::create_objects()
1222 for( int i=0; i<(int)Rescale::n_scale_types; ++i ) {
1223 add_item(new CreateBD_ScaleItem(this, i, Rescale::scale_types[i]));
1225 set_value(gui->thread->use_scale);
1228 int CreateBD_Scale::handle_event()
1235 CreateBD_MediaSize::CreateBD_MediaSize(CreateBD_GUI *gui, int x, int y)
1236 : BC_PopupTextBox(gui, 0, 0, x, y, xS(70),yS(50))
1241 CreateBD_MediaSize::~CreateBD_MediaSize()
1245 int CreateBD_MediaSize::handle_event()
1247 gui->disk_space->update();
1251 CreateBD_Profile::CreateBD_Profile(CreateBD_GUI *gui, int x, int y)
1252 : BC_PopupTextBox(gui, 0, 0, x, y, xS(170),yS(50))
1255 strcpy(gui->thread->use_profile, "bluray.m2ts");
1258 CreateBD_Profile::~CreateBD_Profile()
1262 int CreateBD_Profile::handle_event()
1264 strcpy(gui->thread->use_profile, get_text());