Translation system changes part 2: replace macros by strings
[mplayer/glamo.git] / stream / stream_cue.c
blob77afa387db4ffb65e69df9db47be65a09088318f
1 //=================== VideoCD BinCue ==========================
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <fcntl.h>
12 #include "config.h"
13 #include "mp_msg.h"
14 #include "help_mp.h"
16 #include "stream.h"
18 #include "help_mp.h"
19 #include "m_option.h"
20 #include "m_struct.h"
21 #include "libavutil/avstring.h"
23 #define byte unsigned char
24 #define SIZERAW 2352
25 #define SIZEISO_MODE1 2048
26 #define SIZEISO_MODE2_RAW 2352
27 #define SIZEISO_MODE2_FORM1 2048
28 #define SIZEISO_MODE2_FORM2 2336
29 #define AUDIO 0
30 #define MODE1 1
31 #define MODE2 2
32 #define MODE1_2352 10
33 #define MODE2_2352 20
34 #define MODE1_2048 30
35 #define MODE2_2336 40
36 #define UNKNOWN -1
38 static struct stream_priv_s {
39 char* filename;
40 } stream_priv_dflts = {
41 NULL
44 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
45 /// URL definition
46 static const m_option_t stream_opts_fields[] = {
47 { "string", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
48 { NULL, NULL, 0, 0, 0, 0, NULL }
50 static const struct m_struct_st stream_opts = {
51 "cue",
52 sizeof(struct stream_priv_s),
53 &stream_priv_dflts,
54 stream_opts_fields
57 static FILE* fd_cue;
58 static int fd_bin = 0;
60 static char bin_filename[256];
62 static char cue_filename[256];
63 static char bincue_path[256];
66 typedef struct track
68 unsigned short mode;
69 unsigned short minute;
70 unsigned short second;
71 unsigned short frame;
73 /* (min*60 + sec) * 75 + fps */
75 unsigned long start_sector;
77 /* = the sizes in bytes off all tracks bevor this one */
78 /* its needed if there are mode1 tracks befor the mpeg tracks */
79 unsigned long start_offset;
81 /* unsigned char num[3]; */
82 } tTrack;
84 /* max 99 tracks on a cd */
85 static tTrack tracks[100];
87 static struct cue_track_pos {
88 int track;
89 unsigned short mode;
90 unsigned short minute;
91 unsigned short second;
92 unsigned short frame;
93 } cue_current_pos;
95 /* number of tracks on the cd */
96 static int nTracks = 0;
98 static int digits2int(char s[2], int errval) {
99 uint8_t a = s[0] - '0';
100 uint8_t b = s[1] - '0';
101 if (a > 9 || b > 9)
102 return errval;
103 return a * 10 + b;
106 /* presumes Line is preloaded with the "current" line of the file */
107 static int cue_getTrackinfo(char *Line, tTrack *track)
109 int already_set = 0;
111 /* Get the 'mode' */
112 if (strncmp(&Line[2], "TRACK ", 6)==0)
114 /* strncpy(track->num, &Line[8], 2); track->num[2] = '\0'; */
116 track->mode = UNKNOWN;
117 if(strncmp(&Line[11], "AUDIO", 5)==0) track->mode = AUDIO;
118 if(strncmp(&Line[11], "MODE1/2352", 10)==0) track->mode = MODE1_2352;
119 if(strncmp(&Line[11], "MODE1/2048", 10)==0) track->mode = MODE1_2048;
120 if(strncmp(&Line[11], "MODE2/2352", 10)==0) track->mode = MODE2_2352;
121 if(strncmp(&Line[11], "MODE2/2336", 10)==0) track->mode = MODE2_2336;
123 else return 1;
125 /* Get the track indexes */
126 while(1) {
127 if(! fgets( Line, 256, fd_cue ) ) { break;}
129 if (strncmp(&Line[2], "TRACK ", 6)==0)
131 /* next track starting */
132 break;
135 /* Track 0 or 1, take the first an get fill the values*/
136 if (strncmp(&Line[4], "INDEX ", 6)==0)
138 /* check stuff here so if the answer is false the else stuff below won't be executed */
139 if ((already_set == 0) && digits2int(Line + 10, 100) <= 1)
141 already_set = 1;
143 track->minute = digits2int(Line + 13, 0);
144 track->second = digits2int(Line + 16, 0);
145 track->frame = digits2int(Line + 19, 0);
148 else if (strncmp(&Line[4], "PREGAP ", 7)==0) { ; /* ignore */ }
149 else if (strncmp(&Line[4], "FLAGS ", 6)==0) { ; /* ignore */ }
150 else mp_tmsg (MSGT_OPEN,MSGL_INFO,
151 "[bincue] Unexpected cuefile line: %s\n", Line);
153 return 0;
158 /* FIXME: the string operations ( strcpy,strcat ) below depend
159 * on the arrays to have the same size, thus we need to make
160 * sure the sizes are in sync.
162 static int cue_find_bin (char *firstline) {
163 int i,j;
164 char s[256];
165 char t[256];
167 /* get the filename out of that */
168 /* 12345 6 */
169 mp_msg (MSGT_OPEN,MSGL_INFO, "[bincue] cue_find_bin(%s)\n", firstline);
170 if (strncmp(firstline, "FILE \"",6)==0)
172 i = 0;
173 j = 0;
174 while ( firstline[6 + i] != '"')
176 bin_filename[j] = firstline[6 + i];
178 /* if I found a path info, than delete all bevor it */
179 switch (bin_filename[j])
181 case '\\':
182 j = 0;
183 break;
185 case '/':
186 j = 0;
187 break;
189 default:
190 j++;
192 i++;
194 bin_filename[j+1] = '\0';
198 /* now try to open that file, without path */
199 fd_bin = open (bin_filename, O_RDONLY);
200 if (fd_bin == -1)
202 mp_tmsg(MSGT_OPEN,MSGL_STATUS, "[bincue] bin filename tested: %s\n",
203 bin_filename);
205 /* now try to find it with the path of the cue file */
206 snprintf(s,sizeof( s ),"%s/%s",bincue_path,bin_filename);
207 fd_bin = open (s, O_RDONLY);
208 if (fd_bin == -1)
210 mp_tmsg(MSGT_OPEN,MSGL_STATUS,
211 "[bincue] bin filename tested: %s\n", s);
212 /* now I would say the whole filename is shit, build our own */
213 strncpy(s, cue_filename, strlen(cue_filename) - 3 );
214 s[strlen(cue_filename) - 3] = '\0';
215 strcat(s, "bin");
216 fd_bin = open (s, O_RDONLY);
217 if (fd_bin == -1)
219 mp_tmsg(MSGT_OPEN,MSGL_STATUS,
220 "[bincue] bin filename tested: %s\n", s);
222 /* ok try it with path */
223 snprintf(t, sizeof( t ), "%s/%s", bincue_path, s);
224 fd_bin = open (t, O_RDONLY);
225 if (fd_bin == -1)
227 mp_tmsg(MSGT_OPEN,MSGL_STATUS,
228 "[bincue] bin filename tested: %s\n",t);
229 /* now I would say the whole filename is shit, build our own */
230 strncpy(s, cue_filename, strlen(cue_filename) - 3 );
231 s[strlen(cue_filename) - 3] = '\0';
232 strcat(s, "img");
233 fd_bin = open (s, O_RDONLY);
234 if (fd_bin == -1)
236 mp_tmsg(MSGT_OPEN,MSGL_STATUS,
237 "[bincue] bin filename tested: %s\n", s);
238 /* ok try it with path */
239 snprintf(t, sizeof( t ), "%s/%s", bincue_path, s);
240 fd_bin = open (t, O_RDONLY);
241 if (fd_bin == -1)
243 mp_tmsg(MSGT_OPEN,MSGL_STATUS,
244 "[bincue] bin filename tested: %s\n", s);
246 /* I'll give up */
247 mp_tmsg(MSGT_OPEN,MSGL_ERR,
248 "[bincue] Couldn't find the bin file - giving up.\n");
249 return -1;
252 } else strcpy(bin_filename, t);
254 } else strcpy(bin_filename, s);
256 } else strcpy(bin_filename, s);
260 mp_tmsg(MSGT_OPEN,MSGL_INFO,
261 "[bincue] Using bin file %s.\n", bin_filename);
262 return 0;
265 static inline int cue_msf_2_sector(int minute, int second, int frame) {
266 return frame + (second + minute * 60 ) * 75;
269 static inline int cue_get_msf(void) {
270 return cue_msf_2_sector (cue_current_pos.minute,
271 cue_current_pos.second,
272 cue_current_pos.frame);
275 static inline void cue_set_msf(unsigned int sect){
276 cue_current_pos.frame=sect%75;
277 sect=sect/75;
278 cue_current_pos.second=sect%60;
279 sect=sect/60;
280 cue_current_pos.minute=sect;
283 static inline int cue_mode_2_sector_size(int mode)
285 switch (mode)
287 case AUDIO: return AUDIO;
288 case MODE1_2352: return SIZERAW;
289 case MODE1_2048: return SIZEISO_MODE1;
290 case MODE2_2352: return SIZEISO_MODE2_RAW;
291 case MODE2_2336: return SIZEISO_MODE2_FORM2;
293 default:
294 mp_tmsg(MSGT_OPEN,MSGL_FATAL,
295 "[bincue] unknown mode for binfile. Should not happen. Aborting.\n");
296 abort();
302 static int cue_read_cue (char *in_cue_filename)
304 struct stat filestat;
305 char sLine[256];
306 unsigned int sect;
307 char *s,*t;
308 int i;
310 /* we have no tracks at the beginning */
311 nTracks = 0;
313 fd_bin = 0;
315 /* split the filename into a path and filename part */
316 s = strdup(in_cue_filename);
317 t = strrchr(s, '/');
318 if (t == (char *)NULL)
319 t = ".";
320 else {
321 *t = '\0';
322 t = s;
323 if (*t == '\0')
324 strcpy(t, "/");
327 av_strlcpy(bincue_path,t,sizeof( bincue_path ));
328 mp_msg(MSGT_OPEN,MSGL_V,"dirname: %s, cuepath: %s\n", t, bincue_path);
330 /* no path at all? */
331 if (strcmp(bincue_path, ".") == 0) {
332 mp_msg(MSGT_OPEN,MSGL_V,"bincue_path: %s\n", bincue_path);
333 av_strlcpy(cue_filename,in_cue_filename,sizeof( cue_filename ));
334 } else {
335 av_strlcpy(cue_filename,in_cue_filename + strlen(bincue_path) + 1,
336 sizeof( cue_filename ));
341 /* open the cue file */
342 fd_cue = fopen (in_cue_filename, "r");
343 if (fd_cue == NULL)
345 mp_tmsg(MSGT_OPEN,MSGL_ERR,
346 "[bincue] Cannot open %s.\n", in_cue_filename);
347 return -1;
350 /* read the first line and hand it to find_bin, which will
351 test more than one possible name of the file */
353 if(! fgets( sLine, 256, fd_cue ) )
355 mp_tmsg(MSGT_OPEN,MSGL_ERR,
356 "[bincue] Error reading from %s\n", in_cue_filename);
357 fclose (fd_cue);
358 return -1;
361 if (cue_find_bin(sLine)) {
362 fclose (fd_cue);
363 return -1;
367 /* now build the track list */
368 /* red the next line and call our track finder */
369 if(! fgets( sLine, 256, fd_cue ) )
371 mp_tmsg(MSGT_OPEN,MSGL_ERR,
372 "[bincue] Error reading from %s\n", in_cue_filename);
373 fclose (fd_cue);
374 return -1;
377 while(!feof(fd_cue))
379 if (cue_getTrackinfo(sLine, &tracks[nTracks++]) != 0)
381 mp_tmsg(MSGT_OPEN,MSGL_ERR,
382 "[bincue] Error reading from %s\n", in_cue_filename);
383 fclose (fd_cue);
384 return -1;
388 /* make a fake track with stands for the Lead out */
389 if (fstat (fd_bin, &filestat) == -1) {
390 mp_tmsg(MSGT_OPEN,MSGL_ERR,
391 "[bincue] Error getting size of bin file.\n");
392 fclose (fd_cue);
393 return -1;
396 sect = filestat.st_size / 2352;
398 tracks[nTracks].frame = sect%75;
399 sect=sect/75;
400 tracks[nTracks].second = sect%60;
401 sect=sect/60;
402 tracks[nTracks].minute = sect;
405 /* let's calculate the start sectors and offsets */
406 for(i = 0; i <= nTracks; i++)
408 tracks[i].start_sector = cue_msf_2_sector(tracks[i].minute,
409 tracks[nTracks].second,
410 tracks[nTracks].frame);
412 /* if we're the first track we don't need to offset of the one befor */
413 if (i == 0)
415 /* was always 0 on my svcds, but who knows */
416 tracks[0].start_offset = tracks[0].start_sector *
417 cue_mode_2_sector_size(tracks[0].mode);
418 } else
420 tracks[i].start_offset = tracks[i-1].start_offset +
421 (tracks[i].start_sector - tracks[i-1].start_sector) *
422 cue_mode_2_sector_size(tracks[i-1].mode);
426 fclose (fd_cue);
428 return fd_bin;
434 static int cue_read_toc_entry(void) {
436 int track = cue_current_pos.track - 1;
438 /* check if its a valid track, if not return -1 */
439 if (track >= nTracks)
440 return -1;
443 switch (tracks[track].mode)
445 case AUDIO:
446 cue_current_pos.mode = AUDIO;
447 break;
448 case MODE1_2352:
449 cue_current_pos.mode = MODE1;
450 break;
451 case MODE1_2048:
452 cue_current_pos.mode = MODE1;
453 break;
454 default: /* MODE2_2352 and MODE2_2336 */
455 cue_current_pos.mode = MODE2;
457 cue_current_pos.minute = tracks[track].minute;
458 cue_current_pos.second = tracks[track].second;
459 cue_current_pos.frame = tracks[track].frame;
461 return 0;
464 static int cue_vcd_seek_to_track (int track){
465 cue_current_pos.track = track;
467 if (cue_read_toc_entry ())
468 return -1;
470 return VCD_SECTOR_DATA * cue_get_msf();
473 static int cue_vcd_get_track_end (int track){
474 cue_current_pos.frame = tracks[track].frame;
475 cue_current_pos.second = tracks[track].second;
476 cue_current_pos.minute = tracks[track].minute;
478 return VCD_SECTOR_DATA * cue_get_msf();
481 static void cue_vcd_read_toc(void){
482 int i;
483 for (i = 0; i < nTracks; ++i) {
485 mp_tmsg(MSGT_OPEN,MSGL_INFO,
486 "track %02d: format=%d %02d:%02d:%02d\n",
487 i+1,
488 tracks[i].mode,
489 tracks[i].minute,
490 tracks[i].second,
491 tracks[i].frame
496 static int cue_vcd_read(stream_t *stream, char *mem, int size) {
497 unsigned long position;
498 int track = cue_current_pos.track - 1;
500 position = tracks[track].start_offset +
501 (cue_msf_2_sector(cue_current_pos.minute,
502 cue_current_pos.second,
503 cue_current_pos.frame) -
504 tracks[track].start_sector)
505 * cue_mode_2_sector_size(tracks[track].mode);
508 if(position >= tracks[track+1].start_offset)
509 return 0;
511 if(lseek(fd_bin, position+VCD_SECTOR_OFFS, SEEK_SET) == -1) {
512 mp_tmsg(MSGT_OPEN,MSGL_ERR, "[bincue] unexpected end of bin file\n");
513 return 0;
516 if(read(fd_bin, mem, VCD_SECTOR_DATA) != VCD_SECTOR_DATA) {
517 mp_tmsg(MSGT_OPEN,MSGL_ERR, "[bincue] Couldn't read %d bytes of payload.\n", VCD_SECTOR_DATA);
518 return 0;
521 cue_current_pos.frame++;
522 if (cue_current_pos.frame==75){
523 cue_current_pos.frame=0;
524 cue_current_pos.second++;
525 if (cue_current_pos.second==60){
526 cue_current_pos.second=0;
527 cue_current_pos.minute++;
531 return VCD_SECTOR_DATA;
534 static int seek(stream_t *s,off_t newpos) {
535 s->pos=newpos;
536 cue_set_msf(s->pos/VCD_SECTOR_DATA);
537 return 1;
541 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
542 struct stream_priv_s* p = (struct stream_priv_s*)opts;
543 int ret,ret2,f,track = 0;
544 char *filename = NULL, *colon = NULL;
546 if(mode != STREAM_READ || !p->filename) {
547 m_struct_free(&stream_opts,opts);
548 return STREAM_UNSUPPORTED;
550 filename = strdup(p->filename);
551 if(!filename) {
552 m_struct_free(&stream_opts,opts);
553 return STREAM_UNSUPPORTED;
555 colon = strstr(filename, ":");
556 if(colon) {
557 if(strlen(colon)>1)
558 track = atoi(colon+1);
559 *colon = 0;
561 if(!track)
562 track = 1;
564 f = cue_read_cue(filename);
565 if(f < 0) {
566 m_struct_free(&stream_opts,opts);
567 return STREAM_UNSUPPORTED;
569 cue_vcd_read_toc();
570 ret2=cue_vcd_get_track_end(track);
571 ret=cue_vcd_seek_to_track(track);
572 if(ret<0){
573 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Error selecting VCD track." " (seek)\n");
574 return STREAM_UNSUPPORTED;
576 mp_tmsg(MSGT_OPEN,MSGL_INFO,"CUE stream_open, filename=%s, track=%d, available tracks: %d -> %d\n", filename, track, ret, ret2);
578 stream->fd = f;
579 stream->type = STREAMTYPE_VCDBINCUE;
580 stream->sector_size = VCD_SECTOR_DATA;
581 stream->flags = STREAM_READ | STREAM_SEEK_FW;
582 stream->start_pos = ret;
583 stream->end_pos = ret2;
584 stream->fill_buffer = cue_vcd_read;
585 stream->seek = seek;
587 free(filename);
588 m_struct_free(&stream_opts,opts);
589 return STREAM_OK;
592 const stream_info_t stream_info_cue = {
593 "CUE track",
594 "cue",
595 "Albeu",
596 "based on the code from ???",
597 open_s,
598 { "cue", NULL },
599 &stream_opts,
600 1 // Urls are an option string