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 PARSE_X264_OPT("stats", stats);
00466 if (x4->psy >= 0)
00467 x4->params.analyse.b_psy = x4->psy;
00468 if (x4->rc_lookahead >= 0)
00469 x4->params.rc.i_lookahead = x4->rc_lookahead;
00470 if (x4->weightp >= 0)
00471 x4->params.analyse.i_weighted_pred = x4->weightp;
00472 if (x4->weightb >= 0)
00473 x4->params.analyse.b_weighted_bipred = x4->weightb;
00474 if (x4->cplxblur >= 0)
00475 x4->params.rc.f_complexity_blur = x4->cplxblur;
00476
00477 if (x4->ssim >= 0)
00478 x4->params.analyse.b_ssim = x4->ssim;
00479 if (x4->intra_refresh >= 0)
00480 x4->params.b_intra_refresh = x4->intra_refresh;
00481 if (x4->b_bias != INT_MIN)
00482 x4->params.i_bframe_bias = x4->b_bias;
00483 if (x4->b_pyramid >= 0)
00484 x4->params.i_bframe_pyramid = x4->b_pyramid;
00485 if (x4->mixed_refs >= 0)
00486 x4->params.analyse.b_mixed_references = x4->mixed_refs;
00487 if (x4->dct8x8 >= 0)
00488 x4->params.analyse.b_transform_8x8 = x4->dct8x8;
00489 if (x4->fast_pskip >= 0)
00490 x4->params.analyse.b_fast_pskip = x4->fast_pskip;
00491 if (x4->aud >= 0)
00492 x4->params.b_aud = x4->aud;
00493 if (x4->mbtree >= 0)
00494 x4->params.rc.b_mb_tree = x4->mbtree;
00495 if (x4->direct_pred >= 0)
00496 x4->params.analyse.i_direct_mv_pred = x4->direct_pred;
00497
00498 if (x4->slice_max_size >= 0)
00499 x4->params.i_slice_max_size = x4->slice_max_size;
00500
00501 if (x4->fastfirstpass)
00502 x264_param_apply_fastfirstpass(&x4->params);
00503
00504 if (x4->profile)
00505 if (x264_param_apply_profile(&x4->params, x4->profile) < 0) {
00506 av_log(avctx, AV_LOG_ERROR, "Error setting profile %s.\n", x4->profile);
00507 return AVERROR(EINVAL);
00508 }
00509
00510 x4->params.i_width = avctx->width;
00511 x4->params.i_height = avctx->height;
00512 av_reduce(&sw, &sh, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 4096);
00513 x4->params.vui.i_sar_width = sw;
00514 x4->params.vui.i_sar_height = sh;
00515 x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
00516 x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
00517
00518 x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
00519
00520 x4->params.i_threads = avctx->thread_count;
00521
00522 x4->params.b_interlaced = avctx->flags & CODEC_FLAG_INTERLACED_DCT;
00523
00524
00525
00526 x4->params.i_slice_count = avctx->slices;
00527
00528 x4->params.vui.b_fullrange = avctx->pix_fmt == PIX_FMT_YUVJ420P;
00529
00530 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER)
00531 x4->params.b_repeat_headers = 0;
00532
00533
00534 avctx->has_b_frames = x4->params.i_bframe ?
00535 x4->params.i_bframe_pyramid ? 2 : 1 : 0;
00536 if (avctx->max_b_frames < 0)
00537 avctx->max_b_frames = 0;
00538
00539 avctx->bit_rate = x4->params.rc.i_bitrate*1000;
00540 #if FF_API_X264_GLOBAL_OPTS
00541 avctx->crf = x4->params.rc.f_rf_constant;
00542 #endif
00543
00544 x4->enc = x264_encoder_open(&x4->params);
00545 if (!x4->enc)
00546 return -1;
00547
00548 avctx->coded_frame = &x4->out_pic;
00549
00550 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
00551 x264_nal_t *nal;
00552 int nnal, s, i;
00553
00554 s = x264_encoder_headers(x4->enc, &nal, &nnal);
00555
00556 for (i = 0; i < nnal; i++)
00557 if (nal[i].i_type == NAL_SEI)
00558 av_log(avctx, AV_LOG_INFO, "%s\n", nal[i].p_payload+25);
00559
00560 avctx->extradata = av_malloc(s);
00561 avctx->extradata_size = encode_nals(avctx, avctx->extradata, s, nal, nnal, 1);
00562 }
00563
00564 return 0;
00565 }
00566
00567 static const enum PixelFormat pix_fmts_8bit[] = {
00568 PIX_FMT_YUV420P,
00569 PIX_FMT_YUVJ420P,
00570 PIX_FMT_YUV422P,
00571 PIX_FMT_YUV444P,
00572 PIX_FMT_NONE
00573 };
00574 static const enum PixelFormat pix_fmts_9bit[] = {
00575 PIX_FMT_YUV420P9,
00576 PIX_FMT_YUV444P9,
00577 PIX_FMT_NONE
00578 };
00579 static const enum PixelFormat pix_fmts_10bit[] = {
00580 PIX_FMT_YUV420P10,
00581 PIX_FMT_YUV422P10,
00582 PIX_FMT_YUV444P10,
00583 PIX_FMT_NONE
00584 };
00585 static const enum PixelFormat pix_fmts_8bit_rgb[] = {
00586 #ifdef X264_CSP_BGR
00587 PIX_FMT_BGR24,
00588 PIX_FMT_RGB24,
00589 #endif
00590 PIX_FMT_NONE
00591 };
00592
00593 static av_cold void X264_init_static(AVCodec *codec)
00594 {
00595 if (x264_bit_depth == 8)
00596 codec->pix_fmts = pix_fmts_8bit;
00597 else if (x264_bit_depth == 9)
00598 codec->pix_fmts = pix_fmts_9bit;
00599 else if (x264_bit_depth == 10)
00600 codec->pix_fmts = pix_fmts_10bit;
00601 }
00602
00603 #define OFFSET(x) offsetof(X264Context, x)
00604 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00605 static const AVOption options[] = {
00606 { "preset", "Set the encoding preset (cf. x264 --fullhelp)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},
00607 { "tune", "Tune the encoding params (cf. x264 --fullhelp)", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
00608 { "profile", "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
00609 { "fastfirstpass", "Use fast settings when encoding first pass", OFFSET(fastfirstpass), AV_OPT_TYPE_INT, { 1 }, 0, 1, VE},
00610 {"level", "Specify level (as defined by Annex A)", OFFSET(level), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
00611 {"passlogfile", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
00612 {"wpredp", "Weighted prediction for P-frames", OFFSET(wpredp), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
00613 {"x264opts", "x264 options", OFFSET(x264opts), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
00614 { "crf", "Select the quality for constant quality mode", OFFSET(crf), AV_OPT_TYPE_FLOAT, {-1 }, -1, FLT_MAX, VE },
00615 { "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 },
00616 { "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE },
00617 { "aq-mode", "AQ method", OFFSET(aq_mode), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "aq_mode"},
00618 { "none", NULL, 0, AV_OPT_TYPE_CONST, {X264_AQ_NONE}, INT_MIN, INT_MAX, VE, "aq_mode" },
00619 { "variance", "Variance AQ (complexity mask)", 0, AV_OPT_TYPE_CONST, {X264_AQ_VARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
00620 { "autovariance", "Auto-variance AQ (experimental)", 0, AV_OPT_TYPE_CONST, {X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
00621 { "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},
00622 { "psy", "Use psychovisual optimizations.", OFFSET(psy), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
00623 { "psy-rd", "Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.", OFFSET(psy_rd), AV_OPT_TYPE_STRING, {0 }, 0, 0, VE},
00624 { "rc-lookahead", "Number of frames to look ahead for frametype and ratecontrol", OFFSET(rc_lookahead), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE },
00625 { "weightb", "Weighted prediction for B-frames.", OFFSET(weightb), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
00626 { "weightp", "Weighted prediction analysis method.", OFFSET(weightp), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "weightp" },
00627 { "none", NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_NONE}, INT_MIN, INT_MAX, VE, "weightp" },
00628 { "simple", NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX, VE, "weightp" },
00629 { "smart", NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_SMART}, INT_MIN, INT_MAX, VE, "weightp" },
00630 { "ssim", "Calculate and print SSIM stats.", OFFSET(ssim), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
00631 { "intra-refresh", "Use Periodic Intra Refresh instead of IDR frames.",OFFSET(intra_refresh),AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
00632 { "b-bias", "Influences how often B-frames are used", OFFSET(b_bias), AV_OPT_TYPE_INT, {INT_MIN}, INT_MIN, INT_MAX, VE },
00633 { "b-pyramid", "Keep some B-frames as references.", OFFSET(b_pyramid), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "b_pyramid" },
00634 { "none", NULL, 0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX, VE, "b_pyramid" },
00635 { "strict", "Strictly hierarchical pyramid", 0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX, VE, "b_pyramid" },
00636 { "normal", "Non-strict (not Blu-ray compatible)", 0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX, VE, "b_pyramid" },
00637 { "mixed-refs", "One reference per partition, as opposed to one reference per macroblock", OFFSET(mixed_refs), AV_OPT_TYPE_INT, {-1}, -1, 1, VE },
00638 { "8x8dct", "High profile 8x8 transform.", OFFSET(dct8x8), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
00639 { "fast-pskip", NULL, OFFSET(fast_pskip), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
00640 { "aud", "Use access unit delimiters.", OFFSET(aud), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
00641 { "mbtree", "Use macroblock tree ratecontrol.", OFFSET(mbtree), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
00642 { "deblock", "Loop filter parameters, in <alpha:beta> form.", OFFSET(deblock), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
00643 { "cplxblur", "Reduce fluctuations in QP (before curve compression)", OFFSET(cplxblur), AV_OPT_TYPE_FLOAT, {-1 }, -1, FLT_MAX, VE},
00644 { "partitions", "A comma-separated list of partitions to consider. "
00645 "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all", OFFSET(partitions), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
00646 { "direct-pred", "Direct MV prediction mode", OFFSET(direct_pred), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "direct-pred" },
00647 { "none", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_NONE }, 0, 0, VE, "direct-pred" },
00648 { "spatial", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_SPATIAL }, 0, 0, VE, "direct-pred" },
00649 { "temporal", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_TEMPORAL }, 0, 0, VE, "direct-pred" },
00650 { "auto", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_AUTO }, 0, 0, VE, "direct-pred" },
00651 { "slice-max-size","Limit the size of each slice in bytes", OFFSET(slice_max_size),AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE },
00652 { "stats", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
00653 { NULL },
00654 };
00655
00656 static const AVClass class = {
00657 .class_name = "libx264",
00658 .item_name = av_default_item_name,
00659 .option = options,
00660 .version = LIBAVUTIL_VERSION_INT,
00661 };
00662
00663 static const AVClass rgbclass = {
00664 .class_name = "libx264rgb",
00665 .item_name = av_default_item_name,
00666 .option = options,
00667 .version = LIBAVUTIL_VERSION_INT,
00668 };
00669
00670 static const AVCodecDefault x264_defaults[] = {
00671 { "b", "0" },
00672 { "bf", "-1" },
00673 { "flags2", "0" },
00674 { "g", "-1" },
00675 { "qmin", "-1" },
00676 { "qmax", "-1" },
00677 { "qdiff", "-1" },
00678 { "qblur", "-1" },
00679 { "qcomp", "-1" },
00680 { "rc_lookahead", "-1" },
00681 { "refs", "-1" },
00682 { "sc_threshold", "-1" },
00683 { "trellis", "-1" },
00684 { "nr", "-1" },
00685 { "me_range", "-1" },
00686 { "me_method", "-1" },
00687 { "subq", "-1" },
00688 { "b_strategy", "-1" },
00689 { "keyint_min", "-1" },
00690 { "coder", "-1" },
00691 { "cmp", "-1" },
00692 { "threads", AV_STRINGIFY(X264_THREADS_AUTO) },
00693 { NULL },
00694 };
00695
00696 AVCodec ff_libx264_encoder = {
00697 .name = "libx264",
00698 .type = AVMEDIA_TYPE_VIDEO,
00699 .id = CODEC_ID_H264,
00700 .priv_data_size = sizeof(X264Context),
00701 .init = X264_init,
00702 .encode = X264_frame,
00703 .close = X264_close,
00704 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
00705 .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
00706 .priv_class = &class,
00707 .defaults = x264_defaults,
00708 .init_static_data = X264_init_static,
00709 };
00710
00711 AVCodec ff_libx264rgb_encoder = {
00712 .name = "libx264rgb",
00713 .type = AVMEDIA_TYPE_VIDEO,
00714 .id = CODEC_ID_H264,
00715 .priv_data_size = sizeof(X264Context),
00716 .init = X264_init,
00717 .encode = X264_frame,
00718 .close = X264_close,
00719 .capabilities = CODEC_CAP_DELAY,
00720 .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB"),
00721 .priv_class = &rgbclass,
00722 .defaults = x264_defaults,
00723 .pix_fmts = pix_fmts_8bit_rgb,
00724 };