Credit Andrew - improve in-tree documentation
[goodguy/cinelerra.git] / 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             (use_label_chapters && edl->labels ) ) {
189                 if( !use_ffmpeg ) {
190                         fprintf(fp, "cat > $1/dvd.m2v $1/dvd.m2v0*\n");
191                         fprintf(fp, "mplex -M -f 8 -o $1/dvd.mpg $1/dvd.m2v $1/dvd.ac3\n");
192                 }
193                 else
194                         fprintf(fp, "ffmpeg -f concat -safe 0 -i <(for f in \"$1/dvd.mpg0\"*; do "
195                                         "echo \"file '$f'\"; done) -c copy -y $1/dvd.mpg\n");
196         }
197         else
198                 fprintf(fp, "mplex -f 8 -o $1/dvd.mpg $1/dvd.m2v $1/dvd.ac3\n");
199         fprintf(fp,"rm -rf $1/iso\n");
200         fprintf(fp,"mkdir -p $1/iso\n");
201         fprintf(fp,"\n");
202 // dvdauthor ver 0.7.0 requires this to work
203         int norm = dvd_formats[use_standard].norm;
204         const char *name = dvd_norms[norm].name;
205         fprintf(fp,"export VIDEO_FORMAT=%s\n", name);
206         fprintf(fp,"dvdauthor -x - <<eof\n");
207         fprintf(fp,"<dvdauthor dest=\"$1/iso\">\n");
208         fprintf(fp,"  <vmgm>\n");
209         fprintf(fp,"    <fpc> jump title 1; </fpc>\n");
210         fprintf(fp,"  </vmgm>\n");
211         fprintf(fp,"  <titleset>\n");
212         fprintf(fp,"    <titles>\n");
213         char std[BCSTRLEN], *cp = std;
214         for( const char *np=name; *np!=0; ++cp,++np) *cp = *np + 'a'-'A';
215         *cp = 0;
216         fprintf(fp,"    <video format=\"%s\" aspect=\"%d:%d\" resolution=\"%dx%d\"/>\n",
217                 std, (int)session->aspect_w, (int)session->aspect_h,
218                 session->output_w, session->output_h);
219         fprintf(fp,"    <audio format=\"ac3\" lang=\"en\"/>\n");
220         fprintf(fp,"    <pgc>\n");
221         fprintf(fp,"      <vob file=\"$1/dvd.mpg\" chapters=\"");
222         if( use_label_chapters && edl->labels ) {
223                 Label *label = edl->labels->first;
224                 while( label ) {
225                         int secs = label->position;
226                         int mins = secs / 60;
227                         int frms = (label->position-secs) * session->frame_rate;
228                         fprintf(fp,"%d:%02d:%02d.%d", mins/60, mins%60, secs%60, frms);
229                         if( (label=label->next) != 0 ) fprintf(fp, ",");
230                 }
231         }
232         else {
233                 int mins = 0;
234                 for( int secs=0 ; secs<total_length; secs+=10*60 ) {
235                         mins = secs / 60;
236                         fprintf(fp,"%d:%02d:00,", mins/60, mins%60);
237                 }
238                 fprintf(fp,"%d:%02d:00", mins/60, mins%60);
239         }
240         fprintf(fp,"\"/>\n");
241         fprintf(fp,"    </pgc>\n");
242         fprintf(fp,"    </titles>\n");
243         fprintf(fp,"  </titleset>\n");
244         fprintf(fp,"</dvdauthor>\n");
245         fprintf(fp,"eof\n");
246         fprintf(fp,"\n");
247         fprintf(fp,"echo To burn dvd, load blank media and run:\n");
248         fprintf(fp,"echo growisofs -dvd-compat -Z /dev/dvd -dvd-video $1/iso\n");
249         fprintf(fp,"kill $$\n");
250         fprintf(fp,"\n");
251         fclose(fp);
252
253         session->audio_channels = session->audio_tracks =
254                 !use_wide_audio ? DVD_CHANNELS : DVD_WIDE_CHANNELS;
255         for( int i=0; i<MAX_CHANNELS; ++i )
256                 session->achannel_positions[i] = default_audio_channel_position(i, session->audio_channels);
257         int audio_mapping = edl->tracks->recordable_audio_tracks() == DVD_WIDE_CHANNELS &&
258                 !use_wide_audio ? MWindow::AUDIO_5_1_TO_2 : MWindow::AUDIO_1_TO_1;
259         mwindow->remap_audio(audio_mapping);
260
261         double new_samplerate = session->sample_rate;
262         double new_framerate = session->frame_rate;
263         edl->retrack();
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);
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, use_label_chapters);
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 = -1;
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, 0, 0);
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, 0, 0);
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                         vtrk->optimize();
411                 }
412         }
413
414         if( use_resize_tracks )
415                 resize_tracks();
416         if( use_histogram ) {
417 #if 0
418                 sprintf(data, "<HISTOGRAM OUTPUT_MIN_0=0 OUTPUT_MAX_0=1 "
419                         "OUTPUT_MIN_1=0 OUTPUT_MAX_1=1 "
420                         "OUTPUT_MIN_2=0 OUTPUT_MAX_2=1 "
421                         "OUTPUT_MIN_3=0 OUTPUT_MAX_3=1 "
422                         "AUTOMATIC=0 THRESHOLD=9.0-01 PLOT=0 SPLIT=0>"
423                         "<POINTS></POINTS><POINTS></POINTS><POINTS></POINTS>"
424                         "<POINTS><POINT X=6.0e-02 Y=0>"
425                                 "<POINT X=9.4e-01 Y=1></POINTS>");
426 #else
427                 sprintf(data, "<HISTOGRAM AUTOMATIC=0 THRESHOLD=1.0e-01 "
428                         "PLOT=0 SPLIT=0 W=440 H=500 PARADE=0 MODE=3 "
429                         "LOW_OUTPUT_0=0 HIGH_OUTPUT_0=1 LOW_INPUT_0=0 HIGH_INPUT_0=1 GAMMA_0=1 "
430                         "LOW_OUTPUT_1=0 HIGH_OUTPUT_1=1 LOW_INPUT_1=0 HIGH_INPUT_1=1 GAMMA_1=1 "
431                         "LOW_OUTPUT_2=0 HIGH_OUTPUT_2=1 LOW_INPUT_2=0 HIGH_INPUT_2=1 GAMMA_2=1 "
432                         "LOW_OUTPUT_3=0 HIGH_OUTPUT_3=1 LOW_INPUT_3=0.044 HIGH_INPUT_3=0.956 "
433                         "GAMMA_3=1>");
434 #endif
435                 keyframe.set_data(data);
436                 insert_video_plugin("Histogram", &keyframe);
437         }
438         char asset_dir[BCTEXTLEN], jobs_path[BCTEXTLEN];
439         snprintf(asset_dir, sizeof(asset_dir), "%s/%s", tmp_path, asset_title);
440         snprintf(jobs_path, sizeof(jobs_path), "%s/dvd.jobs", asset_dir);
441         mwindow->batch_render->reset(jobs_path);
442         int ret = create_dvd_jobs(&mwindow->batch_render->jobs, asset_dir);
443         mwindow->undo->update_undo_after(_("create dvd"), LOAD_ALL);
444         mwindow->resync_guis();
445         if( ret ) return;
446         mwindow->batch_render->save_jobs();
447         mwindow->batch_render->start();
448 }
449
450 BC_Window* CreateDVD_Thread::new_gui()
451 {
452         strcpy(tmp_path,"/tmp");
453         mwindow->defaults->get("WORK_DIRECTORY", tmp_path);
454         memset(asset_title,0,sizeof(asset_title));
455         time_t dt;  time(&dt);
456         struct tm dtm;  localtime_r(&dt, &dtm);
457         sprintf(asset_title, "dvd_%02d%02d%02d-%02d%02d%02d",
458                 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
459                 dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
460         use_deinterlace = 0;
461         use_scale = Rescale::none;
462         use_histogram = 0;
463         use_inverse_telecine = 0;
464         use_wide_audio = 0;
465         use_ffmpeg = 0;
466         use_resize_tracks = 0;
467         use_label_chapters = 0;
468         use_standard = DVD_NTSC_4x3;
469
470         dvd_size = DVD_SIZE;
471         dvd_width = DVD_WIDTH;
472         dvd_height = DVD_HEIGHT;
473         dvd_aspect_width = DVD_ASPECT_WIDTH;
474         dvd_aspect_height = DVD_ASPECT_HEIGHT;
475         dvd_framerate = DVD_FRAMERATE;
476         dvd_samplerate = DVD_SAMPLERATE;
477         dvd_max_bitrate = DVD_MAX_BITRATE;
478         dvd_kaudio_rate = DVD_KAUDIO_RATE;
479         max_w = 0; max_h = 0;
480
481         int has_standard = -1;
482         if( mwindow->edl ) {
483                 EDLSession *session = mwindow->edl->session;
484                 double framerate = session->frame_rate;
485                 double aspect_ratio = session->aspect_h > 0 ?
486                         session->aspect_w / session->aspect_h > 0 : 1;
487                 int output_w = session->output_w, output_h = session->output_h;
488 // match the session to any known standard
489                 for( int i=0; i<(int)(sizeof(dvd_formats)/sizeof(dvd_formats[0])); ++i ) {
490                         int norm = dvd_formats[i].norm;
491                         if( !EQUIV(framerate, dvd_norms[norm].framerate) ) continue;
492                         if( output_w != dvd_norms[norm].w ) continue;
493                         if( output_h != dvd_norms[norm].h ) continue;
494                         int aspect = dvd_formats[i].aspect;
495                         double dvd_aspect_ratio =
496                                 (double)dvd_aspects[aspect].w / dvd_aspects[aspect].h;
497                         if( !EQUIV(aspect_ratio, dvd_aspect_ratio) ) continue;
498                         has_standard = i;  break;
499                 }
500                 if( has_standard < 0 ) {
501 // or use the default standard
502                         if( !strcmp(mwindow->default_standard, "NTSC") ) has_standard = DVD_NTSC_4x3;
503                         else if( !strcmp(mwindow->default_standard, "PAL") ) has_standard = DVD_PAL_4x3;
504                 }
505         }
506         if( has_standard >= 0 )
507                 use_standard = has_standard;
508
509         option_presets();
510         int scr_x = mwindow->gui->get_screen_x(0, -1);
511         int scr_w = mwindow->gui->get_screen_w(0, -1);
512         int scr_h = mwindow->gui->get_screen_h(0, -1);
513         int w = 520, h = 280;
514         int x = scr_x + scr_w/2 - w/2, y = scr_h/2 - h/2;
515
516         gui = new CreateDVD_GUI(this, x, y, w, h);
517         gui->create_objects();
518         return gui;
519 }
520
521
522 CreateDVD_OK::CreateDVD_OK(CreateDVD_GUI *gui, int x, int y)
523  : BC_OKButton(x, y)
524 {
525         this->gui = gui;
526         set_tooltip(_("end setup, start batch render"));
527 }
528
529 CreateDVD_OK::~CreateDVD_OK()
530 {
531 }
532
533 int CreateDVD_OK::button_press_event()
534 {
535         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
536                 gui->set_done(0);
537                 return 1;
538         }
539         return 0;
540 }
541
542 int CreateDVD_OK::keypress_event()
543 {
544         return 0;
545 }
546
547
548 CreateDVD_Cancel::CreateDVD_Cancel(CreateDVD_GUI *gui, int x, int y)
549  : BC_CancelButton(x, y)
550 {
551         this->gui = gui;
552 }
553
554 CreateDVD_Cancel::~CreateDVD_Cancel()
555 {
556 }
557
558 int CreateDVD_Cancel::button_press_event()
559 {
560         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
561                 gui->set_done(1);
562                 return 1;
563         }
564         return 0;
565 }
566
567
568 CreateDVD_DiskSpace::CreateDVD_DiskSpace(CreateDVD_GUI *gui, int x, int y)
569  : BC_Title(x, y, "", MEDIUMFONT, GREEN)
570 {
571         this->gui = gui;
572 }
573
574 CreateDVD_DiskSpace::~CreateDVD_DiskSpace()
575 {
576 }
577
578 int64_t CreateDVD_DiskSpace::tmp_path_space()
579 {
580         const char *path = gui->thread->tmp_path;
581         if( access(path,R_OK+W_OK) ) return 0;
582         struct statfs sfs;
583         if( statfs(path, &sfs) ) return 0;
584         return (int64_t)sfs.f_bsize * sfs.f_bfree;
585 }
586
587 void CreateDVD_DiskSpace::update()
588 {
589         static const char *suffix[] = { "", "KB", "MB", "GB", "TB", "PB" };
590         int64_t disk_space = tmp_path_space();
591         double media_size = 15e9, msz = 0, m = 1;
592         char sfx[BCSTRLEN];
593         if( sscanf(gui->media_size->get_text(), "%lf%s", &msz, sfx) == 2 ) {
594                 int i = sizeof(suffix)/sizeof(suffix[0]);
595                 while( --i >= 0 && strcmp(sfx, suffix[i]) );
596                 while( --i >= 0 ) m *= 1000;
597                 media_size = msz * m;
598         }
599         m = gui->thread->use_ffmpeg ? 2 : 3;
600         int color = disk_space < media_size*m ? RED : GREEN;
601         int i = 0;
602         for( int64_t space=disk_space; i<5 && (space/=1000)>0; disk_space=space, ++i );
603         char text[BCTEXTLEN];
604         sprintf(text, "%s%3jd%s", _("disk space: "), disk_space, suffix[i]);
605         gui->disk_space->BC_Title::update(text);
606         gui->disk_space->set_color(color);
607 }
608
609 CreateDVD_TmpPath::CreateDVD_TmpPath(CreateDVD_GUI *gui, int x, int y, int w)
610  : BC_TextBox(x, y, w, 1, -(int)sizeof(gui->thread->tmp_path),
611                 gui->thread->tmp_path, 1, MEDIUMFONT)
612 {
613         this->gui = gui;
614 }
615
616 CreateDVD_TmpPath::~CreateDVD_TmpPath()
617 {
618 }
619
620 int CreateDVD_TmpPath::handle_event()
621 {
622         get_text();
623         gui->disk_space->update();
624         return 1;
625 }
626
627
628 CreateDVD_AssetTitle::CreateDVD_AssetTitle(CreateDVD_GUI *gui, int x, int y, int w)
629  : BC_TextBox(x, y, w, 1, -(int)sizeof(gui->thread->asset_title),
630                 gui->thread->asset_title, 1, MEDIUMFONT)
631 {
632         this->gui = gui;
633 }
634
635 CreateDVD_AssetTitle::~CreateDVD_AssetTitle()
636 {
637 }
638
639 int CreateDVD_AssetTitle::handle_event()
640 {
641         get_text();
642         return 1;
643 }
644
645
646 CreateDVD_Deinterlace::CreateDVD_Deinterlace(CreateDVD_GUI *gui, int x, int y)
647  : BC_CheckBox(x, y, &gui->thread->use_deinterlace, _("Deinterlace"))
648 {
649         this->gui = gui;
650 }
651
652 CreateDVD_Deinterlace::~CreateDVD_Deinterlace()
653 {
654 }
655
656 int CreateDVD_Deinterlace::handle_event()
657 {
658         if( get_value() ) {
659                 gui->need_inverse_telecine->set_value(0);
660                 gui->thread->use_inverse_telecine = 0;
661         }
662         return BC_CheckBox::handle_event();
663 }
664
665
666 CreateDVD_InverseTelecine::CreateDVD_InverseTelecine(CreateDVD_GUI *gui, int x, int y)
667  : BC_CheckBox(x, y, &gui->thread->use_inverse_telecine, _("Inverse Telecine"))
668 {
669         this->gui = gui;
670 }
671
672 CreateDVD_InverseTelecine::~CreateDVD_InverseTelecine()
673 {
674 }
675
676 int CreateDVD_InverseTelecine::handle_event()
677 {
678         if( get_value() ) {
679                 gui->need_deinterlace->set_value(0);
680                 gui->thread->use_deinterlace = 0;
681         }
682         return BC_CheckBox::handle_event();
683 }
684
685
686 CreateDVD_ResizeTracks::CreateDVD_ResizeTracks(CreateDVD_GUI *gui, int x, int y)
687  : BC_CheckBox(x, y, &gui->thread->use_resize_tracks, _("Resize Tracks"))
688 {
689         this->gui = gui;
690 }
691
692 CreateDVD_ResizeTracks::~CreateDVD_ResizeTracks()
693 {
694 }
695
696
697 CreateDVD_Histogram::CreateDVD_Histogram(CreateDVD_GUI *gui, int x, int y)
698  : BC_CheckBox(x, y, &gui->thread->use_histogram, _("Histogram"))
699 {
700         this->gui = gui;
701 }
702
703 CreateDVD_Histogram::~CreateDVD_Histogram()
704 {
705 }
706
707 CreateDVD_LabelChapters::CreateDVD_LabelChapters(CreateDVD_GUI *gui, int x, int y)
708  : BC_CheckBox(x, y, &gui->thread->use_label_chapters, _("Chapters at Labels"))
709 {
710         this->gui = gui;
711 }
712
713 CreateDVD_LabelChapters::~CreateDVD_LabelChapters()
714 {
715 }
716
717 CreateDVD_WideAudio::CreateDVD_WideAudio(CreateDVD_GUI *gui, int x, int y)
718  : BC_CheckBox(x, y, &gui->thread->use_wide_audio, _("Audio 5.1"))
719 {
720         this->gui = gui;
721 }
722
723 CreateDVD_WideAudio::~CreateDVD_WideAudio()
724 {
725 }
726
727 CreateDVD_UseFFMpeg::CreateDVD_UseFFMpeg(CreateDVD_GUI *gui, int x, int y)
728  : BC_CheckBox(x, y, &gui->thread->use_ffmpeg, _("Use FFMPEG"))
729 {
730         this->gui = gui;
731 }
732
733 CreateDVD_UseFFMpeg::~CreateDVD_UseFFMpeg()
734 {
735 }
736
737
738
739
740 CreateDVD_GUI::CreateDVD_GUI(CreateDVD_Thread *thread, int x, int y, int w, int h)
741  : BC_Window(_(PROGRAM_NAME ": Create DVD"), x, y, w, h, 50, 50, 1, 0, 1)
742 {
743         this->thread = thread;
744         at_x = at_y = tmp_x = tmp_y = 0;
745         ok_x = ok_y = ok_w = ok_h = 0;
746         cancel_x = cancel_y = cancel_w = cancel_h = 0;
747         asset_title = 0;
748         tmp_path = 0;
749         btmp_path = 0;
750         disk_space = 0;
751         standard = 0;
752         scale = 0;
753         need_deinterlace = 0;
754         need_inverse_telecine = 0;
755         need_resize_tracks = 0;
756         need_histogram = 0;
757         need_wide_audio = 0;
758         need_label_chapters = 0;
759         ok = 0;
760         cancel = 0;
761 }
762
763 CreateDVD_GUI::~CreateDVD_GUI()
764 {
765 }
766
767 void CreateDVD_GUI::create_objects()
768 {
769         lock_window("CreateDVD_GUI::create_objects");
770         int pady = BC_TextBox::calculate_h(this, MEDIUMFONT, 0, 1) + 5;
771         int padx = BC_Title::calculate_w(this, (char*)"X", MEDIUMFONT);
772         int x = padx/2, y = pady/2;
773         BC_Title *title = new BC_Title(x, y, _("Title:"), MEDIUMFONT, YELLOW);
774         add_subwindow(title);
775         at_x = x + title->get_w();  at_y = y;
776         asset_title = new CreateDVD_AssetTitle(this, at_x, at_y, get_w()-at_x-10);
777         add_subwindow(asset_title);
778         y += title->get_h() + pady/2;
779         title = new BC_Title(x, y, _("Work path:"), MEDIUMFONT, YELLOW);
780         add_subwindow(title);
781         tmp_x = x + title->get_w();  tmp_y = y;
782         tmp_path = new CreateDVD_TmpPath(this, tmp_x, tmp_y,  get_w()-tmp_x-35);
783         add_subwindow(tmp_path);
784         btmp_path = new BrowseButton(thread->mwindow->theme, this, tmp_path,
785                 tmp_x+tmp_path->get_w(), tmp_y, "/tmp",
786                 _("Work path"), _("Select a Work directory:"), 1);
787         add_subwindow(btmp_path);
788         y += title->get_h() + pady/2;
789         disk_space = new CreateDVD_DiskSpace(this, x, y);
790         add_subwindow(disk_space);
791         int x0 = get_w() - 170;
792         title = new BC_Title(x0, y, _("Media:"), MEDIUMFONT, YELLOW);
793         add_subwindow(title);
794         int x1 = x0+title->get_w()+padx;
795         media_size = new CreateDVD_MediaSize(this, x1, y);
796         media_size->create_objects();
797         media_sizes.append(new BC_ListBoxItem("4.7GB"));
798         media_sizes.append(new BC_ListBoxItem("8.3GB"));
799         media_size->update_list(&media_sizes);
800         media_size->update(media_sizes[0]->get_text());
801         disk_space->update();
802         y += disk_space->get_h() + pady/2;
803         title = new BC_Title(x, y, _("Format:"), MEDIUMFONT, YELLOW);
804         add_subwindow(title);
805         standard = new CreateDVD_Format(this, title->get_w() + padx, y);
806         add_subwindow(standard);
807         standard->create_objects();
808         x0 -= 30;
809         title = new BC_Title(x0, y, _("Scale:"), MEDIUMFONT, YELLOW);
810         add_subwindow(title);
811         x1 = x0+title->get_w()+padx;
812         scale = new CreateDVD_Scale(this, x1, y);
813         add_subwindow(scale);
814         scale->create_objects();
815         y += standard->get_h() + pady/2;
816         need_deinterlace = new CreateDVD_Deinterlace(this, x, y);
817         add_subwindow(need_deinterlace);
818         x1 = x + 170;
819         int x2 = x1 + 170;
820         need_inverse_telecine = new CreateDVD_InverseTelecine(this, x1, y);
821         add_subwindow(need_inverse_telecine);
822         need_use_ffmpeg = new CreateDVD_UseFFMpeg(this, x2, y);
823         add_subwindow(need_use_ffmpeg);
824         y += need_deinterlace->get_h() + pady/2;
825         need_histogram = new CreateDVD_Histogram(this, x, y);
826         add_subwindow(need_histogram);
827         need_wide_audio = new CreateDVD_WideAudio(this, x1, y);
828         add_subwindow(need_wide_audio);
829         need_resize_tracks = new CreateDVD_ResizeTracks(this, x2, y);
830         add_subwindow(need_resize_tracks);
831         y += need_histogram->get_h() + pady/2;
832         need_label_chapters = new CreateDVD_LabelChapters(this, x1, y);
833         add_subwindow(need_label_chapters);
834         ok_w = BC_OKButton::calculate_w();
835         ok_h = BC_OKButton::calculate_h();
836         ok_x = 10;
837         ok_y = get_h() - ok_h - 10;
838         ok = new CreateDVD_OK(this, ok_x, ok_y);
839         add_subwindow(ok);
840         cancel_w = BC_CancelButton::calculate_w();
841         cancel_h = BC_CancelButton::calculate_h();
842         cancel_x = get_w() - cancel_w - 10,
843         cancel_y = get_h() - cancel_h - 10;
844         cancel = new CreateDVD_Cancel(this, cancel_x, cancel_y);
845         add_subwindow(cancel);
846         show_window();
847         unlock_window();
848 }
849
850 int CreateDVD_GUI::resize_event(int w, int h)
851 {
852         asset_title->reposition_window(at_x, at_y, get_w()-at_x-10);
853         tmp_path->reposition_window(tmp_x, tmp_y,  get_w()-tmp_x-35);
854         btmp_path->reposition_window(tmp_x+tmp_path->get_w(), tmp_y);
855         ok_y = h - ok_h - 10;
856         ok->reposition_window(ok_x, ok_y);
857         cancel_x = w - cancel_w - 10,
858         cancel_y = h - cancel_h - 10;
859         cancel->reposition_window(cancel_x, cancel_y);
860         return 0;
861 }
862
863 int CreateDVD_GUI::translation_event()
864 {
865         return 1;
866 }
867
868 int CreateDVD_GUI::close_event()
869 {
870         set_done(1);
871         return 1;
872 }
873
874 void CreateDVD_GUI::update()
875 {
876         scale->set_value(thread->use_scale);
877         need_deinterlace->set_value(thread->use_deinterlace);
878         need_inverse_telecine->set_value(thread->use_inverse_telecine);
879         need_use_ffmpeg->set_value(thread->use_ffmpeg);
880         need_resize_tracks->set_value(thread->use_resize_tracks);
881         need_histogram->set_value(thread->use_histogram);
882         need_wide_audio->set_value(thread->use_wide_audio);
883         need_label_chapters->set_value(thread->use_label_chapters);
884 }
885
886 int CreateDVD_Thread::
887 insert_video_plugin(const char *title, KeyFrame *default_keyframe)
888 {
889         Tracks *tracks = mwindow->edl->tracks;
890         for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
891                 if( vtrk->data_type != TRACK_VIDEO ) continue;
892                 if( !vtrk->record ) continue;
893                 vtrk->expand_view = 1;
894                 PluginSet *plugin_set = new PluginSet(mwindow->edl, vtrk);
895                 vtrk->plugin_set.append(plugin_set);
896                 Edits *edits = vtrk->edits;
897                 for( Edit *edit=edits->first; edit; edit=edit->next ) {
898                         plugin_set->insert_plugin(_(title),
899                                 edit->startproject, edit->length,
900                                 PLUGIN_STANDALONE, 0, default_keyframe, 0);
901                 }
902                 vtrk->optimize();
903         }
904         return 0;
905 }
906
907 int CreateDVD_Thread::
908 resize_tracks()
909 {
910         Tracks *tracks = mwindow->edl->tracks;
911         int trk_w = max_w, trk_h = max_h;
912         if( trk_w < dvd_width ) trk_w = dvd_width;
913         if( trk_h < dvd_height ) trk_h = dvd_height;
914         for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
915                 if( vtrk->data_type != TRACK_VIDEO ) continue;
916                 if( !vtrk->record ) continue;
917                 vtrk->track_w = trk_w;
918                 vtrk->track_h = trk_h;
919         }
920         return 0;
921 }
922
923 int CreateDVD_Thread::
924 option_presets()
925 {
926 // reset only probed options
927         use_deinterlace = 0;
928         use_scale = Rescale::none;
929         use_resize_tracks = 0;
930         use_wide_audio = 0;
931         use_label_chapters = 0;
932
933         if( !mwindow->edl ) return 1;
934
935         int norm = dvd_formats[use_standard].norm;
936         dvd_width = dvd_norms[norm].w;
937         dvd_height = dvd_norms[norm].h;
938         dvd_framerate = dvd_norms[norm].framerate;
939         int aspect = dvd_formats[use_standard].aspect;
940         dvd_aspect_width = dvd_aspects[aspect].w;
941         dvd_aspect_height = dvd_aspects[aspect].h;
942         double dvd_aspect = dvd_aspect_height > 0 ? dvd_aspect_width/dvd_aspect_height : 1;
943
944         Tracks *tracks = mwindow->edl->tracks;
945         max_w = 0;  max_h = 0;
946         int has_deinterlace = 0, has_scale = 0;
947         for( Track *trk=tracks->first; trk; trk=trk->next ) {
948                 if( !trk->record ) continue;
949                 Edits *edits = trk->edits;
950                 switch( trk->data_type ) {
951                 case TRACK_VIDEO:
952                         for( Edit *edit=edits->first; edit; edit=edit->next ) {
953                                 if( edit->silence() ) continue;
954                                 Indexable *indexable = edit->get_source();
955                                 int w = indexable->get_w();
956                                 if( w > max_w ) max_w = w;
957                                 if( w != dvd_width ) use_scale = Rescale::scaled;
958                                 int h = indexable->get_h();
959                                 if( h > max_h ) max_h = h;
960                                 if( h != dvd_height ) use_scale = Rescale::scaled;
961                                 float aw, ah;
962                                 MWindow::create_aspect_ratio(aw, ah, w, h);
963                                 double aspect = ah > 0 ? aw / ah : 1;
964                                 if( !EQUIV(aspect, dvd_aspect) ) use_scale = Rescale::scaled;
965                         }
966                         for( int i=0; i<trk->plugin_set.size(); ++i ) {
967                                 for( Plugin *plugin = (Plugin*)trk->plugin_set[i]->first;
968                                                 plugin; plugin=(Plugin*)plugin->next ) {
969                                         if( !strcmp(plugin->title, "Deinterlace") )
970                                                 has_deinterlace = 1;
971                                         if( !strcmp(plugin->title, "Auto Scale") ||
972                                             !strcmp(plugin->title, "Scale Ratio") ||
973                                             !strcmp(plugin->title, "Scale") )
974                                                 has_scale = 1;
975                                 }
976                         }
977                         break;
978                 }
979         }
980         if( has_scale )
981                 use_scale = Rescale::none;
982         if( use_scale != Rescale::none ) {
983                 if( max_w != dvd_width ) use_resize_tracks = 1;
984                 if( max_h != dvd_height ) use_resize_tracks = 1;
985         }
986         for( Track *trk=tracks->first; trk && !use_resize_tracks; trk=trk->next ) {
987                 if( !trk->record ) continue;
988                 switch( trk->data_type ) {
989                 case TRACK_VIDEO:
990                         if( trk->track_w != max_w ) use_resize_tracks = 1;
991                         if( trk->track_h != max_h ) use_resize_tracks = 1;
992                         break;
993                 }
994         }
995         if( !has_deinterlace && max_h > 2*dvd_height ) use_deinterlace = 1;
996         Labels *labels = mwindow->edl->labels;
997         use_label_chapters = labels && labels->first ? 1 : 0;
998
999         if( tracks->recordable_audio_tracks() == DVD_WIDE_CHANNELS )
1000                 use_wide_audio = 1;
1001
1002         return 0;
1003 }
1004
1005
1006
1007 CreateDVD_FormatItem::CreateDVD_FormatItem(CreateDVD_Format *popup,
1008                 int standard, const char *text)
1009  : BC_MenuItem(text)
1010 {
1011         this->popup = popup;
1012         this->standard = standard;
1013 }
1014
1015 CreateDVD_FormatItem::~CreateDVD_FormatItem()
1016 {
1017 }
1018
1019 int CreateDVD_FormatItem::handle_event()
1020 {
1021         popup->set_text(get_text());
1022         popup->gui->thread->use_standard = standard;
1023         return popup->handle_event();
1024 }
1025
1026
1027 CreateDVD_Format::CreateDVD_Format(CreateDVD_GUI *gui, int x, int y)
1028  : BC_PopupMenu(x, y, 180, "", 1)
1029 {
1030         this->gui = gui;
1031 }
1032
1033 CreateDVD_Format::~CreateDVD_Format()
1034 {
1035 }
1036
1037 void CreateDVD_Format::create_objects()
1038 {
1039         for( int i=0; i<(int)(sizeof(dvd_formats)/sizeof(dvd_formats[0])); ++i ) {
1040                 int norm = dvd_formats[i].norm;
1041                 int aspect = dvd_formats[i].aspect;
1042                 char item_text[BCTEXTLEN];
1043                 sprintf(item_text,"%4s (%5.2f) %dx%d",
1044                         dvd_norms[norm].name, dvd_norms[norm].framerate,
1045                         dvd_aspects[aspect].w, dvd_aspects[aspect].h);
1046                 add_item(new CreateDVD_FormatItem(this, i, item_text));
1047         }
1048         set_value(gui->thread->use_standard);
1049 }
1050
1051 int CreateDVD_Format::handle_event()
1052 {
1053         gui->thread->option_presets();
1054         gui->update();
1055         return 1;
1056 }
1057
1058
1059 CreateDVD_ScaleItem::CreateDVD_ScaleItem(CreateDVD_Scale *popup,
1060                 int scale, const char *text)
1061  : BC_MenuItem(text)
1062 {
1063         this->popup = popup;
1064         this->scale = scale;
1065 }
1066
1067 CreateDVD_ScaleItem::~CreateDVD_ScaleItem()
1068 {
1069 }
1070
1071 int CreateDVD_ScaleItem::handle_event()
1072 {
1073         popup->gui->thread->use_scale = scale;
1074         popup->set_value(scale);
1075         return popup->handle_event();
1076 }
1077
1078
1079 CreateDVD_Scale::CreateDVD_Scale(CreateDVD_GUI *gui, int x, int y)
1080  : BC_PopupMenu(x, y, 100, "", 1)
1081 {
1082         this->gui = gui;
1083 }
1084
1085 CreateDVD_Scale::~CreateDVD_Scale()
1086 {
1087 }
1088
1089 void CreateDVD_Scale::create_objects()
1090 {
1091
1092         for( int i=0; i<(int)Rescale::n_scale_types; ++i ) {
1093                 add_item(new CreateDVD_ScaleItem(this, i, Rescale::scale_types[i]));
1094         }
1095         set_value(gui->thread->use_scale);
1096 }
1097
1098 int CreateDVD_Scale::handle_event()
1099 {
1100         gui->update();
1101         return 1;
1102 }
1103
1104
1105 CreateDVD_MediaSize::CreateDVD_MediaSize(CreateDVD_GUI *gui, int x, int y)
1106  : BC_PopupTextBox(gui, 0, 0, x, y, 70,50)
1107 {
1108         this->gui = gui;
1109 }
1110
1111 CreateDVD_MediaSize::~CreateDVD_MediaSize()
1112 {
1113 }
1114
1115 int CreateDVD_MediaSize::handle_event()
1116 {
1117         gui->disk_space->update();
1118         return 1;
1119 }
1120