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.

311 lines
9.5KB

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