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