Credit Andrew - add additional 16x9 formats for bluray creation; add additional value...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / bdcreate.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 "bdcreate.h"
24 #include "clip.h"
25 #include "edl.h"
26 #include "edit.h"
27 #include "edits.h"
28 #include "edlsession.h"
29 #include "file.h"
30 #include "filexml.h"
31 #include "interlacemodes.h"
32 #include "keyframe.h"
33 #include "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 // BD Creation
65
66 // selected by timezone
67 #define BD_1920x1080_2997i      0
68 #define BD_1920x1080_25i        3
69
70 static struct bd_format {
71         const char *name;
72         int w, h;
73         double framerate;
74         int wide, interlaced;
75 } bd_formats[] = {
76 // framerates are frames, not fields, per second, *=not standard
77         { "1920x1080 29.97i",   1920,1080, 29.97,  1, ILACE_MODE_TOP_FIRST },
78         { "1920x1080 29.97p*",  1920,1080, 29.97,  1, ILACE_MODE_FAKE_INTERLACE },
79         { "1920x1080 24p",      1920,1080, 24.,    1, ILACE_MODE_NOTINTERLACED },
80         { "1920x1080 25i",      1920,1080, 25.,    1, ILACE_MODE_TOP_FIRST },
81         { "1920x1080 25p*",     1920,1080, 25.,    1, ILACE_MODE_FAKE_INTERLACE },
82         { "1920x1080 23.976p",  1920,1080, 23.976, 1, ILACE_MODE_NOTINTERLACED },
83         { "1440x1080 29.97i",   1440,1080, 29.97,  1, ILACE_MODE_TOP_FIRST },
84         { "1440x1080 25i",      1440,1080, 25.,    1, ILACE_MODE_TOP_FIRST },
85         { "1440x1080 24p",      1440,1080, 24.,    1, ILACE_MODE_NOTINTERLACED },
86         { "1440x1080 23.976p",  1440,1080, 23.976, 1, ILACE_MODE_NOTINTERLACED },
87         { "1280x720  59.94p",   1280,720,  59.94,  1, ILACE_MODE_NOTINTERLACED },
88         { "1280x720  50p",      1280,720,  50.,    1, ILACE_MODE_NOTINTERLACED },
89         { "1280x720  29.97p*",  1280,720,  29.97,  1, ILACE_MODE_NOTINTERLACED },
90         { "1280x720  25p*",     1280,720,  25.,    1, ILACE_MODE_NOTINTERLACED },
91         { "1280x720  24p",      1280,720,  24.,    1, ILACE_MODE_NOTINTERLACED },
92         { "1280x720  23.976p",  1280,720,  23.976, 1, ILACE_MODE_NOTINTERLACED },
93         { "720x576   25i",       720,576,  25.,    0, ILACE_MODE_BOTTOM_FIRST },
94         { "720x576   25p*",      720,576,  25.,    0, ILACE_MODE_NOTINTERLACED },
95         { "720x576(w)   25i",    720,576,  25.,    1, ILACE_MODE_BOTTOM_FIRST },
96         { "720x576(w)   25p*",   720,576,  25.,    1, ILACE_MODE_NOTINTERLACED },
97         { "720x480   29.97i",    720,480,  29.97,  0, ILACE_MODE_BOTTOM_FIRST },
98         { "720x480   29.97p*",   720,480,  29.97,  0, ILACE_MODE_NOTINTERLACED },
99         { "720x480(w)   29.97i",         720,480,  29.97,  1, ILACE_MODE_BOTTOM_FIRST },
100         { "720x480(w)   29.97p*",        720,480,  29.97,  1, ILACE_MODE_NOTINTERLACED },
101
102 };
103
104 static struct bd_profile {
105         const char *name;
106 } bd_profiles[] = {
107         {"bluray.m2ts"},
108         {"bluray_lpcm.m2ts"},
109 };
110
111
112 const int64_t CreateBD_Thread::BD_SIZE = 25000000000;
113 const int CreateBD_Thread::BD_STREAMS = 1;
114 const int CreateBD_Thread::BD_WIDTH = 1920;
115 const int CreateBD_Thread::BD_HEIGHT = 1080;
116 const double CreateBD_Thread::BD_WIDE_ASPECT_WIDTH = 16.;
117 const double CreateBD_Thread::BD_WIDE_ASPECT_HEIGHT = 9.;
118 const double CreateBD_Thread::BD_ASPECT_WIDTH = 4.;
119 const double CreateBD_Thread::BD_ASPECT_HEIGHT = 3.;
120 const double CreateBD_Thread::BD_FRAMERATE = 24000. / 1001.;
121 //const int CreateBD_Thread::BD_MAX_BITRATE = 40000000;
122 const int CreateBD_Thread::BD_MAX_BITRATE = 10000000;
123 const int CreateBD_Thread::BD_CHANNELS = 2;
124 const int CreateBD_Thread::BD_WIDE_CHANNELS = 6;
125 const double CreateBD_Thread::BD_SAMPLERATE = 48000;
126 const double CreateBD_Thread::BD_KAUDIO_RATE = 192;
127 const int CreateBD_Thread::BD_INTERLACE_MODE = ILACE_MODE_NOTINTERLACED;
128
129 CreateBD_MenuItem::CreateBD_MenuItem(MWindow *mwindow)
130  : BC_MenuItem(_("BD Render..."), _("Ctrl-Shift-D"), 'D')
131 {
132         set_ctrl(1);
133         set_shift(1);
134         this->mwindow = mwindow;
135 }
136
137 int CreateBD_MenuItem::handle_event()
138 {
139         mwindow->create_bd->start();
140         return 1;
141 }
142
143 BD_BatchRenderJob::BD_BatchRenderJob(Preferences *preferences, int labeled, int farmed)
144  : BatchRenderJob("BD_JOB", preferences, labeled, farmed)
145 {
146 }
147
148 int BD_BatchRenderJob::get_udfs_mount(char *udfs, char *mopts, char *mntpt)
149 {
150         int ret = 0;
151 // default: mount -t udf -o loop $dir/bd.udfs $dir/udfs
152         strcpy(udfs,"$dir/bd.udfs");
153         strcpy(mopts,"-t udf -o loop $dir/bd.udfs ");
154         strcpy(mntpt,"$dir/udfs");
155         const char *home = getenv("HOME");
156         if( !home ) return ret;
157         FILE *fp = fopen("/etc/fstab","r");
158         if( !fp ) return ret;
159         int len = strlen(home);
160         char line[BCTEXTLEN], typ[BCTEXTLEN], file[BCTEXTLEN];
161         char mpnt[BCTEXTLEN], opts[BCTEXTLEN];
162         while( fgets(line,sizeof(line),fp) ) {
163 // search "/etc/fstab" for: $HOME/img_file $HOME/bluray udf noauto,loop,rw,user
164                 if( line[0] == '#' || line[0] == ';' ) continue;
165                 if( sscanf(line,"%s %s %s %s ", &file[0], &mpnt[0], &typ[0], &opts[0]) != 4 )
166                         continue;
167                 if( strcmp("udf", typ) ) continue;
168                 if( strncmp(home, file, len) || file[len] != '/' ) continue;
169                 if( strncmp(home, mpnt, len) ) continue;
170                 if( strcmp(&mpnt[len], "/bluray") ) continue;
171                 int loop = 0, user = 0, rw = 0, noauto = 0;
172                 char *op = opts, *ep = op + sizeof(opts)-1;
173                 while( op < ep && *op ) {
174                         char opt[BCTEXTLEN], *cp = opt;
175                         while( op < ep && *op && (*cp=*op++)!=',' ) ++cp;
176                         *cp = 0;
177                         if( !strcmp("loop", opt) ) loop = 1;
178                         else if( !strcmp("user", opt) ) user = 1;
179                         else if( !strcmp("noauto", opt) ) noauto = 1;
180                         else if( !strcmp("rw", opt) ) rw = 1;
181                 }
182                 if( loop && user && rw && noauto ) {
183                         strcpy(udfs, file);
184                         strcpy(mopts, "");
185                         strcpy(mntpt, mpnt);
186                         ret = 1;
187                         break;
188                 }
189         }
190         fclose(fp);
191         return ret;
192 }
193
194
195
196 char *BD_BatchRenderJob::create_script(EDL *edl, ArrayList<Indexable *> *idxbls)
197 {
198         char script[BCTEXTLEN];
199         strcpy(script, edl_path);
200
201         FILE *fp = 0;
202         char *bp = strrchr(script,'/');
203         int fd = -1;
204         if( bp ) {
205                 strcpy(bp, "/bd.sh");
206                 fd = open(script, O_WRONLY+O_CREAT+O_TRUNC, 0755);
207         }
208         if( fd >= 0 )
209                 fp = fdopen(fd, "w");
210         if( !fp ) {
211                 char err[BCTEXTLEN], msg[BCTEXTLEN];
212                 strerror_r(errno, err, sizeof(err));
213                 sprintf(msg, _("Unable to save: %s\n-- %s"), script, err);
214                 MainError::show_error(msg);
215                 return 0;
216         }
217         char udfs[BCTEXTLEN], mopts[BCTEXTLEN], mntpt[BCTEXTLEN];
218         int is_usr_mnt = get_udfs_mount(udfs, mopts, mntpt);
219         const char *exec_path = File::get_cinlib_path();
220         fprintf(fp,"#!/bin/bash -ex\n");
221         fprintf(fp,"sdir=`dirname $0`\n");
222         fprintf(fp,"dir=`cd \"$sdir\"; pwd`\n");
223         fprintf(fp,"PATH=$PATH:%s\n",exec_path);
224         fprintf(fp,"mkdir -p $dir/udfs\n");
225         fprintf(fp,"sz=`du -cb $dir/bd.m2ts* | tail -1 | sed -e 's/[    ].*//'`\n");
226         fprintf(fp,"blks=$((sz/2048 + 4096))\n");
227         fprintf(fp,"rm -f %s\n", udfs);
228         fprintf(fp,"if [ -f $dir/bd.meta ]; then\n");
229         fprintf(fp,"sed -i \"s|bd.m2ts|$dir\/bd.m2ts|g\" $dir/bd.meta\n");
230         fprintf(fp,"tsmuxer $dir/bd.meta $dir/bd.iso \n");
231         fprintf(fp,"mv $dir/bd.iso $dir/bd.udfs\n");
232         fprintf(fp,"else\n");
233         fprintf(fp,"mkudffs -b 2048 %s $blks\n", udfs);
234         fprintf(fp,"mount %s%s\n", mopts, mntpt);
235         fprintf(fp,"bdwrite %s $dir/bd.m2ts*\n",mntpt);
236         fprintf(fp,"umount %s\n",mntpt);
237         fprintf(fp,"fi\n");
238         if( is_usr_mnt )
239                 fprintf(fp,"mv -f %s $dir/bd.udfs\n", udfs);
240         fprintf(fp,"echo To burn bluray, load writable media and run:\n");
241         fprintf(fp,"echo for WORM: growisofs -dvd-compat -Z /dev/bd=$dir/bd.udfs\n");
242         fprintf(fp,"echo for RW:   dd if=$dir/bd.udfs of=/dev/bd bs=2048000\n");
243         fprintf(fp,"kill $$\n");
244         fprintf(fp,"\n");
245         fclose(fp);
246         return cstrdup(script);
247 }
248
249
250 CreateBD_Thread::CreateBD_Thread(MWindow *mwindow)
251  : BC_DialogThread()
252 {
253         this->mwindow = mwindow;
254         this->gui = 0;
255         this->use_deinterlace = 0;
256         this->use_scale = Rescale::none;
257         this->use_histogram = 0;
258         this->use_inverse_telecine = 0;
259         this->use_wide_audio = 0;
260         this->use_resize_tracks = 0;
261         this->use_labeled = 0;
262         this->use_farmed = 0;
263         this->use_tsmuxer = 0;
264
265         strcpy(use_profile,"bluray.m2ts");
266
267         this->bd_size = BD_SIZE;
268         this->bd_width = BD_WIDTH;
269         this->bd_height = BD_HEIGHT;
270         this->bd_aspect_width = BD_WIDE_ASPECT_WIDTH;
271         this->bd_aspect_height = BD_WIDE_ASPECT_HEIGHT;
272         this->bd_framerate = BD_FRAMERATE;
273         this->bd_samplerate = BD_SAMPLERATE;
274         this->bd_max_bitrate = BD_MAX_BITRATE;
275         this->bd_kaudio_rate = BD_KAUDIO_RATE;
276         this->max_w = this->max_h = 0;
277         this->batchrender = 0;
278 }
279
280 CreateBD_Thread::~CreateBD_Thread()
281 {
282         close_window();
283 }
284
285 int CreateBD_Thread::create_bd_jobs(ArrayList<BatchRenderJob*> *jobs, const char *asset_dir)
286 {
287         EDL *edl = mwindow->edl;
288         if( !edl || !edl->session ) {
289                 char msg[BCTEXTLEN];
290                 sprintf(msg, _("No EDL/Session"));
291                 MainError::show_error(msg);
292                 return 1;
293         }
294         EDLSession *session = edl->session;
295
296         double total_length = edl->tracks->total_length();
297         if( total_length <= 0 ) {
298                 char msg[BCTEXTLEN];
299                 sprintf(msg, _("No content: %s"), asset_title);
300                 MainError::show_error(msg);
301                 return 1;
302         }
303
304         if( mkdir(asset_dir, 0777) ) {
305                 char err[BCTEXTLEN], msg[BCTEXTLEN];
306                 strerror_r(errno, err, sizeof(err));
307                 sprintf(msg, _("Unable to create directory: %s\n-- %s"), asset_dir, err);
308                 MainError::show_error(msg);
309                 return 1;
310         }
311
312         double old_samplerate = session->sample_rate;
313         double old_framerate = session->frame_rate;
314
315         session->video_channels = BD_STREAMS;
316         session->video_tracks = BD_STREAMS;
317         session->frame_rate = bd_framerate;
318         session->output_w = bd_width;
319         session->output_h = bd_height;
320         session->aspect_w = bd_aspect_width;
321         session->aspect_h = bd_aspect_height;
322         session->sample_rate = bd_samplerate;
323         session->audio_channels = session->audio_tracks =
324                 use_wide_audio ? BD_WIDE_CHANNELS : BD_CHANNELS;
325         session->interlace_mode = bd_interlace_mode;
326
327         session->audio_channels = session->audio_tracks =
328                 !use_wide_audio ? BD_CHANNELS : BD_WIDE_CHANNELS;
329         for( int i=0; i<MAX_CHANNELS; ++i )
330                 session->achannel_positions[i] = default_audio_channel_position(i, session->audio_channels);
331         int audio_mapping = edl->tracks->recordable_audio_tracks() == BD_WIDE_CHANNELS &&
332                 !use_wide_audio ? MWindow::AUDIO_5_1_TO_2 : MWindow::AUDIO_1_TO_1;
333         mwindow->remap_audio(audio_mapping);
334
335         double new_samplerate = session->sample_rate;
336         double new_framerate = session->frame_rate;
337         edl->retrack();
338         edl->rechannel();
339         edl->resample(old_samplerate, new_samplerate, TRACK_AUDIO);
340         edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
341
342         int64_t aud_size = ((bd_kaudio_rate * total_length)/8 + 1000-1) * 1000;
343         int64_t vid_size = bd_size*0.96 - aud_size;
344         int64_t vid_bitrate = (vid_size * 8) / total_length;
345         vid_bitrate /= 1000;  vid_bitrate *= 1000;
346         if( vid_bitrate > bd_max_bitrate ) vid_bitrate = bd_max_bitrate;
347
348         char xml_filename[BCTEXTLEN];
349         sprintf(xml_filename, "%s/bd.xml", asset_dir);
350         FileXML xml_file;
351         edl->save_xml(&xml_file, xml_filename);
352         xml_file.terminate_string();
353         if( xml_file.write_to_file(xml_filename) ) {
354                 char msg[BCTEXTLEN];
355                 sprintf(msg, _("Unable to save: %s"), xml_filename);
356                 MainError::show_error(msg);
357                 return 1;
358         }
359
360         if (use_tsmuxer) {
361         char meta_script[BCTEXTLEN];
362         strcpy(meta_script, "/");
363
364         FILE *fp = 0;
365         char *bp = strrchr(meta_script,'/');
366         int fd = -1;
367         if( bp ) {
368                 char script_filename[BCTEXTLEN];
369                 sprintf(script_filename, "%s/bd.meta", asset_dir);
370                 strcpy(bp, script_filename);
371                 fd = open(meta_script, O_WRONLY+O_CREAT+O_TRUNC, 0755);
372         }
373         if( fd >= 0 )
374                 fp = fdopen(fd, "w");
375         if( !fp ) {
376                 char err[BCTEXTLEN], msg[BCTEXTLEN];
377                 strerror_r(errno, err, sizeof(err));
378                 sprintf(msg, _("Unable to save: %s\n-- %s"), meta_script, err);
379                 MainError::show_error(msg);
380                 return 0;
381         }
382
383
384         fprintf(fp,"MUXOPT --blu-ray --hdmv-descriptors --auto-chapters=5\n");
385         fprintf(fp,"V_MPEG4/ISO/AVC, bd.m2ts, track=4113\n");
386         if(!strcmp(use_profile, "bluray.m2ts"))
387         fprintf(fp,"A_AC3, bd.m2ts, track=4352\n");
388         if(!strcmp(use_profile, "bluray_lpcm.m2ts"))
389         fprintf(fp,"A_LPCM, bd.m2ts, track=4352\n");
390         if(!strcmp(use_profile, "bluray_truehd.m2ts"))
391         fprintf(fp,"A_MLP, bd.m2ts, track=4352\n");
392         fprintf(fp,"\n");
393         fclose(fp);
394
395         }
396
397         BatchRenderJob *job = new BD_BatchRenderJob(mwindow->preferences,
398                 use_labeled, use_farmed);
399         jobs->append(job);
400         strcpy(&job->edl_path[0], xml_filename);
401         Asset *asset = job->asset;
402
403         asset->layers = BD_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         asset->interlace_mode = session->interlace_mode;
409
410         char option_path[BCTEXTLEN];
411         sprintf(&asset->path[0],"%s/bd.m2ts", asset_dir);
412         asset->format = FILE_FFMPEG;
413         strcpy(asset->fformat, "m2ts");
414
415         asset->audio_data = 1;
416         strcpy(asset->acodec, use_profile);
417         //mwindow->defaults->get("DEFAULT_BLURAY_ACODEC", asset->acodec);
418         FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
419         FFMPEG::load_options(option_path, asset->ff_audio_options,
420                          sizeof(asset->ff_audio_options));
421         asset->ff_audio_bitrate = bd_kaudio_rate * 1000;
422
423         asset->video_data = 1;
424         const char *vcodec = use_profile;
425         switch( asset->interlace_mode ) {
426         case ILACE_MODE_TOP_FIRST:    vcodec = "bluray_tff.m2ts";  break;
427         case ILACE_MODE_BOTTOM_FIRST: vcodec = "bluray_bff.m2ts";  break;
428         case ILACE_MODE_FAKE_INTERLACE : vcodec = "bluray_fakeinterlace.m2ts";  break;
429         }
430         strcpy(asset->vcodec, vcodec);
431         //mwindow->defaults->get("DEFAULT_BLURAY_VCODEC", asset->vcodec);
432         FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
433         FFMPEG::load_options(option_path, asset->ff_video_options,
434                  sizeof(asset->ff_video_options));
435         if(session->aspect_w == 16) {
436         char aspect_line[100];
437         float par = (float)((float)asset->width / (float)asset->height);
438         sprintf(aspect_line, "aspect=%f\n", asset->aspect_ratio/par);
439         strcat(asset->ff_video_options, aspect_line);
440         //printf("Aspect line: %s par: %f width: %i height: %i\n ", aspect_line, par, asset->width, asset->height);
441         }
442         asset->ff_video_bitrate = vid_bitrate;
443         asset->ff_video_quality = -1;
444         return 0;
445 }
446
447 void CreateBD_Thread::handle_close_event(int result)
448 {
449         if( result ) return;
450         mwindow->defaults->update("WORK_DIRECTORY", tmp_path);
451         mwindow->batch_render->load_defaults(mwindow->defaults);
452         mwindow->undo->update_undo_before();
453         KeyFrame keyframe;  char data[BCTEXTLEN];
454         if( use_deinterlace ) {
455                 sprintf(data,"<DEINTERLACE MODE=1>");
456                 keyframe.set_data(data);
457                 insert_video_plugin("Deinterlace", &keyframe);
458         }
459         if( use_inverse_telecine ) {
460                 sprintf(data,"<IVTC FRAME_OFFSET=0 FIRST_FIELD=0 "
461                         "AUTOMATIC=1 AUTO_THRESHOLD=2.0e+00 PATTERN=2>");
462                 keyframe.set_data(data);
463                 insert_video_plugin("Inverse Telecine", &keyframe);
464         }
465         if( use_scale != Rescale::none ) {
466                 double bd_aspect = bd_aspect_height > 0 ? bd_aspect_width / bd_aspect_height : 1;
467
468                 Tracks *tracks = mwindow->edl->tracks;
469                 for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
470                         if( vtrk->data_type != TRACK_VIDEO ) continue;
471                         if( !vtrk->is_armed() ) continue;
472                         vtrk->expand_view = 1;
473                         PluginSet *plugin_set = new PluginSet(mwindow->edl, vtrk);
474                         vtrk->plugin_set.append(plugin_set);
475                         Edits *edits = vtrk->edits;
476                         for( Edit *edit=edits->first; edit; edit=edit->next ) {
477                                 Indexable *indexable = edit->get_source();
478                                 if( !indexable ) continue;
479                                 Rescale in(indexable);
480                                 Rescale out(bd_width, bd_height, bd_aspect);
481                                 float src_w, src_h, dst_w, dst_h;
482                                 in.rescale(out,use_scale, src_w,src_h, dst_w,dst_h);
483                                 sprintf(data,"<SCALERATIO TYPE=%d"
484                                         " IN_W=%d IN_H=%d IN_ASPECT_RATIO=%f"
485                                         " OUT_W=%d OUT_H=%d OUT_ASPECT_RATIO=%f"
486                                         " SRC_X=%f SRC_Y=%f SRC_W=%f SRC_H=%f"
487                                         " DST_X=%f DST_Y=%f DST_W=%f DST_H=%f>", use_scale,
488                                         in.w, in.h, in.aspect, out.w, out.h, out.aspect,
489                                         0., 0., src_w, src_h, 0., 0., dst_w, dst_h);
490                                 keyframe.set_data(data);
491                                 plugin_set->insert_plugin(_("Scale Ratio"),
492                                         edit->startproject, edit->length,
493                                         PLUGIN_STANDALONE, 0, &keyframe, 0);
494                         }
495                         vtrk->optimize();
496                 }
497         }
498         if( use_resize_tracks )
499                 resize_tracks();
500         if( use_histogram ) {
501 #if 0
502                 sprintf(data, "<HISTOGRAM OUTPUT_MIN_0=0 OUTPUT_MAX_0=1 "
503                         "OUTPUT_MIN_1=0 OUTPUT_MAX_1=1 "
504                         "OUTPUT_MIN_2=0 OUTPUT_MAX_2=1 "
505                         "OUTPUT_MIN_3=0 OUTPUT_MAX_3=1 "
506                         "AUTOMATIC=0 THRESHOLD=9.0-01 PLOT=0 SPLIT=0>"
507                         "<POINTS></POINTS><POINTS></POINTS><POINTS></POINTS>"
508                         "<POINTS><POINT X=6.0e-02 Y=0>"
509                                 "<POINT X=9.4e-01 Y=1></POINTS>");
510 #else
511                 sprintf(data, "<HISTOGRAM AUTOMATIC=0 THRESHOLD=1.0e-01 "
512                         "PLOT=0 SPLIT=0 W=440 H=500 PARADE=0 MODE=3 "
513                         "LOW_OUTPUT_0=0 HIGH_OUTPUT_0=1 LOW_INPUT_0=0 HIGH_INPUT_0=1 GAMMA_0=1 "
514                         "LOW_OUTPUT_1=0 HIGH_OUTPUT_1=1 LOW_INPUT_1=0 HIGH_INPUT_1=1 GAMMA_1=1 "
515                         "LOW_OUTPUT_2=0 HIGH_OUTPUT_2=1 LOW_INPUT_2=0 HIGH_INPUT_2=1 GAMMA_2=1 "
516                         "LOW_OUTPUT_3=0 HIGH_OUTPUT_3=1 LOW_INPUT_3=0.044 HIGH_INPUT_3=0.956 "
517                         "GAMMA_3=1>");
518 #endif
519                 keyframe.set_data(data);
520                 insert_video_plugin("Histogram", &keyframe);
521         }
522
523         char asset_dir[BCTEXTLEN], jobs_path[BCTEXTLEN];
524         snprintf(asset_dir, sizeof(asset_dir), "%s/%s", tmp_path, asset_title);
525         snprintf(jobs_path, sizeof(jobs_path), "%s/bd.jobs", asset_dir);
526         //batchrender->tsmuxered = use_tsmuxer;
527         mwindow->batch_render->reset(jobs_path);
528         int ret = create_bd_jobs(&mwindow->batch_render->jobs, asset_dir);
529         mwindow->undo->update_undo_after(_("create bd"), LOAD_ALL);
530         mwindow->resync_guis();
531         if( ret ) return;
532         mwindow->batch_render->save_jobs();
533         mwindow->batch_render->start(-use_farmed, -use_labeled);
534 }
535
536 BC_Window* CreateBD_Thread::new_gui()
537 {
538         strcpy(tmp_path, "/tmp");
539         mwindow->defaults->get("WORK_DIRECTORY", tmp_path);
540         memset(asset_title,0,sizeof(asset_title));
541         time_t dt;      time(&dt);
542         struct tm dtm;  localtime_r(&dt, &dtm);
543         sprintf(asset_title, "bd_%02d%02d%02d-%02d%02d%02d",
544                 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
545                 dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
546         use_deinterlace = 0;
547         use_scale = Rescale::none;
548         use_histogram = 0;
549         use_inverse_telecine = 0;
550         use_wide_audio = 0;
551         use_resize_tracks = 0;
552         use_labeled = 0;
553         use_farmed = 0;
554         use_tsmuxer = 0;
555         use_standard = !strcmp(mwindow->default_std(),"NTSC") ?
556                  BD_1920x1080_2997i : BD_1920x1080_25i;
557         bd_size = BD_SIZE;
558         bd_width = BD_WIDTH;
559         bd_height = BD_HEIGHT;
560         bd_aspect_width = BD_WIDE_ASPECT_WIDTH;
561         bd_aspect_height = BD_WIDE_ASPECT_HEIGHT;
562         bd_framerate = BD_FRAMERATE;
563         bd_samplerate = BD_SAMPLERATE;
564         bd_max_bitrate = BD_MAX_BITRATE;
565         bd_kaudio_rate = BD_KAUDIO_RATE;
566         bd_interlace_mode = BD_INTERLACE_MODE;
567         max_w = 0; max_h = 0;
568
569         int has_standard = -1;
570         if( mwindow->edl ) {
571                 EDLSession *session = mwindow->edl->session;
572 // match the session to any known standard
573                 for( int i=0; i<(int)(sizeof(bd_formats)/sizeof(bd_formats[0])); ++i ) {
574                         if( !EQUIV(session->frame_rate, bd_formats[i].framerate) ) continue;
575                         if( session->output_w != bd_formats[i].w ) continue;
576                         if( session->output_h != bd_formats[i].h ) continue;
577                         has_standard = i;  break;
578                 }
579         }
580         if( has_standard >= 0 )
581                 use_standard = has_standard;
582
583         option_presets();
584         int scr_x = mwindow->gui->get_screen_x(0, -1);
585         int scr_w = mwindow->gui->get_screen_w(0, -1);
586         int scr_h = mwindow->gui->get_screen_h(0, -1);
587         int w = xS(560), h = yS(340);
588         int x = scr_x + scr_w/2 - w/2, y = scr_h/2 - h/2;
589
590         gui = new CreateBD_GUI(this, x, y, w, h);
591         gui->create_objects();
592
593         if( getuid() != 0 ) {
594                 mwindow->show_warning(
595                         &mwindow->preferences->bd_warn_root,
596                         _("Must be root to mount UDFS images\n"));
597         }
598         return gui;
599 }
600
601
602 CreateBD_OK::CreateBD_OK(CreateBD_GUI *gui, int x, int y)
603  : BC_OKButton(x, y)
604 {
605         this->gui = gui;
606         set_tooltip(_("end setup, start batch render"));
607 }
608
609 CreateBD_OK::~CreateBD_OK()
610 {
611 }
612
613 int CreateBD_OK::button_press_event()
614 {
615         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
616                 gui->set_done(0);
617                 return 1;
618         }
619         return 0;
620 }
621
622 int CreateBD_OK::keypress_event()
623 {
624         return context_help_check_and_show();
625 }
626
627
628 CreateBD_Cancel::CreateBD_Cancel(CreateBD_GUI *gui, int x, int y)
629  : BC_CancelButton(x, y)
630 {
631         this->gui = gui;
632 }
633
634 CreateBD_Cancel::~CreateBD_Cancel()
635 {
636 }
637
638 int CreateBD_Cancel::button_press_event()
639 {
640         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
641                 gui->set_done(1);
642                 return 1;
643         }
644         return 0;
645 }
646
647
648 CreateBD_DiskSpace::CreateBD_DiskSpace(CreateBD_GUI *gui, int x, int y)
649  : BC_Title(x, y, "", MEDIUMFONT, GREEN)
650 {
651         this->gui = gui;
652 }
653
654 CreateBD_DiskSpace::~CreateBD_DiskSpace()
655 {
656 }
657
658 int64_t CreateBD_DiskSpace::tmp_path_space()
659 {
660         const char *path = gui->thread->tmp_path;
661         if( access(path,R_OK+W_OK) ) return 0;
662         struct statfs sfs;
663         if( statfs(path, &sfs) ) return 0;
664         return (int64_t)sfs.f_bsize * sfs.f_bfree;
665 }
666
667 void CreateBD_DiskSpace::update()
668 {
669         static const char *suffix[] = { "", "KB", "MB", "GB", "TB", "PB" };
670         int64_t disk_space = tmp_path_space();
671         double media_size = 100e9, msz = 0, m = 1;
672         char sfx[BCSTRLEN];
673         if( sscanf(gui->media_size->get_text(), "%lf%s", &msz, sfx) == 2 ) {
674                 int i = sizeof(suffix)/sizeof(suffix[0]);
675                 while( --i >= 0 && strcmp(sfx, suffix[i]) );
676                 while( --i >= 0 ) m *= 1000;
677                 media_size = msz * m;
678         }
679         int color = disk_space < media_size*2 ? RED : GREEN;
680         int i = 0;
681         for( int64_t space=disk_space; i<5 && (space/=1000)>0; disk_space=space, ++i );
682         char text[BCTEXTLEN];
683         sprintf(text, "%s%3jd%s", _("disk space: "), disk_space, suffix[i]);
684         gui->disk_space->BC_Title::update(text);
685         gui->disk_space->set_color(color);
686 }
687
688 CreateBD_TmpPath::CreateBD_TmpPath(CreateBD_GUI *gui, int x, int y, int w)
689  : BC_TextBox(x, y, w, 1, -(int)sizeof(gui->thread->tmp_path),
690                 gui->thread->tmp_path, 1, MEDIUMFONT)
691 {
692         this->gui = gui;
693 }
694
695 CreateBD_TmpPath::~CreateBD_TmpPath()
696 {
697 }
698
699 int CreateBD_TmpPath::handle_event()
700 {
701         get_text();
702         gui->disk_space->update();
703         return 1;
704 }
705
706
707 CreateBD_AssetTitle::CreateBD_AssetTitle(CreateBD_GUI *gui, int x, int y, int w)
708  : BC_TextBox(x, y, w, 1, -(int)sizeof(gui->thread->asset_title),
709                 gui->thread->asset_title, 1, MEDIUMFONT)
710 {
711         this->gui = gui;
712 }
713
714 CreateBD_AssetTitle::~CreateBD_AssetTitle()
715 {
716 }
717
718 int CreateBD_AssetTitle::handle_event()
719 {
720         get_text();
721         return 1;
722 }
723
724 CreateBD_Deinterlace::CreateBD_Deinterlace(CreateBD_GUI *gui, int x, int y)
725  : BC_CheckBox(x, y, &gui->thread->use_deinterlace, _("Deinterlace"))
726 {
727         this->gui = gui;
728 }
729
730 CreateBD_Deinterlace::~CreateBD_Deinterlace()
731 {
732 }
733
734 int CreateBD_Deinterlace::handle_event()
735 {
736         if( get_value() ) {
737                 gui->need_inverse_telecine->set_value(0);
738                 gui->thread->use_inverse_telecine = 0;
739         }
740         return BC_CheckBox::handle_event();
741 }
742
743
744 CreateBD_InverseTelecine::CreateBD_InverseTelecine(CreateBD_GUI *gui, int x, int y)
745  : BC_CheckBox(x, y, &gui->thread->use_inverse_telecine, _("Inverse Telecine"))
746 {
747         this->gui = gui;
748 }
749
750 CreateBD_InverseTelecine::~CreateBD_InverseTelecine()
751 {
752 }
753
754 int CreateBD_InverseTelecine::handle_event()
755 {
756         if( get_value() ) {
757                 gui->need_deinterlace->set_value(0);
758                 gui->thread->use_deinterlace = 0;
759         }
760         return BC_CheckBox::handle_event();
761 }
762
763
764 CreateBD_ResizeTracks::CreateBD_ResizeTracks(CreateBD_GUI *gui, int x, int y)
765  : BC_CheckBox(x, y, &gui->thread->use_resize_tracks, _("Resize Tracks"))
766 {
767         this->gui = gui;
768 }
769
770 CreateBD_ResizeTracks::~CreateBD_ResizeTracks()
771 {
772 }
773
774
775 CreateBD_Histogram::CreateBD_Histogram(CreateBD_GUI *gui, int x, int y)
776  : BC_CheckBox(x, y, &gui->thread->use_histogram, _("Histogram"))
777 {
778         this->gui = gui;
779 }
780
781 CreateBD_Histogram::~CreateBD_Histogram()
782 {
783 }
784
785 CreateBD_LabelChapters::CreateBD_LabelChapters(CreateBD_GUI *gui, int x, int y)
786  : BC_CheckBox(x, y, &gui->thread->use_labeled, _("Chapters at Labels"))
787 {
788         this->gui = gui;
789 }
790
791 CreateBD_LabelChapters::~CreateBD_LabelChapters()
792 {
793 }
794
795 CreateBD_UseRenderFarm::CreateBD_UseRenderFarm(CreateBD_GUI *gui, int x, int y)
796  : BC_CheckBox(x, y, &gui->thread->use_farmed, _("Use render farm"))
797 {
798         this->gui = gui;
799 }
800
801 CreateBD_UseRenderFarm::~CreateBD_UseRenderFarm()
802 {
803 }
804
805
806 CreateBD_WideAudio::CreateBD_WideAudio(CreateBD_GUI *gui, int x, int y)
807  : BC_CheckBox(x, y, &gui->thread->use_wide_audio, _("Audio 5.1"))
808 {
809         this->gui = gui;
810 }
811
812 CreateBD_WideAudio::~CreateBD_WideAudio()
813 {
814 }
815
816 CreateBD_UseTsmuxer::CreateBD_UseTsmuxer(CreateBD_GUI *gui, int x, int y)
817  : BC_CheckBox(x, y, &gui->thread->use_tsmuxer, _("use tsmuxer"))
818 {
819         this->gui = gui;
820 }
821
822 CreateBD_UseTsmuxer::~CreateBD_UseTsmuxer()
823 {
824 }
825
826
827 CreateBD_GUI::CreateBD_GUI(CreateBD_Thread *thread, int x, int y, int w, int h)
828  : BC_Window(_(PROGRAM_NAME ": Create BD"), x, y, w, h, xS(50), yS(50), 1, 0, 1)
829 {
830         this->thread = thread;
831         at_x = at_y = tmp_x = tmp_y = 0;
832         ok_x = ok_y = ok_w = ok_h = 0;
833         cancel_x = cancel_y = cancel_w = cancel_h = 0;
834         asset_title = 0;
835         tmp_path = 0;
836         btmp_path = 0;
837         disk_space = 0;
838         standard = 0;
839         scale = 0;
840         profile = 0;
841         need_deinterlace = 0;
842         need_inverse_telecine = 0;
843         need_resize_tracks = 0;
844         need_histogram = 0;
845         non_standard = 0;
846         need_wide_audio = 0;
847         need_labeled = 0;
848         need_farmed = 0;
849         need_tsmuxer = 0;
850         ok = 0;
851         cancel = 0;
852 // *** CONTEXT_HELP ***
853         context_help_set_keyword("DVD and Bluray Creation");
854 }
855
856 CreateBD_GUI::~CreateBD_GUI()
857 {
858 }
859
860 void CreateBD_GUI::create_objects()
861 {
862         int xs1 = xS(1);
863         int xs10 = xS(10), xs35 = xS(35);
864         int xs60 = xS(60), xs160 = xS(160), xs170 = xS(170);
865         int ys5 = yS(5), ys10 = yS(10);
866         lock_window("CreateBD_GUI::create_objects");
867         int pady = BC_TextBox::calculate_h(this, MEDIUMFONT, 0, 1) + ys5;
868         int padx = BC_Title::calculate_w(this, (char*)"X", MEDIUMFONT);
869         int x = padx/2, y = pady/2;
870         BC_Title *title = new BC_Title(x, y, _("Title:"), MEDIUMFONT, YELLOW);
871         add_subwindow(title);
872         at_x = x + title->get_w();  at_y = y;
873         asset_title = new CreateBD_AssetTitle(this, at_x, at_y, get_w()-at_x-xs10);
874         add_subwindow(asset_title);
875         y += title->get_h() + pady/2;
876         title = new BC_Title(x, y, _("Work path:"), MEDIUMFONT, YELLOW);
877         add_subwindow(title);
878         tmp_x = x + title->get_w();  tmp_y = y;
879         tmp_path = new CreateBD_TmpPath(this, tmp_x, tmp_y,  get_w()-tmp_x-xs35);
880         add_subwindow(tmp_path);
881         btmp_path = new BrowseButton(thread->mwindow->theme, this, tmp_path,
882                 tmp_x+tmp_path->get_w(), tmp_y, "/tmp",
883                 _("Work path"), _("Select a Work directory:"), 1);
884         add_subwindow(btmp_path);
885         y += title->get_h() + pady/2;
886         disk_space = new CreateBD_DiskSpace(this, x, y);
887         add_subwindow(disk_space);
888         int x0 = get_w() - xs170;
889         title = new BC_Title(x0, y, _("Media:"), MEDIUMFONT, YELLOW);
890         add_subwindow(title);
891         int x1 =  x0+title->get_w()+padx;
892         media_size = new CreateBD_MediaSize(this, x1, y);
893         media_size->create_objects();
894         media_sizes.append(new BC_ListBoxItem("25GB"));
895         media_sizes.append(new BC_ListBoxItem("50GB"));
896         media_size->update_list(&media_sizes);
897         media_size->update(media_sizes[0]->get_text());
898         disk_space->update();
899         y += disk_space->get_h() + pady/2;
900
901         title = new BC_Title(x, y, _("Profile:"), MEDIUMFONT, YELLOW);
902         add_subwindow(title);
903         int start_x = x;
904         x += title->get_w()+padx;
905         profile = new CreateBD_Profile(this, x, y);
906         profile->create_objects();
907         profiles.append(new BC_ListBoxItem("bluray.m2ts"));
908         profiles.append(new BC_ListBoxItem("bluray_lpcm.m2ts"));
909 /*      profiles.append(new BC_ListBoxItem("bluray_truehd.m2ts")); */
910         profile->update_list(&profiles);
911         profile->update(profiles[0]->get_text());
912
913         x += profile->get_w()+padx;
914         need_tsmuxer = new CreateBD_UseTsmuxer(this, x, y);
915         add_subwindow(need_tsmuxer);
916         y += need_tsmuxer->get_h() + pady;
917
918         title = new BC_Title(start_x, y, _("Format:"), MEDIUMFONT, YELLOW);
919         add_subwindow(title);
920         standard = new CreateBD_Format(this, title->get_w() + padx, y);
921         add_subwindow(standard);
922         standard->create_objects();
923         standard->set_text(bd_formats[thread->use_standard].name);
924         x0 -= xS(60);
925         title = new BC_Title(x0, y, _("Scale:"), MEDIUMFONT, YELLOW);
926         add_subwindow(title);
927         x1 = x0+title->get_w()+padx;
928         scale = new CreateBD_Scale(this, x1, y);
929         add_subwindow(scale);
930         scale->create_objects();
931         y += standard->get_h() + pady/2;
932         x1 = x;  int y1 = y;
933         need_deinterlace = new CreateBD_Deinterlace(this, start_x, y);
934         add_subwindow(need_deinterlace);
935         y += need_deinterlace->get_h() + pady/2;
936         need_histogram = new CreateBD_Histogram(this, start_x, y);
937         add_subwindow(need_histogram);
938         y += need_histogram->get_h() + pady/2;
939         non_standard = new BC_Title(x1, y+ys5, "", MEDIUMFONT, RED);
940         add_subwindow(non_standard);
941         x1 -= xs60;  y = y1;
942         need_inverse_telecine = new CreateBD_InverseTelecine(this, x1, y);
943         add_subwindow(need_inverse_telecine);
944         y += need_inverse_telecine->get_h() + pady/2;
945         need_wide_audio = new CreateBD_WideAudio(this, x1, y);
946         add_subwindow(need_wide_audio);
947         y += need_wide_audio->get_h() + pady/2;
948         need_resize_tracks = new CreateBD_ResizeTracks(this, x1, y);
949         add_subwindow(need_resize_tracks);
950         x1 += xs170;  y = y1;
951         need_labeled = new CreateBD_LabelChapters(this, x1, y);
952         add_subwindow(need_labeled);
953         y += need_labeled->get_h() + pady/2;
954         need_farmed = new CreateBD_UseRenderFarm(this, x1, y);
955         add_subwindow(need_farmed);
956         ok_w = BC_OKButton::calculate_w();
957         ok_h = BC_OKButton::calculate_h();
958         ok_x = xs1;
959         ok_y = get_h() - ok_h - xs10;
960         ok = new CreateBD_OK(this, ok_x, ok_y);
961         add_subwindow(ok);
962         cancel_w = BC_CancelButton::calculate_w();
963         cancel_h = BC_CancelButton::calculate_h();
964         cancel_x = get_w() - cancel_w - xs10,
965         cancel_y = get_h() - cancel_h - ys10;
966         cancel = new CreateBD_Cancel(this, cancel_x, cancel_y);
967         add_subwindow(cancel);
968         show_window();
969         unlock_window();
970 }
971
972 int CreateBD_GUI::resize_event(int w, int h)
973 {
974         int xs10 = xS(10), xs35 = xS(35);
975         int ys10 = yS(10);
976         asset_title->reposition_window(at_x, at_y, get_w()-at_x-xs10);
977         tmp_path->reposition_window(tmp_x, tmp_y,  get_w()-tmp_x-xs35);
978         btmp_path->reposition_window(tmp_x+tmp_path->get_w(), tmp_y);
979         ok_y = h - ok_h - ys10;
980         ok->reposition_window(ok_x, ok_y);
981         cancel_x = w - cancel_w - xs10,
982         cancel_y = h - cancel_h - ys10;
983         cancel->reposition_window(cancel_x, cancel_y);
984         return 0;
985 }
986
987 int CreateBD_GUI::translation_event()
988 {
989         return 1;
990 }
991
992 int CreateBD_GUI::close_event()
993 {
994         set_done(1);
995         return 1;
996 }
997
998 void CreateBD_GUI::update()
999 {
1000         scale->set_value(thread->use_scale);
1001         need_deinterlace->set_value(thread->use_deinterlace);
1002         need_inverse_telecine->set_value(thread->use_inverse_telecine);
1003         need_resize_tracks->set_value(thread->use_resize_tracks);
1004         need_histogram->set_value(thread->use_histogram);
1005         need_wide_audio->set_value(thread->use_wide_audio);
1006         need_labeled->set_value(thread->use_labeled);
1007         need_farmed->set_value(thread->use_farmed);
1008         need_tsmuxer->set_value(thread->use_tsmuxer);
1009 }
1010
1011 int CreateBD_Thread::
1012 insert_video_plugin(const char *title, KeyFrame *default_keyframe)
1013 {
1014         Tracks *tracks = mwindow->edl->tracks;
1015         for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
1016                 if( vtrk->data_type != TRACK_VIDEO ) continue;
1017                 if( !vtrk->is_armed() ) continue;
1018                 vtrk->expand_view = 1;
1019                 PluginSet *plugin_set = new PluginSet(mwindow->edl, vtrk);
1020                 vtrk->plugin_set.append(plugin_set);
1021                 Edits *edits = vtrk->edits;
1022                 for( Edit *edit=edits->first; edit; edit=edit->next ) {
1023                         plugin_set->insert_plugin(_(title),
1024                                 edit->startproject, edit->length,
1025                                 PLUGIN_STANDALONE, 0, default_keyframe, 0);
1026                 }
1027                 vtrk->optimize();
1028         }
1029         return 0;
1030 }
1031
1032 int CreateBD_Thread::
1033 resize_tracks()
1034 {
1035         Tracks *tracks = mwindow->edl->tracks;
1036         int trk_w = max_w, trk_h = max_h;
1037         if( trk_w < bd_width ) trk_w = bd_width;
1038         if( trk_h < bd_height ) trk_h = bd_height;
1039         for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
1040                 if( vtrk->data_type != TRACK_VIDEO ) continue;
1041                 if( !vtrk->is_armed() ) continue;
1042                 vtrk->track_w = trk_w;
1043                 vtrk->track_h = trk_h;
1044         }
1045         return 0;
1046 }
1047
1048 int CreateBD_Thread::
1049 option_presets()
1050 {
1051 // reset only probed options
1052         use_deinterlace = 0;
1053         use_scale = Rescale::none;
1054         use_resize_tracks = 0;
1055         use_wide_audio = 0;
1056         use_labeled = 0;
1057         use_farmed = 0;
1058         use_tsmuxer = 0;
1059
1060         if( !mwindow->edl ) return 1;
1061
1062         bd_width = bd_formats[use_standard].w;
1063         bd_height = bd_formats[use_standard].h;
1064         bd_framerate = bd_formats[use_standard].framerate;
1065         int wide = bd_formats[use_standard].wide;
1066         bd_aspect_width  = wide < 0 ? 1. :
1067                 wide > 0 ? BD_WIDE_ASPECT_WIDTH  : BD_ASPECT_WIDTH;
1068         bd_aspect_height = wide < 0 ? 1. :
1069                 wide > 0 ? BD_WIDE_ASPECT_HEIGHT : BD_ASPECT_HEIGHT;
1070         bd_interlace_mode = bd_formats[use_standard].interlaced;
1071         double bd_aspect = bd_aspect_width / bd_aspect_height;
1072
1073         Tracks *tracks = mwindow->edl->tracks;
1074         max_w = 0;  max_h = 0;
1075         int has_deinterlace = 0, has_scale = 0;
1076         for( Track *trk=tracks->first; trk; trk=trk->next ) {
1077                 if( !trk->is_armed() ) continue;
1078                 Edits *edits = trk->edits;
1079                 switch( trk->data_type ) {
1080                 case TRACK_VIDEO:
1081                         for( Edit *edit=edits->first; edit; edit=edit->next ) {
1082                                 if( edit->silence() ) continue;
1083                                 Indexable *indexable = edit->get_source();
1084                                 int w = indexable->get_w();
1085                                 if( w > max_w ) max_w = w;
1086                                 if( w != bd_width ) use_scale = Rescale::scaled;
1087                                 int h = indexable->get_h();
1088                                 if( h > max_h ) max_h = h;
1089                                 if( h != bd_height ) use_scale = Rescale::scaled;
1090                                 float aw, ah;
1091                                 MWindow::create_aspect_ratio(aw, ah, w, h);
1092                                 double aspect = ah > 0 ? aw / ah : 1;
1093                                 if( wide >= 0 && !EQUIV(aspect, bd_aspect) )
1094                                         use_scale = Rescale::scaled;
1095                         }
1096                         for( int i=0; i<trk->plugin_set.size(); ++i ) {
1097                                 for( Plugin *plugin = (Plugin*)trk->plugin_set[i]->first;
1098                                                 plugin; plugin=(Plugin*)plugin->next ) {
1099                                         if( !strcmp(plugin->title, "Deinterlace") )
1100                                                 has_deinterlace = 1;
1101                                         if( !strcmp(plugin->title, "Auto Scale") ||
1102                                             !strcmp(plugin->title, "Scale Ratio") ||
1103                                             !strcmp(plugin->title, "Scale") )
1104                                                 has_scale = 1;
1105                                 }
1106                         }
1107                         break;
1108                 }
1109         }
1110         if( has_scale )
1111                 use_scale = Rescale::none;
1112         if( use_scale != Rescale::none ) {
1113                 if( max_w != bd_width ) use_resize_tracks = 1;
1114                 if( max_h != bd_height ) use_resize_tracks = 1;
1115         }
1116         for( Track *trk=tracks->first; trk && !use_resize_tracks; trk=trk->next ) {
1117                 if( !trk->is_armed() ) continue;
1118                 switch( trk->data_type ) {
1119                 case TRACK_VIDEO:
1120                         if( trk->track_w != max_w ) use_resize_tracks = 1;
1121                         if( trk->track_h != max_h ) use_resize_tracks = 1;
1122                         break;
1123                 }
1124         }
1125         if( !has_deinterlace && max_h > 2*bd_height ) use_deinterlace = 1;
1126
1127         if( tracks->recordable_audio_tracks() == BD_WIDE_CHANNELS )
1128                 use_wide_audio = 1;
1129
1130         return 0;
1131 }
1132
1133
1134 CreateBD_FormatItem::CreateBD_FormatItem(CreateBD_Format *popup,
1135                 int standard, const char *name)
1136  : BC_MenuItem(name)
1137 {
1138         this->popup = popup;
1139         this->standard = standard;
1140 }
1141
1142 CreateBD_FormatItem::~CreateBD_FormatItem()
1143 {
1144 }
1145
1146 int CreateBD_FormatItem::handle_event()
1147 {
1148         const char *text = get_text();
1149         int standard = this->standard;
1150         if( standard < 0 ) {
1151                 BC_SubMenu *submenu = get_submenu();
1152                 CreateBD_FormatItem *sub_item0 =
1153                         (CreateBD_FormatItem *)submenu->get_item(0);
1154                 standard = sub_item0->standard;
1155                 text = sub_item0->get_text();
1156         }
1157         popup->gui->thread->use_standard = standard;
1158         popup->set_text(text);
1159         int n = strlen(text)-1;
1160         int not_standard = n >= 0 && text[n] == '*' ? 1 : 0;
1161         popup->gui->non_standard->update(not_standard ? _("                * non-standard format") : "", 0);
1162         return popup->handle_event();
1163 }
1164
1165
1166 CreateBD_Format::CreateBD_Format(CreateBD_GUI *gui, int x, int y)
1167  : BC_PopupMenu(x, y, xS(200), bd_formats[gui->thread->use_standard].name, 1)
1168 {
1169         this->gui = gui;
1170 }
1171
1172 CreateBD_Format::~CreateBD_Format()
1173 {
1174 }
1175
1176 void CreateBD_Format::create_objects()
1177 {
1178         BC_SubMenu *submenu = 0;
1179         CreateBD_FormatItem *item = 0;
1180         int ww = 0, hh = 0;
1181         for( int i=0; i<(int)(sizeof(bd_formats)/sizeof(bd_formats[0])); ++i ) {
1182                 if( ww != bd_formats[i].w || hh != bd_formats[i].h ) {
1183                         ww = bd_formats[i].w;  hh = bd_formats[i].h;
1184                         char string[BCSTRLEN];
1185                         sprintf(string, "%dx%d", ww,hh);
1186                         add_item(item = new CreateBD_FormatItem(this, -1, string));
1187                         item->add_submenu(submenu = new BC_SubMenu());
1188                 }
1189                 submenu->add_submenuitem(new CreateBD_FormatItem(this, i, bd_formats[i].name));
1190         }
1191 }
1192
1193 int CreateBD_Format::handle_event()
1194 {
1195         gui->thread->option_presets();
1196         gui->update();
1197         return 1;
1198 }
1199
1200
1201 CreateBD_ScaleItem::CreateBD_ScaleItem(CreateBD_Scale *popup,
1202                 int scale, const char *text)
1203  : BC_MenuItem(text)
1204 {
1205         this->popup = popup;
1206         this->scale = scale;
1207 }
1208
1209 CreateBD_ScaleItem::~CreateBD_ScaleItem()
1210 {
1211 }
1212
1213 int CreateBD_ScaleItem::handle_event()
1214 {
1215         popup->gui->thread->use_scale = scale;
1216         popup->set_value(scale);
1217         return popup->handle_event();
1218 }
1219
1220
1221 CreateBD_Scale::CreateBD_Scale(CreateBD_GUI *gui, int x, int y)
1222  : BC_PopupMenu(x, y, xS(140), "", 1)
1223 {
1224         this->gui = gui;
1225 }
1226
1227 CreateBD_Scale::~CreateBD_Scale()
1228 {
1229 }
1230
1231 void CreateBD_Scale::create_objects()
1232 {
1233
1234         for( int i=0; i<(int)Rescale::n_scale_types; ++i ) {
1235                 add_item(new CreateBD_ScaleItem(this, i, Rescale::scale_types[i]));
1236         }
1237         set_value(gui->thread->use_scale);
1238 }
1239
1240 int CreateBD_Scale::handle_event()
1241 {
1242         gui->update();
1243         return 1;
1244 }
1245
1246
1247 CreateBD_MediaSize::CreateBD_MediaSize(CreateBD_GUI *gui, int x, int y)
1248  : BC_PopupTextBox(gui, 0, 0, x, y, xS(70),yS(50))
1249 {
1250         this->gui = gui;
1251 }
1252
1253 CreateBD_MediaSize::~CreateBD_MediaSize()
1254 {
1255 }
1256
1257 int CreateBD_MediaSize::handle_event()
1258 {
1259         gui->disk_space->update();
1260         return 1;
1261 }
1262
1263 CreateBD_Profile::CreateBD_Profile(CreateBD_GUI *gui, int x, int y)
1264  : BC_PopupTextBox(gui, 0, 0, x, y, xS(170),yS(50))
1265 {
1266         this->gui = gui;
1267         strcpy(gui->thread->use_profile, "bluray.m2ts");
1268 }
1269
1270 CreateBD_Profile::~CreateBD_Profile()
1271 {
1272 }
1273
1274 int CreateBD_Profile::handle_event()
1275 {
1276         strcpy(gui->thread->use_profile, get_text());
1277         return 1;
1278 }