~ubuntu-branches/ubuntu/edgy/cdparanoia/edgy

« back to all changes in this revision

Viewing changes to interface/common_interface.c

  • Committer: Bazaar Package Importer
  • Author(s): Dale E Martin
  • Date: 2002-03-15 11:23:27 UTC
  • Revision ID: james.westby@ubuntu.com-20020315112327-7e08i0np6duvv5ep
Tags: upstream-3a9.8
ImportĀ upstreamĀ versionĀ 3a9.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************
 
2
 * CopyPolicy: GNU Public License 2 applies
 
3
 * Copyright (C) 1998 Monty xiphmont@mit.edu
 
4
 *
 
5
 * CDROM communication common to all interface methods is done here 
 
6
 * (mostly ioctl stuff, but not ioctls specific to the 'cooked'
 
7
 * interface) 
 
8
 *
 
9
 ******************************************************************/
 
10
 
 
11
#include <math.h>
 
12
#include "low_interface.h"
 
13
#include "utils.h"
 
14
#include "smallft.h"
 
15
 
 
16
#include <linux/hdreg.h>
 
17
 
 
18
/* Test for presence of a cdrom by pinging with the 'CDROMVOLREAD' ioctl() */
 
19
int ioctl_ping_cdrom(int fd){
 
20
  struct cdrom_volctrl volctl;
 
21
  if (ioctl(fd, CDROMVOLREAD, &volctl)) 
 
22
    return(1); /* failure */
 
23
 
 
24
  return(0);
 
25
  /* success! */
 
26
}
 
27
 
 
28
 
 
29
/* Use the ioctl thingy above ping the cdrom; this will get model info */
 
30
char *atapi_drive_info(int fd){
 
31
  /* Work around the fact that the struct grew without warning in
 
32
     2.1/2.0.34 */
 
33
  
 
34
  struct hd_driveid *id=malloc(512); /* the size in 2.0.34 */
 
35
  char *ret;
 
36
 
 
37
  if (!(ioctl(fd, HDIO_GET_IDENTITY, id))) {
 
38
 
 
39
    if(id->model==0 || id->model[0]==0)
 
40
      ret=copystring("Generic Unidentifiable ATAPI CDROM");
 
41
    else
 
42
      ret=copystring(id->model);
 
43
  }else
 
44
    ret=copystring("Generic Unidentifiable CDROM");
 
45
 
 
46
  free(id);
 
47
  return(ret);
 
48
}
 
49
 
 
50
int data_bigendianp(cdrom_drive *d){
 
51
  float lsb_votes=0;
 
52
  float msb_votes=0;
 
53
  int i,checked;
 
54
  int endiancache=d->bigendianp;
 
55
  float *a=calloc(1024,sizeof(float));
 
56
  float *b=calloc(1024,sizeof(float));
 
57
  long readsectors=5;
 
58
  int16_t *buff=malloc(readsectors*CD_FRAMESIZE_RAW);
 
59
 
 
60
  /* look at the starts of the audio tracks */
 
61
  /* if real silence, tool in until some static is found */
 
62
 
 
63
  /* Force no swap for now */
 
64
  d->bigendianp=-1;
 
65
  
 
66
  cdmessage(d,"\nAttempting to determine drive endianness from data...");
 
67
  d->enable_cdda(d,1);
 
68
  for(i=0,checked=0;i<d->tracks;i++){
 
69
    float lsb_energy=0;
 
70
    float msb_energy=0;
 
71
    if(cdda_track_audiop(d,i+1)==1){
 
72
      long firstsector=cdda_track_firstsector(d,i+1);
 
73
      long lastsector=cdda_track_lastsector(d,i+1);
 
74
      int zeroflag=-1;
 
75
      long beginsec=0;
 
76
      
 
77
      /* find a block with nonzero data */
 
78
      
 
79
      while(firstsector+readsectors<=lastsector){
 
80
        int j;
 
81
        
 
82
        if(d->read_audio(d,buff,firstsector,readsectors)>0){
 
83
          
 
84
          /* Avoid scanning through jitter at the edges */
 
85
          for(beginsec=0;beginsec<readsectors;beginsec++){
 
86
            int offset=beginsec*CD_FRAMESIZE_RAW/2;
 
87
            /* Search *half* */
 
88
            for(j=460;j<128+460;j++)
 
89
              if(buff[offset+j]!=0){
 
90
                zeroflag=0;
 
91
                break;
 
92
              }
 
93
            if(!zeroflag)break;
 
94
          }
 
95
          if(!zeroflag)break;
 
96
          firstsector+=readsectors;
 
97
        }else{
 
98
          d->enable_cdda(d,0);
 
99
          free(a);
 
100
          free(b);
 
101
          free(buff);
 
102
          return(-1);
 
103
        }
 
104
      }
 
105
 
 
106
      beginsec*=CD_FRAMESIZE_RAW/2;
 
107
      
 
108
      /* un-interleave for an fft */
 
109
      if(!zeroflag){
 
110
        int j;
 
111
        
 
112
        for(j=0;j<128;j++)a[j]=le16_to_cpu(buff[j*2+beginsec+460]);
 
113
        for(j=0;j<128;j++)b[j]=le16_to_cpu(buff[j*2+beginsec+461]);
 
114
        fft_forward(128,a,NULL,NULL);
 
115
        fft_forward(128,b,NULL,NULL);
 
116
        for(j=0;j<128;j++)lsb_energy+=fabs(a[j])+fabs(b[j]);
 
117
        
 
118
        for(j=0;j<128;j++)a[j]=be16_to_cpu(buff[j*2+beginsec+460]);
 
119
        for(j=0;j<128;j++)b[j]=be16_to_cpu(buff[j*2+beginsec+461]);
 
120
        fft_forward(128,a,NULL,NULL);
 
121
        fft_forward(128,b,NULL,NULL);
 
122
        for(j=0;j<128;j++)msb_energy+=fabs(a[j])+fabs(b[j]);
 
123
      }
 
124
    }
 
125
    if(lsb_energy<msb_energy){
 
126
      lsb_votes+=msb_energy/lsb_energy;
 
127
      checked++;
 
128
    }else
 
129
      if(lsb_energy>msb_energy){
 
130
        msb_votes+=lsb_energy/msb_energy;
 
131
        checked++;
 
132
      }
 
133
 
 
134
    if(checked==5 && (lsb_votes==0 || msb_votes==0))break;
 
135
    cdmessage(d,".");
 
136
  }
 
137
 
 
138
  free(buff);
 
139
  free(a);
 
140
  free(b);
 
141
  d->bigendianp=endiancache;
 
142
  d->enable_cdda(d,0);
 
143
 
 
144
  /* How did we vote?  Be potentially noisy */
 
145
  if(lsb_votes>msb_votes){
 
146
    char buffer[256];
 
147
    cdmessage(d,"\n\tData appears to be coming back little endian.\n");
 
148
    sprintf(buffer,"\tcertainty: %d%%\n",(int)
 
149
            (100.*lsb_votes/(lsb_votes+msb_votes)+.5));
 
150
    cdmessage(d,buffer);
 
151
    return(0);
 
152
  }else{
 
153
    if(msb_votes>lsb_votes){
 
154
      char buffer[256];
 
155
      cdmessage(d,"\n\tData appears to be coming back big endian.\n");
 
156
      sprintf(buffer,"\tcertainty: %d%%\n",(int)
 
157
              (100.*msb_votes/(lsb_votes+msb_votes)+.5));
 
158
      cdmessage(d,buffer);
 
159
      return(1);
 
160
    }
 
161
 
 
162
    cdmessage(d,"\n\tCannot determine CDROM drive endianness.\n");
 
163
    return(bigendianp());
 
164
    return(-1);
 
165
  }
 
166
}
 
