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.

314 lines
9.5KB

  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. char sar[12];
  68. int sar_num, sar_den;
  69. int nnal;
  70. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL &&
  71. !av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_w &&
  72. !av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_h) {
  73. av_log(avctx, AV_LOG_ERROR,
  74. "4:4:4 support is not fully defined for HEVC yet. "
  75. "Set -strict experimental to encode anyway.\n");
  76. return AVERROR(ENOSYS);
  77. }
  78. avctx->coded_frame = av_frame_alloc();
  79. if (!avctx->coded_frame) {
  80. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
  81. return AVERROR(ENOMEM);
  82. }
  83. ctx->params = x265_param_alloc();
  84. if (!ctx->params) {
  85. av_log(avctx, AV_LOG_ERROR, "Could not allocate x265 param structure.\n");
  86. return AVERROR(ENOMEM);
  87. }
  88. if (x265_param_default_preset(ctx->params, ctx->preset, ctx->tune) < 0) {
  89. av_log(avctx, AV_LOG_ERROR, "Invalid preset or tune.\n");
  90. return AVERROR(EINVAL);
  91. }
  92. ctx->params->frameNumThreads = avctx->thread_count;
  93. ctx->params->fpsNum = avctx->time_base.den;
  94. ctx->params->fpsDenom = avctx->time_base.num * avctx->ticks_per_frame;
  95. ctx->params->sourceWidth = avctx->width;
  96. ctx->params->sourceHeight = avctx->height;
  97. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
  98. av_reduce(&sar_num, &sar_den,
  99. avctx->sample_aspect_ratio.num,
  100. avctx->sample_aspect_ratio.den, 65535);
  101. snprintf(sar, sizeof(sar), "%d:%d", sar_num, sar_den);
  102. if (x265_param_parse(ctx->params, "sar", sar) == X265_PARAM_BAD_VALUE) {
  103. av_log(avctx, AV_LOG_ERROR, "Invalid SAR: %d:%d.\n", sar_num, sar_den);
  104. return AVERROR_INVALIDDATA;
  105. }
  106. }
  107. if (x265_max_bit_depth == 8)
  108. ctx->params->internalBitDepth = 8;
  109. else if (x265_max_bit_depth == 12)
  110. ctx->params->internalBitDepth = 10;
  111. switch (avctx->pix_fmt) {
  112. case AV_PIX_FMT_YUV420P:
  113. case AV_PIX_FMT_YUV420P10:
  114. ctx->params->internalCsp = X265_CSP_I420;
  115. break;
  116. case AV_PIX_FMT_YUV444P:
  117. case AV_PIX_FMT_YUV444P10:
  118. ctx->params->internalCsp = X265_CSP_I444;
  119. break;
  120. }
  121. if (avctx->bit_rate > 0) {
  122. ctx->params->rc.bitrate = avctx->bit_rate / 1000;
  123. ctx->params->rc.rateControlMode = X265_RC_ABR;
  124. }
  125. if (!(avctx->flags & CODEC_FLAG_GLOBAL_HEADER))
  126. ctx->params->bRepeatHeaders = 1;
  127. if (ctx->x265_opts) {
  128. AVDictionary *dict = NULL;
  129. AVDictionaryEntry *en = NULL;
  130. if (!av_dict_parse_string(&dict, ctx->x265_opts, "=", ":", 0)) {
  131. while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
  132. int parse_ret = x265_param_parse(ctx->params, en->key, en->value);
  133. switch (parse_ret) {
  134. case X265_PARAM_BAD_NAME:
  135. av_log(avctx, AV_LOG_WARNING,
  136. "Unknown option: %s.\n", en->key);
  137. break;
  138. case X265_PARAM_BAD_VALUE:
  139. av_log(avctx, AV_LOG_WARNING,
  140. "Invalid value for %s: %s.\n", en->key, en->value);
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. av_dict_free(&dict);
  147. }
  148. }
  149. ctx->encoder = x265_encoder_open(ctx->params);
  150. if (!ctx->encoder) {
  151. av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
  152. libx265_encode_close(avctx);
  153. return AVERROR_INVALIDDATA;
  154. }
  155. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  156. avctx->extradata_size = x265_encoder_headers(ctx->encoder, &nal, &nnal);
  157. if (avctx->extradata_size <= 0) {
  158. av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
  159. libx265_encode_close(avctx);
  160. return AVERROR_INVALIDDATA;
  161. }
  162. avctx->extradata = av_malloc(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  163. if (!avctx->extradata) {
  164. av_log(avctx, AV_LOG_ERROR,
  165. "Cannot allocate HEVC header of size %d.\n", avctx->extradata_size);
  166. libx265_encode_close(avctx);
  167. return AVERROR(ENOMEM);
  168. }
  169. memcpy(avctx->extradata, nal[0].payload, avctx->extradata_size);
  170. }
  171. return 0;
  172. }
  173. static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  174. const AVFrame *pic, int *got_packet)
  175. {
  176. libx265Context *ctx = avctx->priv_data;
  177. x265_picture x265pic;
  178. x265_picture x265pic_out = { { 0 } };
  179. x265_nal *nal;
  180. uint8_t *dst;
  181. int payload = 0;
  182. int nnal;
  183. int ret;
  184. int i;
  185. x265_picture_init(ctx->params, &x265pic);
  186. if (pic) {
  187. for (i = 0; i < 3; i++) {
  188. x265pic.planes[i] = pic->data[i];
  189. x265pic.stride[i] = pic->linesize[i];
  190. }
  191. x265pic.pts = pic->pts;
  192. x265pic.bitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1;
  193. }
  194. ret = x265_encoder_encode(ctx->encoder, &nal, &nnal,
  195. pic ? &x265pic : NULL, &x265pic_out);
  196. if (ret < 0)
  197. return AVERROR_UNKNOWN;
  198. if (!nnal)
  199. return 0;
  200. for (i = 0; i < nnal; i++)
  201. payload += nal[i].sizeBytes;
  202. ret = ff_alloc_packet(pkt, payload);
  203. if (ret < 0) {
  204. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  205. return ret;
  206. }
  207. dst = pkt->data;
  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. };