Skip to content

Commit

Permalink
format: apply .clang-format style
Browse files Browse the repository at this point in the history
  • Loading branch information
senyai committed Dec 28, 2024
1 parent 37f886b commit e2c37a7
Show file tree
Hide file tree
Showing 17 changed files with 1,143 additions and 878 deletions.
203 changes: 203 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
BasedOnStyle: LLVM

# Always break after an open bracket, if the parameters
# don’t fit on a single line, e.g.:
#
# someLongFunction(
# argument1, argument2);
#
AlignAfterOpenBracket: AlwaysBreak

# Break constructor initializers after the colon and commas.
#
# Constructor() :
# initializer1(),
# initializer2()
#
BreakConstructorInitializers: AfterColon

# If false, a function call’s arguments will either be all on the same
# line or will have one line each.
#
# true:
# void f() {
# f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
# }
#
# false:
# void f() {
# f(aaaaaaaaaaaaaaaaaaaa,
# aaaaaaaaaaaaaaaaaaaa,
# aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
# }
#
BinPackArguments: false

# If false, a function declaration’s or function definition’s parameters will
# either all be on the same line or will have one line each.
#
# true:
# void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa,
# int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
#
# false:
# void f(int aaaaaaaaaaaaaaaaaaaa,
# int aaaaaaaaaaaaaaaaaaaa,
# int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
#
BinPackParameters: false

# Don’t align escaped newlines.
#
# #define A \
# int aaaa; \
# int b; \
# int dddddddddd;
#
AlignEscapedNewlines: DontAlign

# The number of spaces before trailing line comments (// - comments).
#
# ajosn_pos_t pos; // pos is public to show the position
#
SpacesBeforeTrailingComments: 2

# If true, aligns trailing comments.
#
# true: false:
# int a; // My comment a vs. int a; // My comment a
# int b = 2; // comment b int b = 2; // comment about b
#
AlignTrailingComments: false


# If true, clang-format will attempt to re-flow comments.
#
# false:
# // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information
# /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information */
#
# true:
# // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of
# // information
# /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of
# * information */
#
ReflowComments: false

# Never merge blocks into a single line.
#
# while (true) {
# }
# while (true) {
# continue;
# }
#
AllowShortBlocksOnASingleLine: Never

# Never merge functions into a single line.
AllowShortFunctionsOnASingleLine: None

# Never put short ifs on the same line.
#
# if (a)
# return;
#
# if (b)
# return;
# else
# return;
#
# if (c)
# return;
# else {
# return;
# }
#
AllowShortIfStatementsOnASingleLine: Never

# Align reference to the left.
#
# int& a;
#
# not available in older formatter versions
# ReferenceAlignment: Left

# Align pointer to the left.
#
# int* a;
#
PointerAlignment: Left

# Indent width for line continuations.
#
# int i = // VeryVeryVeryVeryVeryLongComment
# longFunction( // Again a long comment
# arg);
ContinuationIndentWidth: 4

# The number of columns to use for indentation.
#
# void f() {
# someFunction();
# if (true, false) {
# f();
# }
# }
IndentWidth: 2

# If the constructor initializers don’t fit on a line,
# put each initializer on its own line.
#
# true:
# SomeClass::Constructor()
# : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) {
# return 0;
# }
#
# false:
# SomeClass::Constructor()
# : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa),
# aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) {
# return 0;
# }
#
ConstructorInitializerAllOnOneLineOrOnePerLine: true

# If a constructor definition with a member initializer list doesn’t fit
# on a single line, allow putting all member initializers onto the next line,
# if `ConstructorInitializerAllOnOneLineOrOnePerLine` is true. Note that
# this parameter has no effect if
# `ConstructorInitializerAllOnOneLineOrOnePerLine` is false.
#
# true:
# MyClass::MyClass() :
# member0(0), member1(2) {}
#
# false:
# MyClass::MyClass() :
# member0(0),
# member1(2) {}
#
AllowAllConstructorInitializersOnNextLine: false

# Always break after template declaration.
#
# template <typename T>
# T foo() {
# }
# template <typename T>
# T foo(int aaaaaaaaaaaaaaaaaaaaa,
# int bbbbbbbbbbbbbbbbbbbbb) {
# }
#
AlwaysBreakTemplateDeclarations: Yes

# Never merge lambdas into a single line.
AllowShortLambdasOnASingleLine: None

Standard: c++17
UseTab: Never

# AllowAllArgumentsOnNextLine: false
61 changes: 35 additions & 26 deletions include/videoreader/videoreader.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <memory>