167
 
 
168
/************************************************************************/
 
169
/* Here we fix up a couple of things that will never happen.  yeah,
 
170
   right.  The multisession stuff is from Hannu's code; it assumes it
 
171
   knows the leasoud/leadin size. */
 
172
 
 
173
int FixupTOC(cdrom_drive *d,int tracks){
 
174
  struct cdrom_multisession ms_str;
 
175
  int j;
 
176
  
 
177
  /* First off, make sure the 'starting sector' is >=0 */
 
178
  
 
179
  for(j=0;j<tracks;j++){
 
180
    if(d->disc_toc[j].dwStartSector<0){
 
181
      cdmessage(d,"\n\tTOC entry claims a negative start offset: massaging"
 
182
                ".\n");
 
183
      d->disc_toc[j].dwStartSector=0;
 
184
    }
 
185
    if(j<tracks-1 && d->disc_toc[j].dwStartSector>
 
186
       d->disc_toc[j+1].dwStartSector){
 
187
      cdmessage(d,"\n\tTOC entry claims an overly large start offset: massaging"
 
188
                ".\n");
 
189
      d->disc_toc[j].dwStartSector=0;
 
190
    }
 
191
 
 
192
  }
 
193
  /* Make sure the listed 'starting sectors' are actually increasing.
 
194
     Flag things that are blatant/stupid/wrong */
 
195
  {
 
196
    long last=d->disc_toc[0].dwStartSector;
 
197
    for(j=1;j<tracks;j++){
 
198
      if(d->disc_toc[j].dwStartSector<last){
 
199
        cdmessage(d,"\n\tTOC entries claim non-increasing offsets: massaging"
 
200
                  ".\n");
 
201
         d->disc_toc[j].dwStartSector=last;
 
202
        
 
203
      }
 
204
      last=d->disc_toc[j].dwStartSector;
 
205
    }
 
206
  }
 
207
 
 
208
  /* For a scsi device, the ioctl must go to the specialized SCSI
 
209
     CDROM device, not the generic device. */
 
210
 
 
211
  if (d->ioctl_fd != -1) {
 
212
    int result;
 
213
 
 
214
    ms_str.addr_format = CDROM_LBA;
 
215
    result = ioctl(d->ioctl_fd, CDROMMULTISESSION, &ms_str);
 
216
    if (result == -1) return -1;
 
217
 
 
218
    if (ms_str.addr.lba > 100) {
 
219
 
 
220
      /* This is an odd little piece of code --Monty */
 
221
 
 
222
      /* believe the multisession offset :-) */
 
223
      /* adjust end of last audio track to be in the first session */
 
224
      for (j = tracks-1; j >= 0; j--) {
 
225
        if (j > 0 && !IS_AUDIO(d,j) && IS_AUDIO(d,j-1)) {
 
226
          if (d->disc_toc[j].dwStartSector > ms_str.addr.lba - 11400) 
 
227
            d->disc_toc[j].dwStartSector = ms_str.addr.lba - 11400;
 
228
          break;
 
229
        }
 
230
      }
 
231
      return 1;
 
232
    }
 
233
  }
 
234
  return 0;
 
235
}
 
236
 
 
237