10 #include "libmpdemux/demuxer.h"
17 #ifndef CD_FRAMESIZE_RAW
18 #define CD_FRAMESIZE_RAW CDIO_CD_FRAMESIZE_RAW
22 extern char *cdrom_device
;
24 static struct cdda_params
{
48 #define ST_OFF(f) M_ST_OFF(struct cdda_params,f)
49 m_option_t cdda_params_fields
[] = {
50 { "speed", ST_OFF(speed
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
51 { "paranoia", ST_OFF(paranoia_mode
), CONF_TYPE_INT
,M_OPT_RANGE
, 0, 2, NULL
},
52 { "generic-dev", ST_OFF(generic_dev
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
53 { "sector-size", ST_OFF(sector_size
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
54 { "overlap", ST_OFF(search_overlap
), CONF_TYPE_INT
, M_OPT_RANGE
,0,75, NULL
},
55 { "toc-bias", ST_OFF(toc_bias
), CONF_TYPE_INT
, 0, 0, 0, NULL
},
56 { "toc-offset", ST_OFF(toc_offset
), CONF_TYPE_INT
, 0, 0, 0, NULL
},
57 { "noskip", ST_OFF(no_skip
), CONF_TYPE_FLAG
, 0 , 0, 1, NULL
},
58 { "skip", ST_OFF(no_skip
), CONF_TYPE_FLAG
, 0 , 1, 0, NULL
},
59 { "device", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
60 { "span", ST_OFF(span
), CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
62 { "hostname", ST_OFF(span
), CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
63 { "port", ST_OFF(speed
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
64 { "filename", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
65 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
67 static struct m_struct_st stream_opts
= {
69 sizeof(struct cdda_params
),
74 /// We keep these options but now they set the defaults
75 m_option_t cdda_opts
[] = {
76 { "speed", &cdda_dflts
.speed
, CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
77 { "paranoia", &cdda_dflts
.paranoia_mode
, CONF_TYPE_INT
,M_OPT_RANGE
, 0, 2, NULL
},
78 { "generic-dev", &cdda_dflts
.generic_dev
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
79 { "sector-size", &cdda_dflts
.sector_size
, CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
80 { "overlap", &cdda_dflts
.search_overlap
, CONF_TYPE_INT
, M_OPT_RANGE
,0,75, NULL
},
81 { "toc-bias", &cdda_dflts
.toc_bias
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
82 { "toc-offset", &cdda_dflts
.toc_offset
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
83 { "noskip", &cdda_dflts
.no_skip
, CONF_TYPE_FLAG
, 0 , 0, 1, NULL
},
84 { "skip", &cdda_dflts
.no_skip
, CONF_TYPE_FLAG
, 0 , 1, 0, NULL
},
85 { "device", &cdda_dflts
.device
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
86 { "span", &cdda_dflts
.span
, CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
87 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
90 extern int cdd_identify(const char *dev
);
91 extern int cddb_resolve(const char *dev
, char **xmcd_file
);
92 extern cd_info_t
* cddb_parse_xmcd(char *xmcd_file
);
94 static int seek(stream_t
* s
,off_t pos
);
95 static int fill_buffer(stream_t
* s
, char* buffer
, int max_len
);
96 static void close_cdda(stream_t
* s
);
98 static int open_cdda(stream_t
*st
,int m
, void* opts
, int* file_format
) {
99 struct cdda_params
* p
= (struct cdda_params
*)opts
;
100 int mode
= p
->paranoia_mode
;
101 int offset
= p
->toc_offset
;
103 cdrom_drive
* cdd
= NULL
;
105 cdrom_drive_t
* cdd
= NULL
;
108 cd_info_t
*cd_info
,*cddb_info
= NULL
;
109 unsigned int audiolen
=0;
112 char *xmcd_file
= NULL
;
114 if(m
!= STREAM_READ
) {
115 m_struct_free(&stream_opts
,opts
);
116 return STREAM_UNSUPORTED
;
121 p
->device
= strdup(cdrom_device
);
123 p
->device
= strdup(DEFAULT_CDROM_DEVICE
);
126 #ifdef MPLAYER_NETWORK
127 // cdd_identify returns -1 if it cannot read the TOC,
128 // in which case there is no point in calling cddb_resolve
129 if(cdd_identify(p
->device
) >= 0 && strncmp(st
->url
,"cddb",4) == 0) {
130 i
= cddb_resolve(p
->device
, &xmcd_file
);
132 cddb_info
= cddb_parse_xmcd(xmcd_file
);
140 cdd
= cdda_identify_scsi(p
->generic_dev
,p
->device
,0,NULL
);
143 #if defined(__NetBSD__)
144 cdd
= cdda_identify_scsi(p
->device
,p
->device
,0,NULL
);
146 cdd
= cdda_identify(p
->device
,0,NULL
);
150 mp_msg(MSGT_OPEN
,MSGL_ERR
,MSGTR_MPDEMUX_CDDA_CantOpenCDDADevice
);
151 m_struct_free(&stream_opts
,opts
);
156 cdda_verbose_set(cdd
, CDDA_MESSAGE_FORGETIT
, CDDA_MESSAGE_FORGETIT
);
159 cdd
->nsectors
= p
->sector_size
;
161 cdd
->bigbuff
= p
->sector_size
* CD_FRAMESIZE_RAW
;
165 if(cdda_open(cdd
) != 0) {
166 mp_msg(MSGT_OPEN
,MSGL_ERR
,MSGTR_MPDEMUX_CDDA_CantOpenDisc
);
168 m_struct_free(&stream_opts
,opts
);
173 cd_info
= cd_info_new();
174 mp_msg(MSGT_OPEN
,MSGL_INFO
,MSGTR_MPDEMUX_CDDA_AudioCDFoundWithNTracks
,cdda_tracks(cdd
));
175 for(i
=0;i
<cdd
->tracks
;i
++) {
177 long sec
=cdda_track_firstsector(cdd
,i
+1);
178 long off
=cdda_track_lastsector(cdd
,i
+1)-sec
+1;
180 sprintf(track_name
, "Track %d", i
+1);
181 cd_info_add_track(cd_info
, track_name
, i
+1, (unsigned int)(off
/(60*75)), (unsigned int)((off
/75)%60), (unsigned int)(off
%75), sec
, off
);
184 cd_info
->min
= (unsigned int)(audiolen
/(60*75));
185 cd_info
->sec
= (unsigned int)((audiolen
/75)%60);
186 cd_info
->msec
= (unsigned int)(audiolen
%75);
188 priv
= malloc(sizeof(cdda_priv
));
189 memset(priv
, 0, sizeof(cdda_priv
));
191 priv
->cd_info
= cd_info
;
194 offset
-= cdda_track_firstsector(cdd
,1);
198 for(i
= 0 ; i
< cdd
->tracks
+ 1 ; i
++)
199 cdd
->disc_toc
[i
].dwStartSector
+= offset
;
203 cdda_speed_set(cdd
,p
->speed
);
205 last_track
= cdda_tracks(cdd
);
206 if (p
->span
.start
> last_track
) p
->span
.start
= last_track
;
207 if (p
->span
.end
< p
->span
.start
) p
->span
.end
= p
->span
.start
;
208 if (p
->span
.end
> last_track
) p
->span
.end
= last_track
;
210 priv
->start_sector
= cdda_track_firstsector(cdd
,p
->span
.start
);
212 priv
->start_sector
= cdda_disc_firstsector(cdd
);
215 priv
->end_sector
= cdda_track_lastsector(cdd
,p
->span
.end
);
217 priv
->end_sector
= cdda_disc_lastsector(cdd
);
219 priv
->cdp
= paranoia_init(cdd
);
220 if(priv
->cdp
== NULL
) {
223 cd_info_free(cd_info
);
224 m_struct_free(&stream_opts
,opts
);
230 mode
= PARANOIA_MODE_DISABLE
;
232 mode
= PARANOIA_MODE_OVERLAP
;
234 mode
= PARANOIA_MODE_FULL
;
237 mode
|= PARANOIA_MODE_NEVERSKIP
;
239 paranoia_modeset(cdd
, mode
);
241 if(p
->search_overlap
>= 0)
242 paranoia_overlapset(cdd
,p
->search_overlap
);
244 paranoia_modeset(priv
->cdp
, mode
);
246 if(p
->search_overlap
>= 0)
247 paranoia_overlapset(priv
->cdp
,p
->search_overlap
);
250 paranoia_seek(priv
->cdp
,priv
->start_sector
,SEEK_SET
);
251 priv
->sector
= priv
->start_sector
;
253 #ifdef MPLAYER_NETWORK
255 cd_info_free(cd_info
);
256 priv
->cd_info
= cddb_info
;
257 cd_info_debug( cddb_info
);
262 st
->start_pos
= priv
->start_sector
*CD_FRAMESIZE_RAW
;
263 st
->end_pos
= priv
->end_sector
*CD_FRAMESIZE_RAW
;
264 st
->type
= STREAMTYPE_CDDA
;
265 st
->sector_size
= CD_FRAMESIZE_RAW
;
267 st
->fill_buffer
= fill_buffer
;
269 st
->close
= close_cdda
;
271 *file_format
= DEMUXER_TYPE_RAWAUDIO
;
273 m_struct_free(&stream_opts
,opts
);
279 static void cdparanoia_callback(long inpos
, int function
) {
281 static void cdparanoia_callback(long int inpos
, paranoia_cb_mode_t function
) {
285 static int fill_buffer(stream_t
* s
, char* buffer
, int max_len
) {
286 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
287 cd_track_t
*cd_track
;
291 buf
= paranoia_read(p
->cdp
,cdparanoia_callback
);
295 #ifdef WORDS_BIGENDIAN
296 for(i
=0;i
<CD_FRAMESIZE_RAW
/2;i
++)
297 buf
[i
]=le2me_16(buf
[i
]);
301 s
->pos
= p
->sector
*CD_FRAMESIZE_RAW
;
302 memcpy(buffer
,buf
,CD_FRAMESIZE_RAW
);
304 if((p
->sector
< p
->start_sector
) || (p
->sector
>= p
->end_sector
)) {
309 for(i
=0;i
<p
->cd
->tracks
;i
++){
310 if(p
->cd
->disc_toc
[i
].dwStartSector
==p
->sector
-1) {
311 cd_track
= cd_info_get_track(p
->cd_info
, i
+1);
312 //printf("Track %d, sector=%d\n", i, p->sector-1);
313 if( cd_track
!=NULL
) {
314 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
315 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
322 return CD_FRAMESIZE_RAW
;
325 static int seek(stream_t
* s
,off_t newpos
) {
326 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
327 cd_track_t
*cd_track
;
329 int current_track
=0, seeked_track
=0;
338 sec
= s
->pos
/CD_FRAMESIZE_RAW
;
339 //printf("pos: %d, sec: %d ## %d\n", (int)s->pos, (int)sec, CD_FRAMESIZE_RAW);
340 //printf("sector: %d new: %d\n", p->sector, sec );
342 for(i
=0;i
<p
->cd
->tracks
;i
++){
343 // printf("trk #%d: %d .. %d\n",i,p->cd->disc_toc[i].dwStartSector,p->cd->disc_toc[i+1].dwStartSector);
344 if( p
->sector
>=p
->cd
->disc_toc
[i
].dwStartSector
&& p
->sector
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
347 if( sec
>=p
->cd
->disc_toc
[i
].dwStartSector
&& sec
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
351 //printf("current: %d, seeked: %d\n", current_track, seeked_track);
352 if( current_track
!=seeked_track
) {
353 //printf("Track %d, sector=%d\n", seeked_track, sec);
354 cd_track
= cd_info_get_track(p
->cd_info
, seeked_track
+1);
355 if( cd_track
!=NULL
) {
356 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
357 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
362 if(sec
< p
->start_sector
)
363 sec
= p
->start_sector
;
364 else if(sec
> p
->end_sector
)
369 // s->pos = sec*CD_FRAMESIZE_RAW;
371 //printf("seek: %d, sec: %d\n", (int)s->pos, sec);
372 paranoia_seek(p
->cdp
,sec
,SEEK_SET
);
376 static void close_cdda(stream_t
* s
) {
377 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
378 paranoia_free(p
->cdp
);
380 cd_info_free(p
->cd_info
);
384 stream_info_t stream_info_cdda
= {
390 { "cdda", "cddb", NULL
},
392 1 // Urls are an option string