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.

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