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.

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