1 /*
2 * libx265 encoder
3 *
4 * Copyright (c) 2013-2014 Derek Buitenhuis
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #if defined(_MSC_VER)
24 #define X265_API_IMPORTS 1
25 #endif
26
27 #include <x265.h>
29
36
39
42
48
50 {
51 switch (naltype) {
52 case NAL_UNIT_CODED_SLICE_BLA_W_LP:
53 case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
54 case NAL_UNIT_CODED_SLICE_BLA_N_LP:
55 case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
56 case NAL_UNIT_CODED_SLICE_IDR_N_LP:
57 case NAL_UNIT_CODED_SLICE_CRA:
58 return 1;
59 default:
60 return 0;
61 }
62 }
63
65 {
67
69
70 x265_param_free(ctx->
params);
71
73 x265_encoder_close(ctx->
encoder);
74
75 return 0;
76 }
77
79 {
81
85 "4:2:2 and 4:4:4 support is not fully defined for HEVC yet. "
86 "Set -strict experimental to encode anyway.\n");
88 }
89
94 }
95
96 ctx->
params = x265_param_alloc();
100 }
101
105 }
106
113
115 char sar[12];
116 int sar_num, sar_den;
117
121 snprintf(sar,
sizeof(sar),
"%d:%d", sar_num, sar_den);
122 if (x265_param_parse(ctx->
params,
"sar", sar) == X265_PARAM_BAD_VALUE) {
125 }
126 }
127
131 ctx->
params->internalCsp = X265_CSP_I420;
132 break;
135 ctx->
params->internalCsp = X265_CSP_I422;
136 break;
139 ctx->
params->internalCsp = X265_CSP_I444;
140 break;
141 }
142
144 char crf[6];
145
147 if (x265_param_parse(ctx->
params,
"crf", crf) == X265_PARAM_BAD_VALUE) {
150 }
153 ctx->
params->rc.rateControlMode = X265_RC_ABR;
154 }
155
157 ctx->
params->bRepeatHeaders = 1;
158
162
165 int parse_ret = x265_param_parse(ctx->
params, en->
key, en->
value);
166
167 switch (parse_ret) {
168 case X265_PARAM_BAD_NAME:
170 "Unknown option: %s.\n", en->
key);
171 break;
172 case X265_PARAM_BAD_VALUE:
174 "Invalid value for %s: %s.\n", en->
key, en->
value);
175 break;
176 default:
177 break;
178 }
179 }
181 }
182 }
183
189 }
190
192 x265_nal *nal;
193 int nnal;
194
200 }
201
205 "Cannot allocate HEVC header of size %d.\n", avctx->
extradata_size);
208 }
209
211 }
212
213 return 0;
214 }
215
217 const AVFrame *pic,
int *got_packet)
218 {
220 x265_picture x265pic;
221 x265_picture x265pic_out = { { 0 } };
222 x265_nal *nal;
224 int payload = 0;
225 int nnal;
227 int i;
228
229 x265_picture_init(ctx->
params, &x265pic);
230
231 if (pic) {
232 for (i = 0; i < 3; i++) {
233 x265pic.planes[i] = pic->
data[i];
234 x265pic.stride[i] = pic->
linesize[i];
235 }
236
237 x265pic.pts = pic->
pts;
239
243 X265_TYPE_AUTO;
244 }
245
246 ret = x265_encoder_encode(ctx->
encoder, &nal, &nnal,
247 pic ? &x265pic :
NULL, &x265pic_out);
248 if (ret < 0)
250
251 if (!nnal)
252 return 0;
253
254 for (i = 0; i < nnal; i++)
255 payload += nal[i].sizeBytes;
256
258 if (ret < 0) {
261 }
263
264 for (i = 0; i < nnal; i++) {
265 memcpy(dst, nal[i].payload, nal[i].sizeBytes);
266 dst += nal[i].sizeBytes;
267
270 }
271
272 pkt->
pts = x265pic_out.pts;
273 pkt->
dts = x265pic_out.dts;
274
275 *got_packet = 1;
276 return 0;
277 }
278
284 };
285
294 };
295
297 {
298 if (x265_max_bit_depth == 8)
300 else if (x265_max_bit_depth == 12)
302 }
303
304 #define OFFSET(x) offsetof(libx265Context, x)
305 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
310 {
"x265-params",
"set the x265 configuration using a :-separated list of key=value parameters",
OFFSET(x265_opts),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
VE },
312 };
313
319 };
320
322 { "b", "0" },
324 };
325
336 .priv_class = &class,
339 };