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