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.

717 lines
28KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/internal.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/mem.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "libavutil/stereo3d.h"
  26. #include "avcodec.h"
  27. #include "internal.h"
  28. #if defined(_MSC_VER)
  29. #define X264_API_IMPORTS 1
  30. #endif
  31. #include <x264.h>
  32. #include <float.h>
  33. #include <math.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. typedef struct X264Context {
  38. AVClass *class;
  39. x264_param_t params;
  40. x264_t *enc;
  41. x264_picture_t pic;
  42. uint8_t *sei;
  43. int sei_size;
  44. char *preset;
  45. char *tune;
  46. char *profile;
  47. int fastfirstpass;
  48. float crf;
  49. float crf_max;
  50. int cqp;
  51. int aq_mode;
  52. float aq_strength;
  53. char *psy_rd;
  54. int psy;
  55. int rc_lookahead;
  56. int weightp;
  57. int weightb;
  58. int ssim;
  59. int intra_refresh;
  60. int bluray_compat;
  61. int b_bias;
  62. int b_pyramid;
  63. int mixed_refs;
  64. int dct8x8;
  65. int fast_pskip;
  66. int aud;
  67. int mbtree;
  68. char *deblock;
  69. float cplxblur;
  70. char *partitions;
  71. int direct_pred;
  72. int slice_max_size;
  73. char *stats;
  74. int nal_hrd;
  75. char *x264_params;
  76. } X264Context;
  77. static void X264_log(void *p, int level, const char *fmt, va_list args)
  78. {
  79. static const int level_map[] = {
  80. [X264_LOG_ERROR] = AV_LOG_ERROR,
  81. [X264_LOG_WARNING] = AV_LOG_WARNING,
  82. [X264_LOG_INFO] = AV_LOG_INFO,
  83. [X264_LOG_DEBUG] = AV_LOG_DEBUG
  84. };
  85. if (level < 0 || level > X264_LOG_DEBUG)
  86. return;
  87. av_vlog(p, level_map[level], fmt, args);
  88. }
  89. static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
  90. x264_nal_t *nals, int nnal)
  91. {
  92. X264Context *x4 = ctx->priv_data;
  93. uint8_t *p;
  94. int i, size = x4->sei_size, ret;
  95. if (!nnal)
  96. return 0;
  97. for (i = 0; i < nnal; i++)
  98. size += nals[i].i_payload;
  99. if ((ret = ff_alloc_packet(pkt, size)) < 0)
  100. return ret;
  101. p = pkt->data;
  102. /* Write the SEI as part of the first frame. */
  103. if (x4->sei_size > 0 && nnal > 0) {
  104. memcpy(p, x4->sei, x4->sei_size);
  105. p += x4->sei_size;
  106. x4->sei_size = 0;
  107. }
  108. for (i = 0; i < nnal; i++){
  109. memcpy(p, nals[i].p_payload, nals[i].i_payload);
  110. p += nals[i].i_payload;
  111. }
  112. return 1;
  113. }
  114. static void reconfig_encoder(AVCodecContext *ctx, const AVFrame *frame)
  115. {
  116. X264Context *x4 = ctx->priv_data;
  117. AVFrameSideData *side_data;
  118. if (x4->params.b_tff != frame->top_field_first) {
  119. x4->params.b_tff = frame->top_field_first;
  120. x264_encoder_reconfig(x4->enc, &x4->params);
  121. }
  122. if (x4->params.vui.i_sar_height != ctx->sample_aspect_ratio.den ||
  123. x4->params.vui.i_sar_width != ctx->sample_aspect_ratio.num) {
  124. x4->params.vui.i_sar_height = ctx->sample_aspect_ratio.den;
  125. x4->params.vui.i_sar_width = ctx->sample_aspect_ratio.num;
  126. x264_encoder_reconfig(x4->enc, &x4->params);
  127. }
  128. if (x4->params.rc.i_vbv_buffer_size != ctx->rc_buffer_size / 1000 ||
  129. x4->params.rc.i_vbv_max_bitrate != ctx->rc_max_rate / 1000) {
  130. x4->params.rc.i_vbv_buffer_size = ctx->rc_buffer_size / 1000;
  131. x4->params.rc.i_vbv_max_bitrate = ctx->rc_max_rate / 1000;
  132. x264_encoder_reconfig(x4->enc, &x4->params);
  133. }
  134. if (x4->params.rc.i_rc_method == X264_RC_ABR &&
  135. x4->params.rc.i_bitrate != ctx->bit_rate / 1000) {
  136. x4->params.rc.i_bitrate = ctx->bit_rate / 1000;
  137. x264_encoder_reconfig(x4->enc, &x4->params);
  138. }
  139. if (x4->crf >= 0 &&
  140. x4->params.rc.i_rc_method == X264_RC_CRF &&
  141. x4->params.rc.f_rf_constant != x4->crf) {
  142. x4->params.rc.f_rf_constant = x4->crf;
  143. x264_encoder_reconfig(x4->enc, &x4->params);
  144. }
  145. if (x4->params.rc.i_rc_method == X264_RC_CQP &&
  146. x4->params.rc.i_qp_constant != x4->cqp) {
  147. x4->params.rc.i_qp_constant = x4->cqp;
  148. x264_encoder_reconfig(x4->enc, &x4->params);
  149. }
  150. if (x4->crf_max >= 0 &&
  151. x4->params.rc.f_rf_constant_max != x4->crf_max) {
  152. x4->params.rc.f_rf_constant_max = x4->crf_max;
  153. x264_encoder_reconfig(x4->enc, &x4->params);
  154. }
  155. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_STEREO3D);
  156. if (side_data) {
  157. AVStereo3D *stereo = (AVStereo3D *)side_data->data;
  158. int fpa_type;
  159. switch (stereo->type) {
  160. case AV_STEREO3D_CHECKERBOARD:
  161. fpa_type = 0;
  162. break;
  163. case AV_STEREO3D_COLUMNS:
  164. fpa_type = 1;
  165. break;
  166. case AV_STEREO3D_LINES:
  167. fpa_type = 2;
  168. break;
  169. case AV_STEREO3D_SIDEBYSIDE:
  170. fpa_type = 3;
  171. break;
  172. case AV_STEREO3D_TOPBOTTOM:
  173. fpa_type = 4;
  174. break;
  175. case AV_STEREO3D_FRAMESEQUENCE:
  176. fpa_type = 5;
  177. break;
  178. default:
  179. fpa_type = -1;
  180. break;
  181. }
  182. if (fpa_type != x4->params.i_frame_packing) {
  183. x4->params.i_frame_packing = fpa_type;
  184. x264_encoder_reconfig(x4->enc, &x4->params);
  185. }
  186. }
  187. }
  188. static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
  189. int *got_packet)
  190. {
  191. X264Context *x4 = ctx->priv_data;
  192. x264_nal_t *nal;
  193. int nnal, i, ret;
  194. x264_picture_t pic_out;
  195. x264_picture_init( &x4->pic );
  196. x4->pic.img.i_csp = x4->params.i_csp;
  197. if (x264_bit_depth > 8)
  198. x4->pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
  199. x4->pic.img.i_plane = 3;
  200. if (frame) {
  201. for (i = 0; i < 3; i++) {
  202. x4->pic.img.plane[i] = frame->data[i];
  203. x4->pic.img.i_stride[i] = frame->linesize[i];
  204. }
  205. x4->pic.i_pts = frame->pts;
  206. x4->pic.i_type =
  207. frame->pict_type == AV_PICTURE_TYPE_I ? X264_TYPE_KEYFRAME :
  208. frame->pict_type == AV_PICTURE_TYPE_P ? X264_TYPE_P :
  209. frame->pict_type == AV_PICTURE_TYPE_B ? X264_TYPE_B :
  210. X264_TYPE_AUTO;
  211. reconfig_encoder(ctx, frame);
  212. }
  213. do {
  214. if (x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, &pic_out) < 0)
  215. return AVERROR_UNKNOWN;
  216. ret = encode_nals(ctx, pkt, nal, nnal);
  217. if (ret < 0)
  218. return ret;
  219. } while (!ret && !frame && x264_encoder_delayed_frames(x4->enc));
  220. pkt->pts = pic_out.i_pts;
  221. pkt->dts = pic_out.i_dts;
  222. switch (pic_out.i_type) {
  223. case X264_TYPE_IDR:
  224. case X264_TYPE_I:
  225. ctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  226. break;
  227. case X264_TYPE_P:
  228. ctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
  229. break;
  230. case X264_TYPE_B:
  231. case X264_TYPE_BREF:
  232. ctx->coded_frame->pict_type = AV_PICTURE_TYPE_B;
  233. break;
  234. }
  235. pkt->flags |= AV_PKT_FLAG_KEY*pic_out.b_keyframe;
  236. if (ret)
  237. ctx->coded_frame->quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
  238. *got_packet = ret;
  239. return 0;
  240. }
  241. static av_cold int X264_close(AVCodecContext *avctx)
  242. {
  243. X264Context *x4 = avctx->priv_data;
  244. av_freep(&avctx->extradata);
  245. av_freep(&x4->sei);
  246. if (x4->enc) {
  247. x264_encoder_close(x4->enc);
  248. x4->enc = NULL;
  249. }
  250. av_frame_free(&avctx->coded_frame);
  251. return 0;
  252. }
  253. static int convert_pix_fmt(enum AVPixelFormat pix_fmt)
  254. {
  255. switch (pix_fmt) {
  256. case AV_PIX_FMT_YUV420P:
  257. case AV_PIX_FMT_YUVJ420P:
  258. case AV_PIX_FMT_YUV420P9:
  259. case AV_PIX_FMT_YUV420P10: return X264_CSP_I420;
  260. case AV_PIX_FMT_YUV422P:
  261. case AV_PIX_FMT_YUVJ422P:
  262. case AV_PIX_FMT_YUV422P10: return X264_CSP_I422;
  263. case AV_PIX_FMT_YUV444P:
  264. case AV_PIX_FMT_YUVJ444P:
  265. case AV_PIX_FMT_YUV444P9:
  266. case AV_PIX_FMT_YUV444P10: return X264_CSP_I444;
  267. case AV_PIX_FMT_NV12: return X264_CSP_NV12;
  268. case AV_PIX_FMT_NV16:
  269. case AV_PIX_FMT_NV20: return X264_CSP_NV16;
  270. };
  271. return 0;
  272. }
  273. #define PARSE_X264_OPT(name, var)\
  274. if (x4->var && x264_param_parse(&x4->params, name, x4->var) < 0) {\
  275. av_log(avctx, AV_LOG_ERROR, "Error parsing option '%s' with value '%s'.\n", name, x4->var);\
  276. return AVERROR(EINVAL);\
  277. }
  278. static av_cold int X264_init(AVCodecContext *avctx)
  279. {
  280. X264Context *x4 = avctx->priv_data;
  281. x264_param_default(&x4->params);
  282. x4->params.b_deblocking_filter = avctx->flags & CODEC_FLAG_LOOP_FILTER;
  283. if (x4->preset || x4->tune)
  284. if (x264_param_default_preset(&x4->params, x4->preset, x4->tune) < 0) {
  285. av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", x4->preset, x4->tune);
  286. return AVERROR(EINVAL);
  287. }
  288. if (avctx->level > 0)
  289. x4->params.i_level_idc = avctx->level;
  290. x4->params.pf_log = X264_log;
  291. x4->params.p_log_private = avctx;
  292. x4->params.i_log_level = X264_LOG_DEBUG;
  293. x4->params.i_csp = convert_pix_fmt(avctx->pix_fmt);
  294. if (avctx->bit_rate) {
  295. x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
  296. x4->params.rc.i_rc_method = X264_RC_ABR;
  297. }
  298. x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
  299. x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000;
  300. x4->params.rc.b_stat_write = avctx->flags & CODEC_FLAG_PASS1;
  301. if (avctx->flags & CODEC_FLAG_PASS2) {
  302. x4->params.rc.b_stat_read = 1;
  303. } else {
  304. if (x4->crf >= 0) {
  305. x4->params.rc.i_rc_method = X264_RC_CRF;
  306. x4->params.rc.f_rf_constant = x4->crf;
  307. } else if (x4->cqp >= 0) {
  308. x4->params.rc.i_rc_method = X264_RC_CQP;
  309. x4->params.rc.i_qp_constant = x4->cqp;
  310. }
  311. if (x4->crf_max >= 0)
  312. x4->params.rc.f_rf_constant_max = x4->crf_max;
  313. }
  314. if (avctx->rc_buffer_size && avctx->rc_initial_buffer_occupancy > 0 &&
  315. (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
  316. x4->params.rc.f_vbv_buffer_init =
  317. (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
  318. }
  319. if (avctx->i_quant_factor > 0)
  320. x4->params.rc.f_ip_factor = 1 / fabs(avctx->i_quant_factor);
  321. x4->params.rc.f_pb_factor = avctx->b_quant_factor;
  322. x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
  323. if (avctx->me_method == ME_EPZS)
  324. x4->params.analyse.i_me_method = X264_ME_DIA;
  325. else if (avctx->me_method == ME_HEX)
  326. x4->params.analyse.i_me_method = X264_ME_HEX;
  327. else if (avctx->me_method == ME_UMH)
  328. x4->params.analyse.i_me_method = X264_ME_UMH;
  329. else if (avctx->me_method == ME_FULL)
  330. x4->params.analyse.i_me_method = X264_ME_ESA;
  331. else if (avctx->me_method == ME_TESA)
  332. x4->params.analyse.i_me_method = X264_ME_TESA;
  333. if (avctx->gop_size >= 0)
  334. x4->params.i_keyint_max = avctx->gop_size;
  335. if (avctx->max_b_frames >= 0)
  336. x4->params.i_bframe = avctx->max_b_frames;
  337. if (avctx->scenechange_threshold >= 0)
  338. x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
  339. if (avctx->qmin >= 0)
  340. x4->params.rc.i_qp_min = avctx->qmin;
  341. if (avctx->qmax >= 0)
  342. x4->params.rc.i_qp_max = avctx->qmax;
  343. if (avctx->max_qdiff >= 0)
  344. x4->params.rc.i_qp_step = avctx->max_qdiff;
  345. if (avctx->qblur >= 0)
  346. x4->params.rc.f_qblur = avctx->qblur; /* temporally blur quants */
  347. if (avctx->qcompress >= 0)
  348. x4->params.rc.f_qcompress = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
  349. if (avctx->refs >= 0)
  350. x4->params.i_frame_reference = avctx->refs;
  351. if (avctx->trellis >= 0)
  352. x4->params.analyse.i_trellis = avctx->trellis;
  353. if (avctx->me_range >= 0)
  354. x4->params.analyse.i_me_range = avctx->me_range;
  355. if (avctx->noise_reduction >= 0)
  356. x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
  357. if (avctx->me_subpel_quality >= 0)
  358. x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
  359. if (avctx->b_frame_strategy >= 0)
  360. x4->params.i_bframe_adaptive = avctx->b_frame_strategy;
  361. if (avctx->keyint_min >= 0)
  362. x4->params.i_keyint_min = avctx->keyint_min;
  363. if (avctx->coder_type >= 0)
  364. x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
  365. if (avctx->me_cmp >= 0)
  366. x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA;
  367. if (x4->aq_mode >= 0)
  368. x4->params.rc.i_aq_mode = x4->aq_mode;
  369. if (x4->aq_strength >= 0)
  370. x4->params.rc.f_aq_strength = x4->aq_strength;
  371. PARSE_X264_OPT("psy-rd", psy_rd);
  372. PARSE_X264_OPT("deblock", deblock);
  373. PARSE_X264_OPT("partitions", partitions);
  374. PARSE_X264_OPT("stats", stats);
  375. if (x4->psy >= 0)
  376. x4->params.analyse.b_psy = x4->psy;
  377. if (x4->rc_lookahead >= 0)
  378. x4->params.rc.i_lookahead = x4->rc_lookahead;
  379. if (x4->weightp >= 0)
  380. x4->params.analyse.i_weighted_pred = x4->weightp;
  381. if (x4->weightb >= 0)
  382. x4->params.analyse.b_weighted_bipred = x4->weightb;
  383. if (x4->cplxblur >= 0)
  384. x4->params.rc.f_complexity_blur = x4->cplxblur;
  385. if (x4->ssim >= 0)
  386. x4->params.analyse.b_ssim = x4->ssim;
  387. if (x4->intra_refresh >= 0)
  388. x4->params.b_intra_refresh = x4->intra_refresh;
  389. if (x4->bluray_compat >= 0) {
  390. x4->params.b_bluray_compat = x4->bluray_compat;
  391. x4->params.b_vfr_input = 0;
  392. }
  393. if (x4->b_bias != INT_MIN)
  394. x4->params.i_bframe_bias = x4->b_bias;
  395. if (x4->b_pyramid >= 0)
  396. x4->params.i_bframe_pyramid = x4->b_pyramid;
  397. if (x4->mixed_refs >= 0)
  398. x4->params.analyse.b_mixed_references = x4->mixed_refs;
  399. if (x4->dct8x8 >= 0)
  400. x4->params.analyse.b_transform_8x8 = x4->dct8x8;
  401. if (x4->fast_pskip >= 0)
  402. x4->params.analyse.b_fast_pskip = x4->fast_pskip;
  403. if (x4->aud >= 0)
  404. x4->params.b_aud = x4->aud;
  405. if (x4->mbtree >= 0)
  406. x4->params.rc.b_mb_tree = x4->mbtree;
  407. if (x4->direct_pred >= 0)
  408. x4->params.analyse.i_direct_mv_pred = x4->direct_pred;
  409. if (x4->slice_max_size >= 0)
  410. x4->params.i_slice_max_size = x4->slice_max_size;
  411. if (x4->fastfirstpass)
  412. x264_param_apply_fastfirstpass(&x4->params);
  413. if (x4->nal_hrd >= 0)
  414. x4->params.i_nal_hrd = x4->nal_hrd;
  415. if (x4->profile)
  416. if (x264_param_apply_profile(&x4->params, x4->profile) < 0) {
  417. av_log(avctx, AV_LOG_ERROR, "Error setting profile %s.\n", x4->profile);
  418. return AVERROR(EINVAL);
  419. }
  420. x4->params.i_width = avctx->width;
  421. x4->params.i_height = avctx->height;
  422. x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
  423. x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
  424. x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
  425. x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
  426. x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
  427. x4->params.i_threads = avctx->thread_count;
  428. if (avctx->thread_type)
  429. x4->params.b_sliced_threads = avctx->thread_type == FF_THREAD_SLICE;
  430. x4->params.b_interlaced = avctx->flags & CODEC_FLAG_INTERLACED_DCT;
  431. x4->params.b_open_gop = !(avctx->flags & CODEC_FLAG_CLOSED_GOP);
  432. x4->params.i_slice_count = avctx->slices;
  433. x4->params.vui.b_fullrange = avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
  434. avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
  435. avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
  436. avctx->color_range == AVCOL_RANGE_JPEG;
  437. // x264 validates the values internally
  438. x4->params.vui.i_colorprim = avctx->color_primaries;
  439. x4->params.vui.i_transfer = avctx->color_trc;
  440. x4->params.vui.i_colmatrix = avctx->colorspace;
  441. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER)
  442. x4->params.b_repeat_headers = 0;
  443. if (x4->x264_params) {
  444. AVDictionary *dict = NULL;
  445. AVDictionaryEntry *en = NULL;
  446. if (!av_dict_parse_string(&dict, x4->x264_params, "=", ":", 0)) {
  447. while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
  448. if (x264_param_parse(&x4->params, en->key, en->value) < 0)
  449. av_log(avctx, AV_LOG_WARNING,
  450. "Error parsing option '%s = %s'.\n",
  451. en->key, en->value);
  452. }
  453. av_dict_free(&dict);
  454. }
  455. }
  456. // update AVCodecContext with x264 parameters
  457. avctx->has_b_frames = x4->params.i_bframe ?
  458. x4->params.i_bframe_pyramid ? 2 : 1 : 0;
  459. if (avctx->max_b_frames < 0)
  460. avctx->max_b_frames = 0;
  461. avctx->bit_rate = x4->params.rc.i_bitrate*1000;
  462. x4->enc = x264_encoder_open(&x4->params);
  463. if (!x4->enc)
  464. return AVERROR_UNKNOWN;
  465. avctx->coded_frame = av_frame_alloc();
  466. if (!avctx->coded_frame)
  467. return AVERROR(ENOMEM);
  468. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  469. x264_nal_t *nal;
  470. uint8_t *p;
  471. int nnal, s, i;
  472. s = x264_encoder_headers(x4->enc, &nal, &nnal);
  473. avctx->extradata = p = av_malloc(s);
  474. if (!p)
  475. return AVERROR(ENOMEM);
  476. for (i = 0; i < nnal; i++) {
  477. /* Don't put the SEI in extradata. */
  478. if (nal[i].i_type == NAL_SEI) {
  479. av_log(avctx, AV_LOG_INFO, "%s\n", nal[i].p_payload+25);
  480. x4->sei_size = nal[i].i_payload;
  481. x4->sei = av_malloc(x4->sei_size);
  482. if (!x4->sei)
  483. return AVERROR(ENOMEM);
  484. memcpy(x4->sei, nal[i].p_payload, nal[i].i_payload);
  485. continue;
  486. }
  487. memcpy(p, nal[i].p_payload, nal[i].i_payload);
  488. p += nal[i].i_payload;
  489. }
  490. avctx->extradata_size = p - avctx->extradata;
  491. }
  492. return 0;
  493. }
  494. static const enum AVPixelFormat pix_fmts_8bit[] = {
  495. AV_PIX_FMT_YUV420P,
  496. AV_PIX_FMT_YUVJ420P,
  497. AV_PIX_FMT_YUV422P,
  498. AV_PIX_FMT_YUVJ422P,
  499. AV_PIX_FMT_YUV444P,
  500. AV_PIX_FMT_YUVJ444P,
  501. AV_PIX_FMT_NV12,
  502. AV_PIX_FMT_NV16,
  503. AV_PIX_FMT_NONE
  504. };
  505. static const enum AVPixelFormat pix_fmts_9bit[] = {
  506. AV_PIX_FMT_YUV420P9,
  507. AV_PIX_FMT_YUV444P9,
  508. AV_PIX_FMT_NONE
  509. };
  510. static const enum AVPixelFormat pix_fmts_10bit[] = {
  511. AV_PIX_FMT_YUV420P10,
  512. AV_PIX_FMT_YUV422P10,
  513. AV_PIX_FMT_YUV444P10,
  514. AV_PIX_FMT_NV20,
  515. AV_PIX_FMT_NONE
  516. };
  517. static av_cold void X264_init_static(AVCodec *codec)
  518. {
  519. if (x264_bit_depth == 8)
  520. codec->pix_fmts = pix_fmts_8bit;
  521. else if (x264_bit_depth == 9)
  522. codec->pix_fmts = pix_fmts_9bit;
  523. else if (x264_bit_depth == 10)
  524. codec->pix_fmts = pix_fmts_10bit;
  525. }
  526. #define OFFSET(x) offsetof(X264Context, x)
  527. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  528. static const AVOption options[] = {
  529. { "preset", "Set the encoding preset (cf. x264 --fullhelp)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},
  530. { "tune", "Tune the encoding params (cf. x264 --fullhelp)", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  531. { "profile", "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  532. { "fastfirstpass", "Use fast settings when encoding first pass", OFFSET(fastfirstpass), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE},
  533. { "crf", "Select the quality for constant quality mode", OFFSET(crf), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE },
  534. { "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 },
  535. { "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },
  536. { "aq-mode", "AQ method", OFFSET(aq_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "aq_mode"},
  537. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  538. { "variance", "Variance AQ (complexity mask)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  539. { "autovariance", "Auto-variance AQ (experimental)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },
  540. { "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},
  541. { "psy", "Use psychovisual optimizations.", OFFSET(psy), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  542. { "psy-rd", "Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.", OFFSET(psy_rd), AV_OPT_TYPE_STRING, {0 }, 0, 0, VE},
  543. { "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 },
  544. { "weightb", "Weighted prediction for B-frames.", OFFSET(weightb), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  545. { "weightp", "Weighted prediction analysis method.", OFFSET(weightp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "weightp" },
  546. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX, VE, "weightp" },
  547. { "simple", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX, VE, "weightp" },
  548. { "smart", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX, VE, "weightp" },
  549. { "ssim", "Calculate and print SSIM stats.", OFFSET(ssim), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  550. { "intra-refresh", "Use Periodic Intra Refresh instead of IDR frames.",OFFSET(intra_refresh),AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  551. { "bluray-compat", "Bluray compatibility workarounds.", OFFSET(bluray_compat) ,AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  552. { "b-bias", "Influences how often B-frames are used", OFFSET(b_bias), AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX, VE },
  553. { "b-pyramid", "Keep some B-frames as references.", OFFSET(b_pyramid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "b_pyramid" },
  554. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  555. { "strict", "Strictly hierarchical pyramid", 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  556. { "normal", "Non-strict (not Blu-ray compatible)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX, VE, "b_pyramid" },
  557. { "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 },
  558. { "8x8dct", "High profile 8x8 transform.", OFFSET(dct8x8), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
  559. { "fast-pskip", NULL, OFFSET(fast_pskip), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
  560. { "aud", "Use access unit delimiters.", OFFSET(aud), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
  561. { "mbtree", "Use macroblock tree ratecontrol.", OFFSET(mbtree), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE},
  562. { "deblock", "Loop filter parameters, in <alpha:beta> form.", OFFSET(deblock), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  563. { "cplxblur", "Reduce fluctuations in QP (before curve compression)", OFFSET(cplxblur), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE},
  564. { "partitions", "A comma-separated list of partitions to consider. "
  565. "Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all", OFFSET(partitions), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},
  566. { "direct-pred", "Direct MV prediction mode", OFFSET(direct_pred), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "direct-pred" },
  567. { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0, VE, "direct-pred" },
  568. { "spatial", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0, VE, "direct-pred" },
  569. { "temporal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0, VE, "direct-pred" },
  570. { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0, VE, "direct-pred" },
  571. { "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 },
  572. { "stats", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  573. { "nal-hrd", "Signal HRD information (requires vbv-bufsize; "
  574. "cbr not allowed in .mp4)", OFFSET(nal_hrd), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "nal-hrd" },
  575. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX, VE, "nal-hrd" },
  576. { "vbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX, VE, "nal-hrd" },
  577. { "cbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX, VE, "nal-hrd" },
  578. { "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 },
  579. { NULL },
  580. };
  581. static const AVClass class = {
  582. .class_name = "libx264",
  583. .item_name = av_default_item_name,
  584. .option = options,
  585. .version = LIBAVUTIL_VERSION_INT,
  586. };
  587. static const AVCodecDefault x264_defaults[] = {
  588. { "b", "0" },
  589. { "bf", "-1" },
  590. { "g", "-1" },
  591. { "i_qfactor", "-1" },
  592. { "qmin", "-1" },
  593. { "qmax", "-1" },
  594. { "qdiff", "-1" },
  595. { "qblur", "-1" },
  596. { "qcomp", "-1" },
  597. { "refs", "-1" },
  598. { "sc_threshold", "-1" },
  599. { "trellis", "-1" },
  600. { "nr", "-1" },
  601. { "me_range", "-1" },
  602. { "me_method", "-1" },
  603. { "subq", "-1" },
  604. { "b_strategy", "-1" },
  605. { "keyint_min", "-1" },
  606. { "coder", "-1" },
  607. { "cmp", "-1" },
  608. { "threads", AV_STRINGIFY(X264_THREADS_AUTO) },
  609. { "thread_type", "0" },
  610. { "flags", "+cgop" },
  611. { "rc_init_occupancy","-1" },
  612. { NULL },
  613. };
  614. AVCodec ff_libx264_encoder = {
  615. .name = "libx264",
  616. .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  617. .type = AVMEDIA_TYPE_VIDEO,
  618. .id = AV_CODEC_ID_H264,
  619. .priv_data_size = sizeof(X264Context),
  620. .init = X264_init,
  621. .encode2 = X264_frame,
  622. .close = X264_close,
  623. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
  624. .priv_class = &class,
  625. .defaults = x264_defaults,
  626. .init_static_data = X264_init_static,
  627. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  628. FF_CODEC_CAP_INIT_CLEANUP,
  629. };