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 static const 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 const struct m_struct_st stream_opts
= {
70 sizeof(struct cdda_params
),
75 /// We keep these options but now they set the defaults
76 const 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 int cdd_identify(const char *dev
);
92 int cddb_resolve(const char *dev
, char **xmcd_file
);
93 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 get_track_by_sector(cdda_priv
*p
, unsigned int sector
) {
101 for (i
= p
->cd
->tracks
; i
>= 0 ; --i
)
102 if (p
->cd
->disc_toc
[i
].dwStartSector
<= sector
)
107 static int control(stream_t
*stream
, int cmd
, void *arg
) {
108 cdda_priv
* p
= stream
->priv
;
110 case STREAM_CTRL_GET_NUM_CHAPTERS
:
112 int start_track
= get_track_by_sector(p
, p
->start_sector
);
113 int end_track
= get_track_by_sector(p
, p
->end_sector
);
114 *(unsigned int *)arg
= end_track
+ 1 - start_track
;
117 case STREAM_CTRL_SEEK_TO_CHAPTER
:
120 unsigned int track
= *(unsigned int *)arg
;
121 int start_track
= get_track_by_sector(p
, p
->start_sector
);
123 track
+= start_track
;
124 if (track
>= p
->cd
->tracks
) {
128 seek_sector
= track
<= 0 ? p
->start_sector
129 : p
->cd
->disc_toc
[track
].dwStartSector
;
130 r
= seek(stream
, seek_sector
* CD_FRAMESIZE_RAW
);
135 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
137 int start_track
= get_track_by_sector(p
, p
->start_sector
);
138 int cur_track
= get_track_by_sector(p
, p
->sector
);
139 *(unsigned int *)arg
= cur_track
- start_track
;
143 return STREAM_UNSUPPORTED
;
146 static int open_cdda(stream_t
*st
,int m
, void* opts
, int* file_format
) {
147 struct cdda_params
* p
= (struct cdda_params
*)opts
;
148 int mode
= p
->paranoia_mode
;
149 int offset
= p
->toc_offset
;
150 #ifndef CONFIG_LIBCDIO
151 cdrom_drive
* cdd
= NULL
;
153 cdrom_drive_t
* cdd
= NULL
;
156 cd_info_t
*cd_info
,*cddb_info
= NULL
;
157 unsigned int audiolen
=0;
160 char *xmcd_file
= NULL
;
162 if(m
!= STREAM_READ
) {
163 m_struct_free(&stream_opts
,opts
);
164 return STREAM_UNSUPPORTED
;
169 p
->device
= strdup(cdrom_device
);
171 p
->device
= strdup(DEFAULT_CDROM_DEVICE
);
175 // cdd_identify returns -1 if it cannot read the TOC,
176 // in which case there is no point in calling cddb_resolve
177 if(cdd_identify(p
->device
) >= 0 && strncmp(st
->url
,"cddb",4) == 0) {
178 i
= cddb_resolve(p
->device
, &xmcd_file
);
180 cddb_info
= cddb_parse_xmcd(xmcd_file
);
186 #ifndef CONFIG_LIBCDIO
188 cdd
= cdda_identify_scsi(p
->generic_dev
,p
->device
,0,NULL
);
191 #if defined(__NetBSD__)
192 cdd
= cdda_identify_scsi(p
->device
,p
->device
,0,NULL
);
194 cdd
= cdda_identify(p
->device
,0,NULL
);
198 mp_msg(MSGT_OPEN
,MSGL_ERR
,MSGTR_MPDEMUX_CDDA_CantOpenCDDADevice
);
199 m_struct_free(&stream_opts
,opts
);
204 cdda_verbose_set(cdd
, CDDA_MESSAGE_FORGETIT
, CDDA_MESSAGE_FORGETIT
);
207 cdd
->nsectors
= p
->sector_size
;
208 #ifndef CONFIG_LIBCDIO
209 cdd
->bigbuff
= p
->sector_size
* CD_FRAMESIZE_RAW
;
213 if(cdda_open(cdd
) != 0) {
214 mp_msg(MSGT_OPEN
,MSGL_ERR
,MSGTR_MPDEMUX_CDDA_CantOpenDisc
);
216 m_struct_free(&stream_opts
,opts
);
221 cd_info
= cd_info_new();
222 mp_msg(MSGT_OPEN
,MSGL_INFO
,MSGTR_MPDEMUX_CDDA_AudioCDFoundWithNTracks
,cdda_tracks(cdd
));
223 for(i
=0;i
<cdd
->tracks
;i
++) {
225 long sec
=cdda_track_firstsector(cdd
,i
+1);
226 long off
=cdda_track_lastsector(cdd
,i
+1)-sec
+1;
228 sprintf(track_name
, "Track %d", i
+1);
229 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
);
232 cd_info
->min
= (unsigned int)(audiolen
/(60*75));
233 cd_info
->sec
= (unsigned int)((audiolen
/75)%60);
234 cd_info
->msec
= (unsigned int)(audiolen
%75);
236 priv
= malloc(sizeof(cdda_priv
));
237 memset(priv
, 0, sizeof(cdda_priv
));
239 priv
->cd_info
= cd_info
;
242 offset
-= cdda_track_firstsector(cdd
,1);
246 for(i
= 0 ; i
< cdd
->tracks
+ 1 ; i
++)
247 cdd
->disc_toc
[i
].dwStartSector
+= offset
;
251 cdda_speed_set(cdd
,p
->speed
);
253 last_track
= cdda_tracks(cdd
);
254 if (p
->span
.start
> last_track
) p
->span
.start
= last_track
;
255 if (p
->span
.end
< p
->span
.start
) p
->span
.end
= p
->span
.start
;
256 if (p
->span
.end
> last_track
) p
->span
.end
= last_track
;
258 priv
->start_sector
= cdda_track_firstsector(cdd
,p
->span
.start
);
260 priv
->start_sector
= cdda_disc_firstsector(cdd
);
263 priv
->end_sector
= cdda_track_lastsector(cdd
,p
->span
.end
);
265 priv
->end_sector
= cdda_disc_lastsector(cdd
);
267 priv
->cdp
= paranoia_init(cdd
);
268 if(priv
->cdp
== NULL
) {
271 cd_info_free(cd_info
);
272 m_struct_free(&stream_opts
,opts
);
278 mode
= PARANOIA_MODE_DISABLE
;
280 mode
= PARANOIA_MODE_OVERLAP
;
282 mode
= PARANOIA_MODE_FULL
;
285 mode
|= PARANOIA_MODE_NEVERSKIP
;
286 #ifndef CONFIG_LIBCDIO
287 paranoia_modeset(cdd
, mode
);
289 if(p
->search_overlap
>= 0)
290 paranoia_overlapset(cdd
,p
->search_overlap
);
292 paranoia_modeset(priv
->cdp
, mode
);
294 if(p
->search_overlap
>= 0)
295 paranoia_overlapset(priv
->cdp
,p
->search_overlap
);
298 paranoia_seek(priv
->cdp
,priv
->start_sector
,SEEK_SET
);
299 priv
->sector
= priv
->start_sector
;
303 cd_info_free(cd_info
);
304 priv
->cd_info
= cddb_info
;
305 cd_info_debug( cddb_info
);
310 st
->start_pos
= priv
->start_sector
*CD_FRAMESIZE_RAW
;
311 st
->end_pos
= (priv
->end_sector
+ 1) * CD_FRAMESIZE_RAW
;
312 st
->type
= STREAMTYPE_CDDA
;
313 st
->sector_size
= CD_FRAMESIZE_RAW
;
315 st
->fill_buffer
= fill_buffer
;
317 st
->control
= control
;
318 st
->close
= close_cdda
;
320 *file_format
= DEMUXER_TYPE_RAWAUDIO
;
322 m_struct_free(&stream_opts
,opts
);
327 #ifndef CONFIG_LIBCDIO
328 static void cdparanoia_callback(long inpos
, int function
) {
330 static void cdparanoia_callback(long int inpos
, paranoia_cb_mode_t function
) {
334 static int fill_buffer(stream_t
* s
, char* buffer
, int max_len
) {
335 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
336 cd_track_t
*cd_track
;
340 if((p
->sector
< p
->start_sector
) || (p
->sector
> p
->end_sector
)) {
345 buf
= paranoia_read(p
->cdp
,cdparanoia_callback
);
350 for(i
=0;i
<CD_FRAMESIZE_RAW
/2;i
++)
351 buf
[i
]=le2me_16(buf
[i
]);
355 memcpy(buffer
,buf
,CD_FRAMESIZE_RAW
);
357 for(i
=0;i
<p
->cd
->tracks
;i
++){
358 if(p
->cd
->disc_toc
[i
].dwStartSector
==p
->sector
-1) {
359 cd_track
= cd_info_get_track(p
->cd_info
, i
+1);
360 //printf("Track %d, sector=%d\n", i, p->sector-1);
361 if( cd_track
!=NULL
) {
362 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
363 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
370 return CD_FRAMESIZE_RAW
;
373 static int seek(stream_t
* s
,off_t newpos
) {
374 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
375 cd_track_t
*cd_track
;
377 int current_track
=0, seeked_track
=0;
378 int seek_to_track
= 0;
382 sec
= s
->pos
/CD_FRAMESIZE_RAW
;
383 if (s
->pos
< 0 || sec
> p
->end_sector
) {
388 //printf("pos: %d, sec: %d ## %d\n", (int)s->pos, (int)sec, CD_FRAMESIZE_RAW);
389 //printf("sector: %d new: %d\n", p->sector, sec );
391 for(i
=0;i
<p
->cd
->tracks
;i
++){
392 // printf("trk #%d: %d .. %d\n",i,p->cd->disc_toc[i].dwStartSector,p->cd->disc_toc[i+1].dwStartSector);
393 if( p
->sector
>=p
->cd
->disc_toc
[i
].dwStartSector
&& p
->sector
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
396 if( sec
>=p
->cd
->disc_toc
[i
].dwStartSector
&& sec
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
398 seek_to_track
= sec
== p
->cd
->disc_toc
[i
].dwStartSector
;
401 //printf("current: %d, seeked: %d\n", current_track, seeked_track);
402 if (current_track
!= seeked_track
&& !seek_to_track
) {
403 //printf("Track %d, sector=%d\n", seeked_track, sec);
404 cd_track
= cd_info_get_track(p
->cd_info
, seeked_track
+1);
405 if( cd_track
!=NULL
) {
406 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
407 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
412 if(sec
< p
->start_sector
)
413 sec
= p
->start_sector
;
414 else if(sec
> p
->end_sector
)
419 // s->pos = sec*CD_FRAMESIZE_RAW;
421 //printf("seek: %d, sec: %d\n", (int)s->pos, sec);
422 paranoia_seek(p
->cdp
,sec
,SEEK_SET
);
426 static void close_cdda(stream_t
* s
) {
427 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
428 paranoia_free(p
->cdp
);
430 cd_info_free(p
->cd_info
);
434 const stream_info_t stream_info_cdda
= {
446 1 // Urls are an option string