1 // Copyright 2005-6 Ben Hutchings <ben@decadentplace.org.uk>.
2 // See the file "COPYING" for licence details.
7 #include <glibmm/spawn.h>
10 #include "generate_dvd.hpp"
12 dvd_contents::menu::menu()
13 : vob_temp(new temp_file("webdvd-vob-"))
18 void generate_dvd(const dvd_contents & contents,
19 const std::string & output_dir)
21 temp_file temp("webdvd-dvdauthor-");
23 std::ofstream file(temp.get_name().c_str());
25 // We generate code that uses registers in the following way:
28 // g1: current location
29 // g12: location that last jumped to a video
31 // All locations are divided into two bitfields: the least
32 // significant 10 bits are a page/menu number and the most
33 // significant 6 bits are a link/button number, and numbering
34 // starts at 1, not 0. This is done for compatibility with
35 // the encoding of the s8 (button) register.
37 static const int button_mult = dvd::reg_s8_button_mult;
38 static const int menu_mask = button_mult - 1;
39 static const int button_mask = (1 << dvd::reg_bits) - button_mult;
46 for (unsigned menu_num = 0;
47 menu_num != contents.menus.size();
50 const dvd_contents::menu & menu = contents.menus[menu_num];
54 // This is the first (title) menu, displayed when the
55 // disc is first played.
57 " <pgc entry='title'>\n"
59 // Initialise the current location if it is not set
60 // (all general registers are initially 0).
62 " g1 = " << 1 + button_mult << ";\n";
71 // When a title finishes or the user presses the menu
72 // button, this always jumps to the titleset's root menu.
73 // We want to return the user to the last menu they used.
74 // So we arrange for each titleset's root menu to return
75 // to the vmgm title menu and then dispatch from there to
76 // whatever the correct menu is. We determine the correct
77 // menu by looking at the menu part of g1.
79 file << " g0 = g1 & " << menu_mask << ";\n";
81 // There is a limit of 128 VM instructions in each PGC.
82 // Therefore in each menu's <pre> section we generate
83 // jumps to menus with numbers greater by 512, 256, 128,
84 // ..., 1 where (a) such a menu exists, (b) this menu
85 // number is divisible by twice that increment and (c) the
86 // correct menu is that or a later menu. Thus each menu
87 // has at most 10 such conditional jumps and is reachable
88 // by at most 10 jumps from the title menu. This chain of
89 // jumps might take too long on some players; this has yet
90 // to be investigated.
92 for (std::size_t menu_incr = (menu_mask + 1) / 2;
96 if (menu_num + menu_incr < contents.menus.size()
97 && (menu_num & (menu_incr * 2 - 1)) == 0)
100 " if (g0 ge " << 1 + menu_num + menu_incr
102 " jump menu " << 1 + menu_num + menu_incr
108 // Highlight the appropriate button.
109 " s8 = g1 & " << button_mask << ";\n"
111 " <vob file='" << menu.vob_temp->get_name() << "'/>\n";
113 for (unsigned button_num = 0;
114 button_num != menu.entries.size();
117 const dvd_contents::pgc_ref & target =
118 menu.entries[button_num];
120 file << " <button> ";
122 if (target.type == dvd_contents::menu_pgc)
124 unsigned target_button_num;
126 if (target.sub_index)
128 target_button_num = target.sub_index;
132 // Look for a button on the new menu that links
133 // back to this one. If there is one, set that to
134 // be the highlighted button; otherwise, use the
136 const std::vector<dvd_contents::pgc_ref> &
137 target_menu_entries =
138 contents.menus[target.index].entries;
139 dvd_contents::pgc_ref this_pgc(dvd_contents::menu_pgc,
141 target_button_num = target_menu_entries.size();
142 while (target_button_num != 0
143 && (target_menu_entries[--target_button_num]
146 target_button_num += 1;
151 + target_button_num * button_mult)
152 << "; jump menu " << 1 + target.index << ";";
156 assert(target.type == dvd_contents::title_pgc);
159 << 1 + menu_num + (1 + button_num) * button_mult
162 if (target.sub_index)
163 file << " chapter " << target.sub_index;
167 file << " </button>\n";
177 // Generate a titleset for each title. This appears to make
178 // jumping to titles a whole lot simpler (but limits us to 99
180 for (unsigned title_num = 0;
181 title_num != contents.titles.size();
186 // Generate a dummy menu so that the menu button on the
187 // remote control will work.
189 " <pgc entry='root'>\n"
190 " <pre> jump vmgm menu; </pre>\n"
195 // Record calling location.
196 " <pre> g12 = g1; </pre>\n"
197 << contents.titles[title_num].vob_list <<
198 // If the menu location has not been changed during
199 // the title, set the location to be the following
200 // button in the menu. In any case, return to some
202 " <post> if (g1 eq g12) g1 = g1 + " << button_mult
203 << "; call menu; </post>\n"
215 const char * argv[] = {
217 "-o", output_dir.c_str(),
218 "-x", temp.get_name().c_str(),
222 Glib::spawn_sync(".",
223 Glib::ArrayHandle<std::string>(
224 argv, sizeof(argv)/sizeof(argv[0]),
225 Glib::OWNERSHIP_NONE),
226 Glib::SPAWN_SEARCH_PATH
227 | Glib::SPAWN_STDOUT_TO_DEV_NULL,
231 if (command_result != 0)
232 throw std::runtime_error("dvdauthor failed");