FFmpeg
Data Structures | Macros | Enumerations | Functions | Variables
shared.c File Reference
#include "libavutil/attributes.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/crc.h"
#include "libavutil/hash.h"
#include "libavutil/file_open.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/time.h"
#include "url.h"
#include <errno.h>
#include <fcntl.h>
#include <stdatomic.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

Go to the source code of this file.

Data Structures

struct  Block
 
struct  Spacemap
 
struct  SharedContext
 

Macros

#define HASH_METHOD   "SHA512/256"
 This hash should be resistant against collision attacks, so that an attacker could not generate e.g. More...
 
#define HASH_SIZE   32
 
#define HEADER_MAGIC   MKTAG(u'\xFF', 'S', 'h', '$')
 
#define HEADER_VERSION   2
 
#define DEF_SET_ONCE(ctype, atype)
 
#define OFFSET(x)   offsetof(SharedContext, x)
 
#define D   AV_OPT_FLAG_DECODING_PARAM
 

Enumerations

enum  BlockState { BLOCK_NONE = 0, BLOCK_PENDING, BLOCK_FAILED }
 

Functions

static int hash_uri (uint8_t hash[HASH_SIZE], const char *uri)
 
static uint16_t get_block_crc (const uint8_t *block, size_t block_size)
 
static int shared_close (URLContext *h)
 
static int cache_map (URLContext *h, int64_t filesize)
 
static int spacemap_init (URLContext *h, const uint8_t hash[HASH_SIZE])
 
static int spacemap_grow (URLContext *h, int64_t block)
 
static int64_t get_filesize (URLContext *h)
 
static int set_filesize (URLContext *h, int64_t new_size)
 
static int shared_open (URLContext *h, const char *arg, int flags, AVDictionary **options)
 
static int spacemap_remap (URLContext *h, size_t map_size)
 
static int read_cache (SharedContext *s, uint8_t *buf, size_t size, off_t offset)
 
static int write_cache (SharedContext *s, const uint8_t *buf, size_t size, off_t offset)
 
static size_t clamp_size (URLContext *h, size_t size, int64_t pos)
 
static int shared_read (URLContext *h, unsigned char *buf, int size)
 
static int64_t shared_seek (URLContext *h, int64_t pos, int whence)
 
static int shared_get_file_handle (URLContext *h)
 
static int shared_get_short_seek (URLContext *h)
 

Variables

static const AVOption options []
 
static const AVClass shared_context_class
 
const URLProtocol ff_shared_protocol
 

Macro Definition Documentation

◆ HASH_METHOD

#define HASH_METHOD   "SHA512/256"

This hash should be resistant against collision attacks, so that an attacker could not generate e.g.

two different URIs that map to the same cache file. This requires at least 64 bits of collision resistance in practice (i.e. 128 bits = 16 bytes of hash size). However, we can be conservative by computing e.g. a 256 bit hash and storing it inside the file header for verification.

Note that due to the way we use atomics, we should avoid zero bytes in the resulting hash; hence we tweak the input slightly to avoid this. The resulting loss in hash strength is negligible, since 32 bytes is already much more than needed.

Definition at line 57 of file shared.c.

◆ HASH_SIZE

#define HASH_SIZE   32

Definition at line 58 of file shared.c.

◆ HEADER_MAGIC

#define HEADER_MAGIC   MKTAG(u'\xFF', 'S', 'h', '$')

Definition at line 78 of file shared.c.

◆ HEADER_VERSION

#define HEADER_VERSION   2

Definition at line 79 of file shared.c.

◆ DEF_SET_ONCE

#define DEF_SET_ONCE (   ctype,
  atype 
)
Value:
static int set_once_##atype(atomic_##atype *const ptr, const ctype value) \
{ \
ctype prev = 0; \
av_assert1(value != 0); \
ptr, &prev, value, memory_order_release, memory_order_relaxed)) \
return 1; \
else if (prev == value) \
return 0; \
else \
return AVERROR(EINVAL); \
}

Definition at line 122 of file shared.c.

◆ OFFSET

#define OFFSET (   x)    offsetof(SharedContext, x)

Definition at line 852 of file shared.c.

◆ D

Definition at line 853 of file shared.c.

Enumeration Type Documentation

◆ BlockState

enum BlockState
Enumerator
BLOCK_NONE 

block is not cached

BLOCK_PENDING 

a thread is currently trying to write this block

BLOCK_FAILED 

the underlying I/O source failed to read this block

Definition at line 81 of file shared.c.

Function Documentation

◆ hash_uri()

static int hash_uri ( uint8_t  hash[HASH_SIZE],
const char *  uri 
)
static

Definition at line 60 of file shared.c.

Referenced by shared_open().

◆ get_block_crc()

static uint16_t get_block_crc ( const uint8_t *  block,
size_t  block_size 
)
static

Definition at line 93 of file shared.c.

Referenced by shared_read().

◆ shared_close()

static int shared_close ( URLContext h)
static

Definition at line 177 of file shared.c.

Referenced by shared_open().

◆ cache_map()

static int cache_map ( URLContext h,
int64_t  filesize 
)
static

