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.

988 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. #ifdef X264_CSP_NV21
  335. case AV_PIX_FMT_NV21: return X264_CSP_NV21;
  336. #endif
  337. };
  338. return 0;
  339. }
  340. #define PARSE_X264_OPT(name, var)\
  341. if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
  342. av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
  343. return AVERROR(EINVAL);\
  344. }
  345. static av_cold int X264_init(AVCodecContext *avctx)
  346. {
  347. X264Context *x4 = avctx->priv_data;
  348. int sw,sh;
  349. if (avctx->global_quality > 0)
  350. av_log(avctx, AV_LOG_WARNING, "-qscale is ignored, -crf is recommended.\n");
  351. #if CONFIG_LIBX262_ENCODER
  352. if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  353. x4->params.b_mpeg2 = 1;
  354. x264_param_default_mpeg2(&x4->params);
  355. } else
  356. #endif
  357. x264_param_default(&x4->params);
  358. x4->params.b_deblocking_filter = avctx->flags & AV_CODEC_FLAG_LOOP_FILTER;
  359. if (x4->preset || x4->tune)
  360. if (x264_param_default_preset(&x4->params, x4->preset, x4->tune) < 0) {
  361. int i;
  362. av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", x4->preset, x4->tune);
  363. av_log(avctx, AV_LOG_INFO, "Possible presets:");
  364. for (i = 0; x264_preset_names[i]; i++)
  365. av_log(avctx, AV_LOG_INFO, " %s", x264_preset_names[i]);
  366. av_log(avctx, AV_LOG_INFO, "\n");
  367. av_log(avctx, AV_LOG_INFO, "Possible tunes:");
  368. for (i = 0; x264_tune_names[i]; i++)
  369. av_log(avctx, AV_LOG_INFO, " %s", x264_tune_names[i]);
  370. av_log(avctx, AV_LOG_INFO, "\n");
  371. return AVERROR(EINVAL);
  372. }
  373. if (avctx->level > 0)
  374. x4->params.i_level_idc = avctx->level;
  375. x4->params.pf_log = X264_log;
  376. x4->params.p_log_private = avctx;
  377. x4->params.i_log_level = X264_LOG_DEBUG;
  378. x4->params.i_csp = convert_pix_fmt(avctx->pix_fmt);
  379. OPT_STR("weightp", x4->wpredp);
  380. if (avctx->bit_rate) {
  381. x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
  382. x4->params.rc.i_rc_method = X264_RC_ABR;
  383. }
  384. x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
  385. x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000;
  386. x4->params.rc.b_stat_write = avctx->flags & AV_CODEC_FLAG_PASS1;
  387. if (avctx->flags & AV_CODEC_FLAG_PASS2) {
  388. x4->params.rc.b_stat_read = 1;
  389. } else {
  390. if (x4->crf >= 0) {
  391. x4->params.rc.i_rc_method = X264_RC_CRF;
  392. x4->params.rc.f_rf_constant = x4->crf;
  393. } else if (x4->cqp >= 0) {
  394. x4->params.rc.i_rc_method = X264_RC_CQP;
  395. x4->params.rc.i_qp_constant = x4->cqp;
  396. }
  397. if (x4->crf_max >= 0)
  398. x4->params.rc.f_rf_constant_max = x4->crf_max;
  399. }
  400. if (avctx->rc_buffer_size && avctx->rc_initial_buffer_occupancy > 0 &&
  401. (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
  402. x4->params.rc.f_vbv_buffer_init =
  403. (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
  404. }
  405. OPT_STR("level", x4->level);
  406. if (avctx->i_quant_factor > 0)
  407. x4->params.rc.f_ip_factor = 1 / fabs(avctx->i_quant_factor);
  408. if (avctx->b_quant_factor > 0)
  409. x4->params.rc.f_pb_factor = avctx->b_quant_factor;
  410. if (avctx->chromaoffset)
  411. x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
  412. if (avctx->gop_size >= 0)
  413. x4->params.i_keyint_max = avctx->gop_size;
  414. if (avctx->max_b_frames >= 0)
  415. x4->params.i_bframe = avctx->max_b_frames;
  416. if (avctx->scenechange_threshold >= 0)
  417. x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
  418. if (avctx->qmin >= 0)
  419. x4->params.rc.i_qp_min = avctx->qmin;
  420. if (avctx->qmax >= 0)
  421. x4->params.rc.i_qp_max = avctx->qmax;
  422. if (avctx->max_qdiff >= 0)
  423. x4->params.rc.i_qp_step = avctx->max_qdiff;
  424. if (avctx->qblur >= 0)
  425. x4->params.rc.f_qblur = avctx->qblur; /* temporally blur quants */
  426. if (avctx->qcompress >= 0)
  427. x4->params.rc.f_qcompress = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
  428. if (avctx->refs >= 0)
  429. x4->params.i_frame_reference = avctx->refs;
  430. else if (x4->level) {
  431. int i;
  432. int mbn = FF_CEIL_RSHIFT(avctx->width, 4) * FF_CEIL_RSHIFT(avctx->height, 4);
  433. int level_id = -1;
  434. char *tail;
  435. int scale = X264_BUILD < 129 ? 384 : 1;
  436. if (!strcmp(x4->level, "1b")) {
  437. level_id = 9;
  438. } else if (strlen(x4->level) <= 3){
  439. level_id = av_strtod(x4->level, &tail) * 10 + 0.5;
  440. if (*tail)
  441. level_id = -1;
  442. }
  443. if (level_id <= 0)
  444. av_log(avctx, AV_LOG_WARNING, "Failed to parse level\n");
  445. for (i = 0; i<x264_levels[i].level_idc; i++)
  446. if (x264_levels[i].level_idc == level_id)
  447. x4->params.i_frame_reference = av_clip(x264_levels[i].dpb / mbn / scale, 1, x4->params.i_frame_reference);
  448. }
  449. if (avctx->trellis >= 0)
  450. x4->params.analyse.i_trellis = avctx->trellis;
  451. if (avctx->me_range >= 0)
  452. x4->params.analyse.i_me_range = avctx->me_range;
  453. if (avctx->noise_reduction >= 0)
  454. x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
  455. if (avctx->me_subpel_quality >= 0)
  456. x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
  457. if (avctx->b_frame_strategy >= 0)
  458. x4->params.i_bframe_adaptive = avctx->b_frame_strategy;
  459. if (avctx->keyint_min >= 0)
  460. x4->params.i_keyint_min = avctx->keyint_min;
  461. if (avctx->coder_type >= 0)
  462. x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
  463. if (avctx->me_cmp >= 0)
  464. x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA;
  465. if (x4->aq_mode >= 0)
  466. x4->params.rc.i_aq_mode = x4->aq_mode;
  467. if (x4->aq_strength >= 0)
  468. x4->params.rc.f_aq_strength = x4->aq_strength;
  469. PARSE_X264_OPT("psy-rd", psy_rd);
  470. PARSE_X264_OPT("deblock", deblock);
  471. PARSE_X264_OPT("partitions", partitions);
  472. PARSE_X264_OPT("stats", stats);
  473. if (x4->psy >= 0)
  474. x4->params.analyse.b_psy = x4->psy;
  475. if (x4->rc_lookahead >= 0)
  476. x4->params.rc.i_lookahead = x4->rc_lookahead;
  477. if (x4->weightp >= 0)
  478. x4->params.analyse.i_weighted_pred = x4->weightp;
  479. if (x4->weightb >= 0)
  480. x4->params.analyse.b_weighted_bipred = x4->weightb;
  481. if (x4->cplxblur >= 0)
  482. x4->params.rc.f_complexity_blur = x4->cplxblur;
  483. if (x4->ssim >= 0)
  484. x4->params.analyse.b_ssim = x4->ssim;
  485. if (x4->intra_refresh >= 0)
  486. x4->params.b_intra_refresh = x4->intra_refresh;
  487. if (x4->bluray_compat >= 0) {
  488. x4->params.b_bluray_compat = x4->bluray_compat;
  489. x4->params.b_vfr_input = 0;
  490. }
  491. if (x4->avcintra_class >= 0)
  492. #if X264_BUILD >= 142
  493. x4->params.i_avcintra_class = x4->avcintra_class;
  494. #else
  495. av_log(avctx, AV_LOG_ERROR,
  496. "x264 too old for AVC Intra, at least version 142 needed\n");
  497. #endif
  498. if (x4->b_bias != INT_MIN)
  499. x4->params.i_bframe_bias = x4->b_bias;
  500. if (x4->b_pyramid >= 0)
  501. x4->params.i_bframe_pyramid = x4->b_pyramid;
  502. if (x4->mixed_refs >= 0)
  503. x4->params.analyse.b_mixed_references = x4->mixed_refs;
  504. if (x4->dct8x8 >= 0)
  505. x4->params.analyse.b_transform_8x8 = x4->dct8x8;
  506. if (x4->fast_pskip >= 0)
  507. x4->params.analyse.b_fast_pskip = x4->fast_pskip;
  508. if (x4->aud >= 0)
  509. x4->params.b_aud = x4->aud;
  510. if (x4->mbtree >= 0)
  511. x4->params.rc.b_mb_tree = x4->mbtree;
  512. if (x4->direct_pred >= 0)
  513. x4->params.analyse.i_direct_mv_pred = x4->direct_pred;
  514. if (x4->slice_max_size >= 0)
  515. x4->params.i_slice_max_size = x4->slice_max_size;
  516. else {
  517. /*
  518. * Allow x264 to be instructed through AVCodecContext about the maximum
  519. * size of the RTP payload. For example, this enables the production of
  520. * payload suitable for the H.264 RTP packetization-mode 0 i.e. single
  521. * NAL unit per RTP packet.
  522. */
  523. if (avctx->rtp_payload_size)
  524. x4->params.i_slice_max_size = avctx->rtp_payload_size;
  525. }
  526. if (x4->fastfirstpass)
  527. x264_param_apply_fastfirstpass(&x4->params);
  528. /* Allow specifying the x264 profile through AVCodecContext. */
  529. if (!x4->profile)
  530. switch (avctx->profile) {
  531. case FF_PROFILE_H264_BASELINE:
  532. x4->profile = av_strdup("baseline");
  533. break;
  534. case FF_PROFILE_H264_HIGH:
  535. x4->profile = av_strdup("high");
  536. break;
  537. case FF_PROFILE_H264_HIGH_10:
  538. x4->profile = av_strdup("high10");
  539. break;
  540. case FF_PROFILE_H264_HIGH_422:
  541. x4->profile = av_strdup("high422");
  542. break;
  543. case FF_PROFILE_H264_HIGH_444:
  544. x4->profile = av_strdup("high444");
  545. break;
  546. case FF_PROFILE_H264_MAIN:
  547. x4->profile = av_strdup("main");
  548. break;
  549. default:
  550. break;
  551. }
  552. if (x4->nal_hrd >= 0)
  553. x4->params.i_nal_hrd = x4->nal_hrd;
  554. if (x4->motion_est >= 0) {
  555. x4->params.analyse.i_me_method = x4->motion_est;
  556. #if FF_API_MOTION_EST
  557. FF_DISABLE_DEPRECATION_WARNINGS
  558. } else {
  559. if (avctx->me_method == ME_EPZS)
  560. x4->params.analyse.i_me_method = X264_ME_DIA;
  561. else if (avctx->me_method == ME_HEX)
  562. x4->params.analyse.i_me_method = X264_ME_HEX;
  563. else if (avctx->me_method == ME_UMH)
  564. x4->params.analyse.i_me_method = X264_ME_UMH;
  565. else if (avctx->me_method == ME_FULL)
  566. x4->params.analyse.i_me_method = X264_ME_ESA;
  567. else if (avctx->me_method == ME_TESA)
  568. x4->params.analyse.i_me_method = X264_ME_TESA;
  569. FF_ENABLE_DEPRECATION_WARNINGS
  570. #endif
  571. }
  572. if (x4->profile)
  573. if (x264_param_apply_profile(&x4->params, x4->profile) < 0) {
  574. int i;
  575. av_log(avctx, AV_LOG_ERROR, "Error setting profile %s.\n", x4->profile);
  576. av_log(avctx, AV_LOG_INFO, "Possible profiles:");
  577. for (i = 0; x264_profile_names[i]; i++)
  578. av_log(avctx, AV_LOG_INFO, " %s", x264_profile_names[i]);
  579. av_log(avctx, AV_LOG_INFO, "\n");
  580. return AVERROR(EINVAL);
  581. }
  582. x4->params.i_width = avctx->width;
  583. x4->params.i_height = avctx->height;
  584. av_reduce(&sw, &sh, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 4096);
  585. x4->params.vui.i_sar_width = sw;
  586. x4->params.vui.i_sar_height = sh;
  587. x4->params.i_timebase_den = avctx->time_base.den;
  588. x4->params.i_timebase_num = avctx->time_base.num;
  589. x4->params.i_fps_num = avctx->time_base.den;
  590. x4->params.i_fps_den = avctx->time_base.num * avctx->ticks_per_frame;
  591. x4->params.analyse.b_psnr = avctx->flags & AV_CODEC_FLAG_PSNR;
  592. x4->params.i_threads = avctx->thread_count;
  593. if (avctx->thread_type)
  594. x4->params.b_sliced_threads = avctx->thread_type == FF_THREAD_SLICE;
  595. x4->params.b_interlaced = avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT;
  596. x4->params.b_open_gop = !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP);
  597. x4->params.i_slice_count = avctx->slices;
  598. x4->params.vui.b_fullrange = avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
  599. avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
  600. avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
  601. avctx->color_range == AVCOL_RANGE_JPEG;
  602. if (avctx->colorspace != AVCOL_SPC_UNSPECIFIED)
  603. x4->params.vui.i_colmatrix = avctx->colorspace;
  604. if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED)
  605. x4->params.vui.i_colorprim = avctx->color_primaries;
  606. if (avctx->color_trc != AVCOL_TRC_UNSPECIFIED)
  607. x4->params.vui.i_transfer = avctx->color_trc;
  608. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)
  609. x4->params.b_repeat_headers = 0;
  610. if(x4->x264opts){
  611. const char *p= x4->x264opts;
  612. while(p){
  613. char param[256]={0}, val[256]={0};
  614. if(sscanf(p, "%255[^:=]=%255[^:]", param, val) == 1){
  615. OPT_STR(param, "1");
  616. }else
  617. OPT_STR(param, val);
  618. p= strchr(p, ':');
  619. p+=!!p;
  620. }
  621. }
  622. if (x4->x264_params) {
  623. AVDictionary *dict = NULL;
  624. AVDictionaryEntry *en = NULL;
  625. if (!av_dict_parse_string(&dict, x4->x264_params, "=", ":", 0)) {
  626. while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
  627. if (x264_param_parse(&x4->params, en->key, en->value) < 0)
  628. av_log(avctx, AV_LOG_WARNING,
  629. "Error parsing option '%s = %s'.\n",
  630. en->key, en->value);
  631. }
  632. av_dict_free(&dict);
  633. }
  634. }
  635. // update AVCodecContext with x264 parameters
  636. avctx->has_b_frames = x4->params.i_bframe ?
  637. x4->params.i_bframe_pyramid ? 2 : 1 : 0;
  638. if (avctx->max_b_frames < 0)
  639. avctx->max_b_frames = 0;
  640. avctx->bit_rate = x4->params.rc.i_bitrate*1000;
  641. x4->enc = x264_encoder_open(&x4->params);
  642. if (!x4->enc)
  643. return AVERROR_EXTERNAL;
  644. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  645. x264_nal_t *nal;
  646. uint8_t *p;
  647. int nnal, s, i;
  648. s = x264_encoder_headers(x4->enc, &nal, &nnal);
  649. avctx->extradata = p = av_malloc(s);
  650. if (!p)
  651. return AVERROR(ENOMEM);
  652. for (i = 0; i < nnal; i++) {
  653. /* Don't put the SEI in extradata. */
  654. if (nal[i].i_type == NAL_SEI) {
  655. av_log(avctx, AV_LOG_INFO, "%s\n", nal[i].p_payload+25);
  656. x4->sei_size = nal[i].i_payload;
  657. x4->sei = av_malloc(x4->sei_size);
  658. if (!x4->sei)
  659. return AVERROR(ENOMEM);
  660. memcpy(x4->sei, nal[i].p_payload, nal[i].i_payload);
  661. continue;
  662. }
  663. memcpy(p, nal[i].p_payload, nal[i].i_payload);
  664. p += nal[i].i_payload;
  665. }
  666. avctx->extradata_size = p - avctx->extradata;
  667. }
  668. return 0;
  669. }
  670. static const enum AVPixelFormat pix_fmts_8bit[] = {
  671. AV_PIX_FMT_YUV420P,
  672. AV_PIX_FMT_YUVJ420P,
  673. AV_PIX_FMT_YUV422P,
  674. AV_PIX_FMT_YUVJ422P,
  675. AV_PIX_FMT_YUV444P,
  676. AV_PIX_FMT_YUVJ444P,
  677. AV_PIX_FMT_NV12,
  678. AV_PIX_FMT_NV16,
  679. #ifdef X264_CSP_NV21
  680. AV_PIX_FMT_NV21,
  681. #endif
  682. AV_PIX_FMT_NONE
  683. };
  684. static const enum AVPixelFormat pix_fmts_9bit[] = {
  685. AV_PIX_FMT_YUV420P9,
  686. AV_PIX_FMT_YUV444P9,
  687. AV_PIX_FMT_NONE
  688. };
  689. static const enum AVPixelFormat pix_fmts_10bit[] = {
  690. AV_PIX_FMT_YUV420P10,
  691. AV_PIX_FMT_YUV422P10,
  692. AV_PIX_FMT_YUV444P10,
  693. AV_PIX_FMT_NV20,
  694. AV_PIX_FMT_NONE
  695. };
  696. static const enum AVPixelFormat pix_fmts_8bit_rgb[] = {
  697. #ifdef X264_CSP_BGR
  698. AV_PIX_FMT_BGR0,
  699. AV_PIX_FMT_BGR24,
  700. AV_PIX_FMT_RGB24,
  701. #endif
  702. AV_PIX_FMT_NONE
  703. };
  704. static av_cold void X264_init_static(AVCodec *codec)
  705. {
  706. if (x264_bit_depth == 8)
  707. codec->pix_fmts = pix_fmts_8bit;
  708. else if (x264_bit_depth == 9)
  709. codec->pix_fmts = pix_fmts_9bit;
  710. else if (x264_bit_depth == 10)
  711. codec->pix_fmts = pix_fmts_10bit;
  712. }
  713. #define OFFSET(x) offsetof(X264Context, x)
  714. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  715. static const AVOption options[] = {
  716. { "preset", "Set the encoding preset (cf. x264 --fullhelp)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},
  717. { "tune", "Tune the encoding params (cf. x264 --fullhelp)", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  718. { "profile", "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  719. { "fastfirstpass", "Use fast settings when encoding first pass", OFFSET(fastfirstpass), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE},
  720. {"level", "Specify level (as defined by Annex A)", OFFSET(level), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
  721. {"passlogfile", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
  722. {"wpredp", "Weighted prediction for P-frames", OFFSET(wpredp), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
  723. {"x264opts", "x264 options", OFFSET(x264opts), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
  724. { "crf", "Select the quality for constant quality mode", OFFSET(crf), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE },
  725. { "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 },
  726. { "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },
  727. { "aq-mode", "AQ method", OFFSET(aq_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "aq_mode"},
  728. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  729. { "variance", "Variance AQ (complexity mask)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  730. { "autovariance", "Auto-variance AQ", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  731. #if X264_BUILD >= 144
  732. { "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" },
  733. #endif
  734. { "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},
  735. { "psy", "Use psychovisual optimizations.", OFFSET(psy), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  736. { "psy-rd", "Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.", OFFSET(psy_rd), AV_OPT_TYPE_STRING, {0 }, 0, 0, VE},
  737. { "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 },
  738. { "weightb", "Weighted prediction for B-frames.", OFFSET(weightb), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  739. { "weightp", "Weighted prediction analysis method.", OFFSET(weightp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "weightp" },
  740. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX, VE, "weightp" },
  741. { "simple", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX, VE, "weightp" },
  742. { "smart", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX, VE, "weightp" },
  743. { "ssim", "Calculate and print SSIM stats.", OFFSET(ssim), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  744. { "intra-refresh", "Use Periodic Intra Refresh instead of IDR frames.",OFFSET(intra_refresh),AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  745. { "bluray-compat", "Bluray compatibility workarounds.", OFFSET(bluray_compat) ,AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  746. { "b-bias", "Influences how often B-frames are used", OFFSET(b_bias), AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX, VE },
  747. { "b-pyramid", "Keep some B-frames as references.", OFFSET(b_pyramid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "b_pyramid" },
  748. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  749. { "strict", "Strictly hierarchical pyramid", 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  750. { "normal", "Non-strict (not Blu-ray compatible)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  751. { "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 },
  752. { "8x8dct", "High profile 8x8 transform.", OFFSET(dct8x8), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
  753. { "fast-pskip", NULL, OFFSET(fast_pskip), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
  754. { "aud", "Use access unit delimiters.", OFFSET(aud), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
  755. { "mbtree", "Use macroblock tree ratecontrol.", OFFSET(mbtree), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
  756. { "deblock", "Loop filter parameters, in <alpha:beta> form.", OFFSET(deblock), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  757. { "cplxblur", "Reduce fluctuations in QP (before curve compression)", OFFSET(cplxblur), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE},
  758. { "partitions", "A comma-separated list of partitions to consider. "
  759. "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all", OFFSET(partitions), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  760. { "direct-pred", "Direct MV prediction mode", OFFSET(direct_pred), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "direct-pred" },
  761. { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0, VE, "direct-pred" },
  762. { "spatial", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0, VE, "direct-pred" },
  763. { "temporal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0, VE, "direct-pred" },
  764. { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0, VE, "direct-pred" },
  765. { "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 },
  766. { "stats", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  767. { "nal-hrd", "Signal HRD information (requires vbv-bufsize; "
  768. "cbr not allowed in .mp4)", OFFSET(nal_hrd), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "nal-hrd" },
  769. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX, VE, "nal-hrd" },
  770. { "vbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX, VE, "nal-hrd" },
  771. { "cbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX, VE, "nal-hrd" },
  772. { "avcintra-class","AVC-Intra class 50/100/200", OFFSET(avcintra_class),AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 200 , VE},
  773. { "motion-est", "Set motion estimation method", OFFSET(motion_est), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, X264_ME_TESA, VE, "motion-est"},
  774. { "dia", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_DIA }, INT_MIN, INT_MAX, VE, "motion-est" },
  775. { "hex", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_HEX }, INT_MIN, INT_MAX, VE, "motion-est" },
  776. { "umh", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_UMH }, INT_MIN, INT_MAX, VE, "motion-est" },
  777. { "esa", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_ESA }, INT_MIN, INT_MAX, VE, "motion-est" },
  778. { "tesa", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_TESA }, INT_MIN, INT_MAX, VE, "motion-est" },
  779. { "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 },
  780. { NULL },
  781. };
  782. static const AVCodecDefault x264_defaults[] = {
  783. { "b", "0" },
  784. { "bf", "-1" },
  785. { "flags2", "0" },
  786. { "g", "-1" },
  787. { "i_qfactor", "-1" },
  788. { "b_qfactor", "-1" },
  789. { "qmin", "-1" },
  790. { "qmax", "-1" },
  791. { "qdiff", "-1" },
  792. { "qblur", "-1" },
  793. { "qcomp", "-1" },
  794. // { "rc_lookahead", "-1" },
  795. { "refs", "-1" },
  796. { "sc_threshold", "-1" },
  797. { "trellis", "-1" },
  798. { "nr", "-1" },
  799. { "me_range", "-1" },
  800. #if FF_API_MOTION_EST
  801. { "me_method", "-1" },
  802. #endif
  803. { "subq", "-1" },
  804. { "b_strategy", "-1" },
  805. { "keyint_min", "-1" },
  806. { "coder", "-1" },
  807. { "cmp", "-1" },
  808. { "threads", AV_STRINGIFY(X264_THREADS_AUTO) },
  809. { "thread_type", "0" },
  810. { "flags", "+cgop" },
  811. { "rc_init_occupancy","-1" },
  812. { NULL },
  813. };
  814. #if CONFIG_LIBX264_ENCODER
  815. static const AVClass x264_class = {
  816. .class_name = "libx264",
  817. .item_name = av_default_item_name,
  818. .option = options,
  819. .version = LIBAVUTIL_VERSION_INT,
  820. };
  821. static const AVClass rgbclass = {
  822. .class_name = "libx264rgb",
  823. .item_name = av_default_item_name,
  824. .option = options,
  825. .version = LIBAVUTIL_VERSION_INT,
  826. };
  827. AVCodec ff_libx264_encoder = {
  828. .name = "libx264",
  829. .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  830. .type = AVMEDIA_TYPE_VIDEO,
  831. .id = AV_CODEC_ID_H264,
  832. .priv_data_size = sizeof(X264Context),
  833. .init = X264_init,
  834. .encode2 = X264_frame,
  835. .close = X264_close,
  836. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  837. .priv_class = &x264_class,
  838. .defaults = x264_defaults,
  839. .init_static_data = X264_init_static,
  840. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  841. FF_CODEC_CAP_INIT_CLEANUP,
  842. };
  843. AVCodec ff_libx264rgb_encoder = {
  844. .name = "libx264rgb",
  845. .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB"),
  846. .type = AVMEDIA_TYPE_VIDEO,
  847. .id = AV_CODEC_ID_H264,
  848. .priv_data_size = sizeof(X264Context),
  849. .init = X264_init,
  850. .encode2 = X264_frame,
  851. .close = X264_close,
  852. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  853. .priv_class = &rgbclass,
  854. .defaults = x264_defaults,
  855. .pix_fmts = pix_fmts_8bit_rgb,
  856. };
  857. #endif
  858. #if CONFIG_LIBX262_ENCODER
  859. static const AVClass X262_class = {
  860. .class_name = "libx262",
  861. .item_name = av_default_item_name,
  862. .option = options,
  863. .version = LIBAVUTIL_VERSION_INT,
  864. };
  865. AVCodec ff_libx262_encoder = {
  866. .name = "libx262",
  867. .long_name = NULL_IF_CONFIG_SMALL("libx262 MPEG2VIDEO"),
  868. .type = AVMEDIA_TYPE_VIDEO,
  869. .id = AV_CODEC_ID_MPEG2VIDEO,
  870. .priv_data_size = sizeof(X264Context),
  871. .init = X264_init,
  872. .encode2 = X264_frame,
  873. .close = X264_close,
  874. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  875. .priv_class = &X262_class,
  876. .defaults = x264_defaults,
  877. .pix_fmts = pix_fmts_8bit,
  878. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  879. FF_CODEC_CAP_INIT_CLEANUP,
  880. };
  881. #endif