FFmpeg
Loading...
Searching...
No Matches
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 FFmpeg.
5 *
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
23 * libcdio CD grabbing
24 */
25
26#include "config.h"
27
28#if HAVE_CDIO_PARANOIA_H
29#include <cdio/cdda.h>
30#include <cdio/paranoia.h>
31#elif HAVE_CDIO_PARANOIA_PARANOIA_H
32#include <cdio/paranoia/cdda.h>
33#include <cdio/paranoia/paranoia.h>
34#endif
35
36#include "libavutil/log.h"
37#include "libavutil/opt.h"
38
40#include "libavformat/demux.h"
42
43typedef struct CDIOContext {
44 const AVClass *class;
45 cdrom_drive_t *drive;
46 cdrom_paranoia_t *paranoia;
48
49 /* private options */
50 int speed;
53
55{
56 CDIOContext *s = ctx->priv_data;
57 AVStream *st;
58 int ret, i;
59 char *err = NULL;
60
61 if (!(st = avformat_new_stream(ctx, NULL)))
62 return AVERROR(ENOMEM);
63 s->drive = cdio_cddap_identify(ctx->url, CDDA_MESSAGE_LOGIT, &err);
64 if (!s->drive) {
65 av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->url);
66 return AVERROR(EINVAL);
67 }
68 if (err) {
69 av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
70 free(err);
71 }
72 if ((ret = cdio_cddap_open(s->drive)) < 0 || !s->drive->opened) {
73 av_log(ctx, AV_LOG_ERROR, "Could not open disk in drive %s.\n", ctx->url);
74 return AVERROR(EINVAL);
75 }
76
77 cdio_cddap_verbose_set(s->drive, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);
78 if (s->speed)
79 cdio_cddap_speed_set(s->drive, s->speed);
80
81 s->paranoia = cdio_paranoia_init(s->drive);
82 if (!s->paranoia) {
83 av_log(ctx, AV_LOG_ERROR, "Could not init paranoia.\n");
84 return AVERROR(EINVAL);
85 }
86 cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);
87
89 if (s->drive->bigendianp)
91 else
93 st->codecpar->sample_rate = 44100;
95 if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
96 s->drive->audio_first_sector != CDIO_INVALID_LSN)
97 st->duration = s->drive->audio_last_sector - s->drive->audio_first_sector;
98 else if (s->drive->tracks)
99 st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
100 avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW,
102
103 for (i = 0; i < s->drive->tracks; i++) {
104 char title[16];
105 snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
106 avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
107 s->drive->disc_toc[i+1].dwStartSector, title);
108 }
109
110 s->last_sector = cdio_cddap_disc_lastsector(s->drive);
111
112 return 0;
113}
114
116{
117 CDIOContext *s = ctx->priv_data;
118 int ret;
119 uint16_t *buf;
120 char *err = NULL;
121
122 buf = cdio_paranoia_read(s->paranoia, NULL);
123 if (!buf)
124 return AVERROR_EOF;
125
126 if (err = cdio_cddap_errors(s->drive)) {
127 av_log(ctx, AV_LOG_ERROR, "%s\n", err);
128 free(err);
129 err = NULL;
130 }
131 if (err = cdio_cddap_messages(s->drive)) {
132 av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
133 free(err);
134 err = NULL;
135 }
136
137 if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
138 return ret;
139 memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
140 return 0;
141}
142
144{
145 CDIOContext *s = ctx->priv_data;
146 cdio_paranoia_free(s->paranoia);
147 cdio_cddap_close(s->drive);
148 return 0;
149}
150
151static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
152 int flags)
153{
154 CDIOContext *s = ctx->priv_data;
155 AVStream *st = ctx->streams[0];
156
157 cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
158 avpriv_update_cur_dts(ctx, st, timestamp);
159 return 0;
160}
161
162#define OFFSET(x) offsetof(CDIOContext, x)
163#define DEC AV_OPT_FLAG_DECODING_PARAM
164static const AVOption options[] = {
165 { "speed", "set drive reading speed", OFFSET(speed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
166 { "paranoia_mode", "set error recovery mode", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { .i64 = PARANOIA_MODE_DISABLE }, INT_MIN, INT_MAX, DEC, .unit = "paranoia_mode" },
167 { "disable", "apply no fixups", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_DISABLE }, 0, 0, DEC, .unit = "paranoia_mode" },
168 { "verify", "verify data integrity in overlap area", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_VERIFY }, 0, 0, DEC, .unit = "paranoia_mode" },
169 { "overlap", "perform overlapped reads", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_OVERLAP }, 0, 0, DEC, .unit = "paranoia_mode" },
170 { "neverskip", "do not skip failed reads", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, .unit = "paranoia_mode" },
171 { "full", "apply all recovery modes", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_FULL }, 0, 0, DEC, .unit = "paranoia_mode" },
172 { NULL },
173};
174
175static const AVClass libcdio_class = {
176 .class_name = "libcdio indev",
177 .item_name = av_default_item_name,
178 .option = options,
179 .version = LIBAVUTIL_VERSION_INT,
181};
182
184 .p.name = "libcdio",
185 .p.flags = AVFMT_NOFILE,
186 .p.priv_class = &libcdio_class,
187 .read_header = read_header,
188 .read_packet = read_packet,
189 .read_close = read_close,
190 .read_seek = read_seek,
191 .priv_data_size = sizeof(CDIOContext),
192};
const FFInputFormat ff_libcdio_demuxer
Definition libcdio.c:183
int32_t
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
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:488
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
AVChapter * avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition demux_utils.c:43
void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
Definition seek.c:37
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:331
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_STEREO
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define av_cold
Definition attributes.h:117
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition libcdio.c:115
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
static const AVClass libcdio_class
Definition libcdio.c:175
#define OFFSET(x)
Definition libcdio.c:162
static av_cold int read_header(AVFormatContext *ctx)
Definition libcdio.c:54
#define DEC
Definition librsvgdec.c:149
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
Definition log.h:44
AVOptions.
#define snprintf
Definition snprintf.h:34
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
int32_t last_sector
Definition libcdio.c:47
cdrom_paranoia_t * paranoia
Definition libcdio.c:46
cdrom_drive_t * drive
Definition libcdio.c:45
int paranoia_mode
Definition libcdio.c:51
int speed
Definition libcdio.c:50
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49