Debian Codegraph @

src/vm/play.c

raw
1 /*
2 * Copyright (C) 2000, 2001 Håkan Hjort
3 * Copyright (C) 2001 Rich Wareham <[email protected]>
4 * 2002-2004 the dvdnav project
5 *
6 * This file is part of libdvdnav, a DVD navigation library. It is modified
7 * from a file originally part of the Ogle DVD player.
8 *
9 * libdvdnav is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * libdvdnav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with libdvdnav; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include <dvdread/nav_types.h>
33 #include <dvdread/ifo_read.h>
34 #include "dvdnav/dvdnav.h"
35
36 #include "decoder.h"
37 #include "vm.h"
38 #include "play.h"
39 #include "vm/getset.h"
40
41 #include "dvdnav_internal.h"
42 #include "logger.h"
43
44 /* Playback control */
45
46 link_t play_PGC(vm_t *vm) {
47 link_t link_values;
48
49 #ifdef TRACE
50 if((vm->state).domain != DVD_DOMAIN_FirstPlay) {
51 Log3(vm, "play_PGC: (vm->state).pgcN (%i)", get_PGCN(vm));
52 } else {
53 Log3(vm, "play_PGC: first_play_pgc");
54 }
55 #endif
56
57 /* This must be set before the pre-commands are executed because they
58 * might contain a CallSS that will save resume state */
59
60 /* FIXME: This may be only a temporary fix for something... */
61 (vm->state).pgN = 1;
62 (vm->state).cellN = 0;
63 (vm->state).blockN = 0;
64
65 /* Handle random playback mode by choosing our initial program
66 * number randomly - some discs end up jumping to nowhere if you
67 * always choose the first cell.
68 */
69 if ((vm->state).pgc->pg_playback_mode!=0 &&
70 ((vm->state).pgc->pg_playback_mode & 0x80)==0) {
71 int pgCnt = ((vm->state).pgc->pg_playback_mode & 0x7f) + 1;
72 if (pgCnt > (vm->state).pgc->nr_of_programs) {
73 pgCnt = (vm->state).pgc->nr_of_programs;
74 }
75 if (pgCnt>1) {
76 (vm->state).pgN = 1 + ((int) ((float) pgCnt * rand()/(RAND_MAX+1.0)));
77 }
78 }
79
80 /* eval -> updates the state and returns either
81 - some kind of jump (Jump(TT/SS/VTS_TTN/CallSS/link C/PG/PGC/PTTN)
82 - just play video i.e first PG
83 (This is what happens if you fall of the end of the pre_cmds)
84 - or an error (are there more cases?) */
85 if((vm->state).pgc->command_tbl && (vm->state).pgc->command_tbl->nr_of_pre) {
86 if(vmEval_CMD((vm->state).pgc->command_tbl->pre_cmds,
87 (vm->state).pgc->command_tbl->nr_of_pre,
88 &(vm->state).registers, &link_values)) {
89 /* link_values contains the 'jump' return value */
90 return link_values;
91 } else {
92 #ifdef TRACE
93 Log3(vm, "PGC pre commands didn't do a Jump, Link or Call");
94 #endif
95 }
96 }
97 return play_PG(vm);
98 }
99
100 link_t play_PGC_PG(vm_t *vm, int pgN) {
101 link_t link_values;
102
103 #ifdef TRACE
104 if((vm->state).domain != DVD_DOMAIN_FirstPlay) {
105 Log3(vm, "play_PGC_PG: (vm->state).pgcN (%i)", get_PGCN(vm));
106 } else {
107 Log3(vm, "play_PGC_PG: first_play_pgc");
108 }
109 #endif
110
111 /* This must be set before the pre-commands are executed because they
112 * might contain a CallSS that will save resume state */
113
114 /* FIXME: This may be only a temporary fix for something... */
115 (vm->state).pgN = pgN;
116 (vm->state).cellN = 0;
117 (vm->state).blockN = 0;
118
119 /* eval -> updates the state and returns either
120 - some kind of jump (Jump(TT/SS/VTS_TTN/CallSS/link C/PG/PGC/PTTN)
121 - just play video i.e first PG
122 (This is what happens if you fall of the end of the pre_cmds)
123 - or an error (are there more cases?) */
124 if((vm->state).pgc->command_tbl && (vm->state).pgc->command_tbl->nr_of_pre) {
125 if(vmEval_CMD((vm->state).pgc->command_tbl->pre_cmds,
126 (vm->state).pgc->command_tbl->nr_of_pre,
127 &(vm->state).registers, &link_values)) {
128 /* link_values contains the 'jump' return value */
129 return link_values;
130 } else {
131 #ifdef TRACE
132 Log3(vm, "PGC pre commands didn't do a Jump, Link or Call");
133 #endif
134 }
135 }
136 return play_PG(vm);
137 }
138
139 link_t play_PGC_post(vm_t *vm) {
140 link_t link_values = { LinkNoLink, 0, 0, 0 };
141
142 #ifdef TRACE
143 Log3(vm, "play_PGC_post:");
144 #endif
145
146 /* eval -> updates the state and returns either
147 - some kind of jump (Jump(TT/SS/VTS_TTN/CallSS/link C/PG/PGC/PTTN)
148 - just go to next PGC
149 (This is what happens if you fall of the end of the post_cmds)
150 - or an error (are there more cases?) */
151 if((vm->state).pgc->command_tbl && (vm->state).pgc->command_tbl->nr_of_post &&
152 vmEval_CMD((vm->state).pgc->command_tbl->post_cmds,
153 (vm->state).pgc->command_tbl->nr_of_post,
154 &(vm->state).registers, &link_values)) {
155 return link_values;
156 }
157
158 #ifdef TRACE
159 Log3(vm, "** Fell of the end of the pgc, continuing in NextPGC");
160 #endif
161 /* Should end up in the STOP_DOMAIN if next_pgc is 0. */
162 if(!set_PGCN(vm, (vm->state).pgc->next_pgc_nr)) {
163 link_values.command = Exit;
164 return link_values;
165 }
166 return play_PGC(vm);
167 }
168
169 link_t play_PG(vm_t *vm) {
170 #ifdef TRACE
171 Log3(vm, "play_PG: (vm->state).pgN (%i)", (vm->state).pgN);
172 #endif
173
174 assert((vm->state).pgN > 0);
175 if((vm->state).pgN > (vm->state).pgc->nr_of_programs) {
176 #ifdef TRACE
177 Log3(vm, "play_PG: (vm->state).pgN (%i) > pgc->nr_of_programs (%i)",
178 (vm->state).pgN, (vm->state).pgc->nr_of_programs );
179 #endif
180 if((vm->state).pgN > (vm->state).pgc->nr_of_programs + 1) {
181 /* bogus link, ignore it */
182 link_t link_values = { LinkNoLink, 0, 0, 0 };
183 return link_values;
184 }
185 return play_PGC_post(vm);
186 }
187
188 (vm->state).cellN = (vm->state).pgc->program_map[(vm->state).pgN - 1];
189
190 return play_Cell(vm);
191 }
192
193 link_t play_Cell(vm_t *vm) {
194 static const link_t play_this = {PlayThis, /* Block in Cell */ 0, 0, 0};
195
196 #ifdef TRACE
197 Log3(vm, "play_Cell: (vm->state).cellN (%i)", (vm->state).cellN);
198 #endif
199
200 assert((vm->state).cellN > 0);
201 if((vm->state).cellN > (vm->state).pgc->nr_of_cells) {
202 #ifdef TRACE
203 Log3(vm, "(vm->state).cellN (%i) > pgc->nr_of_cells (%i)",
204 (vm->state).cellN, (vm->state).pgc->nr_of_cells );
205 #endif
206 if((vm->state).cellN > (vm->state).pgc->nr_of_cells + 1) {
207 /* bogus link, ignore it */
208 link_t link_values = { LinkNoLink, 0, 0, 0 };
209 return link_values;
210 }
211 return play_PGC_post(vm);
212 }
213
214 /* Multi angle/Interleaved */
215 switch((vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_mode) {
216 case 0: /* Normal */
217 assert((vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_type == 0);
218 break;
219 case 1: /* The first cell in the block */
220 switch((vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_type) {
221 case 0: /* Not part of a block */
222 assert(0);
223 break;
224 case 1: /* Angle block */
225 /* Loop and check each cell instead? So we don't get outside the block? */
226 (vm->state).cellN += (vm->state).AGL_REG - 1;
227 #ifdef DVDNAV_STRICT
228 assert((vm->state).cellN <= (vm->state).pgc->nr_of_cells);
229 assert((vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_mode != 0);
230 assert((vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_type == 1);
231 #else
232 if (!((vm->state).cellN <= (vm->state).pgc->nr_of_cells) ||
233 !((vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_mode != 0) ||
234 !((vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_type == 1)) {
235 Log1(vm, "Invalid angle block");
236 (vm->state).cellN -= (vm->state).AGL_REG - 1;
237 }
238 #endif
239 break;
240 case 2: /* reserved */
241 case 3: /* reserved */
242 default:
243 Log1(vm, "Invalid? Cell block_mode (%d), block_type (%d)",
244 (vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_mode,
245 (vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_type);
246 assert(0);
247 }
248 break;
249 case 2: /* Cell in the block */
250 case 3: /* Last cell in the block */
251 /* These might perhaps happen for RSM or LinkC commands? */
252 default:
253 Log1(vm, "Cell is in block but did not enter at first cell!");
254 }
255
256 /* Updates (vm->state).pgN and PTTN_REG */
257 if(!set_PGN(vm)) {
258 /* Should not happen */
259 assert(0);
260 return play_PGC_post(vm);
261 }
262 (vm->state).cell_restart++;
263 (vm->state).blockN = 0;
264 #ifdef TRACE
265 Log3(vm, "Cell should restart here");
266 #endif
267 return play_this;
268 }
269
270 link_t play_Cell_post(vm_t *vm) {
271 const cell_playback_t *cell;
272
273 #ifdef TRACE
274 Log3(vm, "play_Cell_post: (vm->state).cellN (%i)", (vm->state).cellN);
275 #endif
276
277 cell = &(vm->state).pgc->cell_playback[(vm->state).cellN - 1];
278
279 /* Still time is already taken care of before we get called. */
280
281 /* Deal with a Cell command, if any */
282 if(cell->cell_cmd_nr != 0) {
283 link_t link_values;
284
285 if ((vm->state).pgc->command_tbl != NULL &&
286 (vm->state).pgc->command_tbl->nr_of_cell >= cell->cell_cmd_nr) {
287 #ifdef TRACE
288 Log3(vm, "Cell command present, executing");
289 #endif
290 if(vmEval_CMD(&(vm->state).pgc->command_tbl->cell_cmds[cell->cell_cmd_nr - 1], 1,
291 &(vm->state).registers, &link_values)) {
292 return link_values;
293 } else {
294 #ifdef TRACE
295 Log3(vm, "Cell command didn't do a Jump, Link or Call");
296 #endif
297 }
298 } else {
299 #ifdef TRACE
300 Log3(vm, "Invalid Cell command");
301 #endif
302 }
303 }
304
305 /* Where to continue after playing the cell... */
306 /* Multi angle/Interleaved */
307 switch((vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_mode) {
308 case 0: /* Normal */
309 assert((vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_type == 0);
310 (vm->state).cellN++;
311 break;
312 case 1: /* The first cell in the block */
313 case 2: /* A cell in the block */
314 case 3: /* The last cell in the block */
315 default:
316 switch((vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_type) {
317 case 0: /* Not part of a block */
318 assert(0);
319 break;
320 case 1: /* Angle block */
321 /* Skip the 'other' angles */
322 (vm->state).cellN++;
323 while((vm->state).cellN <= (vm->state).pgc->nr_of_cells &&
324 (vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_mode >= 2) {
325 (vm->state).cellN++;
326 }
327 break;
328 case 2: /* reserved */
329 case 3: /* reserved */
330 default:
331 Log1(vm, "Invalid? Cell block_mode (%d), block_type (%d)",
332 (vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_mode,
333 (vm->state).pgc->cell_playback[(vm->state).cellN - 1].block_type);
334 assert(0);
335 }
336 break;
337 }
338
339 /* Figure out the correct pgN for the new cell */
340 if(!set_PGN(vm)) {
341 #ifdef TRACE
342 Log3(vm, "last cell in this PGC");
343 #endif
344 return play_PGC_post(vm);
345 }
346 return play_Cell(vm);
347 }