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