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.

1125 lines
43KB

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