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.

1117 lines
43KB

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