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.

249 lines
7.5KB

  1. /*
  2. * Vidvox Hap decoder
  3. * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. return AVERROR_INVALIDDATA;
  71. switch (ctx->section_type & 0xF0) {
  72. case HAP_COMP_NONE:
  73. /* Only DXTC texture compression */
  74. ctx->tex_data = gbc->buffer;
  75. ctx->tex_size = length;
  76. compressorstr = "none";
  77. break;
  78. case HAP_COMP_SNAPPY:
  79. snappy_size = ff_snappy_peek_uncompressed_length(gbc);
  80. ret = av_reallocp(&ctx->snappied, snappy_size);
  81. if (ret < 0) {
  82. return ret;
  83. }
  84. /* Uncompress the frame */
  85. ret = ff_snappy_uncompress(gbc, ctx->snappied, &snappy_size);
  86. if (ret < 0) {
  87. av_log(avctx, AV_LOG_ERROR, "Snappy uncompress error\n");
  88. return ret;
  89. }
  90. ctx->tex_data = ctx->snappied;
  91. ctx->tex_size = snappy_size;
  92. compressorstr = "snappy";
  93. break;
  94. case HAP_COMP_COMPLEX:
  95. compressorstr = "complex";
  96. avpriv_request_sample(avctx, "Complex Hap compressor");
  97. return AVERROR_PATCHWELCOME;
  98. break;
  99. default:
  100. av_log(avctx, AV_LOG_ERROR,
  101. "Invalid compressor mode %02X.\n", ctx->section_type);
  102. return AVERROR_INVALIDDATA;
  103. }
  104. av_log(avctx, AV_LOG_DEBUG, "%s compressor\n", compressorstr);
  105. return 0;
  106. }
  107. static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
  108. int block_nb, int thread_nb)
  109. {
  110. HapContext *ctx = avctx->priv_data;
  111. AVFrame *frame = arg;
  112. int x = (TEXTURE_BLOCK_W * block_nb) % avctx->coded_width;
  113. int y = TEXTURE_BLOCK_H * (TEXTURE_BLOCK_W * block_nb / avctx->coded_width);
  114. uint8_t *p = frame->data[0] + x * 4 + y * frame->linesize[0];
  115. const uint8_t *d = ctx->tex_data + block_nb * ctx->tex_rat;
  116. ctx->tex_fun(p, frame->linesize[0], d);
  117. return 0;
  118. }
  119. static int hap_decode(AVCodecContext *avctx, void *data,
  120. int *got_frame, AVPacket *avpkt)
  121. {
  122. HapContext *ctx = avctx->priv_data;
  123. ThreadFrame tframe;
  124. int ret, length;
  125. int blocks = avctx->coded_width * avctx->coded_height / (TEXTURE_BLOCK_W * TEXTURE_BLOCK_H);
  126. bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
  127. /* Check for section header */
  128. length = parse_section_header(avctx);
  129. if (length < 0) {
  130. av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
  131. return length;
  132. }
  133. /* Prepare the texture buffer and decompress function */
  134. ret = setup_texture(avctx, length);
  135. if (ret < 0)
  136. return ret;
  137. /* Get the output frame ready to receive data */
  138. tframe.f = data;
  139. ret = ff_thread_get_buffer(avctx, &tframe, 0);
  140. if (ret < 0)
  141. return ret;
  142. if (avctx->codec->update_thread_context)
  143. ff_thread_finish_setup(avctx);
  144. /* Use the decompress function on the texture, one block per thread */
  145. avctx->execute2(avctx, decompress_texture_thread, tframe.f, NULL, blocks);
  146. /* Frame is ready to be output */
  147. tframe.f->pict_type = AV_PICTURE_TYPE_I;
  148. tframe.f->key_frame = 1;
  149. *got_frame = 1;
  150. return avpkt->size;
  151. }
  152. static av_cold int hap_init(AVCodecContext *avctx)
  153. {
  154. HapContext *ctx = avctx->priv_data;
  155. const char *texture_name;
  156. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  157. if (ret < 0) {
  158. av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
  159. avctx->width, avctx->height);
  160. return ret;
  161. }
  162. /* Since codec is based on 4x4 blocks, size is aligned to 4 */
  163. avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
  164. avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
  165. /* Technically only one mode has alpha, but 32 bits are easier to handle */
  166. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  167. ff_texturedsp_init(&ctx->dxtc);
  168. switch (avctx->codec_tag) {
  169. case MKTAG('H','a','p','1'):
  170. texture_name = "DXT1";
  171. ctx->tex_rat = 8;
  172. ctx->tex_fun = ctx->dxtc.dxt1_block;
  173. break;
  174. case MKTAG('H','a','p','5'):
  175. texture_name = "DXT5";
  176. ctx->tex_rat = 16;
  177. ctx->tex_fun = ctx->dxtc.dxt5_block;
  178. break;
  179. case MKTAG('H','a','p','Y'):
  180. texture_name = "DXT5-YCoCg-scaled";
  181. ctx->tex_rat = 16;
  182. ctx->tex_fun = ctx->dxtc.dxt5ys_block;
  183. break;
  184. default:
  185. return AVERROR_DECODER_NOT_FOUND;
  186. }
  187. av_log(avctx, AV_LOG_DEBUG, "%s texture\n", texture_name);
  188. return 0;
  189. }
  190. static av_cold int hap_close(AVCodecContext *avctx)
  191. {
  192. HapContext *ctx = avctx->priv_data;
  193. av_freep(&ctx->snappied);
  194. return 0;
  195. }
  196. AVCodec ff_hap_decoder = {
  197. .name = "hap",
  198. .long_name = NULL_IF_CONFIG_SMALL("Vidvox Hap decoder"),
  199. .type = AVMEDIA_TYPE_VIDEO,
  200. .id = AV_CODEC_ID_HAP,
  201. .init = hap_init,
  202. .decode = hap_decode,
  203. .close = hap_close,
  204. .priv_data_size = sizeof(HapContext),
  205. .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS |
  206. CODEC_CAP_DR1,
  207. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  208. FF_CODEC_CAP_INIT_CLEANUP,
  209. };