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.

1273 lines
48KB

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