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.

335 lines
11KB

  1. /*
  2. * Vidvox Hap encoder
  3. * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. * Copyright (C) 2015 Tom Butterworth <bangnoise@gmail.com>
  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. /**
  23. * @file
  24. * Hap encoder
  25. *
  26. * Fourcc: Hap1, Hap5, HapY
  27. *
  28. * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
  29. */
  30. #include <stdint.h>
  31. #include "snappy-c.h"
  32. #include "libavutil/frame.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/intreadwrite.h"
  35. #include "libavutil/opt.h"
  36. #include "avcodec.h"
  37. #include "bytestream.h"
  38. #include "hap.h"
  39. #include "internal.h"
  40. #include "texturedsp.h"
  41. #define HAP_MAX_CHUNKS 64
  42. enum HapHeaderLength {
  43. /* Short header: four bytes with a 24 bit size value */
  44. HAP_HDR_SHORT = 4,
  45. /* Long header: eight bytes with a 32 bit size value */
  46. HAP_HDR_LONG = 8,
  47. };
  48. static void compress_texture(AVCodecContext *avctx, const AVFrame *f)
  49. {
  50. HapContext *ctx = avctx->priv_data;
  51. uint8_t *out = ctx->tex_buf;
  52. int i, j;
  53. for (j = 0; j < avctx->height; j += 4) {
  54. for (i = 0; i < avctx->width; i += 4) {
  55. uint8_t *p = f->data[0] + i * 4 + j * f->linesize[0];
  56. const int step = ctx->tex_fun(out, f->linesize[0], p);
  57. out += step;
  58. }
  59. }
  60. }
  61. /* section_length does not include the header */
  62. static void hap_write_section_header(PutByteContext *pbc,
  63. enum HapHeaderLength header_length,
  64. int section_length,
  65. enum HapSectionType section_type)
  66. {
  67. /* The first three bytes are the length of the section (not including the
  68. * header) or zero if using an eight-byte header.
  69. * For an eight-byte header, the length is in the last four bytes.
  70. * The fourth byte stores the section type. */
  71. bytestream2_put_le24(pbc, header_length == HAP_HDR_LONG ? 0 : section_length);
  72. bytestream2_put_byte(pbc, section_type);
  73. if (header_length == HAP_HDR_LONG) {
  74. bytestream2_put_le32(pbc, section_length);
  75. }
  76. }
  77. static int hap_compress_frame(AVCodecContext *avctx, uint8_t *dst)
  78. {
  79. HapContext *ctx = avctx->priv_data;
  80. int i, final_size = 0;
  81. for (i = 0; i < ctx->chunk_count; i++) {
  82. HapChunk *chunk = &ctx->chunks[i];
  83. uint8_t *chunk_src, *chunk_dst;
  84. int ret;
  85. if (i == 0) {
  86. chunk->compressed_offset = 0;
  87. } else {
  88. chunk->compressed_offset = ctx->chunks[i-1].compressed_offset
  89. + ctx->chunks[i-1].compressed_size;
  90. }
  91. chunk->uncompressed_size = ctx->tex_size / ctx->chunk_count;
  92. chunk->uncompressed_offset = i * chunk->uncompressed_size;
  93. chunk->compressed_size = ctx->max_snappy;
  94. chunk_src = ctx->tex_buf + chunk->uncompressed_offset;
  95. chunk_dst = dst + chunk->compressed_offset;
  96. /* Compress with snappy too, write directly on packet buffer. */
  97. ret = snappy_compress(chunk_src, chunk->uncompressed_size,
  98. chunk_dst, &chunk->compressed_size);
  99. if (ret != SNAPPY_OK) {
  100. av_log(avctx, AV_LOG_ERROR, "Snappy compress error.\n");
  101. return AVERROR_BUG;
  102. }
  103. /* If there is no gain from snappy, just use the raw texture. */
  104. if (chunk->compressed_size >= chunk->uncompressed_size) {
  105. av_log(avctx, AV_LOG_VERBOSE,
  106. "Snappy buffer bigger than uncompressed (%zu >= %zu bytes).\n",
  107. chunk->compressed_size, chunk->uncompressed_size);
  108. memcpy(chunk_dst, chunk_src, chunk->uncompressed_size);
  109. chunk->compressor = HAP_COMP_NONE;
  110. chunk->compressed_size = chunk->uncompressed_size;
  111. } else {
  112. chunk->compressor = HAP_COMP_SNAPPY;
  113. }
  114. final_size += chunk->compressed_size;
  115. }
  116. return final_size;
  117. }
  118. static int hap_decode_instructions_length(HapContext *ctx)
  119. {
  120. /* Second-Stage Compressor Table (one byte per entry)
  121. * + Chunk Size Table (four bytes per entry)
  122. * + headers for both sections (short versions)
  123. * = chunk_count + (4 * chunk_count) + 4 + 4 */
  124. return (5 * ctx->chunk_count) + 8;
  125. }
  126. static int hap_header_length(HapContext *ctx)
  127. {
  128. /* Top section header (long version) */
  129. int length = HAP_HDR_LONG;
  130. if (ctx->chunk_count > 1) {
  131. /* Decode Instructions header (short) + Decode Instructions Container */
  132. length += HAP_HDR_SHORT + hap_decode_instructions_length(ctx);
  133. }
  134. return length;
  135. }
  136. static void hap_write_frame_header(HapContext *ctx, uint8_t *dst, int frame_length)
  137. {
  138. PutByteContext pbc;
  139. int i;
  140. bytestream2_init_writer(&pbc, dst, frame_length);
  141. if (ctx->chunk_count == 1) {
  142. /* Write a simple header */
  143. hap_write_section_header(&pbc, HAP_HDR_LONG, frame_length - 8,
  144. ctx->chunks[0].compressor | ctx->opt_tex_fmt);
  145. } else {
  146. /* Write a complex header with Decode Instructions Container */
  147. hap_write_section_header(&pbc, HAP_HDR_LONG, frame_length - 8,
  148. HAP_COMP_COMPLEX | ctx->opt_tex_fmt);
  149. hap_write_section_header(&pbc, HAP_HDR_SHORT, hap_decode_instructions_length(ctx),
  150. HAP_ST_DECODE_INSTRUCTIONS);
  151. hap_write_section_header(&pbc, HAP_HDR_SHORT, ctx->chunk_count,
  152. HAP_ST_COMPRESSOR_TABLE);
  153. for (i = 0; i < ctx->chunk_count; i++) {
  154. bytestream2_put_byte(&pbc, ctx->chunks[i].compressor >> 4);
  155. }
  156. hap_write_section_header(&pbc, HAP_HDR_SHORT, ctx->chunk_count * 4,
  157. HAP_ST_SIZE_TABLE);
  158. for (i = 0; i < ctx->chunk_count; i++) {
  159. bytestream2_put_le32(&pbc, ctx->chunks[i].compressed_size);
  160. }
  161. }
  162. }
  163. static int hap_encode(AVCodecContext *avctx, AVPacket *pkt,
  164. const AVFrame *frame, int *got_packet)
  165. {
  166. HapContext *ctx = avctx->priv_data;
  167. int header_length = hap_header_length(ctx);
  168. int final_data_size, ret;
  169. int pktsize = FFMAX(ctx->tex_size, ctx->max_snappy * ctx->chunk_count) + header_length;
  170. /* Allocate maximum size packet, shrink later. */
  171. ret = ff_alloc_packet(pkt, pktsize);
  172. if (ret < 0)
  173. return ret;
  174. /* DXTC compression. */
  175. compress_texture(avctx, frame);
  176. /* Compress (using Snappy) the frame */
  177. final_data_size = hap_compress_frame(avctx, pkt->data + header_length);
  178. if (final_data_size < 0)
  179. return final_data_size;
  180. /* Write header at the start. */
  181. hap_write_frame_header(ctx, pkt->data, final_data_size + header_length);
  182. av_shrink_packet(pkt, final_data_size + header_length);
  183. pkt->flags |= AV_PKT_FLAG_KEY;
  184. *got_packet = 1;
  185. return 0;
  186. }
  187. static av_cold int hap_init(AVCodecContext *avctx)
  188. {
  189. HapContext *ctx = avctx->priv_data;
  190. int ratio;
  191. int corrected_chunk_count;
  192. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  193. if (ret < 0) {
  194. av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
  195. avctx->width, avctx->height);
  196. return ret;
  197. }
  198. if (avctx->width % 4 || avctx->height % 4) {
  199. av_log(avctx, AV_LOG_ERROR, "Video size %dx%d is not multiple of 4.\n",
  200. avctx->width, avctx->height);
  201. return AVERROR_INVALIDDATA;
  202. }
  203. ff_texturedspenc_init(&ctx->dxtc);
  204. switch (ctx->opt_tex_fmt) {
  205. case HAP_FMT_RGBDXT1:
  206. ratio = 8;
  207. avctx->codec_tag = MKTAG('H', 'a', 'p', '1');
  208. avctx->bits_per_coded_sample = 24;
  209. ctx->tex_fun = ctx->dxtc.dxt1_block;
  210. break;
  211. case HAP_FMT_RGBADXT5:
  212. ratio = 4;
  213. avctx->codec_tag = MKTAG('H', 'a', 'p', '5');
  214. avctx->bits_per_coded_sample = 32;
  215. ctx->tex_fun = ctx->dxtc.dxt5_block;
  216. break;
  217. case HAP_FMT_YCOCGDXT5:
  218. ratio = 4;
  219. avctx->codec_tag = MKTAG('H', 'a', 'p', 'Y');
  220. avctx->bits_per_coded_sample = 24;
  221. ctx->tex_fun = ctx->dxtc.dxt5ys_block;
  222. break;
  223. default:
  224. av_log(avctx, AV_LOG_ERROR, "Invalid format %02X\n", ctx->opt_tex_fmt);
  225. return AVERROR_INVALIDDATA;
  226. }
  227. /* Texture compression ratio is constant, so can we computer
  228. * beforehand the final size of the uncompressed buffer. */
  229. ctx->tex_size = FFALIGN(avctx->width, TEXTURE_BLOCK_W) *
  230. FFALIGN(avctx->height, TEXTURE_BLOCK_H) * 4 / ratio;
  231. /* Round the chunk count to divide evenly on DXT block edges */
  232. corrected_chunk_count = av_clip(ctx->opt_chunk_count, 1, HAP_MAX_CHUNKS);
  233. while ((ctx->tex_size / (64 / ratio)) % corrected_chunk_count != 0) {
  234. corrected_chunk_count--;
  235. }
  236. if (corrected_chunk_count != ctx->opt_chunk_count) {
  237. av_log(avctx, AV_LOG_INFO, "%d chunks requested but %d used.\n",
  238. ctx->opt_chunk_count, corrected_chunk_count);
  239. }
  240. ret = ff_hap_set_chunk_count(ctx, corrected_chunk_count, 1);
  241. if (ret != 0)
  242. return ret;
  243. ctx->max_snappy = snappy_max_compressed_length(ctx->tex_size / corrected_chunk_count);
  244. ctx->tex_buf = av_malloc(ctx->tex_size);
  245. if (!ctx->tex_buf)
  246. return AVERROR(ENOMEM);
  247. return 0;
  248. }
  249. static av_cold int hap_close(AVCodecContext *avctx)
  250. {
  251. HapContext *ctx = avctx->priv_data;
  252. ff_hap_free_context(ctx);
  253. return 0;
  254. }
  255. #define OFFSET(x) offsetof(HapContext, x)
  256. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  257. static const AVOption options[] = {
  258. { "format", NULL, OFFSET(opt_tex_fmt), AV_OPT_TYPE_INT, { .i64 = HAP_FMT_RGBDXT1 }, HAP_FMT_RGBDXT1, HAP_FMT_YCOCGDXT5, FLAGS, "format" },
  259. { "hap", "Hap 1 (DXT1 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBDXT1 }, 0, 0, FLAGS, "format" },
  260. { "hap_alpha", "Hap Alpha (DXT5 textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_RGBADXT5 }, 0, 0, FLAGS, "format" },
  261. { "hap_q", "Hap Q (DXT5-YCoCg textures)", 0, AV_OPT_TYPE_CONST, { .i64 = HAP_FMT_YCOCGDXT5 }, 0, 0, FLAGS, "format" },
  262. { "chunks", "chunk count", OFFSET(opt_chunk_count), AV_OPT_TYPE_INT, {.i64 = 1 }, 1, HAP_MAX_CHUNKS, FLAGS, },
  263. { NULL },
  264. };
  265. static const AVClass hapenc_class = {
  266. .class_name = "Hap encoder",
  267. .item_name = av_default_item_name,
  268. .option = options,
  269. .version = LIBAVUTIL_VERSION_INT,
  270. };
  271. AVCodec ff_hap_encoder = {
  272. .name = "hap",
  273. .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap encoder"),
  274. .type = AVMEDIA_TYPE_VIDEO,
  275. .id = AV_CODEC_ID_HAP,
  276. .priv_data_size = sizeof(HapContext),
  277. .priv_class = &hapenc_class,
  278. .init = hap_init,
  279. .encode2 = hap_encode,
  280. .close = hap_close,
  281. .pix_fmts = (const enum AVPixelFormat[]) {
  282. AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE,
  283. },
  284. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  285. FF_CODEC_CAP_INIT_CLEANUP,
  286. };