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.

368 lines
13KB

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