f4f907d3f75276af0c2fac2e7e30a1e85233a8b6
[goodguy/history.git] / cinelerra-5.0 / cinelerra / bdcreate.C
1 #include "asset.h"
2 #include "bdcreate.h"
3 #include "edl.h"
4 #include "edit.h"
5 #include "edits.h"
6 #include "edlsession.h"
7 #include "file.inc"
8 #include "filexml.h"
9 #include "format.inc"
10 #include "keyframe.h"
11 #include "labels.h"
12 #include "mainerror.h"
13 #include "mainundo.h"
14 #include "mwindow.h"
15 #include "mwindowgui.h"
16 #include "plugin.h"
17 #include "pluginset.h"
18 #include "track.h"
19 #include "tracks.h"
20
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <sys/stat.h>
25 #include <sys/statfs.h>
26
27 // BD Creation
28
29 const int64_t CreateBD_Thread::BD_SIZE = 25000000000;
30 const int CreateBD_Thread::BD_STREAMS = 1;
31 const int CreateBD_Thread::BD_WIDTH = 1920;
32 const int CreateBD_Thread::BD_HEIGHT = 1080;
33 const double CreateBD_Thread::BD_ASPECT_WIDTH = 4.;
34 const double CreateBD_Thread::BD_ASPECT_HEIGHT = 3.;
35 const double CreateBD_Thread::BD_WIDE_ASPECT_WIDTH = 16.;
36 const double CreateBD_Thread::BD_WIDE_ASPECT_HEIGHT = 9.;
37 const double CreateBD_Thread::BD_FRAMERATE = 24000. / 1001.;
38 //const int CreateBD_Thread::BD_MAX_BITRATE = 40000000;
39 const int CreateBD_Thread::BD_MAX_BITRATE = 8000000;
40 const int CreateBD_Thread::BD_CHANNELS = 2;
41 const int CreateBD_Thread::BD_WIDE_CHANNELS = 6;
42 const double CreateBD_Thread::BD_SAMPLERATE = 48000;
43 const double CreateBD_Thread::BD_KAUDIO_RATE = 224;
44
45
46 CreateBD_MenuItem::CreateBD_MenuItem(MWindow *mwindow)
47  : BC_MenuItem(_("BD Render..."), _("Ctrl-d"), 'd')
48 {
49         set_ctrl(1); 
50         this->mwindow = mwindow;
51 }
52
53 int CreateBD_MenuItem::handle_event()
54 {
55         mwindow->create_bd->start();
56         return 1;
57 }
58
59
60 CreateBD_Thread::CreateBD_Thread(MWindow *mwindow)
61  : BC_DialogThread()
62 {
63         this->mwindow = mwindow;
64         this->gui = 0;
65         this->use_deinterlace = 0;
66         this->use_inverse_telecine = 0;
67         this->use_scale = 0;
68         this->use_resize_tracks = 0;
69         this->use_histogram = 0;
70         this->use_wide_audio = 0;
71         this->use_wide_aspect = 0;
72         this->use_label_chapters = 0;
73 }
74
75 CreateBD_Thread::~CreateBD_Thread()
76 {
77         close_window();
78 }
79
80 int CreateBD_Thread::create_bd_jobs(ArrayList<BatchRenderJob*> *jobs,
81         const char *tmp_path, const char *asset_title)
82 {
83         EDL *edl = mwindow->edl;
84         if( !edl || !edl->session ) {
85                 char msg[BCTEXTLEN];
86                 sprintf(msg, _("No EDL/Session"));
87                 MainError::show_error(msg);
88                 return 1;
89         }
90         EDLSession *session = edl->session;
91
92         double total_length = edl->tracks->total_length();
93         if( total_length <= 0 ) {
94                 char msg[BCTEXTLEN];
95                 sprintf(msg, _("No content: %s"), asset_title);
96                 MainError::show_error(msg);
97                 return 1;
98         }
99
100         char asset_dir[BCTEXTLEN];
101         sprintf(asset_dir, "%s/%s", tmp_path, asset_title);
102
103         if( mkdir(asset_dir, 0777) ) {
104                 char err[BCTEXTLEN], msg[BCTEXTLEN];
105                 strerror_r(errno, err, sizeof(err));
106                 sprintf(msg, _("Unable to create directory: %s\n-- %s"), asset_dir, err);
107                 MainError::show_error(msg);
108                 return 1;
109         }
110
111         double old_samplerate = session->sample_rate;
112         double old_framerate = session->frame_rate;
113
114         session->video_channels = BD_STREAMS;
115         session->video_tracks = BD_STREAMS;
116 // use session framerate
117 //      session->frame_rate = BD_FRAMERATE;
118         session->output_w = BD_WIDTH;
119         session->output_h = BD_HEIGHT;
120         session->aspect_w = use_wide_aspect ? BD_WIDE_ASPECT_WIDTH : BD_ASPECT_WIDTH;
121         session->aspect_h = use_wide_aspect ? BD_WIDE_ASPECT_HEIGHT : BD_ASPECT_HEIGHT;
122         session->sample_rate = BD_SAMPLERATE;
123         session->audio_channels = session->audio_tracks =
124                 use_wide_audio ? BD_WIDE_CHANNELS : BD_CHANNELS;
125
126         char script_filename[BCTEXTLEN];
127         sprintf(script_filename, "%s/bd.sh", asset_dir);
128         int fd = open(script_filename, O_WRONLY+O_CREAT+O_TRUNC, 0755);
129         FILE *fp = fdopen(fd, "w");
130         if( !fp ) {
131                 char err[BCTEXTLEN], msg[BCTEXTLEN];
132                 strerror_r(errno, err, sizeof(err));
133                 sprintf(msg, _("Unable to save: %s\n-- %s"), script_filename, err);
134                 MainError::show_error(msg);
135                 return 1;
136         }
137         char exe_path[BCTEXTLEN];
138         get_exe_path(exe_path);
139         fprintf(fp,"#!/bin/bash -ex\n");
140         fprintf(fp,"PATH=$PATH:%s\n",exe_path);
141         fprintf(fp,"mkdir -p $1/udfs\n");
142         fprintf(fp,"sz=`du -sb $1/bd.m2ts | sed -e 's/[ \t].*//'`\n");
143         fprintf(fp,"blks=$((sz/2048 + 4096))\n");
144         fprintf(fp,"mkudffs $1/bd.udfs $blks\n");
145         fprintf(fp,"mount -o loop $1/bd.udfs $1/udfs\n");
146         fprintf(fp,"bdwrite $1/udfs $1/bd.m2ts\n");
147         fprintf(fp,"umount $1/udfs\n");
148         fprintf(fp,"echo To burn bluray, load writable media and run:\n");
149         fprintf(fp,"echo growisofs -dvd-compat -Z /dev/bd=$1/bd.udfs\n");
150         fprintf(fp,"\n");
151         fclose(fp);
152
153         if( use_wide_audio ) {
154                 session->audio_channels = session->audio_tracks = BD_WIDE_CHANNELS;
155                 session->achannel_positions[0] = 90;
156                 session->achannel_positions[1] = 150;
157                 session->achannel_positions[2] = 30;
158                 session->achannel_positions[3] = 210;
159                 session->achannel_positions[4] = 330;
160                 session->achannel_positions[5] = 270;
161                 if( edl->tracks->recordable_audio_tracks() == BD_WIDE_CHANNELS )
162                         mwindow->remap_audio(MWindow::AUDIO_1_TO_1);
163         }
164         else {
165                 session->audio_channels = session->audio_tracks = BD_CHANNELS;
166                 session->achannel_positions[0] = 180;
167                 session->achannel_positions[1] = 0;
168                 if( edl->tracks->recordable_audio_tracks() == BD_WIDE_CHANNELS )
169                         mwindow->remap_audio(MWindow::AUDIO_5_1_TO_2);
170         }
171
172         double new_samplerate = session->sample_rate;
173         double new_framerate = session->frame_rate;
174         edl->rechannel();
175         edl->resample(old_samplerate, new_samplerate, TRACK_AUDIO);
176         edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
177
178         int64_t aud_size = ((BD_KAUDIO_RATE * total_length)/8 + 1000-1) * 1000;
179         int64_t vid_size = BD_SIZE*0.96 - aud_size;
180         int64_t vid_bitrate = (vid_size * 8) / total_length;
181         vid_bitrate /= 1000;  vid_bitrate *= 1000;
182         if( vid_bitrate > BD_MAX_BITRATE ) vid_bitrate = BD_MAX_BITRATE;
183
184         char xml_filename[BCTEXTLEN];
185         sprintf(xml_filename, "%s/bd.xml", asset_dir);
186         FileXML xml_file;
187         edl->save_xml(&xml_file, xml_filename, 0, 0);
188         xml_file.terminate_string();
189         if( xml_file.write_to_file(xml_filename) ) {
190                 char msg[BCTEXTLEN];
191                 sprintf(msg, _("Unable to save: %s"), xml_filename);
192                 MainError::show_error(msg);
193                 return 1;
194         }
195
196         BatchRenderJob *job = new BatchRenderJob(mwindow->preferences);
197         jobs->append(job);
198         strcpy(&job->edl_path[0], xml_filename);
199         Asset *asset = job->asset;
200
201         asset->layers = BD_STREAMS;
202         asset->frame_rate = session->frame_rate;
203         asset->width = session->output_w;
204         asset->height = session->output_h;
205         asset->aspect_ratio = session->aspect_w / session->aspect_h;
206
207         char option_path[BCTEXTLEN];
208         sprintf(&asset->path[0],"%s/bd.m2ts", asset_dir);
209         asset->format = FILE_FFMPEG;
210         strcpy(asset->fformat, "m2ts");
211
212         asset->audio_data = 1;
213         strcpy(asset->acodec, "bluray.m2ts");
214         FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
215         FFMPEG::load_options(option_path, asset->ff_audio_options,
216                          sizeof(asset->ff_audio_options));
217         asset->ff_audio_bitrate = BD_KAUDIO_RATE * 1000;
218
219         asset->video_data = 1;
220         strcpy(asset->vcodec, "bluray.m2ts");
221         FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
222         FFMPEG::load_options(option_path, asset->ff_video_options,
223                  sizeof(asset->ff_video_options));
224         asset->ff_video_bitrate = vid_bitrate;
225         asset->ff_video_quality = 0;
226
227         int len = strlen(asset->ff_video_options);
228         char *cp = asset->ff_video_options + len;
229         snprintf(cp, sizeof(asset->ff_video_options)-len-1,
230                 "aspect %.5f\n", asset->aspect_ratio);
231
232         job = new BatchRenderJob(mwindow->preferences);
233         jobs->append(job);
234         job->edl_path[0] = '@';
235         strcpy(&job->edl_path[1], script_filename);
236         strcpy(&job->asset->path[0], asset_dir);
237
238         return 0;
239 }
240
241 void CreateBD_Thread::handle_close_event(int result)
242 {
243         if( result ) return;
244         mwindow->batch_render->load_defaults(mwindow->defaults);
245         mwindow->undo->update_undo_before();
246         KeyFrame keyframe;  char data[BCTEXTLEN];
247         if( use_deinterlace ) {
248                 sprintf(data,"<DEINTERLACE MODE=1>");
249                 keyframe.set_data(data);
250                 insert_video_plugin("Deinterlace", &keyframe);
251         }
252         if( use_inverse_telecine ) {
253                 sprintf(data,"<IVTC FRAME_OFFSET=0 FIRST_FIELD=0 "
254                         "AUTOMATIC=1 AUTO_THRESHOLD=2.0e+00 PATTERN=2>");
255                 keyframe.set_data(data);
256                 insert_video_plugin("Inverse Telecine", &keyframe);
257         }
258         if( use_scale ) {
259                 sprintf(data,"<PHOTOSCALE WIDTH=%d HEIGHT=%d USE_FILE=1>", BD_WIDTH, BD_HEIGHT);
260                 keyframe.set_data(data);
261                 insert_video_plugin("Auto Scale", &keyframe);
262         }
263         if( use_resize_tracks )
264                 resize_tracks();
265         if( use_histogram ) {
266 #if 0
267                 sprintf(data, "<HISTOGRAM OUTPUT_MIN_0=0 OUTPUT_MAX_0=1 "
268                         "OUTPUT_MIN_1=0 OUTPUT_MAX_1=1 "
269                         "OUTPUT_MIN_2=0 OUTPUT_MAX_2=1 "
270                         "OUTPUT_MIN_3=0 OUTPUT_MAX_3=1 "
271                         "AUTOMATIC=0 THRESHOLD=9.0-01 PLOT=0 SPLIT=0>"
272                         "<POINTS></POINTS><POINTS></POINTS><POINTS></POINTS>"
273                         "<POINTS><POINT X=6.0e-02 Y=0>"
274                                 "<POINT X=9.4e-01 Y=1></POINTS>");
275 #else
276                 sprintf(data, "<HISTOGRAM AUTOMATIC=0 THRESHOLD=1.0e-01 "
277                         "PLOT=0 SPLIT=0 W=440 H=500 PARADE=0 MODE=3 "
278                         "LOW_OUTPUT_0=0 HIGH_OUTPUT_0=1 LOW_INPUT_0=0 HIGH_INPUT_0=1 GAMMA_0=1 "
279                         "LOW_OUTPUT_1=0 HIGH_OUTPUT_1=1 LOW_INPUT_1=0 HIGH_INPUT_1=1 GAMMA_1=1 "
280                         "LOW_OUTPUT_2=0 HIGH_OUTPUT_2=1 LOW_INPUT_2=0 HIGH_INPUT_2=1 GAMMA_2=1 "
281                         "LOW_OUTPUT_3=0 HIGH_OUTPUT_3=1 LOW_INPUT_3=0.044 HIGH_INPUT_3=0.956 "
282                         "GAMMA_3=1>");
283 #endif
284                 keyframe.set_data(data);
285                 insert_video_plugin("Histogram", &keyframe);
286         }
287         create_bd_jobs(&mwindow->batch_render->jobs, tmp_path, asset_title);
288         mwindow->save_backup();
289         mwindow->undo->update_undo_after(_("create bd"), LOAD_ALL);
290         mwindow->resync_guis();
291         mwindow->batch_render->handle_close_event(0);
292         mwindow->batch_render->start();
293 }
294
295 BC_Window* CreateBD_Thread::new_gui()
296 {
297         memset(tmp_path,0,sizeof(tmp_path));
298         strcpy(tmp_path,"/tmp");
299         memset(asset_title,0,sizeof(asset_title));
300         time_t dt;      time(&dt);
301         struct tm dtm;  localtime_r(&dt, &dtm);
302         sprintf(asset_title, "bd_%02d%02d%02d-%02d%02d%02d",
303                 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
304                 dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
305         use_deinterlace = 0;
306         use_inverse_telecine = 0;
307         use_scale = 0;
308         use_resize_tracks = 0;
309         use_histogram = 0;
310         use_wide_audio = 0;
311         use_wide_aspect = 0;
312         use_label_chapters = 0;
313         option_presets();
314         int scr_x = mwindow->gui->get_screen_x(0, -1);
315         int scr_w = mwindow->gui->get_screen_w(0, -1);
316         int scr_h = mwindow->gui->get_screen_h(0, -1);
317         int w = 500, h = 250;
318         int x = scr_x + scr_w/2 - w/2, y = scr_h/2 - h/2;
319
320         gui = new CreateBD_GUI(this, x, y, w, h);
321         gui->create_objects();
322         return gui;
323 }
324
325
326 CreateBD_OK::CreateBD_OK(CreateBD_GUI *gui, int x, int y)
327  : BC_OKButton(x, y)
328 {
329         this->gui = gui;
330         set_tooltip(_("end setup, start batch render"));
331 }
332
333 CreateBD_OK::~CreateBD_OK()
334 {
335 }
336
337 int CreateBD_OK::button_press_event()
338 {
339         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
340                 gui->set_done(0);
341                 return 1;
342         }
343         return 0;
344 }
345
346 int CreateBD_OK::keypress_event()
347 {
348         return 0;
349 }
350
351
352 CreateBD_Cancel::CreateBD_Cancel(CreateBD_GUI *gui, int x, int y)
353  : BC_CancelButton(x, y)
354 {
355         this->gui = gui;
356 }
357
358 CreateBD_Cancel::~CreateBD_Cancel()
359 {
360 }
361
362 int CreateBD_Cancel::button_press_event()
363 {
364         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
365                 gui->set_done(1);
366                 return 1;
367         }
368         return 0;
369 }
370
371
372 CreateBD_DiskSpace::CreateBD_DiskSpace(CreateBD_GUI *gui, int x, int y)
373  : BC_Title(x, y, "", MEDIUMFONT, GREEN)
374 {
375         this->gui = gui;
376 }
377
378 CreateBD_DiskSpace::~CreateBD_DiskSpace()
379 {
380 }
381
382 int64_t CreateBD_DiskSpace::tmp_path_space()
383 {
384         const char *path = gui->tmp_path->get_text();
385         if( access(path,R_OK+W_OK) ) return 0;
386         struct statfs sfs;
387         if( statfs(path, &sfs) ) return 0;
388         return (int64_t)sfs.f_bsize * sfs.f_bfree;
389 }
390
391 void CreateBD_DiskSpace::update()
392 {
393 //      gui->disk_space->set_color(get_bg_color());
394         int64_t disk_space = tmp_path_space();
395         int color = disk_space<gui->needed_disk_space ? RED : GREEN;
396         static const char *suffix[] = { "", "KB", "MB", "GB", "TB", "PB" };
397         int i = 0;
398         for( int64_t space=disk_space; i<5 && (space/=1000)>0; disk_space=space, ++i );
399         char text[BCTEXTLEN];
400         sprintf(text, "%s" _LDv(3) "%s", _("disk space: "), disk_space, suffix[i]);
401         gui->disk_space->BC_Title::update(text);
402         gui->disk_space->set_color(color);
403 }
404
405 CreateBD_TmpPath::CreateBD_TmpPath(CreateBD_GUI *gui, int x, int y, int w)
406  : BC_TextBox(x, y, w, 1, -(int)sizeof(gui->thread->tmp_path),
407                 gui->thread->tmp_path, 1, MEDIUMFONT)
408 {
409         this->gui = gui;
410 }
411
412 CreateBD_TmpPath::~CreateBD_TmpPath()
413 {
414 }
415
416 int CreateBD_TmpPath::handle_event()
417 {
418         gui->disk_space->update();
419         return 1;
420 }
421
422
423 CreateBD_AssetTitle::CreateBD_AssetTitle(CreateBD_GUI *gui, int x, int y, int w)
424  : BC_TextBox(x, y, w, 1, 0, gui->thread->asset_title, 1, MEDIUMFONT)
425 {
426         this->gui = gui;
427 }
428
429 CreateBD_AssetTitle::~CreateBD_AssetTitle()
430 {
431 }
432
433
434 CreateBD_Deinterlace::CreateBD_Deinterlace(CreateBD_GUI *gui, int x, int y)
435  : BC_CheckBox(x, y, &gui->thread->use_deinterlace, _("Deinterlace"))
436 {
437         this->gui = gui;
438 }
439
440 CreateBD_Deinterlace::~CreateBD_Deinterlace()
441 {
442 }
443
444 int CreateBD_Deinterlace::handle_event()
445 {
446         if( get_value() ) {
447                 gui->need_inverse_telecine->set_value(0);
448                 gui->thread->use_inverse_telecine = 0;
449         }
450         return BC_CheckBox::handle_event();
451 }
452
453
454 CreateBD_InverseTelecine::CreateBD_InverseTelecine(CreateBD_GUI *gui, int x, int y)
455  : BC_CheckBox(x, y, &gui->thread->use_inverse_telecine, _("Inverse Telecine"))
456 {
457         this->gui = gui;
458 }
459
460 CreateBD_InverseTelecine::~CreateBD_InverseTelecine()
461 {
462 }
463
464 int CreateBD_InverseTelecine::handle_event()
465 {
466         if( get_value() ) {
467                 gui->need_deinterlace->set_value(0);
468                 gui->thread->use_deinterlace = 0;
469         }
470         return BC_CheckBox::handle_event();
471 }
472
473
474 CreateBD_Scale::CreateBD_Scale(CreateBD_GUI *gui, int x, int y)
475  : BC_CheckBox(x, y, &gui->thread->use_scale, _("Scale"))
476 {
477         this->gui = gui;
478 }
479
480 CreateBD_Scale::~CreateBD_Scale()
481 {
482 }
483
484
485 CreateBD_ResizeTracks::CreateBD_ResizeTracks(CreateBD_GUI *gui, int x, int y)
486  : BC_CheckBox(x, y, &gui->thread->use_resize_tracks, _("Resize Tracks"))
487 {
488         this->gui = gui;
489 }
490
491 CreateBD_ResizeTracks::~CreateBD_ResizeTracks()
492 {
493 }
494
495
496 CreateBD_Histogram::CreateBD_Histogram(CreateBD_GUI *gui, int x, int y)
497  : BC_CheckBox(x, y, &gui->thread->use_histogram, _("Histogram"))
498 {
499         this->gui = gui;
500 }
501
502 CreateBD_Histogram::~CreateBD_Histogram()
503 {
504 }
505
506 CreateBD_LabelChapters::CreateBD_LabelChapters(CreateBD_GUI *gui, int x, int y)
507  : BC_CheckBox(x, y, &gui->thread->use_label_chapters, _("Chapters at Labels"))
508 {
509         this->gui = gui;
510 }
511
512 CreateBD_LabelChapters::~CreateBD_LabelChapters()
513 {
514 }
515
516 CreateBD_WideAudio::CreateBD_WideAudio(CreateBD_GUI *gui, int x, int y)
517  : BC_CheckBox(x, y, &gui->thread->use_wide_audio, _("Audio 5.1"))
518 {
519         this->gui = gui;
520 }
521
522 CreateBD_WideAudio::~CreateBD_WideAudio()
523 {
524 }
525
526 CreateBD_WideAspect::CreateBD_WideAspect(CreateBD_GUI *gui, int x, int y)
527  : BC_CheckBox(x, y, &gui->thread->use_wide_aspect, _("Aspect 16x9"))
528 {
529         this->gui = gui;
530 }
531
532 CreateBD_WideAspect::~CreateBD_WideAspect()
533 {
534 }
535
536
537
538 CreateBD_GUI::CreateBD_GUI(CreateBD_Thread *thread, int x, int y, int w, int h)
539  : BC_Window(_(PROGRAM_NAME ": Create BD"), x, y, w, h, 50, 50, 1, 0, 1)
540 {
541         this->thread = thread;
542         at_x = at_y = tmp_x = tmp_y = 0;
543         ok_x = ok_y = ok_w = ok_h = 0;
544         cancel_x = cancel_y = cancel_w = cancel_h = 0;
545         asset_title = 0;
546         tmp_path = 0;
547         disk_space = 0;
548         needed_disk_space = 100e9;
549         need_deinterlace = 0;
550         need_inverse_telecine = 0;
551         need_scale = 0;
552         need_resize_tracks = 0;
553         need_histogram = 0;
554         need_wide_audio = 0;
555         need_wide_aspect = 0;
556         need_label_chapters = 0;
557         ok = 0;
558         cancel = 0;
559 }
560
561 CreateBD_GUI::~CreateBD_GUI()
562 {
563 }
564
565 void CreateBD_GUI::create_objects()
566 {
567         lock_window("CreateBD_GUI::create_objects");
568         int pady = BC_TextBox::calculate_h(this, MEDIUMFONT, 0, 1) + 5;
569         int padx = BC_Title::calculate_w(this, (char*)"X", MEDIUMFONT);
570         int x = padx/2, y = pady/2;
571         BC_Title *title = new BC_Title(x, y, _("Title:"), MEDIUMFONT, YELLOW);
572         add_subwindow(title);
573         at_x = x + title->get_w();  at_y = y;
574         asset_title = new CreateBD_AssetTitle(this, at_x, at_y, get_w()-at_x-10);
575         add_subwindow(asset_title);
576         y += title->get_h() + pady/2;
577         title = new BC_Title(x, y, _("tmp path:"), MEDIUMFONT, YELLOW);
578         add_subwindow(title);
579         tmp_x = x + title->get_w();  tmp_y = y;
580         tmp_path = new CreateBD_TmpPath(this, tmp_x, tmp_y,  get_w()-tmp_x-10);
581         add_subwindow(tmp_path);
582         y += title->get_h() + pady/2;
583         disk_space = new CreateBD_DiskSpace(this, x, y);
584         add_subwindow(disk_space);
585         disk_space->update();
586         y += disk_space->get_h() + pady/2;
587         need_deinterlace = new CreateBD_Deinterlace(this, x, y);
588         add_subwindow(need_deinterlace);
589         int x1 = x + 150, x2 = x1 + 150;
590         need_inverse_telecine = new CreateBD_InverseTelecine(this, x1, y);
591         add_subwindow(need_inverse_telecine);
592         y += need_deinterlace->get_h() + pady/2;
593         need_scale = new CreateBD_Scale(this, x, y);
594         add_subwindow(need_scale);
595         need_wide_audio = new CreateBD_WideAudio(this, x1, y);
596         add_subwindow(need_wide_audio);
597         need_resize_tracks = new CreateBD_ResizeTracks(this, x2, y);
598         add_subwindow(need_resize_tracks);
599         y += need_scale->get_h() + pady/2;
600         need_histogram = new CreateBD_Histogram(this, x, y);
601         add_subwindow(need_histogram);
602         need_wide_aspect = new CreateBD_WideAspect(this, x1, y);
603         add_subwindow(need_wide_aspect);
604 //      need_label_chapters = new CreateBD_LabelChapters(this, x2, y);
605 //      add_subwindow(need_label_chapters);
606         ok_w = BC_OKButton::calculate_w();
607         ok_h = BC_OKButton::calculate_h();
608         ok_x = 10;
609         ok_y = get_h() - ok_h - 10;
610         ok = new CreateBD_OK(this, ok_x, ok_y);
611         add_subwindow(ok);
612         cancel_w = BC_CancelButton::calculate_w();
613         cancel_h = BC_CancelButton::calculate_h();
614         cancel_x = get_w() - cancel_w - 10,
615         cancel_y = get_h() - cancel_h - 10;
616         cancel = new CreateBD_Cancel(this, cancel_x, cancel_y);
617         add_subwindow(cancel);
618         show_window();
619         unlock_window();
620 }
621
622 int CreateBD_GUI::resize_event(int w, int h)
623 {
624         asset_title->reposition_window(at_x, at_y, get_w()-at_x-10);
625         tmp_path->reposition_window(tmp_x, tmp_y,  get_w()-tmp_x-10);
626         ok_y = h - ok_h - 10;
627         ok->reposition_window(ok_x, ok_y);
628         cancel_x = w - cancel_w - 10,
629         cancel_y = h - cancel_h - 10;
630         cancel->reposition_window(cancel_x, cancel_y);
631         return 0;
632 }
633
634 int CreateBD_GUI::translation_event()
635 {
636         return 1;
637 }
638
639 int CreateBD_GUI::close_event()
640 {
641         set_done(1);
642         return 1;
643 }
644
645 int CreateBD_Thread::
646 insert_video_plugin(const char *title, KeyFrame *default_keyframe)
647 {
648         Tracks *tracks = mwindow->edl->tracks;
649         for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
650                 if( vtrk->data_type != TRACK_VIDEO ) continue;
651                 if( !vtrk->record ) continue;
652                 vtrk->expand_view = 1;
653                 PluginSet *plugin_set = new PluginSet(mwindow->edl, vtrk);
654                 vtrk->plugin_set.append(plugin_set);
655                 Edits *edits = vtrk->edits;
656                 for( Edit *edit=edits->first; edit; edit=edit->next ) {
657                         plugin_set->insert_plugin(title,
658                                 edit->startproject, edit->length,
659                                 PLUGIN_STANDALONE, 0, default_keyframe, 0);
660                 }
661                 vtrk->optimize();
662         }
663         return 0;
664 }
665
666 int CreateBD_Thread::
667 resize_tracks()
668 {
669         Tracks *tracks = mwindow->edl->tracks;
670 #if 0
671         int max_w = 0, max_h = 0;
672         for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
673                 if( vtrk->data_type != TRACK_VIDEO ) continue;
674                 if( !vtrk->record ) continue;
675                 Edits *edits = vtrk->edits;
676                 for( Edit *edit=edits->first; edit; edit=edit->next ) {
677                         Indexable *indexable = edit->get_source();
678                         int w = indexable->get_w();
679                         if( w > max_w ) max_w = w;
680                         int h = indexable->get_h();
681                         if( h > max_h ) max_h = h;
682                 }
683         }
684 #endif
685         for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
686                 if( vtrk->data_type != TRACK_VIDEO ) continue;
687                 if( !vtrk->record ) continue;
688                 vtrk->track_w = BD_WIDTH; // max_w;
689                 vtrk->track_h = BD_HEIGHT; // max_h;
690         }
691         return 0;
692 }
693
694 int CreateBD_Thread::
695 option_presets()
696 {
697         if( !mwindow->edl ) return 1;
698         Tracks *tracks = mwindow->edl->tracks;
699         int max_w = 0, max_h = 0;
700         int has_deinterlace = 0, has_scale = 0;
701         for( Track *trk=tracks->first; trk; trk=trk->next ) {
702                 if( !trk->record ) continue;
703                 Edits *edits = trk->edits;
704                 switch( trk->data_type ) {
705                 case TRACK_VIDEO:
706                         for( Edit *edit=edits->first; edit; edit=edit->next ) {
707                                 Indexable *indexable = edit->get_source();
708                                 int w = indexable->get_w();
709                                 if( w > max_w ) max_w = w;
710                                 if( w != BD_WIDTH ) use_scale = 1;
711                                 int h = indexable->get_h();
712                                 if( h > max_h ) max_h = h;
713                                 if( h != BD_HEIGHT ) use_scale = 1;
714                         }
715                         for( int i=0; i<trk->plugin_set.size(); ++i ) {
716                                 for(Plugin *plugin = (Plugin*)trk->plugin_set[i]->first;
717                                                 plugin;
718                                                 plugin = (Plugin*)plugin->next) {
719                                         if( !strcmp(plugin->title, "Deinterlace") )
720                                                 has_deinterlace = 1;
721                                         if( !strcmp(plugin->title, "Auto Scale") ||
722                                             !strcmp(plugin->title, "Scale") )
723                                                 has_scale = 1;
724                                 }
725                         }
726                         break;
727                 }
728         }
729         if( has_scale )
730                 use_scale = 0;
731         if( use_scale ) {
732                 if( max_w != BD_WIDTH ) use_resize_tracks = 1;
733                 if( max_h != BD_HEIGHT ) use_resize_tracks = 1;
734         }
735         for( Track *trk=tracks->first; trk && !use_resize_tracks; trk=trk->next ) {
736                 if( !trk->record ) continue;
737                 switch( trk->data_type ) {
738                 case TRACK_VIDEO:
739                         if( trk->track_w != max_w ) use_resize_tracks = 1;
740                         if( trk->track_h != max_h ) use_resize_tracks = 1;
741                         break;
742                 }
743         }
744         if( !has_deinterlace && max_h > 2*BD_HEIGHT ) use_deinterlace = 1;
745         // Labels *labels = mwindow->edl->labels;
746         // use_label_chapters = labels && labels->first ? 1 : 0;
747         float w, h;
748         MWindow::create_aspect_ratio(w, h, max_w, max_h);
749         if( w == BD_WIDE_ASPECT_WIDTH && h == BD_WIDE_ASPECT_HEIGHT )
750                 use_wide_aspect = 1;
751         if( tracks->recordable_audio_tracks() == BD_WIDE_CHANNELS )
752                 use_wide_audio = 1;
753         return 0;
754 }
755