Repository navigation
Expand file tree
/
Copy pathJpgEncoder.cpp
More file actions
92 lines (76 loc) · 1.75 KB
/
Copy pathJpgEncoder.cpp
File metadata and controls
92 lines (76 loc) · 1.75 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
#include <stdexcept>
#include "JpgEncoder.hpp"
#include "util/debug.h"
JpgEncoder::JpgEncoder(unsigned int prePadding, unsigned int postPadding)
: mTjHandle(tjInitCompress()),
mSubsampling(TJSAMP_420),
mEncodedData(NULL),
mPrePadding(prePadding),
mPostPadding(postPadding),
mMaxWidth(0),
mMaxHeight(0)
{
}
JpgEncoder::~JpgEncoder() {
tjFree(mEncodedData);
}
bool
JpgEncoder::encode(Minicap::Frame* frame, unsigned int quality) {
unsigned char* offset = getEncodedData();
return 0 == tjCompress2(
mTjHandle,
(unsigned char*) frame->data,
frame->width,
frame->stride * frame->bpp,
frame->height,
convertFormat(frame->format),
&offset,
&mEncodedSize,
mSubsampling,
quality,
TJFLAG_FASTDCT | TJFLAG_NOREALLOC
);
}
int
JpgEncoder::getEncodedSize() {
return mEncodedSize;
}
unsigned char*
JpgEncoder::getEncodedData() {
return mEncodedData + mPrePadding;
}
bool
JpgEncoder::reserveData(uint32_t width, uint32_t height) {
if (width == mMaxWidth && height == mMaxHeight) {
return 0;
}
tjFree(mEncodedData);
unsigned long maxSize = mPrePadding + mPostPadding + tjBufSize(
width,
height,
mSubsampling
);
MCINFO("Allocating %ld bytes for JPG encoder", maxSize);
mEncodedData = tjAlloc(maxSize);
if (mEncodedData == NULL) {
return false;
}
mMaxWidth = width;
mMaxHeight = height;
return true;
}
int
JpgEncoder::convertFormat(Minicap::Format format) {
switch (format) {
case Minicap::FORMAT_RGBA_8888:
return TJPF_RGBA;
case Minicap::FORMAT_RGBX_8888:
return TJPF_RGBX;
case Minicap::FORMAT_RGB_888:
return TJPF_RGB;
case Minicap::FORMAT_BGRA_8888:
return TJPF_BGRA;
default:
throw std::runtime_error("Unsupported pixel format");
}
}