libx264.c 27 KB

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