FFmpeg
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 
39 #include "libavformat/avformat.h"
40 #include "libavformat/internal.h"
41 
42 typedef struct CDIOContext {
43  const AVClass *class;
44  cdrom_drive_t *drive;
45  cdrom_paranoia_t *paranoia;
47 
48  /* private options */
49  int speed;
51 } CDIOContext;
52 
54 {
56  AVStream *st;
57  int ret, i;
58  char *err = NULL;
59 
60  if (!(st = avformat_new_stream(ctx, NULL)))
61  return AVERROR(ENOMEM);
62  s->drive = cdio_cddap_identify(ctx->url, CDDA_MESSAGE_LOGIT, &err);
63  if (!s->drive) {
64  av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->url);
65  return AVERROR(EINVAL);
66  }
67  if (err) {
68  av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
69  free(err);
70  }
71  if ((ret = cdio_cddap_open(s->drive)) < 0 || !s->drive->opened) {
72  av_log(ctx, AV_LOG_ERROR, "Could not open disk in drive %s.\n", ctx->url);
73  return AVERROR(EINVAL);
74  }
75 
76  cdio_cddap_verbose_set(s->drive, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);
77  if (s->speed)
78  cdio_cddap_speed_set(s->drive, s->speed);
79 
80  s->paranoia = cdio_paranoia_init(s->drive);
81  if (!s->paranoia) {
82  av_log(ctx, AV_LOG_ERROR, "Could not init paranoia.\n");
83  return AVERROR(EINVAL);
84  }
85  cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);
86 
88  if (s->drive->bigendianp)
90  else
92  st->codecpar->sample_rate = 44100;
93  st->codecpar->channels = 2;
94  if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
95  s->drive->audio_first_sector != CDIO_INVALID_LSN)
96  st->duration = s->drive->audio_last_sector - s->drive->audio_first_sector;
97  else if (s->drive->tracks)
98  st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
99  avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2 * st->codecpar->channels * st->codecpar->sample_rate);
100 
101  for (i = 0; i < s->drive->tracks; i++) {
102  char title[16];
103  snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
104  avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
105  s->drive->disc_toc[i+1].dwStartSector, title);
106  }
107 
108  s->last_sector = cdio_cddap_disc_lastsector(s->drive);
109 
110  return 0;
111 }
112 
114 {
116  int ret;
117  uint16_t *buf;
118  char *err = NULL;
119 
120  buf = cdio_paranoia_read(s->paranoia, NULL);
121  if (!buf)
122  return AVERROR_EOF;
123 
124  if (err = cdio_cddap_errors(s->drive)) {
125  av_log(ctx, AV_LOG_ERROR, "%s\n", err);
126  free(err);
127  err = NULL;
128  }
129  if (err = cdio_cddap_messages(s->drive)) {
130  av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
131  free(err);
132  err = NULL;
133  }
134 
135  if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
136  return ret;
137  memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
138  return 0;
139 }
140 
142 {
144  cdio_paranoia_free(s->paranoia);
145  cdio_cddap_close(s->drive);
146  return 0;
147 }
148 
149 static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
150  int flags)
151 {
153  AVStream *st = ctx->streams[0];
154 
155  cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
156  avpriv_update_cur_dts(ctx, st, timestamp);
157  return 0;
158 }
159 
160 #define OFFSET(x) offsetof(CDIOContext, x)
161 #define DEC AV_OPT_FLAG_DECODING_PARAM
162 static const AVOption options[] = {
163  { "speed", "set drive reading speed", OFFSET(speed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
164  { "paranoia_mode", "set error recovery mode", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { .i64 = PARANOIA_MODE_DISABLE }, INT_MIN, INT_MAX, DEC, "paranoia_mode" },
165  { "disable", "apply no fixups", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_DISABLE }, 0, 0, DEC, "paranoia_mode" },
166  { "verify", "verify data integrity in overlap area", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_VERIFY }, 0, 0, DEC, "paranoia_mode" },
167  { "overlap", "perform overlapped reads", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_OVERLAP }, 0, 0, DEC, "paranoia_mode" },
168  { "neverskip", "do not skip failed reads", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, "paranoia_mode" },
169  { "full", "apply all recovery modes", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_FULL }, 0, 0, DEC, "paranoia_mode" },
170  { NULL },
171 };
172 
173 static const AVClass libcdio_class = {
174  .class_name = "libcdio indev",
175  .item_name = av_default_item_name,
176  .option = options,
177  .version = LIBAVUTIL_VERSION_INT,
179 };
180 
182  .name = "libcdio",
183  .read_header = read_header,
184  .read_packet = read_packet,
185  .read_close = read_close,
186  .read_seek = read_seek,
187  .priv_data_size = sizeof(CDIOContext),
188  .flags = AVFMT_NOFILE,
189  .priv_class = &libcdio_class,
190 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:314
CDIOContext
Definition: libcdio.c:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:768
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
CDIOContext::last_sector
int32_t last_sector
Definition: libcdio.c:46
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1268
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
CDIOContext::drive
cdrom_drive_t * drive
Definition: libcdio.c:44
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:315
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:149
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:141
OFFSET
#define OFFSET(x)
Definition: libcdio.c:160
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:985
CDIOContext::paranoia_mode
int paranoia_mode
Definition: libcdio.c:50
AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
@ AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT
Definition: log.h:43
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:650
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:655
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
avpriv_update_cur_dts
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:32
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:965
NULL
#define NULL
Definition: coverity.c:32
CDIOContext::speed
int speed
Definition: libcdio.c:49
read_header
static av_cold int read_header(AVFormatContext *ctx)
Definition: libcdio.c:53
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
AVFormatContext::url
char * url
input or output URL.
Definition: avformat.h:1283
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:464
options
static const AVOption options[]
Definition: libcdio.c:162
log.h
ff_libcdio_demuxer
const AVInputFormat ff_libcdio_demuxer
Definition: libcdio.c:181
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
avpriv_new_chapter
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: utils.c:883
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
AVClass::class_name
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:71
avformat.h
CDIOContext::paranoia
cdrom_paranoia_t * paranoia
Definition: libcdio.c:45
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
read_packet
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:113
avpriv_set_pts_info
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: utils.c:1196
DEC
#define DEC
Definition: libcdio.c:161
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AV_OPT_TYPE_FLAGS
@ AV_OPT_TYPE_FLAGS
Definition: opt.h:223
int32_t
int32_t
Definition: audioconvert.c:56
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
libcdio_class
static const AVClass libcdio_class
Definition: libcdio.c:173
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
snprintf
#define snprintf
Definition: snprintf.h:34
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1228