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!=NULL && (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);
381 for (i = 0; x264_preset_names[i]; i++)
385 for (i = 0; x264_tune_names[i]; i++)
391 if (avctx->
level > 0)
395 x4->
params.p_log_private = avctx;
396 x4->
params.i_log_level = X264_LOG_DEBUG;
403 x4->
params.rc.i_rc_method = X264_RC_ABR;
409 x4->
params.rc.b_stat_read = 1;
412 x4->
params.rc.i_rc_method = X264_RC_CRF;
414 }
else if (x4->
cqp >= 0) {
415 x4->
params.rc.i_rc_method = X264_RC_CQP;
425 x4->
params.rc.f_vbv_buffer_init =
435 x4->
params.analyse.i_me_method = X264_ME_DIA;
437 x4->
params.analyse.i_me_method = X264_ME_HEX;
439 x4->
params.analyse.i_me_method = X264_ME_UMH;
441 x4->
params.analyse.i_me_method = X264_ME_ESA;
443 x4->
params.analyse.i_me_method = X264_ME_TESA;
451 if (avctx->
qmin >= 0)
453 if (avctx->
qmax >= 0)
457 if (avctx->
qblur >= 0)
461 if (avctx->
refs >= 0)
463 else if (x4->
level) {
468 int scale = X264_BUILD < 129 ? 384 : 1;
470 if (!strcmp(x4->
level,
"1b")) {
472 }
else if (strlen(x4->
level) <= 3){
480 for (i = 0; i<x264_levels[i].level_idc; i++)
481 if (x264_levels[i].level_idc == level_id)
482 x4->
params.i_frame_reference = av_clip(x264_levels[i].dpb / mbn / scale, 1, x4->
params.i_frame_reference);
527 x4->
params.b_vfr_input = 0;
529 if (x4->
b_bias != INT_MIN)
560 x264_param_apply_fastfirstpass(&x4->
params);
591 if (x264_param_apply_profile(&x4->
params, x4->
profile) < 0) {
595 for (i = 0; x264_profile_names[i]; i++)
604 x4->
params.vui.i_sar_width = sw;
605 x4->
params.vui.i_sar_height = sh;
636 x4->
params.b_repeat_headers = 0;
641 char param[256]={0},
val[256]={0};
642 if(sscanf(p,
"%255[^:=]=%255[^:]", param,
val) == 1){
659 "Error parsing option '%s = %s'.\n",
669 x4->
params.i_bframe_pyramid ? 2 : 1 : 0;
675 x4->
enc = x264_encoder_open(&x4->
params);
688 s = x264_encoder_headers(x4->
enc, &nal, &nnal);
691 for (i = 0; i < nnal; i++) {
693 if (nal[i].i_type ==
NAL_SEI) {
697 memcpy(x4->
sei, nal[i].p_payload, nal[i].i_payload);
700 memcpy(p, nal[i].p_payload, nal[i].i_payload);
701 p += nal[i].i_payload;
742 if (x264_bit_depth == 8)
744 else if (x264_bit_depth == 9)
746 else if (x264_bit_depth == 10)
750 #define OFFSET(x) offsetof(X264Context, x)
751 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
756 {
"fastfirstpass",
"Use fast settings when encoding first pass",
OFFSET(fastfirstpass),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
VE},
761 {
"crf",
"Select the quality for constant quality mode",
OFFSET(crf),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE },
762 {
"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 },
763 {
"qp",
"Constant quantization parameter rate control method",
OFFSET(cqp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE },
765 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
766 {
"variance",
"Variance AQ (complexity mask)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
767 {
"autovariance",
"Auto-variance AQ (experimental)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
768 {
"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},
770 {
"psy-rd",
"Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.",
OFFSET(psy_rd),
AV_OPT_TYPE_STRING, {0 }, 0, 0,
VE},
771 {
"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 },
772 {
"weightb",
"Weighted prediction for B-frames.",
OFFSET(weightb),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
773 {
"weightp",
"Weighted prediction analysis method.",
OFFSET(weightp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"weightp" },
774 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX,
VE,
"weightp" },
775 {
"simple", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX,
VE,
"weightp" },
776 {
"smart", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX,
VE,
"weightp" },
778 {
"intra-refresh",
"Use Periodic Intra Refresh instead of IDR frames.",
OFFSET(intra_refresh),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
779 {
"bluray-compat",
"Bluray compatibility workarounds.",
OFFSET(bluray_compat) ,
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
780 {
"b-bias",
"Influences how often B-frames are used",
OFFSET(b_bias),
AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX,
VE },
781 {
"b-pyramid",
"Keep some B-frames as references.",
OFFSET(b_pyramid),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"b_pyramid" },
782 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
783 {
"strict",
"Strictly hierarchical pyramid", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
784 {
"normal",
"Non-strict (not Blu-ray compatible)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
785 {
"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 },
791 {
"cplxblur",
"Reduce fluctuations in QP (before curve compression)",
OFFSET(cplxblur),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE},
792 {
"partitions",
"A comma-separated list of partitions to consider. "
793 "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all",
OFFSET(partitions),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
VE},
794 {
"direct-pred",
"Direct MV prediction mode",
OFFSET(direct_pred),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"direct-pred" },
795 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0,
VE,
"direct-pred" },
796 {
"spatial", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0,
VE,
"direct-pred" },
797 {
"temporal", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0,
VE,
"direct-pred" },
798 {
"auto", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0,
VE,
"direct-pred" },
799 {
"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 },
801 {
"nal-hrd",
"Signal HRD information (requires vbv-bufsize; "
803 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
804 {
"vbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
805 {
"cbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
806 {
"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 },
829 {
"i_qfactor",
"-1" },
837 {
"sc_threshold",
"-1" },
840 {
"me_range",
"-1" },
841 {
"me_method",
"-1" },
843 {
"b_strategy",
"-1" },
844 {
"keyint_min",
"-1" },
848 {
"thread_type",
"0" },
849 {
"flags",
"+cgop" },
850 {
"rc_init_occupancy",
"-1" },
864 .priv_class = &x264_class,
870 .
name =
"libx264rgb",
879 .priv_class = &rgbclass,