FFmpegfs Fuse Multi Media Filesystem 2.16
buffer.h
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
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 volatile int m_fd;
105 uint8_t * m_buffer;
110 // Index for frame sets
111 std::string m_cachefile_idx;
112 volatile int m_fd_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);
173 bool release(int flags = CACHE_CLOSE_NOOPT);
178 virtual size_t bufsize() const override;
179
185 virtual int openio(LPVIRTUALFILE virtualfile) override;
192 virtual size_t readio(void *data, size_t size) override;
203 size_t read_frame(std::vector<uint8_t> * data, uint32_t frame_no);
208 virtual int error() const override;
212 virtual int64_t duration() const override;
217 virtual size_t size() const override;
223 virtual size_t size(uint32_t segment_no) const;
228 virtual size_t tell() const override;
234 virtual size_t tell(uint32_t segment_no) const;
248 virtual int seek(int64_t offset, int whence) override;
263 virtual int seek(int64_t offset, int whence, uint32_t segment_no);
268 virtual bool eof() const override;
274 virtual bool eof(uint32_t segment_no) const;
278 virtual void closeio() override;
285 size_t writeio(const uint8_t* data, size_t length);
293 size_t write_frame(const uint8_t* data, size_t length, uint32_t frame_no);
298 bool flush();
303 bool clear();
309 bool reserve(size_t size);
319 size_t buffer_watermark(uint32_t segment_no = 0) const;
327 bool copy(std::vector<uint8_t> * out_data, size_t offset, uint32_t segment_no = 0);
336 bool copy(uint8_t* out_data, size_t offset, size_t bufsize, uint32_t segment_no = 0);
342 const std::string & cachefile(uint32_t segment_no) const;
351 static const std::string & make_cachefile_name(std::string *cachefile, const std::string & filename, const std::string &fileext, bool is_idx);
357 static bool remove_file(const std::string & filename);
363 bool have_frame(uint32_t frame_no);
367 void finished_segment();
373 bool is_segment_finished(uint32_t segment_no) const;
381 bool open_file(uint32_t segment_no, uint32_t flags, size_t defaultsize = 0);
388 bool close_file(uint32_t segment_no, uint32_t flags);
389
390protected:
396 bool remove_cachefile(uint32_t segment_no = 0) const;
401 bool is_open();
402
403private:
413 uint8_t* write_prepare(size_t length);
422 void increment_pos(size_t increment);
432 bool reallocate(size_t newsize);
447 bool map_file(const std::string & filename, volatile int *fd, uint8_t **p, size_t *filesize, bool *isdefaultsize, size_t defaultsize, bool truncate) const;
457 bool unmap_file(const std::string & filename, volatile int *fd, uint8_t **p, size_t len, size_t *filesize) const;
458
464 LPCACHEINFO cacheinfo(uint32_t segment_no);
470 LPCCACHEINFO const_cacheinfo(uint32_t segment_no) const;
471
472private:
473 std::recursive_mutex m_mutex;
475 uint32_t m_cur_open;
477 std::vector<CACHEINFO> m_ci;
478};
479
480#endif
#define CACHE_CLOSE_NOOPT
Dummy, do nothing special.
Definition: buffer.h:45
The Buffer class.
Definition: buffer.h:56
std::vector< CACHEINFO > m_ci
Cache info.
Definition: buffer.h:477
virtual int64_t duration() const override
Get the duration of the file, in AV_TIME_BASE fractional seconds.
Definition: buffer.cc:846
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:1048
void increment_pos(size_t increment)
Increment buffer position.
Definition: buffer.cc:766
LPCACHEINFO cacheinfo(uint32_t segment_no)
Get cache information.
Definition: buffer.cc:1122
uint8_t * write_prepare(size_t length)
Prepare for the writing operation.
Definition: buffer.cc:749
bool release(int flags=CACHE_CLOSE_NOOPT)
Release cache buffer.
Definition: buffer.cc:491
uint32_t m_cur_open
Number of open files.
Definition: buffer.h:475
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:1097
virtual int seek(int64_t offset, int whence) override
Seek to position in file.
Definition: buffer.cc:771
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:668
bool clear()
Clear (delete) buffer.
Definition: buffer.cc:584
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:358
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:441
virtual void closeio() override
Close buffer.
Definition: buffer.cc:1058
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:473
bool is_segment_finished(uint32_t segment_no) const
Return true if transcoding of the segment is finished.
Definition: buffer.cc:1109
static bool remove_file(const std::string &filename)
Remove (unlink) the file.
Definition: buffer.cc:995
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:695
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:961
virtual size_t readio(void *data, size_t size) override
Not implemented.
Definition: buffer.cc:1009
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:1063
bool flush()
Flush buffer to disk.
Definition: buffer.cc:556
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:975
bool reserve(size_t size)
Reserve memory without changing size to reduce re-allocations.
Definition: buffer.cc:624
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:933
virtual int error() const override
Get last error.
Definition: buffer.cc:1043
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:882
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:1138
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:828
LPCACHEINFO m_cur_ci
Convenience pointer to current write segment.
Definition: buffer.h:474
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:541
Buffer()
Create Buffer object.
Definition: buffer.cc:42
bool is_open()
Check if the cache file is open.
Definition: buffer.cc:1082
virtual size_t size() const override
Get the value of the internal buffer size pointer.
Definition: buffer.cc:851
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:869
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:1016
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
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
volatile int m_fd_idx
File handle for index.
Definition: buffer.h:112
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
volatile int m_fd
File handle for buffer.
Definition: buffer.h:104
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