File: track.c

package info (click to toggle)
libcdio 0.78.2%2Bdfsg1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 11,492 kB
  • ctags: 4,911
  • sloc: ansic: 36,479; sh: 9,268; cpp: 2,554; makefile: 736; perl: 29
file content (315 lines) | stat: -rw-r--r-- 8,548 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
    $Id: track.c,v 1.4 2005/02/06 04:20:25 rocky Exp $

    Copyright (C) 2003, 2004, 2005 Rocky Bernstein <rocky@panix.com>
    Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/*! Track-related routines. */


#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <cdio/cdio.h>
#include "cdio_private.h"

const char *track_format2str[6] = 
  {
    "audio", "CD-i", "XA", "data", "PSX", "error"
  };

/* Variables to hold debugger-helping enumerations */
enum cdio_track_enums;

/*!
  Return the number of the first track. 
  CDIO_INVALID_TRACK is returned on error.
*/
track_t
cdio_get_first_track_num (const CdIo_t *p_cdio)
{
  if (NULL == p_cdio) return CDIO_INVALID_TRACK;

  if (p_cdio->op.get_first_track_num) {
    return p_cdio->op.get_first_track_num (p_cdio->env);
  } else {
    return CDIO_INVALID_TRACK;
  }
}

/*!
  Return the last track number.
  CDIO_INVALID_TRACK is returned on error.
*/
track_t
cdio_get_last_track_num (const CdIo_t *p_cdio)
{
  if (NULL == p_cdio) return CDIO_INVALID_TRACK;
  {
    const track_t i_first_track = cdio_get_first_track_num(p_cdio);
    if ( CDIO_INVALID_TRACK != i_first_track ) {
      const track_t i_tracks = cdio_get_num_tracks(p_cdio);
      if ( CDIO_INVALID_TRACK != i_tracks ) 
	return i_first_track + i_tracks - 1;
    }
    return CDIO_INVALID_TRACK;
  }
}

/*! Return number of channels in track: 2 or 4; -2 if not
  implemented or -1 for error.
  Not meaningful if track is not an audio track.
*/
int
cdio_get_track_channels(const CdIo_t *p_cdio, track_t i_track)
{
  if (p_cdio->op.get_track_channels) {
    return p_cdio->op.get_track_channels (p_cdio->env, i_track);
  } else {
    return -2;
  }
}

/*! Return copy protection status on a track. Is this meaningful
  if not an audio track?
*/
track_flag_t
cdio_get_track_copy_permit(const CdIo_t *p_cdio, track_t i_track)
{
  if (p_cdio->op.get_track_copy_permit) {
    return p_cdio->op.get_track_copy_permit (p_cdio->env, i_track);
  } else {
    return CDIO_TRACK_FLAG_UNKNOWN;
  }
}

/*!  
  Get format of track. 
*/
track_format_t
cdio_get_track_format(const CdIo_t *p_cdio, track_t i_track)
{
  if (!p_cdio) return TRACK_FORMAT_ERROR;

  if (p_cdio->op.get_track_format) {
    return p_cdio->op.get_track_format (p_cdio->env, i_track);
  } else {
    return TRACK_FORMAT_ERROR;
  }
}
/*!  
  Return the Joliet level recognized for p_cdio.
*/
uint8_t 
cdio_get_joliet_level(const CdIo_t *p_cdio)
{
  if (!p_cdio) return 0;
  {
    const generic_img_private_t *p_env 
      = (generic_img_private_t *) (p_cdio->env);
    return p_env->i_joliet_level;
  }
}

/*! 
  Return the number of tracks in the current medium.
  CDIO_INVALID_TRACK is returned on error.
*/
track_t
cdio_get_num_tracks (const CdIo_t *p_cdio)
{
  if (p_cdio == NULL) return CDIO_INVALID_TRACK;

  if (p_cdio->op.get_num_tracks) {
    return p_cdio->op.get_num_tracks (p_cdio->env);
  } else {
    return CDIO_INVALID_TRACK;
  }
}

/*! Find the track which contans lsn.
    CDIO_INVALID_TRACK is returned if the lsn outside of the CD or
    if there was some error. 
    
    If the lsn is before the pregap of the first track 0 is returned.
    Otherwise we return the track that spans the lsn.
*/
track_t
cdio_get_track(const CdIo_t *p_cdio, lsn_t lsn)
{
  if (!p_cdio) return CDIO_INVALID_TRACK;
  
  {
    track_t i_low_track   = cdio_get_first_track_num(p_cdio);
    track_t i_high_track  = cdio_get_last_track_num(p_cdio)+1; /* LEADOUT */

    if (CDIO_INVALID_TRACK == i_low_track 
	|| CDIO_INVALID_TRACK == i_high_track ) return CDIO_INVALID_TRACK;
    
    if (lsn < cdio_get_track_lsn(p_cdio, i_low_track))
      return 0; /* We're in the pre-gap of first track */

    if (lsn > cdio_get_track_lsn(p_cdio, i_high_track))
      return CDIO_INVALID_TRACK; /* We're beyond the end. */

    do {
      const track_t i_mid = (i_low_track + i_high_track) / 2;
      const lsn_t i_mid_lsn = cdio_get_track_lsn(p_cdio, i_mid);
      if (lsn <= i_mid_lsn) i_high_track = i_mid - 1;
      if (lsn >= i_mid_lsn) i_low_track  = i_mid + 1;
    } while ( i_low_track <= i_high_track );

    return (i_low_track > i_high_track + 1) 
      ? i_high_track + 1 : i_high_track;
  }
}

