32 #define X264_API_IMPORTS 1
89 static const int level_map[] = {
96 if (level < 0 || level > X264_LOG_DEBUG)
99 av_vlog(p, level_map[level], fmt, args);
104 const x264_nal_t *nals,
int nnal)
113 for (i = 0; i < nnal; i++)
114 size += nals[i].i_payload;
133 for (i = 0; i < nnal; i++){
134 memcpy(p, nals[i].p_payload, nals[i].i_payload);
135 p += nals[i].i_payload;
166 x264_picture_t pic_out = {0};
169 x264_picture_init( &x4->
pic );
171 if (x264_bit_depth > 8)
172 x4->
pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
176 for (i = 0; i < x4->
pic.img.i_plane; i++) {
177 x4->
pic.img.plane[i] = frame->
data[i];
181 x4->
pic.i_pts = frame->
pts;
191 x264_encoder_reconfig(x4->
enc, &x4->
params);
197 x264_encoder_reconfig(x4->
enc, &x4->
params);
204 x264_encoder_reconfig(x4->
enc, &x4->
params);
207 if (x4->
params.rc.i_rc_method == X264_RC_ABR &&
210 x264_encoder_reconfig(x4->
enc, &x4->
params);
214 x4->
params.rc.i_rc_method == X264_RC_CRF &&
215 x4->
params.rc.f_rf_constant != x4->
crf) {
217 x264_encoder_reconfig(x4->
enc, &x4->
params);
220 if (x4->
params.rc.i_rc_method == X264_RC_CQP &&
222 x4->
params.rc.i_qp_constant != x4->
cqp) {
224 x264_encoder_reconfig(x4->
enc, &x4->
params);
230 x264_encoder_reconfig(x4->
enc, &x4->
params);
239 switch (stereo->
type) {
263 if (fpa_type != x4->
params.i_frame_packing) {
264 x4->
params.i_frame_packing = fpa_type;
265 x264_encoder_reconfig(x4->
enc, &x4->
params);
270 if (x264_encoder_encode(x4->
enc, &nal, &nnal, frame? &x4->
pic: NULL, &pic_out) < 0)
276 }
while (!ret && !frame && x264_encoder_delayed_frames(x4->
enc));
278 pkt->
pts = pic_out.i_pts;
279 pkt->
dts = pic_out.i_dts;
281 switch (pic_out.i_type) {
311 x264_encoder_close(x4->
enc);
318 #define OPT_STR(opt, param) \
321 if (param && (ret = x264_param_parse(&x4->params, opt, param)) < 0) { \
322 if(ret == X264_PARAM_BAD_NAME) \
323 av_log(avctx, AV_LOG_ERROR, \
324 "bad option '%s': '%s'\n", opt, param); \
326 av_log(avctx, AV_LOG_ERROR, \
327 "bad value for '%s': '%s'\n", opt, param); \
360 #define PARSE_X264_OPT(name, var)\
361 if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
362 av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
363 return AVERROR(EINVAL);\
374 x264_param_default(&x4->
params);
383 for (i = 0; x264_preset_names[i]; i++)
387 for (i = 0; x264_tune_names[i]; i++)
393 if (avctx->
level > 0)
397 x4->
params.p_log_private = avctx;
398 x4->
params.i_log_level = X264_LOG_DEBUG;
405 x4->
params.rc.i_rc_method = X264_RC_ABR;
411 x4->
params.rc.b_stat_read = 1;
414 x4->
params.rc.i_rc_method = X264_RC_CRF;
416 }
else if (x4->
cqp >= 0) {
417 x4->
params.rc.i_rc_method = X264_RC_CQP;
427 x4->
params.rc.f_vbv_buffer_init =
441 x4->
params.analyse.i_me_method = X264_ME_DIA;
443 x4->
params.analyse.i_me_method = X264_ME_HEX;
445 x4->
params.analyse.i_me_method = X264_ME_UMH;
447 x4->
params.analyse.i_me_method = X264_ME_ESA;
449 x4->
params.analyse.i_me_method = X264_ME_TESA;
457 if (avctx->
qmin >= 0)
459 if (avctx->
qmax >= 0)
463 if (avctx->
qblur >= 0)
467 if (avctx->
refs >= 0)
469 else if (x4->
level) {
474 int scale = X264_BUILD < 129 ? 384 : 1;
476 if (!strcmp(x4->
level,
"1b")) {
478 }
else if (strlen(x4->
level) <= 3){
486 for (i = 0; i<x264_levels[i].level_idc; i++)
487 if (x264_levels[i].level_idc == level_id)
488 x4->
params.i_frame_reference = av_clip(x264_levels[i].dpb / mbn / scale, 1, x4->
params.i_frame_reference);
533 x4->
params.b_vfr_input = 0;
536 #if X264_BUILD >= 142
540 "x264 too old for AVC Intra, at least version 142 needed\n");
542 if (x4->
b_bias != INT_MIN)
573 x264_param_apply_fastfirstpass(&x4->
params);
604 if (x264_param_apply_profile(&x4->
params, x4->
profile) < 0) {
608 for (i = 0; x264_profile_names[i]; i++)
617 x4->
params.vui.i_sar_width = sw;
618 x4->
params.vui.i_sar_height = sh;
649 x4->
params.b_repeat_headers = 0;
654 char param[256]={0},
val[256]={0};
655 if(sscanf(p,
"%255[^:=]=%255[^:]", param,
val) == 1){
672 "Error parsing option '%s = %s'.\n",
682 x4->
params.i_bframe_pyramid ? 2 : 1 : 0;
688 x4->
enc = x264_encoder_open(&x4->
params);
701 s = x264_encoder_headers(x4->
enc, &nal, &nnal);
704 for (i = 0; i < nnal; i++) {
706 if (nal[i].i_type ==
NAL_SEI) {
710 memcpy(x4->
sei, nal[i].p_payload, nal[i].i_payload);
713 memcpy(p, nal[i].p_payload, nal[i].i_payload);
714 p += nal[i].i_payload;
755 if (x264_bit_depth == 8)
757 else if (x264_bit_depth == 9)
759 else if (x264_bit_depth == 10)
763 #define OFFSET(x) offsetof(X264Context, x)
764 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
769 {
"fastfirstpass",
"Use fast settings when encoding first pass",
OFFSET(fastfirstpass),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
VE},
774 {
"crf",
"Select the quality for constant quality mode",
OFFSET(crf),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE },
775 {
"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 },
776 {
"qp",
"Constant quantization parameter rate control method",
OFFSET(cqp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE },
778 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
779 {
"variance",
"Variance AQ (complexity mask)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
780 {
"autovariance",
"Auto-variance AQ (experimental)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
781 {
"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},
783 {
"psy-rd",
"Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.",
OFFSET(psy_rd),
AV_OPT_TYPE_STRING, {0 }, 0, 0,
VE},
784 {
"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 },
785 {
"weightb",
"Weighted prediction for B-frames.",
OFFSET(weightb),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
786 {
"weightp",
"Weighted prediction analysis method.",
OFFSET(weightp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"weightp" },
787 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX,
VE,
"weightp" },
788 {
"simple", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX,
VE,
"weightp" },
789 {
"smart", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX,
VE,
"weightp" },
791 {
"intra-refresh",
"Use Periodic Intra Refresh instead of IDR frames.",
OFFSET(intra_refresh),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
792 {
"bluray-compat",
"Bluray compatibility workarounds.",
OFFSET(bluray_compat) ,
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
793 {
"b-bias",
"Influences how often B-frames are used",
OFFSET(b_bias),
AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX,
VE },
794 {
"b-pyramid",
"Keep some B-frames as references.",
OFFSET(b_pyramid),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"b_pyramid" },
795 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
796 {
"strict",
"Strictly hierarchical pyramid", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
797 {
"normal",
"Non-strict (not Blu-ray compatible)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
798 {
"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 },
804 {
"cplxblur",
"Reduce fluctuations in QP (before curve compression)",
OFFSET(cplxblur),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE},
805 {
"partitions",
"A comma-separated list of partitions to consider. "
806 "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all",
OFFSET(partitions),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
VE},
807 {
"direct-pred",
"Direct MV prediction mode",
OFFSET(direct_pred),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"direct-pred" },
808 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0,
VE,
"direct-pred" },
809 {
"spatial", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0,
VE,
"direct-pred" },
810 {
"temporal", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0,
VE,
"direct-pred" },
811 {
"auto", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0,
VE,
"direct-pred" },
812 {
"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 },
814 {
"nal-hrd",
"Signal HRD information (requires vbv-bufsize; "
816 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
817 {
"vbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
818 {
"cbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
819 {
"avcintra-class",
"AVC-Intra class 50/100/200",
OFFSET(avcintra_class),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 200 ,
VE},
820 {
"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 },
843 {
"i_qfactor",
"-1" },
844 {
"b_qfactor",
"-1" },
852 {
"sc_threshold",
"-1" },
855 {
"me_range",
"-1" },
856 {
"me_method",
"-1" },
858 {
"b_strategy",
"-1" },
859 {
"keyint_min",
"-1" },
863 {
"thread_type",
"0" },
864 {
"flags",
"+cgop" },
865 {
"rc_init_occupancy",
"-1" },
879 .priv_class = &x264_class,
885 .
name =
"libx264rgb",
894 .priv_class = &rgbclass,