-
Notifications
You must be signed in to change notification settings - Fork 10
/
ImageEncoder.cpp
162 lines (127 loc) · 3.12 KB
/
ImageEncoder.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
#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);
png_byte* frameBytes;
frame->GetBytes((void**) &frameBytes);
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);
}
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;
}