annotate read_cache.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
1 /*
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
2 * Copyright (C) 2000 Rich Wareham <richwareham@users.sourceforge.net>
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
3 * 2001-2004 the dvdnav project
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
4 *
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
5 * This file is part of libdvdnav, a DVD navigation library.
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
6 *
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
7 * libdvdnav is free software; you can redistribute it and/or modify
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
10 * (at your option) any later version.
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
11 *
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
12 * libdvdnav is distributed in the hope that it will be useful,
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
15 * GNU General Public License for more details.
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
16 *
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
20 *
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
21 * $Id$
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
22 *
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
23 */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
24 /*
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
25 * There was a multithreaded read ahead cache in here for some time, but
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
26 * it had only been used for a short time. If you want to have a look at it,
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
27 * search the CVS attic.
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
28 */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
29
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
30 #ifdef HAVE_CONFIG_H
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
31 #include "config.h"
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
32 #endif
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
33
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
34 #include <inttypes.h>
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
35 #include <stdlib.h>
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
36 #include <limits.h>
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
37 #include <sys/time.h>
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
38 #include <time.h>
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
39 #include "dvd_types.h"
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
40 #include "nav_types.h"
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
41 #include "ifo_types.h"
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
42 #include "remap.h"
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
43 #include "vm/decoder.h"
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
44 #include "vm/vm.h"
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
45 #include "dvdnav.h"
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
46 #include "dvdnav_internal.h"
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
47 #include "read_cache.h"
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
48
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
49 #define READ_CACHE_CHUNKS 10
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
50
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
51 /* all cache chunks must be memory aligned to allow use of raw devices */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
52 #define ALIGNMENT 2048
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
53
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
54 #define READ_AHEAD_SIZE_MIN 4
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
55 #define READ_AHEAD_SIZE_MAX 512
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
56
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
57 typedef struct read_cache_chunk_s {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
58 uint8_t *cache_buffer;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
59 uint8_t *cache_buffer_base; /* used in malloc and free for alignment */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
60 int32_t cache_start_sector; /* -1 means cache invalid */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
61 int32_t cache_read_count; /* this many sectors are already read */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
62 size_t cache_block_count; /* this many sectors will go in this chunk */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
63 size_t cache_malloc_size;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
64 int cache_valid;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
65 int usage_count; /* counts how many buffers where issued from this chunk */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
66 } read_cache_chunk_t;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
67
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
68 struct read_cache_s {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
69 read_cache_chunk_t chunk[READ_CACHE_CHUNKS];
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
70 int current;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
71 int freeing; /* is set to one when we are about to dispose the cache */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
72 uint32_t read_ahead_size;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
73 int read_ahead_incr;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
74 int last_sector;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
75 pthread_mutex_t lock;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
76
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
77 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
78 dvdnav_t *dvd_self;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
79 };
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
80
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
81 /*
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
82 #define READ_CACHE_TRACE 0
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
83 */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
84
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
85 #ifdef __GNUC__
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
86 # if READ_CACHE_TRACE
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
87 # define dprintf(fmt, args...) fprintf(MSG_OUT, "libdvdnav: %s: "fmt, __func__ , ## args)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
88 # else
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
89 # define dprintf(fmt, args...) /* Nowt */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
90 # endif
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
91 #else
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
92 # if READ_CACHE_TRACE
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
93 # define dprintf(fmt, ...) fprintf(MSG_OUT, "libdvdnav: %s: "fmt, __func__ , __VA_ARGS__)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
94 # else
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
95 #ifdef _MSC_VER
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
96 # define dprintf(fmt, str) /* Nowt */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
97 #else
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
98 # define dprintf(fmt, ...) /* Nowt */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
99 #endif /* _MSC_VER */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
100 # endif
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
101 #endif
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
102
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
103
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
104 read_cache_t *dvdnav_read_cache_new(dvdnav_t* dvd_self) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
105 read_cache_t *self;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
106 int i;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
107
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
108 self = (read_cache_t *)malloc(sizeof(read_cache_t));
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
109
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
110 if(self) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
111 self->current = 0;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
112 self->freeing = 0;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
113 self->dvd_self = dvd_self;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
114 self->last_sector = 0;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
115 self->read_ahead_size = READ_AHEAD_SIZE_MIN;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
116 self->read_ahead_incr = 0;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
117 pthread_mutex_init(&self->lock, NULL);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
118 dvdnav_read_cache_clear(self);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
119 for (i = 0; i < READ_CACHE_CHUNKS; i++) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
120 self->chunk[i].cache_buffer = NULL;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
121 self->chunk[i].usage_count = 0;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
122 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
123 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
124
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
125 return self;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
126 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
127
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
128 void dvdnav_read_cache_free(read_cache_t* self) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
129 dvdnav_t *tmp;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
130 int i;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
131
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
132 pthread_mutex_lock(&self->lock);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
133 self->freeing = 1;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
134 for (i = 0; i < READ_CACHE_CHUNKS; i++)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
135 if (self->chunk[i].cache_buffer && self->chunk[i].usage_count == 0) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
136 free(self->chunk[i].cache_buffer_base);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
137 self->chunk[i].cache_buffer = NULL;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
138 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
139 pthread_mutex_unlock(&self->lock);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
140
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
141 for (i = 0; i < READ_CACHE_CHUNKS; i++)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
142 if (self->chunk[i].cache_buffer) return;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
143
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
144 /* all buffers returned, free everything */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
145 tmp = self->dvd_self;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
146 pthread_mutex_destroy(&self->lock);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
147 free(self);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
148 free(tmp);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
149 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
150
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
151 /* This function MUST be called whenever self->file changes. */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
152 void dvdnav_read_cache_clear(read_cache_t *self) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
153 int i;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
154
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
155 if(!self)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
156 return;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
157
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
158 pthread_mutex_lock(&self->lock);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
159 for (i = 0; i < READ_CACHE_CHUNKS; i++)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
160 self->chunk[i].cache_valid = 0;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
161 pthread_mutex_unlock(&self->lock);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
162 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
163
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
164 /* This function is called just after reading the NAV packet. */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
165 void dvdnav_pre_cache_blocks(read_cache_t *self, int sector, size_t block_count) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
166 int i, use;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
167
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
168 if(!self)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
169 return;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
170
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
171 if(!self->dvd_self->use_read_ahead)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
172 return;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
173
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
174 pthread_mutex_lock(&self->lock);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
175
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
176 /* find a free cache chunk that best fits the required size */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
177 use = -1;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
178 for (i = 0; i < READ_CACHE_CHUNKS; i++)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
179 if (self->chunk[i].usage_count == 0 && self->chunk[i].cache_buffer &&
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
180 self->chunk[i].cache_malloc_size >= block_count &&
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
181 (use == -1 || self->chunk[use].cache_malloc_size > self->chunk[i].cache_malloc_size))
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
182 use = i;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
183
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
184 if (use == -1) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
185 /* we haven't found a cache chunk, so we try to reallocate an existing one */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
186 for (i = 0; i < READ_CACHE_CHUNKS; i++)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
187 if (self->chunk[i].usage_count == 0 && self->chunk[i].cache_buffer &&
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
188 (use == -1 || self->chunk[use].cache_malloc_size < self->chunk[i].cache_malloc_size))
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
189 use = i;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
190 if (use >= 0) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
191 self->chunk[use].cache_buffer_base = realloc(self->chunk[use].cache_buffer_base,
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
192 block_count * DVD_VIDEO_LB_LEN + ALIGNMENT);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
193 self->chunk[use].cache_buffer =
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
194 (uint8_t *)(((uintptr_t)self->chunk[use].cache_buffer_base & ~((uintptr_t)(ALIGNMENT - 1))) + ALIGNMENT);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
195 dprintf("pre_cache DVD read realloc happened\n");
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
196 self->chunk[use].cache_malloc_size = block_count;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
197 } else {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
198 /* we still haven't found a cache chunk, let's allocate a new one */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
199 for (i = 0; i < READ_CACHE_CHUNKS; i++)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
200 if (!self->chunk[i].cache_buffer) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
201 use = i;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
202 break;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
203 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
204 if (use >= 0) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
205 /* We start with a sensible figure for the first malloc of 500 blocks.
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
206 * Some DVDs I have seen venture to 450 blocks.
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
207 * This is so that fewer realloc's happen if at all.
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
208 */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
209 self->chunk[i].cache_buffer_base =
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
210 malloc((block_count > 500 ? block_count : 500) * DVD_VIDEO_LB_LEN + ALIGNMENT);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
211 self->chunk[i].cache_buffer =
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
212 (uint8_t *)(((uintptr_t)self->chunk[i].cache_buffer_base & ~((uintptr_t)(ALIGNMENT - 1))) + ALIGNMENT);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
213 self->chunk[i].cache_malloc_size = block_count > 500 ? block_count : 500;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
214 dprintf("pre_cache DVD read malloc %d blocks\n",
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
215 (block_count > 500 ? block_count : 500 ));
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
216 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
217 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
218 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
219
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
220 if (use >= 0) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
221 self->chunk[use].cache_start_sector = sector;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
222 self->chunk[use].cache_block_count = block_count;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
223 self->chunk[use].cache_read_count = 0;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
224 self->chunk[use].cache_valid = 1;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
225 self->current = use;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
226 } else {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
227 dprintf("pre_caching was impossible, no cache chunk available\n");
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
228 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
229 pthread_mutex_unlock(&self->lock);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
230 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
231
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
232 int dvdnav_read_cache_block(read_cache_t *self, int sector, size_t block_count, uint8_t **buf) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
233 int i, use;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
234 int start;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
235 int size;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
236 int incr;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
237 uint8_t *read_ahead_buf;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
238 int32_t res;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
239
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
240 if(!self)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
241 return 0;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
242
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
243 use = -1;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
244
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
245 if(self->dvd_self->use_read_ahead) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
246 /* first check, if sector is in current chunk */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
247 read_cache_chunk_t cur = self->chunk[self->current];
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
248 if (cur.cache_valid && sector >= cur.cache_start_sector &&
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
249 sector <= (cur.cache_start_sector + cur.cache_read_count) &&
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
250 sector + block_count <= cur.cache_start_sector + cur.cache_block_count)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
251 use = self->current;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
252 else
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
253 for (i = 0; i < READ_CACHE_CHUNKS; i++)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
254 if (self->chunk[i].cache_valid &&
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
255 sector >= self->chunk[i].cache_start_sector &&
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
256 sector <= (self->chunk[i].cache_start_sector + self->chunk[i].cache_read_count) &&
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
257 sector + block_count <= self->chunk[i].cache_start_sector + self->chunk[i].cache_block_count)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
258 use = i;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
259 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
260
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
261 if (use >= 0) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
262 read_cache_chunk_t *chunk;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
263
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
264 /* Increment read-ahead size if sector follows the last sector */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
265 if (sector == (self->last_sector + 1)) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
266 if (self->read_ahead_incr < READ_AHEAD_SIZE_MAX)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
267 self->read_ahead_incr++;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
268 } else {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
269 self->read_ahead_size = READ_AHEAD_SIZE_MIN;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
270 self->read_ahead_incr = 0;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
271 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
272 self->last_sector = sector;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
273
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
274 /* The following resources need to be protected by a mutex :
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
275 * self->chunk[*].cache_buffer
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
276 * self->chunk[*].cache_malloc_size
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
277 * self->chunk[*].usage_count
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
278 */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
279 pthread_mutex_lock(&self->lock);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
280 chunk = &self->chunk[use];
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
281 read_ahead_buf = chunk->cache_buffer + chunk->cache_read_count * DVD_VIDEO_LB_LEN;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
282 *buf = chunk->cache_buffer + (sector - chunk->cache_start_sector) * DVD_VIDEO_LB_LEN;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
283 chunk->usage_count++;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
284 pthread_mutex_unlock(&self->lock);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
285
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
286 dprintf("libdvdnav: sector=%d, start_sector=%d, last_sector=%d\n", sector, chunk->cache_start_sector, chunk->cache_start_sector + chunk->cache_block_count);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
287
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
288 /* read_ahead_size */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
289 incr = self->read_ahead_incr >> 1;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
290 if ((self->read_ahead_size + incr) > READ_AHEAD_SIZE_MAX) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
291 self->read_ahead_size = READ_AHEAD_SIZE_MAX;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
292 } else {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
293 self->read_ahead_size += incr;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
294 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
295
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
296 /* real read size */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
297 start = chunk->cache_start_sector + chunk->cache_read_count;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
298 if (chunk->cache_read_count + self->read_ahead_size > chunk->cache_block_count) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
299 size = chunk->cache_block_count - chunk->cache_read_count;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
300 } else {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
301 size = self->read_ahead_size;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
302 /* ensure that the sector we want will be read */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
303 if (sector >= chunk->cache_start_sector + chunk->cache_read_count + size)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
304 size = sector - chunk->cache_start_sector - chunk->cache_read_count;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
305 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
306 dprintf("libdvdnav: read_ahead_size=%d, size=%d\n", self->read_ahead_size, size);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
307
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
308 if (size)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
309 chunk->cache_read_count += DVDReadBlocks(self->dvd_self->file,
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
310 start,
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
311 size,
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
312 read_ahead_buf);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
313
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
314 res = DVD_VIDEO_LB_LEN * block_count;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
315
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
316 } else {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
317
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
318 if (self->dvd_self->use_read_ahead)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
319 dprintf("cache miss on sector %d\n", sector);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
320
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
321 res = DVDReadBlocks(self->dvd_self->file,
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
322 sector,
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
323 block_count,
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
324 *buf) * DVD_VIDEO_LB_LEN;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
325 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
326
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
327 return res;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
328
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
329 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
330
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
331 dvdnav_status_t dvdnav_free_cache_block(dvdnav_t *self, unsigned char *buf) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
332 read_cache_t *cache;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
333 int i;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
334
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
335 if (!self)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
336 return DVDNAV_STATUS_ERR;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
337
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
338 cache = self->cache;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
339 if (!cache)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
340 return DVDNAV_STATUS_ERR;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
341
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
342 pthread_mutex_lock(&cache->lock);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
343 for (i = 0; i < READ_CACHE_CHUNKS; i++) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
344 if (cache->chunk[i].cache_buffer && buf >= cache->chunk[i].cache_buffer &&
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
345 buf < cache->chunk[i].cache_buffer + cache->chunk[i].cache_malloc_size * DVD_VIDEO_LB_LEN) {
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
346 cache->chunk[i].usage_count--;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
347 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
348 }
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
349 pthread_mutex_unlock(&cache->lock);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
350
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
351 if (cache->freeing)
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
352 /* when we want to dispose the cache, try freeing it now */
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
353 dvdnav_read_cache_free(cache);
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
354
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
355 return DVDNAV_STATUS_OK;
427b7da5cbdb first split of dvdread; it's just a copy of dvdnav still to be cleaned
nicodvb
parents:
diff changeset
356 }