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