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.

249 lines
7.2KB

  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. LibkvazaarContext *ctx = avctx->priv_data;
  122. *got_packet_ptr = 0;
  123. if (frame) {
  124. int i = 0;
  125. av_assert0(frame->width == ctx->config->width);
  126. av_assert0(frame->height == ctx->config->height);
  127. av_assert0(frame->format == avctx->pix_fmt);
  128. // Allocate input picture for kvazaar.
  129. img_in = ctx->api->picture_alloc(frame->width, frame->height);
  130. if (!img_in) {
  131. av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
  132. retval = AVERROR(ENOMEM);
  133. goto done;
  134. }
  135. // Copy pixels from frame to img_in.
  136. for (i = 0; i < 3; ++i) {
  137. uint8_t *dst = img_in->data[i];
  138. uint8_t *src = frame->data[i];
  139. int width = (i == 0) ? frame->width : (frame->width / 2);
  140. int height = (i == 0) ? frame->height : (frame->height / 2);
  141. int y = 0;
  142. for (y = 0; y < height; ++y) {
  143. memcpy(dst, src, width);
  144. src += frame->linesize[i];
  145. dst += width;
  146. }
  147. }
  148. }
  149. if (!ctx->api->encoder_encode(ctx->encoder, img_in, &data_out, &len_out, NULL)) {
  150. av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
  151. retval = AVERROR_EXTERNAL;
  152. goto done;
  153. }
  154. if (data_out) {
  155. kvz_data_chunk *chunk = NULL;
  156. uint64_t written = 0;
  157. retval = ff_alloc_packet(avpkt, len_out);
  158. if (retval < 0) {
  159. av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
  160. goto done;
  161. }
  162. for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
  163. av_assert0(written + chunk->len <= len_out);
  164. memcpy(avpkt->data + written, chunk->data, chunk->len);
  165. written += chunk->len;
  166. }
  167. *got_packet_ptr = 1;
  168. ctx->api->chunk_free(data_out);
  169. data_out = NULL;
  170. }
  171. done:
  172. if (img_in) ctx->api->picture_free(img_in);
  173. if (data_out) ctx->api->chunk_free(data_out);
  174. return retval;
  175. }
  176. static const enum AVPixelFormat pix_fmts[] = {
  177. AV_PIX_FMT_YUV420P,
  178. AV_PIX_FMT_NONE
  179. };
  180. static const AVOption options[] = {
  181. { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of name=value pairs.",
  182. offsetof(LibkvazaarContext, kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0,
  183. AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
  184. { NULL },
  185. };
  186. static const AVClass class = {
  187. .class_name = "libkvazaar",
  188. .item_name = av_default_item_name,
  189. .option = options,
  190. .version = LIBAVUTIL_VERSION_INT,
  191. };
  192. static const AVCodecDefault defaults[] = {
  193. { "b", "0" },
  194. { NULL },
  195. };
  196. AVCodec ff_libkvazaar_encoder = {
  197. .name = "libkvazaar",
  198. .long_name = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
  199. .type = AVMEDIA_TYPE_VIDEO,
  200. .id = AV_CODEC_ID_HEVC,
  201. .capabilities = AV_CODEC_CAP_DELAY,
  202. .pix_fmts = pix_fmts,
  203. .priv_class = &class,
  204. .priv_data_size = sizeof(LibkvazaarContext),
  205. .defaults = defaults,
  206. .init = libkvazaar_init,
  207. .encode2 = libkvazaar_encode,
  208. .close = libkvazaar_close,
  209. };