annotate read_cache.c @ 14:ebf344d11bde src

Some improvements to decoder.c Registers should be updated correctly now, but still needs checking.
author jcdutton
date Wed, 10 Apr 2002 13:09:40 +0000
parents 328eadb3f37e
children 1f29402ef2ef
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
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
28 #include "read_cache.h"
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
29
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
30 /* This function MUST be called whenever self->file changes. */
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
31 void dvdnav_read_cache_clear(dvdnav_t *self) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
32 if(!self)
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
33 return;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
34
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
35 self->cache_start_sector = -1;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
36 self->cache_valid = 0;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
37 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
38 /* This function is called just after reading the NAV packet. */
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
39 void dvdnav_pre_cache_blocks(dvdnav_t *self, int sector, size_t block_count) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
40 int result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
41
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
42 if(!self)
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
43 return;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
44
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
45 if(!self->use_read_ahead) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
46 self->cache_valid = 0;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
47 self->cache_start_sector = -1;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
48 return;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
49 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
50
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
51 if (self->cache_buffer) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
52 if( block_count > self->cache_malloc_size) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
53 self->cache_buffer = realloc(self->cache_buffer, block_count * DVD_VIDEO_LB_LEN);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
54 self->cache_malloc_size = block_count;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
55 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
56 } else {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
57 self->cache_buffer = malloc(block_count * DVD_VIDEO_LB_LEN);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
58 self->cache_malloc_size = block_count;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
59 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
60 self->cache_start_sector = sector;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
61 self->cache_block_count = block_count;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
62 result = DVDReadBlocks( self->file, sector, block_count, self->cache_buffer);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
63 self->cache_valid = 1;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
64 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
65
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
66 /* This function will do the cache read once implemented */
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
67 int dvdnav_read_cache_block( dvdnav_t *self, int sector, size_t block_count, uint8_t *buf) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
68 int result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
69
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
70 if(!self)
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
71 return 0;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
72
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
73 if(self->cache_valid && self->use_read_ahead) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
74 if (self->cache_start_sector != -1 ) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
75 if ((sector >= self->cache_start_sector) &&
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
76 (sector < self->cache_start_sector + self->cache_block_count)) {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
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);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
78 return DVD_VIDEO_LB_LEN;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
79 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
80 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
81 } else {
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
82 result = DVDReadBlocks( self->file, sector, block_count, buf);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
83 return result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
84 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
85
3
328eadb3f37e Added initial example programs directory and make sure all debug/error output goes to stderr.
richwareham
parents: 0
diff changeset
86 fprintf(stderr,"DVD read cache miss! sector=%d, start=%d\n", sector, self->cache_start_sector);
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
87 result = DVDReadBlocks( self->file, sector, block_count, buf);
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
88 return result;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
89 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
90
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
91