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.

690 lines
23KB

  1. /*
  2. * libx265 encoder
  3. *
  4. * Copyright (c) 2013-2014 Derek Buitenhuis
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #if defined(_MSC_VER)
  23. #define X265_API_IMPORTS 1
  24. #endif
  25. #include <x265.h>
  26. #include <float.h>
  27. #include "libavutil/internal.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avcodec.h"
  32. #include "internal.h"
  33. typedef struct libx265Context {
  34. const AVClass *class;
  35. x265_encoder *encoder;
  36. x265_param *params;
  37. const x265_api *api;
  38. float crf;
  39. int cqp;
  40. int forced_idr;
  41. char *preset;
  42. char *tune;
  43. char *profile;
  44. char *x265_opts;
  45. /**
  46. * If the encoder does not support ROI then warn the first time we
  47. * encounter a frame with ROI side data.
  48. */
  49. int roi_warned;
  50. } libx265Context;
  51. static int is_keyframe(NalUnitType naltype)
  52. {
  53. switch (naltype) {
  54. case NAL_UNIT_CODED_SLICE_BLA_W_LP:
  55. case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
  56. case NAL_UNIT_CODED_SLICE_BLA_N_LP:
  57. case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
  58. case NAL_UNIT_CODED_SLICE_IDR_N_LP:
  59. case NAL_UNIT_CODED_SLICE_CRA:
  60. return 1;
  61. default:
  62. return 0;
  63. }
  64. }
  65. static av_cold int libx265_encode_close(AVCodecContext *avctx)
  66. {
  67. libx265Context *ctx = avctx->priv_data;
  68. ctx->api->param_free(ctx->params);
  69. if (ctx->encoder)
  70. ctx->api->encoder_close(ctx->encoder);
  71. return 0;
  72. }
  73. static av_cold int libx265_param_parse_float(AVCodecContext *avctx,
  74. const char *key, float value)
  75. {
  76. libx265Context *ctx = avctx->priv_data;
  77. char buf[256];
  78. snprintf(buf, sizeof(buf), "%2.2f", value);
  79. if (ctx->api->param_parse(ctx->params, key, buf) == X265_PARAM_BAD_VALUE) {
  80. av_log(avctx, AV_LOG_ERROR, "Invalid value %2.2f for param \"%s\".\n", value, key);
  81. return AVERROR(EINVAL);
  82. }
  83. return 0;
  84. }
  85. static av_cold int libx265_param_parse_int(AVCodecContext *avctx,
  86. const char *key, int value)
  87. {
  88. libx265Context *ctx = avctx->priv_data;
  89. char buf[256];
  90. snprintf(buf, sizeof(buf), "%d", value);
  91. if (ctx->api->param_parse(ctx->params, key, buf) == X265_PARAM_BAD_VALUE) {
  92. av_log(avctx, AV_LOG_ERROR, "Invalid value %d for param \"%s\".\n", value, key);
  93. return AVERROR(EINVAL);
  94. }
  95. return 0;
  96. }
  97. static av_cold int libx265_encode_init(AVCodecContext *avctx)
  98. {
  99. libx265Context *ctx = avctx->priv_data;
  100. AVCPBProperties *cpb_props = NULL;
  101. int ret;
  102. ctx->api = x265_api_get(av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth);
  103. if (!ctx->api)
  104. ctx->api = x265_api_get(0);
  105. ctx->params = ctx->api->param_alloc();
  106. if (!ctx->params) {
  107. av_log(avctx, AV_LOG_ERROR, "Could not allocate x265 param structure.\n");
  108. return AVERROR(ENOMEM);
  109. }
  110. if (ctx->api->param_default_preset(ctx->params, ctx->preset, ctx->tune) < 0) {
  111. int i;
  112. av_log(avctx, AV_LOG_ERROR, "Error setting preset/tune %s/%s.\n", ctx->preset, ctx->tune);
  113. av_log(avctx, AV_LOG_INFO, "Possible presets:");
  114. for (i = 0; x265_preset_names[i]; i++)
  115. av_log(avctx, AV_LOG_INFO, " %s", x265_preset_names[i]);
  116. av_log(avctx, AV_LOG_INFO, "\n");
  117. av_log(avctx, AV_LOG_INFO, "Possible tunes:");
  118. for (i = 0; x265_tune_names[i]; i++)
  119. av_log(avctx, AV_LOG_INFO, " %s", x265_tune_names[i]);
  120. av_log(avctx, AV_LOG_INFO, "\n");
  121. return AVERROR(EINVAL);
  122. }
  123. ctx->params->frameNumThreads = avctx->thread_count;
  124. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  125. ctx->params->fpsNum = avctx->framerate.num;
  126. ctx->params->fpsDenom = avctx->framerate.den;
  127. } else {
  128. ctx->params->fpsNum = avctx->time_base.den;
  129. ctx->params->fpsDenom = avctx->time_base.num * avctx->ticks_per_frame;
  130. }
  131. ctx->params->sourceWidth = avctx->width;
  132. ctx->params->sourceHeight = avctx->height;
  133. ctx->params->bEnablePsnr = !!(avctx->flags & AV_CODEC_FLAG_PSNR);
  134. ctx->params->bOpenGOP = !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP);
  135. /* Tune the CTU size based on input resolution. */
  136. if (ctx->params->sourceWidth < 64 || ctx->params->sourceHeight < 64)
  137. ctx->params->maxCUSize = 32;
  138. if (ctx->params->sourceWidth < 32 || ctx->params->sourceHeight < 32)
  139. ctx->params->maxCUSize = 16;
  140. if (ctx->params->sourceWidth < 16 || ctx->params->sourceHeight < 16) {
  141. av_log(avctx, AV_LOG_ERROR, "Image size is too small (%dx%d).\n",
  142. ctx->params->sourceWidth, ctx->params->sourceHeight);
  143. return AVERROR(EINVAL);
  144. }
  145. ctx->params->vui.bEnableVideoSignalTypePresentFlag = 1;
  146. ctx->params->vui.bEnableVideoFullRangeFlag = avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
  147. avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
  148. avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
  149. avctx->color_range == AVCOL_RANGE_JPEG;
  150. if ((avctx->color_primaries <= AVCOL_PRI_SMPTE432 &&
  151. avctx->color_primaries != AVCOL_PRI_UNSPECIFIED) ||
  152. (avctx->color_trc <= AVCOL_TRC_ARIB_STD_B67 &&
  153. avctx->color_trc != AVCOL_TRC_UNSPECIFIED) ||
  154. (avctx->colorspace <= AVCOL_SPC_ICTCP &&
  155. avctx->colorspace != AVCOL_SPC_UNSPECIFIED)) {
  156. ctx->params->vui.bEnableColorDescriptionPresentFlag = 1;
  157. // x265 validates the parameters internally
  158. ctx->params->vui.colorPrimaries = avctx->color_primaries;
  159. ctx->params->vui.transferCharacteristics = avctx->color_trc;
  160. #if X265_BUILD >= 159
  161. if (avctx->color_trc == AVCOL_TRC_ARIB_STD_B67)
  162. ctx->params->preferredTransferCharacteristics = ctx->params->vui.transferCharacteristics;
  163. #endif
  164. ctx->params->vui.matrixCoeffs = avctx->colorspace;
  165. }
  166. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
  167. char sar[12];
  168. int sar_num, sar_den;
  169. av_reduce(&sar_num, &sar_den,
  170. avctx->sample_aspect_ratio.num,
  171. avctx->sample_aspect_ratio.den, 65535);
  172. snprintf(sar, sizeof(sar), "%d:%d", sar_num, sar_den);
  173. if (ctx->api->param_parse(ctx->params, "sar", sar) == X265_PARAM_BAD_VALUE) {
  174. av_log(avctx, AV_LOG_ERROR, "Invalid SAR: %d:%d.\n", sar_num, sar_den);
  175. return AVERROR_INVALIDDATA;
  176. }
  177. }
  178. switch (avctx->pix_fmt) {
  179. case AV_PIX_FMT_YUV420P:
  180. case AV_PIX_FMT_YUV420P10:
  181. case AV_PIX_FMT_YUV420P12:
  182. ctx->params->internalCsp = X265_CSP_I420;
  183. break;
  184. case AV_PIX_FMT_YUV422P:
  185. case AV_PIX_FMT_YUV422P10:
  186. case AV_PIX_FMT_YUV422P12:
  187. ctx->params->internalCsp = X265_CSP_I422;
  188. break;
  189. case AV_PIX_FMT_GBRP:
  190. case AV_PIX_FMT_GBRP10:
  191. case AV_PIX_FMT_GBRP12:
  192. ctx->params->vui.matrixCoeffs = AVCOL_SPC_RGB;
  193. ctx->params->vui.bEnableVideoSignalTypePresentFlag = 1;
  194. ctx->params->vui.bEnableColorDescriptionPresentFlag = 1;
  195. case AV_PIX_FMT_YUV444P:
  196. case AV_PIX_FMT_YUV444P10:
  197. case AV_PIX_FMT_YUV444P12:
  198. ctx->params->internalCsp = X265_CSP_I444;
  199. break;
  200. case AV_PIX_FMT_GRAY8:
  201. case AV_PIX_FMT_GRAY10:
  202. case AV_PIX_FMT_GRAY12:
  203. if (ctx->api->api_build_number < 85) {
  204. av_log(avctx, AV_LOG_ERROR,
  205. "libx265 version is %d, must be at least 85 for gray encoding.\n",
  206. ctx->api->api_build_number);
  207. return AVERROR_INVALIDDATA;
  208. }
  209. ctx->params->internalCsp = X265_CSP_I400;
  210. break;
  211. }
  212. if (ctx->crf >= 0) {
  213. char crf[6];
  214. snprintf(crf, sizeof(crf), "%2.2f", ctx->crf);
  215. if (ctx->api->param_parse(ctx->params, "crf", crf) == X265_PARAM_BAD_VALUE) {
  216. av_log(avctx, AV_LOG_ERROR, "Invalid crf: %2.2f.\n", ctx->crf);
  217. return AVERROR(EINVAL);
  218. }
  219. } else if (avctx->bit_rate > 0) {
  220. ctx->params->rc.bitrate = avctx->bit_rate / 1000;
  221. ctx->params->rc.rateControlMode = X265_RC_ABR;
  222. } else if (ctx->cqp >= 0) {
  223. ret = libx265_param_parse_int(avctx, "qp", ctx->cqp);
  224. if (ret < 0)
  225. return ret;
  226. }
  227. #if X265_BUILD >= 89
  228. if (avctx->qmin >= 0) {
  229. ret = libx265_param_parse_int(avctx, "qpmin", avctx->qmin);
  230. if (ret < 0)
  231. return ret;
  232. }
  233. if (avctx->qmax >= 0) {
  234. ret = libx265_param_parse_int(avctx, "qpmax", avctx->qmax);
  235. if (ret < 0)
  236. return ret;
  237. }
  238. #endif
  239. if (avctx->max_qdiff >= 0) {
  240. ret = libx265_param_parse_int(avctx, "qpstep", avctx->max_qdiff);
  241. if (ret < 0)
  242. return ret;
  243. }
  244. if (avctx->qblur >= 0) {
  245. ret = libx265_param_parse_float(avctx, "qblur", avctx->qblur);
  246. if (ret < 0)
  247. return ret;
  248. }
  249. if (avctx->qcompress >= 0) {
  250. ret = libx265_param_parse_float(avctx, "qcomp", avctx->qcompress);
  251. if (ret < 0)
  252. return ret;
  253. }
  254. if (avctx->i_quant_factor >= 0) {
  255. ret = libx265_param_parse_float(avctx, "ipratio", avctx->i_quant_factor);
  256. if (ret < 0)
  257. return ret;
  258. }
  259. if (avctx->b_quant_factor >= 0) {
  260. ret = libx265_param_parse_float(avctx, "pbratio", avctx->b_quant_factor);
  261. if (ret < 0)
  262. return ret;
  263. }
  264. ctx->params->rc.vbvBufferSize = avctx->rc_buffer_size / 1000;
  265. ctx->params->rc.vbvMaxBitrate = avctx->rc_max_rate / 1000;
  266. cpb_props = ff_add_cpb_side_data(avctx);
  267. if (!cpb_props)
  268. return AVERROR(ENOMEM);
  269. cpb_props->buffer_size = ctx->params->rc.vbvBufferSize * 1000;
  270. cpb_props->max_bitrate = ctx->params->rc.vbvMaxBitrate * 1000;
  271. cpb_props->avg_bitrate = ctx->params->rc.bitrate * 1000;
  272. if (!(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER))
  273. ctx->params->bRepeatHeaders = 1;
  274. if (avctx->gop_size >= 0) {
  275. ret = libx265_param_parse_int(avctx, "keyint", avctx->gop_size);
  276. if (ret < 0)
  277. return ret;
  278. }
  279. if (avctx->keyint_min > 0) {
  280. ret = libx265_param_parse_int(avctx, "min-keyint", avctx->keyint_min);
  281. if (ret < 0)
  282. return ret;
  283. }
  284. if (avctx->max_b_frames >= 0) {
  285. ret = libx265_param_parse_int(avctx, "bframes", avctx->max_b_frames);
  286. if (ret < 0)
  287. return ret;
  288. }
  289. if (avctx->refs >= 0) {
  290. ret = libx265_param_parse_int(avctx, "ref", avctx->refs);
  291. if (ret < 0)
  292. return ret;
  293. }
  294. if (ctx->x265_opts) {
  295. AVDictionary *dict = NULL;
  296. AVDictionaryEntry *en = NULL;
  297. if (!av_dict_parse_string(&dict, ctx->x265_opts, "=", ":", 0)) {
  298. while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
  299. int parse_ret = ctx->api->param_parse(ctx->params, en->key, en->value);
  300. switch (parse_ret) {
  301. case X265_PARAM_BAD_NAME:
  302. av_log(avctx, AV_LOG_WARNING,
  303. "Unknown option: %s.\n", en->key);
  304. break;
  305. case X265_PARAM_BAD_VALUE:
  306. av_log(avctx, AV_LOG_WARNING,
  307. "Invalid value for %s: %s.\n", en->key, en->value);
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. av_dict_free(&dict);
  314. }
  315. }
  316. if (ctx->params->rc.vbvBufferSize && avctx->rc_initial_buffer_occupancy > 1000 &&
  317. ctx->params->rc.vbvBufferInit == 0.9) {
  318. ctx->params->rc.vbvBufferInit = (float)avctx->rc_initial_buffer_occupancy / 1000;
  319. }
  320. if (ctx->profile) {
  321. if (ctx->api->param_apply_profile(ctx->params, ctx->profile) < 0) {
  322. int i;
  323. av_log(avctx, AV_LOG_ERROR, "Invalid or incompatible profile set: %s.\n", ctx->profile);
  324. av_log(avctx, AV_LOG_INFO, "Possible profiles:");
  325. for (i = 0; x265_profile_names[i]; i++)
  326. av_log(avctx, AV_LOG_INFO, " %s", x265_profile_names[i]);
  327. av_log(avctx, AV_LOG_INFO, "\n");
  328. return AVERROR(EINVAL);
  329. }
  330. }
  331. ctx->encoder = ctx->api->encoder_open(ctx->params);
  332. if (!ctx->encoder) {
  333. av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
  334. libx265_encode_close(avctx);
  335. return AVERROR_INVALIDDATA;
  336. }
  337. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  338. x265_nal *nal;
  339. int nnal;
  340. avctx->extradata_size = ctx->api->encoder_headers(ctx->encoder, &nal, &nnal);
  341. if (avctx->extradata_size <= 0) {
  342. av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
  343. libx265_encode_close(avctx);
  344. return AVERROR_INVALIDDATA;
  345. }
  346. avctx->extradata = av_malloc(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  347. if (!avctx->extradata) {
  348. av_log(avctx, AV_LOG_ERROR,
  349. "Cannot allocate HEVC header of size %d.\n", avctx->extradata_size);
  350. libx265_encode_close(avctx);
  351. return AVERROR(ENOMEM);
  352. }
  353. memcpy(avctx->extradata, nal[0].payload, avctx->extradata_size);
  354. }
  355. return 0;
  356. }
  357. static av_cold int libx265_encode_set_roi(libx265Context *ctx, const AVFrame *frame, x265_picture* pic)
  358. {
  359. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
  360. if (sd) {
  361. if (ctx->params->rc.aqMode == X265_AQ_NONE) {
  362. if (!ctx->roi_warned) {
  363. ctx->roi_warned = 1;
  364. av_log(ctx, AV_LOG_WARNING, "Adaptive quantization must be enabled to use ROI encoding, skipping ROI.\n");
  365. }
  366. } else {
  367. /* 8x8 block when qg-size is 8, 16*16 block otherwise. */
  368. int mb_size = (ctx->params->rc.qgSize == 8) ? 8 : 16;
  369. int mbx = (frame->width + mb_size - 1) / mb_size;
  370. int mby = (frame->height + mb_size - 1) / mb_size;
  371. int qp_range = 51 + 6 * (pic->bitDepth - 8);
  372. int nb_rois;
  373. const AVRegionOfInterest *roi;
  374. uint32_t roi_size;
  375. float *qoffsets; /* will be freed after encode is called. */
  376. roi = (const AVRegionOfInterest*)sd->data;
  377. roi_size = roi->self_size;
  378. if (!roi_size || sd->size % roi_size != 0) {
  379. av_log(ctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.\n");
  380. return AVERROR(EINVAL);
  381. }
  382. nb_rois = sd->size / roi_size;
  383. qoffsets = av_mallocz_array(mbx * mby, sizeof(*qoffsets));
  384. if (!qoffsets)
  385. return AVERROR(ENOMEM);
  386. // This list must be iterated in reverse because the first
  387. // region in the list applies when regions overlap.
  388. for (int i = nb_rois - 1; i >= 0; i--) {
  389. int startx, endx, starty, endy;
  390. float qoffset;
  391. roi = (const AVRegionOfInterest*)(sd->data + roi_size * i);
  392. starty = FFMIN(mby, roi->top / mb_size);
  393. endy = FFMIN(mby, (roi->bottom + mb_size - 1)/ mb_size);
  394. startx = FFMIN(mbx, roi->left / mb_size);
  395. endx = FFMIN(mbx, (roi->right + mb_size - 1)/ mb_size);
  396. if (roi->qoffset.den == 0) {
  397. av_free(qoffsets);
  398. av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.qoffset.den must not be zero.\n");
  399. return AVERROR(EINVAL);
  400. }
  401. qoffset = roi->qoffset.num * 1.0f / roi->qoffset.den;
  402. qoffset = av_clipf(qoffset * qp_range, -qp_range, +qp_range);
  403. for (int y = starty; y < endy; y++)
  404. for (int x = startx; x < endx; x++)
  405. qoffsets[x + y*mbx] = qoffset;
  406. }
  407. pic->quantOffsets = qoffsets;
  408. }
  409. }
  410. return 0;
  411. }
  412. static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  413. const AVFrame *pic, int *got_packet)
  414. {
  415. libx265Context *ctx = avctx->priv_data;
  416. x265_picture x265pic;
  417. x265_picture x265pic_out = { 0 };
  418. x265_nal *nal;
  419. uint8_t *dst;
  420. int pict_type;
  421. int payload = 0;
  422. int nnal;
  423. int ret;
  424. int i;
  425. ctx->api->picture_init(ctx->params, &x265pic);
  426. if (pic) {
  427. for (i = 0; i < 3; i++) {
  428. x265pic.planes[i] = pic->data[i];
  429. x265pic.stride[i] = pic->linesize[i];
  430. }
  431. x265pic.pts = pic->pts;
  432. x265pic.bitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth;
  433. x265pic.sliceType = pic->pict_type == AV_PICTURE_TYPE_I ?
  434. (ctx->forced_idr ? X265_TYPE_IDR : X265_TYPE_I) :
  435. pic->pict_type == AV_PICTURE_TYPE_P ? X265_TYPE_P :
  436. pic->pict_type == AV_PICTURE_TYPE_B ? X265_TYPE_B :
  437. X265_TYPE_AUTO;
  438. ret = libx265_encode_set_roi(ctx, pic, &x265pic);
  439. if (ret < 0)
  440. return ret;
  441. }
  442. ret = ctx->api->encoder_encode(ctx->encoder, &nal, &nnal,
  443. pic ? &x265pic : NULL, &x265pic_out);
  444. av_freep(&x265pic.quantOffsets);
  445. if (ret < 0)
  446. return AVERROR_EXTERNAL;
  447. if (!nnal)
  448. return 0;
  449. for (i = 0; i < nnal; i++)
  450. payload += nal[i].sizeBytes;
  451. ret = ff_alloc_packet2(avctx, pkt, payload, payload);
  452. if (ret < 0) {
  453. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  454. return ret;
  455. }
  456. dst = pkt->data;
  457. for (i = 0; i < nnal; i++) {
  458. memcpy(dst, nal[i].payload, nal[i].sizeBytes);
  459. dst += nal[i].sizeBytes;
  460. if (is_keyframe(nal[i].type))
  461. pkt->flags |= AV_PKT_FLAG_KEY;
  462. }
  463. pkt->pts = x265pic_out.pts;
  464. pkt->dts = x265pic_out.dts;
  465. switch (x265pic_out.sliceType) {
  466. case X265_TYPE_IDR:
  467. case X265_TYPE_I:
  468. pict_type = AV_PICTURE_TYPE_I;
  469. break;
  470. case X265_TYPE_P:
  471. pict_type = AV_PICTURE_TYPE_P;
  472. break;
  473. case X265_TYPE_B:
  474. case X265_TYPE_BREF:
  475. pict_type = AV_PICTURE_TYPE_B;
  476. break;
  477. }
  478. #if FF_API_CODED_FRAME
  479. FF_DISABLE_DEPRECATION_WARNINGS
  480. avctx->coded_frame->pict_type = pict_type;
  481. FF_ENABLE_DEPRECATION_WARNINGS
  482. #endif
  483. #if X265_BUILD >= 130
  484. if (x265pic_out.sliceType == X265_TYPE_B)
  485. #else
  486. if (x265pic_out.frameData.sliceType == 'b')
  487. #endif
  488. pkt->flags |= AV_PKT_FLAG_DISPOSABLE;
  489. ff_side_data_set_encoder_stats(pkt, x265pic_out.frameData.qp * FF_QP2LAMBDA, NULL, 0, pict_type);
  490. *got_packet = 1;
  491. return 0;
  492. }
  493. static const enum AVPixelFormat x265_csp_eight[] = {
  494. AV_PIX_FMT_YUV420P,
  495. AV_PIX_FMT_YUVJ420P,
  496. AV_PIX_FMT_YUV422P,
  497. AV_PIX_FMT_YUVJ422P,
  498. AV_PIX_FMT_YUV444P,
  499. AV_PIX_FMT_YUVJ444P,
  500. AV_PIX_FMT_GBRP,
  501. AV_PIX_FMT_GRAY8,
  502. AV_PIX_FMT_NONE
  503. };
  504. static const enum AVPixelFormat x265_csp_ten[] = {
  505. AV_PIX_FMT_YUV420P,
  506. AV_PIX_FMT_YUVJ420P,
  507. AV_PIX_FMT_YUV422P,
  508. AV_PIX_FMT_YUVJ422P,
  509. AV_PIX_FMT_YUV444P,
  510. AV_PIX_FMT_YUVJ444P,
  511. AV_PIX_FMT_GBRP,
  512. AV_PIX_FMT_YUV420P10,
  513. AV_PIX_FMT_YUV422P10,
  514. AV_PIX_FMT_YUV444P10,
  515. AV_PIX_FMT_GBRP10,
  516. AV_PIX_FMT_GRAY8,
  517. AV_PIX_FMT_GRAY10,
  518. AV_PIX_FMT_NONE
  519. };
  520. static const enum AVPixelFormat x265_csp_twelve[] = {
  521. AV_PIX_FMT_YUV420P,
  522. AV_PIX_FMT_YUVJ420P,
  523. AV_PIX_FMT_YUV422P,
  524. AV_PIX_FMT_YUVJ422P,
  525. AV_PIX_FMT_YUV444P,
  526. AV_PIX_FMT_YUVJ444P,
  527. AV_PIX_FMT_GBRP,
  528. AV_PIX_FMT_YUV420P10,
  529. AV_PIX_FMT_YUV422P10,
  530. AV_PIX_FMT_YUV444P10,
  531. AV_PIX_FMT_GBRP10,
  532. AV_PIX_FMT_YUV420P12,
  533. AV_PIX_FMT_YUV422P12,
  534. AV_PIX_FMT_YUV444P12,
  535. AV_PIX_FMT_GBRP12,
  536. AV_PIX_FMT_GRAY8,
  537. AV_PIX_FMT_GRAY10,
  538. AV_PIX_FMT_GRAY12,
  539. AV_PIX_FMT_NONE
  540. };
  541. static av_cold void libx265_encode_init_csp(AVCodec *codec)
  542. {
  543. if (x265_api_get(12))
  544. codec->pix_fmts = x265_csp_twelve;
  545. else if (x265_api_get(10))
  546. codec->pix_fmts = x265_csp_ten;
  547. else if (x265_api_get(8))
  548. codec->pix_fmts = x265_csp_eight;
  549. }
  550. #define OFFSET(x) offsetof(libx265Context, x)
  551. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  552. static const AVOption options[] = {
  553. { "crf", "set the x265 crf", OFFSET(crf), AV_OPT_TYPE_FLOAT, { .dbl = -1 }, -1, FLT_MAX, VE },
  554. { "qp", "set the x265 qp", OFFSET(cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },
  555. { "forced-idr", "if forcing keyframes, force them as IDR frames", OFFSET(forced_idr),AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  556. { "preset", "set the x265 preset", OFFSET(preset), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  557. { "tune", "set the x265 tune parameter", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  558. { "profile", "set the x265 profile", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  559. { "x265-params", "set the x265 configuration using a :-separated list of key=value parameters", OFFSET(x265_opts), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  560. { NULL }
  561. };
  562. static const AVClass class = {
  563. .class_name = "libx265",
  564. .item_name = av_default_item_name,
  565. .option = options,
  566. .version = LIBAVUTIL_VERSION_INT,
  567. };
  568. static const AVCodecDefault x265_defaults[] = {
  569. { "b", "0" },
  570. { "bf", "-1" },
  571. { "g", "-1" },
  572. { "keyint_min", "-1" },
  573. { "refs", "-1" },
  574. { "qmin", "-1" },
  575. { "qmax", "-1" },
  576. { "qdiff", "-1" },
  577. { "qblur", "-1" },
  578. { "qcomp", "-1" },
  579. { "i_qfactor", "-1" },
  580. { "b_qfactor", "-1" },
  581. { NULL },
  582. };
  583. AVCodec ff_libx265_encoder = {
  584. .name = "libx265",
  585. .long_name = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
  586. .type = AVMEDIA_TYPE_VIDEO,
  587. .id = AV_CODEC_ID_HEVC,
  588. .init = libx265_encode_init,
  589. .init_static_data = libx265_encode_init_csp,
  590. .encode2 = libx265_encode_frame,
  591. .close = libx265_encode_close,
  592. .priv_data_size = sizeof(libx265Context),
  593. .priv_class = &class,
  594. .defaults = x265_defaults,
  595. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  596. .wrapper_name = "libx265",
  597. };