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.

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