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