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.

315 lines
9.1KB

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