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