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>
39 #include "libavutil/avstring.h"
42 #define SIZEISO_MODE1 2048
43 #define SIZEISO_MODE2_RAW 2352
44 #define SIZEISO_MODE2_FORM1 2048
45 #define SIZEISO_MODE2_FORM2 2336
55 static struct stream_priv_s
{
57 } stream_priv_dflts
= {
61 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
63 static const m_option_t stream_opts_fields
[] = {
64 { "string", ST_OFF(filename
), CONF_TYPE_STRING
, 0, 0 ,0, NULL
},
65 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
67 static const struct m_struct_st stream_opts
= {
69 sizeof(struct stream_priv_s
),
74 static char cue_filename
[256];
75 static char bincue_path
[256];
81 unsigned short minute
;
82 unsigned short second
;
85 /* (min*60 + sec) * 75 + fps */
87 unsigned long start_sector
;
89 /* = the sizes in bytes off all tracks bevor this one */
90 /* its needed if there are mode1 tracks befor the mpeg tracks */
91 unsigned long start_offset
;
93 /* unsigned char num[3]; */
96 /* max 99 tracks on a cd */
97 static tTrack tracks
[100];
99 static struct cue_track_pos
{
102 unsigned short minute
;
103 unsigned short second
;
104 unsigned short frame
;
107 /* number of tracks on the cd */
108 static int nTracks
= 0;
110 static int digits2int(char s
[2], int errval
) {
111 uint8_t a
= s
[0] - '0';
112 uint8_t b
= s
[1] - '0';
118 /* presumes Line is preloaded with the "current" line of the file */
119 static int cue_getTrackinfo(FILE *fd_cue
, char *Line
, tTrack
*track
)
124 if (strncmp(&Line
[2], "TRACK ", 6)==0)
126 /* strncpy(track->num, &Line[8], 2); track->num[2] = '\0'; */
128 track
->mode
= UNKNOWN
;
129 if(strncmp(&Line
[11], "AUDIO", 5)==0) track
->mode
= AUDIO
;
130 if(strncmp(&Line
[11], "MODE1/2352", 10)==0) track
->mode
= MODE1_2352
;
131 if(strncmp(&Line
[11], "MODE1/2048", 10)==0) track
->mode
= MODE1_2048
;
132 if(strncmp(&Line
[11], "MODE2/2352", 10)==0) track
->mode
= MODE2_2352
;
133 if(strncmp(&Line
[11], "MODE2/2336", 10)==0) track
->mode
= MODE2_2336
;
137 /* Get the track indexes */
139 if(! fgets( Line
, 256, fd_cue
) ) { break;}
141 if (strncmp(&Line
[2], "TRACK ", 6)==0)
143 /* next track starting */
147 /* Track 0 or 1, take the first an get fill the values*/
148 if (strncmp(&Line
[4], "INDEX ", 6)==0)
150 /* check stuff here so if the answer is false the else stuff below won't be executed */
151 if ((already_set
== 0) && digits2int(Line
+ 10, 100) <= 1)
155 track
->minute
= digits2int(Line
+ 13, 0);
156 track
->second
= digits2int(Line
+ 16, 0);
157 track
->frame
= digits2int(Line
+ 19, 0);
160 else if (strncmp(&Line
[4], "PREGAP ", 7)==0) { ; /* ignore */ }
161 else if (strncmp(&Line
[4], "FLAGS ", 6)==0) { ; /* ignore */ }
162 else mp_msg (MSGT_OPEN
,MSGL_INFO
,
163 MSGTR_MPDEMUX_CUEREAD_UnexpectedCuefileLine
, Line
);
170 /* FIXME: the string operations ( strcpy,strcat ) below depend
171 * on the arrays to have the same size, thus we need to make
172 * sure the sizes are in sync.
174 static int cue_find_bin (char *firstline
) {
175 const char *cur_name
;
177 char bin_filename
[256];
182 /* get the filename out of that */
184 mp_msg (MSGT_OPEN
,MSGL_INFO
, "[bincue] cue_find_bin(%s)\n", firstline
);
185 if (strncmp(firstline
, "FILE \"",6)==0)
189 while ( firstline
[6 + i
] != '"')
191 bin_filename
[j
] = firstline
[6 + i
];
193 /* if I found a path info, than delete all bevor it */
194 switch (bin_filename
[j
])
209 bin_filename
[j
+1] = '\0';
214 for (i
= 0; fd_bin
== -1 && i
< 6; i
++) {
217 /* now try to open that file, without path */
218 cur_name
= bin_filename
;
221 /* now try to find it with the path of the cue file */
222 snprintf(s
,sizeof( s
),"%s/%s",bincue_path
,bin_filename
);
226 /* now I would say the whole filename is shit, build our own */
227 strncpy(s
, cue_filename
, strlen(cue_filename
) - 3 );
228 s
[strlen(cue_filename
) - 3] = '\0';
233 /* ok try it with path */
234 snprintf(t
, sizeof( t
), "%s/%s", bincue_path
, s
);
235 fd_bin
= open (t
, O_RDONLY
);
239 /* now I would say the whole filename is shit, build our own */
240 strncpy(s
, cue_filename
, strlen(cue_filename
) - 3 );
241 s
[strlen(cue_filename
) - 3] = '\0';
246 /* ok try it with path */
247 snprintf(t
, sizeof( t
), "%s/%s", bincue_path
, s
);
251 fd_bin
= open(cur_name
, O_RDONLY
);
253 mp_msg(MSGT_OPEN
,MSGL_STATUS
, MSGTR_MPDEMUX_CUEREAD_BinFilenameTested
,
261 mp_msg(MSGT_OPEN
,MSGL_ERR
,
262 MSGTR_MPDEMUX_CUEREAD_CannotFindBinFile
);
266 mp_msg(MSGT_OPEN
,MSGL_INFO
,
267 MSGTR_MPDEMUX_CUEREAD_UsingBinFile
, cur_name
);
271 static inline int cue_msf_2_sector(int minute
, int second
, int frame
) {
272 return frame
+ (second
+ minute
* 60 ) * 75;
275 static inline int cue_get_msf(void) {
276 return cue_msf_2_sector (cue_current_pos
.minute
,
277 cue_current_pos
.second
,
278 cue_current_pos
.frame
);
281 static inline void cue_set_msf(unsigned int sect
){
282 cue_current_pos
.frame
=sect
%75;
284 cue_current_pos
.second
=sect
%60;
286 cue_current_pos
.minute
=sect
;
289 static inline int cue_mode_2_sector_size(int mode
)
293 case AUDIO
: return AUDIO
;
294 case MODE1_2352
: return SIZERAW
;
295 case MODE1_2048
: return SIZEISO_MODE1
;
296 case MODE2_2352
: return SIZEISO_MODE2_RAW
;
297 case MODE2_2336
: return SIZEISO_MODE2_FORM2
;
300 mp_msg(MSGT_OPEN
,MSGL_FATAL
,
301 MSGTR_MPDEMUX_CUEREAD_UnknownModeForBinfile
);
308 static int cue_read_cue (char *in_cue_filename
)
310 struct stat filestat
;
318 /* we have no tracks at the beginning */
321 /* split the filename into a path and filename part */
322 s
= strdup(in_cue_filename
);
324 if (t
== (char *)NULL
)
333 av_strlcpy(bincue_path
,t
,sizeof( bincue_path
));
334 mp_msg(MSGT_OPEN
,MSGL_V
,"dirname: %s, cuepath: %s\n", t
, bincue_path
);
338 /* no path at all? */
339 if (strcmp(bincue_path
, ".") == 0) {
340 mp_msg(MSGT_OPEN
,MSGL_V
,"bincue_path: %s\n", bincue_path
);
341 av_strlcpy(cue_filename
,in_cue_filename
,sizeof( cue_filename
));
343 av_strlcpy(cue_filename
,in_cue_filename
+ strlen(bincue_path
) + 1,
344 sizeof( cue_filename
));
349 /* open the cue file */
350 fd_cue
= fopen (in_cue_filename
, "r");
353 mp_msg(MSGT_OPEN
,MSGL_ERR
,
354 MSGTR_MPDEMUX_CUEREAD_CannotOpenCueFile
, in_cue_filename
);
358 /* read the first line and hand it to find_bin, which will
359 test more than one possible name of the file */
361 if(! fgets( sLine
, sizeof(sLine
), fd_cue
) )
363 mp_msg(MSGT_OPEN
,MSGL_ERR
,
364 MSGTR_MPDEMUX_CUEREAD_ErrReadingFromCueFile
, in_cue_filename
);
369 fd_bin
= cue_find_bin(sLine
);
376 /* now build the track list */
377 /* red the next line and call our track finder */
378 if(! fgets( sLine
, sizeof(sLine
), fd_cue
) )
380 mp_msg(MSGT_OPEN
,MSGL_ERR
,
381 MSGTR_MPDEMUX_CUEREAD_ErrReadingFromCueFile
, in_cue_filename
);
388 if (cue_getTrackinfo(fd_cue
, sLine
, &tracks
[nTracks
++]) != 0)
390 mp_msg(MSGT_OPEN
,MSGL_ERR
,
391 MSGTR_MPDEMUX_CUEREAD_ErrReadingFromCueFile
, in_cue_filename
);
397 /* make a fake track with stands for the Lead out */
398 if (fstat (fd_bin
, &filestat
) == -1) {
399 mp_msg(MSGT_OPEN
,MSGL_ERR
,
400 MSGTR_MPDEMUX_CUEREAD_ErrGettingBinFileSize
);
405 sect
= filestat
.st_size
/ 2352;
407 tracks
[nTracks
].frame
= sect
%75;
409 tracks
[nTracks
].second
= sect
%60;
411 tracks
[nTracks
].minute
= sect
;
414 /* let's calculate the start sectors and offsets */
415 for(i
= 0; i
<= nTracks
; i
++)
417 tracks
[i
].start_sector
= cue_msf_2_sector(tracks
[i
].minute
,
418 tracks
[nTracks
].second
,
419 tracks
[nTracks
].frame
);
421 /* if we're the first track we don't need to offset of the one befor */
424 /* was always 0 on my svcds, but who knows */
425 tracks
[0].start_offset
= tracks
[0].start_sector
*
426 cue_mode_2_sector_size(tracks
[0].mode
);
429 tracks
[i
].start_offset
= tracks
[i
-1].start_offset
+
430 (tracks
[i
].start_sector
- tracks
[i
-1].start_sector
) *
431 cue_mode_2_sector_size(tracks
[i
-1].mode
);
443 static int cue_read_toc_entry(int track
) {
444 /* check if its a valid track, if not return -1 */
445 if (track
<= 0 || track
> nTracks
)
449 cue_current_pos
.track
= track
;
451 switch (tracks
[track
].mode
)
454 cue_current_pos
.mode
= AUDIO
;
457 cue_current_pos
.mode
= MODE1
;
460 cue_current_pos
.mode
= MODE1
;
462 default: /* MODE2_2352 and MODE2_2336 */
463 cue_current_pos
.mode
= MODE2
;
465 cue_current_pos
.minute
= tracks
[track
].minute
;
466 cue_current_pos
.second
= tracks
[track
].second
;
467 cue_current_pos
.frame
= tracks
[track
].frame
;
472 static int cue_vcd_get_track_end (int track
){
473 int sector
= cue_msf_2_sector(tracks
[track
].minute
, tracks
[track
].second
,
474 tracks
[track
].frame
);
476 return VCD_SECTOR_DATA
* sector
;
479 static int seek(stream_t
*s
,off_t newpos
) {
481 cue_set_msf(s
->pos
/VCD_SECTOR_DATA
);
485 static int cue_vcd_seek_to_track (stream_t
*stream
, int track
){
487 if (cue_read_toc_entry (track
))
490 pos
= VCD_SECTOR_DATA
* cue_get_msf();
491 stream
->start_pos
= pos
;
492 stream
->end_pos
= cue_vcd_get_track_end(track
);
497 static void cue_vcd_read_toc(void){
499 for (i
= 0; i
< nTracks
; ++i
) {
501 mp_msg(MSGT_OPEN
,MSGL_INFO
,
502 MSGTR_MPDEMUX_CUEREAD_InfoTrackFormat
,
512 static int cue_vcd_read(stream_t
*stream
, char *mem
, int size
) {
513 unsigned long position
;
514 int fd_bin
= stream
->fd
;
515 int track
= cue_current_pos
.track
- 1;
517 position
= tracks
[track
].start_offset
+
518 (cue_msf_2_sector(cue_current_pos
.minute
,
519 cue_current_pos
.second
,
520 cue_current_pos
.frame
) -
521 tracks
[track
].start_sector
)
522 * cue_mode_2_sector_size(tracks
[track
].mode
);
525 if(position
>= tracks
[track
+1].start_offset
)
528 if(lseek(fd_bin
, position
+VCD_SECTOR_OFFS
, SEEK_SET
) == -1) {
529 mp_msg(MSGT_OPEN
,MSGL_ERR
, MSGTR_MPDEMUX_CUEREAD_UnexpectedBinFileEOF
);
533 if(read(fd_bin
, mem
, VCD_SECTOR_DATA
) != VCD_SECTOR_DATA
) {
534 mp_msg(MSGT_OPEN
,MSGL_ERR
, MSGTR_MPDEMUX_CUEREAD_CannotReadNBytesOfPayload
, VCD_SECTOR_DATA
);
538 cue_current_pos
.frame
++;
539 if (cue_current_pos
.frame
==75){
540 cue_current_pos
.frame
=0;
541 cue_current_pos
.second
++;
542 if (cue_current_pos
.second
==60){
543 cue_current_pos
.second
=0;
544 cue_current_pos
.minute
++;
548 return VCD_SECTOR_DATA
;
551 static int control(stream_t
*stream
, int cmd
, void *arg
) {
553 case STREAM_CTRL_GET_NUM_CHAPTERS
:
555 *(unsigned int *)arg
= nTracks
;
558 case STREAM_CTRL_SEEK_TO_CHAPTER
:
561 unsigned int track
= *(unsigned int *)arg
+ 1;
562 r
= cue_vcd_seek_to_track(stream
, track
);
568 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
570 *(unsigned int *)arg
= cue_current_pos
.track
- 1;
574 return STREAM_UNSUPPORTED
;
577 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
578 struct stream_priv_s
* p
= (struct stream_priv_s
*)opts
;
580 char *filename
= NULL
, *colon
= NULL
;
582 if(mode
!= STREAM_READ
|| !p
->filename
) {
583 m_struct_free(&stream_opts
,opts
);
584 return STREAM_UNSUPPORTED
;
586 filename
= strdup(p
->filename
);
588 m_struct_free(&stream_opts
,opts
);
589 return STREAM_UNSUPPORTED
;
591 colon
= strstr(filename
, ":");
594 track
= atoi(colon
+1);
600 f
= cue_read_cue(filename
);
602 m_struct_free(&stream_opts
,opts
);
603 return STREAM_UNSUPPORTED
;
606 ret
=cue_vcd_seek_to_track(stream
, track
);
608 mp_msg(MSGT_OPEN
,MSGL_ERR
,MSGTR_ErrTrackSelect
" (seek)\n");
609 return STREAM_UNSUPPORTED
;
611 mp_msg(MSGT_OPEN
,MSGL_INFO
,MSGTR_MPDEMUX_CUEREAD_CueStreamInfo_FilenameTrackTracksavail
,
612 filename
, track
, ret
, (int)stream
->end_pos
);
615 stream
->type
= STREAMTYPE_VCDBINCUE
;
616 stream
->sector_size
= VCD_SECTOR_DATA
;
617 stream
->flags
= STREAM_READ
| MP_STREAM_SEEK_FW
;
618 stream
->fill_buffer
= cue_vcd_read
;
620 stream
->control
= control
;
623 m_struct_free(&stream_opts
,opts
);
627 const stream_info_t stream_info_cue
= {
631 "based on the code from ???",
635 1 // Urls are an option string