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.

285 lines
8.2KB

  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. avctx->coded_frame = av_frame_alloc();
  72. if (!avctx->coded_frame) {
  73. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
  74. return AVERROR(ENOMEM);
  75. }
  76. ctx->params = x265_param_alloc();
  77. if (!ctx->params) {
  78. av_log(avctx, AV_LOG_ERROR, "Could not allocate x265 param structure.\n");
  79. return AVERROR(ENOMEM);
  80. }
  81. x265_param_default(ctx->params);
  82. if (x265_param_default_preset(ctx->params, ctx->preset, ctx->tune) < 0) {
  83. av_log(avctx, AV_LOG_ERROR, "Invalid preset or tune.\n");
  84. return AVERROR(EINVAL);
  85. }
  86. ctx->params->frameNumThreads = avctx->thread_count;
  87. ctx->params->frameRate = (int) (avctx->time_base.den / avctx->time_base.num);
  88. ctx->params->sourceWidth = avctx->width;
  89. ctx->params->sourceHeight = avctx->height;
  90. ctx->params->inputBitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1;
  91. if (avctx->bit_rate > 0) {
  92. ctx->params->rc.bitrate = avctx->bit_rate / 1000;
  93. ctx->params->rc.rateControlMode = X265_RC_ABR;
  94. }
  95. if (ctx->x265_opts) {
  96. AVDictionary *dict = NULL;
  97. AVDictionaryEntry *en = NULL;
  98. if (!av_dict_parse_string(&dict, ctx->x265_opts, "=", ":", 0)) {
  99. while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
  100. int parse_ret = x265_param_parse(ctx->params, en->key, en->value);
  101. switch (parse_ret) {
  102. case X265_PARAM_BAD_NAME:
  103. av_log(avctx, AV_LOG_WARNING,
  104. "Unknown option: %s.\n", en->key);
  105. break;
  106. case X265_PARAM_BAD_VALUE:
  107. av_log(avctx, AV_LOG_WARNING,
  108. "Invalid value for %s: %s.\n", en->key, en->value);
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. av_dict_free(&dict);
  115. }
  116. }
  117. ctx->encoder = x265_encoder_open(ctx->params);
  118. if (!ctx->encoder) {
  119. av_log(avctx, AV_LOG_ERROR, "Cannot open libx265 encoder.\n");
  120. libx265_encode_close(avctx);
  121. return AVERROR_INVALIDDATA;
  122. }
  123. ret = x265_encoder_headers(ctx->encoder, &nal, &nnal);
  124. if (ret < 0) {
  125. av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
  126. libx265_encode_close(avctx);
  127. return AVERROR_INVALIDDATA;
  128. }
  129. for (i = 0; i < nnal; i++)
  130. ctx->header_size += nal[i].sizeBytes;
  131. ctx->header = av_malloc(ctx->header_size);
  132. if (!ctx->header) {
  133. av_log(avctx, AV_LOG_ERROR,
  134. "Cannot allocate HEVC header of size %d.\n", ctx->header_size);
  135. libx265_encode_close(avctx);
  136. return AVERROR(ENOMEM);
  137. }
  138. buf = ctx->header;
  139. for (i = 0; i < nnal; i++) {
  140. memcpy(buf, nal[i].payload, nal[i].sizeBytes);
  141. buf += nal[i].sizeBytes;
  142. }
  143. return 0;
  144. }
  145. static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  146. const AVFrame *pic, int *got_packet)
  147. {
  148. libx265Context *ctx = avctx->priv_data;
  149. x265_picture x265pic;
  150. x265_picture x265pic_out = { { 0 } };
  151. x265_nal *nal;
  152. uint8_t *dst;
  153. int payload = 0;
  154. int nnal;
  155. int ret;
  156. int i;
  157. if (pic) {
  158. for (i = 0; i < 3; i++) {
  159. x265pic.planes[i] = pic->data[i];
  160. x265pic.stride[i] = pic->linesize[i];
  161. }
  162. x265pic.pts = pic->pts;
  163. }
  164. ret = x265_encoder_encode(ctx->encoder, &nal, &nnal,
  165. pic ? &x265pic : NULL, &x265pic_out);
  166. if (ret < 0)
  167. return AVERROR_UNKNOWN;
  168. if (!nnal)
  169. return 0;
  170. for (i = 0; i < nnal; i++)
  171. payload += nal[i].sizeBytes;
  172. payload += ctx->header_size;
  173. ret = ff_alloc_packet(pkt, payload);
  174. if (ret < 0) {
  175. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  176. return ret;
  177. }
  178. dst = pkt->data;
  179. if (ctx->header) {
  180. memcpy(dst, ctx->header, ctx->header_size);
  181. dst += ctx->header_size;
  182. av_freep(&ctx->header);
  183. ctx->header_size = 0;
  184. }
  185. for (i = 0; i < nnal; i++) {
  186. memcpy(dst, nal[i].payload, nal[i].sizeBytes);
  187. dst += nal[i].sizeBytes;
  188. if (is_keyframe(nal[i].type))
  189. pkt->flags |= AV_PKT_FLAG_KEY;
  190. }
  191. pkt->pts = x265pic_out.pts;
  192. pkt->dts = x265pic_out.dts;
  193. *got_packet = 1;
  194. return 0;
  195. }
  196. static const enum AVPixelFormat x265_csp_eight[] = {
  197. AV_PIX_FMT_YUV420P,
  198. AV_PIX_FMT_NONE
  199. };
  200. static const enum AVPixelFormat x265_csp_twelve[] = {
  201. AV_PIX_FMT_YUV420P,
  202. AV_PIX_FMT_YUV420P10,
  203. AV_PIX_FMT_NONE
  204. };
  205. static av_cold void libx265_encode_init_csp(AVCodec *codec)
  206. {
  207. if (x265_max_bit_depth == 8)
  208. codec->pix_fmts = x265_csp_eight;
  209. else if (x265_max_bit_depth == 12)
  210. codec->pix_fmts = x265_csp_twelve;
  211. }
  212. #define OFFSET(x) offsetof(libx265Context, x)
  213. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  214. static const AVOption options[] = {
  215. { "preset", "set the x265 preset", OFFSET(preset), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  216. { "tune", "set the x265 tune parameter", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  217. { "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 },
  218. { NULL }
  219. };
  220. static const AVClass class = {
  221. .class_name = "libx265",
  222. .item_name = av_default_item_name,
  223. .option = options,
  224. .version = LIBAVUTIL_VERSION_INT,
  225. };
  226. AVCodec ff_libx265_encoder = {
  227. .name = "libx265",
  228. .long_name = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
  229. .type = AVMEDIA_TYPE_VIDEO,
  230. .id = AV_CODEC_ID_HEVC,
  231. .init = libx265_encode_init,
  232. .init_static_data = libx265_encode_init_csp,
  233. .encode2 = libx265_encode_frame,
  234. .close = libx265_encode_close,
  235. .priv_data_size = sizeof(libx265Context),
  236. .priv_class = &class,
  237. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
  238. };