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