Fix vf_tcdump's compilation
[mplayer/kovensky.git] / stream / stream_cdda.c
blobca36b73dbee7e6a22f6577b50df974a5f570a87e
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"
35 #ifndef CD_FRAMESIZE_RAW
36 #define CD_FRAMESIZE_RAW CDIO_CD_FRAMESIZE_RAW
37 #endif
40 extern char *cdrom_device;
42 static struct cdda_params {
43 int speed;
44 int paranoia_mode;
45 char* generic_dev;
46 int sector_size;
47 int search_overlap;
48 int toc_bias;
49 int toc_offset;
50 int no_skip;
51 char* device;
52 m_span_t span;
53 } cdda_dflts = {
54 -1,
56 NULL,
58 -1,
62 NULL,
63 { 0, 0 }
66 #define ST_OFF(f) M_ST_OFF(struct cdda_params,f)
67 static const m_option_t cdda_params_fields[] = {
68 { "speed", ST_OFF(speed), CONF_TYPE_INT, M_OPT_RANGE,1,100, NULL },
69 { "paranoia", ST_OFF(paranoia_mode), CONF_TYPE_INT,M_OPT_RANGE, 0, 2, NULL },
70 { "generic-dev", ST_OFF(generic_dev), CONF_TYPE_STRING, 0, 0, 0, NULL },
71 { "sector-size", ST_OFF(sector_size), CONF_TYPE_INT, M_OPT_RANGE,1,100, NULL },
72 { "overlap", ST_OFF(search_overlap), CONF_TYPE_INT, M_OPT_RANGE,0,75, NULL },
73 { "toc-bias", ST_OFF(toc_bias), CONF_TYPE_INT, 0, 0, 0, NULL },
74 { "toc-offset", ST_OFF(toc_offset), CONF_TYPE_INT, 0, 0, 0, NULL },
75 { "noskip", ST_OFF(no_skip), CONF_TYPE_FLAG, 0 , 0, 1, NULL },
76 { "skip", ST_OFF(no_skip), CONF_TYPE_FLAG, 0 , 1, 0, NULL },
77 { "device", ST_OFF(device), CONF_TYPE_STRING, 0, 0, 0, NULL },
78 { "span", ST_OFF(span), CONF_TYPE_OBJ_PARAMS, 0, 0, 0, &m_span_params_def },
79 /// For url parsing
80 { "hostname", ST_OFF(span), CONF_TYPE_OBJ_PARAMS, 0, 0, 0, &m_span_params_def },
81 { "port", ST_OFF(speed), CONF_TYPE_INT, M_OPT_RANGE,1,100, NULL },
82 { "filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0, 0, NULL },
83 {NULL, NULL, 0, 0, 0, 0, NULL}
85 static const struct m_struct_st stream_opts = {
86 "cdda",
87 sizeof(struct cdda_params),
88 &cdda_dflts,
89 cdda_params_fields
92 /// We keep these options but now they set the defaults
93 const m_option_t cdda_opts[] = {
94 { "speed", &cdda_dflts.speed, CONF_TYPE_INT, M_OPT_RANGE,1,100, NULL },
95 { "paranoia", &cdda_dflts.paranoia_mode, CONF_TYPE_INT,M_OPT_RANGE, 0, 2, NULL },
96 { "generic-dev", &cdda_dflts.generic_dev, CONF_TYPE_STRING, 0, 0, 0, NULL },
97 { "sector-size", &cdda_dflts.sector_size, CONF_TYPE_INT, M_OPT_RANGE,1,100, NULL },
98 { "overlap", &cdda_dflts.search_overlap, CONF_TYPE_INT, M_OPT_RANGE,0,75, NULL },
99 { "toc-bias", &cdda_dflts.toc_bias, CONF_TYPE_INT, 0, 0, 0, NULL },
100 { "toc-offset", &cdda_dflts.toc_offset, CONF_TYPE_INT, 0, 0, 0, NULL },
101 { "noskip", &cdda_dflts.no_skip, CONF_TYPE_FLAG, 0 , 0, 1, NULL },
102 { "skip", &cdda_dflts.no_skip, CONF_TYPE_FLAG, 0 , 1, 0, NULL },
103 { "device", &cdda_dflts.device, CONF_TYPE_STRING, 0, 0, 0, NULL },
104 { "span", &cdda_dflts.span, CONF_TYPE_OBJ_PARAMS, 0, 0, 0, &m_span_params_def },
105 {NULL, NULL, 0, 0, 0, 0, NULL}
108 int cdd_identify(const char *dev);
109 int cddb_resolve(const char *dev, char **xmcd_file);
110 cd_info_t* cddb_parse_xmcd(char *xmcd_file);
112 static int seek(stream_t* s,off_t pos);
113 static int fill_buffer(stream_t* s, char* buffer, int max_len);
114 static void close_cdda(stream_t* s);
116 static int get_track_by_sector(cdda_priv *p, unsigned int sector) {
117 int i;
118 for (i = p->cd->tracks; i >= 0 ; --i)
119 if (p->cd->disc_toc[i].dwStartSector <= sector)
120 break;
121 return i;
124 static int control(stream_t *stream, int cmd, void *arg) {
125 cdda_priv* p = stream->priv;
126 switch(cmd) {
127 case STREAM_CTRL_GET_NUM_CHAPTERS:
129 int start_track = get_track_by_sector(p, p->start_sector);
130 int end_track = get_track_by_sector(p, p->end_sector);
131 *(unsigned int *)arg = end_track + 1 - start_track;
132 return STREAM_OK;
134 case STREAM_CTRL_SEEK_TO_CHAPTER:
136 int r;
137 unsigned int track = *(unsigned int *)arg;
138 int start_track = get_track_by_sector(p, p->start_sector);
139 int seek_sector;
140 track += start_track;
141 if (track >= p->cd->tracks) {
142 stream->eof = 1;
143 return STREAM_ERROR;
145 seek_sector = track <= 0 ? p->start_sector
146 : p->cd->disc_toc[track].dwStartSector;
147 r = seek(stream, seek_sector * CD_FRAMESIZE_RAW);
148 if (r)
149 return STREAM_OK;
150 break;
152 case STREAM_CTRL_GET_CURRENT_CHAPTER:
154 int start_track = get_track_by_sector(p, p->start_sector);
155 int cur_track = get_track_by_sector(p, p->sector);
156 *(unsigned int *)arg = cur_track - start_track;
157 return STREAM_OK;
160 return STREAM_UNSUPPORTED;
163 static int open_cdda(stream_t *st,int m, void* opts, int* file_format) {
164 struct cdda_params* p = (struct cdda_params*)opts;
165 int mode = p->paranoia_mode;
166 int offset = p->toc_offset;
167 #ifndef CONFIG_LIBCDIO
168 cdrom_drive* cdd = NULL;
169 #else
170 cdrom_drive_t* cdd = NULL;
171 #endif
172 cdda_priv* priv;
173 cd_info_t *cd_info,*cddb_info = NULL;
174 unsigned int audiolen=0;
175 int last_track;
176 int i;
177 char *xmcd_file = NULL;
179 if(m != STREAM_READ) {
180 m_struct_free(&stream_opts,opts);
181 return STREAM_UNSUPPORTED;
184 if(!p->device) {
185 if (cdrom_device)
186 p->device = strdup(cdrom_device);
187 else
188 p->device = strdup(DEFAULT_CDROM_DEVICE);
191 #ifdef CONFIG_CDDB
192 // cdd_identify returns -1 if it cannot read the TOC,
193 // in which case there is no point in calling cddb_resolve
194 if(cdd_identify(p->device) >= 0 && strncmp(st->url,"cddb",4) == 0) {
195 i = cddb_resolve(p->device, &xmcd_file);
196 if(i == 0) {
197 cddb_info = cddb_parse_xmcd(xmcd_file);
198 free(xmcd_file);
201 #endif
203 #ifndef CONFIG_LIBCDIO
204 if(p->generic_dev)
205 cdd = cdda_identify_scsi(p->generic_dev,p->device,0,NULL);
206 else
207 #endif
208 #if defined(__NetBSD__)
209 cdd = cdda_identify_scsi(p->device,p->device,0,NULL);
210 #else
211 cdd = cdda_identify(p->device,0,NULL);
212 #endif
214 if(!cdd) {
215 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Can't open CDDA device.\n");
216 m_struct_free(&stream_opts,opts);
217 free(cddb_info);
218 return STREAM_ERROR;
221 cdda_verbose_set(cdd, CDDA_MESSAGE_FORGETIT, CDDA_MESSAGE_FORGETIT);
223 if(p->sector_size) {
224 cdd->nsectors = p->sector_size;
225 #ifndef CONFIG_LIBCDIO
226 cdd->bigbuff = p->sector_size * CD_FRAMESIZE_RAW;
227 #endif
230 if(cdda_open(cdd) != 0) {
231 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Can't open disc.\n");
232 cdda_close(cdd);
233 m_struct_free(&stream_opts,opts);
234 free(cddb_info);
235 return STREAM_ERROR;
238 cd_info = cd_info_new();
239 mp_tmsg(MSGT_OPEN,MSGL_INFO,"Found audio CD with %ld tracks.\n",cdda_tracks(cdd));
240 for(i=0;i<cdd->tracks;i++) {
241 char track_name[80];
242 long sec=cdda_track_firstsector(cdd,i+1);
243 long off=cdda_track_lastsector(cdd,i+1)-sec+1;
245 sprintf(track_name, "Track %d", i+1);
246 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 );
247 audiolen += off;
249 cd_info->min = (unsigned int)(audiolen/(60*75));
250 cd_info->sec = (unsigned int)((audiolen/75)%60);
251 cd_info->msec = (unsigned int)(audiolen%75);
253 priv = malloc(sizeof(cdda_priv));
254 memset(priv, 0, sizeof(cdda_priv));
255 priv->cd = cdd;
256 priv->cd_info = cd_info;
258 if(p->toc_bias)
259 offset -= cdda_track_firstsector(cdd,1);
261 if(offset) {
262 int i;
263 for(i = 0 ; i < cdd->tracks + 1 ; i++)
264 cdd->disc_toc[i].dwStartSector += offset;
267 if(p->speed)
268 cdda_speed_set(cdd,p->speed);
270 last_track = cdda_tracks(cdd);
271 if (p->span.start > last_track) p->span.start = last_track;
272 if (p->span.end < p->span.start) p->span.end = p->span.start;
273 if (p->span.end > last_track) p->span.end = last_track;
274 if(p->span.start)
275 priv->start_sector = cdda_track_firstsector(cdd,p->span.start);
276 else
277 priv->start_sector = cdda_disc_firstsector(cdd);
279 if(p->span.end) {
280 priv->end_sector = cdda_track_lastsector(cdd,p->span.end);
281 } else
282 priv->end_sector = cdda_disc_lastsector(cdd);
284 priv->cdp = paranoia_init(cdd);
285 if(priv->cdp == NULL) {
286 cdda_close(cdd);
287 free(priv);
288 cd_info_free(cd_info);
289 m_struct_free(&stream_opts,opts);
290 free(cddb_info);
291 return STREAM_ERROR;
294 if(mode == 0)
295 mode = PARANOIA_MODE_DISABLE;
296 else if(mode == 1)
297 mode = PARANOIA_MODE_OVERLAP;
298 else
299 mode = PARANOIA_MODE_FULL;
301 if(p->no_skip)
302 mode |= PARANOIA_MODE_NEVERSKIP;
303 #ifndef CONFIG_LIBCDIO
304 paranoia_modeset(cdd, mode);
306 if(p->search_overlap >= 0)
307 paranoia_overlapset(cdd,p->search_overlap);
308 #else
309 paranoia_modeset(priv->cdp, mode);
311 if(p->search_overlap >= 0)
312 paranoia_overlapset(priv->cdp,p->search_overlap);
313 #endif
315 paranoia_seek(priv->cdp,priv->start_sector,SEEK_SET);
316 priv->sector = priv->start_sector;
318 #ifdef CONFIG_CDDB
319 if(cddb_info) {
320 cd_info_free(cd_info);
321 priv->cd_info = cddb_info;
322 cd_info_debug( cddb_info );
324 #endif
326 st->priv = priv;
327 st->start_pos = priv->start_sector*CD_FRAMESIZE_RAW;
328 st->end_pos = (priv->end_sector + 1) * CD_FRAMESIZE_RAW;
329 st->type = STREAMTYPE_CDDA;
330 st->sector_size = CD_FRAMESIZE_RAW;
332 st->fill_buffer = fill_buffer;
333 st->seek = seek;
334 st->control = control;
335 st->close = close_cdda;
337 *file_format = DEMUXER_TYPE_RAWAUDIO;
339 m_struct_free(&stream_opts,opts);
341 return STREAM_OK;
344 #ifndef CONFIG_LIBCDIO
345 static void cdparanoia_callback(long inpos, int function) {
346 #else
347 static void cdparanoia_callback(long int inpos, paranoia_cb_mode_t function) {
348 #endif
351 static int fill_buffer(stream_t* s, char* buffer, int max_len) {
352 cdda_priv* p = (cdda_priv*)s->priv;
353 cd_track_t *cd_track;
354 int16_t * buf;
355 int i;
357 if((p->sector < p->start_sector) || (p->sector > p->end_sector)) {
358 s->eof = 1;
359 return 0;
362 buf = paranoia_read(p->cdp,cdparanoia_callback);
363 if (!buf)
364 return 0;
366 #if HAVE_BIGENDIAN
367 for(i=0;i<CD_FRAMESIZE_RAW/2;i++)
368 buf[i]=le2me_16(buf[i]);
369 #endif
371 p->sector++;
372 memcpy(buffer,buf,CD_FRAMESIZE_RAW);
374 for(i=0;i<p->cd->tracks;i++){
375 if(p->cd->disc_toc[i].dwStartSector==p->sector-1) {
376 cd_track = cd_info_get_track(p->cd_info, i+1);
377 //printf("Track %d, sector=%d\n", i, p->sector-1);
378 if( cd_track!=NULL ) {
379 mp_msg(MSGT_SEEK, MSGL_INFO, "\n%s\n", cd_track->name);
380 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDA_TRACK=%d\n", cd_track->track_nb);
382 break;
387 return CD_FRAMESIZE_RAW;
390 static int seek(stream_t* s,off_t newpos) {
391 cdda_priv* p = (cdda_priv*)s->priv;
392 cd_track_t *cd_track;
393 int sec;
394 int current_track=0, seeked_track=0;
395 int seek_to_track = 0;
396 int i;
398 s->pos = newpos;
399 sec = s->pos/CD_FRAMESIZE_RAW;
400 if (s->pos < 0 || sec > p->end_sector) {
401 s->eof = 1;
402 return 0;
405 //printf("pos: %d, sec: %d ## %d\n", (int)s->pos, (int)sec, CD_FRAMESIZE_RAW);
406 //printf("sector: %d new: %d\n", p->sector, sec );
408 for(i=0;i<p->cd->tracks;i++){
409 // printf("trk #%d: %d .. %d\n",i,p->cd->disc_toc[i].dwStartSector,p->cd->disc_toc[i+1].dwStartSector);
410 if( p->sector>=p->cd->disc_toc[i].dwStartSector && p->sector<p->cd->disc_toc[i+1].dwStartSector ) {
411 current_track = i;
413 if( sec>=p->cd->disc_toc[i].dwStartSector && sec<p->cd->disc_toc[i+1].dwStartSector ) {
414 seeked_track = i;
415 seek_to_track = sec == p->cd->disc_toc[i].dwStartSector;
418 //printf("current: %d, seeked: %d\n", current_track, seeked_track);
419 if (current_track != seeked_track && !seek_to_track) {
420 //printf("Track %d, sector=%d\n", seeked_track, sec);
421 cd_track = cd_info_get_track(p->cd_info, seeked_track+1);
422 if( cd_track!=NULL ) {
423 mp_msg(MSGT_SEEK, MSGL_INFO, "\n%s\n", cd_track->name);
424 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDA_TRACK=%d\n", cd_track->track_nb);
428 #if 0
429 if(sec < p->start_sector)
430 sec = p->start_sector;
431 else if(sec > p->end_sector)
432 sec = p->end_sector;
433 #endif
435 p->sector = sec;
436 // s->pos = sec*CD_FRAMESIZE_RAW;
438 //printf("seek: %d, sec: %d\n", (int)s->pos, sec);
439 paranoia_seek(p->cdp,sec,SEEK_SET);
440 return 1;
443 static void close_cdda(stream_t* s) {
444 cdda_priv* p = (cdda_priv*)s->priv;
445 paranoia_free(p->cdp);
446 cdda_close(p->cd);
447 cd_info_free(p->cd_info);
448 free(p);
451 const stream_info_t stream_info_cdda = {
452 "CDDA",
453 "cdda",
454 "Albeu",
456 open_cdda,
457 { "cdda",
458 #ifdef CONFIG_CDDB
459 "cddb",
460 #endif
461 NULL },
462 &stream_opts,
463 1 // Urls are an option string