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.

320 lines
10.0KB

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