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.

1099 lines
42KB

  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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/eval.h"
  22. #include "libavutil/internal.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/mem.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "libavutil/stereo3d.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "avcodec.h"
  29. #include "internal.h"
  30. #if defined(_MSC_VER)
  31. #define X264_API_IMPORTS 1
  32. #endif
  33. #include <x264.h>
  34. #include <float.h>
  35. #include <math.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. typedef struct X264Context {
  40. AVClass *class;
  41. x264_param_t params;
  42. x264_t *enc;
  43. x264_picture_t pic;
  44. uint8_t *sei;
  45. int sei_size;
  46. char *preset;
  47. char *tune;
  48. char *profile;
  49. char *level;
  50. int fastfirstpass;
  51. char *wpredp;
  52. char *x264opts;
  53. float crf;
  54. float crf_max;
  55. int cqp;
  56. int aq_mode;
  57. float aq_strength;
  58. char *psy_rd;
  59. int psy;
  60. int rc_lookahead;
  61. int weightp;
  62. int weightb;
  63. int ssim;
  64. int intra_refresh;
  65. int bluray_compat;
  66. int b_bias;
  67. int b_pyramid;
  68. int mixed_refs;
  69. int dct8x8;
  70. int fast_pskip;
  71. int aud;
  72. int mbtree;
  73. char *deblock;
  74. float cplxblur;
  75. char *partitions;
  76. int direct_pred;
  77. int slice_max_size;
  78. char *stats;
  79. int nal_hrd;
  80. int avcintra_class;
  81. int motion_est;
  82. int forced_idr;
  83. int coder;
  84. int a53_cc;
  85. int b_frame_strategy;
  86. int chroma_offset;
  87. int scenechange_threshold;
  88. int noise_reduction;
  89. char *x264_params;
  90. } X264Context;
  91. static void X264_log(void *p, int level, const char *fmt, va_list args)
  92. {
  93. static const int level_map[] = {
  94. [X264_LOG_ERROR] = AV_LOG_ERROR,
  95. [X264_LOG_WARNING] = AV_LOG_WARNING,
  96. [X264_LOG_INFO] = AV_LOG_INFO,
  97. [X264_LOG_DEBUG] = AV_LOG_DEBUG
  98. };
  99. if (level < 0 || level > X264_LOG_DEBUG)
  100. return;
  101. av_vlog(p, level_map[level], fmt, args);
  102. }
  103. static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
  104. const x264_nal_t *nals, int nnal)
  105. {
  106. X264Context *x4 = ctx->priv_data;
  107. uint8_t *p;
  108. int i, size = x4->sei_size, ret;
  109. if (!nnal)
  110. return 0;
  111. for (i = 0; i < nnal; i++)
  112. size += nals[i].i_payload;
  113. if ((ret = ff_alloc_packet2(ctx, pkt, size, 0)) < 0)
  114. return ret;
  115. p = pkt->data;
  116. /* Write the SEI as part of the first frame. */
  117. if (x4->sei_size > 0 && nnal > 0) {
  118. if (x4->sei_size > size) {
  119. av_log(ctx, AV_LOG_ERROR, "Error: nal buffer is too small\n");
  120. return -1;
  121. }
  122. memcpy(p, x4->sei, x4->sei_size);
  123. p += x4->sei_size;
  124. x4->sei_size = 0;
  125. av_freep(&x4->sei);
  126. }
  127. for (i = 0; i < nnal; i++){
  128. memcpy(p, nals[i].p_payload, nals[i].i_payload);
  129. p += nals[i].i_payload;
  130. }
  131. return 1;
  132. }
  133. static int avfmt2_num_planes(int avfmt)
  134. {
  135. switch (avfmt) {
  136. case AV_PIX_FMT_YUV420P:
  137. case AV_PIX_FMT_YUVJ420P:
  138. case AV_PIX_FMT_YUV420P9:
  139. case AV_PIX_FMT_YUV420P10:
  140. case AV_PIX_FMT_YUV444P:
  141. return 3;
  142. case AV_PIX_FMT_BGR0:
  143. case AV_PIX_FMT_BGR24:
  144. case AV_PIX_FMT_RGB24:
  145. return 1;
  146. default:
  147. return 3;
  148. }
  149. }
  150. static void reconfig_encoder(AVCodecContext *ctx, const AVFrame *frame)
  151. {
  152. X264Context *x4 = ctx->priv_data;
  153. AVFrameSideData *side_data;
  154. if (x4->avcintra_class < 0) {
  155. if (x4->params.b_interlaced && x4->params.b_tff != frame->top_field_first) {
  156. x4->params.b_tff = frame->top_field_first;
  157. x264_encoder_reconfig(x4->enc, &x4->params);
  158. }
  159. if (x4->params.vui.i_sar_height*ctx->sample_aspect_ratio.num != ctx->sample_aspect_ratio.den * x4->params.vui.i_sar_width) {
  160. x4->params.vui.i_sar_height = ctx->sample_aspect_ratio.den;
  161. x4->params.vui.i_sar_width = ctx->sample_aspect_ratio.num;
  162. x264_encoder_reconfig(x4->enc, &x4->params);
  163. }
  164. if (x4->params.rc.i_vbv_buffer_size != ctx->rc_buffer_size / 1000 ||
  165. x4->params.rc.i_vbv_max_bitrate != ctx->rc_max_rate / 1000) {
  166. x4->params.rc.i_vbv_buffer_size = ctx->rc_buffer_size / 1000;
  167. x4->params.rc.i_vbv_max_bitrate = ctx->rc_max_rate / 1000;
  168. x264_encoder_reconfig(x4->enc, &x4->params);
  169. }
  170. if (x4->params.rc.i_rc_method == X264_RC_ABR &&
  171. x4->params.rc.i_bitrate != ctx->bit_rate / 1000) {
  172. x4->params.rc.i_bitrate = ctx->bit_rate / 1000;
  173. x264_encoder_reconfig(x4->enc, &x4->params);
  174. }
  175. if (x4->crf >= 0 &&
  176. x4->params.rc.i_rc_method == X264_RC_CRF &&
  177. x4->params.rc.f_rf_constant != x4->crf) {
  178. x4->params.rc.f_rf_constant = x4->crf;
  179. x264_encoder_reconfig(x4->enc, &x4->params);
  180. }
  181. if (x4->params.rc.i_rc_method == X264_RC_CQP &&
  182. x4->cqp >= 0 &&
  183. x4->params.rc.i_qp_constant != x4->cqp) {
  184. x4->params.rc.i_qp_constant = x4->cqp;
  185. x264_encoder_reconfig(x4->enc, &x4->params);
  186. }
  187. if (x4->crf_max >= 0 &&
  188. x4->params.rc.f_rf_constant_max != x4->crf_max) {
  189. x4->params.rc.f_rf_constant_max = x4->crf_max;
  190. x264_encoder_reconfig(x4->enc, &x4->params);
  191. }
  192. }
  193. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_STEREO3D);
  194. if (side_data) {
  195. AVStereo3D *stereo = (AVStereo3D *)side_data->data;
  196. int fpa_type;
  197. switch (stereo->type) {
  198. case AV_STEREO3D_CHECKERBOARD:
  199. fpa_type = 0;
  200. break;
  201. case AV_STEREO3D_COLUMNS:
  202. fpa_type = 1;
  203. break;
  204. case AV_STEREO3D_LINES:
  205. fpa_type = 2;
  206. break;
  207. case AV_STEREO3D_SIDEBYSIDE:
  208. fpa_type = 3;
  209. break;
  210. case AV_STEREO3D_TOPBOTTOM:
  211. fpa_type = 4;
  212. break;
  213. case AV_STEREO3D_FRAMESEQUENCE:
  214. fpa_type = 5;
  215. break;
  216. #if X264_BUILD >= 145
  217. case AV_STEREO3D_2D:
  218. fpa_type = 6;
  219. break;
  220. #endif
  221. default:
  222. fpa_type = -1;
  223. break;
  224. }
  225. /* Inverted mode is not supported by x264 */
  226. if (stereo->flags & AV_STEREO3D_FLAG_INVERT) {
  227. av_log(ctx, AV_LOG_WARNING,
  228. "Ignoring unsupported inverted stereo value %d\n", fpa_type);
  229. fpa_type = -1;
  230. }
  231. if (fpa_type != x4->params.i_frame_packing) {
  232. x4->params.i_frame_packing = fpa_type;
  233. x264_encoder_reconfig(x4->enc, &x4->params);
  234. }
  235. }
  236. }
  237. static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
  238. int *got_packet)
  239. {
  240. X264Context *x4 = ctx->priv_data;
  241. x264_nal_t *nal;
  242. int nnal, i, ret;
  243. x264_picture_t pic_out = {0};
  244. int pict_type;
  245. x264_picture_init( &x4->pic );
  246. x4->pic.img.i_csp = x4->params.i_csp;
  247. if (x264_bit_depth > 8)
  248. x4->pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
  249. x4->pic.img.i_plane = avfmt2_num_planes(ctx->pix_fmt);
  250. if (frame) {
  251. for (i = 0; i < x4->pic.img.i_plane; i++) {
  252. x4->pic.img.plane[i] = frame->data[i];
  253. x4->pic.img.i_stride[i] = frame->linesize[i];
  254. }
  255. x4->pic.i_pts = frame->pts;
  256. switch (frame->pict_type) {
  257. case AV_PICTURE_TYPE_I:
  258. x4->pic.i_type = x4->forced_idr >= 0 ? X264_TYPE_IDR
  259. : X264_TYPE_KEYFRAME;
  260. break;
  261. case AV_PICTURE_TYPE_P:
  262. x4->pic.i_type = X264_TYPE_P;
  263. break;
  264. case AV_PICTURE_TYPE_B:
  265. x4->pic.i_type = X264_TYPE_B;
  266. break;
  267. default:
  268. x4->pic.i_type = X264_TYPE_AUTO;
  269. break;
  270. }
  271. reconfig_encoder(ctx, frame);
  272. if (x4->a53_cc) {
  273. void *sei_data;
  274. size_t sei_size;
  275. ret = ff_alloc_a53_sei(frame, 0, &sei_data, &sei_size);
  276. if (ret < 0) {
  277. av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
  278. } else if (sei_data) {
  279. x4->pic.extra_sei.payloads = av_mallocz(sizeof(x4->pic.extra_sei.payloads[0]));
  280. if (x4->pic.extra_sei.payloads == NULL) {
  281. av_log(ctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
  282. av_free(sei_data);
  283. } else {
  284. x4->pic.extra_sei.sei_free = av_free;
  285. x4->pic.extra_sei.payloads[0].payload_size = sei_size;
  286. x4->pic.extra_sei.payloads[0].payload = sei_data;
  287. x4->pic.extra_sei.num_payloads = 1;
  288. x4->pic.extra_sei.payloads[0].payload_type = 4;
  289. }
  290. }
  291. }
  292. }
  293. do {
  294. if (x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, &pic_out) < 0)
  295. return AVERROR_EXTERNAL;
  296. ret = encode_nals(ctx, pkt, nal, nnal);
  297. if (ret < 0)
  298. return ret;
  299. } while (!ret && !frame && x264_encoder_delayed_frames(x4->enc));
  300. pkt->pts = pic_out.i_pts;
  301. pkt->dts = pic_out.i_dts;
  302. switch (pic_out.i_type) {
  303. case X264_TYPE_IDR:
  304. case X264_TYPE_I:
  305. pict_type = AV_PICTURE_TYPE_I;
  306. break;
  307. case X264_TYPE_P:
  308. pict_type = AV_PICTURE_TYPE_P;
  309. break;
  310. case X264_TYPE_B:
  311. case X264_TYPE_BREF:
  312. pict_type = AV_PICTURE_TYPE_B;
  313. break;
  314. default:
  315. pict_type = AV_PICTURE_TYPE_NONE;
  316. }
  317. #if FF_API_CODED_FRAME
  318. FF_DISABLE_DEPRECATION_WARNINGS
  319. ctx->coded_frame->pict_type = pict_type;
  320. FF_ENABLE_DEPRECATION_WARNINGS
  321. #endif
  322. pkt->flags |= AV_PKT_FLAG_KEY*pic_out.b_keyframe;
  323. if (ret) {
  324. ff_side_data_set_encoder_stats(pkt, (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA, NULL, 0, pict_type);
  325. #if FF_API_CODED_FRAME
  326. FF_DISABLE_DEPRECATION_WARNINGS
  327. ctx->coded_frame->quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
  328. FF_ENABLE_DEPRECATION_WARNINGS
  329. #endif
  330. }
  331. *got_packet = ret;
  332. return 0;
  333. }
  334. static av_cold int X264_close(AVCodecContext *avctx)
  335. {
  336. X264Context *x4 = avctx->priv_data;
  337. av_freep(&avctx->extradata);
  338. av_freep(&x4->sei);
  339. if (x4->enc) {
  340. x264_encoder_close(x4->enc);
  341. x4->enc = NULL;
  342. }
  343. return 0;
  344. }
  345. #define OPT_STR(opt, param) \
  346. do { \
  347. int ret; \
  348. if ((ret = x264_param_parse(&x4->params, opt, param)) < 0) { \
  349. if(ret == X264_PARAM_BAD_NAME) \
  350. av_log(avctx, AV_LOG_ERROR, \
  351. "bad option '%s': '%s'\n", opt, param); \
  352. else \
  353. av_log(avctx, AV_LOG_ERROR, \
  354. "bad value for '%s': '%s'\n", opt, param); \
  355. return -1; \
  356. } \
  357. } while (0)
  358. static int convert_pix_fmt(enum AVPixelFormat pix_fmt)
  359. {
  360. switch (pix_fmt) {
  361. case AV_PIX_FMT_YUV420P:
  362. case AV_PIX_FMT_YUVJ420P:
  363. case AV_PIX_FMT_YUV420P9:
  364. case AV_PIX_FMT_YUV420P10: return X264_CSP_I420;
  365. case AV_PIX_FMT_YUV422P:
  366. case AV_PIX_FMT_YUVJ422P:
  367. case AV_PIX_FMT_YUV422P10: return X264_CSP_I422;
  368. case AV_PIX_FMT_YUV444P:
  369. case AV_PIX_FMT_YUVJ444P:
  370. case AV_PIX_FMT_YUV444P9:
  371. case AV_PIX_FMT_YUV444P10: return X264_CSP_I444;
  372. #if CONFIG_LIBX264RGB_ENCODER
  373. case AV_PIX_FMT_BGR0:
  374. return X264_CSP_BGRA;
  375. case AV_PIX_FMT_BGR24:
  376. return X264_CSP_BGR;
  377. case AV_PIX_FMT_RGB24:
  378. return X264_CSP_RGB;
  379. #endif
  380. case AV_PIX_FMT_NV12: return X264_CSP_NV12;
  381. case AV_PIX_FMT_NV16:
  382. case AV_PIX_FMT_NV20: return X264_CSP_NV16;
  383. #ifdef X264_CSP_NV21
  384. case AV_PIX_FMT_NV21: return X264_CSP_NV21;
  385. #endif
  386. };
  387. return 0;
  388. }
  389. #define PARSE_X264_OPT(name, var)\
  390. if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
  391. av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
  392. return AVERROR(EINVAL);\
  393. }
  394. static av_cold int X264_init(AVCodecContext *avctx)
  395. {
  396. X264Context *x4 = avctx->priv_data;
  397. AVCPBProperties *cpb_props;
  398. int sw,sh;
  399. if (avctx->global_quality > 0)
  400. av_log(avctx, AV_LOG_WARNING, "-qscale is ignored, -crf is recommended.\n");
  401. #if CONFIG_LIBX262_ENCODER
  402. if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  403. x4->params.b_mpeg2 = 1;
  404. x264_param_default_mpeg2(&x4->params);
  405. } else
  406. #endif
  407. x264_param_default(&x4->params);
  408. x4->params.b_deblocking_filter = avctx->flags & AV_CODEC_FLAG_LOOP_FILTER;
  409. if (x4->preset || x4->tune)
  410. if (x264_param_default_preset(&x4->params, x4->preset, x4->tune) < 0) {
  411. int i;
  412. av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", x4->preset, x4->tune);
  413. av_log(avctx, AV_LOG_INFO, "Possible presets:");
  414. for (i = 0; x264_preset_names[i]; i++)
  415. av_log(avctx, AV_LOG_INFO, " %s", x264_preset_names[i]);
  416. av_log(avctx, AV_LOG_INFO, "\n");
  417. av_log(avctx, AV_LOG_INFO, "Possible tunes:");
  418. for (i = 0; x264_tune_names[i]; i++)
  419. av_log(avctx, AV_LOG_INFO, " %s", x264_tune_names[i]);
  420. av_log(avctx, AV_LOG_INFO, "\n");
  421. return AVERROR(EINVAL);
  422. }
  423. if (avctx->level > 0)
  424. x4->params.i_level_idc = avctx->level;
  425. x4->params.pf_log = X264_log;
  426. x4->params.p_log_private = avctx;
  427. x4->params.i_log_level = X264_LOG_DEBUG;
  428. x4->params.i_csp = convert_pix_fmt(avctx->pix_fmt);
  429. PARSE_X264_OPT("weightp", wpredp);
  430. if (avctx->bit_rate) {
  431. x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
  432. x4->params.rc.i_rc_method = X264_RC_ABR;
  433. }
  434. x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
  435. x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000;
  436. x4->params.rc.b_stat_write = avctx->flags & AV_CODEC_FLAG_PASS1;
  437. if (avctx->flags & AV_CODEC_FLAG_PASS2) {
  438. x4->params.rc.b_stat_read = 1;
  439. } else {
  440. if (x4->crf >= 0) {
  441. x4->params.rc.i_rc_method = X264_RC_CRF;
  442. x4->params.rc.f_rf_constant = x4->crf;
  443. } else if (x4->cqp >= 0) {
  444. x4->params.rc.i_rc_method = X264_RC_CQP;
  445. x4->params.rc.i_qp_constant = x4->cqp;
  446. }
  447. if (x4->crf_max >= 0)
  448. x4->params.rc.f_rf_constant_max = x4->crf_max;
  449. }
  450. if (avctx->rc_buffer_size && avctx->rc_initial_buffer_occupancy > 0 &&
  451. (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
  452. x4->params.rc.f_vbv_buffer_init =
  453. (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
  454. }
  455. PARSE_X264_OPT("level", level);
  456. if (avctx->i_quant_factor > 0)
  457. x4->params.rc.f_ip_factor = 1 / fabs(avctx->i_quant_factor);
  458. if (avctx->b_quant_factor > 0)
  459. x4->params.rc.f_pb_factor = avctx->b_quant_factor;
  460. #if FF_API_PRIVATE_OPT
  461. FF_DISABLE_DEPRECATION_WARNINGS
  462. if (avctx->chromaoffset >= 0)
  463. x4->chroma_offset = avctx->chromaoffset;
  464. FF_ENABLE_DEPRECATION_WARNINGS
  465. #endif
  466. if (x4->chroma_offset >= 0)
  467. x4->params.analyse.i_chroma_qp_offset = x4->chroma_offset;
  468. if (avctx->gop_size >= 0)
  469. x4->params.i_keyint_max = avctx->gop_size;
  470. if (avctx->max_b_frames >= 0)
  471. x4->params.i_bframe = avctx->max_b_frames;
  472. #if FF_API_PRIVATE_OPT
  473. FF_DISABLE_DEPRECATION_WARNINGS
  474. if (avctx->scenechange_threshold >= 0)
  475. x4->scenechange_threshold = avctx->scenechange_threshold;
  476. FF_ENABLE_DEPRECATION_WARNINGS
  477. #endif
  478. if (x4->scenechange_threshold >= 0)
  479. x4->params.i_scenecut_threshold = x4->scenechange_threshold;
  480. if (avctx->qmin >= 0)
  481. x4->params.rc.i_qp_min = avctx->qmin;
  482. if (avctx->qmax >= 0)
  483. x4->params.rc.i_qp_max = avctx->qmax;
  484. if (avctx->max_qdiff >= 0)
  485. x4->params.rc.i_qp_step = avctx->max_qdiff;
  486. if (avctx->qblur >= 0)
  487. x4->params.rc.f_qblur = avctx->qblur; /* temporally blur quants */
  488. if (avctx->qcompress >= 0)
  489. x4->params.rc.f_qcompress = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
  490. if (avctx->refs >= 0)
  491. x4->params.i_frame_reference = avctx->refs;
  492. else if (x4->level) {
  493. int i;
  494. int mbn = AV_CEIL_RSHIFT(avctx->width, 4) * AV_CEIL_RSHIFT(avctx->height, 4);
  495. int level_id = -1;
  496. char *tail;
  497. int scale = X264_BUILD < 129 ? 384 : 1;
  498. if (!strcmp(x4->level, "1b")) {
  499. level_id = 9;
  500. } else if (strlen(x4->level) <= 3){
  501. level_id = av_strtod(x4->level, &tail) * 10 + 0.5;
  502. if (*tail)
  503. level_id = -1;
  504. }
  505. if (level_id <= 0)
  506. av_log(avctx, AV_LOG_WARNING, "Failed to parse level\n");
  507. for (i = 0; i<x264_levels[i].level_idc; i++)
  508. if (x264_levels[i].level_idc == level_id)
  509. x4->params.i_frame_reference = av_clip(x264_levels[i].dpb / mbn / scale, 1, x4->params.i_frame_reference);
  510. }
  511. if (avctx->trellis >= 0)
  512. x4->params.analyse.i_trellis = avctx->trellis;
  513. if (avctx->me_range >= 0)
  514. x4->params.analyse.i_me_range = avctx->me_range;
  515. #if FF_API_PRIVATE_OPT
  516. FF_DISABLE_DEPRECATION_WARNINGS
  517. if (avctx->noise_reduction >= 0)
  518. x4->noise_reduction = avctx->noise_reduction;
  519. FF_ENABLE_DEPRECATION_WARNINGS
  520. #endif
  521. if (x4->noise_reduction >= 0)
  522. x4->params.analyse.i_noise_reduction = x4->noise_reduction;
  523. if (avctx->me_subpel_quality >= 0)
  524. x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
  525. #if FF_API_PRIVATE_OPT
  526. FF_DISABLE_DEPRECATION_WARNINGS
  527. if (avctx->b_frame_strategy >= 0)
  528. x4->b_frame_strategy = avctx->b_frame_strategy;
  529. FF_ENABLE_DEPRECATION_WARNINGS
  530. #endif
  531. if (avctx->keyint_min >= 0)
  532. x4->params.i_keyint_min = avctx->keyint_min;
  533. #if FF_API_CODER_TYPE
  534. FF_DISABLE_DEPRECATION_WARNINGS
  535. if (avctx->coder_type >= 0)
  536. x4->coder = avctx->coder_type == FF_CODER_TYPE_AC;
  537. FF_ENABLE_DEPRECATION_WARNINGS
  538. #endif
  539. if (avctx->me_cmp >= 0)
  540. x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA;
  541. if (x4->aq_mode >= 0)
  542. x4->params.rc.i_aq_mode = x4->aq_mode;
  543. if (x4->aq_strength >= 0)
  544. x4->params.rc.f_aq_strength = x4->aq_strength;
  545. PARSE_X264_OPT("psy-rd", psy_rd);
  546. PARSE_X264_OPT("deblock", deblock);
  547. PARSE_X264_OPT("partitions", partitions);
  548. PARSE_X264_OPT("stats", stats);
  549. if (x4->psy >= 0)
  550. x4->params.analyse.b_psy = x4->psy;
  551. if (x4->rc_lookahead >= 0)
  552. x4->params.rc.i_lookahead = x4->rc_lookahead;
  553. if (x4->weightp >= 0)
  554. x4->params.analyse.i_weighted_pred = x4->weightp;
  555. if (x4->weightb >= 0)
  556. x4->params.analyse.b_weighted_bipred = x4->weightb;
  557. if (x4->cplxblur >= 0)
  558. x4->params.rc.f_complexity_blur = x4->cplxblur;
  559. if (x4->ssim >= 0)
  560. x4->params.analyse.b_ssim = x4->ssim;
  561. if (x4->intra_refresh >= 0)
  562. x4->params.b_intra_refresh = x4->intra_refresh;
  563. if (x4->bluray_compat >= 0) {
  564. x4->params.b_bluray_compat = x4->bluray_compat;
  565. x4->params.b_vfr_input = 0;
  566. }
  567. if (x4->avcintra_class >= 0)
  568. #if X264_BUILD >= 142
  569. x4->params.i_avcintra_class = x4->avcintra_class;
  570. #else
  571. av_log(avctx, AV_LOG_ERROR,
  572. "x264 too old for AVC Intra, at least version 142 needed\n");
  573. #endif
  574. if (x4->b_bias != INT_MIN)
  575. x4->params.i_bframe_bias = x4->b_bias;
  576. if (x4->b_pyramid >= 0)
  577. x4->params.i_bframe_pyramid = x4->b_pyramid;
  578. if (x4->mixed_refs >= 0)
  579. x4->params.analyse.b_mixed_references = x4->mixed_refs;
  580. if (x4->dct8x8 >= 0)
  581. x4->params.analyse.b_transform_8x8 = x4->dct8x8;
  582. if (x4->fast_pskip >= 0)
  583. x4->params.analyse.b_fast_pskip = x4->fast_pskip;
  584. if (x4->aud >= 0)
  585. x4->params.b_aud = x4->aud;
  586. if (x4->mbtree >= 0)
  587. x4->params.rc.b_mb_tree = x4->mbtree;
  588. if (x4->direct_pred >= 0)
  589. x4->params.analyse.i_direct_mv_pred = x4->direct_pred;
  590. if (x4->slice_max_size >= 0)
  591. x4->params.i_slice_max_size = x4->slice_max_size;
  592. if (x4->fastfirstpass)
  593. x264_param_apply_fastfirstpass(&x4->params);
  594. /* Allow specifying the x264 profile through AVCodecContext. */
  595. if (!x4->profile)
  596. switch (avctx->profile) {
  597. case FF_PROFILE_H264_BASELINE:
  598. x4->profile = av_strdup("baseline");
  599. break;
  600. case FF_PROFILE_H264_HIGH:
  601. x4->profile = av_strdup("high");
  602. break;
  603. case FF_PROFILE_H264_HIGH_10:
  604. x4->profile = av_strdup("high10");
  605. break;
  606. case FF_PROFILE_H264_HIGH_422:
  607. x4->profile = av_strdup("high422");
  608. break;
  609. case FF_PROFILE_H264_HIGH_444:
  610. x4->profile = av_strdup("high444");
  611. break;
  612. case FF_PROFILE_H264_MAIN:
  613. x4->profile = av_strdup("main");
  614. break;
  615. default:
  616. break;
  617. }
  618. if (x4->nal_hrd >= 0)
  619. x4->params.i_nal_hrd = x4->nal_hrd;
  620. if (x4->motion_est >= 0) {
  621. x4->params.analyse.i_me_method = x4->motion_est;
  622. #if FF_API_MOTION_EST
  623. FF_DISABLE_DEPRECATION_WARNINGS
  624. } else {
  625. if (avctx->me_method == ME_EPZS)
  626. x4->params.analyse.i_me_method = X264_ME_DIA;
  627. else if (avctx->me_method == ME_HEX)
  628. x4->params.analyse.i_me_method = X264_ME_HEX;
  629. else if (avctx->me_method == ME_UMH)
  630. x4->params.analyse.i_me_method = X264_ME_UMH;
  631. else if (avctx->me_method == ME_FULL)
  632. x4->params.analyse.i_me_method = X264_ME_ESA;
  633. else if (avctx->me_method == ME_TESA)
  634. x4->params.analyse.i_me_method = X264_ME_TESA;
  635. FF_ENABLE_DEPRECATION_WARNINGS
  636. #endif
  637. }
  638. if (x4->coder >= 0)
  639. x4->params.b_cabac = x4->coder;
  640. if (x4->b_frame_strategy >= 0)
  641. x4->params.i_bframe_adaptive = x4->b_frame_strategy;
  642. if (x4->profile)
  643. if (x264_param_apply_profile(&x4->params, x4->profile) < 0) {
  644. int i;
  645. av_log(avctx, AV_LOG_ERROR, "Error setting profile %s.\n", x4->profile);
  646. av_log(avctx, AV_LOG_INFO, "Possible profiles:");
  647. for (i = 0; x264_profile_names[i]; i++)
  648. av_log(avctx, AV_LOG_INFO, " %s", x264_profile_names[i]);
  649. av_log(avctx, AV_LOG_INFO, "\n");
  650. return AVERROR(EINVAL);
  651. }
  652. x4->params.i_width = avctx->width;
  653. x4->params.i_height = avctx->height;
  654. av_reduce(&sw, &sh, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 4096);
  655. x4->params.vui.i_sar_width = sw;
  656. x4->params.vui.i_sar_height = sh;
  657. x4->params.i_timebase_den = avctx->time_base.den;
  658. x4->params.i_timebase_num = avctx->time_base.num;
  659. x4->params.i_fps_num = avctx->time_base.den;
  660. x4->params.i_fps_den = avctx->time_base.num * avctx->ticks_per_frame;
  661. x4->params.analyse.b_psnr = avctx->flags & AV_CODEC_FLAG_PSNR;
  662. x4->params.i_threads = avctx->thread_count;
  663. if (avctx->thread_type)
  664. x4->params.b_sliced_threads = avctx->thread_type == FF_THREAD_SLICE;
  665. x4->params.b_interlaced = avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT;
  666. x4->params.b_open_gop = !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP);
  667. x4->params.i_slice_count = avctx->slices;
  668. x4->params.vui.b_fullrange = avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
  669. avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
  670. avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
  671. avctx->color_range == AVCOL_RANGE_JPEG;
  672. if (avctx->colorspace != AVCOL_SPC_UNSPECIFIED)
  673. x4->params.vui.i_colmatrix = avctx->colorspace;
  674. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED)
  675. x4->params.vui.i_colorprim = avctx->color_primaries;
  676. if (avctx->color_trc != AVCOL_TRC_UNSPECIFIED)
  677. x4->params.vui.i_transfer = avctx->color_trc;
  678. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)
  679. x4->params.b_repeat_headers = 0;
  680. if(x4->x264opts){
  681. const char *p= x4->x264opts;
  682. while(p){
  683. char param[4096]={0}, val[4096]={0};
  684. if(sscanf(p, "%4095[^:=]=%4095[^:]", param, val) == 1){
  685. OPT_STR(param, "1");
  686. }else
  687. OPT_STR(param, val);
  688. p= strchr(p, ':');
  689. p+=!!p;
  690. }
  691. }
  692. if (x4->x264_params) {
  693. AVDictionary *dict = NULL;
  694. AVDictionaryEntry *en = NULL;
  695. if (!av_dict_parse_string(&dict, x4->x264_params, "=", ":", 0)) {
  696. while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
  697. if (x264_param_parse(&x4->params, en->key, en->value) < 0)
  698. av_log(avctx, AV_LOG_WARNING,
  699. "Error parsing option '%s = %s'.\n",
  700. en->key, en->value);
  701. }
  702. av_dict_free(&dict);
  703. }
  704. }
  705. // update AVCodecContext with x264 parameters
  706. avctx->has_b_frames = x4->params.i_bframe ?
  707. x4->params.i_bframe_pyramid ? 2 : 1 : 0;
  708. if (avctx->max_b_frames < 0)
  709. avctx->max_b_frames = 0;
  710. avctx->bit_rate = x4->params.rc.i_bitrate*1000;
  711. x4->enc = x264_encoder_open(&x4->params);
  712. if (!x4->enc)
  713. return AVERROR_EXTERNAL;
  714. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  715. x264_nal_t *nal;
  716. uint8_t *p;
  717. int nnal, s, i;
  718. s = x264_encoder_headers(x4->enc, &nal, &nnal);
  719. avctx->extradata = p = av_mallocz(s + AV_INPUT_BUFFER_PADDING_SIZE);
  720. if (!p)
  721. return AVERROR(ENOMEM);
  722. for (i = 0; i < nnal; i++) {
  723. /* Don't put the SEI in extradata. */
  724. if (nal[i].i_type == NAL_SEI) {
  725. av_log(avctx, AV_LOG_INFO, "%s\n", nal[i].p_payload+25);
  726. x4->sei_size = nal[i].i_payload;
  727. x4->sei = av_malloc(x4->sei_size);
  728. if (!x4->sei)
  729. return AVERROR(ENOMEM);
  730. memcpy(x4->sei, nal[i].p_payload, nal[i].i_payload);
  731. continue;
  732. }
  733. memcpy(p, nal[i].p_payload, nal[i].i_payload);
  734. p += nal[i].i_payload;
  735. }
  736. avctx->extradata_size = p - avctx->extradata;
  737. }
  738. cpb_props = ff_add_cpb_side_data(avctx);
  739. if (!cpb_props)
  740. return AVERROR(ENOMEM);
  741. cpb_props->buffer_size = x4->params.rc.i_vbv_buffer_size * 1000;
  742. cpb_props->max_bitrate = x4->params.rc.i_vbv_max_bitrate * 1000;
  743. cpb_props->avg_bitrate = x4->params.rc.i_bitrate * 1000;
  744. return 0;
  745. }
  746. static const enum AVPixelFormat pix_fmts_8bit[] = {
  747. AV_PIX_FMT_YUV420P,
  748. AV_PIX_FMT_YUVJ420P,
  749. AV_PIX_FMT_YUV422P,
  750. AV_PIX_FMT_YUVJ422P,
  751. AV_PIX_FMT_YUV444P,
  752. AV_PIX_FMT_YUVJ444P,
  753. AV_PIX_FMT_NV12,
  754. AV_PIX_FMT_NV16,
  755. #ifdef X264_CSP_NV21
  756. AV_PIX_FMT_NV21,
  757. #endif
  758. AV_PIX_FMT_NONE
  759. };
  760. static const enum AVPixelFormat pix_fmts_9bit[] = {
  761. AV_PIX_FMT_YUV420P9,
  762. AV_PIX_FMT_YUV444P9,
  763. AV_PIX_FMT_NONE
  764. };
  765. static const enum AVPixelFormat pix_fmts_10bit[] = {
  766. AV_PIX_FMT_YUV420P10,
  767. AV_PIX_FMT_YUV422P10,
  768. AV_PIX_FMT_YUV444P10,
  769. AV_PIX_FMT_NV20,
  770. AV_PIX_FMT_NONE
  771. };
  772. #if CONFIG_LIBX264RGB_ENCODER
  773. static const enum AVPixelFormat pix_fmts_8bit_rgb[] = {
  774. AV_PIX_FMT_BGR0,
  775. AV_PIX_FMT_BGR24,
  776. AV_PIX_FMT_RGB24,
  777. AV_PIX_FMT_NONE
  778. };
  779. #endif
  780. static av_cold void X264_init_static(AVCodec *codec)
  781. {
  782. if (x264_bit_depth == 8)
  783. codec->pix_fmts = pix_fmts_8bit;
  784. else if (x264_bit_depth == 9)
  785. codec->pix_fmts = pix_fmts_9bit;
  786. else if (x264_bit_depth == 10)
  787. codec->pix_fmts = pix_fmts_10bit;
  788. }
  789. #define OFFSET(x) offsetof(X264Context, x)
  790. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  791. static const AVOption options[] = {
  792. { "preset", "Set the encoding preset (cf. x264 --fullhelp)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},
  793. { "tune", "Tune the encoding params (cf. x264 --fullhelp)", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  794. { "profile", "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  795. { "fastfirstpass", "Use fast settings when encoding first pass", OFFSET(fastfirstpass), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE},
  796. {"level", "Specify level (as defined by Annex A)", OFFSET(level), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
  797. {"passlogfile", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
  798. {"wpredp", "Weighted prediction for P-frames", OFFSET(wpredp), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
  799. {"a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VE},
  800. {"x264opts", "x264 options", OFFSET(x264opts), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
  801. { "crf", "Select the quality for constant quality mode", OFFSET(crf), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE },
  802. { "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 },
  803. { "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },
  804. { "aq-mode", "AQ method", OFFSET(aq_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "aq_mode"},
  805. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  806. { "variance", "Variance AQ (complexity mask)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  807. { "autovariance", "Auto-variance AQ", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  808. #if X264_BUILD >= 144
  809. { "autovariance-biased", "Auto-variance AQ with bias to dark scenes", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE_BIASED}, INT_MIN, INT_MAX, VE, "aq_mode" },
  810. #endif
  811. { "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},
  812. { "psy", "Use psychovisual optimizations.", OFFSET(psy), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
  813. { "psy-rd", "Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.", OFFSET(psy_rd), AV_OPT_TYPE_STRING, {0 }, 0, 0, VE},
  814. { "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 },
  815. { "weightb", "Weighted prediction for B-frames.", OFFSET(weightb), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
  816. { "weightp", "Weighted prediction analysis method.", OFFSET(weightp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "weightp" },
  817. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX, VE, "weightp" },
  818. { "simple", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX, VE, "weightp" },
  819. { "smart", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX, VE, "weightp" },
  820. { "ssim", "Calculate and print SSIM stats.", OFFSET(ssim), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
  821. { "intra-refresh", "Use Periodic Intra Refresh instead of IDR frames.",OFFSET(intra_refresh),AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
  822. { "bluray-compat", "Bluray compatibility workarounds.", OFFSET(bluray_compat) ,AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
  823. { "b-bias", "Influences how often B-frames are used", OFFSET(b_bias), AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX, VE },
  824. { "b-pyramid", "Keep some B-frames as references.", OFFSET(b_pyramid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "b_pyramid" },
  825. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  826. { "strict", "Strictly hierarchical pyramid", 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  827. { "normal", "Non-strict (not Blu-ray compatible)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  828. { "mixed-refs", "One reference per partition, as opposed to one reference per macroblock", OFFSET(mixed_refs), AV_OPT_TYPE_BOOL, { .i64 = -1}, -1, 1, VE },
  829. { "8x8dct", "High profile 8x8 transform.", OFFSET(dct8x8), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE},
  830. { "fast-pskip", NULL, OFFSET(fast_pskip), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE},
  831. { "aud", "Use access unit delimiters.", OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE},
  832. { "mbtree", "Use macroblock tree ratecontrol.", OFFSET(mbtree), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE},
  833. { "deblock", "Loop filter parameters, in <alpha:beta> form.", OFFSET(deblock), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  834. { "cplxblur", "Reduce fluctuations in QP (before curve compression)", OFFSET(cplxblur), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE},
  835. { "partitions", "A comma-separated list of partitions to consider. "
  836. "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all", OFFSET(partitions), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  837. { "direct-pred", "Direct MV prediction mode", OFFSET(direct_pred), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "direct-pred" },
  838. { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0, VE, "direct-pred" },
  839. { "spatial", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0, VE, "direct-pred" },
  840. { "temporal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0, VE, "direct-pred" },
  841. { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0, VE, "direct-pred" },
  842. { "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 },
  843. { "stats", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  844. { "nal-hrd", "Signal HRD information (requires vbv-bufsize; "
  845. "cbr not allowed in .mp4)", OFFSET(nal_hrd), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "nal-hrd" },
  846. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX, VE, "nal-hrd" },
  847. { "vbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX, VE, "nal-hrd" },
  848. { "cbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX, VE, "nal-hrd" },
  849. { "avcintra-class","AVC-Intra class 50/100/200", OFFSET(avcintra_class),AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 200 , VE},
  850. { "motion-est", "Set motion estimation method", OFFSET(motion_est), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, X264_ME_TESA, VE, "motion-est"},
  851. { "dia", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_DIA }, INT_MIN, INT_MAX, VE, "motion-est" },
  852. { "hex", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_HEX }, INT_MIN, INT_MAX, VE, "motion-est" },
  853. { "umh", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_UMH }, INT_MIN, INT_MAX, VE, "motion-est" },
  854. { "esa", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_ESA }, INT_MIN, INT_MAX, VE, "motion-est" },
  855. { "tesa", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_TESA }, INT_MIN, INT_MAX, VE, "motion-est" },
  856. { "forced-idr", "If forcing keyframes, force them as IDR frames.", OFFSET(forced_idr), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },
  857. { "coder", "Coder type", OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE, "coder" },
  858. { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, VE, "coder" },
  859. { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "coder" },
  860. { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "coder" },
  861. { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "coder" },
  862. { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "coder" },
  863. { "b_strategy", "Strategy to choose between I/P/B-frames", OFFSET(b_frame_strategy), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, VE },
  864. { "chromaoffset", "QP difference between chroma and luma", OFFSET(chroma_offset), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE },
  865. { "sc_threshold", "Scene change threshold", OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE },
  866. { "noise_reduction", "Noise reduction", OFFSET(noise_reduction), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE },
  867. { "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 },
  868. { NULL },
  869. };
  870. static const AVCodecDefault x264_defaults[] = {
  871. { "b", "0" },
  872. { "bf", "-1" },
  873. { "flags2", "0" },
  874. { "g", "-1" },
  875. { "i_qfactor", "-1" },
  876. { "b_qfactor", "-1" },
  877. { "qmin", "-1" },
  878. { "qmax", "-1" },
  879. { "qdiff", "-1" },
  880. { "qblur", "-1" },
  881. { "qcomp", "-1" },
  882. // { "rc_lookahead", "-1" },
  883. { "refs", "-1" },
  884. #if FF_API_PRIVATE_OPT
  885. { "sc_threshold", "-1" },
  886. #endif
  887. { "trellis", "-1" },
  888. #if FF_API_PRIVATE_OPT
  889. { "nr", "-1" },
  890. #endif
  891. { "me_range", "-1" },
  892. #if FF_API_MOTION_EST
  893. { "me_method", "-1" },
  894. #endif
  895. { "subq", "-1" },
  896. #if FF_API_PRIVATE_OPT
  897. { "b_strategy", "-1" },
  898. #endif
  899. { "keyint_min", "-1" },
  900. #if FF_API_CODER_TYPE
  901. { "coder", "-1" },
  902. #endif
  903. { "cmp", "-1" },
  904. { "threads", AV_STRINGIFY(X264_THREADS_AUTO) },
  905. { "thread_type", "0" },
  906. { "flags", "+cgop" },
  907. { "rc_init_occupancy","-1" },
  908. { NULL },
  909. };
  910. #if CONFIG_LIBX264_ENCODER
  911. static const AVClass x264_class = {
  912. .class_name = "libx264",
  913. .item_name = av_default_item_name,
  914. .option = options,
  915. .version = LIBAVUTIL_VERSION_INT,
  916. };
  917. AVCodec ff_libx264_encoder = {
  918. .name = "libx264",
  919. .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  920. .type = AVMEDIA_TYPE_VIDEO,
  921. .id = AV_CODEC_ID_H264,
  922. .priv_data_size = sizeof(X264Context),
  923. .init = X264_init,
  924. .encode2 = X264_frame,
  925. .close = X264_close,
  926. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  927. .priv_class = &x264_class,
  928. .defaults = x264_defaults,
  929. .init_static_data = X264_init_static,
  930. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  931. FF_CODEC_CAP_INIT_CLEANUP,
  932. };
  933. #endif
  934. #if CONFIG_LIBX264RGB_ENCODER
  935. static const AVClass rgbclass = {
  936. .class_name = "libx264rgb",
  937. .item_name = av_default_item_name,
  938. .option = options,
  939. .version = LIBAVUTIL_VERSION_INT,
  940. };
  941. AVCodec ff_libx264rgb_encoder = {
  942. .name = "libx264rgb",
  943. .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB"),
  944. .type = AVMEDIA_TYPE_VIDEO,
  945. .id = AV_CODEC_ID_H264,
  946. .priv_data_size = sizeof(X264Context),
  947. .init = X264_init,
  948. .encode2 = X264_frame,
  949. .close = X264_close,
  950. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  951. .priv_class = &rgbclass,
  952. .defaults = x264_defaults,
  953. .pix_fmts = pix_fmts_8bit_rgb,
  954. };
  955. #endif
  956. #if CONFIG_LIBX262_ENCODER
  957. static const AVClass X262_class = {
  958. .class_name = "libx262",
  959. .item_name = av_default_item_name,
  960. .option = options,
  961. .version = LIBAVUTIL_VERSION_INT,
  962. };
  963. AVCodec ff_libx262_encoder = {
  964. .name = "libx262",
  965. .long_name = NULL_IF_CONFIG_SMALL("libx262 MPEG2VIDEO"),
  966. .type = AVMEDIA_TYPE_VIDEO,
  967. .id = AV_CODEC_ID_MPEG2VIDEO,
  968. .priv_data_size = sizeof(X264Context),
  969. .init = X264_init,
  970. .encode2 = X264_frame,
  971. .close = X264_close,
  972. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  973. .priv_class = &X262_class,
  974. .defaults = x264_defaults,
  975. .pix_fmts = pix_fmts_8bit,
  976. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  977. FF_CODEC_CAP_INIT_CLEANUP,
  978. };
  979. #endif