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.
25 #include <sys/types.h>
37 #include "libavutil/avstring.h"
40 #define SIZEISO_MODE1 2048
41 #define SIZEISO_MODE2_RAW 2352
42 #define SIZEISO_MODE2_FORM1 2048
43 #define SIZEISO_MODE2_FORM2 2336
53 static struct stream_priv_s
{
55 } stream_priv_dflts
= {
59 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
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
= {
67 sizeof(struct stream_priv_s
),
72 static char cue_filename
[256];
73 static char bincue_path
[256];
79 unsigned short minute
;
80 unsigned short second
;
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]; */
94 /* max 99 tracks on a cd */
95 static tTrack tracks
[100];
97 static struct cue_track_pos
{
100 unsigned short minute
;
101 unsigned short second
;
102 unsigned short frame
;
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';
116 /* presumes Line is preloaded with the "current" line of the file */
117 static int cue_getTrackinfo(FILE *fd_cue
, char *Line
, tTrack
*track
)
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
;
135 /* Get the track indexes */
137 if(! fgets( Line
, 256, fd_cue
) ) { break;}
139 if (strncmp(&Line
[2], "TRACK ", 6)==0)
141 /* next track starting */
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)
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
);
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
;
175 char bin_filename
[256];
180 /* get the filename out of that */
182 mp_msg (MSGT_OPEN
,MSGL_INFO
, "[bincue] cue_find_bin(%s)\n", firstline
);
183 if (strncmp(firstline
, "FILE \"",6)==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
])
207 bin_filename
[j
+1] = '\0';
212 for (i
= 0; fd_bin
== -1 && i
< 6; i
++) {
215 /* now try to open that file, without path */
216 cur_name
= bin_filename
;
219 /* now try to find it with the path of the cue file */
220 snprintf(s
,sizeof( s
),"%s/%s",bincue_path
,bin_filename
);
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';
231 /* ok try it with path */
232 snprintf(t
, sizeof( t
), "%s/%s", bincue_path
, s
);
233 fd_bin
= open (t
, O_RDONLY
);
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';
244 /* ok try it with path */
245 snprintf(t
, sizeof( t
), "%s/%s", bincue_path
, s
);
249 fd_bin
= open(cur_name
, O_RDONLY
);
251 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
, "[bincue] bin filename tested: %s\n",
259 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,
260 "[bincue] Couldn't find the bin file - giving up.\n");
264 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,
265 "[bincue] Using bin file %s.\n", cur_name
);
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;
282 cue_current_pos
.second
=sect
%60;
284 cue_current_pos
.minute
=sect
;
287 static inline int cue_mode_2_sector_size(int 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
;
298 mp_tmsg(MSGT_OPEN
,MSGL_FATAL
,
299 "[bincue] unknown mode for binfile. Should not happen. Aborting.\n");
306 static int cue_read_cue (char *in_cue_filename
)
308 struct stat filestat
;
316 /* we have no tracks at the beginning */
319 /* split the filename into a path and filename part */
320 s
= strdup(in_cue_filename
);
322 if (t
== (char *)NULL
)
331 av_strlcpy(bincue_path
,t
,sizeof( bincue_path
));
332 mp_msg(MSGT_OPEN
,MSGL_V
,"dirname: %s, cuepath: %s\n", t
, bincue_path
);
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
));
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");
351 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,
352 "[bincue] Cannot open %s.\n", in_cue_filename
);
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
);
367 fd_bin
= cue_find_bin(sLine
);
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
);
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
);
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");
403 sect
= filestat
.st_size
/ 2352;
405 tracks
[nTracks
].frame
= sect
%75;
407 tracks
[nTracks
].second
= 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 */
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
);
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
);
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
)
447 cue_current_pos
.track
= track
;
449 switch (tracks
[track
].mode
)
452 cue_current_pos
.mode
= AUDIO
;
455 cue_current_pos
.mode
= MODE1
;
458 cue_current_pos
.mode
= MODE1
;
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
;
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
) {
479 cue_set_msf(s
->pos
/VCD_SECTOR_DATA
);
483 static int cue_vcd_seek_to_track (stream_t
*stream
, int track
){
485 if (cue_read_toc_entry (track
))
488 pos
= VCD_SECTOR_DATA
* cue_get_msf();
489 stream
->start_pos
= pos
;
490 stream
->end_pos
= cue_vcd_get_track_end(track
);
495 static void cue_vcd_read_toc(void){
497 for (i
= 0; i
< nTracks
; ++i
) {
499 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,
500 "track %02d: format=%d %02d:%02d:%02d\n",
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
)
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");
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
);
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
) {
551 case STREAM_CTRL_GET_NUM_CHAPTERS
:
553 *(unsigned int *)arg
= nTracks
;
556 case STREAM_CTRL_SEEK_TO_CHAPTER
:
559 unsigned int track
= *(unsigned int *)arg
+ 1;
560 r
= cue_vcd_seek_to_track(stream
, track
);
566 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
568 *(unsigned int *)arg
= cue_current_pos
.track
- 1;
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
;
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
);
586 m_struct_free(&stream_opts
,opts
);
587 return STREAM_UNSUPPORTED
;
589 colon
= strstr(filename
, ":");
592 track
= atoi(colon
+1);
598 f
= cue_read_cue(filename
);
600 m_struct_free(&stream_opts
,opts
);
601 return STREAM_UNSUPPORTED
;
604 ret
=cue_vcd_seek_to_track(stream
, track
);
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
);
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
;
621 stream
->control
= control
;
624 m_struct_free(&stream_opts
,opts
);
628 const stream_info_t stream_info_cue
= {
632 "based on the code from ???",
636 1 // Urls are an option string