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.

730 lines
29KB

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