Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / wodim / cd_misc.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)cd_misc.c        1.10 01/10/29 Copyright 1997 J. Schilling */
14 /*
15  *      Misc CD support routines
16  *
17  *      Copyright (c) 1997 J. Schilling
18  */
19 /*
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License version 2
22  * as published by the Free Software Foundation.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License along with
30  * this program; see the file COPYING.  If not, write to the Free Software
31  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32  */
33
34 #include <mconfig.h>
35 #include <standard.h>
36 #include <utypes.h>     /* Includes <sys/types.h> for caddr_t */
37 #include <stdio.h>
38 #include <schily.h>
39
40 #include "wodim.h"
41
42 int     from_bcd(int b);
43 int     to_bcd(int i);
44 long    msf_to_lba(int m, int s, int f, BOOL force_positive);
45 BOOL    lba_to_msf(long lba, msf_t *mp);
46 void    sec_to_msf(long sec, msf_t *mp);
47 void    print_min_atip(long li, long lo);
48
49 int 
50 from_bcd(int b)
51 {
52         return ((b & 0x0F) + 10 * (((b)>> 4) & 0x0F));
53 }
54
55 int 
56 to_bcd(int i)
57 {
58         return (i % 10 | ((i / 10) % 10) << 4);
59 }
60
61 long 
62 msf_to_lba(int m, int s, int f, BOOL force_positive)
63 {
64         long    ret = m * 60 + s;
65
66         ret *= 75;
67         ret += f;
68         if (m < 90 || force_positive)
69                 ret -= 150;
70         else
71                 ret -= 450150;
72         return (ret);
73 }
74
75 BOOL 
76 lba_to_msf(long lba, msf_t *mp)
77 {
78         int     m;
79         int     s;
80         int     f;
81
82 #ifdef  __follow_redbook__
83         if (lba >= -150 && lba < 405000) {      /* lba <= 404849 */
84 #else
85         if (lba >= -150) {
86 #endif
87                 m = (lba + 150) / 60 / 75;
88                 s = (lba + 150 - m*60*75)  / 75;
89                 f = (lba + 150 - m*60*75 - s*75);
90
91         } else if (lba >= -45150 && lba <= -151) {
92                 m = (lba + 450150) / 60 / 75;
93                 s = (lba + 450150 - m*60*75)  / 75;
94                 f = (lba + 450150 - m*60*75 - s*75);
95
96         } else {
97                 mp->msf_min   = -1;
98                 mp->msf_sec   = -1;
99                 mp->msf_frame = -1;
100
101                 return (FALSE);
102         }
103         mp->msf_min   = m;
104         mp->msf_sec   = s;
105         mp->msf_frame = f;
106
107         if (lba > 404849)                       /* 404850 -> 404999: lead out */
108                 return (FALSE);
109         return (TRUE);
110 }
111
112 void 
113 sec_to_msf(long sec, msf_t *mp)
114 {
115         int     m;
116         int     s;
117         int     f;
118
119         m = (sec) / 60 / 75;
120         s = (sec - m*60*75)  / 75;
121         f = (sec - m*60*75 - s*75);
122
123         mp->msf_min   = m;
124         mp->msf_sec   = s;
125         mp->msf_frame = f;
126 }
127
128 void 
129 print_min_atip(long li, long lo)
130 {
131         msf_t   msf;
132
133         if (li < 0) {
134                 lba_to_msf(li, &msf);
135
136                 printf("  ATIP start of lead in:  %ld (%02d:%02d/%02d)\n",
137                         li, msf.msf_min, msf.msf_sec, msf.msf_frame);
138         }
139         if (lo > 0) {
140                 lba_to_msf(lo, &msf);
141                 printf("  ATIP start of lead out: %ld (%02d:%02d/%02d)\n",
142                         lo, msf.msf_min, msf.msf_sec, msf.msf_frame);
143         }
144 }