forked from mirrorer/libbpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x265_glue.c
210 lines (180 loc) · 5.7 KB
/
x265_glue.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* x265 encoder front-end
*
* Copyright (c) 2014 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include "bpgenc.h"
#include "x265.h"
struct HEVCEncoderContext {
x265_encoder *enc;
x265_picture *pic;
uint8_t *buf;
int buf_len, buf_size;
};
static HEVCEncoderContext *x265_open(const HEVCEncodeParams *params)
{
HEVCEncoderContext *s;
x265_param *p;
int preset_index;
const char *preset;
s = malloc(sizeof(HEVCEncoderContext));
memset(s, 0, sizeof(*s));
if (params->bit_depth != x265_max_bit_depth) {
fprintf(stderr, "x265 is compiled to support only %d bit depth. Use the '-b %d' option to force the bit depth.\n",
x265_max_bit_depth, x265_max_bit_depth);
return NULL;
}
if (params->chroma_format == BPG_FORMAT_GRAY) {
fprintf(stderr, "x265 does not support monochrome (or alpha) data yet. Plase use the jctvc encoder.\n");
return NULL;
}
p = x265_param_alloc();
preset_index = params->compress_level; /* 9 is placebo */
preset = x265_preset_names[preset_index];
if (params->verbose)
printf("Using x265 preset: %s\n", preset);
x265_param_default_preset(p, preset, "ssim");
p->bRepeatHeaders = 1;
p->decodedPictureHashSEI = params->sei_decoded_picture_hash;
p->sourceWidth = params->width;
p->sourceHeight = params->height;
switch(params->chroma_format) {
case BPG_FORMAT_GRAY:
p->internalCsp = X265_CSP_I400;
break;
case BPG_FORMAT_420:
p->internalCsp = X265_CSP_I420;
break;
case BPG_FORMAT_422:
p->internalCsp = X265_CSP_I422;
break;
case BPG_FORMAT_444:
p->internalCsp = X265_CSP_I444;
break;
default:
abort();
}
if (params->intra_only) {
p->keyframeMax = 1; /* only I frames */
p->totalFrames = 1;
} else {
p->keyframeMax = 250;
p->totalFrames = 0;
p->maxNumReferences = 1;
p->bframes = 0;
}
p->bEnableRectInter = 1;
p->bEnableAMP = 1; /* cannot use 0 due to header restriction */
p->internalBitDepth = params->bit_depth;
p->bEmitInfoSEI = 0;
if (params->verbose)
p->logLevel = X265_LOG_INFO;
else
p->logLevel = X265_LOG_NONE;
/* dummy frame rate */
p->fpsNum = 25;
p->fpsDenom = 1;
p->rc.rateControlMode = X265_RC_CQP;
/* XXX: why do we need this offset to match the JCTVC quality ? */
if (params->bit_depth == 10)
p->rc.qp = params->qp + 7;
else
p->rc.qp = params->qp + 1;
p->bLossless = params->lossless;
s->enc = x265_encoder_open(p);
s->pic = x265_picture_alloc();
x265_picture_init(p, s->pic);
s->pic->colorSpace = p->internalCsp;
x265_param_free(p);
return s;
}
static void add_nal(HEVCEncoderContext *s, const uint8_t *data, int data_len)
{
int new_size, size;
size = s->buf_len + data_len;
if (size > s->buf_size) {
new_size = (s->buf_size * 3) / 2;
if (new_size < size)
new_size = size;
s->buf = realloc(s->buf, new_size);
s->buf_size = new_size;
}
memcpy(s->buf + s->buf_len, data, data_len);
s->buf_len += data_len;
}
static int x265_encode(HEVCEncoderContext *s, Image *img)
{
int c_count, i, ret;
x265_picture *pic;
uint32_t nal_count;
x265_nal *p_nal;
pic = s->pic;
if (img->format == BPG_FORMAT_GRAY)
c_count = 1;
else
c_count = 3;
for(i = 0; i < c_count; i++) {
pic->planes[i] = img->data[i];
pic->stride[i] = img->linesize[i];
}
pic->bitDepth = img->bit_depth;
ret = x265_encoder_encode(s->enc, &p_nal, &nal_count, pic, NULL);
if (ret > 0) {
for(i = 0; i < nal_count; i++) {
add_nal(s, p_nal[i].payload, p_nal[i].sizeBytes);
}
}
return 0;
}
static int x265_close(HEVCEncoderContext *s, uint8_t **pbuf)
{
int buf_len, ret, i;
uint32_t nal_count;
x265_nal *p_nal;
/* get last compressed pictures */
for(;;) {
ret = x265_encoder_encode(s->enc, &p_nal, &nal_count, NULL, NULL);
if (ret <= 0)
break;
for(i = 0; i < nal_count; i++) {
add_nal(s, p_nal[i].payload, p_nal[i].sizeBytes);
}
}
if (s->buf_len < s->buf_size) {
s->buf = realloc(s->buf, s->buf_len);
}
*pbuf = s->buf;
buf_len = s->buf_len;
x265_encoder_close(s->enc);
x265_picture_free(s->pic);
free(s);
return buf_len;
}
HEVCEncoder x265_hevc_encoder = {
.open = x265_open,
.encode = x265_encode,
.close = x265_close,
};