docs: document --quvi-format
[mplayer.git] / stream / stream_cdda.c
blob0fcd3dbc9b29b2377a23530a6d5f993743ae7511
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 #include <cdio/cdda.h>
21 #include <cdio/paranoia.h>
22 #include <cdio/cdio.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdbool.h>
28 #include "talloc.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 extern char *cdrom_device;
43 typedef struct {
44 cdrom_drive_t *cd;
45 cdrom_paranoia_t *cdp;
46 int sector;
47 int start_sector;
48 int end_sector;
49 cd_info_t *cd_info;
50 } cdda_priv;
52 static struct cdda_params {
53 int speed;
54 int paranoia_mode;
55 char *generic_dev;
56 int sector_size;
57 int search_overlap;
58 int toc_bias;
59 int toc_offset;
60 int no_skip;
61 char *device;
62 m_span_t span;
63 } cdda_dflts = {
64 .search_overlap = -1,
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, 0, 100, NULL},
70 {"paranoia", ST_OFF(paranoia_mode), CONF_TYPE_INT, M_OPT_RANGE, 0, 2,
71 NULL},
72 {"generic-dev", ST_OFF(generic_dev), CONF_TYPE_STRING, 0, 0, 0, NULL},
73 {"sector-size", ST_OFF(sector_size), CONF_TYPE_INT, M_OPT_RANGE, 1, 100,
74 NULL},
75 {"overlap", ST_OFF(search_overlap), CONF_TYPE_INT, M_OPT_RANGE, -1, 75,
76 NULL},
77 {"toc-bias", ST_OFF(toc_bias), CONF_TYPE_INT, 0, 0, 0, NULL},
78 {"toc-offset", ST_OFF(toc_offset), CONF_TYPE_INT, 0, 0, 0, NULL},
79 {"noskip", ST_OFF(no_skip), CONF_TYPE_FLAG, 0, 0, 1, NULL},
80 {"skip", ST_OFF(no_skip), CONF_TYPE_FLAG, 0, 1, 0, NULL},
81 {"device", ST_OFF(device), CONF_TYPE_STRING, 0, 0, 0, NULL},
82 {"span", ST_OFF(span), CONF_TYPE_OBJ_PARAMS, 0, 0, 0,
83 (void *)&m_span_params_def},
84 /// For url parsing
85 {"hostname", ST_OFF(span), CONF_TYPE_OBJ_PARAMS, 0, 0, 0,
86 (void *)&m_span_params_def},
87 {"port", ST_OFF(speed), CONF_TYPE_INT, M_OPT_RANGE, 1, 100, NULL},
88 {"filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0, 0, NULL},
89 {0}
91 static const struct m_struct_st stream_opts = {
92 "cdda",
93 sizeof(struct cdda_params),
94 &cdda_dflts,
95 cdda_params_fields
98 /// We keep these options but now they set the defaults
99 const m_option_t cdda_opts[] = {
100 {"speed", &cdda_dflts.speed, CONF_TYPE_INT, M_OPT_RANGE, 1, 100, NULL},
101 {"paranoia", &cdda_dflts.paranoia_mode, CONF_TYPE_INT, M_OPT_RANGE, 0, 2,
102 NULL},
103 {"generic-dev", &cdda_dflts.generic_dev, CONF_TYPE_STRING, 0, 0, 0, NULL},
104 {"sector-size", &cdda_dflts.sector_size, CONF_TYPE_INT, M_OPT_RANGE, 1,
105 100, NULL},
106 {"overlap", &cdda_dflts.search_overlap, CONF_TYPE_INT, M_OPT_RANGE, 0, 75,
107 NULL},
108 {"toc-bias", &cdda_dflts.toc_bias, CONF_TYPE_INT, 0, 0, 0, NULL},
109 {"toc-offset", &cdda_dflts.toc_offset, CONF_TYPE_INT, 0, 0, 0, NULL},
110 {"noskip", &cdda_dflts.no_skip, CONF_TYPE_FLAG, 0, 0, 1, NULL},
111 {"skip", &cdda_dflts.no_skip, CONF_TYPE_FLAG, 0, 1, 0, NULL},
112 {"device", &cdda_dflts.device, CONF_TYPE_STRING, 0, 0, 0, NULL},
113 {"span", &cdda_dflts.span, CONF_TYPE_OBJ_PARAMS, 0, 0, 0,
114 (void *)&m_span_params_def},
115 {NULL, NULL, 0, 0, 0, 0, NULL}
118 static const char *cdtext_name[] = {
119 [CDTEXT_ARRANGER] = "Arranger",
120 [CDTEXT_COMPOSER] = "Composer",
121 [CDTEXT_MESSAGE] = "Message",
122 [CDTEXT_ISRC] = "ISRC",
123 [CDTEXT_PERFORMER] = "Performer",
124 [CDTEXT_SONGWRITER] = "Songwriter",
125 [CDTEXT_TITLE] = "Title",
126 [CDTEXT_UPC_EAN] = "UPC_EAN",
129 static bool print_cdtext(stream_t *s, int track)
131 cdda_priv* p = (cdda_priv*)s->priv;
132 cdtext_t *text = cdio_get_cdtext(p->cd->p_cdio, track);
133 if (text) {
134 mp_msg(MSGT_SEEK, MSGL_INFO, "CD-Text (%s):\n", track ? "track" : "CD");
135 for (int i = 0; i < sizeof(cdtext_name) / sizeof(cdtext_name[0]); i++) {
136 const char *name = cdtext_name[i];
137 const char *value = cdtext_get_const(i, text);
138 if (name && value)
139 mp_msg(MSGT_SEEK, MSGL_INFO, " %s: '%s'\n", name, value);
141 return true;
143 return false;
146 static void print_track_info(stream_t *s, int track)
148 cdda_priv* p = (cdda_priv*)s->priv;
149 cd_track_t *cd_track = cd_info_get_track(p->cd_info, track);
150 if( cd_track!=NULL ) {
151 mp_msg(MSGT_SEEK, MSGL_INFO, "\n%s\n", cd_track->name);
152 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDA_TRACK=%d\n",
153 cd_track->track_nb);
155 if (print_cdtext(s, track)) {
156 // hack for term OSD overwriting the last line of CDTEXT
157 mp_msg(MSGT_SEEK, MSGL_INFO, "\n");
161 static void cdparanoia_callback(long int inpos, paranoia_cb_mode_t function)
165 static int fill_buffer(stream_t *s, char *buffer, int max_len)
167 cdda_priv *p = (cdda_priv *)s->priv;
168 int16_t *buf;
169 int i;
171 if ((p->sector < p->start_sector) || (p->sector > p->end_sector)) {
172 s->eof = 1;
173 return 0;
176 buf = paranoia_read(p->cdp, cdparanoia_callback);
177 if (!buf)
178 return 0;
180 #if HAVE_BIGENDIAN
181 for (i = 0; i < CDIO_CD_FRAMESIZE_RAW / 2; i++)
182 buf[i] = le2me_16(buf[i]);
183 #endif
185 p->sector++;
186 memcpy(buffer, buf, CDIO_CD_FRAMESIZE_RAW);
188 for (i = 0; i < p->cd->tracks; i++) {
189 if (p->cd->disc_toc[i].dwStartSector == p->sector - 1) {
190 print_track_info(s, i + 1);
191 break;
195 return CDIO_CD_FRAMESIZE_RAW;
198 static int seek(stream_t *s, off_t newpos)
200 cdda_priv *p = (cdda_priv *)s->priv;
201 int sec;
202 int current_track = 0, seeked_track = 0;
203 int seek_to_track = 0;
204 int i;
206 s->pos = newpos;
207 sec = s->pos / CDIO_CD_FRAMESIZE_RAW;
208 if (s->pos < 0 || sec > p->end_sector) {
209 s->eof = 1;
210 p->sector = p->end_sector + 1;
211 return 0;
214 for (i = 0; i < p->cd->tracks; i++) {
215 if (p->sector >= p->cd->disc_toc[i].dwStartSector
216 && p->sector < p->cd->disc_toc[i + 1].dwStartSector)
217 current_track = i;
218 if (sec >= p->cd->disc_toc[i].dwStartSector
219 && sec < p->cd->disc_toc[i + 1].dwStartSector)
221 seeked_track = i;
222 seek_to_track = sec == p->cd->disc_toc[i].dwStartSector;
225 if (current_track != seeked_track && !seek_to_track)
226 print_track_info(s, seeked_track + 1);
228 p->sector = sec;
230 paranoia_seek(p->cdp, sec, SEEK_SET);
231 return 1;
234 static void close_cdda(stream_t *s)
236 cdda_priv *p = (cdda_priv *)s->priv;
237 paranoia_free(p->cdp);
238 cdda_close(p->cd);
239 cd_info_free(p->cd_info);
240 free(p);
243 static int get_track_by_sector(cdda_priv *p, unsigned int sector)
245 int i;
246 for (i = p->cd->tracks; i >= 0; --i)
247 if (p->cd->disc_toc[i].dwStartSector <= sector)
248 break;
249 return i;
252 static int control(stream_t *stream, int cmd, void *arg)
254 cdda_priv *p = stream->priv;
255 switch (cmd) {
256 case STREAM_CTRL_GET_NUM_CHAPTERS:
258 int start_track = get_track_by_sector(p, p->start_sector);
259 int end_track = get_track_by_sector(p, p->end_sector);
260 if (start_track == -1 || end_track == -1)
261 return STREAM_ERROR;
262 *(unsigned int *)arg = end_track + 1 - start_track;
263 return STREAM_OK;
265 case STREAM_CTRL_SEEK_TO_CHAPTER:
267 int r;
268 unsigned int track = *(unsigned int *)arg;
269 int start_track = get_track_by_sector(p, p->start_sector);
270 int end_track = get_track_by_sector(p, p->end_sector);
271 int seek_sector;
272 if (start_track == -1 || end_track == -1)
273 return STREAM_ERROR;
274 track += start_track;
275 if (track > end_track) {
276 seek(stream, (p->end_sector + 1) * CDIO_CD_FRAMESIZE_RAW);
277 // seeking beyond EOF should not be an error,
278 // the cache cannot handle changing stream pos and
279 // returning error.
280 return STREAM_OK;
282 seek_sector = track <= 0 ? p->start_sector
283 : p->cd->disc_toc[track].dwStartSector;
284 r = seek(stream, seek_sector * CDIO_CD_FRAMESIZE_RAW);
285 if (r)
286 return STREAM_OK;
287 break;
289 case STREAM_CTRL_GET_CURRENT_CHAPTER:
291 int start_track = get_track_by_sector(p, p->start_sector);
292 int cur_track = get_track_by_sector(p, p->sector);
293 if (start_track == -1 || cur_track == -1)
294 return STREAM_ERROR;
295 *(unsigned int *)arg = cur_track - start_track;
296 return STREAM_OK;
299 return STREAM_UNSUPPORTED;
302 static int open_cdda(stream_t *st, int m, void *opts, int *file_format)
304 struct cdda_params *p = (struct cdda_params *)opts;
305 int mode = p->paranoia_mode;
306 int offset = p->toc_offset;
307 cdrom_drive_t *cdd = NULL;
308 cdda_priv *priv;
309 cd_info_t *cd_info, *cddb_info = NULL;
310 unsigned int audiolen = 0;
311 int last_track;
312 int i;
313 char *xmcd_file = NULL;
315 if (m != STREAM_READ) {
316 m_struct_free(&stream_opts, opts);
317 return STREAM_UNSUPPORTED;
320 if (!p->device) {
321 if (cdrom_device)
322 p->device = talloc_strdup(NULL, cdrom_device);
323 else
324 p->device = talloc_strdup(NULL, DEFAULT_CDROM_DEVICE);
327 #ifdef CONFIG_CDDB
328 // cdd_identify returns -1 if it cannot read the TOC,
329 // in which case there is no point in calling cddb_resolve
330 if (cdd_identify(p->device) >= 0 && strncmp(st->url, "cddb", 4) == 0) {
331 i = cddb_resolve(p->device, &xmcd_file);
332 if (i == 0) {
333 cddb_info = cddb_parse_xmcd(xmcd_file);
334 free(xmcd_file);
337 #endif
339 #if defined(__NetBSD__)
340 cdd = cdda_identify_scsi(p->device, p->device, 0, NULL);
341 #else
342 cdd = cdda_identify(p->device, 0, NULL);
343 #endif
345 if (!cdd) {
346 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Can't open CDDA device.\n");
347 m_struct_free(&stream_opts, opts);
348 free(cddb_info);
349 return STREAM_ERROR;
352 cdda_verbose_set(cdd, CDDA_MESSAGE_FORGETIT, CDDA_MESSAGE_FORGETIT);
354 if (p->sector_size)
355 cdd->nsectors = p->sector_size;
357 if (cdda_open(cdd) != 0) {
358 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Can't open disc.\n");
359 cdda_close(cdd);
360 m_struct_free(&stream_opts, opts);
361 free(cddb_info);
362 return STREAM_ERROR;
365 cd_info = cd_info_new();
366 mp_tmsg(MSGT_OPEN, MSGL_INFO, "Found audio CD with %d tracks.\n",
367 (int)cdda_tracks(cdd));
368 for (i = 0; i < cdd->tracks; i++) {
369 char track_name[80];
370 long sec = cdda_track_firstsector(cdd, i + 1);
371 long off = cdda_track_lastsector(cdd, i + 1) - sec + 1;
373 sprintf(track_name, "Track %d", i + 1);
374 cd_info_add_track(cd_info, track_name, i + 1,
375 (unsigned int)(off / (60 * 75)),
376 (unsigned int)((off / 75) % 60),
377 (unsigned int)(off % 75), sec, off);
378 audiolen += off;
380 cd_info->min = (unsigned int)(audiolen / (60 * 75));
381 cd_info->sec = (unsigned int)((audiolen / 75) % 60);
382 cd_info->msec = (unsigned int)(audiolen % 75);
384 priv = malloc(sizeof(cdda_priv));
385 memset(priv, 0, sizeof(cdda_priv));
386 priv->cd = cdd;
387 priv->cd_info = cd_info;
389 if (p->toc_bias)
390 offset -= cdda_track_firstsector(cdd, 1);
392 if (offset) {
393 int i;
394 for (i = 0; i < cdd->tracks + 1; i++)
395 cdd->disc_toc[i].dwStartSector += offset;
398 if (p->speed > 0)
399 cdda_speed_set(cdd, p->speed);
401 last_track = cdda_tracks(cdd);
402 if (p->span.start > last_track)
403 p->span.start = last_track;
404 if (p->span.end < p->span.start)
405 p->span.end = p->span.start;
406 if (p->span.end > last_track)
407 p->span.end = last_track;
408 if (p->span.start)
409 priv->start_sector = cdda_track_firstsector(cdd, p->span.start);
410 else
411 priv->start_sector = cdda_disc_firstsector(cdd);
413 if (p->span.end)
414 priv->end_sector = cdda_track_lastsector(cdd, p->span.end);
415 else
416 priv->end_sector = cdda_disc_lastsector(cdd);
418 priv->cdp = paranoia_init(cdd);
419 if (priv->cdp == NULL) {
420 cdda_close(cdd);
421 free(priv);
422 cd_info_free(cd_info);
423 m_struct_free(&stream_opts, opts);
424 free(cddb_info);
425 return STREAM_ERROR;
428 if (mode == 0)
429 mode = PARANOIA_MODE_DISABLE;
430 else if (mode == 1)
431 mode = PARANOIA_MODE_OVERLAP;
432 else
433 mode = PARANOIA_MODE_FULL;
435 if (p->no_skip)
436 mode |= PARANOIA_MODE_NEVERSKIP;
437 else
438 mode &= ~PARANOIA_MODE_NEVERSKIP;
440 if (p->search_overlap > 0)
441 mode |= PARANOIA_MODE_OVERLAP;
442 else if (p->search_overlap == 0)
443 mode &= ~PARANOIA_MODE_OVERLAP;
445 paranoia_modeset(priv->cdp, mode);
447 if (p->search_overlap > 0)
448 paranoia_overlapset(priv->cdp, p->search_overlap);
450 paranoia_seek(priv->cdp, priv->start_sector, SEEK_SET);
451 priv->sector = priv->start_sector;
453 #ifdef CONFIG_CDDB
454 if (cddb_info) {
455 cd_info_free(cd_info);
456 priv->cd_info = cddb_info;
457 cd_info_debug(cddb_info);
459 #endif
461 st->priv = priv;
462 st->start_pos = priv->start_sector * CDIO_CD_FRAMESIZE_RAW;
463 st->end_pos = (priv->end_sector + 1) * CDIO_CD_FRAMESIZE_RAW;
464 st->type = STREAMTYPE_CDDA;
465 st->sector_size = CDIO_CD_FRAMESIZE_RAW;
467 st->fill_buffer = fill_buffer;
468 st->seek = seek;
469 st->control = control;
470 st->close = close_cdda;
472 *file_format = DEMUXER_TYPE_RAWAUDIO;
474 m_struct_free(&stream_opts, opts);
476 print_cdtext(st, 0);
478 return STREAM_OK;
481 const stream_info_t stream_info_cdda = {
482 "CDDA",
483 "cdda",
484 "Albeu",
486 open_cdda,
487 {"cdda",
488 #ifdef CONFIG_CDDB
489 "cddb",
490 #endif
491 NULL },
492 &stream_opts,
493 .opts_url = 1,