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.

239 lines
7.0KB

  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. /* Uncompress the frame */
  98. ret = ff_snappy_uncompress(gbc, &ctx->snappied, &snappy_size);
  99. if (ret < 0) {
  100. av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
  101. return ret;
  102. }
  103. ctx->tex_data = ctx->snappied;
  104. ctx->tex_size = snappy_size;
  105. compressorstr = "snappy";
  106. break;
  107. case HAP_COMP_COMPLEX:
  108. compressorstr = "complex";
  109. avpriv_request_sample(avctx, "Complex Hap compressor");
  110. return AVERROR_PATCHWELCOME;
  111. break;
  112. default:
  113. av_log(avctx, AV_LOG_ERROR,
  114. "Invalid compressor mode %02X.\n", ctx->section_type);
  115. return AVERROR_INVALIDDATA;
  116. }
  117. av_log(avctx, AV_LOG_DEBUG, "%s texture with %s compressor\n",
  118. texture_name, compressorstr);
  119. return 0;
  120. }
  121. static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
  122. int block_nb, int thread_nb)
  123. {
  124. HapContext *ctx = avctx->priv_data;
  125. AVFrame *frame = arg;
  126. int x = (TEXTURE_BLOCK_W * block_nb) % avctx->coded_width;
  127. int y = TEXTURE_BLOCK_H * (TEXTURE_BLOCK_W * block_nb / avctx->coded_width);
  128. uint8_t *p = frame->data[0] + x * 4 + y * frame->linesize[0];
  129. const uint8_t *d = ctx->tex_data + block_nb * ctx->tex_rat;
  130. ctx->tex_fun(p, frame->linesize[0], d);
  131. return 0;
  132. }
  133. static int hap_decode(AVCodecContext *avctx, void *data,
  134. int *got_frame, AVPacket *avpkt)
  135. {
  136. HapContext *ctx = avctx->priv_data;
  137. ThreadFrame tframe;
  138. int ret, length;
  139. int blocks = avctx->coded_width * avctx->coded_height / (TEXTURE_BLOCK_W * TEXTURE_BLOCK_H);
  140. bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
  141. /* Check for section header */
  142. length = parse_section_header(avctx);
  143. if (length < 0) {
  144. av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
  145. return length;
  146. }
  147. /* Prepare the texture buffer and decompress function */
  148. ret = setup_texture(avctx, length);
  149. if (ret < 0)
  150. return ret;
  151. /* Get the output frame ready to receive data */
  152. tframe.f = data;
  153. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  154. if (ret < 0)
  155. return ret;
  156. ff_thread_finish_setup(avctx);
  157. /* Use the decompress function on the texture, one block per thread */
  158. avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, blocks);
  159. /* Frame is ready to be output */
  160. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  161. tframe.f->key_frame = 1;
  162. *got_frame = 1;
  163. return avpkt->size;
  164. }
  165. static av_cold int hap_init(AVCodecContext *avctx)
  166. {
  167. HapContext *ctx = avctx->priv_data;
  168. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  169. if (ret < 0) {
  170. av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
  171. avctx->width, avctx->height);
  172. return ret;
  173. }
  174. /* Since codec is based on 4x4 blocks, size is aligned to 4 */
  175. avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
  176. avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
  177. /* Technically only one mode has alpha, but 32 bits are easier to handle */
  178. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  179. ff_texturedsp_init(&ctx->dxtc);
  180. return 0;
  181. }
  182. static av_cold int hap_close(AVCodecContext *avctx)
  183. {
  184. HapContext *ctx = avctx->priv_data;
  185. av_freep(&ctx->snappied);
  186. return 0;
  187. }
  188. AVCodec ff_hap_decoder = {
  189. .name = "hap",
  190. .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
  191. .type = AVMEDIA_TYPE_VIDEO,
  192. .id = AV_CODEC_ID_HAP,
  193. .init = hap_init,
  194. .decode = hap_decode,
  195. .close = hap_close,
  196. .priv_data_size = sizeof(HapContext),
  197. .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS |
  198. CODEC_CAP_DR1,
  199. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  200. FF_CODEC_CAP_INIT_CLEANUP,
  201. };