23 #include "libavutil/opt.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/pixdesc.h"
30 #define X264_API_IMPORTS 1
87 static const int level_map[] = {
94 if (level < 0 || level > X264_LOG_DEBUG)
97 av_vlog(p, level_map[level], fmt, args);
102 x264_nal_t *nals,
int nnal)
111 for (i = 0; i < nnal; i++)
112 size += nals[i].i_payload;
131 for (i = 0; i < nnal; i++){
132 memcpy(p, nals[i].p_payload, nals[i].i_payload);
133 p += nals[i].i_payload;
164 x264_picture_t pic_out = {0};
166 x264_picture_init( &x4->
pic );
168 if (x264_bit_depth > 8)
169 x4->
pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
173 for (i = 0; i < x4->
pic.img.i_plane; i++) {
174 x4->
pic.img.plane[i] = frame->
data[i];
178 x4->
pic.i_pts = frame->
pts;
186 x264_encoder_reconfig(x4->
enc, &x4->
params);
192 x264_encoder_reconfig(x4->
enc, &x4->
params);
197 if (x264_encoder_encode(x4->
enc, &nal, &nnal, frame? &x4->
pic: NULL, &pic_out) < 0)
203 }
while (!ret && !frame && x264_encoder_delayed_frames(x4->
enc));
205 pkt->
pts = pic_out.i_pts;
206 pkt->
dts = pic_out.i_dts;
208 switch (pic_out.i_type) {
238 x264_encoder_close(x4->
enc);
243 #define OPT_STR(opt, param) \
246 if (param!=NULL && (ret = x264_param_parse(&x4->params, opt, param)) < 0) { \
247 if(ret == X264_PARAM_BAD_NAME) \
248 av_log(avctx, AV_LOG_ERROR, \
249 "bad option '%s': '%s'\n", opt, param); \
251 av_log(avctx, AV_LOG_ERROR, \
252 "bad value for '%s': '%s'\n", opt, param); \
285 #define PARSE_X264_OPT(name, var)\
286 if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
287 av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
288 return AVERROR(EINVAL);\
296 x264_param_default(&x4->
params);
307 for (i = 0; x264_preset_names[i]; i++)
311 for (i = 0; x264_tune_names[i]; i++)
317 if (avctx->
level > 0)
321 x4->
params.p_log_private = avctx;
322 x4->
params.i_log_level = X264_LOG_DEBUG;
329 x4->
params.rc.i_rc_method = X264_RC_ABR;
335 x4->
params.rc.b_stat_read = 1;
338 x4->
params.rc.i_rc_method = X264_RC_CRF;
340 }
else if (x4->
cqp >= 0) {
341 x4->
params.rc.i_rc_method = X264_RC_CQP;
351 x4->
params.rc.f_vbv_buffer_init =
360 char param[256]={0},
val[256]={0};
361 if(sscanf(p,
"%255[^:=]=%255[^:]", param,
val) == 1){
374 x4->
params.analyse.i_me_method = X264_ME_DIA;
376 x4->
params.analyse.i_me_method = X264_ME_HEX;
378 x4->
params.analyse.i_me_method = X264_ME_UMH;
380 x4->
params.analyse.i_me_method = X264_ME_ESA;
382 x4->
params.analyse.i_me_method = X264_ME_TESA;
390 if (avctx->
qmin >= 0)
392 if (avctx->
qmax >= 0)
396 if (avctx->
qblur >= 0)
400 if (avctx->
refs >= 0)
444 x4->
params.b_vfr_input = 0;
446 if (x4->
b_bias != INT_MIN)
477 x264_param_apply_fastfirstpass(&x4->
params);
508 if (x264_param_apply_profile(&x4->
params, x4->
profile) < 0) {
512 for (i = 0; x264_profile_names[i]; i++)
521 x4->
params.vui.i_sar_width = sw;
522 x4->
params.vui.i_sar_height = sh;
543 x4->
params.b_repeat_headers = 0;
553 "Error parsing option '%s = %s'.\n",
563 x4->
params.i_bframe_pyramid ? 2 : 1 : 0;
569 x4->
enc = x264_encoder_open(&x4->
params);
580 s = x264_encoder_headers(x4->
enc, &nal, &nnal);
583 for (i = 0; i < nnal; i++) {
585 if (nal[i].i_type ==
NAL_SEI) {
589 memcpy(x4->
sei, nal[i].p_payload, nal[i].i_payload);
592 memcpy(p, nal[i].p_payload, nal[i].i_payload);
593 p += nal[i].i_payload;
634 if (x264_bit_depth == 8)
636 else if (x264_bit_depth == 9)
638 else if (x264_bit_depth == 10)
642 #define OFFSET(x) offsetof(X264Context, x)
643 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
648 {
"fastfirstpass",
"Use fast settings when encoding first pass",
OFFSET(fastfirstpass),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
VE},
653 {
"crf",
"Select the quality for constant quality mode",
OFFSET(crf),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE },
654 {
"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 },
655 {
"qp",
"Constant quantization parameter rate control method",
OFFSET(cqp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE },
657 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
658 {
"variance",
"Variance AQ (complexity mask)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
659 {
"autovariance",
"Auto-variance AQ (experimental)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX,
VE,
"aq_mode" },
660 {
"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},
662 {
"psy-rd",
"Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.",
OFFSET(psy_rd),
AV_OPT_TYPE_STRING, {0 }, 0, 0,
VE},
663 {
"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 },
664 {
"weightb",
"Weighted prediction for B-frames.",
OFFSET(weightb),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
665 {
"weightp",
"Weighted prediction analysis method.",
OFFSET(weightp),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"weightp" },
666 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX,
VE,
"weightp" },
667 {
"simple", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX,
VE,
"weightp" },
668 {
"smart", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX,
VE,
"weightp" },
670 {
"intra-refresh",
"Use Periodic Intra Refresh instead of IDR frames.",
OFFSET(intra_refresh),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
671 {
"bluray-compat",
"Bluray compatibility workarounds.",
OFFSET(bluray_compat) ,
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
VE },
672 {
"b-bias",
"Influences how often B-frames are used",
OFFSET(b_bias),
AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX,
VE },
673 {
"b-pyramid",
"Keep some B-frames as references.",
OFFSET(b_pyramid),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"b_pyramid" },
674 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
675 {
"strict",
"Strictly hierarchical pyramid", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
676 {
"normal",
"Non-strict (not Blu-ray compatible)", 0,
AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX,
VE,
"b_pyramid" },
677 {
"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 },
683 {
"cplxblur",
"Reduce fluctuations in QP (before curve compression)",
OFFSET(cplxblur),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE},
684 {
"partitions",
"A comma-separated list of partitions to consider. "
685 "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all",
OFFSET(partitions),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
VE},
686 {
"direct-pred",
"Direct MV prediction mode",
OFFSET(direct_pred),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE,
"direct-pred" },
687 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0,
VE,
"direct-pred" },
688 {
"spatial", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0,
VE,
"direct-pred" },
689 {
"temporal", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0,
VE,
"direct-pred" },
690 {
"auto", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0,
VE,
"direct-pred" },
691 {
"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 },
693 {
"nal-hrd",
"Signal HRD information (requires vbv-bufsize; "
695 {
"none", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
696 {
"vbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
697 {
"cbr", NULL, 0,
AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX,
VE,
"nal-hrd" },
698 {
"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 },
721 {
"i_qfactor",
"-1" },
729 {
"sc_threshold",
"-1" },
732 {
"me_range",
"-1" },
733 {
"me_method",
"-1" },
735 {
"b_strategy",
"-1" },
736 {
"keyint_min",
"-1" },
740 {
"thread_type",
"0" },
741 {
"flags",
"+cgop" },
742 {
"rc_init_occupancy",
"-1" },
756 .priv_class = &x264_class,
762 .
name =
"libx264rgb",
771 .priv_class = &rgbclass,
static int convert_pix_fmt(enum AVPixelFormat pix_fmt)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
const char const char void * val
static int avfmt2_num_planes(int avfmt)
This structure describes decoded (raw) audio or video data.
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
AVCodec ff_libx264_encoder
float qblur
amount of qscale smoothing over time (0.0-1.0)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
char * av_strdup(const char *s) av_malloc_attrib
Duplicate the string s.
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
static av_cold int init(AVCodecContext *avctx)
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
#define FF_PROFILE_H264_BASELINE
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
packed RGB 8:8:8, 24bpp, RGBRGB...
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
static enum AVPixelFormat pix_fmts_8bit_rgb[]
enhanced predictive zonal search
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static void X264_log(void *p, int level, const char *fmt, va_list args)
static av_cold int X264_init(AVCodecContext *avctx)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int scenechange_threshold
scene change detection threshold 0 is default, larger means fewer detected scene changes.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
packed RGB 8:8:8, 24bpp, BGRBGR...
#define CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
static const AVClass rgbclass
const char * av_default_item_name(void *ctx)
Return the context name.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
const char * name
Name of the codec implementation.
int me_cmp
motion estimation comparison function
enum AVPixelFormat pix_fmt
const OptionDef options[]
#define FF_PROFILE_H264_HIGH
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int chromaoffset
chroma qp offset from luma
#define CODEC_FLAG_LOOP_FILTER
loop filter
static enum AVPixelFormat pix_fmts_10bit[]
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int has_b_frames
Size of the frame reordering buffer in the decoder.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
int qmax
maximum quantizer
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static const AVClass x264_class
void av_dict_free(AVDictionary **m)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
int rc_max_rate
maximum bitrate
float i_quant_factor
qscale factor between P and I-frames If > 0 then the last p frame quantizer will be used (q= lastp_q*...
transformed exhaustive search algorithm
Libavcodec external API header.
AVPixelFormat
Pixel format.
int flags
A combination of AV_PKT_FLAG values.
int rc_buffer_size
decoder bitstream buffer size
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
common internal API header
int refs
number of reference frames
static enum AVPixelFormat pix_fmts_9bit[]
#define PARSE_X264_OPT(name, var)
int bit_rate
the average bitrate
enum AVPictureType pict_type
Picture type of the frame.
#define OPT_STR(opt, param)
int width
picture width / height.
#define FF_PROFILE_H264_HIGH_10
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
#define CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
int max_qdiff
maximum quantizer difference between frames
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
static enum AVPixelFormat pix_fmts_8bit[]
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
static const AVCodecDefault x264_defaults[]
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add to a dictionary.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define AV_LOG_INFO
Standard information.
static av_cold int X264_close(AVCodecContext *avctx)
#define FF_PROFILE_H264_HIGH_422
main external API structure.
static void close(AVCodecParserContext *s)
int qmin
minimum quantizer
#define AV_PIX_FMT_YUV444P9
Describe the class of an AVClass context structure.
AVFrame * coded_frame
the picture in the bitstream
#define AV_PIX_FMT_YUV420P9
void void av_vlog(void *avcl, int level, const char *fmt, va_list vl)
Send the specified message to the log if the level is less than or equal to the current av_log_level...
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
#define CODEC_FLAG_CLOSED_GOP
interleaved chroma YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
int noise_reduction
noise reduction strength
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
static av_cold void X264_init_static(AVCodec *codec)
common internal api header.
#define FF_PROFILE_H264_HIGH_444
#define AV_PIX_FMT_YUV444P10
#define FF_PROFILE_H264_MAIN
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
int trellis
trellis RD quantization
int slices
Number of slices.
int top_field_first
If the content is interlaced, is top field displayed first.
uneven multi-hexagon search
AVCodec ff_libx264rgb_encoder
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define AV_PIX_FMT_YUV420P10
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
#define AV_PIX_FMT_YUV422P10
#define AV_DICT_IGNORE_SUFFIX
static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
static const AVCodecDefault defaults[]
int me_method
Motion estimation algorithm used for video coding.
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
This structure stores compressed data.
int me_subpel_quality
subpel ME quality
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int thread_type
Which multithreading methods to use.
int keyint_min
minimum GOP size
static int encode_nals(AVCodecContext *ctx, AVPacket *pkt, x264_nal_t *nals, int nnal)