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.

286 lines
8.4KB

  1. /*
  2. * libkvazaar encoder
  3. *
  4. * Copyright (c) 2015 Tampere University of Technology
  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 <kvazaar.h>
  23. #include <string.h>
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "avcodec.h"
  30. #include "internal.h"
  31. typedef struct LibkvazaarContext {
  32. const AVClass *class;
  33. const kvz_api *api;
  34. kvz_encoder *encoder;
  35. kvz_config *config;
  36. char *kvz_params;
  37. } LibkvazaarContext;
  38. static av_cold int libkvazaar_init(AVCodecContext *avctx)
  39. {
  40. int retval = 0;
  41. kvz_config *cfg = NULL;
  42. kvz_encoder *enc = NULL;
  43. const kvz_api *const api = kvz_api_get(8);
  44. LibkvazaarContext *const ctx = avctx->priv_data;
  45. // Kvazaar requires width and height to be multiples of eight.
  46. if (avctx->width % 8 || avctx->height % 8) {
  47. av_log(avctx, AV_LOG_ERROR, "Video dimensions are not a multiple of 8.\n");
  48. retval = AVERROR_INVALIDDATA;
  49. goto done;
  50. }
  51. cfg = api->config_alloc();
  52. if (!cfg) {
  53. av_log(avctx, AV_LOG_ERROR, "Could not allocate kvazaar config structure.\n");
  54. retval = AVERROR(ENOMEM);
  55. goto done;
  56. }
  57. if (!api->config_init(cfg)) {
  58. av_log(avctx, AV_LOG_ERROR, "Could not initialize kvazaar config structure.\n");
  59. retval = AVERROR_EXTERNAL;
  60. goto done;
  61. }
  62. cfg->width = avctx->width;
  63. cfg->height = avctx->height;
  64. cfg->framerate =
  65. (double)(avctx->time_base.num * avctx->ticks_per_frame) / avctx->time_base.den;
  66. cfg->threads = avctx->thread_count;
  67. cfg->target_bitrate = avctx->bit_rate;
  68. cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
  69. cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
  70. if (ctx->kvz_params) {
  71. AVDictionary *dict = NULL;
  72. if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
  73. AVDictionaryEntry *entry = NULL;
  74. while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
  75. if (!api->config_parse(cfg, entry->key, entry->value)) {
  76. av_log(avctx, AV_LOG_WARNING,
  77. "Invalid option: %s=%s.\n",
  78. entry->key, entry->value);
  79. }
  80. }
  81. av_dict_free(&dict);
  82. }
  83. }
  84. enc = api->encoder_open(cfg);
  85. if (!enc) {
  86. av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
  87. retval = AVERROR_EXTERNAL;
  88. goto done;
  89. }
  90. ctx->api = api;
  91. ctx->encoder = enc;
  92. ctx->config = cfg;
  93. enc = NULL;
  94. cfg = NULL;
  95. done:
  96. api->config_destroy(cfg);
  97. api->encoder_close(enc);
  98. return retval;
  99. }
  100. static av_cold int libkvazaar_close(AVCodecContext *avctx)
  101. {
  102. LibkvazaarContext *ctx = avctx->priv_data;
  103. if (!ctx->api) return 0;
  104. if (ctx->encoder) {
  105. ctx->api->encoder_close(ctx->encoder);
  106. ctx->encoder = NULL;
  107. }
  108. if (ctx->config) {
  109. ctx->api->config_destroy(ctx->config);
  110. ctx->config = NULL;
  111. }
  112. return 0;
  113. }
  114. static int libkvazaar_encode(AVCodecContext *avctx,
  115. AVPacket *avpkt,
  116. const AVFrame *frame,
  117. int *got_packet_ptr)
  118. {
  119. int retval = 0;
  120. kvz_picture *img_in = NULL;
  121. kvz_data_chunk *data_out = NULL;
  122. uint32_t len_out = 0;
  123. kvz_picture *recon_pic = NULL;
  124. kvz_frame_info frame_info;
  125. LibkvazaarContext *ctx = avctx->priv_data;
  126. *got_packet_ptr = 0;
  127. if (frame) {
  128. if (frame->width != ctx->config->width ||
  129. frame->height != ctx->config->height) {
  130. av_log(avctx, AV_LOG_ERROR,
  131. "Changing video dimensions during encoding is not supported. "
  132. "(changed from %dx%d to %dx%d)\n",
  133. ctx->config->width, ctx->config->height,
  134. frame->width, frame->height);
  135. retval = AVERROR_INVALIDDATA;
  136. goto done;
  137. }
  138. if (frame->format != avctx->pix_fmt) {
  139. av_log(avctx, AV_LOG_ERROR,
  140. "Changing pixel format during encoding is not supported. "
  141. "(changed from %s to %s)\n",
  142. av_get_pix_fmt_name(avctx->pix_fmt),
  143. av_get_pix_fmt_name(frame->format));
  144. retval = AVERROR_INVALIDDATA;
  145. goto done;
  146. }
  147. // Allocate input picture for kvazaar.
  148. img_in = ctx->api->picture_alloc(frame->width, frame->height);
  149. if (!img_in) {
  150. av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
  151. retval = AVERROR(ENOMEM);
  152. goto done;
  153. }
  154. // Copy pixels from frame to img_in.
  155. {
  156. int dst_linesizes[4] = {
  157. frame->width,
  158. frame->width / 2,
  159. frame->width / 2,
  160. 0
  161. };
  162. av_image_copy(img_in->data, dst_linesizes,
  163. frame->data, frame->linesize,
  164. frame->format, frame->width, frame->height);
  165. }
  166. img_in->pts = frame->pts;
  167. }
  168. if (!ctx->api->encoder_encode(ctx->encoder, img_in,
  169. &data_out, &len_out,
  170. &recon_pic, NULL,
  171. &frame_info)) {
  172. av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
  173. retval = AVERROR_EXTERNAL;
  174. goto done;
  175. }
  176. if (data_out) {
  177. kvz_data_chunk *chunk = NULL;
  178. uint64_t written = 0;
  179. retval = ff_alloc_packet(avpkt, len_out);
  180. if (retval < 0) {
  181. av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
  182. goto done;
  183. }
  184. for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
  185. av_assert0(written + chunk->len <= len_out);
  186. memcpy(avpkt->data + written, chunk->data, chunk->len);
  187. written += chunk->len;
  188. }
  189. *got_packet_ptr = 1;
  190. ctx->api->chunk_free(data_out);
  191. data_out = NULL;
  192. avpkt->pts = recon_pic->pts;
  193. avpkt->dts = recon_pic->dts;
  194. avpkt->flags = 0;
  195. // IRAP VCL NAL unit types span the range
  196. // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
  197. if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
  198. frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
  199. avpkt->flags |= AV_PKT_FLAG_KEY;
  200. }
  201. }
  202. done:
  203. ctx->api->picture_free(img_in);
  204. ctx->api->picture_free(recon_pic);
  205. ctx->api->chunk_free(data_out);
  206. return retval;
  207. }
  208. static const enum AVPixelFormat pix_fmts[] = {
  209. AV_PIX_FMT_YUV420P,
  210. AV_PIX_FMT_NONE
  211. };
  212. static const AVOption options[] = {
  213. { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of name=value pairs.",
  214. offsetof(LibkvazaarContext, kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0,
  215. AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  216. { NULL },
  217. };
  218. static const AVClass class = {
  219. .class_name = "libkvazaar",
  220. .item_name = av_default_item_name,
  221. .option = options,
  222. .version = LIBAVUTIL_VERSION_INT,
  223. };
  224. static const AVCodecDefault defaults[] = {
  225. { "b", "0" },
  226. { NULL },
  227. };
  228. AVCodec ff_libkvazaar_encoder = {
  229. .name = "libkvazaar",
  230. .long_name = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
  231. .type = AVMEDIA_TYPE_VIDEO,
  232. .id = AV_CODEC_ID_HEVC,
  233. .capabilities = AV_CODEC_CAP_DELAY,
  234. .pix_fmts = pix_fmts,
  235. .priv_class = &class,
  236. .priv_data_size = sizeof(LibkvazaarContext),
  237. .defaults = defaults,
  238. .init = libkvazaar_init,
  239. .encode2 = libkvazaar_encode,
  240. .close = libkvazaar_close,
  241. };