]> git.sesse.net Git - ffmpeg/blob - libavdevice/libcdio.c
Merge commit '5d471b73d20616f5ac701ff62e5de49465cda264'
[ffmpeg] / libavdevice / libcdio.c
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
21 /**
22  * @file
23  * libcdio CD grabbing
24  */
25
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 typedef struct CDIOContext {
37     const AVClass       *class;
38     cdrom_drive_t       *drive;
39     cdrom_paranoia_t *paranoia;
40     int32_t last_sector;
41
42     /* private options */
43     int speed;
44     int paranoia_mode;
45 } CDIOContext;
46
47 static av_cold int read_header(AVFormatContext *ctx)
48 {
49     CDIOContext *s = ctx->priv_data;
50     AVStream *st;
51     int ret, i;
52     char *err = NULL;
53
54     if (!(st = avformat_new_stream(ctx, NULL)))
55         return AVERROR(ENOMEM);
56     s->drive = cdio_cddap_identify(ctx->filename, CDDA_MESSAGE_LOGIT, &err);
57     if (!s->drive) {
58         av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->filename);
59         return AVERROR(EINVAL);
60     }
61     if (err) {
62         av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
63         free(err);
64     }
65     if ((ret = cdio_cddap_open(s->drive)) < 0 || !s->drive->opened) {
66         av_log(ctx, AV_LOG_ERROR, "Could not open disk in drive %s.\n", ctx->filename);
67         return AVERROR(EINVAL);
68     }
69
70     cdio_cddap_verbose_set(s->drive, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);
71     if (s->speed)
72         cdio_cddap_speed_set(s->drive, s->speed);
73
74     s->paranoia = cdio_paranoia_init(s->drive);
75     if (!s->paranoia) {
76         av_log(ctx, AV_LOG_ERROR, "Could not init paranoia.\n");
77         return AVERROR(EINVAL);
78     }
79     cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);
80
81     st->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
82     if (s->drive->bigendianp)
83         st->codec->codec_id    = AV_CODEC_ID_PCM_S16BE;
84     else
85         st->codec->codec_id    = AV_CODEC_ID_PCM_S16LE;
86     st->codec->sample_rate     = 44100;
87     st->codec->channels        = 2;
88     if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
89         s->drive->audio_first_sector != CDIO_INVALID_LSN)
90         st->duration           = s->drive->audio_last_sector - s->drive->audio_first_sector;
91     else if (s->drive->tracks)
92         st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
93     avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2*st->codec->channels*st->codec->sample_rate);
94
95     for (i = 0; i < s->drive->tracks; i++) {
96         char title[16];
97         snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
98         avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
99                        s->drive->disc_toc[i+1].dwStartSector, title);
100     }
101
102     s->last_sector = cdio_cddap_disc_lastsector(s->drive);
103
104     return 0;
105 }
106
107 static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
108 {
109     CDIOContext *s = ctx->priv_data;
110     int ret;
111     uint16_t *buf;
112     char *err = NULL;
113
114     if (ctx->streams[0]->cur_dts > s->last_sector)
115         return AVERROR_EOF;
116
117     buf = cdio_paranoia_read(s->paranoia, NULL);
118     if (!buf)
119         return AVERROR_EOF;
120
121     if (err = cdio_cddap_errors(s->drive)) {
122         av_log(ctx, AV_LOG_ERROR, "%s\n", err);
123         free(err);
124         err = NULL;
125     }
126     if (err = cdio_cddap_messages(s->drive)) {
127         av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
128         free(err);
129         err = NULL;
130     }
131
132     if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
133         return ret;
134     memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
135     return 0;
136 }
137
138 static av_cold int read_close(AVFormatContext *ctx)
139 {
140     CDIOContext *s = ctx->priv_data;
141     cdio_paranoia_free(s->paranoia);
142     cdio_cddap_close(s->drive);
143     return 0;
144 }
145
146 static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
147                      int flags)
148 {
149     CDIOContext *s = ctx->priv_data;
150     AVStream *st = ctx->streams[0];
151
152     cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
153     st->cur_dts = timestamp;
154     return 0;
155 }
156
157 #define OFFSET(x) offsetof(CDIOContext, x)
158 #define DEC AV_OPT_FLAG_DECODING_PARAM
159 static const AVOption options[] = {
160     { "speed",              "Drive reading speed.", OFFSET(speed),         AV_OPT_TYPE_INT,   { .i64 = 0 }, 0,       INT_MAX, DEC },
161     { "paranoia_mode",      "Error recovery mode.", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT_MIN, INT_MAX, DEC, "paranoia_mode" },
162         { "verify",         "Verify data integrity in overlap area", 0,    AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_VERIFY },    0, 0, DEC, "paranoia_mode" },
163         { "overlap",        "Perform overlapped reads.",             0,    AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_OVERLAP },   0, 0, DEC, "paranoia_mode" },
164         { "neverskip",      "Do not skip failed reads.",             0,    AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, "paranoia_mode" },
165     { NULL },
166 };
167
168 static const AVClass libcdio_class = {
169     .class_name = "libcdio indev",
170     .item_name  = av_default_item_name,
171     .option     = options,
172     .version    = LIBAVUTIL_VERSION_INT,
173 };
174
175 AVInputFormat ff_libcdio_demuxer = {
176     .name           = "libcdio",
177     .read_header    = read_header,
178     .read_packet    = read_packet,
179     .read_close     = read_close,
180     .read_seek      = read_seek,
181     .priv_data_size = sizeof(CDIOContext),
182     .flags          = AVFMT_NOFILE,
183     .priv_class     = &libcdio_class,
184 };