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.

503 lines
17KB

  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. if (ctx->chunks[i].compressed_size > UINT32_MAX - running_size)
  99. return AVERROR_INVALIDDATA;
  100. running_size += ctx->chunks[i].compressed_size;
  101. }
  102. }
  103. return 0;
  104. }
  105. static int hap_can_use_tex_in_place(HapContext *ctx)
  106. {
  107. int i;
  108. size_t running_offset = 0;
  109. for (i = 0; i < ctx->chunk_count; i++) {
  110. if (ctx->chunks[i].compressed_offset != running_offset
  111. || ctx->chunks[i].compressor != HAP_COMP_NONE)
  112. return 0;
  113. running_offset += ctx->chunks[i].compressed_size;
  114. }
  115. return 1;
  116. }
  117. static int hap_parse_frame_header(AVCodecContext *avctx)
  118. {
  119. HapContext *ctx = avctx->priv_data;
  120. GetByteContext *gbc = &ctx->gbc;
  121. int section_size;
  122. enum HapSectionType section_type;
  123. const char *compressorstr;
  124. int i, ret;
  125. ret = ff_hap_parse_section_header(gbc, &ctx->texture_section_size, &section_type);
  126. if (ret != 0)
  127. return ret;
  128. if ((avctx->codec_tag == MKTAG('H','a','p','1') && (section_type & 0x0F) != HAP_FMT_RGBDXT1) ||
  129. (avctx->codec_tag == MKTAG('H','a','p','5') && (section_type & 0x0F) != HAP_FMT_RGBADXT5) ||
  130. (avctx->codec_tag == MKTAG('H','a','p','Y') && (section_type & 0x0F) != HAP_FMT_YCOCGDXT5) ||
  131. (avctx->codec_tag == MKTAG('H','a','p','A') && (section_type & 0x0F) != HAP_FMT_RGTC1) ||
  132. ((avctx->codec_tag == MKTAG('H','a','p','M') && (section_type & 0x0F) != HAP_FMT_RGTC1) &&
  133. (section_type & 0x0F) != HAP_FMT_YCOCGDXT5)) {
  134. av_log(avctx, AV_LOG_ERROR,
  135. "Invalid texture format %#04x.\n", section_type & 0x0F);
  136. return AVERROR_INVALIDDATA;
  137. }
  138. switch (section_type & 0xF0) {
  139. case HAP_COMP_NONE:
  140. case HAP_COMP_SNAPPY:
  141. ret = ff_hap_set_chunk_count(ctx, 1, 1);
  142. if (ret == 0) {
  143. ctx->chunks[0].compressor = section_type & 0xF0;
  144. ctx->chunks[0].compressed_offset = 0;
  145. ctx->chunks[0].compressed_size = ctx->texture_section_size;
  146. }
  147. if (ctx->chunks[0].compressor == HAP_COMP_NONE) {
  148. compressorstr = "none";
  149. } else {
  150. compressorstr = "snappy";
  151. }
  152. break;
  153. case HAP_COMP_COMPLEX:
  154. ret = ff_hap_parse_section_header(gbc, &section_size, &section_type);
  155. if (ret == 0 && section_type != HAP_ST_DECODE_INSTRUCTIONS)
  156. ret = AVERROR_INVALIDDATA;
  157. if (ret == 0)
  158. ret = hap_parse_decode_instructions(ctx, section_size);
  159. compressorstr = "complex";
  160. break;
  161. default:
  162. ret = AVERROR_INVALIDDATA;
  163. break;
  164. }
  165. if (ret != 0)
  166. return ret;
  167. /* Check the frame is valid and read the uncompressed chunk sizes */
  168. ctx->tex_size = 0;
  169. for (i = 0; i < ctx->chunk_count; i++) {
  170. HapChunk *chunk = &ctx->chunks[i];
  171. /* Check the compressed buffer is valid */
  172. if (chunk->compressed_offset + (uint64_t)chunk->compressed_size > bytestream2_get_bytes_left(gbc))
  173. return AVERROR_INVALIDDATA;
  174. /* Chunks are unpacked sequentially, ctx->tex_size is the uncompressed
  175. * size thus far */
  176. chunk->uncompressed_offset = ctx->tex_size;
  177. /* Fill out uncompressed size */
  178. if (chunk->compressor == HAP_COMP_SNAPPY) {
  179. GetByteContext gbc_tmp;
  180. int64_t uncompressed_size;
  181. bytestream2_init(&gbc_tmp, gbc->buffer + chunk->compressed_offset,
  182. chunk->compressed_size);
  183. uncompressed_size = ff_snappy_peek_uncompressed_length(&gbc_tmp);
  184. if (uncompressed_size < 0) {
  185. return uncompressed_size;
  186. }
  187. chunk->uncompressed_size = uncompressed_size;
  188. } else if (chunk->compressor == HAP_COMP_NONE) {
  189. chunk->uncompressed_size = chunk->compressed_size;
  190. } else {
  191. return AVERROR_INVALIDDATA;
  192. }
  193. ctx->tex_size += chunk->uncompressed_size;
  194. }
  195. av_log(avctx, AV_LOG_DEBUG, "%s compressor\n", compressorstr);
  196. return ret;
  197. }
  198. static int decompress_chunks_thread(AVCodecContext *avctx, void *arg,
  199. int chunk_nb, int thread_nb)
  200. {
  201. HapContext *ctx = avctx->priv_data;
  202. HapChunk *chunk = &ctx->chunks[chunk_nb];
  203. GetByteContext gbc;
  204. uint8_t *dst = ctx->tex_buf + chunk->uncompressed_offset;
  205. bytestream2_init(&gbc, ctx->gbc.buffer + chunk->compressed_offset, chunk->compressed_size);
  206. if (chunk->compressor == HAP_COMP_SNAPPY) {
  207. int ret;
  208. int64_t uncompressed_size = ctx->tex_size;
  209. /* Uncompress the frame */
  210. ret = ff_snappy_uncompress(&gbc, dst, &uncompressed_size);
  211. if (ret < 0) {
  212. av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
  213. return ret;
  214. }
  215. } else if (chunk->compressor == HAP_COMP_NONE) {
  216. bytestream2_get_buffer(&gbc, dst, chunk->compressed_size);
  217. }
  218. return 0;
  219. }
  220. static int decompress_texture_thread_internal(AVCodecContext *avctx, void *arg,
  221. int slice, int thread_nb, int texture_num)
  222. {
  223. HapContext *ctx = avctx->priv_data;
  224. AVFrame *frame = arg;
  225. const uint8_t *d = ctx->tex_data;
  226. int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
  227. int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
  228. int x, y;
  229. int start_slice, end_slice;
  230. int base_blocks_per_slice = h_block / ctx->slice_count;
  231. int remainder_blocks = h_block % ctx->slice_count;
  232. /* When the frame height (in blocks) doesn't divide evenly between the
  233. * number of slices, spread the remaining blocks evenly between the first
  234. * operations */
  235. start_slice = slice * base_blocks_per_slice;
  236. /* Add any extra blocks (one per slice) that have been added before this slice */
  237. start_slice += FFMIN(slice, remainder_blocks);
  238. end_slice = start_slice + base_blocks_per_slice;
  239. /* Add an extra block if there are still remainder blocks to be accounted for */
  240. if (slice < remainder_blocks)
  241. end_slice++;
  242. for (y = start_slice; y < end_slice; y++) {
  243. uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
  244. int off = y * w_block;
  245. for (x = 0; x < w_block; x++) {
  246. if (texture_num == 0) {
  247. ctx->tex_fun(p + x * 4 * ctx->uncompress_pix_size, frame->linesize[0],
  248. d + (off + x) * ctx->tex_rat);
  249. } else {
  250. ctx->tex_fun2(p + x * 4 * ctx->uncompress_pix_size, frame->linesize[0],
  251. d + (off + x) * ctx->tex_rat2);
  252. }
  253. }
  254. }
  255. return 0;
  256. }
  257. static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
  258. int slice, int thread_nb)
  259. {
  260. return decompress_texture_thread_internal(avctx, arg, slice, thread_nb, 0);
  261. }
  262. static int decompress_texture2_thread(AVCodecContext *avctx, void *arg,
  263. int slice, int thread_nb)
  264. {
  265. return decompress_texture_thread_internal(avctx, arg, slice, thread_nb, 1);
  266. }
  267. static int hap_decode(AVCodecContext *avctx, void *data,
  268. int *got_frame, AVPacket *avpkt)
  269. {
  270. HapContext *ctx = avctx->priv_data;
  271. ThreadFrame tframe;
  272. int ret, i, t;
  273. int section_size;
  274. enum HapSectionType section_type;
  275. int start_texture_section = 0;
  276. int tex_rat[2] = {0, 0};
  277. bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
  278. tex_rat[0] = ctx->tex_rat;
  279. /* check for multi texture header */
  280. if (ctx->texture_count == 2) {
  281. ret = ff_hap_parse_section_header(&ctx->gbc, &section_size, &section_type);
  282. if (ret != 0)
  283. return ret;
  284. if ((section_type & 0x0F) != 0x0D) {
  285. av_log(avctx, AV_LOG_ERROR, "Invalid section type in 2 textures mode %#04x.\n", section_type);
  286. return AVERROR_INVALIDDATA;
  287. }
  288. start_texture_section = 4;
  289. tex_rat[1] = ctx->tex_rat2;
  290. }
  291. /* Get the output frame ready to receive data */
  292. tframe.f = data;
  293. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  294. if (ret < 0)
  295. return ret;
  296. for (t = 0; t < ctx->texture_count; t++) {
  297. bytestream2_seek(&ctx->gbc, start_texture_section, SEEK_SET);
  298. /* Check for section header */
  299. ret = hap_parse_frame_header(avctx);
  300. if (ret < 0)
  301. return ret;
  302. if (ctx->tex_size != (avctx->coded_width / TEXTURE_BLOCK_W)
  303. *(avctx->coded_height / TEXTURE_BLOCK_H)
  304. *tex_rat[t]) {
  305. av_log(avctx, AV_LOG_ERROR, "uncompressed size mismatches\n");
  306. return AVERROR_INVALIDDATA;
  307. }
  308. start_texture_section += ctx->texture_section_size + 4;
  309. if (avctx->codec->update_thread_context)
  310. ff_thread_finish_setup(avctx);
  311. /* Unpack the DXT texture */
  312. if (hap_can_use_tex_in_place(ctx)) {
  313. int tex_size;
  314. /* Only DXTC texture compression in a contiguous block */
  315. ctx->tex_data = ctx->gbc.buffer;
  316. tex_size = FFMIN(ctx->texture_section_size, bytestream2_get_bytes_left(&ctx->gbc));
  317. if (tex_size < (avctx->coded_width / TEXTURE_BLOCK_W)
  318. *(avctx->coded_height / TEXTURE_BLOCK_H)
  319. *tex_rat[t]) {
  320. av_log(avctx, AV_LOG_ERROR, "Insufficient data\n");
  321. return AVERROR_INVALIDDATA;
  322. }
  323. } else {
  324. /* Perform the second-stage decompression */
  325. ret = av_reallocp(&ctx->tex_buf, ctx->tex_size);
  326. if (ret < 0)
  327. return ret;
  328. avctx->execute2(avctx, decompress_chunks_thread, NULL,
  329. ctx->chunk_results, ctx->chunk_count);
  330. for (i = 0; i < ctx->chunk_count; i++) {
  331. if (ctx->chunk_results[i] < 0)
  332. return ctx->chunk_results[i];
  333. }
  334. ctx->tex_data = ctx->tex_buf;
  335. }
  336. /* Use the decompress function on the texture, one block per thread */
  337. if (t == 0){
  338. avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, ctx->slice_count);
  339. } else{
  340. tframe.f = data;
  341. avctx->execute2(avctx, decompress_texture2_thread, tframe.f, NULL, ctx->slice_count);
  342. }
  343. }
  344. /* Frame is ready to be output */
  345. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  346. tframe.f->key_frame = 1;
  347. *got_frame = 1;
  348. return avpkt->size;
  349. }
  350. static av_cold int hap_init(AVCodecContext *avctx)
  351. {
  352. HapContext *ctx = avctx->priv_data;
  353. const char *texture_name;
  354. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  355. if (ret < 0) {
  356. av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
  357. avctx->width, avctx->height);
  358. return ret;
  359. }
  360. /* Since codec is based on 4x4 blocks, size is aligned to 4 */
  361. avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
  362. avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
  363. ff_texturedsp_init(&ctx->dxtc);
  364. ctx->texture_count = 1;
  365. ctx->uncompress_pix_size = 4;
  366. switch (avctx->codec_tag) {
  367. case MKTAG('H','a','p','1'):
  368. texture_name = "DXT1";
  369. ctx->tex_rat = 8;
  370. ctx->tex_fun = ctx->dxtc.dxt1_block;
  371. avctx->pix_fmt = AV_PIX_FMT_RGB0;
  372. break;
  373. case MKTAG('H','a','p','5'):
  374. texture_name = "DXT5";
  375. ctx->tex_rat = 16;
  376. ctx->tex_fun = ctx->dxtc.dxt5_block;
  377. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  378. break;
  379. case MKTAG('H','a','p','Y'):
  380. texture_name = "DXT5-YCoCg-scaled";
  381. ctx->tex_rat = 16;
  382. ctx->tex_fun = ctx->dxtc.dxt5ys_block;
  383. avctx->pix_fmt = AV_PIX_FMT_RGB0;
  384. break;
  385. case MKTAG('H','a','p','A'):
  386. texture_name = "RGTC1";
  387. ctx->tex_rat = 8;
  388. ctx->tex_fun = ctx->dxtc.rgtc1u_gray_block;
  389. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  390. ctx->uncompress_pix_size = 1;
  391. break;
  392. case MKTAG('H','a','p','M'):
  393. texture_name = "DXT5-YCoCg-scaled / RGTC1";
  394. ctx->tex_rat = 16;
  395. ctx->tex_rat2 = 8;
  396. ctx->tex_fun = ctx->dxtc.dxt5ys_block;
  397. ctx->tex_fun2 = ctx->dxtc.rgtc1u_alpha_block;
  398. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  399. ctx->texture_count = 2;
  400. break;
  401. default:
  402. return AVERROR_DECODER_NOT_FOUND;
  403. }
  404. av_log(avctx, AV_LOG_DEBUG, "%s texture\n", texture_name);
  405. ctx->slice_count = av_clip(avctx->thread_count, 1,
  406. avctx->coded_height / TEXTURE_BLOCK_H);
  407. return 0;
  408. }
  409. static av_cold int hap_close(AVCodecContext *avctx)
  410. {
  411. HapContext *ctx = avctx->priv_data;
  412. ff_hap_free_context(ctx);
  413. return 0;
  414. }
  415. AVCodec ff_hap_decoder = {
  416. .name = "hap",
  417. .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap"),
  418. .type = AVMEDIA_TYPE_VIDEO,
  419. .id = AV_CODEC_ID_HAP,
  420. .init = hap_init,
  421. .decode = hap_decode,
  422. .close = hap_close,
  423. .priv_data_size = sizeof(HapContext),
  424. .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS |
  425. AV_CODEC_CAP_DR1,
  426. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  427. FF_CODEC_CAP_INIT_CLEANUP,
  428. .codec_tags = (const uint32_t []){
  429. MKTAG('H','a','p','1'),
  430. MKTAG('H','a','p','5'),
  431. MKTAG('H','a','p','Y'),
  432. MKTAG('H','a','p','A'),
  433. MKTAG('H','a','p','M'),
  434. FF_CODEC_TAGS_END,
  435. },
  436. };