30 #define X264_API_IMPORTS 1
86 static const int level_map[] = {
93 if (level < 0 || level > X264_LOG_DEBUG)
96 av_vlog(p, level_map[level], fmt, args);
101 x264_nal_t *nals,
int nnal)
110 for (i = 0; i < nnal; i++)
111 size += nals[i].i_payload;
130 for (i = 0; i < nnal; i++){
131 memcpy(p, nals[i].p_payload, nals[i].i_payload);
132 p += nals[i].i_payload;
163 x264_picture_t pic_out = {0};
165 x264_picture_init( &x4->
pic );
167 if (x264_bit_depth > 8)
168 x4->
pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
172 for (i = 0; i < x4->
pic.img.i_plane; i++) {
173 x4->
pic.img.plane[i] = frame->
data[i];
177 x4->
pic.i_pts = frame->
pts;
185 x264_encoder_reconfig(x4->
enc, &x4->
params);
191 x264_encoder_reconfig(x4->
enc, &x4->
params);
196 if (x264_encoder_encode(x4->
enc, &nal, &nnal, frame? &x4->
pic: NULL, &pic_out) < 0)
202 }
while (!ret && !frame && x264_encoder_delayed_frames(x4->
enc));
204 pkt->
pts = pic_out.i_pts;
205 pkt->
dts = pic_out.i_dts;
207 switch (pic_out.i_type) {
237 x264_encoder_close(x4->
enc);
244 #define OPT_STR(opt, param) \
247 if (param!=NULL && (ret = x264_param_parse(&x4->params, opt, param)) < 0) { \
248 if(ret == X264_PARAM_BAD_NAME) \
249 av_log(avctx, AV_LOG_ERROR, \
250 "bad option '%s': '%s'\n", opt, param); \
252 av_log(avctx, AV_LOG_ERROR, \
253 "bad value for '%s': '%s'\n", opt, param); \
286 #define PARSE_X264_OPT(name, var)\
287 if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
288 av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
289 return AVERROR(EINVAL);\
297 x264_param_default(&x4->
params);
308 for (i = 0; x264_preset_names[i]; i++)
312 for (i = 0; x264_tune_names[i]; i++)
318 if (avctx->
level > 0)
322 x4->
params.p_log_private = avctx;
323 x4->
params.i_log_level = X264_LOG_DEBUG;
330 x4->
params.rc.i_rc_method = X264_RC_ABR;
336 x4->
params.rc.b_stat_read = 1;
339 x4->
params.rc.i_rc_method = X264_RC_CRF;
341 }
else if (x4->
cqp >= 0) {
342 x4->
params.rc.i_rc_method = X264_RC_CQP;
352 x4->
params.rc.f_vbv_buffer_init =
361 char param[256]={0},
val[256]={0};
362 if(sscanf(p,
"%255[^:=]=%255[^:]", param,
val) == 1){
375 x4->
params.analyse.i_me_method = X264_ME_DIA;
377 x4->
params.analyse.i_me_method = X264_ME_HEX;
379 x4->
params.analyse.i_me_method = X264_ME_UMH;
381 x4->
params.analyse.i_me_method = X264_ME_ESA;
383 x4->
params.analyse.i_me_method = X264_ME_TESA;
391 if (avctx->
qmin >= 0)
393 if (avctx->
qmax >= 0)
397 if (avctx->
qblur >= 0)
401 if (avctx->
refs >= 0)
445 x4->
params.b_vfr_input = 0;
447 if (x4->
b_bias != INT_MIN)
478 x264_param_apply_fastfirstpass(&x4->
params);
509 if (x264_param_apply_profile(&x4->
params, x4->
profile) < 0) {
513 for (i = 0; x264_profile_names[i]; i++)
522 x4->
params.vui.i_sar_width = sw;
523 x4->
params.vui.i_sar_height = sh;
544 x4->
params.b_repeat_headers = 0;
554 "Error parsing option '%s = %s'.\n",
564 x4->
params.i_bframe_pyramid ? 2 : 1 : 0;
570 x4->
enc = x264_encoder_open(&x4->
params);
583 s = x264_encoder_headers(x4->
enc, &nal, &nnal);
586 for (i = 0; i < nnal; i++) {
588 if (nal[i].i_type ==
NAL_SEI) {
592 memcpy(x4->
sei, nal[i].p_payload, nal[i].i_payload);
595 memcpy(p, nal[i].p_payload, nal[i].i_payload);
596 p += nal[i].i_payload;
637 if (x264_bit_depth == 8)
639 else if (x264_bit_depth == 9)
641 else if (x264_bit_depth == 10)
645 #define OFFSET(x) offsetof(X264Context, x)
646 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
651 {
"fastfirstpass",
"Use fast settings when encoding first pass",
OFFSET(fastfirstpass),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
VE},
656 {
"crf",
"Select the quality for constant quality mode",
OFFSET(crf),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE },
657 {
"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 },
658 {
"qp",
"Constant quantization parameter rate control method",
OFFSET(cqp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE },
660 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
661 {
"variance",
"Variance AQ (complexity mask)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
662 {
"autovariance",
"Auto-variance AQ (experimental)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
663 {
"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},
665 {
"psy-rd",
"Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.",
OFFSET(psy_rd),
AV_OPT_TYPE_STRING, {0 }, 0, 0,
VE},
666 {
"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 },
667 {
"weightb",
"Weighted prediction for B-frames.",
OFFSET(weightb),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
668 {
"weightp",
"Weighted prediction analysis method.",
OFFSET(weightp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"weightp" },
669 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX,
VE,
"weightp" },
670 {
"simple", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX,
VE,
"weightp" },
671 {
"smart", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX,
VE,
"weightp" },
673 {
"intra-refresh",
"Use Periodic Intra Refresh instead of IDR frames.",
OFFSET(intra_refresh),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
674 {
"bluray-compat",
"Bluray compatibility workarounds.",
OFFSET(bluray_compat) ,
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
675 {
"b-bias",
"Influences how often B-frames are used",
OFFSET(b_bias),
AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX,
VE },
676 {
"b-pyramid",
"Keep some B-frames as references.",
OFFSET(b_pyramid),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"b_pyramid" },
677 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
678 {
"strict",
"Strictly hierarchical pyramid", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
679 {
"normal",
"Non-strict (not Blu-ray compatible)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
680 {
"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 },
686 {
"cplxblur",
"Reduce fluctuations in QP (before curve compression)",
OFFSET(cplxblur),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE},
687 {
"partitions",
"A comma-separated list of partitions to consider. "
688 "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all",
OFFSET(partitions),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
VE},
689 {
"direct-pred",
"Direct MV prediction mode",
OFFSET(direct_pred),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"direct-pred" },
690 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0,
VE,
"direct-pred" },
691 {
"spatial", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0,
VE,
"direct-pred" },
692 {
"temporal", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0,
VE,
"direct-pred" },
693 {
"auto", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0,
VE,
"direct-pred" },
694 {
"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 },
696 {
"nal-hrd",
"Signal HRD information (requires vbv-bufsize; "
698 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
699 {
"vbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
700 {
"cbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
701 {
"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 },
724 {
"i_qfactor",
"-1" },
732 {
"sc_threshold",
"-1" },
735 {
"me_range",
"-1" },
736 {
"me_method",
"-1" },
738 {
"b_strategy",
"-1" },
739 {
"keyint_min",
"-1" },
743 {
"thread_type",
"0" },
744 {
"flags",
"+cgop" },
745 {
"rc_init_occupancy",
"-1" },
759 .priv_class = &x264_class,
765 .
name =
"libx264rgb",
774 .priv_class = &rgbclass,