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.

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