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