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.

263 lines
7.6KB

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