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.

430 lines
14KB

  1. /*
  2. * Vidvox Hap decoder
  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 decoder
  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 "libavutil/imgutils.h"
  32. #include "avcodec.h"
  33. #include "bytestream.h"
  34. #include "hap.h"
  35. #include "internal.h"
  36. #include "snappy.h"
  37. #include "texturedsp.h"
  38. #include "thread.h"
  39. /* The first three bytes are the size of the section past the header, or zero
  40. * if the length is stored in the next long word. The fourth byte in the first
  41. * long word indicates the type of the current section. */
  42. static int parse_section_header(GetByteContext *gbc, int *section_size,
  43. enum HapSectionType *section_type)
  44. {
  45. if (bytestream2_get_bytes_left(gbc) < 4)
  46. return AVERROR_INVALIDDATA;
  47. *section_size = bytestream2_get_le24(gbc);
  48. *section_type = bytestream2_get_byte(gbc);
  49. if (*section_size == 0) {
  50. if (bytestream2_get_bytes_left(gbc) < 4)
  51. return AVERROR_INVALIDDATA;
  52. *section_size = bytestream2_get_le32(gbc);
  53. }
  54. if (*section_size > bytestream2_get_bytes_left(gbc))
  55. return AVERROR_INVALIDDATA;
  56. else
  57. return 0;
  58. }
  59. static int hap_parse_decode_instructions(HapContext *ctx, int size)
  60. {
  61. GetByteContext *gbc = &ctx->gbc;
  62. int section_size;
  63. enum HapSectionType section_type;
  64. int is_first_table = 1, had_offsets = 0, had_compressors = 0, had_sizes = 0;
  65. int i, ret;
  66. while (size > 0) {
  67. int stream_remaining = bytestream2_get_bytes_left(gbc);
  68. ret = parse_section_header(gbc, &section_size, &section_type);
  69. if (ret != 0)
  70. return ret;
  71. size -= stream_remaining - bytestream2_get_bytes_left(gbc);
  72. switch (section_type) {
  73. case HAP_ST_COMPRESSOR_TABLE:
  74. ret = ff_hap_set_chunk_count(ctx, section_size, is_first_table);
  75. if (ret != 0)
  76. return ret;
  77. for (i = 0; i < section_size; i++) {
  78. ctx->chunks[i].compressor = bytestream2_get_byte(gbc) << 4;
  79. }
  80. had_compressors = 1;
  81. is_first_table = 0;
  82. break;
  83. case HAP_ST_SIZE_TABLE:
  84. ret = ff_hap_set_chunk_count(ctx, section_size / 4, is_first_table);
  85. if (ret != 0)
  86. return ret;
  87. for (i = 0; i < section_size / 4; i++) {
  88. ctx->chunks[i].compressed_size = bytestream2_get_le32(gbc);
  89. }
  90. had_sizes = 1;
  91. is_first_table = 0;
  92. break;
  93. case HAP_ST_OFFSET_TABLE:
  94. ret = ff_hap_set_chunk_count(ctx, section_size / 4, is_first_table);
  95. if (ret != 0)
  96. return ret;
  97. for (i = 0; i < section_size / 4; i++) {
  98. ctx->chunks[i].compressed_offset = bytestream2_get_le32(gbc);
  99. }
  100. had_offsets = 1;
  101. is_first_table = 0;
  102. break;
  103. default:
  104. break;
  105. }
  106. size -= section_size;
  107. }
  108. if (!had_sizes || !had_compressors)
  109. return AVERROR_INVALIDDATA;
  110. /* The offsets table is optional. If not present than calculate offsets by
  111. * summing the sizes of preceding chunks. */
  112. if (!had_offsets) {
  113. size_t running_size = 0;
  114. for (i = 0; i < ctx->chunk_count; i++) {
  115. ctx->chunks[i].compressed_offset = running_size;
  116. running_size += ctx->chunks[i].compressed_size;
  117. }
  118. }
  119. return 0;
  120. }
  121. static int hap_can_use_tex_in_place(HapContext *ctx)
  122. {
  123. int i;
  124. size_t running_offset = 0;
  125. for (i = 0; i < ctx->chunk_count; i++) {
  126. if (ctx->chunks[i].compressed_offset != running_offset
  127. || ctx->chunks[i].compressor != HAP_COMP_NONE)
  128. return 0;
  129. running_offset += ctx->chunks[i].compressed_size;
  130. }
  131. return 1;
  132. }
  133. static int hap_parse_frame_header(AVCodecContext *avctx)
  134. {
  135. HapContext *ctx = avctx->priv_data;
  136. GetByteContext *gbc = &ctx->gbc;
  137. int section_size;
  138. enum HapSectionType section_type;
  139. const char *compressorstr;
  140. int i, ret;
  141. ret = parse_section_header(gbc, &section_size, &section_type);
  142. if (ret != 0)
  143. return ret;
  144. if ((avctx->codec_tag == MKTAG('H','a','p','1') && (section_type & 0x0F) != HAP_FMT_RGBDXT1) ||
  145. (avctx->codec_tag == MKTAG('H','a','p','5') && (section_type & 0x0F) != HAP_FMT_RGBADXT5) ||
  146. (avctx->codec_tag == MKTAG('H','a','p','Y') && (section_type & 0x0F) != HAP_FMT_YCOCGDXT5)) {
  147. av_log(avctx, AV_LOG_ERROR,
  148. "Invalid texture format %#04x.\n", section_type & 0x0F);
  149. return AVERROR_INVALIDDATA;
  150. }
  151. switch (section_type & 0xF0) {
  152. case HAP_COMP_NONE:
  153. case HAP_COMP_SNAPPY:
  154. ret = ff_hap_set_chunk_count(ctx, 1, 1);
  155. if (ret == 0) {
  156. ctx->chunks[0].compressor = section_type & 0xF0;
  157. ctx->chunks[0].compressed_offset = 0;
  158. ctx->chunks[0].compressed_size = section_size;
  159. }
  160. if (ctx->chunks[0].compressor == HAP_COMP_NONE) {
  161. compressorstr = "none";
  162. } else {
  163. compressorstr = "snappy";
  164. }
  165. break;
  166. case HAP_COMP_COMPLEX:
  167. ret = parse_section_header(gbc, &section_size, &section_type);
  168. if (ret == 0 && section_type != HAP_ST_DECODE_INSTRUCTIONS)
  169. ret = AVERROR_INVALIDDATA;
  170. if (ret == 0)
  171. ret = hap_parse_decode_instructions(ctx, section_size);
  172. compressorstr = "complex";
  173. break;
  174. default:
  175. ret = AVERROR_INVALIDDATA;
  176. break;
  177. }
  178. if (ret != 0)
  179. return ret;
  180. /* Check the frame is valid and read the uncompressed chunk sizes */
  181. ctx->tex_size = 0;
  182. for (i = 0; i < ctx->chunk_count; i++) {
  183. HapChunk *chunk = &ctx->chunks[i];
  184. /* Check the compressed buffer is valid */
  185. if (chunk->compressed_offset + chunk->compressed_size > bytestream2_get_bytes_left(gbc))
  186. return AVERROR_INVALIDDATA;
  187. /* Chunks are unpacked sequentially, ctx->tex_size is the uncompressed
  188. * size thus far */
  189. chunk->uncompressed_offset = ctx->tex_size;
  190. /* Fill out uncompressed size */
  191. if (chunk->compressor == HAP_COMP_SNAPPY) {
  192. GetByteContext gbc_tmp;
  193. int64_t uncompressed_size;
  194. bytestream2_init(&gbc_tmp, gbc->buffer + chunk->compressed_offset,
  195. chunk->compressed_size);
  196. uncompressed_size = ff_snappy_peek_uncompressed_length(&gbc_tmp);
  197. if (uncompressed_size < 0) {
  198. return uncompressed_size;
  199. }
  200. chunk->uncompressed_size = uncompressed_size;
  201. } else if (chunk->compressor == HAP_COMP_NONE) {
  202. chunk->uncompressed_size = chunk->compressed_size;
  203. } else {
  204. return AVERROR_INVALIDDATA;
  205. }
  206. ctx->tex_size += chunk->uncompressed_size;
  207. }
  208. av_log(avctx, AV_LOG_DEBUG, "%s compressor\n", compressorstr);
  209. return ret;
  210. }
  211. static int decompress_chunks_thread(AVCodecContext *avctx, void *arg,
  212. int chunk_nb, int thread_nb)
  213. {
  214. HapContext *ctx = avctx->priv_data;
  215. HapChunk *chunk = &ctx->chunks[chunk_nb];
  216. GetByteContext gbc;
  217. uint8_t *dst = ctx->tex_buf + chunk->uncompressed_offset;
  218. bytestream2_init(&gbc, ctx->gbc.buffer + chunk->compressed_offset, chunk->compressed_size);
  219. if (chunk->compressor == HAP_COMP_SNAPPY) {
  220. int ret;
  221. int64_t uncompressed_size = ctx->tex_size;
  222. /* Uncompress the frame */
  223. ret = ff_snappy_uncompress(&gbc, dst, &uncompressed_size);
  224. if (ret < 0) {
  225. av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
  226. return ret;
  227. }
  228. } else if (chunk->compressor == HAP_COMP_NONE) {
  229. bytestream2_get_buffer(&gbc, dst, chunk->compressed_size);
  230. }
  231. return 0;
  232. }
  233. static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
  234. int slice, int thread_nb)
  235. {
  236. HapContext *ctx = avctx->priv_data;
  237. AVFrame *frame = arg;
  238. const uint8_t *d = ctx->tex_data;
  239. int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
  240. int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
  241. int x, y;
  242. int start_slice, end_slice;
  243. int base_blocks_per_slice = h_block / ctx->slice_count;
  244. int remainder_blocks = h_block % ctx->slice_count;
  245. /* When the frame height (in blocks) doesn't divide evenly between the
  246. * number of slices, spread the remaining blocks evenly between the first
  247. * operations */
  248. start_slice = slice * base_blocks_per_slice;
  249. /* Add any extra blocks (one per slice) that have been added before this slice */
  250. start_slice += FFMIN(slice, remainder_blocks);
  251. end_slice = start_slice + base_blocks_per_slice;
  252. /* Add an extra block if there are still remainder blocks to be accounted for */
  253. if (slice < remainder_blocks)
  254. end_slice++;
  255. for (y = start_slice; y < end_slice; y++) {
  256. uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
  257. int off = y * w_block;
  258. for (x = 0; x < w_block; x++) {
  259. ctx->tex_fun(p + x * 16, frame->linesize[0],
  260. d + (off + x) * ctx->tex_rat);
  261. }
  262. }
  263. return 0;
  264. }
  265. static int hap_decode(AVCodecContext *avctx, void *data,
  266. int *got_frame, AVPacket *avpkt)
  267. {
  268. HapContext *ctx = avctx->priv_data;
  269. ThreadFrame tframe;
  270. int ret, i;
  271. bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
  272. /* Check for section header */
  273. ret = hap_parse_frame_header(avctx);
  274. if (ret < 0)
  275. return ret;
  276. /* Get the output frame ready to receive data */
  277. tframe.f = data;
  278. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  279. if (ret < 0)
  280. return ret;
  281. ff_thread_finish_setup(avctx);
  282. /* Unpack the DXT texture */
  283. if (hap_can_use_tex_in_place(ctx)) {
  284. /* Only DXTC texture compression in a contiguous block */
  285. ctx->tex_data = ctx->gbc.buffer;
  286. } else {
  287. /* Perform the second-stage decompression */
  288. ret = av_reallocp(&ctx->tex_buf, ctx->tex_size);
  289. if (ret < 0)
  290. return ret;
  291. avctx->execute2(avctx, decompress_chunks_thread, NULL,
  292. ctx->chunk_results, ctx->chunk_count);
  293. for (i = 0; i < ctx->chunk_count; i++) {
  294. if (ctx->chunk_results[i] < 0)
  295. return ctx->chunk_results[i];
  296. }
  297. ctx->tex_data = ctx->tex_buf;
  298. }
  299. /* Use the decompress function on the texture, one block per thread */
  300. avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, ctx->slice_count);
  301. /* Frame is ready to be output */
  302. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  303. tframe.f->key_frame = 1;
  304. *got_frame = 1;
  305. return avpkt->size;
  306. }
  307. static av_cold int hap_init(AVCodecContext *avctx)
  308. {
  309. HapContext *ctx = avctx->priv_data;
  310. const char *texture_name;
  311. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  312. if (ret < 0) {
  313. av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
  314. avctx->width, avctx->height);
  315. return ret;
  316. }
  317. /* Since codec is based on 4x4 blocks, size is aligned to 4 */
  318. avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
  319. avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
  320. /* Technically only one mode has alpha, but 32 bits are easier to handle */
  321. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  322. ff_texturedsp_init(&ctx->dxtc);
  323. switch (avctx->codec_tag) {
  324. case MKTAG('H','a','p','1'):
  325. texture_name = "DXT1";
  326. ctx->tex_rat = 8;
  327. ctx->tex_fun = ctx->dxtc.dxt1_block;
  328. break;
  329. case MKTAG('H','a','p','5'):
  330. texture_name = "DXT5";
  331. ctx->tex_rat = 16;
  332. ctx->tex_fun = ctx->dxtc.dxt5_block;
  333. break;
  334. case MKTAG('H','a','p','Y'):
  335. texture_name = "DXT5-YCoCg-scaled";
  336. ctx->tex_rat = 16;
  337. ctx->tex_fun = ctx->dxtc.dxt5ys_block;
  338. break;
  339. default:
  340. return AVERROR_DECODER_NOT_FOUND;
  341. }
  342. av_log(avctx, AV_LOG_DEBUG, "%s texture\n", texture_name);
  343. ctx->slice_count = av_clip(avctx->thread_count, 1,
  344. avctx->coded_height / TEXTURE_BLOCK_H);
  345. return 0;
  346. }
  347. static av_cold int hap_close(AVCodecContext *avctx)
  348. {
  349. HapContext *ctx = avctx->priv_data;
  350. ff_hap_free_context(ctx);
  351. return 0;
  352. }
  353. AVCodec ff_hap_decoder = {
  354. .name = "hap",
  355. .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
  356. .type = AVMEDIA_TYPE_VIDEO,
  357. .id = AV_CODEC_ID_HAP,
  358. .init = hap_init,
  359. .decode = hap_decode,
  360. .close = hap_close,
  361. .priv_data_size = sizeof(HapContext),
  362. .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS |
  363. AV_CODEC_CAP_DR1,
  364. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  365. FF_CODEC_CAP_INIT_CLEANUP,
  366. };