]> git.sesse.net Git - vlc/blob - modules/access/dvd/seek.c
* ./configure.ac.in: removed -W in favour of -Wtraditional.
[vlc] / modules / access / dvd / seek.c
1 /* seek.c: functions to navigate through DVD.
2  *****************************************************************************
3  * Copyright (C) 1998-2001 VideoLAN
4  * $Id: seek.c,v 1.3 2002/12/06 16:34:04 sam Exp $
5  *
6  * Author: Stéphane Borel <stef@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #ifdef HAVE_UNISTD_H
34 #   include <unistd.h>
35 #endif
36
37 #include <fcntl.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <string.h>
41 #include <errno.h>
42
43 #ifdef STRNCASECMP_IN_STRINGS_H
44 #   include <strings.h>
45 #endif
46
47 #ifdef GOD_DAMN_DMCA
48 #   include "dvdcss.h"
49 #else
50 #   include <dvdcss/dvdcss.h>
51 #endif
52
53 #include "dvd.h"
54 #include "seek.h"
55 #include "ifo.h"
56
57 #define title \
58     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
59 #define cell  p_dvd->p_ifo->vts.cell_inf
60
61 int CellIsInterleaved( thread_dvd_data_t * p_dvd )
62 {
63     return title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000;
64 }
65
66 int CellPrg2Map( thread_dvd_data_t * p_dvd )
67 {
68     int     i_cell;
69
70     i_cell = p_dvd->i_map_cell;
71
72     if( i_cell >= cell.i_cell_nb )
73     {
74         return -1;
75     }
76
77     while( ( i_cell < cell.i_cell_nb ) &&
78            ( ( title.p_cell_pos[p_dvd->i_prg_cell].i_vob_id !=
79                cell.p_cell_map[i_cell].i_vob_id ) ||
80              ( title.p_cell_pos[p_dvd->i_prg_cell].i_cell_id !=
81                cell.p_cell_map[i_cell].i_cell_id ) ) )
82     {
83         i_cell++;
84     }
85
86     if( i_cell >= cell.i_cell_nb )
87     {
88         return -1;
89     }
90
91     return i_cell;
92 }
93
94 int CellAngleOffset( thread_dvd_data_t * p_dvd, int i_prg_cell )
95 {
96     int     i_cell_off;
97
98     if( i_prg_cell >= title.i_cell_nb )
99     {
100         return 0;
101     }
102
103     /* basic handling of angles */
104     switch( ( ( title.p_cell_play[i_prg_cell].i_category & 0xf000 )
105                     >> 12 ) )
106     {
107         /* we enter a muli-angle section */
108         case 0x5:
109             i_cell_off = p_dvd->i_angle - 1;
110             p_dvd->i_angle_cell = 0;
111             break;
112         /* we exit a multi-angle section */
113         case 0x9:
114         case 0xd:
115             i_cell_off = p_dvd->i_angle_nb - p_dvd->i_angle;
116             break;
117         default:
118             i_cell_off = 0;
119     }
120
121     return i_cell_off;
122 }
123
124 int CellFirstSector( thread_dvd_data_t * p_dvd )
125 {
126     return __MAX( cell.p_cell_map[p_dvd->i_map_cell].i_first_sector,
127                   title.p_cell_play[p_dvd->i_prg_cell].i_first_sector );
128 }
129
130 int CellLastSector( thread_dvd_data_t * p_dvd )
131 {
132     return __MIN( cell.p_cell_map[p_dvd->i_map_cell].i_last_sector,
133                   title.p_cell_play[p_dvd->i_prg_cell].i_last_sector );
134 }
135
136 int NextCellPrg( thread_dvd_data_t * p_dvd )
137 {
138     unsigned int i_cell = p_dvd->i_prg_cell;
139
140     if( p_dvd->i_vts_lb > title.p_cell_play[i_cell].i_last_sector )
141     {
142         i_cell ++;
143         i_cell += CellAngleOffset( p_dvd, i_cell );
144
145         if( i_cell >= title.i_cell_nb )
146         {
147             return -1;
148         }
149     }
150
151     return i_cell;
152 }
153
154 int Lb2CellPrg( thread_dvd_data_t * p_dvd )
155 {
156     unsigned int i_cell = 0;
157
158     while( p_dvd->i_vts_lb > title.p_cell_play[i_cell].i_last_sector )
159     {
160         i_cell ++;
161         i_cell += CellAngleOffset( p_dvd, i_cell );
162
163         if( i_cell >= title.i_cell_nb )
164         {
165             return -1;
166         }
167     }
168
169     return i_cell;
170 }
171
172 int Lb2CellMap( thread_dvd_data_t * p_dvd )
173 {
174     int     i_cell = 0;
175
176     while( p_dvd->i_vts_lb > cell.p_cell_map[i_cell].i_last_sector )
177     {
178         i_cell ++;
179
180         if( i_cell >= cell.i_cell_nb )
181         {
182             return -1;
183         }
184     }
185
186     return i_cell;
187 }
188
189 int LbMaxOnce( thread_dvd_data_t * p_dvd )
190 {
191     int i_block_once = p_dvd->i_last_lb + 1 - p_dvd->i_vts_lb;
192
193     /* Get the position of the next cell if we're at cell end */
194     if( i_block_once <= 0 )
195     {
196         p_dvd->i_map_cell++;
197         p_dvd->i_angle_cell++;
198
199         p_dvd->i_prg_cell = NextCellPrg( p_dvd );
200         if( p_dvd->i_prg_cell < 0 )
201         {
202             /* EOF */
203             return 0;
204         }
205
206         p_dvd->i_map_cell = CellPrg2Map( p_dvd );
207         if( p_dvd->i_map_cell < 0 )
208         {
209             return 0;
210         }
211
212         p_dvd->i_vts_lb   = CellFirstSector( p_dvd );
213         p_dvd->i_last_lb  = CellLastSector( p_dvd );
214
215         p_dvd->i_chapter = NextChapter( p_dvd );
216         if( p_dvd->i_chapter < 0 )
217         {
218             return 0;
219         }
220
221         /* Position the fd pointer on the right address */
222         if( dvdcss_seek( p_dvd->dvdhandle,
223                          p_dvd->i_vts_start + p_dvd->i_vts_lb,
224                          DVDCSS_SEEK_MPEG ) < 0 )
225         {
226 #if 0
227             intf_ErrMsg( "dvd error: %s",
228                          dvdcss_error( p_dvd->dvdhandle ) );
229 #endif
230             return 0;
231         }
232
233         i_block_once = p_dvd->i_last_lb + 1 - p_dvd->i_vts_lb;
234     }
235
236     return i_block_once;
237 }
238
239
240 int CellPrg2Chapter( thread_dvd_data_t * p_dvd )
241 {
242     unsigned int i_chapter = 1;
243     unsigned int i_cell    = p_dvd->i_prg_cell;
244
245     if( CellIsInterleaved( p_dvd ) )
246     {
247         i_cell -= (p_dvd->i_angle - 1);
248     }
249
250     while( title.chapter_map.pi_start_cell[i_chapter] <= i_cell+1 )
251     {
252         i_chapter ++;
253         if( i_chapter >= p_dvd->i_chapter_nb )
254         {
255             return p_dvd->i_chapter_nb;
256         }
257     }
258
259     return i_chapter;
260 }
261
262 int NextChapter( thread_dvd_data_t * p_dvd )
263 {
264     int i_cell = p_dvd->i_prg_cell;
265
266     if( CellIsInterleaved( p_dvd ) )
267     {
268         i_cell -= (p_dvd->i_angle - 1);
269     }
270
271     if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <= i_cell+1 )
272     {
273         p_dvd->i_chapter++;
274         if( p_dvd->i_chapter > p_dvd->i_chapter_nb )
275         {
276             return -1;
277         }
278         p_dvd->b_new_chapter = 1;
279
280         return p_dvd->i_chapter;
281     }
282
283     return p_dvd->i_chapter;
284 }
285
286
287
288 int DVDSetChapter( thread_dvd_data_t * p_dvd, unsigned int i_chapter )
289 {
290     if( i_chapter <= 0 || i_chapter > p_dvd->i_chapter_nb )
291     {
292         i_chapter = 1;
293     }
294
295     if( p_dvd->i_chapter != i_chapter )
296     {
297         /* Find cell index in Program chain for current chapter */
298         p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
299         p_dvd->i_prg_cell += CellAngleOffset( p_dvd, p_dvd->i_prg_cell );
300         if( i_chapter < p_dvd->i_chapter )
301         {
302             p_dvd->i_map_cell = 0;
303         }
304         p_dvd->i_map_cell = CellPrg2Map( p_dvd );
305         p_dvd->i_vts_lb   = CellFirstSector( p_dvd );
306         p_dvd->i_last_lb  = CellLastSector( p_dvd );
307
308         /* Position the fd pointer on the right address */
309         if( dvdcss_seek( p_dvd->dvdhandle,
310                          p_dvd->i_vts_start + p_dvd->i_vts_lb,
311                          DVDCSS_SEEK_MPEG ) < 0 )
312         {
313 #if 0
314             intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
315 #endif
316             return -1;
317         }
318
319 #if 0
320         intf_WarnMsg( 4, "dvd info: chapter %d prg_cell %d map_cell %d",
321                 i_chapter, p_dvd->i_prg_cell, p_dvd->i_map_cell );
322 #endif
323     }
324
325     return i_chapter;
326 }
327
328
329 #undef cell
330 #undef title