9 #include "libavutil/common.h"
11 #include "libmpdemux/demuxer.h"
18 #ifndef CD_FRAMESIZE_RAW
19 #define CD_FRAMESIZE_RAW CDIO_CD_FRAMESIZE_RAW
23 extern char *cdrom_device
;
25 static struct cdda_params
{
49 #define ST_OFF(f) M_ST_OFF(struct cdda_params,f)
50 m_option_t cdda_params_fields
[] = {
51 { "speed", ST_OFF(speed
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
52 { "paranoia", ST_OFF(paranoia_mode
), CONF_TYPE_INT
,M_OPT_RANGE
, 0, 2, NULL
},
53 { "generic-dev", ST_OFF(generic_dev
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
54 { "sector-size", ST_OFF(sector_size
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
55 { "overlap", ST_OFF(search_overlap
), CONF_TYPE_INT
, M_OPT_RANGE
,0,75, NULL
},
56 { "toc-bias", ST_OFF(toc_bias
), CONF_TYPE_INT
, 0, 0, 0, NULL
},
57 { "toc-offset", ST_OFF(toc_offset
), CONF_TYPE_INT
, 0, 0, 0, NULL
},
58 { "noskip", ST_OFF(no_skip
), CONF_TYPE_FLAG
, 0 , 0, 1, NULL
},
59 { "skip", ST_OFF(no_skip
), CONF_TYPE_FLAG
, 0 , 1, 0, NULL
},
60 { "device", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
61 { "span", ST_OFF(span
), CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
63 { "hostname", ST_OFF(span
), CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
64 { "port", ST_OFF(speed
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
65 { "filename", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
66 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
68 static struct m_struct_st stream_opts
= {
70 sizeof(struct cdda_params
),
75 /// We keep these options but now they set the defaults
76 m_option_t cdda_opts
[] = {
77 { "speed", &cdda_dflts
.speed
, CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
78 { "paranoia", &cdda_dflts
.paranoia_mode
, CONF_TYPE_INT
,M_OPT_RANGE
, 0, 2, NULL
},
79 { "generic-dev", &cdda_dflts
.generic_dev
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
80 { "sector-size", &cdda_dflts
.sector_size
, CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
81 { "overlap", &cdda_dflts
.search_overlap
, CONF_TYPE_INT
, M_OPT_RANGE
,0,75, NULL
},
82 { "toc-bias", &cdda_dflts
.toc_bias
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
83 { "toc-offset", &cdda_dflts
.toc_offset
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
84 { "noskip", &cdda_dflts
.no_skip
, CONF_TYPE_FLAG
, 0 , 0, 1, NULL
},
85 { "skip", &cdda_dflts
.no_skip
, CONF_TYPE_FLAG
, 0 , 1, 0, NULL
},
86 { "device", &cdda_dflts
.device
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
87 { "span", &cdda_dflts
.span
, CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
88 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
91 extern int cdd_identify(const char *dev
);
92 extern int cddb_resolve(const char *dev
, char **xmcd_file
);
93 extern cd_info_t
* cddb_parse_xmcd(char *xmcd_file
);
95 static int seek(stream_t
* s
,off_t pos
);
96 static int fill_buffer(stream_t
* s
, char* buffer
, int max_len
);
97 static void close_cdda(stream_t
* s
);
99 static int open_cdda(stream_t
*st
,int m
, void* opts
, int* file_format
) {
100 struct cdda_params
* p
= (struct cdda_params
*)opts
;
101 int mode
= p
->paranoia_mode
;
102 int offset
= p
->toc_offset
;
104 cdrom_drive
* cdd
= NULL
;
106 cdrom_drive_t
* cdd
= NULL
;
109 cd_info_t
*cd_info
,*cddb_info
= NULL
;
110 unsigned int audiolen
=0;
113 char *xmcd_file
= NULL
;
115 if(m
!= STREAM_READ
) {
116 m_struct_free(&stream_opts
,opts
);
117 return STREAM_UNSUPORTED
;
122 p
->device
= strdup(cdrom_device
);
124 p
->device
= strdup(DEFAULT_CDROM_DEVICE
);
128 // cdd_identify returns -1 if it cannot read the TOC,
129 // in which case there is no point in calling cddb_resolve
130 if(cdd_identify(p
->device
) >= 0 && strncmp(st
->url
,"cddb",4) == 0) {
131 i
= cddb_resolve(p
->device
, &xmcd_file
);
133 cddb_info
= cddb_parse_xmcd(xmcd_file
);
141 cdd
= cdda_identify_scsi(p
->generic_dev
,p
->device
,0,NULL
);
144 #if defined(__NetBSD__)
145 cdd
= cdda_identify_scsi(p
->device
,p
->device
,0,NULL
);
147 cdd
= cdda_identify(p
->device
,0,NULL
);
151 mp_msg(MSGT_OPEN
,MSGL_ERR
,MSGTR_MPDEMUX_CDDA_CantOpenCDDADevice
);
152 m_struct_free(&stream_opts
,opts
);
157 cdda_verbose_set(cdd
, CDDA_MESSAGE_FORGETIT
, CDDA_MESSAGE_FORGETIT
);
160 cdd
->nsectors
= p
->sector_size
;
162 cdd
->bigbuff
= p
->sector_size
* CD_FRAMESIZE_RAW
;
166 if(cdda_open(cdd
) != 0) {
167 mp_msg(MSGT_OPEN
,MSGL_ERR
,MSGTR_MPDEMUX_CDDA_CantOpenDisc
);
169 m_struct_free(&stream_opts
,opts
);
174 cd_info
= cd_info_new();
175 mp_msg(MSGT_OPEN
,MSGL_INFO
,MSGTR_MPDEMUX_CDDA_AudioCDFoundWithNTracks
,cdda_tracks(cdd
));
176 for(i
=0;i
<cdd
->tracks
;i
++) {
178 long sec
=cdda_track_firstsector(cdd
,i
+1);
179 long off
=cdda_track_lastsector(cdd
,i
+1)-sec
+1;
181 sprintf(track_name
, "Track %d", i
+1);
182 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
);
185 cd_info
->min
= (unsigned int)(audiolen
/(60*75));
186 cd_info
->sec
= (unsigned int)((audiolen
/75)%60);
187 cd_info
->msec
= (unsigned int)(audiolen
%75);
189 priv
= malloc(sizeof(cdda_priv
));
190 memset(priv
, 0, sizeof(cdda_priv
));
192 priv
->cd_info
= cd_info
;
195 offset
-= cdda_track_firstsector(cdd
,1);
199 for(i
= 0 ; i
< cdd
->tracks
+ 1 ; i
++)
200 cdd
->disc_toc
[i
].dwStartSector
+= offset
;
204 cdda_speed_set(cdd
,p
->speed
);
206 last_track
= cdda_tracks(cdd
);
207 if (p
->span
.start
> last_track
) p
->span
.start
= last_track
;
208 if (p
->span
.end
< p
->span
.start
) p
->span
.end
= p
->span
.start
;
209 if (p
->span
.end
> last_track
) p
->span
.end
= last_track
;
211 priv
->start_sector
= cdda_track_firstsector(cdd
,p
->span
.start
);
213 priv
->start_sector
= cdda_disc_firstsector(cdd
);
216 priv
->end_sector
= cdda_track_lastsector(cdd
,p
->span
.end
);
218 priv
->end_sector
= cdda_disc_lastsector(cdd
);
220 priv
->cdp
= paranoia_init(cdd
);
221 if(priv
->cdp
== NULL
) {
224 cd_info_free(cd_info
);
225 m_struct_free(&stream_opts
,opts
);
231 mode
= PARANOIA_MODE_DISABLE
;
233 mode
= PARANOIA_MODE_OVERLAP
;
235 mode
= PARANOIA_MODE_FULL
;
238 mode
|= PARANOIA_MODE_NEVERSKIP
;
240 paranoia_modeset(cdd
, mode
);
242 if(p
->search_overlap
>= 0)
243 paranoia_overlapset(cdd
,p
->search_overlap
);
245 paranoia_modeset(priv
->cdp
, mode
);
247 if(p
->search_overlap
>= 0)
248 paranoia_overlapset(priv
->cdp
,p
->search_overlap
);
251 paranoia_seek(priv
->cdp
,priv
->start_sector
,SEEK_SET
);
252 priv
->sector
= priv
->start_sector
;
256 cd_info_free(cd_info
);
257 priv
->cd_info
= cddb_info
;
258 cd_info_debug( cddb_info
);
263 st
->start_pos
= priv
->start_sector
*CD_FRAMESIZE_RAW
;
264 st
->end_pos
= priv
->end_sector
*CD_FRAMESIZE_RAW
;
265 st
->type
= STREAMTYPE_CDDA
;
266 st
->sector_size
= CD_FRAMESIZE_RAW
;
268 st
->fill_buffer
= fill_buffer
;
270 st
->close
= close_cdda
;
272 *file_format
= DEMUXER_TYPE_RAWAUDIO
;
274 m_struct_free(&stream_opts
,opts
);
280 static void cdparanoia_callback(long inpos
, int function
) {
282 static void cdparanoia_callback(long int inpos
, paranoia_cb_mode_t function
) {
286 static int fill_buffer(stream_t
* s
, char* buffer
, int max_len
) {
287 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
288 cd_track_t
*cd_track
;
292 buf
= paranoia_read(p
->cdp
,cdparanoia_callback
);
296 #ifdef WORDS_BIGENDIAN
297 for(i
=0;i
<CD_FRAMESIZE_RAW
/2;i
++)
298 buf
[i
]=le2me_16(buf
[i
]);
302 s
->pos
= p
->sector
*CD_FRAMESIZE_RAW
;
303 memcpy(buffer
,buf
,CD_FRAMESIZE_RAW
);
305 if((p
->sector
< p
->start_sector
) || (p
->sector
>= p
->end_sector
)) {
310 for(i
=0;i
<p
->cd
->tracks
;i
++){
311 if(p
->cd
->disc_toc
[i
].dwStartSector
==p
->sector
-1) {
312 cd_track
= cd_info_get_track(p
->cd_info
, i
+1);
313 //printf("Track %d, sector=%d\n", i, p->sector-1);
314 if( cd_track
!=NULL
) {
315 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
316 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
323 return CD_FRAMESIZE_RAW
;
326 static int seek(stream_t
* s
,off_t newpos
) {
327 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
328 cd_track_t
*cd_track
;
330 int current_track
=0, seeked_track
=0;
339 sec
= s
->pos
/CD_FRAMESIZE_RAW
;
340 //printf("pos: %d, sec: %d ## %d\n", (int)s->pos, (int)sec, CD_FRAMESIZE_RAW);
341 //printf("sector: %d new: %d\n", p->sector, sec );
343 for(i
=0;i
<p
->cd
->tracks
;i
++){
344 // printf("trk #%d: %d .. %d\n",i,p->cd->disc_toc[i].dwStartSector,p->cd->disc_toc[i+1].dwStartSector);
345 if( p
->sector
>=p
->cd
->disc_toc
[i
].dwStartSector
&& p
->sector
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
348 if( sec
>=p
->cd
->disc_toc
[i
].dwStartSector
&& sec
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
352 //printf("current: %d, seeked: %d\n", current_track, seeked_track);
353 if( current_track
!=seeked_track
) {
354 //printf("Track %d, sector=%d\n", seeked_track, sec);
355 cd_track
= cd_info_get_track(p
->cd_info
, seeked_track
+1);
356 if( cd_track
!=NULL
) {
357 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
358 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
363 if(sec
< p
->start_sector
)
364 sec
= p
->start_sector
;
365 else if(sec
> p
->end_sector
)
370 // s->pos = sec*CD_FRAMESIZE_RAW;
372 //printf("seek: %d, sec: %d\n", (int)s->pos, sec);
373 paranoia_seek(p
->cdp
,sec
,SEEK_SET
);
377 static void close_cdda(stream_t
* s
) {
378 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
379 paranoia_free(p
->cdp
);
381 cd_info_free(p
->cd_info
);
385 stream_info_t stream_info_cdda
= {
397 1 // Urls are an option string