2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "libavutil/common.h"
29 #include "libmpdemux/demuxer.h"
36 #ifndef CD_FRAMESIZE_RAW
37 #define CD_FRAMESIZE_RAW CDIO_CD_FRAMESIZE_RAW
41 extern char *cdrom_device
;
43 static struct cdda_params
{
67 #define ST_OFF(f) M_ST_OFF(struct cdda_params,f)
68 static const m_option_t cdda_params_fields
[] = {
69 { "speed", ST_OFF(speed
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
70 { "paranoia", ST_OFF(paranoia_mode
), CONF_TYPE_INT
,M_OPT_RANGE
, 0, 2, NULL
},
71 { "generic-dev", ST_OFF(generic_dev
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
72 { "sector-size", ST_OFF(sector_size
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
73 { "overlap", ST_OFF(search_overlap
), CONF_TYPE_INT
, M_OPT_RANGE
,0,75, NULL
},
74 { "toc-bias", ST_OFF(toc_bias
), CONF_TYPE_INT
, 0, 0, 0, NULL
},
75 { "toc-offset", ST_OFF(toc_offset
), CONF_TYPE_INT
, 0, 0, 0, NULL
},
76 { "noskip", ST_OFF(no_skip
), CONF_TYPE_FLAG
, 0 , 0, 1, NULL
},
77 { "skip", ST_OFF(no_skip
), CONF_TYPE_FLAG
, 0 , 1, 0, NULL
},
78 { "device", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
79 { "span", ST_OFF(span
), CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
81 { "hostname", ST_OFF(span
), CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
82 { "port", ST_OFF(speed
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
83 { "filename", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
84 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
86 static const struct m_struct_st stream_opts
= {
88 sizeof(struct cdda_params
),
93 /// We keep these options but now they set the defaults
94 const m_option_t cdda_opts
[] = {
95 { "speed", &cdda_dflts
.speed
, CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
96 { "paranoia", &cdda_dflts
.paranoia_mode
, CONF_TYPE_INT
,M_OPT_RANGE
, 0, 2, NULL
},
97 { "generic-dev", &cdda_dflts
.generic_dev
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
98 { "sector-size", &cdda_dflts
.sector_size
, CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
99 { "overlap", &cdda_dflts
.search_overlap
, CONF_TYPE_INT
, M_OPT_RANGE
,0,75, NULL
},
100 { "toc-bias", &cdda_dflts
.toc_bias
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
101 { "toc-offset", &cdda_dflts
.toc_offset
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
102 { "noskip", &cdda_dflts
.no_skip
, CONF_TYPE_FLAG
, 0 , 0, 1, NULL
},
103 { "skip", &cdda_dflts
.no_skip
, CONF_TYPE_FLAG
, 0 , 1, 0, NULL
},
104 { "device", &cdda_dflts
.device
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
105 { "span", &cdda_dflts
.span
, CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
106 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
109 int cdd_identify(const char *dev
);
110 int cddb_resolve(const char *dev
, char **xmcd_file
);
111 cd_info_t
* cddb_parse_xmcd(char *xmcd_file
);
113 static int seek(stream_t
* s
,off_t pos
);
114 static int fill_buffer(stream_t
* s
, char* buffer
, int max_len
);
115 static void close_cdda(stream_t
* s
);
117 static int get_track_by_sector(cdda_priv
*p
, unsigned int sector
) {
119 for (i
= p
->cd
->tracks
; i
>= 0 ; --i
)
120 if (p
->cd
->disc_toc
[i
].dwStartSector
<= sector
)
125 static int control(stream_t
*stream
, int cmd
, void *arg
) {
126 cdda_priv
* p
= stream
->priv
;
128 case STREAM_CTRL_GET_NUM_CHAPTERS
:
130 int start_track
= get_track_by_sector(p
, p
->start_sector
);
131 int end_track
= get_track_by_sector(p
, p
->end_sector
);
132 *(unsigned int *)arg
= end_track
+ 1 - start_track
;
135 case STREAM_CTRL_SEEK_TO_CHAPTER
:
138 unsigned int track
= *(unsigned int *)arg
;
139 int start_track
= get_track_by_sector(p
, p
->start_sector
);
141 track
+= start_track
;
142 if (track
>= p
->cd
->tracks
) {
146 seek_sector
= track
<= 0 ? p
->start_sector
147 : p
->cd
->disc_toc
[track
].dwStartSector
;
148 r
= seek(stream
, seek_sector
* CD_FRAMESIZE_RAW
);
153 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
155 int start_track
= get_track_by_sector(p
, p
->start_sector
);
156 int cur_track
= get_track_by_sector(p
, p
->sector
);
157 *(unsigned int *)arg
= cur_track
- start_track
;
161 return STREAM_UNSUPPORTED
;
164 static int open_cdda(stream_t
*st
,int m
, void* opts
, int* file_format
) {
165 struct cdda_params
* p
= (struct cdda_params
*)opts
;
166 int mode
= p
->paranoia_mode
;
167 int offset
= p
->toc_offset
;
168 #ifndef CONFIG_LIBCDIO
169 cdrom_drive
* cdd
= NULL
;
171 cdrom_drive_t
* cdd
= NULL
;
174 cd_info_t
*cd_info
,*cddb_info
= NULL
;
175 unsigned int audiolen
=0;
178 char *xmcd_file
= NULL
;
180 if(m
!= STREAM_READ
) {
181 m_struct_free(&stream_opts
,opts
);
182 return STREAM_UNSUPPORTED
;
187 p
->device
= strdup(cdrom_device
);
189 p
->device
= strdup(DEFAULT_CDROM_DEVICE
);
193 // cdd_identify returns -1 if it cannot read the TOC,
194 // in which case there is no point in calling cddb_resolve
195 if(cdd_identify(p
->device
) >= 0 && strncmp(st
->url
,"cddb",4) == 0) {
196 i
= cddb_resolve(p
->device
, &xmcd_file
);
198 cddb_info
= cddb_parse_xmcd(xmcd_file
);
204 #ifndef CONFIG_LIBCDIO
206 cdd
= cdda_identify_scsi(p
->generic_dev
,p
->device
,0,NULL
);
209 #if defined(__NetBSD__)
210 cdd
= cdda_identify_scsi(p
->device
,p
->device
,0,NULL
);
212 cdd
= cdda_identify(p
->device
,0,NULL
);
216 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Can't open CDDA device.\n");
217 m_struct_free(&stream_opts
,opts
);
222 cdda_verbose_set(cdd
, CDDA_MESSAGE_FORGETIT
, CDDA_MESSAGE_FORGETIT
);
225 cdd
->nsectors
= p
->sector_size
;
226 #ifndef CONFIG_LIBCDIO
227 cdd
->bigbuff
= p
->sector_size
* CD_FRAMESIZE_RAW
;
231 if(cdda_open(cdd
) != 0) {
232 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Can't open disc.\n");
234 m_struct_free(&stream_opts
,opts
);
239 cd_info
= cd_info_new();
240 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,"Found audio CD with %ld tracks.\n",cdda_tracks(cdd
));
241 for(i
=0;i
<cdd
->tracks
;i
++) {
243 long sec
=cdda_track_firstsector(cdd
,i
+1);
244 long off
=cdda_track_lastsector(cdd
,i
+1)-sec
+1;
246 sprintf(track_name
, "Track %d", i
+1);
247 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
);
250 cd_info
->min
= (unsigned int)(audiolen
/(60*75));
251 cd_info
->sec
= (unsigned int)((audiolen
/75)%60);
252 cd_info
->msec
= (unsigned int)(audiolen
%75);
254 priv
= malloc(sizeof(cdda_priv
));
255 memset(priv
, 0, sizeof(cdda_priv
));
257 priv
->cd_info
= cd_info
;
260 offset
-= cdda_track_firstsector(cdd
,1);
264 for(i
= 0 ; i
< cdd
->tracks
+ 1 ; i
++)
265 cdd
->disc_toc
[i
].dwStartSector
+= offset
;
269 cdda_speed_set(cdd
,p
->speed
);
271 last_track
= cdda_tracks(cdd
);
272 if (p
->span
.start
> last_track
) p
->span
.start
= last_track
;
273 if (p
->span
.end
< p
->span
.start
) p
->span
.end
= p
->span
.start
;
274 if (p
->span
.end
> last_track
) p
->span
.end
= last_track
;
276 priv
->start_sector
= cdda_track_firstsector(cdd
,p
->span
.start
);
278 priv
->start_sector
= cdda_disc_firstsector(cdd
);
281 priv
->end_sector
= cdda_track_lastsector(cdd
,p
->span
.end
);
283 priv
->end_sector
= cdda_disc_lastsector(cdd
);
285 priv
->cdp
= paranoia_init(cdd
);
286 if(priv
->cdp
== NULL
) {
289 cd_info_free(cd_info
);
290 m_struct_free(&stream_opts
,opts
);
296 mode
= PARANOIA_MODE_DISABLE
;
298 mode
= PARANOIA_MODE_OVERLAP
;
300 mode
= PARANOIA_MODE_FULL
;
303 mode
|= PARANOIA_MODE_NEVERSKIP
;
304 #ifndef CONFIG_LIBCDIO
305 paranoia_modeset(cdd
, mode
);
307 if(p
->search_overlap
>= 0)
308 paranoia_overlapset(cdd
,p
->search_overlap
);
310 paranoia_modeset(priv
->cdp
, mode
);
312 if(p
->search_overlap
>= 0)
313 paranoia_overlapset(priv
->cdp
,p
->search_overlap
);
316 paranoia_seek(priv
->cdp
,priv
->start_sector
,SEEK_SET
);
317 priv
->sector
= priv
->start_sector
;
321 cd_info_free(cd_info
);
322 priv
->cd_info
= cddb_info
;
323 cd_info_debug( cddb_info
);
328 st
->start_pos
= priv
->start_sector
*CD_FRAMESIZE_RAW
;
329 st
->end_pos
= (priv
->end_sector
+ 1) * CD_FRAMESIZE_RAW
;
330 st
->type
= STREAMTYPE_CDDA
;
331 st
->sector_size
= CD_FRAMESIZE_RAW
;
333 st
->fill_buffer
= fill_buffer
;
335 st
->control
= control
;
336 st
->close
= close_cdda
;
338 *file_format
= DEMUXER_TYPE_RAWAUDIO
;
340 m_struct_free(&stream_opts
,opts
);
345 #ifndef CONFIG_LIBCDIO
346 static void cdparanoia_callback(long inpos
, int function
) {
348 static void cdparanoia_callback(long int inpos
, paranoia_cb_mode_t function
) {
352 static int fill_buffer(stream_t
* s
, char* buffer
, int max_len
) {
353 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
354 cd_track_t
*cd_track
;
358 if((p
->sector
< p
->start_sector
) || (p
->sector
> p
->end_sector
)) {
363 buf
= paranoia_read(p
->cdp
,cdparanoia_callback
);
368 for(i
=0;i
<CD_FRAMESIZE_RAW
/2;i
++)
369 buf
[i
]=le2me_16(buf
[i
]);
373 memcpy(buffer
,buf
,CD_FRAMESIZE_RAW
);
375 for(i
=0;i
<p
->cd
->tracks
;i
++){
376 if(p
->cd
->disc_toc
[i
].dwStartSector
==p
->sector
-1) {
377 cd_track
= cd_info_get_track(p
->cd_info
, i
+1);
378 //printf("Track %d, sector=%d\n", i, p->sector-1);
379 if( cd_track
!=NULL
) {
380 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
381 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
388 return CD_FRAMESIZE_RAW
;
391 static int seek(stream_t
* s
,off_t newpos
) {
392 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
393 cd_track_t
*cd_track
;
395 int current_track
=0, seeked_track
=0;
396 int seek_to_track
= 0;
400 sec
= s
->pos
/CD_FRAMESIZE_RAW
;
401 if (s
->pos
< 0 || sec
> p
->end_sector
) {
406 //printf("pos: %d, sec: %d ## %d\n", (int)s->pos, (int)sec, CD_FRAMESIZE_RAW);
407 //printf("sector: %d new: %d\n", p->sector, sec );
409 for(i
=0;i
<p
->cd
->tracks
;i
++){
410 // printf("trk #%d: %d .. %d\n",i,p->cd->disc_toc[i].dwStartSector,p->cd->disc_toc[i+1].dwStartSector);
411 if( p
->sector
>=p
->cd
->disc_toc
[i
].dwStartSector
&& p
->sector
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
414 if( sec
>=p
->cd
->disc_toc
[i
].dwStartSector
&& sec
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
416 seek_to_track
= sec
== p
->cd
->disc_toc
[i
].dwStartSector
;
419 //printf("current: %d, seeked: %d\n", current_track, seeked_track);
420 if (current_track
!= seeked_track
&& !seek_to_track
) {
421 //printf("Track %d, sector=%d\n", seeked_track, sec);
422 cd_track
= cd_info_get_track(p
->cd_info
, seeked_track
+1);
423 if( cd_track
!=NULL
) {
424 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
425 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
430 if(sec
< p
->start_sector
)
431 sec
= p
->start_sector
;
432 else if(sec
> p
->end_sector
)
437 // s->pos = sec*CD_FRAMESIZE_RAW;
439 //printf("seek: %d, sec: %d\n", (int)s->pos, sec);
440 paranoia_seek(p
->cdp
,sec
,SEEK_SET
);
444 static void close_cdda(stream_t
* s
) {
445 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
446 paranoia_free(p
->cdp
);
448 cd_info_free(p
->cd_info
);
452 const stream_info_t stream_info_cdda
= {
464 1 // Urls are an option string