libcdio.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Anton Khirnov <anton@khirnov.net>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #include <cdio/cdda.h>
27 #include <cdio/paranoia.h>
28 
29 #include "libavutil/log.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 
33 #include "libavformat/avformat.h"
34 #include "libavformat/internal.h"
35 
36 /* cdio returns some malloced strings that need to be free()d */
37 #undef free
38 
39 typedef struct CDIOContext {
40  cdrom_drive_t *drive;
41  cdrom_paranoia_t *paranoia;
42  int32_t last_sector;
43 
44  /* private options */
45  int speed;
47 } CDIOContext;
48 
50 {
51  CDIOContext *s = ctx->priv_data;
52  AVStream *st;
53  int ret, i;
54  char *err = NULL;
55 
56  if (!(st = avformat_new_stream(ctx, NULL)))
57  return AVERROR(ENOMEM);
58  s->drive = cdio_cddap_identify(ctx->filename, CDDA_MESSAGE_LOGIT, &err);
59  if (!s->drive) {
60  av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->filename);
61  return AVERROR(EINVAL);
62  }
63  if (err) {
64  av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
65  free(err);
66  }
67  if ((ret = cdio_cddap_open(s->drive)) < 0 || !s->drive->opened) {
68  av_log(ctx, AV_LOG_ERROR, "Could not open disk in drive %s.\n", ctx->filename);
69  return AVERROR(EINVAL);
70  }
71 
72  cdio_cddap_verbose_set(s->drive, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);
73  if (s->speed)
74  cdio_cddap_speed_set(s->drive, s->speed);
75 
76  s->paranoia = cdio_paranoia_init(s->drive);
77  if (!s->paranoia) {
78  av_log(ctx, AV_LOG_ERROR, "Could not init paranoia.\n");
79  return AVERROR(EINVAL);
80  }
81  cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);
82 
84  if (s->drive->bigendianp)
86  else
88  st->codec->sample_rate = 44100;
89  st->codec->channels = 2;
90  if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
91  s->drive->audio_first_sector != CDIO_INVALID_LSN)
92  st->duration = s->drive->audio_last_sector - s->drive->audio_first_sector;
93  else if (s->drive->tracks)
94  st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
95  avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2*st->codec->channels*st->codec->sample_rate);
96 
97  for (i = 0; i < s->drive->tracks; i++) {
98  char title[16];
99  snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
100  avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
101  s->drive->disc_toc[i+1].dwStartSector, title);
102  }
103 
104  s->last_sector = cdio_cddap_disc_lastsector(s->drive);
105 
106  return 0;
107 }
108 
109 static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
110 {
111  CDIOContext *s = ctx->priv_data;
112  int ret;
113  uint16_t *buf;
114  char *err = NULL;
115 
116  if (ctx->streams[0]->cur_dts > s->last_sector)
117  return AVERROR_EOF;
118 
119  buf = cdio_paranoia_read(s->paranoia, NULL);
120  if (!buf)
121  return AVERROR_EOF;
122 
123  if (err = cdio_cddap_errors(s->drive)) {
124  av_log(ctx, AV_LOG_ERROR, "%s\n", err);
125  free(err);
126  err = NULL;
127  }
128  if (err = cdio_cddap_messages(s->drive)) {
129  av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
130  free(err);
131  err = NULL;
132  }
133 
134  if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
135  return ret;
136  memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
137  return 0;
138 }
139 
141 {
142  CDIOContext *s = ctx->priv_data;
143  cdio_paranoia_free(s->paranoia);
144  cdio_cddap_close(s->drive);
145  return 0;
146 }
147 
148 static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
149  int flags)
150 {
151  CDIOContext *s = ctx->priv_data;
152  AVStream *st = ctx->streams[0];
153 
154  cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
155  st->cur_dts = timestamp;
156  return 0;
157 }
158 
159 #define OFFSET(x) offsetof(CDIOContext, x)
160 #define DEC AV_OPT_FLAG_DECODING_PARAM
161 static const AVOption options[] = {
162  { "speed", "Drive reading speed.", OFFSET(speed), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, DEC },
163  { "paranoia_mode", "Error recovery mode.", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { 0 }, INT_MIN, INT_MAX, DEC, "paranoia_mode" },
164  { "verify", "Verify data integrity in overlap area", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_VERIFY }, 0, 0, DEC, "paranoia_mode" },
165  { "overlap", "Perform overlapped reads.", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_OVERLAP }, 0, 0, DEC, "paranoia_mode" },
166  { "neverskip", "Do not skip failed reads.", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, "paranoia_mode" },
167  { NULL },
168 };
169 
170 static const AVClass libcdio_class = {
171  .class_name = "libcdio indev",
172  .item_name = av_default_item_name,
173  .option = options,
174  .version = LIBAVUTIL_VERSION_INT,
175 };
176 
178  .name = "libcdio",
179  .read_header = read_header,
180  .read_packet = read_packet,
181  .read_close = read_close,
182  .read_seek = read_seek,
183  .priv_data_size = sizeof(CDIOContext),
184  .flags = AVFMT_NOFILE,
185  .priv_class = &libcdio_class,
186 };
cdrom_drive_t * drive
Definition: libcdio.c:40
struct CDIOContext CDIOContext
AVOption.
Definition: opt.h:244
memory handling functions
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:148
AVOptions.
#define free
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:2849
static av_cold int read_header(AVFormatContext *ctx, AVFormatParameters *ap)
Definition: libcdio.c:49
Format I/O context.
Definition: avformat.h:863
int64_t cur_dts
Definition: avformat.h:797
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
#define OFFSET(x)
Definition: libcdio.c:159
#define av_cold
Definition: attributes.h:71
AVStream ** streams
Definition: avformat.h:908
uint8_t * data
Definition: avcodec.h:908
static int flags
Definition: log.c:34
#define AVERROR_EOF
End of file.
Definition: error.h:51
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:140
int paranoia_mode
Definition: libcdio.c:46
int32_t last_sector
Definition: libcdio.c:42
#define AVERROR(e)
Definition: error.h:43
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
AVCodecContext * codec
codec context
Definition: avformat.h:623
#define AV_LOG_VERBOSE
Definition: log.h:120
char filename[1024]
input or output filename
Definition: avformat.h:910
LIBAVUTIL_VERSION_INT
Definition: eval.c:50
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1574
int sample_rate
samples per second
Definition: avcodec.h:1456
av_default_item_name
Definition: dnxhdenc.c:43
static const AVOption options[]
Definition: libcdio.c:161
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:109
Describe the class of an AVClass context structure.
Definition: log.h:33
int speed
Definition: libcdio.c:45
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:684
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:368
cdrom_paranoia_t * paranoia
Definition: libcdio.c:41
int channels
number of audio channels
Definition: avcodec.h:1457
void * priv_data
Format private data.
Definition: avformat.h:883
#define DEC
Definition: libcdio.c:160
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:652
AVInputFormat ff_libcdio_demuxer
Definition: libcdio.c:177
static const AVClass libcdio_class
Definition: libcdio.c:170
enum CodecID codec_id
Definition: avcodec.h:1575