FFmpegfs Fuse Multi Media Filesystem 2.19
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-2026 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
32#ifndef BUFFER_H
33#define BUFFER_H
34
35#pragma once
36
37#include "fileio.h"
38
39#include <mutex>
40#include <vector>
41#include <stddef.h>
42
43#define CACHE_CHECK_BIT(mask, var) ((mask) == (mask & (var)))
45#define CACHE_CLOSE_NOOPT 0x00000000
46#define CACHE_CLOSE_FREE 0x00000001
47#define CACHE_CLOSE_DELETE (0x00000002 | CACHE_CLOSE_FREE)
49#define CACHE_FLAG_RO 0x00000001
50#define CACHE_FLAG_RW 0x00000002
55class Buffer : public FileIO
56{
64 static constexpr int PREALLOC_FACTOR = 5;
65public:
69 typedef struct _tagCACHEINFO
70 {
71 public:
73 : m_fd(-1)
74 , m_buffer(nullptr)
75 , m_buffer_pos(0)
77 , m_buffer_size(0)
78 , m_seg_finished(false)
79 , m_fd_idx(-1)
80 , m_buffer_idx(nullptr)
82 , m_flags(0)
85 {
86 }
87
91 void reset()
92 {
93 m_fd = -1;
94 m_buffer = nullptr;
95 m_buffer_pos = 0;
97 m_buffer_size = 0;
100 }
101
102 // Main cache
103 std::string m_cachefile;
104 int m_fd;
105 uint8_t * m_buffer;
110 // Index for frame sets
111 std::string m_cachefile_idx;
113 uint8_t * m_buffer_idx;
115 // Flags
116 uint32_t m_flags;
117 // Statistics
119 unsigned int m_buffer_writes;
121 typedef CACHEINFO const * LPCCACHEINFO;
123public:
127 explicit Buffer();
133 virtual ~Buffer();
138 virtual VIRTUALTYPE type() const override;
144 bool init(bool erase_cache);
151 bool set_segment(uint32_t segment_no, size_t size);
156 uint32_t segment_count();
161 uint32_t current_segment_no();
167 bool segment_exists(uint32_t segment_no);
178 bool cachefile_valid(uint32_t segment_no);
191 bool invalidate_segment(uint32_t segment_no = 0);
197 bool release(int flags = CACHE_CLOSE_NOOPT);
202 virtual size_t bufsize() const override;
203
209 virtual int openio(LPVIRTUALFILE virtualfile) override;
216 virtual size_t readio(void *data, size_t size) override;
227 size_t read_frame(std::vector<uint8_t> * data, uint32_t frame_no);
232 virtual int error() const override;
236 virtual int64_t duration() const override;
241 virtual size_t size() const override;
247 virtual size_t size(uint32_t segment_no) const;
252 virtual size_t tell() const override;
258 virtual size_t tell(uint32_t segment_no) const;
272 virtual int seek(int64_t offset, int whence) override;
287 virtual int seek(int64_t offset, int whence, uint32_t segment_no);
292 virtual bool eof() const override;
298 virtual bool eof(uint32_t segment_no) const;
302 virtual void closeio() override;
309 size_t writeio(const uint8_t* data, size_t length);
317 size_t write_frame(const uint8_t* data, size_t length, uint32_t frame_no);
322 bool flush();
327 bool clear();
333 bool reserve(size_t size);
343 size_t buffer_watermark(uint32_t segment_no = 0) const;
351 bool copy(std::vector<uint8_t> * out_data, size_t offset, uint32_t segment_no = 0);
360 bool copy(uint8_t* out_data, size_t offset, size_t bufsize, uint32_t segment_no = 0);
366 const std::string & cachefile(uint32_t segment_no) const;
375 static const std::string & make_cachefile_name(std::string *cachefile, const std::string & filename, const std::string &fileext, bool is_idx);
381 static bool remove_file(const std::string & filename);
387 bool have_frame(uint32_t frame_no);
391 void finished_segment();
397 bool is_segment_finished(uint32_t segment_no) const;
405 bool open_file(uint32_t segment_no, uint32_t flags, size_t defaultsize = 0);
412 bool close_file(uint32_t segment_no, uint32_t flags);
413
414protected:
420 bool remove_cachefile(uint32_t segment_no = 0) const;
425 bool is_open();
426
427private:
437 uint8_t* write_prepare(size_t length);
446 void increment_pos(size_t increment);
456 bool reallocate(size_t newsize);
471 bool map_file(const std::string & filename, volatile int *fd, uint8_t **p, size_t *filesize, bool *isdefaultsize, size_t defaultsize, bool truncate) const;
481 bool unmap_file(const std::string & filename, volatile int *fd, uint8_t **p, size_t len, size_t *filesize) const;
482
488 LPCACHEINFO cacheinfo(uint32_t segment_no);
494 LPCCACHEINFO const_cacheinfo(uint32_t segment_no) const;
495
496private:
497 std::recursive_mutex m_mutex;
499 uint32_t m_cur_open;
501 std::vector<CACHEINFO> m_ci;
502};
503
504#endif
#define CACHE_CLOSE_NOOPT
Dummy, do nothing special.
Definition buffer.h:45
The Buffer class.
Definition buffer.h:56
bool invalidate_segment(uint32_t segment_no=0)
Invalidate the requested cache segment/file.
Definition buffer.cc:405
std::vector< CACHEINFO > m_ci
Cache info.
Definition buffer.h:501
virtual int64_t duration() const override
Get the duration of the file, in AV_TIME_BASE fractional seconds.
Definition buffer.cc:944
static constexpr int PREALLOC_FACTOR
PREALLOC_FACTOR - Number of elements allocated on reallocate calls Number of elements allocated on re...
Definition buffer.h:64
virtual bool eof() const override
Check if at end of file.
Definition buffer.cc:1146
void increment_pos(size_t increment)
Increment buffer position.
Definition buffer.cc:864
LPCACHEINFO cacheinfo(uint32_t segment_no)
Get cache information.
Definition buffer.cc:1220
uint8_t * write_prepare(size_t length)
Prepare for the writing operation.
Definition buffer.cc:847
bool release(int flags=CACHE_CLOSE_NOOPT)
Release cache buffer.
Definition buffer.cc:589
uint32_t m_cur_open
Number of open files.
Definition buffer.h:499
virtual int openio(LPVIRTUALFILE virtualfile) override
Open a virtual file.
Definition buffer.cc:64
bool segment_exists(uint32_t segment_no)
Check if segment exists.
Definition buffer.cc:347
void finished_segment()
Complete the segment decoding.
Definition buffer.cc:1195
bool cachefile_valid(uint32_t segment_no)
Check whether the physical cache file for the requested segment exists and contains data.
Definition buffer.cc:371
virtual int seek(int64_t offset, int whence) override
Seek to position in file.
Definition buffer.cc:869
size_t writeio(const uint8_t *data, size_t length)
Write data to the current position in the buffer. The position pointer will be updated.
Definition buffer.cc:766
bool clear()
Clear (delete) buffer.
Definition buffer.cc:682
bool map_file(const std::string &filename, volatile int *fd, uint8_t **p, size_t *filesize, bool *isdefaultsize, size_t defaultsize, bool truncate) const
Map memory to a file.
Definition buffer.cc:456
virtual ~Buffer()
Free Buffer object.
Definition buffer.cc:49
bool unmap_file(const std::string &filename, volatile int *fd, uint8_t **p, size_t len, size_t *filesize) const
Unmap memory from the file.
Definition buffer.cc:539
virtual void closeio() override
Close buffer.
Definition buffer.cc:1156
virtual VIRTUALTYPE type() const override
Get type of this virtual file.
Definition buffer.cc:54
bool init(bool erase_cache)
Initialise cache.
Definition buffer.cc:179
std::recursive_mutex m_mutex
Access mutex.
Definition buffer.h:497
bool is_segment_finished(uint32_t segment_no) const
Return true if transcoding of the segment is finished.
Definition buffer.cc:1207
static bool remove_file(const std::string &filename)
Remove (unlink) the file.
Definition buffer.cc:1093
size_t write_frame(const uint8_t *data, size_t length, uint32_t frame_no)
Write image data for the frame number into the buffer.
Definition buffer.cc:793
CACHEINFO const * LPCCACHEINFO
Pointer to const version of CACHEINFO.
Definition buffer.h:121
const std::string & cachefile(uint32_t segment_no) const
Get cache filename.
Definition buffer.cc:1059
virtual size_t readio(void *data, size_t size) override
Not implemented.
Definition buffer.cc:1107
bool have_frame(uint32_t frame_no)
Check if we have the requested frame number. Works only when processing a frame set.
Definition buffer.cc:1161
bool flush()
Flush buffer to disk.
Definition buffer.cc:654
static const std::string & make_cachefile_name(std::string *cachefile, const std::string &filename, const std::string &fileext, bool is_idx)
Make up a cache file name, including the full path.
Definition buffer.cc:1073
bool reserve(size_t size)
Reserve memory without changing size to reduce re-allocations.
Definition buffer.cc:722
uint32_t current_segment_no()
Get the currently selected segment.
Definition buffer.cc:336
struct Buffer::_tagCACHEINFO CACHEINFO
Structure to hold current cache state.
bool reallocate(size_t newsize)
Reallocate the buffer to a new size.
Definition buffer.cc:1031
virtual int error() const override
Get last error.
Definition buffer.cc:1141
bool copy(std::vector< uint8_t > *out_data, size_t offset, uint32_t segment_no=0)
Copy buffered data into output buffer.
Definition buffer.cc:980
virtual size_t bufsize() const override
Size of this buffer.
Definition buffer.cc:59
LPCCACHEINFO const_cacheinfo(uint32_t segment_no) const
Get cache information.
Definition buffer.cc:1236
uint32_t segment_count()
Get segment count.
Definition buffer.cc:329
struct Buffer::_tagCACHEINFO * LPCACHEINFO
Pointer version of CACHEINFO.
virtual size_t tell() const override
Get the value of the internal read position pointer.
Definition buffer.cc:926
LPCACHEINFO m_cur_ci
Convenience pointer to current write segment.
Definition buffer.h:498
bool set_segment(uint32_t segment_no, size_t size)
Set the current segment.
Definition buffer.cc:303
bool remove_cachefile(uint32_t segment_no=0) const
Remove the cachefile.
Definition buffer.cc:639
Buffer()
Create Buffer object.
Definition buffer.cc:42
bool is_open()
Check if the cache file is open.
Definition buffer.cc:1180
virtual size_t size() const override
Get the value of the internal buffer size pointer.
Definition buffer.cc:949
bool open_file(uint32_t segment_no, uint32_t flags, size_t defaultsize=0)
Open the cache file if not already open.
Definition buffer.cc:77
bool close_file(uint32_t segment_no, uint32_t flags)
If it hasn't already been done, close the cache file.
Definition buffer.cc:137
size_t buffer_watermark(uint32_t segment_no=0) const
Return the current watermark of the file while transcoding.
Definition buffer.cc:967
size_t read_frame(std::vector< uint8_t > *data, uint32_t frame_no)
Write image data for the frame number into the buffer.
Definition buffer.cc:1114
Base class for I/O.
Definition fileio.h:260
LPVIRTUALFILE virtualfile()
Get virtual file object.
Definition fileio.cc:113
const std::string & filename() const
Get source filename.
Definition fileio.cc:123
FileIO class.
VIRTUALTYPE
Virtual file types enum.
Definition fileio.h:92
Structure to hold current cache state.
Definition buffer.h:70
int m_fd
File handle for buffer.
Definition buffer.h:104
size_t m_buffer_write_size
Sum of bytes written to the buffer.
Definition buffer.h:118
uint8_t * m_buffer
Pointer to buffer memory.
Definition buffer.h:105
bool m_seg_finished
True if segment completely decoded.
Definition buffer.h:109
unsigned int m_buffer_writes
Total number of writes to the buffer.
Definition buffer.h:119
size_t m_buffer_size_idx
Size of index buffer.
Definition buffer.h:114
uint8_t * m_buffer_idx
Pointer to index memory.
Definition buffer.h:113
size_t m_buffer_size
Current buffer size.
Definition buffer.h:108
std::string m_cachefile
Cache file name.
Definition buffer.h:103
uint32_t m_flags
CACHE_FLAG_* options.
Definition buffer.h:116
int m_fd_idx
File handle for index.
Definition buffer.h:112
size_t m_buffer_watermark
Number of bytes in buffer.
Definition buffer.h:107
size_t m_buffer_pos
Read/write position.
Definition buffer.h:106
std::string m_cachefile_idx
Index file name.
Definition buffer.h:111
void reset()
Reset buffer pointers.
Definition buffer.h:91
Virtual file definition.
Definition fileio.h:123