2011-09-13 15:23:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Anton Khirnov <anton@khirnov.net>
|
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* This file is part of FFmpeg.
|
2011-09-13 15:23:06 +00:00
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2011-09-13 15:23:06 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2011-09-13 15:23:06 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2013-08-15 21:12:51 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2011-09-13 15:23:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* libcdio CD grabbing
|
|
|
|
*/
|
|
|
|
|
2013-01-17 10:24:01 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#if HAVE_CDIO_PARANOIA_H
|
2011-09-13 15:23:06 +00:00
|
|
|
#include <cdio/cdda.h>
|
|
|
|
#include <cdio/paranoia.h>
|
2013-01-17 10:24:01 +00:00
|
|
|
#elif HAVE_CDIO_PARANOIA_PARANOIA_H
|
|
|
|
#include <cdio/paranoia/cdda.h>
|
|
|
|
#include <cdio/paranoia/paranoia.h>
|
|
|
|
#endif
|
2011-09-13 15:23:06 +00:00
|
|
|
|
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "libavutil/mem.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
|
|
|
#include "libavformat/avformat.h"
|
|
|
|
#include "libavformat/internal.h"
|
|
|
|
|
|
|
|
typedef struct CDIOContext {
|
2011-10-03 13:40:40 +00:00
|
|
|
const AVClass *class;
|
2011-09-13 15:23:06 +00:00
|
|
|
cdrom_drive_t *drive;
|
|
|
|
cdrom_paranoia_t *paranoia;
|
|
|
|
int32_t last_sector;
|
|
|
|
|
|
|
|
/* private options */
|
|
|
|
int speed;
|
|
|
|
int paranoia_mode;
|
|
|
|
} CDIOContext;
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static av_cold int read_header(AVFormatContext *ctx)
|
2011-09-13 15:23:06 +00:00
|
|
|
{
|
|
|
|
CDIOContext *s = ctx->priv_data;
|
|
|
|
AVStream *st;
|
|
|
|
int ret, i;
|
|
|
|
char *err = NULL;
|
|
|
|
|
2011-06-18 09:43:24 +00:00
|
|
|
if (!(st = avformat_new_stream(ctx, NULL)))
|
2011-09-13 15:23:06 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2017-12-29 22:29:52 +00:00
|
|
|
s->drive = cdio_cddap_identify(ctx->url, CDDA_MESSAGE_LOGIT, &err);
|
2011-09-13 15:23:06 +00:00
|
|
|
if (!s->drive) {
|
2017-12-29 22:29:52 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->url);
|
2011-09-13 15:23:06 +00:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
|
|
|
|
free(err);
|
|
|
|
}
|
|
|
|
if ((ret = cdio_cddap_open(s->drive)) < 0 || !s->drive->opened) {
|
2017-12-29 22:29:52 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Could not open disk in drive %s.\n", ctx->url);
|
2011-09-13 15:23:06 +00:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
cdio_cddap_verbose_set(s->drive, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);
|
|
|
|
if (s->speed)
|
|
|
|
cdio_cddap_speed_set(s->drive, s->speed);
|
|
|
|
|
|
|
|
s->paranoia = cdio_paranoia_init(s->drive);
|
|
|
|
if (!s->paranoia) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Could not init paranoia.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
2011-09-13 15:23:06 +00:00
|
|
|
if (s->drive->bigendianp)
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
|
2011-09-13 15:23:06 +00:00
|
|
|
else
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
|
|
|
|
st->codecpar->sample_rate = 44100;
|
|
|
|
st->codecpar->channels = 2;
|
2011-09-13 15:23:06 +00:00
|
|
|
if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
|
|
|
|
s->drive->audio_first_sector != CDIO_INVALID_LSN)
|
|
|
|
st->duration = s->drive->audio_last_sector - s->drive->audio_first_sector;
|
|
|
|
else if (s->drive->tracks)
|
|
|
|
st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2 * st->codecpar->channels * st->codecpar->sample_rate);
|
2011-09-13 15:23:06 +00:00
|
|
|
|
|
|
|
for (i = 0; i < s->drive->tracks; i++) {
|
|
|
|
char title[16];
|
|
|
|
snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
|
2011-10-17 06:58:50 +00:00
|
|
|
avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
|
2011-09-13 15:23:06 +00:00
|
|
|
s->drive->disc_toc[i+1].dwStartSector, title);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->last_sector = cdio_cddap_disc_lastsector(s->drive);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
CDIOContext *s = ctx->priv_data;
|
|
|
|
int ret;
|
|
|
|
uint16_t *buf;
|
|
|
|
char *err = NULL;
|
|
|
|
|
|
|
|
if (ctx->streams[0]->cur_dts > s->last_sector)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
buf = cdio_paranoia_read(s->paranoia, NULL);
|
|
|
|
if (!buf)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
if (err = cdio_cddap_errors(s->drive)) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "%s\n", err);
|
|
|
|
free(err);
|
|
|
|
err = NULL;
|
|
|
|
}
|
|
|
|
if (err = cdio_cddap_messages(s->drive)) {
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
|
|
|
|
free(err);
|
|
|
|
err = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
|
|
|
|
return ret;
|
|
|
|
memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int read_close(AVFormatContext *ctx)
|
|
|
|
{
|
|
|
|
CDIOContext *s = ctx->priv_data;
|
|
|
|
cdio_paranoia_free(s->paranoia);
|
|
|
|
cdio_cddap_close(s->drive);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
CDIOContext *s = ctx->priv_data;
|
|
|
|
AVStream *st = ctx->streams[0];
|
|
|
|
|
|
|
|
cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
|
|
|
|
st->cur_dts = timestamp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(CDIOContext, x)
|
|
|
|
#define DEC AV_OPT_FLAG_DECODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
2015-01-30 17:16:07 +00:00
|
|
|
{ "speed", "set drive reading speed", OFFSET(speed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
|
2015-01-30 17:30:17 +00:00
|
|
|
{ "paranoia_mode", "set error recovery mode", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { .i64 = PARANOIA_MODE_DISABLE }, INT_MIN, INT_MAX, DEC, "paranoia_mode" },
|
|
|
|
{ "disable", "apply no fixups", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_DISABLE }, 0, 0, DEC, "paranoia_mode" },
|
2015-01-30 17:16:07 +00:00
|
|
|
{ "verify", "verify data integrity in overlap area", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_VERIFY }, 0, 0, DEC, "paranoia_mode" },
|
|
|
|
{ "overlap", "perform overlapped reads", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_OVERLAP }, 0, 0, DEC, "paranoia_mode" },
|
|
|
|
{ "neverskip", "do not skip failed reads", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, "paranoia_mode" },
|
2015-01-30 17:30:17 +00:00
|
|
|
{ "full", "apply all recovery modes", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_FULL }, 0, 0, DEC, "paranoia_mode" },
|
2011-09-13 15:23:06 +00:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass libcdio_class = {
|
|
|
|
.class_name = "libcdio indev",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
2014-02-22 22:32:51 +00:00
|
|
|
.category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
|
2011-09-13 15:23:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AVInputFormat ff_libcdio_demuxer = {
|
|
|
|
.name = "libcdio",
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
|
|
|
.read_close = read_close,
|
|
|
|
.read_seek = read_seek,
|
|
|
|
.priv_data_size = sizeof(CDIOContext),
|
|
|
|
.flags = AVFMT_NOFILE,
|
|
|
|
.priv_class = &libcdio_class,
|
|
|
|
};
|