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.
20 #ifndef CONFIG_LIBCDIO
21 #include <cdda_interface.h>
22 #include <cdda_paranoia.h>
24 #include <cdio/cdda.h>
25 #include <cdio/paranoia.h>
33 #include "libavutil/common.h"
35 #include "libmpdemux/demuxer.h"
41 #ifndef CD_FRAMESIZE_RAW
42 #define CD_FRAMESIZE_RAW CDIO_CD_FRAMESIZE_RAW
46 extern char *cdrom_device
;
49 #ifndef CONFIG_LIBCDIO
54 cdrom_paranoia_t
* cdp
;
62 static struct cdda_params
{
86 #define ST_OFF(f) M_ST_OFF(struct cdda_params,f)
87 static const m_option_t cdda_params_fields
[] = {
88 { "speed", ST_OFF(speed
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
89 { "paranoia", ST_OFF(paranoia_mode
), CONF_TYPE_INT
,M_OPT_RANGE
, 0, 2, NULL
},
90 { "generic-dev", ST_OFF(generic_dev
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
91 { "sector-size", ST_OFF(sector_size
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
92 { "overlap", ST_OFF(search_overlap
), CONF_TYPE_INT
, M_OPT_RANGE
,0,75, NULL
},
93 { "toc-bias", ST_OFF(toc_bias
), CONF_TYPE_INT
, 0, 0, 0, NULL
},
94 { "toc-offset", ST_OFF(toc_offset
), CONF_TYPE_INT
, 0, 0, 0, NULL
},
95 { "noskip", ST_OFF(no_skip
), CONF_TYPE_FLAG
, 0 , 0, 1, NULL
},
96 { "skip", ST_OFF(no_skip
), CONF_TYPE_FLAG
, 0 , 1, 0, NULL
},
97 { "device", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
98 { "span", ST_OFF(span
), CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
100 { "hostname", ST_OFF(span
), CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
101 { "port", ST_OFF(speed
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
102 { "filename", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
103 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
105 static const struct m_struct_st stream_opts
= {
107 sizeof(struct cdda_params
),
112 /// We keep these options but now they set the defaults
113 const m_option_t cdda_opts
[] = {
114 { "speed", &cdda_dflts
.speed
, CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
115 { "paranoia", &cdda_dflts
.paranoia_mode
, CONF_TYPE_INT
,M_OPT_RANGE
, 0, 2, NULL
},
116 { "generic-dev", &cdda_dflts
.generic_dev
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
117 { "sector-size", &cdda_dflts
.sector_size
, CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
118 { "overlap", &cdda_dflts
.search_overlap
, CONF_TYPE_INT
, M_OPT_RANGE
,0,75, NULL
},
119 { "toc-bias", &cdda_dflts
.toc_bias
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
120 { "toc-offset", &cdda_dflts
.toc_offset
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
121 { "noskip", &cdda_dflts
.no_skip
, CONF_TYPE_FLAG
, 0 , 0, 1, NULL
},
122 { "skip", &cdda_dflts
.no_skip
, CONF_TYPE_FLAG
, 0 , 1, 0, NULL
},
123 { "device", &cdda_dflts
.device
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
124 { "span", &cdda_dflts
.span
, CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, &m_span_params_def
},
125 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
128 #ifndef CONFIG_LIBCDIO
129 static void cdparanoia_callback(long inpos
, int function
) {
131 static void cdparanoia_callback(long int inpos
, paranoia_cb_mode_t function
) {
135 static int fill_buffer(stream_t
* s
, char* buffer
, int max_len
) {
136 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
137 cd_track_t
*cd_track
;
141 if((p
->sector
< p
->start_sector
) || (p
->sector
> p
->end_sector
)) {
146 buf
= paranoia_read(p
->cdp
,cdparanoia_callback
);
151 for(i
=0;i
<CD_FRAMESIZE_RAW
/2;i
++)
152 buf
[i
]=le2me_16(buf
[i
]);
156 memcpy(buffer
,buf
,CD_FRAMESIZE_RAW
);
158 for(i
=0;i
<p
->cd
->tracks
;i
++){
159 if(p
->cd
->disc_toc
[i
].dwStartSector
==p
->sector
-1) {
160 cd_track
= cd_info_get_track(p
->cd_info
, i
+1);
161 //printf("Track %d, sector=%d\n", i, p->sector-1);
162 if( cd_track
!=NULL
) {
163 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
164 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
171 return CD_FRAMESIZE_RAW
;
174 static int seek(stream_t
* s
,off_t newpos
) {
175 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
176 cd_track_t
*cd_track
;
178 int current_track
=0, seeked_track
=0;
179 int seek_to_track
= 0;
183 sec
= s
->pos
/CD_FRAMESIZE_RAW
;
184 if (s
->pos
< 0 || sec
> p
->end_sector
) {
189 //printf("pos: %d, sec: %d ## %d\n", (int)s->pos, (int)sec, CD_FRAMESIZE_RAW);
190 //printf("sector: %d new: %d\n", p->sector, sec );
192 for(i
=0;i
<p
->cd
->tracks
;i
++){
193 // printf("trk #%d: %d .. %d\n",i,p->cd->disc_toc[i].dwStartSector,p->cd->disc_toc[i+1].dwStartSector);
194 if( p
->sector
>=p
->cd
->disc_toc
[i
].dwStartSector
&& p
->sector
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
197 if( sec
>=p
->cd
->disc_toc
[i
].dwStartSector
&& sec
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
199 seek_to_track
= sec
== p
->cd
->disc_toc
[i
].dwStartSector
;
202 //printf("current: %d, seeked: %d\n", current_track, seeked_track);
203 if (current_track
!= seeked_track
&& !seek_to_track
) {
204 //printf("Track %d, sector=%d\n", seeked_track, sec);
205 cd_track
= cd_info_get_track(p
->cd_info
, seeked_track
+1);
206 if( cd_track
!=NULL
) {
207 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
208 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
213 if(sec
< p
->start_sector
)
214 sec
= p
->start_sector
;
215 else if(sec
> p
->end_sector
)
220 // s->pos = sec*CD_FRAMESIZE_RAW;
222 //printf("seek: %d, sec: %d\n", (int)s->pos, sec);
223 paranoia_seek(p
->cdp
,sec
,SEEK_SET
);
227 static void close_cdda(stream_t
* s
) {
228 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
229 paranoia_free(p
->cdp
);
231 cd_info_free(p
->cd_info
);
235 static int get_track_by_sector(cdda_priv
*p
, unsigned int sector
) {
237 for (i
= p
->cd
->tracks
; i
>= 0 ; --i
)
238 if (p
->cd
->disc_toc
[i
].dwStartSector
<= sector
)
243 static int control(stream_t
*stream
, int cmd
, void *arg
) {
244 cdda_priv
* p
= stream
->priv
;
246 case STREAM_CTRL_GET_NUM_CHAPTERS
:
248 int start_track
= get_track_by_sector(p
, p
->start_sector
);
249 int end_track
= get_track_by_sector(p
, p
->end_sector
);
250 *(unsigned int *)arg
= end_track
+ 1 - start_track
;
253 case STREAM_CTRL_SEEK_TO_CHAPTER
:
256 unsigned int track
= *(unsigned int *)arg
;
257 int start_track
= get_track_by_sector(p
, p
->start_sector
);
259 track
+= start_track
;
260 if (track
>= p
->cd
->tracks
) {
264 seek_sector
= track
<= 0 ? p
->start_sector
265 : p
->cd
->disc_toc
[track
].dwStartSector
;
266 r
= seek(stream
, seek_sector
* CD_FRAMESIZE_RAW
);
271 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
273 int start_track
= get_track_by_sector(p
, p
->start_sector
);
274 int cur_track
= get_track_by_sector(p
, p
->sector
);
275 *(unsigned int *)arg
= cur_track
- start_track
;
279 return STREAM_UNSUPPORTED
;
282 static int open_cdda(stream_t
*st
,int m
, void* opts
, int* file_format
) {
283 struct cdda_params
* p
= (struct cdda_params
*)opts
;
284 int mode
= p
->paranoia_mode
;
285 int offset
= p
->toc_offset
;
286 #ifndef CONFIG_LIBCDIO
287 cdrom_drive
* cdd
= NULL
;
289 cdrom_drive_t
* cdd
= NULL
;
292 cd_info_t
*cd_info
,*cddb_info
= NULL
;
293 unsigned int audiolen
=0;
296 char *xmcd_file
= NULL
;
298 if(m
!= STREAM_READ
) {
299 m_struct_free(&stream_opts
,opts
);
300 return STREAM_UNSUPPORTED
;
305 p
->device
= strdup(cdrom_device
);
307 p
->device
= strdup(DEFAULT_CDROM_DEVICE
);
311 // cdd_identify returns -1 if it cannot read the TOC,
312 // in which case there is no point in calling cddb_resolve
313 if(cdd_identify(p
->device
) >= 0 && strncmp(st
->url
,"cddb",4) == 0) {
314 i
= cddb_resolve(p
->device
, &xmcd_file
);
316 cddb_info
= cddb_parse_xmcd(xmcd_file
);
322 #ifndef CONFIG_LIBCDIO
324 cdd
= cdda_identify_scsi(p
->generic_dev
,p
->device
,0,NULL
);
327 #if defined(__NetBSD__)
328 cdd
= cdda_identify_scsi(p
->device
,p
->device
,0,NULL
);
330 cdd
= cdda_identify(p
->device
,0,NULL
);
334 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Can't open CDDA device.\n");
335 m_struct_free(&stream_opts
,opts
);
340 cdda_verbose_set(cdd
, CDDA_MESSAGE_FORGETIT
, CDDA_MESSAGE_FORGETIT
);
343 cdd
->nsectors
= p
->sector_size
;
344 #ifndef CONFIG_LIBCDIO
345 cdd
->bigbuff
= p
->sector_size
* CD_FRAMESIZE_RAW
;
349 if(cdda_open(cdd
) != 0) {
350 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Can't open disc.\n");
352 m_struct_free(&stream_opts
,opts
);
357 cd_info
= cd_info_new();
358 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,"Found audio CD with %d tracks.\n",cdda_tracks(cdd
));
359 for(i
=0;i
<cdd
->tracks
;i
++) {
361 long sec
=cdda_track_firstsector(cdd
,i
+1);
362 long off
=cdda_track_lastsector(cdd
,i
+1)-sec
+1;
364 sprintf(track_name
, "Track %d", i
+1);
365 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
);
368 cd_info
->min
= (unsigned int)(audiolen
/(60*75));
369 cd_info
->sec
= (unsigned int)((audiolen
/75)%60);
370 cd_info
->msec
= (unsigned int)(audiolen
%75);
372 priv
= malloc(sizeof(cdda_priv
));
373 memset(priv
, 0, sizeof(cdda_priv
));
375 priv
->cd_info
= cd_info
;
378 offset
-= cdda_track_firstsector(cdd
,1);
382 for(i
= 0 ; i
< cdd
->tracks
+ 1 ; i
++)
383 cdd
->disc_toc
[i
].dwStartSector
+= offset
;
387 cdda_speed_set(cdd
,p
->speed
);
389 last_track
= cdda_tracks(cdd
);
390 if (p
->span
.start
> last_track
) p
->span
.start
= last_track
;
391 if (p
->span
.end
< p
->span
.start
) p
->span
.end
= p
->span
.start
;
392 if (p
->span
.end
> last_track
) p
->span
.end
= last_track
;
394 priv
->start_sector
= cdda_track_firstsector(cdd
,p
->span
.start
);
396 priv
->start_sector
= cdda_disc_firstsector(cdd
);
399 priv
->end_sector
= cdda_track_lastsector(cdd
,p
->span
.end
);
401 priv
->end_sector
= cdda_disc_lastsector(cdd
);
403 priv
->cdp
= paranoia_init(cdd
);
404 if(priv
->cdp
== NULL
) {
407 cd_info_free(cd_info
);
408 m_struct_free(&stream_opts
,opts
);
414 mode
= PARANOIA_MODE_DISABLE
;
416 mode
= PARANOIA_MODE_OVERLAP
;
418 mode
= PARANOIA_MODE_FULL
;
421 mode
|= PARANOIA_MODE_NEVERSKIP
;
422 #ifndef CONFIG_LIBCDIO
423 paranoia_modeset(cdd
, mode
);
425 if(p
->search_overlap
>= 0)
426 paranoia_overlapset(cdd
,p
->search_overlap
);
428 paranoia_modeset(priv
->cdp
, mode
);
430 if(p
->search_overlap
>= 0)
431 paranoia_overlapset(priv
->cdp
,p
->search_overlap
);
434 paranoia_seek(priv
->cdp
,priv
->start_sector
,SEEK_SET
);
435 priv
->sector
= priv
->start_sector
;
439 cd_info_free(cd_info
);
440 priv
->cd_info
= cddb_info
;
441 cd_info_debug( cddb_info
);
446 st
->start_pos
= priv
->start_sector
*CD_FRAMESIZE_RAW
;
447 st
->end_pos
= (priv
->end_sector
+ 1) * CD_FRAMESIZE_RAW
;
448 st
->type
= STREAMTYPE_CDDA
;
449 st
->sector_size
= CD_FRAMESIZE_RAW
;
451 st
->fill_buffer
= fill_buffer
;
453 st
->control
= control
;
454 st
->close
= close_cdda
;
456 *file_format
= DEMUXER_TYPE_RAWAUDIO
;
458 m_struct_free(&stream_opts
,opts
);
463 const stream_info_t stream_info_cdda
= {
475 1 // Urls are an option string