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