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.

305 lines
9.3KB

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