183
255
x4->params.rc.i_rc_method = X264_RC_CQP;
184
256
x4->params.rc.i_qp_constant = avctx->cqp;
188
// if neither crf nor cqp modes are selected we have to enable the RC
189
// we do it this way because we cannot check if the bitrate has been set
190
if (!(avctx->crf || (avctx->cqp > -1)))
191
x4->params.rc.i_rc_method = X264_RC_ABR;
193
x4->params.i_bframe = avctx->max_b_frames;
194
x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
195
x4->params.i_bframe_adaptive = avctx->b_frame_strategy;
196
x4->params.i_bframe_bias = avctx->bframebias;
197
x4->params.i_bframe_pyramid = avctx->flags2 & CODEC_FLAG2_BPYRAMID ? X264_B_PYRAMID_NORMAL : X264_B_PYRAMID_NONE;
198
avctx->has_b_frames = avctx->flags2 & CODEC_FLAG2_BPYRAMID ? 2 : !!avctx->max_b_frames;
200
x4->params.i_keyint_min = avctx->keyint_min;
201
if (x4->params.i_keyint_min > x4->params.i_keyint_max)
202
x4->params.i_keyint_min = x4->params.i_keyint_max;
204
x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
206
x4->params.b_deblocking_filter = avctx->flags & CODEC_FLAG_LOOP_FILTER;
207
x4->params.i_deblocking_filter_alphac0 = avctx->deblockalpha;
208
x4->params.i_deblocking_filter_beta = avctx->deblockbeta;
210
x4->params.rc.i_qp_min = avctx->qmin;
211
x4->params.rc.i_qp_max = avctx->qmax;
212
x4->params.rc.i_qp_step = avctx->max_qdiff;
214
x4->params.rc.f_qcompress = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
215
x4->params.rc.f_qblur = avctx->qblur; /* temporally blur quants */
216
x4->params.rc.f_complexity_blur = avctx->complexityblur;
218
x4->params.i_frame_reference = avctx->refs;
220
x4->params.i_width = avctx->width;
221
x4->params.i_height = avctx->height;
222
x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
223
x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
224
x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
225
x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
227
x4->params.analyse.inter = 0;
261
x4->params.rc.i_rc_method = X264_RC_CRF;
262
x4->params.rc.f_rf_constant = x4->crf;
263
} else if (x4->cqp >= 0) {
264
x4->params.rc.i_rc_method = X264_RC_CQP;
265
x4->params.rc.i_qp_constant = x4->cqp;
268
if (x4->crf_max >= 0)
269
x4->params.rc.f_rf_constant_max = x4->crf_max;
272
if (avctx->rc_buffer_size && avctx->rc_initial_buffer_occupancy &&
273
(avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
274
x4->params.rc.f_vbv_buffer_init =
275
(float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
278
x4->params.rc.f_ip_factor = 1 / fabs(avctx->i_quant_factor);
279
x4->params.rc.f_pb_factor = avctx->b_quant_factor;
280
x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
282
#if FF_API_X264_GLOBAL_OPTS
283
if (avctx->aq_mode >= 0)
284
x4->params.rc.i_aq_mode = avctx->aq_mode;
285
if (avctx->aq_strength >= 0)
286
x4->params.rc.f_aq_strength = avctx->aq_strength;
287
if (avctx->psy_rd >= 0)
288
x4->params.analyse.f_psy_rd = avctx->psy_rd;
289
if (avctx->psy_trellis >= 0)
290
x4->params.analyse.f_psy_trellis = avctx->psy_trellis;
291
if (avctx->rc_lookahead >= 0)
292
x4->params.rc.i_lookahead = avctx->rc_lookahead;
293
if (avctx->weighted_p_pred >= 0)
294
x4->params.analyse.i_weighted_pred = avctx->weighted_p_pred;
295
if (avctx->bframebias)
296
x4->params.i_bframe_bias = avctx->bframebias;
297
if (avctx->deblockalpha)
298
x4->params.i_deblocking_filter_alphac0 = avctx->deblockalpha;
299
if (avctx->deblockbeta)
300
x4->params.i_deblocking_filter_beta = avctx->deblockbeta;
301
if (avctx->complexityblur >= 0)
302
x4->params.rc.f_complexity_blur = avctx->complexityblur;
303
if (avctx->directpred >= 0)
304
x4->params.analyse.i_direct_mv_pred = avctx->directpred;
228
305
if (avctx->partitions) {
229
306
if (avctx->partitions & X264_PART_I4X4)
230
307
x4->params.analyse.inter |= X264_ANALYSE_I4x4;
253
336
x4->params.analyse.i_me_method = X264_ME_ESA;
254
337
else if (avctx->me_method == ME_TESA)
255
338
x4->params.analyse.i_me_method = X264_ME_TESA;
256
else x4->params.analyse.i_me_method = X264_ME_HEX;
258
x4->params.rc.i_aq_mode = avctx->aq_mode;
259
x4->params.rc.f_aq_strength = avctx->aq_strength;
260
x4->params.rc.i_lookahead = avctx->rc_lookahead;
262
x4->params.analyse.b_psy = avctx->flags2 & CODEC_FLAG2_PSY;
263
x4->params.analyse.f_psy_rd = avctx->psy_rd;
264
x4->params.analyse.f_psy_trellis = avctx->psy_trellis;
266
x4->params.analyse.i_me_range = avctx->me_range;
267
x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
269
x4->params.analyse.b_mixed_references = avctx->flags2 & CODEC_FLAG2_MIXED_REFS;
270
x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA;
271
x4->params.analyse.b_transform_8x8 = avctx->flags2 & CODEC_FLAG2_8X8DCT;
272
x4->params.analyse.b_fast_pskip = avctx->flags2 & CODEC_FLAG2_FASTPSKIP;
274
x4->params.analyse.i_trellis = avctx->trellis;
275
x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
277
if (avctx->level > 0)
278
x4->params.i_level_idc = avctx->level;
280
if (avctx->rc_buffer_size && avctx->rc_initial_buffer_occupancy &&
281
(avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
282
x4->params.rc.f_vbv_buffer_init =
283
(float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
286
x4->params.rc.b_mb_tree = !!(avctx->flags2 & CODEC_FLAG2_MBTREE);
287
x4->params.rc.f_ip_factor = 1 / fabs(avctx->i_quant_factor);
288
x4->params.rc.f_pb_factor = avctx->b_quant_factor;
289
x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
340
if (avctx->gop_size >= 0)
341
x4->params.i_keyint_max = avctx->gop_size;
342
if (avctx->max_b_frames >= 0)
343
x4->params.i_bframe = avctx->max_b_frames;
344
if (avctx->scenechange_threshold >= 0)
345
x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
346
if (avctx->qmin >= 0)
347
x4->params.rc.i_qp_min = avctx->qmin;
348
if (avctx->qmax >= 0)
349
x4->params.rc.i_qp_max = avctx->qmax;
350
if (avctx->max_qdiff >= 0)
351
x4->params.rc.i_qp_step = avctx->max_qdiff;
352
if (avctx->qblur >= 0)
353
x4->params.rc.f_qblur = avctx->qblur; /* temporally blur quants */
354
if (avctx->qcompress >= 0)
355
x4->params.rc.f_qcompress = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
356
if (avctx->refs >= 0)
357
x4->params.i_frame_reference = avctx->refs;
358
if (avctx->trellis >= 0)
359
x4->params.analyse.i_trellis = avctx->trellis;
360
if (avctx->me_range >= 0)
361
x4->params.analyse.i_me_range = avctx->me_range;
362
if (avctx->noise_reduction >= 0)
363
x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
364
if (avctx->me_subpel_quality >= 0)
365
x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
366
if (avctx->b_frame_strategy >= 0)
367
x4->params.i_bframe_adaptive = avctx->b_frame_strategy;
368
if (avctx->keyint_min >= 0)
369
x4->params.i_keyint_min = avctx->keyint_min;
370
if (avctx->coder_type >= 0)
371
x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
372
if (avctx->me_cmp >= 0)
373
x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA;
375
if (x4->aq_mode >= 0)
376
x4->params.rc.i_aq_mode = x4->aq_mode;
377
if (x4->aq_strength >= 0)
378
x4->params.rc.f_aq_strength = x4->aq_strength;
379
PARSE_X264_OPT("psy-rd", psy_rd);
380
PARSE_X264_OPT("deblock", deblock);
381
PARSE_X264_OPT("partitions", partitions);
383
x4->params.analyse.b_psy = x4->psy;
384
if (x4->rc_lookahead >= 0)
385
x4->params.rc.i_lookahead = x4->rc_lookahead;
386
if (x4->weightp >= 0)
387
x4->params.analyse.i_weighted_pred = x4->weightp;
388
if (x4->weightb >= 0)
389
x4->params.analyse.b_weighted_bipred = x4->weightb;
390
if (x4->cplxblur >= 0)
391
x4->params.rc.f_complexity_blur = x4->cplxblur;
394
x4->params.analyse.b_ssim = x4->ssim;
395
if (x4->intra_refresh >= 0)
396
x4->params.b_intra_refresh = x4->intra_refresh;
397
if (x4->b_bias != INT_MIN)
398
x4->params.i_bframe_bias = x4->b_bias;
399
if (x4->b_pyramid >= 0)
400
x4->params.i_bframe_pyramid = x4->b_pyramid;
401
if (x4->mixed_refs >= 0)
402
x4->params.analyse.b_mixed_references = x4->mixed_refs;
404
x4->params.analyse.b_transform_8x8 = x4->dct8x8;
405
if (x4->fast_pskip >= 0)
406
x4->params.analyse.b_fast_pskip = x4->fast_pskip;
408
x4->params.b_aud = x4->aud;
410
x4->params.rc.b_mb_tree = x4->mbtree;
411
if (x4->direct_pred >= 0)
412
x4->params.analyse.i_direct_mv_pred = x4->direct_pred;
414
if (x4->slice_max_size >= 0)
415
x4->params.i_slice_max_size = x4->slice_max_size;
417
if (x4->fastfirstpass)
418
x264_param_apply_fastfirstpass(&x4->params);
421
if (x264_param_apply_profile(&x4->params, x4->profile) < 0) {
422
av_log(avctx, AV_LOG_ERROR, "Error setting profile %s.\n", x4->profile);
423
return AVERROR(EINVAL);
426
x4->params.i_width = avctx->width;
427
x4->params.i_height = avctx->height;
428
x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
429
x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
430
x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
431
x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
291
433
x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
292
x4->params.analyse.b_ssim = avctx->flags2 & CODEC_FLAG2_SSIM;
293
x4->params.i_log_level = X264_LOG_DEBUG;
295
x4->params.b_aud = avctx->flags2 & CODEC_FLAG2_AUD;
297
435
x4->params.i_threads = avctx->thread_count;
479
static const enum PixelFormat pix_fmts_8bit[] = {
486
static const enum PixelFormat pix_fmts_9bit[] = {
491
static const enum PixelFormat pix_fmts_10bit[] = {
498
static av_cold void X264_init_static(AVCodec *codec)
500
if (x264_bit_depth == 8)
501
codec->pix_fmts = pix_fmts_8bit;
502
else if (x264_bit_depth == 9)
503
codec->pix_fmts = pix_fmts_9bit;
504
else if (x264_bit_depth == 10)
505
codec->pix_fmts = pix_fmts_10bit;
508
#define OFFSET(x) offsetof(X264Context, x)
509
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
510
static const AVOption options[] = {
511
{ "preset", "Set the encoding preset (cf. x264 --fullhelp)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},
512
{ "tune", "Tune the encoding params (cf. x264 --fullhelp)", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
513
{ "profile", "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
514
{ "fastfirstpass", "Use fast settings when encoding first pass", OFFSET(fastfirstpass), AV_OPT_TYPE_INT, { 1 }, 0, 1, VE},
515
{ "crf", "Select the quality for constant quality mode", OFFSET(crf), AV_OPT_TYPE_FLOAT, {-1 }, -1, FLT_MAX, VE },
516
{ "crf_max", "In CRF mode, prevents VBV from lowering quality beyond this point.",OFFSET(crf_max), AV_OPT_TYPE_FLOAT, {-1 }, -1, FLT_MAX, VE },
517
{ "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE },
518
{ "aq-mode", "AQ method", OFFSET(aq_mode), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "aq_mode"},
519
{ "none", NULL, 0, AV_OPT_TYPE_CONST, {X264_AQ_NONE}, INT_MIN, INT_MAX, VE, "aq_mode" },
520
{ "variance", "Variance AQ (complexity mask)", 0, AV_OPT_TYPE_CONST, {X264_AQ_VARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
521
{ "autovariance", "Auto-variance AQ (experimental)", 0, AV_OPT_TYPE_CONST, {X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
522
{ "aq-strength", "AQ strength. Reduces blocking and blurring in flat and textured areas.", OFFSET(aq_strength), AV_OPT_TYPE_FLOAT, {-1}, -1, FLT_MAX, VE},
523
{ "psy", "Use psychovisual optimizations.", OFFSET(psy), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
524
{ "psy-rd", "Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.", OFFSET(psy_rd), AV_OPT_TYPE_STRING, {0 }, 0, 0, VE},
525
{ "rc-lookahead", "Number of frames to look ahead for frametype and ratecontrol", OFFSET(rc_lookahead), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE },
526
{ "weightb", "Weighted prediction for B-frames.", OFFSET(weightb), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
527
{ "weightp", "Weighted prediction analysis method.", OFFSET(weightp), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "weightp" },
528
{ "none", NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_NONE}, INT_MIN, INT_MAX, VE, "weightp" },
529
{ "simple", NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX, VE, "weightp" },
530
{ "smart", NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_SMART}, INT_MIN, INT_MAX, VE, "weightp" },
531
{ "ssim", "Calculate and print SSIM stats.", OFFSET(ssim), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
532
{ "intra-refresh", "Use Periodic Intra Refresh instead of IDR frames.",OFFSET(intra_refresh),AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
533
{ "b-bias", "Influences how often B-frames are used", OFFSET(b_bias), AV_OPT_TYPE_INT, {INT_MIN}, INT_MIN, INT_MAX, VE },
534
{ "b-pyramid", "Keep some B-frames as references.", OFFSET(b_pyramid), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "b_pyramid" },
535
{ "none", NULL, 0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX, VE, "b_pyramid" },
536
{ "strict", "Strictly hierarchical pyramid", 0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX, VE, "b_pyramid" },
537
{ "normal", "Non-strict (not Blu-ray compatible)", 0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX, VE, "b_pyramid" },
538
{ "mixed-refs", "One reference per partition, as opposed to one reference per macroblock", OFFSET(mixed_refs), AV_OPT_TYPE_INT, {-1}, -1, 1, VE },
539
{ "8x8dct", "High profile 8x8 transform.", OFFSET(dct8x8), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
540
{ "fast-pskip", NULL, OFFSET(fast_pskip), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
541
{ "aud", "Use access unit delimiters.", OFFSET(aud), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
542
{ "mbtree", "Use macroblock tree ratecontrol.", OFFSET(mbtree), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
543
{ "deblock", "Loop filter parameters, in <alpha:beta> form.", OFFSET(deblock), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
544
{ "cplxblur", "Reduce fluctuations in QP (before curve compression)", OFFSET(cplxblur), AV_OPT_TYPE_FLOAT, {-1 }, -1, FLT_MAX, VE},
545
{ "partitions", "A comma-separated list of partitions to consider. "
546
"Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all", OFFSET(partitions), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
547
{ "direct-pred", "Direct MV prediction mode", OFFSET(direct_pred), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "direct-pred" },
548
{ "none", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_NONE }, 0, 0, VE, "direct-pred" },
549
{ "spatial", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_SPATIAL }, 0, 0, VE, "direct-pred" },
550
{ "temporal", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_TEMPORAL }, 0, 0, VE, "direct-pred" },
551
{ "auto", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_AUTO }, 0, 0, VE, "direct-pred" },
552
{ "slice-max-size","Constant quantization parameter rate control method",OFFSET(slice_max_size), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE },
556
static const AVClass class = {
557
.class_name = "libx264",
558
.item_name = av_default_item_name,
560
.version = LIBAVUTIL_VERSION_INT,
563
static const AVCodecDefault x264_defaults[] = {
573
{ "sc_threshold", "-1" },
576
{ "me_range", "-1" },
577
{ "me_method", "-1" },
579
{ "b_strategy", "-1" },
580
{ "keyint_min", "-1" },
583
{ "threads", AV_STRINGIFY(X264_THREADS_AUTO) },
333
587
AVCodec ff_libx264_encoder = {
334
588
.name = "libx264",
335
589
.type = AVMEDIA_TYPE_VIDEO,