FFmpegfs Fuse Multi Media Filesystem 2.16
vcdio.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-2024 Norbert Schlia (nschlia@oblivion-software.de)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 * On Debian systems, the complete text of the GNU General Public License
19 * Version 3 can be found in `/usr/share/common-licenses/GPL-3'.
20 *
21 * On Debian systems, the complete text of the GNU General Public License
22 * Version 3 can be found in `/usr/share/common-licenses/GPL-3'.
23 */
24
25#ifdef USE_LIBVCD
26
37#include "vcdio.h"
38#include "vcd/vcdutils.h"
39#include "logging.h"
40
42 : m_fpi(nullptr)
43 , m_full_title(false)
44 , m_track_no(0)
45 , m_chapter_no(0)
46 , m_start_pos(0)
47 , m_end_pos(0)
48{
49
50}
51
53{
54 pvt_close();
55}
56
58{
59 return VIRTUALTYPE::VCD;
60}
61
62size_t VcdIO::bufsize() const
63{
64 return (32 * 1024);
65}
66
68{
69 std::string src_filename;
70
72
73 if (virtualfile != nullptr)
74 {
80 }
81 else
82 {
83 m_full_title = false;
84 m_track_no = 1;
85 m_chapter_no = 0;
86 m_start_pos = 0;
87 m_end_pos = size();
88 }
89
90 VCDUTILS::locate_video(path(), m_track_no, src_filename);
91
92 Logging::info(src_filename.c_str(), "Opening the input VCD.");
93
94 m_fpi = fopen(src_filename.c_str(), "rb");
95
96 if (m_fpi == nullptr)
97 {
98 return errno;
99 }
100
101 return seek(0, SEEK_SET);
102}
103
104size_t VcdIO::readio(void * data, size_t size)
105{
106 if (static_cast<size_t>(ftell(m_fpi)) + size > m_end_pos)
107 {
108 size = static_cast<size_t>(m_end_pos - static_cast<uint64_t>(ftell(m_fpi)));
109 }
110
111 if (!size)
112 {
113 return 0;
114 }
115
116 return fread(data, 1, size, m_fpi);
117}
118
119int VcdIO::error() const
120{
121 return ferror(m_fpi);
122}
123
124int64_t VcdIO::duration() const
125{
126 return AV_NOPTS_VALUE;
127}
128
129size_t VcdIO::size() const
130{
131 if (m_fpi == nullptr)
132 {
133 errno = EINVAL;
134 return 0;
135 }
136
137 if (m_end_pos)
138 {
139 return static_cast<size_t>(m_end_pos - m_start_pos);
140 }
141
142 struct stat stbuf;
143 fstat(fileno(m_fpi), &stbuf);
144 return static_cast<size_t>(stbuf.st_size);
145}
146
147size_t VcdIO::tell() const
148{
149 return static_cast<size_t>(static_cast<uint64_t>(ftell(m_fpi)) - m_start_pos);
150}
151
152int VcdIO::seek(int64_t offset, int whence)
153{
154 off_t seek_pos;
155 switch (whence)
156 {
157 case SEEK_SET:
158 {
159 seek_pos = static_cast<off_t>(m_start_pos) + offset;
160 break;
161 }
162 case SEEK_CUR:
163 {
164 seek_pos = static_cast<off_t>(m_start_pos) + ftell(m_fpi) + offset;
165 break;
166 }
167 case SEEK_END:
168 {
169 seek_pos = static_cast<off_t>(m_end_pos) - offset;
170 break;
171 }
172 default:
173 {
174 errno = EINVAL;
175 return (EOF);
176 }
177 }
178
179 if (static_cast<uint64_t>(seek_pos) > m_end_pos)
180 {
181 seek_pos = static_cast<off_t>(m_end_pos); // Cannot go beyond EOF. Set position to end, leave errno untouched.
182 }
183
184 if (static_cast<uint64_t>(seek_pos) < m_start_pos) // Cannot go before head, leave position untouched, set errno.
185 {
186 errno = EINVAL;
187 return (EOF);
188 }
189
190 return fseek(m_fpi, static_cast<long int>(seek_pos), SEEK_SET);
191}
192
193bool VcdIO::eof() const
194{
195 return ((feof(m_fpi) || (static_cast<uint64_t>(ftell(m_fpi)) >= m_end_pos)) ? true : false);
196}
197
199{
200 pvt_close();
201}
202
204{
205 FILE *fpi = m_fpi;
206 if (fpi != nullptr)
207 {
208 m_fpi = nullptr;
209 fclose(fpi);
210 }
211}
212
213#endif // USE_LIBVCD
const std::string & path() const
Path to source file (without file name)
Definition: fileio.cc:118
LPVIRTUALFILE virtualfile()
Get virtual file object.
Definition: fileio.cc:113
void set_virtualfile(LPVIRTUALFILE virtualfile)
Set the virtual file object.
Definition: fileio.cc:96
static void info(const T filename, const std::string &format_string, Args &&...args)
Write info level log entry.
Definition: logging.h:201
virtual ~VcdIO()
Free VcdIO object.
Definition: vcdio.cc:52
bool m_full_title
If true, ignore m_chapter_no and provide full track.
Definition: vcdio.h:143
virtual int seek(int64_t offset, int whence) override
Seek to position in file.
Definition: vcdio.cc:152
virtual size_t size() const override
Get the file size.
Definition: vcdio.cc:129
virtual void closeio() override
Close virtual file.
Definition: vcdio.cc:198
VcdIO()
Create VcdIO object.
Definition: vcdio.cc:41
int m_chapter_no
Chapter number (1..)
Definition: vcdio.h:145
void pvt_close()
Close virtual file. Non-virtual version to be safely called from constructor/destructor.
Definition: vcdio.cc:203
uint64_t m_end_pos
End offset in bytes (not including this byte)
Definition: vcdio.h:148
virtual int openio(LPVIRTUALFILE virtualfile) override
Open a virtual file.
Definition: vcdio.cc:67
FILE * m_fpi
File pointer to source media.
Definition: vcdio.h:141
int m_track_no
Track number (1..)
Definition: vcdio.h:144
virtual int64_t duration() const override
Get the duration of the file, in AV_TIME_BASE fractional seconds.
Definition: vcdio.cc:124
virtual size_t readio(void *data, size_t size) override
Read data from file.
Definition: vcdio.cc:104
uint64_t m_start_pos
Start offset in bytes.
Definition: vcdio.h:147
virtual VIRTUALTYPE type() const override
Get type of the virtual file.
Definition: vcdio.cc:57
virtual bool eof() const override
Check if at end of file.
Definition: vcdio.cc:193
virtual int error() const override
Get last error.
Definition: vcdio.cc:119
virtual size_t bufsize() const override
Get the ideal buffer size.
Definition: vcdio.cc:62
virtual size_t tell() const override
Get current read position.
Definition: vcdio.cc:147
VIRTUALTYPE
Virtual file types enum.
Definition: fileio.h:92
@ VCD
Video CD file.
Provide various log facilities to stderr, disk or syslog.
int locate_video(const std::string &path, int track_no, std::string &fullname)
Locate AVSEQ*DAT/MPEG video file for track_no.
Definition: vcdutils.cc:88
int m_chapter_no
Chapter number (1..)
Definition: fileio.h:179
uint64_t m_start_pos
Start offset in bytes.
Definition: fileio.h:180
uint64_t m_end_pos
End offset in bytes (not including this byte)
Definition: fileio.h:181
int m_track_no
Track number (1..)
Definition: fileio.h:178
Virtual file definition.
Definition: fileio.h:123
bool m_full_title
If true, ignore m_chapter_no and provide full track.
Definition: fileio.h:155
struct VIRTUALFILE::VCD_CHAPTER m_vcd
S/VCD track/chapter info.
Video CD and Super Video CD I/O.
S/VCD utility functions.