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