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