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.

325 lines
9.6KB

  1. /*
  2. * libx265 encoder
  3. *
  4. * Copyright (c) 2013-2014 Derek Buitenhuis
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 sar_num, sar_den;
  69. int nnal;
  70. int ret;
  71. int i;
  72. if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL &&
  73. !av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_w &&
  74. !av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_h) {
  75. av_log(avctx, AV_LOG_ERROR,
  76. "4:4:4 support is not fully defined for HEVC yet. "
  77. "Set -strict experimental to encode anyway.\n");
  78. return AVERROR(ENOSYS);
  79. }
  80. avctx->coded_frame = av_frame_alloc();
  81. if (!avctx->coded_frame) {
  82. av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
  83. return AVERROR(ENOMEM);
  84. }
  85. ctx->params = x265_param_alloc();
  86. if (!ctx->params) {
  87. av_log(avctx, AV_LOG_ERROR, "Could not allocate x265 param structure.\n");
  88. return AVERROR(ENOMEM);
  89. }
  90. if (x265_param_default_preset(ctx->params, ctx->preset, ctx->tune) < 0) {
  91. av_log(avctx, AV_LOG_ERROR, "Invalid preset or tune.\n");
  92. return AVERROR(EINVAL);
  93. }
  94. ctx->params->frameNumThreads = avctx->thread_count;
  95. ctx->params->fpsNum = avctx->time_base.den;
  96. ctx->params->fpsDenom = avctx->time_base.num * avctx->ticks_per_frame;
  97. ctx->params->sourceWidth = avctx->width;
  98. ctx->params->sourceHeight = avctx->height;
  99. av_reduce(&sar_num, &sar_den,
  100. avctx->sample_aspect_ratio.num,
  101. avctx->sample_aspect_ratio.den, 4096);
  102. ctx->params->bEnableVuiParametersPresentFlag = 1;
  103. ctx->params->bEnableAspectRatioIdc = 1;
  104. ctx->params->aspectRatioIdc = 255;
  105. ctx->params->sarWidth = sar_num;
  106. ctx->params->sarHeight = sar_den;
  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 (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. ret = x265_encoder_headers(ctx->encoder, &nal, &nnal);
  154. if (ret < 0) {
  155. av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
  156. libx265_encode_close(avctx);
  157. return AVERROR_INVALIDDATA;
  158. }
  159. for (i = 0; i < nnal; i++)
  160. ctx->header_size += nal[i].sizeBytes;
  161. ctx->header = av_malloc(ctx->header_size);
  162. if (!ctx->header) {
  163. av_log(avctx, AV_LOG_ERROR,
  164. "Cannot allocate HEVC header of size %d.\n", ctx->header_size);
  165. libx265_encode_close(avctx);
  166. return AVERROR(ENOMEM);
  167. }
  168. buf = ctx->header;
  169. for (i = 0; i < nnal; i++) {
  170. memcpy(buf, nal[i].payload, nal[i].sizeBytes);
  171. buf += nal[i].sizeBytes;
  172. }
  173. return 0;
  174. }
  175. static int libx265_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  176. const AVFrame *pic, int *got_packet)
  177. {
  178. libx265Context *ctx = avctx->priv_data;
  179. x265_picture x265pic;
  180. x265_picture x265pic_out = { { 0 } };
  181. x265_nal *nal;
  182. uint8_t *dst;
  183. int payload = 0;
  184. int nnal;
  185. int ret;
  186. int i;
  187. x265_picture_init(ctx->params, &x265pic);
  188. if (pic) {
  189. for (i = 0; i < 3; i++) {
  190. x265pic.planes[i] = pic->data[i];
  191. x265pic.stride[i] = pic->linesize[i];
  192. }
  193. x265pic.pts = pic->pts;
  194. x265pic.bitDepth = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1;
  195. }
  196. ret = x265_encoder_encode(ctx->encoder, &nal, &nnal,
  197. pic ? &x265pic : NULL, &x265pic_out);
  198. if (ret < 0)
  199. return AVERROR_UNKNOWN;
  200. if (!nnal)
  201. return 0;
  202. for (i = 0; i < nnal; i++)
  203. payload += nal[i].sizeBytes;
  204. payload += ctx->header_size;
  205. ret = ff_alloc_packet(pkt, payload);
  206. if (ret < 0) {
  207. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  208. return ret;
  209. }
  210. dst = pkt->data;
  211. if (ctx->header) {
  212. memcpy(dst, ctx->header, ctx->header_size);
  213. dst += ctx->header_size;
  214. av_freep(&ctx->header);
  215. ctx->header_size = 0;
  216. }
  217. for (i = 0; i < nnal; i++) {
  218. memcpy(dst, nal[i].payload, nal[i].sizeBytes);
  219. dst += nal[i].sizeBytes;
  220. if (is_keyframe(nal[i].type))
  221. pkt->flags |= AV_PKT_FLAG_KEY;
  222. }
  223. pkt->pts = x265pic_out.pts;
  224. pkt->dts = x265pic_out.dts;
  225. *got_packet = 1;
  226. return 0;
  227. }
  228. static const enum AVPixelFormat x265_csp_eight[] = {
  229. AV_PIX_FMT_YUV420P,
  230. AV_PIX_FMT_YUV444P,
  231. AV_PIX_FMT_NONE
  232. };
  233. static const enum AVPixelFormat x265_csp_twelve[] = {
  234. AV_PIX_FMT_YUV420P,
  235. AV_PIX_FMT_YUV444P,
  236. AV_PIX_FMT_YUV420P10,
  237. AV_PIX_FMT_YUV444P10,
  238. AV_PIX_FMT_NONE
  239. };
  240. static av_cold void libx265_encode_init_csp(AVCodec *codec)
  241. {
  242. if (x265_max_bit_depth == 8)
  243. codec->pix_fmts = x265_csp_eight;
  244. else if (x265_max_bit_depth == 12)
  245. codec->pix_fmts = x265_csp_twelve;
  246. }
  247. #define OFFSET(x) offsetof(libx265Context, x)
  248. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  249. static const AVOption options[] = {
  250. { "preset", "set the x265 preset", OFFSET(preset), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  251. { "tune", "set the x265 tune parameter", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
  252. { "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 },
  253. { NULL }
  254. };
  255. static const AVClass class = {
  256. .class_name = "libx265",
  257. .item_name = av_default_item_name,
  258. .option = options,
  259. .version = LIBAVUTIL_VERSION_INT,
  260. };
  261. AVCodec ff_libx265_encoder = {
  262. .name = "libx265",
  263. .long_name = NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
  264. .type = AVMEDIA_TYPE_VIDEO,
  265. .id = AV_CODEC_ID_HEVC,
  266. .init = libx265_encode_init,
  267. .init_static_data = libx265_encode_init_csp,
  268. .encode2 = libx265_encode_frame,
  269. .close = libx265_encode_close,
  270. .priv_data_size = sizeof(libx265Context),
  271. .priv_class = &class,
  272. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
  273. };