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.

1217 lines
47KB

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