h264_metadata: Add option to delete filler data
[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
43e0e0c4
LB
26#include "config.h"
27
28#if HAVE_CDIO_PARANOIA_H
e417d80d
AK
29#include <cdio/cdda.h>
30#include <cdio/paranoia.h>
43e0e0c4
LB
31#elif HAVE_CDIO_PARANOIA_PARANOIA_H
32#include <cdio/paranoia/cdda.h>
33#include <cdio/paranoia/paranoia.h>
34#endif
e417d80d
AK
35
36#include "libavutil/log.h"
37#include "libavutil/mem.h"
38#include "libavutil/opt.h"
39
40#include "libavformat/avformat.h"
41#include "libavformat/internal.h"
42
e417d80d 43typedef struct CDIOContext {
87392b1f 44 AVClass *class;
e417d80d
AK
45 cdrom_drive_t *drive;
46 cdrom_paranoia_t *paranoia;
47 int32_t last_sector;
48
49 /* private options */
50 int speed;
51 int paranoia_mode;
52} CDIOContext;
53
6e9651d1 54static av_cold int read_header(AVFormatContext *ctx)
e417d80d
AK
55{
56 CDIOContext *s = ctx->priv_data;
57 AVStream *st;
58 int ret, i;
59 char *err = NULL;
60
3b3bbdd3 61 if (!(st = avformat_new_stream(ctx, NULL)))
e417d80d
AK
62 return AVERROR(ENOMEM);
63 s->drive = cdio_cddap_identify(ctx->filename, CDDA_MESSAGE_LOGIT, &err);
64 if (!s->drive) {
65 av_log(ctx, AV_LOG_ERROR, "Could not open drive %s.\n", ctx->filename);
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->filename);
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
9200514a 88 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
e417d80d 89 if (s->drive->bigendianp)
9200514a 90 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
e417d80d 91 else
9200514a
AK
92 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
93 st->codecpar->sample_rate = 44100;
94 st->codecpar->channels = 2;
e417d80d
AK
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;
9200514a 100 avpriv_set_pts_info(st, 64, CDIO_CD_FRAMESIZE_RAW, 2 * st->codecpar->channels * st->codecpar->sample_rate);
e417d80d
AK
101
102 for (i = 0; i < s->drive->tracks; i++) {
103 char title[16];
104 snprintf(title, sizeof(title), "track %02d", s->drive->disc_toc[i].bTrack);
1fa395e4 105 avpriv_new_chapter(ctx, i, st->time_base, s->drive->disc_toc[i].dwStartSector,
e417d80d
AK
106 s->drive->disc_toc[i+1].dwStartSector, title);
107 }
108
109 s->last_sector = cdio_cddap_disc_lastsector(s->drive);
110
111 return 0;
112}
113
114static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
115{
116 CDIOContext *s = ctx->priv_data;
117 int ret;
118 uint16_t *buf;
119 char *err = NULL;
120
121 if (ctx->streams[0]->cur_dts > s->last_sector)
122 return AVERROR_EOF;
123
124 buf = cdio_paranoia_read(s->paranoia, NULL);
125 if (!buf)
126 return AVERROR_EOF;
127
128 if (err = cdio_cddap_errors(s->drive)) {
129 av_log(ctx, AV_LOG_ERROR, "%s\n", err);
130 free(err);
131 err = NULL;
132 }
133 if (err = cdio_cddap_messages(s->drive)) {
134 av_log(ctx, AV_LOG_VERBOSE, "%s\n", err);
135 free(err);
136 err = NULL;
137 }
138
139 if ((ret = av_new_packet(pkt, CDIO_CD_FRAMESIZE_RAW)) < 0)
140 return ret;
141 memcpy(pkt->data, buf, CDIO_CD_FRAMESIZE_RAW);
142 return 0;
143}
144
145static av_cold int read_close(AVFormatContext *ctx)
146{
147 CDIOContext *s = ctx->priv_data;
148 cdio_paranoia_free(s->paranoia);
149 cdio_cddap_close(s->drive);
150 return 0;
151}
152
153static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp,
154 int flags)
155{
156 CDIOContext *s = ctx->priv_data;
157 AVStream *st = ctx->streams[0];
158
159 cdio_paranoia_seek(s->paranoia, timestamp, SEEK_SET);
160 st->cur_dts = timestamp;
161 return 0;
162}
163
164#define OFFSET(x) offsetof(CDIOContext, x)
165#define DEC AV_OPT_FLAG_DECODING_PARAM
166static const AVOption options[] = {
e6153f17 167 { "speed", "Drive reading speed.", OFFSET(speed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, DEC },
d58dd4b5 168 { "paranoia_mode", "Error recovery mode.", OFFSET(paranoia_mode), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT_MIN, INT_MAX, DEC, "paranoia_mode" },
124134e4
MS
169 { "verify", "Verify data integrity in overlap area", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_VERIFY }, 0, 0, DEC, "paranoia_mode" },
170 { "overlap", "Perform overlapped reads.", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_OVERLAP }, 0, 0, DEC, "paranoia_mode" },
171 { "neverskip", "Do not skip failed reads.", 0, AV_OPT_TYPE_CONST, { .i64 = PARANOIA_MODE_NEVERSKIP }, 0, 0, DEC, "paranoia_mode" },
e417d80d
AK
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,
180};
181
182AVInputFormat ff_libcdio_demuxer = {
183 .name = "libcdio",
184 .read_header = read_header,
185 .read_packet = read_packet,
186 .read_close = read_close,
187 .read_seek = read_seek,
188 .priv_data_size = sizeof(CDIOContext),
189 .flags = AVFMT_NOFILE,
190 .priv_class = &libcdio_class,
191};