annotate read_cache.c @ 37:832ca4921e04 src

Fixed (what appears to be) an error in JumpVTS_PTT implementation, it didn't call play_PGC after jumping.
author richwareham
date Thu, 30 May 2002 15:56:41 +0000
parents 1f29402ef2ef
children a049c3753f32
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
1 /*
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
2 * Copyright (C) 2000 Rich Wareham <richwareham@users.sourceforge.net>
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
3 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
4 * This file is part of libdvdnav, a DVD navigation library.
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
5 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
6 * libdvdnav is free software; you can redistribute it and/or modify
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
9 * (at your option) any later version.
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
10 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
11 * libdvdnav is distributed in the hope that it will be useful,
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
14 * GNU General Public License for more details.
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
15 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
19 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
20 * $Id$
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
21 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
22 */
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
23
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
25 #include "config.h"
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
26 #endif
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
27
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
28 #include "dvdnav.h"
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
29 #include "read_cache.h"
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
30
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
31 /* Read-ahead cache structure. */
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
32 #if _MULTITHREAD_
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
33 struct read_cache_s {
37
832ca4921e04 Fixed (what appears to be) an error in JumpVTS_PTT implementation, it didn't call play_PGC after jumping.
richwareham
parents: 34
diff changeset
34
832ca4921e04 Fixed (what appears to be) an error in JumpVTS_PTT implementation, it didn't call play_PGC after jumping.
richwareham
parents: 34
diff changeset
35 /* Bounds on read ahead buffer */
832ca4921e04 Fixed (what appears to be) an error in JumpVTS_PTT implementation, it didn't call play_PGC after jumping.
richwareham
parents: 34
diff changeset
36 off_t low_bound;
832ca4921e04 Fixed (what appears to be) an error in JumpVTS_PTT implementation, it didn't call play_PGC after jumping.
richwareham
parents: 34
diff changeset
37 off_t high_bound;
832ca4921e04 Fixed (what appears to be) an error in JumpVTS_PTT implementation, it didn't call play_PGC after jumping.
richwareham
parents: 34
diff changeset
38
832ca4921e04 Fixed (what appears to be) an error in JumpVTS_PTT implementation, it didn't call play_PGC after jumping.
richwareham
parents: 34
diff changeset
39 /* Where we are currently 'reading' from */
832ca4921e04 Fixed (what appears to be) an error in JumpVTS_PTT implementation, it didn't call play_PGC after jumping.
richwareham
parents: 34
diff changeset
40
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
41 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
42 dvdnav_t *dvd_self;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
43 };
37
832ca4921e04 Fixed (what appears to be) an error in JumpVTS_PTT implementation, it didn't call play_PGC after jumping.
richwareham
parents: 34
diff changeset
44
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
45 #else
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
46 struct read_cache_s {
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
47 /* Read-ahead cache. */
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
48 uint8_t *cache_buffer;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
49 int32_t cache_start_sector; /* -1 means cache invalid */
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
50 size_t cache_block_count;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
51 size_t cache_malloc_size;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
52 int cache_valid;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
53
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
54 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
55 dvdnav_t *dvd_self;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
56 };
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
57 #endif
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
58
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
59 read_cache_t *dvdnav_read_cache_new(dvdnav_t* dvd_self) {
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
60 read_cache_t *me;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
61
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
62 me = (read_cache_t*)malloc(sizeof(struct read_cache_s));
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
63
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
64 if(me) {
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
65 me->dvd_self = dvd_self;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
66
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
67 dvdnav_read_cache_clear(me);
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
68 me->cache_buffer = NULL;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
69 }
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
70
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
71 /* this->cache_start_sector = -1;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
72 this->cache_block_count = 0;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
73 this->cache_valid = 0; */
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
74
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
75 return me;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
76 }
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
77
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
78 void dvdnav_read_cache_free(read_cache_t* self) {
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
79 if(self->cache_buffer) {
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
80 free(self->cache_buffer);
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
81 self->cache_buffer = NULL;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
82 }
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
83
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
84 free(self);
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
85 }
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
86
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
87 /* This function MUST be called whenever self->file changes. */
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
88 void dvdnav_read_cache_clear(read_cache_t *self) {
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
89 if(!self)
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
90 return;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
91
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
92 self->cache_start_sector = -1;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
93 self->cache_valid = 0;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
94 }
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
95
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
96 /* This function is called just after reading the NAV packet. */
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
97 void dvdnav_pre_cache_blocks(read_cache_t *self, int sector, size_t block_count) {
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
98 int result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
99
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
100 if(!self)
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
101 return;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
102
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
103 if(!self->dvd_self->use_read_ahead) {
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
104 self->cache_valid = 0;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
105 self->cache_start_sector = -1;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
106 return;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
107 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
108
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
109 if (self->cache_buffer) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
110 if( block_count > self->cache_malloc_size) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
111 self->cache_buffer = realloc(self->cache_buffer, block_count * DVD_VIDEO_LB_LEN);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
112 self->cache_malloc_size = block_count;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
113 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
114 } else {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
115 self->cache_buffer = malloc(block_count * DVD_VIDEO_LB_LEN);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
116 self->cache_malloc_size = block_count;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
117 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
118 self->cache_start_sector = sector;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
119 self->cache_block_count = block_count;
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
120 result = DVDReadBlocks( self->dvd_self->file, sector, block_count, self->cache_buffer);
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
121 self->cache_valid = 1;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
122 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
123
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
124 /* This function will do the cache read once implemented */
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
125 int dvdnav_read_cache_block( read_cache_t *self, int sector, size_t block_count, uint8_t *buf) {
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
126 int result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
127
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
128 if(!self)
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
129 return 0;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
130
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
131 if(self->cache_valid && self->dvd_self->use_read_ahead) {
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
132 if (self->cache_start_sector != -1 ) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
133 if ((sector >= self->cache_start_sector) &&
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
134 (sector < self->cache_start_sector + self->cache_block_count)) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
135 memcpy(buf, self->cache_buffer + ((off_t)((off_t)sector - (off_t)self->cache_start_sector) * DVD_VIDEO_LB_LEN), DVD_VIDEO_LB_LEN);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
136 return DVD_VIDEO_LB_LEN;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
137 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
138 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
139 } else {
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
140 result = DVDReadBlocks( self->dvd_self->file, sector, block_count, buf);
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
141 return result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
142 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
143
3
328eadb3f37e Added initial example programs directory and make sure all debug/error output goes to stderr.
richwareham
parents: 0
diff changeset
144 fprintf(stderr,"DVD read cache miss! sector=%d, start=%d\n", sector, self->cache_start_sector);
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
145 result = DVDReadBlocks( self->dvd_self->file, sector, block_count, buf);
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
146 return result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
147 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
148
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
149