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
16 int encx264Init( hb_work_object_t
*, hb_job_t
* );
17 int encx264Work( hb_work_object_t
*, hb_buffer_t
**, hb_buffer_t
** );
18 void encx264Close( hb_work_object_t
* );
20 hb_work_object_t hb_encx264
=
23 "H.264/AVC encoder (libx264)",
29 #define DTS_BUFFER_SIZE 32
32 * The frame info struct remembers information about each frame across calls
33 * to x264_encoder_encode. Since frames are uniquely identified by their
34 * timestamp, we use some bits of the timestamp as an index. The LSB is
35 * chosen so that two successive frames will have different values in the
36 * bits over any plausible range of frame rates. (Starting with bit 8 allows
37 * any frame rate slower than 352fps.) The MSB determines the size of the array.
38 * It is chosen so that two frames can't use the same slot during the
39 * encoder's max frame delay (set by the standard as 16 frames) and so
40 * that, up to some minimum frame rate, frames are guaranteed to map to
41 * different slots. (An MSB of 17 which is 2^(17-8+1) = 1024 slots guarantees
42 * no collisions down to a rate of .7 fps).
44 #define FRAME_INFO_MAX2 (8) // 2^8 = 256; 90000/256 = 352 frames/sec
45 #define FRAME_INFO_MIN2 (17) // 2^17 = 128K; 90000/131072 = 1.4 frames/sec
46 #define FRAME_INFO_SIZE (1 << (FRAME_INFO_MIN2 - FRAME_INFO_MAX2 + 1))
47 #define FRAME_INFO_MASK (FRAME_INFO_SIZE - 1)
49 struct hb_work_private_s
53 x264_picture_t pic_in
;
58 int64_t last_stop
; // Debugging - stop time of previous input frame
60 hb_list_t
*delayed_chapters
;
61 int64_t next_chapter_pts
;
64 } frame_info
[FRAME_INFO_SIZE
];
69 // used in delayed_chapters list
76 /***********************************************************************
77 * hb_work_encx264_init
78 ***********************************************************************
80 **********************************************************************/
81 int encx264Init( hb_work_object_t
* w
, hb_job_t
* job
)
87 hb_work_private_t
* pv
= calloc( 1, sizeof( hb_work_private_t
) );
91 pv
->next_chapter_pts
= AV_NOPTS_VALUE
;
92 pv
->delayed_chapters
= hb_list_init();
94 if (x264_param_default_preset(¶m
,
95 job
->encoder_preset
, job
->encoder_tune
) < 0)
102 /* If the PSNR or SSIM tunes are in use, enable the relevant metric */
103 if (job
->encoder_tune
!= NULL
&& *job
->encoder_tune
)
105 char *tmp
= strdup(job
->encoder_tune
);
106 char *tok
= strtok(tmp
, ",./-+");
109 if (!strncasecmp(tok
, "psnr", 4))
111 param
.analyse
.b_psnr
= 1;
114 if (!strncasecmp(tok
, "ssim", 4))
116 param
.analyse
.b_ssim
= 1;
120 while ((tok
= strtok(NULL
, ",./-+")) != NULL
);
124 /* Some HandBrake-specific defaults; users can override them
125 * using the encoder_options string. */
126 if( job
->pass_id
== HB_PASS_ENCODE_2ND
&& job
->cfr
!= 1 )
128 hb_interjob_t
* interjob
= hb_interjob_get( job
->h
);
129 param
.i_fps_num
= interjob
->vrate
.num
;
130 param
.i_fps_den
= interjob
->vrate
.den
;
134 param
.i_fps_num
= job
->vrate
.num
;
135 param
.i_fps_den
= job
->vrate
.den
;
139 param
.i_timebase_num
= 0;
140 param
.i_timebase_den
= 0;
141 param
.b_vfr_input
= 0;
145 param
.i_timebase_num
= 1;
146 param
.i_timebase_den
= 90000;
149 /* Set min:max keyframe intervals to 1:10 of fps;
150 * adjust +0.5 for when fps has remainder to bump
151 * { 23.976, 29.976, 59.94 } to { 24, 30, 60 }. */
152 param
.i_keyint_min
= (double)job
->vrate
.num
/ job
->vrate
.den
+ 0.5;
153 param
.i_keyint_max
= 10 * param
.i_keyint_min
;
155 param
.i_log_level
= X264_LOG_INFO
;
157 /* set up the VUI color model & gamma to match what the COLR atom
158 * set in muxmp4.c says. See libhb/muxmp4.c for notes. */
159 if( job
->color_matrix_code
== 4 )
162 param
.vui
.i_colorprim
= job
->color_prim
;
163 param
.vui
.i_transfer
= job
->color_transfer
;
164 param
.vui
.i_colmatrix
= job
->color_matrix
;
166 else if( job
->color_matrix_code
== 3 )
168 // ITU BT.709 HD content
169 param
.vui
.i_colorprim
= HB_COLR_PRI_BT709
;
170 param
.vui
.i_transfer
= HB_COLR_TRA_BT709
;
171 param
.vui
.i_colmatrix
= HB_COLR_MAT_BT709
;
173 else if( job
->color_matrix_code
== 2 )
175 // ITU BT.601 DVD or SD TV content (PAL)
176 param
.vui
.i_colorprim
= HB_COLR_PRI_EBUTECH
;
177 param
.vui
.i_transfer
= HB_COLR_TRA_BT709
;
178 param
.vui
.i_colmatrix
= HB_COLR_MAT_SMPTE170M
;
180 else if( job
->color_matrix_code
== 1 )
182 // ITU BT.601 DVD or SD TV content (NTSC)
183 param
.vui
.i_colorprim
= HB_COLR_PRI_SMPTEC
;
184 param
.vui
.i_transfer
= HB_COLR_TRA_BT709
;
185 param
.vui
.i_colmatrix
= HB_COLR_MAT_SMPTE170M
;
189 // detected during scan
190 param
.vui
.i_colorprim
= job
->title
->color_prim
;
191 param
.vui
.i_transfer
= job
->title
->color_transfer
;
192 param
.vui
.i_colmatrix
= job
->title
->color_matrix
;
195 /* place job->encoder_options in an hb_dict_t for convenience */
196 hb_dict_t
* x264_opts
= NULL
;
197 if (job
->encoder_options
!= NULL
&& *job
->encoder_options
)
199 x264_opts
= hb_encopts_to_dict(job
->encoder_options
, job
->vcodec
);
201 /* iterate through x264_opts and have libx264 parse the options for us */
204 for (iter
= hb_dict_iter_init(x264_opts
);
205 iter
!= HB_DICT_ITER_DONE
;
206 iter
= hb_dict_iter_next(x264_opts
, iter
))
208 const char *key
= hb_dict_iter_key(iter
);
209 hb_value_t
*value
= hb_dict_iter_value(iter
);
210 char *str
= hb_value_get_string_xform(value
);
212 /* Here's where the strings are passed to libx264 for parsing. */
213 ret
= x264_param_parse(¶m
, key
, str
);
215 /* Let x264 sanity check the options for us */
216 if (ret
== X264_PARAM_BAD_NAME
)
217 hb_log( "x264 options: Unknown suboption %s", key
);
218 if (ret
== X264_PARAM_BAD_VALUE
)
219 hb_log( "x264 options: Bad argument %s=%s", key
,
220 str
? str
: "(null)" );
223 hb_dict_free(&x264_opts
);
225 /* Reload colorimetry settings in case custom values were set
226 * in the encoder_options string */
227 job
->color_matrix_code
= 4;
228 job
->color_prim
= param
.vui
.i_colorprim
;
229 job
->color_transfer
= param
.vui
.i_transfer
;
230 job
->color_matrix
= param
.vui
.i_colmatrix
;
232 /* For 25 fps sources, HandBrake's explicit keyints will match the x264 defaults:
233 * min-keyint 25 (same as auto), keyint 250. */
234 if( param
.i_keyint_min
!= 25 || param
.i_keyint_max
!= 250 )
238 if ( param
.i_fps_num
/ param
.i_fps_den
< param
.i_keyint_max
/ 10 )
239 min_auto
= param
.i_fps_num
/ param
.i_fps_den
;
241 min_auto
= param
.i_keyint_max
/ 10;
243 char min
[40], max
[40];
244 param
.i_keyint_min
== X264_KEYINT_MIN_AUTO
?
245 snprintf( min
, 40, "auto (%d)", min_auto
) :
246 snprintf( min
, 40, "%d", param
.i_keyint_min
);
248 param
.i_keyint_max
== X264_KEYINT_MAX_INFINITE
?
249 snprintf( max
, 40, "infinite" ) :
250 snprintf( max
, 40, "%d", param
.i_keyint_max
);
252 hb_log( "encx264: min-keyint: %s, keyint: %s", min
, max
);
255 /* Settings which can't be overriden in the encoder_options string
256 * (muxer-specific settings, resolution, ratecontrol, etc.). */
258 /* Disable annexb. Inserts size into nal header instead of start code. */
261 param
.i_width
= job
->width
;
262 param
.i_height
= job
->height
;
264 param
.vui
.i_sar_width
= job
->par
.num
;
265 param
.vui
.i_sar_height
= job
->par
.den
;
267 if( job
->vquality
>= 0 )
270 param
.rc
.i_rc_method
= X264_RC_CRF
;
271 param
.rc
.f_rf_constant
= job
->vquality
;
272 hb_log( "encx264: encoding at constant RF %f", param
.rc
.f_rf_constant
);
276 /* Average bitrate */
277 param
.rc
.i_rc_method
= X264_RC_ABR
;
278 param
.rc
.i_bitrate
= job
->vbitrate
;
279 hb_log( "encx264: encoding at average bitrate %d", param
.rc
.i_bitrate
);
280 if( job
->pass_id
== HB_PASS_ENCODE_1ST
||
281 job
->pass_id
== HB_PASS_ENCODE_2ND
)
283 memset( pv
->filename
, 0, 1024 );
284 hb_get_tempory_filename( job
->h
, pv
->filename
, "x264.log" );
286 switch( job
->pass_id
)
288 case HB_PASS_ENCODE_1ST
:
289 param
.rc
.b_stat_read
= 0;
290 param
.rc
.b_stat_write
= 1;
291 param
.rc
.psz_stat_out
= pv
->filename
;
293 case HB_PASS_ENCODE_2ND
:
294 param
.rc
.b_stat_read
= 1;
295 param
.rc
.b_stat_write
= 0;
296 param
.rc
.psz_stat_in
= pv
->filename
;
301 /* Apply profile and level settings last, if present. */
302 if (job
->encoder_profile
!= NULL
&& *job
->encoder_profile
)
304 if (hb_apply_h264_profile(¶m
, job
->encoder_profile
, 1))
311 if (job
->encoder_level
!= NULL
&& *job
->encoder_level
)
313 if (hb_apply_h264_level(¶m
, job
->encoder_level
,
314 job
->encoder_profile
, 1) < 0)
322 /* Turbo first pass */
323 if( job
->pass_id
== HB_PASS_ENCODE_1ST
&& job
->fastfirstpass
== 1 )
325 x264_param_apply_fastfirstpass( ¶m
);
328 /* B-pyramid is enabled by default. */
331 if( !param
.i_bframe
)
335 else if( !param
.i_bframe_pyramid
)
340 /* Log the unparsed x264 options string. */
341 char *x264_opts_unparsed
= hb_x264_param_unparse(job
->encoder_preset
,
343 job
->encoder_options
,
344 job
->encoder_profile
,
348 if( x264_opts_unparsed
!= NULL
)
350 hb_log( "encx264: unparsed options: %s", x264_opts_unparsed
);
352 free( x264_opts_unparsed
);
354 hb_deep_log( 2, "encx264: opening libx264 (pass %d)", job
->pass_id
);
355 pv
->x264
= x264_encoder_open( ¶m
);
356 if ( pv
->x264
== NULL
)
358 hb_error("encx264: x264_encoder_open failed.");
364 x264_encoder_headers( pv
->x264
, &nal
, &nal_count
);
366 /* Sequence Parameter Set */
367 memcpy(w
->config
->h264
.sps
, nal
[0].p_payload
+ 4, nal
[0].i_payload
- 4);
368 w
->config
->h264
.sps_length
= nal
[0].i_payload
- 4;
370 /* Picture Parameter Set */
371 memcpy(w
->config
->h264
.pps
, nal
[1].p_payload
+ 4, nal
[1].i_payload
- 4);
372 w
->config
->h264
.pps_length
= nal
[1].i_payload
- 4;
374 x264_picture_init( &pv
->pic_in
);
376 pv
->pic_in
.img
.i_csp
= X264_CSP_I420
;
377 pv
->pic_in
.img
.i_plane
= 3;
381 int uvsize
= hb_image_stride(AV_PIX_FMT_YUV420P
, job
->width
, 1) *
382 hb_image_height(AV_PIX_FMT_YUV420P
, job
->height
, 1);
383 pv
->grey_data
= malloc(uvsize
);
384 memset(pv
->grey_data
, 0x80, uvsize
);
385 pv
->pic_in
.img
.plane
[1] = pv
->pic_in
.img
.plane
[2] = pv
->grey_data
;
391 void encx264Close( hb_work_object_t
* w
)
393 hb_work_private_t
* pv
= w
->private_data
;
395 if (pv
->delayed_chapters
!= NULL
)
397 struct chapter_s
*item
;
398 while ((item
= hb_list_item(pv
->delayed_chapters
, 0)) != NULL
)
400 hb_list_rem(pv
->delayed_chapters
, item
);
403 hb_list_close(&pv
->delayed_chapters
);
406 free( pv
->grey_data
);
407 x264_encoder_close( pv
->x264
);
409 w
->private_data
= NULL
;
415 * see comments in definition of 'frame_info' in pv struct for description
416 * of what these routines are doing.
418 static void save_frame_info( hb_work_private_t
* pv
, hb_buffer_t
* in
)
420 int i
= (in
->s
.start
>> FRAME_INFO_MAX2
) & FRAME_INFO_MASK
;
421 pv
->frame_info
[i
].duration
= in
->s
.stop
- in
->s
.start
;
424 static int64_t get_frame_duration( hb_work_private_t
* pv
, int64_t pts
)
426 int i
= (pts
>> FRAME_INFO_MAX2
) & FRAME_INFO_MASK
;
427 return pv
->frame_info
[i
].duration
;
430 static hb_buffer_t
*nal_encode( hb_work_object_t
*w
, x264_picture_t
*pic_out
,
431 int i_nal
, x264_nal_t
*nal
)
433 hb_buffer_t
*buf
= NULL
;
434 hb_work_private_t
*pv
= w
->private_data
;
435 hb_job_t
*job
= pv
->job
;
437 /* Should be way too large */
438 buf
= hb_video_buffer_init( job
->width
, job
->height
);
440 buf
->s
.frametype
= 0;
442 // use the pts to get the original frame's duration.
443 buf
->s
.duration
= get_frame_duration( pv
, pic_out
->i_pts
);
444 buf
->s
.start
= pic_out
->i_pts
;
445 buf
->s
.stop
= buf
->s
.start
+ buf
->s
.duration
;
446 buf
->s
.renderOffset
= pic_out
->i_dts
;
447 if ( !w
->config
->h264
.init_delay
&& pic_out
->i_dts
< 0 )
449 w
->config
->h264
.init_delay
= -pic_out
->i_dts
;
452 /* Encode all the NALs we were given into buf.
453 NOTE: This code assumes one video frame per NAL (but there can
454 be other stuff like SPS and/or PPS). If there are multiple
455 frames we only get the duration of the first which will
456 eventually screw up the muxer & decoder. */
458 for( i
= 0; i
< i_nal
; i
++ )
460 int size
= nal
[i
].i_payload
;
461 memcpy(buf
->data
+ buf
->size
, nal
[i
].p_payload
, size
);
467 /* H.264 in .mp4 or .mkv */
468 switch( nal
[i
].i_type
)
470 /* Sequence Parameter Set & Program Parameter Set go in the
471 * mp4 header so skip them here
484 /* Decide what type of frame we have. */
485 switch( pic_out
->i_type
)
488 // Handled in b_keyframe check below.
492 buf
->s
.frametype
= HB_FRAME_I
;
496 buf
->s
.frametype
= HB_FRAME_P
;
500 buf
->s
.frametype
= HB_FRAME_B
;
503 /* This is for b-pyramid, which has reference b-frames
504 However, it doesn't seem to ever be used... */
506 buf
->s
.frametype
= HB_FRAME_BREF
;
509 // If it isn't the above, what type of frame is it??
511 buf
->s
.frametype
= 0;
515 /* Since libx264 doesn't tell us when b-frames are
516 themselves reference frames, figure it out on our own. */
517 if( (buf
->s
.frametype
== HB_FRAME_B
) &&
518 (nal
[i
].i_ref_idc
!= NAL_PRIORITY_DISPOSABLE
) )
519 buf
->s
.frametype
= HB_FRAME_BREF
;
521 /* Expose disposable bit to muxer. */
522 if( nal
[i
].i_ref_idc
== NAL_PRIORITY_DISPOSABLE
)
523 buf
->s
.flags
&= ~HB_FRAME_REF
;
525 buf
->s
.flags
|= HB_FRAME_REF
;
527 // PIR has no IDR frames, but x264 marks recovery points
528 // as keyframes. So fake an IDR at these points. This flag
529 // is also set for real IDR frames.
530 if( pic_out
->b_keyframe
)
532 buf
->s
.frametype
= HB_FRAME_IDR
;
533 /* if we have a chapter marker pending and this
534 frame's presentation time stamp is at or after
535 the marker's time stamp, use this as the
537 if (pv
->next_chapter_pts
!= AV_NOPTS_VALUE
&&
538 pv
->next_chapter_pts
<= pic_out
->i_pts
)
540 // we're no longer looking for this chapter
541 pv
->next_chapter_pts
= AV_NOPTS_VALUE
;
543 // get the chapter index from the list
544 struct chapter_s
*item
= hb_list_item(pv
->delayed_chapters
, 0);
547 // we're done with this chapter
548 buf
->s
.new_chap
= item
->index
;
549 hb_list_rem(pv
->delayed_chapters
, item
);
552 // we may still have another pending chapter
553 item
= hb_list_item(pv
->delayed_chapters
, 0);
556 // we're looking for this one now
557 // we still need it, don't remove it
558 pv
->next_chapter_pts
= item
->start
;
566 // make sure we found at least one video frame
567 if ( buf
->size
<= 0 )
569 // no video - discard the buf
570 hb_buffer_close( &buf
);
575 static hb_buffer_t
*x264_encode( hb_work_object_t
*w
, hb_buffer_t
*in
)
577 hb_work_private_t
*pv
= w
->private_data
;
578 hb_job_t
*job
= pv
->job
;
580 /* Point x264 at our current buffers Y(UV) data. */
581 pv
->pic_in
.img
.i_stride
[0] = in
->plane
[0].stride
;
582 pv
->pic_in
.img
.i_stride
[1] = in
->plane
[1].stride
;
583 pv
->pic_in
.img
.i_stride
[2] = in
->plane
[2].stride
;
584 pv
->pic_in
.img
.plane
[0] = in
->plane
[0].data
;
585 if( !job
->grayscale
)
587 pv
->pic_in
.img
.plane
[1] = in
->plane
[1].data
;
588 pv
->pic_in
.img
.plane
[2] = in
->plane
[2].data
;
591 if( in
->s
.new_chap
&& job
->chapter_markers
)
593 /* chapters have to start with an IDR frame so request that this
594 frame be coded as IDR. Since there may be up to 16 frames
595 currently buffered in the encoder remember the timestamp so
596 when this frame finally pops out of the encoder we'll mark
597 its buffer as the start of a chapter. */
598 pv
->pic_in
.i_type
= X264_TYPE_IDR
;
599 if (pv
->next_chapter_pts
== AV_NOPTS_VALUE
)
601 pv
->next_chapter_pts
= in
->s
.start
;
604 * Chapter markers are sometimes so close we can get a new one before the
605 * previous marker has been through the encoding queue.
607 * Dropping markers can cause weird side-effects downstream, including but
608 * not limited to missing chapters in the output, so we need to save it
611 struct chapter_s
*item
= malloc(sizeof(struct chapter_s
));
614 item
->start
= in
->s
.start
;
615 item
->index
= in
->s
.new_chap
;
616 hb_list_add(pv
->delayed_chapters
, item
);
618 /* don't let 'work_loop' put a chapter mark on the wrong buffer */
623 pv
->pic_in
.i_type
= X264_TYPE_AUTO
;
626 /* XXX this is temporary debugging code to check that the upstream
627 * modules (render & sync) have generated a continuous, self-consistent
628 * frame stream with the current frame's start time equal to the
629 * previous frame's stop time.
631 if( pv
->last_stop
!= in
->s
.start
)
633 hb_log("encx264 input continuity err: last stop %"PRId64
" start %"PRId64
,
634 pv
->last_stop
, in
->s
.start
);
636 pv
->last_stop
= in
->s
.stop
;
638 // Remember info about this frame that we need to pass across
639 // the x264_encoder_encode call (since it reorders frames).
640 save_frame_info( pv
, in
);
642 /* Feed the input PTS to x264 so it can figure out proper output PTS */
643 pv
->pic_in
.i_pts
= in
->s
.start
;
645 x264_picture_t pic_out
;
649 x264_encoder_encode( pv
->x264
, &nal
, &i_nal
, &pv
->pic_in
, &pic_out
);
652 return nal_encode( w
, &pic_out
, i_nal
, nal
);
657 int encx264Work( hb_work_object_t
* w
, hb_buffer_t
** buf_in
,
658 hb_buffer_t
** buf_out
)
660 hb_work_private_t
*pv
= w
->private_data
;
661 hb_buffer_t
*in
= *buf_in
;
665 if (in
->s
.flags
& HB_BUF_FLAG_EOF
)
667 // EOF on input. Flush any frames still in the decoder then
668 // send the eof downstream to tell the muxer we're done.
669 x264_picture_t pic_out
;
672 hb_buffer_t
*last_buf
= NULL
;
674 while ( x264_encoder_delayed_frames( pv
->x264
) )
676 x264_encoder_encode( pv
->x264
, &nal
, &i_nal
, NULL
, &pic_out
);
682 hb_buffer_t
*buf
= nal_encode( w
, &pic_out
, i_nal
, nal
);
686 if ( last_buf
== NULL
)
689 last_buf
->next
= buf
;
693 // Flushed everything - add the eof to the end of the chain.
694 if ( last_buf
== NULL
)
703 // Not EOF - encode the packet & wrap it in a NAL
706 *buf_out
= x264_encode( w
, in
);
710 int hb_apply_h264_profile(x264_param_t
*param
, const char *h264_profile
,
713 if (h264_profile
!= NULL
&&
714 strcasecmp(h264_profile
, hb_h264_profile_names
[0]) != 0)
717 * baseline profile doesn't support interlacing
719 if ((param
->b_interlaced
||
720 param
->b_fake_interlaced
) &&
721 !strcasecmp(h264_profile
, "baseline"))
725 hb_log("hb_apply_h264_profile [warning]: baseline profile doesn't support interlacing, disabling");
727 param
->b_interlaced
= param
->b_fake_interlaced
= 0;
730 * lossless requires High 4:4:4 Predictive profile
732 if (param
->rc
.f_rf_constant
< 1.0 &&
733 param
->rc
.i_rc_method
== X264_RC_CRF
&&
734 strcasecmp(h264_profile
, "high444") != 0)
738 hb_log("hb_apply_h264_profile [warning]: lossless requires high444 profile, disabling");
740 param
->rc
.f_rf_constant
= 1.0;
742 if (!strcasecmp(h264_profile
, "high10") ||
743 !strcasecmp(h264_profile
, "high422"))
745 // arbitrary profile names may be specified via the CLI
746 // map unsupported high10 and high422 profiles to high
747 return x264_param_apply_profile(param
, "high");
749 return x264_param_apply_profile(param
, h264_profile
);
751 else if (!strcasecmp(h264_profile
, hb_h264_profile_names
[0]))
753 // "auto", do nothing
758 // error (profile not a string), abort
759 hb_error("hb_apply_h264_profile: no profile specified");
764 int hb_check_h264_level(const char *h264_level
, int width
, int height
,
765 int fps_num
, int fps_den
, int interlaced
,
769 x264_param_default(¶m
);
770 param
.i_width
= width
;
771 param
.i_height
= height
;
772 param
.i_fps_num
= fps_num
;
773 param
.i_fps_den
= fps_den
;
774 param
.b_interlaced
= !!interlaced
;
775 param
.b_fake_interlaced
= !!fake_interlaced
;
776 return (hb_apply_h264_level(¶m
, h264_level
, NULL
, 0) != 0);
779 int hb_apply_h264_level(x264_param_t
*param
, const char *h264_level
,
780 const char *h264_profile
, int verbose
)
783 const x264_level_t
*x264_level
= NULL
;
784 int i
, i_mb_size
, i_mb_rate
, i_mb_width
, i_mb_height
, max_mb_side
, ret
;
787 * find the x264_level_t corresponding to the requested level
789 if (h264_level
!= NULL
&&
790 strcasecmp(h264_level
, hb_h264_level_names
[0]) != 0)
792 for (i
= 0; hb_h264_level_values
[i
]; i
++)
794 if (!strcmp(hb_h264_level_names
[i
], h264_level
))
796 int val
= hb_h264_level_values
[i
];
797 for (i
= 0; x264_levels
[i
].level_idc
; i
++)
799 if (x264_levels
[i
].level_idc
== val
)
801 x264_level
= &x264_levels
[i
];
808 if (x264_level
== NULL
)
810 // error (invalid or unsupported level), abort
811 hb_error("hb_apply_h264_level: invalid level %s", h264_level
);
815 else if(!strcasecmp(h264_level
, hb_h264_level_names
[0]))
817 // "auto", do nothing
822 // error (level not a string), abort
823 hb_error("hb_apply_h264_level: no level specified");
828 * the H.264 profile determines VBV constraints
832 // Main or Baseline (equivalent)
833 HB_ENCX264_PROFILE_MAIN
,
834 // High (no 4:2:2 or 10-bit support, so anything lossy is equivalent)
835 HB_ENCX264_PROFILE_HIGH
,
836 // Lossless (4:2:0 8-bit for now)
837 HB_ENCX264_PROFILE_HIGH444
,
838 } hb_encx264_profile
;
843 * TODO: we need to guess the profile like x264_sps_init does, otherwise
844 * we'll get an error when setting a Main-incompatible VBV and
845 * x264_sps_init() guesses Main profile. x264_sps_init() may eventually take
846 * VBV into account when guessing profile, at which point this code can be
850 if (h264_profile
!= NULL
&& *h264_profile
)
852 // if the user explicitly specified a profile, don't guess it
853 if (!strcasecmp(h264_profile
, "high444"))
855 hb_encx264_profile
= HB_ENCX264_PROFILE_HIGH444
;
857 else if (!strcasecmp(h264_profile
, "main") ||
858 !strcasecmp(h264_profile
, "baseline"))
860 hb_encx264_profile
= HB_ENCX264_PROFILE_MAIN
;
864 hb_encx264_profile
= HB_ENCX264_PROFILE_HIGH
;
870 // guess the H.264 profile if the user didn't request one
871 if (param
->rc
.i_rc_method
== X264_RC_CRF
&&
872 param
->rc
.f_rf_constant
< 1.0)
874 hb_encx264_profile
= HB_ENCX264_PROFILE_HIGH444
;
876 else if (param
->analyse
.b_transform_8x8
||
877 param
->i_cqm_preset
!= X264_CQM_FLAT
)
879 hb_encx264_profile
= HB_ENCX264_PROFILE_HIGH
;
883 hb_encx264_profile
= HB_ENCX264_PROFILE_MAIN
;
888 * we need at least width and height in order to apply a level correctly
890 if (param
->i_width
<= 0 || param
->i_height
<= 0)
892 // error (invalid width or height), abort
893 hb_error("hb_apply_h264_level: invalid resolution (width: %d, height: %d)",
894 param
->i_width
, param
->i_height
);
899 * a return value of 1 means there were warnings
904 * some levels do not support interlaced encoding
906 if (x264_level
->frame_only
&& (param
->b_interlaced
||
907 param
->b_fake_interlaced
))
911 hb_log("hb_apply_h264_level [warning]: interlaced flag not supported for level %s, disabling",
915 param
->b_interlaced
= param
->b_fake_interlaced
= 0;
919 * frame dimensions and rate (in macroblocks)
921 i_mb_width
= (param
->i_width
+ 15) / 16;
922 i_mb_height
= (param
->i_height
+ 15) / 16;
923 if (param
->b_interlaced
|| param
->b_fake_interlaced
)
925 // interlaced: encoded height must divide cleanly by 32
926 i_mb_height
= (i_mb_height
+ 1) & ~1;
928 i_mb_size
= i_mb_width
* i_mb_height
;
929 if (param
->i_fps_den
<= 0 || param
->i_fps_num
<= 0)
936 i_mb_rate
= (int64_t)i_mb_size
* param
->i_fps_num
/ param
->i_fps_den
;
937 f_framerate
= (float)param
->i_fps_num
/ param
->i_fps_den
;
941 * sanitize ref/frameref
943 if (param
->i_keyint_max
!= 1)
945 int i_max_dec_frame_buffering
=
946 MAX(MIN(x264_level
->dpb
/ i_mb_size
, 16), 1);
947 param
->i_frame_reference
=
948 MIN(i_max_dec_frame_buffering
, param
->i_frame_reference
);
950 * some level and resolution combos may require as little as 1 ref;
951 * bframes and b-pyramid are not compatible with this scenario
953 if (i_max_dec_frame_buffering
< 2)
957 else if (i_max_dec_frame_buffering
< 4)
959 param
->i_bframe_pyramid
= X264_B_PYRAMID_NONE
;
964 * set and/or sanitize the VBV (if not lossless)
966 if (hb_encx264_profile
!= HB_ENCX264_PROFILE_HIGH444
)
968 // High profile allows for higher VBV bufsize/maxrate
969 int cbp_factor
= hb_encx264_profile
== HB_ENCX264_PROFILE_HIGH
? 5 : 4;
970 if (!param
->rc
.i_vbv_max_bitrate
)
972 param
->rc
.i_vbv_max_bitrate
= (x264_level
->bitrate
* cbp_factor
) / 4;
976 param
->rc
.i_vbv_max_bitrate
=
977 MIN(param
->rc
.i_vbv_max_bitrate
,
978 (x264_level
->bitrate
* cbp_factor
) / 4);
980 if (!param
->rc
.i_vbv_buffer_size
)
982 param
->rc
.i_vbv_buffer_size
= (x264_level
->cpb
* cbp_factor
) / 4;
986 param
->rc
.i_vbv_buffer_size
=
987 MIN(param
->rc
.i_vbv_buffer_size
,
988 (x264_level
->cpb
* cbp_factor
) / 4);
993 * sanitize mvrange/mv-range
995 param
->analyse
.i_mv_range
=
996 MIN(param
->analyse
.i_mv_range
,
997 x264_level
->mv_range
>> !!param
->b_interlaced
);
1000 * TODO: check the rest of the limits
1004 * things we can do nothing about (too late to change resolution or fps),
1005 * print warnings if we're not being quiet
1007 if (x264_level
->frame_size
< i_mb_size
)
1011 hb_log("hb_apply_h264_level [warning]: frame size (%dx%d, %d macroblocks) too high for level %s (max. %d macroblocks)",
1012 i_mb_width
* 16, i_mb_height
* 16, i_mb_size
, h264_level
,
1013 x264_level
->frame_size
);
1017 else if (x264_level
->mbps
< i_mb_rate
)
1021 hb_log("hb_apply_h264_level [warning]: framerate (%.3f) too high for level %s at %dx%d (max. %.3f)",
1022 f_framerate
, h264_level
, param
->i_width
, param
->i_height
,
1023 (float)x264_level
->mbps
/ i_mb_size
);
1028 * width or height squared may not exceed 8 * frame_size (in macroblocks)
1029 * thus neither dimension may exceed sqrt(8 * frame_size)
1031 max_mb_side
= sqrt(x264_level
->frame_size
* 8);
1032 if (i_mb_width
> max_mb_side
)
1036 hb_log("hb_apply_h264_level [warning]: frame too wide (%d) for level %s (max. %d)",
1037 param
->i_width
, h264_level
, max_mb_side
* 16);
1041 if (i_mb_height
> max_mb_side
)
1045 hb_log("hb_apply_h264_level [warning]: frame too tall (%d) for level %s (max. %d)",
1046 param
->i_height
, h264_level
, max_mb_side
* 16);
1052 * level successfully applied, yay!
1054 param
->i_level_idc
= x264_level
->level_idc
;
1058 static hb_value_t
* value_pair(hb_value_t
* v1
, hb_value_t
* v2
)
1060 hb_value_t
*array
= hb_value_array_init();
1061 hb_value_array_append(array
, v1
);
1062 hb_value_array_append(array
, v2
);
1066 char * hb_x264_param_unparse(const char *x264_preset
, const char *x264_tune
,
1067 const char *x264_encopts
, const char *h264_profile
,
1068 const char *h264_level
, int width
, int height
)
1071 char *unparsed_opts
;
1072 hb_dict_t
*x264_opts
;
1073 x264_param_t defaults
, param
;
1076 * get the global x264 defaults (what we compare against)
1078 x264_param_default(&defaults
);
1081 * apply the defaults, preset and tune
1083 if (x264_param_default_preset(¶m
, x264_preset
, x264_tune
) < 0)
1086 * Note: GUIs should be able to always specifiy valid preset/tunes, so
1087 * this code will hopefully never be reached
1089 return strdup("hb_x264_param_unparse: invalid x264 preset/tune");
1093 * place additional x264 options in a dictionary
1095 x264_opts
= hb_encopts_to_dict(x264_encopts
, HB_VCODEC_X264
);
1098 * some libx264 options are set via dedicated widgets in the video tab or
1099 * hardcoded in libhb, and have no effect when present in the advanced x264
1102 * clear them from x264_opts so as to not apply then during unparse.
1104 hb_dict_remove(x264_opts
, "qp");
1105 hb_dict_remove(x264_opts
, "qp_constant");
1106 hb_dict_remove(x264_opts
, "crf");
1107 hb_dict_remove(x264_opts
, "bitrate");
1108 hb_dict_remove(x264_opts
, "fps");
1109 hb_dict_remove(x264_opts
, "force-cfr");
1110 hb_dict_remove(x264_opts
, "sar");
1111 hb_dict_remove(x264_opts
, "annexb");
1114 * apply the additional x264 options
1116 hb_dict_iter_t iter
;
1117 for (iter
= hb_dict_iter_init(x264_opts
);
1118 iter
!= HB_DICT_ITER_DONE
;
1119 iter
= hb_dict_iter_next(x264_opts
, iter
))
1121 const char *key
= hb_dict_iter_key(iter
);
1122 hb_value_t
*value
= hb_dict_iter_value(iter
);
1123 char *str
= hb_value_get_string_xform(value
);
1125 // let's not pollute GUI logs with x264_param_parse return codes
1126 x264_param_parse(¶m
, key
, str
);
1131 * apply the x264 profile, if specified
1133 if (h264_profile
!= NULL
&& *h264_profile
)
1135 // be quiet so at to not pollute GUI logs
1136 hb_apply_h264_profile(¶m
, h264_profile
, 0);
1140 * apply the h264 level, if specified
1142 if (h264_level
!= NULL
&& *h264_level
)
1144 // set width/height to avoid issues in hb_apply_h264_level
1145 param
.i_width
= width
;
1146 param
.i_height
= height
;
1147 // be quiet so at to not pollute GUI logs
1148 hb_apply_h264_level(¶m
, h264_level
, h264_profile
, 0);
1152 * if x264_encopts is NULL, x264_opts wasn't initialized
1154 if (x264_opts
== NULL
&& (x264_opts
= hb_dict_init()) == NULL
)
1156 return strdup("hb_x264_param_unparse: could not initialize hb_dict_t");
1160 * x264 lets you specify some options in multiple ways. For options that we
1161 * do unparse, clear the forms that don't match how we unparse said option
1162 * from the x264_opts dictionary.
1164 * actual synonyms are already handled by hb_encopts_to_dict().
1166 * "no-deblock" is a special case as it can't be unparsed to "deblock=0"
1168 * also, don't bother with forms that aren't allowed by the x264 CLI, such
1169 * as "no-bframes" - there are too many.
1171 hb_dict_remove(x264_opts
, "no-sliced-threads");
1172 hb_dict_remove(x264_opts
, "no-scenecut");
1173 hb_dict_remove(x264_opts
, "no-b-adapt");
1174 hb_dict_remove(x264_opts
, "no-weightb");
1175 hb_dict_remove(x264_opts
, "no-cabac");
1176 hb_dict_remove(x264_opts
, "interlaced"); // we unparse to tff/bff
1177 hb_dict_remove(x264_opts
, "no-interlaced");
1178 hb_dict_remove(x264_opts
, "no-8x8dct");
1179 hb_dict_remove(x264_opts
, "no-mixed-refs");
1180 hb_dict_remove(x264_opts
, "no-fast-pskip");
1181 hb_dict_remove(x264_opts
, "no-dct-decimate");
1182 hb_dict_remove(x264_opts
, "no-psy");
1183 hb_dict_remove(x264_opts
, "no-mbtree");
1186 * compare defaults to param and unparse to the x264_opts dictionary
1188 if (!param
.b_sliced_threads
!= !defaults
.b_sliced_threads
)
1190 // can be modified by: tune zerolatency
1191 hb_dict_set(x264_opts
, "sliced-threads",
1192 hb_value_bool(!!param
.b_sliced_threads
));
1196 hb_dict_remove(x264_opts
, "sliced-threads");
1198 if (param
.i_sync_lookahead
!= defaults
.i_sync_lookahead
)
1200 // can be modified by: tune zerolatency
1201 hb_dict_set(x264_opts
, "sync-lookahead",
1202 hb_value_int(param
.i_sync_lookahead
));
1206 hb_dict_remove(x264_opts
, "sync-lookahead");
1208 if (param
.i_level_idc
!= defaults
.i_level_idc
)
1210 // can be modified by: level
1211 for (i
= 0; hb_h264_level_values
[i
]; i
++)
1212 if (param
.i_level_idc
== hb_h264_level_values
[i
])
1213 hb_dict_set(x264_opts
, "level",
1214 hb_value_string(hb_h264_level_names
[i
]));
1218 hb_dict_remove(x264_opts
, "level");
1220 if (param
.i_frame_reference
!= defaults
.i_frame_reference
)
1222 // can be modified by: presets, tunes, level
1223 hb_dict_set(x264_opts
, "ref", hb_value_int(param
.i_frame_reference
));
1227 hb_dict_remove(x264_opts
, "ref");
1229 if (param
.i_scenecut_threshold
!= defaults
.i_scenecut_threshold
)
1231 // can be modified by: preset ultrafast
1232 hb_dict_set(x264_opts
, "scenecut",
1233 hb_value_int(param
.i_scenecut_threshold
));
1237 hb_dict_remove(x264_opts
, "scenecut");
1239 if (param
.i_bframe
!= defaults
.i_bframe
)
1241 // can be modified by: presets, tunes, profile, level
1242 hb_dict_set(x264_opts
, "bframes", hb_value_int(param
.i_bframe
));
1246 hb_dict_remove(x264_opts
, "bframes");
1248 if (param
.i_bframe
> 0)
1250 if (param
.i_bframe_adaptive
!= defaults
.i_bframe_adaptive
)
1252 // can be modified by: presets
1253 hb_dict_set(x264_opts
, "b-adapt",
1254 hb_value_int(param
.i_bframe_adaptive
));
1258 hb_dict_remove(x264_opts
, "b-adapt");
1260 if (param
.i_bframe
> 1 &&
1261 param
.i_bframe_pyramid
!= defaults
.i_bframe_pyramid
)
1263 // can be modified by: level
1264 if (param
.i_bframe_pyramid
< X264_B_PYRAMID_NONE
)
1265 param
.i_bframe_pyramid
= X264_B_PYRAMID_NONE
;
1266 if (param
.i_bframe_pyramid
> X264_B_PYRAMID_NORMAL
)
1267 param
.i_bframe_pyramid
= X264_B_PYRAMID_NORMAL
;
1268 for (i
= 0; x264_b_pyramid_names
[i
] != NULL
; i
++)
1269 if (param
.i_bframe_pyramid
== i
)
1270 hb_dict_set(x264_opts
, "b-pyramid",
1271 hb_value_string(x264_b_pyramid_names
[i
]));
1275 hb_dict_remove(x264_opts
, "b-pyramid");
1277 if (param
.analyse
.i_direct_mv_pred
!= defaults
.analyse
.i_direct_mv_pred
)
1279 // can be modified by: presets
1280 if (param
.analyse
.i_direct_mv_pred
< X264_DIRECT_PRED_NONE
)
1281 param
.analyse
.i_direct_mv_pred
= X264_DIRECT_PRED_NONE
;
1282 if (param
.analyse
.i_direct_mv_pred
> X264_DIRECT_PRED_AUTO
)
1283 param
.analyse
.i_direct_mv_pred
= X264_DIRECT_PRED_AUTO
;
1284 for (i
= 0; x264_direct_pred_names
[i
] != NULL
; i
++)
1285 if (param
.analyse
.i_direct_mv_pred
== i
)
1286 hb_dict_set(x264_opts
, "direct",
1287 hb_value_string(x264_direct_pred_names
[i
]));
1291 hb_dict_remove(x264_opts
, "direct");
1293 if (!param
.analyse
.b_weighted_bipred
!=
1294 !defaults
.analyse
.b_weighted_bipred
)
1296 // can be modified by: preset ultrafast, tune fastdecode
1297 hb_dict_set(x264_opts
, "weightb",
1298 hb_value_bool(!!param
.analyse
.b_weighted_bipred
));
1302 hb_dict_remove(x264_opts
, "weightb");
1307 // no bframes, these options have no effect
1308 hb_dict_remove(x264_opts
, "b-adapt");
1309 hb_dict_remove(x264_opts
, "b-pyramid");
1310 hb_dict_remove(x264_opts
, "direct");
1311 hb_dict_remove(x264_opts
, "weightb");
1312 hb_dict_remove(x264_opts
, "b-bias");
1313 hb_dict_remove(x264_opts
, "open-gop");
1315 if (!param
.b_deblocking_filter
!= !defaults
.b_deblocking_filter
)
1317 // can be modified by: preset ultrafast, tune fastdecode
1318 hb_dict_set(x264_opts
, "no-deblock",
1319 hb_value_bool(!param
.b_deblocking_filter
));
1323 hb_dict_remove(x264_opts
, "no-deblock");
1325 if (param
.b_deblocking_filter
&&
1326 (param
.i_deblocking_filter_alphac0
!= defaults
.i_deblocking_filter_alphac0
||
1327 param
.i_deblocking_filter_beta
!= defaults
.i_deblocking_filter_beta
))
1329 // can be modified by: tunes
1330 hb_dict_set(x264_opts
, "deblock",
1331 value_pair(hb_value_int(param
.i_deblocking_filter_alphac0
),
1332 hb_value_int(param
.i_deblocking_filter_beta
)));
1336 hb_dict_remove(x264_opts
, "deblock");
1338 if (!param
.b_cabac
!= !defaults
.b_cabac
)
1340 // can be modified by: preset ultrafast, tune fastdecode, profile
1341 hb_dict_set(x264_opts
, "cabac", hb_value_bool(!!param
.b_cabac
));
1345 hb_dict_remove(x264_opts
, "cabac");
1347 if (param
.b_interlaced
!= defaults
.b_interlaced
)
1351 hb_dict_set(x264_opts
, "tff", hb_value_bool(1));
1352 hb_dict_remove(x264_opts
, "bff");
1356 hb_dict_set(x264_opts
, "bff", hb_value_bool(1));
1357 hb_dict_remove(x264_opts
, "tff");
1359 hb_dict_remove(x264_opts
, "fake-interlaced");
1361 else if (param
.b_fake_interlaced
!= defaults
.b_fake_interlaced
)
1363 hb_dict_set(x264_opts
, "fake-interlaced", hb_value_bool(1));
1364 hb_dict_remove(x264_opts
, "tff");
1365 hb_dict_remove(x264_opts
, "bff");
1369 hb_dict_remove(x264_opts
, "tff");
1370 hb_dict_remove(x264_opts
, "bff");
1371 hb_dict_remove(x264_opts
, "fake-interlaced");
1373 if (param
.i_cqm_preset
== defaults
.i_cqm_preset
&&
1374 param
.psz_cqm_file
== defaults
.psz_cqm_file
)
1376 // can be reset to default by: profile
1377 hb_dict_remove(x264_opts
, "cqm");
1378 hb_dict_remove(x264_opts
, "cqm4");
1379 hb_dict_remove(x264_opts
, "cqm8");
1380 hb_dict_remove(x264_opts
, "cqm4i");
1381 hb_dict_remove(x264_opts
, "cqm4p");
1382 hb_dict_remove(x264_opts
, "cqm8i");
1383 hb_dict_remove(x264_opts
, "cqm8p");
1384 hb_dict_remove(x264_opts
, "cqm4iy");
1385 hb_dict_remove(x264_opts
, "cqm4ic");
1386 hb_dict_remove(x264_opts
, "cqm4py");
1387 hb_dict_remove(x264_opts
, "cqm4pc");
1390 * Note: param.analyse.intra can only be modified directly or by using
1391 * x264 --preset ultrafast, but not via the "analyse" option
1393 if (param
.analyse
.inter
!= defaults
.analyse
.inter
)
1395 // can be modified by: presets, tune touhou
1396 if (!param
.analyse
.inter
)
1398 hb_dict_set(x264_opts
, "analyse", hb_value_string("none"));
1400 else if ((param
.analyse
.inter
& X264_ANALYSE_I4x4
) &&
1401 (param
.analyse
.inter
& X264_ANALYSE_I8x8
) &&
1402 (param
.analyse
.inter
& X264_ANALYSE_PSUB16x16
) &&
1403 (param
.analyse
.inter
& X264_ANALYSE_PSUB8x8
) &&
1404 (param
.analyse
.inter
& X264_ANALYSE_BSUB16x16
))
1406 hb_dict_set(x264_opts
, "analyse", hb_value_string("all"));
1410 hb_value_t
*array
= hb_value_array_init();
1411 if (param
.analyse
.inter
& X264_ANALYSE_I4x4
)
1413 hb_value_array_append(array
, hb_value_string("i4x4"));
1415 if (param
.analyse
.inter
& X264_ANALYSE_I8x8
)
1417 hb_value_array_append(array
, hb_value_string("i8x8"));
1419 if (param
.analyse
.inter
& X264_ANALYSE_PSUB16x16
)
1421 hb_value_array_append(array
, hb_value_string("p8x8"));
1423 if (param
.analyse
.inter
& X264_ANALYSE_PSUB8x8
)
1425 hb_value_array_append(array
, hb_value_string("p4x4"));
1427 if (param
.analyse
.inter
& X264_ANALYSE_BSUB16x16
)
1429 hb_value_array_append(array
, hb_value_string("b8x8"));
1431 hb_dict_set(x264_opts
, "analyse", array
);
1436 hb_dict_remove(x264_opts
, "analyse");
1438 if (!param
.analyse
.b_transform_8x8
!= !defaults
.analyse
.b_transform_8x8
)
1440 // can be modified by: preset ultrafast, profile
1441 hb_dict_set(x264_opts
, "8x8dct",
1442 hb_value_bool(!!param
.analyse
.b_transform_8x8
));
1446 hb_dict_remove(x264_opts
, "8x8dct");
1448 if (param
.analyse
.i_weighted_pred
!= defaults
.analyse
.i_weighted_pred
)
1450 // can be modified by: presets, tune fastdecode, profile
1451 hb_dict_set(x264_opts
, "weightp",
1452 hb_value_int(param
.analyse
.i_weighted_pred
));
1456 hb_dict_remove(x264_opts
, "weightp");
1458 if (param
.analyse
.i_me_method
!= defaults
.analyse
.i_me_method
)
1460 // can be modified by: presets
1461 if (param
.analyse
.i_me_method
< X264_ME_DIA
)
1462 param
.analyse
.i_me_method
= X264_ME_DIA
;
1463 if (param
.analyse
.i_me_method
> X264_ME_TESA
)
1464 param
.analyse
.i_me_method
= X264_ME_TESA
;
1465 for (i
= 0; x264_motion_est_names
[i
] != NULL
; i
++)
1466 if (param
.analyse
.i_me_method
== i
)
1467 hb_dict_set(x264_opts
, "me",
1468 hb_value_string(x264_motion_est_names
[i
]));
1472 hb_dict_remove(x264_opts
, "me");
1474 if (param
.analyse
.i_me_range
!= defaults
.analyse
.i_me_range
)
1476 // can be modified by: presets
1477 hb_dict_set(x264_opts
, "merange",
1478 hb_value_int(param
.analyse
.i_me_range
));
1482 hb_dict_remove(x264_opts
, "merange");
1484 if (param
.analyse
.i_mv_range
!= defaults
.analyse
.i_mv_range
)
1486 // can be modified by: level
1487 hb_dict_set(x264_opts
, "mvrange",
1488 hb_value_int(param
.analyse
.i_mv_range
));
1492 hb_dict_remove(x264_opts
, "mvrange");
1494 if (param
.analyse
.i_subpel_refine
> 9 && (param
.rc
.i_aq_mode
== 0 ||
1495 param
.analyse
.i_trellis
< 2))
1497 // subme 10 and higher require AQ and trellis 2
1498 param
.analyse
.i_subpel_refine
= 9;
1500 if (param
.analyse
.i_subpel_refine
!= defaults
.analyse
.i_subpel_refine
)
1502 // can be modified by: presets
1503 hb_dict_set(x264_opts
, "subme",
1504 hb_value_int(param
.analyse
.i_subpel_refine
));
1508 hb_dict_remove(x264_opts
, "subme");
1510 if (!param
.analyse
.b_mixed_references
!=
1511 !defaults
.analyse
.b_mixed_references
)
1513 // can be modified by: presets
1514 hb_dict_set(x264_opts
, "mixed-refs",
1515 hb_value_bool(!!param
.analyse
.b_mixed_references
));
1519 hb_dict_remove(x264_opts
, "mixed-refs");
1521 if (param
.analyse
.i_trellis
!= defaults
.analyse
.i_trellis
)
1523 // can be modified by: presets
1524 hb_dict_set(x264_opts
, "trellis",
1525 hb_value_int(param
.analyse
.i_trellis
));
1529 hb_dict_remove(x264_opts
, "trellis");
1531 if (!param
.analyse
.b_fast_pskip
!= !defaults
.analyse
.b_fast_pskip
)
1533 // can be modified by: preset placebo
1534 hb_dict_set(x264_opts
, "fast-pskip",
1535 hb_value_bool(!!param
.analyse
.b_fast_pskip
));
1539 hb_dict_remove(x264_opts
, "fast-pskip");
1541 if (!param
.analyse
.b_dct_decimate
!= !defaults
.analyse
.b_dct_decimate
)
1543 // can be modified by: tune grain
1544 hb_dict_set(x264_opts
, "dct-decimate",
1545 hb_value_bool(!!param
.analyse
.b_dct_decimate
));
1549 hb_dict_remove(x264_opts
, "dct-decimate");
1551 if (!param
.analyse
.b_psy
!= !defaults
.analyse
.b_psy
)
1553 // can be modified by: tunes
1554 hb_dict_set(x264_opts
, "psy", hb_value_bool(!!param
.analyse
.b_psy
));
1558 hb_dict_remove(x264_opts
, "psy");
1560 if (param
.analyse
.b_psy
&&
1561 (param
.analyse
.f_psy_rd
!= defaults
.analyse
.f_psy_rd
||
1562 param
.analyse
.f_psy_trellis
!= defaults
.analyse
.f_psy_trellis
))
1564 // can be modified by: tunes
1565 hb_dict_set(x264_opts
, "psy-rd",
1566 value_pair(hb_value_double(param
.analyse
.f_psy_rd
),
1567 hb_value_double(param
.analyse
.f_psy_trellis
)));
1571 hb_dict_remove(x264_opts
, "psy-rd");
1574 * Note: while deadzone is incompatible with trellis, it still has a slight
1575 * effect on the output even when trellis is on, so always unparse it.
1577 if (param
.analyse
.i_luma_deadzone
[0] != defaults
.analyse
.i_luma_deadzone
[0])
1579 // can be modified by: tune grain
1580 hb_dict_set(x264_opts
, "deadzone-inter",
1581 hb_value_int(param
.analyse
.i_luma_deadzone
[0]));
1585 hb_dict_remove(x264_opts
, "deadzone-inter");
1587 if (param
.analyse
.i_luma_deadzone
[1] != defaults
.analyse
.i_luma_deadzone
[1])
1589 // can be modified by: tune grain
1590 hb_dict_set(x264_opts
, "deadzone-intra",
1591 hb_value_int(param
.analyse
.i_luma_deadzone
[1]));
1595 hb_dict_remove(x264_opts
, "deadzone-intra");
1597 if (param
.rc
.i_vbv_buffer_size
!= defaults
.rc
.i_vbv_buffer_size
)
1599 // can be modified by: level
1600 hb_dict_set(x264_opts
, "vbv-bufsize",
1601 hb_value_int(param
.rc
.i_vbv_buffer_size
));
1602 if (param
.rc
.i_vbv_max_bitrate
!= defaults
.rc
.i_vbv_max_bitrate
)
1604 // can be modified by: level
1605 hb_dict_set(x264_opts
, "vbv-maxrate",
1606 hb_value_int(param
.rc
.i_vbv_max_bitrate
));
1610 hb_dict_remove(x264_opts
, "vbv-maxrate");
1615 hb_dict_remove(x264_opts
, "vbv-bufsize");
1616 hb_dict_remove(x264_opts
, "vbv-maxrate");
1618 if (param
.rc
.f_ip_factor
!= defaults
.rc
.f_ip_factor
)
1620 // can be modified by: tune grain
1621 hb_dict_set(x264_opts
, "ipratio",
1622 hb_value_double(param
.rc
.f_ip_factor
));
1626 hb_dict_remove(x264_opts
, "ipratio");
1628 if (param
.i_bframe
> 0 && !param
.rc
.b_mb_tree
&&
1629 param
.rc
.f_pb_factor
!= defaults
.rc
.f_pb_factor
)
1631 // can be modified by: tune grain
1632 hb_dict_set(x264_opts
, "pbratio",
1633 hb_value_double(param
.rc
.f_pb_factor
));
1637 // pbratio requires bframes and is incomaptible with mbtree
1638 hb_dict_remove(x264_opts
, "pbratio");
1640 if (param
.rc
.f_qcompress
!= defaults
.rc
.f_qcompress
)
1642 // can be modified by: tune grain
1643 hb_dict_set(x264_opts
, "qcomp", hb_value_double(param
.rc
.f_qcompress
));
1647 hb_dict_remove(x264_opts
, "qcomp");
1649 if (param
.rc
.i_aq_mode
!= defaults
.rc
.i_aq_mode
)
1651 // can be modified by: preset ultrafast, tune psnr
1652 hb_dict_set(x264_opts
, "aq-mode", hb_value_int(param
.rc
.i_aq_mode
));
1656 hb_dict_remove(x264_opts
, "aq-mode");
1658 if (param
.rc
.i_aq_mode
> 0 &&
1659 param
.rc
.f_aq_strength
!= defaults
.rc
.f_aq_strength
)
1661 // can be modified by: tunes
1662 hb_dict_set(x264_opts
, "aq-strength",
1663 hb_value_double(param
.rc
.f_aq_strength
));
1667 hb_dict_remove(x264_opts
, "aq-strength");
1669 if (!param
.rc
.b_mb_tree
!= !defaults
.rc
.b_mb_tree
)
1671 // can be modified by: presets, tune zerolatency
1672 hb_dict_set(x264_opts
, "mbtree", hb_value_bool(!!param
.rc
.b_mb_tree
));
1676 hb_dict_remove(x264_opts
, "mbtree");
1678 if (param
.rc
.i_lookahead
!= defaults
.rc
.i_lookahead
)
1680 // can be modified by: presets, tune zerolatency
1681 hb_dict_set(x264_opts
, "rc-lookahead",
1682 hb_value_int(param
.rc
.i_lookahead
));
1686 hb_dict_remove(x264_opts
, "rc-lookahead");
1688 if (!param
.b_vfr_input
!= !defaults
.b_vfr_input
)
1690 // can be modified by: tune zerolatency
1691 hb_dict_set(x264_opts
, "force-cfr", hb_value_bool(!param
.b_vfr_input
));
1695 hb_dict_remove(x264_opts
, "force-cfr");
1698 /* convert the x264_opts dictionary to an encopts string */
1699 unparsed_opts
= hb_dict_to_encopts(x264_opts
);
1700 hb_dict_free(&x264_opts
);
1703 return unparsed_opts
;
1706 const char * hb_x264_encopt_name(const char *name
)
1709 for (i
= 0; hb_x264_encopt_synonyms
[i
][0] != NULL
; i
++)
1710 if (!strcmp(name
, hb_x264_encopt_synonyms
[i
][1]))
1711 return hb_x264_encopt_synonyms
[i
][0];