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
24 #include "dvdcreate.h"
28 #include "edlsession.h"
31 #include "interlacemodes.h"
34 #include "mainerror.h"
37 #include "mwindowgui.h"
39 #include "pluginset.h"
40 #include "preferences.h"
48 #if !defined(__FreeBSD__)
50 #if !defined(__NetBSD__)
51 #include <sys/statfs.h>
54 #include <sys/param.h>
55 #include <sys/mount.h>
58 #if defined(__NetBSD__)
59 #include <sys/statvfs.h>
61 #define statfs statvfs
66 #define DVD_PAL_16x9 1
67 #define DVD_NTSC_4x3 2
68 #define DVD_NTSC_16x9 3
70 #define DVD_NORM_PAL 0
71 #define DVD_NORM_NTSC 1
73 #define DVD_ASPECT_4x3 0
74 #define DVD_ASPECT_16x9 1
76 static struct dvd_norm {
81 { "PAL", 720,576, 25 },
82 { "NTSC", 720,480, 29.97 },
85 static struct dvd_aspect {
94 static struct dvd_format {
97 { DVD_NORM_PAL, DVD_ASPECT_4x3, },
98 { DVD_NORM_PAL, DVD_ASPECT_16x9, },
99 { DVD_NORM_NTSC, DVD_ASPECT_4x3, },
100 { DVD_NORM_NTSC, DVD_ASPECT_16x9, },
103 const int64_t CreateDVD_Thread::DVD_SIZE = 4700000000;
104 const int CreateDVD_Thread::DVD_STREAMS = 1;
105 const int CreateDVD_Thread::DVD_WIDTH = 720;
106 const int CreateDVD_Thread::DVD_HEIGHT = 480;
107 const double CreateDVD_Thread::DVD_ASPECT_WIDTH = 4.;
108 const double CreateDVD_Thread::DVD_ASPECT_HEIGHT = 3.;
109 const double CreateDVD_Thread::DVD_WIDE_ASPECT_WIDTH = 16.;
110 const double CreateDVD_Thread::DVD_WIDE_ASPECT_HEIGHT = 9.;
111 const double CreateDVD_Thread::DVD_FRAMERATE = 30000. / 1001.;
112 const int CreateDVD_Thread::DVD_MAX_BITRATE = 8000000;
113 const int CreateDVD_Thread::DVD_CHANNELS = 2;
114 const int CreateDVD_Thread::DVD_WIDE_CHANNELS = 6;
115 const double CreateDVD_Thread::DVD_SAMPLERATE = 48000;
116 const double CreateDVD_Thread::DVD_KAUDIO_RATE = 224;
119 CreateDVD_MenuItem::CreateDVD_MenuItem(MWindow *mwindow)
120 : BC_MenuItem(_("DVD Render..."), _("Alt-d"), 'd')
123 this->mwindow = mwindow;
126 int CreateDVD_MenuItem::handle_event()
128 mwindow->create_dvd->start();
133 DVD_BatchRenderJob::DVD_BatchRenderJob(Preferences *preferences,
134 int labeled, int farmed, int standard, int muxed)
135 : BatchRenderJob("DVD_JOB", preferences, labeled, farmed)
137 this->standard = standard;
145 void DVD_BatchRenderJob::copy_from(DVD_BatchRenderJob *src)
147 standard = src->standard;
149 BatchRenderJob::copy_from(src);
152 DVD_BatchRenderJob *DVD_BatchRenderJob::copy()
154 DVD_BatchRenderJob *t = new DVD_BatchRenderJob(preferences,
155 labeled, farmed, standard, muxed);
160 void DVD_BatchRenderJob::load(FileXML *file)
162 standard = file->tag.get_property("STANDARD", standard);
163 muxed = file->tag.get_property("MUXED", muxed);
164 BatchRenderJob::load(file);
167 void DVD_BatchRenderJob::save(FileXML *file)
169 file->tag.set_property("STANDARD", standard);
170 file->tag.set_property("MUXED", muxed);
171 BatchRenderJob::save(file);
174 void DVD_BatchRenderJob::create_chapter(double pos)
176 fprintf(fp,"%s", !chapter++? "\" chapters=\"" : ",");
177 int secs = pos, mins = secs/60;
178 int frms = (pos-secs) * edl->session->frame_rate;
179 fprintf(fp,"%d:%02d:%02d.%d", mins/60, mins%60, secs%60, frms);
182 char *DVD_BatchRenderJob::create_script(EDL *edl, ArrayList<Indexable *> *idxbls)
184 char script[BCTEXTLEN];
185 strcpy(script, edl_path);
188 char *bp = strrchr(script,'/');
191 strcpy(bp, "/dvd.sh");
192 fd = open(script, O_WRONLY+O_CREAT+O_TRUNC, 0755);
195 fp = fdopen(fd, "w");
197 char err[BCTEXTLEN], msg[BCTEXTLEN];
198 strerror_r(errno, err, sizeof(err));
199 sprintf(msg, _("Unable to save: %s\n-- %s"), script, err);
200 MainError::show_error(msg);
204 fprintf(fp,"#!/bin/bash\n");
205 fprintf(fp,"sdir=`dirname $0`\n");
206 fprintf(fp,"dir=`cd \"$sdir\"; pwd`\n");
207 fprintf(fp,"echo \"running %s\"\n", script);
209 const char *exec_path = File::get_cinlib_path();
210 fprintf(fp,"PATH=$PATH:%s\n",exec_path);
211 int file_seq = farmed || labeled ? 1 : 0;
214 fprintf(fp, "cat > $dir/dvd.m2v $dir/dvd.m2v[0-9]*\n");
215 fprintf(fp, "mplex -M -f 8 -o $dir/dvd.mpg $dir/dvd.m2v $dir/dvd.ac3\n");
219 fprintf(fp, "mplex -f 8 -o $dir/dvd.mpg $dir/dvd.m2v $dir/dvd.ac3\n");
221 fprintf(fp,"rm -rf $dir/iso\n");
222 fprintf(fp,"mkdir -p $dir/iso\n");
224 // dvdauthor ver 0.7.0 requires this to work
225 int norm = dvd_formats[standard].norm;
226 const char *name = dvd_norms[norm].name;
227 fprintf(fp,"export VIDEO_FORMAT=%s\n", name);
228 fprintf(fp,"dvdauthor -x - <<eof\n");
229 fprintf(fp,"<dvdauthor dest=\"$dir/iso\">\n");
230 fprintf(fp," <vmgm>\n");
231 fprintf(fp," <fpc> jump title 1; </fpc>\n");
232 fprintf(fp," </vmgm>\n");
233 fprintf(fp," <titleset>\n");
234 fprintf(fp," <titles>\n");
235 char std[BCSTRLEN], *cp = std;
236 for( const char *np=name; *np!=0; ++cp,++np) *cp = *np + 'a'-'A';
238 EDLSession *session = edl->session;
239 fprintf(fp," <video format=\"%s\" aspect=\"%d:%d\" resolution=\"%dx%d\"/>\n",
240 std, (int)session->aspect_w, (int)session->aspect_h,
241 session->output_w, session->output_h);
242 fprintf(fp," <audio format=\"ac3\" lang=\"en\"/>\n");
243 fprintf(fp," <pgc>\n");
244 int total_idxbls = !file_seq ? 1 : idxbls->size();
247 double total_length = edl->tracks->total_length();
248 Label *label = edl->labels->first;
249 for( int i=0; i<total_idxbls; ++i ) {
250 Indexable *idxbl = idxbls->get(i);
251 double video_length = idxbl->have_video() && idxbl->get_frame_rate() > 0 ?
252 (double)idxbl->get_video_frames() / idxbl->get_frame_rate() : 0 ;
253 double audio_length = idxbl->have_audio() && idxbl->get_sample_rate() > 0 ?
254 (double)idxbl->get_audio_samples() / idxbl->get_sample_rate() : 0 ;
255 double length = idxbl->have_video() && idxbl->have_audio() ?
256 bmin(video_length, audio_length) :
257 idxbl->have_video() ? video_length :
258 idxbl->have_audio() ? audio_length : 0;
259 fprintf(fp," <vob file=\"%s", !file_seq ? "$dir/dvd.mpg" : idxbl->path);
261 double vob_end = i+1>=total_idxbls ? total_length : vob_pos + length;
263 while( label && label->position < vob_end ) {
264 create_chapter(label->position - vob_pos);
269 while( secs < vob_end ) {
270 create_chapter(secs - vob_pos);
271 secs += 10*60; // ch every 10 minutes
274 fprintf(fp,"\"/>\n");
277 fprintf(fp," </pgc>\n");
278 fprintf(fp," </titles>\n");
279 fprintf(fp," </titleset>\n");
280 fprintf(fp,"</dvdauthor>\n");
283 fprintf(fp,"echo To burn dvd, load blank media and run:\n");
284 fprintf(fp,"echo growisofs -dvd-compat -Z /dev/dvd -dvd-video $dir/iso\n");
285 fprintf(fp,"kill $$\n");
288 return cstrdup(script);
292 CreateDVD_Thread::CreateDVD_Thread(MWindow *mwindow)
295 this->mwindow = mwindow;
297 this->use_deinterlace = 0;
299 this->use_histogram = 0;
300 this->use_inverse_telecine = 0;
301 this->use_wide_audio = 0;
302 this->use_ffmpeg = 0;
303 this->use_resize_tracks = 0;
304 this->use_labeled = 0;
305 this->use_farmed = 0;
307 this->dvd_size = DVD_SIZE;
308 this->dvd_width = DVD_WIDTH;
309 this->dvd_height = DVD_HEIGHT;
310 this->dvd_aspect_width = DVD_ASPECT_WIDTH;
311 this->dvd_aspect_height = DVD_ASPECT_HEIGHT;
312 this->dvd_framerate = DVD_FRAMERATE;
313 this->dvd_samplerate = DVD_SAMPLERATE;
314 this->dvd_max_bitrate = DVD_MAX_BITRATE;
315 this->dvd_kaudio_rate = DVD_KAUDIO_RATE;
316 this->max_w = this->max_h = 0;
319 CreateDVD_Thread::~CreateDVD_Thread()
324 int CreateDVD_Thread::create_dvd_jobs(ArrayList<BatchRenderJob*> *jobs, const char *asset_dir)
326 EDL *edl = mwindow->edl;
327 if( !edl || !edl->session ) {
329 sprintf(msg, _("No EDL/Session"));
330 MainError::show_error(msg);
333 EDLSession *session = edl->session;
334 double total_length = edl->tracks->total_length();
335 if( total_length <= 0 ) {
337 sprintf(msg, _("No content: %s"), asset_title);
338 MainError::show_error(msg);
342 if( mkdir(asset_dir, 0777) ) {
343 char err[BCTEXTLEN], msg[BCTEXTLEN];
344 strerror_r(errno, err, sizeof(err));
345 sprintf(msg, _("Unable to create directory: %s\n-- %s"), asset_dir, err);
346 MainError::show_error(msg);
350 double old_samplerate = session->sample_rate;
351 double old_framerate = session->frame_rate;
353 session->video_channels = DVD_STREAMS;
354 session->video_tracks = DVD_STREAMS;
355 session->frame_rate = dvd_framerate;
356 session->output_w = dvd_width;
357 session->output_h = dvd_height;
358 session->aspect_w = dvd_aspect_width;
359 session->aspect_h = dvd_aspect_height;
360 session->sample_rate = dvd_samplerate;
361 session->audio_channels = session->audio_tracks =
362 use_wide_audio ? DVD_WIDE_CHANNELS : DVD_CHANNELS;
364 session->audio_channels = session->audio_tracks =
365 !use_wide_audio ? DVD_CHANNELS : DVD_WIDE_CHANNELS;
366 for( int i=0; i<MAX_CHANNELS; ++i )
367 session->achannel_positions[i] = default_audio_channel_position(i, session->audio_channels);
368 int audio_mapping = edl->tracks->recordable_audio_tracks() == DVD_WIDE_CHANNELS &&
369 !use_wide_audio ? MWindow::AUDIO_5_1_TO_2 : MWindow::AUDIO_1_TO_1;
370 mwindow->remap_audio(audio_mapping);
372 double new_samplerate = session->sample_rate;
373 double new_framerate = session->frame_rate;
376 edl->resample(old_samplerate, new_samplerate, TRACK_AUDIO);
377 edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
379 int64_t aud_size = ((dvd_kaudio_rate * total_length)/8 + 1000-1) * 1000;
380 int64_t vid_size = dvd_size*0.96 - aud_size;
381 int64_t vid_bitrate = (vid_size * 8) / total_length;
382 vid_bitrate /= 1000; vid_bitrate *= 1000;
383 if( vid_bitrate > dvd_max_bitrate )
384 vid_bitrate = dvd_max_bitrate;
386 char xml_filename[BCTEXTLEN];
387 sprintf(xml_filename, "%s/dvd.xml", asset_dir);
389 edl->save_xml(&xml_file, xml_filename);
390 xml_file.terminate_string();
391 if( xml_file.write_to_file(xml_filename) ) {
393 sprintf(msg, _("Unable to save: %s"), xml_filename);
394 MainError::show_error(msg);
398 BatchRenderJob *job = new DVD_BatchRenderJob(mwindow->preferences,
399 use_labeled, use_farmed, use_standard, 0);// use_ffmpeg);
401 strcpy(&job->edl_path[0], xml_filename);
402 Asset *asset = job->asset;
404 asset->layers = DVD_STREAMS;
405 asset->frame_rate = session->frame_rate;
406 asset->width = session->output_w;
407 asset->height = session->output_h;
408 asset->aspect_ratio = session->aspect_w / session->aspect_h;
411 char option_path[BCTEXTLEN];
412 sprintf(&asset->path[0],"%s/dvd.mpg", asset_dir);
413 asset->format = FILE_FFMPEG;
414 strcpy(asset->fformat, "dvd");
415 // if there are many renderfarm jobs, then there are small audio fragments of
416 // silence that are used at the end of a render to fill the last audio "block".
417 // this extra data gradually skews the audio/video sync. Therefore, the audio
418 // is not rendered muxed for ffmpeg, and is remuxed as with mjpeg rendering.
419 // since this audio is in one file, the only fragment is at the end and is ok.
421 asset->audio_data = 1;
422 asset->channels = session->audio_channels;
423 asset->sample_rate = session->sample_rate;
424 strcpy(asset->acodec, "dvd.dvd");
425 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
426 FFMPEG::load_options(option_path, asset->ff_audio_options,
427 sizeof(asset->ff_audio_options));
428 asset->ff_audio_bitrate = dvd_kaudio_rate * 1000;
430 asset->video_data = 1;
431 strcpy(asset->vcodec, "dvd.dvd");
432 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
433 FFMPEG::load_options(option_path, asset->ff_video_options,
434 sizeof(asset->ff_video_options));
435 asset->ff_video_bitrate = vid_bitrate;
436 asset->ff_video_quality = -1;
437 use_farmed = job->farmed;
439 asset->video_data = 1;
440 strcpy(asset->vcodec, "raw.dvd");
441 sprintf(&asset->path[0],"%s/dvd.m2v", asset_dir);
442 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
443 FFMPEG::load_options(option_path, asset->ff_video_options,
444 sizeof(asset->ff_video_options));
445 asset->ff_video_bitrate = vid_bitrate;
446 asset->ff_video_quality = -1;
447 use_farmed = job->farmed;
449 job = new BatchRenderJob(mwindow->preferences, 0, 0);
451 strcpy(&job->edl_path[0], xml_filename);
453 sprintf(&asset->path[0],"%s/dvd.ac3", asset_dir);
454 asset->format = FILE_AC3;
455 asset->audio_data = 1;
456 asset->channels = session->audio_channels;
457 asset->sample_rate = session->sample_rate;
458 asset->ac3_bitrate = dvd_kaudio_rate;
462 sprintf(&asset->path[0],"%s/dvd.m2v", asset_dir);
463 asset->video_data = 1;
464 asset->format = FILE_VMPEG;
465 asset->vmpeg_cmodel = BC_YUV420P;
466 asset->vmpeg_fix_bitrate = 1;
467 asset->vmpeg_bitrate = vid_bitrate;
468 asset->vmpeg_quantization = 15;
469 asset->vmpeg_iframe_distance = 15;
470 if(session->interlace_mode == ILACE_MODE_NOTINTERLACED || use_deinterlace)
471 { asset->vmpeg_progressive = 1; } else {
472 asset->vmpeg_progressive = 0; }
473 asset->vmpeg_denoise = 0;
474 asset->vmpeg_seq_codes = 0;
475 asset->vmpeg_derivative = 2;
476 asset->vmpeg_preset = 8;
477 asset->vmpeg_field_order = 0;
478 if(session->interlace_mode == ILACE_MODE_BOTTOM_FIRST && !use_deinterlace)
479 { asset->vmpeg_field_order = 1; } else {
480 asset->vmpeg_field_order = 0; }
481 asset->vmpeg_pframe_distance = 0;
482 use_farmed = job->farmed;
483 job = new BatchRenderJob(mwindow->preferences, 0, 0);
485 strcpy(&job->edl_path[0], xml_filename);
488 sprintf(&asset->path[0],"%s/dvd.ac3", asset_dir);
489 asset->audio_data = 1;
490 asset->format = FILE_AC3;
491 asset->channels = session->audio_channels;
492 asset->sample_rate = session->sample_rate;
494 asset->byte_order = 0;
498 asset->ac3_bitrate = dvd_kaudio_rate;
504 void CreateDVD_Thread::handle_close_event(int result)
507 mwindow->defaults->update("WORK_DIRECTORY", tmp_path);
508 mwindow->batch_render->load_defaults(mwindow->defaults);
509 mwindow->undo->update_undo_before();
510 KeyFrame keyframe; char data[BCTEXTLEN];
511 if( use_deinterlace ) {
512 sprintf(data,"<DEINTERLACE MODE=1>");
513 keyframe.set_data(data);
514 insert_video_plugin("Deinterlace", &keyframe);
516 if( use_inverse_telecine ) {
517 sprintf(data,"<IVTC FRAME_OFFSET=0 FIRST_FIELD=0 "
518 "AUTOMATIC=1 AUTO_THRESHOLD=2.0e+00 PATTERN=2>");
519 keyframe.set_data(data);
520 insert_video_plugin("Inverse Telecine", &keyframe);
522 if( use_scale != Rescale::none ) {
523 double dvd_aspect = dvd_aspect_height > 0 ? dvd_aspect_width/dvd_aspect_height : 1;
525 Tracks *tracks = mwindow->edl->tracks;
526 for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
527 if( vtrk->data_type != TRACK_VIDEO ) continue;
528 if( !vtrk->is_armed() ) continue;
529 vtrk->expand_view = 1;
530 PluginSet *plugin_set = new PluginSet(mwindow->edl, vtrk);
531 vtrk->plugin_set.append(plugin_set);
532 Edits *edits = vtrk->edits;
533 for( Edit *edit=edits->first; edit; edit=edit->next ) {
534 Indexable *indexable = edit->get_source();
535 if( !indexable ) continue;
536 Rescale in(indexable);
537 Rescale out(dvd_width, dvd_height, dvd_aspect);
538 float src_w, src_h, dst_w, dst_h;
539 in.rescale(out,use_scale, src_w,src_h, dst_w,dst_h);
540 sprintf(data,"<SCALERATIO TYPE=%d"
541 " IN_W=%d IN_H=%d IN_ASPECT_RATIO=%f"
542 " OUT_W=%d OUT_H=%d OUT_ASPECT_RATIO=%f"
543 " SRC_X=%f SRC_Y=%f SRC_W=%f SRC_H=%f"
544 " DST_X=%f DST_Y=%f DST_W=%f DST_H=%f>", use_scale,
545 in.w, in.h, in.aspect, out.w, out.h, out.aspect,
546 0., 0., src_w, src_h, 0., 0., dst_w, dst_h);
547 keyframe.set_data(data);
548 plugin_set->insert_plugin(_("Scale Ratio"),
549 edit->startproject, edit->length,
550 PLUGIN_STANDALONE, 0, &keyframe, 0);
556 if( use_resize_tracks )
558 if( use_histogram ) {
560 sprintf(data, "<HISTOGRAM OUTPUT_MIN_0=0 OUTPUT_MAX_0=1 "
561 "OUTPUT_MIN_1=0 OUTPUT_MAX_1=1 "
562 "OUTPUT_MIN_2=0 OUTPUT_MAX_2=1 "
563 "OUTPUT_MIN_3=0 OUTPUT_MAX_3=1 "
564 "AUTOMATIC=0 THRESHOLD=9.0-01 PLOT=0 SPLIT=0>"
565 "<POINTS></POINTS><POINTS></POINTS><POINTS></POINTS>"
566 "<POINTS><POINT X=6.0e-02 Y=0>"
567 "<POINT X=9.4e-01 Y=1></POINTS>");
569 sprintf(data, "<HISTOGRAM AUTOMATIC=0 THRESHOLD=1.0e-01 "
570 "PLOT=0 SPLIT=0 W=440 H=500 PARADE=0 MODE=3 "
571 "LOW_OUTPUT_0=0 HIGH_OUTPUT_0=1 LOW_INPUT_0=0 HIGH_INPUT_0=1 GAMMA_0=1 "
572 "LOW_OUTPUT_1=0 HIGH_OUTPUT_1=1 LOW_INPUT_1=0 HIGH_INPUT_1=1 GAMMA_1=1 "
573 "LOW_OUTPUT_2=0 HIGH_OUTPUT_2=1 LOW_INPUT_2=0 HIGH_INPUT_2=1 GAMMA_2=1 "
574 "LOW_OUTPUT_3=0 HIGH_OUTPUT_3=1 LOW_INPUT_3=0.044 HIGH_INPUT_3=0.956 "
577 keyframe.set_data(data);
578 insert_video_plugin("Histogram", &keyframe);
580 char asset_dir[BCTEXTLEN], jobs_path[BCTEXTLEN];
581 snprintf(asset_dir, sizeof(asset_dir), "%s/%s", tmp_path, asset_title);
582 snprintf(jobs_path, sizeof(jobs_path), "%s/dvd.jobs", asset_dir);
583 mwindow->batch_render->reset(jobs_path);
584 int ret = create_dvd_jobs(&mwindow->batch_render->jobs, asset_dir);
585 mwindow->undo->update_undo_after(_("create dvd"), LOAD_ALL);
586 mwindow->resync_guis();
588 mwindow->batch_render->save_jobs();
589 mwindow->batch_render->start(-use_farmed, -use_labeled);
592 BC_Window* CreateDVD_Thread::new_gui()
594 strcpy(tmp_path,"/tmp");
595 mwindow->defaults->get("WORK_DIRECTORY", tmp_path);
596 memset(asset_title,0,sizeof(asset_title));
597 time_t dt; time(&dt);
598 struct tm dtm; localtime_r(&dt, &dtm);
599 sprintf(asset_title, "dvd_%02d%02d%02d-%02d%02d%02d",
600 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
601 dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
603 use_scale = Rescale::none;
605 use_inverse_telecine = 0;
608 use_resize_tracks = 0;
611 use_standard = DVD_NTSC_4x3;
614 dvd_width = DVD_WIDTH;
615 dvd_height = DVD_HEIGHT;
616 dvd_aspect_width = DVD_ASPECT_WIDTH;
617 dvd_aspect_height = DVD_ASPECT_HEIGHT;
618 dvd_framerate = DVD_FRAMERATE;
619 dvd_samplerate = DVD_SAMPLERATE;
620 dvd_max_bitrate = DVD_MAX_BITRATE;
621 dvd_kaudio_rate = DVD_KAUDIO_RATE;
622 max_w = 0; max_h = 0;
624 int has_standard = -1;
626 EDLSession *session = mwindow->edl->session;
627 double framerate = session->frame_rate;
628 double aspect_ratio = session->aspect_h > 0 ?
629 session->aspect_w / session->aspect_h > 0 : 1;
630 int output_w = session->output_w, output_h = session->output_h;
631 // match the session to any known standard
632 for( int i=0; i<(int)(sizeof(dvd_formats)/sizeof(dvd_formats[0])); ++i ) {
633 int norm = dvd_formats[i].norm;
634 if( !EQUIV(framerate, dvd_norms[norm].framerate) ) continue;
635 if( output_w != dvd_norms[norm].w ) continue;
636 if( output_h != dvd_norms[norm].h ) continue;
637 int aspect = dvd_formats[i].aspect;
638 double dvd_aspect_ratio =
639 (double)dvd_aspects[aspect].w / dvd_aspects[aspect].h;
640 if( !EQUIV(aspect_ratio, dvd_aspect_ratio) ) continue;
641 has_standard = i; break;
643 if( has_standard < 0 ) {
644 // or use the default standard
645 if( !strcmp(mwindow->default_standard, "NTSC") ) has_standard = DVD_NTSC_4x3;
646 else if( !strcmp(mwindow->default_standard, "PAL") ) has_standard = DVD_PAL_4x3;
649 if( has_standard >= 0 )
650 use_standard = has_standard;
653 int scr_x = mwindow->gui->get_screen_x(0, -1);
654 int scr_w = mwindow->gui->get_screen_w(0, -1);
655 int scr_h = mwindow->gui->get_screen_h(0, -1);
656 int w = xS(560), h = yS(280);
657 int x = scr_x + scr_w/2 - w/2, y = scr_h/2 - h/2;
659 gui = new CreateDVD_GUI(this, x, y, w, h);
660 gui->create_objects();
665 CreateDVD_OK::CreateDVD_OK(CreateDVD_GUI *gui, int x, int y)
669 set_tooltip(_("end setup, start batch render"));
672 CreateDVD_OK::~CreateDVD_OK()
676 int CreateDVD_OK::button_press_event()
678 if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
685 int CreateDVD_OK::keypress_event()
687 return context_help_check_and_show();
691 CreateDVD_Cancel::CreateDVD_Cancel(CreateDVD_GUI *gui, int x, int y)
692 : BC_CancelButton(x, y)
697 CreateDVD_Cancel::~CreateDVD_Cancel()
701 int CreateDVD_Cancel::button_press_event()
703 if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
711 CreateDVD_DiskSpace::CreateDVD_DiskSpace(CreateDVD_GUI *gui, int x, int y)
712 : BC_Title(x, y, "", MEDIUMFONT, GREEN)
717 CreateDVD_DiskSpace::~CreateDVD_DiskSpace()
721 int64_t CreateDVD_DiskSpace::tmp_path_space()
723 const char *path = gui->thread->tmp_path;
724 if( access(path,R_OK+W_OK) ) return 0;
726 if( statfs(path, &sfs) ) return 0;
727 return (int64_t)sfs.f_bsize * sfs.f_bfree;
730 void CreateDVD_DiskSpace::update()
732 static const char *suffix[] = { "", "KB", "MB", "GB", "TB", "PB" };
733 int64_t disk_space = tmp_path_space();
734 double media_size = 15e9, msz = 0, m = 1;
736 if( sscanf(gui->media_size->get_text(), "%lf%s", &msz, sfx) == 2 ) {
737 int i = sizeof(suffix)/sizeof(suffix[0]);
738 while( --i >= 0 && strcmp(sfx, suffix[i]) );
739 while( --i >= 0 ) m *= 1000;
740 media_size = msz * m;
742 m = gui->thread->use_ffmpeg ? 2 : 3;
743 int color = disk_space < media_size*m ? RED : GREEN;
745 for( int64_t space=disk_space; i<5 && (space/=1000)>0; disk_space=space, ++i );
746 char text[BCTEXTLEN];
747 sprintf(text, "%s%3jd%s", _("disk space: "), disk_space, suffix[i]);
748 gui->disk_space->BC_Title::update(text);
749 gui->disk_space->set_color(color);
752 CreateDVD_TmpPath::CreateDVD_TmpPath(CreateDVD_GUI *gui, int x, int y, int w)
753 : BC_TextBox(x, y, w, 1, -(int)sizeof(gui->thread->tmp_path),
754 gui->thread->tmp_path, 1, MEDIUMFONT)
759 CreateDVD_TmpPath::~CreateDVD_TmpPath()
763 int CreateDVD_TmpPath::handle_event()
766 gui->disk_space->update();
771 CreateDVD_AssetTitle::CreateDVD_AssetTitle(CreateDVD_GUI *gui, int x, int y, int w)
772 : BC_TextBox(x, y, w, 1, -(int)sizeof(gui->thread->asset_title),
773 gui->thread->asset_title, 1, MEDIUMFONT)
778 CreateDVD_AssetTitle::~CreateDVD_AssetTitle()
782 int CreateDVD_AssetTitle::handle_event()
789 CreateDVD_Deinterlace::CreateDVD_Deinterlace(CreateDVD_GUI *gui, int x, int y)
790 : BC_CheckBox(x, y, &gui->thread->use_deinterlace, _("Deinterlace"))
795 CreateDVD_Deinterlace::~CreateDVD_Deinterlace()
799 int CreateDVD_Deinterlace::handle_event()
802 gui->need_inverse_telecine->set_value(0);
803 gui->thread->use_inverse_telecine = 0;
805 return BC_CheckBox::handle_event();
809 CreateDVD_InverseTelecine::CreateDVD_InverseTelecine(CreateDVD_GUI *gui, int x, int y)
810 : BC_CheckBox(x, y, &gui->thread->use_inverse_telecine, _("Inverse Telecine"))
815 CreateDVD_InverseTelecine::~CreateDVD_InverseTelecine()
819 int CreateDVD_InverseTelecine::handle_event()
822 gui->need_deinterlace->set_value(0);
823 gui->thread->use_deinterlace = 0;
825 return BC_CheckBox::handle_event();
829 CreateDVD_ResizeTracks::CreateDVD_ResizeTracks(CreateDVD_GUI *gui, int x, int y)
830 : BC_CheckBox(x, y, &gui->thread->use_resize_tracks, _("Resize Tracks"))
835 CreateDVD_ResizeTracks::~CreateDVD_ResizeTracks()
840 CreateDVD_Histogram::CreateDVD_Histogram(CreateDVD_GUI *gui, int x, int y)
841 : BC_CheckBox(x, y, &gui->thread->use_histogram, _("Histogram"))
846 CreateDVD_Histogram::~CreateDVD_Histogram()
850 CreateDVD_LabelChapters::CreateDVD_LabelChapters(CreateDVD_GUI *gui, int x, int y)
851 : BC_CheckBox(x, y, &gui->thread->use_labeled, _("Chapters at Labels"))
856 CreateDVD_LabelChapters::~CreateDVD_LabelChapters()
860 CreateDVD_UseRenderFarm::CreateDVD_UseRenderFarm(CreateDVD_GUI *gui, int x, int y)
861 : BC_CheckBox(x, y, &gui->thread->use_farmed, _("Use render farm"))
866 CreateDVD_UseRenderFarm::~CreateDVD_UseRenderFarm()
870 CreateDVD_WideAudio::CreateDVD_WideAudio(CreateDVD_GUI *gui, int x, int y)
871 : BC_CheckBox(x, y, &gui->thread->use_wide_audio, _("Audio 5.1"))
876 CreateDVD_WideAudio::~CreateDVD_WideAudio()
880 CreateDVD_UseFFMpeg::CreateDVD_UseFFMpeg(CreateDVD_GUI *gui, int x, int y)
881 : BC_CheckBox(x, y, &gui->thread->use_ffmpeg, _("Use FFMPEG"))
886 CreateDVD_UseFFMpeg::~CreateDVD_UseFFMpeg()
893 CreateDVD_GUI::CreateDVD_GUI(CreateDVD_Thread *thread, int x, int y, int w, int h)
894 : BC_Window(_(PROGRAM_NAME ": Create DVD"), x, y, w, h, xS(50), yS(50), 1, 0, 1)
896 this->thread = thread;
897 at_x = at_y = tmp_x = tmp_y = 0;
898 ok_x = ok_y = ok_w = ok_h = 0;
899 cancel_x = cancel_y = cancel_w = cancel_h = 0;
906 need_deinterlace = 0;
907 need_inverse_telecine = 0;
908 need_resize_tracks = 0;
915 // *** CONTEXT_HELP ***
916 context_help_set_keyword("DVD and Bluray Creation");
919 CreateDVD_GUI::~CreateDVD_GUI()
923 void CreateDVD_GUI::create_objects()
925 int xs10 = xS(10), xs35 = xS(35), xs170 = xS(170);
926 int ys5 = yS(5), ys10 = yS(10);
927 lock_window("CreateDVD_GUI::create_objects");
928 int pady = BC_TextBox::calculate_h(this, MEDIUMFONT, 0, 1) + ys5;
929 int padx = BC_Title::calculate_w(this, (char*)"X", MEDIUMFONT);
930 int x = padx/2, y = pady/2;
931 BC_Title *title = new BC_Title(x, y, _("Title:"), MEDIUMFONT, YELLOW);
932 add_subwindow(title);
933 at_x = x + title->get_w(); at_y = y;
934 asset_title = new CreateDVD_AssetTitle(this, at_x, at_y, get_w()-at_x-xs10);
935 add_subwindow(asset_title);
936 y += title->get_h() + pady/2;
937 title = new BC_Title(x, y, _("Work path:"), MEDIUMFONT, YELLOW);
938 add_subwindow(title);
939 tmp_x = x + title->get_w(); tmp_y = y;
940 tmp_path = new CreateDVD_TmpPath(this, tmp_x, tmp_y, get_w()-tmp_x-xs35);
941 add_subwindow(tmp_path);
942 btmp_path = new BrowseButton(thread->mwindow->theme, this, tmp_path,
943 tmp_x+tmp_path->get_w(), tmp_y, "/tmp",
944 _("Work path"), _("Select a Work directory:"), 1);
945 add_subwindow(btmp_path);
946 y += title->get_h() + pady/2;
947 disk_space = new CreateDVD_DiskSpace(this, x, y);
948 add_subwindow(disk_space);
949 int x0 = get_w() - xs170;
950 title = new BC_Title(x0, y, _("Media:"), MEDIUMFONT, YELLOW);
951 add_subwindow(title);
952 int x1 = x0+title->get_w()+padx;
953 media_size = new CreateDVD_MediaSize(this, x1, y);
954 media_size->create_objects();
955 media_sizes.append(new BC_ListBoxItem("4.7GB"));
956 media_sizes.append(new BC_ListBoxItem("8.3GB"));
957 media_size->update_list(&media_sizes);
958 media_size->update(media_sizes[0]->get_text());
959 disk_space->update();
960 y += disk_space->get_h() + pady/2;
961 title = new BC_Title(x, y, _("Format:"), MEDIUMFONT, YELLOW);
962 add_subwindow(title);
963 standard = new CreateDVD_Format(this, title->get_w() + padx, y);
964 add_subwindow(standard);
965 standard->create_objects();
967 title = new BC_Title(x0, y, _("Scale:"), MEDIUMFONT, YELLOW);
968 add_subwindow(title);
969 x1 = x0+title->get_w()+padx;
970 scale = new CreateDVD_Scale(this, x1, y);
971 add_subwindow(scale);
972 scale->create_objects();
973 y += standard->get_h() + pady/2;
975 need_deinterlace = new CreateDVD_Deinterlace(this, x1, y);
976 add_subwindow(need_deinterlace);
977 y += need_deinterlace->get_h() + pady/2;
978 need_histogram = new CreateDVD_Histogram(this, x, y);
979 add_subwindow(need_histogram);
981 need_inverse_telecine = new CreateDVD_InverseTelecine(this, x1, y);
982 add_subwindow(need_inverse_telecine);
983 y += need_inverse_telecine->get_h() + pady/2;
984 need_wide_audio = new CreateDVD_WideAudio(this, x1, y);
985 add_subwindow(need_wide_audio);
986 y += need_wide_audio->get_h() + pady/2;
987 need_use_ffmpeg = new CreateDVD_UseFFMpeg(this, x1, y);
988 add_subwindow(need_use_ffmpeg);
989 y += need_use_ffmpeg->get_h() + pady/2;
990 need_resize_tracks = new CreateDVD_ResizeTracks(this, x1, y);
991 add_subwindow(need_resize_tracks);
993 need_labeled = new CreateDVD_LabelChapters(this, x1, y);
994 add_subwindow(need_labeled);
995 y += need_labeled->get_h() + pady/2;
996 need_farmed = new CreateDVD_UseRenderFarm(this, x1, y);
997 add_subwindow(need_farmed);
998 ok_w = BC_OKButton::calculate_w();
999 ok_h = BC_OKButton::calculate_h();
1001 ok_y = get_h() - ok_h - ys10;
1002 ok = new CreateDVD_OK(this, ok_x, ok_y);
1004 cancel_w = BC_CancelButton::calculate_w();
1005 cancel_h = BC_CancelButton::calculate_h();
1006 cancel_x = get_w() - cancel_w - xs10,
1007 cancel_y = get_h() - cancel_h - ys10;
1008 cancel = new CreateDVD_Cancel(this, cancel_x, cancel_y);
1009 add_subwindow(cancel);
1014 int CreateDVD_GUI::resize_event(int w, int h)
1016 int xs10 = xS(10), xs35 = xS(35);
1018 asset_title->reposition_window(at_x, at_y, get_w()-at_x-xs10);
1019 tmp_path->reposition_window(tmp_x, tmp_y, get_w()-tmp_x-xs35);
1020 btmp_path->reposition_window(tmp_x+tmp_path->get_w(), tmp_y);
1021 ok_y = h - ok_h - ys10;
1022 ok->reposition_window(ok_x, ok_y);
1023 cancel_x = w - cancel_w - xs10,
1024 cancel_y = h - cancel_h - ys10;
1025 cancel->reposition_window(cancel_x, cancel_y);
1029 int CreateDVD_GUI::translation_event()
1034 int CreateDVD_GUI::close_event()
1040 void CreateDVD_GUI::update()
1042 scale->set_value(thread->use_scale);
1043 need_deinterlace->set_value(thread->use_deinterlace);
1044 need_inverse_telecine->set_value(thread->use_inverse_telecine);
1045 need_use_ffmpeg->set_value(thread->use_ffmpeg);
1046 need_resize_tracks->set_value(thread->use_resize_tracks);
1047 need_histogram->set_value(thread->use_histogram);
1048 need_wide_audio->set_value(thread->use_wide_audio);
1049 need_labeled->set_value(thread->use_labeled);
1050 need_farmed->set_value(thread->use_farmed);
1053 int CreateDVD_Thread::
1054 insert_video_plugin(const char *title, KeyFrame *default_keyframe)
1056 Tracks *tracks = mwindow->edl->tracks;
1057 for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
1058 if( vtrk->data_type != TRACK_VIDEO ) continue;
1059 if( !vtrk->is_armed() ) continue;
1060 vtrk->expand_view = 1;
1061 PluginSet *plugin_set = new PluginSet(mwindow->edl, vtrk);
1062 vtrk->plugin_set.append(plugin_set);
1063 Edits *edits = vtrk->edits;
1064 for( Edit *edit=edits->first; edit; edit=edit->next ) {
1065 plugin_set->insert_plugin(_(title),
1066 edit->startproject, edit->length,
1067 PLUGIN_STANDALONE, 0, default_keyframe, 0);
1074 int CreateDVD_Thread::
1077 Tracks *tracks = mwindow->edl->tracks;
1078 int trk_w = max_w, trk_h = max_h;
1079 if( trk_w < dvd_width ) trk_w = dvd_width;
1080 if( trk_h < dvd_height ) trk_h = dvd_height;
1081 for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
1082 if( vtrk->data_type != TRACK_VIDEO ) continue;
1083 if( !vtrk->is_armed() ) continue;
1084 vtrk->track_w = trk_w;
1085 vtrk->track_h = trk_h;
1090 int CreateDVD_Thread::
1093 // reset only probed options
1094 use_deinterlace = 0;
1095 use_scale = Rescale::none;
1096 use_resize_tracks = 0;
1101 if( !mwindow->edl ) return 1;
1103 int norm = dvd_formats[use_standard].norm;
1104 dvd_width = dvd_norms[norm].w;
1105 dvd_height = dvd_norms[norm].h;
1106 dvd_framerate = dvd_norms[norm].framerate;
1107 int aspect = dvd_formats[use_standard].aspect;
1108 dvd_aspect_width = dvd_aspects[aspect].w;
1109 dvd_aspect_height = dvd_aspects[aspect].h;
1110 double dvd_aspect = dvd_aspect_height > 0 ? dvd_aspect_width/dvd_aspect_height : 1;
1112 Tracks *tracks = mwindow->edl->tracks;
1113 max_w = 0; max_h = 0;
1114 int has_deinterlace = 0, has_scale = 0;
1115 for( Track *trk=tracks->first; trk; trk=trk->next ) {
1116 if( !trk->is_armed() ) continue;
1117 Edits *edits = trk->edits;
1118 switch( trk->data_type ) {
1120 for( Edit *edit=edits->first; edit; edit=edit->next ) {
1121 if( edit->silence() ) continue;
1122 Indexable *indexable = edit->get_source();
1123 int w = indexable->get_w();
1124 if( w > max_w ) max_w = w;
1125 if( w != dvd_width ) use_scale = Rescale::scaled;
1126 int h = indexable->get_h();
1127 if( h > max_h ) max_h = h;
1128 if( h != dvd_height ) use_scale = Rescale::scaled;
1130 MWindow::create_aspect_ratio(aw, ah, w, h);
1131 double aspect = ah > 0 ? aw / ah : 1;
1132 if( !EQUIV(aspect, dvd_aspect) ) use_scale = Rescale::scaled;
1134 for( int i=0; i<trk->plugin_set.size(); ++i ) {
1135 for( Plugin *plugin = (Plugin*)trk->plugin_set[i]->first;
1136 plugin; plugin=(Plugin*)plugin->next ) {
1137 if( !strcmp(plugin->title, "Deinterlace") )
1138 has_deinterlace = 1;
1139 if( !strcmp(plugin->title, "Auto Scale") ||
1140 !strcmp(plugin->title, "Scale Ratio") ||
1141 !strcmp(plugin->title, "Scale") )
1149 use_scale = Rescale::none;
1150 if( use_scale != Rescale::none ) {
1151 if( max_w != dvd_width ) use_resize_tracks = 1;
1152 if( max_h != dvd_height ) use_resize_tracks = 1;
1154 for( Track *trk=tracks->first; trk && !use_resize_tracks; trk=trk->next ) {
1155 if( !trk->is_armed() ) continue;
1156 switch( trk->data_type ) {
1158 if( trk->track_w != max_w ) use_resize_tracks = 1;
1159 if( trk->track_h != max_h ) use_resize_tracks = 1;
1163 if( !has_deinterlace && max_h > 2*dvd_height ) use_deinterlace = 1;
1164 Labels *labels = mwindow->edl->labels;
1165 use_labeled = labels && labels->first ? 1 : 0;
1167 if( tracks->recordable_audio_tracks() == DVD_WIDE_CHANNELS )
1170 use_farmed = mwindow->preferences->use_renderfarm;
1176 CreateDVD_FormatItem::CreateDVD_FormatItem(CreateDVD_Format *popup,
1177 int standard, const char *text)
1180 this->popup = popup;
1181 this->standard = standard;
1184 CreateDVD_FormatItem::~CreateDVD_FormatItem()
1188 int CreateDVD_FormatItem::handle_event()
1190 popup->set_text(get_text());
1191 popup->gui->thread->use_standard = standard;
1192 return popup->handle_event();
1196 CreateDVD_Format::CreateDVD_Format(CreateDVD_GUI *gui, int x, int y)
1197 : BC_PopupMenu(x, y, xS(180), "", 1)
1202 CreateDVD_Format::~CreateDVD_Format()
1206 void CreateDVD_Format::create_objects()
1208 for( int i=0; i<(int)(sizeof(dvd_formats)/sizeof(dvd_formats[0])); ++i ) {
1209 int norm = dvd_formats[i].norm;
1210 int aspect = dvd_formats[i].aspect;
1211 char item_text[BCTEXTLEN];
1212 sprintf(item_text,"%4s (%5.2f) %dx%d",
1213 dvd_norms[norm].name, dvd_norms[norm].framerate,
1214 dvd_aspects[aspect].w, dvd_aspects[aspect].h);
1215 add_item(new CreateDVD_FormatItem(this, i, item_text));
1217 set_value(gui->thread->use_standard);
1220 int CreateDVD_Format::handle_event()
1222 gui->thread->option_presets();
1228 CreateDVD_ScaleItem::CreateDVD_ScaleItem(CreateDVD_Scale *popup,
1229 int scale, const char *text)
1232 this->popup = popup;
1233 this->scale = scale;
1236 CreateDVD_ScaleItem::~CreateDVD_ScaleItem()
1240 int CreateDVD_ScaleItem::handle_event()
1242 popup->gui->thread->use_scale = scale;
1243 popup->set_value(scale);
1244 return popup->handle_event();
1248 CreateDVD_Scale::CreateDVD_Scale(CreateDVD_GUI *gui, int x, int y)
1249 : BC_PopupMenu(x, y, xS(140), "", 1)
1254 CreateDVD_Scale::~CreateDVD_Scale()
1258 void CreateDVD_Scale::create_objects()
1261 for( int i=0; i<(int)Rescale::n_scale_types; ++i ) {
1262 add_item(new CreateDVD_ScaleItem(this, i, Rescale::scale_types[i]));
1264 set_value(gui->thread->use_scale);
1267 int CreateDVD_Scale::handle_event()
1274 CreateDVD_MediaSize::CreateDVD_MediaSize(CreateDVD_GUI *gui, int x, int y)
1275 : BC_PopupTextBox(gui, 0, 0, x, y, xS(70), 50)
1280 CreateDVD_MediaSize::~CreateDVD_MediaSize()
1284 int CreateDVD_MediaSize::handle_event()
1286 gui->disk_space->update();