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.

312 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. 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. av_reduce(&sar_num, &sar_den,
  98. avctx->sample_aspect_ratio.num,
  99. avctx->sample_aspect_ratio.den, 65535);
  100. snprintf(sar, sizeof(sar), "%d:%d", sar_num, sar_den);
  101. if (x265_param_parse(ctx->params, "sar", sar) == X265_PARAM_BAD_VALUE) {
  102. av_log(avctx, AV_LOG_ERROR, "Invalid SAR: %d:%d.\n", sar_num, sar_den);
  103. return AVERROR_INVALIDDATA;
  104. }
  105. if (x265_max_bit_depth == 8)
  106. ctx->params->internalBitDepth = 8;
  107. else if (x265_max_bit_depth == 12)
  108. ctx->params->internalBitDepth = 10;
  109. switch (avctx->pix_fmt) {
  110. case AV_PIX_FMT_YUV420P:
  111. case AV_PIX_FMT_YUV420P10:
  112. ctx->params->internalCsp = X265_CSP_I420;
  113. break;
  114. case AV_PIX_FMT_YUV444P:
  115. case AV_PIX_FMT_YUV444P10:
  116. ctx->params->internalCsp = X265_CSP_I444;
  117. break;
  118. }
  119. if (avctx->bit_rate > 0) {
  120. ctx->params->rc.bitrate = avctx->bit_rate / 1000;
  121. ctx->params->rc.rateControlMode = X265_RC_ABR;
  122. }
  123. if (!(avctx->flags & CODEC_FLAG_GLOBAL_HEADER))
  124. ctx->params->bRepeatHeaders = 1;
  125. if (ctx->x265_opts) {
  126. AVDictionary *dict = NULL;
  127. AVDictionaryEntry *en = NULL;
  128. if (!av_dict_parse_string(&dict, ctx->x265_opts, "=", ":", 0)) {
  129. while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
  130. int parse_ret = x265_param_parse(ctx->params, en->key, en->value);
  131. switch (parse_ret) {
  132. case X265_PARAM_BAD_NAME:
  133. av_log(avctx, AV_LOG_WARNING,
  134. "Unknown option: %s.\n", en->key);
  135. break;
  136. case X265_PARAM_BAD_VALUE:
  137. av_log(avctx, AV_LOG_WARNING,
  138. "Invalid value for %s: %s.\n", en->key, en->value);
  139. break;
  140. default:
  141. break;
  142. }
  143. }
  144. av_dict_free(&dict);
  145. }
  146. }
  147. ctx->encoder = x265_encoder_open(ctx->params);
  148. if (!ctx->encoder) {
  149. av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
  150. libx265_encode_close(avctx);
  151. return AVERROR_INVALIDDATA;
  152. }
  153. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  154. avctx->extradata_size = x265_encoder_headers(ctx->encoder, &nal, &nnal);
  155. if (avctx->extradata_size <= 0) {
  156. av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
  157. libx265_encode_close(avctx);
  158. return AVERROR_INVALIDDATA;
  159. }
  160. avctx->extradata = av_malloc(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  161. if (!avctx->extradata) {
  162. av_log(avctx, AV_LOG_ERROR,
  163. "Cannot allocate HEVC header of size %d.\n", avctx->extradata_size);
  164. libx265_encode_close(avctx);
  165. return AVERROR(ENOMEM);
  166. }
  167. memcpy(avctx->extradata, nal[0].payload, avctx->extradata_size);
  168. }
  169. return 0;
  170. }
  171. static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  172. const AVFrame *pic, int *got_packet)
  173. {
  174. libx265Context *ctx = avctx->priv_data;
  175. x265_picture x265pic;
  176. x265_picture x265pic_out = { { 0 } };
  177. x265_nal *nal;
  178. uint8_t *dst;
  179. int payload = 0;
  180. int nnal;
  181. int ret;
  182. int i;
  183. x265_picture_init(ctx->params, &x265pic);
  184. if (pic) {
  185. for (i = 0; i < 3; i++) {
  186. x265pic.planes[i] = pic->data[i];
  187. x265pic.stride[i] = pic->linesize[i];
  188. }
  189. x265pic.pts = pic->pts;
  190. x265pic.bitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1;
  191. }
  192. ret = x265_encoder_encode(ctx->encoder, &nal, &nnal,
  193. pic ? &x265pic : NULL, &x265pic_out);
  194. if (ret < 0)
  195. return AVERROR_UNKNOWN;
  196. if (!nnal)
  197. return 0;
  198. for (i = 0; i < nnal; i++)
  199. payload += nal[i].sizeBytes;
  200. ret = ff_alloc_packet(pkt, payload);
  201. if (ret < 0) {
  202. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  203. return ret;
  204. }
  205. dst = pkt->data;
  206. for (i = 0; i < nnal; i++) {
  207. memcpy(dst, nal[i].payload, nal[i].sizeBytes);
  208. dst += nal[i].sizeBytes;
  209. if (is_keyframe(nal[i].type))
  210. pkt->flags |= AV_PKT_FLAG_KEY;
  211. }
  212. pkt->pts = x265pic_out.pts;
  213. pkt->dts = x265pic_out.dts;
  214. *got_packet = 1;
  215. return 0;
  216. }
  217. static const enum AVPixelFormat x265_csp_eight[] = {
  218. AV_PIX_FMT_YUV420P,
  219. AV_PIX_FMT_YUV444P,
  220. AV_PIX_FMT_NONE
  221. };
  222. static const enum AVPixelFormat x265_csp_twelve[] = {
  223. AV_PIX_FMT_YUV420P,
  224. AV_PIX_FMT_YUV444P,
  225. AV_PIX_FMT_YUV420P10,
  226. AV_PIX_FMT_YUV444P10,
  227. AV_PIX_FMT_NONE
  228. };
  229. static av_cold void libx265_encode_init_csp(AVCodec *codec)
  230. {
  231. if (x265_max_bit_depth == 8)
  232. codec->pix_fmts = x265_csp_eight;
  233. else if (x265_max_bit_depth == 12)
  234. codec->pix_fmts = x265_csp_twelve;
  235. }
  236. #define OFFSET(x) offsetof(libx265Context, x)
  237. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  238. static const AVOption options[] = {
  239. { "preset", "set the x265 preset", OFFSET(preset), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  240. { "tune", "set the x265 tune parameter", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  241. { "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 },
  242. { NULL }
  243. };
  244. static const AVClass class = {
  245. .class_name = "libx265",
  246. .item_name = av_default_item_name,
  247. .option = options,
  248. .version = LIBAVUTIL_VERSION_INT,
  249. };
  250. AVCodec ff_libx265_encoder = {
  251. .name = "libx265",
  252. .long_name = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
  253. .type = AVMEDIA_TYPE_VIDEO,
  254. .id = AV_CODEC_ID_HEVC,
  255. .init = libx265_encode_init,
  256. .init_static_data = libx265_encode_init_csp,
  257. .encode2 = libx265_encode_frame,
  258. .close = libx265_encode_close,
  259. .priv_data_size = sizeof(libx265Context),
  260. .priv_class = &class,
  261. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
  262. };