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.

749 lines
30KB

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