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.

1082 lines
42KB

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