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.

311 lines
9.4KB

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