Remove unnecessary inclusions of [sys/]time.h
[libav.git] / libavdevice / libcdio.c
CommitLineData
e417d80d
AK
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/* cdio returns some malloced strings that need to be free()d */
37#undef free
38
39typedef struct CDIOContext {
87392b1f 40 AVClass *class;
e417d80d
AK
41 cdrom_drive_t *drive;
42 cdrom_paranoia_t *paranoia;
43 int32_t last_sector;
44
45 /* private options */
46 int speed;
47 int paranoia_mode;
48} CDIOContext;
49
6e9651d1 50static av_cold int read_header(AVFormatContext *ctx)
e417d80d
AK
51{
52 CDIOContext *s = ctx->priv_data;
53 AVStream *st;
54 int ret, i;
55 char *err = NULL;
56
3b3bbdd3 57 if (!(st = avformat_new_stream(ctx, NULL)))
e417d80d
AK
58 return AVERROR(ENOMEM);
59 s->drive = cdio_cddap_identify(ctx->filename, CDDA_MESSAGE_LOGIT, &err);
60 if (!s->drive) {
61 av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->filename);
62 return AVERROR(EINVAL);
63 }
64 if (err) {
65 av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
66 free(err);
67 }
68 if ((ret = cdio_cddap_open(s->drive)) < 0 || !s->drive->opened) {
69 av_log(ctx, AV_LOG_ERROR, "Could not open disk in drive %s.\n", ctx->filename);
70 return AVERROR(EINVAL);
71 }
72
73 cdio_cddap_verbose_set(s->drive, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);
74 if (s->speed)
75 cdio_cddap_speed_set(s->drive, s->speed);
76
77 s->paranoia = cdio_paranoia_init(s->drive);
78 if (!s->paranoia) {
79 av_log(ctx, AV_LOG_ERROR, "Could not init paranoia.\n");
80 return AVERROR(EINVAL);
81 }
82 cdio_paranoia_modeset(s->paranoia, s->paranoia_mode);
83
84 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
85 if (s->drive->bigendianp)
86 st->codec->codec_id = CODEC_ID_PCM_S16BE;
87 else
88 st->codec->codec_id = CODEC_ID_PCM_S16LE;
89 st->codec->sample_rate = 44100;
90 st->codec->channels = 2;
91 if (s->drive->audio_last_sector != CDIO_INVALID_LSN &&
92 s->drive->audio_first_sector != CDIO_INVALID_LSN)
93 st->duration = s->drive->audio_last_sector - s->drive->audio_first_sector;
94 else if (s->drive->tracks)
95 st->duration = s->drive->disc_toc[s->drive->tracks].dwStartSector;
c3f9ebf7 96 avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2*st->codec->channels*st->codec->sample_rate);
e417d80d
AK
97
98 for (i = 0; i < s->drive->tracks; i++) {
99 char title[16];
100 snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
1fa395e4 101 avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
e417d80d
AK
102 s->drive->disc_toc[i+1].dwStartSector, title);
103 }
104
105 s->last_sector = cdio_cddap_disc_lastsector(s->drive);
106
107 return 0;
108}
109
110static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
111{
112 CDIOContext *s = ctx->priv_data;
113 int ret;
114 uint16_t *buf;
115 char *err = NULL;
116
117 if (ctx->streams[0]->cur_dts > s->last_sector)
118 return AVERROR_EOF;
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
141static av_cold int read_close(AVFormatContext *ctx)
142{
143 CDIOContext *s = ctx->priv_data;
144 cdio_paranoia_free(s->paranoia);
145 cdio_cddap_close(s->drive);
146 return 0;
147}
148
149static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
150 int flags)
151{
152 CDIOContext *s = ctx->priv_data;
153 AVStream *st = ctx->streams[0];
154
155 cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
156 st->cur_dts = timestamp;
157 return 0;
158}
159
160#define OFFSET(x) offsetof(CDIOContext, x)
161#define DEC AV_OPT_FLAG_DECODING_PARAM
162static const AVOption options[] = {
145f741e
AK
163 { "speed", "Drive reading speed.", OFFSET(speed), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, DEC },
164 { "paranoia_mode", "Error recovery mode.", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { 0 }, INT_MIN, INT_MAX, DEC, "paranoia_mode" },
165 { "verify", "Verify data integrity in overlap area", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_VERIFY }, 0, 0, DEC, "paranoia_mode" },
166 { "overlap", "Perform overlapped reads.", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_OVERLAP }, 0, 0, DEC, "paranoia_mode" },
167 { "neverskip", "Do not skip failed reads.", 0, AV_OPT_TYPE_CONST, { PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, "paranoia_mode" },
e417d80d
AK
168 { NULL },
169};
170
171static const AVClass libcdio_class = {
172 .class_name = "libcdio indev",
173 .item_name = av_default_item_name,
174 .option = options,
175 .version = LIBAVUTIL_VERSION_INT,
176};
177
178AVInputFormat ff_libcdio_demuxer = {
179 .name = "libcdio",
180 .read_header = read_header,
181 .read_packet = read_packet,
182 .read_close = read_close,
183 .read_seek = read_seek,
184 .priv_data_size = sizeof(CDIOContext),
185 .flags = AVFMT_NOFILE,
186 .priv_class = &libcdio_class,
187};