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