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.

982 lines
38KB

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