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