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