c752ba812a8feda6437f8d59bf916157637fbcc1
[goodguy/history.git] / cinelerra-5.1 / cinelerra / dvdcreate.C
1 #include "asset.h"
2 #include "clip.h"
3 #include "dvdcreate.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
29 #define DVD_PAL_4x3     0
30 #define DVD_PAL_16x9    1
31 #define DVD_NTSC_4x3    2
32 #define DVD_NTSC_16x9   3
33
34 #define DVD_NORM_PAL    0
35 #define DVD_NORM_NTSC   1
36
37 #define DVD_ASPECT_4x3  0
38 #define DVD_ASPECT_16x9 1
39
40 static struct dvd_norm {
41         const char *name;
42         int w, h;
43         double framerate;
44 } dvd_norms[] = {
45         { "PAL",  720,576, 25 },
46         { "NTSC", 720,480, 29.97 },
47 };
48
49 static struct dvd_aspect {
50         int w, h;
51 } dvd_aspects[] = {
52         { 4, 3, },
53         { 16, 9, },
54 };
55
56 // DVD Creation
57
58 static struct dvd_format {
59         int norm, aspect;
60 } dvd_formats[] = {
61         { DVD_NORM_PAL,  DVD_ASPECT_4x3, },
62         { DVD_NORM_PAL,  DVD_ASPECT_16x9, },
63         { DVD_NORM_NTSC, DVD_ASPECT_4x3, },
64         { DVD_NORM_NTSC, DVD_ASPECT_16x9, },
65 };
66
67 const int64_t CreateDVD_Thread::DVD_SIZE = 4700000000;
68 const int CreateDVD_Thread::DVD_STREAMS = 1;
69 const int CreateDVD_Thread::DVD_WIDTH = 720;
70 const int CreateDVD_Thread::DVD_HEIGHT = 480;
71 const double CreateDVD_Thread::DVD_ASPECT_WIDTH = 4.;
72 const double CreateDVD_Thread::DVD_ASPECT_HEIGHT = 3.;
73 const double CreateDVD_Thread::DVD_WIDE_ASPECT_WIDTH = 16.;
74 const double CreateDVD_Thread::DVD_WIDE_ASPECT_HEIGHT = 9.;
75 const double CreateDVD_Thread::DVD_FRAMERATE = 30000. / 1001.;
76 const int CreateDVD_Thread::DVD_MAX_BITRATE = 8000000;
77 const int CreateDVD_Thread::DVD_CHANNELS = 2;
78 const int CreateDVD_Thread::DVD_WIDE_CHANNELS = 6;
79 const double CreateDVD_Thread::DVD_SAMPLERATE = 48000;
80 const double CreateDVD_Thread::DVD_KAUDIO_RATE = 224;
81
82
83 CreateDVD_MenuItem::CreateDVD_MenuItem(MWindow *mwindow)
84  : BC_MenuItem(_("DVD Render..."), _("Shift-D"), 'D')
85 {
86         set_shift(1); 
87         this->mwindow = mwindow;
88 }
89
90 int CreateDVD_MenuItem::handle_event()
91 {
92         mwindow->create_dvd->start();
93         return 1;
94 }
95
96
97 CreateDVD_Thread::CreateDVD_Thread(MWindow *mwindow)
98  : BC_DialogThread()
99 {
100         this->mwindow = mwindow;
101         this->gui = 0;
102         this->use_deinterlace = 0;
103         this->use_scale = 0;
104         this->use_histogram = 0;
105         this->use_inverse_telecine = 0;
106         this->use_wide_audio = 0;
107         this->use_ffmpeg = 0;
108         this->use_resize_tracks = 0;
109         this->use_label_chapters = 0;
110
111         this->dvd_size = DVD_SIZE;
112         this->dvd_width = DVD_WIDTH;
113         this->dvd_height = DVD_HEIGHT;
114         this->dvd_aspect_width = DVD_ASPECT_WIDTH;
115         this->dvd_aspect_height = DVD_ASPECT_HEIGHT;
116         this->dvd_framerate = DVD_FRAMERATE;
117         this->dvd_samplerate = DVD_SAMPLERATE;
118         this->dvd_max_bitrate = DVD_MAX_BITRATE;
119         this->dvd_kaudio_rate = DVD_KAUDIO_RATE;
120         this->max_w = this->max_h = 0;
121 }
122
123 CreateDVD_Thread::~CreateDVD_Thread()
124 {
125         close_window();
126 }
127
128 int CreateDVD_Thread::create_dvd_jobs(ArrayList<BatchRenderJob*> *jobs,
129         const char *tmp_path, const char *asset_title)
130 {
131         EDL *edl = mwindow->edl;
132         if( !edl || !edl->session ) {
133                 char msg[BCTEXTLEN];
134                 sprintf(msg, _("No EDL/Session"));
135                 MainError::show_error(msg);
136                 return 1;
137         }
138         EDLSession *session = edl->session;
139
140         double total_length = edl->tracks->total_length();
141         if( total_length <= 0 ) {
142                 char msg[BCTEXTLEN];
143                 sprintf(msg, _("No content: %s"), asset_title);
144                 MainError::show_error(msg);
145                 return 1;
146         }
147
148         char asset_dir[BCTEXTLEN];
149         sprintf(asset_dir, "%s/%s", tmp_path, asset_title);
150
151         if( mkdir(asset_dir, 0777) ) {
152                 char err[BCTEXTLEN], msg[BCTEXTLEN];
153                 strerror_r(errno, err, sizeof(err));
154                 sprintf(msg, _("Unable to create directory: %s\n-- %s"), asset_dir, err);
155                 MainError::show_error(msg);
156                 return 1;
157         }
158
159         double old_samplerate = session->sample_rate;
160         double old_framerate = session->frame_rate;
161
162         session->video_channels = DVD_STREAMS;
163         session->video_tracks = DVD_STREAMS;
164         session->frame_rate = dvd_framerate;
165         session->output_w = dvd_width;
166         session->output_h = dvd_height;
167         session->aspect_w = dvd_aspect_width;
168         session->aspect_h = dvd_aspect_height;
169         session->sample_rate = dvd_samplerate;
170         session->audio_channels = session->audio_tracks =
171                 use_wide_audio ? DVD_WIDE_CHANNELS : DVD_CHANNELS;
172
173         char script_filename[BCTEXTLEN];
174         sprintf(script_filename, "%s/dvd.sh", asset_dir);
175         int fd = open(script_filename, O_WRONLY+O_CREAT+O_TRUNC, 0755);
176         FILE *fp = fdopen(fd, "w");
177         if( !fp ) {
178                 char err[BCTEXTLEN], msg[BCTEXTLEN];
179                 strerror_r(errno, err, sizeof(err));
180                 sprintf(msg, _("Unable to save: %s\n-- %s"), script_filename, err);
181                 MainError::show_error(msg);
182                 return 1;
183         }
184         fprintf(fp,"#!/bin/bash\n");
185         fprintf(fp,"echo \"running %s\" $# $*\n", script_filename);
186         fprintf(fp,"\n");
187         const char *exec_path = File::get_cinlib_path();
188         fprintf(fp,"PATH=$PATH:%s\n",exec_path);
189         if( !use_ffmpeg ) {
190                 fprintf(fp,"mplex -f 8 -o $1/dvd.mpg $1/dvd.m2v $1/dvd.ac3\n");
191                 fprintf(fp,"\n");
192         }
193         fprintf(fp,"rm -rf $1/iso\n");
194         fprintf(fp,"mkdir -p $1/iso\n");
195         fprintf(fp,"\n");
196 // dvdauthor ver 0.7.0 requires this to work
197         int norm = dvd_formats[use_standard].norm;
198         const char *name = dvd_norms[norm].name;
199         fprintf(fp,"export VIDEO_FORMAT=%s\n", name);
200         fprintf(fp,"dvdauthor -x - <<eof\n");
201         fprintf(fp,"<dvdauthor dest=\"$1/iso\">\n");
202         fprintf(fp,"  <vmgm>\n");
203         fprintf(fp,"    <fpc> jump title 1; </fpc>\n");
204         fprintf(fp,"  </vmgm>\n");
205         fprintf(fp,"  <titleset>\n");
206         fprintf(fp,"    <titles>\n");
207         char std[BCSTRLEN], *cp = std;
208         for( const char *np=name; *np!=0; ++cp,++np) *cp = *np + 'a'-'A';
209         *cp = 0;
210         fprintf(fp,"    <video format=\"%s\" aspect=\"%d:%d\" resolution=\"%dx%d\"/>\n",
211                 std, (int)session->aspect_w, (int)session->aspect_h,
212                 session->output_w, session->output_h);
213         fprintf(fp,"    <audio format=\"ac3\" lang=\"en\"/>\n");
214         fprintf(fp,"    <pgc>\n");
215         fprintf(fp,"      <vob file=\"$1/dvd.mpg\" chapters=\"");
216         if( use_label_chapters && edl->labels ) {
217                 Label *label = edl->labels->first;
218                 while( label ) {
219                         int secs = label->position;
220                         int mins = secs / 60;
221                         int frms = (label->position-secs) * session->frame_rate;
222                         fprintf(fp,"%d:%02d:%02d.%d", mins/60, mins%60, secs%60, frms);
223                         if( (label=label->next) != 0 ) fprintf(fp, ",");
224                 }
225         }
226         else {
227                 int mins = 0;
228                 for( int secs=0 ; secs<total_length; secs+=10*60 ) {
229                         mins = secs / 60;
230                         fprintf(fp,"%d:%02d:00,", mins/60, mins%60);
231                 }
232                 fprintf(fp,"%d:%02d:00", mins/60, mins%60);
233         }
234         fprintf(fp,"\"/>\n");
235         fprintf(fp,"    </pgc>\n");
236         fprintf(fp,"    </titles>\n");
237         fprintf(fp,"  </titleset>\n");
238         fprintf(fp,"</dvdauthor>\n");
239         fprintf(fp,"eof\n");
240         fprintf(fp,"\n");
241         fprintf(fp,"echo To burn dvd, load blank media and run:\n");
242         fprintf(fp,"echo growisofs -dvd-compat -Z /dev/dvd -dvd-video $1/iso\n");
243         fprintf(fp,"\n");
244         fclose(fp);
245
246         if( use_wide_audio ) {
247                 session->audio_channels = session->audio_tracks = DVD_WIDE_CHANNELS;
248                 session->achannel_positions[0] = 90;
249                 session->achannel_positions[1] = 150;
250                 session->achannel_positions[2] = 30;
251                 session->achannel_positions[3] = 210;
252                 session->achannel_positions[4] = 330;
253                 session->achannel_positions[5] = 270;
254                 if( edl->tracks->recordable_audio_tracks() == DVD_WIDE_CHANNELS )
255                         mwindow->remap_audio(MWindow::AUDIO_1_TO_1);
256         }
257         else {
258                 session->audio_channels = session->audio_tracks = DVD_CHANNELS;
259                 session->achannel_positions[0] = 180;
260                 session->achannel_positions[1] = 0;
261                 if( edl->tracks->recordable_audio_tracks() == DVD_WIDE_CHANNELS )
262                         mwindow->remap_audio(MWindow::AUDIO_5_1_TO_2);
263         }
264
265         double new_samplerate = session->sample_rate;
266         double new_framerate = session->frame_rate;
267         edl->rechannel();
268         edl->resample(old_samplerate, new_samplerate, TRACK_AUDIO);
269         edl->resample(old_framerate, new_framerate, TRACK_VIDEO);
270
271         int64_t aud_size = ((dvd_kaudio_rate * total_length)/8 + 1000-1) * 1000;
272         int64_t vid_size = dvd_size*0.96 - aud_size;
273         int64_t vid_bitrate = (vid_size * 8) / total_length;
274         vid_bitrate /= 1000;  vid_bitrate *= 1000;
275         if( vid_bitrate > dvd_max_bitrate )
276                 vid_bitrate = dvd_max_bitrate;
277
278         char xml_filename[BCTEXTLEN];
279         sprintf(xml_filename, "%s/dvd.xml", asset_dir);
280         FileXML xml_file;
281         edl->save_xml(&xml_file, xml_filename, 0, 0);
282         xml_file.terminate_string();
283         if( xml_file.write_to_file(xml_filename) ) {
284                 char msg[BCTEXTLEN];
285                 sprintf(msg, _("Unable to save: %s"), xml_filename);
286                 MainError::show_error(msg);
287                 return 1;
288         }
289
290         BatchRenderJob *job = new BatchRenderJob(mwindow->preferences);
291         jobs->append(job);
292         strcpy(&job->edl_path[0], xml_filename);
293         Asset *asset = job->asset;
294
295         asset->layers = DVD_STREAMS;
296         asset->frame_rate = session->frame_rate;
297         asset->width = session->output_w;
298         asset->height = session->output_h;
299         asset->aspect_ratio = session->aspect_w / session->aspect_h;
300
301         if( use_ffmpeg ) {
302                 char option_path[BCTEXTLEN];
303                 sprintf(&asset->path[0],"%s/dvd.mpg", asset_dir);
304                 asset->format = FILE_FFMPEG;
305                 strcpy(asset->fformat, "dvd");
306
307                 asset->audio_data = 1;
308                 strcpy(asset->acodec, "dvd.dvd");
309                 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
310                 FFMPEG::load_options(option_path, asset->ff_audio_options,
311                          sizeof(asset->ff_audio_options));
312                 asset->ff_audio_bitrate = dvd_kaudio_rate * 1000;
313
314                 asset->video_data = 1;
315                 strcpy(asset->vcodec, "dvd.dvd");
316                 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
317                 FFMPEG::load_options(option_path, asset->ff_video_options,
318                          sizeof(asset->ff_video_options));
319                 asset->ff_video_bitrate = vid_bitrate;
320                 asset->ff_video_quality = 0;
321         }
322         else {
323                 sprintf(&asset->path[0],"%s/dvd.m2v", asset_dir);
324                 asset->video_data = 1;
325                 asset->format = FILE_VMPEG;
326                 asset->vmpeg_cmodel = BC_YUV420P;
327                 asset->vmpeg_fix_bitrate = 1;
328                 asset->vmpeg_bitrate = vid_bitrate;
329                 asset->vmpeg_quantization = 15;
330                 asset->vmpeg_iframe_distance = 15;
331                 asset->vmpeg_progressive = 0;
332                 asset->vmpeg_denoise = 0;
333                 asset->vmpeg_seq_codes = 0;
334                 asset->vmpeg_derivative = 2;
335                 asset->vmpeg_preset = 8;
336                 asset->vmpeg_field_order = 0;
337                 asset->vmpeg_pframe_distance = 0;
338                 job = new BatchRenderJob(mwindow->preferences);
339                 jobs->append(job);
340                 strcpy(&job->edl_path[0], xml_filename);
341                 asset = job->asset;
342                 
343                 sprintf(&asset->path[0],"%s/dvd.ac3", asset_dir);
344                 asset->audio_data = 1;
345                 asset->format = FILE_AC3;
346                 asset->channels = session->audio_channels;
347                 asset->sample_rate = session->sample_rate;
348                 asset->bits = 16;
349                 asset->byte_order = 0;
350                 asset->signed_ = 1;
351                 asset->header = 0;
352                 asset->dither = 0;
353                 asset->ac3_bitrate = dvd_kaudio_rate;
354         }
355
356         job = new BatchRenderJob(mwindow->preferences);
357         jobs->append(job);
358         job->edl_path[0] = '@';
359         strcpy(&job->edl_path[1], script_filename);
360         strcpy(&job->asset->path[0], asset_dir);
361
362         return 0;
363 }
364
365 void CreateDVD_Thread::handle_close_event(int result)
366 {
367         if( result ) return;
368         mwindow->batch_render->load_defaults(mwindow->defaults);
369         mwindow->undo->update_undo_before();
370         KeyFrame keyframe;  char data[BCTEXTLEN];
371         if( use_deinterlace ) {
372                 sprintf(data,"<DEINTERLACE MODE=1>");
373                 keyframe.set_data(data);
374                 insert_video_plugin("Deinterlace", &keyframe);
375         }
376         if( use_inverse_telecine ) {
377                 sprintf(data,"<IVTC FRAME_OFFSET=0 FIRST_FIELD=0 "
378                         "AUTOMATIC=1 AUTO_THRESHOLD=2.0e+00 PATTERN=2>");
379                 keyframe.set_data(data);
380                 insert_video_plugin("Inverse Telecine", &keyframe);
381         }
382         if( use_scale != Rescale::none ) {
383                 double dvd_aspect = dvd_aspect_height > 0 ? dvd_aspect_width/dvd_aspect_height : 1;
384
385                 Tracks *tracks = mwindow->edl->tracks;
386                 for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
387                         if( vtrk->data_type != TRACK_VIDEO ) continue;
388                         if( !vtrk->record ) continue;
389                         vtrk->expand_view = 1;
390                         PluginSet *plugin_set = new PluginSet(mwindow->edl, vtrk);
391                         vtrk->plugin_set.append(plugin_set);
392                         Edits *edits = vtrk->edits;
393                         for( Edit *edit=edits->first; edit; edit=edit->next ) {
394                                 Indexable *indexable = edit->get_source();
395                                 if( !indexable ) continue;
396                                 Rescale in(indexable);
397                                 Rescale out(dvd_width, dvd_height, dvd_aspect);
398                                 float src_w, src_h, dst_w, dst_h;
399                                 in.rescale(out,use_scale, src_w,src_h, dst_w,dst_h);
400                                 sprintf(data,"<SCALERATIO TYPE=%d"
401                                         " IN_W=%d IN_H=%d IN_ASPECT_RATIO=%f"
402                                         " OUT_W=%d OUT_H=%d OUT_ASPECT_RATIO=%f"
403                                         " SRC_X=%f SRC_Y=%f SRC_W=%f SRC_H=%f"
404                                         " DST_X=%f DST_Y=%f DST_W=%f DST_H=%f>", use_scale,
405                                         in.w, in.h, in.aspect, out.w, out.h, out.aspect,
406                                         0., 0., src_w, src_h, 0., 0., dst_w, dst_h);
407                                 keyframe.set_data(data);
408                                 plugin_set->insert_plugin(_("Scale Ratio"),
409                                         edit->startproject, edit->length,
410                                         PLUGIN_STANDALONE, 0, &keyframe, 0);
411                         }
412                 }
413         }
414
415         if( use_resize_tracks )
416                 resize_tracks();
417         if( use_histogram ) {
418 #if 0
419                 sprintf(data, "<HISTOGRAM OUTPUT_MIN_0=0 OUTPUT_MAX_0=1 "
420                         "OUTPUT_MIN_1=0 OUTPUT_MAX_1=1 "
421                         "OUTPUT_MIN_2=0 OUTPUT_MAX_2=1 "
422                         "OUTPUT_MIN_3=0 OUTPUT_MAX_3=1 "
423                         "AUTOMATIC=0 THRESHOLD=9.0-01 PLOT=0 SPLIT=0>"
424                         "<POINTS></POINTS><POINTS></POINTS><POINTS></POINTS>"
425                         "<POINTS><POINT X=6.0e-02 Y=0>"
426                                 "<POINT X=9.4e-01 Y=1></POINTS>");
427 #else
428                 sprintf(data, "<HISTOGRAM AUTOMATIC=0 THRESHOLD=1.0e-01 "
429                         "PLOT=0 SPLIT=0 W=440 H=500 PARADE=0 MODE=3 "
430                         "LOW_OUTPUT_0=0 HIGH_OUTPUT_0=1 LOW_INPUT_0=0 HIGH_INPUT_0=1 GAMMA_0=1 "
431                         "LOW_OUTPUT_1=0 HIGH_OUTPUT_1=1 LOW_INPUT_1=0 HIGH_INPUT_1=1 GAMMA_1=1 "
432                         "LOW_OUTPUT_2=0 HIGH_OUTPUT_2=1 LOW_INPUT_2=0 HIGH_INPUT_2=1 GAMMA_2=1 "
433                         "LOW_OUTPUT_3=0 HIGH_OUTPUT_3=1 LOW_INPUT_3=0.044 HIGH_INPUT_3=0.956 "
434                         "GAMMA_3=1>");
435 #endif
436                 keyframe.set_data(data);
437                 insert_video_plugin("Histogram", &keyframe);
438         }
439         mwindow->batch_render->reset(1);
440         create_dvd_jobs(&mwindow->batch_render->jobs, tmp_path, asset_title);
441         mwindow->save_backup();
442         mwindow->undo->update_undo_after(_("create dvd"), LOAD_ALL);
443         mwindow->resync_guis();
444         mwindow->batch_render->handle_close_event(0);
445         mwindow->batch_render->start();
446 }
447
448 BC_Window* CreateDVD_Thread::new_gui()
449 {
450         memset(tmp_path,0,sizeof(tmp_path));
451         strcpy(tmp_path,"/tmp");
452         memset(asset_title,0,sizeof(asset_title));
453         time_t dt;  time(&dt);
454         struct tm dtm;  localtime_r(&dt, &dtm);
455         sprintf(asset_title, "dvd_%02d%02d%02d-%02d%02d%02d",
456                 dtm.tm_year+1900, dtm.tm_mon+1, dtm.tm_mday,
457                 dtm.tm_hour, dtm.tm_min, dtm.tm_sec);
458         use_deinterlace = 0;
459         use_scale = Rescale::none;
460         use_histogram = 0;
461         use_inverse_telecine = 0;
462         use_wide_audio = 0;
463         use_ffmpeg = 0;
464         use_resize_tracks = 0;
465         use_label_chapters = 0;
466         use_standard = DVD_NTSC_4x3;
467
468         dvd_size = DVD_SIZE;
469         dvd_width = DVD_WIDTH;
470         dvd_height = DVD_HEIGHT;
471         dvd_aspect_width = DVD_ASPECT_WIDTH;
472         dvd_aspect_height = DVD_ASPECT_HEIGHT;
473         dvd_framerate = DVD_FRAMERATE;
474         dvd_samplerate = DVD_SAMPLERATE;
475         dvd_max_bitrate = DVD_MAX_BITRATE;
476         dvd_kaudio_rate = DVD_KAUDIO_RATE;
477         max_w = 0; max_h = 0;
478
479         int has_standard = -1;
480         if( mwindow->edl ) {
481                 EDLSession *session = mwindow->edl->session;
482                 double framerate = session->frame_rate;
483                 double aspect_ratio = session->aspect_h > 0 ?
484                         session->aspect_w / session->aspect_h > 0 : 1;
485                 int output_w = session->output_w, output_h = session->output_h;
486 // match the session to any known standard
487                 for( int i=0; i<(int)(sizeof(dvd_formats)/sizeof(dvd_formats[0])); ++i ) {
488                         int norm = dvd_formats[i].norm;
489                         if( !EQUIV(framerate, dvd_norms[norm].framerate) ) continue;
490                         if( output_w != dvd_norms[norm].w ) continue;
491                         if( output_h != dvd_norms[norm].h ) continue;
492                         int aspect = dvd_formats[i].aspect;
493                         double dvd_aspect_ratio =
494                                 (double)dvd_aspects[aspect].w / dvd_aspects[aspect].h; 
495                         if( !EQUIV(aspect_ratio, dvd_aspect_ratio) ) continue;
496                         has_standard = i;  break;
497                 }
498                 if( has_standard < 0 ) {
499 // or use the default standard
500                         if( !strcmp(mwindow->default_standard, "NTSC") ) has_standard = DVD_NTSC_4x3;
501                         else if( !strcmp(mwindow->default_standard, "PAL") ) has_standard = DVD_PAL_4x3;
502                 }
503         }
504         if( has_standard >= 0 )
505                 use_standard = has_standard;
506
507         option_presets();
508         int scr_x = mwindow->gui->get_screen_x(0, -1);
509         int scr_w = mwindow->gui->get_screen_w(0, -1);
510         int scr_h = mwindow->gui->get_screen_h(0, -1);
511         int w = 520, h = 280;
512         int x = scr_x + scr_w/2 - w/2, y = scr_h/2 - h/2;
513
514         gui = new CreateDVD_GUI(this, x, y, w, h);
515         gui->create_objects();
516         return gui;
517 }
518
519
520 CreateDVD_OK::CreateDVD_OK(CreateDVD_GUI *gui, int x, int y)
521  : BC_OKButton(x, y)
522 {
523         this->gui = gui;
524         set_tooltip(_("end setup, start batch render"));
525 }
526
527 CreateDVD_OK::~CreateDVD_OK()
528 {
529 }
530
531 int CreateDVD_OK::button_press_event()
532 {
533         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
534                 gui->set_done(0);
535                 return 1;
536         }
537         return 0;
538 }
539
540 int CreateDVD_OK::keypress_event()
541 {
542         return 0;
543 }
544
545
546 CreateDVD_Cancel::CreateDVD_Cancel(CreateDVD_GUI *gui, int x, int y)
547  : BC_CancelButton(x, y)
548 {
549         this->gui = gui;
550 }
551
552 CreateDVD_Cancel::~CreateDVD_Cancel()
553 {
554 }
555
556 int CreateDVD_Cancel::button_press_event()
557 {
558         if(get_buttonpress() == 1 && is_event_win() && cursor_inside()) {
559                 gui->set_done(1);
560                 return 1;
561         }
562         return 0;
563 }
564
565
566 CreateDVD_DiskSpace::CreateDVD_DiskSpace(CreateDVD_GUI *gui, int x, int y)
567  : BC_Title(x, y, "", MEDIUMFONT, GREEN)
568 {
569         this->gui = gui;
570 }
571
572 CreateDVD_DiskSpace::~CreateDVD_DiskSpace()
573 {
574 }
575
576 int64_t CreateDVD_DiskSpace::tmp_path_space()
577 {
578         const char *path = gui->tmp_path->get_text();
579         if( access(path,R_OK+W_OK) ) return 0;
580         struct statfs sfs;
581         if( statfs(path, &sfs) ) return 0;
582         return (int64_t)sfs.f_bsize * sfs.f_bfree;
583 }
584
585 void CreateDVD_DiskSpace::update()
586 {
587         static const char *suffix[] = { "", "KB", "MB", "GB", "TB", "PB" };
588         int64_t disk_space = tmp_path_space();
589         double media_size = 15e9, msz = 0, m = 1;
590         char sfx[BCSTRLEN];
591         if( sscanf(gui->media_size->get_text(), "%lf%s", &msz, sfx) == 2 ) {
592                 int i = sizeof(suffix)/sizeof(suffix[0]);
593                 while( --i >= 0 && strcmp(sfx, suffix[i]) );
594                 while( --i >= 0 ) m *= 1000;
595                 media_size = msz * m;
596         }
597         m = gui->thread->use_ffmpeg ? 2 : 3;
598         int color = disk_space < media_size*m ? RED : GREEN;
599         int i = 0;
600         for( int64_t space=disk_space; i<5 && (space/=1000)>0; disk_space=space, ++i );
601         char text[BCTEXTLEN];
602         sprintf(text, "%s%3jd%s", _("disk space: "), disk_space, suffix[i]);
603         gui->disk_space->BC_Title::update(text);
604         gui->disk_space->set_color(color);
605 }
606
607 CreateDVD_TmpPath::CreateDVD_TmpPath(CreateDVD_GUI *gui, int x, int y, int w)
608  : BC_TextBox(x, y, w, 1, -(int)sizeof(gui->thread->tmp_path),
609                 gui->thread->tmp_path, 1, MEDIUMFONT)
610 {
611         this->gui = gui;
612 }
613
614 CreateDVD_TmpPath::~CreateDVD_TmpPath()
615 {
616 }
617
618 int CreateDVD_TmpPath::handle_event()
619 {
620         gui->disk_space->update();
621         return 1;
622 }
623
624
625 CreateDVD_AssetTitle::CreateDVD_AssetTitle(CreateDVD_GUI *gui, int x, int y, int w)
626  : BC_TextBox(x, y, w, 1, 0, gui->thread->asset_title, 1, MEDIUMFONT)
627 {
628         this->gui = gui;
629 }
630
631 CreateDVD_AssetTitle::~CreateDVD_AssetTitle()
632 {
633 }
634
635
636 CreateDVD_Deinterlace::CreateDVD_Deinterlace(CreateDVD_GUI *gui, int x, int y)
637  : BC_CheckBox(x, y, &gui->thread->use_deinterlace, _("Deinterlace"))
638 {
639         this->gui = gui;
640 }
641
642 CreateDVD_Deinterlace::~CreateDVD_Deinterlace()
643 {
644 }
645
646 int CreateDVD_Deinterlace::handle_event()
647 {
648         if( get_value() ) {
649                 gui->need_inverse_telecine->set_value(0);
650                 gui->thread->use_inverse_telecine = 0;
651         }
652         return BC_CheckBox::handle_event();
653 }
654
655
656 CreateDVD_InverseTelecine::CreateDVD_InverseTelecine(CreateDVD_GUI *gui, int x, int y)
657  : BC_CheckBox(x, y, &gui->thread->use_inverse_telecine, _("Inverse Telecine"))
658 {
659         this->gui = gui;
660 }
661
662 CreateDVD_InverseTelecine::~CreateDVD_InverseTelecine()
663 {
664 }
665
666 int CreateDVD_InverseTelecine::handle_event()
667 {
668         if( get_value() ) {
669                 gui->need_deinterlace->set_value(0);
670                 gui->thread->use_deinterlace = 0;
671         }
672         return BC_CheckBox::handle_event();
673 }
674
675
676 CreateDVD_ResizeTracks::CreateDVD_ResizeTracks(CreateDVD_GUI *gui, int x, int y)
677  : BC_CheckBox(x, y, &gui->thread->use_resize_tracks, _("Resize Tracks"))
678 {
679         this->gui = gui;
680 }
681
682 CreateDVD_ResizeTracks::~CreateDVD_ResizeTracks()
683 {
684 }
685
686
687 CreateDVD_Histogram::CreateDVD_Histogram(CreateDVD_GUI *gui, int x, int y)
688  : BC_CheckBox(x, y, &gui->thread->use_histogram, _("Histogram"))
689 {
690         this->gui = gui;
691 }
692
693 CreateDVD_Histogram::~CreateDVD_Histogram()
694 {
695 }
696
697 CreateDVD_LabelChapters::CreateDVD_LabelChapters(CreateDVD_GUI *gui, int x, int y)
698  : BC_CheckBox(x, y, &gui->thread->use_label_chapters, _("Chapters at Labels"))
699 {
700         this->gui = gui;
701 }
702
703 CreateDVD_LabelChapters::~CreateDVD_LabelChapters()
704 {
705 }
706
707 CreateDVD_WideAudio::CreateDVD_WideAudio(CreateDVD_GUI *gui, int x, int y)
708  : BC_CheckBox(x, y, &gui->thread->use_wide_audio, _("Audio 5.1"))
709 {
710         this->gui = gui;
711 }
712
713 CreateDVD_WideAudio::~CreateDVD_WideAudio()
714 {
715 }
716
717 CreateDVD_UseFFMpeg::CreateDVD_UseFFMpeg(CreateDVD_GUI *gui, int x, int y)
718  : BC_CheckBox(x, y, &gui->thread->use_ffmpeg, _("Use FFMPEG"))
719 {
720         this->gui = gui;
721 }
722
723 CreateDVD_UseFFMpeg::~CreateDVD_UseFFMpeg()
724 {
725 }
726
727
728
729
730 CreateDVD_GUI::CreateDVD_GUI(CreateDVD_Thread *thread, int x, int y, int w, int h)
731  : BC_Window(_(PROGRAM_NAME ": Create DVD"), x, y, w, h, 50, 50, 1, 0, 1)
732 {
733         this->thread = thread;
734         at_x = at_y = tmp_x = tmp_y = 0;
735         ok_x = ok_y = ok_w = ok_h = 0;
736         cancel_x = cancel_y = cancel_w = cancel_h = 0;
737         asset_title = 0;
738         tmp_path = 0;
739         btmp_path = 0;
740         disk_space = 0;
741         standard = 0;
742         scale = 0;
743         need_deinterlace = 0;
744         need_inverse_telecine = 0;
745         need_resize_tracks = 0;
746         need_histogram = 0;
747         need_wide_audio = 0;
748         need_label_chapters = 0;
749         ok = 0;
750         cancel = 0;
751 }
752
753 CreateDVD_GUI::~CreateDVD_GUI()
754 {
755 }
756
757 void CreateDVD_GUI::create_objects()
758 {
759         lock_window("CreateDVD_GUI::create_objects");
760         int pady = BC_TextBox::calculate_h(this, MEDIUMFONT, 0, 1) + 5;
761         int padx = BC_Title::calculate_w(this, (char*)"X", MEDIUMFONT);
762         int x = padx/2, y = pady/2;
763         BC_Title *title = new BC_Title(x, y, _("Title:"), MEDIUMFONT, YELLOW);
764         add_subwindow(title);
765         at_x = x + title->get_w();  at_y = y;
766         asset_title = new CreateDVD_AssetTitle(this, at_x, at_y, get_w()-at_x-10);
767         add_subwindow(asset_title);
768         y += title->get_h() + pady/2;
769         title = new BC_Title(x, y, _("Work path:"), MEDIUMFONT, YELLOW);
770         add_subwindow(title);
771         tmp_x = x + title->get_w();  tmp_y = y;
772         tmp_path = new CreateDVD_TmpPath(this, tmp_x, tmp_y,  get_w()-tmp_x-35);
773         add_subwindow(tmp_path);
774         btmp_path = new BrowseButton(thread->mwindow, this, tmp_path,
775                 tmp_x+tmp_path->get_w(), tmp_y, "/tmp",
776                 _("Work path"), _("Select a Work directory:"), 1);
777         add_subwindow(btmp_path);
778         y += title->get_h() + pady/2;
779         disk_space = new CreateDVD_DiskSpace(this, x, y);
780         add_subwindow(disk_space);
781         int x0 = get_w() - 170;
782         title = new BC_Title(x0, y, _("Media:"), MEDIUMFONT, YELLOW);
783         add_subwindow(title);
784         int x1 = x0+title->get_w()+padx;
785         media_size = new CreateDVD_MediaSize(this, x1, y);
786         media_size->create_objects();
787         media_sizes.append(new BC_ListBoxItem("4.7GB"));
788         media_sizes.append(new BC_ListBoxItem("8.3GB"));
789         media_size->update_list(&media_sizes);
790         media_size->update(media_sizes[0]->get_text());
791         disk_space->update();
792         y += disk_space->get_h() + pady/2;
793         title = new BC_Title(x, y, _("Format:"), MEDIUMFONT, YELLOW);
794         add_subwindow(title);
795         standard = new CreateDVD_Format(this, title->get_w() + padx, y);
796         add_subwindow(standard);
797         standard->create_objects();
798         x0 -= 30;
799         title = new BC_Title(x0, y, _("Scale:"), MEDIUMFONT, YELLOW);
800         add_subwindow(title);
801         x1 = x0+title->get_w()+padx;
802         scale = new CreateDVD_Scale(this, x1, y);
803         add_subwindow(scale);
804         scale->create_objects();
805         y += standard->get_h() + pady/2;
806         need_deinterlace = new CreateDVD_Deinterlace(this, x, y);
807         add_subwindow(need_deinterlace);
808         x1 = x + 170;
809         int x2 = x1 + 170;
810         need_inverse_telecine = new CreateDVD_InverseTelecine(this, x1, y);
811         add_subwindow(need_inverse_telecine);
812         need_use_ffmpeg = new CreateDVD_UseFFMpeg(this, x2, y);
813         add_subwindow(need_use_ffmpeg);
814         y += need_deinterlace->get_h() + pady/2;
815         need_histogram = new CreateDVD_Histogram(this, x, y);
816         add_subwindow(need_histogram);
817         need_wide_audio = new CreateDVD_WideAudio(this, x1, y);
818         add_subwindow(need_wide_audio);
819         need_resize_tracks = new CreateDVD_ResizeTracks(this, x2, y);
820         add_subwindow(need_resize_tracks);
821         y += need_histogram->get_h() + pady/2;
822         need_label_chapters = new CreateDVD_LabelChapters(this, x1, y);
823         add_subwindow(need_label_chapters);
824         ok_w = BC_OKButton::calculate_w();
825         ok_h = BC_OKButton::calculate_h();
826         ok_x = 10;
827         ok_y = get_h() - ok_h - 10;
828         ok = new CreateDVD_OK(this, ok_x, ok_y);
829         add_subwindow(ok);
830         cancel_w = BC_CancelButton::calculate_w();
831         cancel_h = BC_CancelButton::calculate_h();
832         cancel_x = get_w() - cancel_w - 10,
833         cancel_y = get_h() - cancel_h - 10;
834         cancel = new CreateDVD_Cancel(this, cancel_x, cancel_y);
835         add_subwindow(cancel);
836         show_window();
837         unlock_window();
838 }
839
840 int CreateDVD_GUI::resize_event(int w, int h)
841 {
842         asset_title->reposition_window(at_x, at_y, get_w()-at_x-10);
843         tmp_path->reposition_window(tmp_x, tmp_y,  get_w()-tmp_x-35);
844         btmp_path->reposition_window(tmp_x+tmp_path->get_w(), tmp_y);
845         ok_y = h - ok_h - 10;
846         ok->reposition_window(ok_x, ok_y);
847         cancel_x = w - cancel_w - 10,
848         cancel_y = h - cancel_h - 10;
849         cancel->reposition_window(cancel_x, cancel_y);
850         return 0;
851 }
852
853 int CreateDVD_GUI::translation_event()
854 {
855         return 1;
856 }
857
858 int CreateDVD_GUI::close_event()
859 {
860         set_done(1);
861         return 1;
862 }
863
864 void CreateDVD_GUI::update()
865 {
866         scale->set_value(thread->use_scale);
867         need_deinterlace->set_value(thread->use_deinterlace);
868         need_inverse_telecine->set_value(thread->use_inverse_telecine);
869         need_use_ffmpeg->set_value(thread->use_ffmpeg);
870         need_resize_tracks->set_value(thread->use_resize_tracks);
871         need_histogram->set_value(thread->use_histogram);
872         need_wide_audio->set_value(thread->use_wide_audio);
873         need_label_chapters->set_value(thread->use_label_chapters);
874 }
875
876 int CreateDVD_Thread::
877 insert_video_plugin(const char *title, KeyFrame *default_keyframe)
878 {
879         Tracks *tracks = mwindow->edl->tracks;
880         for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
881                 if( vtrk->data_type != TRACK_VIDEO ) continue;
882                 if( !vtrk->record ) continue;
883                 vtrk->expand_view = 1;
884                 PluginSet *plugin_set = new PluginSet(mwindow->edl, vtrk);
885                 vtrk->plugin_set.append(plugin_set);
886                 Edits *edits = vtrk->edits;
887                 for( Edit *edit=edits->first; edit; edit=edit->next ) {
888                         plugin_set->insert_plugin(_(title),
889                                 edit->startproject, edit->length,
890                                 PLUGIN_STANDALONE, 0, default_keyframe, 0);
891                 }
892         }
893         return 0;
894 }
895
896 int CreateDVD_Thread::
897 resize_tracks()
898 {
899         Tracks *tracks = mwindow->edl->tracks;
900         int trk_w = max_w, trk_h = max_h;
901         if( trk_w < dvd_width ) trk_w = dvd_width;
902         if( trk_h < dvd_height ) trk_h = dvd_height;
903         for( Track *vtrk=tracks->first; vtrk; vtrk=vtrk->next ) {
904                 if( vtrk->data_type != TRACK_VIDEO ) continue;
905                 if( !vtrk->record ) continue;
906                 vtrk->track_w = trk_w;
907                 vtrk->track_h = trk_h;
908         }
909         return 0;
910 }
911
912 int CreateDVD_Thread::
913 option_presets()
914 {
915 // reset only probed options
916         use_deinterlace = 0;
917         use_scale = Rescale::none;
918         use_resize_tracks = 0;
919         use_wide_audio = 0;
920         use_label_chapters = 0;
921
922         if( !mwindow->edl ) return 1;
923
924         int norm = dvd_formats[use_standard].norm;
925         dvd_width = dvd_norms[norm].w;
926         dvd_height = dvd_norms[norm].h;
927         dvd_framerate = dvd_norms[norm].framerate;
928         int aspect = dvd_formats[use_standard].aspect;
929         dvd_aspect_width = dvd_aspects[aspect].w;
930         dvd_aspect_height = dvd_aspects[aspect].h;
931         double dvd_aspect = dvd_aspect_height > 0 ? dvd_aspect_width/dvd_aspect_height : 1;
932
933         Tracks *tracks = mwindow->edl->tracks;
934         max_w = 0;  max_h = 0;
935         int has_deinterlace = 0, has_scale = 0;
936         for( Track *trk=tracks->first; trk; trk=trk->next ) {
937                 if( !trk->record ) continue;
938                 Edits *edits = trk->edits;
939                 switch( trk->data_type ) {
940                 case TRACK_VIDEO:
941                         for( Edit *edit=edits->first; edit; edit=edit->next ) {
942                                 if( edit->silence() ) continue;
943                                 Indexable *indexable = edit->get_source();
944                                 int w = indexable->get_w();
945                                 if( w > max_w ) max_w = w;
946                                 if( w != dvd_width ) use_scale = Rescale::scaled;
947                                 int h = indexable->get_h();
948                                 if( h > max_h ) max_h = h;
949                                 if( h != dvd_height ) use_scale = Rescale::scaled;
950                                 float aw, ah;
951                                 MWindow::create_aspect_ratio(aw, ah, w, h);
952                                 double aspect = ah > 0 ? aw / ah : 1;
953                                 if( !EQUIV(aspect, dvd_aspect) ) use_scale = Rescale::scaled;
954                         }
955                         for( int i=0; i<trk->plugin_set.size(); ++i ) {
956                                 for(Plugin *plugin = (Plugin*)trk->plugin_set[i]->first;
957                                                 plugin;
958                                                 plugin = (Plugin*)plugin->next) {
959                                         if( !strcmp(plugin->title, _("Deinterlace")) )
960                                                 has_deinterlace = 1;
961                                         if( !strcmp(plugin->title, _("Auto Scale")) ||
962                                             !strcmp(plugin->title, _("Scale Ratio")) ||
963                                             !strcmp(plugin->title, _("Scale")) )
964                                                 has_scale = 1;
965                                 }
966                         }
967                         break;
968                 }
969         }
970         if( has_scale )
971                 use_scale = Rescale::none;
972         if( use_scale != Rescale::none ) {
973                 if( max_w != dvd_width ) use_resize_tracks = 1;
974                 if( max_h != dvd_height ) use_resize_tracks = 1;
975         }
976         for( Track *trk=tracks->first; trk && !use_resize_tracks; trk=trk->next ) {
977                 if( !trk->record ) continue;
978                 switch( trk->data_type ) {
979                 case TRACK_VIDEO:
980                         if( trk->track_w != max_w ) use_resize_tracks = 1;
981                         if( trk->track_h != max_h ) use_resize_tracks = 1;
982                         break;
983                 }
984         }
985         if( !has_deinterlace && max_h > 2*dvd_height ) use_deinterlace = 1;
986         Labels *labels = mwindow->edl->labels;
987         use_label_chapters = labels && labels->first ? 1 : 0;
988
989         if( tracks->recordable_audio_tracks() == DVD_WIDE_CHANNELS )
990                 use_wide_audio = 1;
991
992         return 0;
993 }
994
995
996
997 CreateDVD_FormatItem::CreateDVD_FormatItem(CreateDVD_Format *popup,
998                 int standard, const char *text)
999  : BC_MenuItem(text)
1000 {
1001         this->popup = popup;
1002         this->standard = standard;
1003 }
1004
1005 CreateDVD_FormatItem::~CreateDVD_FormatItem()
1006 {
1007 }
1008
1009 int CreateDVD_FormatItem::handle_event()
1010 {
1011         popup->set_text(get_text());
1012         popup->gui->thread->use_standard = standard;
1013         return popup->handle_event();
1014 }
1015
1016
1017 CreateDVD_Format::CreateDVD_Format(CreateDVD_GUI *gui, int x, int y)
1018  : BC_PopupMenu(x, y, 180, "", 1)
1019 {
1020         this->gui = gui;
1021 }
1022
1023 CreateDVD_Format::~CreateDVD_Format()
1024 {
1025 }
1026
1027 void CreateDVD_Format::create_objects()
1028 {
1029         for( int i=0; i<(int)(sizeof(dvd_formats)/sizeof(dvd_formats[0])); ++i ) {
1030                 int norm = dvd_formats[i].norm;
1031                 int aspect = dvd_formats[i].aspect;
1032                 char item_text[BCTEXTLEN];
1033                 sprintf(item_text,"%4s (%5.2f) %dx%d",
1034                         dvd_norms[norm].name, dvd_norms[norm].framerate,
1035                         dvd_aspects[aspect].w, dvd_aspects[aspect].h);
1036                 add_item(new CreateDVD_FormatItem(this, i, item_text));
1037         }
1038         set_value(gui->thread->use_standard);
1039 }
1040
1041 int CreateDVD_Format::handle_event()
1042 {
1043         gui->thread->option_presets();
1044         gui->update();
1045         return 1;
1046 }
1047
1048
1049 CreateDVD_ScaleItem::CreateDVD_ScaleItem(CreateDVD_Scale *popup,
1050                 int scale, const char *text)
1051  : BC_MenuItem(text)
1052 {
1053         this->popup = popup;
1054         this->scale = scale;
1055 }
1056
1057 CreateDVD_ScaleItem::~CreateDVD_ScaleItem()
1058 {
1059 }
1060
1061 int CreateDVD_ScaleItem::handle_event()
1062 {
1063         popup->gui->thread->use_scale = scale;
1064         popup->set_value(scale);
1065         return popup->handle_event();
1066 }
1067
1068
1069 CreateDVD_Scale::CreateDVD_Scale(CreateDVD_GUI *gui, int x, int y)
1070  : BC_PopupMenu(x, y, 100, "", 1)
1071 {
1072         this->gui = gui;
1073 }
1074
1075 CreateDVD_Scale::~CreateDVD_Scale()
1076 {
1077 }
1078
1079 void CreateDVD_Scale::create_objects()
1080 {
1081
1082         for( int i=0; i<(int)Rescale::n_scale_types; ++i ) {
1083                 add_item(new CreateDVD_ScaleItem(this, i, Rescale::scale_types[i]));
1084         }
1085         set_value(gui->thread->use_scale);
1086 }
1087
1088 int CreateDVD_Scale::handle_event()
1089 {
1090         gui->update();
1091         return 1;
1092 }
1093
1094
1095 CreateDVD_MediaSize::CreateDVD_MediaSize(CreateDVD_GUI *gui, int x, int y)
1096  : BC_PopupTextBox(gui, 0, 0, x, y, 70,50)
1097 {
1098         this->gui = gui;
1099 }
1100
1101 CreateDVD_MediaSize::~CreateDVD_MediaSize()
1102 {
1103 }
1104
1105 int CreateDVD_MediaSize::handle_event()
1106 {
1107         gui->disk_space->update();
1108         return 1;
1109 }
1110