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