]> git.decadent.org.uk Git - videolink.git/blob - generate_dvd.cpp
Added vertical padding of buttons to even y coordinates since dvdauthor claims odd...
[videolink.git] / generate_dvd.cpp
1 // Copyright 2005-6 Ben Hutchings <ben@decadent.org.uk>.
2 // See the file "COPYING" for licence details.
3
4 #include <cerrno>
5 #include <cstring>
6 #include <fstream>
7 #include <iomanip>
8 #include <iostream>
9 #include <ostream>
10 #include <sstream>
11 #include <stdexcept>
12
13 #include <gdkmm/pixbuf.h>
14 #include <glibmm/miscutils.h>
15 #include <glibmm/spawn.h>
16
17 #include "dvd.hpp"
18 #include "generate_dvd.hpp"
19 #include "xml_utils.hpp"
20
21 namespace
22 {
23     // Return a closeness metric of an "end" rectangle to a "start"
24     // rectangle in the upward (-1) or downward (+1) direction.  Given
25     // several possible "end" rectangles, the one that seems visually
26     // closest in the given direction should have the highest value of
27     // this metric.  This is necessarily a heuristic function!    
28     double directed_closeness(const rectangle & start, const rectangle & end,
29                               int y_dir)
30     {
31         // The obvious approach is to use the centres of the
32         // rectangles.  However, for the "end" rectangle, using the
33         // horizontal position nearest the centre of the "start"
34         // rectangle seems to produce more reasonable results.  For
35         // example, if there are two "end" rectangles equally near to
36         // the "start" rectangle in terms of vertical distance and one
37         // of them horizontally overlaps the centre of the "start"
38         // rectangle, we want to pick that one even if the centre of
39         // that rectangle is further away from the centre of the
40         // "start" rectangle.
41         int start_x = (start.left + start.right) / 2;
42         int start_y = (start.top + start.bottom) / 2;
43         int end_y = (end.top + end.bottom) / 2;
44         int end_x;
45         if (end.right < start_x)
46             end_x = end.right;
47         else if (end.left > start_x)
48             end_x = end.left;
49         else
50             end_x = start_x;
51
52         // Return cosine of angle between the line between these points
53         // and the vertical, divided by the distance between the points
54         // if that is defined and positive; otherwise return 0.
55         int vertical_distance = (end_y - start_y) * y_dir;
56         if (vertical_distance <= 0)
57             return 0.0;
58         double distance_squared =
59             (end_x - start_x) * (end_x - start_x)
60             + (end_y - start_y) * (end_y - start_y);
61         return vertical_distance / distance_squared;
62     }
63
64     std::string temp_file_name(const temp_dir & dir,
65                                std::string base_name,
66                                unsigned index=0)
67     {
68         if (index != 0)
69         {
70             std::size_t index_pos = base_name.find("%3d");
71             assert(index_pos != std::string::npos);
72             base_name[index_pos] = '0' + index / 100;
73             base_name[index_pos + 1] = '0' + (index / 10) % 10;
74             base_name[index_pos + 2] = '0' + index % 10;
75         }
76
77         return Glib::build_filename(dir.get_name(), base_name);
78     }
79
80     // We would like to use just a single frame for the menu but this
81     // seems not to be legal or compatible.  The minimum length of a
82     // cell is 0.4 seconds but I've seen a static menu using 12 frames
83     // on a commercial "PAL" disc so let's use 12 frames regardless.
84     unsigned menu_duration_frames(const video::frame_params & params)
85     {
86         return 12;
87     }
88     double menu_duration_seconds(const video::frame_params & params)
89     {
90         return double(menu_duration_frames(params))
91             / double(params.rate_numer)
92             * double(params.rate_denom);
93     }
94
95     void throw_length_error(const char * limit_type, std::size_t limit)
96     {
97         std::ostringstream oss;
98         oss << "exceeded DVD limit: " << limit_type << " > " << limit;
99         throw std::length_error(oss.str());
100     }
101
102     // dvdauthor uses some menu numbers to represent entry points -
103     // distinct from the actual numbers of the menus assigned as those
104     // entry points - resulting in a practical limit of 119 per
105     // domain.  This seems to be an oddity of the parser that could be
106     // fixed, but for now we'll have to work with it.
107     const unsigned dvdauthor_anonymous_menus_max = dvd::domain_pgcs_max - 8;
108
109     // The current navigation code packs menu and button number into a
110     // single register, so the number of menus is limited to
111     // dvd::reg_s8_button_mult - 1 == 1023.  However temp_file_name()
112     // is limited to 999 numbered files and it seems pointless to
113     // change it to get another 24.
114     // If people really need more we could use separate menu and
115     // button number registers, possibly allowing up to 11900 menus
116     // (the size of the indirect jump tables might become a problem
117     // though).
118     const unsigned menus_max = 999;
119 }
120
121 dvd_generator::dvd_generator(const video::frame_params & frame_params,
122                              mpeg_encoder encoder)
123         : temp_dir_("videolink-"),
124           frame_params_(frame_params),
125           encoder_(encoder)
126 {}
127
128 dvd_generator::pgc_ref dvd_generator::add_menu()
129 {
130     pgc_ref next_menu(menu_pgc, menus_.size());
131
132     if (next_menu.index == menus_max)
133         throw_length_error("number of menus", menus_max);
134
135     menus_.resize(next_menu.index + 1);
136     return next_menu;
137 }
138
139 void dvd_generator::add_menu_entry(unsigned index,
140                                    const rectangle & area,
141                                    const pgc_ref & target)
142 {
143     assert(index < menus_.size());
144     assert(target.type == menu_pgc && target.index < menus_.size()
145            || target.type == title_pgc && target.index < titles_.size());
146
147     if (menus_[index].entries.size() == dvd::menu_buttons_max)
148         throw_length_error("number of buttons", dvd::menu_buttons_max);
149
150     menu_entry new_entry = { area, target };
151     menus_[index].entries.push_back(new_entry);
152 }
153
154 void dvd_generator::generate_menu_vob(unsigned index,
155                                       Glib::RefPtr<Gdk::Pixbuf> background,
156                                       Glib::RefPtr<Gdk::Pixbuf> highlights)
157     const
158 {
159     assert(index < menus_.size());
160     const menu & this_menu = menus_[index];
161
162     std::string background_name(
163         temp_file_name(temp_dir_, "menu-%3d-back.png", 1 + index));
164     std::cout << "INFO: Saving " << background_name << std::endl;
165     background->save(background_name, "png");
166
167     std::string highlights_name(
168         temp_file_name(temp_dir_, "menu-%3d-links.png", 1 + index));
169     std::cout << "INFO: Saving " << highlights_name << std::endl;
170     highlights->save(highlights_name, "png");
171
172     std::string spumux_name(
173         temp_file_name(temp_dir_, "menu-%3d.subpictures", 1 + index));
174     std::ofstream spumux_file(spumux_name.c_str());
175     spumux_file <<
176         "<subpictures>\n"
177         "  <stream>\n"
178         "    <spu force='yes' start='00:00:00.00'\n"
179         "        highlight='" << highlights_name << "'\n"
180         "        select='" << highlights_name << "'>\n";
181     int button_count = this_menu.entries.size();
182     for (int i = 0; i != button_count; ++i)
183     {
184         const menu_entry & this_entry = this_menu.entries[i];
185
186         // We program left and right to cycle through the buttons in
187         // the order the entries were added.  This should result in
188         // left and right behaving like the tab and shift-tab keys
189         // would in the browser.  Hopefully that's a sensible order.
190         // We program up and down to behave geometrically.
191         int up_button = i, down_button = i;
192         double up_closeness = 0.0, down_closeness = 0.0;
193         for (int j = 0; j != button_count; ++j)
194         {
195             const menu_entry & other_entry = this_menu.entries[j];
196             double closeness = directed_closeness(
197                 this_entry.area, other_entry.area, -1);
198             if (closeness > up_closeness)
199             {
200                 up_button = j;
201                 up_closeness = closeness;
202             }
203             else
204             {
205                 closeness = directed_closeness(
206                     this_entry.area, other_entry.area, 1);
207                 if (closeness > down_closeness)
208                 {
209                     down_button = j;
210                     down_closeness = closeness;
211                 }
212             }
213         }
214         // Pad vertically to even y coordinates since dvdauthor claims
215         // odd values may result in incorrect display.
216         // XXX This may cause overlappping where it wasn't previously
217         // a problem.
218         spumux_file << "      <button"
219             " x0='" << this_entry.area.left << "'"
220             " y0='" << (this_entry.area.top & ~1) << "'"
221             " x1='" << this_entry.area.right << "'"
222             " y1='" << ((this_entry.area.bottom + 1) & ~1) << "'"
223             " left='" << (i == 0 ? button_count : i) << "'"
224             " right='" << 1 + (i + 1) % button_count << "'"
225             " up='" << 1 + up_button << "'"
226             " down='" << 1 + down_button << "'"
227             "/>\n";
228     }
229     spumux_file <<
230         "    </spu>\n"
231         "  </stream>\n"
232         "</subpictures>\n";
233     spumux_file.close();
234     if (!spumux_file)
235         throw std::runtime_error("Failed to write control file for spumux");
236
237     std::ostringstream command_stream;
238     unsigned frame_count(menu_duration_frames(frame_params_));
239     if (encoder_ == mpeg_encoder_ffmpeg)
240     {
241         for (unsigned i = 0; i != frame_count; ++i)
242         {
243             std::string frame_name(background_name);
244             frame_name.push_back('-');
245             frame_name.push_back('0' + i / 10);
246             frame_name.push_back('0' + i % 10);
247             if (symlink(background_name.c_str(), frame_name.c_str()) != 0)
248                 throw std::runtime_error(
249                     std::string("symlink: ").append(std::strerror(errno)));
250         }
251         command_stream <<
252             "ffmpeg -f image2 -vcodec png"
253             " -r " << frame_params_.rate_numer <<
254             "/" << frame_params_.rate_denom <<
255             " -i " << background_name << "-%02d"
256             " -target " << frame_params_.common_name <<  "-dvd"
257             " -vcodec mpeg2video -aspect 4:3 -an -y /dev/stdout";
258     }
259     else
260     {
261         assert(encoder_ == mpeg_encoder_mjpegtools_old
262                || encoder_ == mpeg_encoder_mjpegtools_new);
263         command_stream
264             << "pngtopnm " << background_name
265             << " | ppmtoy4m -v0 -n" << frame_count << " -F"
266             << frame_params_.rate_numer << ":" << frame_params_.rate_denom
267             << " -A" << frame_params_.pixel_ratio_width
268             << ":" << frame_params_.pixel_ratio_height
269             << " -Ip ";
270         // The chroma subsampling keywords changed between
271         // versions 1.6.2 and 1.8 of mjpegtools.  There is no
272         // keyword that works with both.
273         if (encoder_ == mpeg_encoder_mjpegtools_old)
274             command_stream << "-S420_mpeg2";
275         else
276             command_stream << "-S420mpeg2";
277         command_stream <<
278             " | mpeg2enc -v0 -f8 -a2 -o/dev/stdout"
279             " | mplex -v0 -f8 -o/dev/stdout /dev/stdin";
280     }
281     command_stream
282         << " | spumux -v0 -mdvd " << spumux_name
283         << " > " << temp_file_name(temp_dir_, "menu-%3d.mpeg", 1 + index);
284     std::string command(command_stream.str());
285     const char * argv[] = {
286         "/bin/sh", "-c", command.c_str(), 0
287     };
288     std::cout << "INFO: Running " << command << std::endl;
289     int command_result;
290     Glib::spawn_sync(".",
291                      Glib::ArrayHandle<std::string>(
292                          argv, sizeof(argv)/sizeof(argv[0]),
293                          Glib::OWNERSHIP_NONE),
294                      Glib::SPAWN_STDOUT_TO_DEV_NULL,
295                      sigc::slot<void>(),
296                      0, 0,
297                      &command_result);
298     if (command_result != 0)
299         throw std::runtime_error("spumux pipeline failed");
300 }
301
302 dvd_generator::pgc_ref dvd_generator::add_title(vob_list & content)
303 {
304     pgc_ref next_title(title_pgc, titles_.size());
305
306     // Check against maximum number of titles.
307     if (next_title.index == dvd::titles_max)
308         throw_length_error("number of titles", dvd::titles_max);
309
310     titles_.resize(next_title.index + 1);
311     titles_[next_title.index].swap(content);
312     return next_title;
313 }
314
315 void dvd_generator::generate(const std::string & output_dir) const
316 {
317     // This function uses a mixture of 0-based and 1-based numbering,
318     // due to the differing conventions of the language and the DVD
319     // format.  Variable names ending in "_index" indicate 0-based
320     // indices and variable names ending in "_num" indicate 1-based
321     // numbers.
322
323     std::string name(temp_file_name(temp_dir_, "videolink.dvdauthor"));
324     std::ofstream file(name.c_str());
325     file << "<dvdauthor>\n";
326
327     // We generate code that uses registers in the following way:
328     //
329     // g0: Scratch.
330     // g1: Target location when jumping between menus.  Top 6 bits are
331     //     the button number (like s8) and bottom 10 bits are the menu
332     //     number.  This is used for selecting the appropriate button
333     //     when entering a menu, for completing indirect jumps between
334     //     domains, and for jumping to the correct menu after exiting a
335     //     title.  This is set to 0 in the pre-routine of the target
336     //     menu.
337     // g2: Current location in menus.  This is used for jumping to the
338     //     correct menu when the player exits a title.
339     // g3: Target chapter number plus 1 when jumping to a title.
340     //     This is used to jump to the correct chapter and to
341     //     distinguish between indirect jumps to menus and titles.
342     //     This is set to 0 in the pre-routine of the target title.
343     // g4: Source menu location used to jump to a title.  This is
344     //     compared with g2 to determine whether to increment the
345     //     button number if the title is played to the end.
346
347     static const unsigned button_mult = dvd::reg_s8_button_mult;
348     static const unsigned menu_mask = button_mult - 1;
349     static const unsigned button_mask = (1U << dvd::reg_bits) - button_mult;
350
351     // Iterate over VMGM and titlesets.  For these purposes, we
352     // consider the VMGM to be titleset 0.
353
354     // We need a titleset for each title, and we may also need titlesets to
355     // hold extra menus if we have too many for the VMGM.
356     // Also, we need at least one titleset.
357     const unsigned titleset_end = std::max<unsigned>(
358             1U + std::max<unsigned>(1U, titles_.size()),
359             (menus_.size() + dvdauthor_anonymous_menus_max - 1)
360                  / dvdauthor_anonymous_menus_max);
361
362     for (unsigned titleset_num = 0;
363          titleset_num != titleset_end;
364          ++titleset_num)
365     {
366         const char * const outer_element_name =
367             titleset_num == 0 ? "vmgm" : "titleset";
368         const bool have_real_title =
369             titleset_num != 0 && titleset_num <= titles_.size();
370         const bool have_real_menus =
371             titleset_num * dvdauthor_anonymous_menus_max < menus_.size();
372
373         file << "  <" << outer_element_name << ">\n"
374              << "    <menus>\n";
375
376         const unsigned menu_begin = titleset_num * dvdauthor_anonymous_menus_max;
377         const unsigned menu_end =
378             have_real_menus
379             ? std::min((titleset_num + 1) * dvdauthor_anonymous_menus_max,
380                        menus_.size())
381             : menu_begin + 1;
382
383         for (unsigned menu_index = menu_begin;
384              menu_index != menu_end;
385              ++menu_index)
386         {
387             // There are various cases in which menus may be called:
388             //
389             // 1. The user follows a direct link to the menu.
390             // 2. The user follows an indirect link to some other menu
391             //    and that goes via this menu.  This is distinguished
392             //    from case 1 by the value of g1.  We must jump to or
393             //    at least toward the other menu.
394             // 3. The title menu is called when the disc is first
395             //    played or the user presses the "top menu" button.
396             //    This is distinguished from cases 2 and 3 by g1 == 0.
397             //    We make this look like case 1.
398             // 4. The root menu of a titleset is called when the user
399             //    follows an indirect link to the title.  This is
400             //    distinguished from all other cases by g3 != 0.  We
401             //    must jump to the title.
402             // 5. The root menu of a titleset is called when the title
403             //    ends or the user presses the "menu" button during
404             //    the title.  This is distinguished from cases 1, 2
405             //    and 4 by g1 == 0 and g3 == 0.  We must jump to the
406             //    latest menu (which can turn into case 1 or 2).
407             //
408             // Cases 3 and 5 do not apply to the same menus so they
409             // do not need to be distinguished.
410
411             if (menu_index == 0)
412             {
413                 // Title menu.
414                 file <<
415                     "      <pgc entry='title'>\n"
416                     "        <pre>\n"
417                     "          if (g1 eq 0)\n" // case 3
418                     "            g1 = " << 1 + button_mult << ";\n";
419             }
420             else if (menu_index == titleset_num * dvdauthor_anonymous_menus_max)
421             {
422                 // Root menu.
423                 file <<
424                     "      <pgc entry='root'>\n"
425                     "        <pre>\n";
426                 if (have_real_title)
427                 {
428                     file <<
429                         "          if (g3 ne 0)\n" // case 4
430                         "            jump title 1;\n"
431                         "          if (g1 eq 0) {\n" // case 5
432                         "            g1 = g2;\n"
433                         "            jump vmgm menu entry title;\n"
434                         "          }\n";
435                 }
436             }
437             else
438             {
439                 // Some other menu.
440                 file <<
441                     "      <pgc>\n"
442                     "        <pre>\n";
443             }
444
445             if (!have_real_menus)
446             {
447                 // This is a root menu only reachable from the title.
448                 file <<
449                     "        </pre>\n"
450                     "      </pgc>\n";
451                 continue;
452             }
453
454             const menu & this_menu = menus_[menu_index];
455
456             // Detect and handle case 2.
457             //
458             // There is a limit of 128 VM instructions in each PGC.
459             // Also, we can't jump to an arbitrary menu in another
460             // domain.  Finally, we can't do computed jumps.
461             // Therefore we statically expand and distribute a binary
462             // search across the menus, resulting in a code size of
463             // O(log(menu_count)) in each menu.  In practice there are
464             // at most 11 conditional jumps needed in any menu.
465             //
466             // The initial bounds of the binary search are strange
467             // because we must ensure that any jump between titlesets
468             // is to the first menu of the titleset, marked as the
469             // root entry.
470
471             // Mask target location to get the target menu.
472             file << "          g0 = g1 &amp; " << menu_mask << ";\n";
473
474             for (unsigned
475                      bottom = 0,
476                      top = 16 * dvdauthor_anonymous_menus_max;
477                  top - bottom > 1;)
478             {
479                 unsigned middle = (bottom + top) / 2;
480                 if (menu_index == bottom && middle < menus_.size())
481                 {
482                     file << "          if (g0 ge " << 1 + middle << ")\n"
483                          << "            jump ";
484                     unsigned target_titleset_num =
485                         middle / dvdauthor_anonymous_menus_max;
486                     if (target_titleset_num != titleset_num)
487                     {
488                         assert(middle % dvdauthor_anonymous_menus_max == 0);
489                         file << "titleset " << target_titleset_num
490                              << " menu entry root";
491                     }
492                     else
493                     {
494                         file << "menu "
495                              << 1 + middle % dvdauthor_anonymous_menus_max;
496                     }
497                     file << ";\n";
498                 }
499                 if (menu_index >= middle)
500                     bottom = middle;
501                 else
502                     top = middle;
503             }
504
505             // Case 1.
506
507             // Highlight the appropriate button.
508             file << "          s8 = g1 &amp; " << button_mask << ";\n";
509
510             // Copy the target location to the current location and
511             // then clear the target location so that the title menu
512             // can distinguish cases 2 and 3.
513             file <<
514                 "          g2 = g1;\n"
515                 "          g1 = 0;\n";
516
517             file <<
518                 "        </pre>\n"
519                 "        <vob file='"
520                  << temp_file_name(temp_dir_, "menu-%3d.mpeg",
521                                    1 + menu_index)
522                  << "'>\n"
523                 // Define a cell covering the whole menu and set a still
524                 // time at the end of that, since it seems all players
525                 // support that but some ignore a still time set on a PGC.
526                 "          <cell start='0' end='"
527                  << std::fixed << std::setprecision(4)
528                  << menu_duration_seconds(frame_params_) << "'"
529                 " chapter='yes' pause='inf'/>\n"
530                 "        </vob>\n";
531
532             for (unsigned button_index = 0;
533                  button_index != this_menu.entries.size();
534                  ++button_index)
535             {
536                 const pgc_ref & target =
537                     this_menu.entries[button_index].target;
538
539                 file << "        <button> ";
540
541                 if (target.type == menu_pgc)
542                 {
543                     unsigned target_button_num;
544
545                     if (target.sub_index)
546                     {
547                         target_button_num = target.sub_index;
548                     }
549                     else
550                     {
551                         // Look for a button on the new menu that links
552                         // back to this one.  If there is one, set that to
553                         // be the highlighted button; otherwise, use the
554                         // first button.
555                         const std::vector<menu_entry> & target_menu_entries =
556                             menus_[target.index].entries;
557                         pgc_ref this_pgc(menu_pgc, menu_index);
558                         target_button_num = target_menu_entries.size();
559                         while (target_button_num != 1
560                                && (target_menu_entries[target_button_num - 1].target
561                                    != this_pgc))
562                             --target_button_num;
563                     }
564                          
565                     // Set new menu location.
566                     file << "g1 = "
567                          << (1 + target.index + target_button_num * button_mult)
568                          << "; ";
569                     // Jump to the target menu.
570                     unsigned target_titleset_num =
571                         target.index / dvdauthor_anonymous_menus_max;
572                     if (target_titleset_num == titleset_num)
573                         file << "jump menu "
574                              << 1 + (target.index
575                                      % dvdauthor_anonymous_menus_max)
576                              << "; ";
577                     else if (target_titleset_num == 0)
578                         file << "jump vmgm menu entry title; ";
579                     else
580                         file << "jump titleset " << target_titleset_num
581                              << " menu entry root; ";
582                 }
583                 else
584                 {
585                     assert(target.type == title_pgc);
586
587                     // Record current menu location and set target chapter
588                     // number.
589                     file <<
590                         "g2 = " << (1 + menu_index
591                                     + (1 + button_index) * button_mult) << "; "
592                         "g3 = " << 1 + target.sub_index << "; ";
593                     // Jump to the target title, possibly via its titleset's
594                     // root menu.
595                     unsigned target_titleset_num = 1 + target.index;
596                     if (titleset_num == 0)
597                         file << "jump title " << target_titleset_num << "; ";
598                     else if (target_titleset_num == titleset_num)
599                         file << "jump title 1; ";
600                     else
601                         file << "jump titleset " << target_titleset_num
602                              << " menu entry root; ";
603                 }
604
605                 file <<  "</button>\n";
606             }
607
608             file <<
609                 "      </pgc>\n";
610         }
611
612         file << "    </menus>\n";
613
614         if (have_real_title)
615         {
616             file <<
617                 "    <titles>\n"
618                 "      <pgc>\n";
619
620             file << "        <pre>\n";
621
622             // Count chapters in the title.
623             unsigned n_chapters = 0;
624             for (vob_list::const_iterator
625                      it = titles_[titleset_num - 1].begin(),
626                      end = titles_[titleset_num - 1].end();
627                  it != end;
628                  ++it)
629             {
630                 // Chapter start times may be specified in the "chapters"
631                 // attribute as a comma-separated list.  If this is not
632                 // specified then the beginning of each file starts a new
633                 // chapter.  Thus the number of chapters in each file is
634                 // the number of commas in the chapter attribute, plus 1.
635                 ++n_chapters;
636                 std::size_t pos = 0;
637                 while ((pos = it->chapters.find(',', pos)) != std::string::npos)
638                 {
639                     ++n_chapters;
640                     ++pos;
641                 }
642             }
643
644             // Move the chapter number to scratch so the root menu can
645             // distinguish cases 4 and 5.
646             file << "          g0 = g3; g3 = 0;\n";
647
648             // Copy the latest menu location for use by the post-routine.
649             file << "          g4 = g2;\n";
650
651             // Jump to the correct chapter.
652             for (unsigned chapter_num = 1;
653                  chapter_num <= n_chapters;
654                  ++chapter_num)
655                 file <<
656                     "          if (g0 eq " << 1 + chapter_num << ")\n"
657                     "            jump chapter " << chapter_num << ";\n";
658
659             file << "        </pre>\n";
660
661             for (vob_list::const_iterator
662                      it = titles_[titleset_num - 1].begin(),
663                      end = titles_[titleset_num - 1].end();
664                  it != end;
665                  ++it)
666             {
667                 file << "        <vob file='" << xml_escape(it->file) << "'";
668                 if (!it->chapters.empty())
669                     file << " chapters='" << xml_escape(it->chapters) << "'";
670                 if (!it->pause.empty())
671                     file << " pause='" << xml_escape(it->pause) << "'";
672                 file << "/>\n";
673             }
674
675             // If the user has not exited to the menus and then
676             // resumed the title, set the latest menu location to be
677             // the button after the one that linked to this title.
678             // In any case, return to the (root) menu which will
679             // then jump to the correct menu.
680             file <<
681                 "        <post>\n"
682                 "          if (g2 eq g4)\n"
683                 "            g2 = g2 + " << button_mult << ";\n"
684                 "          call menu;\n"
685                 "        </post>\n"
686                 "      </pgc>\n"
687                 "    </titles>\n";
688         }
689         else if (titleset_num != 0) // && !have_real_title
690         {
691             file << "    <titles><pgc/></titles>\n";
692         }
693
694         file << "  </" << outer_element_name << ">\n";
695     }
696
697     file << "</dvdauthor>\n";
698     file.close();
699
700     {
701         const char * argv[] = {
702             "dvdauthor",
703             "-o", output_dir.c_str(),
704             "-x", name.c_str(),
705             0
706         };
707         int command_result;
708         Glib::spawn_sync(".",
709                          Glib::ArrayHandle<std::string>(
710                              argv, sizeof(argv)/sizeof(argv[0]),
711                              Glib::OWNERSHIP_NONE),
712                          Glib::SPAWN_SEARCH_PATH
713                          | Glib::SPAWN_STDOUT_TO_DEV_NULL,
714                          sigc::slot<void>(),
715                          0, 0,
716                          &command_result);
717         if (command_result != 0)
718             throw std::runtime_error("dvdauthor failed");
719     }
720 }