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>
35 #include "libavutil/common.h"
37 #include "libmpdemux/demuxer.h"
43 #ifndef CD_FRAMESIZE_RAW
44 #define CD_FRAMESIZE_RAW CDIO_CD_FRAMESIZE_RAW
48 extern char *cdrom_device
;
51 #ifndef CONFIG_LIBCDIO
56 cdrom_paranoia_t
* cdp
;
64 static struct cdda_params
{
88 #define ST_OFF(f) M_ST_OFF(struct cdda_params,f)
89 static const m_option_t cdda_params_fields
[] = {
90 { "speed", ST_OFF(speed
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
91 { "paranoia", ST_OFF(paranoia_mode
), CONF_TYPE_INT
,M_OPT_RANGE
, 0, 2, NULL
},
92 { "generic-dev", ST_OFF(generic_dev
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
93 { "sector-size", ST_OFF(sector_size
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
94 { "overlap", ST_OFF(search_overlap
), CONF_TYPE_INT
, M_OPT_RANGE
,0,75, NULL
},
95 { "toc-bias", ST_OFF(toc_bias
), CONF_TYPE_INT
, 0, 0, 0, NULL
},
96 { "toc-offset", ST_OFF(toc_offset
), CONF_TYPE_INT
, 0, 0, 0, NULL
},
97 { "noskip", ST_OFF(no_skip
), CONF_TYPE_FLAG
, 0 , 0, 1, NULL
},
98 { "skip", ST_OFF(no_skip
), CONF_TYPE_FLAG
, 0 , 1, 0, NULL
},
99 { "device", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
100 { "span", ST_OFF(span
), CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, (void *)&m_span_params_def
},
102 { "hostname", ST_OFF(span
), CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, (void *)&m_span_params_def
},
103 { "port", ST_OFF(speed
), CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
104 { "filename", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0, 0, NULL
},
105 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
107 static const struct m_struct_st stream_opts
= {
109 sizeof(struct cdda_params
),
114 /// We keep these options but now they set the defaults
115 const m_option_t cdda_opts
[] = {
116 { "speed", &cdda_dflts
.speed
, CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
117 { "paranoia", &cdda_dflts
.paranoia_mode
, CONF_TYPE_INT
,M_OPT_RANGE
, 0, 2, NULL
},
118 { "generic-dev", &cdda_dflts
.generic_dev
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
119 { "sector-size", &cdda_dflts
.sector_size
, CONF_TYPE_INT
, M_OPT_RANGE
,1,100, NULL
},
120 { "overlap", &cdda_dflts
.search_overlap
, CONF_TYPE_INT
, M_OPT_RANGE
,0,75, NULL
},
121 { "toc-bias", &cdda_dflts
.toc_bias
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
122 { "toc-offset", &cdda_dflts
.toc_offset
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
123 { "noskip", &cdda_dflts
.no_skip
, CONF_TYPE_FLAG
, 0 , 0, 1, NULL
},
124 { "skip", &cdda_dflts
.no_skip
, CONF_TYPE_FLAG
, 0 , 1, 0, NULL
},
125 { "device", &cdda_dflts
.device
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
126 { "span", &cdda_dflts
.span
, CONF_TYPE_OBJ_PARAMS
, 0, 0, 0, (void *)&m_span_params_def
},
127 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
130 #ifndef CONFIG_LIBCDIO
131 static void cdparanoia_callback(long inpos
, int function
) {
133 static void cdparanoia_callback(long int inpos
, paranoia_cb_mode_t function
) {
137 static int fill_buffer(stream_t
* s
, char* buffer
, int max_len
) {
138 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
139 cd_track_t
*cd_track
;
143 if((p
->sector
< p
->start_sector
) || (p
->sector
> p
->end_sector
)) {
148 buf
= paranoia_read(p
->cdp
,cdparanoia_callback
);
153 for(i
=0;i
<CD_FRAMESIZE_RAW
/2;i
++)
154 buf
[i
]=le2me_16(buf
[i
]);
158 memcpy(buffer
,buf
,CD_FRAMESIZE_RAW
);
160 for(i
=0;i
<p
->cd
->tracks
;i
++){
161 if(p
->cd
->disc_toc
[i
].dwStartSector
==p
->sector
-1) {
162 cd_track
= cd_info_get_track(p
->cd_info
, i
+1);
163 //printf("Track %d, sector=%d\n", i, p->sector-1);
164 if( cd_track
!=NULL
) {
165 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
166 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
173 return CD_FRAMESIZE_RAW
;
176 static int seek(stream_t
* s
,off_t newpos
) {
177 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
178 cd_track_t
*cd_track
;
180 int current_track
=0, seeked_track
=0;
181 int seek_to_track
= 0;
185 sec
= s
->pos
/CD_FRAMESIZE_RAW
;
186 if (s
->pos
< 0 || sec
> p
->end_sector
) {
191 //printf("pos: %d, sec: %d ## %d\n", (int)s->pos, (int)sec, CD_FRAMESIZE_RAW);
192 //printf("sector: %d new: %d\n", p->sector, sec );
194 for(i
=0;i
<p
->cd
->tracks
;i
++){
195 // printf("trk #%d: %d .. %d\n",i,p->cd->disc_toc[i].dwStartSector,p->cd->disc_toc[i+1].dwStartSector);
196 if( p
->sector
>=p
->cd
->disc_toc
[i
].dwStartSector
&& p
->sector
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
199 if( sec
>=p
->cd
->disc_toc
[i
].dwStartSector
&& sec
<p
->cd
->disc_toc
[i
+1].dwStartSector
) {
201 seek_to_track
= sec
== p
->cd
->disc_toc
[i
].dwStartSector
;
204 //printf("current: %d, seeked: %d\n", current_track, seeked_track);
205 if (current_track
!= seeked_track
&& !seek_to_track
) {
206 //printf("Track %d, sector=%d\n", seeked_track, sec);
207 cd_track
= cd_info_get_track(p
->cd_info
, seeked_track
+1);
208 if( cd_track
!=NULL
) {
209 mp_msg(MSGT_SEEK
, MSGL_INFO
, "\n%s\n", cd_track
->name
);
210 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK=%d\n", cd_track
->track_nb
);
215 if(sec
< p
->start_sector
)
216 sec
= p
->start_sector
;
217 else if(sec
> p
->end_sector
)
222 // s->pos = sec*CD_FRAMESIZE_RAW;
224 //printf("seek: %d, sec: %d\n", (int)s->pos, sec);
225 paranoia_seek(p
->cdp
,sec
,SEEK_SET
);
229 static void close_cdda(stream_t
* s
) {
230 cdda_priv
* p
= (cdda_priv
*)s
->priv
;
231 paranoia_free(p
->cdp
);
233 cd_info_free(p
->cd_info
);
237 static int get_track_by_sector(cdda_priv
*p
, unsigned int sector
) {
239 for (i
= p
->cd
->tracks
; i
>= 0 ; --i
)
240 if (p
->cd
->disc_toc
[i
].dwStartSector
<= sector
)
245 static int control(stream_t
*stream
, int cmd
, void *arg
) {
246 cdda_priv
* p
= stream
->priv
;
248 case STREAM_CTRL_GET_NUM_CHAPTERS
:
250 int start_track
= get_track_by_sector(p
, p
->start_sector
);
251 int end_track
= get_track_by_sector(p
, p
->end_sector
);
252 *(unsigned int *)arg
= end_track
+ 1 - start_track
;
255 case STREAM_CTRL_SEEK_TO_CHAPTER
:
258 unsigned int track
= *(unsigned int *)arg
;
259 int start_track
= get_track_by_sector(p
, p
->start_sector
);
261 track
+= start_track
;
262 if (track
>= p
->cd
->tracks
) {
266 seek_sector
= track
<= 0 ? p
->start_sector
267 : p
->cd
->disc_toc
[track
].dwStartSector
;
268 r
= seek(stream
, seek_sector
* CD_FRAMESIZE_RAW
);
273 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
275 int start_track
= get_track_by_sector(p
, p
->start_sector
);
276 int cur_track
= get_track_by_sector(p
, p
->sector
);
277 *(unsigned int *)arg
= cur_track
- start_track
;
281 return STREAM_UNSUPPORTED
;
284 static int open_cdda(stream_t
*st
,int m
, void* opts
, int* file_format
) {
285 struct cdda_params
* p
= (struct cdda_params
*)opts
;
286 int mode
= p
->paranoia_mode
;
287 int offset
= p
->toc_offset
;
288 #ifndef CONFIG_LIBCDIO
289 cdrom_drive
* cdd
= NULL
;
291 cdrom_drive_t
* cdd
= NULL
;
294 cd_info_t
*cd_info
,*cddb_info
= NULL
;
295 unsigned int audiolen
=0;
298 char *xmcd_file
= NULL
;
300 if(m
!= STREAM_READ
) {
301 m_struct_free(&stream_opts
,opts
);
302 return STREAM_UNSUPPORTED
;
307 p
->device
= talloc_strdup(NULL
, cdrom_device
);
309 p
->device
= talloc_strdup(NULL
, DEFAULT_CDROM_DEVICE
);
313 // cdd_identify returns -1 if it cannot read the TOC,
314 // in which case there is no point in calling cddb_resolve
315 if(cdd_identify(p
->device
) >= 0 && strncmp(st
->url
,"cddb",4) == 0) {
316 i
= cddb_resolve(p
->device
, &xmcd_file
);
318 cddb_info
= cddb_parse_xmcd(xmcd_file
);
324 #ifndef CONFIG_LIBCDIO
326 cdd
= cdda_identify_scsi(p
->generic_dev
,p
->device
,0,NULL
);
329 #if defined(__NetBSD__)
330 cdd
= cdda_identify_scsi(p
->device
,p
->device
,0,NULL
);
332 cdd
= cdda_identify(p
->device
,0,NULL
);
336 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Can't open CDDA device.\n");
337 m_struct_free(&stream_opts
,opts
);
342 cdda_verbose_set(cdd
, CDDA_MESSAGE_FORGETIT
, CDDA_MESSAGE_FORGETIT
);
345 cdd
->nsectors
= p
->sector_size
;
346 #ifndef CONFIG_LIBCDIO
347 cdd
->bigbuff
= p
->sector_size
* CD_FRAMESIZE_RAW
;
351 if(cdda_open(cdd
) != 0) {
352 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Can't open disc.\n");
354 m_struct_free(&stream_opts
,opts
);
359 cd_info
= cd_info_new();
360 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,"Found audio CD with %d tracks.\n", (int)cdda_tracks(cdd
));
361 for(i
=0;i
<cdd
->tracks
;i
++) {
363 long sec
=cdda_track_firstsector(cdd
,i
+1);
364 long off
=cdda_track_lastsector(cdd
,i
+1)-sec
+1;
366 sprintf(track_name
, "Track %d", i
+1);
367 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
);
370 cd_info
->min
= (unsigned int)(audiolen
/(60*75));
371 cd_info
->sec
= (unsigned int)((audiolen
/75)%60);
372 cd_info
->msec
= (unsigned int)(audiolen
%75);
374 priv
= malloc(sizeof(cdda_priv
));
375 memset(priv
, 0, sizeof(cdda_priv
));
377 priv
->cd_info
= cd_info
;
380 offset
-= cdda_track_firstsector(cdd
,1);
384 for(i
= 0 ; i
< cdd
->tracks
+ 1 ; i
++)
385 cdd
->disc_toc
[i
].dwStartSector
+= offset
;
389 cdda_speed_set(cdd
,p
->speed
);
391 last_track
= cdda_tracks(cdd
);
392 if (p
->span
.start
> last_track
) p
->span
.start
= last_track
;
393 if (p
->span
.end
< p
->span
.start
) p
->span
.end
= p
->span
.start
;
394 if (p
->span
.end
> last_track
) p
->span
.end
= last_track
;
396 priv
->start_sector
= cdda_track_firstsector(cdd
,p
->span
.start
);
398 priv
->start_sector
= cdda_disc_firstsector(cdd
);
401 priv
->end_sector
= cdda_track_lastsector(cdd
,p
->span
.end
);
403 priv
->end_sector
= cdda_disc_lastsector(cdd
);
405 priv
->cdp
= paranoia_init(cdd
);
406 if(priv
->cdp
== NULL
) {
409 cd_info_free(cd_info
);
410 m_struct_free(&stream_opts
,opts
);
416 mode
= PARANOIA_MODE_DISABLE
;
418 mode
= PARANOIA_MODE_OVERLAP
;
420 mode
= PARANOIA_MODE_FULL
;
423 mode
|= PARANOIA_MODE_NEVERSKIP
;
424 #ifndef CONFIG_LIBCDIO
425 // HACK against libcdparanoia's stupid caching model that
426 // queues up a huge number of requests leading to stuttering
427 paranoia_cachemodel_size(priv
->cdp
, 24);
428 paranoia_modeset(cdd
, mode
);
430 if(p
->search_overlap
>= 0)
431 paranoia_overlapset(cdd
,p
->search_overlap
);
433 paranoia_modeset(priv
->cdp
, mode
);
435 if(p
->search_overlap
>= 0)
436 paranoia_overlapset(priv
->cdp
,p
->search_overlap
);
439 paranoia_seek(priv
->cdp
,priv
->start_sector
,SEEK_SET
);
440 priv
->sector
= priv
->start_sector
;
444 cd_info_free(cd_info
);
445 priv
->cd_info
= cddb_info
;
446 cd_info_debug( cddb_info
);
451 st
->start_pos
= priv
->start_sector
*CD_FRAMESIZE_RAW
;
452 st
->end_pos
= (priv
->end_sector
+ 1) * CD_FRAMESIZE_RAW
;
453 st
->type
= STREAMTYPE_CDDA
;
454 st
->sector_size
= CD_FRAMESIZE_RAW
;
456 st
->fill_buffer
= fill_buffer
;
458 st
->control
= control
;
459 st
->close
= close_cdda
;
461 *file_format
= DEMUXER_TYPE_RAWAUDIO
;
463 m_struct_free(&stream_opts
,opts
);
468 const stream_info_t stream_info_cdda
= {
480 1 // Urls are an option string