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