/*!
  Return true if we have XA data (green, mode2 form1) or
  XA data (green, mode2 form2). That is track begins:
  sync - header - subheader
  12     4      -  8
  
  FIXME: there's gotta be a better design for this and get_track_format?
*/
bool
cdio_get_track_green(const CdIo_t *p_cdio, track_t i_track)
{
  if (p_cdio == NULL) {
    return false;
  }

  if (p_cdio->op.get_track_green) {
    return p_cdio->op.get_track_green (p_cdio->env, i_track);
  } else {
    return false;
  }
}

/*!  
  Return the starting LBA for track number
  track_num in cdio.  Tracks numbers start at 1.
  The "leadout" track is specified either by
  using track_num LEADOUT_TRACK or the total tracks+1.
  CDIO_INVALID_LBA is returned on error.
*/
lba_t
cdio_get_track_lba(const CdIo_t *p_cdio, track_t i_track)
{
  if (!p_cdio) return CDIO_INVALID_LBA;

  if (p_cdio->op.get_track_lba) {
    return p_cdio->op.get_track_lba (p_cdio->env, i_track);
  } else {
    msf_t msf;
    if (p_cdio->op.get_track_msf) 
      if (cdio_get_track_msf(p_cdio, i_track, &msf))
        return cdio_msf_to_lba(&msf);
    return CDIO_INVALID_LBA;
  }
}

/*!  
  Return the starting LSN for track number
  i_track in cdio.  Tracks numbers start at 1.
  The "leadout" track is specified either by
  using i_track LEADOUT_TRACK or the total tracks+1.
  CDIO_INVALID_LBA is returned on error.
*/
lsn_t
cdio_get_track_lsn(const CdIo_t *p_cdio, track_t i_track)
{
  if (p_cdio == NULL) return CDIO_INVALID_LSN;

  if (p_cdio->op.get_track_lba) {
    return cdio_lba_to_lsn(p_cdio->op.get_track_lba (p_cdio->env, i_track));
  } else {
    msf_t msf;
    if (cdio_get_track_msf(p_cdio, i_track, &msf))
      return cdio_msf_to_lsn(&msf);
    return CDIO_INVALID_LSN;
  }
}

/*!  
  Return the ending LSN for track number
  i_track in cdio.  CDIO_INVALID_LSN is returned on error.
*/
lsn_t
cdio_get_track_last_lsn(const CdIo_t *p_cdio, track_t i_track)
{
  lsn_t lsn = cdio_get_track_lsn(p_cdio, i_track+1);

  if (CDIO_INVALID_LSN == lsn) return CDIO_INVALID_LSN;
  /* Safe, we've always the leadout. */
  return lsn - 1;
}

/*!  
  Return the starting MSF (minutes/secs/frames) for track number
  i_track in cdio.  Track numbers start at 1.
  The "leadout" track is specified either by
  using i_track LEADOUT_TRACK or the total tracks+1.
  False is returned if there is no track entry.
*/
bool
cdio_get_track_msf(const CdIo_t *p_cdio, track_t i_track, /*out*/ msf_t *msf)
{
  if (!p_cdio) return false;

  if (p_cdio->op.get_track_msf) {
    return p_cdio->op.get_track_msf (p_cdio->env, i_track, msf);
  } else if (p_cdio->op.get_track_lba) {
    lba_t lba = p_cdio->op.get_track_lba (p_cdio->env, i_track);
    if (lba  == CDIO_INVALID_LBA) return false;
    cdio_lba_to_msf(lba, msf);
    return true;
  } else {
    return false;
  }
}

/*! Return copy protection status on a track. Is this meaningful
  if not an audio track?
*/
track_flag_t
cdio_get_track_preemphasis(const CdIo *p_cdio, track_t i_track)
{
  if (p_cdio->op.get_track_preemphasis) {
    return p_cdio->op.get_track_preemphasis (p_cdio->env, i_track);
  } else {
    return CDIO_TRACK_FLAG_UNKNOWN;
  }
}

/*!  
  Return the number of sectors between this track an the next.  This
  includes any pregap sectors before the start of the next track.
  Tracks start at 1.
  0 is returned if there is an error.
*/
unsigned int
cdio_get_track_sec_count(const CdIo_t *p_cdio, track_t i_track)
{
  const track_t i_tracks = cdio_get_num_tracks(p_cdio);

  if (i_track >=1 && i_track <= i_tracks) 
    return ( cdio_get_track_lba(p_cdio, i_track+1) 
             - cdio_get_track_lba(p_cdio, i_track) );
  return 0;
}