stream_dvd: fix incorrect assumption about chapter count
[mplayer/glamo.git] / stream / stream_cdda.c
blobaa459a7e3d2864be4466699104aafa1b3982ba58
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 "stream.h"
31 #include "m_option.h"
32 #include "m_struct.h"
33 #include "libavutil/common.h"
34 #include "mpbswap.h"
35 #include "libmpdemux/demuxer.h"
37 #include "cdd.h"
39 #include "mp_msg.h"
41 #ifndef CD_FRAMESIZE_RAW
42 #define CD_FRAMESIZE_RAW CDIO_CD_FRAMESIZE_RAW
43 #endif
46 extern char *cdrom_device;
48 typedef struct {
49 #ifndef CONFIG_LIBCDIO
50 cdrom_drive* cd;
51 cdrom_paranoia* cdp;
52 #else
53 cdrom_drive_t* cd;
54 cdrom_paranoia_t* cdp;
55 #endif
56 int sector;
57 int start_sector;
58 int end_sector;
59 cd_info_t *cd_info;
60 } cdda_priv;
62 static struct cdda_params {
63 int speed;
64 int paranoia_mode;
65 char* generic_dev;
66 int sector_size;
67 int search_overlap;
68 int toc_bias;
69 int toc_offset;
70 int no_skip;
71 char* device;
72 m_span_t span;
73 } cdda_dflts = {
74 -1,
76 NULL,
78 -1,
82 NULL,
83 { 0, 0 }
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 },
99 /// For url parsing
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 = {
106 "cdda",
107 sizeof(struct cdda_params),
108 &cdda_dflts,
109 cdda_params_fields
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) {
130 #else
131 static void cdparanoia_callback(long int inpos, paranoia_cb_mode_t function) {
132 #endif
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;
138 int16_t * buf;
139 int i;
141 if((p->sector < p->start_sector) || (p->sector > p->end_sector)) {
142 s->eof = 1;
143 return 0;
146 buf = paranoia_read(p->cdp,cdparanoia_callback);
147 if (!buf)
148 return 0;
150 #if HAVE_BIGENDIAN
151 for(i=0;i<CD_FRAMESIZE_RAW/2;i++)
152 buf[i]=le2me_16(buf[i]);
153 #endif
155 p->sector++;
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);
166 break;
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;
177 int sec;
178 int current_track=0, seeked_track=0;
179 int seek_to_track = 0;
180 int i;
182 s->pos = newpos;
183 sec = s->pos/CD_FRAMESIZE_RAW;
184 if (s->pos < 0 || sec > p->end_sector) {
185 s->eof = 1;
186 return 0;
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 ) {
195 current_track = i;
197 if( sec>=p->cd->disc_toc[i].dwStartSector && sec<p->cd->disc_toc[i+1].dwStartSector ) {
198 seeked_track = i;
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);
212 #if 0
213 if(sec < p->start_sector)
214 sec = p->start_sector;
215 else if(sec > p->end_sector)
216 sec = p->end_sector;
217 #endif
219 p->sector = sec;
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);
224 return 1;
227 static void close_cdda(stream_t* s) {
228 cdda_priv* p = (cdda_priv*)s->priv;
229 paranoia_free(p->cdp);
230 cdda_close(p->cd);
231 cd_info_free(p->cd_info);
232 free(p);
235 static int get_track_by_sector(cdda_priv *p, unsigned int sector) {
236 int i;
237 for (i = p->cd->tracks; i >= 0 ; --i)
238 if (p->cd->disc_toc[i].dwStartSector <= sector)
239 break;
240 return i;
243 static int control(stream_t *stream, int cmd, void *arg) {
244 cdda_priv* p = stream->priv;
245 switch(cmd) {
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;
251 return STREAM_OK;
253 case STREAM_CTRL_SEEK_TO_CHAPTER:
255 int r;
256 unsigned int track = *(unsigned int *)arg;
257 int start_track = get_track_by_sector(p, p->start_sector);
258 int seek_sector;
259 track += start_track;
260 if (track >= p->cd->tracks) {
261 stream->eof = 1;
262 return STREAM_ERROR;
264 seek_sector = track <= 0 ? p->start_sector
265 : p->cd->disc_toc[track].dwStartSector;
266 r = seek(stream, seek_sector * CD_FRAMESIZE_RAW);
267 if (r)
268 return STREAM_OK;
269 break;
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;
276 return STREAM_OK;
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;
288 #else
289 cdrom_drive_t* cdd = NULL;
290 #endif
291 cdda_priv* priv;
292 cd_info_t *cd_info,*cddb_info = NULL;
293 unsigned int audiolen=0;
294 int last_track;
295 int i;
296 char *xmcd_file = NULL;
298 if(m != STREAM_READ) {
299 m_struct_free(&stream_opts,opts);
300 return STREAM_UNSUPPORTED;
303 if(!p->device) {
304 if (cdrom_device)
305 p->device = strdup(cdrom_device);
306 else
307 p->device = strdup(DEFAULT_CDROM_DEVICE);
310 #ifdef CONFIG_CDDB
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);
315 if(i == 0) {
316 cddb_info = cddb_parse_xmcd(xmcd_file);
317 free(xmcd_file);
320 #endif
322 #ifndef CONFIG_LIBCDIO
323 if(p->generic_dev)
324 cdd = cdda_identify_scsi(p->generic_dev,p->device,0,NULL);
325 else
326 #endif
327 #if defined(__NetBSD__)
328 cdd = cdda_identify_scsi(p->device,p->device,0,NULL);
329 #else
330 cdd = cdda_identify(p->device,0,NULL);
331 #endif
333 if(!cdd) {
334 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Can't open CDDA device.\n");
335 m_struct_free(&stream_opts,opts);
336 free(cddb_info);
337 return STREAM_ERROR;
340 cdda_verbose_set(cdd, CDDA_MESSAGE_FORGETIT, CDDA_MESSAGE_FORGETIT);
342 if(p->sector_size) {
343 cdd->nsectors = p->sector_size;
344 #ifndef CONFIG_LIBCDIO
345 cdd->bigbuff = p->sector_size * CD_FRAMESIZE_RAW;
346 #endif
349 if(cdda_open(cdd) != 0) {
350 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Can't open disc.\n");
351 cdda_close(cdd);
352 m_struct_free(&stream_opts,opts);
353 free(cddb_info);
354 return STREAM_ERROR;
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++) {
360 char track_name[80];
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 );
366 audiolen += 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));
374 priv->cd = cdd;
375 priv->cd_info = cd_info;
377 if(p->toc_bias)
378 offset -= cdda_track_firstsector(cdd,1);
380 if(offset) {
381 int i;
382 for(i = 0 ; i < cdd->tracks + 1 ; i++)
383 cdd->disc_toc[i].dwStartSector += offset;
386 if(p->speed)
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;
393 if(p->span.start)
394 priv->start_sector = cdda_track_firstsector(cdd,p->span.start);
395 else
396 priv->start_sector = cdda_disc_firstsector(cdd);
398 if(p->span.end) {
399 priv->end_sector = cdda_track_lastsector(cdd,p->span.end);
400 } else
401 priv->end_sector = cdda_disc_lastsector(cdd);
403 priv->cdp = paranoia_init(cdd);
404 if(priv->cdp == NULL) {
405 cdda_close(cdd);
406 free(priv);
407 cd_info_free(cd_info);
408 m_struct_free(&stream_opts,opts);
409 free(cddb_info);
410 return STREAM_ERROR;
413 if(mode == 0)
414 mode = PARANOIA_MODE_DISABLE;
415 else if(mode == 1)
416 mode = PARANOIA_MODE_OVERLAP;
417 else
418 mode = PARANOIA_MODE_FULL;
420 if(p->no_skip)
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);
427 #else
428 paranoia_modeset(priv->cdp, mode);
430 if(p->search_overlap >= 0)
431 paranoia_overlapset(priv->cdp,p->search_overlap);
432 #endif
434 paranoia_seek(priv->cdp,priv->start_sector,SEEK_SET);
435 priv->sector = priv->start_sector;
437 #ifdef CONFIG_CDDB
438 if(cddb_info) {
439 cd_info_free(cd_info);
440 priv->cd_info = cddb_info;
441 cd_info_debug( cddb_info );
443 #endif
445 st->priv = priv;
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;
452 st->seek = seek;
453 st->control = control;
454 st->close = close_cdda;
456 *file_format = DEMUXER_TYPE_RAWAUDIO;
458 m_struct_free(&stream_opts,opts);
460 return STREAM_OK;
463 const stream_info_t stream_info_cdda = {
464 "CDDA",
465 "cdda",
466 "Albeu",
468 open_cdda,
469 { "cdda",
470 #ifdef CONFIG_CDDB
471 "cddb",
472 #endif
473 NULL },
474 &stream_opts,
475 1 // Urls are an option string