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.

488 lines
16KB

  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. * HapQA and HAPAlphaOnly added by Jokyo Images
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * Hap decoder
  27. *
  28. * Fourcc: Hap1, Hap5, HapY, HapA, HapM
  29. *
  30. * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
  31. */
  32. #include <stdint.h>
  33. #include "libavutil/imgutils.h"
  34. #include "avcodec.h"
  35. #include "bytestream.h"
  36. #include "hap.h"
  37. #include "internal.h"
  38. #include "snappy.h"
  39. #include "texturedsp.h"
  40. #include "thread.h"
  41. static int hap_parse_decode_instructions(HapContext *ctx, int size)
  42. {
  43. GetByteContext *gbc = &ctx->gbc;
  44. int section_size;
  45. enum HapSectionType section_type;
  46. int is_first_table = 1, had_offsets = 0, had_compressors = 0, had_sizes = 0;
  47. int i, ret;
  48. while (size > 0) {
  49. int stream_remaining = bytestream2_get_bytes_left(gbc);
  50. ret = ff_hap_parse_section_header(gbc, &section_size, &section_type);
  51. if (ret != 0)
  52. return ret;
  53. size -= stream_remaining - bytestream2_get_bytes_left(gbc);
  54. switch (section_type) {
  55. case HAP_ST_COMPRESSOR_TABLE:
  56. ret = ff_hap_set_chunk_count(ctx, section_size, is_first_table);
  57. if (ret != 0)
  58. return ret;
  59. for (i = 0; i < section_size; i++) {
  60. ctx->chunks[i].compressor = bytestream2_get_byte(gbc) << 4;
  61. }
  62. had_compressors = 1;
  63. is_first_table = 0;
  64. break;
  65. case HAP_ST_SIZE_TABLE:
  66. ret = ff_hap_set_chunk_count(ctx, section_size / 4, is_first_table);
  67. if (ret != 0)
  68. return ret;
  69. for (i = 0; i < section_size / 4; i++) {
  70. ctx->chunks[i].compressed_size = bytestream2_get_le32(gbc);
  71. }
  72. had_sizes = 1;
  73. is_first_table = 0;
  74. break;
  75. case HAP_ST_OFFSET_TABLE:
  76. ret = ff_hap_set_chunk_count(ctx, section_size / 4, is_first_table);
  77. if (ret != 0)
  78. return ret;
  79. for (i = 0; i < section_size / 4; i++) {
  80. ctx->chunks[i].compressed_offset = bytestream2_get_le32(gbc);
  81. }
  82. had_offsets = 1;
  83. is_first_table = 0;
  84. break;
  85. default:
  86. break;
  87. }
  88. size -= section_size;
  89. }
  90. if (!had_sizes || !had_compressors)
  91. return AVERROR_INVALIDDATA;
  92. /* The offsets table is optional. If not present than calculate offsets by
  93. * summing the sizes of preceding chunks. */
  94. if (!had_offsets) {
  95. size_t running_size = 0;
  96. for (i = 0; i < ctx->chunk_count; i++) {
  97. ctx->chunks[i].compressed_offset = running_size;
  98. running_size += ctx->chunks[i].compressed_size;
  99. }
  100. }
  101. return 0;
  102. }
  103. static int hap_can_use_tex_in_place(HapContext *ctx)
  104. {
  105. int i;
  106. size_t running_offset = 0;
  107. for (i = 0; i < ctx->chunk_count; i++) {
  108. if (ctx->chunks[i].compressed_offset != running_offset
  109. || ctx->chunks[i].compressor != HAP_COMP_NONE)
  110. return 0;
  111. running_offset += ctx->chunks[i].compressed_size;
  112. }
  113. return 1;
  114. }
  115. static int hap_parse_frame_header(AVCodecContext *avctx)
  116. {
  117. HapContext *ctx = avctx->priv_data;
  118. GetByteContext *gbc = &ctx->gbc;
  119. int section_size;
  120. enum HapSectionType section_type;
  121. const char *compressorstr;
  122. int i, ret;
  123. ret = ff_hap_parse_section_header(gbc, &ctx->texture_section_size, &section_type);
  124. if (ret != 0)
  125. return ret;
  126. if ((avctx->codec_tag == MKTAG('H','a','p','1') && (section_type & 0x0F) != HAP_FMT_RGBDXT1) ||
  127. (avctx->codec_tag == MKTAG('H','a','p','5') && (section_type & 0x0F) != HAP_FMT_RGBADXT5) ||
  128. (avctx->codec_tag == MKTAG('H','a','p','Y') && (section_type & 0x0F) != HAP_FMT_YCOCGDXT5) ||
  129. (avctx->codec_tag == MKTAG('H','a','p','A') && (section_type & 0x0F) != HAP_FMT_RGTC1) ||
  130. ((avctx->codec_tag == MKTAG('H','a','p','M') && (section_type & 0x0F) != HAP_FMT_RGTC1) &&
  131. (section_type & 0x0F) != HAP_FMT_YCOCGDXT5)) {
  132. av_log(avctx, AV_LOG_ERROR,
  133. "Invalid texture format %#04x.\n", section_type & 0x0F);
  134. return AVERROR_INVALIDDATA;
  135. }
  136. switch (section_type & 0xF0) {
  137. case HAP_COMP_NONE:
  138. case HAP_COMP_SNAPPY:
  139. ret = ff_hap_set_chunk_count(ctx, 1, 1);
  140. if (ret == 0) {
  141. ctx->chunks[0].compressor = section_type & 0xF0;
  142. ctx->chunks[0].compressed_offset = 0;
  143. ctx->chunks[0].compressed_size = ctx->texture_section_size;
  144. }
  145. if (ctx->chunks[0].compressor == HAP_COMP_NONE) {
  146. compressorstr = "none";
  147. } else {
  148. compressorstr = "snappy";
  149. }
  150. break;
  151. case HAP_COMP_COMPLEX:
  152. ret = ff_hap_parse_section_header(gbc, &section_size, &section_type);
  153. if (ret == 0 && section_type != HAP_ST_DECODE_INSTRUCTIONS)
  154. ret = AVERROR_INVALIDDATA;
  155. if (ret == 0)
  156. ret = hap_parse_decode_instructions(ctx, section_size);
  157. compressorstr = "complex";
  158. break;
  159. default:
  160. ret = AVERROR_INVALIDDATA;
  161. break;
  162. }
  163. if (ret != 0)
  164. return ret;
  165. /* Check the frame is valid and read the uncompressed chunk sizes */
  166. ctx->tex_size = 0;
  167. for (i = 0; i < ctx->chunk_count; i++) {
  168. HapChunk *chunk = &ctx->chunks[i];
  169. /* Check the compressed buffer is valid */
  170. if (chunk->compressed_offset + chunk->compressed_size > bytestream2_get_bytes_left(gbc))
  171. return AVERROR_INVALIDDATA;
  172. /* Chunks are unpacked sequentially, ctx->tex_size is the uncompressed
  173. * size thus far */
  174. chunk->uncompressed_offset = ctx->tex_size;
  175. /* Fill out uncompressed size */
  176. if (chunk->compressor == HAP_COMP_SNAPPY) {
  177. GetByteContext gbc_tmp;
  178. int64_t uncompressed_size;
  179. bytestream2_init(&gbc_tmp, gbc->buffer + chunk->compressed_offset,
  180. chunk->compressed_size);
  181. uncompressed_size = ff_snappy_peek_uncompressed_length(&gbc_tmp);
  182. if (uncompressed_size < 0) {
  183. return uncompressed_size;
  184. }
  185. chunk->uncompressed_size = uncompressed_size;
  186. } else if (chunk->compressor == HAP_COMP_NONE) {
  187. chunk->uncompressed_size = chunk->compressed_size;
  188. } else {
  189. return AVERROR_INVALIDDATA;
  190. }
  191. ctx->tex_size += chunk->uncompressed_size;
  192. }
  193. av_log(avctx, AV_LOG_DEBUG, "%s compressor\n", compressorstr);
  194. return ret;
  195. }
  196. static int decompress_chunks_thread(AVCodecContext *avctx, void *arg,
  197. int chunk_nb, int thread_nb)
  198. {
  199. HapContext *ctx = avctx->priv_data;
  200. HapChunk *chunk = &ctx->chunks[chunk_nb];
  201. GetByteContext gbc;
  202. uint8_t *dst = ctx->tex_buf + chunk->uncompressed_offset;
  203. bytestream2_init(&gbc, ctx->gbc.buffer + chunk->compressed_offset, chunk->compressed_size);
  204. if (chunk->compressor == HAP_COMP_SNAPPY) {
  205. int ret;
  206. int64_t uncompressed_size = ctx->tex_size;
  207. /* Uncompress the frame */
  208. ret = ff_snappy_uncompress(&gbc, dst, &uncompressed_size);
  209. if (ret < 0) {
  210. av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
  211. return ret;
  212. }
  213. } else if (chunk->compressor == HAP_COMP_NONE) {
  214. bytestream2_get_buffer(&gbc, dst, chunk->compressed_size);
  215. }
  216. return 0;
  217. }
  218. static int decompress_texture_thread_internal(AVCodecContext *avctx, void *arg,
  219. int slice, int thread_nb, int texture_num)
  220. {
  221. HapContext *ctx = avctx->priv_data;
  222. AVFrame *frame = arg;
  223. const uint8_t *d = ctx->tex_data;
  224. int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
  225. int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
  226. int x, y;
  227. int start_slice, end_slice;
  228. int base_blocks_per_slice = h_block / ctx->slice_count;
  229. int remainder_blocks = h_block % ctx->slice_count;
  230. /* When the frame height (in blocks) doesn't divide evenly between the
  231. * number of slices, spread the remaining blocks evenly between the first
  232. * operations */
  233. start_slice = slice * base_blocks_per_slice;
  234. /* Add any extra blocks (one per slice) that have been added before this slice */
  235. start_slice += FFMIN(slice, remainder_blocks);
  236. end_slice = start_slice + base_blocks_per_slice;
  237. /* Add an extra block if there are still remainder blocks to be accounted for */
  238. if (slice < remainder_blocks)
  239. end_slice++;
  240. for (y = start_slice; y < end_slice; y++) {
  241. uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
  242. int off = y * w_block;
  243. for (x = 0; x < w_block; x++) {
  244. if (texture_num == 0) {
  245. ctx->tex_fun(p + x * 4 * ctx->uncompress_pix_size, frame->linesize[0],
  246. d + (off + x) * ctx->tex_rat);
  247. } else {
  248. ctx->tex_fun2(p + x * 4 * ctx->uncompress_pix_size, frame->linesize[0],
  249. d + (off + x) * ctx->tex_rat2);
  250. }
  251. }
  252. }
  253. return 0;
  254. }
  255. static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
  256. int slice, int thread_nb)
  257. {
  258. return decompress_texture_thread_internal(avctx, arg, slice, thread_nb, 0);
  259. }
  260. static int decompress_texture2_thread(AVCodecContext *avctx, void *arg,
  261. int slice, int thread_nb)
  262. {
  263. return decompress_texture_thread_internal(avctx, arg, slice, thread_nb, 1);
  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, t;
  271. int tex_size;
  272. int section_size;
  273. enum HapSectionType section_type;
  274. int start_texture_section = 0;
  275. int tex_rat[2] = {0, 0};
  276. bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
  277. tex_rat[0] = ctx->tex_rat;
  278. /* check for multi texture header */
  279. if (ctx->texture_count == 2) {
  280. ret = ff_hap_parse_section_header(&ctx->gbc, &section_size, &section_type);
  281. if (ret != 0)
  282. return ret;
  283. if ((section_type & 0x0F) != 0x0D) {
  284. av_log(avctx, AV_LOG_ERROR, "Invalid section type in 2 textures mode %#04x.\n", section_type);
  285. return AVERROR_INVALIDDATA;
  286. }
  287. start_texture_section = 4;
  288. tex_rat[1] = ctx->tex_rat2;
  289. }
  290. /* Get the output frame ready to receive data */
  291. tframe.f = data;
  292. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  293. if (ret < 0)
  294. return ret;
  295. for (t = 0; t < ctx->texture_count; t++) {
  296. bytestream2_seek(&ctx->gbc, start_texture_section, SEEK_SET);
  297. /* Check for section header */
  298. ret = hap_parse_frame_header(avctx);
  299. if (ret < 0)
  300. return ret;
  301. start_texture_section += ctx->texture_section_size + 4;
  302. if (avctx->codec->update_thread_context)
  303. ff_thread_finish_setup(avctx);
  304. /* Unpack the DXT texture */
  305. if (hap_can_use_tex_in_place(ctx)) {
  306. /* Only DXTC texture compression in a contiguous block */
  307. ctx->tex_data = ctx->gbc.buffer;
  308. tex_size = FFMIN(ctx->texture_section_size, bytestream2_get_bytes_left(&ctx->gbc));
  309. } else {
  310. /* Perform the second-stage decompression */
  311. ret = av_reallocp(&ctx->tex_buf, ctx->tex_size);
  312. if (ret < 0)
  313. return ret;
  314. avctx->execute2(avctx, decompress_chunks_thread, NULL,
  315. ctx->chunk_results, ctx->chunk_count);
  316. for (i = 0; i < ctx->chunk_count; i++) {
  317. if (ctx->chunk_results[i] < 0)
  318. return ctx->chunk_results[i];
  319. }
  320. ctx->tex_data = ctx->tex_buf;
  321. tex_size = ctx->tex_size;
  322. }
  323. if (tex_size < (avctx->coded_width / TEXTURE_BLOCK_W)
  324. *(avctx->coded_height / TEXTURE_BLOCK_H)
  325. *tex_rat[t]) {
  326. av_log(avctx, AV_LOG_ERROR, "Insufficient data\n");
  327. return AVERROR_INVALIDDATA;
  328. }
  329. /* Use the decompress function on the texture, one block per thread */
  330. if (t == 0){
  331. avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, ctx->slice_count);
  332. } else{
  333. tframe.f = data;
  334. avctx->execute2(avctx, decompress_texture2_thread, tframe.f, NULL, ctx->slice_count);
  335. }
  336. }
  337. /* Frame is ready to be output */
  338. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  339. tframe.f->key_frame = 1;
  340. *got_frame = 1;
  341. return avpkt->size;
  342. }
  343. static av_cold int hap_init(AVCodecContext *avctx)
  344. {
  345. HapContext *ctx = avctx->priv_data;
  346. const char *texture_name;
  347. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  348. if (ret < 0) {
  349. av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
  350. avctx->width, avctx->height);
  351. return ret;
  352. }
  353. /* Since codec is based on 4x4 blocks, size is aligned to 4 */
  354. avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
  355. avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
  356. ff_texturedsp_init(&ctx->dxtc);
  357. ctx->texture_count = 1;
  358. ctx->uncompress_pix_size = 4;
  359. switch (avctx->codec_tag) {
  360. case MKTAG('H','a','p','1'):
  361. texture_name = "DXT1";
  362. ctx->tex_rat = 8;
  363. ctx->tex_fun = ctx->dxtc.dxt1_block;
  364. avctx->pix_fmt = AV_PIX_FMT_RGB0;
  365. break;
  366. case MKTAG('H','a','p','5'):
  367. texture_name = "DXT5";
  368. ctx->tex_rat = 16;
  369. ctx->tex_fun = ctx->dxtc.dxt5_block;
  370. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  371. break;
  372. case MKTAG('H','a','p','Y'):
  373. texture_name = "DXT5-YCoCg-scaled";
  374. ctx->tex_rat = 16;
  375. ctx->tex_fun = ctx->dxtc.dxt5ys_block;
  376. avctx->pix_fmt = AV_PIX_FMT_RGB0;
  377. break;
  378. case MKTAG('H','a','p','A'):
  379. texture_name = "RGTC1";
  380. ctx->tex_rat = 8;
  381. ctx->tex_fun = ctx->dxtc.rgtc1u_gray_block;
  382. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  383. ctx->uncompress_pix_size = 1;
  384. break;
  385. case MKTAG('H','a','p','M'):
  386. texture_name = "DXT5-YCoCg-scaled / RGTC1";
  387. ctx->tex_rat = 16;
  388. ctx->tex_rat2 = 8;
  389. ctx->tex_fun = ctx->dxtc.dxt5ys_block;
  390. ctx->tex_fun2 = ctx->dxtc.rgtc1u_alpha_block;
  391. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  392. ctx->texture_count = 2;
  393. break;
  394. default:
  395. return AVERROR_DECODER_NOT_FOUND;
  396. }
  397. av_log(avctx, AV_LOG_DEBUG, "%s texture\n", texture_name);
  398. ctx->slice_count = av_clip(avctx->thread_count, 1,
  399. avctx->coded_height / TEXTURE_BLOCK_H);
  400. return 0;
  401. }
  402. static av_cold int hap_close(AVCodecContext *avctx)
  403. {
  404. HapContext *ctx = avctx->priv_data;
  405. ff_hap_free_context(ctx);
  406. return 0;
  407. }
  408. AVCodec ff_hap_decoder = {
  409. .name = "hap",
  410. .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap"),
  411. .type = AVMEDIA_TYPE_VIDEO,
  412. .id = AV_CODEC_ID_HAP,
  413. .init = hap_init,
  414. .decode = hap_decode,
  415. .close = hap_close,
  416. .priv_data_size = sizeof(HapContext),
  417. .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS |
  418. AV_CODEC_CAP_DR1,
  419. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  420. FF_CODEC_CAP_INIT_CLEANUP,
  421. };