Debian Codegraph @

src/dvdnav.c

raw
1 /*
2 * Copyright (C) 2000 Rich Wareham <[email protected]>
3 *
4 * This file is part of libdvdnav, a DVD navigation library.
5 *
6 * libdvdnav is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * libdvdnav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with libdvdnav; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 /* Un-comment the following to enable additional Log3() function calls.
26 * Do not forget to #define TRACE in src/vm/vm.h (required) */
27 /*
28 #define LOG_DEBUG
29 */
30
31 #include <inttypes.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <limits.h>
36 #include <string.h>
37 #include <sys/time.h>
38 #include "dvdnav/dvdnav.h"
39 #include <dvdread/dvd_reader.h>
40 #include <dvdread/nav_types.h>
41 #include "vm/decoder.h"
42 #include "vm/vm.h"
43 #include "vm/getset.h"
44 #include "dvdnav_internal.h"
45 #include "logger.h"
46 #include "read_cache.h"
47 #include <dvdread/nav_read.h>
48
49 static dvdnav_status_t dvdnav_clear(dvdnav_t * this) {
50 /* clear everything except file, vm, mutex, readahead */
51
52 pthread_mutex_lock(&this->vm_lock);
53 if (this->file) DVDCloseFile(this->file);
54 this->file = NULL;
55
56 memset(&this->position_current,0,sizeof(this->position_current));
57 memset(&this->pci,0,sizeof(this->pci));
58 memset(&this->dsi,0,sizeof(this->dsi));
59 this->last_cmd_nav_lbn = SRI_END_OF_CELL;
60
61 /* Set initial values of flags */
62 this->skip_still = 0;
63 this->sync_wait = 0;
64 this->sync_wait_skip = 0;
65 this->spu_clut_changed = 0;
66 this->started = 0;
67 this->cur_cell_time = 0;
68
69 dvdnav_read_cache_clear(this->cache);
70 pthread_mutex_unlock(&this->vm_lock);
71
72 return DVDNAV_STATUS_OK;
73 }
74
75 dvdnav_status_t dvdnav_dup(dvdnav_t **dest, dvdnav_t *src) {
76 dvdnav_t *this;
77
78 (*dest) = NULL;
79 this = (dvdnav_t*)malloc(sizeof(dvdnav_t));
80 if (!this)
81 return DVDNAV_STATUS_ERR;
82
83 memcpy(this, src, sizeof(dvdnav_t));
84 this->file = NULL;
85 this->vm = NULL;
86 this->path = NULL;
87 this->cache = NULL;
88
89 pthread_mutex_init(&this->vm_lock, NULL);
90
91 this->vm = vm_new_copy(src->vm);
92 if (!this->vm)
93 goto fail;
94
95 this->path = strdup(src->path);
96 if (!this->path)
97 goto fail;
98
99 /* Start the read-ahead cache. */
100 this->cache = dvdnav_read_cache_new(this);
101 if (!this->cache)
102 goto fail;
103
104 (*dest) = this;
105 return DVDNAV_STATUS_OK;
106
107 fail:
108 printerr("Error initialising the DVD VM.");
109 pthread_mutex_destroy(&this->vm_lock);
110 vm_free_vm(this->vm);
111 free(this->path);
112 free(this);
113 return DVDNAV_STATUS_ERR;
114 }
115
116 dvdnav_status_t dvdnav_free_dup(dvdnav_t *this) {
117
118 #ifdef LOG_DEBUG
119 Log3(this, "free_dup:called");
120 #endif
121
122 if (this->file) {
123 pthread_mutex_lock(&this->vm_lock);
124 DVDCloseFile(this->file);
125 #ifdef LOG_DEBUG
126 Log3(this, "close:file closing");
127 #endif
128 this->file = NULL;
129 pthread_mutex_unlock(&this->vm_lock);
130 }
131
132 /* Free the VM */
133 if(this->vm)
134 vm_free_copy(this->vm);
135
136 pthread_mutex_destroy(&this->vm_lock);
137
138 free(this->path);
139
140 /* We leave the final freeing of the entire structure to the cache,
141 * because we don't know, if there are still buffers out in the wild,
142 * that must return first. */
143 if(this->cache)
144 dvdnav_read_cache_free(this->cache);
145 else
146 free(this);
147
148 return DVDNAV_STATUS_OK;
149 }
150
151 static dvdnav_status_t dvdnav_open_common(dvdnav_t** dest,
152 void *priv, const dvdnav_logger_cb *logcb,
153 const char *path,
154 dvdnav_stream_cb *stream_cb) {
155 dvdnav_t *this;
156 struct timeval time;
157
158 /* Create a new structure */
159 (*dest) = NULL;
160 this = (dvdnav_t*)calloc(1, sizeof(dvdnav_t));
161 if(!this)
162 return DVDNAV_STATUS_ERR;
163
164 this->priv = priv;
165 if(logcb)
166 this->logcb = *logcb;
167
168 pthread_mutex_init(&this->vm_lock, NULL);
169 /* Initialise the error string */
170 printerr("");
171
172 /* Initialise the VM */
173 this->vm = vm_new_vm(priv, logcb);
174 if(!this->vm) {
175 goto fail;
176 }
177 if(!vm_reset(this->vm, path, priv, stream_cb)) {
178 goto fail;
179 }
180
181 /* Set the path. */
182 if(path != NULL)
183 {
184 this->path = strdup(path);
185 if(!this->path)
186 goto fail;
187 }
188
189 /* Pre-open and close a file so that the CSS-keys are cached. */
190 this->file = DVDOpenFile(vm_get_dvd_reader(this->vm), 0, DVD_READ_MENU_VOBS);
191
192 /* Start the read-ahead cache. */
193 this->cache = dvdnav_read_cache_new(this);
194 if(!this->cache)
195 goto fail;
196
197 /* Seed the random numbers. So that the DVD VM Command rand()
198 * gives a different start value each time a DVD is played. */
199 gettimeofday(&time, NULL);
200 srand(time.tv_usec);
201
202 dvdnav_clear(this);
203
204 (*dest) = this;
205 return DVDNAV_STATUS_OK;
206
207 fail:
208 pthread_mutex_destroy(&this->vm_lock);
209 vm_free_vm(this->vm);
210 free(this->path);
211 free(this);
212 return DVDNAV_STATUS_ERR;
213 }
214
215 dvdnav_status_t dvdnav_open(dvdnav_t** dest, const char *path) {
216 return dvdnav_open_common(dest, NULL, NULL, path, NULL);
217 }
218
219 dvdnav_status_t dvdnav_open2(dvdnav_t** dest,
220 void *priv,const dvdnav_logger_cb *logcb,
221 const char *path) {
222 return dvdnav_open_common(dest, priv, logcb, path, NULL);
223 }
224
225 dvdnav_status_t dvdnav_open_stream(dvdnav_t** dest,
226 void *priv, dvdnav_stream_cb *stream_cb) {
227 return dvdnav_open_common(dest, priv, NULL, NULL, stream_cb);
228 }
229
230 dvdnav_status_t dvdnav_open_stream2(dvdnav_t** dest,
231 void *priv,const dvdnav_logger_cb *logcb,
232 dvdnav_stream_cb *stream_cb) {
233 return dvdnav_open_common(dest, priv, logcb, NULL, stream_cb);
234 }
235
236 dvdnav_status_t dvdnav_close(dvdnav_t *this) {
237
238 #ifdef LOG_DEBUG
239 Log3(this, "close:called");
240 #endif
241
242 if (this->file) {
243 pthread_mutex_lock(&this->vm_lock);
244 DVDCloseFile(this->file);
245 #ifdef LOG_DEBUG
246 Log3(this, "close:file closing");
247 #endif
248 this->file = NULL;
249 pthread_mutex_unlock(&this->vm_lock);
250 }
251
252 /* Free the VM */
253 if(this->vm)
254 vm_free_vm(this->vm);
255
256 pthread_mutex_destroy(&this->vm_lock);
257
258 free(this->path);
259
260 /* We leave the final freeing of the entire structure to the cache,
261 * because we don't know, if there are still buffers out in the wild,
262 * that must return first. */
263 if(this->cache)
264 dvdnav_read_cache_free(this->cache);
265 else
266 free(this);
267
268 return DVDNAV_STATUS_OK;
269 }
270
271 dvdnav_status_t dvdnav_reset(dvdnav_t *this) {
272 dvdnav_status_t result;
273
274 #ifdef LOG_DEBUG
275 Log3(this, "reset:called");
276 #endif
277
278 pthread_mutex_lock(&this->vm_lock);
279
280 #ifdef LOG_DEBUG
281 Log3(this, "resetting vm");
282 #endif
283 if(!vm_reset(this->vm, NULL, NULL, NULL)) {
284 printerr("Error restarting the VM.");
285 pthread_mutex_unlock(&this->vm_lock);
286 return DVDNAV_STATUS_ERR;
287 }
288 #ifdef LOG_DEBUG
289 Log3(this, "clearing dvdnav");
290 #endif
291 pthread_mutex_unlock(&this->vm_lock);
292 result = dvdnav_clear(this);
293
294 return result;
295 }
296
297 dvdnav_status_t dvdnav_path(dvdnav_t *this, const char** path) {
298 (*path) = this->path;
299
300 return DVDNAV_STATUS_OK;
301 }
302
303 const char* dvdnav_err_to_string(dvdnav_t *this) {
304
305 if(!this)
306 return "Hey! You gave me a NULL pointer you naughty person!";
307
308 return this->err_str;
309 }
310
311 /* converts a dvd_time_t to PTS ticks */
312 int64_t dvdnav_convert_time(const dvd_time_t *time) {
313 int64_t result;
314 int64_t frames;
315
316 result = ((int64_t)(time->hour >> 4 )) * 10 * 60 * 60 * 90000;
317 result += ((int64_t)(time->hour & 0x0f)) * 60 * 60 * 90000;
318 result += ((int64_t)(time->minute >> 4 )) * 10 * 60 * 90000;
319 result += ((int64_t)(time->minute & 0x0f)) * 60 * 90000;
320 result += ((int64_t)(time->second >> 4 )) * 10 * 90000;
321 result += ((int64_t)(time->second & 0x0f)) * 90000;
322
323 frames = ((time->frame_u & 0x30) >> 4) * 10;
324 frames += ((time->frame_u & 0x0f) ) ;
325
326 if (time->frame_u & 0x80)
327 result += frames * 3000;
328 else
329 result += frames * 3600;
330
331 return result;
332 }
333
334 /*
335 * Returns 1 if block contains NAV packet, 0 otherwise.
336 * Processes said NAV packet if present.
337 *
338 * Most of the code in here is copied from xine's MPEG demuxer
339 * so any bugs which are found in that should be corrected here also.
340 */
341 static int32_t dvdnav_decode_packet(dvdnav_t *this, uint8_t *p,
342 dsi_t *nav_dsi, pci_t *nav_pci) {
343 int32_t bMpeg1 = 0;
344 uint32_t nHeaderLen;
345 uint32_t nPacketLen;
346 uint32_t nStreamID;
347
348 if (p[3] == 0xBA) { /* program stream pack header */
349 int32_t nStuffingBytes;
350
351 bMpeg1 = (p[4] & 0x40) == 0;
352
353 if (bMpeg1) {
354 p += 12;
355 } else { /* mpeg2 */
356 nStuffingBytes = p[0xD] & 0x07;
357 p += 14 + nStuffingBytes;
358 }
359 }
360
361 if (p[3] == 0xbb) { /* program stream system header */
362 nHeaderLen = (p[4] << 8) | p[5];
363 p += 6 + nHeaderLen;
364 }
365
366 /* we should now have a PES packet here */
367 if (p[0] || p[1] || (p[2] != 1)) {
368 Log1(this, "demux error! %02x %02x %02x (should be 0x000001)",p[0],p[1],p[2]);
369 return 0;
370 }
371
372 nPacketLen = p[4] << 8 | p[5];
373 nStreamID = p[3];
374
375 nHeaderLen = 6;
376 p += nHeaderLen;
377
378 if (nStreamID == 0xbf) { /* Private stream 2 */
379 #if 0
380 char buffer[80 * 3 + 1];
381 int32_t i;
382 for(i=0;i<80;i++)
383 sprintf(&buffer[i*3], "%02x ",p[i-6]);
384 Log3(this, "nav packet=%u %s",p-p_start-6, buffer);
385 #endif
386
387 if(p[0] == 0x00) {
388 navRead_PCI(nav_pci, p+1);
389 }
390
391 p += nPacketLen;
392
393 /* We should now have a DSI packet. */
394 if(p[6] == 0x01) {
395 nPacketLen = p[4] << 8 | p[5];
396 p += 6;
397 navRead_DSI(nav_dsi, p+1);
398 }
399 return 1;
400 }
401 return 0;
402 }
403
404 /* DSI is used for most angle stuff.
405 * PCI is used for only non-seemless angle stuff
406 */
407 static int32_t dvdnav_get_vobu(dvdnav_t *this, dsi_t *nav_dsi, pci_t *nav_pci, dvdnav_vobu_t *vobu) {
408 uint32_t next;
409 int32_t angle, num_angle;
410
411 vobu->vobu_start = nav_dsi->dsi_gi.nv_pck_lbn; /* Absolute offset from start of disk */
412 vobu->vobu_length = nav_dsi->dsi_gi.vobu_ea; /* Relative offset from vobu_start */
413
414 /*
415 * If we're not at the end of this cell, we can determine the next
416 * VOBU to display using the VOBU_SRI information section of the
417 * DSI. Using this value correctly follows the current angle,
418 * avoiding the doubled scenes in The Matrix, and makes our life
419 * really happy.
420 *
421 * vobu_next is an offset value, 0x3fffffff = SRI_END_OF_CELL
422 * DVDs are about 6 Gigs, which is only up to 0x300000 blocks
423 * Should really assert if bit 31 != 1
424 */
425
426 #if 0
427 /* Old code -- may still be useful one day */
428 if(nav_dsi->vobu_sri.next_vobu != SRI_END_OF_CELL ) {
429 vobu->vobu_next = ( nav_dsi->vobu_sri.next_vobu & 0x3fffffff );
430 } else {
431 vobu->vobu_next = vobu->vobu_length;
432 }
433 #else
434 /* Relative offset from vobu_start */
435 vobu->vobu_next = ( nav_dsi->vobu_sri.next_vobu & 0x3fffffff );
436 #endif
437
438 vm_get_angle_info(this->vm, &angle, &num_angle);
439
440 /* FIMXE: The angle reset doesn't work for some reason for the moment */
441 #if 0
442 if((num_angle < angle) && (angle != 1)) {
443 Log3(this, "angle ends!");
444
445 /* This is to switch back to angle one when we
446 * finish with angles. */
447 dvdnav_angle_change(this, 1);
448 }
449 #endif
450 /* only use ILVU information if we are at the last vobunit in ILVU */
451 /* otherwise we will miss nav packets from vobunits inbetween */
452 if(num_angle != 0 && (nav_dsi->sml_pbi.category & DSI_ILVU_MASK) == (DSI_ILVU_BLOCK | DSI_ILVU_LAST)) {
453
454 if((next = nav_pci->nsml_agli.nsml_agl_dsta[angle-1]) != 0) {
455 if((next & 0x3fffffff) != 0) {
456 if(next & 0x80000000)
457 vobu->vobu_next = - (int32_t)(next & 0x3fffffff);
458 else
459 vobu->vobu_next = + (int32_t)(next & 0x3fffffff);
460 }
461 } else if((next = nav_dsi->sml_agli.data[angle-1].address) != 0) {
462 vobu->vobu_length = nav_dsi->sml_pbi.ilvu_ea;
463
464 if((next & 0x80000000) && (next != 0x7fffffff))
465 vobu->vobu_next = - (int32_t)(next & 0x3fffffff);
466 else
467 vobu->vobu_next = + (int32_t)(next & 0x3fffffff);
468 }
469 }
470
471 return 1;
472 }
473
474 /*
475 * These are the main get_next_block function which actually get the media stream video and audio etc.
476 *
477 * There are two versions: The second one is using the zero-copy read ahead cache and therefore
478 * hands out pointers targeting directly into the cache.
479 * The first one uses a memcopy to fill this cache block into the application provided memory.
480 * The benefit of this first one is that no special memory management is needed. The application is
481 * the only one responsible of allocating and freeing the memory associated with the pointer.
482 * The drawback is the additional memcopy.
483 */
484
485 dvdnav_status_t dvdnav_get_next_block(dvdnav_t *this, uint8_t *buf,
486 int32_t *event, int32_t *len) {
487 unsigned char *block;
488 dvdnav_status_t status;
489
490 block = buf;
491 status = dvdnav_get_next_cache_block(this, &block, event, len);
492 if (status == DVDNAV_STATUS_OK && block != buf) {
493 /* we received a block from the cache, copy it, so we can give it back */
494 memcpy(buf, block, DVD_VIDEO_LB_LEN);
495 dvdnav_free_cache_block(this, block);
496 }
497 return status;
498 }
499
500 int64_t dvdnav_get_current_time(dvdnav_t *this) {
501 int i;
502 int64_t tm=0;
503 dvd_state_t *state = &this->vm->state;
504
505 if(! state->pgc)
506 return 0;
507
508 for(i=0; i<state->cellN-1; i++) {
509 if(!
510 (state->pgc->cell_playback[i].block_type == BLOCK_TYPE_ANGLE_BLOCK &&
511 state->pgc->cell_playback[i].block_mode != BLOCK_MODE_FIRST_CELL)
512 )
513 tm += dvdnav_convert_time(&state->pgc->cell_playback[i].playback_time);
514 }
515 tm += this->cur_cell_time;
516
517 return tm;
518 }
519
520 dvdnav_status_t dvdnav_get_next_cache_block(dvdnav_t *this, uint8_t **buf,
521 int32_t *event, int32_t *len) {
522 dvd_state_t *state;
523 int32_t result;
524
525 if(!this)
526 return DVDNAV_STATUS_ERR;
527
528 pthread_mutex_lock(&this->vm_lock);
529
530 if(!this->started) {
531 /* Start the VM */
532 if (!vm_start(this->vm)) {
533 printerr("Encrypted or faulty DVD");
534 pthread_mutex_unlock(&this->vm_lock);
535 return DVDNAV_STATUS_ERR;
536 }
537 this->started = 1;
538 }
539
540 if (!this->vm->state.pgc) {
541 printerr("No current PGC.");
542 pthread_mutex_unlock(&this->vm_lock);
543 return DVDNAV_STATUS_ERR;
544 }
545
546 state = &(this->vm->state);
547 (*event) = DVDNAV_NOP;
548 (*len) = 0;
549
550 /* Check the STOP flag */
551 if(this->vm->stopped) {
552 vm_stop(this->vm);
553 (*event) = DVDNAV_STOP;
554 this->started = 0;
555 pthread_mutex_unlock(&this->vm_lock);
556 return DVDNAV_STATUS_OK;
557 }
558
559 vm_position_get(this->vm, &this->position_next);
560
561 #ifdef LOG_DEBUG
562 Log3(this->vm, "POS-NEXT ");
563 vm_position_print(this->vm, &this->position_next);
564 Log3(this->vm, "POS-CUR ");
565 vm_position_print(this->vm, &this->position_current);
566 #endif
567
568 /* did we hop? */
569 if(this->position_current.hop_channel != this->position_next.hop_channel) {
570 (*event) = DVDNAV_HOP_CHANNEL;
571 #ifdef LOG_DEBUG
572 Log3(this->vm, "HOP_CHANNEL");
573 #endif
574 if (this->position_next.hop_channel - this->position_current.hop_channel >= HOP_SEEK) {
575 int32_t num_angles = 0, current;
576
577 /* we seeked -> check for multiple angles */
578 vm_get_angle_info(this->vm, &current, &num_angles);
579 if (num_angles > 1) {
580 int32_t result, block;
581 /* we have to skip the first VOBU when seeking in a multiangle feature,
582 * because it might belong to the wrong angle */
583 block = this->position_next.cell_start + this->position_next.block;
584 result = dvdnav_read_cache_block(this->cache, block, 1, buf);
585 if(result <= 0) {
586 printerr("Error reading NAV packet.");
587 pthread_mutex_unlock(&this->vm_lock);
588 return DVDNAV_STATUS_ERR;
589 }
590 /* Decode nav into pci and dsi. Then get next VOBU info. */
591 if(!dvdnav_decode_packet(this, *buf, &this->dsi, &this->pci)) {
592 printerr("Expected NAV packet but none found.");
593 pthread_mutex_unlock(&this->vm_lock);
594 return DVDNAV_STATUS_ERR;
595 }
596 dvdnav_get_vobu(this, &this->dsi, &this->pci, &this->vobu);
597 /* skip to next, if there is a next */
598 if (this->vobu.vobu_next != SRI_END_OF_CELL) {
599 this->vobu.vobu_start += this->vobu.vobu_next;
600 this->vobu.vobu_next = 0;
601 }
602 /* update VM state */
603 this->vm->state.blockN = this->vobu.vobu_start - this->position_next.cell_start;
604 }
605 }
606 this->position_current.hop_channel = this->position_next.hop_channel;
607 /* update VOBU info */
608 this->vobu.vobu_start = this->position_next.cell_start + this->position_next.block;
609 this->vobu.vobu_next = 0;
610 /* Make blockN == vobu_length to do expected_nav */
611 this->vobu.vobu_length = 0;
612 this->vobu.blockN = 0;
613 this->sync_wait = 0;
614 pthread_mutex_unlock(&this->vm_lock);
615 return DVDNAV_STATUS_OK;
616 }
617
618 /* Check the HIGHLIGHT flag */
619 if(this->position_current.button != this->position_next.button) {
620 dvdnav_highlight_event_t *hevent = (dvdnav_highlight_event_t *)*buf;
621
622 (*event) = DVDNAV_HIGHLIGHT;
623 #ifdef LOG_DEBUG
624 Log3(this, "HIGHLIGHT");
625 #endif
626 (*len) = sizeof(dvdnav_highlight_event_t);
627 hevent->display = 1;
628 hevent->buttonN = this->position_next.button;
629 this->position_current.button = this->position_next.button;
630 pthread_mutex_unlock(&this->vm_lock);
631 return DVDNAV_STATUS_OK;
632 }
633
634 /* Check the WAIT flag */
635 if(this->sync_wait) {
636 (*event) = DVDNAV_WAIT;
637 #ifdef LOG_DEBUG
638 Log3(this, "WAIT");
639 #endif
640 (*len) = 0;
641 pthread_mutex_unlock(&this->vm_lock);
642 return DVDNAV_STATUS_OK;
643 }
644
645 /* Check to see if we need to change the currently opened VOB or open
646 * a new one because we don't currently have an opened VOB. */
647 if((this->file == NULL) ||
648 (this->position_current.vts != this->position_next.vts) ||
649 (this->position_current.domain != this->position_next.domain)) {
650 dvd_read_domain_t domain;
651 int32_t vtsN;
652 dvdnav_vts_change_event_t *vts_event = (dvdnav_vts_change_event_t *)*buf;
653
654 if(this->file) {
655 DVDCloseFile(this->file);
656 this->file = NULL;
657 }
658
659 vts_event->old_vtsN = this->position_current.vts;
660 vts_event->old_domain = this->position_current.domain;
661
662 /* Use the DOMAIN to find whether to open menu or title VOBs */
663 switch(this->position_next.domain) {
664 case DVD_DOMAIN_FirstPlay:
665 case DVD_DOMAIN_VMGM:
666 domain = DVD_READ_MENU_VOBS;
667 vtsN = 0;
668 break;
669 case DVD_DOMAIN_VTSMenu:
670 domain = DVD_READ_MENU_VOBS;
671 vtsN = this->position_next.vts;
672 break;
673 case DVD_DOMAIN_VTSTitle:
674 domain = DVD_READ_TITLE_VOBS;
675 vtsN = this->position_next.vts;
676 break;
677 default:
678 printerr("Unknown domain when changing VTS.");
679 pthread_mutex_unlock(&this->vm_lock);
680 return DVDNAV_STATUS_ERR;
681 }
682
683 this->position_current.vts = this->position_next.vts;
684 this->position_current.domain = this->position_next.domain;
685 dvdnav_read_cache_clear(this->cache);
686 this->file = DVDOpenFile(vm_get_dvd_reader(this->vm), vtsN, domain);
687 vts_event->new_vtsN = this->position_next.vts;
688 vts_event->new_domain = this->position_next.domain;
689
690 /* If couldn't open the file for some reason, moan */
691 if(this->file == NULL) {
692 printerrf("Error opening vtsN=%i, domain=%i.", vtsN, domain);
693 pthread_mutex_unlock(&this->vm_lock);
694 return DVDNAV_STATUS_ERR;
695 }
696
697 /* File opened successfully so return a VTS change event */
698 (*event) = DVDNAV_VTS_CHANGE;
699 #ifdef LOG_DEBUG
700 Log3(this, "VTS_CHANGE");
701 #endif
702 (*len) = sizeof(dvdnav_vts_change_event_t);
703
704 this->spu_clut_changed = 1;
705 this->position_current.cell = -1; /* Force an update */
706 this->position_current.spu_channel = -1; /* Force an update */
707 this->position_current.audio_channel = -1; /* Force an update */;
708
709 pthread_mutex_unlock(&this->vm_lock);
710 return DVDNAV_STATUS_OK;
711 }
712
713 /* Check if the cell changed */
714 if( (this->position_current.cell != this->position_next.cell) ||
715 (this->position_current.cell_restart != this->position_next.cell_restart) ||
716 (this->position_current.cell_start != this->position_next.cell_start) ) {
717 dvdnav_cell_change_event_t *cell_event = (dvdnav_cell_change_event_t *)*buf;
718 int32_t first_cell_nr, last_cell_nr, i;
719 dvd_state_t *state = &this->vm->state;
720
721 this->cur_cell_time = 0;
722 (*event) = DVDNAV_CELL_CHANGE;
723 #ifdef LOG_DEBUG
724 Log3(this, "CELL_CHANGE");
725 #endif
726 (*len) = sizeof(dvdnav_cell_change_event_t);
727
728 cell_event->cellN = state->cellN;
729 cell_event->pgN = state->pgN;
730 cell_event->cell_length =
731 (state->pgc->cell_playback[state->cellN - 1].last_sector + 1);
732
733 cell_event->pg_length = 0;
734 /* Find start cell of program. */
735 first_cell_nr = state->pgc->program_map[state->pgN-1];
736 /* Find end cell of program */
737 if(state->pgN < state->pgc->nr_of_programs)
738 last_cell_nr = state->pgc->program_map[state->pgN] - 1;
739 else
740 last_cell_nr = state->pgc->nr_of_cells;
741 for (i = first_cell_nr; i <= last_cell_nr; i++)
742 cell_event->pg_length +=
743 (state->pgc->cell_playback[i - 1].last_sector + 1);
744
745 cell_event->pgc_length = dvdnav_convert_time(&state->pgc->playback_time);
746
747 cell_event->cell_start = 0;
748 for (i = 1; i < state->cellN; i++)
749 cell_event->cell_start +=
750 (state->pgc->cell_playback[i - 1].last_sector + 1);
751
752 cell_event->pg_start = 0;
753 for (i = 1; i < state->pgc->program_map[state->pgN-1]; i++)
754 cell_event->pg_start +=
755 (state->pgc->cell_playback[i - 1].last_sector + 1);
756
757 this->position_current.cell = this->position_next.cell;
758 this->position_current.cell_restart = this->position_next.cell_restart;
759 this->position_current.cell_start = this->position_next.cell_start;
760 this->position_current.block = this->position_next.block;
761
762 /* vobu info is used for mid cell resumes */
763 this->vobu.vobu_start = this->position_next.cell_start + this->position_next.block;
764 this->vobu.vobu_next = 0;
765 /* Make blockN == vobu_length to do expected_nav */
766 this->vobu.vobu_length = 0;
767 this->vobu.blockN = 0;
768
769 /* update the spu palette at least on PGC changes */
770 this->spu_clut_changed = 1;
771 this->position_current.spu_channel = -1; /* Force an update */
772 this->position_current.audio_channel = -1; /* Force an update */
773
774 pthread_mutex_unlock(&this->vm_lock);
775 return DVDNAV_STATUS_OK;
776 }
777
778 /* has the CLUT changed? */
779 if(this->spu_clut_changed) {
780 (*event) = DVDNAV_SPU_CLUT_CHANGE;
781 #ifdef LOG_DEBUG
782 Log3(this, "SPU_CLUT_CHANGE");
783 #endif
784 (*len) = 16 * sizeof(uint32_t);
785 memcpy(*buf, state->pgc->palette, sizeof(state->pgc->palette));
786 this->spu_clut_changed = 0;
787 pthread_mutex_unlock(&this->vm_lock);
788 return DVDNAV_STATUS_OK;
789 }
790
791 /* has the SPU channel changed? */
792 if(this->position_current.spu_channel != this->position_next.spu_channel) {
793 dvdnav_spu_stream_change_event_t *stream_change = (dvdnav_spu_stream_change_event_t *)*buf;
794
795 (*event) = DVDNAV_SPU_STREAM_CHANGE;
796 #ifdef LOG_DEBUG
797 Log3(this, "SPU_STREAM_CHANGE");
798 #endif
799 (*len) = sizeof(dvdnav_spu_stream_change_event_t);
800 stream_change->physical_wide = vm_get_subp_active_stream(this->vm, 0);
801 stream_change->physical_letterbox = vm_get_subp_active_stream(this->vm, 1);
802 stream_change->physical_pan_scan = vm_get_subp_active_stream(this->vm, 2);
803 this->position_current.spu_channel = this->position_next.spu_channel;
804 #ifdef LOG_DEBUG
805 Log3(this, "SPU_STREAM_CHANGE stream_id_wide=%d",stream_change->physical_wide);
806 Log3(this, "SPU_STREAM_CHANGE stream_id_letterbox=%d",stream_change->physical_letterbox);
807 Log3(this, "SPU_STREAM_CHANGE stream_id_pan_scan=%d",stream_change->physical_pan_scan);
808 Log3(this, "SPU_STREAM_CHANGE returning DVDNAV_STATUS_OK");
809 #endif
810 /* This is not really the right place to do this. FOSL_BTNN should set the register
811 * at HLI_S_PTM rather than when we enter the SPU. As well we should activate FOAC_BTNN
812 * at HLI_E_PTM
813 */
814 if (this->pci.hli.hl_gi.fosl_btnn != 0) {
815 set_HL_BTN(this->vm, this->pci.hli.hl_gi.fosl_btnn);
816 }
817
818 pthread_mutex_unlock(&this->vm_lock);
819 return DVDNAV_STATUS_OK;
820 }
821
822 /* has the audio channel changed? */
823 if(this->position_current.audio_channel != this->position_next.audio_channel) {
824 dvdnav_audio_stream_change_event_t *stream_change = (dvdnav_audio_stream_change_event_t *)*buf;
825
826 (*event) = DVDNAV_AUDIO_STREAM_CHANGE;
827 #ifdef LOG_DEBUG
828 Log3(this, "AUDIO_STREAM_CHANGE");
829 #endif
830 (*len) = sizeof(dvdnav_audio_stream_change_event_t);
831 stream_change->physical = vm_get_audio_active_stream( this->vm );
832 stream_change->logical = this->position_next.audio_channel;
833 this->position_current.audio_channel = this->position_next.audio_channel;
834 #ifdef LOG_DEBUG
835 Log3(this, "AUDIO_STREAM_CHANGE stream_id=%d returning DVDNAV_STATUS_OK",stream_change->physical);
836 #endif
837 pthread_mutex_unlock(&this->vm_lock);
838 return DVDNAV_STATUS_OK;
839 }
840
841 /* Check the STILLFRAME flag */
842 if(this->position_current.still != 0) {
843 dvdnav_still_event_t *still_event = (dvdnav_still_event_t *)*buf;
844
845 (*event) = DVDNAV_STILL_FRAME;
846 #ifdef LOG_DEBUG
847 Log3(this, "STILL_FRAME");
848 #endif
849 (*len) = sizeof(dvdnav_still_event_t);
850 still_event->length = this->position_current.still;
851 pthread_mutex_unlock(&this->vm_lock);
852 return DVDNAV_STATUS_OK;
853 }
854
855 /* Have we reached the end of a VOBU? */
856 if (this->vobu.blockN >= this->vobu.vobu_length) {
857
858 /* Have we reached the end of a cell? */
859 if(this->vobu.vobu_next == SRI_END_OF_CELL) {
860 /* End of Cell from NAV DSI info */
861 #ifdef LOG_DEBUG
862 Log3(this, "Still set to %x", this->position_next.still);
863 #endif
864 this->position_current.still = this->position_next.still;
865
866 /* we are about to leave a cell, so a lot of state changes could occur;
867 * under certain conditions, the application should get in sync with us before this,
868 * otherwise it might show stills or menus too shortly */
869 if ((this->position_current.still || this->pci.hli.hl_gi.hli_ss) && !this->sync_wait_skip)
870 this->sync_wait = 1;
871
872 if(!this->position_current.still || this->skip_still ) {
873 /* no active cell still -> get us to the next cell */
874 vm_get_next_cell(this->vm);
875 this->position_current.still = 0; /* still gets activated at end of cell */
876 this->skip_still = 0;
877 this->sync_wait_skip = 0;
878 }
879 /* handle related state changes in next iteration */
880 (*event) = DVDNAV_NOP;
881 (*len) = 0;
882 pthread_mutex_unlock(&this->vm_lock);
883 return DVDNAV_STATUS_OK;
884 }
885
886 /* at the start of the next VOBU -> expecting NAV packet */
887 result = dvdnav_read_cache_block(this->cache, this->vobu.vobu_start + this->vobu.vobu_next, 1, buf);
888
889 if(result <= 0) {
890 printerr("Error reading NAV packet.");
891 pthread_mutex_unlock(&this->vm_lock);
892 return DVDNAV_STATUS_ERR;
893 }
894 /* Decode nav into pci and dsi. Then get next VOBU info. */
895 if(!dvdnav_decode_packet(this, *buf, &this->dsi, &this->pci)) {
896 printerr("Expected NAV packet but none found.");
897 pthread_mutex_unlock(&this->vm_lock);
898 return DVDNAV_STATUS_ERR;
899 }
900 /* We need to update the vm state->blockN with which VOBU we are in.
901 * This is so RSM resumes to the VOBU level and not just the CELL level.
902 */
903 this->vm->state.blockN = this->vobu.vobu_start - this->position_current.cell_start;
904
905 dvdnav_get_vobu(this, &this->dsi, &this->pci, &this->vobu);
906 this->vobu.blockN = 0;
907 /* Give the cache a hint about the size of next VOBU.
908 * This improves pre-caching, because the VOBU will almost certainly be read entirely.
909 */
910 dvdnav_pre_cache_blocks(this->cache, this->vobu.vobu_start+1, this->vobu.vobu_length+1);
911
912 /* release NAV menu filter, when we reach the same NAV packet again */
913 if (this->last_cmd_nav_lbn == this->pci.pci_gi.nv_pck_lbn)
914 this->last_cmd_nav_lbn = SRI_END_OF_CELL;
915
916 /* Successfully got a NAV packet */
917 (*event) = DVDNAV_NAV_PACKET;
918 #ifdef LOG_DEBUG
919 Log3(this, "NAV_PACKET");
920 #endif
921 (*len) = 2048;
922 this->cur_cell_time = dvdnav_convert_time(&this->dsi.dsi_gi.c_eltm);
923 pthread_mutex_unlock(&this->vm_lock);
924 return DVDNAV_STATUS_OK;
925 }
926
927 /* If we've got here, it must just be a normal block. */
928 if(!this->file) {
929 printerr("Attempting to read without opening file.");
930 pthread_mutex_unlock(&this->vm_lock);
931 return DVDNAV_STATUS_ERR;
932 }
933
934 this->vobu.blockN++;
935 result = dvdnav_read_cache_block(this->cache, this->vobu.vobu_start + this->vobu.blockN, 1, buf);
936 if(result <= 0) {
937 printerr("Error reading from DVD.");
938 pthread_mutex_unlock(&this->vm_lock);
939 return DVDNAV_STATUS_ERR;
940 }
941 (*event) = DVDNAV_BLOCK_OK;
942 (*len) = 2048;
943
944 pthread_mutex_unlock(&this->vm_lock);
945 return DVDNAV_STATUS_OK;
946 }
947
948 dvdnav_status_t dvdnav_get_title_string(dvdnav_t *this, const char **title_str) {
949 (*title_str) = this->vm->dvd_name;
950 return DVDNAV_STATUS_OK;
951 }
952
953 dvdnav_status_t dvdnav_get_serial_string(dvdnav_t *this, const char **serial_str) {
954 (*serial_str) = this->vm->dvd_serial;
955 return DVDNAV_STATUS_OK;
956 }
957
958 char * dvdnav_get_volid_string(dvdnav_t *this) {
959 if (!this || !this->vm || !this->vm->dvd) {
960 printerr("Invalid state, vm or reader not available.");
961 return NULL;
962 }
963
964 char *volid_str = malloc(33);
965 if (volid_str == NULL) {
966 printerr("Insufficient memory available.");
967 return NULL;
968 }
969
970 if (DVDUDFVolumeInfo(this->vm->dvd, volid_str, 32, NULL, 0) == -1) {
971 if (DVDISOVolumeInfo(this->vm->dvd, volid_str, 33, NULL, 0) == -1) {
972 printerr("Failed to obtain volume id.");
973 free(volid_str);
974 return NULL;
975 }
976 }
977 return volid_str;
978 }
979
980 uint8_t dvdnav_get_video_aspect(dvdnav_t *this) {
981 uint8_t retval;
982
983 if(!this->started) {
984 printerr("Virtual DVD machine not started.");
985 return -1;
986 }
987
988 pthread_mutex_lock(&this->vm_lock);
989 retval = (uint8_t)vm_get_video_aspect(this->vm);
990 pthread_mutex_unlock(&this->vm_lock);
991
992 return retval;
993 }
994 dvdnav_status_t dvdnav_get_video_resolution(dvdnav_t *this, uint32_t *width, uint32_t *height) {
995 int w, h;
996
997 if(!this->started) {
998 printerr("Virtual DVD machine not started.");
999 return DVDNAV_STATUS_ERR;
1000 }
1001
1002 pthread_mutex_lock(&this->vm_lock);
1003 vm_get_video_res(this->vm, &w, &h);
1004 pthread_mutex_unlock(&this->vm_lock);
1005
1006 *width = w;
1007 *height = h;
1008 return DVDNAV_STATUS_OK;
1009 }
1010
1011 uint8_t dvdnav_get_video_scale_permission(dvdnav_t *this) {
1012 uint8_t retval;
1013
1014 if(!this->started) {
1015 printerr("Virtual DVD machine not started.");
1016 return -1;
1017 }
1018
1019 pthread_mutex_lock(&this->vm_lock);
1020 retval = (uint8_t)vm_get_video_scale_permission(this->vm);
1021 pthread_mutex_unlock(&this->vm_lock);
1022
1023 return retval;
1024 }
1025
1026 uint16_t dvdnav_audio_stream_to_lang(dvdnav_t *this, uint8_t stream) {
1027 audio_attr_t attr;
1028
1029 if(!this->started) {
1030 printerr("Virtual DVD machine not started.");
1031 return -1;
1032 }
1033
1034 pthread_mutex_lock(&this->vm_lock);
1035 attr = vm_get_audio_attr(this->vm, stream);
1036 pthread_mutex_unlock(&this->vm_lock);
1037
1038 if(attr.lang_type != 1)
1039 return 0xffff;
1040
1041 return attr.lang_code;
1042 }
1043
1044 uint16_t dvdnav_audio_stream_format(dvdnav_t *this, uint8_t stream) {
1045 audio_attr_t attr;
1046 uint16_t format;
1047
1048 if(!this->started) {
1049 printerr("Virtual DVD machine not started.");
1050 return -1; /* 0xffff */
1051 }
1052
1053 pthread_mutex_lock(&this->vm_lock);
1054 attr = vm_get_audio_attr(this->vm, stream);
1055 pthread_mutex_unlock(&this->vm_lock);
1056
1057 switch(attr.audio_format) {
1058 case 0:
1059 format = DVD_AUDIO_FORMAT_AC3;
1060 break;
1061 case 2: /* MPEG-1 or MPEG-2 without extension bitstream. */
1062 case 3: /* MPEG-2 with extension bitstream. */
1063 format = DVD_AUDIO_FORMAT_MPEG2_EXT;
1064 break;
1065 case 4:
1066 format = DVD_AUDIO_FORMAT_LPCM;
1067 break;
1068 case 6:
1069 format = DVD_AUDIO_FORMAT_DTS;
1070 break;
1071 case 7:
1072 format = DVD_AUDIO_FORMAT_SDDS;
1073 break;
1074 default:
1075 format = 0xffff;
1076 break;
1077 }
1078
1079 return format;
1080 }
1081
1082 uint16_t dvdnav_audio_stream_channels(dvdnav_t *this, uint8_t stream) {
1083 audio_attr_t attr;
1084
1085 if(!this->started) {
1086 printerr("Virtual DVD machine not started.");
1087 return -1; /* 0xffff */
1088 }
1089
1090 pthread_mutex_lock(&this->vm_lock);
1091 attr = vm_get_audio_attr(this->vm, stream);
1092 pthread_mutex_unlock(&this->vm_lock);
1093
1094 return attr.channels + 1;
1095 }
1096
1097 uint16_t dvdnav_spu_stream_to_lang(dvdnav_t *this, uint8_t stream) {
1098 subp_attr_t attr;
1099
1100 if(!this->started) {
1101 printerr("Virtual DVD machine not started.");
1102 return -1;
1103 }
1104
1105 pthread_mutex_lock(&this->vm_lock);
1106 attr = vm_get_subp_attr(this->vm, stream);
1107 pthread_mutex_unlock(&this->vm_lock);
1108
1109 if(attr.type != 1)
1110 return 0xffff;
1111
1112 return attr.lang_code;
1113 }
1114
1115 int8_t dvdnav_get_audio_logical_stream(dvdnav_t *this, uint8_t audio_num) {
1116 int8_t retval;
1117
1118 if(!this->started) {
1119 printerr("Virtual DVD machine not started.");
1120 return -1;
1121 }
1122
1123 pthread_mutex_lock(&this->vm_lock);
1124 if (!this->vm->state.pgc) {
1125 printerr("No current PGC.");
1126 pthread_mutex_unlock(&this->vm_lock);
1127 return -1;
1128 }
1129 retval = vm_get_audio_stream(this->vm, audio_num);
1130 pthread_mutex_unlock(&this->vm_lock);
1131
1132 return retval;
1133 }
1134
1135 int8_t dvdnav_get_number_of_streams(dvdnav_t *this, dvdnav_stream_type_t stream_type) {
1136
1137 if (stream_type != DVD_SUBTITLE_STREAM && stream_type != DVD_AUDIO_STREAM) {
1138 printerr("Invalid provided stream type");
1139 return -1;
1140 }
1141
1142 if (!this->started) {
1143 printerr("Virtual DVD machine not started.");
1144 return -1;
1145 }
1146
1147 pthread_mutex_lock(&this->vm_lock);
1148 if (!this->vm->state.pgc) {
1149 printerr("No current PGC.");
1150 pthread_mutex_unlock(&this->vm_lock);
1151 return -1;
1152 }
1153
1154 if (this->vm->state.domain != DVD_DOMAIN_VTSTitle &&
1155 this->vm->state.domain != DVD_DOMAIN_VTSMenu)
1156 {
1157 printerr("Invalid domain provided");
1158 pthread_mutex_unlock(&this->vm_lock);
1159 return -1;
1160 }
1161
1162 int8_t count = 0;
1163 switch (stream_type) {
1164 case DVD_SUBTITLE_STREAM:
1165 for (int i = 0; i < 32; i++)
1166 {
1167 if (this->vm->state.pgc->subp_control[i] & (1<<31))
1168 count++;
1169 }
1170 break;
1171 case DVD_AUDIO_STREAM:
1172 for (int i = 0; i < 8; i++)
1173 {
1174 if (this->vm->state.pgc->audio_control[i] & (1<<15))
1175 count++;
1176 }
1177 break;
1178 }
1179 pthread_mutex_unlock(&this->vm_lock);
1180 return count;
1181 }
1182
1183 dvdnav_status_t dvdnav_get_audio_attr(dvdnav_t *this, uint8_t audio_num, audio_attr_t *audio_attr) {
1184 if(!this->started) {
1185 printerr("Virtual DVD machine not started.");
1186 return DVDNAV_STATUS_ERR;
1187 }
1188 pthread_mutex_lock(&this->vm_lock);
1189 if (!this->vm->state.pgc) {
1190 printerr("No current PGC.");
1191 pthread_mutex_unlock(&this->vm_lock);
1192 return DVDNAV_STATUS_ERR;
1193 }
1194 *audio_attr=vm_get_audio_attr(this->vm, audio_num);
1195 pthread_mutex_unlock(&this->vm_lock);
1196
1197 return DVDNAV_STATUS_OK;
1198 }
1199
1200 int8_t dvdnav_get_spu_logical_stream(dvdnav_t *this, uint8_t subp_num) {
1201 int8_t retval;
1202
1203 if(!this->started) {
1204 printerr("Virtual DVD machine not started.");
1205 return -1;
1206 }
1207
1208 pthread_mutex_lock(&this->vm_lock);
1209 if (!this->vm->state.pgc) {
1210 printerr("No current PGC.");
1211 pthread_mutex_unlock(&this->vm_lock);
1212 return -1;
1213 }
1214 retval = vm_get_subp_stream(this->vm, subp_num, 0);
1215 pthread_mutex_unlock(&this->vm_lock);
1216
1217 return retval;
1218 }
1219
1220 dvdnav_status_t dvdnav_get_spu_attr(dvdnav_t *this, uint8_t audio_num, subp_attr_t *subp_attr) {
1221 if(!this->started) {
1222 printerr("Virtual DVD machine not started.");
1223 return DVDNAV_STATUS_ERR;
1224 }
1225 pthread_mutex_lock(&this->vm_lock);
1226 if (!this->vm->state.pgc) {
1227 printerr("No current PGC.");
1228 pthread_mutex_unlock(&this->vm_lock);
1229 return DVDNAV_STATUS_ERR;
1230 }
1231 *subp_attr=vm_get_subp_attr(this->vm, audio_num);
1232 pthread_mutex_unlock(&this->vm_lock);
1233 return DVDNAV_STATUS_OK;
1234 }
1235
1236 int8_t dvdnav_get_active_audio_stream(dvdnav_t *this) {
1237 int8_t retval;
1238
1239 if(!this->started) {
1240 printerr("Virtual DVD machine not started.");
1241 return -1;
1242 }
1243
1244 pthread_mutex_lock(&this->vm_lock);
1245 if (!this->vm->state.pgc) {
1246 printerr("No current PGC.");
1247 pthread_mutex_unlock(&this->vm_lock);
1248 return -1;
1249 }
1250 retval = vm_get_audio_active_stream(this->vm);
1251 pthread_mutex_unlock(&this->vm_lock);
1252
1253 return retval;
1254 }
1255
1256 int8_t dvdnav_get_active_spu_stream(dvdnav_t *this) {
1257 int8_t retval;
1258
1259 if(!this->started) {
1260 printerr("Virtual DVD machine not started.");
1261 return -1;
1262 }
1263
1264 pthread_mutex_lock(&this->vm_lock);
1265 if (!this->vm->state.pgc) {
1266 printerr("No current PGC.");
1267 pthread_mutex_unlock(&this->vm_lock);
1268 return -1;
1269 }
1270 retval = vm_get_subp_active_stream(this->vm, 0);
1271 pthread_mutex_unlock(&this->vm_lock);
1272
1273 return retval;
1274 }
1275
1276 dvdnav_status_t dvdnav_set_active_stream(dvdnav_t *this, uint8_t stream_num, dvdnav_stream_type_t stream_type) {
1277 if (stream_type != DVD_SUBTITLE_STREAM && stream_type != DVD_AUDIO_STREAM) {
1278 printerr("Invalid provided stream type");
1279 return DVDNAV_STATUS_ERR;
1280 }
1281
1282 if (!this->started) {
1283 printerr("Virtual DVD machine not started.");
1284 return DVDNAV_STATUS_ERR;
1285 }
1286
1287 pthread_mutex_lock(&this->vm_lock);
1288 if (!this->vm->state.pgc) {
1289 printerr("No current PGC.");
1290 pthread_mutex_unlock(&this->vm_lock);
1291 return DVDNAV_STATUS_ERR;
1292 }
1293
1294 if (this->vm->state.domain != DVD_DOMAIN_VTSTitle &&
1295 this->vm->state.domain != DVD_DOMAIN_VTSMenu)
1296 {
1297 printerr("Invalid active domain");
1298 pthread_mutex_unlock(&this->vm_lock);
1299 return DVDNAV_STATUS_ERR;
1300 }
1301
1302 switch (stream_type) {
1303 case DVD_SUBTITLE_STREAM:
1304 if (stream_num >= 32 ||
1305 !(this->vm->state.pgc->subp_control[stream_num] & (1 << 31))) {
1306 printerr("Invalid stream index not allowed");
1307 pthread_mutex_unlock(&this->vm_lock);
1308 return DVDNAV_STATUS_ERR;
1309 }
1310 // set state without changing the current visibility
1311 this->vm->state.SPST_REG = stream_num | (this->vm->state.SPST_REG & 0x40);
1312 break;
1313 case DVD_AUDIO_STREAM:
1314 if (stream_num >= 8 ||
1315 !(this->vm->state.pgc->audio_control[stream_num] & (1 << 15))) {
1316 printerr("Invalid stream index not allowed");
1317 pthread_mutex_unlock(&this->vm_lock);
1318 return DVDNAV_STATUS_ERR;
1319 }
1320 this->vm->state.AST_REG = stream_num;
1321 break;
1322 }
1323 pthread_mutex_unlock(&this->vm_lock);
1324 return DVDNAV_STATUS_OK;
1325 }
1326
1327 dvdnav_status_t dvdnav_toggle_spu_stream(dvdnav_t *this, uint8_t visibility) {
1328 if(!this->started) {
1329 printerr("Virtual DVD machine not started.");
1330 return DVDNAV_STATUS_ERR;
1331 }
1332
1333 pthread_mutex_lock(&this->vm_lock);
1334 switch(visibility) {
1335 case 0: /* disable */
1336 this->vm->state.SPST_REG &= ~0x40;
1337 break;
1338 case 1: /* enable */
1339 this->vm->state.SPST_REG |= 0x40;
1340 break;
1341 default:
1342 printerr("Invalid provided enabled_flag value");
1343 pthread_mutex_unlock(&this->vm_lock);
1344 return DVDNAV_STATUS_ERR;
1345 }
1346 pthread_mutex_unlock(&this->vm_lock);
1347 return DVDNAV_STATUS_OK;
1348 }
1349
1350 static int8_t dvdnav_is_domain(dvdnav_t *this, DVDDomain_t domain) {
1351 int8_t retval;
1352
1353 if(!this->started) {
1354 printerr("Virtual DVD machine not started.");
1355 return -1;
1356 }
1357
1358 pthread_mutex_lock(&this->vm_lock);
1359 retval = (this->vm->state.domain == domain);
1360 pthread_mutex_unlock(&this->vm_lock);
1361
1362 return retval;
1363 }
1364
1365 /* First Play domain. (Menu) */
1366 int8_t dvdnav_is_domain_fp(dvdnav_t *this) {
1367 return dvdnav_is_domain(this, DVD_DOMAIN_FirstPlay);
1368 }
1369 /* Video management Menu domain. (Menu) */
1370 int8_t dvdnav_is_domain_vmgm(dvdnav_t *this) {
1371 return dvdnav_is_domain(this, DVD_DOMAIN_VMGM);
1372 }
1373 /* Video Title Menu domain (Menu) */
1374 int8_t dvdnav_is_domain_vtsm(dvdnav_t *this) {
1375 return dvdnav_is_domain(this, DVD_DOMAIN_VTSMenu);
1376 }
1377 /* Video Title domain (playing movie). */
1378 int8_t dvdnav_is_domain_vts(dvdnav_t *this) {
1379 return dvdnav_is_domain(this, DVD_DOMAIN_VTSTitle);
1380 }
1381
1382 /* Generally delegate angle information handling to VM */
1383 dvdnav_status_t dvdnav_angle_change(dvdnav_t *this, int32_t angle) {
1384 int32_t num, current;
1385
1386 pthread_mutex_lock(&this->vm_lock);
1387 vm_get_angle_info(this->vm, &current, &num);
1388 /* Set angle SPRM if valid */
1389 if((angle > 0) && (angle <= num)) {
1390 this->vm->state.AGL_REG = angle;
1391 } else {
1392 printerr("Passed an invalid angle number.");
1393 pthread_mutex_unlock(&this->vm_lock);
1394 return DVDNAV_STATUS_ERR;
1395 }
1396 pthread_mutex_unlock(&this->vm_lock);
1397
1398 return DVDNAV_STATUS_OK;
1399 }
1400
1401 dvdnav_status_t dvdnav_get_angle_info(dvdnav_t *this, int32_t *current_angle,
1402 int32_t *number_of_angles) {
1403 pthread_mutex_lock(&this->vm_lock);
1404 vm_get_angle_info(this->vm, current_angle, number_of_angles);
1405 pthread_mutex_unlock(&this->vm_lock);
1406
1407 return DVDNAV_STATUS_OK;
1408 }
1409
1410 dvdnav_status_t dvdnav_get_disk_region_mask(dvdnav_t *this, int32_t *region_mask) {
1411 pthread_mutex_lock(&this->vm_lock);
1412 if (!this->vm || !this->vm->vmgi || !this->vm->vmgi->vmgi_mat) {
1413 printerr("Bad VM state.");
1414 pthread_mutex_unlock(&this->vm_lock);
1415 return DVDNAV_STATUS_ERR;
1416 }
1417
1418 (*region_mask) = ((this->vm->vmgi->vmgi_mat->vmg_category >> 16) & 0xff) ^ 0xff;
1419 pthread_mutex_unlock(&this->vm_lock);
1420 return DVDNAV_STATUS_OK;
1421 }
1422
1423 pci_t* dvdnav_get_current_nav_pci(dvdnav_t *this) {
1424 if(!this) return 0;
1425 return &this->pci;
1426 }
1427
1428 dsi_t* dvdnav_get_current_nav_dsi(dvdnav_t *this) {
1429 if(!this) return 0;
1430 return &this->dsi;
1431 }
1432
1433 uint32_t dvdnav_get_next_still_flag(dvdnav_t *this) {
1434 if(!this) return -1;
1435 return this->position_next.still;
1436 }
1437
1438 user_ops_t dvdnav_get_restrictions(dvdnav_t* this) {
1439 /*
1440 * user_ops_t is a structure of 32 bits. We want to compute
1441 * the union of two of those bitfields so to make this quicker
1442 * than performing 32 ORs, we will access them as 32bits words.
1443 */
1444 union {
1445 user_ops_t ops_struct;
1446 uint32_t ops_int;
1447 } ops, tmp;
1448
1449 ops.ops_int = 0;
1450
1451 if(!this || !this->started) {
1452 printerr("Virtual DVD machine not started.");
1453 return ops.ops_struct;
1454 }
1455
1456 pthread_mutex_lock(&this->vm_lock);
1457 ops.ops_struct = this->pci.pci_gi.vobu_uop_ctl;
1458
1459 if(this->vm && this->vm->state.pgc) {
1460 tmp.ops_struct = this->vm->state.pgc->prohibited_ops;
1461 ops.ops_int |= tmp.ops_int;
1462 }
1463 pthread_mutex_unlock(&this->vm_lock);
1464
1465 return ops.ops_struct;
1466 }
1467
1468 const char* dvdnav_version(void) {
1469 return VERSION;
1470 }