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