add system build to autoconf/automake
[goodguy/history.git] / cinelerra-5.1 / cinelerra / dvdcreate.C
1 #include "asset.h"
2 #include "clip.h"
3 #include "dvdcreate.h"
4 #include "edl.h"
5 #include "edit.h"
6 #include "edits.h"
7 #include "edlsession.h"
8 #include "file.h"
9 #include "filexml.h"
10 #include "keyframe.h"
11 #include "labels.h"
12 #include "mainerror.h"
13 #include "mainundo.h"
14 #include "mwindow.h"
15 #include "mwindowgui.h"
16 #include "plugin.h"
17 #include "pluginset.h"
18 #include "track.h"
19 #include "tracks.h"
20
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <sys/stat.h>
25 #include <sys/statfs.h>
26
27 // DVD Creation
28
29 #define HD_1920x1080_2997       0
30 #define HD_1920x1080_2500       1
31 #define HD_1280x720_5994p       2
32 #define HD_1280x720_5000p       3
33 #define HD_720x576_5000p        4
34 #define HD_720x576_2500         5
35 #define HD_720x480_5994p        6
36 #define HD_720x480_2997         7
37
38 static struct hd_format {
39         const char *name;
40         int w, h;
41         double framerate;
42 } hd_formats[] = {
43         { "1920x1080 29.97",      1920,1080, 29.97  },
44         { "1920x1080 25",         1920,1080, 25     },
45         { "1280x720 59.94p",      1280,720,  59.94  },
46         { "1280x720 50p",         1280,720,  50     },
47         { "720x576 50p(PAL)",      720,576,  50     },
48         { "720x576 25 (PAL)",      720,576,  25     },
49         { "720x480 59.94p(NTSC)",  720,480,  59.94  },
50         { "720x480 29.97 (NTSC)",  720,480,  29.97  },
51 };
52
53 const int64_t CreateDVD_Thread::DVD_SIZE = 4700000000;
54 const int CreateDVD_Thread::DVD_STREAMS = 1;
55 const int CreateDVD_Thread::DVD_WIDTH = 720;
56 const int CreateDVD_Thread::DVD_HEIGHT = 480;
57 const double CreateDVD_Thread::DVD_ASPECT_WIDTH = 4.;
58 const double CreateDVD_Thread::DVD_ASPECT_HEIGHT = 3.;
59 const double CreateDVD_Thread::DVD_WIDE_ASPECT_WIDTH = 16.;
60 const double CreateDVD_Thread::DVD_WIDE_ASPECT_HEIGHT = 9.;
61 const double CreateDVD_Thread::DVD_FRAMERATE = 30000. / 1001.;
62 const int CreateDVD_Thread::DVD_MAX_BITRATE = 8000000;
63 const int CreateDVD_Thread::DVD_CHANNELS = 2;
64 const int CreateDVD_Thread::DVD_WIDE_CHANNELS = 6;
65 const double CreateDVD_Thread::DVD_SAMPLERATE = 48000;
66 const double CreateDVD_Thread::DVD_KAUDIO_RATE = 224;
67
68
69 CreateDVD_MenuItem::CreateDVD_MenuItem(MWindow *mwindow)
70  : BC_MenuItem(_("DVD Render..."), _("Shift-D"), 'D')
71 {
72         set_shift(1); 
73         this->mwindow = mwindow;
74 }
75
76 int CreateDVD_MenuItem::handle_event()
77 {
78         mwindow->create_dvd->start();
79         return 1;
80 }
81
82
83 CreateDVD_Thread::CreateDVD_Thread(MWindow *mwindow)
84  : BC_DialogThread()
85 {
86         this->mwindow = mwindow;
87         this->gui = 0;
88         this->use_deinterlace = 0;
89         this->use_scale = 0;
90         this->use_histogram = 0;
91         this->use_inverse_telecine = 0;
92         this->use_wide_audio = 0;
93         this->use_wide_aspect = 0;
94         this->use_ffmpeg = 0;
95         this->use_resize_tracks = 0;
96         this->use_label_chapters = 0;
97
98         this->dvd_size = DVD_SIZE;
99         this->dvd_width = DVD_WIDTH;
100         this->dvd_height = DVD_HEIGHT;
101         this->dvd_aspect_width = DVD_ASPECT_WIDTH;
102         this->dvd_aspect_height = DVD_ASPECT_HEIGHT;
103         this->dvd_framerate = DVD_FRAMERATE;
104         this->dvd_samplerate = DVD_SAMPLERATE;
105         this->dvd_max_bitrate = DVD_MAX_BITRATE;
106         this->dvd_kaudio_rate = DVD_KAUDIO_RATE;
107         this->max_w = this->max_h = 0;
108 }
109
110 CreateDVD_Thread::~CreateDVD_Thread()
111 {
112         close_window();
113 }
114
115 int CreateDVD_Thread::create_dvd_jobs(ArrayList<BatchRenderJob*> *jobs,
116         const char *tmp_path, const char *asset_title)
117 {
118         EDL *edl = mwindow->edl;
119         if( !edl || !edl->session ) {
120                 char msg[BCTEXTLEN];
121                 sprintf(msg, _("No EDL/Session"));
122                 MainError::show_error(msg);
123                 return 1;
124         }
125         EDLSession *session = edl->session;
126
127         double total_length = edl->tracks->total_length();
128         if( total_length <= 0 ) {
129                 char msg[BCTEXTLEN];
130                 sprintf(msg, _("No content: %s"), asset_title);
131                 MainError::show_error(msg);
132                 return 1;
133         }
134
135         char asset_dir[BCTEXTLEN];
136         sprintf(asset_dir, "%s/%s", tmp_path, asset_title);
137
138         if( mkdir(asset_dir, 0777) ) {
139                 char err[BCTEXTLEN], msg[BCTEXTLEN];
140                 strerror_r(errno, err, sizeof(err));
141                 sprintf(msg, _("Unable to create directory: %s\n-- %s"), asset_dir, err);
142                 MainError::show_error(msg);
143                 return 1;
144         }
145
146         double old_samplerate = session->sample_rate;
147         double old_framerate = session->frame_rate;
148
149         session->video_channels = DVD_STREAMS;
150         session->video_tracks = DVD_STREAMS;
151         session->frame_rate = dvd_framerate;
152         session->output_w = dvd_width;
153         session->output_h = dvd_height;
154         session->aspect_w = dvd_aspect_width;
155         session->aspect_h = dvd_aspect_height;
156         session->sample_rate = dvd_samplerate;
157         session->audio_channels = session->audio_tracks =
158                 use_wide_audio ? DVD_WIDE_CHANNELS : DVD_CHANNELS;
159
160         char script_filename[BCTEXTLEN];
161         sprintf(script_filename, "%s/dvd.sh", asset_dir);
162         int fd = open(script_filename, O_WRONLY+O_CREAT+O_TRUNC, 0755);
163         FILE *fp = fdopen(fd, "w");
164         if( !fp ) {
165                 char err[BCTEXTLEN], msg[BCTEXTLEN];
166                 strerror_r(errno, err, sizeof(err));
167                 sprintf(msg, _("Unable to save: %s\n-- %s"), script_filename, err);
168                 MainError::show_error(msg);
169                 return 1;
170         }
171         fprintf(fp,"#!/bin/bash\n");
172         fprintf(fp,"echo \"running %s\" $# $*\n", script_filename);
173         fprintf(fp,"\n");
174         const char *exec_path = File::get_cinlib_path();
175         fprintf(fp,"PATH=$PATH:%s\n",exec_path);
176         if( !use_ffmpeg ) {
177                 fprintf(fp,"mplex -f 8 -o $1/dvd.mpg $1/dvd.m2v $1/dvd.ac3\n");
178                 fprintf(fp,"\n");
179         }
180         fprintf(fp,"rm -rf $1/iso\n");
181         fprintf(fp,"mkdir -p $1/iso\n");
182         fprintf(fp,"\n");
183 // dvdauthor ver 0.7.0 requires this to work
184         fprintf(fp,"export VIDEO_FORMAT=%s\n",
185                 use_standard == HD_720x576_2500 ? "PAL" : "NTSC");
186         fprintf(fp,"dvdauthor -x - <<eof\n");
187         fprintf(fp,"<dvdauthor dest=\"$1/iso\">\n");
188         fprintf(fp,"  <vmgm>\n");
189         fprintf(fp,"    <fpc> jump title 1; </fpc>\n");
190         fprintf(fp,"  </vmgm>\n");
191         fprintf(fp,"  <titleset>\n");
192         fprintf(fp,"    <titles>\n");
193         fprintf(fp,"    <video format=\"%s\" aspect=\"%d:%d\" resolution=\"%dx%d\"/>\n",
194                 use_standard == HD_720x576_2500 ? "pal" : "ntsc",
195                 (int)session->aspect_w, (int)session->aspect_h,
196                 session->output_w, session->output_h);
197         fprintf(fp,"    <audio format=\"ac3\" lang=\"en\"/>\n");
198         fprintf(fp,"    <pgc>\n");
199         fprintf(fp,"      <vob file=\"$1/dvd.mpg\" chapters=\"");
200         if( use_label_chapters && edl->labels ) {
201                 Label *label = edl->labels->first;
202                 while( label ) {
203                         int secs = label->position;
204                         int mins = secs / 60;
205                         int frms = (label->position-secs) * session->frame_rate;
206                         fprintf(fp,"%d:%02d:%02d.%d", mins/60, mins%60, secs%60, frms);
207                         if( (label=label->next) != 0 ) fprintf(fp, ",");
208                 }
209         }
210         else {
211                 int mins = 0;
212                 for( int secs=0 ; secs<total_length; secs+=10*60 ) {
213                         mins = secs / 60;
214                         fprintf(fp,"%d:%02d:00,", mins/60, mins%60);
215                 }
216                 fprintf(fp,"%d:%02d:00", mins/60, mins%60);
217         }
218         fprintf(fp,"\"/>\n");
219         fprintf(fp,"    </pgc>\n");
220         fprintf(fp,"    </titles>\n");
221         fprintf(fp,"  </titleset>\n");
222         fprintf(fp,"</dvdauthor>\n");
223         fprintf(fp,"eof\n");
224         fprintf(fp,"\n");
225         fprintf(fp,"echo To burn dvd, load blank media and run:\n");
226         fprintf(fp,"echo growisofs -dvd-compat -Z /dev/dvd -dvd-video $1/iso\n");
227         fprintf(fp,"\n");
228         fclose(fp);
229
230         if( use_wide_audio ) {
231                 session->audio_channels = session->audio_tracks = DVD_WIDE_CHANNELS;
232                 session->achannel_positions[0] = 90;
233                 session->achannel_positions[1] = 150;
234                 session->achannel_positions[2] = 30;
235                 session->achannel_positions[3] = 210;
236                 session->achannel_positions[4] = 330;
237                 session->achannel_positions[5] = 270;
238                 if( edl->tracks->recordable_audio_tracks() == DVD_WIDE_CHANNELS )
239                         mwindow->remap_audio(MWindow::AUDIO_1_TO_1);
240         }
241         else {
242                 session->audio_channels = session->audio_tracks = DVD_CHANNELS;
243                 session->achannel_positions[0] = 180;
244                 session->achannel_positions[1] = 0;
245                 if( edl->tracks->recordable_audio_tracks() == DVD_WIDE_CHANNELS )
246                         mwindow->remap_audio(MWindow::AUDIO_5_1_TO_2);
247         }
248
249         double new_samplerate = session->sample_rate;
250         double new_framerate = session->frame_rate;
251         edl->rechannel();
252         edl->resample(old_samplerate, new_samplerate, TRACK_AUDIO);
253         edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
254
255         int64_t aud_size = ((dvd_kaudio_rate * total_length)/8 + 1000-1) * 1000;
256         int64_t vid_size = dvd_size*0.96 - aud_size;
257         int64_t vid_bitrate = (vid_size * 8) / total_length;
258         vid_bitrate /= 1000;  vid_bitrate *= 1000;
259         if( vid_bitrate > dvd_max_bitrate )
260                 vid_bitrate = dvd_max_bitrate;
261
262         char xml_filename[BCTEXTLEN];
263         sprintf(xml_filename, "%s/dvd.xml", asset_dir);
264         FileXML xml_file;
265         edl->save_xml(&xml_file, xml_filename, 0, 0);
266         xml_file.terminate_string();
267         if( xml_file.write_to_file(xml_filename) ) {
268                 char msg[BCTEXTLEN];
269                 sprintf(msg, _("Unable to save: %s"), xml_filename);
270                 MainError::show_error(msg);
271                 return 1;
272         }
273
274         BatchRenderJob *job = new BatchRenderJob(mwindow->preferences);
275         jobs->append(job);
276         strcpy(&job->edl_path[0], xml_filename);
277         Asset *asset = job->asset;
278
279         asset->layers = DVD_STREAMS;
280         asset->frame_rate = session->frame_rate;
281         asset->width = session->output_w;
282         asset->height = session->output_h;
283         asset->aspect_ratio = session->aspect_w / session->aspect_h;
284
285         if( use_ffmpeg ) {
286                 char option_path[BCTEXTLEN];
287                 sprintf(&asset->path[0],"%s/dvd.mpg", asset_dir);
288                 asset->format = FILE_FFMPEG;
289                 strcpy(asset->fformat, "dvd");
290
291                 asset->audio_data = 1;
292                 strcpy(asset->acodec, "dvd.dvd");
293                 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
294                 FFMPEG::load_options(option_path, asset->ff_audio_options,
295                          sizeof(asset->ff_audio_options));
296                 asset->ff_audio_bitrate = dvd_kaudio_rate * 1000;
297
298                 asset->video_data = 1;
299                 strcpy(asset->vcodec, "dvd.dvd");
300                 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
301                 FFMPEG::load_options(option_path, asset->ff_video_options,
302                          sizeof(asset->ff_video_options));
303                 asset->ff_video_bitrate = vid_bitrate;
304                 asset->ff_video_quality = 0;
305         }
306         else {
307                 sprintf(&asset->path[0],"%s/dvd.m2v", asset_dir);
308                 asset->video_data = 1;
309                 asset->format = FILE_VMPEG;
310                 asset->vmpeg_cmodel = BC_YUV420P;
311                 asset->vmpeg_fix_bitrate = 1;
312                 asset->vmpeg_bitrate = vid_bitrate;
313                 asset->vmpeg_quantization = 15;
314                 asset->vmpeg_iframe_distance = 15;
315                 asset->vmpeg_progressive = 0;
316                 asset->vmpeg_denoise = 0;
317                 asset->vmpeg_seq_codes = 0;
318                 asset->vmpeg_derivative = 2;
319                 asset->vmpeg_preset = 8;
320                 asset->vmpeg_field_order = 0;
321                 asset->vmpeg_pframe_distance = 0;
322                 job = new BatchRenderJob(mwindow->preferences);
323                 jobs->append(job);
324                 strcpy(&job->edl_path[0], xml_filename);
325                 asset = job->asset;
326                 
327                 sprintf(&asset->path[0],"%s/dvd.ac3", asset_dir);
328                 asset->audio_data = 1;
329                 asset->format = FILE_AC3;
330                 asset->channels = session->audio_channels;
331                 asset->sample_rate = session->sample_rate;
332                 asset->bits = 16;
333                 asset->byte_order = 0;
334                 asset->signed_ = 1;
335                 asset->header = 0;
336                 asset->dither = 0;
337                 asset->ac3_bitrate = dvd_kaudio_rate;
338         }
339
340         job = new BatchRenderJob(mwindow->preferences);
341         jobs->append(job);
342         job->edl_path[0] = '@';
343         strcpy(&job->edl_path[1], script_filename);
344         strcpy(&job->asset->path[0], asset_dir);
345
346         return 0;
347 }
348
349 void CreateDVD_Thread::handle_close_event(int result)
350 {
351         if( result ) return;
352         mwindow->batch_render->load_defaults(mwindow->defaults);
353         mwindow->undo->update_undo_before();
354         KeyFrame keyframe;  char data[BCTEXTLEN];
355         if( use_deinterlace ) {
356                 sprintf(data,"<DEINTERLACE MODE=1>");
357                 keyframe.set_data(data);
358                 insert_video_plugin("Deinterlace", &keyframe);
359         }
360         if( use_inverse_telecine ) {
361                 sprintf(data,"<IVTC FRAME_OFFSET=0 FIRST_FIELD=0 "
362                         "AUTOMATIC=1 AUTO_THRESHOLD=2.0e+00 PATTERN=2>");
363                 keyframe.set_data(data);
364                 insert_video_plugin("Inverse Telecine", &keyframe);
365         }
366         if( use_scale ) {
367                 sprintf(data,"<SCALE TYPE=%d X_FACTOR=%f Y_FACTOR=%f "
368                         "WIDTH=%d HEIGHT=%d CONSTRAIN=0>",
369                         max_w >= dvd_width || max_h >= dvd_height ? 1 : 0,
370                         max_w > 0 ? (double)dvd_width/max_w : 1,
371                         max_h > 0 ? (double)dvd_height/max_h : 1,
372                         dvd_width, dvd_height);
373                 keyframe.set_data(data);
374                 insert_video_plugin("Scale", &keyframe);
375         }
376         if( use_resize_tracks )
377                 resize_tracks();
378         if( use_histogram ) {
379 #if 0
380                 sprintf(data, "<HISTOGRAM OUTPUT_MIN_0=0 OUTPUT_MAX_0=1 "
381                         "OUTPUT_MIN_1=0 OUTPUT_MAX_1=1 "
382                         "OUTPUT_MIN_2=0 OUTPUT_MAX_2=1 "
383                         "OUTPUT_MIN_3=0 OUTPUT_MAX_3=1 "
384                         "AUTOMATIC=0 THRESHOLD=9.0-01 PLOT=0 SPLIT=0>"
385                         "<POINTS></POINTS><POINTS></POINTS><POINTS></POINTS>"
386                         "<POINTS><POINT X=6.0e-02 Y=0>"
387                                 "<POINT X=9.4e-01 Y=1></POINTS>");
388 #else
389                 sprintf(data, "<HISTOGRAM AUTOMATIC=0 THRESHOLD=1.0e-01 "
390                         "PLOT=0 SPLIT=0 W=440 H=500 PARADE=0 MODE=3 "
391                         "LOW_OUTPUT_0=0 HIGH_OUTPUT_0=1 LOW_INPUT_0=0 HIGH_INPUT_0=1 GAMMA_0=1 "
392                         "LOW_OUTPUT_1=0 HIGH_OUTPUT_1=1 LOW_INPUT_1=0 HIGH_INPUT_1=1 GAMMA_1=1 "
393                         "LOW_OUTPUT_2=0 HIGH_OUTPUT_2=1 LOW_INPUT_2=0 HIGH_INPUT_2=1 GAMMA_2=1 "
394                         "LOW_OUTPUT_3=0 HIGH_OUTPUT_3=1 LOW_INPUT_3=0.044 HIGH_INPUT_3=0.956 "
395                         "GAMMA_3=1>");
396 #endif
397                 keyframe.set_data(data);
398                 insert_video_plugin("Histogram", &keyframe);
399         }
400         mwindow->batch_render->reset();
401         create_dvd_jobs(&mwindow->batch_render->jobs, tmp_path, asset_title);
402         mwindow->save_backup();
403         mwindow->undo->update_undo_after(_("create dvd"), LOAD_ALL);
404         mwindow->resync_guis();
405         mwindow->batch_render->handle_close_event(0);
406         mwindow->batch_render->start();
407 }
408
409 BC_Window* CreateDVD_Thread::new_gui()
410 {
411         memset(tmp_path,0,sizeof(tmp_path));
412         strcpy(tmp_path,"/tmp");
413         memset(asset_title,0,sizeof(asset_title));
414         time_t dt;  time(&dt);
415         struct tm dtm;  localtime_r(&dt, &dtm);
416         sprintf(asset_title, "dvd_%02d%02d%02d-%02d%02d%02d",
417                 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
418                 dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
419         use_deinterlace = 0;
420         use_scale = 0;
421         use_histogram = 0;
422         use_inverse_telecine = 0;
423         use_wide_audio = 0;
424         use_wide_aspect = 0;
425         use_ffmpeg = 0;
426         use_resize_tracks = 0;
427         use_label_chapters = 0;
428         use_standard = HD_720x480_2997;
429
430         dvd_size = DVD_SIZE;
431         dvd_width = DVD_WIDTH;
432         dvd_height = DVD_HEIGHT;
433         dvd_aspect_width = DVD_ASPECT_WIDTH;
434         dvd_aspect_height = DVD_ASPECT_HEIGHT;
435         dvd_framerate = DVD_FRAMERATE;
436         dvd_samplerate = DVD_SAMPLERATE;
437         dvd_max_bitrate = DVD_MAX_BITRATE;
438         dvd_kaudio_rate = DVD_KAUDIO_RATE;
439         max_w = 0; max_h = 0;
440
441         int has_standard = -1;
442         if( mwindow->edl ) {
443                 EDLSession *session = mwindow->edl->session;
444 // match the session to any known standard
445                 for( int i=0; i<(int)(sizeof(hd_formats)/sizeof(hd_formats[0])); ++i ) {
446                         if( !EQUIV(session->frame_rate, hd_formats[i].framerate) ) continue;
447                         if( session->output_w != hd_formats[i].w ) continue;
448                         if( session->output_h != hd_formats[i].h ) continue;
449                         has_standard = i;  break;
450                 }
451                 if( has_standard < 0 ) {
452 // or use the default standard
453                         if( !strcmp(mwindow->default_standard, "NTSC") ) has_standard = HD_720x480_2997;
454                         else if( !strcmp(mwindow->default_standard, "PAL") ) has_standard = HD_720x576_2500;
455                 }
456         }
457         use_standard = has_standard >= 0 ? has_standard : HD_720x480_2997;
458
459         option_presets();
460         int scr_x = mwindow->gui->get_screen_x(0, -1);
461         int scr_w = mwindow->gui->get_screen_w(0, -1);
462         int scr_h = mwindow->gui->get_screen_h(0, -1);
463         int w = 500, h = 280;
464         int x = scr_x + scr_w/2 - w/2, y = scr_h/2 - h/2;
465
466         gui = new CreateDVD_GUI(this, x, y, w, h);
467         gui->create_objects();
468         return gui;
469 }
470
471
472 CreateDVD_OK::CreateDVD_OK(CreateDVD_GUI *gui, int x, int y)
473  : BC_OKButton(x, y)
474 {
475         this->gui = gui;
476         set_tooltip(_("end setup, start batch render"));
477 }
478
479 CreateDVD_OK::~CreateDVD_OK()
480 {
481 }
482
483 int CreateDVD_OK::button_press_event()
484 {
485         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
486                 gui->set_done(0);
487                 return 1;
488         }
489         return 0;
490 }
491
492 int CreateDVD_OK::keypress_event()
493 {
494         return 0;
495 }
496
497
498 CreateDVD_Cancel::CreateDVD_Cancel(CreateDVD_GUI *gui, int x, int y)
499  : BC_CancelButton(x, y)
500 {
501         this->gui = gui;
502 }
503
504 CreateDVD_Cancel::~CreateDVD_Cancel()
505 {
506 }
507
508 int CreateDVD_Cancel::button_press_event()
509 {
510         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
511                 gui->set_done(1);
512                 return 1;
513         }
514         return 0;
515 }
516
517
518 CreateDVD_DiskSpace::CreateDVD_DiskSpace(CreateDVD_GUI *gui, int x, int y)
519  : BC_Title(x, y, "", MEDIUMFONT, GREEN)
520 {
521         this->gui = gui;
522 }
523
524 CreateDVD_DiskSpace::~CreateDVD_DiskSpace()
525 {
526 }
527
528 int64_t CreateDVD_DiskSpace::tmp_path_space()
529 {
530         const char *path = gui->tmp_path->get_text();
531         if( access(path,R_OK+W_OK) ) return 0;
532         struct statfs sfs;
533         if( statfs(path, &sfs) ) return 0;
534         return (int64_t)sfs.f_bsize * sfs.f_bfree;
535 }
536
537 void CreateDVD_DiskSpace::update()
538 {
539         static const char *suffix[] = { "", "KB", "MB", "GB", "TB", "PB" };
540         int64_t disk_space = tmp_path_space();
541         double media_size = 15e9, msz = 0, m = 1;
542         char sfx[BCSTRLEN];
543         if( sscanf(gui->media_size->get_text(), "%lf%s", &msz, sfx) == 2 ) {
544                 int i = sizeof(suffix)/sizeof(suffix[0]);
545                 while( --i >= 0 && strcmp(sfx, suffix[i]) );
546                 while( --i >= 0 ) m *= 1000;
547                 media_size = msz * m;
548         }
549         m = gui->thread->use_ffmpeg ? 2 : 3;
550         int color = disk_space < media_size*m ? RED : GREEN;
551         int i = 0;
552         for( int64_t space=disk_space; i<5 && (space/=1000)>0; disk_space=space, ++i );
553         char text[BCTEXTLEN];
554         sprintf(text, "%s%3jd%s", _("disk space: "), disk_space, suffix[i]);
555         gui->disk_space->BC_Title::update(text);
556         gui->disk_space->set_color(color);
557 }
558
559 CreateDVD_TmpPath::CreateDVD_TmpPath(CreateDVD_GUI *gui, int x, int y, int w)
560  : BC_TextBox(x, y, w, 1, -(int)sizeof(gui->thread->tmp_path),
561                 gui->thread->tmp_path, 1, MEDIUMFONT)
562 {
563         this->gui = gui;
564 }
565
566 CreateDVD_TmpPath::~CreateDVD_TmpPath()
567 {
568 }
569
570 int CreateDVD_TmpPath::handle_event()
571 {
572         gui->disk_space->update();
573         return 1;
574 }
575
576
577 CreateDVD_AssetTitle::CreateDVD_AssetTitle(CreateDVD_GUI *gui, int x, int y, int w)
578  : BC_TextBox(x, y, w, 1, 0, gui->thread->asset_title, 1, MEDIUMFONT)
579 {
580         this->gui = gui;
581 }
582
583 CreateDVD_AssetTitle::~CreateDVD_AssetTitle()
584 {
585 }
586
587
588 CreateDVD_Deinterlace::CreateDVD_Deinterlace(CreateDVD_GUI *gui, int x, int y)
589  : BC_CheckBox(x, y, &gui->thread->use_deinterlace, _("Deinterlace"))
590 {
591         this->gui = gui;
592 }
593
594 CreateDVD_Deinterlace::~CreateDVD_Deinterlace()
595 {
596 }
597
598 int CreateDVD_Deinterlace::handle_event()
599 {
600         if( get_value() ) {
601                 gui->need_inverse_telecine->set_value(0);
602                 gui->thread->use_inverse_telecine = 0;
603         }
604         return BC_CheckBox::handle_event();
605 }
606
607
608 CreateDVD_InverseTelecine::CreateDVD_InverseTelecine(CreateDVD_GUI *gui, int x, int y)
609  : BC_CheckBox(x, y, &gui->thread->use_inverse_telecine, _("Inverse Telecine"))
610 {
611         this->gui = gui;
612 }
613
614 CreateDVD_InverseTelecine::~CreateDVD_InverseTelecine()
615 {
616 }
617
618 int CreateDVD_InverseTelecine::handle_event()
619 {
620         if( get_value() ) {
621                 gui->need_deinterlace->set_value(0);
622                 gui->thread->use_deinterlace = 0;
623         }
624         return BC_CheckBox::handle_event();
625 }
626
627
628 CreateDVD_Scale::CreateDVD_Scale(CreateDVD_GUI *gui, int x, int y)
629  : BC_CheckBox(x, y, &gui->thread->use_scale, _("Scale"))
630 {
631         this->gui = gui;
632 }
633
634 CreateDVD_Scale::~CreateDVD_Scale()
635 {
636 }
637
638
639 CreateDVD_ResizeTracks::CreateDVD_ResizeTracks(CreateDVD_GUI *gui, int x, int y)
640  : BC_CheckBox(x, y, &gui->thread->use_resize_tracks, _("Resize Tracks"))
641 {
642         this->gui = gui;
643 }
644
645 CreateDVD_ResizeTracks::~CreateDVD_ResizeTracks()
646 {
647 }
648
649
650 CreateDVD_Histogram::CreateDVD_Histogram(CreateDVD_GUI *gui, int x, int y)
651  : BC_CheckBox(x, y, &gui->thread->use_histogram, _("Histogram"))
652 {
653         this->gui = gui;
654 }
655
656 CreateDVD_Histogram::~CreateDVD_Histogram()
657 {
658 }
659
660 CreateDVD_LabelChapters::CreateDVD_LabelChapters(CreateDVD_GUI *gui, int x, int y)
661  : BC_CheckBox(x, y, &gui->thread->use_label_chapters, _("Chapters at Labels"))
662 {
663         this->gui = gui;
664 }
665
666 CreateDVD_LabelChapters::~CreateDVD_LabelChapters()
667 {
668 }
669
670 CreateDVD_WideAudio::CreateDVD_WideAudio(CreateDVD_GUI *gui, int x, int y)
671  : BC_CheckBox(x, y, &gui->thread->use_wide_audio, _("Audio 5.1"))
672 {
673         this->gui = gui;
674 }
675
676 CreateDVD_WideAudio::~CreateDVD_WideAudio()
677 {
678 }
679
680 CreateDVD_WideAspect::CreateDVD_WideAspect(CreateDVD_GUI *gui, int x, int y)
681  : BC_CheckBox(x, y, &gui->thread->use_wide_aspect, _("Aspect 16x9"))
682 {
683         this->gui = gui;
684 }
685
686 CreateDVD_WideAspect::~CreateDVD_WideAspect()
687 {
688 }
689
690 CreateDVD_UseFFMpeg::CreateDVD_UseFFMpeg(CreateDVD_GUI *gui, int x, int y)
691  : BC_CheckBox(x, y, &gui->thread->use_ffmpeg, _("Use FFMPEG"))
692 {
693         this->gui = gui;
694 }
695
696 CreateDVD_UseFFMpeg::~CreateDVD_UseFFMpeg()
697 {
698 }
699
700
701
702
703 CreateDVD_GUI::CreateDVD_GUI(CreateDVD_Thread *thread, int x, int y, int w, int h)
704  : BC_Window(_(PROGRAM_NAME ": Create DVD"), x, y, w, h, 50, 50, 1, 0, 1)
705 {
706         this->thread = thread;
707         at_x = at_y = tmp_x = tmp_y = 0;
708         ok_x = ok_y = ok_w = ok_h = 0;
709         cancel_x = cancel_y = cancel_w = cancel_h = 0;
710         asset_title = 0;
711         tmp_path = 0;
712         btmp_path = 0;
713         disk_space = 0;
714         need_deinterlace = 0;
715         need_inverse_telecine = 0;
716         need_scale = 0;
717         need_resize_tracks = 0;
718         need_histogram = 0;
719         need_wide_audio = 0;
720         need_wide_aspect = 0;
721         need_label_chapters = 0;
722         ok = 0;
723         cancel = 0;
724 }
725
726 CreateDVD_GUI::~CreateDVD_GUI()
727 {
728 }
729
730 void CreateDVD_GUI::create_objects()
731 {
732         lock_window("CreateDVD_GUI::create_objects");
733         int pady = BC_TextBox::calculate_h(this, MEDIUMFONT, 0, 1) + 5;
734         int padx = BC_Title::calculate_w(this, (char*)"X", MEDIUMFONT);
735         int x = padx/2, y = pady/2;
736         BC_Title *title = new BC_Title(x, y, _("Title:"), MEDIUMFONT, YELLOW);
737         add_subwindow(title);
738         at_x = x + title->get_w();  at_y = y;
739         asset_title = new CreateDVD_AssetTitle(this, at_x, at_y, get_w()-at_x-10);
740         add_subwindow(asset_title);
741         y += title->get_h() + pady/2;
742         title = new BC_Title(x, y, _("Work path:"), MEDIUMFONT, YELLOW);
743         add_subwindow(title);
744         tmp_x = x + title->get_w();  tmp_y = y;
745         tmp_path = new CreateDVD_TmpPath(this, tmp_x, tmp_y,  get_w()-tmp_x-35);
746         add_subwindow(tmp_path);
747         btmp_path = new BrowseButton(thread->mwindow, this, tmp_path,
748                 tmp_x+tmp_path->get_w(), tmp_y, "/tmp",
749                 _("Work path"), _("Select a Work directory:"), 1);
750         add_subwindow(btmp_path);
751         y += title->get_h() + pady/2;
752         disk_space = new CreateDVD_DiskSpace(this, x, y);
753         add_subwindow(disk_space);
754         int x0 = get_w() - 170;
755         title = new BC_Title(x0, y, _("Media:"), MEDIUMFONT, YELLOW);
756         add_subwindow(title);
757         x0 +=  title->get_w() + padx;
758         media_size = new CreateDVD_MediaSize(this, x0, y);
759         media_size->create_objects();
760         media_sizes.append(new BC_ListBoxItem("4.7GB"));
761         media_sizes.append(new BC_ListBoxItem("8.3GB"));
762         media_size->update_list(&media_sizes);
763         media_size->update(media_sizes[0]->get_text());
764         disk_space->update();
765         x0 = x;
766         y += disk_space->get_h() + pady/2;
767         title = new BC_Title(x0, y, _("Format:"), MEDIUMFONT, YELLOW);
768         add_subwindow(title);
769         x0 +=  title->get_w() + padx;
770         standard = new CreateDVD_Format(this, x0, y);
771         add_subwindow(standard);
772         standard->create_objects();
773         y += standard->get_h() + pady/2;
774         need_deinterlace = new CreateDVD_Deinterlace(this, x, y);
775         add_subwindow(need_deinterlace);
776         int x1 = x + 150, x2 = x1 + 150;
777         need_inverse_telecine = new CreateDVD_InverseTelecine(this, x1, y);
778         add_subwindow(need_inverse_telecine);
779         need_use_ffmpeg = new CreateDVD_UseFFMpeg(this, x2, y);
780         add_subwindow(need_use_ffmpeg);
781         y += need_deinterlace->get_h() + pady/2;
782         need_scale = new CreateDVD_Scale(this, x, y);
783         add_subwindow(need_scale);
784         need_wide_audio = new CreateDVD_WideAudio(this, x1, y);
785         add_subwindow(need_wide_audio);
786         need_resize_tracks = new CreateDVD_ResizeTracks(this, x2, y);
787         add_subwindow(need_resize_tracks);
788         y += need_scale->get_h() + pady/2;
789         need_histogram = new CreateDVD_Histogram(this, x, y);
790         add_subwindow(need_histogram);
791         need_wide_aspect = new CreateDVD_WideAspect(this, x1, y);
792         add_subwindow(need_wide_aspect);
793         need_label_chapters = new CreateDVD_LabelChapters(this, x2, y);
794         add_subwindow(need_label_chapters);
795         ok_w = BC_OKButton::calculate_w();
796         ok_h = BC_OKButton::calculate_h();
797         ok_x = 10;
798         ok_y = get_h() - ok_h - 10;
799         ok = new CreateDVD_OK(this, ok_x, ok_y);
800         add_subwindow(ok);
801         cancel_w = BC_CancelButton::calculate_w();
802         cancel_h = BC_CancelButton::calculate_h();
803         cancel_x = get_w() - cancel_w - 10,
804         cancel_y = get_h() - cancel_h - 10;
805         cancel = new CreateDVD_Cancel(this, cancel_x, cancel_y);
806         add_subwindow(cancel);
807         show_window();
808         unlock_window();
809 }
810
811 int CreateDVD_GUI::resize_event(int w, int h)
812 {
813         asset_title->reposition_window(at_x, at_y, get_w()-at_x-10);
814         tmp_path->reposition_window(tmp_x, tmp_y,  get_w()-tmp_x-35);
815         btmp_path->reposition_window(tmp_x+tmp_path->get_w(), tmp_y);
816         ok_y = h - ok_h - 10;
817         ok->reposition_window(ok_x, ok_y);
818         cancel_x = w - cancel_w - 10,
819         cancel_y = h - cancel_h - 10;
820         cancel->reposition_window(cancel_x, cancel_y);
821         return 0;
822 }
823
824 int CreateDVD_GUI::translation_event()
825 {
826         return 1;
827 }
828
829 int CreateDVD_GUI::close_event()
830 {
831         set_done(1);
832         return 1;
833 }
834
835 void CreateDVD_GUI::update()
836 {
837         need_deinterlace->set_value(thread->use_deinterlace);
838         need_inverse_telecine->set_value(thread->use_inverse_telecine);
839         need_scale->set_value(thread->use_scale);
840         need_use_ffmpeg->set_value(thread->use_ffmpeg);
841         need_resize_tracks->set_value(thread->use_resize_tracks);
842         need_histogram->set_value(thread->use_histogram);
843         need_wide_audio->set_value(thread->use_wide_audio);
844         need_wide_aspect->set_value(thread->use_wide_aspect);
845         need_label_chapters->set_value(thread->use_label_chapters);
846 }
847
848 int CreateDVD_Thread::
849 insert_video_plugin(const char *title, KeyFrame *default_keyframe)
850 {
851         Tracks *tracks = mwindow->edl->tracks;
852         for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
853                 if( vtrk->data_type != TRACK_VIDEO ) continue;
854                 if( !vtrk->record ) continue;
855                 vtrk->expand_view = 1;
856                 PluginSet *plugin_set = new PluginSet(mwindow->edl, vtrk);
857                 vtrk->plugin_set.append(plugin_set);
858                 Edits *edits = vtrk->edits;
859                 for( Edit *edit=edits->first; edit; edit=edit->next ) {
860                         plugin_set->insert_plugin(_(title),
861                                 edit->startproject, edit->length,
862                                 PLUGIN_STANDALONE, 0, default_keyframe, 0);
863                 }
864                 vtrk->optimize();
865         }
866         return 0;
867 }
868
869 int CreateDVD_Thread::
870 resize_tracks()
871 {
872         Tracks *tracks = mwindow->edl->tracks;
873         int trk_w = max_w, trk_h = max_h;
874         if( trk_w < dvd_width ) trk_w = dvd_width;
875         if( trk_h < dvd_height ) trk_h = dvd_height;
876         for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
877                 if( vtrk->data_type != TRACK_VIDEO ) continue;
878                 if( !vtrk->record ) continue;
879                 vtrk->track_w = trk_w;
880                 vtrk->track_h = trk_h;
881         }
882         return 0;
883 }
884
885 int CreateDVD_Thread::
886 option_presets()
887 {
888 // reset only probed options
889         use_deinterlace = 0;
890         use_scale = 0;
891         use_resize_tracks = 0;
892         use_wide_audio = 0;
893         use_wide_aspect = 0;
894         use_label_chapters = 0;
895
896         if( !mwindow->edl ) return 1;
897
898         dvd_width = hd_formats[use_standard].w;
899         dvd_height = hd_formats[use_standard].h;
900         dvd_framerate = hd_formats[use_standard].framerate;
901
902         Tracks *tracks = mwindow->edl->tracks;
903         max_w = 0;  max_h = 0;
904         int has_deinterlace = 0, has_scale = 0;
905         for( Track *trk=tracks->first; trk; trk=trk->next ) {
906                 if( !trk->record ) continue;
907                 Edits *edits = trk->edits;
908                 switch( trk->data_type ) {
909                 case TRACK_VIDEO:
910                         for( Edit *edit=edits->first; edit; edit=edit->next ) {
911                                 if( edit->silence() ) continue;
912                                 Indexable *indexable = edit->get_source();
913                                 int w = indexable->get_w();
914                                 if( w > max_w ) max_w = w;
915                                 if( w != dvd_width ) use_scale = 1;
916                                 int h = indexable->get_h();
917                                 if( h > max_h ) max_h = h;
918                                 if( h != dvd_height ) use_scale = 1;
919                         }
920                         for( int i=0; i<trk->plugin_set.size(); ++i ) {
921                                 for(Plugin *plugin = (Plugin*)trk->plugin_set[i]->first;
922                                                 plugin;
923                                                 plugin = (Plugin*)plugin->next) {
924                                         if( !strcmp(plugin->title, _("Deinterlace")) )
925                                                 has_deinterlace = 1;
926                                         if( !strcmp(plugin->title, _("Auto Scale")) ||
927                                             !strcmp(plugin->title, _("Scale")) )
928                                                 has_scale = 1;
929                                 }
930                         }
931                         break;
932                 }
933         }
934         if( has_scale )
935                 use_scale = 0;
936         if( use_scale ) {
937                 if( max_w != dvd_width ) use_resize_tracks = 1;
938                 if( max_h != dvd_height ) use_resize_tracks = 1;
939         }
940         for( Track *trk=tracks->first; trk && !use_resize_tracks; trk=trk->next ) {
941                 if( !trk->record ) continue;
942                 switch( trk->data_type ) {
943                 case TRACK_VIDEO:
944                         if( trk->track_w != max_w ) use_resize_tracks = 1;
945                         if( trk->track_h != max_h ) use_resize_tracks = 1;
946                         break;
947                 }
948         }
949         if( !has_deinterlace && max_h > 2*dvd_height ) use_deinterlace = 1;
950         Labels *labels = mwindow->edl->labels;
951         use_label_chapters = labels && labels->first ? 1 : 0;
952
953         float aw, ah;
954         MWindow::create_aspect_ratio(aw, ah, max_w, max_h);
955         if( aw == DVD_WIDE_ASPECT_WIDTH && ah == DVD_WIDE_ASPECT_HEIGHT )
956                 use_wide_aspect = 1;
957         dvd_aspect_width = use_wide_aspect ? DVD_WIDE_ASPECT_WIDTH : DVD_ASPECT_WIDTH;
958         dvd_aspect_height = use_wide_aspect ? DVD_WIDE_ASPECT_HEIGHT : DVD_ASPECT_HEIGHT;
959
960         if( tracks->recordable_audio_tracks() == DVD_WIDE_CHANNELS )
961                 use_wide_audio = 1;
962
963         return 0;
964 }
965
966
967
968 CreateDVD_FormatItem::CreateDVD_FormatItem(CreateDVD_Format *popup,
969                 int standard, const char *name)
970  : BC_MenuItem(name)
971 {
972         this->popup = popup;
973         this->standard = standard;
974 }
975
976 CreateDVD_FormatItem::~CreateDVD_FormatItem()
977 {
978 }
979
980 int CreateDVD_FormatItem::handle_event()
981 {
982         popup->set_text(get_text());
983         popup->gui->thread->use_standard = standard;
984         return popup->handle_event();
985 }
986
987
988 CreateDVD_Format::CreateDVD_Format(CreateDVD_GUI *gui, int x, int y)
989  : BC_PopupMenu(x, y, 180, hd_formats[gui->thread->use_standard].name, 1)
990 {
991         this->gui = gui;
992 }
993
994 CreateDVD_Format::~CreateDVD_Format()
995 {
996 }
997
998 void CreateDVD_Format::create_objects()
999 {
1000         for( int i=0; i<(int)(sizeof(hd_formats)/sizeof(hd_formats[0])); ++i ) {
1001                 add_item(new CreateDVD_FormatItem(this, i, hd_formats[i].name));
1002         }
1003 }
1004
1005 int CreateDVD_Format::handle_event()
1006 {
1007         gui->thread->option_presets();
1008         gui->update();
1009         return 1;
1010 }
1011
1012 CreateDVD_MediaSize::CreateDVD_MediaSize(CreateDVD_GUI *gui, int x, int y)
1013  : BC_PopupTextBox(gui, 0, 0, x, y, 70,50)
1014 {
1015         this->gui = gui;
1016 }
1017
1018 CreateDVD_MediaSize::~CreateDVD_MediaSize()
1019 {
1020 }
1021
1022 int CreateDVD_MediaSize::handle_event()
1023 {
1024         gui->disk_space->update();
1025         return 1;
1026 }
1027