summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/input/cdromInputStream.cpp
blob: 1cf3f9051f99baab10089aad9bbbdc291f02654a (plain)
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
/*
  reads input data from cdrom
  Copyright (C) 1999  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */


#include "cdromInputStream.h"
#include "cdromRawAccess.h"
#include "cdromToc.h"
#include <string.h>
#include "inputDetector.h"

#include <iostream>

using namespace std;

CDRomInputStream::CDRomInputStream() {
  cdRomToc=new CDRomToc();
  cdRomRawAccess=new CDRomRawAccess(cdRomToc);

  buflen=0;
  bufCurrent=NULL;

  currentMinute=0;
  currentSecond=2;
  currentFrame=0;
  byteCounter=0;
}


CDRomInputStream::~CDRomInputStream() {
  delete cdRomRawAccess;
  delete cdRomToc;
}


int CDRomInputStream::readCurrent() {
  int ok=cdRomRawAccess->read(currentMinute,currentSecond,currentFrame);
  if (ok==false) {
    if (cdRomRawAccess->eof() == false) {
      int pos=cdRomToc->getNextTocEntryPos(currentMinute,
					   currentSecond,
					   currentFrame);

      // now try to read a few sectors
      int cnt=0;
      int back=false;

      while(1) {
	// jump forward
	int i;
	for(i=0;i<_CDROM_FRAMES-currentFrame;i++) {
	  next_sector();
	}
	cout << "trying next ..."<<endl;
	ok=cdRomRawAccess->read(currentMinute,currentSecond,currentFrame);
	if (ok) {
	  bufCurrent=cdRomRawAccess->getBufferStart();
	  buflen=cdRomRawAccess->getBufferLen();
	  return true;
	}
	cnt++;
	if (cnt > 100) {
	  break;
	}
      }
      cout << "last possible jump"<<endl;
      if (pos > 1) {
	TocEntry* tocEntry=cdRomToc->getTocEntry(pos-1);
	currentMinute=tocEntry->minute;
	currentSecond=tocEntry->second;
	currentFrame=tocEntry->frame;
	back=cdRomRawAccess->read(currentMinute,currentSecond,currentFrame);
	if (back) {
	  bufCurrent=cdRomRawAccess->getBufferStart();
	  buflen=cdRomRawAccess->getBufferLen();
	}
      }
      return back;
    }
    return false;
  }
  bufCurrent=cdRomRawAccess->getBufferStart();
  buflen=cdRomRawAccess->getBufferLen();
  return true;
}


int CDRomInputStream::getByteDirect() {
  int back;
  if (buflen==0) {
    fillBuffer();
  }
  if (buflen==0){
    return EOF;
  }
  back=*bufCurrent;
  buflen--;
  bufCurrent++;
  byteCounter++;
  return back;
}


int CDRomInputStream::read(char* ptr,int size) {
  char* dest=(char*)ptr;
  int bytesRead=0;
  int doRead=size;
  int canRead;

  while(eof() == false) {
    if (buflen == 0) {
      if (fillBuffer() == false) {
	return 0;
      }
      continue;
    }
    canRead=buflen;
    if (doRead < canRead) {
      canRead=doRead;
    }
    memcpy((void*)dest,(void*)bufCurrent,canRead);
    buflen-=canRead;
    bufCurrent+=canRead;
    bytesRead+=canRead;
    dest+=canRead;
    doRead-=canRead;
    if (doRead == 0) {
      byteCounter+=bytesRead;
      return bytesRead;
    }
  }
  return 0;
}

int CDRomInputStream::eof() {
  return cdRomRawAccess->eof();
}




long CDRomInputStream::getBytePosition() {
  return byteCounter;
}


void CDRomInputStream::print() {
}


void CDRomInputStream::next_sector() {
  currentFrame++;
  if (currentFrame>=_CDROM_FRAMES) {
    currentFrame = 0;
    currentSecond++;
    if (currentSecond>=_CDROM_SECS) {
      currentSecond = 0;
      currentMinute++;
    }
  }
}

int CDRomInputStream::open(const char* file) {
  cout << "CDRomInputStream::open:"<<file<<endl;
  char* noExtension=InputDetector::getWithoutExtension(file);
  cout << "CDRomInputStream::noExt:"<<noExtension<<endl;
  if (noExtension == NULL) {
    return false;
  }
  cdRomToc->open(noExtension);
  cdRomRawAccess->open(noExtension);
  if (isOpen()==false) {
    return false;
  }
  setUrl(noExtension);
  int entries=cdRomToc->getTocEntries();
  cdRomToc->print();
  if (entries == 1) {
    cerr << "only lead out"<<endl;
  }
  
  //  cdRomRawAccess->insertTocEntry(0,2,0);
  //cdRomToc->insertTocEntry(1,13,5);

  TocEntry* tocEntry=cdRomToc->getTocEntry(0);
  currentMinute=tocEntry->minute;
  currentSecond=tocEntry->second;
  currentFrame=tocEntry->frame;
  delete noExtension;
   
  return readCurrent();
}


void CDRomInputStream::close() {
  cdRomRawAccess->close();
  byteCounter=0;
  setUrl(NULL);
}


int CDRomInputStream::isOpen() {
  return cdRomRawAccess->isOpen();
}




long CDRomInputStream::getBytePos(int min,int sec) {
  long back;
  // 2324 is the size of a cdfram
  back=sec*_CDROM_FRAMES*2324;
  back=back+min*60*_CDROM_FRAMES*2324;
  cout << "CDRomInputStream::getByteLength"<<back<<endl;
  return back; 
}

long CDRomInputStream::getByteLength() {
  // we get the length out of the toc and then multiply like hell
  long totalSecs=cdRomToc->getEndSecond();
  long min=totalSecs/60;
  long sec=totalSecs-60*min;

  long back=getBytePos(min,sec);

  return back; 
}


int CDRomInputStream::seek(long posInBytes) {
  int entries=cdRomToc->getTocEntries();

  TocEntry* firstEntry;
  if (entries == 0) {
    return false;
  }
  if (posInBytes < 0) {
    return false;
  }
  firstEntry=cdRomToc->getTocEntry(0);
  
  long startByte=getBytePos(firstEntry->minute,firstEntry->second+1);

  posInBytes=posInBytes+startByte;


  float fmin=(float)posInBytes/(float)(60*_CDROM_FRAMES*2324);
  int min=(int)fmin;


  long sec=posInBytes-min*(60*_CDROM_FRAMES*2324);
  sec=sec/(_CDROM_FRAMES*2324);

  byteCounter=posInBytes;
  if (cdRomRawAccess->read(min,sec,0)==false) {
    return false;
  }
  setTimePos(min*60+sec);

  return true;
}


int CDRomInputStream::setTimePos(int second) {
  currentFrame=0;
  currentMinute=second/60;
  currentSecond=second%60;
  
  return fillBuffer();
}





int CDRomInputStream::fillBuffer() {
  int maxNoData=30;
  int cnt=0;
  if (buflen==0) {
    while (cnt < maxNoData) {
      next_sector();
      if (readCurrent() == false) {
	return false;
      }
      if (cdRomRawAccess->isData() == false) {
	//	cerr << "no data"<<endl;
	cnt++;
      } else {
	return true;
      }
    }
    return false;
  }
  return true;
}