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.

329 lines
9.6KB

  1. /*
  2. * libx265 encoder
  3. *
  4. * Copyright (c) 2013-2014 Derek Buitenhuis
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <x265.h>
  23. #include "libavutil/internal.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. #if defined(_MSC_VER)
  30. #define X265_API_IMPORTS 1
  31. #endif
  32. typedef struct libx265Context {
  33. const AVClass *class;
  34. x265_encoder *encoder;
  35. x265_param *params;
  36. uint8_t *header;
  37. int header_size;
  38. char *preset;
  39. char *tune;
  40. char *x265_opts;
  41. } libx265Context;
  42. static int is_keyframe(NalUnitType naltype)
  43. {
  44. switch (naltype) {
  45. case NAL_UNIT_CODED_SLICE_BLA_W_LP:
  46. case NAL_UNIT_CODED_SLICE_BLA_W_RADL:
  47. case NAL_UNIT_CODED_SLICE_BLA_N_LP:
  48. case NAL_UNIT_CODED_SLICE_IDR_W_RADL:
  49. case NAL_UNIT_CODED_SLICE_IDR_N_LP:
  50. case NAL_UNIT_CODED_SLICE_CRA:
  51. return 1;
  52. default:
  53. return 0;
  54. }
  55. }
  56. static av_cold int libx265_encode_close(AVCodecContext *avctx)
  57. {
  58. libx265Context *ctx = avctx->priv_data;
  59. av_frame_free(&avctx->coded_frame);
  60. av_freep(&ctx->header);
  61. x265_param_free(ctx->params);
  62. if (ctx->encoder)
  63. x265_encoder_close(ctx->encoder);
  64. return 0;
  65. }
  66. static av_cold int libx265_encode_init(AVCodecContext *avctx)
  67. {
  68. libx265Context *ctx = avctx->priv_data;
  69. x265_nal *nal;
  70. uint8_t *buf;
  71. int sar_num, sar_den;
  72. int nnal;
  73. int ret;
  74. int i;
  75. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL &&
  76. !av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_w &&
  77. !av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_h) {
  78. av_log(avctx, AV_LOG_ERROR,
  79. "4:4:4 support is not fully defined for HEVC yet. "
  80. "Set -strict experimental to encode anyway.\n");
  81. return AVERROR(ENOSYS);
  82. }
  83. avctx->coded_frame = av_frame_alloc();
  84. if (!avctx->coded_frame) {
  85. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
  86. return AVERROR(ENOMEM);
  87. }
  88. ctx->params = x265_param_alloc();
  89. if (!ctx->params) {
  90. av_log(avctx, AV_LOG_ERROR, "Could not allocate x265 param structure.\n");
  91. return AVERROR(ENOMEM);
  92. }
  93. if (x265_param_default_preset(ctx->params, ctx->preset, ctx->tune) < 0) {
  94. av_log(avctx, AV_LOG_ERROR, "Invalid preset or tune.\n");
  95. return AVERROR(EINVAL);
  96. }
  97. ctx->params->frameNumThreads = avctx->thread_count;
  98. ctx->params->fpsNum = avctx->time_base.den;
  99. ctx->params->fpsDenom = avctx->time_base.num * avctx->ticks_per_frame;
  100. ctx->params->sourceWidth = avctx->width;
  101. ctx->params->sourceHeight = avctx->height;
  102. av_reduce(&sar_num, &sar_den,
  103. avctx->sample_aspect_ratio.num,
  104. avctx->sample_aspect_ratio.den, 4096);
  105. ctx->params->vui.bEnableVuiParametersPresentFlag = 1;
  106. ctx->params->vui.bEnableAspectRatioIdc = 1;
  107. ctx->params->vui.aspectRatioIdc = 255;
  108. ctx->params->vui.sarWidth = sar_num;
  109. ctx->params->vui.sarHeight = sar_den;
  110. if (x265_max_bit_depth == 8)
  111. ctx->params->internalBitDepth = 8;
  112. else if (x265_max_bit_depth == 12)
  113. ctx->params->internalBitDepth = 10;
  114. switch (avctx->pix_fmt) {
  115. case AV_PIX_FMT_YUV420P:
  116. case AV_PIX_FMT_YUV420P10:
  117. ctx->params->internalCsp = X265_CSP_I420;
  118. break;
  119. case AV_PIX_FMT_YUV444P:
  120. case AV_PIX_FMT_YUV444P10:
  121. ctx->params->internalCsp = X265_CSP_I444;
  122. break;
  123. }
  124. if (avctx->bit_rate > 0) {
  125. ctx->params->rc.bitrate = avctx->bit_rate / 1000;
  126. ctx->params->rc.rateControlMode = X265_RC_ABR;
  127. }
  128. if (ctx->x265_opts) {
  129. AVDictionary *dict = NULL;
  130. AVDictionaryEntry *en = NULL;
  131. if (!av_dict_parse_string(&dict, ctx->x265_opts, "=", ":", 0)) {
  132. while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
  133. int parse_ret = x265_param_parse(ctx->params, en->key, en->value);
  134. switch (parse_ret) {
  135. case X265_PARAM_BAD_NAME:
  136. av_log(avctx, AV_LOG_WARNING,
  137. "Unknown option: %s.\n", en->key);
  138. break;
  139. case X265_PARAM_BAD_VALUE:
  140. av_log(avctx, AV_LOG_WARNING,
  141. "Invalid value for %s: %s.\n", en->key, en->value);
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. av_dict_free(&dict);
  148. }
  149. }
  150. ctx->encoder = x265_encoder_open(ctx->params);
  151. if (!ctx->encoder) {
  152. av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
  153. libx265_encode_close(avctx);
  154. return AVERROR_INVALIDDATA;
  155. }
  156. ret = x265_encoder_headers(ctx->encoder, &nal, &nnal);
  157. if (ret < 0) {
  158. av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
  159. libx265_encode_close(avctx);
  160. return AVERROR_INVALIDDATA;
  161. }
  162. for (i = 0; i < nnal; i++)
  163. ctx->header_size += nal[i].sizeBytes;
  164. ctx->header = av_malloc(ctx->header_size);
  165. if (!ctx->header) {
  166. av_log(avctx, AV_LOG_ERROR,
  167. "Cannot allocate HEVC header of size %d.\n", ctx->header_size);
  168. libx265_encode_close(avctx);
  169. return AVERROR(ENOMEM);
  170. }
  171. buf = ctx->header;
  172. for (i = 0; i < nnal; i++) {
  173. memcpy(buf, nal[i].payload, nal[i].sizeBytes);
  174. buf += nal[i].sizeBytes;
  175. }
  176. return 0;
  177. }
  178. static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  179. const AVFrame *pic, int *got_packet)
  180. {
  181. libx265Context *ctx = avctx->priv_data;
  182. x265_picture x265pic;
  183. x265_picture x265pic_out = { { 0 } };
  184. x265_nal *nal;
  185. uint8_t *dst;
  186. int payload = 0;
  187. int nnal;
  188. int ret;
  189. int i;
  190. x265_picture_init(ctx->params, &x265pic);
  191. if (pic) {
  192. for (i = 0; i < 3; i++) {
  193. x265pic.planes[i] = pic->data[i];
  194. x265pic.stride[i] = pic->linesize[i];
  195. }
  196. x265pic.pts = pic->pts;
  197. x265pic.bitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1;
  198. }
  199. ret = x265_encoder_encode(ctx->encoder, &nal, &nnal,
  200. pic ? &x265pic : NULL, &x265pic_out);
  201. if (ret < 0)
  202. return AVERROR_UNKNOWN;
  203. if (!nnal)
  204. return 0;
  205. for (i = 0; i < nnal; i++)
  206. payload += nal[i].sizeBytes;
  207. payload += ctx->header_size;
  208. ret = ff_alloc_packet(pkt, payload);
  209. if (ret < 0) {
  210. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  211. return ret;
  212. }
  213. dst = pkt->data;
  214. if (ctx->header) {
  215. memcpy(dst, ctx->header, ctx->header_size);
  216. dst += ctx->header_size;
  217. av_freep(&ctx->header);
  218. ctx->header_size = 0;
  219. }
  220. for (i = 0; i < nnal; i++) {
  221. memcpy(dst, nal[i].payload, nal[i].sizeBytes);
  222. dst += nal[i].sizeBytes;
  223. if (is_keyframe(nal[i].type))
  224. pkt->flags |= AV_PKT_FLAG_KEY;
  225. }
  226. pkt->pts = x265pic_out.pts;
  227. pkt->dts = x265pic_out.dts;
  228. *got_packet = 1;
  229. return 0;
  230. }
  231. static const enum AVPixelFormat x265_csp_eight[] = {
  232. AV_PIX_FMT_YUV420P,
  233. AV_PIX_FMT_YUV444P,
  234. AV_PIX_FMT_NONE
  235. };
  236. static const enum AVPixelFormat x265_csp_twelve[] = {
  237. AV_PIX_FMT_YUV420P,
  238. AV_PIX_FMT_YUV444P,
  239. AV_PIX_FMT_YUV420P10,
  240. AV_PIX_FMT_YUV444P10,
  241. AV_PIX_FMT_NONE
  242. };
  243. static av_cold void libx265_encode_init_csp(AVCodec *codec)
  244. {
  245. if (x265_max_bit_depth == 8)
  246. codec->pix_fmts = x265_csp_eight;
  247. else if (x265_max_bit_depth == 12)
  248. codec->pix_fmts = x265_csp_twelve;
  249. }
  250. #define OFFSET(x) offsetof(libx265Context, x)
  251. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  252. static const AVOption options[] = {
  253. { "preset", "set the x265 preset", OFFSET(preset), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  254. { "tune", "set the x265 tune parameter", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  255. { "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 },
  256. { NULL }
  257. };
  258. static const AVClass class = {
  259. .class_name = "libx265",
  260. .item_name = av_default_item_name,
  261. .option = options,
  262. .version = LIBAVUTIL_VERSION_INT,
  263. };
  264. AVCodec ff_libx265_encoder = {
  265. .name = "libx265",
  266. .long_name = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
  267. .type = AVMEDIA_TYPE_VIDEO,
  268. .id = AV_CODEC_ID_HEVC,
  269. .init = libx265_encode_init,
  270. .init_static_data = libx265_encode_init_csp,
  271. .encode2 = libx265_encode_frame,
  272. .close = libx265_encode_close,
  273. .priv_data_size = sizeof(libx265Context),
  274. .priv_class = &class,
  275. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
  276. };