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.

1045 lines
41KB

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