mpv/stream/stream_cdda.c

406 lines
10 KiB
C
Raw Normal View History

/*
* Original author: Albeu
*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <cdio/cdio.h>
#if CDIO_API_VERSION < 6
#define OLD_API
#endif
#ifdef OLD_API
#include <cdio/cdda.h>
#include <cdio/paranoia.h>
#else
#include <cdio/paranoia/cdda.h>
#include <cdio/paranoia/paranoia.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "mpv_talloc.h"
#include "stream.h"
#include "options/m_option.h"
#include "options/m_config.h"
#include "options/options.h"
#include "common/msg.h"
#include "config.h"
#if !HAVE_GPL
#error GPL only
#endif
typedef struct cdda_params {
2012-03-20 23:31:54 +00:00
cdrom_drive_t *cd;
cdrom_paranoia_t *cdp;
int sector;
int start_sector;
int end_sector;
// options
2012-03-20 23:31:54 +00:00
int speed;
int paranoia_mode;
int sector_size;
int search_overlap;
int toc_bias;
int toc_offset;
int skip;
2012-03-20 23:31:54 +00:00
char *device;
int span[2];
int cdtext;
} cdda_priv;
#define OPT_BASE_STRUCT struct cdda_params
const struct m_sub_options stream_cdda_conf = {
.opts = (const m_option_t[]) {
OPT_INTRANGE("speed", speed, 0, 1, 100),
OPT_INTRANGE("paranoia", paranoia_mode, 0, 0, 2),
OPT_INTRANGE("sector-size", sector_size, 0, 1, 100),
OPT_INTRANGE("overlap", search_overlap, 0, 0, 75),
OPT_INT("toc-bias", toc_bias, 0),
OPT_INT("toc-offset", toc_offset, 0),
OPT_FLAG("skip", skip, 0),
OPT_INTPAIR("span", span, 0),
OPT_FLAG("cdtext", cdtext, 0),
{0}
},
.size = sizeof(struct cdda_params),
.defaults = &(const struct cdda_params){
.search_overlap = -1,
.skip = 1,
},
};
static const char *const cdtext_name[] = {
#ifdef OLD_API
[CDTEXT_ARRANGER] = "Arranger",
[CDTEXT_COMPOSER] = "Composer",
[CDTEXT_MESSAGE] = "Message",
[CDTEXT_ISRC] = "ISRC",
[CDTEXT_PERFORMER] = "Performer",
[CDTEXT_SONGWRITER] = "Songwriter",
[CDTEXT_TITLE] = "Title",
[CDTEXT_UPC_EAN] = "UPC_EAN",
#else
[CDTEXT_FIELD_ARRANGER] = "Arranger",
[CDTEXT_FIELD_COMPOSER] = "Composer",
[CDTEXT_FIELD_MESSAGE] = "Message",
[CDTEXT_FIELD_ISRC] = "ISRC",
[CDTEXT_FIELD_PERFORMER] = "Performer",
[CDTEXT_FIELD_SONGWRITER] = "Songwriter",
[CDTEXT_FIELD_TITLE] = "Title",
[CDTEXT_FIELD_UPC_EAN] = "UPC_EAN",
#endif
};
static void print_cdtext(stream_t *s, int track)
{
cdda_priv* p = (cdda_priv*)s->priv;
if (!p->cdtext)
return;
#ifdef OLD_API
cdtext_t *text = cdio_get_cdtext(p->cd->p_cdio, track);
#else
cdtext_t *text = cdio_get_cdtext(p->cd->p_cdio);
#endif
int header = 0;
if (text) {
for (int i = 0; i < sizeof(cdtext_name) / sizeof(cdtext_name[0]); i++) {
const char *name = cdtext_name[i];
#ifdef OLD_API
const char *value = cdtext_get_const(i, text);
#else
const char *value = cdtext_get_const(text, i, track);
#endif
if (name && value) {
if (!header)
MP_INFO(s, "CD-Text (%s):\n", track ? "track" : "CD");
header = 1;
MP_INFO(s, " %s: '%s'\n", name, value);
}
}
}
}
static void print_track_info(stream_t *s, int track)
{
MP_INFO(s, "Switched to track %d\n", track);
print_cdtext(s, track);
}
2012-03-20 23:31:54 +00:00
static void cdparanoia_callback(long int inpos, paranoia_cb_mode_t function)
{
}
static int fill_buffer(stream_t *s, void *buffer, int max_len)
2012-03-20 23:31:54 +00:00
{
cdda_priv *p = (cdda_priv *)s->priv;
int16_t *buf;
int i;
if (max_len < CDIO_CD_FRAMESIZE_RAW)
return -1;
2012-03-20 23:31:54 +00:00
if ((p->sector < p->start_sector) || (p->sector > p->end_sector)) {
return 0;
}
2012-03-20 23:31:54 +00:00
buf = paranoia_read(p->cdp, cdparanoia_callback);
if (!buf)
return 0;
2012-03-20 23:31:54 +00:00
p->sector++;
memcpy(buffer, buf, CDIO_CD_FRAMESIZE_RAW);
2012-03-20 23:31:54 +00:00
for (i = 0; i < p->cd->tracks; i++) {
if (p->cd->disc_toc[i].dwStartSector == p->sector - 1) {
print_track_info(s, i + 1);
2012-03-20 23:31:54 +00:00
break;
}
}
return CDIO_CD_FRAMESIZE_RAW;
}
static int seek(stream_t *s, int64_t newpos)
2012-03-20 23:31:54 +00:00
{
cdda_priv *p = (cdda_priv *)s->priv;
int sec;
int current_track = 0, seeked_track = 0;
int seek_to_track = 0;
int i;
newpos += p->start_sector * CDIO_CD_FRAMESIZE_RAW;
sec = newpos / CDIO_CD_FRAMESIZE_RAW;
if (newpos < 0 || sec > p->end_sector) {
2012-03-20 23:31:54 +00:00
p->sector = p->end_sector + 1;
return 0;
}
for (i = 0; i < p->cd->tracks; i++) {
if (p->sector >= p->cd->disc_toc[i].dwStartSector
&& p->sector < p->cd->disc_toc[i + 1].dwStartSector)
current_track = i;
if (sec >= p->cd->disc_toc[i].dwStartSector
&& sec < p->cd->disc_toc[i + 1].dwStartSector)
{
seeked_track = i;
seek_to_track = sec == p->cd->disc_toc[i].dwStartSector;
}
}
if (current_track != seeked_track && !seek_to_track)
print_track_info(s, seeked_track + 1);
2012-03-20 23:31:54 +00:00
p->sector = sec;
2012-03-20 23:31:54 +00:00
paranoia_seek(p->cdp, sec, SEEK_SET);
return 1;
}
2012-03-20 23:31:54 +00:00
static void close_cdda(stream_t *s)
{
cdda_priv *p = (cdda_priv *)s->priv;
paranoia_free(p->cdp);
cdda_close(p->cd);
}
demux: restore some of the DVD/BD/CDDA interaction layers This partially reverts commit a9d83eac40c94f44d19fab7b6955331f10efe301 ("Remove optical disc fancification layers"). Mostly due to the timestamp crap, this was never really going to work. The playback layer is sensitive to timestamps, and derives the playback time directly from the low level packet timestamps. DVD/BD works differently, and libdvdnav/libbluray do not make it easy at all to compensate for this. Which is why it never worked well, but not doing it at all is even more awful. demux_disc.c tried this and rewrote packet timestamps from low level TS to playback time. So restore demux_disc.c, which should bring behavior back to the old often non-working but slightly better state. I did not revert anything that affects components above the demuxer layer. For example, the properties for switching DVD angles or listing disc titles are still gone. (Disc titles could be reimplemented as editions. But not by me.) This commit modifies the reverted code a bit; this can't be avoided, because the internal API changed quite a bit. The old seek resync in demux_lavf.c (which was a hack) is replaced with a hack. SEEK_FORCE and demux_params.external_stream are new additions. Some of this could/should be further cleaned up. If you don't want "proper" DVD/BD support to disappear, you should probably volunteer. Now why am I wasting my time for this? Just because some idiot users are too lazy to rip their ever-wearing out shitty physical discs? Then why should I not be lazy and drop support completely? They won't even be thankful for me maintaining this horrible garbage for no compensation.
2019-10-02 22:22:18 +00:00
static int get_track_by_sector(cdda_priv *p, unsigned int sector)
{
int i;
for (i = p->cd->tracks; i >= 0; --i)
if (p->cd->disc_toc[i].dwStartSector <= sector)
break;
return i;
}
2012-03-20 23:31:54 +00:00
static int control(stream_t *stream, int cmd, void *arg)
{
cdda_priv *p = stream->priv;
switch (cmd) {
demux: restore some of the DVD/BD/CDDA interaction layers This partially reverts commit a9d83eac40c94f44d19fab7b6955331f10efe301 ("Remove optical disc fancification layers"). Mostly due to the timestamp crap, this was never really going to work. The playback layer is sensitive to timestamps, and derives the playback time directly from the low level packet timestamps. DVD/BD works differently, and libdvdnav/libbluray do not make it easy at all to compensate for this. Which is why it never worked well, but not doing it at all is even more awful. demux_disc.c tried this and rewrote packet timestamps from low level TS to playback time. So restore demux_disc.c, which should bring behavior back to the old often non-working but slightly better state. I did not revert anything that affects components above the demuxer layer. For example, the properties for switching DVD angles or listing disc titles are still gone. (Disc titles could be reimplemented as editions. But not by me.) This commit modifies the reverted code a bit; this can't be avoided, because the internal API changed quite a bit. The old seek resync in demux_lavf.c (which was a hack) is replaced with a hack. SEEK_FORCE and demux_params.external_stream are new additions. Some of this could/should be further cleaned up. If you don't want "proper" DVD/BD support to disappear, you should probably volunteer. Now why am I wasting my time for this? Just because some idiot users are too lazy to rip their ever-wearing out shitty physical discs? Then why should I not be lazy and drop support completely? They won't even be thankful for me maintaining this horrible garbage for no compensation.
2019-10-02 22:22:18 +00:00
case STREAM_CTRL_GET_NUM_CHAPTERS:
{
int start_track = get_track_by_sector(p, p->start_sector);
int end_track = get_track_by_sector(p, p->end_sector);
if (start_track == -1 || end_track == -1)
return STREAM_ERROR;
*(unsigned int *)arg = end_track + 1 - start_track;
return STREAM_OK;
}
case STREAM_CTRL_GET_CHAPTER_TIME:
{
int track = *(double *)arg;
int start_track = get_track_by_sector(p, p->start_sector);
int end_track = get_track_by_sector(p, p->end_sector);
track += start_track;
if (track > end_track)
return STREAM_ERROR;
int64_t sector = p->cd->disc_toc[track].dwStartSector;
int64_t pos = sector * CDIO_CD_FRAMESIZE_RAW;
// Assume standard audio CD: 44.1khz, 2 channels, s16 samples
*(double *)arg = pos / (44100.0 * 2 * 2);
return STREAM_OK;
}
}
2012-03-20 23:31:54 +00:00
return STREAM_UNSUPPORTED;
}
static int64_t get_size(stream_t *st)
{
cdda_priv *p = st->priv;
return (p->end_sector + 1 - p->start_sector) * CDIO_CD_FRAMESIZE_RAW;
}
static int open_cdda(stream_t *st)
2012-03-20 23:31:54 +00:00
{
st->priv = mp_get_config_group(st, st->global, &stream_cdda_conf);
cdda_priv *priv = st->priv;
cdda_priv *p = priv;
2012-03-20 23:31:54 +00:00
int mode = p->paranoia_mode;
int offset = p->toc_offset;
cdrom_drive_t *cdd = NULL;
int last_track;
char *global_device;
mp_read_option_raw(st->global, "cdrom-device", &m_option_type_string,
&global_device);
talloc_steal(st, global_device);
if (st->path[0]) {
p->device = st->path;
} else if (global_device && global_device[0]) {
p->device = global_device;
} else {
p->device = DEFAULT_CDROM_DEVICE;
2012-03-20 23:31:54 +00:00
}
#if defined(__NetBSD__)
2012-03-20 23:31:54 +00:00
cdd = cdda_identify_scsi(p->device, p->device, 0, NULL);
#else
2012-03-20 23:31:54 +00:00
cdd = cdda_identify(p->device, 0, NULL);
#endif
2012-03-20 23:31:54 +00:00
if (!cdd) {
MP_ERR(st, "Can't open CDDA device.\n");
2012-03-20 23:31:54 +00:00
return STREAM_ERROR;
}
2012-03-20 23:31:54 +00:00
cdda_verbose_set(cdd, CDDA_MESSAGE_FORGETIT, CDDA_MESSAGE_FORGETIT);
if (p->sector_size)
2012-03-20 23:31:54 +00:00
cdd->nsectors = p->sector_size;
if (cdda_open(cdd) != 0) {
MP_ERR(st, "Can't open disc.\n");
2012-03-20 23:31:54 +00:00
cdda_close(cdd);
return STREAM_ERROR;
}
priv->cd = cdd;
if (p->toc_bias)
offset -= cdda_track_firstsector(cdd, 1);
if (offset) {
for (int n = 0; n < cdd->tracks + 1; n++)
cdd->disc_toc[n].dwStartSector += offset;
2012-03-20 23:31:54 +00:00
}
if (p->speed > 0)
cdda_speed_set(cdd, p->speed);
last_track = cdda_tracks(cdd);
if (p->span[0] > last_track)
p->span[0] = last_track;
if (p->span[1] < p->span[0])
p->span[1] = p->span[0];
if (p->span[1] > last_track)
p->span[1] = last_track;
if (p->span[0])
priv->start_sector = cdda_track_firstsector(cdd, p->span[0]);
2012-03-20 23:31:54 +00:00
else
priv->start_sector = cdda_disc_firstsector(cdd);
if (p->span[1])
priv->end_sector = cdda_track_lastsector(cdd, p->span[1]);
2012-03-20 23:31:54 +00:00
else
priv->end_sector = cdda_disc_lastsector(cdd);
priv->cdp = paranoia_init(cdd);
if (priv->cdp == NULL) {
cdda_close(cdd);
free(priv);
return STREAM_ERROR;
}
if (mode == 0)
mode = PARANOIA_MODE_DISABLE;
else if (mode == 1)
mode = PARANOIA_MODE_OVERLAP;
else
mode = PARANOIA_MODE_FULL;
if (p->skip)
2012-03-20 23:31:54 +00:00
mode &= ~PARANOIA_MODE_NEVERSKIP;
else
mode |= PARANOIA_MODE_NEVERSKIP;
2012-03-20 23:31:54 +00:00
if (p->search_overlap > 0)
mode |= PARANOIA_MODE_OVERLAP;
else if (p->search_overlap == 0)
mode &= ~PARANOIA_MODE_OVERLAP;
paranoia_modeset(priv->cdp, mode);
2012-03-20 23:31:54 +00:00
if (p->search_overlap > 0)
paranoia_overlapset(priv->cdp, p->search_overlap);
2012-03-20 23:31:54 +00:00
paranoia_seek(priv->cdp, priv->start_sector, SEEK_SET);
priv->sector = priv->start_sector;
2012-03-20 23:31:54 +00:00
st->priv = priv;
2012-03-20 23:31:54 +00:00
st->fill_buffer = fill_buffer;
st->seek = seek;
st->seekable = true;
2012-03-20 23:31:54 +00:00
st->control = control;
st->get_size = get_size;
2012-03-20 23:31:54 +00:00
st->close = close_cdda;
st->streaming = true;
st->demuxer = "+disc";
print_cdtext(st, 0);
2012-03-20 23:31:54 +00:00
return STREAM_OK;
}
const stream_info_t stream_info_cdda = {
.name = "cdda",
.open = open_cdda,
.protocols = (const char*const[]){"cdda", NULL },
stream, demux: redo origin policy thing mpv has a very weak and very annoying policy that determines whether a playlist should be used or not. For example, if you play a remote playlist, you usually don't want it to be able to read local filesystem entries. (Although for a media player the impact is small I guess.) It's weak and annoying as in that it does not prevent certain cases which could be interpreted as bad in some cases, such as allowing playlists on the local filesystem to reference remote URLs. It probably barely makes sense, but we just want to exclude some other "definitely not a good idea" things, all while playlists generally just work, so whatever. The policy is: - from the command line anything is played - local playlists can reference anything except "unsafe" streams ("unsafe" means special stream inputs like libavfilter graphs) - remote playlists can reference only remote URLs - things like "memory://" and archives are "transparent" to this This commit does... something. It replaces the weird stream flags with a slightly clearer "origin" value, which is now consequently passed down and used everywhere. It fixes some deviations from the described policy. I wanted to force archives to reference only content within them, but this would probably have been more complicated (or required different abstractions), and I'm too lazy to figure it out, so archives are now "transparent" (playlists within archives behave the same outside). There may be a lot of bugs in this. This is unfortunately a very noisy commit because: - every stream open call now needs to pass the origin - so does every demuxer open call (=> params param. gets mandatory) - most stream were changed to provide the "origin" value - the origin value needed to be passed along in a lot of places - I was too lazy to split the commit Fixes: #7274
2019-12-20 08:41:42 +00:00
.stream_origin = STREAM_ORIGIN_UNSAFE,
};