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.

205 lines
6.5KB

  1. /*
  2. * Vidvox Hap encoder
  3. * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Hap encoder
  24. *
  25. * Fourcc: Hap1, Hap5, HapY
  26. *
  27. * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
  28. */
  29. #include <stdint.h>
  30. #include "snappy-c.h"
  31. #include "libavutil/frame.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/intreadwrite.h"
  34. #include "libavutil/opt.h"
  35. #include "avcodec.h"
  36. #include "bytestream.h"
  37. #include "hap.h"
  38. #include "internal.h"
  39. #include "texturedsp.h"
  40. /* A fixed header size allows to skip a memcpy */
  41. #define HEADER_SIZE 8
  42. static void compress_texture(AVCodecContext *avctx, const AVFrame *f)
  43. {
  44. HapContext *ctx = avctx->priv_data;
  45. uint8_t *out = ctx->tex_buf;
  46. int i, j;
  47. for (j = 0; j < avctx->height; j += 4) {
  48. for (i = 0; i < avctx->width; i += 4) {
  49. uint8_t *p = f->data[0] + i * 4 + j * f->linesize[0];
  50. const int step = ctx->tex_fun(out, f->linesize[0], p);
  51. out += step;
  52. }
  53. }
  54. }
  55. static int hap_encode(AVCodecContext *avctx, AVPacket *pkt,
  56. const AVFrame *frame, int *got_packet)
  57. {
  58. HapContext *ctx = avctx->priv_data;
  59. size_t final_size = ctx->max_snappy;
  60. int ret, comp = HAP_COMP_SNAPPY;
  61. int pktsize = FFMAX(ctx->tex_size, ctx->max_snappy) + HEADER_SIZE;
  62. /* Allocate maximum size packet, shrink later. */
  63. ret = ff_alloc_packet(pkt, pktsize);
  64. if (ret < 0)
  65. return ret;
  66. /* DXTC compression. */
  67. compress_texture(avctx, frame);
  68. /* Compress with snappy too, write directly on packet buffer. */
  69. ret = snappy_compress(ctx->tex_buf, ctx->tex_size,
  70. pkt->data + HEADER_SIZE, &final_size);
  71. if (ret != SNAPPY_OK) {
  72. av_log(avctx, AV_LOG_ERROR, "Snappy compress error.\n");
  73. return AVERROR_BUG;
  74. }
  75. /* If there is no gain from snappy, just use the raw texture. */
  76. if (final_size > ctx->tex_size) {
  77. comp = HAP_COMP_NONE;
  78. av_log(avctx, AV_LOG_VERBOSE,
  79. "Snappy buffer bigger than uncompressed (%lu > %lu bytes).\n",
  80. final_size, ctx->tex_size);
  81. memcpy(pkt->data + HEADER_SIZE, ctx->tex_buf, ctx->tex_size);
  82. final_size = ctx->tex_size;
  83. }
  84. /* Write header at the start. */
  85. AV_WL24(pkt->data, 0);
  86. AV_WL32(pkt->data + 4, final_size);
  87. pkt->data[3] = comp | ctx->section_type;
  88. av_shrink_packet(pkt, final_size + HEADER_SIZE);
  89. pkt->flags |= AV_PKT_FLAG_KEY;
  90. *got_packet = 1;
  91. return 0;
  92. }
  93. static av_cold int hap_init(AVCodecContext *avctx)
  94. {
  95. HapContext *ctx = avctx->priv_data;
  96. int ratio;
  97. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  98. if (ret < 0) {
  99. av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
  100. avctx->width, avctx->height);
  101. return ret;
  102. }
  103. if (avctx->width % 4 || avctx->height % 4) {
  104. av_log(avctx, AV_LOG_ERROR, "Video size %dx%d is not multiple of 4.\n",
  105. avctx->width, avctx->height);
  106. return AVERROR_INVALIDDATA;
  107. }
  108. ff_texturedspenc_init(&ctx->dxtc);
  109. switch (ctx->section_type & 0x0F) {
  110. case HAP_FMT_RGBDXT1:
  111. ratio = 8;
  112. avctx->codec_tag = MKTAG('H', 'a', 'p', '1');
  113. ctx->tex_fun = ctx->dxtc.dxt1_block;
  114. break;
  115. case HAP_FMT_RGBADXT5:
  116. ratio = 4;
  117. avctx->codec_tag = MKTAG('H', 'a', 'p', '5');
  118. ctx->tex_fun = ctx->dxtc.dxt5_block;
  119. break;
  120. case HAP_FMT_YCOCGDXT5:
  121. ratio = 4;
  122. avctx->codec_tag = MKTAG('H', 'a', 'p', 'Y');
  123. ctx->tex_fun = ctx->dxtc.dxt5ys_block;
  124. break;
  125. default:
  126. av_log(avctx, AV_LOG_ERROR, "Invalid format %02X\n", ctx->section_type);
  127. return AVERROR_INVALIDDATA;
  128. }
  129. /* Texture compression ratio is constant, so can we computer
  130. * beforehand the final size of the uncompressed buffer. */
  131. ctx->tex_size = FFALIGN(avctx->width, TEXTURE_BLOCK_W) *
  132. FFALIGN(avctx->height, TEXTURE_BLOCK_H) * 4 / ratio;
  133. ctx->max_snappy = snappy_max_compressed_length(ctx->tex_size);
  134. ctx->tex_buf = av_malloc(ctx->tex_size);
  135. if (!ctx->tex_buf)
  136. return AVERROR(ENOMEM);
  137. return 0;
  138. }
  139. static av_cold int hap_close(AVCodecContext *avctx)
  140. {
  141. HapContext *ctx = avctx->priv_data;
  142. av_freep(&ctx->tex_buf);
  143. return 0;
  144. }
  145. #define OFFSET(x) offsetof(HapContext, x)
  146. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  147. static const AVOption options[] = {
  148. { "format", NULL, OFFSET(section_type), AV_OPT_TYPE_INT, { .i64 = HAP_FMT_RGBDXT1 }, HAP_FMT_RGBDXT1, HAP_FMT_YCOCGDXT5, FLAGS, "format" },
  149. { "hap", "Hap 1 (DXT1 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBDXT1 }, 0, 0, FLAGS, "format" },
  150. { "hap_alpha", "Hap Alpha (DXT5 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBADXT5 }, 0, 0, FLAGS, "format" },
  151. { "hap_q", "Hap Q (DXT5-YCoCg textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_YCOCGDXT5 }, 0, 0, FLAGS, "format" },
  152. { NULL },
  153. };
  154. static const AVClass hapenc_class = {
  155. .class_name = "Hap encoder",
  156. .item_name = av_default_item_name,
  157. .option = options,
  158. .version = LIBAVUTIL_VERSION_INT,
  159. };
  160. AVCodec ff_hap_encoder = {
  161. .name = "hap",
  162. .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap encoder"),
  163. .type = AVMEDIA_TYPE_VIDEO,
  164. .id = AV_CODEC_ID_HAP,
  165. .priv_data_size = sizeof(HapContext),
  166. .priv_class = &hapenc_class,
  167. .init = hap_init,
  168. .encode2 = hap_encode,
  169. .close = hap_close,
  170. .pix_fmts = (const enum AVPixelFormat[]) {
  171. AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE,
  172. },
  173. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  174. FF_CODEC_CAP_INIT_CLEANUP,
  175. };