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.

685 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. AVDictionary *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. {
  295. AVDictionaryEntry *en = NULL;
  296. while ((en = av_dict_get(ctx->x265_opts, "", en, AV_DICT_IGNORE_SUFFIX))) {
  297. int parse_ret = ctx->api->param_parse(ctx->params, en->key, en->value);
  298. switch (parse_ret) {
  299. case X265_PARAM_BAD_NAME:
  300. av_log(avctx, AV_LOG_WARNING,
  301. "Unknown option: %s.\n", en->key);
  302. break;
  303. case X265_PARAM_BAD_VALUE:
  304. av_log(avctx, AV_LOG_WARNING,
  305. "Invalid value for %s: %s.\n", en->key, en->value);
  306. break;
  307. default:
  308. break;
  309. }
  310. }
  311. }
  312. if (ctx->params->rc.vbvBufferSize && avctx->rc_initial_buffer_occupancy > 1000 &&
  313. ctx->params->rc.vbvBufferInit == 0.9) {
  314. ctx->params->rc.vbvBufferInit = (float)avctx->rc_initial_buffer_occupancy / 1000;
  315. }
  316. if (ctx->profile) {
  317. if (ctx->api->param_apply_profile(ctx->params, ctx->profile) < 0) {
  318. int i;
  319. av_log(avctx, AV_LOG_ERROR, "Invalid or incompatible profile set: %s.\n", ctx->profile);
  320. av_log(avctx, AV_LOG_INFO, "Possible profiles:");
  321. for (i = 0; x265_profile_names[i]; i++)
  322. av_log(avctx, AV_LOG_INFO, " %s", x265_profile_names[i]);
  323. av_log(avctx, AV_LOG_INFO, "\n");
  324. return AVERROR(EINVAL);
  325. }
  326. }
  327. ctx->encoder = ctx->api->encoder_open(ctx->params);
  328. if (!ctx->encoder) {
  329. av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
  330. libx265_encode_close(avctx);
  331. return AVERROR_INVALIDDATA;
  332. }
  333. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  334. x265_nal *nal;
  335. int nnal;
  336. avctx->extradata_size = ctx->api->encoder_headers(ctx->encoder, &nal, &nnal);
  337. if (avctx->extradata_size <= 0) {
  338. av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
  339. libx265_encode_close(avctx);
  340. return AVERROR_INVALIDDATA;
  341. }
  342. avctx->extradata = av_malloc(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  343. if (!avctx->extradata) {
  344. av_log(avctx, AV_LOG_ERROR,
  345. "Cannot allocate HEVC header of size %d.\n", avctx->extradata_size);
  346. libx265_encode_close(avctx);
  347. return AVERROR(ENOMEM);
  348. }
  349. memcpy(avctx->extradata, nal[0].payload, avctx->extradata_size);
  350. }
  351. return 0;
  352. }
  353. static av_cold int libx265_encode_set_roi(libx265Context *ctx, const AVFrame *frame, x265_picture* pic)
  354. {
  355. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
  356. if (sd) {
  357. if (ctx->params->rc.aqMode == X265_AQ_NONE) {
  358. if (!ctx->roi_warned) {
  359. ctx->roi_warned = 1;
  360. av_log(ctx, AV_LOG_WARNING, "Adaptive quantization must be enabled to use ROI encoding, skipping ROI.\n");
  361. }
  362. } else {
  363. /* 8x8 block when qg-size is 8, 16*16 block otherwise. */
  364. int mb_size = (ctx->params->rc.qgSize == 8) ? 8 : 16;
  365. int mbx = (frame->width + mb_size - 1) / mb_size;
  366. int mby = (frame->height + mb_size - 1) / mb_size;
  367. int qp_range = 51 + 6 * (pic->bitDepth - 8);
  368. int nb_rois;
  369. const AVRegionOfInterest *roi;
  370. uint32_t roi_size;
  371. float *qoffsets; /* will be freed after encode is called. */
  372. roi = (const AVRegionOfInterest*)sd->data;
  373. roi_size = roi->self_size;
  374. if (!roi_size || sd->size % roi_size != 0) {
  375. av_log(ctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.\n");
  376. return AVERROR(EINVAL);
  377. }
  378. nb_rois = sd->size / roi_size;
  379. qoffsets = av_mallocz_array(mbx * mby, sizeof(*qoffsets));
  380. if (!qoffsets)
  381. return AVERROR(ENOMEM);
  382. // This list must be iterated in reverse because the first
  383. // region in the list applies when regions overlap.
  384. for (int i = nb_rois - 1; i >= 0; i--) {
  385. int startx, endx, starty, endy;
  386. float qoffset;
  387. roi = (const AVRegionOfInterest*)(sd->data + roi_size * i);
  388. starty = FFMIN(mby, roi->top / mb_size);
  389. endy = FFMIN(mby, (roi->bottom + mb_size - 1)/ mb_size);
  390. startx = FFMIN(mbx, roi->left / mb_size);
  391. endx = FFMIN(mbx, (roi->right + mb_size - 1)/ mb_size);
  392. if (roi->qoffset.den == 0) {
  393. av_free(qoffsets);
  394. av_log(ctx, AV_LOG_ERROR, "AVRegionOfInterest.qoffset.den must not be zero.\n");
  395. return AVERROR(EINVAL);
  396. }
  397. qoffset = roi->qoffset.num * 1.0f / roi->qoffset.den;
  398. qoffset = av_clipf(qoffset * qp_range, -qp_range, +qp_range);
  399. for (int y = starty; y < endy; y++)
  400. for (int x = startx; x < endx; x++)
  401. qoffsets[x + y*mbx] = qoffset;
  402. }
  403. pic->quantOffsets = qoffsets;
  404. }
  405. }
  406. return 0;
  407. }
  408. static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  409. const AVFrame *pic, int *got_packet)
  410. {
  411. libx265Context *ctx = avctx->priv_data;
  412. x265_picture x265pic;
  413. x265_picture x265pic_out = { 0 };
  414. x265_nal *nal;
  415. uint8_t *dst;
  416. int pict_type;
  417. int payload = 0;
  418. int nnal;
  419. int ret;
  420. int i;
  421. ctx->api->picture_init(ctx->params, &x265pic);
  422. if (pic) {
  423. for (i = 0; i < 3; i++) {
  424. x265pic.planes[i] = pic->data[i];
  425. x265pic.stride[i] = pic->linesize[i];
  426. }
  427. x265pic.pts = pic->pts;
  428. x265pic.bitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth;
  429. x265pic.sliceType = pic->pict_type == AV_PICTURE_TYPE_I ?
  430. (ctx->forced_idr ? X265_TYPE_IDR : X265_TYPE_I) :
  431. pic->pict_type == AV_PICTURE_TYPE_P ? X265_TYPE_P :
  432. pic->pict_type == AV_PICTURE_TYPE_B ? X265_TYPE_B :
  433. X265_TYPE_AUTO;
  434. ret = libx265_encode_set_roi(ctx, pic, &x265pic);
  435. if (ret < 0)
  436. return ret;
  437. }
  438. ret = ctx->api->encoder_encode(ctx->encoder, &nal, &nnal,
  439. pic ? &x265pic : NULL, &x265pic_out);
  440. av_freep(&x265pic.quantOffsets);
  441. if (ret < 0)
  442. return AVERROR_EXTERNAL;
  443. if (!nnal)
  444. return 0;
  445. for (i = 0; i < nnal; i++)
  446. payload += nal[i].sizeBytes;
  447. ret = ff_alloc_packet2(avctx, pkt, payload, payload);
  448. if (ret < 0) {
  449. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  450. return ret;
  451. }
  452. dst = pkt->data;
  453. for (i = 0; i < nnal; i++) {
  454. memcpy(dst, nal[i].payload, nal[i].sizeBytes);
  455. dst += nal[i].sizeBytes;
  456. if (is_keyframe(nal[i].type))
  457. pkt->flags |= AV_PKT_FLAG_KEY;
  458. }
  459. pkt->pts = x265pic_out.pts;
  460. pkt->dts = x265pic_out.dts;
  461. switch (x265pic_out.sliceType) {
  462. case X265_TYPE_IDR:
  463. case X265_TYPE_I:
  464. pict_type = AV_PICTURE_TYPE_I;
  465. break;
  466. case X265_TYPE_P:
  467. pict_type = AV_PICTURE_TYPE_P;
  468. break;
  469. case X265_TYPE_B:
  470. case X265_TYPE_BREF:
  471. pict_type = AV_PICTURE_TYPE_B;
  472. break;
  473. }
  474. #if FF_API_CODED_FRAME
  475. FF_DISABLE_DEPRECATION_WARNINGS
  476. avctx->coded_frame->pict_type = pict_type;
  477. FF_ENABLE_DEPRECATION_WARNINGS
  478. #endif
  479. #if X265_BUILD >= 130
  480. if (x265pic_out.sliceType == X265_TYPE_B)
  481. #else
  482. if (x265pic_out.frameData.sliceType == 'b')
  483. #endif
  484. pkt->flags |= AV_PKT_FLAG_DISPOSABLE;
  485. ff_side_data_set_encoder_stats(pkt, x265pic_out.frameData.qp * FF_QP2LAMBDA, NULL, 0, pict_type);
  486. *got_packet = 1;
  487. return 0;
  488. }
  489. static const enum AVPixelFormat x265_csp_eight[] = {
  490. AV_PIX_FMT_YUV420P,
  491. AV_PIX_FMT_YUVJ420P,
  492. AV_PIX_FMT_YUV422P,
  493. AV_PIX_FMT_YUVJ422P,
  494. AV_PIX_FMT_YUV444P,
  495. AV_PIX_FMT_YUVJ444P,
  496. AV_PIX_FMT_GBRP,
  497. AV_PIX_FMT_GRAY8,
  498. AV_PIX_FMT_NONE
  499. };
  500. static const enum AVPixelFormat x265_csp_ten[] = {
  501. AV_PIX_FMT_YUV420P,
  502. AV_PIX_FMT_YUVJ420P,
  503. AV_PIX_FMT_YUV422P,
  504. AV_PIX_FMT_YUVJ422P,
  505. AV_PIX_FMT_YUV444P,
  506. AV_PIX_FMT_YUVJ444P,
  507. AV_PIX_FMT_GBRP,
  508. AV_PIX_FMT_YUV420P10,
  509. AV_PIX_FMT_YUV422P10,
  510. AV_PIX_FMT_YUV444P10,
  511. AV_PIX_FMT_GBRP10,
  512. AV_PIX_FMT_GRAY8,
  513. AV_PIX_FMT_GRAY10,
  514. AV_PIX_FMT_NONE
  515. };
  516. static const enum AVPixelFormat x265_csp_twelve[] = {
  517. AV_PIX_FMT_YUV420P,
  518. AV_PIX_FMT_YUVJ420P,
  519. AV_PIX_FMT_YUV422P,
  520. AV_PIX_FMT_YUVJ422P,
  521. AV_PIX_FMT_YUV444P,
  522. AV_PIX_FMT_YUVJ444P,
  523. AV_PIX_FMT_GBRP,
  524. AV_PIX_FMT_YUV420P10,
  525. AV_PIX_FMT_YUV422P10,
  526. AV_PIX_FMT_YUV444P10,
  527. AV_PIX_FMT_GBRP10,
  528. AV_PIX_FMT_YUV420P12,
  529. AV_PIX_FMT_YUV422P12,
  530. AV_PIX_FMT_YUV444P12,
  531. AV_PIX_FMT_GBRP12,
  532. AV_PIX_FMT_GRAY8,
  533. AV_PIX_FMT_GRAY10,
  534. AV_PIX_FMT_GRAY12,
  535. AV_PIX_FMT_NONE
  536. };
  537. static av_cold void libx265_encode_init_csp(AVCodec *codec)
  538. {
  539. if (x265_api_get(12))
  540. codec->pix_fmts = x265_csp_twelve;
  541. else if (x265_api_get(10))
  542. codec->pix_fmts = x265_csp_ten;
  543. else if (x265_api_get(8))
  544. codec->pix_fmts = x265_csp_eight;
  545. }
  546. #define OFFSET(x) offsetof(libx265Context, x)
  547. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  548. static const AVOption options[] = {
  549. { "crf", "set the x265 crf", OFFSET(crf), AV_OPT_TYPE_FLOAT, { .dbl = -1 }, -1, FLT_MAX, VE },
  550. { "qp", "set the x265 qp", OFFSET(cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },
  551. { "forced-idr", "if forcing keyframes, force them as IDR frames", OFFSET(forced_idr),AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  552. { "preset", "set the x265 preset", OFFSET(preset), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  553. { "tune", "set the x265 tune parameter", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  554. { "profile", "set the x265 profile", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  555. { "x265-params", "set the x265 configuration using a :-separated list of key=value parameters", OFFSET(x265_opts), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
  556. { NULL }
  557. };
  558. static const AVClass class = {
  559. .class_name = "libx265",
  560. .item_name = av_default_item_name,
  561. .option = options,
  562. .version = LIBAVUTIL_VERSION_INT,
  563. };
  564. static const AVCodecDefault x265_defaults[] = {
  565. { "b", "0" },
  566. { "bf", "-1" },
  567. { "g", "-1" },
  568. { "keyint_min", "-1" },
  569. { "refs", "-1" },
  570. { "qmin", "-1" },
  571. { "qmax", "-1" },
  572. { "qdiff", "-1" },
  573. { "qblur", "-1" },
  574. { "qcomp", "-1" },
  575. { "i_qfactor", "-1" },
  576. { "b_qfactor", "-1" },
  577. { NULL },
  578. };
  579. AVCodec ff_libx265_encoder = {
  580. .name = "libx265",
  581. .long_name = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
  582. .type = AVMEDIA_TYPE_VIDEO,
  583. .id = AV_CODEC_ID_HEVC,
  584. .init = libx265_encode_init,
  585. .init_static_data = libx265_encode_init_csp,
  586. .encode2 = libx265_encode_frame,
  587. .close = libx265_encode_close,
  588. .priv_data_size = sizeof(libx265Context),
  589. .priv_class = &class,
  590. .defaults = x265_defaults,
  591. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  592. .wrapper_name = "libx265",
  593. };