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