-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathpng_saver.h
58 lines (49 loc) · 1.5 KB
/
png_saver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once
#include <memory>
#include <stdexcept>
#include <string>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/frame.h>
}
class PngSaver {
public:
class EncodingException : public std::runtime_error {
public:
explicit EncodingException(const std::string& message) : std::runtime_error(message) {}
};
class IOException : public std::runtime_error {
public:
explicit IOException(const std::string& message) : std::runtime_error(message) {}
};
static void save(const AVFrame* frame, const std::string& filename);
private:
struct AVFrameDeleter {
void operator()(AVFrame* frame) const {
if (frame) {
av_freep(&frame->data[0]);
av_frame_free(&frame);
}
}
};
struct AVCodecContextDeleter {
void operator()(AVCodecContext* codec_ctx) const {
if (codec_ctx) {
avcodec_free_context(&codec_ctx);
}
}
};
struct AVPacketDeleter {
void operator()(AVPacket* packet) const {
if (packet) {
av_packet_free(&packet);
}
}
};
using AVFramePtr = std::unique_ptr<AVFrame, AVFrameDeleter>;
using AVCodecContextPtr = std::unique_ptr<AVCodecContext, AVCodecContextDeleter>;
using AVPacketPtr = std::unique_ptr<AVPacket, AVPacketDeleter>;
static void save_with_ffmpeg(const AVFrame* frame, const std::string& filename);
static void save_with_stb(const AVFrame* frame, const std::string& filename);
static AVFrame* convert(const AVFrame* frame, const AVPixelFormat output_format);
};