32 #define X264_API_IMPORTS 1
88 static const int level_map[] = {
95 if (level < 0 || level > X264_LOG_DEBUG)
98 av_vlog(p, level_map[level], fmt, args);
103 x264_nal_t *nals,
int nnal)
112 for (i = 0; i < nnal; i++)
113 size += nals[i].i_payload;
132 for (i = 0; i < nnal; i++){
133 memcpy(p, nals[i].p_payload, nals[i].i_payload);
134 p += nals[i].i_payload;
165 x264_picture_t pic_out = {0};
168 x264_picture_init( &x4->
pic );
170 if (x264_bit_depth > 8)
171 x4->
pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
175 for (i = 0; i < x4->
pic.img.i_plane; i++) {
176 x4->
pic.img.plane[i] = frame->
data[i];
180 x4->
pic.i_pts = frame->
pts;
188 x264_encoder_reconfig(x4->
enc, &x4->
params);
194 x264_encoder_reconfig(x4->
enc, &x4->
params);
201 x264_encoder_reconfig(x4->
enc, &x4->
params);
204 if (x4->
params.rc.i_rc_method == X264_RC_ABR &&
207 x264_encoder_reconfig(x4->
enc, &x4->
params);
211 x4->
params.rc.i_rc_method == X264_RC_CRF &&
212 x4->
params.rc.f_rf_constant != x4->
crf) {
214 x264_encoder_reconfig(x4->
enc, &x4->
params);
217 if (x4->
params.rc.i_rc_method == X264_RC_CQP &&
219 x4->
params.rc.i_qp_constant != x4->
cqp) {
221 x264_encoder_reconfig(x4->
enc, &x4->
params);
227 x264_encoder_reconfig(x4->
enc, &x4->
params);
235 switch (stereo->
type) {
259 if (fpa_type != x4->
params.i_frame_packing) {
260 x4->
params.i_frame_packing = fpa_type;
261 x264_encoder_reconfig(x4->
enc, &x4->
params);
266 if (x264_encoder_encode(x4->
enc, &nal, &nnal, frame? &x4->
pic: NULL, &pic_out) < 0)
272 }
while (!ret && !frame && x264_encoder_delayed_frames(x4->
enc));
274 pkt->
pts = pic_out.i_pts;
275 pkt->
dts = pic_out.i_dts;
277 switch (pic_out.i_type) {
307 x264_encoder_close(x4->
enc);
314 #define OPT_STR(opt, param) \
317 if (param && (ret = x264_param_parse(&x4->params, opt, param)) < 0) { \
318 if(ret == X264_PARAM_BAD_NAME) \
319 av_log(avctx, AV_LOG_ERROR, \
320 "bad option '%s': '%s'\n", opt, param); \
322 av_log(avctx, AV_LOG_ERROR, \
323 "bad value for '%s': '%s'\n", opt, param); \
356 #define PARSE_X264_OPT(name, var)\
357 if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
358 av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
359 return AVERROR(EINVAL);\
370 x264_param_default(&x4->
params);
379 for (i = 0; x264_preset_names[i]; i++)
383 for (i = 0; x264_tune_names[i]; i++)
389 if (avctx->
level > 0)
393 x4->
params.p_log_private = avctx;
394 x4->
params.i_log_level = X264_LOG_DEBUG;
401 x4->
params.rc.i_rc_method = X264_RC_ABR;
407 x4->
params.rc.b_stat_read = 1;
410 x4->
params.rc.i_rc_method = X264_RC_CRF;
412 }
else if (x4->
cqp >= 0) {
413 x4->
params.rc.i_rc_method = X264_RC_CQP;
423 x4->
params.rc.f_vbv_buffer_init =
437 x4->
params.analyse.i_me_method = X264_ME_DIA;
439 x4->
params.analyse.i_me_method = X264_ME_HEX;
441 x4->
params.analyse.i_me_method = X264_ME_UMH;
443 x4->
params.analyse.i_me_method = X264_ME_ESA;
445 x4->
params.analyse.i_me_method = X264_ME_TESA;
453 if (avctx->
qmin >= 0)
455 if (avctx->
qmax >= 0)
459 if (avctx->
qblur >= 0)
463 if (avctx->
refs >= 0)
465 else if (x4->
level) {
470 int scale = X264_BUILD < 129 ? 384 : 1;
472 if (!strcmp(x4->
level,
"1b")) {
474 }
else if (strlen(x4->
level) <= 3){
482 for (i = 0; i<x264_levels[i].level_idc; i++)
483 if (x264_levels[i].level_idc == level_id)
484 x4->
params.i_frame_reference = av_clip(x264_levels[i].dpb / mbn / scale, 1, x4->
params.i_frame_reference);
529 x4->
params.b_vfr_input = 0;
531 if (x4->
b_bias != INT_MIN)
562 x264_param_apply_fastfirstpass(&x4->
params);
593 if (x264_param_apply_profile(&x4->
params, x4->
profile) < 0) {
597 for (i = 0; x264_profile_names[i]; i++)
606 x4->
params.vui.i_sar_width = sw;
607 x4->
params.vui.i_sar_height = sh;
638 x4->
params.b_repeat_headers = 0;
643 char param[256]={0},
val[256]={0};
644 if(sscanf(p,
"%255[^:=]=%255[^:]", param,
val) == 1){
661 "Error parsing option '%s = %s'.\n",
671 x4->
params.i_bframe_pyramid ? 2 : 1 : 0;
677 x4->
enc = x264_encoder_open(&x4->
params);
690 s = x264_encoder_headers(x4->
enc, &nal, &nnal);
693 for (i = 0; i < nnal; i++) {
695 if (nal[i].i_type ==
NAL_SEI) {
699 memcpy(x4->
sei, nal[i].p_payload, nal[i].i_payload);
702 memcpy(p, nal[i].p_payload, nal[i].i_payload);
703 p += nal[i].i_payload;
744 if (x264_bit_depth == 8)
746 else if (x264_bit_depth == 9)
748 else if (x264_bit_depth == 10)
752 #define OFFSET(x) offsetof(X264Context, x)
753 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
758 {
"fastfirstpass",
"Use fast settings when encoding first pass",
OFFSET(fastfirstpass),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
VE},
763 {
"crf",
"Select the quality for constant quality mode",
OFFSET(crf),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE },
764 {
"crf_max",
"In CRF mode, prevents VBV from lowering quality beyond this point.",
OFFSET(crf_max),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE },
765 {
"qp",
"Constant quantization parameter rate control method",
OFFSET(cqp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE },
767 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
768 {
"variance",
"Variance AQ (complexity mask)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
769 {
"autovariance",
"Auto-variance AQ (experimental)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
770 {
"aq-strength",
"AQ strength. Reduces blocking and blurring in flat and textured areas.",
OFFSET(aq_strength),
AV_OPT_TYPE_FLOAT, {.dbl = -1}, -1, FLT_MAX,
VE},
772 {
"psy-rd",
"Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.",
OFFSET(psy_rd),
AV_OPT_TYPE_STRING, {0 }, 0, 0,
VE},
773 {
"rc-lookahead",
"Number of frames to look ahead for frametype and ratecontrol",
OFFSET(rc_lookahead),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE },
774 {
"weightb",
"Weighted prediction for B-frames.",
OFFSET(weightb),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
775 {
"weightp",
"Weighted prediction analysis method.",
OFFSET(weightp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"weightp" },
776 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX,
VE,
"weightp" },
777 {
"simple", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX,
VE,
"weightp" },
778 {
"smart", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX,
VE,
"weightp" },
780 {
"intra-refresh",
"Use Periodic Intra Refresh instead of IDR frames.",
OFFSET(intra_refresh),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
781 {
"bluray-compat",
"Bluray compatibility workarounds.",
OFFSET(bluray_compat) ,
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
782 {
"b-bias",
"Influences how often B-frames are used",
OFFSET(b_bias),
AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX,
VE },
783 {
"b-pyramid",
"Keep some B-frames as references.",
OFFSET(b_pyramid),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"b_pyramid" },
784 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
785 {
"strict",
"Strictly hierarchical pyramid", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
786 {
"normal",
"Non-strict (not Blu-ray compatible)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
787 {
"mixed-refs",
"One reference per partition, as opposed to one reference per macroblock",
OFFSET(mixed_refs),
AV_OPT_TYPE_INT, { .i64 = -1}, -1, 1,
VE },
793 {
"cplxblur",
"Reduce fluctuations in QP (before curve compression)",
OFFSET(cplxblur),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE},
794 {
"partitions",
"A comma-separated list of partitions to consider. "
795 "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all",
OFFSET(partitions),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
VE},
796 {
"direct-pred",
"Direct MV prediction mode",
OFFSET(direct_pred),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"direct-pred" },
797 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0,
VE,
"direct-pred" },
798 {
"spatial", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0,
VE,
"direct-pred" },
799 {
"temporal", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0,
VE,
"direct-pred" },
800 {
"auto", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0,
VE,
"direct-pred" },
801 {
"slice-max-size",
"Limit the size of each slice in bytes",
OFFSET(slice_max_size),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE },
803 {
"nal-hrd",
"Signal HRD information (requires vbv-bufsize; "
805 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
806 {
"vbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
807 {
"cbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
808 {
"x264-params",
"Override the x264 configuration using a :-separated list of key=value parameters",
OFFSET(x264_params),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
VE },
831 {
"i_qfactor",
"-1" },
832 {
"b_qfactor",
"-1" },
840 {
"sc_threshold",
"-1" },
843 {
"me_range",
"-1" },
844 {
"me_method",
"-1" },
846 {
"b_strategy",
"-1" },
847 {
"keyint_min",
"-1" },
851 {
"thread_type",
"0" },
852 {
"flags",
"+cgop" },
853 {
"rc_init_occupancy",
"-1" },
867 .priv_class = &x264_class,
873 .
name =
"libx264rgb",
882 .priv_class = &rgbclass,