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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
/* encx265.c
Copyright (c) 2003-2015 HandBrake Team
This file is part of the HandBrake source code
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License v2.
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#ifdef USE_X265
#include "hb.h"
#include "hb_dict.h"
#include "h265_common.h"
#include "x265.h"
int encx265Init (hb_work_object_t*, hb_job_t*);
int encx265Work (hb_work_object_t*, hb_buffer_t**, hb_buffer_t**);
void encx265Close(hb_work_object_t*);
hb_work_object_t hb_encx265 =
{
WORK_ENCX265,
"H.265/HEVC encoder (libx265)",
encx265Init,
encx265Work,
encx265Close,
};
#define FRAME_INFO_MAX2 (8) // 2^8 = 256; 90000/256 = 352 frames/sec
#define FRAME_INFO_MIN2 (17) // 2^17 = 128K; 90000/131072 = 1.4 frames/sec
#define FRAME_INFO_SIZE (1 << (FRAME_INFO_MIN2 - FRAME_INFO_MAX2 + 1))
#define FRAME_INFO_MASK (FRAME_INFO_SIZE - 1)
static const char * const hb_x265_encopt_synonyms[][2] =
{
{ "me", "motion", },
{ NULL, NULL, },
};
struct hb_work_private_s
{
hb_job_t *job;
x265_encoder *x265;
x265_param *param;
int64_t last_stop;
uint32_t frames_in;
hb_list_t *delayed_chapters;
int64_t next_chapter_pts;
struct
{
int64_t duration;
}
frame_info[FRAME_INFO_SIZE];
char csvfn[1024];
};
// used in delayed_chapters list
struct chapter_s
{
int index;
int64_t start;
};
static int param_parse(x265_param *param, const char *key, const char *value)
{
int ret = x265_param_parse(param, key, value);
// let x265 sanity check the options for us
switch (ret)
{
case X265_PARAM_BAD_NAME:
hb_log("encx265: unknown option '%s'", key);
break;
case X265_PARAM_BAD_VALUE:
hb_log("encx265: bad argument '%s=%s'", key, value ? value : "(null)");
break;
default:
break;
}
return ret;
}
/***********************************************************************
* hb_work_encx265_init
***********************************************************************
*
**********************************************************************/
int encx265Init(hb_work_object_t *w, hb_job_t *job)
{
hb_work_private_t *pv = calloc(1, sizeof(hb_work_private_t));
pv->next_chapter_pts = AV_NOPTS_VALUE;
pv->delayed_chapters = hb_list_init();
pv->job = job;
w->private_data = pv;
int ret, vrate, vrate_base;
x265_nal *nal;
uint32_t nnal;
x265_param *param = pv->param = x265_param_alloc();
if (x265_param_default_preset(param,
job->encoder_preset, job->encoder_tune) < 0)
{
goto fail;
}
/* If the PSNR or SSIM tunes are in use, enable the relevant metric */
param->bEnablePsnr = param->bEnableSsim = 0;
if (job->encoder_tune != NULL && *job->encoder_tune)
{
char *tmp = strdup(job->encoder_tune);
char *tok = strtok(tmp, ",./-+");
do
{
if (!strncasecmp(tok, "psnr", 4))
{
param->bEnablePsnr = 1;
break;
}
if (!strncasecmp(tok, "ssim", 4))
{
param->bEnableSsim = 1;
break;
}
}
while ((tok = strtok(NULL, ",./-+")) != NULL);
free(tmp);
}
/*
* Some HandBrake-specific defaults; users can override them
* using the encoder_options string.
*/
hb_reduce(&vrate, &vrate_base, job->vrate, job->vrate_base);
param->fpsNum = vrate;
param->fpsDenom = vrate_base;
param->keyframeMin = (int)((double)vrate / (double)vrate_base + 0.5);
param->keyframeMax = param->keyframeMin * 10;
/*
* Video Signal Type (color description only).
*
* Use x265_param_parse (let x265 determine which bEnable
* flags, if any, should be set in the x265_param struct).
*/
char colorprim[11], transfer[11], colormatrix[11];
switch (job->color_matrix_code)
{
case 1: // ITU BT.601 DVD or SD TV content (NTSC)
strcpy(colorprim, "smpte170m");
strcpy(transfer, "bt709");
strcpy(colormatrix, "smpte170m");
break;
case 2: // ITU BT.601 DVD or SD TV content (PAL)
strcpy(colorprim, "bt470bg");
strcpy(transfer, "bt709");
strcpy(colormatrix, "smpte170m");
break;
case 3: // ITU BT.709 HD content
strcpy(colorprim, "bt709");
strcpy(transfer, "bt709");
strcpy(colormatrix, "bt709");
break;
case 4: // custom
snprintf(colorprim, sizeof(colorprim), "%d", job->color_prim);
snprintf(transfer, sizeof(transfer), "%d", job->color_transfer);
snprintf(colormatrix, sizeof(colormatrix), "%d", job->color_matrix);
break;
default: // detected during scan
snprintf(colorprim, sizeof(colorprim), "%d", job->title->color_prim);
snprintf(transfer, sizeof(transfer), "%d", job->title->color_transfer);
snprintf(colormatrix, sizeof(colormatrix), "%d", job->title->color_matrix);
break;
}
if (param_parse(param, "colorprim", colorprim) ||
param_parse(param, "transfer", transfer) ||
param_parse(param, "colormatrix", colormatrix))
{
goto fail;
}
/* iterate through x265_opts and parse the options */
hb_dict_entry_t *entry = NULL;
hb_dict_t *x265_opts = hb_encopts_to_dict(job->encoder_options, job->vcodec);
while ((entry = hb_dict_next(x265_opts, entry)) != NULL)
{
// here's where the strings are passed to libx265 for parsing
if (param_parse(param, entry->key, entry->value))
{
hb_dict_free(&x265_opts);
goto fail;
}
}
hb_dict_free(&x265_opts);
/*
* Reload colorimetry settings in case custom
* values were set in the encoder_options string.
*/
job->color_matrix_code = 4;
job->color_prim = param->vui.colorPrimaries;
job->color_transfer = param->vui.transferCharacteristics;
job->color_matrix = param->vui.matrixCoeffs;
/*
* Settings which can't be overriden in the encodeer_options string
* (muxer-specific settings, resolution, ratecontrol, etc.).
*/
param->bRepeatHeaders = 0;
param->sourceWidth = job->width;
param->sourceHeight = job->height;
if (job->anamorphic.mode)
{
/*
* Let x265 determnine whether to use an aspect ratio
* index vs. the extended SAR index + SAR width/height.
*/
char sar[22];
snprintf(sar, sizeof(sar), "%d:%d",
job->anamorphic.par_width, job->anamorphic.par_height);
if (param_parse(param, "sar", sar))
{
goto fail;
}
}
if (job->vquality > 0)
{
param->rc.rateControlMode = X265_RC_CRF;
param->rc.rfConstant = job->vquality;
}
else
{
param->rc.rateControlMode = X265_RC_ABR;
param->rc.bitrate = job->vbitrate;
if (job->pass > 0 && job->pass < 3)
{
char stats_file[1024] = "";
char pass[2];
snprintf(pass, sizeof(pass), "%d", job->pass);
hb_get_tempory_filename(job->h, stats_file, "x265.log");
if (param_parse(param, "stats", stats_file) ||
param_parse(param, "pass", pass))
{
goto fail;
}
if (job->pass == 1 && job->fastfirstpass == 0 &&
param_parse(param, "slow-firstpass", "1"))
{
goto fail;
}
}
}
/* statsfile (but not 2-pass) */
memset(pv->csvfn, 0, sizeof(pv->csvfn));
if (param->logLevel >= X265_LOG_DEBUG)
{
if (param->csvfn == NULL)
{
hb_get_tempory_filename(job->h, pv->csvfn, "x265.csv");
param->csvfn = pv->csvfn;
}
else
{
strncpy(pv->csvfn, param->csvfn, sizeof(pv->csvfn));
}
}
/* Apply profile and level settings last. */
if (job->encoder_profile != NULL &&
strcasecmp(job->encoder_profile, hb_h265_profile_names[0]) != 0 &&
x265_param_apply_profile(param, job->encoder_profile) < 0)
{
goto fail;
}
/* we should now know whether B-frames are enabled */
job->areBframes = (param->bframes > 0) + (param->bframes > 0 &&
param->bBPyramid > 0);
pv->x265 = x265_encoder_open(param);
if (pv->x265 == NULL)
{
hb_error("encx265: x265_encoder_open failed.");
goto fail;
}
/*
* x265's output (headers and bitstream) are in Annex B format.
*
* Write the header as is, and let the muxer reformat
* the extradata and output bitstream properly for us.
*/
ret = x265_encoder_headers(pv->x265, &nal, &nnal);
if (ret < 0)
{
hb_error("encx265: x265_encoder_headers failed (%d)", ret);
goto fail;
}
if (ret > sizeof(w->config->h265.headers))
{
hb_error("encx265: bitstream headers too large (%d)", ret);
goto fail;
}
memcpy(w->config->h265.headers, nal->payload, ret);
w->config->h265.headers_length = ret;
return 0;
fail:
w->private_data = NULL;
free(pv);
return 1;
}
void encx265Close(hb_work_object_t *w)
{
hb_work_private_t *pv = w->private_data;
if (pv->delayed_chapters != NULL)
{
struct chapter_s *item;
while ((item = hb_list_item(pv->delayed_chapters, 0)) != NULL)
{
hb_list_rem(pv->delayed_chapters, item);
free(item);
}
hb_list_close(&pv->delayed_chapters);
}
x265_param_free(pv->param);
x265_encoder_close(pv->x265);
free(pv);
w->private_data = NULL;
}
/*
* see comments in definition of 'frame_info' in pv struct for description
* of what these routines are doing.
*/
static void save_frame_info(hb_work_private_t *pv, hb_buffer_t *in)
{
int i = (in->s.start >> FRAME_INFO_MAX2) & FRAME_INFO_MASK;
pv->frame_info[i].duration = in->s.stop - in->s.start;
}
static int64_t get_frame_duration(hb_work_private_t * pv, int64_t pts)
{
int i = (pts >> FRAME_INFO_MAX2) & FRAME_INFO_MASK;
return pv->frame_info[i].duration;
}
static hb_buffer_t* nal_encode(hb_work_object_t *w,
x265_picture *pic_out,
x265_nal *nal, uint32_t nnal)
{
hb_work_private_t *pv = w->private_data;
hb_job_t *job = pv->job;
hb_buffer_t *buf = NULL;
int i;
if (nnal <= 0)
{
return NULL;
}
buf = hb_video_buffer_init(job->width, job->height);
if (buf == NULL)
{
return NULL;
}
buf->size = 0;
// copy the bitstream data
for (i = 0; i < nnal; i++)
{
memcpy(buf->data + buf->size, nal[i].payload, nal[i].sizeBytes);
buf->size += nal[i].sizeBytes;
}
// use the pts to get the original frame's duration.
buf->s.duration = get_frame_duration(pv, pic_out->pts);
buf->s.stop = pic_out->pts + buf->s.duration;
buf->s.start = pic_out->pts;
buf->s.renderOffset = pic_out->dts;
if (w->config->h264.init_delay == 0 && pic_out->dts < 0)
{
w->config->h264.init_delay -= pic_out->dts;
}
switch (pic_out->sliceType)
{
case X265_TYPE_IDR:
buf->s.frametype = HB_FRAME_IDR;
break;
case X265_TYPE_I:
buf->s.frametype = HB_FRAME_I;
break;
case X265_TYPE_P:
buf->s.frametype = HB_FRAME_P;
break;
case X265_TYPE_B:
buf->s.frametype = HB_FRAME_B;
break;
case X265_TYPE_BREF:
buf->s.frametype = HB_FRAME_BREF;
break;
default:
buf->s.frametype = 0;
break;
}
if (pv->next_chapter_pts != AV_NOPTS_VALUE &&
pv->next_chapter_pts <= pic_out->pts &&
pic_out->sliceType == X265_TYPE_IDR)
{
// we're no longer looking for this chapter
pv->next_chapter_pts = AV_NOPTS_VALUE;
// get the chapter index from the list
struct chapter_s *item = hb_list_item(pv->delayed_chapters, 0);
if (item != NULL)
{
// we're done with this chapter
hb_list_rem(pv->delayed_chapters, item);
buf->s.new_chap = item->index;
free(item);
// we may still have another pending chapter
item = hb_list_item(pv->delayed_chapters, 0);
if (item != NULL)
{
// we're looking for this one now
// we still need it, don't remove it
pv->next_chapter_pts = item->start;
}
}
}
// discard empty buffers (no video)
if (buf->size <= 0)
{
hb_buffer_close(&buf);
}
return buf;
}
static hb_buffer_t* x265_encode(hb_work_object_t *w, hb_buffer_t *in)
{
hb_work_private_t *pv = w->private_data;
hb_job_t *job = pv->job;
x265_picture pic_in, pic_out;
x265_nal *nal;
uint32_t nnal;
x265_picture_init(pv->param, &pic_in);
pic_in.stride[0] = in->plane[0].stride;
pic_in.stride[1] = in->plane[1].stride;
pic_in.stride[2] = in->plane[2].stride;
pic_in.planes[0] = in->plane[0].data;
pic_in.planes[1] = in->plane[1].data;
pic_in.planes[2] = in->plane[2].data;
pic_in.poc = pv->frames_in++;
pic_in.pts = in->s.start;
pic_in.bitDepth = 8;
if (in->s.new_chap && job->chapter_markers)
{
if (pv->next_chapter_pts == AV_NOPTS_VALUE)
{
pv->next_chapter_pts = in->s.start;
}
/*
* Chapter markers are sometimes so close we can get a new one before
* the previous marker has been through the encoding queue.
*
* Dropping markers can cause weird side-effects downstream, including
* but not limited to missing chapters in the output, so we need to save
* it somehow.
*/
struct chapter_s *item = malloc(sizeof(struct chapter_s));
if (item != NULL)
{
item->start = in->s.start;
item->index = in->s.new_chap;
hb_list_add(pv->delayed_chapters, item);
}
/* don't let 'work_loop' put a chapter mark on the wrong buffer */
in->s.new_chap = 0;
/*
* Chapters have to start with an IDR frame so request that this frame be
* coded as IDR. Since there may be up to 16 frames currently buffered in
* the encoder, remember the timestamp so when this frame finally pops out
* of the encoder we'll mark its buffer as the start of a chapter.
*/
pic_in.sliceType = X265_TYPE_IDR;
}
else
{
pic_in.sliceType = X265_TYPE_AUTO;
}
if (pv->last_stop != in->s.start)
{
hb_log("encx265 input continuity err: last stop %"PRId64" start %"PRId64,
pv->last_stop, in->s.start);
}
pv->last_stop = in->s.stop;
save_frame_info(pv, in);
if (x265_encoder_encode(pv->x265, &nal, &nnal, &pic_in, &pic_out) > 0)
{
return nal_encode(w, &pic_out, nal, nnal);
}
return NULL;
}
int encx265Work(hb_work_object_t *w, hb_buffer_t **buf_in, hb_buffer_t **buf_out)
{
hb_work_private_t *pv = w->private_data;
hb_buffer_t *in = *buf_in;
if (in->size <= 0)
{
uint32_t nnal;
x265_nal *nal;
x265_picture pic_out;
hb_buffer_t *last_buf = NULL;
// flush delayed frames
while (x265_encoder_encode(pv->x265, &nal, &nnal, NULL, &pic_out) > 0)
{
hb_buffer_t *buf = nal_encode(w, &pic_out, nal, nnal);
if (buf != NULL)
{
if (last_buf == NULL)
{
*buf_out = buf;
}
else
{
last_buf->next = buf;
}
last_buf = buf;
}
}
// add the EOF to the end of the chain
if (last_buf == NULL)
{
*buf_out = in;
}
else
{
last_buf->next = in;
}
*buf_in = NULL;
return HB_WORK_DONE;
}
*buf_out = x265_encode(w, in);
return HB_WORK_OK;
}
const char* hb_x265_encopt_name(const char *name)
{
int i;
for (i = 0; hb_x265_encopt_synonyms[i][0] != NULL; i++)
if (!strcmp(name, hb_x265_encopt_synonyms[i][1]))
return hb_x265_encopt_synonyms[i][0];
return name;
}
#endif
|