Ignore svn changes up to r30505
[mplayer/kovensky.git] / stream / stream_cdda.c
blob8f8e414e2366637210ea96ae7c9ed14d544d6fda
1 /*
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.
19 #include "config.h"
21 #include <stdio.h>
22 #include <stdlib.h>
24 #include "stream.h"
25 #include "m_option.h"
26 #include "m_struct.h"
27 #include "libavutil/common.h"
28 #include "mpbswap.h"
29 #include "libmpdemux/demuxer.h"
31 #include "cdd.h"
33 #include "mp_msg.h"
34 #include "help_mp.h"
36 #ifndef CD_FRAMESIZE_RAW
37 #define CD_FRAMESIZE_RAW CDIO_CD_FRAMESIZE_RAW
38 #endif
41 extern char *cdrom_device;
43 static struct cdda_params {
44 int speed;
45 int paranoia_mode;
46 char* generic_dev;
47 int sector_size;
48 int search_overlap;
49 int toc_bias;
50 int toc_offset;
51 int no_skip;
52 char* device;
53 m_span_t span;
54 } cdda_dflts = {
55 -1,
57 NULL,
59 -1,
63 NULL,
64 { 0, 0 }
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 },
80 /// For url parsing
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 = {
87 "cdda",
88 sizeof(struct cdda_params),
89 &cdda_dflts,
90 cdda_params_fields
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) {
118 int i;
119 for (i = p->cd->tracks; i >= 0 ; --i)
120 if (p->cd->disc_toc[i].dwStartSector <= sector)
121 break;
122 return i;
125 static int control(stream_t *stream, int cmd, void *arg) {
126 cdda_priv* p = stream->priv;
127 switch(cmd) {
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;
133 return STREAM_OK;
135 case STREAM_CTRL_SEEK_TO_CHAPTER:
137 int r;
138 unsigned int track = *(unsigned int *)arg;
139 int start_track = get_track_by_sector(p, p->start_sector);
140 int seek_sector;
141 track += start_track;
142 if (track >= p->cd->tracks) {
143 stream->eof = 1;
144 return STREAM_ERROR;
146 seek_sector = track <= 0 ? p->start_sector
147 : p->cd->disc_toc[track].dwStartSector;
148 r = seek(stream, seek_sector * CD_FRAMESIZE_RAW);
149 if (r)
150 return STREAM_OK;
151 break;
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;
158 return STREAM_OK;
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;
170 #else
171 cdrom_drive_t* cdd = NULL;
172 #endif
173 cdda_priv* priv;
174 cd_info_t *cd_info,*cddb_info = NULL;
175 unsigned int audiolen=0;
176 int last_track;
177 int i;
178 char *xmcd_file = NULL;
180 if(m != STREAM_READ) {
181 m_struct_free(&stream_opts,opts);
182 return STREAM_UNSUPPORTED;
185 if(!p->device) {
186 if (cdrom_device)
187 p->device = strdup(cdrom_device);
188 else
189 p->device = strdup(DEFAULT_CDROM_DEVICE);
192 #ifdef CONFIG_CDDB
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);
197 if(i == 0) {
198 cddb_info = cddb_parse_xmcd(xmcd_file);
199 free(xmcd_file);
202 #endif
204 #ifndef CONFIG_LIBCDIO
205 if(p->generic_dev)
206 cdd = cdda_identify_scsi(p->generic_dev,p->device,0,NULL);
207 else
208 #endif
209 #if defined(__NetBSD__)
210 cdd = cdda_identify_scsi(p->device,p->device,0,NULL);
211 #else
212 cdd = cdda_identify(p->device,0,NULL);
213 #endif
215 if(!cdd) {
216 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Can't open CDDA device.\n");
217 m_struct_free(&stream_opts,opts);
218 free(cddb_info);
219 return STREAM_ERROR;
222 cdda_verbose_set(cdd, CDDA_MESSAGE_FORGETIT, CDDA_MESSAGE_FORGETIT);
224 if(p->sector_size) {
225 cdd->nsectors = p->sector_size;
226 #ifndef CONFIG_LIBCDIO
227 cdd->bigbuff = p->sector_size * CD_FRAMESIZE_RAW;
228 #endif
231 if(cdda_open(cdd) != 0) {
232 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Can't open disc.\n");
233 cdda_close(cdd);
234 m_struct_free(&stream_opts,opts);
235 free(cddb_info);
236 return STREAM_ERROR;
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++) {
242 char track_name[80];
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 );
248 audiolen += 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));
256 priv->cd = cdd;
257 priv->cd_info = cd_info;
259 if(p->toc_bias)
260 offset -= cdda_track_firstsector(cdd,1);
262 if(offset) {
263 int i;
264 for(i = 0 ; i < cdd->tracks + 1 ; i++)
265 cdd->disc_toc[i].dwStartSector += offset;
268 if(p->speed)
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;
275 if(p->span.start)
276 priv->start_sector = cdda_track_firstsector(cdd,p->span.start);
277 else
278 priv->start_sector = cdda_disc_firstsector(cdd);
280 if(p->span.end) {
281 priv->end_sector = cdda_track_lastsector(cdd,p->span.end);
282 } else
283 priv->end_sector = cdda_disc_lastsector(cdd);
285 priv->cdp = paranoia_init(cdd);
286 if(priv->cdp == NULL) {
287 cdda_close(cdd);
288 free(priv);
289 cd_info_free(cd_info);
290 m_struct_free(&stream_opts,opts);
291 free(cddb_info);
292 return STREAM_ERROR;
295 if(mode == 0)
296 mode = PARANOIA_MODE_DISABLE;
297 else if(mode == 1)
298 mode = PARANOIA_MODE_OVERLAP;
299 else
300 mode = PARANOIA_MODE_FULL;
302 if(p->no_skip)
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);
309 #else
310 paranoia_modeset(priv->cdp, mode);
312 if(p->search_overlap >= 0)
313 paranoia_overlapset(priv->cdp,p->search_overlap);
314 #endif
316 paranoia_seek(priv->cdp,priv->start_sector,SEEK_SET);
317 priv->sector = priv->start_sector;
319 #ifdef CONFIG_CDDB
320 if(cddb_info) {
321 cd_info_free(cd_info);
322 priv->cd_info = cddb_info;
323 cd_info_debug( cddb_info );
325 #endif
327 st->priv = priv;
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;
334 st->seek = seek;
335 st->control = control;
336 st->close = close_cdda;
338 *file_format = DEMUXER_TYPE_RAWAUDIO;
340 m_struct_free(&stream_opts,opts);
342 return STREAM_OK;
345 #ifndef CONFIG_LIBCDIO
346 static void cdparanoia_callback(long inpos, int function) {
347 #else
348 static void cdparanoia_callback(long int inpos, paranoia_cb_mode_t function) {
349 #endif
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;
355 int16_t * buf;
356 int i;
358 if((p->sector < p->start_sector) || (p->sector > p->end_sector)) {
359 s->eof = 1;
360 return 0;
363 buf = paranoia_read(p->cdp,cdparanoia_callback);
364 if (!buf)
365 return 0;
367 #if HAVE_BIGENDIAN
368 for(i=0;i<CD_FRAMESIZE_RAW/2;i++)
369 buf[i]=le2me_16(buf[i]);
370 #endif
372 p->sector++;
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);
383 break;
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;
394 int sec;
395 int current_track=0, seeked_track=0;
396 int seek_to_track = 0;
397 int i;
399 s->pos = newpos;
400 sec = s->pos/CD_FRAMESIZE_RAW;
401 if (s->pos < 0 || sec > p->end_sector) {
402 s->eof = 1;
403 return 0;
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 ) {
412 current_track = i;
414 if( sec>=p->cd->disc_toc[i].dwStartSector && sec<p->cd->disc_toc[i+1].dwStartSector ) {
415 seeked_track = i;
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);
429 #if 0
430 if(sec < p->start_sector)
431 sec = p->start_sector;
432 else if(sec > p->end_sector)
433 sec = p->end_sector;
434 #endif
436 p->sector = sec;
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);
441 return 1;
444 static void close_cdda(stream_t* s) {
445 cdda_priv* p = (cdda_priv*)s->priv;
446 paranoia_free(p->cdp);
447 cdda_close(p->cd);
448 cd_info_free(p->cd_info);
449 free(p);
452 const stream_info_t stream_info_cdda = {
453 "CDDA",
454 "cdda",
455 "Albeu",
457 open_cdda,
458 { "cdda",
459 #ifdef CONFIG_CDDB
460 "cddb",
461 #endif
462 NULL },
463 &stream_opts,
464 1 // Urls are an option string