mixer: support native audio driver mute
[mplayer.git] / stream / stream_cdda.c
blob317292faec7405ec1ad3223076beb35c4df5ac42
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"
20 #ifndef CONFIG_LIBCDIO
21 #include <cdda_interface.h>
22 #include <cdda_paranoia.h>
23 #else
24 #include <cdio/cdda.h>
25 #include <cdio/paranoia.h>
26 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include "talloc.h"
32 #include "stream.h"
33 #include "m_option.h"
34 #include "m_struct.h"
35 #include "libavutil/common.h"
36 #include "mpbswap.h"
37 #include "libmpdemux/demuxer.h"
39 #include "cdd.h"
41 #include "mp_msg.h"
43 #ifndef CD_FRAMESIZE_RAW
44 #define CD_FRAMESIZE_RAW CDIO_CD_FRAMESIZE_RAW
45 #endif
48 extern char *cdrom_device;
50 typedef struct {
51 #ifndef CONFIG_LIBCDIO
52 cdrom_drive* cd;
53 cdrom_paranoia* cdp;
54 #else
55 cdrom_drive_t* cd;
56 cdrom_paranoia_t* cdp;
57 #endif
58 int sector;
59 int start_sector;
60 int end_sector;
61 cd_info_t *cd_info;
62 } cdda_priv;
64 static struct cdda_params {
65 int speed;
66 int paranoia_mode;
67 char* generic_dev;
68 int sector_size;
69 int search_overlap;
70 int toc_bias;
71 int toc_offset;
72 int no_skip;
73 char* device;
74 m_span_t span;
75 } cdda_dflts = {
76 -1,
78 NULL,
80 -1,
84 NULL,
85 { 0, 0 }
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 },
101 /// For url parsing
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 = {
108 "cdda",
109 sizeof(struct cdda_params),
110 &cdda_dflts,
111 cdda_params_fields
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) {
132 #else
133 static void cdparanoia_callback(long int inpos, paranoia_cb_mode_t function) {
134 #endif
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;
140 int16_t * buf;
141 int i;
143 if((p->sector < p->start_sector) || (p->sector > p->end_sector)) {
144 s->eof = 1;
145 return 0;
148 buf = paranoia_read(p->cdp,cdparanoia_callback);
149 if (!buf)
150 return 0;
152 #if HAVE_BIGENDIAN
153 for(i=0;i<CD_FRAMESIZE_RAW/2;i++)
154 buf[i]=le2me_16(buf[i]);
155 #endif
157 p->sector++;
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);
168 break;
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;
179 int sec;
180 int current_track=0, seeked_track=0;
181 int seek_to_track = 0;
182 int i;
184 s->pos = newpos;
185 sec = s->pos/CD_FRAMESIZE_RAW;
186 if (s->pos < 0 || sec > p->end_sector) {
187 s->eof = 1;
188 return 0;
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 ) {
197 current_track = i;
199 if( sec>=p->cd->disc_toc[i].dwStartSector && sec<p->cd->disc_toc[i+1].dwStartSector ) {
200 seeked_track = i;
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);
214 #if 0
215 if(sec < p->start_sector)
216 sec = p->start_sector;
217 else if(sec > p->end_sector)
218 sec = p->end_sector;
219 #endif
221 p->sector = sec;
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);
226 return 1;
229 static void close_cdda(stream_t* s) {
230 cdda_priv* p = (cdda_priv*)s->priv;
231 paranoia_free(p->cdp);
232 cdda_close(p->cd);
233 cd_info_free(p->cd_info);
234 free(p);
237 static int get_track_by_sector(cdda_priv *p, unsigned int sector) {
238 int i;
239 for (i = p->cd->tracks; i >= 0 ; --i)
240 if (p->cd->disc_toc[i].dwStartSector <= sector)
241 break;
242 return i;
245 static int control(stream_t *stream, int cmd, void *arg) {
246 cdda_priv* p = stream->priv;
247 switch(cmd) {
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;
253 return STREAM_OK;
255 case STREAM_CTRL_SEEK_TO_CHAPTER:
257 int r;
258 unsigned int track = *(unsigned int *)arg;
259 int start_track = get_track_by_sector(p, p->start_sector);
260 int seek_sector;
261 track += start_track;
262 if (track >= p->cd->tracks) {
263 stream->eof = 1;
264 return STREAM_ERROR;
266 seek_sector = track <= 0 ? p->start_sector
267 : p->cd->disc_toc[track].dwStartSector;
268 r = seek(stream, seek_sector * CD_FRAMESIZE_RAW);
269 if (r)
270 return STREAM_OK;
271 break;
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;
278 return STREAM_OK;
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;
290 #else
291 cdrom_drive_t* cdd = NULL;
292 #endif
293 cdda_priv* priv;
294 cd_info_t *cd_info,*cddb_info = NULL;
295 unsigned int audiolen=0;
296 int last_track;
297 int i;
298 char *xmcd_file = NULL;
300 if(m != STREAM_READ) {
301 m_struct_free(&stream_opts,opts);
302 return STREAM_UNSUPPORTED;
305 if(!p->device) {
306 if (cdrom_device)
307 p->device = talloc_strdup(NULL, cdrom_device);
308 else
309 p->device = talloc_strdup(NULL, DEFAULT_CDROM_DEVICE);
312 #ifdef CONFIG_CDDB
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);
317 if(i == 0) {
318 cddb_info = cddb_parse_xmcd(xmcd_file);
319 free(xmcd_file);
322 #endif
324 #ifndef CONFIG_LIBCDIO
325 if(p->generic_dev)
326 cdd = cdda_identify_scsi(p->generic_dev,p->device,0,NULL);
327 else
328 #endif
329 #if defined(__NetBSD__)
330 cdd = cdda_identify_scsi(p->device,p->device,0,NULL);
331 #else
332 cdd = cdda_identify(p->device,0,NULL);
333 #endif
335 if(!cdd) {
336 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Can't open CDDA device.\n");
337 m_struct_free(&stream_opts,opts);
338 free(cddb_info);
339 return STREAM_ERROR;
342 cdda_verbose_set(cdd, CDDA_MESSAGE_FORGETIT, CDDA_MESSAGE_FORGETIT);
344 if(p->sector_size) {
345 cdd->nsectors = p->sector_size;
346 #ifndef CONFIG_LIBCDIO
347 cdd->bigbuff = p->sector_size * CD_FRAMESIZE_RAW;
348 #endif
351 if(cdda_open(cdd) != 0) {
352 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Can't open disc.\n");
353 cdda_close(cdd);
354 m_struct_free(&stream_opts,opts);
355 free(cddb_info);
356 return STREAM_ERROR;
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++) {
362 char track_name[80];
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 );
368 audiolen += 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));
376 priv->cd = cdd;
377 priv->cd_info = cd_info;
379 if(p->toc_bias)
380 offset -= cdda_track_firstsector(cdd,1);
382 if(offset) {
383 int i;
384 for(i = 0 ; i < cdd->tracks + 1 ; i++)
385 cdd->disc_toc[i].dwStartSector += offset;
388 if(p->speed)
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;
395 if(p->span.start)
396 priv->start_sector = cdda_track_firstsector(cdd,p->span.start);
397 else
398 priv->start_sector = cdda_disc_firstsector(cdd);
400 if(p->span.end) {
401 priv->end_sector = cdda_track_lastsector(cdd,p->span.end);
402 } else
403 priv->end_sector = cdda_disc_lastsector(cdd);
405 priv->cdp = paranoia_init(cdd);
406 if(priv->cdp == NULL) {
407 cdda_close(cdd);
408 free(priv);
409 cd_info_free(cd_info);
410 m_struct_free(&stream_opts,opts);
411 free(cddb_info);
412 return STREAM_ERROR;
415 if(mode == 0)
416 mode = PARANOIA_MODE_DISABLE;
417 else if(mode == 1)
418 mode = PARANOIA_MODE_OVERLAP;
419 else
420 mode = PARANOIA_MODE_FULL;
422 if(p->no_skip)
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);
432 #else
433 paranoia_modeset(priv->cdp, mode);
435 if(p->search_overlap >= 0)
436 paranoia_overlapset(priv->cdp,p->search_overlap);
437 #endif
439 paranoia_seek(priv->cdp,priv->start_sector,SEEK_SET);
440 priv->sector = priv->start_sector;
442 #ifdef CONFIG_CDDB
443 if(cddb_info) {
444 cd_info_free(cd_info);
445 priv->cd_info = cddb_info;
446 cd_info_debug( cddb_info );
448 #endif
450 st->priv = priv;
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;
457 st->seek = seek;
458 st->control = control;
459 st->close = close_cdda;
461 *file_format = DEMUXER_TYPE_RAWAUDIO;
463 m_struct_free(&stream_opts,opts);
465 return STREAM_OK;
468 const stream_info_t stream_info_cdda = {
469 "CDDA",
470 "cdda",
471 "Albeu",
473 open_cdda,
474 { "cdda",
475 #ifdef CONFIG_CDDB
476 "cddb",
477 #endif
478 NULL },
479 &stream_opts,
480 1 // Urls are an option string