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.

300 lines
9.1KB

  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/error.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/opt.h"
  31. #include "avcodec.h"
  32. #include "internal.h"
  33. typedef struct LibkvazaarContext {
  34. const AVClass *class;
  35. const kvz_api *api;
  36. kvz_encoder *encoder;
  37. kvz_config *config;
  38. char *kvz_params;
  39. } LibkvazaarContext;
  40. static av_cold int libkvazaar_init(AVCodecContext *avctx)
  41. {
  42. LibkvazaarContext *const ctx = avctx->priv_data;
  43. const kvz_api *const api = ctx->api = kvz_api_get(8);
  44. kvz_config *cfg = NULL;
  45. kvz_encoder *enc = NULL;
  46. /* Kvazaar requires width and height to be multiples of eight. */
  47. if (avctx->width % 8 || avctx->height % 8) {
  48. av_log(avctx, AV_LOG_ERROR,
  49. "Video dimensions are not a multiple of 8 (%dx%d).\n",
  50. avctx->width, avctx->height);
  51. return AVERROR(ENOSYS);
  52. }
  53. ctx->config = cfg = api->config_alloc();
  54. if (!cfg) {
  55. av_log(avctx, AV_LOG_ERROR,
  56. "Could not allocate kvazaar config structure.\n");
  57. return AVERROR(ENOMEM);
  58. }
  59. if (!api->config_init(cfg)) {
  60. av_log(avctx, AV_LOG_ERROR,
  61. "Could not initialize kvazaar config structure.\n");
  62. return AVERROR_BUG;
  63. }
  64. cfg->width = avctx->width;
  65. cfg->height = avctx->height;
  66. cfg->framerate =
  67. avctx->time_base.den / (double)(avctx->time_base.num * avctx->ticks_per_frame);
  68. cfg->target_bitrate = avctx->bit_rate;
  69. cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
  70. cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
  71. if (ctx->kvz_params) {
  72. AVDictionary *dict = NULL;
  73. if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
  74. AVDictionaryEntry *entry = NULL;
  75. while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
  76. if (!api->config_parse(cfg, entry->key, entry->value)) {
  77. av_log(avctx, AV_LOG_WARNING, "Invalid option: %s=%s.\n",
  78. entry->key, entry->value);
  79. }
  80. }
  81. av_dict_free(&dict);
  82. }
  83. }
  84. ctx->encoder = enc = api->encoder_open(cfg);
  85. if (!enc) {
  86. av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
  87. return AVERROR_BUG;
  88. }
  89. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  90. kvz_data_chunk *data_out = NULL;
  91. kvz_data_chunk *chunk = NULL;
  92. uint32_t len_out;
  93. uint8_t *p;
  94. if (!api->encoder_headers(enc, &data_out, &len_out))
  95. return AVERROR(ENOMEM);
  96. avctx->extradata = p = av_mallocz(len_out + AV_INPUT_BUFFER_PADDING_SIZE);
  97. if (!p) {
  98. ctx->api->chunk_free(data_out);
  99. return AVERROR(ENOMEM);
  100. }
  101. avctx->extradata_size = len_out;
  102. for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
  103. memcpy(p, chunk->data, chunk->len);
  104. p += chunk->len;
  105. }
  106. ctx->api->chunk_free(data_out);
  107. }
  108. return 0;
  109. }
  110. static av_cold int libkvazaar_close(AVCodecContext *avctx)
  111. {
  112. LibkvazaarContext *ctx = avctx->priv_data;
  113. if (ctx->api) {
  114. ctx->api->encoder_close(ctx->encoder);
  115. ctx->api->config_destroy(ctx->config);
  116. }
  117. if (avctx->extradata)
  118. av_freep(&avctx->extradata);
  119. return 0;
  120. }
  121. static int libkvazaar_encode(AVCodecContext *avctx,
  122. AVPacket *avpkt,
  123. const AVFrame *frame,
  124. int *got_packet_ptr)
  125. {
  126. LibkvazaarContext *ctx = avctx->priv_data;
  127. kvz_picture *input_pic = NULL;
  128. kvz_picture *recon_pic = NULL;
  129. kvz_frame_info frame_info;
  130. kvz_data_chunk *data_out = NULL;
  131. uint32_t len_out = 0;
  132. int retval = 0;
  133. *got_packet_ptr = 0;
  134. if (frame) {
  135. if (frame->width != ctx->config->width ||
  136. frame->height != ctx->config->height) {
  137. av_log(avctx, AV_LOG_ERROR,
  138. "Changing video dimensions during encoding is not supported. "
  139. "(changed from %dx%d to %dx%d)\n",
  140. ctx->config->width, ctx->config->height,
  141. frame->width, frame->height);
  142. retval = AVERROR_INVALIDDATA;
  143. goto done;
  144. }
  145. if (frame->format != avctx->pix_fmt) {
  146. av_log(avctx, AV_LOG_ERROR,
  147. "Changing pixel format during encoding is not supported. "
  148. "(changed from %s to %s)\n",
  149. av_get_pix_fmt_name(avctx->pix_fmt),
  150. av_get_pix_fmt_name(frame->format));
  151. retval = AVERROR_INVALIDDATA;
  152. goto done;
  153. }
  154. // Allocate input picture for kvazaar.
  155. input_pic = ctx->api->picture_alloc(frame->width, frame->height);
  156. if (!input_pic) {
  157. av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
  158. retval = AVERROR(ENOMEM);
  159. goto done;
  160. }
  161. // Copy pixels from frame to input_pic.
  162. {
  163. int dst_linesizes[4] = {
  164. frame->width,
  165. frame->width / 2,
  166. frame->width / 2,
  167. 0
  168. };
  169. av_image_copy(input_pic->data, dst_linesizes,
  170. frame->data, frame->linesize,
  171. frame->format, frame->width, frame->height);
  172. }
  173. input_pic->pts = frame->pts;
  174. }
  175. retval = ctx->api->encoder_encode(ctx->encoder,
  176. input_pic,
  177. &data_out, &len_out,
  178. &recon_pic, NULL,
  179. &frame_info);
  180. if (!retval) {
  181. av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
  182. retval = AVERROR_INVALIDDATA;
  183. goto done;
  184. }
  185. else
  186. retval = 0; /* kvazaar returns 1 on success */
  187. if (data_out) {
  188. kvz_data_chunk *chunk = NULL;
  189. uint64_t written = 0;
  190. retval = ff_alloc_packet(avpkt, len_out);
  191. if (retval < 0) {
  192. av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
  193. goto done;
  194. }
  195. for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
  196. av_assert0(written + chunk->len <= len_out);
  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. };