1 /*
2     Copyright (C) 2003, 2004, 2005, 2006, 2008, 2012
3      Rocky Bernstein <rocky@gnu.org>
4     Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
5 
6     This program 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 3 of the License, or
9     (at your option) any later version.
10 
11     This program 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, see <http://www.gnu.org/licenses/>.
18 */
19 /*!
20    \file sector.h
21    \brief Things related to CD-ROM layout: tracks, sector sizes, MSFs, LBAs.
22 
23   A CD-ROM physical sector size is 2048, 2052, 2056, 2324, 2332, 2336,
24   2340, or 2352 bytes long.
25 
26   Sector types of the standard CD-ROM data formats:
27 
28 \verbatim
29   format   sector type               user data size (bytes)
30   -----------------------------------------------------------------------------
31     1     (Red Book)    CD-DA          2352    (CDIO_CD_FRAMESIZE_RAW)
32     2     (Yellow Book) Mode1 Form1    2048    (CDIO_CD_FRAMESIZE)
33     3     (Yellow Book) Mode1 Form2    2336    (M2RAW_SECTOR_SIZE)
34     4     (Green Book)  Mode2 Form1    2048    (CDIO_CD_FRAMESIZE)
35     5     (Green Book)  Mode2 Form2    2328    (2324+4 spare bytes)
36 
37 
38         The layout of the standard CD-ROM data formats:
39   -----------------------------------------------------------------------------
40   - audio (red):                  | audio_sample_bytes |
41                                   |        2352        |
42 
43   - data (yellow, mode1):         | sync - head - data - EDC - zero - ECC |
44                                   |  12  -   4  - 2048 -  4  -   8  - 276 |
45 
46   - data (yellow, mode2):         | sync - head - data |
47                                  |  12  -   4  - 2336 |
48 
49   - XA data (green, mode2 form1): | sync - head - sub - data - EDC - ECC |
50                                   |  12  -   4  -  8  - 2048 -  4  - 276 |
51 
52   - XA data (green, mode2 form2): | sync - head - sub - data - Spare |
53                                   |  12  -   4  -  8  - 2324 -  4    |
54 \endverbatim
55 
56 
57 */
58 
59 #ifndef CDIO_SECTOR_H_
60 #define CDIO_SECTOR_H_
61 
62 #ifdef __cplusplus
63     extern "C" {
64 #endif
65 
66 #include <cdio/types.h>
67 
68       /*! Information that can be obtained through a Read Subchannel
69         command.
70       */
71       typedef enum cdio_subchannel
72         {
73           CDIO_SUBCHANNEL_SUBQ_DATA             = 0,
74           CDIO_SUBCHANNEL_CURRENT_POSITION      = 1,
75           CDIO_SUBCHANNEL_MEDIA_CATALOG         = 2,
76           CDIO_SUBCHANNEL_TRACK_ISRC            = 3
77         } cdio_subchannel;
78 
79       /*! track flags
80        * Q Sub-channel Control Field (4.2.3.3)
81        */
82       typedef enum {
83         NONE =                  0x00,   /* no flags set */
84         PRE_EMPHASIS =          0x01,   /* audio track recorded with pre-emphasis */
85         COPY_PERMITTED =        0x02,   /* digital copy permitted */
86         DATA =                  0x04,   /* data track */
87         FOUR_CHANNEL_AUDIO =    0x08,   /* 4 audio channels */
88         SCMS =                  0x10    /* SCMS (5.29.2.7) */
89       } flag_t;
90 
91 #define CDIO_PREGAP_SECTORS  150
92 #define CDIO_POSTGAP_SECTORS 150
93 
94       /*! An enumeration for some of the CDIO_CD \#defines below. This isn't
95         really an enumeration one would really use in a program it is to
96         be helpful in debuggers where wants just to refer to the CDIO_CD_
97         names and get something.
98       */
99       extern enum cdio_cd_enums {
100         CDIO_CD_MINS =              74,   /**< max. minutes per CD, not really
101                                              a limit */
102         CDIO_CD_SECS_PER_MIN =      60,   /**< seconds per minute */
103         CDIO_CD_FRAMES_PER_SEC =    75,   /**< frames per second */
104         CDIO_CD_SYNC_SIZE =         12,   /**< 12 sync bytes per raw data
105                                              frame */
106         CDIO_CD_CHUNK_SIZE =        24,   /**< lowest-level "data bytes
107                                              piece" */
108         CDIO_CD_NUM_OF_CHUNKS =     98,   /**< chunks per frame */
109         CDIO_CD_FRAMESIZE_SUB =     96,   /**< subchannel data "frame" size */
110         CDIO_CD_HEADER_SIZE =        4,   /**< header (address) bytes per raw
111                                              frame */
112         CDIO_CD_SUBHEADER_SIZE =     8,   /**< subheader bytes per raw XA data
113                                              frame */
114         CDIO_CD_ECC_SIZE =         276,   /**< bytes ECC per most raw data
115                                              frame types */
116         CDIO_CD_FRAMESIZE =       2048,   /**< bytes per frame, "cooked"
117                                              mode */
118         CDIO_CD_FRAMESIZE_RAW =   2352,   /**< bytes per frame, "raw" mode */
119         CDIO_CD_FRAMESIZE_RAWER = 2646,   /**< The maximum possible
120                                              returned  */
121         CDIO_CD_FRAMESIZE_RAW1  = 2340,
122         CDIO_CD_FRAMESIZE_RAW0  = 2336,
123         CDIO_CD_MAX_SESSIONS =      99,
124         CDIO_CD_MIN_SESSION_NO =     1,   /**<, Smallest CD session number */
125         CDIO_CD_MAX_LSN =       450150,   /**< Largest LSN in a CD */
126         CDIO_CD_MIN_LSN      = -450150,   /**< Smallest LSN in a CD */
127       } cdio_cd_enums;
128 
129       /*!
130         Some generally useful CD-ROM information -- mostly based on the above.
131         This is from linux.h - not to slight other OS's. This was the first
132         place I came across such useful stuff.
133       */
134 #define CDIO_CD_MINS              74   /**< max. minutes per CD, not really
135                                           a limit */
136 #define CDIO_CD_SECS_PER_MIN      60   /**< seconds per minute */
137 #define CDIO_CD_FRAMES_PER_SEC    75   /**< frames per second */
138 #define CDIO_CD_SYNC_SIZE         12   /**< 12 sync bytes per raw data frame */
139 #define CDIO_CD_CHUNK_SIZE        24   /**< lowest-level "data bytes piece" */
140 #define CDIO_CD_NUM_OF_CHUNKS     98   /**< chunks per frame */
141 #define CDIO_CD_FRAMESIZE_SUB     96   /**< subchannel data "frame" size */
142 #define CDIO_CD_HEADER_SIZE        4   /**< header (address) bytes per raw
143                                           data frame */
144 #define CDIO_CD_SUBHEADER_SIZE     8   /**< subheader bytes per raw XA data
145                                             frame */
146 #define CDIO_CD_EDC_SIZE           4   /**< bytes EDC per most raw data
147                                           frame types */
148 #define CDIO_CD_M1F1_ZERO_SIZE     8   /**< bytes zero per yellow book mode
149                                           1 frame */
150 #define CDIO_CD_ECC_SIZE         276   /**< bytes ECC per most raw data frame
151                                           types */
152 #define CDIO_CD_FRAMESIZE       2048   /**< bytes per frame, "cooked" mode */
153 #define CDIO_CD_FRAMESIZE_RAW   2352   /**< bytes per frame, "raw" mode */
154 #define CDIO_CD_FRAMESIZE_RAWER 2646   /**< The maximum possible returned
155                                           bytes */
156 #define CDIO_CD_FRAMESIZE_RAW1 (CDIO_CD_FRAMESIZE_RAW-CDIO_CD_SYNC_SIZE) /*2340*/
157 #define CDIO_CD_FRAMESIZE_RAW0 (CDIO_CD_FRAMESIZE_RAW-CDIO_CD_SYNC_SIZE-CDIO_CD_HEADER_SIZE) /*2336*/
158 
159       /*! "before data" part of raw XA (green, mode2) frame */
160 #define CDIO_CD_XA_HEADER (CDIO_CD_HEADER_SIZE+CDIO_CD_SUBHEADER_SIZE)
161 
162       /*! "after data" part of raw XA (green, mode2 form1) frame */
163 #define CDIO_CD_XA_TAIL   (CDIO_CD_EDC_SIZE+CDIO_CD_ECC_SIZE)
164 
165       /*! "before data" sync bytes + header of XA (green, mode2) frame */
166 #define CDIO_CD_XA_SYNC_HEADER   (CDIO_CD_SYNC_SIZE+CDIO_CD_XA_HEADER)
167 
168       /*! String of bytes used to identify the beginning of a Mode 1 or
169           Mode 2 sector. */
170       extern const uint8_t CDIO_SECTOR_SYNC_HEADER[CDIO_CD_SYNC_SIZE];
171       /**<
172         {0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0};
173       */
174 
175       /*! An enumeration for some of the M2*_SECTOR_SIZE \#defines
176         below. This isn't really an enumeration one would really use in a
177         program it is to be helpful in debuggers where wants just to refer
178         to the M2*_SECTOR_SIZE names and get something.
179       */
180       extern enum m2_sector_enums {
181         M2F2_SECTOR_SIZE  = 2324,
182         M2SUB_SECTOR_SIZE = 2332,
183         M2RAW_SECTOR_SIZE = 2336
184       } m2_sector_enums;
185 
186 #define M2F2_SECTOR_SIZE    2324
187 #define M2SUB_SECTOR_SIZE   2332
188 #define M2RAW_SECTOR_SIZE   2336
189 
190       /*! Largest CD session number */
191 #define CDIO_CD_MAX_SESSIONS    99
192       /*! Smallest CD session number */
193 #define CDIO_CD_MIN_SESSION_NO   1
194 
195       /*! Largest LSN in a CD */
196 #define CDIO_CD_MAX_LSN   450150
197       /*! Smallest LSN in a CD */
198 #define CDIO_CD_MIN_LSN  -450150
199 
200 
201 #define CDIO_CD_FRAMES_PER_MIN                          \
202       (CDIO_CD_FRAMES_PER_SEC*CDIO_CD_SECS_PER_MIN)
203 
204 typedef enum cdio_cd_minutes_sectors
205 {
206         CDIO_CD_74MIN_SECTORS = UINT32_C(74) * CDIO_CD_FRAMES_PER_MIN,
207         CDIO_CD_80MIN_SECTORS = UINT32_C(80) * CDIO_CD_FRAMES_PER_MIN,
208         CDIO_CD_90MIN_SECTORS = UINT32_C(90) * CDIO_CD_FRAMES_PER_MIN,
209 
210         CDIO_CD_MAX_SECTORS   = UINT32_C(100) * CDIO_CD_FRAMES_PER_MIN - CDIO_PREGAP_SECTORS
211 } cdio_cd_minutes_sectors;
212 
213 #define msf_t_SIZEOF 3
214 
215       /*!
216         Convert an LBA into a string representation of the MSF.
217         \warning cdio_lba_to_msf_str returns new allocated string */
218       char *cdio_lba_to_msf_str (lba_t i_lba);
219 
220       /*!
221         Convert an MSF into a string representation of the MSF.
222         \warning cdio_msf_to_msf_str returns new allocated string */
223       char *cdio_msf_to_str (const msf_t *p_msf);
224 
225       /*!
226         Convert an LBA into the corresponding LSN.
227       */
228       lba_t cdio_lba_to_lsn (lba_t i_lba);
229 
230       /*!
231         Convert an LBA into the corresponding MSF.
232       */
233       void  cdio_lba_to_msf(lba_t i_lba, msf_t *p_msf);
234 
235       /*!
236         Convert an LSN into the corresponding LBA.
237         CDIO_INVALID_LBA is returned if there is an error.
238       */
239       lba_t cdio_lsn_to_lba (lsn_t i_lsn);
240 
241       /*!
242         Convert an LSN into the corresponding MSF.
243       */
244       void  cdio_lsn_to_msf (lsn_t i_lsn, msf_t *p_msf);
245 
246       /*!
247         Convert a MSF into the corresponding LBA.
248         CDIO_INVALID_LBA is returned if there is an error.
249       */
250       lba_t cdio_msf_to_lba (const msf_t *p_msf);
251 
252       /*!
253         Convert a MSF into the corresponding LSN.
254         CDIO_INVALID_LSN is returned if there is an error.
255       */
256       lsn_t cdio_msf_to_lsn (const msf_t *p_msf);
257 
258       /*!
259         Convert a MSF - broken out as 3 integer components into the
260         corresponding LBA.
261         CDIO_INVALID_LBA is returned if there is an error.
262       */
263       lba_t cdio_msf3_to_lba (unsigned int minutes, unsigned int seconds,
264                               unsigned int frames);
265 
266       /*!
267         Convert a string of the form MM:SS:FF into the corresponding LBA.
268         CDIO_INVALID_LBA is returned if there is an error.
269       */
270       lba_t cdio_mmssff_to_lba (const char *psz_mmssff);
271 
272 #ifdef __cplusplus
273     }
274 #endif
275 
276 #ifndef DO_NOT_WANT_PARANOIA_COMPATIBILITY
277 /** For compatibility with good ol' paranoia */
278 #define CD_FRAMESIZE_RAW        CDIO_CD_FRAMESIZE_RAW
279 #endif /*DO_NOT_WANT_PARANOIA_COMPATIBILITY*/
280 
281 #endif /* CDIO_SECTOR_H_ */
282 
283 
284 /*
285  * Local variables:
286  *  c-file-style: "gnu"
287  *  tab-width: 8
288  *  indent-tabs-mode: nil
289  * End:
290  */
291