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