• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavdevice/libcdio.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Anton Khirnov <anton@khirnov.net>
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 /* cdio returns some malloced strings that need to be free()d */
00037 #undef free
00038 
00039 typedef struct CDIOContext {
00040     const AVClass       *class;
00041     cdrom_drive_t       *drive;
00042     cdrom_paranoia_t *paranoia;
00043     int32_t last_sector;
00044 
00045     /* private options */
00046     int speed;
00047     int paranoia_mode;
00048 } CDIOContext;
00049 
00050 static av_cold int read_header(AVFormatContext *ctx, AVFormatParameters *ap)
00051 {
00052     CDIOContext *s = ctx->priv_data;
00053     AVStream *st;
00054     int ret, i;
00055     char *err = NULL;
00056 
00057     if (!(st = avformat_new_stream(ctx, NULL)))
00058         return AVERROR(ENOMEM);
00059     s->drive = cdio_cddap_identify(ctx->filename, CDDA_MESSAGE_LOGIT, &err);
00060     if (!s->drive) {
00061         av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->filename);
00062         return AVERROR(EINVAL);
00063     }
00064     if (err) {
00065         av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
00066         free(err);
00067     }
00068     if ((ret = cdio_cddap_open(s->drive)) < 0 || !s->drive->opened) {
00069         av_log(ctx, AV_LOG_ERROR, "Could not open disk in drive %s.\n", ctx->filename);
00070         return AVERROR(EINVAL);
00071     }
00072 
00073     cdio_cddap_verbose_set(s->drive, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);
00074     if (s->speed)
00075         cdio_cddap_speed_set(s->drive, s->speed);
00076 
00077     s->paranoia = cdio_paranoia_init(s->drive);
00078     if (!s->paranoia) {
00079         av_log(ctx, AV_LOG_ERROR, "Could not init paranoia.\n");
00080         return AVERROR(EINVAL);
00081     }
00082     cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);
00083 
00084     st->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
00085     if (s->drive->bigendianp)
00086         st->codec->codec_id    = CODEC_ID_PCM_S16BE;
00087     else
00088         st->codec->codec_id    = CODEC_ID_PCM_S16LE;
00089     st->codec->sample_rate     = 44100;
00090     st->codec->channels        = 2;
00091     if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
00092         s->drive->audio_first_sector != CDIO_INVALID_LSN)
00093         st->duration           = s->drive->audio_last_sector - s->drive->audio_first_sector;
00094     else if (s->drive->tracks)
00095         st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
00096     avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2*st->codec->channels*st->codec->sample_rate);
00097 
00098     for (i = 0; i < s->drive->tracks; i++) {
00099         char title[16];
00100         snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
00101         avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
00102                        s->drive->disc_toc[i+1].dwStartSector, title);
00103     }
00104 
00105     s->last_sector = cdio_cddap_disc_lastsector(s->drive);
00106 
00107     return 0;
00108 }
00109 
00110 static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
00111 {
00112     CDIOContext *s = ctx->priv_data;
00113     int ret;
00114     uint16_t *buf;
00115     char *err = NULL;
00116 
00117     if (ctx->streams[0]->cur_dts > s->last_sector)
00118         return AVERROR_EOF;
00119 
00120     buf = cdio_paranoia_read(s->paranoia, NULL);
00121     if (!buf)
00122         return AVERROR_EOF;
00123 
00124     if (err = cdio_cddap_errors(s->drive)) {
00125         av_log(ctx, AV_LOG_ERROR, "%s\n", err);
00126         free(err);
00127         err = NULL;
00128     }
00129     if (err = cdio_cddap_messages(s->drive)) {
00130         av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
00131         free(err);
00132         err = NULL;
00133     }
00134 
00135     if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
00136         return ret;
00137     memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
00138     return 0;
00139 }
00140 
00141 static av_cold int read_close(AVFormatContext *ctx)
00142 {
00143     CDIOContext *s = ctx->priv_data;
00144     cdio_paranoia_free(s->paranoia);
00145     cdio_cddap_close(s->drive);
00146     return 0;
00147 }
00148 
00149 static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
00150                      int flags)
00151 {
00152     CDIOContext *s = ctx->priv_data;
00153     AVStream *st = ctx->streams[0];
00154 
00155     cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
00156     st->cur_dts = timestamp;
00157     return 0;
00158 }
00159 
00160 #define OFFSET(x) offsetof(CDIOContext, x)
00161 #define DEC AV_OPT_FLAG_DECODING_PARAM
00162 static const AVOption options[] = {
00163     { "speed",              "Drive reading speed.", OFFSET(speed),         AV_OPT_TYPE_INT,   { 0 }, 0,       INT_MAX, DEC },
00164     { "paranoia_mode",      "Error recovery mode.", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { 0 }, INT_MIN, INT_MAX, DEC, "paranoia_mode" },
00165         { "verify",         "Verify data integrity in overlap area", 0,    AV_OPT_TYPE_CONST, { PARANOIA_MODE_VERIFY },    0, 0, DEC, "paranoia_mode" },
00166         { "overlap",        "Perform overlapped reads.",             0,    AV_OPT_TYPE_CONST, { PARANOIA_MODE_OVERLAP },   0, 0, DEC, "paranoia_mode" },
00167         { "neverskip",      "Do not skip failed reads.",             0,    AV_OPT_TYPE_CONST, { PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, "paranoia_mode" },
00168     { NULL },
00169 };
00170 
00171 static const AVClass libcdio_class = {
00172     .class_name = "libcdio indev",
00173     .item_name  = av_default_item_name,
00174     .option     = options,
00175     .version    = LIBAVUTIL_VERSION_INT,
00176 };
00177 
00178 AVInputFormat ff_libcdio_demuxer = {
00179     .name           = "libcdio",
00180     .read_header    = read_header,
00181     .read_packet    = read_packet,
00182     .read_close     = read_close,
00183     .read_seek      = read_seek,
00184     .priv_data_size = sizeof(CDIOContext),
00185     .flags          = AVFMT_NOFILE,
00186     .priv_class     = &libcdio_class,
00187 };
Generated on Fri Feb 1 2013 14:34:48 for FFmpeg by doxygen 1.7.1