hapenc.c 13 KB

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