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.

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