Making mosaic images with ffmpeg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.1 KiB

#pragma once
extern "C" {
#include <libavutil/mem.h>
#include <libavutil/pixfmt.h>
typedef struct AVDictionary AVDictionary;
typedef struct AVFormatContext AVFormatContext;
typedef struct AVInputFormat AVInputFormat;
typedef struct AVCodecContext AVCodecContext;
typedef struct AVCodec AVCodec;
typedef struct AVPacket AVPacket;
typedef struct AVFrame AVFrame;
}
#include <memory>
#include <stdexcept>
namespace av {
// Generic error class
struct error : public std::runtime_error {
using std::runtime_error::runtime_error;
};
// Type of a free function (for unique_ptr)
template <typename T>
using deleter = void(*)(T*);
// AVFormatContext related
using format_context = std::unique_ptr<AVFormatContext, deleter<AVFormatContext>>;
format_context format_open_input(std::string const & filename, AVInputFormat* format, AVDictionary** options);
format_context format_alloc_context();
// AVCodec related
using open_codec = std::unique_ptr<AVCodecContext, deleter<AVCodecContext>>;
open_codec codec_open(AVCodecContext* ctx, AVCodec* codec, AVDictionary** options);
// AVPacket related (this is somewhat strange, but matches the usecase)
// I need to rethink this
using packet_buffer = AVPacket;
using packet = std::unique_ptr<AVPacket, deleter<AVPacket>>;
packet read_frame(format_context & ctx, packet_buffer & p);
// AVFrame related
using frame = std::unique_ptr<AVFrame, deleter<AVFrame>>;
frame frame_alloc();
frame frame_clone(frame const & f);
AVPixelFormat get_format(frame const & f);
// Allocator
template <typename T>
struct allocator {
using value_type = T;
using size_type = size_t;
T* allocate(size_type n) const {
auto ptr = av_malloc(n * sizeof(T));
if(!ptr) throw std::bad_alloc();
return static_cast<T*>(ptr);
}
void deallocate(T* ptr, size_type /*n*/) const noexcept {
av_free(ptr);
}
};
template <typename T, typename S>
bool operator==(allocator<T> const &, allocator<S> const &) noexcept { return true; }
template <typename T, typename S>
bool operator!=(allocator<T> const &, allocator<S> const &) noexcept { return false; }
}