00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/internal.h"
00023 #include "libavutil/opt.h"
00024 #include "libavutil/mem.h"
00025 #include "libavutil/pixdesc.h"
00026 #include "avcodec.h"
00027 #include "internal.h"
00028 #include <x264.h>
00029 #include <float.h>
00030 #include <math.h>
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034
00035 typedef struct X264Context {
00036 AVClass *class;
00037 x264_param_t params;
00038 x264_t *enc;
00039 x264_picture_t pic;
00040 uint8_t *sei;
00041 int sei_size;
00042 AVFrame out_pic;
00043 char *preset;
00044 char *tune;
00045 char *profile;
00046 char *level;
00047 int fastfirstpass;
00048 char *wpredp;
00049 char *x264opts;
00050 float crf;
00051 float crf_max;
00052 int cqp;
00053 int aq_mode;
00054 float aq_strength;
00055 char *psy_rd;
00056 int psy;
00057 int rc_lookahead;
00058 int weightp;
00059 int weightb;
00060 int ssim;
00061 int intra_refresh;
00062 int b_bias;
00063 int b_pyramid;
00064 int mixed_refs;
00065 int dct8x8;
00066 int fast_pskip;
00067 int aud;
00068 int mbtree;
00069 char *deblock;
00070 float cplxblur;
00071 char *partitions;
00072 int direct_pred;
00073 int slice_max_size;
00074 char *stats;
00075 int nal_hrd;
00076 } X264Context;
00077
00078 static void X264_log(void *p, int level, const char *fmt, va_list args)
00079 {
00080 static const int level_map[] = {
00081 [X264_LOG_ERROR] = AV_LOG_ERROR,
00082 [X264_LOG_WARNING] = AV_LOG_WARNING,
00083 [X264_LOG_INFO] = AV_LOG_INFO,
00084 [X264_LOG_DEBUG] = AV_LOG_DEBUG
00085 };
00086
00087 if (level < 0 || level > X264_LOG_DEBUG)
00088 return;
00089
00090 av_vlog(p, level_map[level], fmt, args);
00091 }
00092
00093
00094 static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
00095 x264_nal_t *nals, int nnal)
00096 {
00097 X264Context *x4 = ctx->priv_data;
00098 uint8_t *p;
00099 int i, size = x4->sei_size, ret;
00100
00101 if (!nnal)
00102 return 0;
00103
00104 for (i = 0; i < nnal; i++)
00105 size += nals[i].i_payload;
00106
00107 if ((ret = ff_alloc_packet2(ctx, pkt, size)) < 0)
00108 return ret;
00109
00110 p = pkt->data;
00111
00112
00113 if (x4->sei_size > 0 && nnal > 0) {
00114 if (x4->sei_size > size) {
00115 av_log(ctx, AV_LOG_ERROR, "Error: nal buffer is too small\n");
00116 return -1;
00117 }
00118 memcpy(p, x4->sei, x4->sei_size);
00119 p += x4->sei_size;
00120 x4->sei_size = 0;
00121 av_freep(&x4->sei);
00122 }
00123
00124 for (i = 0; i < nnal; i++){
00125 memcpy(p, nals[i].p_payload, nals[i].i_payload);
00126 p += nals[i].i_payload;
00127 }
00128
00129 return 1;
00130 }
00131
00132 static int avfmt2_num_planes(int avfmt)
00133 {
00134 switch (avfmt) {
00135 case PIX_FMT_YUV420P:
00136 case PIX_FMT_YUVJ420P:
00137 case PIX_FMT_YUV420P9:
00138 case PIX_FMT_YUV420P10:
00139 case PIX_FMT_YUV444P:
00140 return 3;
00141
00142 case PIX_FMT_BGR24:
00143 case PIX_FMT_RGB24:
00144 return 1;
00145
00146 default:
00147 return 3;
00148 }
00149 }
00150
00151 static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
00152 int *got_packet)
00153 {
00154 X264Context *x4 = ctx->priv_data;
00155 x264_nal_t *nal;
00156 int nnal, i, ret;
00157 x264_picture_t pic_out;
00158
00159 x264_picture_init( &x4->pic );
00160 x4->pic.img.i_csp = x4->params.i_csp;
00161 if (x264_bit_depth > 8)
00162 x4->pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
00163 x4->pic.img.i_plane = avfmt2_num_planes(ctx->pix_fmt);
00164
00165 if (frame) {
00166 for (i = 0; i < x4->pic.img.i_plane; i++) {
00167 x4->pic.img.plane[i] = frame->data[i];
00168 x4->pic.img.i_stride[i] = frame->linesize[i];
00169 }
00170
00171 x4->pic.i_pts = frame->pts;
00172 x4->pic.i_type =
00173 frame->pict_type == AV_PICTURE_TYPE_I ? X264_TYPE_KEYFRAME :
00174 frame->pict_type == AV_PICTURE_TYPE_P ? X264_TYPE_P :
00175 frame->pict_type == AV_PICTURE_TYPE_B ? X264_TYPE_B :
00176 X264_TYPE_AUTO;
00177 if (x4->params.b_tff != frame->top_field_first) {
00178 x4->params.b_tff = frame->top_field_first;
00179 x264_encoder_reconfig(x4->enc, &x4->params);
00180 }
00181 if (x4->params.vui.i_sar_height != ctx->sample_aspect_ratio.den ||
00182 x4->params.vui.i_sar_width != ctx->sample_aspect_ratio.num) {
00183 x4->params.vui.i_sar_height = ctx->sample_aspect_ratio.den;
00184 x4->params.vui.i_sar_width = ctx->sample_aspect_ratio.num;
00185 x264_encoder_reconfig(x4->enc, &x4->params);
00186 }
00187 }
00188
00189 do {
00190 if (x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, &pic_out) < 0)
00191 return -1;
00192
00193 ret = encode_nals(ctx, pkt, nal, nnal);
00194 if (ret < 0)
00195 return -1;
00196 } while (!ret && !frame && x264_encoder_delayed_frames(x4->enc));
00197
00198 pkt->pts = pic_out.i_pts;
00199 pkt->dts = pic_out.i_dts;
00200
00201 switch (pic_out.i_type) {
00202 case X264_TYPE_IDR:
00203 case X264_TYPE_I:
00204 x4->out_pic.pict_type = AV_PICTURE_TYPE_I;
00205 break;
00206 case X264_TYPE_P:
00207 x4->out_pic.pict_type = AV_PICTURE_TYPE_P;
00208 break;
00209 case X264_TYPE_B:
00210 case X264_TYPE_BREF:
00211 x4->out_pic.pict_type = AV_PICTURE_TYPE_B;
00212 break;
00213 }
00214
00215 pkt->flags |= AV_PKT_FLAG_KEY*pic_out.b_keyframe;
00216 if (ret)
00217 x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
00218
00219 *got_packet = ret;
00220 return 0;
00221 }
00222
00223 static av_cold int X264_close(AVCodecContext *avctx)
00224 {
00225 X264Context *x4 = avctx->priv_data;
00226
00227 av_freep(&avctx->extradata);
00228 av_free(x4->sei);
00229
00230 if (x4->enc)
00231 x264_encoder_close(x4->enc);
00232
00233 return 0;
00234 }
00235
00236 #define OPT_STR(opt, param) \
00237 do { \
00238 int ret; \
00239 if (param && (ret = x264_param_parse(&x4->params, opt, param)) < 0) { \
00240 if(ret == X264_PARAM_BAD_NAME) \
00241 av_log(avctx, AV_LOG_ERROR, \
00242 "bad option '%s': '%s'\n", opt, param); \
00243 else \
00244 av_log(avctx, AV_LOG_ERROR, \
00245 "bad value for '%s': '%s'\n", opt, param); \
00246 return -1; \
00247 } \
00248 } while (0)
00249
00250 static int convert_pix_fmt(enum PixelFormat pix_fmt)
00251 {
00252 switch (pix_fmt) {
00253 case PIX_FMT_YUV420P:
00254 case PIX_FMT_YUVJ420P:
00255 case PIX_FMT_YUV420P9:
00256 case PIX_FMT_YUV420P10: return X264_CSP_I420;
00257 case PIX_FMT_YUV422P:
00258 case PIX_FMT_YUV422P10: return X264_CSP_I422;
00259 case PIX_FMT_YUV444P:
00260 case PIX_FMT_YUV444P9:
00261 case PIX_FMT_YUV444P10: return X264_CSP_I444;
00262 #ifdef X264_CSP_BGR
00263 case PIX_FMT_BGR24:
00264 return X264_CSP_BGR;
00265
00266 case PIX_FMT_RGB24:
00267 return X264_CSP_RGB;
00268 #endif
00269 };
00270 return 0;
00271 }
00272
00273 #define PARSE_X264_OPT(name, var)\
00274 if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
00275 av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
00276 return AVERROR(EINVAL);\
00277 }
00278
00279 static av_cold int X264_init(AVCodecContext *avctx)
00280 {
00281 X264Context *x4 = avctx->priv_data;
00282 int sw,sh;
00283
00284 x264_param_default(&x4->params);
00285
00286 x4->params.b_deblocking_filter = avctx->flags & CODEC_FLAG_LOOP_FILTER;
00287
00288 x4->params.rc.f_ip_factor = 1 / fabs(avctx->i_quant_factor);
00289 x4->params.rc.f_pb_factor = avctx->b_quant_factor;
00290 x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
00291 if (x4->preset || x4->tune)
00292 if (x264_param_default_preset(&x4->params, x4->preset, x4->tune) < 0) {
00293 int i;
00294 av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", x4->preset, x4->tune);
00295 av_log(avctx, AV_LOG_INFO, "Possible presets:");
00296 for (i = 0; x264_preset_names[i]; i++)
00297 av_log(avctx, AV_LOG_INFO, " %s", x264_preset_names[i]);
00298 av_log(avctx, AV_LOG_INFO, "\n");
00299 av_log(avctx, AV_LOG_INFO, "Possible tunes:");
00300 for (i = 0; x264_tune_names[i]; i++)
00301 av_log(avctx, AV_LOG_INFO, " %s", x264_tune_names[i]);
00302 av_log(avctx, AV_LOG_INFO, "\n");
00303 return AVERROR(EINVAL);
00304 }
00305
00306 if (avctx->level > 0)
00307 x4->params.i_level_idc = avctx->level;
00308
00309 x4->params.pf_log = X264_log;
00310 x4->params.p_log_private = avctx;
00311 x4->params.i_log_level = X264_LOG_DEBUG;
00312 x4->params.i_csp = convert_pix_fmt(avctx->pix_fmt);
00313
00314 OPT_STR("weightp", x4->wpredp);
00315
00316 if (avctx->bit_rate) {
00317 x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
00318 x4->params.rc.i_rc_method = X264_RC_ABR;
00319 }
00320 x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
00321 x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000;
00322 x4->params.rc.b_stat_write = avctx->flags & CODEC_FLAG_PASS1;
00323 if (avctx->flags & CODEC_FLAG_PASS2) {
00324 x4->params.rc.b_stat_read = 1;
00325 } else {
00326 if (x4->crf >= 0) {
00327 x4->params.rc.i_rc_method = X264_RC_CRF;
00328 x4->params.rc.f_rf_constant = x4->crf;
00329 } else if (x4->cqp >= 0) {
00330 x4->params.rc.i_rc_method = X264_RC_CQP;
00331 x4->params.rc.i_qp_constant = x4->cqp;
00332 }
00333
00334 if (x4->crf_max >= 0)
00335 x4->params.rc.f_rf_constant_max = x4->crf_max;
00336 }
00337
00338 if (avctx->rc_buffer_size && avctx->rc_initial_buffer_occupancy &&
00339 (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
00340 x4->params.rc.f_vbv_buffer_init =
00341 (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
00342 }
00343
00344 OPT_STR("level", x4->level);
00345
00346 if(x4->x264opts){
00347 const char *p= x4->x264opts;
00348 while(p){
00349 char param[256]={0}, val[256]={0};
00350 if(sscanf(p, "%255[^:=]=%255[^:]", param, val) == 1){
00351 OPT_STR(param, "1");
00352 }else
00353 OPT_STR(param, val);
00354 p= strchr(p, ':');
00355 p+=!!p;
00356 }
00357 }
00358
00359 if (avctx->me_method == ME_EPZS)
00360 x4->params.analyse.i_me_method = X264_ME_DIA;
00361 else if (avctx->me_method == ME_HEX)
00362 x4->params.analyse.i_me_method = X264_ME_HEX;
00363 else if (avctx->me_method == ME_UMH)
00364 x4->params.analyse.i_me_method = X264_ME_UMH;
00365 else if (avctx->me_method == ME_FULL)
00366 x4->params.analyse.i_me_method = X264_ME_ESA;
00367 else if (avctx->me_method == ME_TESA)
00368 x4->params.analyse.i_me_method = X264_ME_TESA;
00369
00370 if (avctx->gop_size >= 0)
00371 x4->params.i_keyint_max = avctx->gop_size;
00372 if (avctx->max_b_frames >= 0)
00373 x4->params.i_bframe = avctx->max_b_frames;
00374 if (avctx->scenechange_threshold >= 0)
00375 x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
00376 if (avctx->qmin >= 0)
00377 x4->params.rc.i_qp_min = avctx->qmin;
00378 if (avctx->qmax >= 0)
00379 x4->params.rc.i_qp_max = avctx->qmax;
00380 if (avctx->max_qdiff >= 0)
00381 x4->params.rc.i_qp_step = avctx->max_qdiff;
00382 if (avctx->qblur >= 0)
00383 x4->params.rc.f_qblur = avctx->qblur;
00384 if (avctx->qcompress >= 0)
00385 x4->params.rc.f_qcompress = avctx->qcompress;
00386 if (avctx->refs >= 0)
00387 x4->params.i_frame_reference = avctx->refs;
00388 if (avctx->trellis >= 0)
00389 x4->params.analyse.i_trellis = avctx->trellis;
00390 if (avctx->me_range >= 0)
00391 x4->params.analyse.i_me_range = avctx->me_range;
00392 if (avctx->noise_reduction >= 0)
00393 x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
00394 if (avctx->me_subpel_quality >= 0)
00395 x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
00396 if (avctx->b_frame_strategy >= 0)
00397 x4->params.i_bframe_adaptive = avctx->b_frame_strategy;
00398 if (avctx->keyint_min >= 0)
00399 x4->params.i_keyint_min = avctx->keyint_min;
00400 if (avctx->coder_type >= 0)
00401 x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
00402 if (avctx->me_cmp >= 0)
00403 x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA;
00404
00405 if (x4->aq_mode >= 0)
00406 x4->params.rc.i_aq_mode = x4->aq_mode;
00407 if (x4->aq_strength >= 0)
00408 x4->params.rc.f_aq_strength = x4->aq_strength;
00409 PARSE_X264_OPT("psy-rd", psy_rd);
00410 PARSE_X264_OPT("deblock", deblock);
00411 PARSE_X264_OPT("partitions", partitions);
00412 PARSE_X264_OPT("stats", stats);
00413 if (x4->psy >= 0)
00414 x4->params.analyse.b_psy = x4->psy;
00415 if (x4->rc_lookahead >= 0)
00416 x4->params.rc.i_lookahead = x4->rc_lookahead;
00417 if (x4->weightp >= 0)
00418 x4->params.analyse.i_weighted_pred = x4->weightp;
00419 if (x4->weightb >= 0)
00420 x4->params.analyse.b_weighted_bipred = x4->weightb;
00421 if (x4->cplxblur >= 0)
00422 x4->params.rc.f_complexity_blur = x4->cplxblur;
00423
00424 if (x4->ssim >= 0)
00425 x4->params.analyse.b_ssim = x4->ssim;
00426 if (x4->intra_refresh >= 0)
00427 x4->params.b_intra_refresh = x4->intra_refresh;
00428 if (x4->b_bias != INT_MIN)
00429 x4->params.i_bframe_bias = x4->b_bias;
00430 if (x4->b_pyramid >= 0)
00431 x4->params.i_bframe_pyramid = x4->b_pyramid;
00432 if (x4->mixed_refs >= 0)
00433 x4->params.analyse.b_mixed_references = x4->mixed_refs;
00434 if (x4->dct8x8 >= 0)
00435 x4->params.analyse.b_transform_8x8 = x4->dct8x8;
00436 if (x4->fast_pskip >= 0)
00437 x4->params.analyse.b_fast_pskip = x4->fast_pskip;
00438 if (x4->aud >= 0)
00439 x4->params.b_aud = x4->aud;
00440 if (x4->mbtree >= 0)
00441 x4->params.rc.b_mb_tree = x4->mbtree;
00442 if (x4->direct_pred >= 0)
00443 x4->params.analyse.i_direct_mv_pred = x4->direct_pred;
00444
00445 if (x4->slice_max_size >= 0)
00446 x4->params.i_slice_max_size = x4->slice_max_size;
00447 else {
00448
00449
00450
00451
00452
00453
00454 if (avctx->rtp_payload_size)
00455 x4->params.i_slice_max_size = avctx->rtp_payload_size;
00456 }
00457
00458 if (x4->fastfirstpass)
00459 x264_param_apply_fastfirstpass(&x4->params);
00460
00461
00462 if (!x4->profile)
00463 switch (avctx->profile) {
00464 case FF_PROFILE_H264_BASELINE:
00465 x4->profile = av_strdup("baseline");
00466 break;
00467 case FF_PROFILE_H264_HIGH:
00468 x4->profile = av_strdup("high");
00469 break;
00470 case FF_PROFILE_H264_HIGH_10:
00471 x4->profile = av_strdup("high10");
00472 break;
00473 case FF_PROFILE_H264_HIGH_422:
00474 x4->profile = av_strdup("high422");
00475 break;
00476 case FF_PROFILE_H264_HIGH_444:
00477 x4->profile = av_strdup("high444");
00478 break;
00479 case FF_PROFILE_H264_MAIN:
00480 x4->profile = av_strdup("main");
00481 break;
00482 default:
00483 break;
00484 }
00485
00486 if (x4->nal_hrd >= 0)
00487 x4->params.i_nal_hrd = x4->nal_hrd;
00488
00489 if (x4->profile)
00490 if (x264_param_apply_profile(&x4->params, x4->profile) < 0) {
00491 int i;
00492 av_log(avctx, AV_LOG_ERROR, "Error setting profile %s.\n", x4->profile);
00493 av_log(avctx, AV_LOG_INFO, "Possible profiles:");
00494 for (i = 0; x264_profile_names[i]; i++)
00495 av_log(avctx, AV_LOG_INFO, " %s", x264_profile_names[i]);
00496 av_log(avctx, AV_LOG_INFO, "\n");
00497 return AVERROR(EINVAL);
00498 }
00499
00500 x4->params.i_width = avctx->width;
00501 x4->params.i_height = avctx->height;
00502 av_reduce(&sw, &sh, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 4096);
00503 x4->params.vui.i_sar_width = sw;
00504 x4->params.vui.i_sar_height = sh;
00505 x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
00506 x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
00507
00508 x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
00509
00510 x4->params.i_threads = avctx->thread_count;
00511 if (avctx->thread_type)
00512 x4->params.b_sliced_threads = avctx->thread_type == FF_THREAD_SLICE;
00513
00514 x4->params.b_interlaced = avctx->flags & CODEC_FLAG_INTERLACED_DCT;
00515
00516 x4->params.b_open_gop = !(avctx->flags & CODEC_FLAG_CLOSED_GOP);
00517
00518 x4->params.i_slice_count = avctx->slices;
00519
00520 x4->params.vui.b_fullrange = avctx->pix_fmt == PIX_FMT_YUVJ420P;
00521
00522 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER)
00523 x4->params.b_repeat_headers = 0;
00524
00525
00526 avctx->has_b_frames = x4->params.i_bframe ?
00527 x4->params.i_bframe_pyramid ? 2 : 1 : 0;
00528 if (avctx->max_b_frames < 0)
00529 avctx->max_b_frames = 0;
00530
00531 avctx->bit_rate = x4->params.rc.i_bitrate*1000;
00532
00533 x4->enc = x264_encoder_open(&x4->params);
00534 if (!x4->enc)
00535 return -1;
00536
00537 avctx->coded_frame = &x4->out_pic;
00538
00539 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
00540 x264_nal_t *nal;
00541 uint8_t *p;
00542 int nnal, s, i;
00543
00544 s = x264_encoder_headers(x4->enc, &nal, &nnal);
00545 avctx->extradata = p = av_malloc(s);
00546
00547 for (i = 0; i < nnal; i++) {
00548
00549 if (nal[i].i_type == NAL_SEI) {
00550 av_log(avctx, AV_LOG_INFO, "%s\n", nal[i].p_payload+25);
00551 x4->sei_size = nal[i].i_payload;
00552 x4->sei = av_malloc(x4->sei_size);
00553 memcpy(x4->sei, nal[i].p_payload, nal[i].i_payload);
00554 continue;
00555 }
00556 memcpy(p, nal[i].p_payload, nal[i].i_payload);
00557 p += nal[i].i_payload;
00558 }
00559 avctx->extradata_size = p - avctx->extradata;
00560 }
00561
00562 return 0;
00563 }
00564
00565 static const enum PixelFormat pix_fmts_8bit[] = {
00566 PIX_FMT_YUV420P,
00567 PIX_FMT_YUVJ420P,
00568 PIX_FMT_YUV422P,
00569 PIX_FMT_YUV444P,
00570 PIX_FMT_NONE
00571 };
00572 static const enum PixelFormat pix_fmts_9bit[] = {
00573 PIX_FMT_YUV420P9,
00574 PIX_FMT_YUV444P9,
00575 PIX_FMT_NONE
00576 };
00577 static const enum PixelFormat pix_fmts_10bit[] = {
00578 PIX_FMT_YUV420P10,
00579 PIX_FMT_YUV422P10,
00580 PIX_FMT_YUV444P10,
00581 PIX_FMT_NONE
00582 };
00583 static const enum PixelFormat pix_fmts_8bit_rgb[] = {
00584 #ifdef X264_CSP_BGR
00585 PIX_FMT_BGR24,
00586 PIX_FMT_RGB24,
00587 #endif
00588 PIX_FMT_NONE
00589 };
00590
00591 static av_cold void X264_init_static(AVCodec *codec)
00592 {
00593 if (x264_bit_depth == 8)
00594 codec->pix_fmts = pix_fmts_8bit;
00595 else if (x264_bit_depth == 9)
00596 codec->pix_fmts = pix_fmts_9bit;
00597 else if (x264_bit_depth == 10)
00598 codec->pix_fmts = pix_fmts_10bit;
00599 }
00600
00601 #define OFFSET(x) offsetof(X264Context, x)
00602 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00603 static const AVOption options[] = {
00604 { "preset", "Set the encoding preset (cf. x264 --fullhelp)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},
00605 { "tune", "Tune the encoding params (cf. x264 --fullhelp)", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
00606 { "profile", "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
00607 { "fastfirstpass", "Use fast settings when encoding first pass", OFFSET(fastfirstpass), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE},
00608 {"level", "Specify level (as defined by Annex A)", OFFSET(level), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
00609 {"passlogfile", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
00610 {"wpredp", "Weighted prediction for P-frames", OFFSET(wpredp), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
00611 {"x264opts", "x264 options", OFFSET(x264opts), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
00612 { "crf", "Select the quality for constant quality mode", OFFSET(crf), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE },
00613 { "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 },
00614 { "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },
00615 { "aq-mode", "AQ method", OFFSET(aq_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "aq_mode"},
00616 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX, VE, "aq_mode" },
00617 { "variance", "Variance AQ (complexity mask)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
00618 { "autovariance", "Auto-variance AQ (experimental)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
00619 { "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},
00620 { "psy", "Use psychovisual optimizations.", OFFSET(psy), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
00621 { "psy-rd", "Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.", OFFSET(psy_rd), AV_OPT_TYPE_STRING, {0 }, 0, 0, VE},
00622 { "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 },
00623 { "weightb", "Weighted prediction for B-frames.", OFFSET(weightb), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
00624 { "weightp", "Weighted prediction analysis method.", OFFSET(weightp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "weightp" },
00625 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX, VE, "weightp" },
00626 { "simple", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX, VE, "weightp" },
00627 { "smart", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX, VE, "weightp" },
00628 { "ssim", "Calculate and print SSIM stats.", OFFSET(ssim), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
00629 { "intra-refresh", "Use Periodic Intra Refresh instead of IDR frames.",OFFSET(intra_refresh),AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
00630 { "b-bias", "Influences how often B-frames are used", OFFSET(b_bias), AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX, VE },
00631 { "b-pyramid", "Keep some B-frames as references.", OFFSET(b_pyramid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "b_pyramid" },
00632 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX, VE, "b_pyramid" },
00633 { "strict", "Strictly hierarchical pyramid", 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX, VE, "b_pyramid" },
00634 { "normal", "Non-strict (not Blu-ray compatible)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX, VE, "b_pyramid" },
00635 { "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 },
00636 { "8x8dct", "High profile 8x8 transform.", OFFSET(dct8x8), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
00637 { "fast-pskip", NULL, OFFSET(fast_pskip), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
00638 { "aud", "Use access unit delimiters.", OFFSET(aud), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
00639 { "mbtree", "Use macroblock tree ratecontrol.", OFFSET(mbtree), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
00640 { "deblock", "Loop filter parameters, in <alpha:beta> form.", OFFSET(deblock), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
00641 { "cplxblur", "Reduce fluctuations in QP (before curve compression)", OFFSET(cplxblur), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE},
00642 { "partitions", "A comma-separated list of partitions to consider. "
00643 "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all", OFFSET(partitions), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
00644 { "direct-pred", "Direct MV prediction mode", OFFSET(direct_pred), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "direct-pred" },
00645 { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0, VE, "direct-pred" },
00646 { "spatial", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0, VE, "direct-pred" },
00647 { "temporal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0, VE, "direct-pred" },
00648 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0, VE, "direct-pred" },
00649 { "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 },
00650 { "stats", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
00651 { "nal-hrd", "Signal HRD information (requires vbv-bufsize; "
00652 "cbr not allowed in .mp4)", OFFSET(nal_hrd), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "nal-hrd" },
00653 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX, VE, "nal-hrd" },
00654 { "vbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX, VE, "nal-hrd" },
00655 { "cbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX, VE, "nal-hrd" },
00656 { NULL },
00657 };
00658
00659 static const AVClass class = {
00660 .class_name = "libx264",
00661 .item_name = av_default_item_name,
00662 .option = options,
00663 .version = LIBAVUTIL_VERSION_INT,
00664 };
00665
00666 static const AVClass rgbclass = {
00667 .class_name = "libx264rgb",
00668 .item_name = av_default_item_name,
00669 .option = options,
00670 .version = LIBAVUTIL_VERSION_INT,
00671 };
00672
00673 static const AVCodecDefault x264_defaults[] = {
00674 { "b", "0" },
00675 { "bf", "-1" },
00676 { "flags2", "0" },
00677 { "g", "-1" },
00678 { "qmin", "-1" },
00679 { "qmax", "-1" },
00680 { "qdiff", "-1" },
00681 { "qblur", "-1" },
00682 { "qcomp", "-1" },
00683
00684 { "refs", "-1" },
00685 { "sc_threshold", "-1" },
00686 { "trellis", "-1" },
00687 { "nr", "-1" },
00688 { "me_range", "-1" },
00689 { "me_method", "-1" },
00690 { "subq", "-1" },
00691 { "b_strategy", "-1" },
00692 { "keyint_min", "-1" },
00693 { "coder", "-1" },
00694 { "cmp", "-1" },
00695 { "threads", AV_STRINGIFY(X264_THREADS_AUTO) },
00696 { "thread_type", "0" },
00697 { "flags", "+cgop" },
00698 { NULL },
00699 };
00700
00701 AVCodec ff_libx264_encoder = {
00702 .name = "libx264",
00703 .type = AVMEDIA_TYPE_VIDEO,
00704 .id = AV_CODEC_ID_H264,
00705 .priv_data_size = sizeof(X264Context),
00706 .init = X264_init,
00707 .encode2 = X264_frame,
00708 .close = X264_close,
00709 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
00710 .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
00711 .priv_class = &class,
00712 .defaults = x264_defaults,
00713 .init_static_data = X264_init_static,
00714 };
00715
00716 AVCodec ff_libx264rgb_encoder = {
00717 .name = "libx264rgb",
00718 .type = AVMEDIA_TYPE_VIDEO,
00719 .id = AV_CODEC_ID_H264,
00720 .priv_data_size = sizeof(X264Context),
00721 .init = X264_init,
00722 .encode2 = X264_frame,
00723 .close = X264_close,
00724 .capabilities = CODEC_CAP_DELAY,
00725 .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB"),
00726 .priv_class = &rgbclass,
00727 .defaults = x264_defaults,
00728 .pix_fmts = pix_fmts_8bit_rgb,
00729 };