ad_liba52: Fix -ac a52 with tags other than 0x2000 or dnet
[mplayer/greg.git] / stream / stream_cue.c
blob8851595c81a9710a71496e3949227d3ec7e932f7
1 /*
2 * VideoCD BinCue
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <fcntl.h>
30 #include "config.h"
31 #include "mp_msg.h"
33 #include "stream.h"
35 #include "m_option.h"
36 #include "m_struct.h"
37 #include "libavutil/avstring.h"
39 #define SIZERAW 2352
40 #define SIZEISO_MODE1 2048
41 #define SIZEISO_MODE2_RAW 2352
42 #define SIZEISO_MODE2_FORM1 2048
43 #define SIZEISO_MODE2_FORM2 2336
44 #define AUDIO 0
45 #define MODE1 1
46 #define MODE2 2
47 #define MODE1_2352 10
48 #define MODE2_2352 20
49 #define MODE1_2048 30
50 #define MODE2_2336 40
51 #define UNKNOWN -1
53 static struct stream_priv_s {
54 char* filename;
55 } stream_priv_dflts = {
56 NULL
59 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
60 /// URL definition
61 static const m_option_t stream_opts_fields[] = {
62 { "string", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
63 { NULL, NULL, 0, 0, 0, 0, NULL }
65 static const struct m_struct_st stream_opts = {
66 "cue",
67 sizeof(struct stream_priv_s),
68 &stream_priv_dflts,
69 stream_opts_fields
72 static char cue_filename[256];
73 static char bincue_path[256];
76 typedef struct track
78 unsigned short mode;
79 unsigned short minute;
80 unsigned short second;
81 unsigned short frame;
83 /* (min*60 + sec) * 75 + fps */
85 unsigned long start_sector;
87 /* = the sizes in bytes off all tracks bevor this one */
88 /* its needed if there are mode1 tracks befor the mpeg tracks */
89 unsigned long start_offset;
91 /* unsigned char num[3]; */
92 } tTrack;
94 /* max 99 tracks on a cd */
95 static tTrack tracks[100];
97 static struct cue_track_pos {
98 int track;
99 unsigned short mode;
100 unsigned short minute;
101 unsigned short second;
102 unsigned short frame;
103 } cue_current_pos;
105 /* number of tracks on the cd */
106 static int nTracks = 0;
108 static int digits2int(char s[2], int errval) {
109 uint8_t a = s[0] - '0';
110 uint8_t b = s[1] - '0';
111 if (a > 9 || b > 9)
112 return errval;
113 return a * 10 + b;
116 /* presumes Line is preloaded with the "current" line of the file */
117 static int cue_getTrackinfo(FILE *fd_cue, char *Line, tTrack *track)
119 int already_set = 0;
121 /* Get the 'mode' */
122 if (strncmp(&Line[2], "TRACK ", 6)==0)
124 /* strncpy(track->num, &Line[8], 2); track->num[2] = '\0'; */
126 track->mode = UNKNOWN;
127 if(strncmp(&Line[11], "AUDIO", 5)==0) track->mode = AUDIO;
128 if(strncmp(&Line[11], "MODE1/2352", 10)==0) track->mode = MODE1_2352;
129 if(strncmp(&Line[11], "MODE1/2048", 10)==0) track->mode = MODE1_2048;
130 if(strncmp(&Line[11], "MODE2/2352", 10)==0) track->mode = MODE2_2352;
131 if(strncmp(&Line[11], "MODE2/2336", 10)==0) track->mode = MODE2_2336;
133 else return 1;
135 /* Get the track indexes */
136 while(1) {
137 if(! fgets( Line, 256, fd_cue ) ) { break;}
139 if (strncmp(&Line[2], "TRACK ", 6)==0)
141 /* next track starting */
142 break;
145 /* Track 0 or 1, take the first an get fill the values*/
146 if (strncmp(&Line[4], "INDEX ", 6)==0)
148 /* check stuff here so if the answer is false the else stuff below won't be executed */
149 if ((already_set == 0) && digits2int(Line + 10, 100) <= 1)
151 already_set = 1;
153 track->minute = digits2int(Line + 13, 0);
154 track->second = digits2int(Line + 16, 0);
155 track->frame = digits2int(Line + 19, 0);
158 else if (strncmp(&Line[4], "PREGAP ", 7)==0) { ; /* ignore */ }
159 else if (strncmp(&Line[4], "FLAGS ", 6)==0) { ; /* ignore */ }
160 else mp_tmsg (MSGT_OPEN,MSGL_INFO,
161 "[bincue] Unexpected cuefile line: %s\n", Line);
163 return 0;
168 /* FIXME: the string operations ( strcpy,strcat ) below depend
169 * on the arrays to have the same size, thus we need to make
170 * sure the sizes are in sync.
172 static int cue_find_bin (char *firstline) {
173 const char *cur_name;
174 int i,j;
175 char bin_filename[256];
176 char s[256];
177 char t[256];
178 int fd_bin;
180 /* get the filename out of that */
181 /* 12345 6 */
182 mp_msg (MSGT_OPEN,MSGL_INFO, "[bincue] cue_find_bin(%s)\n", firstline);
183 if (strncmp(firstline, "FILE \"",6)==0)
185 i = 0;
186 j = 0;
187 while ( firstline[6 + i] != '"')
189 bin_filename[j] = firstline[6 + i];
191 /* if I found a path info, than delete all bevor it */
192 switch (bin_filename[j])
194 case '\\':
195 j = 0;
196 break;
198 case '/':
199 j = 0;
200 break;
202 default:
203 j++;
205 i++;
207 bin_filename[j+1] = '\0';
211 fd_bin = -1;
212 for (i = 0; fd_bin == -1 && i < 6; i++) {
213 switch (i) {
214 case 0:
215 /* now try to open that file, without path */
216 cur_name = bin_filename;
217 break;
218 case 1:
219 /* now try to find it with the path of the cue file */
220 snprintf(s,sizeof( s ),"%s/%s",bincue_path,bin_filename);
221 cur_name = s;
222 break;
223 case 2:
224 /* now I would say the whole filename is shit, build our own */
225 strncpy(s, cue_filename, strlen(cue_filename) - 3 );
226 s[strlen(cue_filename) - 3] = '\0';
227 strcat(s, "bin");
228 cur_name = s;
229 break;
230 case 3:
231 /* ok try it with path */
232 snprintf(t, sizeof( t ), "%s/%s", bincue_path, s);
233 fd_bin = open (t, O_RDONLY);
234 cur_name = t;
235 break;
236 case 4:
237 /* now I would say the whole filename is shit, build our own */
238 strncpy(s, cue_filename, strlen(cue_filename) - 3 );
239 s[strlen(cue_filename) - 3] = '\0';
240 strcat(s, "img");
241 cur_name = s;
242 break;
243 case 5:
244 /* ok try it with path */
245 snprintf(t, sizeof( t ), "%s/%s", bincue_path, s);
246 cur_name = t;
247 break;
249 fd_bin = open(cur_name, O_RDONLY);
250 if (fd_bin == -1) {
251 mp_tmsg(MSGT_OPEN,MSGL_STATUS, "[bincue] bin filename tested: %s\n",
252 cur_name);
256 if (fd_bin == -1)
258 /* I'll give up */
259 mp_tmsg(MSGT_OPEN,MSGL_ERR,
260 "[bincue] Couldn't find the bin file - giving up.\n");
261 return -1;
264 mp_tmsg(MSGT_OPEN,MSGL_INFO,
265 "[bincue] Using bin file %s.\n", cur_name);
266 return fd_bin;
269 static inline int cue_msf_2_sector(int minute, int second, int frame) {
270 return frame + (second + minute * 60 ) * 75;
273 static inline int cue_get_msf(void) {
274 return cue_msf_2_sector (cue_current_pos.minute,
275 cue_current_pos.second,
276 cue_current_pos.frame);
279 static inline void cue_set_msf(unsigned int sect){
280 cue_current_pos.frame=sect%75;
281 sect=sect/75;
282 cue_current_pos.second=sect%60;
283 sect=sect/60;
284 cue_current_pos.minute=sect;
287 static inline int cue_mode_2_sector_size(int mode)
289 switch (mode)
291 case AUDIO: return AUDIO;
292 case MODE1_2352: return SIZERAW;
293 case MODE1_2048: return SIZEISO_MODE1;
294 case MODE2_2352: return SIZEISO_MODE2_RAW;
295 case MODE2_2336: return SIZEISO_MODE2_FORM2;
297 default:
298 mp_tmsg(MSGT_OPEN,MSGL_FATAL,
299 "[bincue] unknown mode for binfile. Should not happen. Aborting.\n");
300 abort();
306 static int cue_read_cue (char *in_cue_filename)
308 struct stat filestat;
309 char sLine[256];
310 unsigned int sect;
311 char *s,*t;
312 int i;
313 int fd_bin;
314 FILE *fd_cue;
316 /* we have no tracks at the beginning */
317 nTracks = 0;
319 /* split the filename into a path and filename part */
320 s = strdup(in_cue_filename);
321 t = strrchr(s, '/');
322 if (t == (char *)NULL)
323 t = ".";
324 else {
325 *t = '\0';
326 t = s;
327 if (*t == '\0')
328 strcpy(t, "/");
331 av_strlcpy(bincue_path,t,sizeof( bincue_path ));
332 mp_msg(MSGT_OPEN,MSGL_V,"dirname: %s, cuepath: %s\n", t, bincue_path);
333 free(s);
334 s = t = NULL;
336 /* no path at all? */
337 if (strcmp(bincue_path, ".") == 0) {
338 mp_msg(MSGT_OPEN,MSGL_V,"bincue_path: %s\n", bincue_path);
339 av_strlcpy(cue_filename,in_cue_filename,sizeof( cue_filename ));
340 } else {
341 av_strlcpy(cue_filename,in_cue_filename + strlen(bincue_path) + 1,
342 sizeof( cue_filename ));
347 /* open the cue file */
348 fd_cue = fopen (in_cue_filename, "r");
349 if (fd_cue == NULL)
351 mp_tmsg(MSGT_OPEN,MSGL_ERR,
352 "[bincue] Cannot open %s.\n", in_cue_filename);
353 return -1;
356 /* read the first line and hand it to find_bin, which will
357 test more than one possible name of the file */
359 if(! fgets( sLine, sizeof(sLine), fd_cue ) )
361 mp_tmsg(MSGT_OPEN,MSGL_ERR,
362 "[bincue] Error reading from %s\n", in_cue_filename);
363 fclose (fd_cue);
364 return -1;
367 fd_bin = cue_find_bin(sLine);
368 if (fd_bin == -1) {
369 fclose (fd_cue);
370 return -1;
374 /* now build the track list */
375 /* red the next line and call our track finder */
376 if(! fgets( sLine, sizeof(sLine), fd_cue ) )
378 mp_tmsg(MSGT_OPEN,MSGL_ERR,
379 "[bincue] Error reading from %s\n", in_cue_filename);
380 fclose (fd_cue);
381 return -1;
384 while(!feof(fd_cue))
386 if (cue_getTrackinfo(fd_cue, sLine, &tracks[nTracks++]) != 0)
388 mp_tmsg(MSGT_OPEN,MSGL_ERR,
389 "[bincue] Error reading from %s\n", in_cue_filename);
390 fclose (fd_cue);
391 return -1;
395 /* make a fake track with stands for the Lead out */
396 if (fstat (fd_bin, &filestat) == -1) {
397 mp_tmsg(MSGT_OPEN,MSGL_ERR,
398 "[bincue] Error getting size of bin file.\n");
399 fclose (fd_cue);
400 return -1;
403 sect = filestat.st_size / 2352;
405 tracks[nTracks].frame = sect%75;
406 sect=sect/75;
407 tracks[nTracks].second = sect%60;
408 sect=sect/60;
409 tracks[nTracks].minute = sect;
412 /* let's calculate the start sectors and offsets */
413 for(i = 0; i <= nTracks; i++)
415 tracks[i].start_sector = cue_msf_2_sector(tracks[i].minute,
416 tracks[nTracks].second,
417 tracks[nTracks].frame);
419 /* if we're the first track we don't need to offset of the one befor */
420 if (i == 0)
422 /* was always 0 on my svcds, but who knows */
423 tracks[0].start_offset = tracks[0].start_sector *
424 cue_mode_2_sector_size(tracks[0].mode);
425 } else
427 tracks[i].start_offset = tracks[i-1].start_offset +
428 (tracks[i].start_sector - tracks[i-1].start_sector) *
429 cue_mode_2_sector_size(tracks[i-1].mode);
433 fclose (fd_cue);
435 return fd_bin;
441 static int cue_read_toc_entry(int track) {
442 /* check if its a valid track, if not return -1 */
443 if (track <= 0 || track > nTracks)
444 return -1;
447 cue_current_pos.track = track;
448 track--;
449 switch (tracks[track].mode)
451 case AUDIO:
452 cue_current_pos.mode = AUDIO;
453 break;
454 case MODE1_2352:
455 cue_current_pos.mode = MODE1;
456 break;
457 case MODE1_2048:
458 cue_current_pos.mode = MODE1;
459 break;
460 default: /* MODE2_2352 and MODE2_2336 */
461 cue_current_pos.mode = MODE2;
463 cue_current_pos.minute = tracks[track].minute;
464 cue_current_pos.second = tracks[track].second;
465 cue_current_pos.frame = tracks[track].frame;
467 return 0;
470 static int cue_vcd_get_track_end (int track){
471 int sector = cue_msf_2_sector(tracks[track].minute, tracks[track].second,
472 tracks[track].frame);
474 return VCD_SECTOR_DATA * sector;
477 static int seek(stream_t *s,off_t newpos) {
478 s->pos=newpos;
479 cue_set_msf(s->pos/VCD_SECTOR_DATA);
480 return 1;
483 static int cue_vcd_seek_to_track (stream_t *stream, int track){
484 int pos;
485 if (cue_read_toc_entry (track))
486 return -1;
488 pos = VCD_SECTOR_DATA * cue_get_msf();
489 stream->start_pos = pos;
490 stream->end_pos = cue_vcd_get_track_end(track);
491 seek(stream, pos);
492 return pos;
495 static void cue_vcd_read_toc(void){
496 int i;
497 for (i = 0; i < nTracks; ++i) {
499 mp_tmsg(MSGT_OPEN,MSGL_INFO,
500 "track %02d: format=%d %02d:%02d:%02d\n",
501 i+1,
502 tracks[i].mode,
503 tracks[i].minute,
504 tracks[i].second,
505 tracks[i].frame
510 static int cue_vcd_read(stream_t *stream, char *mem, int size) {
511 unsigned long position;
512 int fd_bin = stream->fd;
513 int track = cue_current_pos.track - 1;
515 position = tracks[track].start_offset +
516 (cue_msf_2_sector(cue_current_pos.minute,
517 cue_current_pos.second,
518 cue_current_pos.frame) -
519 tracks[track].start_sector)
520 * cue_mode_2_sector_size(tracks[track].mode);
523 if(position >= tracks[track+1].start_offset)
524 return 0;
526 if(lseek(fd_bin, position+VCD_SECTOR_OFFS, SEEK_SET) == -1) {
527 mp_tmsg(MSGT_OPEN,MSGL_ERR, "[bincue] unexpected end of bin file\n");
528 return 0;
531 if(read(fd_bin, mem, VCD_SECTOR_DATA) != VCD_SECTOR_DATA) {
532 mp_tmsg(MSGT_OPEN,MSGL_ERR, "[bincue] Couldn't read %d bytes of payload.\n", VCD_SECTOR_DATA);
533 return 0;
536 cue_current_pos.frame++;
537 if (cue_current_pos.frame==75){
538 cue_current_pos.frame=0;
539 cue_current_pos.second++;
540 if (cue_current_pos.second==60){
541 cue_current_pos.second=0;
542 cue_current_pos.minute++;
546 return VCD_SECTOR_DATA;
549 static int control(stream_t *stream, int cmd, void *arg) {
550 switch(cmd) {
551 case STREAM_CTRL_GET_NUM_CHAPTERS:
553 *(unsigned int *)arg = nTracks;
554 return STREAM_OK;
556 case STREAM_CTRL_SEEK_TO_CHAPTER:
558 int r;
559 unsigned int track = *(unsigned int *)arg + 1;
560 r = cue_vcd_seek_to_track(stream, track);
561 if (r >= 0) {
562 return STREAM_OK;
564 break;
566 case STREAM_CTRL_GET_CURRENT_CHAPTER:
568 *(unsigned int *)arg = cue_current_pos.track - 1;
569 return STREAM_OK;
572 return STREAM_UNSUPPORTED;
575 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
576 struct stream_priv_s* p = (struct stream_priv_s*)opts;
577 int ret,f,track = 0;
578 char *filename = NULL, *colon = NULL;
580 if(mode != STREAM_READ || !p->filename) {
581 m_struct_free(&stream_opts,opts);
582 return STREAM_UNSUPPORTED;
584 filename = strdup(p->filename);
585 if(!filename) {
586 m_struct_free(&stream_opts,opts);
587 return STREAM_UNSUPPORTED;
589 colon = strstr(filename, ":");
590 if(colon) {
591 if(strlen(colon)>1)
592 track = atoi(colon+1);
593 *colon = 0;
595 if(!track)
596 track = 1;
598 f = cue_read_cue(filename);
599 if(f < 0) {
600 m_struct_free(&stream_opts,opts);
601 return STREAM_UNSUPPORTED;
603 cue_vcd_read_toc();
604 ret=cue_vcd_seek_to_track(stream, track);
605 if(ret<0){
606 mp_msg(MSGT_OPEN, MSGL_ERR, "%s (seek)\n",
607 mp_gtext("Error selecting VCD track."));
608 return STREAM_UNSUPPORTED;
610 mp_tmsg(MSGT_OPEN, MSGL_INFO,
611 "CUE stream_open, filename=%s, track=%d, "
612 "available tracks: %d -> %d\n",
613 filename, track, ret, (int)stream->end_pos);
615 stream->fd = f;
616 stream->type = STREAMTYPE_VCDBINCUE;
617 stream->sector_size = VCD_SECTOR_DATA;
618 stream->flags = STREAM_READ | MP_STREAM_SEEK_FW;
619 stream->fill_buffer = cue_vcd_read;
620 stream->seek = seek;
621 stream->control = control;
623 free(filename);
624 m_struct_free(&stream_opts,opts);
625 return STREAM_OK;
628 const stream_info_t stream_info_cue = {
629 "CUE track",
630 "cue",
631 "Albeu",
632 "based on the code from ???",
633 open_s,
634 { "cue", NULL },
635 &stream_opts,
636 1 // Urls are an option string