forked from voc/decklink-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageEncoder.cpp
More file actions
192 lines (152 loc) · 3.96 KB
/
ImageEncoder.cpp
File metadata and controls
192 lines (152 loc) · 3.96 KB
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
190
191
192
#include "ImageEncoder.h"
#include <sstream>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <png.h>
#include "MutableVideoFrame.h"
ImageEncoder::ImageEncoder(DeviceProber* deviceProber) :
m_refCount(1),
m_deviceProber(deviceProber)
{
m_frameConverter = CreateVideoConversionInstance();
}
ULONG ImageEncoder::AddRef(void)
{
return __sync_add_and_fetch(&m_refCount, 1);
}
ULONG ImageEncoder::Release(void)
{
int32_t newRefValue = __sync_sub_and_fetch(&m_refCount, 1);
if (newRefValue == 0)
{
m_frameConverter->Release(); // does not assert to 0
delete this;
return 0;
}
return newRefValue;
}
std::string ImageEncoder::EncodeImage() {
IDeckLinkVideoFrame* frame = m_deviceProber->GetLastFrame();
if(frame == NULL) {
return "";
}
frame->AddRef();
frame = convertFrameIfReqired(frame);
std::string encodedImage = encodeToPng(frame);
frame->Release();
return encodedImage;
}
void pngWriteCallback(png_structp png_ptr, png_bytep data, png_size_t length)
{
std::stringstream* stream = (std::stringstream*)png_get_io_ptr(png_ptr);
std::string str = std::string((const char*)data, length);
(*stream) << str;
}
std::string ImageEncoder::encodeToPng(IDeckLinkVideoFrame* frame)
{
png_structp png_ptr = png_create_write_struct(
PNG_LIBPNG_VER_STRING,
/* user_error_ptr */ NULL,
/* user_error_fn */ NULL,
/* user_warning_fn */ NULL);
if (!png_ptr) {
std::cerr << "Unable to allocate png_structp" << std::endl;
exit(1);
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
std::cerr << "Unable to allocate png_infop" << std::endl;
exit(1);
}
if (setjmp(png_jmpbuf(png_ptr))) {
std::cerr << "Unable to setup png_jmpbuf" << std::endl;
exit(1);
}
png_set_IHDR(
png_ptr,
info_ptr,
frame->GetWidth(),
frame->GetHeight(),
/* bit_depth */ 8,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
IDeckLinkVideoBuffer* videoBuffer;
png_byte* frameBytes;
HRESULT result = frame->QueryInterface(IID_IDeckLinkVideoBuffer,
(void**)&videoBuffer);
if (result != S_OK)
{
std::cerr << "Unable to query the IDeckLinkVideoFrame interface" << std::endl;
exit(1);
}
// Prepare the buffer for CPU access
result = videoBuffer->StartAccess(bmdBufferAccessWrite);
if (result != S_OK)
{
std::cerr << "Unable to prepare the buffer for CPU access" << std::endl;
exit(1);
}
// Access underlying frame buffer address
result = videoBuffer->GetBytes((void **)&frameBytes);
if (result != S_OK)
{
std::cerr << "Unable to access the underlying frame buffer address" << std::endl;
exit(1);
}
png_bytepp row_pointers = new png_bytep[frame->GetHeight()];
for(long row = 0; row < frame->GetHeight(); row++)
{
row_pointers[row] = frameBytes + (row * frame->GetRowBytes());
}
png_set_rows(
png_ptr,
info_ptr,
row_pointers);
std::stringstream pngBody;
png_set_write_fn(
png_ptr,
&pngBody,
pngWriteCallback,
NULL);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_BGR, NULL);
if (info_ptr != NULL)
{
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
}
if (png_ptr != NULL)
{
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
}
// End the CPU access - the frameBuffer address is no longer guaranteed
videoBuffer->EndAccess(bmdBufferAccessWrite);
// Release buffer
videoBuffer->Release();
delete row_pointers;
return pngBody.str();
}
IDeckLinkVideoFrame* ImageEncoder::convertFrameIfReqired(IDeckLinkVideoFrame* frame)
{
HRESULT result;
if(frame->GetPixelFormat() == bmdFormat8BitBGRA)
{
return frame;
}
IDeckLinkVideoFrame* convertedFrame = new MutableVideoFrame(
frame->GetWidth(),
frame->GetHeight(),
bmdFormat8BitBGRA);
result = m_frameConverter->ConvertFrame(frame, convertedFrame);
if (result != S_OK)
{
fprintf(stderr, "Failed to convert frame\n");
exit(1);
}
frame->Release();
return convertedFrame;
}