You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

568 lines
22KB

  1. /*
  2. * H.264 encoding using the x264 library
  3. * Copyright (C) 2005 Mans Rullgard <mans@mansr.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avcodec.h"
  24. #include "internal.h"
  25. #include <x264.h>
  26. #include <float.h>
  27. #include <math.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. typedef struct X264Context {
  32. AVClass *class;
  33. x264_param_t params;
  34. x264_t *enc;
  35. x264_picture_t pic;
  36. uint8_t *sei;
  37. int sei_size;
  38. AVFrame out_pic;
  39. char *preset;
  40. char *tune;
  41. char *profile;
  42. int fastfirstpass;
  43. float crf;
  44. float crf_max;
  45. int cqp;
  46. int aq_mode;
  47. float aq_strength;
  48. char *psy_rd;
  49. int psy;
  50. int rc_lookahead;
  51. int weightp;
  52. int weightb;
  53. int ssim;
  54. int intra_refresh;
  55. int b_bias;
  56. int b_pyramid;
  57. int mixed_refs;
  58. int dct8x8;
  59. int fast_pskip;
  60. int aud;
  61. int mbtree;
  62. char *deblock;
  63. float cplxblur;
  64. char *partitions;
  65. int direct_pred;
  66. int slice_max_size;
  67. char *stats;
  68. } X264Context;
  69. static void X264_log(void *p, int level, const char *fmt, va_list args)
  70. {
  71. static const int level_map[] = {
  72. [X264_LOG_ERROR] = AV_LOG_ERROR,
  73. [X264_LOG_WARNING] = AV_LOG_WARNING,
  74. [X264_LOG_INFO] = AV_LOG_INFO,
  75. [X264_LOG_DEBUG] = AV_LOG_DEBUG
  76. };
  77. if (level < 0 || level > X264_LOG_DEBUG)
  78. return;
  79. av_vlog(p, level_map[level], fmt, args);
  80. }
  81. static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
  82. x264_nal_t *nals, int nnal)
  83. {
  84. X264Context *x4 = ctx->priv_data;
  85. uint8_t *p;
  86. int i, size = x4->sei_size, ret;
  87. if (!nnal)
  88. return 0;
  89. for (i = 0; i < nnal; i++)
  90. size += nals[i].i_payload;
  91. if ((ret = ff_alloc_packet(pkt, size)) < 0)
  92. return ret;
  93. p = pkt->data;
  94. /* Write the SEI as part of the first frame. */
  95. if (x4->sei_size > 0 && nnal > 0) {
  96. memcpy(p, x4->sei, x4->sei_size);
  97. p += x4->sei_size;
  98. x4->sei_size = 0;
  99. }
  100. for (i = 0; i < nnal; i++){
  101. memcpy(p, nals[i].p_payload, nals[i].i_payload);
  102. p += nals[i].i_payload;
  103. }
  104. return 1;
  105. }
  106. static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
  107. int *got_packet)
  108. {
  109. X264Context *x4 = ctx->priv_data;
  110. x264_nal_t *nal;
  111. int nnal, i, ret;
  112. x264_picture_t pic_out;
  113. x264_picture_init( &x4->pic );
  114. x4->pic.img.i_csp = x4->params.i_csp;
  115. if (x264_bit_depth > 8)
  116. x4->pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
  117. x4->pic.img.i_plane = 3;
  118. if (frame) {
  119. for (i = 0; i < 3; i++) {
  120. x4->pic.img.plane[i] = frame->data[i];
  121. x4->pic.img.i_stride[i] = frame->linesize[i];
  122. }
  123. x4->pic.i_pts = frame->pts;
  124. x4->pic.i_type =
  125. frame->pict_type == AV_PICTURE_TYPE_I ? X264_TYPE_KEYFRAME :
  126. frame->pict_type == AV_PICTURE_TYPE_P ? X264_TYPE_P :
  127. frame->pict_type == AV_PICTURE_TYPE_B ? X264_TYPE_B :
  128. X264_TYPE_AUTO;
  129. if (x4->params.b_tff != frame->top_field_first) {
  130. x4->params.b_tff = frame->top_field_first;
  131. x264_encoder_reconfig(x4->enc, &x4->params);
  132. }
  133. if (x4->params.vui.i_sar_height != ctx->sample_aspect_ratio.den ||
  134. x4->params.vui.i_sar_width != ctx->sample_aspect_ratio.num) {
  135. x4->params.vui.i_sar_height = ctx->sample_aspect_ratio.den;
  136. x4->params.vui.i_sar_width = ctx->sample_aspect_ratio.num;
  137. x264_encoder_reconfig(x4->enc, &x4->params);
  138. }
  139. }
  140. do {
  141. if (x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, &pic_out) < 0)
  142. return -1;
  143. ret = encode_nals(ctx, pkt, nal, nnal);
  144. if (ret < 0)
  145. return -1;
  146. } while (!ret && !frame && x264_encoder_delayed_frames(x4->enc));
  147. pkt->pts = pic_out.i_pts;
  148. pkt->dts = pic_out.i_dts;
  149. switch (pic_out.i_type) {
  150. case X264_TYPE_IDR:
  151. case X264_TYPE_I:
  152. x4->out_pic.pict_type = AV_PICTURE_TYPE_I;
  153. break;
  154. case X264_TYPE_P:
  155. x4->out_pic.pict_type = AV_PICTURE_TYPE_P;
  156. break;
  157. case X264_TYPE_B:
  158. case X264_TYPE_BREF:
  159. x4->out_pic.pict_type = AV_PICTURE_TYPE_B;
  160. break;
  161. }
  162. pkt->flags |= AV_PKT_FLAG_KEY*pic_out.b_keyframe;
  163. if (ret)
  164. x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
  165. *got_packet = ret;
  166. return 0;
  167. }
  168. static av_cold int X264_close(AVCodecContext *avctx)
  169. {
  170. X264Context *x4 = avctx->priv_data;
  171. av_freep(&avctx->extradata);
  172. av_free(x4->sei);
  173. if (x4->enc)
  174. x264_encoder_close(x4->enc);
  175. return 0;
  176. }
  177. static int convert_pix_fmt(enum PixelFormat pix_fmt)
  178. {
  179. switch (pix_fmt) {
  180. case PIX_FMT_YUV420P:
  181. case PIX_FMT_YUVJ420P:
  182. case PIX_FMT_YUV420P9:
  183. case PIX_FMT_YUV420P10: return X264_CSP_I420;
  184. case PIX_FMT_YUV422P:
  185. case PIX_FMT_YUV422P10: return X264_CSP_I422;
  186. case PIX_FMT_YUV444P:
  187. case PIX_FMT_YUV444P9:
  188. case PIX_FMT_YUV444P10: return X264_CSP_I444;
  189. };
  190. return 0;
  191. }
  192. #define PARSE_X264_OPT(name, var)\
  193. if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
  194. av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
  195. return AVERROR(EINVAL);\
  196. }
  197. static av_cold int X264_init(AVCodecContext *avctx)
  198. {
  199. X264Context *x4 = avctx->priv_data;
  200. x264_param_default(&x4->params);
  201. x4->params.b_deblocking_filter = avctx->flags & CODEC_FLAG_LOOP_FILTER;
  202. if (x4->preset || x4->tune)
  203. if (x264_param_default_preset(&x4->params, x4->preset, x4->tune) < 0) {
  204. av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", x4->preset, x4->tune);
  205. return AVERROR(EINVAL);
  206. }
  207. if (avctx->level > 0)
  208. x4->params.i_level_idc = avctx->level;
  209. x4->params.pf_log = X264_log;
  210. x4->params.p_log_private = avctx;
  211. x4->params.i_log_level = X264_LOG_DEBUG;
  212. x4->params.i_csp = convert_pix_fmt(avctx->pix_fmt);
  213. if (avctx->bit_rate) {
  214. x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
  215. x4->params.rc.i_rc_method = X264_RC_ABR;
  216. }
  217. x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
  218. x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000;
  219. x4->params.rc.b_stat_write = avctx->flags & CODEC_FLAG_PASS1;
  220. if (avctx->flags & CODEC_FLAG_PASS2) {
  221. x4->params.rc.b_stat_read = 1;
  222. } else {
  223. if (x4->crf >= 0) {
  224. x4->params.rc.i_rc_method = X264_RC_CRF;
  225. x4->params.rc.f_rf_constant = x4->crf;
  226. } else if (x4->cqp >= 0) {
  227. x4->params.rc.i_rc_method = X264_RC_CQP;
  228. x4->params.rc.i_qp_constant = x4->cqp;
  229. }
  230. if (x4->crf_max >= 0)
  231. x4->params.rc.f_rf_constant_max = x4->crf_max;
  232. }
  233. if (avctx->rc_buffer_size && avctx->rc_initial_buffer_occupancy &&
  234. (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
  235. x4->params.rc.f_vbv_buffer_init =
  236. (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
  237. }
  238. x4->params.rc.f_ip_factor = 1 / fabs(avctx->i_quant_factor);
  239. x4->params.rc.f_pb_factor = avctx->b_quant_factor;
  240. x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
  241. if (avctx->me_method == ME_EPZS)
  242. x4->params.analyse.i_me_method = X264_ME_DIA;
  243. else if (avctx->me_method == ME_HEX)
  244. x4->params.analyse.i_me_method = X264_ME_HEX;
  245. else if (avctx->me_method == ME_UMH)
  246. x4->params.analyse.i_me_method = X264_ME_UMH;
  247. else if (avctx->me_method == ME_FULL)
  248. x4->params.analyse.i_me_method = X264_ME_ESA;
  249. else if (avctx->me_method == ME_TESA)
  250. x4->params.analyse.i_me_method = X264_ME_TESA;
  251. if (avctx->gop_size >= 0)
  252. x4->params.i_keyint_max = avctx->gop_size;
  253. if (avctx->max_b_frames >= 0)
  254. x4->params.i_bframe = avctx->max_b_frames;
  255. if (avctx->scenechange_threshold >= 0)
  256. x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
  257. if (avctx->qmin >= 0)
  258. x4->params.rc.i_qp_min = avctx->qmin;
  259. if (avctx->qmax >= 0)
  260. x4->params.rc.i_qp_max = avctx->qmax;
  261. if (avctx->max_qdiff >= 0)
  262. x4->params.rc.i_qp_step = avctx->max_qdiff;
  263. if (avctx->qblur >= 0)
  264. x4->params.rc.f_qblur = avctx->qblur; /* temporally blur quants */
  265. if (avctx->qcompress >= 0)
  266. x4->params.rc.f_qcompress = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
  267. if (avctx->refs >= 0)
  268. x4->params.i_frame_reference = avctx->refs;
  269. if (avctx->trellis >= 0)
  270. x4->params.analyse.i_trellis = avctx->trellis;
  271. if (avctx->me_range >= 0)
  272. x4->params.analyse.i_me_range = avctx->me_range;
  273. if (avctx->noise_reduction >= 0)
  274. x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
  275. if (avctx->me_subpel_quality >= 0)
  276. x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
  277. if (avctx->b_frame_strategy >= 0)
  278. x4->params.i_bframe_adaptive = avctx->b_frame_strategy;
  279. if (avctx->keyint_min >= 0)
  280. x4->params.i_keyint_min = avctx->keyint_min;
  281. if (avctx->coder_type >= 0)
  282. x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
  283. if (avctx->me_cmp >= 0)
  284. x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA;
  285. if (x4->aq_mode >= 0)
  286. x4->params.rc.i_aq_mode = x4->aq_mode;
  287. if (x4->aq_strength >= 0)
  288. x4->params.rc.f_aq_strength = x4->aq_strength;
  289. PARSE_X264_OPT("psy-rd", psy_rd);
  290. PARSE_X264_OPT("deblock", deblock);
  291. PARSE_X264_OPT("partitions", partitions);
  292. PARSE_X264_OPT("stats", stats);
  293. if (x4->psy >= 0)
  294. x4->params.analyse.b_psy = x4->psy;
  295. if (x4->rc_lookahead >= 0)
  296. x4->params.rc.i_lookahead = x4->rc_lookahead;
  297. if (x4->weightp >= 0)
  298. x4->params.analyse.i_weighted_pred = x4->weightp;
  299. if (x4->weightb >= 0)
  300. x4->params.analyse.b_weighted_bipred = x4->weightb;
  301. if (x4->cplxblur >= 0)
  302. x4->params.rc.f_complexity_blur = x4->cplxblur;
  303. if (x4->ssim >= 0)
  304. x4->params.analyse.b_ssim = x4->ssim;
  305. if (x4->intra_refresh >= 0)
  306. x4->params.b_intra_refresh = x4->intra_refresh;
  307. if (x4->b_bias != INT_MIN)
  308. x4->params.i_bframe_bias = x4->b_bias;
  309. if (x4->b_pyramid >= 0)
  310. x4->params.i_bframe_pyramid = x4->b_pyramid;
  311. if (x4->mixed_refs >= 0)
  312. x4->params.analyse.b_mixed_references = x4->mixed_refs;
  313. if (x4->dct8x8 >= 0)
  314. x4->params.analyse.b_transform_8x8 = x4->dct8x8;
  315. if (x4->fast_pskip >= 0)
  316. x4->params.analyse.b_fast_pskip = x4->fast_pskip;
  317. if (x4->aud >= 0)
  318. x4->params.b_aud = x4->aud;
  319. if (x4->mbtree >= 0)
  320. x4->params.rc.b_mb_tree = x4->mbtree;
  321. if (x4->direct_pred >= 0)
  322. x4->params.analyse.i_direct_mv_pred = x4->direct_pred;
  323. if (x4->slice_max_size >= 0)
  324. x4->params.i_slice_max_size = x4->slice_max_size;
  325. if (x4->fastfirstpass)
  326. x264_param_apply_fastfirstpass(&x4->params);
  327. if (x4->profile)
  328. if (x264_param_apply_profile(&x4->params, x4->profile) < 0) {
  329. av_log(avctx, AV_LOG_ERROR, "Error setting profile %s.\n", x4->profile);
  330. return AVERROR(EINVAL);
  331. }
  332. x4->params.i_width = avctx->width;
  333. x4->params.i_height = avctx->height;
  334. x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
  335. x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
  336. x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
  337. x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
  338. x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
  339. x4->params.i_threads = avctx->thread_count;
  340. if (avctx->thread_type)
  341. x4->params.b_sliced_threads = avctx->thread_type == FF_THREAD_SLICE;
  342. x4->params.b_interlaced = avctx->flags & CODEC_FLAG_INTERLACED_DCT;
  343. x4->params.b_open_gop = !(avctx->flags & CODEC_FLAG_CLOSED_GOP);
  344. x4->params.i_slice_count = avctx->slices;
  345. x4->params.vui.b_fullrange = avctx->pix_fmt == PIX_FMT_YUVJ420P;
  346. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER)
  347. x4->params.b_repeat_headers = 0;
  348. // update AVCodecContext with x264 parameters
  349. avctx->has_b_frames = x4->params.i_bframe ?
  350. x4->params.i_bframe_pyramid ? 2 : 1 : 0;
  351. if (avctx->max_b_frames < 0)
  352. avctx->max_b_frames = 0;
  353. avctx->bit_rate = x4->params.rc.i_bitrate*1000;
  354. x4->enc = x264_encoder_open(&x4->params);
  355. if (!x4->enc)
  356. return -1;
  357. avctx->coded_frame = &x4->out_pic;
  358. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  359. x264_nal_t *nal;
  360. uint8_t *p;
  361. int nnal, s, i;
  362. s = x264_encoder_headers(x4->enc, &nal, &nnal);
  363. avctx->extradata = p = av_malloc(s);
  364. for (i = 0; i < nnal; i++) {
  365. /* Don't put the SEI in extradata. */
  366. if (nal[i].i_type == NAL_SEI) {
  367. av_log(avctx, AV_LOG_INFO, "%s\n", nal[i].p_payload+25);
  368. x4->sei_size = nal[i].i_payload;
  369. x4->sei = av_malloc(x4->sei_size);
  370. memcpy(x4->sei, nal[i].p_payload, nal[i].i_payload);
  371. continue;
  372. }
  373. memcpy(p, nal[i].p_payload, nal[i].i_payload);
  374. p += nal[i].i_payload;
  375. }
  376. avctx->extradata_size = p - avctx->extradata;
  377. }
  378. return 0;
  379. }
  380. static const enum PixelFormat pix_fmts_8bit[] = {
  381. PIX_FMT_YUV420P,
  382. PIX_FMT_YUVJ420P,
  383. PIX_FMT_YUV422P,
  384. PIX_FMT_YUV444P,
  385. PIX_FMT_NONE
  386. };
  387. static const enum PixelFormat pix_fmts_9bit[] = {
  388. PIX_FMT_YUV420P9,
  389. PIX_FMT_YUV444P9,
  390. PIX_FMT_NONE
  391. };
  392. static const enum PixelFormat pix_fmts_10bit[] = {
  393. PIX_FMT_YUV420P10,
  394. PIX_FMT_YUV422P10,
  395. PIX_FMT_YUV444P10,
  396. PIX_FMT_NONE
  397. };
  398. static av_cold void X264_init_static(AVCodec *codec)
  399. {
  400. if (x264_bit_depth == 8)
  401. codec->pix_fmts = pix_fmts_8bit;
  402. else if (x264_bit_depth == 9)
  403. codec->pix_fmts = pix_fmts_9bit;
  404. else if (x264_bit_depth == 10)
  405. codec->pix_fmts = pix_fmts_10bit;
  406. }
  407. #define OFFSET(x) offsetof(X264Context, x)
  408. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  409. static const AVOption options[] = {
  410. { "preset", "Set the encoding preset (cf. x264 --fullhelp)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},
  411. { "tune", "Tune the encoding params (cf. x264 --fullhelp)", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  412. { "profile", "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  413. { "fastfirstpass", "Use fast settings when encoding first pass", OFFSET(fastfirstpass), AV_OPT_TYPE_INT, { 1 }, 0, 1, VE},
  414. { "crf", "Select the quality for constant quality mode", OFFSET(crf), AV_OPT_TYPE_FLOAT, {-1 }, -1, FLT_MAX, VE },
  415. { "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 },
  416. { "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE },
  417. { "aq-mode", "AQ method", OFFSET(aq_mode), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "aq_mode"},
  418. { "none", NULL, 0, AV_OPT_TYPE_CONST, {X264_AQ_NONE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  419. { "variance", "Variance AQ (complexity mask)", 0, AV_OPT_TYPE_CONST, {X264_AQ_VARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  420. { "autovariance", "Auto-variance AQ (experimental)", 0, AV_OPT_TYPE_CONST, {X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  421. { "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},
  422. { "psy", "Use psychovisual optimizations.", OFFSET(psy), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
  423. { "psy-rd", "Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.", OFFSET(psy_rd), AV_OPT_TYPE_STRING, {0 }, 0, 0, VE},
  424. { "rc-lookahead", "Number of frames to look ahead for frametype and ratecontrol", OFFSET(rc_lookahead), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE },
  425. { "weightb", "Weighted prediction for B-frames.", OFFSET(weightb), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
  426. { "weightp", "Weighted prediction analysis method.", OFFSET(weightp), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "weightp" },
  427. { "none", NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_NONE}, INT_MIN, INT_MAX, VE, "weightp" },
  428. { "simple", NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX, VE, "weightp" },
  429. { "smart", NULL, 0, AV_OPT_TYPE_CONST, {X264_WEIGHTP_SMART}, INT_MIN, INT_MAX, VE, "weightp" },
  430. { "ssim", "Calculate and print SSIM stats.", OFFSET(ssim), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
  431. { "intra-refresh", "Use Periodic Intra Refresh instead of IDR frames.",OFFSET(intra_refresh),AV_OPT_TYPE_INT, {-1 }, -1, 1, VE },
  432. { "b-bias", "Influences how often B-frames are used", OFFSET(b_bias), AV_OPT_TYPE_INT, {INT_MIN}, INT_MIN, INT_MAX, VE },
  433. { "b-pyramid", "Keep some B-frames as references.", OFFSET(b_pyramid), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "b_pyramid" },
  434. { "none", NULL, 0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  435. { "strict", "Strictly hierarchical pyramid", 0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  436. { "normal", "Non-strict (not Blu-ray compatible)", 0, AV_OPT_TYPE_CONST, {X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  437. { "mixed-refs", "One reference per partition, as opposed to one reference per macroblock", OFFSET(mixed_refs), AV_OPT_TYPE_INT, {-1}, -1, 1, VE },
  438. { "8x8dct", "High profile 8x8 transform.", OFFSET(dct8x8), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
  439. { "fast-pskip", NULL, OFFSET(fast_pskip), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
  440. { "aud", "Use access unit delimiters.", OFFSET(aud), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
  441. { "mbtree", "Use macroblock tree ratecontrol.", OFFSET(mbtree), AV_OPT_TYPE_INT, {-1 }, -1, 1, VE},
  442. { "deblock", "Loop filter parameters, in <alpha:beta> form.", OFFSET(deblock), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  443. { "cplxblur", "Reduce fluctuations in QP (before curve compression)", OFFSET(cplxblur), AV_OPT_TYPE_FLOAT, {-1 }, -1, FLT_MAX, VE},
  444. { "partitions", "A comma-separated list of partitions to consider. "
  445. "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all", OFFSET(partitions), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  446. { "direct-pred", "Direct MV prediction mode", OFFSET(direct_pred), AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE, "direct-pred" },
  447. { "none", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_NONE }, 0, 0, VE, "direct-pred" },
  448. { "spatial", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_SPATIAL }, 0, 0, VE, "direct-pred" },
  449. { "temporal", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_TEMPORAL }, 0, 0, VE, "direct-pred" },
  450. { "auto", NULL, 0, AV_OPT_TYPE_CONST, { X264_DIRECT_PRED_AUTO }, 0, 0, VE, "direct-pred" },
  451. { "slice-max-size","Limit the size of each slice in bytes", OFFSET(slice_max_size),AV_OPT_TYPE_INT, {-1 }, -1, INT_MAX, VE },
  452. { "stats", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  453. { NULL },
  454. };
  455. static const AVClass class = {
  456. .class_name = "libx264",
  457. .item_name = av_default_item_name,
  458. .option = options,
  459. .version = LIBAVUTIL_VERSION_INT,
  460. };
  461. static const AVCodecDefault x264_defaults[] = {
  462. { "b", "0" },
  463. { "bf", "-1" },
  464. { "g", "-1" },
  465. { "qmin", "-1" },
  466. { "qmax", "-1" },
  467. { "qdiff", "-1" },
  468. { "qblur", "-1" },
  469. { "qcomp", "-1" },
  470. { "refs", "-1" },
  471. { "sc_threshold", "-1" },
  472. { "trellis", "-1" },
  473. { "nr", "-1" },
  474. { "me_range", "-1" },
  475. { "me_method", "-1" },
  476. { "subq", "-1" },
  477. { "b_strategy", "-1" },
  478. { "keyint_min", "-1" },
  479. { "coder", "-1" },
  480. { "cmp", "-1" },
  481. { "threads", AV_STRINGIFY(X264_THREADS_AUTO) },
  482. { "thread_type", "0" },
  483. { NULL },
  484. };
  485. AVCodec ff_libx264_encoder = {
  486. .name = "libx264",
  487. .type = AVMEDIA_TYPE_VIDEO,
  488. .id = CODEC_ID_H264,
  489. .priv_data_size = sizeof(X264Context),
  490. .init = X264_init,
  491. .encode2 = X264_frame,
  492. .close = X264_close,
  493. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
  494. .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  495. .priv_class = &class,
  496. .defaults = x264_defaults,
  497. .init_static_data = X264_init_static,
  498. };