100l, last patch broke window resizing with xv and xvmc.
[mplayer/glamo.git] / stream / stream_cdda.c
blobefb9afcb7f59456bc219ad8945e18232a7bc6df8
1 #include "config.h"
3 #include <stdio.h>
4 #include <stdlib.h>
6 #include "stream.h"
7 #include "m_option.h"
8 #include "m_struct.h"
9 #include "libavutil/common.h"
10 #include "mpbswap.h"
11 #include "libmpdemux/demuxer.h"
13 #include "cdd.h"
15 #include "mp_msg.h"
16 #include "help_mp.h"
18 #ifndef CD_FRAMESIZE_RAW
19 #define CD_FRAMESIZE_RAW CDIO_CD_FRAMESIZE_RAW
20 #endif
23 extern char *cdrom_device;
25 static struct cdda_params {
26 int speed;
27 int paranoia_mode;
28 char* generic_dev;
29 int sector_size;
30 int search_overlap;
31 int toc_bias;
32 int toc_offset;
33 int no_skip;
34 char* device;
35 m_span_t span;
36 } cdda_dflts = {
37 -1,
39 NULL,
41 -1,
45 NULL,
46 { 0, 0 }
49 #define ST_OFF(f) M_ST_OFF(struct cdda_params,f)
50 m_option_t cdda_params_fields[] = {
51 { "speed", ST_OFF(speed), CONF_TYPE_INT, M_OPT_RANGE,1,100, NULL },
52 { "paranoia", ST_OFF(paranoia_mode), CONF_TYPE_INT,M_OPT_RANGE, 0, 2, NULL },
53 { "generic-dev", ST_OFF(generic_dev), CONF_TYPE_STRING, 0, 0, 0, NULL },
54 { "sector-size", ST_OFF(sector_size), CONF_TYPE_INT, M_OPT_RANGE,1,100, NULL },
55 { "overlap", ST_OFF(search_overlap), CONF_TYPE_INT, M_OPT_RANGE,0,75, NULL },
56 { "toc-bias", ST_OFF(toc_bias), CONF_TYPE_INT, 0, 0, 0, NULL },
57 { "toc-offset", ST_OFF(toc_offset), CONF_TYPE_INT, 0, 0, 0, NULL },
58 { "noskip", ST_OFF(no_skip), CONF_TYPE_FLAG, 0 , 0, 1, NULL },
59 { "skip", ST_OFF(no_skip), CONF_TYPE_FLAG, 0 , 1, 0, NULL },
60 { "device", ST_OFF(device), CONF_TYPE_STRING, 0, 0, 0, NULL },
61 { "span", ST_OFF(span), CONF_TYPE_OBJ_PARAMS, 0, 0, 0, &m_span_params_def },
62 /// For url parsing
63 { "hostname", ST_OFF(span), CONF_TYPE_OBJ_PARAMS, 0, 0, 0, &m_span_params_def },
64 { "port", ST_OFF(speed), CONF_TYPE_INT, M_OPT_RANGE,1,100, NULL },
65 { "filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0, 0, NULL },
66 {NULL, NULL, 0, 0, 0, 0, NULL}
68 static struct m_struct_st stream_opts = {
69 "cdda",
70 sizeof(struct cdda_params),
71 &cdda_dflts,
72 cdda_params_fields
75 /// We keep these options but now they set the defaults
76 m_option_t cdda_opts[] = {
77 { "speed", &cdda_dflts.speed, CONF_TYPE_INT, M_OPT_RANGE,1,100, NULL },
78 { "paranoia", &cdda_dflts.paranoia_mode, CONF_TYPE_INT,M_OPT_RANGE, 0, 2, NULL },
79 { "generic-dev", &cdda_dflts.generic_dev, CONF_TYPE_STRING, 0, 0, 0, NULL },
80 { "sector-size", &cdda_dflts.sector_size, CONF_TYPE_INT, M_OPT_RANGE,1,100, NULL },
81 { "overlap", &cdda_dflts.search_overlap, CONF_TYPE_INT, M_OPT_RANGE,0,75, NULL },
82 { "toc-bias", &cdda_dflts.toc_bias, CONF_TYPE_INT, 0, 0, 0, NULL },
83 { "toc-offset", &cdda_dflts.toc_offset, CONF_TYPE_INT, 0, 0, 0, NULL },
84 { "noskip", &cdda_dflts.no_skip, CONF_TYPE_FLAG, 0 , 0, 1, NULL },
85 { "skip", &cdda_dflts.no_skip, CONF_TYPE_FLAG, 0 , 1, 0, NULL },
86 { "device", &cdda_dflts.device, CONF_TYPE_STRING, 0, 0, 0, NULL },
87 { "span", &cdda_dflts.span, CONF_TYPE_OBJ_PARAMS, 0, 0, 0, &m_span_params_def },
88 {NULL, NULL, 0, 0, 0, 0, NULL}
91 extern int cdd_identify(const char *dev);
92 extern int cddb_resolve(const char *dev, char **xmcd_file);
93 extern cd_info_t* cddb_parse_xmcd(char *xmcd_file);
95 static int seek(stream_t* s,off_t pos);
96 static int fill_buffer(stream_t* s, char* buffer, int max_len);
97 static void close_cdda(stream_t* s);
99 static int open_cdda(stream_t *st,int m, void* opts, int* file_format) {
100 struct cdda_params* p = (struct cdda_params*)opts;
101 int mode = p->paranoia_mode;
102 int offset = p->toc_offset;
103 #ifndef HAVE_LIBCDIO
104 cdrom_drive* cdd = NULL;
105 #else
106 cdrom_drive_t* cdd = NULL;
107 #endif
108 cdda_priv* priv;
109 cd_info_t *cd_info,*cddb_info = NULL;
110 unsigned int audiolen=0;
111 int last_track;
112 int i;
113 char *xmcd_file = NULL;
115 if(m != STREAM_READ) {
116 m_struct_free(&stream_opts,opts);
117 return STREAM_UNSUPORTED;
120 if(!p->device) {
121 if (cdrom_device)
122 p->device = strdup(cdrom_device);
123 else
124 p->device = strdup(DEFAULT_CDROM_DEVICE);
127 #ifdef HAVE_CDDB
128 // cdd_identify returns -1 if it cannot read the TOC,
129 // in which case there is no point in calling cddb_resolve
130 if(cdd_identify(p->device) >= 0 && strncmp(st->url,"cddb",4) == 0) {
131 i = cddb_resolve(p->device, &xmcd_file);
132 if(i == 0) {
133 cddb_info = cddb_parse_xmcd(xmcd_file);
134 free(xmcd_file);
137 #endif
139 #ifndef HAVE_LIBCDIO
140 if(p->generic_dev)
141 cdd = cdda_identify_scsi(p->generic_dev,p->device,0,NULL);
142 else
143 #endif
144 #if defined(__NetBSD__)
145 cdd = cdda_identify_scsi(p->device,p->device,0,NULL);
146 #else
147 cdd = cdda_identify(p->device,0,NULL);
148 #endif
150 if(!cdd) {
151 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_MPDEMUX_CDDA_CantOpenCDDADevice);
152 m_struct_free(&stream_opts,opts);
153 free(cddb_info);
154 return STREAM_ERROR;
157 cdda_verbose_set(cdd, CDDA_MESSAGE_FORGETIT, CDDA_MESSAGE_FORGETIT);
159 if(p->sector_size) {
160 cdd->nsectors = p->sector_size;
161 #ifndef HAVE_LIBCDIO
162 cdd->bigbuff = p->sector_size * CD_FRAMESIZE_RAW;
163 #endif
166 if(cdda_open(cdd) != 0) {
167 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_MPDEMUX_CDDA_CantOpenDisc);
168 cdda_close(cdd);
169 m_struct_free(&stream_opts,opts);
170 free(cddb_info);
171 return STREAM_ERROR;
174 cd_info = cd_info_new();
175 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_MPDEMUX_CDDA_AudioCDFoundWithNTracks,cdda_tracks(cdd));
176 for(i=0;i<cdd->tracks;i++) {
177 char track_name[80];
178 long sec=cdda_track_firstsector(cdd,i+1);
179 long off=cdda_track_lastsector(cdd,i+1)-sec+1;
181 sprintf(track_name, "Track %d", i+1);
182 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 );
183 audiolen += off;
185 cd_info->min = (unsigned int)(audiolen/(60*75));
186 cd_info->sec = (unsigned int)((audiolen/75)%60);
187 cd_info->msec = (unsigned int)(audiolen%75);
189 priv = malloc(sizeof(cdda_priv));
190 memset(priv, 0, sizeof(cdda_priv));
191 priv->cd = cdd;
192 priv->cd_info = cd_info;
194 if(p->toc_bias)
195 offset -= cdda_track_firstsector(cdd,1);
197 if(offset) {
198 int i;
199 for(i = 0 ; i < cdd->tracks + 1 ; i++)
200 cdd->disc_toc[i].dwStartSector += offset;
203 if(p->speed)
204 cdda_speed_set(cdd,p->speed);
206 last_track = cdda_tracks(cdd);
207 if (p->span.start > last_track) p->span.start = last_track;
208 if (p->span.end < p->span.start) p->span.end = p->span.start;
209 if (p->span.end > last_track) p->span.end = last_track;
210 if(p->span.start)
211 priv->start_sector = cdda_track_firstsector(cdd,p->span.start);
212 else
213 priv->start_sector = cdda_disc_firstsector(cdd);
215 if(p->span.end) {
216 priv->end_sector = cdda_track_lastsector(cdd,p->span.end);
217 } else
218 priv->end_sector = cdda_disc_lastsector(cdd);
220 priv->cdp = paranoia_init(cdd);
221 if(priv->cdp == NULL) {
222 cdda_close(cdd);
223 free(priv);
224 cd_info_free(cd_info);
225 m_struct_free(&stream_opts,opts);
226 free(cddb_info);
227 return STREAM_ERROR;
230 if(mode == 0)
231 mode = PARANOIA_MODE_DISABLE;
232 else if(mode == 1)
233 mode = PARANOIA_MODE_OVERLAP;
234 else
235 mode = PARANOIA_MODE_FULL;
237 if(p->no_skip)
238 mode |= PARANOIA_MODE_NEVERSKIP;
239 #ifndef HAVE_LIBCDIO
240 paranoia_modeset(cdd, mode);
242 if(p->search_overlap >= 0)
243 paranoia_overlapset(cdd,p->search_overlap);
244 #else
245 paranoia_modeset(priv->cdp, mode);
247 if(p->search_overlap >= 0)
248 paranoia_overlapset(priv->cdp,p->search_overlap);
249 #endif
251 paranoia_seek(priv->cdp,priv->start_sector,SEEK_SET);
252 priv->sector = priv->start_sector;
254 #ifdef HAVE_CDDB
255 if(cddb_info) {
256 cd_info_free(cd_info);
257 priv->cd_info = cddb_info;
258 cd_info_debug( cddb_info );
260 #endif
262 st->priv = priv;
263 st->start_pos = priv->start_sector*CD_FRAMESIZE_RAW;
264 st->end_pos = priv->end_sector*CD_FRAMESIZE_RAW;
265 st->type = STREAMTYPE_CDDA;
266 st->sector_size = CD_FRAMESIZE_RAW;
268 st->fill_buffer = fill_buffer;
269 st->seek = seek;
270 st->close = close_cdda;
272 *file_format = DEMUXER_TYPE_RAWAUDIO;
274 m_struct_free(&stream_opts,opts);
276 return STREAM_OK;
279 #ifndef HAVE_LIBCDIO
280 static void cdparanoia_callback(long inpos, int function) {
281 #else
282 static void cdparanoia_callback(long int inpos, paranoia_cb_mode_t function) {
283 #endif
286 static int fill_buffer(stream_t* s, char* buffer, int max_len) {
287 cdda_priv* p = (cdda_priv*)s->priv;
288 cd_track_t *cd_track;
289 int16_t * buf;
290 int i;
292 buf = paranoia_read(p->cdp,cdparanoia_callback);
293 if (!buf)
294 return 0;
296 #ifdef WORDS_BIGENDIAN
297 for(i=0;i<CD_FRAMESIZE_RAW/2;i++)
298 buf[i]=le2me_16(buf[i]);
299 #endif
301 p->sector++;
302 s->pos = p->sector*CD_FRAMESIZE_RAW;
303 memcpy(buffer,buf,CD_FRAMESIZE_RAW);
305 if((p->sector < p->start_sector) || (p->sector >= p->end_sector)) {
306 s->eof = 1;
307 return 0;
310 for(i=0;i<p->cd->tracks;i++){
311 if(p->cd->disc_toc[i].dwStartSector==p->sector-1) {
312 cd_track = cd_info_get_track(p->cd_info, i+1);
313 //printf("Track %d, sector=%d\n", i, p->sector-1);
314 if( cd_track!=NULL ) {
315 mp_msg(MSGT_SEEK, MSGL_INFO, "\n%s\n", cd_track->name);
316 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDA_TRACK=%d\n", cd_track->track_nb);
318 break;
323 return CD_FRAMESIZE_RAW;
326 static int seek(stream_t* s,off_t newpos) {
327 cdda_priv* p = (cdda_priv*)s->priv;
328 cd_track_t *cd_track;
329 int sec;
330 int current_track=0, seeked_track=0;
331 int i;
333 s->pos = newpos;
334 if(s->pos < 0) {
335 s->eof = 1;
336 return 0;
339 sec = s->pos/CD_FRAMESIZE_RAW;
340 //printf("pos: %d, sec: %d ## %d\n", (int)s->pos, (int)sec, CD_FRAMESIZE_RAW);
341 //printf("sector: %d new: %d\n", p->sector, sec );
343 for(i=0;i<p->cd->tracks;i++){
344 // printf("trk #%d: %d .. %d\n",i,p->cd->disc_toc[i].dwStartSector,p->cd->disc_toc[i+1].dwStartSector);
345 if( p->sector>=p->cd->disc_toc[i].dwStartSector && p->sector<p->cd->disc_toc[i+1].dwStartSector ) {
346 current_track = i;
348 if( sec>=p->cd->disc_toc[i].dwStartSector && sec<p->cd->disc_toc[i+1].dwStartSector ) {
349 seeked_track = i;
352 //printf("current: %d, seeked: %d\n", current_track, seeked_track);
353 if( current_track!=seeked_track ) {
354 //printf("Track %d, sector=%d\n", seeked_track, sec);
355 cd_track = cd_info_get_track(p->cd_info, seeked_track+1);
356 if( cd_track!=NULL ) {
357 mp_msg(MSGT_SEEK, MSGL_INFO, "\n%s\n", cd_track->name);
358 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDA_TRACK=%d\n", cd_track->track_nb);
362 #if 0
363 if(sec < p->start_sector)
364 sec = p->start_sector;
365 else if(sec > p->end_sector)
366 sec = p->end_sector;
367 #endif
369 p->sector = sec;
370 // s->pos = sec*CD_FRAMESIZE_RAW;
372 //printf("seek: %d, sec: %d\n", (int)s->pos, sec);
373 paranoia_seek(p->cdp,sec,SEEK_SET);
374 return 1;
377 static void close_cdda(stream_t* s) {
378 cdda_priv* p = (cdda_priv*)s->priv;
379 paranoia_free(p->cdp);
380 cdda_close(p->cd);
381 cd_info_free(p->cd_info);
382 free(p);
385 stream_info_t stream_info_cdda = {
386 "CDDA",
387 "cdda",
388 "Albeu",
390 open_cdda,
391 { "cdda",
392 #ifdef HAVE_CDDB
393 "cddb",
394 #endif
395 NULL },
396 &stream_opts,
397 1 // Urls are an option string