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.

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