forked from edman007/chiton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_util.cpp
189 lines (161 loc) · 6.02 KB
/
image_util.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**************************************************************************
*
* This file is part of Chiton.
*
* Chiton is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiton is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiton. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright 2021 Ed Martin <[email protected]>
*
**************************************************************************
*/
#include "image_util.hpp"
#include "util.hpp"
#include "file_manager.hpp"
#include "filter.hpp"
ImageUtil::ImageUtil(Database &db, Config &cfg) : db(db), cfg(cfg) {
}
ImageUtil::~ImageUtil(){
}
bool ImageUtil::write_frame_jpg(const AVFrame *frame, std::string &name, const struct timeval *start_time /* = NULL */, rect src/* = {-1, -1, 0, 0}*/){
LDEBUG("Writing JPEG");
//check if it's valid
if (!frame){
LWARN("Cannot write frame out as image, invalid format");
return false;
}
const AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if (!codec) {
LWARN("Cannot find MJPEG encoder!");
return false;
}
AVFrame *cropped_frame = NULL;
if (format_supported(frame, codec)){
LDEBUG("Format is supported" + std::to_string(frame->format));
cropped_frame = apply_rect(frame, src);
} else {
//run it through a single loop of the filter
LDEBUG("Filtering prior to writing");
Filter flt(cfg);
flt.set_target_fmt(codec->pix_fmts[0], AV_CODEC_ID_MJPEG, FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT);
flt.set_source_time_base({1, 1});//I don't think it actually matters...
flt.send_frame(frame);
AVFrame *filtered_frame = av_frame_alloc();
flt.get_frame(filtered_frame);
cropped_frame = apply_rect(filtered_frame, src);
av_frame_free(&filtered_frame);
}
AVCodecContext* c = avcodec_alloc_context3(codec);
if (!c) {
LWARN("Could not allocate video codec context");
return false;
}
//should do avcodec_parameters_to_context or something?
c->bit_rate = 400000;
c->width = cropped_frame->width - cropped_frame->crop_left - cropped_frame->crop_right;
c->height = cropped_frame->height - cropped_frame->crop_top - cropped_frame->crop_bottom;
c->time_base = (AVRational){1,1};
c->pix_fmt = static_cast<enum AVPixelFormat>(cropped_frame->format);
c->sample_aspect_ratio = frame->sample_aspect_ratio;
LINFO("Exporting JPEG " + std::to_string(c->width) + "x" + std::to_string(c->height));
gcff_util.lock();
if (avcodec_open2(c, codec, NULL) < 0){
gcff_util.unlock();
avcodec_free_context(&c);
LWARN("Failed to open codec");
return false;
}
gcff_util.unlock();
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
int ret = avcodec_send_frame(c, cropped_frame);
if (ret){
//failed...free and exit
LWARN("Failed to Send Frame for encoding");
avcodec_free_context(&c);
av_packet_unref(&pkt);
av_frame_free(&cropped_frame);
return false;
}
ret = avcodec_receive_packet(c, &pkt);
avcodec_free_context(&c);
av_frame_free(&cropped_frame);
if (ret){
LWARN("Failed to receive decoded frame");
//failed...free and exit
av_packet_unref(&pkt);
return false;
}
//write out the packet
FileManager fm(db, cfg);
std::string path;
bool success = false;
if (fm.get_image_path(path, name, ".jpg", start_time)){
std::ofstream out_s = fm.get_fstream_write(name, path);
LWARN("Writing to " + name);
if (out_s.is_open()){
out_s.write(reinterpret_cast<char*>(pkt.data), pkt.size);
success = out_s.good();
out_s.close();
} else {
LWARN("Couldn't open JPG");
}
} else {
LWARN("Failed to get image path");
}
av_packet_unref(&pkt);
return success;
}
bool ImageUtil::write_frame_png(const AVFrame *frame, std::string &name, const struct timeval *start_time /* = NULL */, rect src/* = {-1, -1, 0, 0}*/){
LWARN("Can't write out PNGs");
return false;
}
AVFrame* ImageUtil::apply_rect(const AVFrame *frame, rect &src){
//copy the frame
AVFrame* out = av_frame_clone(frame);
//adjust rect and apply if x is not negative
if (src.x >= 0){
//correct any numbers
if (static_cast<unsigned int>(src.x) < frame->crop_left){
src.x = frame->crop_left;
}
if (static_cast<unsigned int>(src.y) < frame->crop_bottom){
src.y = frame->crop_bottom;
}
if (static_cast<unsigned int>(src.w) > (frame->width - src.x - frame->crop_right)){
src.w = frame->width - src.x - frame->crop_right;
}
if (static_cast<unsigned int>(src.h) > (frame->height - src.y - frame->crop_top)){
src.h = frame->height - src.y - frame->crop_top;
}
//and apply the new numbers
out->crop_left = src.x;
out->crop_bottom = src.y;
out->crop_right = out->width - out->crop_left - src.w;
out->crop_top = out->height - out->crop_bottom - src.h;
}
return out;
}
bool ImageUtil::format_supported(const AVFrame *frame, const AVCodec *codec){
if (!codec->pix_fmts){
return true;//is this right? it's actually unknown...but I guess the filter won't help
}
for (const enum AVPixelFormat *p = codec->pix_fmts; *p != AV_PIX_FMT_NONE; p++){
if (*p == static_cast<const enum AVPixelFormat>(frame->format)){
return true;
}
}
return false;
}