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