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