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.

281 lines
8.3KB

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