comparison dvdnav.c @ 0:427b7da5cbdb src

first split of dvdread; it's just a copy of dvdnav still to be cleaned
author nicodvb
date Sun, 01 Jun 2008 08:39:07 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:427b7da5cbdb
1 /*
2 * Copyright (C) 2000 Rich Wareham <richwareham@users.sourceforge.net>
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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 *
20 * $Id$
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 /*
29 #define LOG_DEBUG
30 */
31
32 #include <inttypes.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <string.h>
38 #include <sys/time.h>
39 #include "dvd_types.h"
40 #include "dvd_reader.h"
41 #include "nav_types.h"
42 #include "ifo_types.h" /* For vm_cmd_t */
43 #include "remap.h"
44 #include "vm/decoder.h"
45 #include "vm/vm.h"
46 #include "dvdnav.h"
47 #include "dvdnav_events.h"
48 #include "dvdnav_internal.h"
49 #include "read_cache.h"
50 #include "nav_read.h"
51 #include "remap.h"
52
53 static dvdnav_status_t dvdnav_clear(dvdnav_t * this) {
54 /* clear everything except file, vm, mutex, readahead */
55
56 pthread_mutex_lock(&this->vm_lock);
57 if (this->file) DVDCloseFile(this->file);
58 this->file = NULL;
59
60 memset(&this->pci,0,sizeof(this->pci));
61 memset(&this->dsi,0,sizeof(this->dsi));
62 this->last_cmd_nav_lbn = SRI_END_OF_CELL;
63
64 /* Set initial values of flags */
65 this->position_current.still = 0;
66 this->skip_still = 0;
67 this->sync_wait = 0;
68 this->sync_wait_skip = 0;
69 this->spu_clut_changed = 0;
70 this->started = 0;
71 this->cur_cell_time = 0;
72
73 dvdnav_read_cache_clear(this->cache);
74 pthread_mutex_unlock(&this->vm_lock);
75
76 return DVDNAV_STATUS_OK;
77 }
78
79 dvdnav_status_t dvdnav_open(dvdnav_t** dest, const char *path) {
80 dvdnav_t *this;
81 struct timeval time;
82
83 /* Create a new structure */
84 fprintf(MSG_OUT, "libdvdnav: Using dvdnav version %s from http://dvd.sf.net\n", VERSION);
85
86 (*dest) = NULL;
87 this = (dvdnav_t*)malloc(sizeof(dvdnav_t));
88 if(!this)
89 return DVDNAV_STATUS_ERR;
90 memset(this, 0, (sizeof(dvdnav_t) ) ); /* Make sure this structure is clean */
91
92 pthread_mutex_init(&this->vm_lock, NULL);
93 /* Initialise the error string */
94 printerr("");
95
96 /* Initialise the VM */
97 this->vm = vm_new_vm();
98 if(!this->vm) {
99 printerr("Error initialising the DVD VM.");
100 pthread_mutex_destroy(&this->vm_lock);
101 free(this);
102 return DVDNAV_STATUS_ERR;
103 }
104 if(!vm_reset(this->vm, path)) {
105 printerr("Error starting the VM / opening the DVD device.");
106 pthread_mutex_destroy(&this->vm_lock);
107 vm_free_vm(this->vm);
108 free(this);
109 return DVDNAV_STATUS_ERR;
110 }
111
112 /* Set the path. FIXME: Is a deep copy 'right' */
113 strncpy(this->path, path, MAX_PATH_LEN - 1);
114 this->path[MAX_PATH_LEN - 1] = '\0';
115
116 /* Pre-open and close a file so that the CSS-keys are cached. */
117 this->file = DVDOpenFile(vm_get_dvd_reader(this->vm), 0, DVD_READ_MENU_VOBS);
118
119 /* Start the read-ahead cache. */
120 this->cache = dvdnav_read_cache_new(this);
121
122 /* Seed the random numbers. So that the DVD VM Command rand()
123 * gives a different start value each time a DVD is played. */
124 gettimeofday(&time, NULL);
125 srand(time.tv_usec);
126
127 dvdnav_clear(this);
128
129 (*dest) = this;
130 return DVDNAV_STATUS_OK;
131 }
132
133 dvdnav_status_t dvdnav_close(dvdnav_t *this) {
134
135 #ifdef LOG_DEBUG
136 fprintf(MSG_OUT, "libdvdnav: close:called\n");
137 #endif
138
139 if (this->file) {
140 pthread_mutex_lock(&this->vm_lock);
141 DVDCloseFile(this->file);
142 #ifdef LOG_DEBUG
143 fprintf(MSG_OUT, "libdvdnav: close:file closing\n");
144 #endif
145 this->file = NULL;
146 pthread_mutex_unlock(&this->vm_lock);
147 }
148
149 /* Free the VM */
150 if(this->vm)
151 vm_free_vm(this->vm);
152
153 pthread_mutex_destroy(&this->vm_lock);
154
155 /* We leave the final freeing of the entire structure to the cache,
156 * because we don't know, if there are still buffers out in the wild,
157 * that must return first. */
158 if(this->cache)
159 dvdnav_read_cache_free(this->cache);
160 else
161 free(this);
162
163 return DVDNAV_STATUS_OK;
164 }
165
166 dvdnav_status_t dvdnav_reset(dvdnav_t *this) {
167 dvdnav_status_t result;
168
169 #ifdef LOG_DEBUG
170 fprintf(MSG_OUT, "libdvdnav: reset:called\n");
171 #endif
172
173 pthread_mutex_lock(&this->vm_lock);
174
175 #ifdef LOG_DEBUG
176 fprintf(MSG_OUT, "libdvdnav: reseting vm\n");
177 #endif
178 if(!vm_reset(this->vm, NULL)) {
179 printerr("Error restarting the VM.");
180 pthread_mutex_unlock(&this->vm_lock);
181 return DVDNAV_STATUS_ERR;
182 }
183 #ifdef LOG_DEBUG
184 fprintf(MSG_OUT, "libdvdnav: clearing dvdnav\n");
185 #endif
186 result = dvdnav_clear(this);
187
188 pthread_mutex_unlock(&this->vm_lock);
189 return result;
190 }
191
192 dvdnav_status_t dvdnav_path(dvdnav_t *this, const char** path) {
193 (*path) = this->path;
194
195 return DVDNAV_STATUS_OK;
196 }
197
198 const char* dvdnav_err_to_string(dvdnav_t *this) {
199
200 if(!this)
201 return "Hey! You gave me a NULL pointer you naughty person!";
202
203 return this->err_str;
204 }
205
206 /* converts a dvd_time_t to PTS ticks */
207 int64_t dvdnav_convert_time(dvd_time_t *time) {
208 int64_t result;
209 int64_t frames;
210
211 result = (time->hour >> 4 ) * 10 * 60 * 60 * 90000;
212 result += (time->hour & 0x0f) * 60 * 60 * 90000;
213 result += (time->minute >> 4 ) * 10 * 60 * 90000;
214 result += (time->minute & 0x0f) * 60 * 90000;
215 result += (time->second >> 4 ) * 10 * 90000;
216 result += (time->second & 0x0f) * 90000;
217
218 frames = ((time->frame_u & 0x30) >> 4) * 10;
219 frames += ((time->frame_u & 0x0f) ) ;
220
221 if (time->frame_u & 0x80)
222 result += frames * 3000;
223 else
224 result += frames * 3600;
225
226 return result;
227 }
228
229 /*
230 * Returns 1 if block contains NAV packet, 0 otherwise.
231 * Processes said NAV packet if present.
232 *
233 * Most of the code in here is copied from xine's MPEG demuxer
234 * so any bugs which are found in that should be corrected here also.
235 */
236 static int32_t dvdnav_decode_packet(dvdnav_t *this, uint8_t *p, dsi_t *nav_dsi, pci_t *nav_pci) {
237 int32_t bMpeg1 = 0;
238 uint32_t nHeaderLen;
239 uint32_t nPacketLen;
240 uint32_t nStreamID;
241
242 if (p[3] == 0xBA) { /* program stream pack header */
243 int32_t nStuffingBytes;
244
245 bMpeg1 = (p[4] & 0x40) == 0;
246
247 if (bMpeg1) {
248 p += 12;
249 } else { /* mpeg2 */
250 nStuffingBytes = p[0xD] & 0x07;
251 p += 14 + nStuffingBytes;
252 }
253 }
254
255 if (p[3] == 0xbb) { /* program stream system header */
256 nHeaderLen = (p[4] << 8) | p[5];
257 p += 6 + nHeaderLen;
258 }
259
260 /* we should now have a PES packet here */
261 if (p[0] || p[1] || (p[2] != 1)) {
262 fprintf(MSG_OUT, "libdvdnav: demux error! %02x %02x %02x (should be 0x000001) \n",p[0],p[1],p[2]);
263 return 0;
264 }
265
266 nPacketLen = p[4] << 8 | p[5];
267 nStreamID = p[3];
268
269 nHeaderLen = 6;
270 p += nHeaderLen;
271
272 if (nStreamID == 0xbf) { /* Private stream 2 */
273 #if 0
274 int32_t i;
275 fprintf(MSG_OUT, "libdvdnav: nav packet=%u\n",p-p_start-6);
276 for(i=0;i<80;i++)
277 fprintf(MSG_OUT, "%02x ",p[i-6]);
278 fprintf(MSG_OUT, "\n");
279 #endif
280
281 if(p[0] == 0x00) {
282 navRead_PCI(nav_pci, p+1);
283 }
284
285 p += nPacketLen;
286
287 /* We should now have a DSI packet. */
288 if(p[6] == 0x01) {
289 nPacketLen = p[4] << 8 | p[5];
290 p += 6;
291 navRead_DSI(nav_dsi, p+1);
292 }
293 return 1;
294 }
295 return 0;
296 }
297
298 /* DSI is used for most angle stuff.
299 * PCI is used for only non-seemless angle stuff
300 */
301 static int32_t dvdnav_get_vobu(dvdnav_t *this, dsi_t *nav_dsi, pci_t *nav_pci, dvdnav_vobu_t *vobu) {
302 uint32_t next;
303 int32_t angle, num_angle;
304
305 vobu->vobu_start = nav_dsi->dsi_gi.nv_pck_lbn; /* Absolute offset from start of disk */
306 vobu->vobu_length = nav_dsi->dsi_gi.vobu_ea; /* Relative offset from vobu_start */
307
308 /*
309 * If we're not at the end of this cell, we can determine the next
310 * VOBU to display using the VOBU_SRI information section of the
311 * DSI. Using this value correctly follows the current angle,
312 * avoiding the doubled scenes in The Matrix, and makes our life
313 * really happy.
314 *
315 * vobu_next is an offset value, 0x3fffffff = SRI_END_OF_CELL
316 * DVDs are about 6 Gigs, which is only up to 0x300000 blocks
317 * Should really assert if bit 31 != 1
318 */
319
320 #if 0
321 /* Old code -- may still be useful one day */
322 if(nav_dsi->vobu_sri.next_vobu != SRI_END_OF_CELL ) {
323 vobu->vobu_next = ( nav_dsi->vobu_sri.next_vobu & 0x3fffffff );
324 } else {
325 vobu->vobu_next = vobu->vobu_length;
326 }
327 #else
328 /* Relative offset from vobu_start */
329 vobu->vobu_next = ( nav_dsi->vobu_sri.next_vobu & 0x3fffffff );
330 #endif
331
332 vm_get_angle_info(this->vm, &angle, &num_angle);
333
334 /* FIMXE: The angle reset doesn't work for some reason for the moment */
335 #if 0
336 if((num_angle < angle) && (angle != 1)) {
337 fprintf(MSG_OUT, "libdvdnav: angle ends!\n");
338
339 /* This is to switch back to angle one when we
340 * finish with angles. */
341 dvdnav_angle_change(this, 1);
342 }
343 #endif
344
345 if(num_angle != 0) {
346
347 if((next = nav_pci->nsml_agli.nsml_agl_dsta[angle-1]) != 0) {
348 if((next & 0x3fffffff) != 0) {
349 if(next & 0x80000000)
350 vobu->vobu_next = - (int32_t)(next & 0x3fffffff);
351 else
352 vobu->vobu_next = + (int32_t)(next & 0x3fffffff);
353 }
354 } else if((next = nav_dsi->sml_agli.data[angle-1].address) != 0) {
355 vobu->vobu_length = nav_dsi->sml_pbi.ilvu_ea;
356
357 if((next & 0x80000000) && (next != 0x7fffffff))
358 vobu->vobu_next = - (int32_t)(next & 0x3fffffff);
359 else
360 vobu->vobu_next = + (int32_t)(next & 0x3fffffff);
361 }
362 }
363
364 return 1;
365 }
366
367 /*
368 * These are the main get_next_block function which actually get the media stream video and audio etc.
369 *
370 * There are two versions: The second one is using the zero-copy read ahead cache and therefore
371 * hands out pointers targetting directly into the cache.
372 * The first one uses a memcopy to fill this cache block into the application provided memory.
373 * The benefit of this first one is that no special memory management is needed. The application is
374 * the only one responsible of allocating and freeing the memory associated with the pointer.
375 * The drawback is the additional memcopy.
376 */
377
378 dvdnav_status_t dvdnav_get_next_block(dvdnav_t *this, uint8_t *buf,
379 int32_t *event, int32_t *len) {
380 unsigned char *block;
381 dvdnav_status_t status;
382
383 block = buf;
384 status = dvdnav_get_next_cache_block(this, &block, event, len);
385 if (status == DVDNAV_STATUS_OK && block != buf) {
386 /* we received a block from the cache, copy it, so we can give it back */
387 memcpy(buf, block, DVD_VIDEO_LB_LEN);
388 dvdnav_free_cache_block(this, block);
389 }
390 return status;
391 }
392
393 int64_t dvdnav_get_current_time(dvdnav_t *this) {
394 int i;
395 int64_t tm=0;
396 dvd_state_t *state = &this->vm->state;
397
398 for(i=0; i<state->cellN-1; i++) {
399 if(!
400 (state->pgc->cell_playback[i].block_type == BLOCK_TYPE_ANGLE_BLOCK &&
401 state->pgc->cell_playback[i].block_mode != BLOCK_MODE_FIRST_CELL)
402 )
403 tm += dvdnav_convert_time(&state->pgc->cell_playback[i].playback_time);
404 }
405 tm += this->cur_cell_time;
406
407 return tm;
408 }
409
410 dvdnav_status_t dvdnav_get_next_cache_block(dvdnav_t *this, uint8_t **buf,
411 int32_t *event, int32_t *len) {
412 dvd_state_t *state;
413 int32_t result;
414
415 pthread_mutex_lock(&this->vm_lock);
416
417 if(!this->started) {
418 /* Start the VM */
419 if (!vm_start(this->vm)) {
420 printerr("Encrypted or faulty DVD");
421 pthread_mutex_unlock(&this->vm_lock);
422 return DVDNAV_STATUS_ERR;
423 }
424 this->started = 1;
425 }
426
427 state = &(this->vm->state);
428 (*event) = DVDNAV_NOP;
429 (*len) = 0;
430
431 /* Check the STOP flag */
432 if(this->vm->stopped) {
433 vm_stop(this->vm);
434 (*event) = DVDNAV_STOP;
435 this->started = 0;
436 pthread_mutex_unlock(&this->vm_lock);
437 return DVDNAV_STATUS_OK;
438 }
439
440 vm_position_get(this->vm, &this->position_next);
441
442 #ifdef LOG_DEBUG
443 fprintf(MSG_OUT, "libdvdnav: POS-NEXT ");
444 vm_position_print(this->vm, &this->position_next);
445 fprintf(MSG_OUT, "libdvdnav: POS-CUR ");
446 vm_position_print(this->vm, &this->position_current);
447 #endif
448
449 /* did we hop? */
450 if(this->position_current.hop_channel != this->position_next.hop_channel) {
451 (*event) = DVDNAV_HOP_CHANNEL;
452 #ifdef LOG_DEBUG
453 fprintf(MSG_OUT, "libdvdnav: HOP_CHANNEL\n");
454 #endif
455 if (this->position_next.hop_channel - this->position_current.hop_channel >= HOP_SEEK) {
456 int32_t num_angles = 0, current;
457
458 /* we seeked -> check for multiple angles */
459 vm_get_angle_info(this->vm, &current, &num_angles);
460 if (num_angles > 1) {
461 int32_t result, block;
462 /* we have to skip the first VOBU when seeking in a multiangle feature,
463 * because it might belong to the wrong angle */
464 block = this->position_next.cell_start + this->position_next.block;
465 result = dvdnav_read_cache_block(this->cache, block, 1, buf);
466 if(result <= 0) {
467 printerr("Error reading NAV packet.");
468 pthread_mutex_unlock(&this->vm_lock);
469 return DVDNAV_STATUS_ERR;
470 }
471 /* Decode nav into pci and dsi. Then get next VOBU info. */
472 if(!dvdnav_decode_packet(this, *buf, &this->dsi, &this->pci)) {
473 printerr("Expected NAV packet but none found.");
474 pthread_mutex_unlock(&this->vm_lock);
475 return DVDNAV_STATUS_ERR;
476 }
477 dvdnav_get_vobu(this, &this->dsi, &this->pci, &this->vobu);
478 /* skip to next, if there is a next */
479 if (this->vobu.vobu_next != SRI_END_OF_CELL) {
480 this->vobu.vobu_start += this->vobu.vobu_next;
481 this->vobu.vobu_next = 0;
482 }
483 /* update VM state */
484 this->vm->state.blockN = this->vobu.vobu_start - this->position_next.cell_start;
485 }
486 }
487 this->position_current.hop_channel = this->position_next.hop_channel;
488 /* update VOBU info */
489 this->vobu.vobu_start = this->position_next.cell_start + this->position_next.block;
490 this->vobu.vobu_next = 0;
491 /* Make blockN == vobu_length to do expected_nav */
492 this->vobu.vobu_length = 0;
493 this->vobu.blockN = 0;
494 this->sync_wait = 0;
495 pthread_mutex_unlock(&this->vm_lock);
496 return DVDNAV_STATUS_OK;
497 }
498
499 /* Check the HIGHLIGHT flag */
500 if(this->position_current.button != this->position_next.button) {
501 dvdnav_highlight_event_t *hevent = (dvdnav_highlight_event_t *)*buf;
502
503 (*event) = DVDNAV_HIGHLIGHT;
504 #ifdef LOG_DEBUG
505 fprintf(MSG_OUT, "libdvdnav: HIGHLIGHT\n");
506 #endif
507 (*len) = sizeof(dvdnav_highlight_event_t);
508 hevent->display = 1;
509 hevent->buttonN = this->position_next.button;
510 this->position_current.button = this->position_next.button;
511 pthread_mutex_unlock(&this->vm_lock);
512 return DVDNAV_STATUS_OK;
513 }
514
515 /* Check the WAIT flag */
516 if(this->sync_wait) {
517 (*event) = DVDNAV_WAIT;
518 #ifdef LOG_DEBUG
519 fprintf(MSG_OUT, "libdvdnav: WAIT\n");
520 #endif
521 (*len) = 0;
522 pthread_mutex_unlock(&this->vm_lock);
523 return DVDNAV_STATUS_OK;
524 }
525
526 /* Check to see if we need to change the currently opened VOB */
527 if((this->position_current.vts != this->position_next.vts) ||
528 (this->position_current.domain != this->position_next.domain)) {
529 dvd_read_domain_t domain;
530 int32_t vtsN;
531 dvdnav_vts_change_event_t *vts_event = (dvdnav_vts_change_event_t *)*buf;
532
533 if(this->file) {
534 DVDCloseFile(this->file);
535 this->file = NULL;
536 }
537
538 vts_event->old_vtsN = this->position_current.vts;
539 vts_event->old_domain = this->position_current.domain;
540
541 /* Use the DOMAIN to find whether to open menu or title VOBs */
542 switch(this->position_next.domain) {
543 case FP_DOMAIN:
544 case VMGM_DOMAIN:
545 domain = DVD_READ_MENU_VOBS;
546 vtsN = 0;
547 break;
548 case VTSM_DOMAIN:
549 domain = DVD_READ_MENU_VOBS;
550 vtsN = this->position_next.vts;
551 break;
552 case VTS_DOMAIN:
553 domain = DVD_READ_TITLE_VOBS;
554 vtsN = this->position_next.vts;
555 break;
556 default:
557 printerr("Unknown domain when changing VTS.");
558 pthread_mutex_unlock(&this->vm_lock);
559 return DVDNAV_STATUS_ERR;
560 }
561
562 this->position_current.vts = this->position_next.vts;
563 this->position_current.domain = this->position_next.domain;
564 dvdnav_read_cache_clear(this->cache);
565 this->file = DVDOpenFile(vm_get_dvd_reader(this->vm), vtsN, domain);
566 vts_event->new_vtsN = this->position_next.vts;
567 vts_event->new_domain = this->position_next.domain;
568
569 /* If couldn't open the file for some reason, moan */
570 if(this->file == NULL) {
571 printerrf("Error opening vtsN=%i, domain=%i.", vtsN, domain);
572 pthread_mutex_unlock(&this->vm_lock);
573 return DVDNAV_STATUS_ERR;
574 }
575
576 /* File opened successfully so return a VTS change event */
577 (*event) = DVDNAV_VTS_CHANGE;
578 #ifdef LOG_DEBUG
579 fprintf(MSG_OUT, "libdvdnav: VTS_CHANGE\n");
580 #endif
581 (*len) = sizeof(dvdnav_vts_change_event_t);
582
583 this->spu_clut_changed = 1;
584 this->position_current.cell = -1; /* Force an update */
585 this->position_current.spu_channel = -1; /* Force an update */
586 this->position_current.audio_channel = -1; /* Force an update */;
587
588 pthread_mutex_unlock(&this->vm_lock);
589 return DVDNAV_STATUS_OK;
590 }
591
592 /* Check if the cell changed */
593 if( (this->position_current.cell != this->position_next.cell) ||
594 (this->position_current.cell_restart != this->position_next.cell_restart) ||
595 (this->position_current.cell_start != this->position_next.cell_start) ) {
596 dvdnav_cell_change_event_t *cell_event = (dvdnav_cell_change_event_t *)*buf;
597 int32_t first_cell_nr, last_cell_nr, i;
598 dvd_state_t *state = &this->vm->state;
599
600 this->cur_cell_time = 0;
601 (*event) = DVDNAV_CELL_CHANGE;
602 #ifdef LOG_DEBUG
603 fprintf(MSG_OUT, "libdvdnav: CELL_CHANGE\n");
604 #endif
605 (*len) = sizeof(dvdnav_cell_change_event_t);
606
607 cell_event->cellN = state->cellN;
608 cell_event->pgN = state->pgN;
609 cell_event->cell_length =
610 dvdnav_convert_time(&state->pgc->cell_playback[state->cellN-1].playback_time);
611
612 cell_event->pg_length = 0;
613 /* Find start cell of program. */
614 first_cell_nr = state->pgc->program_map[state->pgN-1];
615 /* Find end cell of program */
616 if(state->pgN < state->pgc->nr_of_programs)
617 last_cell_nr = state->pgc->program_map[state->pgN] - 1;
618 else
619 last_cell_nr = state->pgc->nr_of_cells;
620 for (i = first_cell_nr; i <= last_cell_nr; i++)
621 cell_event->pg_length +=
622 dvdnav_convert_time(&state->pgc->cell_playback[i - 1].playback_time);
623 cell_event->pgc_length = dvdnav_convert_time(&state->pgc->playback_time);
624
625 cell_event->cell_start = 0;
626 for (i = 1; i < state->cellN; i++)
627 cell_event->cell_start +=
628 dvdnav_convert_time(&state->pgc->cell_playback[i - 1].playback_time);
629
630 cell_event->pg_start = 0;
631 for (i = 1; i < state->pgc->program_map[state->pgN-1]; i++)
632 cell_event->pg_start +=
633 dvdnav_convert_time(&state->pgc->cell_playback[i - 1].playback_time);
634
635 this->position_current.cell = this->position_next.cell;
636 this->position_current.cell_restart = this->position_next.cell_restart;
637 this->position_current.cell_start = this->position_next.cell_start;
638 this->position_current.block = this->position_next.block;
639
640 /* vobu info is used for mid cell resumes */
641 this->vobu.vobu_start = this->position_next.cell_start + this->position_next.block;
642 this->vobu.vobu_next = 0;
643 /* Make blockN == vobu_length to do expected_nav */
644 this->vobu.vobu_length = 0;
645 this->vobu.blockN = 0;
646
647 /* update the spu palette at least on PGC changes */
648 this->spu_clut_changed = 1;
649 this->position_current.spu_channel = -1; /* Force an update */
650 this->position_current.audio_channel = -1; /* Force an update */
651
652 pthread_mutex_unlock(&this->vm_lock);
653 return DVDNAV_STATUS_OK;
654 }
655
656 /* has the CLUT changed? */
657 if(this->spu_clut_changed) {
658 (*event) = DVDNAV_SPU_CLUT_CHANGE;
659 #ifdef LOG_DEBUG
660 fprintf(MSG_OUT, "libdvdnav: SPU_CLUT_CHANGE\n");
661 #endif
662 (*len) = 16 * sizeof(uint32_t);
663 memcpy(*buf, &(state->pgc->palette), 16 * sizeof(uint32_t));
664 this->spu_clut_changed = 0;
665 pthread_mutex_unlock(&this->vm_lock);
666 return DVDNAV_STATUS_OK;
667 }
668
669 /* has the SPU channel changed? */
670 if(this->position_current.spu_channel != this->position_next.spu_channel) {
671 dvdnav_spu_stream_change_event_t *stream_change = (dvdnav_spu_stream_change_event_t *)*buf;
672
673 (*event) = DVDNAV_SPU_STREAM_CHANGE;
674 #ifdef LOG_DEBUG
675 fprintf(MSG_OUT, "libdvdnav: SPU_STREAM_CHANGE\n");
676 #endif
677 (*len) = sizeof(dvdnav_spu_stream_change_event_t);
678 stream_change->physical_wide = vm_get_subp_active_stream(this->vm, 0);
679 stream_change->physical_letterbox = vm_get_subp_active_stream(this->vm, 1);
680 stream_change->physical_pan_scan = vm_get_subp_active_stream(this->vm, 2);
681 this->position_current.spu_channel = this->position_next.spu_channel;
682 #ifdef LOG_DEBUG
683 fprintf(MSG_OUT, "libdvdnav: SPU_STREAM_CHANGE stream_id_wide=%d\n",stream_change->physical_wide);
684 fprintf(MSG_OUT, "libdvdnav: SPU_STREAM_CHANGE stream_id_letterbox=%d\n",stream_change->physical_letterbox);
685 fprintf(MSG_OUT, "libdvdnav: SPU_STREAM_CHANGE stream_id_pan_scan=%d\n",stream_change->physical_pan_scan);
686 fprintf(MSG_OUT, "libdvdnav: SPU_STREAM_CHANGE returning DVDNAV_STATUS_OK\n");
687 #endif
688 pthread_mutex_unlock(&this->vm_lock);
689 return DVDNAV_STATUS_OK;
690 }
691
692 /* has the audio channel changed? */
693 if(this->position_current.audio_channel != this->position_next.audio_channel) {
694 dvdnav_audio_stream_change_event_t *stream_change = (dvdnav_audio_stream_change_event_t *)*buf;
695
696 (*event) = DVDNAV_AUDIO_STREAM_CHANGE;
697 #ifdef LOG_DEBUG
698 fprintf(MSG_OUT, "libdvdnav: AUDIO_STREAM_CHANGE\n");
699 #endif
700 (*len) = sizeof(dvdnav_audio_stream_change_event_t);
701 stream_change->physical = vm_get_audio_active_stream( this->vm );
702 this->position_current.audio_channel = this->position_next.audio_channel;
703 #ifdef LOG_DEBUG
704 fprintf(MSG_OUT, "libdvdnav: AUDIO_STREAM_CHANGE stream_id=%d returning DVDNAV_STATUS_OK\n",stream_change->physical);
705 #endif
706 pthread_mutex_unlock(&this->vm_lock);
707 return DVDNAV_STATUS_OK;
708 }
709
710 /* Check the STILLFRAME flag */
711 if(this->position_current.still != 0) {
712 dvdnav_still_event_t *still_event = (dvdnav_still_event_t *)*buf;
713
714 (*event) = DVDNAV_STILL_FRAME;
715 #ifdef LOG_DEBUG
716 fprintf(MSG_OUT, "libdvdnav: STILL_FRAME\n");
717 #endif
718 (*len) = sizeof(dvdnav_still_event_t);
719 still_event->length = this->position_current.still;
720 pthread_mutex_unlock(&this->vm_lock);
721 return DVDNAV_STATUS_OK;
722 }
723
724 /* Have we reached the end of a VOBU? */
725 if (this->vobu.blockN >= this->vobu.vobu_length) {
726
727 /* Have we reached the end of a cell? */
728 if(this->vobu.vobu_next == SRI_END_OF_CELL) {
729 /* End of Cell from NAV DSI info */
730 #ifdef LOG_DEBUG
731 fprintf(MSG_OUT, "libdvdnav: Still set to %x\n", this->position_next.still);
732 #endif
733 this->position_current.still = this->position_next.still;
734
735 /* we are about to leave a cell, so a lot of state changes could occur;
736 * under certain conditions, the application should get in sync with us before this,
737 * otherwise it might show stills or menus too shortly */
738 if ((this->position_current.still || this->pci.hli.hl_gi.hli_ss) && !this->sync_wait_skip) {
739 this->sync_wait = 1;
740 } else {
741 if( this->position_current.still == 0 || this->skip_still ) {
742 /* no active cell still -> get us to the next cell */
743 vm_get_next_cell(this->vm);
744 this->position_current.still = 0; /* still gets activated at end of cell */
745 this->skip_still = 0;
746 this->sync_wait_skip = 0;
747 }
748 }
749 /* handle related state changes in next iteration */
750 (*event) = DVDNAV_NOP;
751 (*len) = 0;
752 pthread_mutex_unlock(&this->vm_lock);
753 return DVDNAV_STATUS_OK;
754 }
755
756 /* Perform remapping jump if necessary (this is always a
757 * VOBU boundary). */
758 if (this->vm->map) {
759 this->vobu.vobu_next = remap_block( this->vm->map,
760 this->vm->state.domain, this->vm->state.TTN_REG,
761 this->vm->state.pgN,
762 this->vobu.vobu_start, this->vobu.vobu_next);
763 }
764
765 /* at the start of the next VOBU -> expecting NAV packet */
766 result = dvdnav_read_cache_block(this->cache, this->vobu.vobu_start + this->vobu.vobu_next, 1, buf);
767
768 if(result <= 0) {
769 printerr("Error reading NAV packet.");
770 pthread_mutex_unlock(&this->vm_lock);
771 return DVDNAV_STATUS_ERR;
772 }
773 /* Decode nav into pci and dsi. Then get next VOBU info. */
774 if(!dvdnav_decode_packet(this, *buf, &this->dsi, &this->pci)) {
775 printerr("Expected NAV packet but none found.");
776 pthread_mutex_unlock(&this->vm_lock);
777 return DVDNAV_STATUS_ERR;
778 }
779 /* We need to update the vm state->blockN with which VOBU we are in.
780 * This is so RSM resumes to the VOBU level and not just the CELL level.
781 */
782 this->vm->state.blockN = this->vobu.vobu_start - this->position_current.cell_start;
783
784 dvdnav_get_vobu(this, &this->dsi, &this->pci, &this->vobu);
785 this->vobu.blockN = 0;
786 /* Give the cache a hint about the size of next VOBU.
787 * This improves pre-caching, because the VOBU will almost certainly be read entirely.
788 */
789 dvdnav_pre_cache_blocks(this->cache, this->vobu.vobu_start+1, this->vobu.vobu_length+1);
790
791 /* release NAV menu filter, when we reach the same NAV packet again */
792 if (this->last_cmd_nav_lbn == this->pci.pci_gi.nv_pck_lbn)
793 this->last_cmd_nav_lbn = SRI_END_OF_CELL;
794
795 /* Successfully got a NAV packet */
796 (*event) = DVDNAV_NAV_PACKET;
797 #ifdef LOG_DEBUG
798 fprintf(MSG_OUT, "libdvdnav: NAV_PACKET\n");
799 #endif
800 (*len) = 2048;
801 this->cur_cell_time = dvdnav_convert_time(&this->dsi.dsi_gi.c_eltm);
802 pthread_mutex_unlock(&this->vm_lock);
803 return DVDNAV_STATUS_OK;
804 }
805
806 /* If we've got here, it must just be a normal block. */
807 if(!this->file) {
808 printerr("Attempting to read without opening file.");
809 pthread_mutex_unlock(&this->vm_lock);
810 return DVDNAV_STATUS_ERR;
811 }
812
813 this->vobu.blockN++;
814 result = dvdnav_read_cache_block(this->cache, this->vobu.vobu_start + this->vobu.blockN, 1, buf);
815 if(result <= 0) {
816 printerr("Error reading from DVD.");
817 pthread_mutex_unlock(&this->vm_lock);
818 return DVDNAV_STATUS_ERR;
819 }
820 (*event) = DVDNAV_BLOCK_OK;
821 (*len) = 2048;
822
823 pthread_mutex_unlock(&this->vm_lock);
824 return DVDNAV_STATUS_OK;
825 }
826
827 dvdnav_status_t dvdnav_get_title_string(dvdnav_t *this, const char **title_str) {
828 (*title_str) = this->vm->dvd_name;
829 return DVDNAV_STATUS_OK;
830 }
831
832 uint8_t dvdnav_get_video_aspect(dvdnav_t *this) {
833 uint8_t retval;
834
835 if(!this->started) {
836 printerr("Virtual DVD machine not started.");
837 return -1;
838 }
839
840 pthread_mutex_lock(&this->vm_lock);
841 retval = (uint8_t)vm_get_video_aspect(this->vm);
842 pthread_mutex_unlock(&this->vm_lock);
843
844 return retval;
845 }
846
847 uint8_t dvdnav_get_video_scale_permission(dvdnav_t *this) {
848 uint8_t retval;
849
850 if(!this->started) {
851 printerr("Virtual DVD machine not started.");
852 return -1;
853 }
854
855 pthread_mutex_lock(&this->vm_lock);
856 retval = (uint8_t)vm_get_video_scale_permission(this->vm);
857 pthread_mutex_unlock(&this->vm_lock);
858
859 return retval;
860 }
861
862 uint16_t dvdnav_audio_stream_to_lang(dvdnav_t *this, uint8_t stream) {
863 audio_attr_t attr;
864
865 if(!this->started) {
866 printerr("Virtual DVD machine not started.");
867 return -1;
868 }
869
870 pthread_mutex_lock(&this->vm_lock);
871 attr = vm_get_audio_attr(this->vm, stream);
872 pthread_mutex_unlock(&this->vm_lock);
873
874 if(attr.lang_type != 1)
875 return 0xffff;
876
877 return attr.lang_code;
878 }
879
880 uint16_t dvdnav_audio_stream_format(dvdnav_t *this, uint8_t stream) {
881 audio_attr_t attr;
882 uint16_t format;
883
884 if(!this->started) {
885 printerr("Virtual DVD machine not started.");
886 return -1; /* 0xffff */
887 }
888
889 pthread_mutex_lock(&this->vm_lock);
890 attr = vm_get_audio_attr(this->vm, stream);
891 pthread_mutex_unlock(&this->vm_lock);
892
893 switch(attr.audio_format) {
894 case 0:
895 format = DVDNAV_FORMAT_AC3;
896 break;
897 case 2: /* MPEG-1 or MPEG-2 without extension bitstream. */
898 case 3: /* MPEG-2 with extension bitstream. */
899 format = DVDNAV_FORMAT_MPEGAUDIO;
900 break;
901 case 4:
902 format = DVDNAV_FORMAT_LPCM;
903 break;
904 case 6:
905 format = DVDNAV_FORMAT_DTS;
906 break;
907 case 7:
908 format = DVDNAV_FORMAT_SDDS;
909 break;
910 default:
911 format = 0xffff;
912 break;
913 }
914
915 return format;
916 }
917
918 uint16_t dvdnav_audio_stream_channels(dvdnav_t *this, uint8_t stream) {
919 audio_attr_t attr;
920
921 if(!this->started) {
922 printerr("Virtual DVD machine not started.");
923 return -1; /* 0xffff */
924 }
925
926 pthread_mutex_lock(&this->vm_lock);
927 attr = vm_get_audio_attr(this->vm, stream);
928 pthread_mutex_unlock(&this->vm_lock);
929
930 return attr.channels + 1;
931 }
932
933 uint16_t dvdnav_spu_stream_to_lang(dvdnav_t *this, uint8_t stream) {
934 subp_attr_t attr;
935
936 if(!this->started) {
937 printerr("Virtual DVD machine not started.");
938 return -1;
939 }
940
941 pthread_mutex_lock(&this->vm_lock);
942 attr = vm_get_subp_attr(this->vm, stream);
943 pthread_mutex_unlock(&this->vm_lock);
944
945 if(attr.type != 1)
946 return 0xffff;
947
948 return attr.lang_code;
949 }
950
951 int8_t dvdnav_get_audio_logical_stream(dvdnav_t *this, uint8_t audio_num) {
952 int8_t retval;
953
954 if(!this->started) {
955 printerr("Virtual DVD machine not started.");
956 return -1;
957 }
958
959 pthread_mutex_lock(&this->vm_lock);
960 if (!this->vm->state.pgc) {
961 printerr("No current PGC.");
962 pthread_mutex_unlock(&this->vm_lock);
963 return -1;
964 }
965 retval = vm_get_audio_stream(this->vm, audio_num);
966 pthread_mutex_unlock(&this->vm_lock);
967
968 return retval;
969 }
970
971 dvdnav_status_t dvdnav_get_audio_attr(dvdnav_t *this, uint8_t audio_num, audio_attr_t *audio_attr) {
972 if(!this->started) {
973 printerr("Virtual DVD machine not started.");
974 return -1;
975 }
976 pthread_mutex_lock(&this->vm_lock);
977 if (!this->vm->state.pgc) {
978 printerr("No current PGC.");
979 pthread_mutex_unlock(&this->vm_lock);
980 return -1;
981 }
982 *audio_attr=vm_get_audio_attr(this->vm, audio_num);
983 pthread_mutex_unlock(&this->vm_lock);
984
985 return DVDNAV_STATUS_OK;
986 }
987
988 int8_t dvdnav_get_spu_logical_stream(dvdnav_t *this, uint8_t subp_num) {
989 int8_t retval;
990
991 if(!this->started) {
992 printerr("Virtual DVD machine not started.");
993 return -1;
994 }
995
996 pthread_mutex_lock(&this->vm_lock);
997 if (!this->vm->state.pgc) {
998 printerr("No current PGC.");
999 pthread_mutex_unlock(&this->vm_lock);
1000 return -1;
1001 }
1002 retval = vm_get_subp_stream(this->vm, subp_num, 0);
1003 pthread_mutex_unlock(&this->vm_lock);
1004
1005 return retval;
1006 }
1007
1008 dvdnav_status_t dvdnav_get_spu_attr(dvdnav_t *this, uint8_t audio_num, subp_attr_t *subp_attr) {
1009 if(!this->started) {
1010 printerr("Virtual DVD machine not started.");
1011 return -1;
1012 }
1013 pthread_mutex_lock(&this->vm_lock);
1014 if (!this->vm->state.pgc) {
1015 printerr("No current PGC.");
1016 pthread_mutex_unlock(&this->vm_lock);
1017 return -1;
1018 }
1019 *subp_attr=vm_get_subp_attr(this->vm, audio_num);
1020 pthread_mutex_unlock(&this->vm_lock);
1021 return DVDNAV_STATUS_OK;
1022 }
1023
1024 int8_t dvdnav_get_active_audio_stream(dvdnav_t *this) {
1025 int8_t retval;
1026
1027 if(!this->started) {
1028 printerr("Virtual DVD machine not started.");
1029 return -1;
1030 }
1031
1032 pthread_mutex_lock(&this->vm_lock);
1033 if (!this->vm->state.pgc) {
1034 printerr("No current PGC.");
1035 pthread_mutex_unlock(&this->vm_lock);
1036 return -1;
1037 }
1038 retval = vm_get_audio_active_stream(this->vm);
1039 pthread_mutex_unlock(&this->vm_lock);
1040
1041 return retval;
1042 }
1043
1044 int8_t dvdnav_get_active_spu_stream(dvdnav_t *this) {
1045 int8_t retval;
1046
1047 if(!this->started) {
1048 printerr("Virtual DVD machine not started.");
1049 return -1;
1050 }
1051
1052 pthread_mutex_lock(&this->vm_lock);
1053 if (!this->vm->state.pgc) {
1054 printerr("No current PGC.");
1055 pthread_mutex_unlock(&this->vm_lock);
1056 return -1;
1057 }
1058 retval = vm_get_subp_active_stream(this->vm, 0);
1059 pthread_mutex_unlock(&this->vm_lock);
1060
1061 return retval;
1062 }
1063
1064 static int8_t dvdnav_is_domain(dvdnav_t *this, domain_t domain) {
1065 int8_t retval;
1066
1067 if(!this->started) {
1068 printerr("Virtual DVD machine not started.");
1069 return -1;
1070 }
1071
1072 pthread_mutex_lock(&this->vm_lock);
1073 retval = (this->vm->state.domain == domain);
1074 pthread_mutex_unlock(&this->vm_lock);
1075
1076 return retval;
1077 }
1078
1079 /* First Play domain. (Menu) */
1080 int8_t dvdnav_is_domain_fp(dvdnav_t *this) {
1081 return dvdnav_is_domain(this, FP_DOMAIN);
1082 }
1083 /* Video management Menu domain. (Menu) */
1084 int8_t dvdnav_is_domain_vmgm(dvdnav_t *this) {
1085 return dvdnav_is_domain(this, VMGM_DOMAIN);
1086 }
1087 /* Video Title Menu domain (Menu) */
1088 int8_t dvdnav_is_domain_vtsm(dvdnav_t *this) {
1089 return dvdnav_is_domain(this, VTSM_DOMAIN);
1090 }
1091 /* Video Title domain (playing movie). */
1092 int8_t dvdnav_is_domain_vts(dvdnav_t *this) {
1093 return dvdnav_is_domain(this, VTS_DOMAIN);
1094 }
1095
1096 /* Generally delegate angle information handling to VM */
1097 dvdnav_status_t dvdnav_angle_change(dvdnav_t *this, int32_t angle) {
1098 int32_t num, current;
1099
1100 pthread_mutex_lock(&this->vm_lock);
1101 vm_get_angle_info(this->vm, &current, &num);
1102 /* Set angle SPRM if valid */
1103 if((angle > 0) && (angle <= num)) {
1104 this->vm->state.AGL_REG = angle;
1105 } else {
1106 printerr("Passed an invalid angle number.");
1107 pthread_mutex_unlock(&this->vm_lock);
1108 return DVDNAV_STATUS_ERR;
1109 }
1110 pthread_mutex_unlock(&this->vm_lock);
1111
1112 return DVDNAV_STATUS_OK;
1113 }
1114
1115 dvdnav_status_t dvdnav_get_angle_info(dvdnav_t *this, int32_t *current_angle,
1116 int32_t *number_of_angles) {
1117 pthread_mutex_lock(&this->vm_lock);
1118 vm_get_angle_info(this->vm, current_angle, number_of_angles);
1119 pthread_mutex_unlock(&this->vm_lock);
1120
1121 return DVDNAV_STATUS_OK;
1122 }
1123
1124 pci_t* dvdnav_get_current_nav_pci(dvdnav_t *this) {
1125 if(!this) return 0;
1126 return &this->pci;
1127 }
1128
1129 dsi_t* dvdnav_get_current_nav_dsi(dvdnav_t *this) {
1130 if(!this) return 0;
1131 return &this->dsi;
1132 }
1133
1134 uint32_t dvdnav_get_next_still_flag(dvdnav_t *this) {
1135 if(!this) return -1;
1136 return this->position_next.still;
1137 }
1138
1139 user_ops_t dvdnav_get_restrictions(dvdnav_t* this) {
1140 /*
1141 * user_ops_t is a structure of 32 bits. We want to compute
1142 * the union of two of those bitfields so to make this quicker
1143 * than performing 32 ORs, we will access them as 32bits words.
1144 */
1145 union {
1146 user_ops_t ops_struct;
1147 uint32_t ops_int;
1148 } ops;
1149
1150 ops.ops_int = 0;
1151
1152 if(!this->started) {
1153 printerr("Virtual DVD machine not started.");
1154 return ops.ops_struct;
1155 }
1156
1157 pthread_mutex_lock(&this->vm_lock);
1158 ops.ops_int |= *(uint32_t*)&this->pci.pci_gi.vobu_uop_ctl;
1159
1160 if(this->vm && this->vm->state.pgc)
1161 ops.ops_int |= *(uint32_t*)&this->vm->state.pgc->prohibited_ops;
1162 pthread_mutex_unlock(&this->vm_lock);
1163
1164 return ops.ops_struct;
1165 }