Debian Codegraph @

src/vm/getset.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 #include <inttypes.h>
32
33 #include <dvdread/nav_types.h>
34 #include <dvdread/ifo_read.h>
35 #include "dvdnav/dvdnav.h"
36
37 #include "decoder.h"
38 #include "vm.h"
39 #include "getset.h"
40 #include "dvdnav_internal.h"
41 #include "logger.h"
42
43 #include "getset.h"
44 /* Set functions */
45
46 int set_TT(vm_t *vm, int tt) {
47 return set_PTT(vm, tt, 1);
48 }
49
50 int set_PTT(vm_t *vm, int tt, int ptt) {
51 assert(tt <= vm->vmgi->tt_srpt->nr_of_srpts);
52 return set_VTS_PTT(vm, vm->vmgi->tt_srpt->title[tt - 1].title_set_nr,
53 vm->vmgi->tt_srpt->title[tt - 1].vts_ttn, ptt);
54 }
55
56 int set_VTS_TT(vm_t *vm, int vtsN, int vts_ttn) {
57 return set_VTS_PTT(vm, vtsN, vts_ttn, 1);
58 }
59
60 int set_VTS_PTT(vm_t *vm, int vtsN, int vts_ttn, int part) {
61 int pgcN, pgN, res;
62
63 (vm->state).domain = DVD_DOMAIN_VTSTitle;
64
65 if (vtsN != (vm->state).vtsN)
66 if (!ifoOpenNewVTSI(vm, vm->dvd, vtsN)) /* Also sets (vm->state).vtsN */
67 return 0;
68
69 if ((vts_ttn < 1) || (vts_ttn > vm->vtsi->vts_ptt_srpt->nr_of_srpts) ||
70 (part < 1) || (part > vm->vtsi->vts_ptt_srpt->title[vts_ttn - 1].nr_of_ptts) ) {
71 return 0;
72 }
73
74 pgcN = vm->vtsi->vts_ptt_srpt->title[vts_ttn - 1].ptt[part - 1].pgcn;
75 pgN = vm->vtsi->vts_ptt_srpt->title[vts_ttn - 1].ptt[part - 1].pgn;
76
77 (vm->state).TT_PGCN_REG = pgcN;
78 (vm->state).PTTN_REG = part;
79 (vm->state).TTN_REG = get_TT(vm, vtsN, vts_ttn);
80 if( (vm->state.TTN_REG) == 0 )
81 return 0;
82
83 (vm->state).VTS_TTN_REG = vts_ttn;
84 (vm->state).vtsN = vtsN; /* Not sure about this one. We can get to it easily from TTN_REG */
85 /* Any other registers? */
86
87 res = set_PGCN(vm, pgcN); /* This clobber's state.pgN (sets it to 1), but we don't want clobbering here. */
88 (vm->state).pgN = pgN;
89 return res;
90 }
91
92 int set_PROG(vm_t *vm, int tt, int pgcn, int pgn) {
93 assert(tt <= vm->vmgi->tt_srpt->nr_of_srpts);
94 return set_VTS_PROG(vm, vm->vmgi->tt_srpt->title[tt - 1].title_set_nr,
95 vm->vmgi->tt_srpt->title[tt - 1].vts_ttn, pgcn, pgn);
96 }
97
98 int set_VTS_PROG(vm_t *vm, int vtsN, int vts_ttn, int pgcn, int pgn) {
99 int pgcN, pgN, res, title, part = 0;
100
101 (vm->state).domain = DVD_DOMAIN_VTSTitle;
102
103 if (vtsN != (vm->state).vtsN)
104 if (!ifoOpenNewVTSI(vm, vm->dvd, vtsN)) /* Also sets (vm->state).vtsN */
105 return 0;
106
107 if ((vts_ttn < 1) || (vts_ttn > vm->vtsi->vts_ptt_srpt->nr_of_srpts)) {
108 return 0;
109 }
110
111 pgcN = pgcn;
112 pgN = pgn;
113
114 (vm->state).TT_PGCN_REG = pgcN;
115 (vm->state).TTN_REG = get_TT(vm, vtsN, vts_ttn);
116 assert( (vm->state.TTN_REG) != 0 );
117 (vm->state).VTS_TTN_REG = vts_ttn;
118 (vm->state).vtsN = vtsN; /* Not sure about this one. We can get to it easily from TTN_REG */
119 /* Any other registers? */
120
121 res = set_PGCN(vm, pgcN); /* This clobber's state.pgN (sets it to 1), but we don't want clobbering here. */
122 (vm->state).pgN = pgN;
123 vm_get_current_title_part(vm, &title, &part);
124 (vm->state).PTTN_REG = part;
125 return res;
126 }
127
128 int set_FP_PGC(vm_t *vm) {
129 if (!vm || !vm->vmgi)
130 return 1;
131 (vm->state).domain = DVD_DOMAIN_FirstPlay;
132 if (!vm->vmgi->first_play_pgc) {
133 return set_PGCN(vm, 1);
134 }
135 (vm->state).pgc = vm->vmgi->first_play_pgc;
136 (vm->state).pgcN = vm->vmgi->vmgi_mat->first_play_pgc;
137 return 1;
138 }
139
140
141 int set_MENU(vm_t *vm, int menu) {
142 assert((vm->state).domain == DVD_DOMAIN_VMGM || (vm->state).domain == DVD_DOMAIN_VTSMenu);
143 return set_PGCN(vm, get_ID(vm, menu));
144 }
145
146 int set_PGCN(vm_t *vm, int pgcN) {
147 const pgcit_t *pgcit;
148
149 pgcit = get_PGCIT(vm);
150 if (pgcit == NULL)
151 return 0;
152
153 if(pgcN < 1 || pgcN > pgcit->nr_of_pgci_srp) {
154 #ifdef TRACE
155 Log3(vm, "** No such pgcN = %d", pgcN);
156 #endif
157 return 0;
158 }
159
160 (vm->state).pgc = pgcit->pgci_srp[pgcN - 1].pgc;
161 (vm->state).pgcN = pgcN;
162 (vm->state).pgN = 1;
163
164 if((vm->state).domain == DVD_DOMAIN_VTSTitle)
165 (vm->state).TT_PGCN_REG = pgcN;
166
167 return 1;
168 }
169
170 /* Figure out the correct pgN from the cell and update (vm->state). */
171 int set_PGN(vm_t *vm) {
172 int new_pgN = 0;
173 int dummy, part = 0;
174
175 if ((vm->state).pgc == NULL)
176 return 0;
177
178 while(new_pgN < (vm->state).pgc->nr_of_programs
179 && (vm->state).cellN >= (vm->state).pgc->program_map[new_pgN])
180 new_pgN++;
181
182 if(new_pgN == (vm->state).pgc->nr_of_programs) /* We are at the last program */
183 if((vm->state).cellN > (vm->state).pgc->nr_of_cells)
184 return 0; /* We are past the last cell */
185
186 (vm->state).pgN = new_pgN;
187
188 if((vm->state).domain == DVD_DOMAIN_VTSTitle) {
189 if((vm->state).TTN_REG > vm->vmgi->tt_srpt->nr_of_srpts)
190 return 0; /* ?? */
191
192 vm_get_current_title_part(vm, &dummy, &part);
193 (vm->state).PTTN_REG = part;
194 }
195 return 1;
196 }
197
198 /* Must be called before domain is changed (set_PGCN()) */
199 void set_RSMinfo(vm_t *vm, int cellN, int blockN) {
200 int i;
201
202 if(cellN) {
203 (vm->state).rsm_cellN = cellN;
204 (vm->state).rsm_blockN = blockN;
205 } else {
206 (vm->state).rsm_cellN = (vm->state).cellN;
207 (vm->state).rsm_blockN = blockN;
208 }
209 (vm->state).rsm_vtsN = (vm->state).vtsN;
210 (vm->state).rsm_pgcN = get_PGCN(vm);
211
212 /* assert((vm->state).rsm_pgcN == (vm->state).TT_PGCN_REG); for DVD_DOMAIN_VTSTitle */
213
214 for(i = 0; i < 5; i++) {
215 (vm->state).rsm_regs[i] = (vm->state).registers.SPRM[4 + i];
216 }
217 }
218
219
220 /* Force the highlight button number register */
221 void set_HL_BTN(vm_t *vm, int btnn)
222 {
223 (vm->state).HL_BTNN_REG = btnn << 10;
224 }
225
226 /* Get functions */
227
228 /* Searches the TT tables, to find the current TT.
229 * returns the current TT.
230 * returns 0 if not found.
231 */
232 int get_TT(vm_t *vm, int vtsN, int vts_ttn) {
233 int i;
234 int tt=0;
235
236 for(i = 1; i <= vm->vmgi->tt_srpt->nr_of_srpts; i++) {
237 if( vm->vmgi->tt_srpt->title[i - 1].title_set_nr == vtsN &&
238 vm->vmgi->tt_srpt->title[i - 1].vts_ttn == vts_ttn) {
239 tt=i;
240 break;
241 }
242 }
243 return tt;
244 }
245
246 /* Search for entry_id match of the PGC Category in the current VTS PGCIT table.
247 * Return pgcN based on entry_id match.
248 */
249 int get_ID(vm_t *vm, int id) {
250 int pgcN, i;
251 const pgcit_t *pgcit;
252
253 /* Relies on state to get the correct pgcit. */
254 pgcit = get_PGCIT(vm);
255 if(pgcit == NULL) {
256 Log1(vm, "PGCIT null!");
257 return 0;
258 }
259 #ifdef TRACE
260 Log3(vm, "** Searching for menu (0x%x) entry PGC", id);
261 #endif
262
263 /* Force high bit set. */
264 id |=0x80;
265
266 /* Get menu/title */
267 for(i = 0; i < pgcit->nr_of_pgci_srp; i++) {
268 if( (pgcit->pgci_srp[i].entry_id) == id) {
269 pgcN = i + 1;
270 #ifdef TRACE
271 Log3(vm, "Found menu.");
272 #endif
273 return pgcN;
274 }
275 }
276 #ifdef TRACE
277 Log3(vm, "** No such id/menu (0x%02x) entry PGC", id & 0x7f);
278 for(i = 0; i < pgcit->nr_of_pgci_srp; i++) {
279 if ( (pgcit->pgci_srp[i].entry_id & 0x80) == 0x80) {
280 Log3(vm, "Available menus: 0x%x",
281 pgcit->pgci_srp[i].entry_id & 0x7f);
282 }
283 }
284 #endif
285 return 0; /* error */
286 }
287
288 /* FIXME: we have a pgcN member in the vm's state now, so this should be obsolete */
289 int get_PGCN(vm_t *vm) {
290 const pgcit_t *pgcit;
291 int pgcN = 1;
292
293 if ((vm->state).pgc == NULL) {
294 return 0; /* error */
295 }
296
297 pgcit = get_PGCIT(vm);
298
299 if (pgcit) {
300 while(pgcN <= pgcit->nr_of_pgci_srp) {
301 if(pgcit->pgci_srp[pgcN - 1].pgc == (vm->state).pgc) {
302 assert((vm->state).pgcN == pgcN);
303 return pgcN;
304 }
305 pgcN++;
306 }
307 }
308 Log0(vm, "get_PGCN failed. Was trying to find pgcN in domain %d",
309 (vm->state).domain);
310 return 0; /* error */
311 }
312
313 const pgcit_t* get_MENU_PGCIT(vm_t *vm, ifo_handle_t *h, uint16_t lang) {
314 int i;
315
316 if(h == NULL || h->pgci_ut == NULL) {
317 Log0(vm, "*** pgci_ut handle is NULL ***");
318 return NULL; /* error? */
319 }
320
321 i = 0;
322 while(i < h->pgci_ut->nr_of_lus
323 && h->pgci_ut->lu[i].lang_code != lang)
324 i++;
325 if(i == h->pgci_ut->nr_of_lus) {
326 Log1(vm, "Language '%c%c' not found, using '%c%c' instead",
327 (char)(lang >> 8), (char)(lang & 0xff),
328 (char)(h->pgci_ut->lu[0].lang_code >> 8),
329 (char)(h->pgci_ut->lu[0].lang_code & 0xff));
330 char *buffer = malloc(3 * h->pgci_ut->nr_of_lus + 1);
331 if(buffer)
332 {
333 buffer[3 * h->pgci_ut->nr_of_lus] = 0;
334 for(i = 0; i < h->pgci_ut->nr_of_lus; i++) {
335 sprintf(&buffer[3*i], "%c%c ",
336 (char)(h->pgci_ut->lu[i].lang_code >> 8),
337 (char)(h->pgci_ut->lu[i].lang_code & 0xff));
338 }
339 Log2(vm, "Menu Languages available: %s", buffer);
340 free(buffer);
341 }
342 i = 0; /* error? */
343 }
344
345 return h->pgci_ut->lu[i].pgcit;
346 }
347
348 /* Uses state to decide what to return */
349 const pgcit_t* get_PGCIT(vm_t *vm) {
350 const pgcit_t *pgcit = NULL;
351
352 switch ((vm->state).domain) {
353 case DVD_DOMAIN_VTSTitle:
354 if(!vm->vtsi) return NULL;
355 pgcit = vm->vtsi->vts_pgcit;
356 break;
357 case DVD_DOMAIN_VTSMenu:
358 if(!vm->vtsi) return NULL;
359 pgcit = get_MENU_PGCIT(vm, vm->vtsi, (vm->state).registers.SPRM[0]);
360 break;
361 case DVD_DOMAIN_VMGM:
362 case DVD_DOMAIN_FirstPlay:
363 pgcit = get_MENU_PGCIT(vm, vm->vmgi, (vm->state).registers.SPRM[0]);
364 break;
365 default:
366 abort();
367 }
368
369 return pgcit;
370 }
371