WinGui: Fix another instance of the Caliburn vs Json.net sillyness where objects...
[HandBrake.git] / libhb / encx265.c
blobad9f5ff8675e6f0cb519ce2de0f2dd2b99c7adfd
1 /* encx265.c
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
8 */
9 #ifdef USE_X265
11 #include "hb.h"
12 #include "hb_dict.h"
13 #include "h265_common.h"
14 #include "x265.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 =
22 WORK_ENCX265,
23 "H.265/HEVC encoder (libx265)",
24 encx265Init,
25 encx265Work,
26 encx265Close,
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] =
36 { "me", "motion", },
37 { NULL, NULL, },
40 struct hb_work_private_s
42 hb_job_t *job;
43 x265_encoder *x265;
44 x265_param *param;
46 int64_t last_stop;
47 uint32_t frames_in;
49 hb_list_t *delayed_chapters;
50 int64_t next_chapter_pts;
52 struct
54 int64_t duration;
56 frame_info[FRAME_INFO_SIZE];
58 char csvfn[1024];
61 // used in delayed_chapters list
62 struct chapter_s
64 int index;
65 int64_t start;
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
72 switch (ret)
74 case X265_PARAM_BAD_NAME:
75 hb_log("encx265: unknown option '%s'", key);
76 break;
77 case X265_PARAM_BAD_VALUE:
78 hb_log("encx265: bad argument '%s=%s'", key, value ? value : "(null)");
79 break;
80 default:
81 break;
83 return ret;
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();
96 pv->job = job;
97 w->private_data = pv;
98 int ret;
99 hb_rational_t vrate;
100 x265_nal *nal;
101 uint32_t nnal;
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);
109 goto fail;
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;
123 break;
125 if (!strncasecmp(tok, "ssim", 4))
127 param->bEnableSsim = 1;
128 break;
131 while ((tok = strtok(NULL, ",./-+")) != NULL);
132 free(tmp);
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");
158 break;
159 case 2: // ITU BT.601 DVD or SD TV content (PAL)
160 strcpy(colorprim, "bt470bg");
161 strcpy(transfer, "bt709");
162 strcpy(colormatrix, "smpte170m");
163 break;
164 case 3: // ITU BT.709 HD content
165 strcpy(colorprim, "bt709");
166 strcpy(transfer, "bt709");
167 strcpy(colormatrix, "bt709");
168 break;
169 case 4: // custom
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);
173 break;
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);
178 break;
180 if (param_parse(param, "colorprim", colorprim) ||
181 param_parse(param, "transfer", transfer) ||
182 param_parse(param, "colormatrix", colormatrix))
184 goto fail;
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);
191 hb_dict_iter_t iter;
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);
203 free(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.
228 char sar[22];
229 snprintf(sar, sizeof(sar), "%d:%d", job->par.num, job->par.den);
230 if (param_parse(param, "sar", sar))
232 goto fail;
235 if (job->vquality > 0)
237 param->rc.rateControlMode = X265_RC_CRF;
238 param->rc.rfConstant = job->vquality;
240 else
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] = "";
248 char pass[2];
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))
254 goto fail;
256 if (job->pass_id == HB_PASS_ENCODE_1ST && job->fastfirstpass == 0 &&
257 param_parse(param, "slow-firstpass", "1"))
259 goto fail;
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;
273 else
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)
284 goto fail;
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 */
292 x265_cleanup();
294 pv->x265 = x265_encoder_open(param);
295 if (pv->x265 == NULL)
297 hb_error("encx265: x265_encoder_open failed.");
298 goto fail;
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);
308 if (ret < 0)
310 hb_error("encx265: x265_encoder_headers failed (%d)", ret);
311 goto fail;
313 if (ret > sizeof(w->config->h265.headers))
315 hb_error("encx265: bitstream headers too large (%d)", ret);
316 goto fail;
318 memcpy(w->config->h265.headers, nal->payload, ret);
319 w->config->h265.headers_length = ret;
321 return 0;
323 fail:
324 w->private_data = NULL;
325 free(pv);
326 return 1;
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);
339 free(item);
341 hb_list_close(&pv->delayed_chapters);
344 x265_param_free(pv->param);
345 x265_encoder_close(pv->x265);
346 free(pv);
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;
372 int i;
374 if (nnal <= 0)
376 return NULL;
379 buf = hb_video_buffer_init(job->width, job->height);
380 if (buf == NULL)
382 return NULL;
385 buf->size = 0;
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)
405 case X265_TYPE_IDR:
406 buf->s.frametype = HB_FRAME_IDR;
407 break;
408 case X265_TYPE_I:
409 buf->s.frametype = HB_FRAME_I;
410 break;
411 case X265_TYPE_P:
412 buf->s.frametype = HB_FRAME_P;
413 break;
414 case X265_TYPE_B:
415 buf->s.frametype = HB_FRAME_B;
416 break;
417 case X265_TYPE_BREF:
418 buf->s.frametype = HB_FRAME_BREF;
419 break;
420 default:
421 buf->s.frametype = 0;
422 break;
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);
434 if (item != NULL)
436 // we're done with this chapter
437 hb_list_rem(pv->delayed_chapters, item);
438 buf->s.new_chap = item->index;
439 free(item);
441 // we may still have another pending chapter
442 item = hb_list_item(pv->delayed_chapters, 0);
443 if (item != NULL)
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)
453 if (buf->size <= 0)
455 hb_buffer_close(&buf);
457 return 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;
465 x265_nal *nal;
466 uint32_t nnal;
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;
478 pic_in.bitDepth = 8;
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
492 * it somehow.
494 struct chapter_s *item = malloc(sizeof(struct chapter_s));
495 if (item != NULL)
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 */
502 in->s.new_chap = 0;
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;
511 else
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);
528 return NULL;
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)
538 uint32_t nnal;
539 x265_nal *nal;
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);
547 if (buf != NULL)
549 if (last_buf == NULL)
551 *buf_out = buf;
553 else
555 last_buf->next = buf;
557 last_buf = buf;
561 // add the EOF to the end of the chain
562 if (last_buf == NULL)
564 *buf_out = in;
566 else
568 last_buf->next = in;
571 *buf_in = NULL;
572 return HB_WORK_DONE;
575 *buf_out = x265_encode(w, in);
576 return HB_WORK_OK;
579 const char* hb_x265_encopt_name(const char *name)
581 int i;
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];
585 return name;
588 #endif