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.

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