Definition at line 333 of file shared.c.

Referenced by set_filesize(), and shared_open().

◆ spacemap_init()

static int spacemap_init ( URLContext h,
const uint8_t  hash[HASH_SIZE] 
)
static

Definition at line 472 of file shared.c.

Referenced by shared_open().

◆ spacemap_grow()

static int spacemap_grow ( URLContext h,
int64_t  block 
)
static

Definition at line 439 of file shared.c.

Referenced by shared_open(), and shared_read().

◆ get_filesize()

static int64_t get_filesize ( URLContext h)
static

Definition at line 203 of file shared.c.

Referenced by clamp_size(), shared_open(), shared_seek(), and spacemap_grow().

◆ set_filesize()

static int set_filesize ( URLContext h,
int64_t  new_size 
)
static

Definition at line 209 of file shared.c.

Referenced by shared_open(), shared_read(), and shared_seek().

◆ shared_open()

static int shared_open ( URLContext h,
const char *  arg,
int  flags,
AVDictionary **  options 
)
static

Definition at line 232 of file shared.c.

◆ spacemap_remap()

static int spacemap_remap ( URLContext h,
size_t  map_size 
)
static

Definition at line 369 of file shared.c.

Referenced by spacemap_grow(), and spacemap_init().

◆ read_cache()

static int read_cache ( SharedContext s,
uint8_t *  buf,
size_t  size,
off_t  offset 
)
static

Definition at line 525 of file shared.c.

Referenced by shared_read().

◆ write_cache()

static int write_cache ( SharedContext s,
const uint8_t *  buf,
size_t  size,
off_t  offset 
)
static

Definition at line 545 of file shared.c.

Referenced by shared_read().

◆ clamp_size()

static size_t clamp_size ( URLContext h,
size_t  size,
int64_t  pos 
)
static

Definition at line 565 of file shared.c.

Referenced by shared_read().

◆ shared_read()

static int shared_read ( URLContext h,
unsigned char *  buf,
int  size 
)
static

Definition at line 576 of file shared.c.

◆ shared_seek()

static int64_t shared_seek ( URLContext h,
int64_t  pos,
int  whence 
)
static

Definition at line 795 of file shared.c.

◆ shared_get_file_handle()

static int shared_get_file_handle ( URLContext h)
static

Definition at line 837 of file shared.c.

◆ shared_get_short_seek()

static int shared_get_short_seek ( URLContext h)
static

Definition at line 843 of file shared.c.

Variable Documentation

◆ options

const AVOption options[]
static
Initial value:
= {
{ "cache_dir", "Directory path for shared file cache", OFFSET(cache_dir), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = D },
{ "block_shift", "Set the base 2 logarithm of the block size", OFFSET(block_shift), AV_OPT_TYPE_INT, {.i64 = 15}, 9, 30, .flags = D },
{ "read_only", "Don't write data to the cache, only read from it", OFFSET(read_only), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, .flags = D },
{ "cache_verify", "Verify correctness of the cache against the source", OFFSET(verify), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, .flags = D },
{ "cache_timeout", "Time in us to wait before re-fetching pending blocks", OFFSET(timeout), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, .flags = D },
{ "retry_errors", "Re-request blocks even if they previously failed", OFFSET(retry_errors), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, .flags = D },
{0},
}

Definition at line 855 of file shared.c.

◆ shared_context_class

const AVClass shared_context_class
static
Initial value:
= {
.class_name = "shared",
.item_name = av_default_item_name,
.option = options,
}

Definition at line 865 of file shared.c.

◆ ff_shared_protocol

const URLProtocol ff_shared_protocol
Initial value:
= {
.name = "shared",
.url_open2 = shared_open,
.url_read = shared_read,
.url_seek = shared_seek,
.url_close = shared_close,
.url_get_file_handle = shared_get_file_handle,
.url_get_short_seek = shared_get_short_seek,
.priv_data_size = sizeof(SharedContext),
.priv_data_class = &shared_context_class,
}

Definition at line 872 of file shared.c.

AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
shared_close
static int shared_close(URLContext *h)
Definition: shared.c:177
ctype
#define ctype
Definition: afir_template.c:46
D
#define D
Definition: shared.c:853
shared_get_file_handle
static int shared_get_file_handle(URLContext *h)
Definition: shared.c:837
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition: opt.h:263
shared_context_class
static const AVClass shared_context_class
Definition: shared.c:865
atomic_compare_exchange_strong_explicit
#define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure)
Definition: stdatomic.h:123
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
NULL
#define NULL
Definition: coverity.c:32
SharedContext
Definition: shared.c:141
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:242
shared_get_short_seek
static int shared_get_short_seek(URLContext *h)
Definition: shared.c:843
shared_seek
static int64_t shared_seek(URLContext *h, int64_t pos, int whence)
Definition: shared.c:795
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
options
static const AVOption options[]
Definition: shared.c:855
shared_open
static int shared_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
Definition: shared.c:232
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
OFFSET
#define OFFSET(x)
Definition: shared.c:852
shared_read
static int shared_read(URLContext *h, unsigned char *buf, int size)
Definition: shared.c:576
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276