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.

422 lines
13KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #include "memory.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, "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 x, y;
  241. int start_slice, end_slice;
  242. start_slice = slice * ctx->slice_size;
  243. end_slice = FFMIN(start_slice + ctx->slice_size, avctx->coded_height);
  244. start_slice /= TEXTURE_BLOCK_H;
  245. end_slice /= TEXTURE_BLOCK_H;
  246. for (y = start_slice; y < end_slice; y++) {
  247. uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
  248. int off = y * w_block;
  249. for (x = 0; x < w_block; x++) {
  250. ctx->tex_fun(p + x * 16, frame->linesize[0],
  251. d + (off + x) * ctx->tex_rat);
  252. }
  253. }
  254. return 0;
  255. }
  256. static int hap_decode(AVCodecContext *avctx, void *data,
  257. int *got_frame, AVPacket *avpkt)
  258. {
  259. HapContext *ctx = avctx->priv_data;
  260. ThreadFrame tframe;
  261. int ret, i;
  262. int slices = FFMIN(avctx->thread_count,
  263. avctx->coded_height / TEXTURE_BLOCK_H);
  264. ctx->slice_size = avctx->coded_height / slices;
  265. bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
  266. /* Check for section header */
  267. ret = hap_parse_frame_header(avctx);
  268. if (ret < 0)
  269. return ret;
  270. /* Get the output frame ready to receive data */
  271. tframe.f = data;
  272. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  273. if (ret < 0)
  274. return ret;
  275. if (avctx->codec->update_thread_context)
  276. ff_thread_finish_setup(avctx);
  277. /* Unpack the DXT texture */
  278. if (hap_can_use_tex_in_place(ctx)) {
  279. /* Only DXTC texture compression in a contiguous block */
  280. ctx->tex_data = ctx->gbc.buffer;
  281. } else {
  282. /* Perform the second-stage decompression */
  283. ret = av_reallocp(&ctx->tex_buf, ctx->tex_size);
  284. if (ret < 0)
  285. return ret;
  286. avctx->execute2(avctx, decompress_chunks_thread, NULL,
  287. ctx->chunk_results, ctx->chunk_count);
  288. for (i = 0; i < ctx->chunk_count; i++) {
  289. if (ctx->chunk_results[i] < 0)
  290. return ctx->chunk_results[i];
  291. }
  292. ctx->tex_data = ctx->tex_buf;
  293. }
  294. /* Use the decompress function on the texture, one block per thread */
  295. avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, slices);
  296. /* Frame is ready to be output */
  297. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  298. tframe.f->key_frame = 1;
  299. *got_frame = 1;
  300. return avpkt->size;
  301. }
  302. static av_cold int hap_init(AVCodecContext *avctx)
  303. {
  304. HapContext *ctx = avctx->priv_data;
  305. const char *texture_name;
  306. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  307. if (ret < 0) {
  308. av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
  309. avctx->width, avctx->height);
  310. return ret;
  311. }
  312. /* Since codec is based on 4x4 blocks, size is aligned to 4 */
  313. avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
  314. avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
  315. /* Technically only one mode has alpha, but 32 bits are easier to handle */
  316. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  317. ff_texturedsp_init(&ctx->dxtc);
  318. switch (avctx->codec_tag) {
  319. case MKTAG('H','a','p','1'):
  320. texture_name = "DXT1";
  321. ctx->tex_rat = 8;
  322. ctx->tex_fun = ctx->dxtc.dxt1_block;
  323. break;
  324. case MKTAG('H','a','p','5'):
  325. texture_name = "DXT5";
  326. ctx->tex_rat = 16;
  327. ctx->tex_fun = ctx->dxtc.dxt5_block;
  328. break;
  329. case MKTAG('H','a','p','Y'):
  330. texture_name = "DXT5-YCoCg-scaled";
  331. ctx->tex_rat = 16;
  332. ctx->tex_fun = ctx->dxtc.dxt5ys_block;
  333. break;
  334. default:
  335. return AVERROR_DECODER_NOT_FOUND;
  336. }
  337. av_log(avctx, AV_LOG_DEBUG, "%s texture\n", texture_name);
  338. return 0;
  339. }
  340. static av_cold int hap_close(AVCodecContext *avctx)
  341. {
  342. HapContext *ctx = avctx->priv_data;
  343. ff_hap_free_context(ctx);
  344. return 0;
  345. }
  346. AVCodec ff_hap_decoder = {
  347. .name = "hap",
  348. .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
  349. .type = AVMEDIA_TYPE_VIDEO,
  350. .id = AV_CODEC_ID_HAP,
  351. .init = hap_init,
  352. .decode = hap_decode,
  353. .close = hap_close,
  354. .priv_data_size = sizeof(HapContext),
  355. .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS |
  356. CODEC_CAP_DR1,
  357. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  358. FF_CODEC_CAP_INIT_CLEANUP,
  359. };