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