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.

601 lines
24KB

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