class VideoReader
{
class VideoReader {
public:
enum class SCALAR_TYPE: int32_t {
U8
};
enum class SCALAR_TYPE : int32_t { U8 };
struct VRImage {
int32_t height;
int32_t width;
int32_t channels;
SCALAR_TYPE scalar_type;
int32_t stride; // 0 when unknown. number of bytes between rows
uint8_t *data; // pointer to the first pixel
void *user_data; // user supplied data, useful for freeing in DeallocateCallback
uint8_t* data; // pointer to the first pixel
void*
user_data; // user supplied data, useful for freeing in DeallocateCallback
};

/**
Expand All @@ -28,20 +25,32 @@ class VideoReader
* When `VRImage.data` is left `nullptr`, the library treats it like
* memory allocation error
*/
using AllocateCallback = void (*)(VRImage *, void*);
using DeallocateCallback = void (*)(VRImage *, void*);
using AllocateCallback = void (*)(VRImage*, void*);
using DeallocateCallback = void (*)(VRImage*, void*);

struct Frame {
using number_t = uint64_t;
using timestamp_s_t = double;
Frame(DeallocateCallback free, void *userdata, VRImage const& image, number_t number, timestamp_s_t timestamp_s) :
number{number}, timestamp_s{timestamp_s}, free{free}, userdata{userdata}, image{image} {}
number_t number; // zero-indexed; this number is not continuous due to possible invalid data frames
Frame(
DeallocateCallback free,
void* userdata,
VRImage const& image,
number_t number,
timestamp_s_t timestamp_s) :
number{number},
timestamp_s{timestamp_s},
free{free},
userdata{userdata},
image{image} {
}
number_t
number; // zero-indexed; this number is not continuous due to possible invalid data frames
timestamp_s_t timestamp_s; // seconds since the start of the video
unsigned char const* extras{}; // nullptr ot msgpack list in requested order. MUST be freed by `free` ca;;
unsigned char const*
extras{}; // nullptr ot msgpack list in requested order. MUST be freed by `free` ca;;
unsigned int extras_size{}; // num bytes in extra
DeallocateCallback free;
void *userdata;
void* userdata;
VRImage image;
~Frame(); // Frees VRImage and extras
};
Expand All @@ -62,7 +71,8 @@ class VideoReader
/**
* Main logging callback. Useful for debugging and to not flood stdout
*/
using LogCallback = void (*)(char const* message, LogLevel log_level, void* userdata);
using LogCallback =
void (*)(char const* message, LogLevel log_level, void* userdata);

public:
// url: file path or any ffmpeg url
Expand All @@ -74,14 +84,13 @@ class VideoReader
//
// see https://ffmpeg.org/ffmpeg-protocols.html for more details
static std::unique_ptr<VideoReader> create(
std::string const& url,
std::vector<std::string> const& parameter_pairs = {}, // size % 2 == 0
std::vector<std::string> const& extras = {}, // for extra_data
AllocateCallback alloc_callback = nullptr,
DeallocateCallback dealloc_callback = nullptr,
LogCallback log_callback = nullptr,
void* userdata = nullptr
);
std::string const& url,
std::vector<std::string> const& parameter_pairs = {}, // size % 2 == 0
std::vector<std::string> const& extras = {}, // for extra_data
AllocateCallback alloc_callback = nullptr,
DeallocateCallback dealloc_callback = nullptr,
LogCallback log_callback = nullptr,
void* userdata = nullptr);
virtual ~VideoReader();

VideoReader& operator=(VideoReader const&) = delete;
Expand All @@ -98,5 +107,5 @@ class VideoReader
// decode: decode the frame (false is useful for skipping frames,
// the result will be a valid frame with uinitialized pixel values)
// frame data are read in a separate thread
virtual FrameUP next_frame(bool decode=true) = 0;
virtual FrameUP next_frame(bool decode = true) = 0;
};
19 changes: 9 additions & 10 deletions include/videoreader/videowriter.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once
#include "videoreader.h"

class VideoWriter
{
class VideoWriter {
public:
struct Impl;

Expand All @@ -13,18 +12,18 @@ class VideoWriter
// log_callback: log callback (currently unused)
// userdata: data for log_callback (currently unused)
VideoWriter(
std::string const& uri,
VideoReader::VRImage const& format,
std::vector<std::string> const& parameter_pairs = {}, // size % 2 == 0
bool realtime = false,
VideoReader::LogCallback log_callback = nullptr,
void* userdata = nullptr
);
std::string const& uri,
VideoReader::VRImage const& format,
std::vector<std::string> const& parameter_pairs = {}, // size % 2 == 0
bool realtime = false,
VideoReader::LogCallback log_callback = nullptr,
void* userdata = nullptr);
VideoWriter& operator=(VideoWriter const&) = delete;
bool push(VideoReader::Frame const&);
void close();

~VideoWriter();

protected:
std::unique_ptr<Impl> impl;
std::unique_ptr<Impl> impl;
};
5 changes: 2 additions & 3 deletions src/ffmpeg_common.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#pragma once
extern "C" {
#include <libavutil/dict.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/dict.h>
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>
}
#include <memory>
#include <string>
#include <vector>


struct AVDictionaryDeleter {
void operator()(AVDictionary* av_dictionary) const noexcept {
av_dict_free(&av_dictionary);
Expand Down
15 changes: 10 additions & 5 deletions src/spinlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

class SpinLock {
std::atomic_flag lck = ATOMIC_FLAG_INIT;

public:
void lock() { while (lck.test_and_set(std::memory_order_acquire)); }
void unlock() { lck.clear(std::memory_order_release); }
void lock() {
while (lck.test_and_set(std::memory_order_acquire))
;
}
void unlock() {
lck.clear(std::memory_order_release);
}
};


// Sorry, not the right place for this code. Maybe rename to common.hpp?
template<typename T>
template <typename T>
inline void remove_every_second_item(T& c) {
auto it = c.begin();
while (it != c.end()){
while (it != c.end()) {
++it;
if (it != c.end()) {
it = c.erase(it);
Expand Down
Loading

0 comments on commit e2c37a7

Please sign in to comment.