comparison read_cache.c @ 0:3ddf0eaece51 src

Initial revision
author richwareham
date Tue, 12 Mar 2002 19:45:53 +0000
parents
children 328eadb3f37e
comparison
equal deleted inserted replaced
-1:000000000000 0:3ddf0eaece51
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 #include "read_cache.h"
29
30 /* This function MUST be called whenever self->file changes. */
31 void dvdnav_read_cache_clear(dvdnav_t *self) {
32 if(!self)
33 return;
34
35 self->cache_start_sector = -1;
36 self->cache_valid = 0;
37 }
38 /* This function is called just after reading the NAV packet. */
39 void dvdnav_pre_cache_blocks(dvdnav_t *self, int sector, size_t block_count) {
40 int result;
41
42 if(!self)
43 return;
44
45 if(!self->use_read_ahead) {
46 self->cache_valid = 0;
47 self->cache_start_sector = -1;
48 return;
49 }
50
51 if (self->cache_buffer) {
52 if( block_count > self->cache_malloc_size) {
53 self->cache_buffer = realloc(self->cache_buffer, block_count * DVD_VIDEO_LB_LEN);
54 self->cache_malloc_size = block_count;
55 }
56 } else {
57 self->cache_buffer = malloc(block_count * DVD_VIDEO_LB_LEN);
58 self->cache_malloc_size = block_count;
59 }
60 self->cache_start_sector = sector;
61 self->cache_block_count = block_count;
62 result = DVDReadBlocks( self->file, sector, block_count, self->cache_buffer);
63 self->cache_valid = 1;
64 }
65
66 /* This function will do the cache read once implemented */
67 int dvdnav_read_cache_block( dvdnav_t *self, int sector, size_t block_count, uint8_t *buf) {
68 int result;
69
70 if(!self)
71 return 0;
72
73 if(self->cache_valid && self->use_read_ahead) {
74 if (self->cache_start_sector != -1 ) {
75 if ((sector >= self->cache_start_sector) &&
76 (sector < self->cache_start_sector + self->cache_block_count)) {
77 memcpy(buf, self->cache_buffer + ((off_t)((off_t)sector - (off_t)self->cache_start_sector) * DVD_VIDEO_LB_LEN), DVD_VIDEO_LB_LEN);
78 return DVD_VIDEO_LB_LEN;
79 }
80 }
81 } else {
82 result = DVDReadBlocks( self->file, sector, block_count, buf);
83 return result;
84 }
85
86 printf("DVD read cache miss! sector=%d, start=%d\n", sector, self->cache_start_sector);
87 result = DVDReadBlocks( self->file, sector, block_count, buf);
88 return result;
89 }
90
91