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