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