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.

1280 lines
49KB

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