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.

301 lines
9.2KB

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