00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include <cdio/cdda.h>
00027 #include <cdio/paranoia.h>
00028
00029 #include "libavutil/log.h"
00030 #include "libavutil/mem.h"
00031 #include "libavutil/opt.h"
00032
00033 #include "libavformat/avformat.h"
00034 #include "libavformat/internal.h"
00035
00036
00037 #undef free
00038
00039 typedef struct CDIOContext {
00040 cdrom_drive_t *drive;
00041 cdrom_paranoia_t *paranoia;
00042 int32_t last_sector;
00043
00044
00045 int speed;
00046 int paranoia_mode;
00047 } CDIOContext;
00048
00049 static av_cold int read_header(AVFormatContext *ctx, AVFormatParameters *ap)
00050 {
00051 CDIOContext *s = ctx->priv_data;
00052 AVStream *st;
00053 int ret, i;
00054 char *err = NULL;
00055
00056 if (!(st = avformat_new_stream(ctx, NULL)))
00057 return AVERROR(ENOMEM);
00058 s->drive = cdio_cddap_identify(ctx->filename, CDDA_MESSAGE_LOGIT, &err);
00059 if (!s->drive) {
00060 av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->filename);
00061 return AVERROR(EINVAL);
00062 }
00063 if (err) {
00064 av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
00065 free(err);
00066 }
00067 if ((ret = cdio_cddap_open(s->drive)) < 0 || !s->drive->opened) {
00068 av_log(ctx, AV_LOG_ERROR, "Could not open disk in drive %s.\n", ctx->filename);
00069 return AVERROR(EINVAL);
00070 }
00071
00072 cdio_cddap_verbose_set(s->drive, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);
00073 if (s->speed)
00074 cdio_cddap_speed_set(s->drive, s->speed);
00075
00076 s->paranoia = cdio_paranoia_init(s->drive);
00077 if (!s->paranoia) {
00078 av_log(ctx, AV_LOG_ERROR, "Could not init paranoia.\n");
00079 return AVERROR(EINVAL);
00080 }
00081 cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);
00082
00083 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00084 if (s->drive->bigendianp)
00085 st->codec->codec_id = CODEC_ID_PCM_S16BE;
00086 else
00087 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00088 st->codec->sample_rate = 44100;
00089 st->codec->channels = 2;
00090 if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
00091 s->drive->audio_first_sector != CDIO_INVALID_LSN)
00092 st->duration = s->drive->audio_last_sector - s->drive->audio_first_sector;
00093 else if (s->drive->tracks)
00094 st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
00095 avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2*st->codec->channels*st->codec->sample_rate);
00096
00097 for (i = 0; i < s->drive->tracks; i++) {
00098 char title[16];
00099 snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
00100 avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
00101 s->drive->disc_toc[i+1].dwStartSector, title);
00102 }
00103
00104 s->last_sector = cdio_cddap_disc_lastsector(s->drive);
00105
00106 return 0;
00107 }
00108
00109 static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
00110 {
00111 CDIOContext *s = ctx->priv_data;
00112 int ret;
00113 uint16_t *buf;
00114 char *err = NULL;
00115
00116 if (ctx->streams[0]->cur_dts > s->last_sector)
00117 return AVERROR_EOF;
00118
00119 buf = cdio_paranoia_read(s->paranoia, NULL);
00120 if (!buf)
00121 return AVERROR_EOF;
00122
00123 if (err = cdio_cddap_errors(s->drive)) {
00124 av_log(ctx, AV_LOG_ERROR, "%s\n", err);
00125 free(err);
00126 err = NULL;
00127 }
00128 if (err = cdio_cddap_messages(s->drive)) {
00129 av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
00130 free(err);
00131 err = NULL;
00132 }
00133
00134 if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
00135 return ret;
00136 memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
00137 return 0;
00138 }
00139
00140 static av_cold int read_close(AVFormatContext *ctx)
00141 {
00142 CDIOContext *s = ctx->priv_data;
00143 cdio_paranoia_free(s->paranoia);
00144 cdio_cddap_close(s->drive);
00145 return 0;
00146 }
00147
00148 static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
00149 int flags)
00150 {
00151 CDIOContext *s = ctx->priv_data;
00152 AVStream *st = ctx->streams[0];
00153
00154 cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
00155 st->cur_dts = timestamp;
00156 return 0;
00157 }
00158
00159 #define OFFSET(x) offsetof(CDIOContext, x)
00160 #define DEC AV_OPT_FLAG_DECODING_PARAM
00161 static const AVOption options[] = {
00162 { "speed", "Drive reading speed.", OFFSET(speed), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, DEC },
00163 { "paranoia_mode", "Error recovery mode.", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { 0 }, INT_MIN, INT_MAX, DEC, "paranoia_mode" },
00164 { "verify", "Verify data integrity in overlap area", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_VERIFY }, 0, 0, DEC, "paranoia_mode" },
00165 { "overlap", "Perform overlapped reads.", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_OVERLAP }, 0, 0, DEC, "paranoia_mode" },
00166 { "neverskip", "Do not skip failed reads.", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, "paranoia_mode" },
00167 { NULL },
00168 };
00169
00170 static const AVClass libcdio_class = {
00171 .class_name = "libcdio indev",
00172 .item_name = av_default_item_name,
00173 .option = options,
00174 .version = LIBAVUTIL_VERSION_INT,
00175 };
00176
00177 AVInputFormat ff_libcdio_demuxer = {
00178 .name = "libcdio",
00179 .read_header = read_header,
00180 .read_packet = read_packet,
00181 .read_close = read_close,
00182 .read_seek = read_seek,
00183 .priv_data_size = sizeof(CDIOContext),
00184 .flags = AVFMT_NOFILE,
00185 .priv_class = &libcdio_class,
00186 };