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.

269 lines
8.1KB

  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 *texture_name;
  66. const char *compressorstr;
  67. int ret;
  68. switch (ctx->section_type & 0x0F) {
  69. case HAP_FMT_RGBDXT1:
  70. ctx->tex_rat = 8;
  71. ctx->tex_fun = ctx->dxtc.dxt1_block;
  72. texture_name = "DXT1";
  73. break;
  74. case HAP_FMT_RGBADXT5:
  75. ctx->tex_rat = 16;
  76. ctx->tex_fun = ctx->dxtc.dxt5_block;
  77. texture_name = "DXT5";
  78. break;
  79. case HAP_FMT_YCOCGDXT5:
  80. ctx->tex_rat = 16;
  81. ctx->tex_fun = ctx->dxtc.dxt5ys_block;
  82. texture_name = "DXT5-YCoCg-scaled";
  83. break;
  84. default:
  85. av_log(avctx, AV_LOG_ERROR,
  86. "Invalid format mode %02X.\n", ctx->section_type);
  87. return AVERROR_INVALIDDATA;
  88. }
  89. switch (ctx->section_type & 0xF0) {
  90. case HAP_COMP_NONE:
  91. /* Only DXTC texture compression */
  92. ctx->tex_data = gbc->buffer;
  93. ctx->tex_size = length;
  94. compressorstr = "none";
  95. break;
  96. case HAP_COMP_SNAPPY:
  97. snappy_size = ff_snappy_peek_uncompressed_length(gbc);
  98. ret = av_reallocp(&ctx->snappied, snappy_size);
  99. if (ret < 0) {
  100. return ret;
  101. }
  102. /* Uncompress the frame */
  103. ret = ff_snappy_uncompress(gbc, ctx->snappied, &snappy_size);
  104. if (ret < 0) {
  105. av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
  106. return ret;
  107. }
  108. ctx->tex_data = ctx->snappied;
  109. ctx->tex_size = snappy_size;
  110. compressorstr = "snappy";
  111. break;
  112. case HAP_COMP_COMPLEX:
  113. compressorstr = "complex";
  114. avpriv_request_sample(avctx, "Complex Hap compressor");
  115. return AVERROR_PATCHWELCOME;
  116. break;
  117. default:
  118. av_log(avctx, AV_LOG_ERROR,
  119. "Invalid compressor mode %02X.\n", ctx->section_type);
  120. return AVERROR_INVALIDDATA;
  121. }
  122. av_log(avctx, AV_LOG_DEBUG, "%s texture with %s compressor\n",
  123. texture_name, compressorstr);
  124. return 0;
  125. }
  126. static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
  127. int slice, int thread_nb)
  128. {
  129. HapContext *ctx = avctx->priv_data;
  130. AVFrame *frame = arg;
  131. const uint8_t *d = ctx->tex_data;
  132. int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
  133. int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
  134. int x, y;
  135. int start_slice, end_slice;
  136. int base_blocks_per_slice = h_block / ctx->slice_count;
  137. int remainder_blocks = h_block % ctx->slice_count;
  138. /* When the frame height (in blocks) doesn't divide evenly between the
  139. * number of slices, spread the remaining blocks evenly between the first
  140. * operations */
  141. start_slice = slice * base_blocks_per_slice;
  142. /* Add any extra blocks (one per slice) that have been added before this slice */
  143. start_slice += FFMIN(slice, remainder_blocks);
  144. end_slice = start_slice + base_blocks_per_slice;
  145. /* Add an extra block if there are still remainder blocks to be accounted for */
  146. if (slice < remainder_blocks)
  147. end_slice++;
  148. for (y = start_slice; y < end_slice; y++) {
  149. uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
  150. int off = y * w_block;
  151. for (x = 0; x < w_block; x++) {
  152. ctx->tex_fun(p + x * 16, frame->linesize[0],
  153. d + (off + x) * ctx->tex_rat);
  154. }
  155. }
  156. return 0;
  157. }
  158. static int hap_decode(AVCodecContext *avctx, void *data,
  159. int *got_frame, AVPacket *avpkt)
  160. {
  161. HapContext *ctx = avctx->priv_data;
  162. ThreadFrame tframe;
  163. int ret, length;
  164. bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
  165. /* Check for section header */
  166. length = parse_section_header(avctx);
  167. if (length < 0) {
  168. av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
  169. return length;
  170. }
  171. /* Prepare the texture buffer and decompress function */
  172. ret = setup_texture(avctx, length);
  173. if (ret < 0)
  174. return ret;
  175. /* Get the output frame ready to receive data */
  176. tframe.f = data;
  177. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  178. if (ret < 0)
  179. return ret;
  180. ff_thread_finish_setup(avctx);
  181. /* Use the decompress function on the texture, one block per thread */
  182. avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, ctx->slice_count);
  183. /* Frame is ready to be output */
  184. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  185. tframe.f->key_frame = 1;
  186. *got_frame = 1;
  187. return avpkt->size;
  188. }
  189. static av_cold int hap_init(AVCodecContext *avctx)
  190. {
  191. HapContext *ctx = avctx->priv_data;
  192. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  193. if (ret < 0) {
  194. av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
  195. avctx->width, avctx->height);
  196. return ret;
  197. }
  198. /* Since codec is based on 4x4 blocks, size is aligned to 4 */
  199. avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
  200. avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
  201. /* Technically only one mode has alpha, but 32 bits are easier to handle */
  202. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  203. ff_texturedsp_init(&ctx->dxtc);
  204. ctx->slice_count = av_clip(avctx->thread_count, 1,
  205. avctx->coded_height / TEXTURE_BLOCK_H);
  206. return 0;
  207. }
  208. static av_cold int hap_close(AVCodecContext *avctx)
  209. {
  210. HapContext *ctx = avctx->priv_data;
  211. av_freep(&ctx->snappied);
  212. return 0;
  213. }
  214. AVCodec ff_hap_decoder = {
  215. .name = "hap",
  216. .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
  217. .type = AVMEDIA_TYPE_VIDEO,
  218. .id = AV_CODEC_ID_HAP,
  219. .init = hap_init,
  220. .decode = hap_decode,
  221. .close = hap_close,
  222. .priv_data_size = sizeof(HapContext),
  223. .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS |
  224. AV_CODEC_CAP_DR1,
  225. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  226. FF_CODEC_CAP_INIT_CLEANUP,
  227. };