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.

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