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.

276 lines
8.5KB

  1. /*
  2. * Vidvox Hap decoder
  3. * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Hap decoder
  24. *
  25. * Fourcc: Hap1, Hap5, HapY
  26. *
  27. * https://github.com/Vidvox/hap/blob/master/documentation/HapVideoDRAFT.md
  28. */
  29. #include <stdint.h>
  30. #include "libavutil/imgutils.h"
  31. #include "avcodec.h"
  32. #include "bytestream.h"
  33. #include "hap.h"
  34. #include "internal.h"
  35. #include "snappy.h"
  36. #include "texturedsp.h"
  37. #include "thread.h"
  38. /* The first three bytes are the size of the section past the header, or zero
  39. * if the length is stored in the next long word. The fourth byte in the first
  40. * long word indicates the type of the current section. */
  41. static int parse_section_header(AVCodecContext *avctx)
  42. {
  43. HapContext *ctx = avctx->priv_data;
  44. GetByteContext *gbc = &ctx->gbc;
  45. int length;
  46. if (bytestream2_get_bytes_left(gbc) < 4)
  47. return AVERROR_INVALIDDATA;
  48. length = bytestream2_get_le24(gbc);
  49. ctx->section_type = bytestream2_get_byte(gbc);
  50. if (length == 0) {
  51. if (bytestream2_get_bytes_left(gbc) < 4)
  52. return AVERROR_INVALIDDATA;
  53. length = bytestream2_get_le32(gbc);
  54. }
  55. if (length > bytestream2_get_bytes_left(gbc) || length == 0)
  56. return AVERROR_INVALIDDATA;
  57. return length;
  58. }
  59. /* Prepare the texture to be decompressed */
  60. static int setup_texture(AVCodecContext *avctx, size_t length)
  61. {
  62. HapContext *ctx = avctx->priv_data;
  63. GetByteContext *gbc = &ctx->gbc;
  64. int64_t snappy_size;
  65. const char *compressorstr;
  66. int ret;
  67. if ((avctx->codec_tag == MKTAG('H','a','p','1') && (ctx->section_type & 0x0F) != HAP_FMT_RGBDXT1) ||
  68. (avctx->codec_tag == MKTAG('H','a','p','5') && (ctx->section_type & 0x0F) != HAP_FMT_RGBADXT5) ||
  69. (avctx->codec_tag == MKTAG('H','a','p','Y') && (ctx->section_type & 0x0F) != HAP_FMT_YCOCGDXT5)) {
  70. av_log(avctx, AV_LOG_ERROR,
  71. "Invalid texture format %#04x.\n", ctx->section_type & 0x0F);
  72. return AVERROR_INVALIDDATA;
  73. }
  74. switch (ctx->section_type & 0xF0) {
  75. case HAP_COMP_NONE:
  76. /* Only DXTC texture compression */
  77. ctx->tex_data = gbc->buffer;
  78. ctx->tex_size = length;
  79. compressorstr = "none";
  80. break;
  81. case HAP_COMP_SNAPPY:
  82. snappy_size = ff_snappy_peek_uncompressed_length(gbc);
  83. ret = av_reallocp(&ctx->snappied, snappy_size);
  84. if (ret < 0) {
  85. return ret;
  86. }
  87. /* Uncompress the frame */
  88. ret = ff_snappy_uncompress(gbc, ctx->snappied, &snappy_size);
  89. if (ret < 0) {
  90. av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
  91. return ret;
  92. }
  93. ctx->tex_data = ctx->snappied;
  94. ctx->tex_size = snappy_size;
  95. compressorstr = "snappy";
  96. break;
  97. case HAP_COMP_COMPLEX:
  98. compressorstr = "complex";
  99. avpriv_request_sample(avctx, "Complex Hap compressor");
  100. return AVERROR_PATCHWELCOME;
  101. break;
  102. default:
  103. av_log(avctx, AV_LOG_ERROR,
  104. "Invalid compressor mode %02X.\n", ctx->section_type);
  105. return AVERROR_INVALIDDATA;
  106. }
  107. av_log(avctx, AV_LOG_DEBUG, "%s compressor\n", compressorstr);
  108. return 0;
  109. }
  110. static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
  111. int slice, int thread_nb)
  112. {
  113. HapContext *ctx = avctx->priv_data;
  114. AVFrame *frame = arg;
  115. const uint8_t *d = ctx->tex_data;
  116. int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
  117. int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
  118. int x, y;
  119. int start_slice, end_slice;
  120. int base_blocks_per_slice = h_block / ctx->slice_count;
  121. int remainder_blocks = h_block % ctx->slice_count;
  122. /* When the frame height (in blocks) doesn't divide evenly between the
  123. * number of slices, spread the remaining blocks evenly between the first
  124. * operations */
  125. start_slice = slice * base_blocks_per_slice;
  126. /* Add any extra blocks (one per slice) that have been added before this slice */
  127. start_slice += FFMIN(slice, remainder_blocks);
  128. end_slice = start_slice + base_blocks_per_slice;
  129. /* Add an extra block if there are still remainder blocks to be accounted for */
  130. if (slice < remainder_blocks)
  131. end_slice++;
  132. for (y = start_slice; y < end_slice; y++) {
  133. uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
  134. int off = y * w_block;
  135. for (x = 0; x < w_block; x++) {
  136. ctx->tex_fun(p + x * 16, frame->linesize[0],
  137. d + (off + x) * ctx->tex_rat);
  138. }
  139. }
  140. return 0;
  141. }
  142. static int hap_decode(AVCodecContext *avctx, void *data,
  143. int *got_frame, AVPacket *avpkt)
  144. {
  145. HapContext *ctx = avctx->priv_data;
  146. ThreadFrame tframe;
  147. int ret, length;
  148. bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
  149. /* Check for section header */
  150. length = parse_section_header(avctx);
  151. if (length < 0) {
  152. av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
  153. return length;
  154. }
  155. /* Prepare the texture buffer and decompress function */
  156. ret = setup_texture(avctx, length);
  157. if (ret < 0)
  158. return ret;
  159. /* Get the output frame ready to receive data */
  160. tframe.f = data;
  161. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  162. if (ret < 0)
  163. return ret;
  164. ff_thread_finish_setup(avctx);
  165. /* Use the decompress function on the texture, one block per thread */
  166. avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, ctx->slice_count);
  167. /* Frame is ready to be output */
  168. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  169. tframe.f->key_frame = 1;
  170. *got_frame = 1;
  171. return avpkt->size;
  172. }
  173. static av_cold int hap_init(AVCodecContext *avctx)
  174. {
  175. HapContext *ctx = avctx->priv_data;
  176. const char *texture_name;
  177. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  178. if (ret < 0) {
  179. av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
  180. avctx->width, avctx->height);
  181. return ret;
  182. }
  183. /* Since codec is based on 4x4 blocks, size is aligned to 4 */
  184. avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
  185. avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
  186. /* Technically only one mode has alpha, but 32 bits are easier to handle */
  187. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  188. ff_texturedsp_init(&ctx->dxtc);
  189. switch (avctx->codec_tag) {
  190. case MKTAG('H','a','p','1'):
  191. texture_name = "DXT1";
  192. ctx->tex_rat = 8;
  193. ctx->tex_fun = ctx->dxtc.dxt1_block;
  194. break;
  195. case MKTAG('H','a','p','5'):
  196. texture_name = "DXT5";
  197. ctx->tex_rat = 16;
  198. ctx->tex_fun = ctx->dxtc.dxt5_block;
  199. break;
  200. case MKTAG('H','a','p','Y'):
  201. texture_name = "DXT5-YCoCg-scaled";
  202. ctx->tex_rat = 16;
  203. ctx->tex_fun = ctx->dxtc.dxt5ys_block;
  204. break;
  205. default:
  206. return AVERROR_DECODER_NOT_FOUND;
  207. }
  208. av_log(avctx, AV_LOG_DEBUG, "%s texture\n", texture_name);
  209. ctx->slice_count = av_clip(avctx->thread_count, 1,
  210. avctx->coded_height / TEXTURE_BLOCK_H);
  211. return 0;
  212. }
  213. static av_cold int hap_close(AVCodecContext *avctx)
  214. {
  215. HapContext *ctx = avctx->priv_data;
  216. av_freep(&ctx->snappied);
  217. return 0;
  218. }
  219. AVCodec ff_hap_decoder = {
  220. .name = "hap",
  221. .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
  222. .type = AVMEDIA_TYPE_VIDEO,
  223. .id = AV_CODEC_ID_HAP,
  224. .init = hap_init,
  225. .decode = hap_decode,
  226. .close = hap_close,
  227. .priv_data_size = sizeof(HapContext),
  228. .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS |
  229. AV_CODEC_CAP_DR1,
  230. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  231. FF_CODEC_CAP_INIT_CLEANUP,
  232. };