/[pcsx2_0.9.7]/trunk/plugins/CDVDpeops/sub.c
ViewVC logotype

Contents of /trunk/plugins/CDVDpeops/sub.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (9 years, 10 months ago) by william
File MIME type: text/plain
File size: 9212 byte(s)
committing r3113 initial commit again...
1 /***************************************************************************
2 sub.c - description
3 -------------------
4 begin : Sun Nov 16 2003
5 copyright : (C) 2003 by Pete Bernert
6 email : BlackDove@addcom.de
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. See also the license.txt file for *
15 * additional informations. *
16 * *
17 ***************************************************************************/
18
19 //*************************************************************************//
20 // History of changes:
21 //
22 // 2003/11/16 - Pete
23 // - generic cleanup for the Peops release
24 //
25 //*************************************************************************//
26
27 /////////////////////////////////////////////////////////
28
29 #include "stdafx.h"
30 #define _IN_SUB
31 #include "externals.h"
32
33 /* TODO (#1#): SUB CHANNEL STUFF */
34
35 /////////////////////////////////////////////////////////
36
37 unsigned char * pCurrSubBuf=NULL; // ptr to emu sub data (or NULL)
38 int iUseSubReading=0; // subdata support (by file or directly)
39 char szSUBF[260]; // sub file name
40 SUB_CACHE * subCache=NULL; // cache memory
41 SUB_DATA * subHead=NULL;
42 int iSUBNum=0; // number of subdata cached
43 unsigned char SubCData[96]; // global data subc buffer
44 unsigned char SubAData[96]; // global audio subc buffer
45
46 /////////////////////////////////////////////////////////
47
48 void BuildSUBCache(void)
49 {
50 FILE * subfile;
51 unsigned char buffer[16], * p;
52 SUB_DATA * plast=NULL,* px;
53
54 if(iUseSubReading) // some subreading wanted?
55 {
56 if(iUseSubReading==1) iUseCaching=0; // -> direct read? no caching done, only 1 sector reads
57 memset(SubCData,0,96); // -> init subc
58 pCurrSubBuf=SubCData; // -> set global ptr
59 }
60 else // no plugin subreading?
61 {
62 pCurrSubBuf=NULL; // -> return NULL as subc buffer to emu
63 }
64
65 memset(SubAData,0,96); // init audio subc buffer
66
67 if(iUseSubReading!=2) return; // no subfile wanted?
68 if(szSUBF[0]==0) return; // no filename given?
69
70 subfile=fopen(szSUBF, "rb"); // open subfile
71 if(subfile==0)
72 {
73 MessageBox(NULL,"No SBI/M3S file found!",libraryName,MB_OK);
74 return;
75 }
76
77 memset(buffer,0,5); // read header
78 fread(buffer, 4, 1, subfile);
79
80 iSUBNum=0;subHead=NULL;
81
82 if(strcmp((char *)buffer,"SBI")==0) // ah, SBI file
83 {
84 while(fread(buffer, 4, 1, subfile)==1) // -> read record header
85 {
86 iSUBNum++; // -> one more sub cache block
87 px=(SUB_DATA *)malloc(sizeof(SUB_DATA)); // -> get cache buff
88 //!!!!!!!!!!!!!!!!!!!!!!!!!!!! // -> and fill it...
89
90 /* TODO (#1#): addr2time subchannel */
91
92
93 px->addr=time2addrB(buffer);
94
95 px->pNext=NULL;
96
97 px->subq[0]=0x41;
98 px->subq[1]=0x01;
99 px->subq[2]=0x01;
100 p=&px->subq[3];
101 addr2timeB(px->addr,p);
102 px->subq[6]=0x00;
103 p=&px->subq[7];
104 addr2timeB(px->addr+150,p);
105
106 if(buffer[3]==1)
107 {
108 fread(px->subq,10, 1, subfile);
109 }
110 else if(buffer[3]==2)
111 {
112 fread(&px->subq[3],3, 1, subfile);
113 }
114 else if(buffer[3]==3)
115 {
116 fread(&px->subq[7],3, 1, subfile);
117 }
118
119 if(plast==NULL) // add new cache block to linked list
120 {
121 plast=subHead=px;
122 }
123 else
124 {
125 plast->pNext=px;plast=px;
126 }
127 }
128 }
129 else // M3S file?
130 { // -> read data, and store all
131 unsigned char min,sec,frame,xmin,xsec,xframe; // -> subs which are different from
132 BOOL b1,b2,goon=TRUE;int iNum=0; // -> the expected calculated values
133
134 xmin=2;
135 xsec=58;
136 xframe=0;
137 min=3;
138 sec=0;
139 frame=0;
140
141 fread(buffer+4, 12, 1, subfile);
142 do
143 {
144 if(itod(min) != buffer[7]||
145 itod(sec) != buffer[8]||
146 itod(frame) != buffer[9])
147 b1=TRUE; else b1=FALSE;
148
149 if(itod(xmin) != buffer[3]||
150 itod(xsec) != buffer[4]||
151 itod(xframe) != buffer[5])
152 b2=TRUE; else b2=FALSE;
153
154 if(buffer[1]!=1) b1=b2=TRUE;
155 if(buffer[2]!=1) b1=b2=TRUE;
156
157 if(b1 || b2)
158 {
159 iSUBNum++;
160 px=(SUB_DATA *)malloc(sizeof(SUB_DATA));
161 px->pNext=NULL;
162 memcpy(px->subq,buffer,10);
163 buffer[7]=itod(min);
164 buffer[8]=itod(sec);
165 buffer[9]=itod(frame);
166 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
167 px->addr=time2addrB(&buffer[7]);
168
169 if(plast==NULL)
170 {plast=subHead=px;}
171 else {plast->pNext=px;plast=px;}
172 }
173
174 xframe=xframe+1;
175 if(xframe>74)
176 {
177 xsec+=1;
178 xframe-=75;
179 if(xsec>59)
180 {
181 xmin+=1;
182 xsec-=60;
183 if(xmin>99)
184 {
185 goon=FALSE;
186 }
187 }
188 }
189
190 frame=frame+1;
191 if(frame>74)
192 {
193 sec+=1;
194 frame-=75;
195 if(sec>59)
196 {
197 min+=1;
198 sec-=60;
199 if(min>99)
200 {
201 goon=FALSE;
202 }
203 }
204 }
205 iNum++;
206 if(iNum>(60*75)) goon=FALSE;
207 }
208 while(goon && (fread(buffer, 16, 1, subfile)==1));
209
210 if(iNum!=(60*75)) goon=FALSE;
211 else
212 if(iSUBNum==(60*75)) goon=FALSE;
213
214 if(!goon)
215 {
216 MessageBox(NULL,"Bad SBI/M3S file!",libraryName,MB_OK);
217 fclose(subfile);
218 FreeSUBCache();
219 return;
220 }
221 }
222
223 subCache=NULL;
224 if(iSUBNum) // something in cache?
225 { // -> create an array with the used addresses, for fast searching access
226 SUB_CACHE * psc;int i;
227 subCache=(SUB_CACHE *)malloc(iSUBNum*sizeof(SUB_CACHE));
228 psc=subCache;px=subHead;
229 for(i=0;i<iSUBNum;i++,psc++,px=(SUB_DATA *)px->pNext)
230 {
231 psc->addr = px->addr;
232 psc->pNext = (void *)px;
233 }
234 iSUBNum--;
235 }
236
237 fclose(subfile);
238 }
239
240 /////////////////////////////////////////////////////////
241 // func for calculating 'right' subdata
242
243 void FakeSubData(unsigned long adr)
244 {
245 SubCData[12]=0x41;
246 SubCData[13]=0x01;
247 SubCData[14]=0x01;
248
249 /* TODO (#1#): addr2time fake sub data
250 */
251
252
253 //!!!!!!!!!!!!!!!!!!!!!!!????
254 addr2timeB(adr, &SubCData[15]);
255 // SubCData[18]=0x00;
256 addr2timeB(adr+150,&SubCData[19]);
257 }
258
259 /////////////////////////////////////////////////////////
260 // check, if for a given addr we have special subdata in cache
261
262 void CheckSUBCache(long addr)
263 {
264 SUB_CACHE * pcstart, * pcend, * pcpos;
265
266 pcstart=subCache; // ptrs to address arrays (start/end)
267 pcend =subCache+iSUBNum;
268
269 if(addr>=pcstart->addr && // easy check, if given addr is between start/end
270 addr<=pcend->addr)
271 {
272 while(1) // now search for sub
273 {
274 if(addr==pcend->addr) {pcpos=pcend;break;} // got it! break
275
276 pcpos=pcstart+(pcend-pcstart)/2; // get the 'middle' address
277 if(pcpos==pcstart) break; // no more checks can be done
278 if(addr<pcpos->addr) // look further...
279 {
280 pcend=pcpos;
281 continue;
282 }
283 if(addr>pcpos->addr)
284 {
285 pcstart=pcpos;
286 continue;
287 }
288 break;
289 }
290
291 if(addr==pcpos->addr) // found some cached data?
292 {
293 SUB_DATA * p=(SUB_DATA *)pcpos->pNext; // -> ptr to data
294 memcpy(&SubCData[12],p->subq,10); // -> get the data
295 return; // -> done
296 }
297 }
298
299 FakeSubData(addr); // no subcdata avail, so fake right one
300 }
301
302 /////////////////////////////////////////////////////////
303 // free all sub cache bufs
304
305 void FreeSUBCache(void)
306 {
307 SUB_DATA * p=subHead;
308 void * pn;
309 while(p)
310 {
311 pn=p->pNext;
312 free(p);
313 p=(SUB_DATA *)pn;
314 }
315 subHead=NULL;
316
317 if(subCache) free(subCache);
318 subCache=NULL;
319 }
320
321 /////////////////////////////////////////////////////////

  ViewVC Help
Powered by ViewVC 1.1.22