3 Copyright (c) 2003-2015 HandBrake Team
4 This file is part of the HandBrake source code
5 Homepage: <http://handbrake.fr/>.
6 It may be used under the terms of the GNU General Public License v2.
7 For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
13 #include "h265_common.h"
16 int encx265Init (hb_work_object_t
*, hb_job_t
*);
17 int encx265Work (hb_work_object_t
*, hb_buffer_t
**, hb_buffer_t
**);
18 void encx265Close(hb_work_object_t
*);
20 hb_work_object_t hb_encx265
=
23 "H.265/HEVC encoder (libx265)",
29 #define FRAME_INFO_MAX2 (8) // 2^8 = 256; 90000/256 = 352 frames/sec
30 #define FRAME_INFO_MIN2 (17) // 2^17 = 128K; 90000/131072 = 1.4 frames/sec
31 #define FRAME_INFO_SIZE (1 << (FRAME_INFO_MIN2 - FRAME_INFO_MAX2 + 1))
32 #define FRAME_INFO_MASK (FRAME_INFO_SIZE - 1)
34 static const char * const hb_x265_encopt_synonyms
[][2] =
40 struct hb_work_private_s
49 hb_list_t
*delayed_chapters
;
50 int64_t next_chapter_pts
;
56 frame_info
[FRAME_INFO_SIZE
];
61 // used in delayed_chapters list
68 static int param_parse(x265_param
*param
, const char *key
, const char *value
)
70 int ret
= x265_param_parse(param
, key
, value
);
71 // let x265 sanity check the options for us
74 case X265_PARAM_BAD_NAME
:
75 hb_log("encx265: unknown option '%s'", key
);
77 case X265_PARAM_BAD_VALUE
:
78 hb_log("encx265: bad argument '%s=%s'", key
, value
? value
: "(null)");
86 /***********************************************************************
87 * hb_work_encx265_init
88 ***********************************************************************
90 **********************************************************************/
91 int encx265Init(hb_work_object_t
*w
, hb_job_t
*job
)
93 hb_work_private_t
*pv
= calloc(1, sizeof(hb_work_private_t
));
94 pv
->next_chapter_pts
= AV_NOPTS_VALUE
;
95 pv
->delayed_chapters
= hb_list_init();
103 x265_param
*param
= pv
->param
= x265_param_alloc();
105 if (x265_param_default_preset(param
,
106 job
->encoder_preset
, job
->encoder_tune
) < 0)
108 hb_error("encx265: x265_param_default_preset failed. Preset (%s) Tune (%s)", job
->encoder_preset
, job
->encoder_tune
);
112 /* If the PSNR or SSIM tunes are in use, enable the relevant metric */
113 param
->bEnablePsnr
= param
->bEnableSsim
= 0;
114 if (job
->encoder_tune
!= NULL
&& *job
->encoder_tune
)
116 char *tmp
= strdup(job
->encoder_tune
);
117 char *tok
= strtok(tmp
, ",./-+");
120 if (!strncasecmp(tok
, "psnr", 4))
122 param
->bEnablePsnr
= 1;
125 if (!strncasecmp(tok
, "ssim", 4))
127 param
->bEnableSsim
= 1;
131 while ((tok
= strtok(NULL
, ",./-+")) != NULL
);
136 * Some HandBrake-specific defaults; users can override them
137 * using the encoder_options string.
139 hb_reduce(&vrate
.num
, &vrate
.den
, job
->vrate
.num
, job
->vrate
.den
);
140 param
->fpsNum
= vrate
.num
;
141 param
->fpsDenom
= vrate
.den
;
142 param
->keyframeMin
= (double)vrate
.num
/ vrate
.den
+ 0.5;
143 param
->keyframeMax
= param
->keyframeMin
* 10;
146 * Video Signal Type (color description only).
148 * Use x265_param_parse (let x265 determine which bEnable
149 * flags, if any, should be set in the x265_param struct).
151 char colorprim
[11], transfer
[11], colormatrix
[11];
152 switch (job
->color_matrix_code
)
154 case 1: // ITU BT.601 DVD or SD TV content (NTSC)
155 strcpy(colorprim
, "smpte170m");
156 strcpy(transfer
, "bt709");
157 strcpy(colormatrix
, "smpte170m");
159 case 2: // ITU BT.601 DVD or SD TV content (PAL)
160 strcpy(colorprim
, "bt470bg");
161 strcpy(transfer
, "bt709");
162 strcpy(colormatrix
, "smpte170m");
164 case 3: // ITU BT.709 HD content
165 strcpy(colorprim
, "bt709");
166 strcpy(transfer
, "bt709");
167 strcpy(colormatrix
, "bt709");
170 snprintf(colorprim
, sizeof(colorprim
), "%d", job
->color_prim
);
171 snprintf(transfer
, sizeof(transfer
), "%d", job
->color_transfer
);
172 snprintf(colormatrix
, sizeof(colormatrix
), "%d", job
->color_matrix
);
174 default: // detected during scan
175 snprintf(colorprim
, sizeof(colorprim
), "%d", job
->title
->color_prim
);
176 snprintf(transfer
, sizeof(transfer
), "%d", job
->title
->color_transfer
);
177 snprintf(colormatrix
, sizeof(colormatrix
), "%d", job
->title
->color_matrix
);
180 if (param_parse(param
, "colorprim", colorprim
) ||
181 param_parse(param
, "transfer", transfer
) ||
182 param_parse(param
, "colormatrix", colormatrix
))
187 /* iterate through x265_opts and parse the options */
188 hb_dict_t
*x265_opts
;
189 x265_opts
= hb_encopts_to_dict(job
->encoder_options
, job
->vcodec
);
192 for (iter
= hb_dict_iter_init(x265_opts
);
193 iter
!= HB_DICT_ITER_DONE
;
194 iter
= hb_dict_iter_next(x265_opts
, iter
))
196 const char *key
= hb_dict_iter_key(iter
);
197 hb_value_t
*value
= hb_dict_iter_value(iter
);
198 char *str
= hb_value_get_string_xform(value
);
200 // here's where the strings are passed to libx265 for parsing
201 // unknown options or bad values are non-fatal, see encx264.c
202 param_parse(param
, key
, str
);
205 hb_dict_free(&x265_opts
);
208 * Reload colorimetry settings in case custom
209 * values were set in the encoder_options string.
211 job
->color_matrix_code
= 4;
212 job
->color_prim
= param
->vui
.colorPrimaries
;
213 job
->color_transfer
= param
->vui
.transferCharacteristics
;
214 job
->color_matrix
= param
->vui
.matrixCoeffs
;
217 * Settings which can't be overriden in the encodeer_options string
218 * (muxer-specific settings, resolution, ratecontrol, etc.).
220 param
->bRepeatHeaders
= 0;
221 param
->sourceWidth
= job
->width
;
222 param
->sourceHeight
= job
->height
;
225 * Let x265 determnine whether to use an aspect ratio
226 * index vs. the extended SAR index + SAR width/height.
229 snprintf(sar
, sizeof(sar
), "%d:%d", job
->par
.num
, job
->par
.den
);
230 if (param_parse(param
, "sar", sar
))
235 if (job
->vquality
> 0)
237 param
->rc
.rateControlMode
= X265_RC_CRF
;
238 param
->rc
.rfConstant
= job
->vquality
;
242 param
->rc
.rateControlMode
= X265_RC_ABR
;
243 param
->rc
.bitrate
= job
->vbitrate
;
244 if (job
->pass_id
== HB_PASS_ENCODE_1ST
||
245 job
->pass_id
== HB_PASS_ENCODE_2ND
)
247 char stats_file
[1024] = "";
249 snprintf(pass
, sizeof(pass
), "%d", job
->pass_id
);
250 hb_get_tempory_filename(job
->h
, stats_file
, "x265.log");
251 if (param_parse(param
, "stats", stats_file
) ||
252 param_parse(param
, "pass", pass
))
256 if (job
->pass_id
== HB_PASS_ENCODE_1ST
&& job
->fastfirstpass
== 0 &&
257 param_parse(param
, "slow-firstpass", "1"))
264 /* statsfile (but not 2-pass) */
265 memset(pv
->csvfn
, 0, sizeof(pv
->csvfn
));
266 if (param
->logLevel
>= X265_LOG_DEBUG
)
268 if (param
->csvfn
== NULL
)
270 hb_get_tempory_filename(job
->h
, pv
->csvfn
, "x265.csv");
271 param
->csvfn
= pv
->csvfn
;
275 strncpy(pv
->csvfn
, param
->csvfn
, sizeof(pv
->csvfn
));
279 /* Apply profile and level settings last. */
280 if (job
->encoder_profile
!= NULL
&&
281 strcasecmp(job
->encoder_profile
, hb_h265_profile_names
[0]) != 0 &&
282 x265_param_apply_profile(param
, job
->encoder_profile
) < 0)
287 /* we should now know whether B-frames are enabled */
288 job
->areBframes
= (param
->bframes
> 0) + (param
->bframes
> 0 &&
289 param
->bBPyramid
> 0);
291 /* Reset global variables before opening a new encoder */
294 pv
->x265
= x265_encoder_open(param
);
295 if (pv
->x265
== NULL
)
297 hb_error("encx265: x265_encoder_open failed.");
302 * x265's output (headers and bitstream) are in Annex B format.
304 * Write the header as is, and let the muxer reformat
305 * the extradata and output bitstream properly for us.
307 ret
= x265_encoder_headers(pv
->x265
, &nal
, &nnal
);
310 hb_error("encx265: x265_encoder_headers failed (%d)", ret
);
313 if (ret
> sizeof(w
->config
->h265
.headers
))
315 hb_error("encx265: bitstream headers too large (%d)", ret
);
318 memcpy(w
->config
->h265
.headers
, nal
->payload
, ret
);
319 w
->config
->h265
.headers_length
= ret
;
324 w
->private_data
= NULL
;
329 void encx265Close(hb_work_object_t
*w
)
331 hb_work_private_t
*pv
= w
->private_data
;
333 if (pv
->delayed_chapters
!= NULL
)
335 struct chapter_s
*item
;
336 while ((item
= hb_list_item(pv
->delayed_chapters
, 0)) != NULL
)
338 hb_list_rem(pv
->delayed_chapters
, item
);
341 hb_list_close(&pv
->delayed_chapters
);
344 x265_param_free(pv
->param
);
345 x265_encoder_close(pv
->x265
);
347 w
->private_data
= NULL
;
351 * see comments in definition of 'frame_info' in pv struct for description
352 * of what these routines are doing.
354 static void save_frame_info(hb_work_private_t
*pv
, hb_buffer_t
*in
)
356 int i
= (in
->s
.start
>> FRAME_INFO_MAX2
) & FRAME_INFO_MASK
;
357 pv
->frame_info
[i
].duration
= in
->s
.stop
- in
->s
.start
;
359 static int64_t get_frame_duration(hb_work_private_t
* pv
, int64_t pts
)
361 int i
= (pts
>> FRAME_INFO_MAX2
) & FRAME_INFO_MASK
;
362 return pv
->frame_info
[i
].duration
;
365 static hb_buffer_t
* nal_encode(hb_work_object_t
*w
,
366 x265_picture
*pic_out
,
367 x265_nal
*nal
, uint32_t nnal
)
369 hb_work_private_t
*pv
= w
->private_data
;
370 hb_job_t
*job
= pv
->job
;
371 hb_buffer_t
*buf
= NULL
;
379 buf
= hb_video_buffer_init(job
->width
, job
->height
);
386 // copy the bitstream data
387 for (i
= 0; i
< nnal
; i
++)
389 memcpy(buf
->data
+ buf
->size
, nal
[i
].payload
, nal
[i
].sizeBytes
);
390 buf
->size
+= nal
[i
].sizeBytes
;
393 // use the pts to get the original frame's duration.
394 buf
->s
.duration
= get_frame_duration(pv
, pic_out
->pts
);
395 buf
->s
.stop
= pic_out
->pts
+ buf
->s
.duration
;
396 buf
->s
.start
= pic_out
->pts
;
397 buf
->s
.renderOffset
= pic_out
->dts
;
398 if (w
->config
->h264
.init_delay
== 0 && pic_out
->dts
< 0)
400 w
->config
->h264
.init_delay
-= pic_out
->dts
;
403 switch (pic_out
->sliceType
)
406 buf
->s
.frametype
= HB_FRAME_IDR
;
409 buf
->s
.frametype
= HB_FRAME_I
;
412 buf
->s
.frametype
= HB_FRAME_P
;
415 buf
->s
.frametype
= HB_FRAME_B
;
418 buf
->s
.frametype
= HB_FRAME_BREF
;
421 buf
->s
.frametype
= 0;
425 if (pv
->next_chapter_pts
!= AV_NOPTS_VALUE
&&
426 pv
->next_chapter_pts
<= pic_out
->pts
&&
427 pic_out
->sliceType
== X265_TYPE_IDR
)
429 // we're no longer looking for this chapter
430 pv
->next_chapter_pts
= AV_NOPTS_VALUE
;
432 // get the chapter index from the list
433 struct chapter_s
*item
= hb_list_item(pv
->delayed_chapters
, 0);
436 // we're done with this chapter
437 hb_list_rem(pv
->delayed_chapters
, item
);
438 buf
->s
.new_chap
= item
->index
;
441 // we may still have another pending chapter
442 item
= hb_list_item(pv
->delayed_chapters
, 0);
445 // we're looking for this one now
446 // we still need it, don't remove it
447 pv
->next_chapter_pts
= item
->start
;
452 // discard empty buffers (no video)
455 hb_buffer_close(&buf
);
460 static hb_buffer_t
* x265_encode(hb_work_object_t
*w
, hb_buffer_t
*in
)
462 hb_work_private_t
*pv
= w
->private_data
;
463 hb_job_t
*job
= pv
->job
;
464 x265_picture pic_in
, pic_out
;
468 x265_picture_init(pv
->param
, &pic_in
);
470 pic_in
.stride
[0] = in
->plane
[0].stride
;
471 pic_in
.stride
[1] = in
->plane
[1].stride
;
472 pic_in
.stride
[2] = in
->plane
[2].stride
;
473 pic_in
.planes
[0] = in
->plane
[0].data
;
474 pic_in
.planes
[1] = in
->plane
[1].data
;
475 pic_in
.planes
[2] = in
->plane
[2].data
;
476 pic_in
.poc
= pv
->frames_in
++;
477 pic_in
.pts
= in
->s
.start
;
480 if (in
->s
.new_chap
&& job
->chapter_markers
)
482 if (pv
->next_chapter_pts
== AV_NOPTS_VALUE
)
484 pv
->next_chapter_pts
= in
->s
.start
;
487 * Chapter markers are sometimes so close we can get a new one before
488 * the previous marker has been through the encoding queue.
490 * Dropping markers can cause weird side-effects downstream, including
491 * but not limited to missing chapters in the output, so we need to save
494 struct chapter_s
*item
= malloc(sizeof(struct chapter_s
));
497 item
->start
= in
->s
.start
;
498 item
->index
= in
->s
.new_chap
;
499 hb_list_add(pv
->delayed_chapters
, item
);
501 /* don't let 'work_loop' put a chapter mark on the wrong buffer */
504 * Chapters have to start with an IDR frame so request that this frame be
505 * coded as IDR. Since there may be up to 16 frames currently buffered in
506 * the encoder, remember the timestamp so when this frame finally pops out
507 * of the encoder we'll mark its buffer as the start of a chapter.
509 pic_in
.sliceType
= X265_TYPE_IDR
;
513 pic_in
.sliceType
= X265_TYPE_AUTO
;
516 if (pv
->last_stop
!= in
->s
.start
)
518 hb_log("encx265 input continuity err: last stop %"PRId64
" start %"PRId64
,
519 pv
->last_stop
, in
->s
.start
);
521 pv
->last_stop
= in
->s
.stop
;
522 save_frame_info(pv
, in
);
524 if (x265_encoder_encode(pv
->x265
, &nal
, &nnal
, &pic_in
, &pic_out
) > 0)
526 return nal_encode(w
, &pic_out
, nal
, nnal
);
531 int encx265Work(hb_work_object_t
*w
, hb_buffer_t
**buf_in
, hb_buffer_t
**buf_out
)
533 hb_work_private_t
*pv
= w
->private_data
;
534 hb_buffer_t
*in
= *buf_in
;
536 if (in
->s
.flags
& HB_BUF_FLAG_EOF
)
540 x265_picture pic_out
;
541 hb_buffer_t
*last_buf
= NULL
;
543 // flush delayed frames
544 while (x265_encoder_encode(pv
->x265
, &nal
, &nnal
, NULL
, &pic_out
) > 0)
546 hb_buffer_t
*buf
= nal_encode(w
, &pic_out
, nal
, nnal
);
549 if (last_buf
== NULL
)
555 last_buf
->next
= buf
;
561 // add the EOF to the end of the chain
562 if (last_buf
== NULL
)
575 *buf_out
= x265_encode(w
, in
);
579 const char* hb_x265_encopt_name(const char *name
)
582 for (i
= 0; hb_x265_encopt_synonyms
[i
][0] != NULL
; i
++)
583 if (!strcmp(name
, hb_x265_encopt_synonyms
[i
][1]))
584 return hb_x265_encopt_synonyms
[i
][0];